1 //
2 // Copyright (C) 2014-2016 LunarG, Inc.
3 // Copyright (C) 2015-2018 Google, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
19 //
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36
37 //
38 // Visit the nodes in the glslang intermediate tree representation to
39 // translate them to SPIR-V.
40 //
41
42 #include "spirv.hpp"
43 #include "GlslangToSpv.h"
44 #include "SpvBuilder.h"
45 namespace spv {
46 #include "GLSL.std.450.h"
47 #include "GLSL.ext.KHR.h"
48 #include "GLSL.ext.EXT.h"
49 #ifdef AMD_EXTENSIONS
50 #include "GLSL.ext.AMD.h"
51 #endif
52 #ifdef NV_EXTENSIONS
53 #include "GLSL.ext.NV.h"
54 #endif
55 }
56
57 // Glslang includes
58 #include "../glslang/MachineIndependent/localintermediate.h"
59 #include "../glslang/MachineIndependent/SymbolTable.h"
60 #include "../glslang/Include/Common.h"
61 #include "../glslang/Include/revision.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:
SpecConstantOpModeGuard(spv::Builder * builder)76 SpecConstantOpModeGuard(spv::Builder* builder)
77 : builder_(builder) {
78 previous_flag_ = builder->isInSpecConstCodeGenMode();
79 }
~SpecConstantOpModeGuard()80 ~SpecConstantOpModeGuard() {
81 previous_flag_ ? builder_->setToSpecConstCodeGenMode()
82 : builder_->setToNormalCodeGenMode();
83 }
turnOnSpecConstantOpMode()84 void turnOnSpecConstantOpMode() {
85 builder_->setToSpecConstCodeGenMode();
86 }
87
88 private:
89 spv::Builder* builder_;
90 bool previous_flag_;
91 };
92
93 struct OpDecorations {
94 spv::Decoration precision;
95 spv::Decoration noContraction;
96 spv::Decoration nonUniform;
97 };
98
99 } // namespace
100
101 //
102 // The main holder of information for translating glslang to SPIR-V.
103 //
104 // Derives from the AST walking base class.
105 //
106 class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
107 public:
108 TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger,
109 glslang::SpvOptions& options);
~TGlslangToSpvTraverser()110 virtual ~TGlslangToSpvTraverser() { }
111
112 bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*);
113 bool visitBinary(glslang::TVisit, glslang::TIntermBinary*);
114 void visitConstantUnion(glslang::TIntermConstantUnion*);
115 bool visitSelection(glslang::TVisit, glslang::TIntermSelection*);
116 bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*);
117 void visitSymbol(glslang::TIntermSymbol* symbol);
118 bool visitUnary(glslang::TVisit, glslang::TIntermUnary*);
119 bool visitLoop(glslang::TVisit, glslang::TIntermLoop*);
120 bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*);
121
122 void finishSpv();
123 void dumpSpv(std::vector<unsigned int>& out);
124
125 protected:
126 TGlslangToSpvTraverser(TGlslangToSpvTraverser&);
127 TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&);
128
129 spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier);
130 spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier);
131 spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier);
132 spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type);
133 spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
134 spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
135 spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
136 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
137 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
138 spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const;
139 spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const;
140 spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, unsigned int& dependencyLength) const;
141 spv::StorageClass TranslateStorageClass(const glslang::TType&);
142 void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType);
143 spv::Id createSpvVariable(const glslang::TIntermSymbol*);
144 spv::Id getSampledType(const glslang::TSampler&);
145 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
146 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
147 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
148 spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false);
149 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&,
150 bool lastBufferBlockMember, bool forwardReferenceOnly = false);
151 bool filterMember(const glslang::TType& member);
152 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
153 glslang::TLayoutPacking, const glslang::TQualifier&);
154 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
155 const glslang::TQualifier&, spv::Id);
156 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
157 spv::Id accessChainLoad(const glslang::TType& type);
158 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
159 void multiTypeStore(const glslang::TType&, spv::Id rValue);
160 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
161 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
162 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
163 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset,
164 int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
165 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
166
167 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
168 bool writableParam(glslang::TStorageQualifier) const;
169 bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam);
170 void makeFunctions(const glslang::TIntermSequence&);
171 void makeGlobalInitializers(const glslang::TIntermSequence&);
172 void visitFunctions(const glslang::TIntermSequence&);
173 void handleFunctionEntry(const glslang::TIntermAggregate* node);
174 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments);
175 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
176 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
177 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
178
179 spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right,
180 glslang::TBasicType typeProxy, bool reduceComparison = true);
181 spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right);
182 spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand,
183 glslang::TBasicType typeProxy);
184 spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand,
185 glslang::TBasicType typeProxy);
186 spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand,
187 glslang::TBasicType typeProxy);
188 spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize);
189 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
190 spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
191 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
192 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands);
193 spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
194 spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
195 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
196 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
197 #ifdef NV_EXTENSIONS
198 void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier);
199 #endif
200 spv::Id createSpvConstant(const glslang::TIntermTyped&);
201 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant);
202 bool isTrivialLeaf(const glslang::TIntermTyped* node);
203 bool isTrivial(const glslang::TIntermTyped* node);
204 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
205 #ifdef AMD_EXTENSIONS
206 spv::Id getExtBuiltins(const char* name);
207 #endif
addPre13Extension(const char * ext)208 void addPre13Extension(const char* ext)
209 {
210 if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3)
211 builder.addExtension(ext);
212 }
213
getBufferReferenceAlignment(const glslang::TType & type) const214 unsigned int getBufferReferenceAlignment(const glslang::TType &type) const {
215 if (type.getBasicType() == glslang::EbtReference) {
216 return type.getReferentType()->getQualifier().hasBufferReferenceAlign() ?
217 (1u << type.getReferentType()->getQualifier().layoutBufferReferenceAlign) : 16u;
218 } else {
219 return 0;
220 }
221 }
222
223 glslang::SpvOptions& options;
224 spv::Function* shaderEntry;
225 spv::Function* currentFunction;
226 spv::Instruction* entryPoint;
227 int sequenceDepth;
228
229 spv::SpvBuildLogger* logger;
230
231 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
232 spv::Builder builder;
233 bool inEntryPoint;
234 bool entryPointTerminated;
235 bool linkageOnly; // true when visiting the set of objects in the AST present only for establishing interface, whether or not they were statically used
236 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
237 const glslang::TIntermediate* glslangIntermediate;
238 spv::Id stdBuiltins;
239 std::unordered_map<const char*, spv::Id> extBuiltinMap;
240
241 std::unordered_map<int, spv::Id> symbolValues;
242 std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer
243 std::unordered_map<std::string, spv::Function*> functionMap;
244 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
245 // for mapping glslang block indices to spv indices (e.g., due to hidden members):
246 std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper;
247 std::stack<bool> breakForLoop; // false means break for switch
248 std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator;
249 // Map pointee types for EbtReference to their forward pointers
250 std::map<const glslang::TType *, spv::Id> forwardPointers;
251 };
252
253 //
254 // Helper functions for translating glslang representations to SPIR-V enumerants.
255 //
256
257 // Translate glslang profile to SPIR-V source language.
TranslateSourceLanguage(glslang::EShSource source,EProfile profile)258 spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
259 {
260 switch (source) {
261 case glslang::EShSourceGlsl:
262 switch (profile) {
263 case ENoProfile:
264 case ECoreProfile:
265 case ECompatibilityProfile:
266 return spv::SourceLanguageGLSL;
267 case EEsProfile:
268 return spv::SourceLanguageESSL;
269 default:
270 return spv::SourceLanguageUnknown;
271 }
272 case glslang::EShSourceHlsl:
273 return spv::SourceLanguageHLSL;
274 default:
275 return spv::SourceLanguageUnknown;
276 }
277 }
278
279 // Translate glslang language (stage) to SPIR-V execution model.
TranslateExecutionModel(EShLanguage stage)280 spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
281 {
282 switch (stage) {
283 case EShLangVertex: return spv::ExecutionModelVertex;
284 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
285 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
286 case EShLangGeometry: return spv::ExecutionModelGeometry;
287 case EShLangFragment: return spv::ExecutionModelFragment;
288 case EShLangCompute: return spv::ExecutionModelGLCompute;
289 #ifdef NV_EXTENSIONS
290 case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV;
291 case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV;
292 case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV;
293 case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV;
294 case EShLangMissNV: return spv::ExecutionModelMissNV;
295 case EShLangCallableNV: return spv::ExecutionModelCallableNV;
296 case EShLangTaskNV: return spv::ExecutionModelTaskNV;
297 case EShLangMeshNV: return spv::ExecutionModelMeshNV;
298 #endif
299 default:
300 assert(0);
301 return spv::ExecutionModelFragment;
302 }
303 }
304
305 // Translate glslang sampler type to SPIR-V dimensionality.
TranslateDimensionality(const glslang::TSampler & sampler)306 spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
307 {
308 switch (sampler.dim) {
309 case glslang::Esd1D: return spv::Dim1D;
310 case glslang::Esd2D: return spv::Dim2D;
311 case glslang::Esd3D: return spv::Dim3D;
312 case glslang::EsdCube: return spv::DimCube;
313 case glslang::EsdRect: return spv::DimRect;
314 case glslang::EsdBuffer: return spv::DimBuffer;
315 case glslang::EsdSubpass: return spv::DimSubpassData;
316 default:
317 assert(0);
318 return spv::Dim2D;
319 }
320 }
321
322 // Translate glslang precision to SPIR-V precision decorations.
TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)323 spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
324 {
325 switch (glslangPrecision) {
326 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
327 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
328 default:
329 return spv::NoPrecision;
330 }
331 }
332
333 // Translate glslang type to SPIR-V precision decorations.
TranslatePrecisionDecoration(const glslang::TType & type)334 spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
335 {
336 return TranslatePrecisionDecoration(type.getQualifier().precision);
337 }
338
339 // Translate glslang type to SPIR-V block decorations.
TranslateBlockDecoration(const glslang::TType & type,bool useStorageBuffer)340 spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer)
341 {
342 if (type.getBasicType() == glslang::EbtBlock) {
343 switch (type.getQualifier().storage) {
344 case glslang::EvqUniform: return spv::DecorationBlock;
345 case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock;
346 case glslang::EvqVaryingIn: return spv::DecorationBlock;
347 case glslang::EvqVaryingOut: return spv::DecorationBlock;
348 #ifdef NV_EXTENSIONS
349 case glslang::EvqPayloadNV: return spv::DecorationBlock;
350 case glslang::EvqPayloadInNV: return spv::DecorationBlock;
351 case glslang::EvqHitAttrNV: return spv::DecorationBlock;
352 case glslang::EvqCallableDataNV: return spv::DecorationBlock;
353 case glslang::EvqCallableDataInNV: return spv::DecorationBlock;
354 #endif
355 default:
356 assert(0);
357 break;
358 }
359 }
360
361 return spv::DecorationMax;
362 }
363
364 // Translate glslang type to SPIR-V memory decorations.
TranslateMemoryDecoration(const glslang::TQualifier & qualifier,std::vector<spv::Decoration> & memory,bool useVulkanMemoryModel)365 void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, bool useVulkanMemoryModel)
366 {
367 if (!useVulkanMemoryModel) {
368 if (qualifier.coherent)
369 memory.push_back(spv::DecorationCoherent);
370 if (qualifier.volatil) {
371 memory.push_back(spv::DecorationVolatile);
372 memory.push_back(spv::DecorationCoherent);
373 }
374 }
375 if (qualifier.restrict)
376 memory.push_back(spv::DecorationRestrict);
377 if (qualifier.readonly)
378 memory.push_back(spv::DecorationNonWritable);
379 if (qualifier.writeonly)
380 memory.push_back(spv::DecorationNonReadable);
381 }
382
383 // Translate glslang type to SPIR-V layout decorations.
TranslateLayoutDecoration(const glslang::TType & type,glslang::TLayoutMatrix matrixLayout)384 spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
385 {
386 if (type.isMatrix()) {
387 switch (matrixLayout) {
388 case glslang::ElmRowMajor:
389 return spv::DecorationRowMajor;
390 case glslang::ElmColumnMajor:
391 return spv::DecorationColMajor;
392 default:
393 // opaque layouts don't need a majorness
394 return spv::DecorationMax;
395 }
396 } else {
397 switch (type.getBasicType()) {
398 default:
399 return spv::DecorationMax;
400 break;
401 case glslang::EbtBlock:
402 switch (type.getQualifier().storage) {
403 case glslang::EvqUniform:
404 case glslang::EvqBuffer:
405 switch (type.getQualifier().layoutPacking) {
406 case glslang::ElpShared: return spv::DecorationGLSLShared;
407 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
408 default:
409 return spv::DecorationMax;
410 }
411 case glslang::EvqVaryingIn:
412 case glslang::EvqVaryingOut:
413 if (type.getQualifier().isTaskMemory()) {
414 switch (type.getQualifier().layoutPacking) {
415 case glslang::ElpShared: return spv::DecorationGLSLShared;
416 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
417 default: break;
418 }
419 } else {
420 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
421 }
422 return spv::DecorationMax;
423 #ifdef NV_EXTENSIONS
424 case glslang::EvqPayloadNV:
425 case glslang::EvqPayloadInNV:
426 case glslang::EvqHitAttrNV:
427 case glslang::EvqCallableDataNV:
428 case glslang::EvqCallableDataInNV:
429 return spv::DecorationMax;
430 #endif
431 default:
432 assert(0);
433 return spv::DecorationMax;
434 }
435 }
436 }
437 }
438
439 // Translate glslang type to SPIR-V interpolation decorations.
440 // Returns spv::DecorationMax when no decoration
441 // should be applied.
TranslateInterpolationDecoration(const glslang::TQualifier & qualifier)442 spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
443 {
444 if (qualifier.smooth)
445 // Smooth decoration doesn't exist in SPIR-V 1.0
446 return spv::DecorationMax;
447 else if (qualifier.nopersp)
448 return spv::DecorationNoPerspective;
449 else if (qualifier.flat)
450 return spv::DecorationFlat;
451 #ifdef AMD_EXTENSIONS
452 else if (qualifier.explicitInterp) {
453 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
454 return spv::DecorationExplicitInterpAMD;
455 }
456 #endif
457 else
458 return spv::DecorationMax;
459 }
460
461 // Translate glslang type to SPIR-V auxiliary storage decorations.
462 // Returns spv::DecorationMax when no decoration
463 // should be applied.
TranslateAuxiliaryStorageDecoration(const glslang::TQualifier & qualifier)464 spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
465 {
466 if (qualifier.patch)
467 return spv::DecorationPatch;
468 else if (qualifier.centroid)
469 return spv::DecorationCentroid;
470 else if (qualifier.sample) {
471 builder.addCapability(spv::CapabilitySampleRateShading);
472 return spv::DecorationSample;
473 } else
474 return spv::DecorationMax;
475 }
476
477 // If glslang type is invariant, return SPIR-V invariant decoration.
TranslateInvariantDecoration(const glslang::TQualifier & qualifier)478 spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
479 {
480 if (qualifier.invariant)
481 return spv::DecorationInvariant;
482 else
483 return spv::DecorationMax;
484 }
485
486 // If glslang type is noContraction, return SPIR-V NoContraction decoration.
TranslateNoContractionDecoration(const glslang::TQualifier & qualifier)487 spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
488 {
489 if (qualifier.noContraction)
490 return spv::DecorationNoContraction;
491 else
492 return spv::DecorationMax;
493 }
494
495 // If glslang type is nonUniform, return SPIR-V NonUniform decoration.
TranslateNonUniformDecoration(const glslang::TQualifier & qualifier)496 spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
497 {
498 if (qualifier.isNonUniform()) {
499 builder.addExtension("SPV_EXT_descriptor_indexing");
500 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
501 return spv::DecorationNonUniformEXT;
502 } else
503 return spv::DecorationMax;
504 }
505
TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags & coherentFlags)506 spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
507 {
508 if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) {
509 return spv::MemoryAccessMaskNone;
510 }
511 spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone;
512 if (coherentFlags.volatil ||
513 coherentFlags.coherent ||
514 coherentFlags.devicecoherent ||
515 coherentFlags.queuefamilycoherent ||
516 coherentFlags.workgroupcoherent ||
517 coherentFlags.subgroupcoherent) {
518 mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask |
519 spv::MemoryAccessMakePointerVisibleKHRMask;
520 }
521 if (coherentFlags.nonprivate) {
522 mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask;
523 }
524 if (coherentFlags.volatil) {
525 mask = mask | spv::MemoryAccessVolatileMask;
526 }
527 if (mask != spv::MemoryAccessMaskNone) {
528 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
529 }
530 return mask;
531 }
532
TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags & coherentFlags)533 spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
534 {
535 if (!glslangIntermediate->usingVulkanMemoryModel()) {
536 return spv::ImageOperandsMaskNone;
537 }
538 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
539 if (coherentFlags.volatil ||
540 coherentFlags.coherent ||
541 coherentFlags.devicecoherent ||
542 coherentFlags.queuefamilycoherent ||
543 coherentFlags.workgroupcoherent ||
544 coherentFlags.subgroupcoherent) {
545 mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask |
546 spv::ImageOperandsMakeTexelVisibleKHRMask;
547 }
548 if (coherentFlags.nonprivate) {
549 mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask;
550 }
551 if (coherentFlags.volatil) {
552 mask = mask | spv::ImageOperandsVolatileTexelKHRMask;
553 }
554 if (mask != spv::ImageOperandsMaskNone) {
555 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
556 }
557 return mask;
558 }
559
TranslateCoherent(const glslang::TType & type)560 spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
561 {
562 spv::Builder::AccessChain::CoherentFlags flags;
563 flags.coherent = type.getQualifier().coherent;
564 flags.devicecoherent = type.getQualifier().devicecoherent;
565 flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent;
566 // shared variables are implicitly workgroupcoherent in GLSL.
567 flags.workgroupcoherent = type.getQualifier().workgroupcoherent ||
568 type.getQualifier().storage == glslang::EvqShared;
569 flags.subgroupcoherent = type.getQualifier().subgroupcoherent;
570 // *coherent variables are implicitly nonprivate in GLSL
571 flags.nonprivate = type.getQualifier().nonprivate ||
572 flags.subgroupcoherent ||
573 flags.workgroupcoherent ||
574 flags.queuefamilycoherent ||
575 flags.devicecoherent ||
576 flags.coherent;
577 flags.volatil = type.getQualifier().volatil;
578 flags.isImage = type.getBasicType() == glslang::EbtSampler;
579 return flags;
580 }
581
TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags & coherentFlags)582 spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
583 {
584 spv::Scope scope;
585 if (coherentFlags.coherent) {
586 // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model
587 scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
588 } else if (coherentFlags.devicecoherent) {
589 scope = spv::ScopeDevice;
590 } else if (coherentFlags.queuefamilycoherent) {
591 scope = spv::ScopeQueueFamilyKHR;
592 } else if (coherentFlags.workgroupcoherent) {
593 scope = spv::ScopeWorkgroup;
594 } else if (coherentFlags.subgroupcoherent) {
595 scope = spv::ScopeSubgroup;
596 } else {
597 scope = spv::ScopeMax;
598 }
599 if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) {
600 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
601 }
602 return scope;
603 }
604
605 // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
606 // associated capabilities when required. For some built-in variables, a capability
607 // is generated only when using the variable in an executable instruction, but not when
608 // just declaring a struct member variable with it. This is true for PointSize,
609 // ClipDistance, and CullDistance.
TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn,bool memberDeclaration)610 spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration)
611 {
612 switch (builtIn) {
613 case glslang::EbvPointSize:
614 // Defer adding the capability until the built-in is actually used.
615 if (! memberDeclaration) {
616 switch (glslangIntermediate->getStage()) {
617 case EShLangGeometry:
618 builder.addCapability(spv::CapabilityGeometryPointSize);
619 break;
620 case EShLangTessControl:
621 case EShLangTessEvaluation:
622 builder.addCapability(spv::CapabilityTessellationPointSize);
623 break;
624 default:
625 break;
626 }
627 }
628 return spv::BuiltInPointSize;
629
630 // These *Distance capabilities logically belong here, but if the member is declared and
631 // then never used, consumers of SPIR-V prefer the capability not be declared.
632 // They are now generated when used, rather than here when declared.
633 // Potentially, the specification should be more clear what the minimum
634 // use needed is to trigger the capability.
635 //
636 case glslang::EbvClipDistance:
637 if (!memberDeclaration)
638 builder.addCapability(spv::CapabilityClipDistance);
639 return spv::BuiltInClipDistance;
640
641 case glslang::EbvCullDistance:
642 if (!memberDeclaration)
643 builder.addCapability(spv::CapabilityCullDistance);
644 return spv::BuiltInCullDistance;
645
646 case glslang::EbvViewportIndex:
647 builder.addCapability(spv::CapabilityMultiViewport);
648 if (glslangIntermediate->getStage() == EShLangVertex ||
649 glslangIntermediate->getStage() == EShLangTessControl ||
650 glslangIntermediate->getStage() == EShLangTessEvaluation) {
651
652 builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer);
653 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
654 }
655 return spv::BuiltInViewportIndex;
656
657 case glslang::EbvSampleId:
658 builder.addCapability(spv::CapabilitySampleRateShading);
659 return spv::BuiltInSampleId;
660
661 case glslang::EbvSamplePosition:
662 builder.addCapability(spv::CapabilitySampleRateShading);
663 return spv::BuiltInSamplePosition;
664
665 case glslang::EbvSampleMask:
666 return spv::BuiltInSampleMask;
667
668 case glslang::EbvLayer:
669 #ifdef NV_EXTENSIONS
670 if (glslangIntermediate->getStage() == EShLangMeshNV) {
671 return spv::BuiltInLayer;
672 }
673 #endif
674 builder.addCapability(spv::CapabilityGeometry);
675 if (glslangIntermediate->getStage() == EShLangVertex ||
676 glslangIntermediate->getStage() == EShLangTessControl ||
677 glslangIntermediate->getStage() == EShLangTessEvaluation) {
678
679 builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer);
680 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
681 }
682 return spv::BuiltInLayer;
683
684 case glslang::EbvPosition: return spv::BuiltInPosition;
685 case glslang::EbvVertexId: return spv::BuiltInVertexId;
686 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
687 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
688 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
689
690 case glslang::EbvBaseVertex:
691 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
692 builder.addCapability(spv::CapabilityDrawParameters);
693 return spv::BuiltInBaseVertex;
694
695 case glslang::EbvBaseInstance:
696 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
697 builder.addCapability(spv::CapabilityDrawParameters);
698 return spv::BuiltInBaseInstance;
699
700 case glslang::EbvDrawId:
701 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
702 builder.addCapability(spv::CapabilityDrawParameters);
703 return spv::BuiltInDrawIndex;
704
705 case glslang::EbvPrimitiveId:
706 if (glslangIntermediate->getStage() == EShLangFragment)
707 builder.addCapability(spv::CapabilityGeometry);
708 return spv::BuiltInPrimitiveId;
709
710 case glslang::EbvFragStencilRef:
711 builder.addExtension(spv::E_SPV_EXT_shader_stencil_export);
712 builder.addCapability(spv::CapabilityStencilExportEXT);
713 return spv::BuiltInFragStencilRefEXT;
714
715 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
716 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
717 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
718 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
719 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
720 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
721 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
722 case glslang::EbvFace: return spv::BuiltInFrontFacing;
723 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
724 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
725 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
726 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
727 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
728 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
729 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
730 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
731
732 case glslang::EbvSubGroupSize:
733 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
734 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
735 return spv::BuiltInSubgroupSize;
736
737 case glslang::EbvSubGroupInvocation:
738 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
739 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
740 return spv::BuiltInSubgroupLocalInvocationId;
741
742 case glslang::EbvSubGroupEqMask:
743 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
744 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
745 return spv::BuiltInSubgroupEqMaskKHR;
746
747 case glslang::EbvSubGroupGeMask:
748 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
749 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
750 return spv::BuiltInSubgroupGeMaskKHR;
751
752 case glslang::EbvSubGroupGtMask:
753 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
754 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
755 return spv::BuiltInSubgroupGtMaskKHR;
756
757 case glslang::EbvSubGroupLeMask:
758 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
759 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
760 return spv::BuiltInSubgroupLeMaskKHR;
761
762 case glslang::EbvSubGroupLtMask:
763 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
764 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
765 return spv::BuiltInSubgroupLtMaskKHR;
766
767 case glslang::EbvNumSubgroups:
768 builder.addCapability(spv::CapabilityGroupNonUniform);
769 return spv::BuiltInNumSubgroups;
770
771 case glslang::EbvSubgroupID:
772 builder.addCapability(spv::CapabilityGroupNonUniform);
773 return spv::BuiltInSubgroupId;
774
775 case glslang::EbvSubgroupSize2:
776 builder.addCapability(spv::CapabilityGroupNonUniform);
777 return spv::BuiltInSubgroupSize;
778
779 case glslang::EbvSubgroupInvocation2:
780 builder.addCapability(spv::CapabilityGroupNonUniform);
781 return spv::BuiltInSubgroupLocalInvocationId;
782
783 case glslang::EbvSubgroupEqMask2:
784 builder.addCapability(spv::CapabilityGroupNonUniform);
785 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
786 return spv::BuiltInSubgroupEqMask;
787
788 case glslang::EbvSubgroupGeMask2:
789 builder.addCapability(spv::CapabilityGroupNonUniform);
790 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
791 return spv::BuiltInSubgroupGeMask;
792
793 case glslang::EbvSubgroupGtMask2:
794 builder.addCapability(spv::CapabilityGroupNonUniform);
795 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
796 return spv::BuiltInSubgroupGtMask;
797
798 case glslang::EbvSubgroupLeMask2:
799 builder.addCapability(spv::CapabilityGroupNonUniform);
800 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
801 return spv::BuiltInSubgroupLeMask;
802
803 case glslang::EbvSubgroupLtMask2:
804 builder.addCapability(spv::CapabilityGroupNonUniform);
805 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
806 return spv::BuiltInSubgroupLtMask;
807 #ifdef AMD_EXTENSIONS
808 case glslang::EbvBaryCoordNoPersp:
809 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
810 return spv::BuiltInBaryCoordNoPerspAMD;
811
812 case glslang::EbvBaryCoordNoPerspCentroid:
813 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
814 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
815
816 case glslang::EbvBaryCoordNoPerspSample:
817 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
818 return spv::BuiltInBaryCoordNoPerspSampleAMD;
819
820 case glslang::EbvBaryCoordSmooth:
821 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
822 return spv::BuiltInBaryCoordSmoothAMD;
823
824 case glslang::EbvBaryCoordSmoothCentroid:
825 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
826 return spv::BuiltInBaryCoordSmoothCentroidAMD;
827
828 case glslang::EbvBaryCoordSmoothSample:
829 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
830 return spv::BuiltInBaryCoordSmoothSampleAMD;
831
832 case glslang::EbvBaryCoordPullModel:
833 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
834 return spv::BuiltInBaryCoordPullModelAMD;
835 #endif
836
837 case glslang::EbvDeviceIndex:
838 addPre13Extension(spv::E_SPV_KHR_device_group);
839 builder.addCapability(spv::CapabilityDeviceGroup);
840 return spv::BuiltInDeviceIndex;
841
842 case glslang::EbvViewIndex:
843 addPre13Extension(spv::E_SPV_KHR_multiview);
844 builder.addCapability(spv::CapabilityMultiView);
845 return spv::BuiltInViewIndex;
846
847 case glslang::EbvFragSizeEXT:
848 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
849 builder.addCapability(spv::CapabilityFragmentDensityEXT);
850 return spv::BuiltInFragSizeEXT;
851
852 case glslang::EbvFragInvocationCountEXT:
853 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
854 builder.addCapability(spv::CapabilityFragmentDensityEXT);
855 return spv::BuiltInFragInvocationCountEXT;
856
857 #ifdef NV_EXTENSIONS
858 case glslang::EbvViewportMaskNV:
859 if (!memberDeclaration) {
860 builder.addExtension(spv::E_SPV_NV_viewport_array2);
861 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
862 }
863 return spv::BuiltInViewportMaskNV;
864 case glslang::EbvSecondaryPositionNV:
865 if (!memberDeclaration) {
866 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
867 builder.addCapability(spv::CapabilityShaderStereoViewNV);
868 }
869 return spv::BuiltInSecondaryPositionNV;
870 case glslang::EbvSecondaryViewportMaskNV:
871 if (!memberDeclaration) {
872 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
873 builder.addCapability(spv::CapabilityShaderStereoViewNV);
874 }
875 return spv::BuiltInSecondaryViewportMaskNV;
876 case glslang::EbvPositionPerViewNV:
877 if (!memberDeclaration) {
878 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
879 builder.addCapability(spv::CapabilityPerViewAttributesNV);
880 }
881 return spv::BuiltInPositionPerViewNV;
882 case glslang::EbvViewportMaskPerViewNV:
883 if (!memberDeclaration) {
884 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
885 builder.addCapability(spv::CapabilityPerViewAttributesNV);
886 }
887 return spv::BuiltInViewportMaskPerViewNV;
888 case glslang::EbvFragFullyCoveredNV:
889 builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered);
890 builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT);
891 return spv::BuiltInFullyCoveredEXT;
892 case glslang::EbvFragmentSizeNV:
893 builder.addExtension(spv::E_SPV_NV_shading_rate);
894 builder.addCapability(spv::CapabilityShadingRateNV);
895 return spv::BuiltInFragmentSizeNV;
896 case glslang::EbvInvocationsPerPixelNV:
897 builder.addExtension(spv::E_SPV_NV_shading_rate);
898 builder.addCapability(spv::CapabilityShadingRateNV);
899 return spv::BuiltInInvocationsPerPixelNV;
900
901 // raytracing
902 case glslang::EbvLaunchIdNV:
903 return spv::BuiltInLaunchIdNV;
904 case glslang::EbvLaunchSizeNV:
905 return spv::BuiltInLaunchSizeNV;
906 case glslang::EbvWorldRayOriginNV:
907 return spv::BuiltInWorldRayOriginNV;
908 case glslang::EbvWorldRayDirectionNV:
909 return spv::BuiltInWorldRayDirectionNV;
910 case glslang::EbvObjectRayOriginNV:
911 return spv::BuiltInObjectRayOriginNV;
912 case glslang::EbvObjectRayDirectionNV:
913 return spv::BuiltInObjectRayDirectionNV;
914 case glslang::EbvRayTminNV:
915 return spv::BuiltInRayTminNV;
916 case glslang::EbvRayTmaxNV:
917 return spv::BuiltInRayTmaxNV;
918 case glslang::EbvInstanceCustomIndexNV:
919 return spv::BuiltInInstanceCustomIndexNV;
920 case glslang::EbvHitTNV:
921 return spv::BuiltInHitTNV;
922 case glslang::EbvHitKindNV:
923 return spv::BuiltInHitKindNV;
924 case glslang::EbvObjectToWorldNV:
925 return spv::BuiltInObjectToWorldNV;
926 case glslang::EbvWorldToObjectNV:
927 return spv::BuiltInWorldToObjectNV;
928 case glslang::EbvIncomingRayFlagsNV:
929 return spv::BuiltInIncomingRayFlagsNV;
930 case glslang::EbvBaryCoordNV:
931 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
932 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
933 return spv::BuiltInBaryCoordNV;
934 case glslang::EbvBaryCoordNoPerspNV:
935 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
936 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
937 return spv::BuiltInBaryCoordNoPerspNV;
938 case glslang::EbvTaskCountNV:
939 return spv::BuiltInTaskCountNV;
940 case glslang::EbvPrimitiveCountNV:
941 return spv::BuiltInPrimitiveCountNV;
942 case glslang::EbvPrimitiveIndicesNV:
943 return spv::BuiltInPrimitiveIndicesNV;
944 case glslang::EbvClipDistancePerViewNV:
945 return spv::BuiltInClipDistancePerViewNV;
946 case glslang::EbvCullDistancePerViewNV:
947 return spv::BuiltInCullDistancePerViewNV;
948 case glslang::EbvLayerPerViewNV:
949 return spv::BuiltInLayerPerViewNV;
950 case glslang::EbvMeshViewCountNV:
951 return spv::BuiltInMeshViewCountNV;
952 case glslang::EbvMeshViewIndicesNV:
953 return spv::BuiltInMeshViewIndicesNV;
954 #endif
955 default:
956 return spv::BuiltInMax;
957 }
958 }
959
960 // Translate glslang image layout format to SPIR-V image format.
TranslateImageFormat(const glslang::TType & type)961 spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
962 {
963 assert(type.getBasicType() == glslang::EbtSampler);
964
965 // Check for capabilities
966 switch (type.getQualifier().layoutFormat) {
967 case glslang::ElfRg32f:
968 case glslang::ElfRg16f:
969 case glslang::ElfR11fG11fB10f:
970 case glslang::ElfR16f:
971 case glslang::ElfRgba16:
972 case glslang::ElfRgb10A2:
973 case glslang::ElfRg16:
974 case glslang::ElfRg8:
975 case glslang::ElfR16:
976 case glslang::ElfR8:
977 case glslang::ElfRgba16Snorm:
978 case glslang::ElfRg16Snorm:
979 case glslang::ElfRg8Snorm:
980 case glslang::ElfR16Snorm:
981 case glslang::ElfR8Snorm:
982
983 case glslang::ElfRg32i:
984 case glslang::ElfRg16i:
985 case glslang::ElfRg8i:
986 case glslang::ElfR16i:
987 case glslang::ElfR8i:
988
989 case glslang::ElfRgb10a2ui:
990 case glslang::ElfRg32ui:
991 case glslang::ElfRg16ui:
992 case glslang::ElfRg8ui:
993 case glslang::ElfR16ui:
994 case glslang::ElfR8ui:
995 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
996 break;
997
998 default:
999 break;
1000 }
1001
1002 // do the translation
1003 switch (type.getQualifier().layoutFormat) {
1004 case glslang::ElfNone: return spv::ImageFormatUnknown;
1005 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
1006 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
1007 case glslang::ElfR32f: return spv::ImageFormatR32f;
1008 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
1009 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
1010 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
1011 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
1012 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
1013 case glslang::ElfR16f: return spv::ImageFormatR16f;
1014 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
1015 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
1016 case glslang::ElfRg16: return spv::ImageFormatRg16;
1017 case glslang::ElfRg8: return spv::ImageFormatRg8;
1018 case glslang::ElfR16: return spv::ImageFormatR16;
1019 case glslang::ElfR8: return spv::ImageFormatR8;
1020 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
1021 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
1022 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
1023 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
1024 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
1025 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
1026 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
1027 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
1028 case glslang::ElfR32i: return spv::ImageFormatR32i;
1029 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
1030 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
1031 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
1032 case glslang::ElfR16i: return spv::ImageFormatR16i;
1033 case glslang::ElfR8i: return spv::ImageFormatR8i;
1034 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
1035 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
1036 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
1037 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
1038 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
1039 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
1040 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
1041 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
1042 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
1043 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
1044 default: return spv::ImageFormatMax;
1045 }
1046 }
1047
TranslateSelectionControl(const glslang::TIntermSelection & selectionNode) const1048 spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const
1049 {
1050 if (selectionNode.getFlatten())
1051 return spv::SelectionControlFlattenMask;
1052 if (selectionNode.getDontFlatten())
1053 return spv::SelectionControlDontFlattenMask;
1054 return spv::SelectionControlMaskNone;
1055 }
1056
TranslateSwitchControl(const glslang::TIntermSwitch & switchNode) const1057 spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const
1058 {
1059 if (switchNode.getFlatten())
1060 return spv::SelectionControlFlattenMask;
1061 if (switchNode.getDontFlatten())
1062 return spv::SelectionControlDontFlattenMask;
1063 return spv::SelectionControlMaskNone;
1064 }
1065
1066 // return a non-0 dependency if the dependency argument must be set
TranslateLoopControl(const glslang::TIntermLoop & loopNode,unsigned int & dependencyLength) const1067 spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
1068 unsigned int& dependencyLength) const
1069 {
1070 spv::LoopControlMask control = spv::LoopControlMaskNone;
1071
1072 if (loopNode.getDontUnroll())
1073 control = control | spv::LoopControlDontUnrollMask;
1074 if (loopNode.getUnroll())
1075 control = control | spv::LoopControlUnrollMask;
1076 if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite)
1077 control = control | spv::LoopControlDependencyInfiniteMask;
1078 else if (loopNode.getLoopDependency() > 0) {
1079 control = control | spv::LoopControlDependencyLengthMask;
1080 dependencyLength = loopNode.getLoopDependency();
1081 }
1082
1083 return control;
1084 }
1085
1086 // Translate glslang type to SPIR-V storage class.
TranslateStorageClass(const glslang::TType & type)1087 spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
1088 {
1089 if (type.getQualifier().isPipeInput())
1090 return spv::StorageClassInput;
1091 if (type.getQualifier().isPipeOutput())
1092 return spv::StorageClassOutput;
1093
1094 if (glslangIntermediate->getSource() != glslang::EShSourceHlsl ||
1095 type.getQualifier().storage == glslang::EvqUniform) {
1096 if (type.getBasicType() == glslang::EbtAtomicUint)
1097 return spv::StorageClassAtomicCounter;
1098 if (type.containsOpaque())
1099 return spv::StorageClassUniformConstant;
1100 }
1101
1102 #ifdef NV_EXTENSIONS
1103 if (type.getQualifier().isUniformOrBuffer() &&
1104 type.getQualifier().layoutShaderRecordNV) {
1105 return spv::StorageClassShaderRecordBufferNV;
1106 }
1107 #endif
1108
1109 if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) {
1110 addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class);
1111 return spv::StorageClassStorageBuffer;
1112 }
1113
1114 if (type.getQualifier().isUniformOrBuffer()) {
1115 if (type.getQualifier().layoutPushConstant)
1116 return spv::StorageClassPushConstant;
1117 if (type.getBasicType() == glslang::EbtBlock)
1118 return spv::StorageClassUniform;
1119 return spv::StorageClassUniformConstant;
1120 }
1121
1122 switch (type.getQualifier().storage) {
1123 case glslang::EvqShared: return spv::StorageClassWorkgroup;
1124 case glslang::EvqGlobal: return spv::StorageClassPrivate;
1125 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
1126 case glslang::EvqTemporary: return spv::StorageClassFunction;
1127 #ifdef NV_EXTENSIONS
1128 case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV;
1129 case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV;
1130 case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV;
1131 case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV;
1132 case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV;
1133 #endif
1134 default:
1135 assert(0);
1136 break;
1137 }
1138
1139 return spv::StorageClassFunction;
1140 }
1141
1142 // Add capabilities pertaining to how an array is indexed.
addIndirectionIndexCapabilities(const glslang::TType & baseType,const glslang::TType & indexType)1143 void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
1144 const glslang::TType& indexType)
1145 {
1146 if (indexType.getQualifier().isNonUniform()) {
1147 // deal with an asserted non-uniform index
1148 // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration
1149 if (baseType.getBasicType() == glslang::EbtSampler) {
1150 if (baseType.getQualifier().hasAttachment())
1151 builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT);
1152 else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer)
1153 builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT);
1154 else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer)
1155 builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT);
1156 else if (baseType.isImage())
1157 builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT);
1158 else if (baseType.isTexture())
1159 builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT);
1160 } else if (baseType.getBasicType() == glslang::EbtBlock) {
1161 if (baseType.getQualifier().storage == glslang::EvqBuffer)
1162 builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT);
1163 else if (baseType.getQualifier().storage == glslang::EvqUniform)
1164 builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT);
1165 }
1166 } else {
1167 // assume a dynamically uniform index
1168 if (baseType.getBasicType() == glslang::EbtSampler) {
1169 if (baseType.getQualifier().hasAttachment()) {
1170 builder.addExtension("SPV_EXT_descriptor_indexing");
1171 builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT);
1172 } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) {
1173 builder.addExtension("SPV_EXT_descriptor_indexing");
1174 builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT);
1175 } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) {
1176 builder.addExtension("SPV_EXT_descriptor_indexing");
1177 builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT);
1178 }
1179 }
1180 }
1181 }
1182
1183 // Return whether or not the given type is something that should be tied to a
1184 // descriptor set.
IsDescriptorResource(const glslang::TType & type)1185 bool IsDescriptorResource(const glslang::TType& type)
1186 {
1187 // uniform and buffer blocks are included, unless it is a push_constant
1188 if (type.getBasicType() == glslang::EbtBlock)
1189 return type.getQualifier().isUniformOrBuffer() &&
1190 #ifdef NV_EXTENSIONS
1191 ! type.getQualifier().layoutShaderRecordNV &&
1192 #endif
1193 ! type.getQualifier().layoutPushConstant;
1194
1195 // non block...
1196 // basically samplerXXX/subpass/sampler/texture are all included
1197 // if they are the global-scope-class, not the function parameter
1198 // (or local, if they ever exist) class.
1199 if (type.getBasicType() == glslang::EbtSampler)
1200 return type.getQualifier().isUniformOrBuffer();
1201
1202 // None of the above.
1203 return false;
1204 }
1205
InheritQualifiers(glslang::TQualifier & child,const glslang::TQualifier & parent)1206 void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
1207 {
1208 if (child.layoutMatrix == glslang::ElmNone)
1209 child.layoutMatrix = parent.layoutMatrix;
1210
1211 if (parent.invariant)
1212 child.invariant = true;
1213 if (parent.nopersp)
1214 child.nopersp = true;
1215 #ifdef AMD_EXTENSIONS
1216 if (parent.explicitInterp)
1217 child.explicitInterp = true;
1218 #endif
1219 if (parent.flat)
1220 child.flat = true;
1221 if (parent.centroid)
1222 child.centroid = true;
1223 if (parent.patch)
1224 child.patch = true;
1225 if (parent.sample)
1226 child.sample = true;
1227 if (parent.coherent)
1228 child.coherent = true;
1229 if (parent.devicecoherent)
1230 child.devicecoherent = true;
1231 if (parent.queuefamilycoherent)
1232 child.queuefamilycoherent = true;
1233 if (parent.workgroupcoherent)
1234 child.workgroupcoherent = true;
1235 if (parent.subgroupcoherent)
1236 child.subgroupcoherent = true;
1237 if (parent.nonprivate)
1238 child.nonprivate = true;
1239 if (parent.volatil)
1240 child.volatil = true;
1241 if (parent.restrict)
1242 child.restrict = true;
1243 if (parent.readonly)
1244 child.readonly = true;
1245 if (parent.writeonly)
1246 child.writeonly = true;
1247 #ifdef NV_EXTENSIONS
1248 if (parent.perPrimitiveNV)
1249 child.perPrimitiveNV = true;
1250 if (parent.perViewNV)
1251 child.perViewNV = true;
1252 if (parent.perTaskNV)
1253 child.perTaskNV = true;
1254 #endif
1255 }
1256
HasNonLayoutQualifiers(const glslang::TType & type,const glslang::TQualifier & qualifier)1257 bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
1258 {
1259 // This should list qualifiers that simultaneous satisfy:
1260 // - struct members might inherit from a struct declaration
1261 // (note that non-block structs don't explicitly inherit,
1262 // only implicitly, meaning no decoration involved)
1263 // - affect decorations on the struct members
1264 // (note smooth does not, and expecting something like volatile
1265 // to effect the whole object)
1266 // - are not part of the offset/st430/etc or row/column-major layout
1267 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
1268 }
1269
1270 //
1271 // Implement the TGlslangToSpvTraverser class.
1272 //
1273
TGlslangToSpvTraverser(unsigned int spvVersion,const glslang::TIntermediate * glslangIntermediate,spv::SpvBuildLogger * buildLogger,glslang::SpvOptions & options)1274 TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate,
1275 spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options)
1276 : TIntermTraverser(true, false, true),
1277 options(options),
1278 shaderEntry(nullptr), currentFunction(nullptr),
1279 sequenceDepth(0), logger(buildLogger),
1280 builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger),
1281 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
1282 glslangIntermediate(glslangIntermediate)
1283 {
1284 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
1285
1286 builder.clearAccessChain();
1287 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()),
1288 glslangIntermediate->getVersion());
1289
1290 if (options.generateDebugInfo) {
1291 builder.setEmitOpLines();
1292 builder.setSourceFile(glslangIntermediate->getSourceFile());
1293
1294 // Set the source shader's text. If for SPV version 1.0, include
1295 // a preamble in comments stating the OpModuleProcessed instructions.
1296 // Otherwise, emit those as actual instructions.
1297 std::string text;
1298 const std::vector<std::string>& processes = glslangIntermediate->getProcesses();
1299 for (int p = 0; p < (int)processes.size(); ++p) {
1300 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) {
1301 text.append("// OpModuleProcessed ");
1302 text.append(processes[p]);
1303 text.append("\n");
1304 } else
1305 builder.addModuleProcessed(processes[p]);
1306 }
1307 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0)
1308 text.append("#line 1\n");
1309 text.append(glslangIntermediate->getSourceText());
1310 builder.setSourceText(text);
1311 // Pass name and text for all included files
1312 const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
1313 for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
1314 builder.addInclude(iItr->first, iItr->second);
1315 }
1316 stdBuiltins = builder.import("GLSL.std.450");
1317
1318 spv::AddressingModel addressingModel = spv::AddressingModelLogical;
1319 spv::MemoryModel memoryModel = spv::MemoryModelGLSL450;
1320
1321 if (glslangIntermediate->usingPhysicalStorageBuffer()) {
1322 addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT;
1323 builder.addExtension(spv::E_SPV_EXT_physical_storage_buffer);
1324 builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT);
1325 };
1326 if (glslangIntermediate->usingVulkanMemoryModel()) {
1327 memoryModel = spv::MemoryModelVulkanKHR;
1328 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
1329 builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model);
1330 }
1331 builder.setMemoryModel(addressingModel, memoryModel);
1332
1333 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
1334 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
1335
1336 // Add the source extensions
1337 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
1338 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
1339 builder.addSourceExtension(it->c_str());
1340
1341 // Add the top-level modes for this shader.
1342
1343 if (glslangIntermediate->getXfbMode()) {
1344 builder.addCapability(spv::CapabilityTransformFeedback);
1345 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
1346 }
1347
1348 unsigned int mode;
1349 switch (glslangIntermediate->getStage()) {
1350 case EShLangVertex:
1351 builder.addCapability(spv::CapabilityShader);
1352 break;
1353
1354 case EShLangTessEvaluation:
1355 case EShLangTessControl:
1356 builder.addCapability(spv::CapabilityTessellation);
1357
1358 glslang::TLayoutGeometry primitive;
1359
1360 if (glslangIntermediate->getStage() == EShLangTessControl) {
1361 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1362 primitive = glslangIntermediate->getOutputPrimitive();
1363 } else {
1364 primitive = glslangIntermediate->getInputPrimitive();
1365 }
1366
1367 switch (primitive) {
1368 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1369 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
1370 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
1371 default: mode = spv::ExecutionModeMax; break;
1372 }
1373 if (mode != spv::ExecutionModeMax)
1374 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1375
1376 switch (glslangIntermediate->getVertexSpacing()) {
1377 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
1378 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
1379 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
1380 default: mode = spv::ExecutionModeMax; break;
1381 }
1382 if (mode != spv::ExecutionModeMax)
1383 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1384
1385 switch (glslangIntermediate->getVertexOrder()) {
1386 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
1387 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
1388 default: mode = spv::ExecutionModeMax; break;
1389 }
1390 if (mode != spv::ExecutionModeMax)
1391 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1392
1393 if (glslangIntermediate->getPointMode())
1394 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
1395 break;
1396
1397 case EShLangGeometry:
1398 builder.addCapability(spv::CapabilityGeometry);
1399 switch (glslangIntermediate->getInputPrimitive()) {
1400 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
1401 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
1402 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
1403 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1404 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
1405 default: mode = spv::ExecutionModeMax; break;
1406 }
1407 if (mode != spv::ExecutionModeMax)
1408 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1409
1410 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
1411
1412 switch (glslangIntermediate->getOutputPrimitive()) {
1413 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1414 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
1415 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
1416 default: mode = spv::ExecutionModeMax; break;
1417 }
1418 if (mode != spv::ExecutionModeMax)
1419 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1420 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1421 break;
1422
1423 case EShLangFragment:
1424 builder.addCapability(spv::CapabilityShader);
1425 if (glslangIntermediate->getPixelCenterInteger())
1426 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
1427
1428 if (glslangIntermediate->getOriginUpperLeft())
1429 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
1430 else
1431 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
1432
1433 if (glslangIntermediate->getEarlyFragmentTests())
1434 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
1435
1436 if (glslangIntermediate->getPostDepthCoverage()) {
1437 builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage);
1438 builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage);
1439 builder.addExtension(spv::E_SPV_KHR_post_depth_coverage);
1440 }
1441
1442 switch(glslangIntermediate->getDepth()) {
1443 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
1444 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
1445 default: mode = spv::ExecutionModeMax; break;
1446 }
1447 if (mode != spv::ExecutionModeMax)
1448 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1449
1450 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
1451 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
1452 break;
1453
1454 case EShLangCompute:
1455 builder.addCapability(spv::CapabilityShader);
1456 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1457 glslangIntermediate->getLocalSize(1),
1458 glslangIntermediate->getLocalSize(2));
1459 #ifdef NV_EXTENSIONS
1460 if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) {
1461 builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV);
1462 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV);
1463 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1464 } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) {
1465 builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV);
1466 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV);
1467 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1468 }
1469 #endif
1470 break;
1471
1472 #ifdef NV_EXTENSIONS
1473 case EShLangRayGenNV:
1474 case EShLangIntersectNV:
1475 case EShLangAnyHitNV:
1476 case EShLangClosestHitNV:
1477 case EShLangMissNV:
1478 case EShLangCallableNV:
1479 builder.addCapability(spv::CapabilityRayTracingNV);
1480 builder.addExtension("SPV_NV_ray_tracing");
1481 break;
1482 case EShLangTaskNV:
1483 case EShLangMeshNV:
1484 builder.addCapability(spv::CapabilityMeshShadingNV);
1485 builder.addExtension(spv::E_SPV_NV_mesh_shader);
1486 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1487 glslangIntermediate->getLocalSize(1),
1488 glslangIntermediate->getLocalSize(2));
1489 if (glslangIntermediate->getStage() == EShLangMeshNV) {
1490 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1491 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives());
1492
1493 switch (glslangIntermediate->getOutputPrimitive()) {
1494 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1495 case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break;
1496 case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break;
1497 default: mode = spv::ExecutionModeMax; break;
1498 }
1499 if (mode != spv::ExecutionModeMax)
1500 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1501 }
1502 break;
1503 #endif
1504
1505 default:
1506 break;
1507 }
1508 }
1509
1510 // Finish creating SPV, after the traversal is complete.
finishSpv()1511 void TGlslangToSpvTraverser::finishSpv()
1512 {
1513 // Finish the entry point function
1514 if (! entryPointTerminated) {
1515 builder.setBuildPoint(shaderEntry->getLastBlock());
1516 builder.leaveFunction();
1517 }
1518
1519 // finish off the entry-point SPV instruction by adding the Input/Output <id>
1520 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
1521 entryPoint->addIdOperand(*it);
1522
1523 // Add capabilities, extensions, remove unneeded decorations, etc.,
1524 // based on the resulting SPIR-V.
1525 builder.postProcess();
1526 }
1527
1528 // Write the SPV into 'out'.
dumpSpv(std::vector<unsigned int> & out)1529 void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
1530 {
1531 builder.dump(out);
1532 }
1533
1534 //
1535 // Implement the traversal functions.
1536 //
1537 // Return true from interior nodes to have the external traversal
1538 // continue on to children. Return false if children were
1539 // already processed.
1540 //
1541
1542 //
1543 // Symbols can turn into
1544 // - uniform/input reads
1545 // - output writes
1546 // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
1547 // - something simple that degenerates into the last bullet
1548 //
visitSymbol(glslang::TIntermSymbol * symbol)1549 void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
1550 {
1551 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1552 if (symbol->getType().getQualifier().isSpecConstant())
1553 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1554
1555 // getSymbolId() will set up all the IO decorations on the first call.
1556 // Formal function parameters were mapped during makeFunctions().
1557 spv::Id id = getSymbolId(symbol);
1558
1559 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
1560 if (builder.isPointer(id)) {
1561 spv::StorageClass sc = builder.getStorageClass(id);
1562 if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) {
1563 if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0)
1564 iOSet.insert(id);
1565 }
1566 }
1567
1568 // Only process non-linkage-only nodes for generating actual static uses
1569 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
1570 // Prepare to generate code for the access
1571
1572 // L-value chains will be computed left to right. We're on the symbol now,
1573 // which is the left-most part of the access chain, so now is "clear" time,
1574 // followed by setting the base.
1575 builder.clearAccessChain();
1576
1577 // For now, we consider all user variables as being in memory, so they are pointers,
1578 // except for
1579 // A) R-Value arguments to a function, which are an intermediate object.
1580 // See comments in handleUserFunctionCall().
1581 // B) Specialization constants (normal constants don't even come in as a variable),
1582 // These are also pure R-values.
1583 glslang::TQualifier qualifier = symbol->getQualifier();
1584 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end())
1585 builder.setAccessChainRValue(id);
1586 else
1587 builder.setAccessChainLValue(id);
1588 }
1589
1590 // Process linkage-only nodes for any special additional interface work.
1591 if (linkageOnly) {
1592 if (glslangIntermediate->getHlslFunctionality1()) {
1593 // Map implicit counter buffers to their originating buffers, which should have been
1594 // seen by now, given earlier pruning of unused counters, and preservation of order
1595 // of declaration.
1596 if (symbol->getType().getQualifier().isUniformOrBuffer()) {
1597 if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) {
1598 // Save possible originating buffers for counter buffers, keyed by
1599 // making the potential counter-buffer name.
1600 std::string keyName = symbol->getName().c_str();
1601 keyName = glslangIntermediate->addCounterBufferName(keyName);
1602 counterOriginator[keyName] = symbol;
1603 } else {
1604 // Handle a counter buffer, by finding the saved originating buffer.
1605 std::string keyName = symbol->getName().c_str();
1606 auto it = counterOriginator.find(keyName);
1607 if (it != counterOriginator.end()) {
1608 id = getSymbolId(it->second);
1609 if (id != spv::NoResult) {
1610 spv::Id counterId = getSymbolId(symbol);
1611 if (counterId != spv::NoResult) {
1612 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
1613 builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId);
1614 }
1615 }
1616 }
1617 }
1618 }
1619 }
1620 }
1621 }
1622
visitBinary(glslang::TVisit,glslang::TIntermBinary * node)1623 bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1624 {
1625 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
1626
1627 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1628 if (node->getType().getQualifier().isSpecConstant())
1629 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1630
1631 // First, handle special cases
1632 switch (node->getOp()) {
1633 case glslang::EOpAssign:
1634 case glslang::EOpAddAssign:
1635 case glslang::EOpSubAssign:
1636 case glslang::EOpMulAssign:
1637 case glslang::EOpVectorTimesMatrixAssign:
1638 case glslang::EOpVectorTimesScalarAssign:
1639 case glslang::EOpMatrixTimesScalarAssign:
1640 case glslang::EOpMatrixTimesMatrixAssign:
1641 case glslang::EOpDivAssign:
1642 case glslang::EOpModAssign:
1643 case glslang::EOpAndAssign:
1644 case glslang::EOpInclusiveOrAssign:
1645 case glslang::EOpExclusiveOrAssign:
1646 case glslang::EOpLeftShiftAssign:
1647 case glslang::EOpRightShiftAssign:
1648 // A bin-op assign "a += b" means the same thing as "a = a + b"
1649 // where a is evaluated before b. For a simple assignment, GLSL
1650 // says to evaluate the left before the right. So, always, left
1651 // node then right node.
1652 {
1653 // get the left l-value, save it away
1654 builder.clearAccessChain();
1655 node->getLeft()->traverse(this);
1656 spv::Builder::AccessChain lValue = builder.getAccessChain();
1657
1658 // evaluate the right
1659 builder.clearAccessChain();
1660 node->getRight()->traverse(this);
1661 spv::Id rValue = accessChainLoad(node->getRight()->getType());
1662
1663 if (node->getOp() != glslang::EOpAssign) {
1664 // the left is also an r-value
1665 builder.setAccessChain(lValue);
1666 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
1667
1668 // do the operation
1669 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
1670 TranslateNoContractionDecoration(node->getType().getQualifier()),
1671 TranslateNonUniformDecoration(node->getType().getQualifier()) };
1672 rValue = createBinaryOperation(node->getOp(), decorations,
1673 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1674 node->getType().getBasicType());
1675
1676 // these all need their counterparts in createBinaryOperation()
1677 assert(rValue != spv::NoResult);
1678 }
1679
1680 // store the result
1681 builder.setAccessChain(lValue);
1682 multiTypeStore(node->getLeft()->getType(), rValue);
1683
1684 // assignments are expressions having an rValue after they are evaluated...
1685 builder.clearAccessChain();
1686 builder.setAccessChainRValue(rValue);
1687 }
1688 return false;
1689 case glslang::EOpIndexDirect:
1690 case glslang::EOpIndexDirectStruct:
1691 {
1692 // Get the left part of the access chain.
1693 node->getLeft()->traverse(this);
1694
1695 // Add the next element in the chain
1696
1697 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
1698 if (! node->getLeft()->getType().isArray() &&
1699 node->getLeft()->getType().isVector() &&
1700 node->getOp() == glslang::EOpIndexDirect) {
1701 // This is essentially a hard-coded vector swizzle of size 1,
1702 // so short circuit the access-chain stuff with a swizzle.
1703 std::vector<unsigned> swizzle;
1704 swizzle.push_back(glslangIndex);
1705 int dummySize;
1706 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1707 TranslateCoherent(node->getLeft()->getType()),
1708 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
1709 } else {
1710
1711 // Load through a block reference is performed with a dot operator that
1712 // is mapped to EOpIndexDirectStruct. When we get to the actual reference,
1713 // do a load and reset the access chain.
1714 if (node->getLeft()->getBasicType() == glslang::EbtReference &&
1715 !node->getLeft()->getType().isArray() &&
1716 node->getOp() == glslang::EOpIndexDirectStruct)
1717 {
1718 spv::Id left = accessChainLoad(node->getLeft()->getType());
1719 builder.clearAccessChain();
1720 builder.setAccessChainLValue(left);
1721 }
1722
1723 int spvIndex = glslangIndex;
1724 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1725 node->getOp() == glslang::EOpIndexDirectStruct)
1726 {
1727 // This may be, e.g., an anonymous block-member selection, which generally need
1728 // index remapping due to hidden members in anonymous blocks.
1729 std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()];
1730 assert(remapper.size() > 0);
1731 spvIndex = remapper[glslangIndex];
1732 }
1733
1734 // normal case for indexing array or structure or block
1735 builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType()));
1736
1737 // Add capabilities here for accessing PointSize and clip/cull distance.
1738 // We have deferred generation of associated capabilities until now.
1739 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
1740 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
1741 }
1742 }
1743 return false;
1744 case glslang::EOpIndexIndirect:
1745 {
1746 // Structure or array or vector indirection.
1747 // Will use native SPIR-V access-chain for struct and array indirection;
1748 // matrices are arrays of vectors, so will also work for a matrix.
1749 // Will use the access chain's 'component' for variable index into a vector.
1750
1751 // This adapter is building access chains left to right.
1752 // Set up the access chain to the left.
1753 node->getLeft()->traverse(this);
1754
1755 // save it so that computing the right side doesn't trash it
1756 spv::Builder::AccessChain partial = builder.getAccessChain();
1757
1758 // compute the next index in the chain
1759 builder.clearAccessChain();
1760 node->getRight()->traverse(this);
1761 spv::Id index = accessChainLoad(node->getRight()->getType());
1762
1763 addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType());
1764
1765 // restore the saved access chain
1766 builder.setAccessChain(partial);
1767
1768 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) {
1769 int dummySize;
1770 builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()),
1771 TranslateCoherent(node->getLeft()->getType()),
1772 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
1773 } else
1774 builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType()));
1775 }
1776 return false;
1777 case glslang::EOpVectorSwizzle:
1778 {
1779 node->getLeft()->traverse(this);
1780 std::vector<unsigned> swizzle;
1781 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
1782 int dummySize;
1783 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1784 TranslateCoherent(node->getLeft()->getType()),
1785 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
1786 }
1787 return false;
1788 case glslang::EOpMatrixSwizzle:
1789 logger->missingFunctionality("matrix swizzle");
1790 return true;
1791 case glslang::EOpLogicalOr:
1792 case glslang::EOpLogicalAnd:
1793 {
1794
1795 // These may require short circuiting, but can sometimes be done as straight
1796 // binary operations. The right operand must be short circuited if it has
1797 // side effects, and should probably be if it is complex.
1798 if (isTrivial(node->getRight()->getAsTyped()))
1799 break; // handle below as a normal binary operation
1800 // otherwise, we need to do dynamic short circuiting on the right operand
1801 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped());
1802 builder.clearAccessChain();
1803 builder.setAccessChainRValue(result);
1804 }
1805 return false;
1806 default:
1807 break;
1808 }
1809
1810 // Assume generic binary op...
1811
1812 // get right operand
1813 builder.clearAccessChain();
1814 node->getLeft()->traverse(this);
1815 spv::Id left = accessChainLoad(node->getLeft()->getType());
1816
1817 // get left operand
1818 builder.clearAccessChain();
1819 node->getRight()->traverse(this);
1820 spv::Id right = accessChainLoad(node->getRight()->getType());
1821
1822 // get result
1823 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
1824 TranslateNoContractionDecoration(node->getType().getQualifier()),
1825 TranslateNonUniformDecoration(node->getType().getQualifier()) };
1826 spv::Id result = createBinaryOperation(node->getOp(), decorations,
1827 convertGlslangToSpvType(node->getType()), left, right,
1828 node->getLeft()->getType().getBasicType());
1829
1830 builder.clearAccessChain();
1831 if (! result) {
1832 logger->missingFunctionality("unknown glslang binary operation");
1833 return true; // pick up a child as the place-holder result
1834 } else {
1835 builder.setAccessChainRValue(result);
1836 return false;
1837 }
1838 }
1839
visitUnary(glslang::TVisit,glslang::TIntermUnary * node)1840 bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
1841 {
1842 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
1843
1844 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1845 if (node->getType().getQualifier().isSpecConstant())
1846 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1847
1848 spv::Id result = spv::NoResult;
1849
1850 // try texturing first
1851 result = createImageTextureFunctionCall(node);
1852 if (result != spv::NoResult) {
1853 builder.clearAccessChain();
1854 builder.setAccessChainRValue(result);
1855
1856 return false; // done with this node
1857 }
1858
1859 // Non-texturing.
1860
1861 if (node->getOp() == glslang::EOpArrayLength) {
1862 // Quite special; won't want to evaluate the operand.
1863
1864 // Currently, the front-end does not allow .length() on an array until it is sized,
1865 // except for the last block membeor of an SSBO.
1866 // TODO: If this changes, link-time sized arrays might show up here, and need their
1867 // size extracted.
1868
1869 // Normal .length() would have been constant folded by the front-end.
1870 // So, this has to be block.lastMember.length().
1871 // SPV wants "block" and member number as the operands, go get them.
1872
1873 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
1874 block->traverse(this);
1875 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst();
1876 spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member);
1877
1878 // GLSL semantics say the result of .length() is an int, while SPIR-V says
1879 // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's
1880 // AST expectation of a signed result.
1881 if (glslangIntermediate->getSource() == glslang::EShSourceGlsl)
1882 length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length);
1883
1884 builder.clearAccessChain();
1885 builder.setAccessChainRValue(length);
1886
1887 return false;
1888 }
1889
1890 // Start by evaluating the operand
1891
1892 // Does it need a swizzle inversion? If so, evaluation is inverted;
1893 // operate first on the swizzle base, then apply the swizzle.
1894 spv::Id invertedType = spv::NoType;
1895 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
1896 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
1897 invertedType = getInvertedSwizzleType(*node->getOperand());
1898
1899 builder.clearAccessChain();
1900 if (invertedType != spv::NoType)
1901 node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this);
1902 else
1903 node->getOperand()->traverse(this);
1904
1905 spv::Id operand = spv::NoResult;
1906
1907 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
1908 node->getOp() == glslang::EOpAtomicCounterDecrement ||
1909 node->getOp() == glslang::EOpAtomicCounter ||
1910 node->getOp() == glslang::EOpInterpolateAtCentroid)
1911 operand = builder.accessChainGetLValue(); // Special case l-value operands
1912 else
1913 operand = accessChainLoad(node->getOperand()->getType());
1914
1915 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
1916 TranslateNoContractionDecoration(node->getType().getQualifier()),
1917 TranslateNonUniformDecoration(node->getType().getQualifier()) };
1918
1919 // it could be a conversion
1920 if (! result)
1921 result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType());
1922
1923 // if not, then possibly an operation
1924 if (! result)
1925 result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType());
1926
1927 if (result) {
1928 if (invertedType) {
1929 result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result);
1930 builder.addDecoration(result, decorations.nonUniform);
1931 }
1932
1933 builder.clearAccessChain();
1934 builder.setAccessChainRValue(result);
1935
1936 return false; // done with this node
1937 }
1938
1939 // it must be a special case, check...
1940 switch (node->getOp()) {
1941 case glslang::EOpPostIncrement:
1942 case glslang::EOpPostDecrement:
1943 case glslang::EOpPreIncrement:
1944 case glslang::EOpPreDecrement:
1945 {
1946 // we need the integer value "1" or the floating point "1.0" to add/subtract
1947 spv::Id one = 0;
1948 if (node->getBasicType() == glslang::EbtFloat)
1949 one = builder.makeFloatConstant(1.0F);
1950 else if (node->getBasicType() == glslang::EbtDouble)
1951 one = builder.makeDoubleConstant(1.0);
1952 else if (node->getBasicType() == glslang::EbtFloat16)
1953 one = builder.makeFloat16Constant(1.0F);
1954 else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8)
1955 one = builder.makeInt8Constant(1);
1956 else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16)
1957 one = builder.makeInt16Constant(1);
1958 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
1959 one = builder.makeInt64Constant(1);
1960 else
1961 one = builder.makeIntConstant(1);
1962 glslang::TOperator op;
1963 if (node->getOp() == glslang::EOpPreIncrement ||
1964 node->getOp() == glslang::EOpPostIncrement)
1965 op = glslang::EOpAdd;
1966 else
1967 op = glslang::EOpSub;
1968
1969 spv::Id result = createBinaryOperation(op, decorations,
1970 convertGlslangToSpvType(node->getType()), operand, one,
1971 node->getType().getBasicType());
1972 assert(result != spv::NoResult);
1973
1974 // The result of operation is always stored, but conditionally the
1975 // consumed result. The consumed result is always an r-value.
1976 builder.accessChainStore(result);
1977 builder.clearAccessChain();
1978 if (node->getOp() == glslang::EOpPreIncrement ||
1979 node->getOp() == glslang::EOpPreDecrement)
1980 builder.setAccessChainRValue(result);
1981 else
1982 builder.setAccessChainRValue(operand);
1983 }
1984
1985 return false;
1986
1987 case glslang::EOpEmitStreamVertex:
1988 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
1989 return false;
1990 case glslang::EOpEndStreamPrimitive:
1991 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
1992 return false;
1993
1994 default:
1995 logger->missingFunctionality("unknown glslang unary");
1996 return true; // pick up operand as placeholder result
1997 }
1998 }
1999
visitAggregate(glslang::TVisit visit,glslang::TIntermAggregate * node)2000 bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
2001 {
2002 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2003 if (node->getType().getQualifier().isSpecConstant())
2004 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2005
2006 spv::Id result = spv::NoResult;
2007 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
2008 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
2009
2010 // try texturing
2011 result = createImageTextureFunctionCall(node);
2012 if (result != spv::NoResult) {
2013 builder.clearAccessChain();
2014 builder.setAccessChainRValue(result);
2015
2016 return false;
2017 } else if (node->getOp() == glslang::EOpImageStore ||
2018 #ifdef AMD_EXTENSIONS
2019 node->getOp() == glslang::EOpImageStoreLod ||
2020 #endif
2021 node->getOp() == glslang::EOpImageAtomicStore) {
2022 // "imageStore" is a special case, which has no result
2023 return false;
2024 }
2025
2026 glslang::TOperator binOp = glslang::EOpNull;
2027 bool reduceComparison = true;
2028 bool isMatrix = false;
2029 bool noReturnValue = false;
2030 bool atomic = false;
2031
2032 assert(node->getOp());
2033
2034 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
2035
2036 switch (node->getOp()) {
2037 case glslang::EOpSequence:
2038 {
2039 if (preVisit)
2040 ++sequenceDepth;
2041 else
2042 --sequenceDepth;
2043
2044 if (sequenceDepth == 1) {
2045 // If this is the parent node of all the functions, we want to see them
2046 // early, so all call points have actual SPIR-V functions to reference.
2047 // In all cases, still let the traverser visit the children for us.
2048 makeFunctions(node->getAsAggregate()->getSequence());
2049
2050 // Also, we want all globals initializers to go into the beginning of the entry point, before
2051 // anything else gets there, so visit out of order, doing them all now.
2052 makeGlobalInitializers(node->getAsAggregate()->getSequence());
2053
2054 // Initializers are done, don't want to visit again, but functions and link objects need to be processed,
2055 // so do them manually.
2056 visitFunctions(node->getAsAggregate()->getSequence());
2057
2058 return false;
2059 }
2060
2061 return true;
2062 }
2063 case glslang::EOpLinkerObjects:
2064 {
2065 if (visit == glslang::EvPreVisit)
2066 linkageOnly = true;
2067 else
2068 linkageOnly = false;
2069
2070 return true;
2071 }
2072 case glslang::EOpComma:
2073 {
2074 // processing from left to right naturally leaves the right-most
2075 // lying around in the access chain
2076 glslang::TIntermSequence& glslangOperands = node->getSequence();
2077 for (int i = 0; i < (int)glslangOperands.size(); ++i)
2078 glslangOperands[i]->traverse(this);
2079
2080 return false;
2081 }
2082 case glslang::EOpFunction:
2083 if (visit == glslang::EvPreVisit) {
2084 if (isShaderEntryPoint(node)) {
2085 inEntryPoint = true;
2086 builder.setBuildPoint(shaderEntry->getLastBlock());
2087 currentFunction = shaderEntry;
2088 } else {
2089 handleFunctionEntry(node);
2090 }
2091 } else {
2092 if (inEntryPoint)
2093 entryPointTerminated = true;
2094 builder.leaveFunction();
2095 inEntryPoint = false;
2096 }
2097
2098 return true;
2099 case glslang::EOpParameters:
2100 // Parameters will have been consumed by EOpFunction processing, but not
2101 // the body, so we still visited the function node's children, making this
2102 // child redundant.
2103 return false;
2104 case glslang::EOpFunctionCall:
2105 {
2106 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2107 if (node->isUserDefined())
2108 result = handleUserFunctionCall(node);
2109 // assert(result); // this can happen for bad shaders because the call graph completeness checking is not yet done
2110 if (result) {
2111 builder.clearAccessChain();
2112 builder.setAccessChainRValue(result);
2113 } else
2114 logger->missingFunctionality("missing user function; linker needs to catch that");
2115
2116 return false;
2117 }
2118 case glslang::EOpConstructMat2x2:
2119 case glslang::EOpConstructMat2x3:
2120 case glslang::EOpConstructMat2x4:
2121 case glslang::EOpConstructMat3x2:
2122 case glslang::EOpConstructMat3x3:
2123 case glslang::EOpConstructMat3x4:
2124 case glslang::EOpConstructMat4x2:
2125 case glslang::EOpConstructMat4x3:
2126 case glslang::EOpConstructMat4x4:
2127 case glslang::EOpConstructDMat2x2:
2128 case glslang::EOpConstructDMat2x3:
2129 case glslang::EOpConstructDMat2x4:
2130 case glslang::EOpConstructDMat3x2:
2131 case glslang::EOpConstructDMat3x3:
2132 case glslang::EOpConstructDMat3x4:
2133 case glslang::EOpConstructDMat4x2:
2134 case glslang::EOpConstructDMat4x3:
2135 case glslang::EOpConstructDMat4x4:
2136 case glslang::EOpConstructIMat2x2:
2137 case glslang::EOpConstructIMat2x3:
2138 case glslang::EOpConstructIMat2x4:
2139 case glslang::EOpConstructIMat3x2:
2140 case glslang::EOpConstructIMat3x3:
2141 case glslang::EOpConstructIMat3x4:
2142 case glslang::EOpConstructIMat4x2:
2143 case glslang::EOpConstructIMat4x3:
2144 case glslang::EOpConstructIMat4x4:
2145 case glslang::EOpConstructUMat2x2:
2146 case glslang::EOpConstructUMat2x3:
2147 case glslang::EOpConstructUMat2x4:
2148 case glslang::EOpConstructUMat3x2:
2149 case glslang::EOpConstructUMat3x3:
2150 case glslang::EOpConstructUMat3x4:
2151 case glslang::EOpConstructUMat4x2:
2152 case glslang::EOpConstructUMat4x3:
2153 case glslang::EOpConstructUMat4x4:
2154 case glslang::EOpConstructBMat2x2:
2155 case glslang::EOpConstructBMat2x3:
2156 case glslang::EOpConstructBMat2x4:
2157 case glslang::EOpConstructBMat3x2:
2158 case glslang::EOpConstructBMat3x3:
2159 case glslang::EOpConstructBMat3x4:
2160 case glslang::EOpConstructBMat4x2:
2161 case glslang::EOpConstructBMat4x3:
2162 case glslang::EOpConstructBMat4x4:
2163 case glslang::EOpConstructF16Mat2x2:
2164 case glslang::EOpConstructF16Mat2x3:
2165 case glslang::EOpConstructF16Mat2x4:
2166 case glslang::EOpConstructF16Mat3x2:
2167 case glslang::EOpConstructF16Mat3x3:
2168 case glslang::EOpConstructF16Mat3x4:
2169 case glslang::EOpConstructF16Mat4x2:
2170 case glslang::EOpConstructF16Mat4x3:
2171 case glslang::EOpConstructF16Mat4x4:
2172 isMatrix = true;
2173 // fall through
2174 case glslang::EOpConstructFloat:
2175 case glslang::EOpConstructVec2:
2176 case glslang::EOpConstructVec3:
2177 case glslang::EOpConstructVec4:
2178 case glslang::EOpConstructDouble:
2179 case glslang::EOpConstructDVec2:
2180 case glslang::EOpConstructDVec3:
2181 case glslang::EOpConstructDVec4:
2182 case glslang::EOpConstructFloat16:
2183 case glslang::EOpConstructF16Vec2:
2184 case glslang::EOpConstructF16Vec3:
2185 case glslang::EOpConstructF16Vec4:
2186 case glslang::EOpConstructBool:
2187 case glslang::EOpConstructBVec2:
2188 case glslang::EOpConstructBVec3:
2189 case glslang::EOpConstructBVec4:
2190 case glslang::EOpConstructInt8:
2191 case glslang::EOpConstructI8Vec2:
2192 case glslang::EOpConstructI8Vec3:
2193 case glslang::EOpConstructI8Vec4:
2194 case glslang::EOpConstructUint8:
2195 case glslang::EOpConstructU8Vec2:
2196 case glslang::EOpConstructU8Vec3:
2197 case glslang::EOpConstructU8Vec4:
2198 case glslang::EOpConstructInt16:
2199 case glslang::EOpConstructI16Vec2:
2200 case glslang::EOpConstructI16Vec3:
2201 case glslang::EOpConstructI16Vec4:
2202 case glslang::EOpConstructUint16:
2203 case glslang::EOpConstructU16Vec2:
2204 case glslang::EOpConstructU16Vec3:
2205 case glslang::EOpConstructU16Vec4:
2206 case glslang::EOpConstructInt:
2207 case glslang::EOpConstructIVec2:
2208 case glslang::EOpConstructIVec3:
2209 case glslang::EOpConstructIVec4:
2210 case glslang::EOpConstructUint:
2211 case glslang::EOpConstructUVec2:
2212 case glslang::EOpConstructUVec3:
2213 case glslang::EOpConstructUVec4:
2214 case glslang::EOpConstructInt64:
2215 case glslang::EOpConstructI64Vec2:
2216 case glslang::EOpConstructI64Vec3:
2217 case glslang::EOpConstructI64Vec4:
2218 case glslang::EOpConstructUint64:
2219 case glslang::EOpConstructU64Vec2:
2220 case glslang::EOpConstructU64Vec3:
2221 case glslang::EOpConstructU64Vec4:
2222 case glslang::EOpConstructStruct:
2223 case glslang::EOpConstructTextureSampler:
2224 case glslang::EOpConstructReference:
2225 {
2226 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2227 std::vector<spv::Id> arguments;
2228 translateArguments(*node, arguments);
2229 spv::Id constructed;
2230 if (node->getOp() == glslang::EOpConstructTextureSampler)
2231 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
2232 else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) {
2233 std::vector<spv::Id> constituents;
2234 for (int c = 0; c < (int)arguments.size(); ++c)
2235 constituents.push_back(arguments[c]);
2236 constructed = builder.createCompositeConstruct(resultType(), constituents);
2237 } else if (isMatrix)
2238 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
2239 else
2240 constructed = builder.createConstructor(precision, arguments, resultType());
2241
2242 builder.clearAccessChain();
2243 builder.setAccessChainRValue(constructed);
2244
2245 return false;
2246 }
2247
2248 // These six are component-wise compares with component-wise results.
2249 // Forward on to createBinaryOperation(), requesting a vector result.
2250 case glslang::EOpLessThan:
2251 case glslang::EOpGreaterThan:
2252 case glslang::EOpLessThanEqual:
2253 case glslang::EOpGreaterThanEqual:
2254 case glslang::EOpVectorEqual:
2255 case glslang::EOpVectorNotEqual:
2256 {
2257 // Map the operation to a binary
2258 binOp = node->getOp();
2259 reduceComparison = false;
2260 switch (node->getOp()) {
2261 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
2262 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
2263 default: binOp = node->getOp(); break;
2264 }
2265
2266 break;
2267 }
2268 case glslang::EOpMul:
2269 // component-wise matrix multiply
2270 binOp = glslang::EOpMul;
2271 break;
2272 case glslang::EOpOuterProduct:
2273 // two vectors multiplied to make a matrix
2274 binOp = glslang::EOpOuterProduct;
2275 break;
2276 case glslang::EOpDot:
2277 {
2278 // for scalar dot product, use multiply
2279 glslang::TIntermSequence& glslangOperands = node->getSequence();
2280 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
2281 binOp = glslang::EOpMul;
2282 break;
2283 }
2284 case glslang::EOpMod:
2285 // when an aggregate, this is the floating-point mod built-in function,
2286 // which can be emitted by the one in createBinaryOperation()
2287 binOp = glslang::EOpMod;
2288 break;
2289 case glslang::EOpEmitVertex:
2290 case glslang::EOpEndPrimitive:
2291 case glslang::EOpBarrier:
2292 case glslang::EOpMemoryBarrier:
2293 case glslang::EOpMemoryBarrierAtomicCounter:
2294 case glslang::EOpMemoryBarrierBuffer:
2295 case glslang::EOpMemoryBarrierImage:
2296 case glslang::EOpMemoryBarrierShared:
2297 case glslang::EOpGroupMemoryBarrier:
2298 case glslang::EOpDeviceMemoryBarrier:
2299 case glslang::EOpAllMemoryBarrierWithGroupSync:
2300 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
2301 case glslang::EOpWorkgroupMemoryBarrier:
2302 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
2303 case glslang::EOpSubgroupBarrier:
2304 case glslang::EOpSubgroupMemoryBarrier:
2305 case glslang::EOpSubgroupMemoryBarrierBuffer:
2306 case glslang::EOpSubgroupMemoryBarrierImage:
2307 case glslang::EOpSubgroupMemoryBarrierShared:
2308 noReturnValue = true;
2309 // These all have 0 operands and will naturally finish up in the code below for 0 operands
2310 break;
2311
2312 case glslang::EOpAtomicStore:
2313 noReturnValue = true;
2314 // fallthrough
2315 case glslang::EOpAtomicLoad:
2316 case glslang::EOpAtomicAdd:
2317 case glslang::EOpAtomicMin:
2318 case glslang::EOpAtomicMax:
2319 case glslang::EOpAtomicAnd:
2320 case glslang::EOpAtomicOr:
2321 case glslang::EOpAtomicXor:
2322 case glslang::EOpAtomicExchange:
2323 case glslang::EOpAtomicCompSwap:
2324 atomic = true;
2325 break;
2326
2327 case glslang::EOpAtomicCounterAdd:
2328 case glslang::EOpAtomicCounterSubtract:
2329 case glslang::EOpAtomicCounterMin:
2330 case glslang::EOpAtomicCounterMax:
2331 case glslang::EOpAtomicCounterAnd:
2332 case glslang::EOpAtomicCounterOr:
2333 case glslang::EOpAtomicCounterXor:
2334 case glslang::EOpAtomicCounterExchange:
2335 case glslang::EOpAtomicCounterCompSwap:
2336 builder.addExtension("SPV_KHR_shader_atomic_counter_ops");
2337 builder.addCapability(spv::CapabilityAtomicStorageOps);
2338 atomic = true;
2339 break;
2340
2341 #ifdef NV_EXTENSIONS
2342 case glslang::EOpIgnoreIntersectionNV:
2343 case glslang::EOpTerminateRayNV:
2344 case glslang::EOpTraceNV:
2345 case glslang::EOpExecuteCallableNV:
2346 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
2347 noReturnValue = true;
2348 break;
2349 #endif
2350
2351 default:
2352 break;
2353 }
2354
2355 //
2356 // See if it maps to a regular operation.
2357 //
2358 if (binOp != glslang::EOpNull) {
2359 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
2360 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
2361 assert(left && right);
2362
2363 builder.clearAccessChain();
2364 left->traverse(this);
2365 spv::Id leftId = accessChainLoad(left->getType());
2366
2367 builder.clearAccessChain();
2368 right->traverse(this);
2369 spv::Id rightId = accessChainLoad(right->getType());
2370
2371 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2372 OpDecorations decorations = { precision,
2373 TranslateNoContractionDecoration(node->getType().getQualifier()),
2374 TranslateNonUniformDecoration(node->getType().getQualifier()) };
2375 result = createBinaryOperation(binOp, decorations,
2376 resultType(), leftId, rightId,
2377 left->getType().getBasicType(), reduceComparison);
2378
2379 // code above should only make binOp that exists in createBinaryOperation
2380 assert(result != spv::NoResult);
2381 builder.clearAccessChain();
2382 builder.setAccessChainRValue(result);
2383
2384 return false;
2385 }
2386
2387 //
2388 // Create the list of operands.
2389 //
2390 glslang::TIntermSequence& glslangOperands = node->getSequence();
2391 std::vector<spv::Id> operands;
2392 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
2393 // special case l-value operands; there are just a few
2394 bool lvalue = false;
2395 switch (node->getOp()) {
2396 case glslang::EOpFrexp:
2397 case glslang::EOpModf:
2398 if (arg == 1)
2399 lvalue = true;
2400 break;
2401 case glslang::EOpInterpolateAtSample:
2402 case glslang::EOpInterpolateAtOffset:
2403 #ifdef AMD_EXTENSIONS
2404 case glslang::EOpInterpolateAtVertex:
2405 #endif
2406 if (arg == 0) {
2407 lvalue = true;
2408
2409 // Does it need a swizzle inversion? If so, evaluation is inverted;
2410 // operate first on the swizzle base, then apply the swizzle.
2411 if (glslangOperands[0]->getAsOperator() &&
2412 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2413 invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
2414 }
2415 break;
2416 case glslang::EOpAtomicAdd:
2417 case glslang::EOpAtomicMin:
2418 case glslang::EOpAtomicMax:
2419 case glslang::EOpAtomicAnd:
2420 case glslang::EOpAtomicOr:
2421 case glslang::EOpAtomicXor:
2422 case glslang::EOpAtomicExchange:
2423 case glslang::EOpAtomicCompSwap:
2424 case glslang::EOpAtomicLoad:
2425 case glslang::EOpAtomicStore:
2426 case glslang::EOpAtomicCounterAdd:
2427 case glslang::EOpAtomicCounterSubtract:
2428 case glslang::EOpAtomicCounterMin:
2429 case glslang::EOpAtomicCounterMax:
2430 case glslang::EOpAtomicCounterAnd:
2431 case glslang::EOpAtomicCounterOr:
2432 case glslang::EOpAtomicCounterXor:
2433 case glslang::EOpAtomicCounterExchange:
2434 case glslang::EOpAtomicCounterCompSwap:
2435 if (arg == 0)
2436 lvalue = true;
2437 break;
2438 case glslang::EOpAddCarry:
2439 case glslang::EOpSubBorrow:
2440 if (arg == 2)
2441 lvalue = true;
2442 break;
2443 case glslang::EOpUMulExtended:
2444 case glslang::EOpIMulExtended:
2445 if (arg >= 2)
2446 lvalue = true;
2447 break;
2448 default:
2449 break;
2450 }
2451 builder.clearAccessChain();
2452 if (invertedType != spv::NoType && arg == 0)
2453 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
2454 else
2455 glslangOperands[arg]->traverse(this);
2456 if (lvalue)
2457 operands.push_back(builder.accessChainGetLValue());
2458 else {
2459 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2460 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
2461 }
2462 }
2463
2464 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2465 if (atomic) {
2466 // Handle all atomics
2467 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
2468 } else {
2469 // Pass through to generic operations.
2470 switch (glslangOperands.size()) {
2471 case 0:
2472 result = createNoArgOperation(node->getOp(), precision, resultType());
2473 break;
2474 case 1:
2475 {
2476 OpDecorations decorations = { precision,
2477 TranslateNoContractionDecoration(node->getType().getQualifier()),
2478 TranslateNonUniformDecoration(node->getType().getQualifier()) };
2479 result = createUnaryOperation(
2480 node->getOp(), decorations,
2481 resultType(), operands.front(),
2482 glslangOperands[0]->getAsTyped()->getBasicType());
2483 }
2484 break;
2485 default:
2486 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
2487 break;
2488 }
2489 if (invertedType)
2490 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
2491 }
2492
2493 if (noReturnValue)
2494 return false;
2495
2496 if (! result) {
2497 logger->missingFunctionality("unknown glslang aggregate");
2498 return true; // pick up a child as a placeholder operand
2499 } else {
2500 builder.clearAccessChain();
2501 builder.setAccessChainRValue(result);
2502 return false;
2503 }
2504 }
2505
2506 // This path handles both if-then-else and ?:
2507 // The if-then-else has a node type of void, while
2508 // ?: has either a void or a non-void node type
2509 //
2510 // Leaving the result, when not void:
2511 // GLSL only has r-values as the result of a :?, but
2512 // if we have an l-value, that can be more efficient if it will
2513 // become the base of a complex r-value expression, because the
2514 // next layer copies r-values into memory to use the access-chain mechanism
visitSelection(glslang::TVisit,glslang::TIntermSelection * node)2515 bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
2516 {
2517 // See if it simple and safe, or required, to execute both sides.
2518 // Crucially, side effects must be either semantically required or avoided,
2519 // and there are performance trade-offs.
2520 // Return true if required or a good idea (and safe) to execute both sides,
2521 // false otherwise.
2522 const auto bothSidesPolicy = [&]() -> bool {
2523 // do we have both sides?
2524 if (node->getTrueBlock() == nullptr ||
2525 node->getFalseBlock() == nullptr)
2526 return false;
2527
2528 // required? (unless we write additional code to look for side effects
2529 // and make performance trade-offs if none are present)
2530 if (!node->getShortCircuit())
2531 return true;
2532
2533 // if not required to execute both, decide based on performance/practicality...
2534
2535 // see if OpSelect can handle it
2536 if ((!node->getType().isScalar() && !node->getType().isVector()) ||
2537 node->getBasicType() == glslang::EbtVoid)
2538 return false;
2539
2540 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
2541 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
2542
2543 // return true if a single operand to ? : is okay for OpSelect
2544 const auto operandOkay = [](glslang::TIntermTyped* node) {
2545 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
2546 };
2547
2548 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
2549 operandOkay(node->getFalseBlock()->getAsTyped());
2550 };
2551
2552 spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue
2553 // emit the condition before doing anything with selection
2554 node->getCondition()->traverse(this);
2555 spv::Id condition = accessChainLoad(node->getCondition()->getType());
2556
2557 // Find a way of executing both sides and selecting the right result.
2558 const auto executeBothSides = [&]() -> void {
2559 // execute both sides
2560 node->getTrueBlock()->traverse(this);
2561 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2562 node->getFalseBlock()->traverse(this);
2563 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2564
2565 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2566
2567 // done if void
2568 if (node->getBasicType() == glslang::EbtVoid)
2569 return;
2570
2571 // emit code to select between trueValue and falseValue
2572
2573 // see if OpSelect can handle it
2574 if (node->getType().isScalar() || node->getType().isVector()) {
2575 // Emit OpSelect for this selection.
2576
2577 // smear condition to vector, if necessary (AST is always scalar)
2578 if (builder.isVector(trueValue))
2579 condition = builder.smearScalar(spv::NoPrecision, condition,
2580 builder.makeVectorType(builder.makeBoolType(),
2581 builder.getNumComponents(trueValue)));
2582
2583 // OpSelect
2584 result = builder.createTriOp(spv::OpSelect,
2585 convertGlslangToSpvType(node->getType()), condition,
2586 trueValue, falseValue);
2587
2588 builder.clearAccessChain();
2589 builder.setAccessChainRValue(result);
2590 } else {
2591 // We need control flow to select the result.
2592 // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path.
2593 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
2594
2595 // Selection control:
2596 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2597
2598 // make an "if" based on the value created by the condition
2599 spv::Builder::If ifBuilder(condition, control, builder);
2600
2601 // emit the "then" statement
2602 builder.createStore(trueValue, result);
2603 ifBuilder.makeBeginElse();
2604 // emit the "else" statement
2605 builder.createStore(falseValue, result);
2606
2607 // finish off the control flow
2608 ifBuilder.makeEndIf();
2609
2610 builder.clearAccessChain();
2611 builder.setAccessChainLValue(result);
2612 }
2613 };
2614
2615 // Execute the one side needed, as per the condition
2616 const auto executeOneSide = [&]() {
2617 // Always emit control flow.
2618 if (node->getBasicType() != glslang::EbtVoid)
2619 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
2620
2621 // Selection control:
2622 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2623
2624 // make an "if" based on the value created by the condition
2625 spv::Builder::If ifBuilder(condition, control, builder);
2626
2627 // emit the "then" statement
2628 if (node->getTrueBlock() != nullptr) {
2629 node->getTrueBlock()->traverse(this);
2630 if (result != spv::NoResult)
2631 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
2632 }
2633
2634 if (node->getFalseBlock() != nullptr) {
2635 ifBuilder.makeBeginElse();
2636 // emit the "else" statement
2637 node->getFalseBlock()->traverse(this);
2638 if (result != spv::NoResult)
2639 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
2640 }
2641
2642 // finish off the control flow
2643 ifBuilder.makeEndIf();
2644
2645 if (result != spv::NoResult) {
2646 builder.clearAccessChain();
2647 builder.setAccessChainLValue(result);
2648 }
2649 };
2650
2651 // Try for OpSelect (or a requirement to execute both sides)
2652 if (bothSidesPolicy()) {
2653 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2654 if (node->getType().getQualifier().isSpecConstant())
2655 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2656 executeBothSides();
2657 } else
2658 executeOneSide();
2659
2660 return false;
2661 }
2662
visitSwitch(glslang::TVisit,glslang::TIntermSwitch * node)2663 bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
2664 {
2665 // emit and get the condition before doing anything with switch
2666 node->getCondition()->traverse(this);
2667 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
2668
2669 // Selection control:
2670 const spv::SelectionControlMask control = TranslateSwitchControl(*node);
2671
2672 // browse the children to sort out code segments
2673 int defaultSegment = -1;
2674 std::vector<TIntermNode*> codeSegments;
2675 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
2676 std::vector<int> caseValues;
2677 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
2678 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
2679 TIntermNode* child = *c;
2680 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
2681 defaultSegment = (int)codeSegments.size();
2682 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
2683 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
2684 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst());
2685 } else
2686 codeSegments.push_back(child);
2687 }
2688
2689 // handle the case where the last code segment is missing, due to no code
2690 // statements between the last case and the end of the switch statement
2691 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
2692 (int)codeSegments.size() == defaultSegment)
2693 codeSegments.push_back(nullptr);
2694
2695 // make the switch statement
2696 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
2697 builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks);
2698
2699 // emit all the code in the segments
2700 breakForLoop.push(false);
2701 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
2702 builder.nextSwitchSegment(segmentBlocks, s);
2703 if (codeSegments[s])
2704 codeSegments[s]->traverse(this);
2705 else
2706 builder.addSwitchBreak();
2707 }
2708 breakForLoop.pop();
2709
2710 builder.endSwitch(segmentBlocks);
2711
2712 return false;
2713 }
2714
visitConstantUnion(glslang::TIntermConstantUnion * node)2715 void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
2716 {
2717 int nextConst = 0;
2718 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
2719
2720 builder.clearAccessChain();
2721 builder.setAccessChainRValue(constant);
2722 }
2723
visitLoop(glslang::TVisit,glslang::TIntermLoop * node)2724 bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
2725 {
2726 auto blocks = builder.makeNewLoop();
2727 builder.createBranch(&blocks.head);
2728
2729 // Loop control:
2730 unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite;
2731 const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength);
2732
2733 // Spec requires back edges to target header blocks, and every header block
2734 // must dominate its merge block. Make a header block first to ensure these
2735 // conditions are met. By definition, it will contain OpLoopMerge, followed
2736 // by a block-ending branch. But we don't want to put any other body/test
2737 // instructions in it, since the body/test may have arbitrary instructions,
2738 // including merges of its own.
2739 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2740 builder.setBuildPoint(&blocks.head);
2741 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength);
2742 if (node->testFirst() && node->getTest()) {
2743 spv::Block& test = builder.makeNewBlock();
2744 builder.createBranch(&test);
2745
2746 builder.setBuildPoint(&test);
2747 node->getTest()->traverse(this);
2748 spv::Id condition = accessChainLoad(node->getTest()->getType());
2749 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
2750
2751 builder.setBuildPoint(&blocks.body);
2752 breakForLoop.push(true);
2753 if (node->getBody())
2754 node->getBody()->traverse(this);
2755 builder.createBranch(&blocks.continue_target);
2756 breakForLoop.pop();
2757
2758 builder.setBuildPoint(&blocks.continue_target);
2759 if (node->getTerminal())
2760 node->getTerminal()->traverse(this);
2761 builder.createBranch(&blocks.head);
2762 } else {
2763 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2764 builder.createBranch(&blocks.body);
2765
2766 breakForLoop.push(true);
2767 builder.setBuildPoint(&blocks.body);
2768 if (node->getBody())
2769 node->getBody()->traverse(this);
2770 builder.createBranch(&blocks.continue_target);
2771 breakForLoop.pop();
2772
2773 builder.setBuildPoint(&blocks.continue_target);
2774 if (node->getTerminal())
2775 node->getTerminal()->traverse(this);
2776 if (node->getTest()) {
2777 node->getTest()->traverse(this);
2778 spv::Id condition =
2779 accessChainLoad(node->getTest()->getType());
2780 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
2781 } else {
2782 // TODO: unless there was a break/return/discard instruction
2783 // somewhere in the body, this is an infinite loop, so we should
2784 // issue a warning.
2785 builder.createBranch(&blocks.head);
2786 }
2787 }
2788 builder.setBuildPoint(&blocks.merge);
2789 builder.closeLoop();
2790 return false;
2791 }
2792
visitBranch(glslang::TVisit,glslang::TIntermBranch * node)2793 bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
2794 {
2795 if (node->getExpression())
2796 node->getExpression()->traverse(this);
2797
2798 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
2799
2800 switch (node->getFlowOp()) {
2801 case glslang::EOpKill:
2802 builder.makeDiscard();
2803 break;
2804 case glslang::EOpBreak:
2805 if (breakForLoop.top())
2806 builder.createLoopExit();
2807 else
2808 builder.addSwitchBreak();
2809 break;
2810 case glslang::EOpContinue:
2811 builder.createLoopContinue();
2812 break;
2813 case glslang::EOpReturn:
2814 if (node->getExpression()) {
2815 const glslang::TType& glslangReturnType = node->getExpression()->getType();
2816 spv::Id returnId = accessChainLoad(glslangReturnType);
2817 if (builder.getTypeId(returnId) != currentFunction->getReturnType()) {
2818 builder.clearAccessChain();
2819 spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType());
2820 builder.setAccessChainLValue(copyId);
2821 multiTypeStore(glslangReturnType, returnId);
2822 returnId = builder.createLoad(copyId);
2823 }
2824 builder.makeReturn(false, returnId);
2825 } else
2826 builder.makeReturn(false);
2827
2828 builder.clearAccessChain();
2829 break;
2830
2831 default:
2832 assert(0);
2833 break;
2834 }
2835
2836 return false;
2837 }
2838
createSpvVariable(const glslang::TIntermSymbol * node)2839 spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node)
2840 {
2841 // First, steer off constants, which are not SPIR-V variables, but
2842 // can still have a mapping to a SPIR-V Id.
2843 // This includes specialization constants.
2844 if (node->getQualifier().isConstant()) {
2845 spv::Id result = createSpvConstant(*node);
2846 if (result != spv::NoResult)
2847 return result;
2848 }
2849
2850 // Now, handle actual variables
2851 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
2852 spv::Id spvType = convertGlslangToSpvType(node->getType());
2853
2854 const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) ||
2855 node->getType().containsBasicType(glslang::EbtInt16) ||
2856 node->getType().containsBasicType(glslang::EbtUint16);
2857 if (contains16BitType) {
2858 switch (storageClass) {
2859 case spv::StorageClassInput:
2860 case spv::StorageClassOutput:
2861 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
2862 builder.addCapability(spv::CapabilityStorageInputOutput16);
2863 break;
2864 case spv::StorageClassPushConstant:
2865 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
2866 builder.addCapability(spv::CapabilityStoragePushConstant16);
2867 break;
2868 case spv::StorageClassUniform:
2869 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
2870 if (node->getType().getQualifier().storage == glslang::EvqBuffer)
2871 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
2872 else
2873 builder.addCapability(spv::CapabilityStorageUniform16);
2874 break;
2875 case spv::StorageClassStorageBuffer:
2876 case spv::StorageClassPhysicalStorageBufferEXT:
2877 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
2878 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
2879 break;
2880 default:
2881 break;
2882 }
2883 }
2884
2885 const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) ||
2886 node->getType().containsBasicType(glslang::EbtUint8);
2887 if (contains8BitType) {
2888 if (storageClass == spv::StorageClassPushConstant) {
2889 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2890 builder.addCapability(spv::CapabilityStoragePushConstant8);
2891 } else if (storageClass == spv::StorageClassUniform) {
2892 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2893 builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess);
2894 } else if (storageClass == spv::StorageClassStorageBuffer) {
2895 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2896 builder.addCapability(spv::CapabilityStorageBuffer8BitAccess);
2897 }
2898 }
2899
2900 const char* name = node->getName().c_str();
2901 if (glslang::IsAnonymous(name))
2902 name = "";
2903
2904 return builder.createVariable(storageClass, spvType, name);
2905 }
2906
2907 // Return type Id of the sampled type.
getSampledType(const glslang::TSampler & sampler)2908 spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
2909 {
2910 switch (sampler.type) {
2911 case glslang::EbtFloat: return builder.makeFloatType(32);
2912 #ifdef AMD_EXTENSIONS
2913 case glslang::EbtFloat16:
2914 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch);
2915 builder.addCapability(spv::CapabilityFloat16ImageAMD);
2916 return builder.makeFloatType(16);
2917 #endif
2918 case glslang::EbtInt: return builder.makeIntType(32);
2919 case glslang::EbtUint: return builder.makeUintType(32);
2920 default:
2921 assert(0);
2922 return builder.makeFloatType(32);
2923 }
2924 }
2925
2926 // If node is a swizzle operation, return the type that should be used if
2927 // the swizzle base is first consumed by another operation, before the swizzle
2928 // is applied.
getInvertedSwizzleType(const glslang::TIntermTyped & node)2929 spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
2930 {
2931 if (node.getAsOperator() &&
2932 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2933 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
2934 else
2935 return spv::NoType;
2936 }
2937
2938 // When inverting a swizzle with a parent op, this function
2939 // will apply the swizzle operation to a completed parent operation.
createInvertedSwizzle(spv::Decoration precision,const glslang::TIntermTyped & node,spv::Id parentResult)2940 spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult)
2941 {
2942 std::vector<unsigned> swizzle;
2943 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
2944 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
2945 }
2946
2947 // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
convertSwizzle(const glslang::TIntermAggregate & node,std::vector<unsigned> & swizzle)2948 void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
2949 {
2950 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
2951 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
2952 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
2953 }
2954
2955 // Convert from a glslang type to an SPV type, by calling into a
2956 // recursive version of this function. This establishes the inherited
2957 // layout state rooted from the top-level type.
convertGlslangToSpvType(const glslang::TType & type,bool forwardReferenceOnly)2958 spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
2959 {
2960 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
2961 }
2962
2963 // Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
2964 // explicitLayout can be kept the same throughout the hierarchical recursive walk.
2965 // Mutually recursive with convertGlslangStructToSpvType().
convertGlslangToSpvType(const glslang::TType & type,glslang::TLayoutPacking explicitLayout,const glslang::TQualifier & qualifier,bool lastBufferBlockMember,bool forwardReferenceOnly)2966 spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
2967 glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
2968 bool lastBufferBlockMember, bool forwardReferenceOnly)
2969 {
2970 spv::Id spvType = spv::NoResult;
2971
2972 switch (type.getBasicType()) {
2973 case glslang::EbtVoid:
2974 spvType = builder.makeVoidType();
2975 assert (! type.isArray());
2976 break;
2977 case glslang::EbtFloat:
2978 spvType = builder.makeFloatType(32);
2979 break;
2980 case glslang::EbtDouble:
2981 spvType = builder.makeFloatType(64);
2982 break;
2983 case glslang::EbtFloat16:
2984 spvType = builder.makeFloatType(16);
2985 break;
2986 case glslang::EbtBool:
2987 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
2988 // a 32-bit int where non-0 means true.
2989 if (explicitLayout != glslang::ElpNone)
2990 spvType = builder.makeUintType(32);
2991 else
2992 spvType = builder.makeBoolType();
2993 break;
2994 case glslang::EbtInt8:
2995 spvType = builder.makeIntType(8);
2996 break;
2997 case glslang::EbtUint8:
2998 spvType = builder.makeUintType(8);
2999 break;
3000 case glslang::EbtInt16:
3001 spvType = builder.makeIntType(16);
3002 break;
3003 case glslang::EbtUint16:
3004 spvType = builder.makeUintType(16);
3005 break;
3006 case glslang::EbtInt:
3007 spvType = builder.makeIntType(32);
3008 break;
3009 case glslang::EbtUint:
3010 spvType = builder.makeUintType(32);
3011 break;
3012 case glslang::EbtInt64:
3013 spvType = builder.makeIntType(64);
3014 break;
3015 case glslang::EbtUint64:
3016 spvType = builder.makeUintType(64);
3017 break;
3018 case glslang::EbtAtomicUint:
3019 builder.addCapability(spv::CapabilityAtomicStorage);
3020 spvType = builder.makeUintType(32);
3021 break;
3022 #ifdef NV_EXTENSIONS
3023 case glslang::EbtAccStructNV:
3024 spvType = builder.makeAccelerationStructureNVType();
3025 break;
3026 #endif
3027 case glslang::EbtSampler:
3028 {
3029 const glslang::TSampler& sampler = type.getSampler();
3030 if (sampler.sampler) {
3031 // pure sampler
3032 spvType = builder.makeSamplerType();
3033 } else {
3034 // an image is present, make its type
3035 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms,
3036 sampler.image ? 2 : 1, TranslateImageFormat(type));
3037 if (sampler.combined) {
3038 // already has both image and sampler, make the combined type
3039 spvType = builder.makeSampledImageType(spvType);
3040 }
3041 }
3042 }
3043 break;
3044 case glslang::EbtStruct:
3045 case glslang::EbtBlock:
3046 {
3047 // If we've seen this struct type, return it
3048 const glslang::TTypeList* glslangMembers = type.getStruct();
3049
3050 // Try to share structs for different layouts, but not yet for other
3051 // kinds of qualification (primarily not yet including interpolant qualification).
3052 if (! HasNonLayoutQualifiers(type, qualifier))
3053 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
3054 if (spvType != spv::NoResult)
3055 break;
3056
3057 // else, we haven't seen it...
3058 if (type.getBasicType() == glslang::EbtBlock)
3059 memberRemapper[glslangMembers].resize(glslangMembers->size());
3060 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
3061 }
3062 break;
3063 case glslang::EbtReference:
3064 {
3065 // Make the forward pointer, then recurse to convert the structure type, then
3066 // patch up the forward pointer with a real pointer type.
3067 if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) {
3068 spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT);
3069 forwardPointers[type.getReferentType()] = forwardId;
3070 }
3071 spvType = forwardPointers[type.getReferentType()];
3072 if (!forwardReferenceOnly) {
3073 spv::Id referentType = convertGlslangToSpvType(*type.getReferentType());
3074 builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT,
3075 forwardPointers[type.getReferentType()],
3076 referentType);
3077 }
3078 }
3079 break;
3080 default:
3081 assert(0);
3082 break;
3083 }
3084
3085 if (type.isMatrix())
3086 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
3087 else {
3088 // If this variable has a vector element count greater than 1, create a SPIR-V vector
3089 if (type.getVectorSize() > 1)
3090 spvType = builder.makeVectorType(spvType, type.getVectorSize());
3091 }
3092
3093 if (type.isArray()) {
3094 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
3095
3096 // Do all but the outer dimension
3097 if (type.getArraySizes()->getNumDims() > 1) {
3098 // We need to decorate array strides for types needing explicit layout, except blocks.
3099 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
3100 // Use a dummy glslang type for querying internal strides of
3101 // arrays of arrays, but using just a one-dimensional array.
3102 glslang::TType simpleArrayType(type, 0); // deference type of the array
3103 while (simpleArrayType.getArraySizes()->getNumDims() > 1)
3104 simpleArrayType.getArraySizes()->dereference();
3105
3106 // Will compute the higher-order strides here, rather than making a whole
3107 // pile of types and doing repetitive recursion on their contents.
3108 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
3109 }
3110
3111 // make the arrays
3112 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
3113 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
3114 if (stride > 0)
3115 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
3116 stride *= type.getArraySizes()->getDimSize(dim);
3117 }
3118 } else {
3119 // single-dimensional array, and don't yet have stride
3120
3121 // We need to decorate array strides for types needing explicit layout, except blocks.
3122 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
3123 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
3124 }
3125
3126 // Do the outer dimension, which might not be known for a runtime-sized array.
3127 // (Unsized arrays that survive through linking will be runtime-sized arrays)
3128 if (type.isSizedArray())
3129 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
3130 else {
3131 if (!lastBufferBlockMember) {
3132 builder.addExtension("SPV_EXT_descriptor_indexing");
3133 builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT);
3134 }
3135 spvType = builder.makeRuntimeArray(spvType);
3136 }
3137 if (stride > 0)
3138 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
3139 }
3140
3141 return spvType;
3142 }
3143
3144 // TODO: this functionality should exist at a higher level, in creating the AST
3145 //
3146 // Identify interface members that don't have their required extension turned on.
3147 //
filterMember(const glslang::TType & member)3148 bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
3149 {
3150 #ifdef NV_EXTENSIONS
3151 auto& extensions = glslangIntermediate->getRequestedExtensions();
3152
3153 if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
3154 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3155 return true;
3156 if (member.getFieldName() == "gl_SecondaryPositionNV" &&
3157 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3158 return true;
3159
3160 if (glslangIntermediate->getStage() != EShLangMeshNV) {
3161 if (member.getFieldName() == "gl_ViewportMask" &&
3162 extensions.find("GL_NV_viewport_array2") == extensions.end())
3163 return true;
3164 if (member.getFieldName() == "gl_PositionPerViewNV" &&
3165 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3166 return true;
3167 if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
3168 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3169 return true;
3170 }
3171 #endif
3172
3173 return false;
3174 };
3175
3176 // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
3177 // explicitLayout can be kept the same throughout the hierarchical recursive walk.
3178 // Mutually recursive with convertGlslangToSpvType().
convertGlslangStructToSpvType(const glslang::TType & type,const glslang::TTypeList * glslangMembers,glslang::TLayoutPacking explicitLayout,const glslang::TQualifier & qualifier)3179 spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
3180 const glslang::TTypeList* glslangMembers,
3181 glslang::TLayoutPacking explicitLayout,
3182 const glslang::TQualifier& qualifier)
3183 {
3184 // Create a vector of struct types for SPIR-V to consume
3185 std::vector<spv::Id> spvMembers;
3186 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks
3187 std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers;
3188 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3189 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3190 if (glslangMember.hiddenMember()) {
3191 ++memberDelta;
3192 if (type.getBasicType() == glslang::EbtBlock)
3193 memberRemapper[glslangMembers][i] = -1;
3194 } else {
3195 if (type.getBasicType() == glslang::EbtBlock) {
3196 memberRemapper[glslangMembers][i] = i - memberDelta;
3197 if (filterMember(glslangMember))
3198 continue;
3199 }
3200 // modify just this child's view of the qualifier
3201 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3202 InheritQualifiers(memberQualifier, qualifier);
3203
3204 // manually inherit location
3205 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
3206 memberQualifier.layoutLocation = qualifier.layoutLocation;
3207
3208 // recurse
3209 bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer &&
3210 i == (int)glslangMembers->size() - 1;
3211
3212 // Make forward pointers for any pointer members, and create a list of members to
3213 // convert to spirv types after creating the struct.
3214 if (glslangMember.getBasicType() == glslang::EbtReference) {
3215 if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) {
3216 deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier));
3217 }
3218 spvMembers.push_back(
3219 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true));
3220 } else {
3221 spvMembers.push_back(
3222 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false));
3223 }
3224 }
3225 }
3226
3227 // Make the SPIR-V type
3228 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
3229 if (! HasNonLayoutQualifiers(type, qualifier))
3230 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
3231
3232 // Decorate it
3233 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
3234
3235 for (int i = 0; i < deferredForwardPointers.size(); ++i) {
3236 auto it = deferredForwardPointers[i];
3237 convertGlslangToSpvType(*it.first, explicitLayout, it.second, false);
3238 }
3239
3240 return spvType;
3241 }
3242
decorateStructType(const glslang::TType & type,const glslang::TTypeList * glslangMembers,glslang::TLayoutPacking explicitLayout,const glslang::TQualifier & qualifier,spv::Id spvType)3243 void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
3244 const glslang::TTypeList* glslangMembers,
3245 glslang::TLayoutPacking explicitLayout,
3246 const glslang::TQualifier& qualifier,
3247 spv::Id spvType)
3248 {
3249 // Name and decorate the non-hidden members
3250 int offset = -1;
3251 int locationOffset = 0; // for use within the members of this struct
3252 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3253 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3254 int member = i;
3255 if (type.getBasicType() == glslang::EbtBlock) {
3256 member = memberRemapper[glslangMembers][i];
3257 if (filterMember(glslangMember))
3258 continue;
3259 }
3260
3261 // modify just this child's view of the qualifier
3262 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3263 InheritQualifiers(memberQualifier, qualifier);
3264
3265 // using -1 above to indicate a hidden member
3266 if (member < 0)
3267 continue;
3268
3269 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
3270 builder.addMemberDecoration(spvType, member,
3271 TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
3272 builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
3273 // Add interpolation and auxiliary storage decorations only to
3274 // top-level members of Input and Output storage classes
3275 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
3276 type.getQualifier().storage == glslang::EvqVaryingOut) {
3277 if (type.getBasicType() == glslang::EbtBlock ||
3278 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
3279 builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
3280 builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
3281 #ifdef NV_EXTENSIONS
3282 addMeshNVDecoration(spvType, member, memberQualifier);
3283 #endif
3284 }
3285 }
3286 builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
3287
3288 if (type.getBasicType() == glslang::EbtBlock &&
3289 qualifier.storage == glslang::EvqBuffer) {
3290 // Add memory decorations only to top-level members of shader storage block
3291 std::vector<spv::Decoration> memory;
3292 TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel());
3293 for (unsigned int i = 0; i < memory.size(); ++i)
3294 builder.addMemberDecoration(spvType, member, memory[i]);
3295 }
3296
3297 // Location assignment was already completed correctly by the front end,
3298 // just track whether a member needs to be decorated.
3299 // Ignore member locations if the container is an array, as that's
3300 // ill-specified and decisions have been made to not allow this.
3301 if (! type.isArray() && memberQualifier.hasLocation())
3302 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation);
3303
3304 if (qualifier.hasLocation()) // track for upcoming inheritance
3305 locationOffset += glslangIntermediate->computeTypeLocationSize(
3306 glslangMember, glslangIntermediate->getStage());
3307
3308 // component, XFB, others
3309 if (glslangMember.getQualifier().hasComponent())
3310 builder.addMemberDecoration(spvType, member, spv::DecorationComponent,
3311 glslangMember.getQualifier().layoutComponent);
3312 if (glslangMember.getQualifier().hasXfbOffset())
3313 builder.addMemberDecoration(spvType, member, spv::DecorationOffset,
3314 glslangMember.getQualifier().layoutXfbOffset);
3315 else if (explicitLayout != glslang::ElpNone) {
3316 // figure out what to do with offset, which is accumulating
3317 int nextOffset;
3318 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
3319 if (offset >= 0)
3320 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
3321 offset = nextOffset;
3322 }
3323
3324 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
3325 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride,
3326 getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
3327
3328 // built-in variable decorations
3329 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
3330 if (builtIn != spv::BuiltInMax)
3331 builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
3332
3333 // nonuniform
3334 builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier()));
3335
3336 if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) {
3337 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
3338 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
3339 memberQualifier.semanticName);
3340 }
3341
3342 #ifdef NV_EXTENSIONS
3343 if (builtIn == spv::BuiltInLayer) {
3344 // SPV_NV_viewport_array2 extension
3345 if (glslangMember.getQualifier().layoutViewportRelative){
3346 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
3347 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
3348 builder.addExtension(spv::E_SPV_NV_viewport_array2);
3349 }
3350 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
3351 builder.addMemberDecoration(spvType, member,
3352 (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
3353 glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
3354 builder.addCapability(spv::CapabilityShaderStereoViewNV);
3355 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
3356 }
3357 }
3358 if (glslangMember.getQualifier().layoutPassthrough) {
3359 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
3360 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
3361 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
3362 }
3363 #endif
3364 }
3365
3366 // Decorate the structure
3367 builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
3368 builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer()));
3369 }
3370
3371 // Turn the expression forming the array size into an id.
3372 // This is not quite trivial, because of specialization constants.
3373 // Sometimes, a raw constant is turned into an Id, and sometimes
3374 // a specialization constant expression is.
makeArraySizeId(const glslang::TArraySizes & arraySizes,int dim)3375 spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
3376 {
3377 // First, see if this is sized with a node, meaning a specialization constant:
3378 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
3379 if (specNode != nullptr) {
3380 builder.clearAccessChain();
3381 specNode->traverse(this);
3382 return accessChainLoad(specNode->getAsTyped()->getType());
3383 }
3384
3385 // Otherwise, need a compile-time (front end) size, get it:
3386 int size = arraySizes.getDimSize(dim);
3387 assert(size > 0);
3388 return builder.makeUintConstant(size);
3389 }
3390
3391 // Wrap the builder's accessChainLoad to:
3392 // - localize handling of RelaxedPrecision
3393 // - use the SPIR-V inferred type instead of another conversion of the glslang type
3394 // (avoids unnecessary work and possible type punning for structures)
3395 // - do conversion of concrete to abstract type
accessChainLoad(const glslang::TType & type)3396 spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
3397 {
3398 spv::Id nominalTypeId = builder.accessChainGetInferredType();
3399
3400 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3401 coherentFlags |= TranslateCoherent(type);
3402
3403 unsigned int alignment = builder.getAccessChain().alignment;
3404 alignment |= getBufferReferenceAlignment(type);
3405
3406 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type),
3407 TranslateNonUniformDecoration(type.getQualifier()),
3408 nominalTypeId,
3409 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask),
3410 TranslateMemoryScope(coherentFlags),
3411 alignment);
3412
3413 // Need to convert to abstract types when necessary
3414 if (type.getBasicType() == glslang::EbtBool) {
3415 if (builder.isScalarType(nominalTypeId)) {
3416 // Conversion for bool
3417 spv::Id boolType = builder.makeBoolType();
3418 if (nominalTypeId != boolType)
3419 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
3420 } else if (builder.isVectorType(nominalTypeId)) {
3421 // Conversion for bvec
3422 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3423 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
3424 if (nominalTypeId != bvecType)
3425 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize));
3426 }
3427 }
3428
3429 return loadedId;
3430 }
3431
3432 // Wrap the builder's accessChainStore to:
3433 // - do conversion of concrete to abstract type
3434 //
3435 // Implicitly uses the existing builder.accessChain as the storage target.
accessChainStore(const glslang::TType & type,spv::Id rvalue)3436 void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
3437 {
3438 // Need to convert to abstract types when necessary
3439 if (type.getBasicType() == glslang::EbtBool) {
3440 spv::Id nominalTypeId = builder.accessChainGetInferredType();
3441
3442 if (builder.isScalarType(nominalTypeId)) {
3443 // Conversion for bool
3444 spv::Id boolType = builder.makeBoolType();
3445 if (nominalTypeId != boolType) {
3446 // keep these outside arguments, for determinant order-of-evaluation
3447 spv::Id one = builder.makeUintConstant(1);
3448 spv::Id zero = builder.makeUintConstant(0);
3449 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
3450 } else if (builder.getTypeId(rvalue) != boolType)
3451 rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0));
3452 } else if (builder.isVectorType(nominalTypeId)) {
3453 // Conversion for bvec
3454 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3455 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
3456 if (nominalTypeId != bvecType) {
3457 // keep these outside arguments, for determinant order-of-evaluation
3458 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
3459 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
3460 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
3461 } else if (builder.getTypeId(rvalue) != bvecType)
3462 rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue,
3463 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
3464 }
3465 }
3466
3467 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3468 coherentFlags |= TranslateCoherent(type);
3469
3470 unsigned int alignment = builder.getAccessChain().alignment;
3471 alignment |= getBufferReferenceAlignment(type);
3472
3473 builder.accessChainStore(rvalue,
3474 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask),
3475 TranslateMemoryScope(coherentFlags), alignment);
3476 }
3477
3478 // For storing when types match at the glslang level, but not might match at the
3479 // SPIR-V level.
3480 //
3481 // This especially happens when a single glslang type expands to multiple
3482 // SPIR-V types, like a struct that is used in a member-undecorated way as well
3483 // as in a member-decorated way.
3484 //
3485 // NOTE: This function can handle any store request; if it's not special it
3486 // simplifies to a simple OpStore.
3487 //
3488 // Implicitly uses the existing builder.accessChain as the storage target.
multiTypeStore(const glslang::TType & type,spv::Id rValue)3489 void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
3490 {
3491 // we only do the complex path here if it's an aggregate
3492 if (! type.isStruct() && ! type.isArray()) {
3493 accessChainStore(type, rValue);
3494 return;
3495 }
3496
3497 // and, it has to be a case of type aliasing
3498 spv::Id rType = builder.getTypeId(rValue);
3499 spv::Id lValue = builder.accessChainGetLValue();
3500 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
3501 if (lType == rType) {
3502 accessChainStore(type, rValue);
3503 return;
3504 }
3505
3506 // Recursively (as needed) copy an aggregate type to a different aggregate type,
3507 // where the two types were the same type in GLSL. This requires member
3508 // by member copy, recursively.
3509
3510 // If an array, copy element by element.
3511 if (type.isArray()) {
3512 glslang::TType glslangElementType(type, 0);
3513 spv::Id elementRType = builder.getContainedTypeId(rType);
3514 for (int index = 0; index < type.getOuterArraySize(); ++index) {
3515 // get the source member
3516 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
3517
3518 // set up the target storage
3519 builder.clearAccessChain();
3520 builder.setAccessChainLValue(lValue);
3521 builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), getBufferReferenceAlignment(type));
3522
3523 // store the member
3524 multiTypeStore(glslangElementType, elementRValue);
3525 }
3526 } else {
3527 assert(type.isStruct());
3528
3529 // loop over structure members
3530 const glslang::TTypeList& members = *type.getStruct();
3531 for (int m = 0; m < (int)members.size(); ++m) {
3532 const glslang::TType& glslangMemberType = *members[m].type;
3533
3534 // get the source member
3535 spv::Id memberRType = builder.getContainedTypeId(rType, m);
3536 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
3537
3538 // set up the target storage
3539 builder.clearAccessChain();
3540 builder.setAccessChainLValue(lValue);
3541 builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), getBufferReferenceAlignment(type));
3542
3543 // store the member
3544 multiTypeStore(glslangMemberType, memberRValue);
3545 }
3546 }
3547 }
3548
3549 // Decide whether or not this type should be
3550 // decorated with offsets and strides, and if so
3551 // whether std140 or std430 rules should be applied.
getExplicitLayout(const glslang::TType & type) const3552 glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
3553 {
3554 // has to be a block
3555 if (type.getBasicType() != glslang::EbtBlock)
3556 return glslang::ElpNone;
3557
3558 // has to be a uniform or buffer block or task in/out blocks
3559 if (type.getQualifier().storage != glslang::EvqUniform &&
3560 type.getQualifier().storage != glslang::EvqBuffer &&
3561 !type.getQualifier().isTaskMemory())
3562 return glslang::ElpNone;
3563
3564 // return the layout to use
3565 switch (type.getQualifier().layoutPacking) {
3566 case glslang::ElpStd140:
3567 case glslang::ElpStd430:
3568 case glslang::ElpScalar:
3569 return type.getQualifier().layoutPacking;
3570 default:
3571 return glslang::ElpNone;
3572 }
3573 }
3574
3575 // Given an array type, returns the integer stride required for that array
getArrayStride(const glslang::TType & arrayType,glslang::TLayoutPacking explicitLayout,glslang::TLayoutMatrix matrixLayout)3576 int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
3577 {
3578 int size;
3579 int stride;
3580 glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
3581
3582 return stride;
3583 }
3584
3585 // Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix
3586 // when used as a member of an interface block
getMatrixStride(const glslang::TType & matrixType,glslang::TLayoutPacking explicitLayout,glslang::TLayoutMatrix matrixLayout)3587 int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
3588 {
3589 glslang::TType elementType;
3590 elementType.shallowCopy(matrixType);
3591 elementType.clearArraySizes();
3592
3593 int size;
3594 int stride;
3595 glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
3596
3597 return stride;
3598 }
3599
3600 // Given a member type of a struct, realign the current offset for it, and compute
3601 // the next (not yet aligned) offset for the next member, which will get aligned
3602 // on the next call.
3603 // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
3604 // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
3605 // -1 means a non-forced member offset (no decoration needed).
updateMemberOffset(const glslang::TType & structType,const glslang::TType & memberType,int & currentOffset,int & nextOffset,glslang::TLayoutPacking explicitLayout,glslang::TLayoutMatrix matrixLayout)3606 void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset,
3607 glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
3608 {
3609 // this will get a positive value when deemed necessary
3610 nextOffset = -1;
3611
3612 // override anything in currentOffset with user-set offset
3613 if (memberType.getQualifier().hasOffset())
3614 currentOffset = memberType.getQualifier().layoutOffset;
3615
3616 // It could be that current linker usage in glslang updated all the layoutOffset,
3617 // in which case the following code does not matter. But, that's not quite right
3618 // once cross-compilation unit GLSL validation is done, as the original user
3619 // settings are needed in layoutOffset, and then the following will come into play.
3620
3621 if (explicitLayout == glslang::ElpNone) {
3622 if (! memberType.getQualifier().hasOffset())
3623 currentOffset = -1;
3624
3625 return;
3626 }
3627
3628 // Getting this far means we need explicit offsets
3629 if (currentOffset < 0)
3630 currentOffset = 0;
3631
3632 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
3633 // but possibly not yet correctly aligned.
3634
3635 int memberSize;
3636 int dummyStride;
3637 int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
3638
3639 // Adjust alignment for HLSL rules
3640 // TODO: make this consistent in early phases of code:
3641 // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
3642 // Until reflection is brought in sync with these adjustments, don't apply to $Global,
3643 // which is the most likely to rely on reflection, and least likely to rely implicit layouts
3644 if (glslangIntermediate->usingHlslOffsets() &&
3645 ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
3646 int dummySize;
3647 int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
3648 if (componentAlignment <= 4)
3649 memberAlignment = componentAlignment;
3650 }
3651
3652 // Bump up to member alignment
3653 glslang::RoundToPow2(currentOffset, memberAlignment);
3654
3655 // Bump up to vec4 if there is a bad straddle
3656 if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset))
3657 glslang::RoundToPow2(currentOffset, 16);
3658
3659 nextOffset = currentOffset + memberSize;
3660 }
3661
declareUseOfStructMember(const glslang::TTypeList & members,int glslangMember)3662 void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
3663 {
3664 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
3665 switch (glslangBuiltIn)
3666 {
3667 case glslang::EbvClipDistance:
3668 case glslang::EbvCullDistance:
3669 case glslang::EbvPointSize:
3670 #ifdef NV_EXTENSIONS
3671 case glslang::EbvViewportMaskNV:
3672 case glslang::EbvSecondaryPositionNV:
3673 case glslang::EbvSecondaryViewportMaskNV:
3674 case glslang::EbvPositionPerViewNV:
3675 case glslang::EbvViewportMaskPerViewNV:
3676 case glslang::EbvTaskCountNV:
3677 case glslang::EbvPrimitiveCountNV:
3678 case glslang::EbvPrimitiveIndicesNV:
3679 case glslang::EbvClipDistancePerViewNV:
3680 case glslang::EbvCullDistancePerViewNV:
3681 case glslang::EbvLayerPerViewNV:
3682 case glslang::EbvMeshViewCountNV:
3683 case glslang::EbvMeshViewIndicesNV:
3684 #endif
3685 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
3686 // Alternately, we could just call this for any glslang built-in, since the
3687 // capability already guards against duplicates.
3688 TranslateBuiltInDecoration(glslangBuiltIn, false);
3689 break;
3690 default:
3691 // Capabilities were already generated when the struct was declared.
3692 break;
3693 }
3694 }
3695
isShaderEntryPoint(const glslang::TIntermAggregate * node)3696 bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
3697 {
3698 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
3699 }
3700
3701 // Does parameter need a place to keep writes, separate from the original?
3702 // Assumes called after originalParam(), which filters out block/buffer/opaque-based
3703 // qualifiers such that we should have only in/out/inout/constreadonly here.
writableParam(glslang::TStorageQualifier qualifier) const3704 bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
3705 {
3706 assert(qualifier == glslang::EvqIn ||
3707 qualifier == glslang::EvqOut ||
3708 qualifier == glslang::EvqInOut ||
3709 qualifier == glslang::EvqConstReadOnly);
3710 return qualifier != glslang::EvqConstReadOnly;
3711 }
3712
3713 // Is parameter pass-by-original?
originalParam(glslang::TStorageQualifier qualifier,const glslang::TType & paramType,bool implicitThisParam)3714 bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
3715 bool implicitThisParam)
3716 {
3717 if (implicitThisParam) // implicit this
3718 return true;
3719 if (glslangIntermediate->getSource() == glslang::EShSourceHlsl)
3720 return paramType.getBasicType() == glslang::EbtBlock;
3721 return paramType.containsOpaque() || // sampler, etc.
3722 (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO
3723 }
3724
3725 // Make all the functions, skeletally, without actually visiting their bodies.
makeFunctions(const glslang::TIntermSequence & glslFunctions)3726 void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
3727 {
3728 const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, bool useVulkanMemoryModel) {
3729 spv::Decoration paramPrecision = TranslatePrecisionDecoration(type);
3730 if (paramPrecision != spv::NoPrecision)
3731 decorations.push_back(paramPrecision);
3732 TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel);
3733 if (type.getBasicType() == glslang::EbtReference) {
3734 // Original and non-writable params pass the pointer directly and
3735 // use restrict/aliased, others are stored to a pointer in Function
3736 // memory and use RestrictPointer/AliasedPointer.
3737 if (originalParam(type.getQualifier().storage, type, false) ||
3738 !writableParam(type.getQualifier().storage)) {
3739 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased);
3740 } else {
3741 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
3742 }
3743 }
3744 };
3745
3746 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
3747 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
3748 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
3749 continue;
3750
3751 // We're on a user function. Set up the basic interface for the function now,
3752 // so that it's available to call. Translating the body will happen later.
3753 //
3754 // Typically (except for a "const in" parameter), an address will be passed to the
3755 // function. What it is an address of varies:
3756 //
3757 // - "in" parameters not marked as "const" can be written to without modifying the calling
3758 // argument so that write needs to be to a copy, hence the address of a copy works.
3759 //
3760 // - "const in" parameters can just be the r-value, as no writes need occur.
3761 //
3762 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
3763 // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy.
3764
3765 std::vector<spv::Id> paramTypes;
3766 std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter
3767 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
3768
3769 bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() ==
3770 glslangIntermediate->implicitThisName;
3771
3772 paramDecorations.resize(parameters.size());
3773 for (int p = 0; p < (int)parameters.size(); ++p) {
3774 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
3775 spv::Id typeId = convertGlslangToSpvType(paramType);
3776 if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0))
3777 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
3778 else if (writableParam(paramType.getQualifier().storage))
3779 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
3780 else
3781 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
3782 getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel());
3783 paramTypes.push_back(typeId);
3784 }
3785
3786 spv::Block* functionBlock;
3787 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
3788 convertGlslangToSpvType(glslFunction->getType()),
3789 glslFunction->getName().c_str(), paramTypes,
3790 paramDecorations, &functionBlock);
3791 if (implicitThis)
3792 function->setImplicitThis();
3793
3794 // Track function to emit/call later
3795 functionMap[glslFunction->getName().c_str()] = function;
3796
3797 // Set the parameter id's
3798 for (int p = 0; p < (int)parameters.size(); ++p) {
3799 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
3800 // give a name too
3801 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
3802 }
3803 }
3804 }
3805
3806 // Process all the initializers, while skipping the functions and link objects
makeGlobalInitializers(const glslang::TIntermSequence & initializers)3807 void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
3808 {
3809 builder.setBuildPoint(shaderEntry->getLastBlock());
3810 for (int i = 0; i < (int)initializers.size(); ++i) {
3811 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
3812 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) {
3813
3814 // We're on a top-level node that's not a function. Treat as an initializer, whose
3815 // code goes into the beginning of the entry point.
3816 initializer->traverse(this);
3817 }
3818 }
3819 }
3820
3821 // Process all the functions, while skipping initializers.
visitFunctions(const glslang::TIntermSequence & glslFunctions)3822 void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
3823 {
3824 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
3825 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
3826 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
3827 node->traverse(this);
3828 }
3829 }
3830
handleFunctionEntry(const glslang::TIntermAggregate * node)3831 void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
3832 {
3833 // SPIR-V functions should already be in the functionMap from the prepass
3834 // that called makeFunctions().
3835 currentFunction = functionMap[node->getName().c_str()];
3836 spv::Block* functionBlock = currentFunction->getEntryBlock();
3837 builder.setBuildPoint(functionBlock);
3838 }
3839
translateArguments(const glslang::TIntermAggregate & node,std::vector<spv::Id> & arguments)3840 void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments)
3841 {
3842 const glslang::TIntermSequence& glslangArguments = node.getSequence();
3843
3844 glslang::TSampler sampler = {};
3845 bool cubeCompare = false;
3846 #ifdef AMD_EXTENSIONS
3847 bool f16ShadowCompare = false;
3848 #endif
3849 if (node.isTexture() || node.isImage()) {
3850 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
3851 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
3852 #ifdef AMD_EXTENSIONS
3853 f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16;
3854 #endif
3855 }
3856
3857 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
3858 builder.clearAccessChain();
3859 glslangArguments[i]->traverse(this);
3860
3861 // Special case l-value operands
3862 bool lvalue = false;
3863 switch (node.getOp()) {
3864 case glslang::EOpImageAtomicAdd:
3865 case glslang::EOpImageAtomicMin:
3866 case glslang::EOpImageAtomicMax:
3867 case glslang::EOpImageAtomicAnd:
3868 case glslang::EOpImageAtomicOr:
3869 case glslang::EOpImageAtomicXor:
3870 case glslang::EOpImageAtomicExchange:
3871 case glslang::EOpImageAtomicCompSwap:
3872 case glslang::EOpImageAtomicLoad:
3873 case glslang::EOpImageAtomicStore:
3874 if (i == 0)
3875 lvalue = true;
3876 break;
3877 case glslang::EOpSparseImageLoad:
3878 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
3879 lvalue = true;
3880 break;
3881 #ifdef AMD_EXTENSIONS
3882 case glslang::EOpSparseTexture:
3883 if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2))
3884 lvalue = true;
3885 break;
3886 case glslang::EOpSparseTextureClamp:
3887 if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3))
3888 lvalue = true;
3889 break;
3890 case glslang::EOpSparseTextureLod:
3891 case glslang::EOpSparseTextureOffset:
3892 if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3))
3893 lvalue = true;
3894 break;
3895 #else
3896 case glslang::EOpSparseTexture:
3897 if ((cubeCompare && i == 3) || (! cubeCompare && i == 2))
3898 lvalue = true;
3899 break;
3900 case glslang::EOpSparseTextureClamp:
3901 if ((cubeCompare && i == 4) || (! cubeCompare && i == 3))
3902 lvalue = true;
3903 break;
3904 case glslang::EOpSparseTextureLod:
3905 case glslang::EOpSparseTextureOffset:
3906 if (i == 3)
3907 lvalue = true;
3908 break;
3909 #endif
3910 case glslang::EOpSparseTextureFetch:
3911 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
3912 lvalue = true;
3913 break;
3914 case glslang::EOpSparseTextureFetchOffset:
3915 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
3916 lvalue = true;
3917 break;
3918 #ifdef AMD_EXTENSIONS
3919 case glslang::EOpSparseTextureLodOffset:
3920 case glslang::EOpSparseTextureGrad:
3921 case glslang::EOpSparseTextureOffsetClamp:
3922 if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4))
3923 lvalue = true;
3924 break;
3925 case glslang::EOpSparseTextureGradOffset:
3926 case glslang::EOpSparseTextureGradClamp:
3927 if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5))
3928 lvalue = true;
3929 break;
3930 case glslang::EOpSparseTextureGradOffsetClamp:
3931 if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6))
3932 lvalue = true;
3933 break;
3934 #else
3935 case glslang::EOpSparseTextureLodOffset:
3936 case glslang::EOpSparseTextureGrad:
3937 case glslang::EOpSparseTextureOffsetClamp:
3938 if (i == 4)
3939 lvalue = true;
3940 break;
3941 case glslang::EOpSparseTextureGradOffset:
3942 case glslang::EOpSparseTextureGradClamp:
3943 if (i == 5)
3944 lvalue = true;
3945 break;
3946 case glslang::EOpSparseTextureGradOffsetClamp:
3947 if (i == 6)
3948 lvalue = true;
3949 break;
3950 #endif
3951 case glslang::EOpSparseTextureGather:
3952 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
3953 lvalue = true;
3954 break;
3955 case glslang::EOpSparseTextureGatherOffset:
3956 case glslang::EOpSparseTextureGatherOffsets:
3957 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
3958 lvalue = true;
3959 break;
3960 #ifdef AMD_EXTENSIONS
3961 case glslang::EOpSparseTextureGatherLod:
3962 if (i == 3)
3963 lvalue = true;
3964 break;
3965 case glslang::EOpSparseTextureGatherLodOffset:
3966 case glslang::EOpSparseTextureGatherLodOffsets:
3967 if (i == 4)
3968 lvalue = true;
3969 break;
3970 case glslang::EOpSparseImageLoadLod:
3971 if (i == 3)
3972 lvalue = true;
3973 break;
3974 #endif
3975 #ifdef NV_EXTENSIONS
3976 case glslang::EOpImageSampleFootprintNV:
3977 if (i == 4)
3978 lvalue = true;
3979 break;
3980 case glslang::EOpImageSampleFootprintClampNV:
3981 case glslang::EOpImageSampleFootprintLodNV:
3982 if (i == 5)
3983 lvalue = true;
3984 break;
3985 case glslang::EOpImageSampleFootprintGradNV:
3986 if (i == 6)
3987 lvalue = true;
3988 break;
3989 case glslang::EOpImageSampleFootprintGradClampNV:
3990 if (i == 7)
3991 lvalue = true;
3992 break;
3993 #endif
3994 default:
3995 break;
3996 }
3997
3998 if (lvalue)
3999 arguments.push_back(builder.accessChainGetLValue());
4000 else
4001 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
4002 }
4003 }
4004
translateArguments(glslang::TIntermUnary & node,std::vector<spv::Id> & arguments)4005 void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
4006 {
4007 builder.clearAccessChain();
4008 node.getOperand()->traverse(this);
4009 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
4010 }
4011
createImageTextureFunctionCall(glslang::TIntermOperator * node)4012 spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
4013 {
4014 if (! node->isImage() && ! node->isTexture())
4015 return spv::NoResult;
4016
4017 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
4018
4019 // Process a GLSL texturing op (will be SPV image)
4020
4021 const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType()
4022 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType();
4023 const glslang::TSampler sampler = imageType.getSampler();
4024 #ifdef AMD_EXTENSIONS
4025 bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate())
4026 ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16
4027 : false;
4028 #endif
4029
4030 std::vector<spv::Id> arguments;
4031 if (node->getAsAggregate())
4032 translateArguments(*node->getAsAggregate(), arguments);
4033 else
4034 translateArguments(*node->getAsUnaryNode(), arguments);
4035 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
4036
4037 spv::Builder::TextureParameters params = { };
4038 params.sampler = arguments[0];
4039
4040 glslang::TCrackedTextureOp cracked;
4041 node->crackTexture(sampler, cracked);
4042
4043 const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint;
4044
4045 // Check for queries
4046 if (cracked.query) {
4047 // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first
4048 if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler))
4049 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4050
4051 switch (node->getOp()) {
4052 case glslang::EOpImageQuerySize:
4053 case glslang::EOpTextureQuerySize:
4054 if (arguments.size() > 1) {
4055 params.lod = arguments[1];
4056 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult);
4057 } else
4058 return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult);
4059 case glslang::EOpImageQuerySamples:
4060 case glslang::EOpTextureQuerySamples:
4061 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult);
4062 case glslang::EOpTextureQueryLod:
4063 params.coords = arguments[1];
4064 return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult);
4065 case glslang::EOpTextureQueryLevels:
4066 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult);
4067 case glslang::EOpSparseTexelsResident:
4068 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
4069 default:
4070 assert(0);
4071 break;
4072 }
4073 }
4074
4075 int components = node->getType().getVectorSize();
4076
4077 if (node->getOp() == glslang::EOpTextureFetch) {
4078 // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed.
4079 // This will only happen through the HLSL path for operator[], so we do not have to handle e.g.
4080 // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic
4081 // here around e.g. which ones return scalars or other types.
4082 components = 4;
4083 }
4084
4085 glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components);
4086
4087 auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); };
4088
4089 // Check for image functions other than queries
4090 if (node->isImage()) {
4091 std::vector<spv::IdImmediate> operands;
4092 auto opIt = arguments.begin();
4093 spv::IdImmediate image = { true, *(opIt++) };
4094 operands.push_back(image);
4095
4096 // Handle subpass operations
4097 // TODO: GLSL should change to have the "MS" only on the type rather than the
4098 // built-in function.
4099 if (cracked.subpass) {
4100 // add on the (0,0) coordinate
4101 spv::Id zero = builder.makeIntConstant(0);
4102 std::vector<spv::Id> comps;
4103 comps.push_back(zero);
4104 comps.push_back(zero);
4105 spv::IdImmediate coord = { true,
4106 builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) };
4107 operands.push_back(coord);
4108 if (sampler.ms) {
4109 spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask };
4110 operands.push_back(imageOperands);
4111 spv::IdImmediate imageOperand = { true, *(opIt++) };
4112 operands.push_back(imageOperand);
4113 }
4114 spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands);
4115 builder.setPrecision(result, precision);
4116 return result;
4117 }
4118
4119 spv::IdImmediate coord = { true, *(opIt++) };
4120 operands.push_back(coord);
4121 #ifdef AMD_EXTENSIONS
4122 if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) {
4123 #else
4124 if (node->getOp() == glslang::EOpImageLoad) {
4125 #endif
4126 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
4127 if (sampler.ms) {
4128 mask = mask | spv::ImageOperandsSampleMask;
4129 }
4130 #ifdef AMD_EXTENSIONS
4131 if (cracked.lod) {
4132 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4133 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4134 mask = mask | spv::ImageOperandsLodMask;
4135 }
4136 #endif
4137 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4138 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
4139 if (mask) {
4140 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4141 operands.push_back(imageOperands);
4142 }
4143 if (mask & spv::ImageOperandsSampleMask) {
4144 spv::IdImmediate imageOperand = { true, *opIt++ };
4145 operands.push_back(imageOperand);
4146 }
4147 #ifdef AMD_EXTENSIONS
4148 if (mask & spv::ImageOperandsLodMask) {
4149 spv::IdImmediate imageOperand = { true, *opIt++ };
4150 operands.push_back(imageOperand);
4151 }
4152 #endif
4153 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
4154 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4155 operands.push_back(imageOperand);
4156 }
4157
4158 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
4159 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
4160
4161 std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands));
4162 builder.setPrecision(result[0], precision);
4163
4164 // If needed, add a conversion constructor to the proper size.
4165 if (components != node->getType().getVectorSize())
4166 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4167
4168 return result[0];
4169 #ifdef AMD_EXTENSIONS
4170 } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) {
4171 #else
4172 } else if (node->getOp() == glslang::EOpImageStore) {
4173 #endif
4174
4175 // Push the texel value before the operands
4176 #ifdef AMD_EXTENSIONS
4177 if (sampler.ms || cracked.lod) {
4178 #else
4179 if (sampler.ms) {
4180 #endif
4181 spv::IdImmediate texel = { true, *(opIt + 1) };
4182 operands.push_back(texel);
4183 } else {
4184 spv::IdImmediate texel = { true, *opIt };
4185 operands.push_back(texel);
4186 }
4187
4188 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
4189 if (sampler.ms) {
4190 mask = mask | spv::ImageOperandsSampleMask;
4191 }
4192 #ifdef AMD_EXTENSIONS
4193 if (cracked.lod) {
4194 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4195 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4196 mask = mask | spv::ImageOperandsLodMask;
4197 }
4198 #endif
4199 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4200 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask);
4201 if (mask) {
4202 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4203 operands.push_back(imageOperands);
4204 }
4205 if (mask & spv::ImageOperandsSampleMask) {
4206 spv::IdImmediate imageOperand = { true, *opIt++ };
4207 operands.push_back(imageOperand);
4208 }
4209 #ifdef AMD_EXTENSIONS
4210 if (mask & spv::ImageOperandsLodMask) {
4211 spv::IdImmediate imageOperand = { true, *opIt++ };
4212 operands.push_back(imageOperand);
4213 }
4214 #endif
4215 if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) {
4216 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4217 operands.push_back(imageOperand);
4218 }
4219
4220 builder.createNoResultOp(spv::OpImageWrite, operands);
4221 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
4222 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
4223 return spv::NoResult;
4224 #ifdef AMD_EXTENSIONS
4225 } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) {
4226 #else
4227 } else if (node->getOp() == glslang::EOpSparseImageLoad) {
4228 #endif
4229 builder.addCapability(spv::CapabilitySparseResidency);
4230 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
4231 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
4232
4233 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
4234 if (sampler.ms) {
4235 mask = mask | spv::ImageOperandsSampleMask;
4236 }
4237 #ifdef AMD_EXTENSIONS
4238 if (cracked.lod) {
4239 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4240 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4241
4242 mask = mask | spv::ImageOperandsLodMask;
4243 }
4244 #endif
4245 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4246 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
4247 if (mask) {
4248 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4249 operands.push_back(imageOperands);
4250 }
4251 if (mask & spv::ImageOperandsSampleMask) {
4252 spv::IdImmediate imageOperand = { true, *opIt++ };
4253 operands.push_back(imageOperand);
4254 }
4255 #ifdef AMD_EXTENSIONS
4256 if (mask & spv::ImageOperandsLodMask) {
4257 spv::IdImmediate imageOperand = { true, *opIt++ };
4258 operands.push_back(imageOperand);
4259 }
4260 #endif
4261 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
4262 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4263 operands.push_back(imageOperand);
4264 }
4265
4266 // Create the return type that was a special structure
4267 spv::Id texelOut = *opIt;
4268 spv::Id typeId0 = resultType();
4269 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
4270 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
4271
4272 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
4273
4274 // Decode the return type
4275 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
4276 return builder.createCompositeExtract(resultId, typeId0, 0);
4277 } else {
4278 // Process image atomic operations
4279
4280 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
4281 // as the first source operand, is required by SPIR-V atomic operations.
4282 // For non-MS, the sample value should be 0
4283 spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) };
4284 operands.push_back(sample);
4285
4286 spv::Id resultTypeId;
4287 // imageAtomicStore has a void return type so base the pointer type on
4288 // the type of the value operand.
4289 if (node->getOp() == glslang::EOpImageAtomicStore) {
4290 resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word));
4291 } else {
4292 resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
4293 }
4294 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
4295
4296 std::vector<spv::Id> operands;
4297 operands.push_back(pointer);
4298 for (; opIt != arguments.end(); ++opIt)
4299 operands.push_back(*opIt);
4300
4301 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
4302 }
4303 }
4304
4305 #ifdef AMD_EXTENSIONS
4306 // Check for fragment mask functions other than queries
4307 if (cracked.fragMask) {
4308 assert(sampler.ms);
4309
4310 auto opIt = arguments.begin();
4311 std::vector<spv::Id> operands;
4312
4313 // Extract the image if necessary
4314 if (builder.isSampledImage(params.sampler))
4315 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4316
4317 operands.push_back(params.sampler);
4318 ++opIt;
4319
4320 if (sampler.isSubpass()) {
4321 // add on the (0,0) coordinate
4322 spv::Id zero = builder.makeIntConstant(0);
4323 std::vector<spv::Id> comps;
4324 comps.push_back(zero);
4325 comps.push_back(zero);
4326 operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps));
4327 }
4328
4329 for (; opIt != arguments.end(); ++opIt)
4330 operands.push_back(*opIt);
4331
4332 spv::Op fragMaskOp = spv::OpNop;
4333 if (node->getOp() == glslang::EOpFragmentMaskFetch)
4334 fragMaskOp = spv::OpFragmentMaskFetchAMD;
4335 else if (node->getOp() == glslang::EOpFragmentFetch)
4336 fragMaskOp = spv::OpFragmentFetchAMD;
4337
4338 builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask);
4339 builder.addCapability(spv::CapabilityFragmentMaskAMD);
4340 return builder.createOp(fragMaskOp, resultType(), operands);
4341 }
4342 #endif
4343
4344 // Check for texture functions other than queries
4345 bool sparse = node->isSparseTexture();
4346 #ifdef NV_EXTENSIONS
4347 bool imageFootprint = node->isImageFootprint();
4348 #endif
4349
4350 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
4351
4352 // check for bias argument
4353 bool bias = false;
4354 #ifdef AMD_EXTENSIONS
4355 if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
4356 #else
4357 if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
4358 #endif
4359 int nonBiasArgCount = 2;
4360 #ifdef AMD_EXTENSIONS
4361 if (cracked.gather)
4362 ++nonBiasArgCount; // comp argument should be present when bias argument is present
4363
4364 if (f16ShadowCompare)
4365 ++nonBiasArgCount;
4366 #endif
4367 if (cracked.offset)
4368 ++nonBiasArgCount;
4369 #ifdef AMD_EXTENSIONS
4370 else if (cracked.offsets)
4371 ++nonBiasArgCount;
4372 #endif
4373 if (cracked.grad)
4374 nonBiasArgCount += 2;
4375 if (cracked.lodClamp)
4376 ++nonBiasArgCount;
4377 if (sparse)
4378 ++nonBiasArgCount;
4379 #ifdef NV_EXTENSIONS
4380 if (imageFootprint)
4381 //Following three extra arguments
4382 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4383 nonBiasArgCount += 3;
4384 #endif
4385 if ((int)arguments.size() > nonBiasArgCount)
4386 bias = true;
4387 }
4388
4389 // See if the sampler param should really be just the SPV image part
4390 if (cracked.fetch) {
4391 // a fetch needs to have the image extracted first
4392 if (builder.isSampledImage(params.sampler))
4393 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4394 }
4395
4396 #ifdef AMD_EXTENSIONS
4397 if (cracked.gather) {
4398 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
4399 if (bias || cracked.lod ||
4400 sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) {
4401 builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod);
4402 builder.addCapability(spv::CapabilityImageGatherBiasLodAMD);
4403 }
4404 }
4405 #endif
4406
4407 // set the rest of the arguments
4408
4409 params.coords = arguments[1];
4410 int extraArgs = 0;
4411 bool noImplicitLod = false;
4412
4413 // sort out where Dref is coming from
4414 #ifdef AMD_EXTENSIONS
4415 if (cubeCompare || f16ShadowCompare) {
4416 #else
4417 if (cubeCompare) {
4418 #endif
4419 params.Dref = arguments[2];
4420 ++extraArgs;
4421 } else if (sampler.shadow && cracked.gather) {
4422 params.Dref = arguments[2];
4423 ++extraArgs;
4424 } else if (sampler.shadow) {
4425 std::vector<spv::Id> indexes;
4426 int dRefComp;
4427 if (cracked.proj)
4428 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
4429 else
4430 dRefComp = builder.getNumComponents(params.coords) - 1;
4431 indexes.push_back(dRefComp);
4432 params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
4433 }
4434
4435 // lod
4436 if (cracked.lod) {
4437 params.lod = arguments[2 + extraArgs];
4438 ++extraArgs;
4439 } else if (glslangIntermediate->getStage() != EShLangFragment
4440 #ifdef NV_EXTENSIONS
4441 // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs
4442 && !(glslangIntermediate->getStage() == EShLangCompute &&
4443 (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone))
4444 #endif
4445 ) {
4446 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
4447 noImplicitLod = true;
4448 }
4449
4450 // multisample
4451 if (sampler.ms) {
4452 params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified
4453 ++extraArgs;
4454 }
4455
4456 // gradient
4457 if (cracked.grad) {
4458 params.gradX = arguments[2 + extraArgs];
4459 params.gradY = arguments[3 + extraArgs];
4460 extraArgs += 2;
4461 }
4462
4463 // offset and offsets
4464 if (cracked.offset) {
4465 params.offset = arguments[2 + extraArgs];
4466 ++extraArgs;
4467 } else if (cracked.offsets) {
4468 params.offsets = arguments[2 + extraArgs];
4469 ++extraArgs;
4470 }
4471
4472 // lod clamp
4473 if (cracked.lodClamp) {
4474 params.lodClamp = arguments[2 + extraArgs];
4475 ++extraArgs;
4476 }
4477 // sparse
4478 if (sparse) {
4479 params.texelOut = arguments[2 + extraArgs];
4480 ++extraArgs;
4481 }
4482
4483 // gather component
4484 if (cracked.gather && ! sampler.shadow) {
4485 // default component is 0, if missing, otherwise an argument
4486 if (2 + extraArgs < (int)arguments.size()) {
4487 params.component = arguments[2 + extraArgs];
4488 ++extraArgs;
4489 } else
4490 params.component = builder.makeIntConstant(0);
4491 }
4492 #ifdef NV_EXTENSIONS
4493 spv::Id resultStruct = spv::NoResult;
4494 if (imageFootprint) {
4495 //Following three extra arguments
4496 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4497 params.granularity = arguments[2 + extraArgs];
4498 params.coarse = arguments[3 + extraArgs];
4499 resultStruct = arguments[4 + extraArgs];
4500 extraArgs += 3;
4501 }
4502 #endif
4503 // bias
4504 if (bias) {
4505 params.bias = arguments[2 + extraArgs];
4506 ++extraArgs;
4507 }
4508
4509 #ifdef NV_EXTENSIONS
4510 if (imageFootprint) {
4511 builder.addExtension(spv::E_SPV_NV_shader_image_footprint);
4512 builder.addCapability(spv::CapabilityImageFootprintNV);
4513
4514
4515 //resultStructType(OpenGL type) contains 5 elements:
4516 //struct gl_TextureFootprint2DNV {
4517 // uvec2 anchor;
4518 // uvec2 offset;
4519 // uvec2 mask;
4520 // uint lod;
4521 // uint granularity;
4522 //};
4523 //or
4524 //struct gl_TextureFootprint3DNV {
4525 // uvec3 anchor;
4526 // uvec3 offset;
4527 // uvec2 mask;
4528 // uint lod;
4529 // uint granularity;
4530 //};
4531 spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct));
4532 assert(builder.isStructType(resultStructType));
4533
4534 //resType (SPIR-V type) contains 6 elements:
4535 //Member 0 must be a Boolean type scalar(LOD),
4536 //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor),
4537 //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset),
4538 //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask),
4539 //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod),
4540 //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity).
4541 std::vector<spv::Id> members;
4542 members.push_back(resultType());
4543 for (int i = 0; i < 5; i++) {
4544 members.push_back(builder.getContainedTypeId(resultStructType, i));
4545 }
4546 spv::Id resType = builder.makeStructType(members, "ResType");
4547
4548 //call ImageFootprintNV
4549 spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params);
4550
4551 //copy resType (SPIR-V type) to resultStructType(OpenGL type)
4552 for (int i = 0; i < 5; i++) {
4553 builder.clearAccessChain();
4554 builder.setAccessChainLValue(resultStruct);
4555
4556 //Accessing to a struct we created, no coherent flag is set
4557 spv::Builder::AccessChain::CoherentFlags flags;
4558 flags.clear();
4559
4560 builder.accessChainPush(builder.makeIntConstant(i), flags, 0);
4561 builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1));
4562 }
4563 return builder.createCompositeExtract(res, resultType(), 0);
4564 }
4565 #endif
4566
4567 // projective component (might not to move)
4568 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
4569 // are divided by the last component of P."
4570 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
4571 // unused components will appear after all used components."
4572 if (cracked.proj) {
4573 int projSourceComp = builder.getNumComponents(params.coords) - 1;
4574 int projTargetComp;
4575 switch (sampler.dim) {
4576 case glslang::Esd1D: projTargetComp = 1; break;
4577 case glslang::Esd2D: projTargetComp = 2; break;
4578 case glslang::EsdRect: projTargetComp = 2; break;
4579 default: projTargetComp = projSourceComp; break;
4580 }
4581 // copy the projective coordinate if we have to
4582 if (projTargetComp != projSourceComp) {
4583 spv::Id projComp = builder.createCompositeExtract(params.coords,
4584 builder.getScalarTypeId(builder.getTypeId(params.coords)),
4585 projSourceComp);
4586 params.coords = builder.createCompositeInsert(projComp, params.coords,
4587 builder.getTypeId(params.coords), projTargetComp);
4588 }
4589 }
4590
4591 // nonprivate
4592 if (imageType.getQualifier().nonprivate) {
4593 params.nonprivate = true;
4594 }
4595
4596 // volatile
4597 if (imageType.getQualifier().volatil) {
4598 params.volatil = true;
4599 }
4600
4601 std::vector<spv::Id> result( 1,
4602 builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params)
4603 );
4604
4605 if (components != node->getType().getVectorSize())
4606 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4607
4608 return result[0];
4609 }
4610
4611 spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
4612 {
4613 // Grab the function's pointer from the previously created function
4614 spv::Function* function = functionMap[node->getName().c_str()];
4615 if (! function)
4616 return 0;
4617
4618 const glslang::TIntermSequence& glslangArgs = node->getSequence();
4619 const glslang::TQualifierList& qualifiers = node->getQualifierList();
4620
4621 // See comments in makeFunctions() for details about the semantics for parameter passing.
4622 //
4623 // These imply we need a four step process:
4624 // 1. Evaluate the arguments
4625 // 2. Allocate and make copies of in, out, and inout arguments
4626 // 3. Make the call
4627 // 4. Copy back the results
4628
4629 // 1. Evaluate the arguments and their types
4630 std::vector<spv::Builder::AccessChain> lValues;
4631 std::vector<spv::Id> rValues;
4632 std::vector<const glslang::TType*> argTypes;
4633 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
4634 argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType());
4635 // build l-value
4636 builder.clearAccessChain();
4637 glslangArgs[a]->traverse(this);
4638 // keep outputs and pass-by-originals as l-values, evaluate others as r-values
4639 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) ||
4640 writableParam(qualifiers[a])) {
4641 // save l-value
4642 lValues.push_back(builder.getAccessChain());
4643 } else {
4644 // process r-value
4645 rValues.push_back(accessChainLoad(*argTypes.back()));
4646 }
4647 }
4648
4649 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
4650 // copy the original into that space.
4651 //
4652 // Also, build up the list of actual arguments to pass in for the call
4653 int lValueCount = 0;
4654 int rValueCount = 0;
4655 std::vector<spv::Id> spvArgs;
4656 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
4657 spv::Id arg;
4658 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) {
4659 builder.setAccessChain(lValues[lValueCount]);
4660 arg = builder.accessChainGetLValue();
4661 ++lValueCount;
4662 } else if (writableParam(qualifiers[a])) {
4663 // need space to hold the copy
4664 arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param");
4665 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
4666 // need to copy the input into output space
4667 builder.setAccessChain(lValues[lValueCount]);
4668 spv::Id copy = accessChainLoad(*argTypes[a]);
4669 builder.clearAccessChain();
4670 builder.setAccessChainLValue(arg);
4671 multiTypeStore(*argTypes[a], copy);
4672 }
4673 ++lValueCount;
4674 } else {
4675 // process r-value, which involves a copy for a type mismatch
4676 if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) {
4677 spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg");
4678 builder.clearAccessChain();
4679 builder.setAccessChainLValue(argCopy);
4680 multiTypeStore(*argTypes[a], rValues[rValueCount]);
4681 arg = builder.createLoad(argCopy);
4682 } else
4683 arg = rValues[rValueCount];
4684 ++rValueCount;
4685 }
4686 spvArgs.push_back(arg);
4687 }
4688
4689 // 3. Make the call.
4690 spv::Id result = builder.createFunctionCall(function, spvArgs);
4691 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
4692
4693 // 4. Copy back out an "out" arguments.
4694 lValueCount = 0;
4695 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
4696 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0))
4697 ++lValueCount;
4698 else if (writableParam(qualifiers[a])) {
4699 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
4700 spv::Id copy = builder.createLoad(spvArgs[a]);
4701 builder.setAccessChain(lValues[lValueCount]);
4702 multiTypeStore(*argTypes[a], copy);
4703 }
4704 ++lValueCount;
4705 }
4706 }
4707
4708 return result;
4709 }
4710
4711 // Translate AST operation to SPV operation, already having SPV-based operands/types.
4712 spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
4713 spv::Id typeId, spv::Id left, spv::Id right,
4714 glslang::TBasicType typeProxy, bool reduceComparison)
4715 {
4716 bool isUnsigned = isTypeUnsignedInt(typeProxy);
4717 bool isFloat = isTypeFloat(typeProxy);
4718 bool isBool = typeProxy == glslang::EbtBool;
4719
4720 spv::Op binOp = spv::OpNop;
4721 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
4722 bool comparison = false;
4723
4724 switch (op) {
4725 case glslang::EOpAdd:
4726 case glslang::EOpAddAssign:
4727 if (isFloat)
4728 binOp = spv::OpFAdd;
4729 else
4730 binOp = spv::OpIAdd;
4731 break;
4732 case glslang::EOpSub:
4733 case glslang::EOpSubAssign:
4734 if (isFloat)
4735 binOp = spv::OpFSub;
4736 else
4737 binOp = spv::OpISub;
4738 break;
4739 case glslang::EOpMul:
4740 case glslang::EOpMulAssign:
4741 if (isFloat)
4742 binOp = spv::OpFMul;
4743 else
4744 binOp = spv::OpIMul;
4745 break;
4746 case glslang::EOpVectorTimesScalar:
4747 case glslang::EOpVectorTimesScalarAssign:
4748 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
4749 if (builder.isVector(right))
4750 std::swap(left, right);
4751 assert(builder.isScalar(right));
4752 needMatchingVectors = false;
4753 binOp = spv::OpVectorTimesScalar;
4754 } else if (isFloat)
4755 binOp = spv::OpFMul;
4756 else
4757 binOp = spv::OpIMul;
4758 break;
4759 case glslang::EOpVectorTimesMatrix:
4760 case glslang::EOpVectorTimesMatrixAssign:
4761 binOp = spv::OpVectorTimesMatrix;
4762 break;
4763 case glslang::EOpMatrixTimesVector:
4764 binOp = spv::OpMatrixTimesVector;
4765 break;
4766 case glslang::EOpMatrixTimesScalar:
4767 case glslang::EOpMatrixTimesScalarAssign:
4768 binOp = spv::OpMatrixTimesScalar;
4769 break;
4770 case glslang::EOpMatrixTimesMatrix:
4771 case glslang::EOpMatrixTimesMatrixAssign:
4772 binOp = spv::OpMatrixTimesMatrix;
4773 break;
4774 case glslang::EOpOuterProduct:
4775 binOp = spv::OpOuterProduct;
4776 needMatchingVectors = false;
4777 break;
4778
4779 case glslang::EOpDiv:
4780 case glslang::EOpDivAssign:
4781 if (isFloat)
4782 binOp = spv::OpFDiv;
4783 else if (isUnsigned)
4784 binOp = spv::OpUDiv;
4785 else
4786 binOp = spv::OpSDiv;
4787 break;
4788 case glslang::EOpMod:
4789 case glslang::EOpModAssign:
4790 if (isFloat)
4791 binOp = spv::OpFMod;
4792 else if (isUnsigned)
4793 binOp = spv::OpUMod;
4794 else
4795 binOp = spv::OpSMod;
4796 break;
4797 case glslang::EOpRightShift:
4798 case glslang::EOpRightShiftAssign:
4799 if (isUnsigned)
4800 binOp = spv::OpShiftRightLogical;
4801 else
4802 binOp = spv::OpShiftRightArithmetic;
4803 break;
4804 case glslang::EOpLeftShift:
4805 case glslang::EOpLeftShiftAssign:
4806 binOp = spv::OpShiftLeftLogical;
4807 break;
4808 case glslang::EOpAnd:
4809 case glslang::EOpAndAssign:
4810 binOp = spv::OpBitwiseAnd;
4811 break;
4812 case glslang::EOpLogicalAnd:
4813 needMatchingVectors = false;
4814 binOp = spv::OpLogicalAnd;
4815 break;
4816 case glslang::EOpInclusiveOr:
4817 case glslang::EOpInclusiveOrAssign:
4818 binOp = spv::OpBitwiseOr;
4819 break;
4820 case glslang::EOpLogicalOr:
4821 needMatchingVectors = false;
4822 binOp = spv::OpLogicalOr;
4823 break;
4824 case glslang::EOpExclusiveOr:
4825 case glslang::EOpExclusiveOrAssign:
4826 binOp = spv::OpBitwiseXor;
4827 break;
4828 case glslang::EOpLogicalXor:
4829 needMatchingVectors = false;
4830 binOp = spv::OpLogicalNotEqual;
4831 break;
4832
4833 case glslang::EOpLessThan:
4834 case glslang::EOpGreaterThan:
4835 case glslang::EOpLessThanEqual:
4836 case glslang::EOpGreaterThanEqual:
4837 case glslang::EOpEqual:
4838 case glslang::EOpNotEqual:
4839 case glslang::EOpVectorEqual:
4840 case glslang::EOpVectorNotEqual:
4841 comparison = true;
4842 break;
4843 default:
4844 break;
4845 }
4846
4847 // handle mapped binary operations (should be non-comparison)
4848 if (binOp != spv::OpNop) {
4849 assert(comparison == false);
4850 if (builder.isMatrix(left) || builder.isMatrix(right))
4851 return createBinaryMatrixOperation(binOp, decorations, typeId, left, right);
4852
4853 // No matrix involved; make both operands be the same number of components, if needed
4854 if (needMatchingVectors)
4855 builder.promoteScalar(decorations.precision, left, right);
4856
4857 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
4858 builder.addDecoration(result, decorations.noContraction);
4859 builder.addDecoration(result, decorations.nonUniform);
4860 return builder.setPrecision(result, decorations.precision);
4861 }
4862
4863 if (! comparison)
4864 return 0;
4865
4866 // Handle comparison instructions
4867
4868 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
4869 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) {
4870 spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual);
4871 builder.addDecoration(result, decorations.nonUniform);
4872 return result;
4873 }
4874
4875 switch (op) {
4876 case glslang::EOpLessThan:
4877 if (isFloat)
4878 binOp = spv::OpFOrdLessThan;
4879 else if (isUnsigned)
4880 binOp = spv::OpULessThan;
4881 else
4882 binOp = spv::OpSLessThan;
4883 break;
4884 case glslang::EOpGreaterThan:
4885 if (isFloat)
4886 binOp = spv::OpFOrdGreaterThan;
4887 else if (isUnsigned)
4888 binOp = spv::OpUGreaterThan;
4889 else
4890 binOp = spv::OpSGreaterThan;
4891 break;
4892 case glslang::EOpLessThanEqual:
4893 if (isFloat)
4894 binOp = spv::OpFOrdLessThanEqual;
4895 else if (isUnsigned)
4896 binOp = spv::OpULessThanEqual;
4897 else
4898 binOp = spv::OpSLessThanEqual;
4899 break;
4900 case glslang::EOpGreaterThanEqual:
4901 if (isFloat)
4902 binOp = spv::OpFOrdGreaterThanEqual;
4903 else if (isUnsigned)
4904 binOp = spv::OpUGreaterThanEqual;
4905 else
4906 binOp = spv::OpSGreaterThanEqual;
4907 break;
4908 case glslang::EOpEqual:
4909 case glslang::EOpVectorEqual:
4910 if (isFloat)
4911 binOp = spv::OpFOrdEqual;
4912 else if (isBool)
4913 binOp = spv::OpLogicalEqual;
4914 else
4915 binOp = spv::OpIEqual;
4916 break;
4917 case glslang::EOpNotEqual:
4918 case glslang::EOpVectorNotEqual:
4919 if (isFloat)
4920 binOp = spv::OpFOrdNotEqual;
4921 else if (isBool)
4922 binOp = spv::OpLogicalNotEqual;
4923 else
4924 binOp = spv::OpINotEqual;
4925 break;
4926 default:
4927 break;
4928 }
4929
4930 if (binOp != spv::OpNop) {
4931 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
4932 builder.addDecoration(result, decorations.noContraction);
4933 builder.addDecoration(result, decorations.nonUniform);
4934 return builder.setPrecision(result, decorations.precision);
4935 }
4936
4937 return 0;
4938 }
4939
4940 //
4941 // Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
4942 // These can be any of:
4943 //
4944 // matrix * scalar
4945 // scalar * matrix
4946 // matrix * matrix linear algebraic
4947 // matrix * vector
4948 // vector * matrix
4949 // matrix * matrix componentwise
4950 // matrix op matrix op in {+, -, /}
4951 // matrix op scalar op in {+, -, /}
4952 // scalar op matrix op in {+, -, /}
4953 //
4954 spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
4955 spv::Id left, spv::Id right)
4956 {
4957 bool firstClass = true;
4958
4959 // First, handle first-class matrix operations (* and matrix/scalar)
4960 switch (op) {
4961 case spv::OpFDiv:
4962 if (builder.isMatrix(left) && builder.isScalar(right)) {
4963 // turn matrix / scalar into a multiply...
4964 spv::Id resultType = builder.getTypeId(right);
4965 right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right);
4966 op = spv::OpMatrixTimesScalar;
4967 } else
4968 firstClass = false;
4969 break;
4970 case spv::OpMatrixTimesScalar:
4971 if (builder.isMatrix(right))
4972 std::swap(left, right);
4973 assert(builder.isScalar(right));
4974 break;
4975 case spv::OpVectorTimesMatrix:
4976 assert(builder.isVector(left));
4977 assert(builder.isMatrix(right));
4978 break;
4979 case spv::OpMatrixTimesVector:
4980 assert(builder.isMatrix(left));
4981 assert(builder.isVector(right));
4982 break;
4983 case spv::OpMatrixTimesMatrix:
4984 assert(builder.isMatrix(left));
4985 assert(builder.isMatrix(right));
4986 break;
4987 default:
4988 firstClass = false;
4989 break;
4990 }
4991
4992 if (firstClass) {
4993 spv::Id result = builder.createBinOp(op, typeId, left, right);
4994 builder.addDecoration(result, decorations.noContraction);
4995 builder.addDecoration(result, decorations.nonUniform);
4996 return builder.setPrecision(result, decorations.precision);
4997 }
4998
4999 // Handle component-wise +, -, *, %, and / for all combinations of type.
5000 // The result type of all of them is the same type as the (a) matrix operand.
5001 // The algorithm is to:
5002 // - break the matrix(es) into vectors
5003 // - smear any scalar to a vector
5004 // - do vector operations
5005 // - make a matrix out the vector results
5006 switch (op) {
5007 case spv::OpFAdd:
5008 case spv::OpFSub:
5009 case spv::OpFDiv:
5010 case spv::OpFMod:
5011 case spv::OpFMul:
5012 {
5013 // one time set up...
5014 bool leftMat = builder.isMatrix(left);
5015 bool rightMat = builder.isMatrix(right);
5016 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
5017 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
5018 spv::Id scalarType = builder.getScalarTypeId(typeId);
5019 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
5020 std::vector<spv::Id> results;
5021 spv::Id smearVec = spv::NoResult;
5022 if (builder.isScalar(left))
5023 smearVec = builder.smearScalar(decorations.precision, left, vecType);
5024 else if (builder.isScalar(right))
5025 smearVec = builder.smearScalar(decorations.precision, right, vecType);
5026
5027 // do each vector op
5028 for (unsigned int c = 0; c < numCols; ++c) {
5029 std::vector<unsigned int> indexes;
5030 indexes.push_back(c);
5031 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
5032 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
5033 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
5034 builder.addDecoration(result, decorations.noContraction);
5035 builder.addDecoration(result, decorations.nonUniform);
5036 results.push_back(builder.setPrecision(result, decorations.precision));
5037 }
5038
5039 // put the pieces together
5040 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
5041 builder.addDecoration(result, decorations.nonUniform);
5042 return result;
5043 }
5044 default:
5045 assert(0);
5046 return spv::NoResult;
5047 }
5048 }
5049
5050 spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
5051 spv::Id operand, glslang::TBasicType typeProxy)
5052 {
5053 spv::Op unaryOp = spv::OpNop;
5054 int extBuiltins = -1;
5055 int libCall = -1;
5056 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5057 bool isFloat = isTypeFloat(typeProxy);
5058
5059 switch (op) {
5060 case glslang::EOpNegative:
5061 if (isFloat) {
5062 unaryOp = spv::OpFNegate;
5063 if (builder.isMatrixType(typeId))
5064 return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy);
5065 } else
5066 unaryOp = spv::OpSNegate;
5067 break;
5068
5069 case glslang::EOpLogicalNot:
5070 case glslang::EOpVectorLogicalNot:
5071 unaryOp = spv::OpLogicalNot;
5072 break;
5073 case glslang::EOpBitwiseNot:
5074 unaryOp = spv::OpNot;
5075 break;
5076
5077 case glslang::EOpDeterminant:
5078 libCall = spv::GLSLstd450Determinant;
5079 break;
5080 case glslang::EOpMatrixInverse:
5081 libCall = spv::GLSLstd450MatrixInverse;
5082 break;
5083 case glslang::EOpTranspose:
5084 unaryOp = spv::OpTranspose;
5085 break;
5086
5087 case glslang::EOpRadians:
5088 libCall = spv::GLSLstd450Radians;
5089 break;
5090 case glslang::EOpDegrees:
5091 libCall = spv::GLSLstd450Degrees;
5092 break;
5093 case glslang::EOpSin:
5094 libCall = spv::GLSLstd450Sin;
5095 break;
5096 case glslang::EOpCos:
5097 libCall = spv::GLSLstd450Cos;
5098 break;
5099 case glslang::EOpTan:
5100 libCall = spv::GLSLstd450Tan;
5101 break;
5102 case glslang::EOpAcos:
5103 libCall = spv::GLSLstd450Acos;
5104 break;
5105 case glslang::EOpAsin:
5106 libCall = spv::GLSLstd450Asin;
5107 break;
5108 case glslang::EOpAtan:
5109 libCall = spv::GLSLstd450Atan;
5110 break;
5111
5112 case glslang::EOpAcosh:
5113 libCall = spv::GLSLstd450Acosh;
5114 break;
5115 case glslang::EOpAsinh:
5116 libCall = spv::GLSLstd450Asinh;
5117 break;
5118 case glslang::EOpAtanh:
5119 libCall = spv::GLSLstd450Atanh;
5120 break;
5121 case glslang::EOpTanh:
5122 libCall = spv::GLSLstd450Tanh;
5123 break;
5124 case glslang::EOpCosh:
5125 libCall = spv::GLSLstd450Cosh;
5126 break;
5127 case glslang::EOpSinh:
5128 libCall = spv::GLSLstd450Sinh;
5129 break;
5130
5131 case glslang::EOpLength:
5132 libCall = spv::GLSLstd450Length;
5133 break;
5134 case glslang::EOpNormalize:
5135 libCall = spv::GLSLstd450Normalize;
5136 break;
5137
5138 case glslang::EOpExp:
5139 libCall = spv::GLSLstd450Exp;
5140 break;
5141 case glslang::EOpLog:
5142 libCall = spv::GLSLstd450Log;
5143 break;
5144 case glslang::EOpExp2:
5145 libCall = spv::GLSLstd450Exp2;
5146 break;
5147 case glslang::EOpLog2:
5148 libCall = spv::GLSLstd450Log2;
5149 break;
5150 case glslang::EOpSqrt:
5151 libCall = spv::GLSLstd450Sqrt;
5152 break;
5153 case glslang::EOpInverseSqrt:
5154 libCall = spv::GLSLstd450InverseSqrt;
5155 break;
5156
5157 case glslang::EOpFloor:
5158 libCall = spv::GLSLstd450Floor;
5159 break;
5160 case glslang::EOpTrunc:
5161 libCall = spv::GLSLstd450Trunc;
5162 break;
5163 case glslang::EOpRound:
5164 libCall = spv::GLSLstd450Round;
5165 break;
5166 case glslang::EOpRoundEven:
5167 libCall = spv::GLSLstd450RoundEven;
5168 break;
5169 case glslang::EOpCeil:
5170 libCall = spv::GLSLstd450Ceil;
5171 break;
5172 case glslang::EOpFract:
5173 libCall = spv::GLSLstd450Fract;
5174 break;
5175
5176 case glslang::EOpIsNan:
5177 unaryOp = spv::OpIsNan;
5178 break;
5179 case glslang::EOpIsInf:
5180 unaryOp = spv::OpIsInf;
5181 break;
5182 case glslang::EOpIsFinite:
5183 unaryOp = spv::OpIsFinite;
5184 break;
5185
5186 case glslang::EOpFloatBitsToInt:
5187 case glslang::EOpFloatBitsToUint:
5188 case glslang::EOpIntBitsToFloat:
5189 case glslang::EOpUintBitsToFloat:
5190 case glslang::EOpDoubleBitsToInt64:
5191 case glslang::EOpDoubleBitsToUint64:
5192 case glslang::EOpInt64BitsToDouble:
5193 case glslang::EOpUint64BitsToDouble:
5194 case glslang::EOpFloat16BitsToInt16:
5195 case glslang::EOpFloat16BitsToUint16:
5196 case glslang::EOpInt16BitsToFloat16:
5197 case glslang::EOpUint16BitsToFloat16:
5198 unaryOp = spv::OpBitcast;
5199 break;
5200
5201 case glslang::EOpPackSnorm2x16:
5202 libCall = spv::GLSLstd450PackSnorm2x16;
5203 break;
5204 case glslang::EOpUnpackSnorm2x16:
5205 libCall = spv::GLSLstd450UnpackSnorm2x16;
5206 break;
5207 case glslang::EOpPackUnorm2x16:
5208 libCall = spv::GLSLstd450PackUnorm2x16;
5209 break;
5210 case glslang::EOpUnpackUnorm2x16:
5211 libCall = spv::GLSLstd450UnpackUnorm2x16;
5212 break;
5213 case glslang::EOpPackHalf2x16:
5214 libCall = spv::GLSLstd450PackHalf2x16;
5215 break;
5216 case glslang::EOpUnpackHalf2x16:
5217 libCall = spv::GLSLstd450UnpackHalf2x16;
5218 break;
5219 case glslang::EOpPackSnorm4x8:
5220 libCall = spv::GLSLstd450PackSnorm4x8;
5221 break;
5222 case glslang::EOpUnpackSnorm4x8:
5223 libCall = spv::GLSLstd450UnpackSnorm4x8;
5224 break;
5225 case glslang::EOpPackUnorm4x8:
5226 libCall = spv::GLSLstd450PackUnorm4x8;
5227 break;
5228 case glslang::EOpUnpackUnorm4x8:
5229 libCall = spv::GLSLstd450UnpackUnorm4x8;
5230 break;
5231 case glslang::EOpPackDouble2x32:
5232 libCall = spv::GLSLstd450PackDouble2x32;
5233 break;
5234 case glslang::EOpUnpackDouble2x32:
5235 libCall = spv::GLSLstd450UnpackDouble2x32;
5236 break;
5237
5238 case glslang::EOpPackInt2x32:
5239 case glslang::EOpUnpackInt2x32:
5240 case glslang::EOpPackUint2x32:
5241 case glslang::EOpUnpackUint2x32:
5242 case glslang::EOpPack16:
5243 case glslang::EOpPack32:
5244 case glslang::EOpPack64:
5245 case glslang::EOpUnpack32:
5246 case glslang::EOpUnpack16:
5247 case glslang::EOpUnpack8:
5248 case glslang::EOpPackInt2x16:
5249 case glslang::EOpUnpackInt2x16:
5250 case glslang::EOpPackUint2x16:
5251 case glslang::EOpUnpackUint2x16:
5252 case glslang::EOpPackInt4x16:
5253 case glslang::EOpUnpackInt4x16:
5254 case glslang::EOpPackUint4x16:
5255 case glslang::EOpUnpackUint4x16:
5256 case glslang::EOpPackFloat2x16:
5257 case glslang::EOpUnpackFloat2x16:
5258 unaryOp = spv::OpBitcast;
5259 break;
5260
5261 case glslang::EOpDPdx:
5262 unaryOp = spv::OpDPdx;
5263 break;
5264 case glslang::EOpDPdy:
5265 unaryOp = spv::OpDPdy;
5266 break;
5267 case glslang::EOpFwidth:
5268 unaryOp = spv::OpFwidth;
5269 break;
5270 case glslang::EOpDPdxFine:
5271 unaryOp = spv::OpDPdxFine;
5272 break;
5273 case glslang::EOpDPdyFine:
5274 unaryOp = spv::OpDPdyFine;
5275 break;
5276 case glslang::EOpFwidthFine:
5277 unaryOp = spv::OpFwidthFine;
5278 break;
5279 case glslang::EOpDPdxCoarse:
5280 unaryOp = spv::OpDPdxCoarse;
5281 break;
5282 case glslang::EOpDPdyCoarse:
5283 unaryOp = spv::OpDPdyCoarse;
5284 break;
5285 case glslang::EOpFwidthCoarse:
5286 unaryOp = spv::OpFwidthCoarse;
5287 break;
5288 case glslang::EOpInterpolateAtCentroid:
5289 #ifdef AMD_EXTENSIONS
5290 if (typeProxy == glslang::EbtFloat16)
5291 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
5292 #endif
5293 libCall = spv::GLSLstd450InterpolateAtCentroid;
5294 break;
5295 case glslang::EOpAny:
5296 unaryOp = spv::OpAny;
5297 break;
5298 case glslang::EOpAll:
5299 unaryOp = spv::OpAll;
5300 break;
5301
5302 case glslang::EOpAbs:
5303 if (isFloat)
5304 libCall = spv::GLSLstd450FAbs;
5305 else
5306 libCall = spv::GLSLstd450SAbs;
5307 break;
5308 case glslang::EOpSign:
5309 if (isFloat)
5310 libCall = spv::GLSLstd450FSign;
5311 else
5312 libCall = spv::GLSLstd450SSign;
5313 break;
5314
5315 case glslang::EOpAtomicCounterIncrement:
5316 case glslang::EOpAtomicCounterDecrement:
5317 case glslang::EOpAtomicCounter:
5318 {
5319 // Handle all of the atomics in one place, in createAtomicOperation()
5320 std::vector<spv::Id> operands;
5321 operands.push_back(operand);
5322 return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy);
5323 }
5324
5325 case glslang::EOpBitFieldReverse:
5326 unaryOp = spv::OpBitReverse;
5327 break;
5328 case glslang::EOpBitCount:
5329 unaryOp = spv::OpBitCount;
5330 break;
5331 case glslang::EOpFindLSB:
5332 libCall = spv::GLSLstd450FindILsb;
5333 break;
5334 case glslang::EOpFindMSB:
5335 if (isUnsigned)
5336 libCall = spv::GLSLstd450FindUMsb;
5337 else
5338 libCall = spv::GLSLstd450FindSMsb;
5339 break;
5340
5341 case glslang::EOpBallot:
5342 case glslang::EOpReadFirstInvocation:
5343 case glslang::EOpAnyInvocation:
5344 case glslang::EOpAllInvocations:
5345 case glslang::EOpAllInvocationsEqual:
5346 #ifdef AMD_EXTENSIONS
5347 case glslang::EOpMinInvocations:
5348 case glslang::EOpMaxInvocations:
5349 case glslang::EOpAddInvocations:
5350 case glslang::EOpMinInvocationsNonUniform:
5351 case glslang::EOpMaxInvocationsNonUniform:
5352 case glslang::EOpAddInvocationsNonUniform:
5353 case glslang::EOpMinInvocationsInclusiveScan:
5354 case glslang::EOpMaxInvocationsInclusiveScan:
5355 case glslang::EOpAddInvocationsInclusiveScan:
5356 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
5357 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
5358 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
5359 case glslang::EOpMinInvocationsExclusiveScan:
5360 case glslang::EOpMaxInvocationsExclusiveScan:
5361 case glslang::EOpAddInvocationsExclusiveScan:
5362 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
5363 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
5364 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
5365 #endif
5366 {
5367 std::vector<spv::Id> operands;
5368 operands.push_back(operand);
5369 return createInvocationsOperation(op, typeId, operands, typeProxy);
5370 }
5371 case glslang::EOpSubgroupAll:
5372 case glslang::EOpSubgroupAny:
5373 case glslang::EOpSubgroupAllEqual:
5374 case glslang::EOpSubgroupBroadcastFirst:
5375 case glslang::EOpSubgroupBallot:
5376 case glslang::EOpSubgroupInverseBallot:
5377 case glslang::EOpSubgroupBallotBitCount:
5378 case glslang::EOpSubgroupBallotInclusiveBitCount:
5379 case glslang::EOpSubgroupBallotExclusiveBitCount:
5380 case glslang::EOpSubgroupBallotFindLSB:
5381 case glslang::EOpSubgroupBallotFindMSB:
5382 case glslang::EOpSubgroupAdd:
5383 case glslang::EOpSubgroupMul:
5384 case glslang::EOpSubgroupMin:
5385 case glslang::EOpSubgroupMax:
5386 case glslang::EOpSubgroupAnd:
5387 case glslang::EOpSubgroupOr:
5388 case glslang::EOpSubgroupXor:
5389 case glslang::EOpSubgroupInclusiveAdd:
5390 case glslang::EOpSubgroupInclusiveMul:
5391 case glslang::EOpSubgroupInclusiveMin:
5392 case glslang::EOpSubgroupInclusiveMax:
5393 case glslang::EOpSubgroupInclusiveAnd:
5394 case glslang::EOpSubgroupInclusiveOr:
5395 case glslang::EOpSubgroupInclusiveXor:
5396 case glslang::EOpSubgroupExclusiveAdd:
5397 case glslang::EOpSubgroupExclusiveMul:
5398 case glslang::EOpSubgroupExclusiveMin:
5399 case glslang::EOpSubgroupExclusiveMax:
5400 case glslang::EOpSubgroupExclusiveAnd:
5401 case glslang::EOpSubgroupExclusiveOr:
5402 case glslang::EOpSubgroupExclusiveXor:
5403 case glslang::EOpSubgroupQuadSwapHorizontal:
5404 case glslang::EOpSubgroupQuadSwapVertical:
5405 case glslang::EOpSubgroupQuadSwapDiagonal: {
5406 std::vector<spv::Id> operands;
5407 operands.push_back(operand);
5408 return createSubgroupOperation(op, typeId, operands, typeProxy);
5409 }
5410 #ifdef AMD_EXTENSIONS
5411 case glslang::EOpMbcnt:
5412 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
5413 libCall = spv::MbcntAMD;
5414 break;
5415
5416 case glslang::EOpCubeFaceIndex:
5417 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5418 libCall = spv::CubeFaceIndexAMD;
5419 break;
5420
5421 case glslang::EOpCubeFaceCoord:
5422 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5423 libCall = spv::CubeFaceCoordAMD;
5424 break;
5425 #endif
5426 #ifdef NV_EXTENSIONS
5427 case glslang::EOpSubgroupPartition:
5428 unaryOp = spv::OpGroupNonUniformPartitionNV;
5429 break;
5430 #endif
5431 case glslang::EOpConstructReference:
5432 unaryOp = spv::OpBitcast;
5433 break;
5434 default:
5435 return 0;
5436 }
5437
5438 spv::Id id;
5439 if (libCall >= 0) {
5440 std::vector<spv::Id> args;
5441 args.push_back(operand);
5442 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
5443 } else {
5444 id = builder.createUnaryOp(unaryOp, typeId, operand);
5445 }
5446
5447 builder.addDecoration(id, decorations.noContraction);
5448 builder.addDecoration(id, decorations.nonUniform);
5449 return builder.setPrecision(id, decorations.precision);
5450 }
5451
5452 // Create a unary operation on a matrix
5453 spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5454 spv::Id operand, glslang::TBasicType /* typeProxy */)
5455 {
5456 // Handle unary operations vector by vector.
5457 // The result type is the same type as the original type.
5458 // The algorithm is to:
5459 // - break the matrix into vectors
5460 // - apply the operation to each vector
5461 // - make a matrix out the vector results
5462
5463 // get the types sorted out
5464 int numCols = builder.getNumColumns(operand);
5465 int numRows = builder.getNumRows(operand);
5466 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
5467 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
5468 std::vector<spv::Id> results;
5469
5470 // do each vector op
5471 for (int c = 0; c < numCols; ++c) {
5472 std::vector<unsigned int> indexes;
5473 indexes.push_back(c);
5474 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
5475 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
5476 builder.addDecoration(destVec, decorations.noContraction);
5477 builder.addDecoration(destVec, decorations.nonUniform);
5478 results.push_back(builder.setPrecision(destVec, decorations.precision));
5479 }
5480
5481 // put the pieces together
5482 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
5483 builder.addDecoration(result, decorations.nonUniform);
5484 return result;
5485 }
5486
5487 // For converting integers where both the bitwidth and the signedness could
5488 // change, but only do the width change here. The caller is still responsible
5489 // for the signedness conversion.
5490 spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize)
5491 {
5492 // Get the result type width, based on the type to convert to.
5493 int width = 32;
5494 switch(op) {
5495 case glslang::EOpConvInt16ToUint8:
5496 case glslang::EOpConvIntToUint8:
5497 case glslang::EOpConvInt64ToUint8:
5498 case glslang::EOpConvUint16ToInt8:
5499 case glslang::EOpConvUintToInt8:
5500 case glslang::EOpConvUint64ToInt8:
5501 width = 8;
5502 break;
5503 case glslang::EOpConvInt8ToUint16:
5504 case glslang::EOpConvIntToUint16:
5505 case glslang::EOpConvInt64ToUint16:
5506 case glslang::EOpConvUint8ToInt16:
5507 case glslang::EOpConvUintToInt16:
5508 case glslang::EOpConvUint64ToInt16:
5509 width = 16;
5510 break;
5511 case glslang::EOpConvInt8ToUint:
5512 case glslang::EOpConvInt16ToUint:
5513 case glslang::EOpConvInt64ToUint:
5514 case glslang::EOpConvUint8ToInt:
5515 case glslang::EOpConvUint16ToInt:
5516 case glslang::EOpConvUint64ToInt:
5517 width = 32;
5518 break;
5519 case glslang::EOpConvInt8ToUint64:
5520 case glslang::EOpConvInt16ToUint64:
5521 case glslang::EOpConvIntToUint64:
5522 case glslang::EOpConvUint8ToInt64:
5523 case glslang::EOpConvUint16ToInt64:
5524 case glslang::EOpConvUintToInt64:
5525 width = 64;
5526 break;
5527
5528 default:
5529 assert(false && "Default missing");
5530 break;
5531 }
5532
5533 // Get the conversion operation and result type,
5534 // based on the target width, but the source type.
5535 spv::Id type = spv::NoType;
5536 spv::Op convOp = spv::OpNop;
5537 switch(op) {
5538 case glslang::EOpConvInt8ToUint16:
5539 case glslang::EOpConvInt8ToUint:
5540 case glslang::EOpConvInt8ToUint64:
5541 case glslang::EOpConvInt16ToUint8:
5542 case glslang::EOpConvInt16ToUint:
5543 case glslang::EOpConvInt16ToUint64:
5544 case glslang::EOpConvIntToUint8:
5545 case glslang::EOpConvIntToUint16:
5546 case glslang::EOpConvIntToUint64:
5547 case glslang::EOpConvInt64ToUint8:
5548 case glslang::EOpConvInt64ToUint16:
5549 case glslang::EOpConvInt64ToUint:
5550 convOp = spv::OpSConvert;
5551 type = builder.makeIntType(width);
5552 break;
5553 default:
5554 convOp = spv::OpUConvert;
5555 type = builder.makeUintType(width);
5556 break;
5557 }
5558
5559 if (vectorSize > 0)
5560 type = builder.makeVectorType(type, vectorSize);
5561
5562 return builder.createUnaryOp(convOp, type, operand);
5563 }
5564
5565 spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
5566 spv::Id operand, glslang::TBasicType typeProxy)
5567 {
5568 spv::Op convOp = spv::OpNop;
5569 spv::Id zero = 0;
5570 spv::Id one = 0;
5571
5572 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
5573
5574 switch (op) {
5575 case glslang::EOpConvInt8ToBool:
5576 case glslang::EOpConvUint8ToBool:
5577 zero = builder.makeUint8Constant(0);
5578 zero = makeSmearedConstant(zero, vectorSize);
5579 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5580 case glslang::EOpConvInt16ToBool:
5581 case glslang::EOpConvUint16ToBool:
5582 zero = builder.makeUint16Constant(0);
5583 zero = makeSmearedConstant(zero, vectorSize);
5584 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5585 case glslang::EOpConvIntToBool:
5586 case glslang::EOpConvUintToBool:
5587 zero = builder.makeUintConstant(0);
5588 zero = makeSmearedConstant(zero, vectorSize);
5589 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5590 case glslang::EOpConvInt64ToBool:
5591 case glslang::EOpConvUint64ToBool:
5592 zero = builder.makeUint64Constant(0);
5593 zero = makeSmearedConstant(zero, vectorSize);
5594 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5595
5596 case glslang::EOpConvFloatToBool:
5597 zero = builder.makeFloatConstant(0.0F);
5598 zero = makeSmearedConstant(zero, vectorSize);
5599 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
5600
5601 case glslang::EOpConvDoubleToBool:
5602 zero = builder.makeDoubleConstant(0.0);
5603 zero = makeSmearedConstant(zero, vectorSize);
5604 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
5605
5606 case glslang::EOpConvFloat16ToBool:
5607 zero = builder.makeFloat16Constant(0.0F);
5608 zero = makeSmearedConstant(zero, vectorSize);
5609 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
5610
5611 case glslang::EOpConvBoolToFloat:
5612 convOp = spv::OpSelect;
5613 zero = builder.makeFloatConstant(0.0F);
5614 one = builder.makeFloatConstant(1.0F);
5615 break;
5616
5617 case glslang::EOpConvBoolToDouble:
5618 convOp = spv::OpSelect;
5619 zero = builder.makeDoubleConstant(0.0);
5620 one = builder.makeDoubleConstant(1.0);
5621 break;
5622
5623 case glslang::EOpConvBoolToFloat16:
5624 convOp = spv::OpSelect;
5625 zero = builder.makeFloat16Constant(0.0F);
5626 one = builder.makeFloat16Constant(1.0F);
5627 break;
5628
5629 case glslang::EOpConvBoolToInt8:
5630 zero = builder.makeInt8Constant(0);
5631 one = builder.makeInt8Constant(1);
5632 convOp = spv::OpSelect;
5633 break;
5634
5635 case glslang::EOpConvBoolToUint8:
5636 zero = builder.makeUint8Constant(0);
5637 one = builder.makeUint8Constant(1);
5638 convOp = spv::OpSelect;
5639 break;
5640
5641 case glslang::EOpConvBoolToInt16:
5642 zero = builder.makeInt16Constant(0);
5643 one = builder.makeInt16Constant(1);
5644 convOp = spv::OpSelect;
5645 break;
5646
5647 case glslang::EOpConvBoolToUint16:
5648 zero = builder.makeUint16Constant(0);
5649 one = builder.makeUint16Constant(1);
5650 convOp = spv::OpSelect;
5651 break;
5652
5653 case glslang::EOpConvBoolToInt:
5654 case glslang::EOpConvBoolToInt64:
5655 if (op == glslang::EOpConvBoolToInt64)
5656 zero = builder.makeInt64Constant(0);
5657 else
5658 zero = builder.makeIntConstant(0);
5659
5660 if (op == glslang::EOpConvBoolToInt64)
5661 one = builder.makeInt64Constant(1);
5662 else
5663 one = builder.makeIntConstant(1);
5664
5665 convOp = spv::OpSelect;
5666 break;
5667
5668 case glslang::EOpConvBoolToUint:
5669 case glslang::EOpConvBoolToUint64:
5670 if (op == glslang::EOpConvBoolToUint64)
5671 zero = builder.makeUint64Constant(0);
5672 else
5673 zero = builder.makeUintConstant(0);
5674
5675 if (op == glslang::EOpConvBoolToUint64)
5676 one = builder.makeUint64Constant(1);
5677 else
5678 one = builder.makeUintConstant(1);
5679
5680 convOp = spv::OpSelect;
5681 break;
5682
5683 case glslang::EOpConvInt8ToFloat16:
5684 case glslang::EOpConvInt8ToFloat:
5685 case glslang::EOpConvInt8ToDouble:
5686 case glslang::EOpConvInt16ToFloat16:
5687 case glslang::EOpConvInt16ToFloat:
5688 case glslang::EOpConvInt16ToDouble:
5689 case glslang::EOpConvIntToFloat16:
5690 case glslang::EOpConvIntToFloat:
5691 case glslang::EOpConvIntToDouble:
5692 case glslang::EOpConvInt64ToFloat:
5693 case glslang::EOpConvInt64ToDouble:
5694 case glslang::EOpConvInt64ToFloat16:
5695 convOp = spv::OpConvertSToF;
5696 break;
5697
5698 case glslang::EOpConvUint8ToFloat16:
5699 case glslang::EOpConvUint8ToFloat:
5700 case glslang::EOpConvUint8ToDouble:
5701 case glslang::EOpConvUint16ToFloat16:
5702 case glslang::EOpConvUint16ToFloat:
5703 case glslang::EOpConvUint16ToDouble:
5704 case glslang::EOpConvUintToFloat16:
5705 case glslang::EOpConvUintToFloat:
5706 case glslang::EOpConvUintToDouble:
5707 case glslang::EOpConvUint64ToFloat:
5708 case glslang::EOpConvUint64ToDouble:
5709 case glslang::EOpConvUint64ToFloat16:
5710 convOp = spv::OpConvertUToF;
5711 break;
5712
5713 case glslang::EOpConvDoubleToFloat:
5714 case glslang::EOpConvFloatToDouble:
5715 case glslang::EOpConvDoubleToFloat16:
5716 case glslang::EOpConvFloat16ToDouble:
5717 case glslang::EOpConvFloatToFloat16:
5718 case glslang::EOpConvFloat16ToFloat:
5719 convOp = spv::OpFConvert;
5720 if (builder.isMatrixType(destType))
5721 return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy);
5722 break;
5723
5724 case glslang::EOpConvFloat16ToInt8:
5725 case glslang::EOpConvFloatToInt8:
5726 case glslang::EOpConvDoubleToInt8:
5727 case glslang::EOpConvFloat16ToInt16:
5728 case glslang::EOpConvFloatToInt16:
5729 case glslang::EOpConvDoubleToInt16:
5730 case glslang::EOpConvFloat16ToInt:
5731 case glslang::EOpConvFloatToInt:
5732 case glslang::EOpConvDoubleToInt:
5733 case glslang::EOpConvFloat16ToInt64:
5734 case glslang::EOpConvFloatToInt64:
5735 case glslang::EOpConvDoubleToInt64:
5736 convOp = spv::OpConvertFToS;
5737 break;
5738
5739 case glslang::EOpConvUint8ToInt8:
5740 case glslang::EOpConvInt8ToUint8:
5741 case glslang::EOpConvUint16ToInt16:
5742 case glslang::EOpConvInt16ToUint16:
5743 case glslang::EOpConvUintToInt:
5744 case glslang::EOpConvIntToUint:
5745 case glslang::EOpConvUint64ToInt64:
5746 case glslang::EOpConvInt64ToUint64:
5747 if (builder.isInSpecConstCodeGenMode()) {
5748 // Build zero scalar or vector for OpIAdd.
5749 if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) {
5750 zero = builder.makeUint8Constant(0);
5751 } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) {
5752 zero = builder.makeUint16Constant(0);
5753 } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) {
5754 zero = builder.makeUint64Constant(0);
5755 } else {
5756 zero = builder.makeUintConstant(0);
5757 }
5758 zero = makeSmearedConstant(zero, vectorSize);
5759 // Use OpIAdd, instead of OpBitcast to do the conversion when
5760 // generating for OpSpecConstantOp instruction.
5761 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
5762 }
5763 // For normal run-time conversion instruction, use OpBitcast.
5764 convOp = spv::OpBitcast;
5765 break;
5766
5767 case glslang::EOpConvFloat16ToUint8:
5768 case glslang::EOpConvFloatToUint8:
5769 case glslang::EOpConvDoubleToUint8:
5770 case glslang::EOpConvFloat16ToUint16:
5771 case glslang::EOpConvFloatToUint16:
5772 case glslang::EOpConvDoubleToUint16:
5773 case glslang::EOpConvFloat16ToUint:
5774 case glslang::EOpConvFloatToUint:
5775 case glslang::EOpConvDoubleToUint:
5776 case glslang::EOpConvFloatToUint64:
5777 case glslang::EOpConvDoubleToUint64:
5778 case glslang::EOpConvFloat16ToUint64:
5779 convOp = spv::OpConvertFToU;
5780 break;
5781
5782 case glslang::EOpConvInt8ToInt16:
5783 case glslang::EOpConvInt8ToInt:
5784 case glslang::EOpConvInt8ToInt64:
5785 case glslang::EOpConvInt16ToInt8:
5786 case glslang::EOpConvInt16ToInt:
5787 case glslang::EOpConvInt16ToInt64:
5788 case glslang::EOpConvIntToInt8:
5789 case glslang::EOpConvIntToInt16:
5790 case glslang::EOpConvIntToInt64:
5791 case glslang::EOpConvInt64ToInt8:
5792 case glslang::EOpConvInt64ToInt16:
5793 case glslang::EOpConvInt64ToInt:
5794 convOp = spv::OpSConvert;
5795 break;
5796
5797 case glslang::EOpConvUint8ToUint16:
5798 case glslang::EOpConvUint8ToUint:
5799 case glslang::EOpConvUint8ToUint64:
5800 case glslang::EOpConvUint16ToUint8:
5801 case glslang::EOpConvUint16ToUint:
5802 case glslang::EOpConvUint16ToUint64:
5803 case glslang::EOpConvUintToUint8:
5804 case glslang::EOpConvUintToUint16:
5805 case glslang::EOpConvUintToUint64:
5806 case glslang::EOpConvUint64ToUint8:
5807 case glslang::EOpConvUint64ToUint16:
5808 case glslang::EOpConvUint64ToUint:
5809 convOp = spv::OpUConvert;
5810 break;
5811
5812 case glslang::EOpConvInt8ToUint16:
5813 case glslang::EOpConvInt8ToUint:
5814 case glslang::EOpConvInt8ToUint64:
5815 case glslang::EOpConvInt16ToUint8:
5816 case glslang::EOpConvInt16ToUint:
5817 case glslang::EOpConvInt16ToUint64:
5818 case glslang::EOpConvIntToUint8:
5819 case glslang::EOpConvIntToUint16:
5820 case glslang::EOpConvIntToUint64:
5821 case glslang::EOpConvInt64ToUint8:
5822 case glslang::EOpConvInt64ToUint16:
5823 case glslang::EOpConvInt64ToUint:
5824 case glslang::EOpConvUint8ToInt16:
5825 case glslang::EOpConvUint8ToInt:
5826 case glslang::EOpConvUint8ToInt64:
5827 case glslang::EOpConvUint16ToInt8:
5828 case glslang::EOpConvUint16ToInt:
5829 case glslang::EOpConvUint16ToInt64:
5830 case glslang::EOpConvUintToInt8:
5831 case glslang::EOpConvUintToInt16:
5832 case glslang::EOpConvUintToInt64:
5833 case glslang::EOpConvUint64ToInt8:
5834 case glslang::EOpConvUint64ToInt16:
5835 case glslang::EOpConvUint64ToInt:
5836 // OpSConvert/OpUConvert + OpBitCast
5837 operand = createIntWidthConversion(op, operand, vectorSize);
5838
5839 if (builder.isInSpecConstCodeGenMode()) {
5840 // Build zero scalar or vector for OpIAdd.
5841 switch(op) {
5842 case glslang::EOpConvInt16ToUint8:
5843 case glslang::EOpConvIntToUint8:
5844 case glslang::EOpConvInt64ToUint8:
5845 case glslang::EOpConvUint16ToInt8:
5846 case glslang::EOpConvUintToInt8:
5847 case glslang::EOpConvUint64ToInt8:
5848 zero = builder.makeUint8Constant(0);
5849 break;
5850 case glslang::EOpConvInt8ToUint16:
5851 case glslang::EOpConvIntToUint16:
5852 case glslang::EOpConvInt64ToUint16:
5853 case glslang::EOpConvUint8ToInt16:
5854 case glslang::EOpConvUintToInt16:
5855 case glslang::EOpConvUint64ToInt16:
5856 zero = builder.makeUint16Constant(0);
5857 break;
5858 case glslang::EOpConvInt8ToUint:
5859 case glslang::EOpConvInt16ToUint:
5860 case glslang::EOpConvInt64ToUint:
5861 case glslang::EOpConvUint8ToInt:
5862 case glslang::EOpConvUint16ToInt:
5863 case glslang::EOpConvUint64ToInt:
5864 zero = builder.makeUintConstant(0);
5865 break;
5866 case glslang::EOpConvInt8ToUint64:
5867 case glslang::EOpConvInt16ToUint64:
5868 case glslang::EOpConvIntToUint64:
5869 case glslang::EOpConvUint8ToInt64:
5870 case glslang::EOpConvUint16ToInt64:
5871 case glslang::EOpConvUintToInt64:
5872 zero = builder.makeUint64Constant(0);
5873 break;
5874 default:
5875 assert(false && "Default missing");
5876 break;
5877 }
5878 zero = makeSmearedConstant(zero, vectorSize);
5879 // Use OpIAdd, instead of OpBitcast to do the conversion when
5880 // generating for OpSpecConstantOp instruction.
5881 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
5882 }
5883 // For normal run-time conversion instruction, use OpBitcast.
5884 convOp = spv::OpBitcast;
5885 break;
5886 case glslang::EOpConvUint64ToPtr:
5887 convOp = spv::OpConvertUToPtr;
5888 break;
5889 case glslang::EOpConvPtrToUint64:
5890 convOp = spv::OpConvertPtrToU;
5891 break;
5892 default:
5893 break;
5894 }
5895
5896 spv::Id result = 0;
5897 if (convOp == spv::OpNop)
5898 return result;
5899
5900 if (convOp == spv::OpSelect) {
5901 zero = makeSmearedConstant(zero, vectorSize);
5902 one = makeSmearedConstant(one, vectorSize);
5903 result = builder.createTriOp(convOp, destType, operand, one, zero);
5904 } else
5905 result = builder.createUnaryOp(convOp, destType, operand);
5906
5907 result = builder.setPrecision(result, decorations.precision);
5908 builder.addDecoration(result, decorations.nonUniform);
5909 return result;
5910 }
5911
5912 spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
5913 {
5914 if (vectorSize == 0)
5915 return constant;
5916
5917 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
5918 std::vector<spv::Id> components;
5919 for (int c = 0; c < vectorSize; ++c)
5920 components.push_back(constant);
5921 return builder.makeCompositeConstant(vectorTypeId, components);
5922 }
5923
5924 // For glslang ops that map to SPV atomic opCodes
5925 spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
5926 {
5927 spv::Op opCode = spv::OpNop;
5928
5929 switch (op) {
5930 case glslang::EOpAtomicAdd:
5931 case glslang::EOpImageAtomicAdd:
5932 case glslang::EOpAtomicCounterAdd:
5933 opCode = spv::OpAtomicIAdd;
5934 break;
5935 case glslang::EOpAtomicCounterSubtract:
5936 opCode = spv::OpAtomicISub;
5937 break;
5938 case glslang::EOpAtomicMin:
5939 case glslang::EOpImageAtomicMin:
5940 case glslang::EOpAtomicCounterMin:
5941 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin;
5942 break;
5943 case glslang::EOpAtomicMax:
5944 case glslang::EOpImageAtomicMax:
5945 case glslang::EOpAtomicCounterMax:
5946 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax;
5947 break;
5948 case glslang::EOpAtomicAnd:
5949 case glslang::EOpImageAtomicAnd:
5950 case glslang::EOpAtomicCounterAnd:
5951 opCode = spv::OpAtomicAnd;
5952 break;
5953 case glslang::EOpAtomicOr:
5954 case glslang::EOpImageAtomicOr:
5955 case glslang::EOpAtomicCounterOr:
5956 opCode = spv::OpAtomicOr;
5957 break;
5958 case glslang::EOpAtomicXor:
5959 case glslang::EOpImageAtomicXor:
5960 case glslang::EOpAtomicCounterXor:
5961 opCode = spv::OpAtomicXor;
5962 break;
5963 case glslang::EOpAtomicExchange:
5964 case glslang::EOpImageAtomicExchange:
5965 case glslang::EOpAtomicCounterExchange:
5966 opCode = spv::OpAtomicExchange;
5967 break;
5968 case glslang::EOpAtomicCompSwap:
5969 case glslang::EOpImageAtomicCompSwap:
5970 case glslang::EOpAtomicCounterCompSwap:
5971 opCode = spv::OpAtomicCompareExchange;
5972 break;
5973 case glslang::EOpAtomicCounterIncrement:
5974 opCode = spv::OpAtomicIIncrement;
5975 break;
5976 case glslang::EOpAtomicCounterDecrement:
5977 opCode = spv::OpAtomicIDecrement;
5978 break;
5979 case glslang::EOpAtomicCounter:
5980 case glslang::EOpImageAtomicLoad:
5981 case glslang::EOpAtomicLoad:
5982 opCode = spv::OpAtomicLoad;
5983 break;
5984 case glslang::EOpAtomicStore:
5985 case glslang::EOpImageAtomicStore:
5986 opCode = spv::OpAtomicStore;
5987 break;
5988 default:
5989 assert(0);
5990 break;
5991 }
5992
5993 if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64)
5994 builder.addCapability(spv::CapabilityInt64Atomics);
5995
5996 // Sort out the operands
5997 // - mapping from glslang -> SPV
5998 // - there are extra SPV operands that are optional in glslang
5999 // - compare-exchange swaps the value and comparator
6000 // - compare-exchange has an extra memory semantics
6001 // - EOpAtomicCounterDecrement needs a post decrement
6002 spv::Id pointerId = 0, compareId = 0, valueId = 0;
6003 // scope defaults to Device in the old model, QueueFamilyKHR in the new model
6004 spv::Id scopeId;
6005 if (glslangIntermediate->usingVulkanMemoryModel()) {
6006 scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR);
6007 } else {
6008 scopeId = builder.makeUintConstant(spv::ScopeDevice);
6009 }
6010 // semantics default to relaxed
6011 spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone);
6012 spv::Id semanticsId2 = semanticsId;
6013
6014 pointerId = operands[0];
6015 if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) {
6016 // no additional operands
6017 } else if (opCode == spv::OpAtomicCompareExchange) {
6018 compareId = operands[1];
6019 valueId = operands[2];
6020 if (operands.size() > 3) {
6021 scopeId = operands[3];
6022 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5]));
6023 semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7]));
6024 }
6025 } else if (opCode == spv::OpAtomicLoad) {
6026 if (operands.size() > 1) {
6027 scopeId = operands[1];
6028 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]));
6029 }
6030 } else {
6031 // atomic store or RMW
6032 valueId = operands[1];
6033 if (operands.size() > 2) {
6034 scopeId = operands[2];
6035 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4]));
6036 }
6037 }
6038
6039 // Check for capabilities
6040 unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2);
6041 if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
6042 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6043 }
6044
6045 if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) {
6046 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6047 }
6048
6049 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
6050 spvAtomicOperands.push_back(pointerId);
6051 spvAtomicOperands.push_back(scopeId);
6052 spvAtomicOperands.push_back(semanticsId);
6053 if (opCode == spv::OpAtomicCompareExchange) {
6054 spvAtomicOperands.push_back(semanticsId2);
6055 spvAtomicOperands.push_back(valueId);
6056 spvAtomicOperands.push_back(compareId);
6057 } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) {
6058 spvAtomicOperands.push_back(valueId);
6059 }
6060
6061 if (opCode == spv::OpAtomicStore) {
6062 builder.createNoResultOp(opCode, spvAtomicOperands);
6063 return 0;
6064 } else {
6065 spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands);
6066
6067 // GLSL and HLSL atomic-counter decrement return post-decrement value,
6068 // while SPIR-V returns pre-decrement value. Translate between these semantics.
6069 if (op == glslang::EOpAtomicCounterDecrement)
6070 resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1));
6071
6072 return resultId;
6073 }
6074 }
6075
6076 // Create group invocation operations.
6077 spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
6078 {
6079 #ifdef AMD_EXTENSIONS
6080 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6081 bool isFloat = isTypeFloat(typeProxy);
6082 #endif
6083
6084 spv::Op opCode = spv::OpNop;
6085 std::vector<spv::IdImmediate> spvGroupOperands;
6086 spv::GroupOperation groupOperation = spv::GroupOperationMax;
6087
6088 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
6089 op == glslang::EOpReadInvocation) {
6090 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
6091 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
6092 } else if (op == glslang::EOpAnyInvocation ||
6093 op == glslang::EOpAllInvocations ||
6094 op == glslang::EOpAllInvocationsEqual) {
6095 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
6096 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
6097 } else {
6098 builder.addCapability(spv::CapabilityGroups);
6099 #ifdef AMD_EXTENSIONS
6100 if (op == glslang::EOpMinInvocationsNonUniform ||
6101 op == glslang::EOpMaxInvocationsNonUniform ||
6102 op == glslang::EOpAddInvocationsNonUniform ||
6103 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6104 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6105 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
6106 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
6107 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
6108 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
6109 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
6110 #endif
6111
6112 #ifdef AMD_EXTENSIONS
6113 switch (op) {
6114 case glslang::EOpMinInvocations:
6115 case glslang::EOpMaxInvocations:
6116 case glslang::EOpAddInvocations:
6117 case glslang::EOpMinInvocationsNonUniform:
6118 case glslang::EOpMaxInvocationsNonUniform:
6119 case glslang::EOpAddInvocationsNonUniform:
6120 groupOperation = spv::GroupOperationReduce;
6121 break;
6122 case glslang::EOpMinInvocationsInclusiveScan:
6123 case glslang::EOpMaxInvocationsInclusiveScan:
6124 case glslang::EOpAddInvocationsInclusiveScan:
6125 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6126 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6127 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6128 groupOperation = spv::GroupOperationInclusiveScan;
6129 break;
6130 case glslang::EOpMinInvocationsExclusiveScan:
6131 case glslang::EOpMaxInvocationsExclusiveScan:
6132 case glslang::EOpAddInvocationsExclusiveScan:
6133 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6134 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6135 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6136 groupOperation = spv::GroupOperationExclusiveScan;
6137 break;
6138 default:
6139 break;
6140 }
6141 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6142 spvGroupOperands.push_back(scope);
6143 if (groupOperation != spv::GroupOperationMax) {
6144 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
6145 spvGroupOperands.push_back(groupOp);
6146 }
6147 #endif
6148 }
6149
6150 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) {
6151 spv::IdImmediate op = { true, *opIt };
6152 spvGroupOperands.push_back(op);
6153 }
6154
6155 switch (op) {
6156 case glslang::EOpAnyInvocation:
6157 opCode = spv::OpSubgroupAnyKHR;
6158 break;
6159 case glslang::EOpAllInvocations:
6160 opCode = spv::OpSubgroupAllKHR;
6161 break;
6162 case glslang::EOpAllInvocationsEqual:
6163 opCode = spv::OpSubgroupAllEqualKHR;
6164 break;
6165 case glslang::EOpReadInvocation:
6166 opCode = spv::OpSubgroupReadInvocationKHR;
6167 if (builder.isVectorType(typeId))
6168 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
6169 break;
6170 case glslang::EOpReadFirstInvocation:
6171 opCode = spv::OpSubgroupFirstInvocationKHR;
6172 break;
6173 case glslang::EOpBallot:
6174 {
6175 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
6176 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
6177 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
6178 //
6179 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
6180 //
6181 spv::Id uintType = builder.makeUintType(32);
6182 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
6183 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
6184
6185 std::vector<spv::Id> components;
6186 components.push_back(builder.createCompositeExtract(result, uintType, 0));
6187 components.push_back(builder.createCompositeExtract(result, uintType, 1));
6188
6189 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
6190 return builder.createUnaryOp(spv::OpBitcast, typeId,
6191 builder.createCompositeConstruct(uvec2Type, components));
6192 }
6193
6194 #ifdef AMD_EXTENSIONS
6195 case glslang::EOpMinInvocations:
6196 case glslang::EOpMaxInvocations:
6197 case glslang::EOpAddInvocations:
6198 case glslang::EOpMinInvocationsInclusiveScan:
6199 case glslang::EOpMaxInvocationsInclusiveScan:
6200 case glslang::EOpAddInvocationsInclusiveScan:
6201 case glslang::EOpMinInvocationsExclusiveScan:
6202 case glslang::EOpMaxInvocationsExclusiveScan:
6203 case glslang::EOpAddInvocationsExclusiveScan:
6204 if (op == glslang::EOpMinInvocations ||
6205 op == glslang::EOpMinInvocationsInclusiveScan ||
6206 op == glslang::EOpMinInvocationsExclusiveScan) {
6207 if (isFloat)
6208 opCode = spv::OpGroupFMin;
6209 else {
6210 if (isUnsigned)
6211 opCode = spv::OpGroupUMin;
6212 else
6213 opCode = spv::OpGroupSMin;
6214 }
6215 } else if (op == glslang::EOpMaxInvocations ||
6216 op == glslang::EOpMaxInvocationsInclusiveScan ||
6217 op == glslang::EOpMaxInvocationsExclusiveScan) {
6218 if (isFloat)
6219 opCode = spv::OpGroupFMax;
6220 else {
6221 if (isUnsigned)
6222 opCode = spv::OpGroupUMax;
6223 else
6224 opCode = spv::OpGroupSMax;
6225 }
6226 } else {
6227 if (isFloat)
6228 opCode = spv::OpGroupFAdd;
6229 else
6230 opCode = spv::OpGroupIAdd;
6231 }
6232
6233 if (builder.isVectorType(typeId))
6234 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
6235
6236 break;
6237 case glslang::EOpMinInvocationsNonUniform:
6238 case glslang::EOpMaxInvocationsNonUniform:
6239 case glslang::EOpAddInvocationsNonUniform:
6240 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6241 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6242 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6243 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6244 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6245 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6246 if (op == glslang::EOpMinInvocationsNonUniform ||
6247 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6248 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
6249 if (isFloat)
6250 opCode = spv::OpGroupFMinNonUniformAMD;
6251 else {
6252 if (isUnsigned)
6253 opCode = spv::OpGroupUMinNonUniformAMD;
6254 else
6255 opCode = spv::OpGroupSMinNonUniformAMD;
6256 }
6257 }
6258 else if (op == glslang::EOpMaxInvocationsNonUniform ||
6259 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6260 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
6261 if (isFloat)
6262 opCode = spv::OpGroupFMaxNonUniformAMD;
6263 else {
6264 if (isUnsigned)
6265 opCode = spv::OpGroupUMaxNonUniformAMD;
6266 else
6267 opCode = spv::OpGroupSMaxNonUniformAMD;
6268 }
6269 }
6270 else {
6271 if (isFloat)
6272 opCode = spv::OpGroupFAddNonUniformAMD;
6273 else
6274 opCode = spv::OpGroupIAddNonUniformAMD;
6275 }
6276
6277 if (builder.isVectorType(typeId))
6278 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
6279
6280 break;
6281 #endif
6282 default:
6283 logger->missingFunctionality("invocation operation");
6284 return spv::NoResult;
6285 }
6286
6287 assert(opCode != spv::OpNop);
6288 return builder.createOp(opCode, typeId, spvGroupOperands);
6289 }
6290
6291 // Create group invocation operations on a vector
6292 spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
6293 spv::Id typeId, std::vector<spv::Id>& operands)
6294 {
6295 #ifdef AMD_EXTENSIONS
6296 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
6297 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
6298 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
6299 op == spv::OpSubgroupReadInvocationKHR ||
6300 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD ||
6301 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD ||
6302 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
6303 #else
6304 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
6305 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
6306 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
6307 op == spv::OpSubgroupReadInvocationKHR);
6308 #endif
6309
6310 // Handle group invocation operations scalar by scalar.
6311 // The result type is the same type as the original type.
6312 // The algorithm is to:
6313 // - break the vector into scalars
6314 // - apply the operation to each scalar
6315 // - make a vector out the scalar results
6316
6317 // get the types sorted out
6318 int numComponents = builder.getNumComponents(operands[0]);
6319 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
6320 std::vector<spv::Id> results;
6321
6322 // do each scalar op
6323 for (int comp = 0; comp < numComponents; ++comp) {
6324 std::vector<unsigned int> indexes;
6325 indexes.push_back(comp);
6326 spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) };
6327 std::vector<spv::IdImmediate> spvGroupOperands;
6328 if (op == spv::OpSubgroupReadInvocationKHR) {
6329 spvGroupOperands.push_back(scalar);
6330 spv::IdImmediate operand = { true, operands[1] };
6331 spvGroupOperands.push_back(operand);
6332 } else if (op == spv::OpGroupBroadcast) {
6333 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6334 spvGroupOperands.push_back(scope);
6335 spvGroupOperands.push_back(scalar);
6336 spv::IdImmediate operand = { true, operands[1] };
6337 spvGroupOperands.push_back(operand);
6338 } else {
6339 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6340 spvGroupOperands.push_back(scope);
6341 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
6342 spvGroupOperands.push_back(groupOp);
6343 spvGroupOperands.push_back(scalar);
6344 }
6345
6346 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
6347 }
6348
6349 // put the pieces together
6350 return builder.createCompositeConstruct(typeId, results);
6351 }
6352
6353 // Create subgroup invocation operations.
6354 spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
6355 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
6356 {
6357 // Add the required capabilities.
6358 switch (op) {
6359 case glslang::EOpSubgroupElect:
6360 builder.addCapability(spv::CapabilityGroupNonUniform);
6361 break;
6362 case glslang::EOpSubgroupAll:
6363 case glslang::EOpSubgroupAny:
6364 case glslang::EOpSubgroupAllEqual:
6365 builder.addCapability(spv::CapabilityGroupNonUniform);
6366 builder.addCapability(spv::CapabilityGroupNonUniformVote);
6367 break;
6368 case glslang::EOpSubgroupBroadcast:
6369 case glslang::EOpSubgroupBroadcastFirst:
6370 case glslang::EOpSubgroupBallot:
6371 case glslang::EOpSubgroupInverseBallot:
6372 case glslang::EOpSubgroupBallotBitExtract:
6373 case glslang::EOpSubgroupBallotBitCount:
6374 case glslang::EOpSubgroupBallotInclusiveBitCount:
6375 case glslang::EOpSubgroupBallotExclusiveBitCount:
6376 case glslang::EOpSubgroupBallotFindLSB:
6377 case glslang::EOpSubgroupBallotFindMSB:
6378 builder.addCapability(spv::CapabilityGroupNonUniform);
6379 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
6380 break;
6381 case glslang::EOpSubgroupShuffle:
6382 case glslang::EOpSubgroupShuffleXor:
6383 builder.addCapability(spv::CapabilityGroupNonUniform);
6384 builder.addCapability(spv::CapabilityGroupNonUniformShuffle);
6385 break;
6386 case glslang::EOpSubgroupShuffleUp:
6387 case glslang::EOpSubgroupShuffleDown:
6388 builder.addCapability(spv::CapabilityGroupNonUniform);
6389 builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative);
6390 break;
6391 case glslang::EOpSubgroupAdd:
6392 case glslang::EOpSubgroupMul:
6393 case glslang::EOpSubgroupMin:
6394 case glslang::EOpSubgroupMax:
6395 case glslang::EOpSubgroupAnd:
6396 case glslang::EOpSubgroupOr:
6397 case glslang::EOpSubgroupXor:
6398 case glslang::EOpSubgroupInclusiveAdd:
6399 case glslang::EOpSubgroupInclusiveMul:
6400 case glslang::EOpSubgroupInclusiveMin:
6401 case glslang::EOpSubgroupInclusiveMax:
6402 case glslang::EOpSubgroupInclusiveAnd:
6403 case glslang::EOpSubgroupInclusiveOr:
6404 case glslang::EOpSubgroupInclusiveXor:
6405 case glslang::EOpSubgroupExclusiveAdd:
6406 case glslang::EOpSubgroupExclusiveMul:
6407 case glslang::EOpSubgroupExclusiveMin:
6408 case glslang::EOpSubgroupExclusiveMax:
6409 case glslang::EOpSubgroupExclusiveAnd:
6410 case glslang::EOpSubgroupExclusiveOr:
6411 case glslang::EOpSubgroupExclusiveXor:
6412 builder.addCapability(spv::CapabilityGroupNonUniform);
6413 builder.addCapability(spv::CapabilityGroupNonUniformArithmetic);
6414 break;
6415 case glslang::EOpSubgroupClusteredAdd:
6416 case glslang::EOpSubgroupClusteredMul:
6417 case glslang::EOpSubgroupClusteredMin:
6418 case glslang::EOpSubgroupClusteredMax:
6419 case glslang::EOpSubgroupClusteredAnd:
6420 case glslang::EOpSubgroupClusteredOr:
6421 case glslang::EOpSubgroupClusteredXor:
6422 builder.addCapability(spv::CapabilityGroupNonUniform);
6423 builder.addCapability(spv::CapabilityGroupNonUniformClustered);
6424 break;
6425 case glslang::EOpSubgroupQuadBroadcast:
6426 case glslang::EOpSubgroupQuadSwapHorizontal:
6427 case glslang::EOpSubgroupQuadSwapVertical:
6428 case glslang::EOpSubgroupQuadSwapDiagonal:
6429 builder.addCapability(spv::CapabilityGroupNonUniform);
6430 builder.addCapability(spv::CapabilityGroupNonUniformQuad);
6431 break;
6432 #ifdef NV_EXTENSIONS
6433 case glslang::EOpSubgroupPartitionedAdd:
6434 case glslang::EOpSubgroupPartitionedMul:
6435 case glslang::EOpSubgroupPartitionedMin:
6436 case glslang::EOpSubgroupPartitionedMax:
6437 case glslang::EOpSubgroupPartitionedAnd:
6438 case glslang::EOpSubgroupPartitionedOr:
6439 case glslang::EOpSubgroupPartitionedXor:
6440 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6441 case glslang::EOpSubgroupPartitionedInclusiveMul:
6442 case glslang::EOpSubgroupPartitionedInclusiveMin:
6443 case glslang::EOpSubgroupPartitionedInclusiveMax:
6444 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6445 case glslang::EOpSubgroupPartitionedInclusiveOr:
6446 case glslang::EOpSubgroupPartitionedInclusiveXor:
6447 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6448 case glslang::EOpSubgroupPartitionedExclusiveMul:
6449 case glslang::EOpSubgroupPartitionedExclusiveMin:
6450 case glslang::EOpSubgroupPartitionedExclusiveMax:
6451 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6452 case glslang::EOpSubgroupPartitionedExclusiveOr:
6453 case glslang::EOpSubgroupPartitionedExclusiveXor:
6454 builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned);
6455 builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV);
6456 break;
6457 #endif
6458 default: assert(0 && "Unhandled subgroup operation!");
6459 }
6460
6461 const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
6462 const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble;
6463 const bool isBool = typeProxy == glslang::EbtBool;
6464
6465 spv::Op opCode = spv::OpNop;
6466
6467 // Figure out which opcode to use.
6468 switch (op) {
6469 case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break;
6470 case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break;
6471 case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break;
6472 case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break;
6473 case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break;
6474 case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break;
6475 case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break;
6476 case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break;
6477 case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break;
6478 case glslang::EOpSubgroupBallotBitCount:
6479 case glslang::EOpSubgroupBallotInclusiveBitCount:
6480 case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break;
6481 case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break;
6482 case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break;
6483 case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break;
6484 case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break;
6485 case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break;
6486 case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break;
6487 case glslang::EOpSubgroupAdd:
6488 case glslang::EOpSubgroupInclusiveAdd:
6489 case glslang::EOpSubgroupExclusiveAdd:
6490 case glslang::EOpSubgroupClusteredAdd:
6491 #ifdef NV_EXTENSIONS
6492 case glslang::EOpSubgroupPartitionedAdd:
6493 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6494 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6495 #endif
6496 if (isFloat) {
6497 opCode = spv::OpGroupNonUniformFAdd;
6498 } else {
6499 opCode = spv::OpGroupNonUniformIAdd;
6500 }
6501 break;
6502 case glslang::EOpSubgroupMul:
6503 case glslang::EOpSubgroupInclusiveMul:
6504 case glslang::EOpSubgroupExclusiveMul:
6505 case glslang::EOpSubgroupClusteredMul:
6506 #ifdef NV_EXTENSIONS
6507 case glslang::EOpSubgroupPartitionedMul:
6508 case glslang::EOpSubgroupPartitionedInclusiveMul:
6509 case glslang::EOpSubgroupPartitionedExclusiveMul:
6510 #endif
6511 if (isFloat) {
6512 opCode = spv::OpGroupNonUniformFMul;
6513 } else {
6514 opCode = spv::OpGroupNonUniformIMul;
6515 }
6516 break;
6517 case glslang::EOpSubgroupMin:
6518 case glslang::EOpSubgroupInclusiveMin:
6519 case glslang::EOpSubgroupExclusiveMin:
6520 case glslang::EOpSubgroupClusteredMin:
6521 #ifdef NV_EXTENSIONS
6522 case glslang::EOpSubgroupPartitionedMin:
6523 case glslang::EOpSubgroupPartitionedInclusiveMin:
6524 case glslang::EOpSubgroupPartitionedExclusiveMin:
6525 #endif
6526 if (isFloat) {
6527 opCode = spv::OpGroupNonUniformFMin;
6528 } else if (isUnsigned) {
6529 opCode = spv::OpGroupNonUniformUMin;
6530 } else {
6531 opCode = spv::OpGroupNonUniformSMin;
6532 }
6533 break;
6534 case glslang::EOpSubgroupMax:
6535 case glslang::EOpSubgroupInclusiveMax:
6536 case glslang::EOpSubgroupExclusiveMax:
6537 case glslang::EOpSubgroupClusteredMax:
6538 #ifdef NV_EXTENSIONS
6539 case glslang::EOpSubgroupPartitionedMax:
6540 case glslang::EOpSubgroupPartitionedInclusiveMax:
6541 case glslang::EOpSubgroupPartitionedExclusiveMax:
6542 #endif
6543 if (isFloat) {
6544 opCode = spv::OpGroupNonUniformFMax;
6545 } else if (isUnsigned) {
6546 opCode = spv::OpGroupNonUniformUMax;
6547 } else {
6548 opCode = spv::OpGroupNonUniformSMax;
6549 }
6550 break;
6551 case glslang::EOpSubgroupAnd:
6552 case glslang::EOpSubgroupInclusiveAnd:
6553 case glslang::EOpSubgroupExclusiveAnd:
6554 case glslang::EOpSubgroupClusteredAnd:
6555 #ifdef NV_EXTENSIONS
6556 case glslang::EOpSubgroupPartitionedAnd:
6557 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6558 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6559 #endif
6560 if (isBool) {
6561 opCode = spv::OpGroupNonUniformLogicalAnd;
6562 } else {
6563 opCode = spv::OpGroupNonUniformBitwiseAnd;
6564 }
6565 break;
6566 case glslang::EOpSubgroupOr:
6567 case glslang::EOpSubgroupInclusiveOr:
6568 case glslang::EOpSubgroupExclusiveOr:
6569 case glslang::EOpSubgroupClusteredOr:
6570 #ifdef NV_EXTENSIONS
6571 case glslang::EOpSubgroupPartitionedOr:
6572 case glslang::EOpSubgroupPartitionedInclusiveOr:
6573 case glslang::EOpSubgroupPartitionedExclusiveOr:
6574 #endif
6575 if (isBool) {
6576 opCode = spv::OpGroupNonUniformLogicalOr;
6577 } else {
6578 opCode = spv::OpGroupNonUniformBitwiseOr;
6579 }
6580 break;
6581 case glslang::EOpSubgroupXor:
6582 case glslang::EOpSubgroupInclusiveXor:
6583 case glslang::EOpSubgroupExclusiveXor:
6584 case glslang::EOpSubgroupClusteredXor:
6585 #ifdef NV_EXTENSIONS
6586 case glslang::EOpSubgroupPartitionedXor:
6587 case glslang::EOpSubgroupPartitionedInclusiveXor:
6588 case glslang::EOpSubgroupPartitionedExclusiveXor:
6589 #endif
6590 if (isBool) {
6591 opCode = spv::OpGroupNonUniformLogicalXor;
6592 } else {
6593 opCode = spv::OpGroupNonUniformBitwiseXor;
6594 }
6595 break;
6596 case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break;
6597 case glslang::EOpSubgroupQuadSwapHorizontal:
6598 case glslang::EOpSubgroupQuadSwapVertical:
6599 case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break;
6600 default: assert(0 && "Unhandled subgroup operation!");
6601 }
6602
6603 // get the right Group Operation
6604 spv::GroupOperation groupOperation = spv::GroupOperationMax;
6605 switch (op) {
6606 default:
6607 break;
6608 case glslang::EOpSubgroupBallotBitCount:
6609 case glslang::EOpSubgroupAdd:
6610 case glslang::EOpSubgroupMul:
6611 case glslang::EOpSubgroupMin:
6612 case glslang::EOpSubgroupMax:
6613 case glslang::EOpSubgroupAnd:
6614 case glslang::EOpSubgroupOr:
6615 case glslang::EOpSubgroupXor:
6616 groupOperation = spv::GroupOperationReduce;
6617 break;
6618 case glslang::EOpSubgroupBallotInclusiveBitCount:
6619 case glslang::EOpSubgroupInclusiveAdd:
6620 case glslang::EOpSubgroupInclusiveMul:
6621 case glslang::EOpSubgroupInclusiveMin:
6622 case glslang::EOpSubgroupInclusiveMax:
6623 case glslang::EOpSubgroupInclusiveAnd:
6624 case glslang::EOpSubgroupInclusiveOr:
6625 case glslang::EOpSubgroupInclusiveXor:
6626 groupOperation = spv::GroupOperationInclusiveScan;
6627 break;
6628 case glslang::EOpSubgroupBallotExclusiveBitCount:
6629 case glslang::EOpSubgroupExclusiveAdd:
6630 case glslang::EOpSubgroupExclusiveMul:
6631 case glslang::EOpSubgroupExclusiveMin:
6632 case glslang::EOpSubgroupExclusiveMax:
6633 case glslang::EOpSubgroupExclusiveAnd:
6634 case glslang::EOpSubgroupExclusiveOr:
6635 case glslang::EOpSubgroupExclusiveXor:
6636 groupOperation = spv::GroupOperationExclusiveScan;
6637 break;
6638 case glslang::EOpSubgroupClusteredAdd:
6639 case glslang::EOpSubgroupClusteredMul:
6640 case glslang::EOpSubgroupClusteredMin:
6641 case glslang::EOpSubgroupClusteredMax:
6642 case glslang::EOpSubgroupClusteredAnd:
6643 case glslang::EOpSubgroupClusteredOr:
6644 case glslang::EOpSubgroupClusteredXor:
6645 groupOperation = spv::GroupOperationClusteredReduce;
6646 break;
6647 #ifdef NV_EXTENSIONS
6648 case glslang::EOpSubgroupPartitionedAdd:
6649 case glslang::EOpSubgroupPartitionedMul:
6650 case glslang::EOpSubgroupPartitionedMin:
6651 case glslang::EOpSubgroupPartitionedMax:
6652 case glslang::EOpSubgroupPartitionedAnd:
6653 case glslang::EOpSubgroupPartitionedOr:
6654 case glslang::EOpSubgroupPartitionedXor:
6655 groupOperation = spv::GroupOperationPartitionedReduceNV;
6656 break;
6657 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6658 case glslang::EOpSubgroupPartitionedInclusiveMul:
6659 case glslang::EOpSubgroupPartitionedInclusiveMin:
6660 case glslang::EOpSubgroupPartitionedInclusiveMax:
6661 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6662 case glslang::EOpSubgroupPartitionedInclusiveOr:
6663 case glslang::EOpSubgroupPartitionedInclusiveXor:
6664 groupOperation = spv::GroupOperationPartitionedInclusiveScanNV;
6665 break;
6666 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6667 case glslang::EOpSubgroupPartitionedExclusiveMul:
6668 case glslang::EOpSubgroupPartitionedExclusiveMin:
6669 case glslang::EOpSubgroupPartitionedExclusiveMax:
6670 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6671 case glslang::EOpSubgroupPartitionedExclusiveOr:
6672 case glslang::EOpSubgroupPartitionedExclusiveXor:
6673 groupOperation = spv::GroupOperationPartitionedExclusiveScanNV;
6674 break;
6675 #endif
6676 }
6677
6678 // build the instruction
6679 std::vector<spv::IdImmediate> spvGroupOperands;
6680
6681 // Every operation begins with the Execution Scope operand.
6682 spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6683 spvGroupOperands.push_back(executionScope);
6684
6685 // Next, for all operations that use a Group Operation, push that as an operand.
6686 if (groupOperation != spv::GroupOperationMax) {
6687 spv::IdImmediate groupOperand = { false, (unsigned)groupOperation };
6688 spvGroupOperands.push_back(groupOperand);
6689 }
6690
6691 // Push back the operands next.
6692 for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) {
6693 spv::IdImmediate operand = { true, *opIt };
6694 spvGroupOperands.push_back(operand);
6695 }
6696
6697 // Some opcodes have additional operands.
6698 spv::Id directionId = spv::NoResult;
6699 switch (op) {
6700 default: break;
6701 case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break;
6702 case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break;
6703 case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break;
6704 }
6705 if (directionId != spv::NoResult) {
6706 spv::IdImmediate direction = { true, directionId };
6707 spvGroupOperands.push_back(direction);
6708 }
6709
6710 return builder.createOp(opCode, typeId, spvGroupOperands);
6711 }
6712
6713 spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
6714 {
6715 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6716 bool isFloat = isTypeFloat(typeProxy);
6717
6718 spv::Op opCode = spv::OpNop;
6719 int extBuiltins = -1;
6720 int libCall = -1;
6721 size_t consumedOperands = operands.size();
6722 spv::Id typeId0 = 0;
6723 if (consumedOperands > 0)
6724 typeId0 = builder.getTypeId(operands[0]);
6725 spv::Id typeId1 = 0;
6726 if (consumedOperands > 1)
6727 typeId1 = builder.getTypeId(operands[1]);
6728 spv::Id frexpIntType = 0;
6729
6730 switch (op) {
6731 case glslang::EOpMin:
6732 if (isFloat)
6733 libCall = spv::GLSLstd450FMin;
6734 else if (isUnsigned)
6735 libCall = spv::GLSLstd450UMin;
6736 else
6737 libCall = spv::GLSLstd450SMin;
6738 builder.promoteScalar(precision, operands.front(), operands.back());
6739 break;
6740 case glslang::EOpModf:
6741 libCall = spv::GLSLstd450Modf;
6742 break;
6743 case glslang::EOpMax:
6744 if (isFloat)
6745 libCall = spv::GLSLstd450FMax;
6746 else if (isUnsigned)
6747 libCall = spv::GLSLstd450UMax;
6748 else
6749 libCall = spv::GLSLstd450SMax;
6750 builder.promoteScalar(precision, operands.front(), operands.back());
6751 break;
6752 case glslang::EOpPow:
6753 libCall = spv::GLSLstd450Pow;
6754 break;
6755 case glslang::EOpDot:
6756 opCode = spv::OpDot;
6757 break;
6758 case glslang::EOpAtan:
6759 libCall = spv::GLSLstd450Atan2;
6760 break;
6761
6762 case glslang::EOpClamp:
6763 if (isFloat)
6764 libCall = spv::GLSLstd450FClamp;
6765 else if (isUnsigned)
6766 libCall = spv::GLSLstd450UClamp;
6767 else
6768 libCall = spv::GLSLstd450SClamp;
6769 builder.promoteScalar(precision, operands.front(), operands[1]);
6770 builder.promoteScalar(precision, operands.front(), operands[2]);
6771 break;
6772 case glslang::EOpMix:
6773 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
6774 assert(isFloat);
6775 libCall = spv::GLSLstd450FMix;
6776 } else {
6777 opCode = spv::OpSelect;
6778 std::swap(operands.front(), operands.back());
6779 }
6780 builder.promoteScalar(precision, operands.front(), operands.back());
6781 break;
6782 case glslang::EOpStep:
6783 libCall = spv::GLSLstd450Step;
6784 builder.promoteScalar(precision, operands.front(), operands.back());
6785 break;
6786 case glslang::EOpSmoothStep:
6787 libCall = spv::GLSLstd450SmoothStep;
6788 builder.promoteScalar(precision, operands[0], operands[2]);
6789 builder.promoteScalar(precision, operands[1], operands[2]);
6790 break;
6791
6792 case glslang::EOpDistance:
6793 libCall = spv::GLSLstd450Distance;
6794 break;
6795 case glslang::EOpCross:
6796 libCall = spv::GLSLstd450Cross;
6797 break;
6798 case glslang::EOpFaceForward:
6799 libCall = spv::GLSLstd450FaceForward;
6800 break;
6801 case glslang::EOpReflect:
6802 libCall = spv::GLSLstd450Reflect;
6803 break;
6804 case glslang::EOpRefract:
6805 libCall = spv::GLSLstd450Refract;
6806 break;
6807 case glslang::EOpInterpolateAtSample:
6808 #ifdef AMD_EXTENSIONS
6809 if (typeProxy == glslang::EbtFloat16)
6810 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6811 #endif
6812 libCall = spv::GLSLstd450InterpolateAtSample;
6813 break;
6814 case glslang::EOpInterpolateAtOffset:
6815 #ifdef AMD_EXTENSIONS
6816 if (typeProxy == glslang::EbtFloat16)
6817 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6818 #endif
6819 libCall = spv::GLSLstd450InterpolateAtOffset;
6820 break;
6821 case glslang::EOpAddCarry:
6822 opCode = spv::OpIAddCarry;
6823 typeId = builder.makeStructResultType(typeId0, typeId0);
6824 consumedOperands = 2;
6825 break;
6826 case glslang::EOpSubBorrow:
6827 opCode = spv::OpISubBorrow;
6828 typeId = builder.makeStructResultType(typeId0, typeId0);
6829 consumedOperands = 2;
6830 break;
6831 case glslang::EOpUMulExtended:
6832 opCode = spv::OpUMulExtended;
6833 typeId = builder.makeStructResultType(typeId0, typeId0);
6834 consumedOperands = 2;
6835 break;
6836 case glslang::EOpIMulExtended:
6837 opCode = spv::OpSMulExtended;
6838 typeId = builder.makeStructResultType(typeId0, typeId0);
6839 consumedOperands = 2;
6840 break;
6841 case glslang::EOpBitfieldExtract:
6842 if (isUnsigned)
6843 opCode = spv::OpBitFieldUExtract;
6844 else
6845 opCode = spv::OpBitFieldSExtract;
6846 break;
6847 case glslang::EOpBitfieldInsert:
6848 opCode = spv::OpBitFieldInsert;
6849 break;
6850
6851 case glslang::EOpFma:
6852 libCall = spv::GLSLstd450Fma;
6853 break;
6854 case glslang::EOpFrexp:
6855 {
6856 libCall = spv::GLSLstd450FrexpStruct;
6857 assert(builder.isPointerType(typeId1));
6858 typeId1 = builder.getContainedTypeId(typeId1);
6859 int width = builder.getScalarTypeWidth(typeId1);
6860 #ifdef AMD_EXTENSIONS
6861 if (width == 16)
6862 // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16
6863 builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16);
6864 #endif
6865 if (builder.getNumComponents(operands[0]) == 1)
6866 frexpIntType = builder.makeIntegerType(width, true);
6867 else
6868 frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0]));
6869 typeId = builder.makeStructResultType(typeId0, frexpIntType);
6870 consumedOperands = 1;
6871 }
6872 break;
6873 case glslang::EOpLdexp:
6874 libCall = spv::GLSLstd450Ldexp;
6875 break;
6876
6877 case glslang::EOpReadInvocation:
6878 return createInvocationsOperation(op, typeId, operands, typeProxy);
6879
6880 case glslang::EOpSubgroupBroadcast:
6881 case glslang::EOpSubgroupBallotBitExtract:
6882 case glslang::EOpSubgroupShuffle:
6883 case glslang::EOpSubgroupShuffleXor:
6884 case glslang::EOpSubgroupShuffleUp:
6885 case glslang::EOpSubgroupShuffleDown:
6886 case glslang::EOpSubgroupClusteredAdd:
6887 case glslang::EOpSubgroupClusteredMul:
6888 case glslang::EOpSubgroupClusteredMin:
6889 case glslang::EOpSubgroupClusteredMax:
6890 case glslang::EOpSubgroupClusteredAnd:
6891 case glslang::EOpSubgroupClusteredOr:
6892 case glslang::EOpSubgroupClusteredXor:
6893 case glslang::EOpSubgroupQuadBroadcast:
6894 #ifdef NV_EXTENSIONS
6895 case glslang::EOpSubgroupPartitionedAdd:
6896 case glslang::EOpSubgroupPartitionedMul:
6897 case glslang::EOpSubgroupPartitionedMin:
6898 case glslang::EOpSubgroupPartitionedMax:
6899 case glslang::EOpSubgroupPartitionedAnd:
6900 case glslang::EOpSubgroupPartitionedOr:
6901 case glslang::EOpSubgroupPartitionedXor:
6902 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6903 case glslang::EOpSubgroupPartitionedInclusiveMul:
6904 case glslang::EOpSubgroupPartitionedInclusiveMin:
6905 case glslang::EOpSubgroupPartitionedInclusiveMax:
6906 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6907 case glslang::EOpSubgroupPartitionedInclusiveOr:
6908 case glslang::EOpSubgroupPartitionedInclusiveXor:
6909 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6910 case glslang::EOpSubgroupPartitionedExclusiveMul:
6911 case glslang::EOpSubgroupPartitionedExclusiveMin:
6912 case glslang::EOpSubgroupPartitionedExclusiveMax:
6913 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6914 case glslang::EOpSubgroupPartitionedExclusiveOr:
6915 case glslang::EOpSubgroupPartitionedExclusiveXor:
6916 #endif
6917 return createSubgroupOperation(op, typeId, operands, typeProxy);
6918
6919 #ifdef AMD_EXTENSIONS
6920 case glslang::EOpSwizzleInvocations:
6921 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6922 libCall = spv::SwizzleInvocationsAMD;
6923 break;
6924 case glslang::EOpSwizzleInvocationsMasked:
6925 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6926 libCall = spv::SwizzleInvocationsMaskedAMD;
6927 break;
6928 case glslang::EOpWriteInvocation:
6929 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6930 libCall = spv::WriteInvocationAMD;
6931 break;
6932
6933 case glslang::EOpMin3:
6934 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6935 if (isFloat)
6936 libCall = spv::FMin3AMD;
6937 else {
6938 if (isUnsigned)
6939 libCall = spv::UMin3AMD;
6940 else
6941 libCall = spv::SMin3AMD;
6942 }
6943 break;
6944 case glslang::EOpMax3:
6945 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6946 if (isFloat)
6947 libCall = spv::FMax3AMD;
6948 else {
6949 if (isUnsigned)
6950 libCall = spv::UMax3AMD;
6951 else
6952 libCall = spv::SMax3AMD;
6953 }
6954 break;
6955 case glslang::EOpMid3:
6956 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6957 if (isFloat)
6958 libCall = spv::FMid3AMD;
6959 else {
6960 if (isUnsigned)
6961 libCall = spv::UMid3AMD;
6962 else
6963 libCall = spv::SMid3AMD;
6964 }
6965 break;
6966
6967 case glslang::EOpInterpolateAtVertex:
6968 if (typeProxy == glslang::EbtFloat16)
6969 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6970 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
6971 libCall = spv::InterpolateAtVertexAMD;
6972 break;
6973 #endif
6974 case glslang::EOpBarrier:
6975 {
6976 // This is for the extended controlBarrier function, with four operands.
6977 // The unextended barrier() goes through createNoArgOperation.
6978 assert(operands.size() == 4);
6979 unsigned int executionScope = builder.getConstantScalar(operands[0]);
6980 unsigned int memoryScope = builder.getConstantScalar(operands[1]);
6981 unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]);
6982 builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
6983 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
6984 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6985 }
6986 if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) {
6987 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6988 }
6989 return 0;
6990 }
6991 break;
6992 case glslang::EOpMemoryBarrier:
6993 {
6994 // This is for the extended memoryBarrier function, with three operands.
6995 // The unextended memoryBarrier() goes through createNoArgOperation.
6996 assert(operands.size() == 3);
6997 unsigned int memoryScope = builder.getConstantScalar(operands[0]);
6998 unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]);
6999 builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
7000 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
7001 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7002 }
7003 if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) {
7004 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7005 }
7006 return 0;
7007 }
7008 break;
7009
7010 #ifdef NV_EXTENSIONS
7011 case glslang::EOpReportIntersectionNV:
7012 {
7013 typeId = builder.makeBoolType();
7014 opCode = spv::OpReportIntersectionNV;
7015 }
7016 break;
7017 case glslang::EOpTraceNV:
7018 {
7019 builder.createNoResultOp(spv::OpTraceNV, operands);
7020 return 0;
7021 }
7022 break;
7023 case glslang::EOpExecuteCallableNV:
7024 {
7025 builder.createNoResultOp(spv::OpExecuteCallableNV, operands);
7026 return 0;
7027 }
7028 break;
7029 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
7030 builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands);
7031 return 0;
7032 #endif
7033 default:
7034 return 0;
7035 }
7036
7037 spv::Id id = 0;
7038 if (libCall >= 0) {
7039 // Use an extended instruction from the standard library.
7040 // Construct the call arguments, without modifying the original operands vector.
7041 // We might need the remaining arguments, e.g. in the EOpFrexp case.
7042 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
7043 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
7044 } else if (opCode == spv::OpDot && !isFloat) {
7045 // int dot(int, int)
7046 // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached
7047 const int componentCount = builder.getNumComponents(operands[0]);
7048 spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]);
7049 builder.setPrecision(mulOp, precision);
7050 id = builder.createCompositeExtract(mulOp, typeId, 0);
7051 for (int i = 1; i < componentCount; ++i) {
7052 builder.setPrecision(id, precision);
7053 id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i));
7054 }
7055 } else {
7056 switch (consumedOperands) {
7057 case 0:
7058 // should all be handled by visitAggregate and createNoArgOperation
7059 assert(0);
7060 return 0;
7061 case 1:
7062 // should all be handled by createUnaryOperation
7063 assert(0);
7064 return 0;
7065 case 2:
7066 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
7067 break;
7068 default:
7069 // anything 3 or over doesn't have l-value operands, so all should be consumed
7070 assert(consumedOperands == operands.size());
7071 id = builder.createOp(opCode, typeId, operands);
7072 break;
7073 }
7074 }
7075
7076 // Decode the return types that were structures
7077 switch (op) {
7078 case glslang::EOpAddCarry:
7079 case glslang::EOpSubBorrow:
7080 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7081 id = builder.createCompositeExtract(id, typeId0, 0);
7082 break;
7083 case glslang::EOpUMulExtended:
7084 case glslang::EOpIMulExtended:
7085 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
7086 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7087 break;
7088 case glslang::EOpFrexp:
7089 {
7090 assert(operands.size() == 2);
7091 if (builder.isFloatType(builder.getScalarTypeId(typeId1))) {
7092 // "exp" is floating-point type (from HLSL intrinsic)
7093 spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1);
7094 member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1);
7095 builder.createStore(member1, operands[1]);
7096 } else
7097 // "exp" is integer type (from GLSL built-in function)
7098 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
7099 id = builder.createCompositeExtract(id, typeId0, 0);
7100 }
7101 break;
7102 default:
7103 break;
7104 }
7105
7106 return builder.setPrecision(id, precision);
7107 }
7108
7109 // Intrinsics with no arguments (or no return value, and no precision).
7110 spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
7111 {
7112 // GLSL memory barriers use queuefamily scope in new model, device scope in old model
7113 spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
7114
7115 switch (op) {
7116 case glslang::EOpEmitVertex:
7117 builder.createNoResultOp(spv::OpEmitVertex);
7118 return 0;
7119 case glslang::EOpEndPrimitive:
7120 builder.createNoResultOp(spv::OpEndPrimitive);
7121 return 0;
7122 case glslang::EOpBarrier:
7123 if (glslangIntermediate->getStage() == EShLangTessControl) {
7124 if (glslangIntermediate->usingVulkanMemoryModel()) {
7125 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7126 spv::MemorySemanticsOutputMemoryKHRMask |
7127 spv::MemorySemanticsAcquireReleaseMask);
7128 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7129 } else {
7130 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone);
7131 }
7132 } else {
7133 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7134 spv::MemorySemanticsWorkgroupMemoryMask |
7135 spv::MemorySemanticsAcquireReleaseMask);
7136 }
7137 return 0;
7138 case glslang::EOpMemoryBarrier:
7139 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory |
7140 spv::MemorySemanticsAcquireReleaseMask);
7141 return 0;
7142 case glslang::EOpMemoryBarrierAtomicCounter:
7143 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask |
7144 spv::MemorySemanticsAcquireReleaseMask);
7145 return 0;
7146 case glslang::EOpMemoryBarrierBuffer:
7147 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask |
7148 spv::MemorySemanticsAcquireReleaseMask);
7149 return 0;
7150 case glslang::EOpMemoryBarrierImage:
7151 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask |
7152 spv::MemorySemanticsAcquireReleaseMask);
7153 return 0;
7154 case glslang::EOpMemoryBarrierShared:
7155 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask |
7156 spv::MemorySemanticsAcquireReleaseMask);
7157 return 0;
7158 case glslang::EOpGroupMemoryBarrier:
7159 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory |
7160 spv::MemorySemanticsAcquireReleaseMask);
7161 return 0;
7162 case glslang::EOpAllMemoryBarrierWithGroupSync:
7163 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice,
7164 spv::MemorySemanticsAllMemory |
7165 spv::MemorySemanticsAcquireReleaseMask);
7166 return 0;
7167 case glslang::EOpDeviceMemoryBarrier:
7168 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7169 spv::MemorySemanticsImageMemoryMask |
7170 spv::MemorySemanticsAcquireReleaseMask);
7171 return 0;
7172 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
7173 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7174 spv::MemorySemanticsImageMemoryMask |
7175 spv::MemorySemanticsAcquireReleaseMask);
7176 return 0;
7177 case glslang::EOpWorkgroupMemoryBarrier:
7178 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7179 spv::MemorySemanticsAcquireReleaseMask);
7180 return 0;
7181 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
7182 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7183 spv::MemorySemanticsWorkgroupMemoryMask |
7184 spv::MemorySemanticsAcquireReleaseMask);
7185 return 0;
7186 case glslang::EOpSubgroupBarrier:
7187 builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7188 spv::MemorySemanticsAcquireReleaseMask);
7189 return spv::NoResult;
7190 case glslang::EOpSubgroupMemoryBarrier:
7191 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7192 spv::MemorySemanticsAcquireReleaseMask);
7193 return spv::NoResult;
7194 case glslang::EOpSubgroupMemoryBarrierBuffer:
7195 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask |
7196 spv::MemorySemanticsAcquireReleaseMask);
7197 return spv::NoResult;
7198 case glslang::EOpSubgroupMemoryBarrierImage:
7199 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask |
7200 spv::MemorySemanticsAcquireReleaseMask);
7201 return spv::NoResult;
7202 case glslang::EOpSubgroupMemoryBarrierShared:
7203 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7204 spv::MemorySemanticsAcquireReleaseMask);
7205 return spv::NoResult;
7206 case glslang::EOpSubgroupElect: {
7207 std::vector<spv::Id> operands;
7208 return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid);
7209 }
7210 #ifdef AMD_EXTENSIONS
7211 case glslang::EOpTime:
7212 {
7213 std::vector<spv::Id> args; // Dummy arguments
7214 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
7215 return builder.setPrecision(id, precision);
7216 }
7217 #endif
7218 #ifdef NV_EXTENSIONS
7219 case glslang::EOpIgnoreIntersectionNV:
7220 builder.createNoResultOp(spv::OpIgnoreIntersectionNV);
7221 return 0;
7222 case glslang::EOpTerminateRayNV:
7223 builder.createNoResultOp(spv::OpTerminateRayNV);
7224 return 0;
7225 #endif
7226 default:
7227 logger->missingFunctionality("unknown operation with no arguments");
7228 return 0;
7229 }
7230 }
7231
7232 spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
7233 {
7234 auto iter = symbolValues.find(symbol->getId());
7235 spv::Id id;
7236 if (symbolValues.end() != iter) {
7237 id = iter->second;
7238 return id;
7239 }
7240
7241 // it was not found, create it
7242 id = createSpvVariable(symbol);
7243 symbolValues[symbol->getId()] = id;
7244
7245 if (symbol->getBasicType() != glslang::EbtBlock) {
7246 builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
7247 builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
7248 builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
7249 #ifdef NV_EXTENSIONS
7250 addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier());
7251 #endif
7252 if (symbol->getType().getQualifier().hasSpecConstantId())
7253 builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
7254 if (symbol->getQualifier().hasIndex())
7255 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
7256 if (symbol->getQualifier().hasComponent())
7257 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
7258 // atomic counters use this:
7259 if (symbol->getQualifier().hasOffset())
7260 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
7261 }
7262
7263 if (symbol->getQualifier().hasLocation())
7264 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
7265 builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
7266 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
7267 builder.addCapability(spv::CapabilityGeometryStreams);
7268 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
7269 }
7270 if (symbol->getQualifier().hasSet())
7271 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
7272 else if (IsDescriptorResource(symbol->getType())) {
7273 // default to 0
7274 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
7275 }
7276 if (symbol->getQualifier().hasBinding())
7277 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
7278 else if (IsDescriptorResource(symbol->getType())) {
7279 // default to 0
7280 builder.addDecoration(id, spv::DecorationBinding, 0);
7281 }
7282 if (symbol->getQualifier().hasAttachment())
7283 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
7284 if (glslangIntermediate->getXfbMode()) {
7285 builder.addCapability(spv::CapabilityTransformFeedback);
7286 if (symbol->getQualifier().hasXfbBuffer()) {
7287 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
7288 unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer);
7289 if (stride != glslang::TQualifier::layoutXfbStrideEnd)
7290 builder.addDecoration(id, spv::DecorationXfbStride, stride);
7291 }
7292 if (symbol->getQualifier().hasXfbOffset())
7293 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
7294 }
7295
7296 if (symbol->getType().isImage()) {
7297 std::vector<spv::Decoration> memory;
7298 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel());
7299 for (unsigned int i = 0; i < memory.size(); ++i)
7300 builder.addDecoration(id, memory[i]);
7301 }
7302
7303 // built-in variable decorations
7304 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
7305 if (builtIn != spv::BuiltInMax)
7306 builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
7307
7308 // nonuniform
7309 builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier()));
7310
7311 #ifdef NV_EXTENSIONS
7312 if (builtIn == spv::BuiltInSampleMask) {
7313 spv::Decoration decoration;
7314 // GL_NV_sample_mask_override_coverage extension
7315 if (glslangIntermediate->getLayoutOverrideCoverage())
7316 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
7317 else
7318 decoration = (spv::Decoration)spv::DecorationMax;
7319 builder.addDecoration(id, decoration);
7320 if (decoration != spv::DecorationMax) {
7321 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
7322 }
7323 }
7324 else if (builtIn == spv::BuiltInLayer) {
7325 // SPV_NV_viewport_array2 extension
7326 if (symbol->getQualifier().layoutViewportRelative) {
7327 builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
7328 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
7329 builder.addExtension(spv::E_SPV_NV_viewport_array2);
7330 }
7331 if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) {
7332 builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
7333 symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
7334 builder.addCapability(spv::CapabilityShaderStereoViewNV);
7335 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
7336 }
7337 }
7338
7339 if (symbol->getQualifier().layoutPassthrough) {
7340 builder.addDecoration(id, spv::DecorationPassthroughNV);
7341 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
7342 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
7343 }
7344 if (symbol->getQualifier().pervertexNV) {
7345 builder.addDecoration(id, spv::DecorationPerVertexNV);
7346 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
7347 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
7348 }
7349 #endif
7350
7351 if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) {
7352 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
7353 builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
7354 symbol->getType().getQualifier().semanticName);
7355 }
7356
7357 if (symbol->getBasicType() == glslang::EbtReference) {
7358 builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
7359 }
7360
7361 return id;
7362 }
7363
7364 #ifdef NV_EXTENSIONS
7365 // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object
7366 void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
7367 {
7368 if (member >= 0) {
7369 if (qualifier.perPrimitiveNV) {
7370 // Need to add capability/extension for fragment shader.
7371 // Mesh shader already adds this by default.
7372 if (glslangIntermediate->getStage() == EShLangFragment) {
7373 builder.addCapability(spv::CapabilityMeshShadingNV);
7374 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7375 }
7376 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV);
7377 }
7378 if (qualifier.perViewNV)
7379 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV);
7380 if (qualifier.perTaskNV)
7381 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV);
7382 } else {
7383 if (qualifier.perPrimitiveNV) {
7384 // Need to add capability/extension for fragment shader.
7385 // Mesh shader already adds this by default.
7386 if (glslangIntermediate->getStage() == EShLangFragment) {
7387 builder.addCapability(spv::CapabilityMeshShadingNV);
7388 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7389 }
7390 builder.addDecoration(id, spv::DecorationPerPrimitiveNV);
7391 }
7392 if (qualifier.perViewNV)
7393 builder.addDecoration(id, spv::DecorationPerViewNV);
7394 if (qualifier.perTaskNV)
7395 builder.addDecoration(id, spv::DecorationPerTaskNV);
7396 }
7397 }
7398 #endif
7399
7400 // Make a full tree of instructions to build a SPIR-V specialization constant,
7401 // or regular constant if possible.
7402 //
7403 // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
7404 //
7405 // Recursively walk the nodes. The nodes form a tree whose leaves are
7406 // regular constants, which themselves are trees that createSpvConstant()
7407 // recursively walks. So, this function walks the "top" of the tree:
7408 // - emit specialization constant-building instructions for specConstant
7409 // - when running into a non-spec-constant, switch to createSpvConstant()
7410 spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
7411 {
7412 assert(node.getQualifier().isConstant());
7413
7414 // Handle front-end constants first (non-specialization constants).
7415 if (! node.getQualifier().specConstant) {
7416 // hand off to the non-spec-constant path
7417 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
7418 int nextConst = 0;
7419 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
7420 nextConst, false);
7421 }
7422
7423 // We now know we have a specialization constant to build
7424
7425 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
7426 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
7427 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
7428 std::vector<spv::Id> dimConstId;
7429 for (int dim = 0; dim < 3; ++dim) {
7430 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
7431 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
7432 if (specConst) {
7433 builder.addDecoration(dimConstId.back(), spv::DecorationSpecId,
7434 glslangIntermediate->getLocalSizeSpecId(dim));
7435 }
7436 }
7437 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
7438 }
7439
7440 // An AST node labelled as specialization constant should be a symbol node.
7441 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
7442 if (auto* sn = node.getAsSymbolNode()) {
7443 spv::Id result;
7444 if (auto* sub_tree = sn->getConstSubtree()) {
7445 // Traverse the constant constructor sub tree like generating normal run-time instructions.
7446 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
7447 // will set the builder into spec constant op instruction generating mode.
7448 sub_tree->traverse(this);
7449 result = accessChainLoad(sub_tree->getType());
7450 } else if (auto* const_union_array = &sn->getConstArray()) {
7451 int nextConst = 0;
7452 result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
7453 } else {
7454 logger->missingFunctionality("Invalid initializer for spec onstant.");
7455 return spv::NoResult;
7456 }
7457 builder.addName(result, sn->getName().c_str());
7458 return result;
7459 }
7460
7461 // Neither a front-end constant node, nor a specialization constant node with constant union array or
7462 // constant sub tree as initializer.
7463 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
7464 return spv::NoResult;
7465 }
7466
7467 // Use 'consts' as the flattened glslang source of scalar constants to recursively
7468 // build the aggregate SPIR-V constant.
7469 //
7470 // If there are not enough elements present in 'consts', 0 will be substituted;
7471 // an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
7472 //
7473 spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
7474 {
7475 // vector of constants for SPIR-V
7476 std::vector<spv::Id> spvConsts;
7477
7478 // Type is used for struct and array constants
7479 spv::Id typeId = convertGlslangToSpvType(glslangType);
7480
7481 if (glslangType.isArray()) {
7482 glslang::TType elementType(glslangType, 0);
7483 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
7484 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
7485 } else if (glslangType.isMatrix()) {
7486 glslang::TType vectorType(glslangType, 0);
7487 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
7488 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
7489 } else if (glslangType.isStruct()) {
7490 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
7491 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
7492 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
7493 } else if (glslangType.getVectorSize() > 1) {
7494 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
7495 bool zero = nextConst >= consts.size();
7496 switch (glslangType.getBasicType()) {
7497 case glslang::EbtInt8:
7498 spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const()));
7499 break;
7500 case glslang::EbtUint8:
7501 spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const()));
7502 break;
7503 case glslang::EbtInt16:
7504 spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const()));
7505 break;
7506 case glslang::EbtUint16:
7507 spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const()));
7508 break;
7509 case glslang::EbtInt:
7510 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
7511 break;
7512 case glslang::EbtUint:
7513 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
7514 break;
7515 case glslang::EbtInt64:
7516 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
7517 break;
7518 case glslang::EbtUint64:
7519 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
7520 break;
7521 case glslang::EbtFloat:
7522 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7523 break;
7524 case glslang::EbtDouble:
7525 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
7526 break;
7527 case glslang::EbtFloat16:
7528 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7529 break;
7530 case glslang::EbtBool:
7531 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
7532 break;
7533 default:
7534 assert(0);
7535 break;
7536 }
7537 ++nextConst;
7538 }
7539 } else {
7540 // we have a non-aggregate (scalar) constant
7541 bool zero = nextConst >= consts.size();
7542 spv::Id scalar = 0;
7543 switch (glslangType.getBasicType()) {
7544 case glslang::EbtInt8:
7545 scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant);
7546 break;
7547 case glslang::EbtUint8:
7548 scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant);
7549 break;
7550 case glslang::EbtInt16:
7551 scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant);
7552 break;
7553 case glslang::EbtUint16:
7554 scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant);
7555 break;
7556 case glslang::EbtInt:
7557 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
7558 break;
7559 case glslang::EbtUint:
7560 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
7561 break;
7562 case glslang::EbtInt64:
7563 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
7564 break;
7565 case glslang::EbtUint64:
7566 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
7567 break;
7568 case glslang::EbtFloat:
7569 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
7570 break;
7571 case glslang::EbtDouble:
7572 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
7573 break;
7574 case glslang::EbtFloat16:
7575 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
7576 break;
7577 case glslang::EbtBool:
7578 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
7579 break;
7580 default:
7581 assert(0);
7582 break;
7583 }
7584 ++nextConst;
7585 return scalar;
7586 }
7587
7588 return builder.makeCompositeConstant(typeId, spvConsts);
7589 }
7590
7591 // Return true if the node is a constant or symbol whose reading has no
7592 // non-trivial observable cost or effect.
7593 bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
7594 {
7595 // don't know what this is
7596 if (node == nullptr)
7597 return false;
7598
7599 // a constant is safe
7600 if (node->getAsConstantUnion() != nullptr)
7601 return true;
7602
7603 // not a symbol means non-trivial
7604 if (node->getAsSymbolNode() == nullptr)
7605 return false;
7606
7607 // a symbol, depends on what's being read
7608 switch (node->getType().getQualifier().storage) {
7609 case glslang::EvqTemporary:
7610 case glslang::EvqGlobal:
7611 case glslang::EvqIn:
7612 case glslang::EvqInOut:
7613 case glslang::EvqConst:
7614 case glslang::EvqConstReadOnly:
7615 case glslang::EvqUniform:
7616 return true;
7617 default:
7618 return false;
7619 }
7620 }
7621
7622 // A node is trivial if it is a single operation with no side effects.
7623 // HLSL (and/or vectors) are always trivial, as it does not short circuit.
7624 // Otherwise, error on the side of saying non-trivial.
7625 // Return true if trivial.
7626 bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
7627 {
7628 if (node == nullptr)
7629 return false;
7630
7631 // count non scalars as trivial, as well as anything coming from HLSL
7632 if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl)
7633 return true;
7634
7635 // symbols and constants are trivial
7636 if (isTrivialLeaf(node))
7637 return true;
7638
7639 // otherwise, it needs to be a simple operation or one or two leaf nodes
7640
7641 // not a simple operation
7642 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
7643 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
7644 if (binaryNode == nullptr && unaryNode == nullptr)
7645 return false;
7646
7647 // not on leaf nodes
7648 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
7649 return false;
7650
7651 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
7652 return false;
7653 }
7654
7655 switch (node->getAsOperator()->getOp()) {
7656 case glslang::EOpLogicalNot:
7657 case glslang::EOpConvIntToBool:
7658 case glslang::EOpConvUintToBool:
7659 case glslang::EOpConvFloatToBool:
7660 case glslang::EOpConvDoubleToBool:
7661 case glslang::EOpEqual:
7662 case glslang::EOpNotEqual:
7663 case glslang::EOpLessThan:
7664 case glslang::EOpGreaterThan:
7665 case glslang::EOpLessThanEqual:
7666 case glslang::EOpGreaterThanEqual:
7667 case glslang::EOpIndexDirect:
7668 case glslang::EOpIndexDirectStruct:
7669 case glslang::EOpLogicalXor:
7670 case glslang::EOpAny:
7671 case glslang::EOpAll:
7672 return true;
7673 default:
7674 return false;
7675 }
7676 }
7677
7678 // Emit short-circuiting code, where 'right' is never evaluated unless
7679 // the left side is true (for &&) or false (for ||).
7680 spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right)
7681 {
7682 spv::Id boolTypeId = builder.makeBoolType();
7683
7684 // emit left operand
7685 builder.clearAccessChain();
7686 left.traverse(this);
7687 spv::Id leftId = accessChainLoad(left.getType());
7688
7689 // Operands to accumulate OpPhi operands
7690 std::vector<spv::Id> phiOperands;
7691 // accumulate left operand's phi information
7692 phiOperands.push_back(leftId);
7693 phiOperands.push_back(builder.getBuildPoint()->getId());
7694
7695 // Make the two kinds of operation symmetric with a "!"
7696 // || => emit "if (! left) result = right"
7697 // && => emit "if ( left) result = right"
7698 //
7699 // TODO: this runtime "not" for || could be avoided by adding functionality
7700 // to 'builder' to have an "else" without an "then"
7701 if (op == glslang::EOpLogicalOr)
7702 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
7703
7704 // make an "if" based on the left value
7705 spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder);
7706
7707 // emit right operand as the "then" part of the "if"
7708 builder.clearAccessChain();
7709 right.traverse(this);
7710 spv::Id rightId = accessChainLoad(right.getType());
7711
7712 // accumulate left operand's phi information
7713 phiOperands.push_back(rightId);
7714 phiOperands.push_back(builder.getBuildPoint()->getId());
7715
7716 // finish the "if"
7717 ifBuilder.makeEndIf();
7718
7719 // phi together the two results
7720 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
7721 }
7722
7723 #ifdef AMD_EXTENSIONS
7724 // Return type Id of the imported set of extended instructions corresponds to the name.
7725 // Import this set if it has not been imported yet.
7726 spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
7727 {
7728 if (extBuiltinMap.find(name) != extBuiltinMap.end())
7729 return extBuiltinMap[name];
7730 else {
7731 builder.addExtension(name);
7732 spv::Id extBuiltins = builder.import(name);
7733 extBuiltinMap[name] = extBuiltins;
7734 return extBuiltins;
7735 }
7736 }
7737 #endif
7738
7739 }; // end anonymous namespace
7740
7741 namespace glslang {
7742
7743 void GetSpirvVersion(std::string& version)
7744 {
7745 const int bufSize = 100;
7746 char buf[bufSize];
7747 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
7748 version = buf;
7749 }
7750
7751 // For low-order part of the generator's magic number. Bump up
7752 // when there is a change in the style (e.g., if SSA form changes,
7753 // or a different instruction sequence to do something gets used).
7754 int GetSpirvGeneratorVersion()
7755 {
7756 // return 1; // start
7757 // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V
7758 // return 3; // change/correct barrier-instruction operands, to match memory model group decisions
7759 // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component
7760 // return 5; // make OpArrayLength result type be an int with signedness of 0
7761 // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code,
7762 // versions 4 and 6 each generate OpArrayLength as it has long been done
7763 return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent
7764 }
7765
7766 // Write SPIR-V out to a binary file
7767 void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
7768 {
7769 std::ofstream out;
7770 out.open(baseName, std::ios::binary | std::ios::out);
7771 if (out.fail())
7772 printf("ERROR: Failed to open file: %s\n", baseName);
7773 for (int i = 0; i < (int)spirv.size(); ++i) {
7774 unsigned int word = spirv[i];
7775 out.write((const char*)&word, 4);
7776 }
7777 out.close();
7778 }
7779
7780 // Write SPIR-V out to a text file with 32-bit hexadecimal words
7781 void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
7782 {
7783 std::ofstream out;
7784 out.open(baseName, std::ios::binary | std::ios::out);
7785 if (out.fail())
7786 printf("ERROR: Failed to open file: %s\n", baseName);
7787 out << "\t// " <<
7788 GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL <<
7789 std::endl;
7790 if (varName != nullptr) {
7791 out << "\t #pragma once" << std::endl;
7792 out << "const uint32_t " << varName << "[] = {" << std::endl;
7793 }
7794 const int WORDS_PER_LINE = 8;
7795 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
7796 out << "\t";
7797 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
7798 const unsigned int word = spirv[i + j];
7799 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
7800 if (i + j + 1 < (int)spirv.size()) {
7801 out << ",";
7802 }
7803 }
7804 out << std::endl;
7805 }
7806 if (varName != nullptr) {
7807 out << "};";
7808 }
7809 out.close();
7810 }
7811
7812 //
7813 // Set up the glslang traversal
7814 //
7815 void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
7816 {
7817 spv::SpvBuildLogger logger;
7818 GlslangToSpv(intermediate, spirv, &logger, options);
7819 }
7820
7821 void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
7822 spv::SpvBuildLogger* logger, SpvOptions* options)
7823 {
7824 TIntermNode* root = intermediate.getTreeRoot();
7825
7826 if (root == 0)
7827 return;
7828
7829 SpvOptions defaultOptions;
7830 if (options == nullptr)
7831 options = &defaultOptions;
7832
7833 GetThreadPoolAllocator().push();
7834
7835 TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
7836 root->traverse(&it);
7837 it.finishSpv();
7838 it.dumpSpv(spirv);
7839
7840 #if ENABLE_OPT
7841 // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
7842 // eg. forward and remove memory writes of opaque types.
7843 if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer)
7844 SpirvToolsLegalize(intermediate, spirv, logger, options);
7845
7846 if (options->validate)
7847 SpirvToolsValidate(intermediate, spirv, logger);
7848
7849 if (options->disassemble)
7850 SpirvToolsDisassemble(std::cout, spirv);
7851
7852 #endif
7853
7854 GetThreadPoolAllocator().pop();
7855 }
7856
7857 }; // end namespace glslang
7858