1 | // |
2 | // Copyright (C) 2014-2016 LunarG, Inc. |
3 | // Copyright (C) 2015-2020 Google, Inc. |
4 | // Copyright (C) 2017 ARM Limited. |
5 | // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
6 | // |
7 | // All rights reserved. |
8 | // |
9 | // Redistribution and use in source and binary forms, with or without |
10 | // modification, are permitted provided that the following conditions |
11 | // are met: |
12 | // |
13 | // Redistributions of source code must retain the above copyright |
14 | // notice, this list of conditions and the following disclaimer. |
15 | // |
16 | // Redistributions in binary form must reproduce the above |
17 | // copyright notice, this list of conditions and the following |
18 | // disclaimer in the documentation and/or other materials provided |
19 | // with the distribution. |
20 | // |
21 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
22 | // contributors may be used to endorse or promote products derived |
23 | // from this software without specific prior written permission. |
24 | // |
25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
29 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36 | // POSSIBILITY OF SUCH DAMAGE. |
37 | |
38 | // |
39 | // Visit the nodes in the glslang intermediate tree representation to |
40 | // translate them to SPIR-V. |
41 | // |
42 | |
43 | #include "spirv.hpp" |
44 | #include "GlslangToSpv.h" |
45 | #include "SpvBuilder.h" |
46 | namespace spv { |
47 | #include "GLSL.std.450.h" |
48 | #include "GLSL.ext.KHR.h" |
49 | #include "GLSL.ext.EXT.h" |
50 | #include "GLSL.ext.AMD.h" |
51 | #include "GLSL.ext.NV.h" |
52 | #include "NonSemanticDebugPrintf.h" |
53 | } |
54 | |
55 | // Glslang includes |
56 | #include "../glslang/MachineIndependent/localintermediate.h" |
57 | #include "../glslang/MachineIndependent/SymbolTable.h" |
58 | #include "../glslang/Include/Common.h" |
59 | |
60 | // Build-time generated includes |
61 | #include "glslang/build_info.h" |
62 | |
63 | #include <fstream> |
64 | #include <iomanip> |
65 | #include <list> |
66 | #include <map> |
67 | #include <stack> |
68 | #include <string> |
69 | #include <vector> |
70 | |
71 | namespace { |
72 | |
73 | namespace { |
74 | class SpecConstantOpModeGuard { |
75 | public: |
76 | SpecConstantOpModeGuard(spv::Builder* builder) |
77 | : builder_(builder) { |
78 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
79 | } |
80 | ~SpecConstantOpModeGuard() { |
81 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
82 | : builder_->setToNormalCodeGenMode(); |
83 | } |
84 | void turnOnSpecConstantOpMode() { |
85 | builder_->setToSpecConstCodeGenMode(); |
86 | } |
87 | |
88 | private: |
89 | spv::Builder* builder_; |
90 | bool previous_flag_; |
91 | }; |
92 | |
93 | struct OpDecorations { |
94 | public: |
95 | OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
96 | precision(precision) |
97 | #ifndef GLSLANG_WEB |
98 | , |
99 | noContraction(noContraction), |
100 | nonUniform(nonUniform) |
101 | #endif |
102 | { } |
103 | |
104 | spv::Decoration precision; |
105 | |
106 | #ifdef GLSLANG_WEB |
107 | void addNoContraction(spv::Builder&, spv::Id) const { } |
108 | void addNonUniform(spv::Builder&, spv::Id) const { } |
109 | #else |
110 | void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); } |
111 | void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); } |
112 | protected: |
113 | spv::Decoration noContraction; |
114 | spv::Decoration nonUniform; |
115 | #endif |
116 | |
117 | }; |
118 | |
119 | } // namespace |
120 | |
121 | using namespace QtShaderTools; |
122 | // |
123 | // The main holder of information for translating glslang to SPIR-V. |
124 | // |
125 | // Derives from the AST walking base class. |
126 | // |
127 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
128 | public: |
129 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
130 | glslang::SpvOptions& options); |
131 | virtual ~TGlslangToSpvTraverser() { } |
132 | |
133 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
134 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
135 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
136 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
137 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
138 | void visitSymbol(glslang::TIntermSymbol* symbol); |
139 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
140 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
141 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
142 | |
143 | void finishSpv(); |
144 | void dumpSpv(std::vector<unsigned int>& out); |
145 | |
146 | protected: |
147 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
148 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
149 | |
150 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
151 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
152 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
153 | spv::Decoration TranslateNonUniformDecoration(const spv::Builder::AccessChain::CoherentFlags& coherentFlags); |
154 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
155 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
156 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
157 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
158 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
159 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
160 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
161 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
162 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
163 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
164 | void TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>&, std::vector<unsigned>&) const; |
165 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
166 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
167 | spv::Id getSampledType(const glslang::TSampler&); |
168 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
169 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
170 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
171 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
172 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
173 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
174 | bool filterMember(const glslang::TType& member); |
175 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
176 | glslang::TLayoutPacking, const glslang::TQualifier&); |
177 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
178 | const glslang::TQualifier&, spv::Id); |
179 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
180 | spv::Id accessChainLoad(const glslang::TType& type); |
181 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
182 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
183 | spv::Id convertLoadedBoolInUniformToUint(const glslang::TType& type, spv::Id nominalTypeId, spv::Id loadedId); |
184 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
185 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
186 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
187 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
188 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
189 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
190 | |
191 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
192 | bool writableParam(glslang::TStorageQualifier) const; |
193 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
194 | void makeFunctions(const glslang::TIntermSequence&); |
195 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
196 | void collectRayTracingLinkerObjects(); |
197 | void visitFunctions(const glslang::TIntermSequence&); |
198 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
199 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
200 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
201 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
202 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
203 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
204 | |
205 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
206 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
207 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
208 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
209 | glslang::TBasicType typeProxy, |
210 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
211 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
212 | glslang::TBasicType typeProxy); |
213 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
214 | glslang::TBasicType typeProxy); |
215 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
216 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
217 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
218 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
219 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
220 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
221 | glslang::TBasicType typeProxy); |
222 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
223 | spv::Id typeId, std::vector<spv::Id>& operands); |
224 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
225 | glslang::TBasicType typeProxy); |
226 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
227 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
228 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
229 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
230 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
231 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
232 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
233 | int& nextConst, bool specConstant); |
234 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
235 | bool isTrivial(const glslang::TIntermTyped* node); |
236 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
237 | spv::Id getExtBuiltins(const char* name); |
238 | std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&); |
239 | spv::Id translateForcedType(spv::Id object); |
240 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
241 | |
242 | glslang::SpvOptions& options; |
243 | spv::Function* shaderEntry; |
244 | spv::Function* currentFunction; |
245 | spv::Instruction* entryPoint; |
246 | int sequenceDepth; |
247 | |
248 | spv::SpvBuildLogger* logger; |
249 | |
250 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
251 | spv::Builder builder; |
252 | bool inEntryPoint; |
253 | bool entryPointTerminated; |
254 | bool linkageOnly; // true when visiting the set of objects in the AST present only for |
255 | // establishing interface, whether or not they were statically used |
256 | std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
257 | const glslang::TIntermediate* glslangIntermediate; |
258 | bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
259 | spv::Id stdBuiltins; |
260 | spv::Id nonSemanticDebugPrintf; |
261 | std::unordered_map<std::string, spv::Id> extBuiltinMap; |
262 | |
263 | std::unordered_map<long long, spv::Id> symbolValues; |
264 | std::unordered_map<uint32_t, spv::Id> builtInVariableIds; |
265 | std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues, |
266 | // rather than a pointer |
267 | std::unordered_map<std::string, spv::Function*> functionMap; |
268 | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
269 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
270 | std::unordered_map<long long, std::vector<int>> memberRemapper; |
271 | // for mapping glslang symbol struct to symbol Id |
272 | std::unordered_map<const glslang::TTypeList*, long long> glslangTypeToIdMap; |
273 | std::stack<bool> breakForLoop; // false means break for switch |
274 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
275 | // Map pointee types for EbtReference to their forward pointers |
276 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
277 | // Type forcing, for when SPIR-V wants a different type than the AST, |
278 | // requiring local translation to and from SPIR-V type on every access. |
279 | // Maps <builtin-variable-id -> AST-required-type-id> |
280 | std::unordered_map<spv::Id, spv::Id> forceType; |
281 | |
282 | // Used later for generating OpTraceKHR/OpExecuteCallableKHR |
283 | std::unordered_map<unsigned int, glslang::TIntermSymbol *> locationToSymbol[2]; |
284 | }; |
285 | |
286 | // |
287 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
288 | // |
289 | |
290 | // Translate glslang profile to SPIR-V source language. |
291 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
292 | { |
293 | #ifdef GLSLANG_WEB |
294 | return spv::SourceLanguageESSL; |
295 | #elif defined(GLSLANG_ANGLE) |
296 | return spv::SourceLanguageGLSL; |
297 | #endif |
298 | |
299 | switch (source) { |
300 | case glslang::EShSourceGlsl: |
301 | switch (profile) { |
302 | case ENoProfile: |
303 | case ECoreProfile: |
304 | case ECompatibilityProfile: |
305 | return spv::SourceLanguageGLSL; |
306 | case EEsProfile: |
307 | return spv::SourceLanguageESSL; |
308 | default: |
309 | return spv::SourceLanguageUnknown; |
310 | } |
311 | case glslang::EShSourceHlsl: |
312 | return spv::SourceLanguageHLSL; |
313 | default: |
314 | return spv::SourceLanguageUnknown; |
315 | } |
316 | } |
317 | |
318 | // Translate glslang language (stage) to SPIR-V execution model. |
319 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
320 | { |
321 | switch (stage) { |
322 | case EShLangVertex: return spv::ExecutionModelVertex; |
323 | case EShLangFragment: return spv::ExecutionModelFragment; |
324 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
325 | #ifndef GLSLANG_WEB |
326 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
327 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
328 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
329 | case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR; |
330 | case EShLangIntersect: return spv::ExecutionModelIntersectionKHR; |
331 | case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR; |
332 | case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR; |
333 | case EShLangMiss: return spv::ExecutionModelMissKHR; |
334 | case EShLangCallable: return spv::ExecutionModelCallableKHR; |
335 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
336 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
337 | #endif |
338 | default: |
339 | assert(0); |
340 | return spv::ExecutionModelFragment; |
341 | } |
342 | } |
343 | |
344 | // Translate glslang sampler type to SPIR-V dimensionality. |
345 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
346 | { |
347 | switch (sampler.dim) { |
348 | case glslang::Esd1D: return spv::Dim1D; |
349 | case glslang::Esd2D: return spv::Dim2D; |
350 | case glslang::Esd3D: return spv::Dim3D; |
351 | case glslang::EsdCube: return spv::DimCube; |
352 | case glslang::EsdRect: return spv::DimRect; |
353 | case glslang::EsdBuffer: return spv::DimBuffer; |
354 | case glslang::EsdSubpass: return spv::DimSubpassData; |
355 | default: |
356 | assert(0); |
357 | return spv::Dim2D; |
358 | } |
359 | } |
360 | |
361 | // Translate glslang precision to SPIR-V precision decorations. |
362 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
363 | { |
364 | switch (glslangPrecision) { |
365 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
366 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
367 | default: |
368 | return spv::NoPrecision; |
369 | } |
370 | } |
371 | |
372 | // Translate glslang type to SPIR-V precision decorations. |
373 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
374 | { |
375 | return TranslatePrecisionDecoration(glslangPrecision: type.getQualifier().precision); |
376 | } |
377 | |
378 | // Translate glslang type to SPIR-V block decorations. |
379 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
380 | { |
381 | if (type.getBasicType() == glslang::EbtBlock) { |
382 | switch (type.getQualifier().storage) { |
383 | case glslang::EvqUniform: return spv::DecorationBlock; |
384 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
385 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
386 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
387 | case glslang::EvqShared: return spv::DecorationBlock; |
388 | #ifndef GLSLANG_WEB |
389 | case glslang::EvqPayload: return spv::DecorationBlock; |
390 | case glslang::EvqPayloadIn: return spv::DecorationBlock; |
391 | case glslang::EvqHitAttr: return spv::DecorationBlock; |
392 | case glslang::EvqCallableData: return spv::DecorationBlock; |
393 | case glslang::EvqCallableDataIn: return spv::DecorationBlock; |
394 | #endif |
395 | default: |
396 | assert(0); |
397 | break; |
398 | } |
399 | } |
400 | |
401 | return spv::DecorationMax; |
402 | } |
403 | |
404 | // Translate glslang type to SPIR-V memory decorations. |
405 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
406 | bool useVulkanMemoryModel) |
407 | { |
408 | if (!useVulkanMemoryModel) { |
409 | if (qualifier.isCoherent()) |
410 | memory.push_back(x: spv::DecorationCoherent); |
411 | if (qualifier.isVolatile()) { |
412 | memory.push_back(x: spv::DecorationVolatile); |
413 | memory.push_back(x: spv::DecorationCoherent); |
414 | } |
415 | } |
416 | if (qualifier.isRestrict()) |
417 | memory.push_back(x: spv::DecorationRestrict); |
418 | if (qualifier.isReadOnly()) |
419 | memory.push_back(x: spv::DecorationNonWritable); |
420 | if (qualifier.isWriteOnly()) |
421 | memory.push_back(x: spv::DecorationNonReadable); |
422 | } |
423 | |
424 | // Translate glslang type to SPIR-V layout decorations. |
425 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
426 | { |
427 | if (type.isMatrix()) { |
428 | switch (matrixLayout) { |
429 | case glslang::ElmRowMajor: |
430 | return spv::DecorationRowMajor; |
431 | case glslang::ElmColumnMajor: |
432 | return spv::DecorationColMajor; |
433 | default: |
434 | // opaque layouts don't need a majorness |
435 | return spv::DecorationMax; |
436 | } |
437 | } else { |
438 | switch (type.getBasicType()) { |
439 | default: |
440 | return spv::DecorationMax; |
441 | break; |
442 | case glslang::EbtBlock: |
443 | switch (type.getQualifier().storage) { |
444 | case glslang::EvqShared: |
445 | case glslang::EvqUniform: |
446 | case glslang::EvqBuffer: |
447 | switch (type.getQualifier().layoutPacking) { |
448 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
449 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
450 | default: |
451 | return spv::DecorationMax; |
452 | } |
453 | case glslang::EvqVaryingIn: |
454 | case glslang::EvqVaryingOut: |
455 | if (type.getQualifier().isTaskMemory()) { |
456 | switch (type.getQualifier().layoutPacking) { |
457 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
458 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
459 | default: break; |
460 | } |
461 | } else { |
462 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
463 | } |
464 | return spv::DecorationMax; |
465 | #ifndef GLSLANG_WEB |
466 | case glslang::EvqPayload: |
467 | case glslang::EvqPayloadIn: |
468 | case glslang::EvqHitAttr: |
469 | case glslang::EvqCallableData: |
470 | case glslang::EvqCallableDataIn: |
471 | return spv::DecorationMax; |
472 | #endif |
473 | default: |
474 | assert(0); |
475 | return spv::DecorationMax; |
476 | } |
477 | } |
478 | } |
479 | } |
480 | |
481 | // Translate glslang type to SPIR-V interpolation decorations. |
482 | // Returns spv::DecorationMax when no decoration |
483 | // should be applied. |
484 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
485 | { |
486 | if (qualifier.smooth) |
487 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
488 | return spv::DecorationMax; |
489 | else if (qualifier.isNonPerspective()) |
490 | return spv::DecorationNoPerspective; |
491 | else if (qualifier.flat) |
492 | return spv::DecorationFlat; |
493 | else if (qualifier.isExplicitInterpolation()) { |
494 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
495 | return spv::DecorationExplicitInterpAMD; |
496 | } |
497 | else |
498 | return spv::DecorationMax; |
499 | } |
500 | |
501 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
502 | // Returns spv::DecorationMax when no decoration |
503 | // should be applied. |
504 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
505 | { |
506 | if (qualifier.centroid) |
507 | return spv::DecorationCentroid; |
508 | #ifndef GLSLANG_WEB |
509 | else if (qualifier.patch) |
510 | return spv::DecorationPatch; |
511 | else if (qualifier.sample) { |
512 | builder.addCapability(cap: spv::CapabilitySampleRateShading); |
513 | return spv::DecorationSample; |
514 | } |
515 | #endif |
516 | |
517 | return spv::DecorationMax; |
518 | } |
519 | |
520 | // If glslang type is invariant, return SPIR-V invariant decoration. |
521 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
522 | { |
523 | if (qualifier.invariant) |
524 | return spv::DecorationInvariant; |
525 | else |
526 | return spv::DecorationMax; |
527 | } |
528 | |
529 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
530 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
531 | { |
532 | #ifndef GLSLANG_WEB |
533 | if (qualifier.isNoContraction()) |
534 | return spv::DecorationNoContraction; |
535 | else |
536 | #endif |
537 | return spv::DecorationMax; |
538 | } |
539 | |
540 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
541 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
542 | { |
543 | #ifndef GLSLANG_WEB |
544 | if (qualifier.isNonUniform()) { |
545 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
546 | builder.addCapability(cap: spv::CapabilityShaderNonUniformEXT); |
547 | return spv::DecorationNonUniformEXT; |
548 | } else |
549 | #endif |
550 | return spv::DecorationMax; |
551 | } |
552 | |
553 | // If lvalue flags contains nonUniform, return SPIR-V NonUniform decoration. |
554 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration( |
555 | const spv::Builder::AccessChain::CoherentFlags& coherentFlags) |
556 | { |
557 | #ifndef GLSLANG_WEB |
558 | if (coherentFlags.isNonUniform()) { |
559 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
560 | builder.addCapability(cap: spv::CapabilityShaderNonUniformEXT); |
561 | return spv::DecorationNonUniformEXT; |
562 | } else |
563 | #endif |
564 | return spv::DecorationMax; |
565 | } |
566 | |
567 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
568 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
569 | { |
570 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
571 | |
572 | #ifndef GLSLANG_WEB |
573 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
574 | return mask; |
575 | |
576 | if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
577 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
578 | spv::MemoryAccessMakePointerVisibleKHRMask; |
579 | } |
580 | |
581 | if (coherentFlags.nonprivate) { |
582 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
583 | } |
584 | if (coherentFlags.volatil) { |
585 | mask = mask | spv::MemoryAccessVolatileMask; |
586 | } |
587 | if (mask != spv::MemoryAccessMaskNone) { |
588 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
589 | } |
590 | #endif |
591 | |
592 | return mask; |
593 | } |
594 | |
595 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
596 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
597 | { |
598 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
599 | |
600 | #ifndef GLSLANG_WEB |
601 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
602 | return mask; |
603 | |
604 | if (coherentFlags.volatil || |
605 | coherentFlags.anyCoherent()) { |
606 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
607 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
608 | } |
609 | if (coherentFlags.nonprivate) { |
610 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
611 | } |
612 | if (coherentFlags.volatil) { |
613 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
614 | } |
615 | if (mask != spv::ImageOperandsMaskNone) { |
616 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
617 | } |
618 | #endif |
619 | |
620 | return mask; |
621 | } |
622 | |
623 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
624 | { |
625 | spv::Builder::AccessChain::CoherentFlags flags = {}; |
626 | #ifndef GLSLANG_WEB |
627 | flags.coherent = type.getQualifier().coherent; |
628 | flags.devicecoherent = type.getQualifier().devicecoherent; |
629 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
630 | // shared variables are implicitly workgroupcoherent in GLSL. |
631 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
632 | type.getQualifier().storage == glslang::EvqShared; |
633 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
634 | flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
635 | flags.volatil = type.getQualifier().volatil; |
636 | // *coherent variables are implicitly nonprivate in GLSL |
637 | flags.nonprivate = type.getQualifier().nonprivate || |
638 | flags.anyCoherent() || |
639 | flags.volatil; |
640 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
641 | #endif |
642 | flags.nonUniform = type.getQualifier().nonUniform; |
643 | return flags; |
644 | } |
645 | |
646 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
647 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
648 | { |
649 | spv::Scope scope = spv::ScopeMax; |
650 | |
651 | #ifndef GLSLANG_WEB |
652 | if (coherentFlags.volatil || coherentFlags.coherent) { |
653 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
654 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
655 | } else if (coherentFlags.devicecoherent) { |
656 | scope = spv::ScopeDevice; |
657 | } else if (coherentFlags.queuefamilycoherent) { |
658 | scope = spv::ScopeQueueFamilyKHR; |
659 | } else if (coherentFlags.workgroupcoherent) { |
660 | scope = spv::ScopeWorkgroup; |
661 | } else if (coherentFlags.subgroupcoherent) { |
662 | scope = spv::ScopeSubgroup; |
663 | } else if (coherentFlags.shadercallcoherent) { |
664 | scope = spv::ScopeShaderCallKHR; |
665 | } |
666 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
667 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
668 | } |
669 | #endif |
670 | |
671 | return scope; |
672 | } |
673 | |
674 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
675 | // associated capabilities when required. For some built-in variables, a capability |
676 | // is generated only when using the variable in an executable instruction, but not when |
677 | // just declaring a struct member variable with it. This is true for PointSize, |
678 | // ClipDistance, and CullDistance. |
679 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
680 | bool memberDeclaration) |
681 | { |
682 | switch (builtIn) { |
683 | case glslang::EbvPointSize: |
684 | #ifndef GLSLANG_WEB |
685 | // Defer adding the capability until the built-in is actually used. |
686 | if (! memberDeclaration) { |
687 | switch (glslangIntermediate->getStage()) { |
688 | case EShLangGeometry: |
689 | builder.addCapability(cap: spv::CapabilityGeometryPointSize); |
690 | break; |
691 | case EShLangTessControl: |
692 | case EShLangTessEvaluation: |
693 | builder.addCapability(cap: spv::CapabilityTessellationPointSize); |
694 | break; |
695 | default: |
696 | break; |
697 | } |
698 | } |
699 | #endif |
700 | return spv::BuiltInPointSize; |
701 | |
702 | case glslang::EbvPosition: return spv::BuiltInPosition; |
703 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
704 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
705 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
706 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
707 | |
708 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
709 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
710 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
711 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
712 | |
713 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
714 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
715 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
716 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
717 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
718 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
719 | |
720 | #ifndef GLSLANG_WEB |
721 | // These *Distance capabilities logically belong here, but if the member is declared and |
722 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
723 | // They are now generated when used, rather than here when declared. |
724 | // Potentially, the specification should be more clear what the minimum |
725 | // use needed is to trigger the capability. |
726 | // |
727 | case glslang::EbvClipDistance: |
728 | if (!memberDeclaration) |
729 | builder.addCapability(cap: spv::CapabilityClipDistance); |
730 | return spv::BuiltInClipDistance; |
731 | |
732 | case glslang::EbvCullDistance: |
733 | if (!memberDeclaration) |
734 | builder.addCapability(cap: spv::CapabilityCullDistance); |
735 | return spv::BuiltInCullDistance; |
736 | |
737 | case glslang::EbvViewportIndex: |
738 | if (glslangIntermediate->getStage() == EShLangGeometry || |
739 | glslangIntermediate->getStage() == EShLangFragment) { |
740 | builder.addCapability(cap: spv::CapabilityMultiViewport); |
741 | } |
742 | if (glslangIntermediate->getStage() == EShLangVertex || |
743 | glslangIntermediate->getStage() == EShLangTessControl || |
744 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
745 | |
746 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
747 | builder.addIncorporatedExtension(ext: spv::E_SPV_EXT_shader_viewport_index_layer, incorporatedVersion: spv::Spv_1_5); |
748 | builder.addCapability(cap: spv::CapabilityShaderViewportIndexLayerEXT); |
749 | } |
750 | else |
751 | builder.addCapability(cap: spv::CapabilityShaderViewportIndex); |
752 | } |
753 | return spv::BuiltInViewportIndex; |
754 | |
755 | case glslang::EbvSampleId: |
756 | builder.addCapability(cap: spv::CapabilitySampleRateShading); |
757 | return spv::BuiltInSampleId; |
758 | |
759 | case glslang::EbvSamplePosition: |
760 | builder.addCapability(cap: spv::CapabilitySampleRateShading); |
761 | return spv::BuiltInSamplePosition; |
762 | |
763 | case glslang::EbvSampleMask: |
764 | return spv::BuiltInSampleMask; |
765 | |
766 | case glslang::EbvLayer: |
767 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
768 | return spv::BuiltInLayer; |
769 | } |
770 | if (glslangIntermediate->getStage() == EShLangGeometry || |
771 | glslangIntermediate->getStage() == EShLangFragment) { |
772 | builder.addCapability(cap: spv::CapabilityGeometry); |
773 | } |
774 | if (glslangIntermediate->getStage() == EShLangVertex || |
775 | glslangIntermediate->getStage() == EShLangTessControl || |
776 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
777 | |
778 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
779 | builder.addIncorporatedExtension(ext: spv::E_SPV_EXT_shader_viewport_index_layer, incorporatedVersion: spv::Spv_1_5); |
780 | builder.addCapability(cap: spv::CapabilityShaderViewportIndexLayerEXT); |
781 | } else |
782 | builder.addCapability(cap: spv::CapabilityShaderLayer); |
783 | } |
784 | return spv::BuiltInLayer; |
785 | |
786 | case glslang::EbvBaseVertex: |
787 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_shader_draw_parameters, incorporatedVersion: spv::Spv_1_3); |
788 | builder.addCapability(cap: spv::CapabilityDrawParameters); |
789 | return spv::BuiltInBaseVertex; |
790 | |
791 | case glslang::EbvBaseInstance: |
792 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_shader_draw_parameters, incorporatedVersion: spv::Spv_1_3); |
793 | builder.addCapability(cap: spv::CapabilityDrawParameters); |
794 | return spv::BuiltInBaseInstance; |
795 | |
796 | case glslang::EbvDrawId: |
797 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_shader_draw_parameters, incorporatedVersion: spv::Spv_1_3); |
798 | builder.addCapability(cap: spv::CapabilityDrawParameters); |
799 | return spv::BuiltInDrawIndex; |
800 | |
801 | case glslang::EbvPrimitiveId: |
802 | if (glslangIntermediate->getStage() == EShLangFragment) |
803 | builder.addCapability(cap: spv::CapabilityGeometry); |
804 | return spv::BuiltInPrimitiveId; |
805 | |
806 | case glslang::EbvFragStencilRef: |
807 | builder.addExtension(ext: spv::E_SPV_EXT_shader_stencil_export); |
808 | builder.addCapability(cap: spv::CapabilityStencilExportEXT); |
809 | return spv::BuiltInFragStencilRefEXT; |
810 | |
811 | case glslang::EbvShadingRateKHR: |
812 | builder.addExtension(ext: spv::E_SPV_KHR_fragment_shading_rate); |
813 | builder.addCapability(cap: spv::CapabilityFragmentShadingRateKHR); |
814 | return spv::BuiltInShadingRateKHR; |
815 | |
816 | case glslang::EbvPrimitiveShadingRateKHR: |
817 | builder.addExtension(ext: spv::E_SPV_KHR_fragment_shading_rate); |
818 | builder.addCapability(cap: spv::CapabilityFragmentShadingRateKHR); |
819 | return spv::BuiltInPrimitiveShadingRateKHR; |
820 | |
821 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
822 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
823 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
824 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
825 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
826 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
827 | |
828 | case glslang::EbvSubGroupSize: |
829 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
830 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
831 | return spv::BuiltInSubgroupSize; |
832 | |
833 | case glslang::EbvSubGroupInvocation: |
834 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
835 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
836 | return spv::BuiltInSubgroupLocalInvocationId; |
837 | |
838 | case glslang::EbvSubGroupEqMask: |
839 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
840 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
841 | return spv::BuiltInSubgroupEqMask; |
842 | |
843 | case glslang::EbvSubGroupGeMask: |
844 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
845 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
846 | return spv::BuiltInSubgroupGeMask; |
847 | |
848 | case glslang::EbvSubGroupGtMask: |
849 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
850 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
851 | return spv::BuiltInSubgroupGtMask; |
852 | |
853 | case glslang::EbvSubGroupLeMask: |
854 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
855 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
856 | return spv::BuiltInSubgroupLeMask; |
857 | |
858 | case glslang::EbvSubGroupLtMask: |
859 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
860 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
861 | return spv::BuiltInSubgroupLtMask; |
862 | |
863 | case glslang::EbvNumSubgroups: |
864 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
865 | return spv::BuiltInNumSubgroups; |
866 | |
867 | case glslang::EbvSubgroupID: |
868 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
869 | return spv::BuiltInSubgroupId; |
870 | |
871 | case glslang::EbvSubgroupSize2: |
872 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
873 | return spv::BuiltInSubgroupSize; |
874 | |
875 | case glslang::EbvSubgroupInvocation2: |
876 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
877 | return spv::BuiltInSubgroupLocalInvocationId; |
878 | |
879 | case glslang::EbvSubgroupEqMask2: |
880 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
881 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
882 | return spv::BuiltInSubgroupEqMask; |
883 | |
884 | case glslang::EbvSubgroupGeMask2: |
885 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
886 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
887 | return spv::BuiltInSubgroupGeMask; |
888 | |
889 | case glslang::EbvSubgroupGtMask2: |
890 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
891 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
892 | return spv::BuiltInSubgroupGtMask; |
893 | |
894 | case glslang::EbvSubgroupLeMask2: |
895 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
896 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
897 | return spv::BuiltInSubgroupLeMask; |
898 | |
899 | case glslang::EbvSubgroupLtMask2: |
900 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
901 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
902 | return spv::BuiltInSubgroupLtMask; |
903 | |
904 | case glslang::EbvBaryCoordNoPersp: |
905 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
906 | return spv::BuiltInBaryCoordNoPerspAMD; |
907 | |
908 | case glslang::EbvBaryCoordNoPerspCentroid: |
909 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
910 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
911 | |
912 | case glslang::EbvBaryCoordNoPerspSample: |
913 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
914 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
915 | |
916 | case glslang::EbvBaryCoordSmooth: |
917 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
918 | return spv::BuiltInBaryCoordSmoothAMD; |
919 | |
920 | case glslang::EbvBaryCoordSmoothCentroid: |
921 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
922 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
923 | |
924 | case glslang::EbvBaryCoordSmoothSample: |
925 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
926 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
927 | |
928 | case glslang::EbvBaryCoordPullModel: |
929 | builder.addExtension(ext: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
930 | return spv::BuiltInBaryCoordPullModelAMD; |
931 | |
932 | case glslang::EbvDeviceIndex: |
933 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_device_group, incorporatedVersion: spv::Spv_1_3); |
934 | builder.addCapability(cap: spv::CapabilityDeviceGroup); |
935 | return spv::BuiltInDeviceIndex; |
936 | |
937 | case glslang::EbvViewIndex: |
938 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_multiview, incorporatedVersion: spv::Spv_1_3); |
939 | builder.addCapability(cap: spv::CapabilityMultiView); |
940 | return spv::BuiltInViewIndex; |
941 | |
942 | case glslang::EbvFragSizeEXT: |
943 | builder.addExtension(ext: spv::E_SPV_EXT_fragment_invocation_density); |
944 | builder.addCapability(cap: spv::CapabilityFragmentDensityEXT); |
945 | return spv::BuiltInFragSizeEXT; |
946 | |
947 | case glslang::EbvFragInvocationCountEXT: |
948 | builder.addExtension(ext: spv::E_SPV_EXT_fragment_invocation_density); |
949 | builder.addCapability(cap: spv::CapabilityFragmentDensityEXT); |
950 | return spv::BuiltInFragInvocationCountEXT; |
951 | |
952 | case glslang::EbvViewportMaskNV: |
953 | if (!memberDeclaration) { |
954 | builder.addExtension(ext: spv::E_SPV_NV_viewport_array2); |
955 | builder.addCapability(cap: spv::CapabilityShaderViewportMaskNV); |
956 | } |
957 | return spv::BuiltInViewportMaskNV; |
958 | case glslang::EbvSecondaryPositionNV: |
959 | if (!memberDeclaration) { |
960 | builder.addExtension(ext: spv::E_SPV_NV_stereo_view_rendering); |
961 | builder.addCapability(cap: spv::CapabilityShaderStereoViewNV); |
962 | } |
963 | return spv::BuiltInSecondaryPositionNV; |
964 | case glslang::EbvSecondaryViewportMaskNV: |
965 | if (!memberDeclaration) { |
966 | builder.addExtension(ext: spv::E_SPV_NV_stereo_view_rendering); |
967 | builder.addCapability(cap: spv::CapabilityShaderStereoViewNV); |
968 | } |
969 | return spv::BuiltInSecondaryViewportMaskNV; |
970 | case glslang::EbvPositionPerViewNV: |
971 | if (!memberDeclaration) { |
972 | builder.addExtension(ext: spv::E_SPV_NVX_multiview_per_view_attributes); |
973 | builder.addCapability(cap: spv::CapabilityPerViewAttributesNV); |
974 | } |
975 | return spv::BuiltInPositionPerViewNV; |
976 | case glslang::EbvViewportMaskPerViewNV: |
977 | if (!memberDeclaration) { |
978 | builder.addExtension(ext: spv::E_SPV_NVX_multiview_per_view_attributes); |
979 | builder.addCapability(cap: spv::CapabilityPerViewAttributesNV); |
980 | } |
981 | return spv::BuiltInViewportMaskPerViewNV; |
982 | case glslang::EbvFragFullyCoveredNV: |
983 | builder.addExtension(ext: spv::E_SPV_EXT_fragment_fully_covered); |
984 | builder.addCapability(cap: spv::CapabilityFragmentFullyCoveredEXT); |
985 | return spv::BuiltInFullyCoveredEXT; |
986 | case glslang::EbvFragmentSizeNV: |
987 | builder.addExtension(ext: spv::E_SPV_NV_shading_rate); |
988 | builder.addCapability(cap: spv::CapabilityShadingRateNV); |
989 | return spv::BuiltInFragmentSizeNV; |
990 | case glslang::EbvInvocationsPerPixelNV: |
991 | builder.addExtension(ext: spv::E_SPV_NV_shading_rate); |
992 | builder.addCapability(cap: spv::CapabilityShadingRateNV); |
993 | return spv::BuiltInInvocationsPerPixelNV; |
994 | |
995 | // ray tracing |
996 | case glslang::EbvLaunchId: |
997 | return spv::BuiltInLaunchIdKHR; |
998 | case glslang::EbvLaunchSize: |
999 | return spv::BuiltInLaunchSizeKHR; |
1000 | case glslang::EbvWorldRayOrigin: |
1001 | return spv::BuiltInWorldRayOriginKHR; |
1002 | case glslang::EbvWorldRayDirection: |
1003 | return spv::BuiltInWorldRayDirectionKHR; |
1004 | case glslang::EbvObjectRayOrigin: |
1005 | return spv::BuiltInObjectRayOriginKHR; |
1006 | case glslang::EbvObjectRayDirection: |
1007 | return spv::BuiltInObjectRayDirectionKHR; |
1008 | case glslang::EbvRayTmin: |
1009 | return spv::BuiltInRayTminKHR; |
1010 | case glslang::EbvRayTmax: |
1011 | return spv::BuiltInRayTmaxKHR; |
1012 | case glslang::EbvCullMask: |
1013 | return spv::BuiltInCullMaskKHR; |
1014 | case glslang::EbvInstanceCustomIndex: |
1015 | return spv::BuiltInInstanceCustomIndexKHR; |
1016 | case glslang::EbvHitT: |
1017 | { |
1018 | // this is a GLSL alias of RayTmax |
1019 | // in SPV_NV_ray_tracing it has a dedicated builtin |
1020 | // but in SPV_KHR_ray_tracing it gets mapped to RayTmax |
1021 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
1022 | if (extensions.find(x: "GL_NV_ray_tracing" ) != extensions.end()) { |
1023 | return spv::BuiltInHitTNV; |
1024 | } else { |
1025 | return spv::BuiltInRayTmaxKHR; |
1026 | } |
1027 | } |
1028 | case glslang::EbvHitKind: |
1029 | return spv::BuiltInHitKindKHR; |
1030 | case glslang::EbvObjectToWorld: |
1031 | case glslang::EbvObjectToWorld3x4: |
1032 | return spv::BuiltInObjectToWorldKHR; |
1033 | case glslang::EbvWorldToObject: |
1034 | case glslang::EbvWorldToObject3x4: |
1035 | return spv::BuiltInWorldToObjectKHR; |
1036 | case glslang::EbvIncomingRayFlags: |
1037 | return spv::BuiltInIncomingRayFlagsKHR; |
1038 | case glslang::EbvGeometryIndex: |
1039 | return spv::BuiltInRayGeometryIndexKHR; |
1040 | case glslang::EbvCurrentRayTimeNV: |
1041 | builder.addExtension(ext: spv::E_SPV_NV_ray_tracing_motion_blur); |
1042 | builder.addCapability(cap: spv::CapabilityRayTracingMotionBlurNV); |
1043 | return spv::BuiltInCurrentRayTimeNV; |
1044 | |
1045 | // barycentrics |
1046 | case glslang::EbvBaryCoordNV: |
1047 | builder.addExtension(ext: spv::E_SPV_NV_fragment_shader_barycentric); |
1048 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricNV); |
1049 | return spv::BuiltInBaryCoordNV; |
1050 | case glslang::EbvBaryCoordNoPerspNV: |
1051 | builder.addExtension(ext: spv::E_SPV_NV_fragment_shader_barycentric); |
1052 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricNV); |
1053 | return spv::BuiltInBaryCoordNoPerspNV; |
1054 | |
1055 | case glslang::EbvBaryCoordEXT: |
1056 | builder.addExtension(ext: spv::E_SPV_KHR_fragment_shader_barycentric); |
1057 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricKHR); |
1058 | return spv::BuiltInBaryCoordKHR; |
1059 | case glslang::EbvBaryCoordNoPerspEXT: |
1060 | builder.addExtension(ext: spv::E_SPV_KHR_fragment_shader_barycentric); |
1061 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricKHR); |
1062 | return spv::BuiltInBaryCoordNoPerspKHR; |
1063 | |
1064 | // mesh shaders |
1065 | case glslang::EbvTaskCountNV: |
1066 | return spv::BuiltInTaskCountNV; |
1067 | case glslang::EbvPrimitiveCountNV: |
1068 | return spv::BuiltInPrimitiveCountNV; |
1069 | case glslang::EbvPrimitiveIndicesNV: |
1070 | return spv::BuiltInPrimitiveIndicesNV; |
1071 | case glslang::EbvClipDistancePerViewNV: |
1072 | return spv::BuiltInClipDistancePerViewNV; |
1073 | case glslang::EbvCullDistancePerViewNV: |
1074 | return spv::BuiltInCullDistancePerViewNV; |
1075 | case glslang::EbvLayerPerViewNV: |
1076 | return spv::BuiltInLayerPerViewNV; |
1077 | case glslang::EbvMeshViewCountNV: |
1078 | return spv::BuiltInMeshViewCountNV; |
1079 | case glslang::EbvMeshViewIndicesNV: |
1080 | return spv::BuiltInMeshViewIndicesNV; |
1081 | |
1082 | // sm builtins |
1083 | case glslang::EbvWarpsPerSM: |
1084 | builder.addExtension(ext: spv::E_SPV_NV_shader_sm_builtins); |
1085 | builder.addCapability(cap: spv::CapabilityShaderSMBuiltinsNV); |
1086 | return spv::BuiltInWarpsPerSMNV; |
1087 | case glslang::EbvSMCount: |
1088 | builder.addExtension(ext: spv::E_SPV_NV_shader_sm_builtins); |
1089 | builder.addCapability(cap: spv::CapabilityShaderSMBuiltinsNV); |
1090 | return spv::BuiltInSMCountNV; |
1091 | case glslang::EbvWarpID: |
1092 | builder.addExtension(ext: spv::E_SPV_NV_shader_sm_builtins); |
1093 | builder.addCapability(cap: spv::CapabilityShaderSMBuiltinsNV); |
1094 | return spv::BuiltInWarpIDNV; |
1095 | case glslang::EbvSMID: |
1096 | builder.addExtension(ext: spv::E_SPV_NV_shader_sm_builtins); |
1097 | builder.addCapability(cap: spv::CapabilityShaderSMBuiltinsNV); |
1098 | return spv::BuiltInSMIDNV; |
1099 | #endif |
1100 | |
1101 | default: |
1102 | return spv::BuiltInMax; |
1103 | } |
1104 | } |
1105 | |
1106 | // Translate glslang image layout format to SPIR-V image format. |
1107 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
1108 | { |
1109 | assert(type.getBasicType() == glslang::EbtSampler); |
1110 | |
1111 | #ifdef GLSLANG_WEB |
1112 | return spv::ImageFormatUnknown; |
1113 | #endif |
1114 | |
1115 | // Check for capabilities |
1116 | switch (type.getQualifier().getFormat()) { |
1117 | case glslang::ElfRg32f: |
1118 | case glslang::ElfRg16f: |
1119 | case glslang::ElfR11fG11fB10f: |
1120 | case glslang::ElfR16f: |
1121 | case glslang::ElfRgba16: |
1122 | case glslang::ElfRgb10A2: |
1123 | case glslang::ElfRg16: |
1124 | case glslang::ElfRg8: |
1125 | case glslang::ElfR16: |
1126 | case glslang::ElfR8: |
1127 | case glslang::ElfRgba16Snorm: |
1128 | case glslang::ElfRg16Snorm: |
1129 | case glslang::ElfRg8Snorm: |
1130 | case glslang::ElfR16Snorm: |
1131 | case glslang::ElfR8Snorm: |
1132 | |
1133 | case glslang::ElfRg32i: |
1134 | case glslang::ElfRg16i: |
1135 | case glslang::ElfRg8i: |
1136 | case glslang::ElfR16i: |
1137 | case glslang::ElfR8i: |
1138 | |
1139 | case glslang::ElfRgb10a2ui: |
1140 | case glslang::ElfRg32ui: |
1141 | case glslang::ElfRg16ui: |
1142 | case glslang::ElfRg8ui: |
1143 | case glslang::ElfR16ui: |
1144 | case glslang::ElfR8ui: |
1145 | builder.addCapability(cap: spv::CapabilityStorageImageExtendedFormats); |
1146 | break; |
1147 | |
1148 | case glslang::ElfR64ui: |
1149 | case glslang::ElfR64i: |
1150 | builder.addExtension(ext: spv::E_SPV_EXT_shader_image_int64); |
1151 | builder.addCapability(cap: spv::CapabilityInt64ImageEXT); |
1152 | default: |
1153 | break; |
1154 | } |
1155 | |
1156 | // do the translation |
1157 | switch (type.getQualifier().getFormat()) { |
1158 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
1159 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
1160 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
1161 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
1162 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
1163 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
1164 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
1165 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
1166 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
1167 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
1168 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
1169 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
1170 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
1171 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
1172 | case glslang::ElfR16: return spv::ImageFormatR16; |
1173 | case glslang::ElfR8: return spv::ImageFormatR8; |
1174 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
1175 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
1176 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
1177 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
1178 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
1179 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
1180 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
1181 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
1182 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
1183 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
1184 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
1185 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
1186 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
1187 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
1188 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
1189 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
1190 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
1191 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
1192 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
1193 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
1194 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
1195 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
1196 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
1197 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
1198 | case glslang::ElfR64ui: return spv::ImageFormatR64ui; |
1199 | case glslang::ElfR64i: return spv::ImageFormatR64i; |
1200 | default: return spv::ImageFormatMax; |
1201 | } |
1202 | } |
1203 | |
1204 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
1205 | const glslang::TIntermSelection& selectionNode) const |
1206 | { |
1207 | if (selectionNode.getFlatten()) |
1208 | return spv::SelectionControlFlattenMask; |
1209 | if (selectionNode.getDontFlatten()) |
1210 | return spv::SelectionControlDontFlattenMask; |
1211 | return spv::SelectionControlMaskNone; |
1212 | } |
1213 | |
1214 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
1215 | const |
1216 | { |
1217 | if (switchNode.getFlatten()) |
1218 | return spv::SelectionControlFlattenMask; |
1219 | if (switchNode.getDontFlatten()) |
1220 | return spv::SelectionControlDontFlattenMask; |
1221 | return spv::SelectionControlMaskNone; |
1222 | } |
1223 | |
1224 | // return a non-0 dependency if the dependency argument must be set |
1225 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
1226 | std::vector<unsigned int>& operands) const |
1227 | { |
1228 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
1229 | |
1230 | if (loopNode.getDontUnroll()) |
1231 | control = control | spv::LoopControlDontUnrollMask; |
1232 | if (loopNode.getUnroll()) |
1233 | control = control | spv::LoopControlUnrollMask; |
1234 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
1235 | control = control | spv::LoopControlDependencyInfiniteMask; |
1236 | else if (loopNode.getLoopDependency() > 0) { |
1237 | control = control | spv::LoopControlDependencyLengthMask; |
1238 | operands.push_back(x: (unsigned int)loopNode.getLoopDependency()); |
1239 | } |
1240 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
1241 | if (loopNode.getMinIterations() > 0) { |
1242 | control = control | spv::LoopControlMinIterationsMask; |
1243 | operands.push_back(x: loopNode.getMinIterations()); |
1244 | } |
1245 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
1246 | control = control | spv::LoopControlMaxIterationsMask; |
1247 | operands.push_back(x: loopNode.getMaxIterations()); |
1248 | } |
1249 | if (loopNode.getIterationMultiple() > 1) { |
1250 | control = control | spv::LoopControlIterationMultipleMask; |
1251 | operands.push_back(x: loopNode.getIterationMultiple()); |
1252 | } |
1253 | if (loopNode.getPeelCount() > 0) { |
1254 | control = control | spv::LoopControlPeelCountMask; |
1255 | operands.push_back(x: loopNode.getPeelCount()); |
1256 | } |
1257 | if (loopNode.getPartialCount() > 0) { |
1258 | control = control | spv::LoopControlPartialCountMask; |
1259 | operands.push_back(x: loopNode.getPartialCount()); |
1260 | } |
1261 | } |
1262 | |
1263 | return control; |
1264 | } |
1265 | |
1266 | // Translate glslang type to SPIR-V storage class. |
1267 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
1268 | { |
1269 | if (type.getBasicType() == glslang::EbtRayQuery) |
1270 | return spv::StorageClassPrivate; |
1271 | #ifndef GLSLANG_WEB |
1272 | if (type.getQualifier().isSpirvByReference()) { |
1273 | if (type.getQualifier().isParamInput() || type.getQualifier().isParamOutput()) |
1274 | return spv::StorageClassFunction; |
1275 | } |
1276 | #endif |
1277 | if (type.getQualifier().isPipeInput()) |
1278 | return spv::StorageClassInput; |
1279 | if (type.getQualifier().isPipeOutput()) |
1280 | return spv::StorageClassOutput; |
1281 | |
1282 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
1283 | type.getQualifier().storage == glslang::EvqUniform) { |
1284 | if (type.isAtomic()) |
1285 | return spv::StorageClassAtomicCounter; |
1286 | if (type.containsOpaque()) |
1287 | return spv::StorageClassUniformConstant; |
1288 | } |
1289 | |
1290 | if (type.getQualifier().isUniformOrBuffer() && |
1291 | type.getQualifier().isShaderRecord()) { |
1292 | return spv::StorageClassShaderRecordBufferKHR; |
1293 | } |
1294 | |
1295 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
1296 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_storage_buffer_storage_class, incorporatedVersion: spv::Spv_1_3); |
1297 | return spv::StorageClassStorageBuffer; |
1298 | } |
1299 | |
1300 | if (type.getQualifier().isUniformOrBuffer()) { |
1301 | if (type.getQualifier().isPushConstant()) |
1302 | return spv::StorageClassPushConstant; |
1303 | if (type.getBasicType() == glslang::EbtBlock) |
1304 | return spv::StorageClassUniform; |
1305 | return spv::StorageClassUniformConstant; |
1306 | } |
1307 | |
1308 | if (type.getQualifier().storage == glslang::EvqShared && type.getBasicType() == glslang::EbtBlock) { |
1309 | builder.addExtension(ext: spv::E_SPV_KHR_workgroup_memory_explicit_layout); |
1310 | builder.addCapability(cap: spv::CapabilityWorkgroupMemoryExplicitLayoutKHR); |
1311 | return spv::StorageClassWorkgroup; |
1312 | } |
1313 | |
1314 | switch (type.getQualifier().storage) { |
1315 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
1316 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
1317 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
1318 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
1319 | #ifndef GLSLANG_WEB |
1320 | case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
1321 | case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
1322 | case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
1323 | case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
1324 | case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
1325 | case glslang::EvqSpirvStorageClass: return static_cast<spv::StorageClass>(type.getQualifier().spirvStorageClass); |
1326 | #endif |
1327 | default: |
1328 | assert(0); |
1329 | break; |
1330 | } |
1331 | |
1332 | return spv::StorageClassFunction; |
1333 | } |
1334 | |
1335 | // Translate glslang constants to SPIR-V literals |
1336 | void TGlslangToSpvTraverser::TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>& constants, |
1337 | std::vector<unsigned>& literals) const |
1338 | { |
1339 | for (auto constant : constants) { |
1340 | if (constant->getBasicType() == glslang::EbtFloat) { |
1341 | float floatValue = static_cast<float>(constant->getConstArray()[0].getDConst()); |
1342 | unsigned literal = *reinterpret_cast<unsigned*>(&floatValue); |
1343 | literals.push_back(x: literal); |
1344 | } else if (constant->getBasicType() == glslang::EbtInt) { |
1345 | unsigned literal = constant->getConstArray()[0].getIConst(); |
1346 | literals.push_back(x: literal); |
1347 | } else if (constant->getBasicType() == glslang::EbtUint) { |
1348 | unsigned literal = constant->getConstArray()[0].getUConst(); |
1349 | literals.push_back(x: literal); |
1350 | } else if (constant->getBasicType() == glslang::EbtBool) { |
1351 | unsigned literal = constant->getConstArray()[0].getBConst(); |
1352 | literals.push_back(x: literal); |
1353 | } else if (constant->getBasicType() == glslang::EbtString) { |
1354 | auto str = constant->getConstArray()[0].getSConst()->c_str(); |
1355 | unsigned literal = 0; |
1356 | char* literalPtr = reinterpret_cast<char*>(&literal); |
1357 | unsigned charCount = 0; |
1358 | char ch = 0; |
1359 | do { |
1360 | ch = *(str++); |
1361 | *(literalPtr++) = ch; |
1362 | ++charCount; |
1363 | if (charCount == 4) { |
1364 | literals.push_back(x: literal); |
1365 | literalPtr = reinterpret_cast<char*>(&literal); |
1366 | charCount = 0; |
1367 | } |
1368 | } while (ch != 0); |
1369 | |
1370 | // Partial literal is padded with 0 |
1371 | if (charCount > 0) { |
1372 | for (; charCount < 4; ++charCount) |
1373 | *(literalPtr++) = 0; |
1374 | literals.push_back(x: literal); |
1375 | } |
1376 | } else |
1377 | assert(0); // Unexpected type |
1378 | } |
1379 | } |
1380 | |
1381 | // Add capabilities pertaining to how an array is indexed. |
1382 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
1383 | const glslang::TType& indexType) |
1384 | { |
1385 | #ifndef GLSLANG_WEB |
1386 | if (indexType.getQualifier().isNonUniform()) { |
1387 | // deal with an asserted non-uniform index |
1388 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
1389 | if (baseType.getBasicType() == glslang::EbtSampler) { |
1390 | if (baseType.getQualifier().hasAttachment()) |
1391 | builder.addCapability(cap: spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
1392 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
1393 | builder.addCapability(cap: spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
1394 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
1395 | builder.addCapability(cap: spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
1396 | else if (baseType.isImage()) |
1397 | builder.addCapability(cap: spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
1398 | else if (baseType.isTexture()) |
1399 | builder.addCapability(cap: spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
1400 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
1401 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
1402 | builder.addCapability(cap: spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
1403 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
1404 | builder.addCapability(cap: spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
1405 | } |
1406 | } else { |
1407 | // assume a dynamically uniform index |
1408 | if (baseType.getBasicType() == glslang::EbtSampler) { |
1409 | if (baseType.getQualifier().hasAttachment()) { |
1410 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
1411 | builder.addCapability(cap: spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
1412 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
1413 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
1414 | builder.addCapability(cap: spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
1415 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
1416 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
1417 | builder.addCapability(cap: spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
1418 | } |
1419 | } |
1420 | } |
1421 | #endif |
1422 | } |
1423 | |
1424 | // Return whether or not the given type is something that should be tied to a |
1425 | // descriptor set. |
1426 | bool IsDescriptorResource(const glslang::TType& type) |
1427 | { |
1428 | // uniform and buffer blocks are included, unless it is a push_constant |
1429 | if (type.getBasicType() == glslang::EbtBlock) |
1430 | return type.getQualifier().isUniformOrBuffer() && |
1431 | ! type.getQualifier().isShaderRecord() && |
1432 | ! type.getQualifier().isPushConstant(); |
1433 | |
1434 | // non block... |
1435 | // basically samplerXXX/subpass/sampler/texture are all included |
1436 | // if they are the global-scope-class, not the function parameter |
1437 | // (or local, if they ever exist) class. |
1438 | if (type.getBasicType() == glslang::EbtSampler || |
1439 | type.getBasicType() == glslang::EbtAccStruct) |
1440 | return type.getQualifier().isUniformOrBuffer(); |
1441 | |
1442 | // None of the above. |
1443 | return false; |
1444 | } |
1445 | |
1446 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
1447 | { |
1448 | if (child.layoutMatrix == glslang::ElmNone) |
1449 | child.layoutMatrix = parent.layoutMatrix; |
1450 | |
1451 | if (parent.invariant) |
1452 | child.invariant = true; |
1453 | if (parent.flat) |
1454 | child.flat = true; |
1455 | if (parent.centroid) |
1456 | child.centroid = true; |
1457 | #ifndef GLSLANG_WEB |
1458 | if (parent.nopersp) |
1459 | child.nopersp = true; |
1460 | if (parent.explicitInterp) |
1461 | child.explicitInterp = true; |
1462 | if (parent.perPrimitiveNV) |
1463 | child.perPrimitiveNV = true; |
1464 | if (parent.perViewNV) |
1465 | child.perViewNV = true; |
1466 | if (parent.perTaskNV) |
1467 | child.perTaskNV = true; |
1468 | if (parent.patch) |
1469 | child.patch = true; |
1470 | if (parent.sample) |
1471 | child.sample = true; |
1472 | if (parent.coherent) |
1473 | child.coherent = true; |
1474 | if (parent.devicecoherent) |
1475 | child.devicecoherent = true; |
1476 | if (parent.queuefamilycoherent) |
1477 | child.queuefamilycoherent = true; |
1478 | if (parent.workgroupcoherent) |
1479 | child.workgroupcoherent = true; |
1480 | if (parent.subgroupcoherent) |
1481 | child.subgroupcoherent = true; |
1482 | if (parent.shadercallcoherent) |
1483 | child.shadercallcoherent = true; |
1484 | if (parent.nonprivate) |
1485 | child.nonprivate = true; |
1486 | if (parent.volatil) |
1487 | child.volatil = true; |
1488 | if (parent.restrict) |
1489 | child.restrict = true; |
1490 | if (parent.readonly) |
1491 | child.readonly = true; |
1492 | if (parent.writeonly) |
1493 | child.writeonly = true; |
1494 | #endif |
1495 | if (parent.nonUniform) |
1496 | child.nonUniform = true; |
1497 | } |
1498 | |
1499 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
1500 | { |
1501 | // This should list qualifiers that simultaneous satisfy: |
1502 | // - struct members might inherit from a struct declaration |
1503 | // (note that non-block structs don't explicitly inherit, |
1504 | // only implicitly, meaning no decoration involved) |
1505 | // - affect decorations on the struct members |
1506 | // (note smooth does not, and expecting something like volatile |
1507 | // to effect the whole object) |
1508 | // - are not part of the offset/st430/etc or row/column-major layout |
1509 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
1510 | } |
1511 | |
1512 | // |
1513 | // Implement the TGlslangToSpvTraverser class. |
1514 | // |
1515 | |
1516 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
1517 | const glslang::TIntermediate* glslangIntermediate, |
1518 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
1519 | TIntermTraverser(true, false, true), |
1520 | options(options), |
1521 | shaderEntry(nullptr), currentFunction(nullptr), |
1522 | sequenceDepth(0), logger(buildLogger), |
1523 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
1524 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
1525 | glslangIntermediate(glslangIntermediate), |
1526 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
1527 | nonSemanticDebugPrintf(0) |
1528 | { |
1529 | spv::ExecutionModel executionModel = TranslateExecutionModel(stage: glslangIntermediate->getStage()); |
1530 | |
1531 | builder.clearAccessChain(); |
1532 | builder.setSource(lang: TranslateSourceLanguage(source: glslangIntermediate->getSource(), profile: glslangIntermediate->getProfile()), |
1533 | version: glslangIntermediate->getVersion()); |
1534 | |
1535 | if (options.generateDebugInfo) { |
1536 | builder.setEmitOpLines(); |
1537 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
1538 | |
1539 | // Set the source shader's text. If for SPV version 1.0, include |
1540 | // a preamble in comments stating the OpModuleProcessed instructions. |
1541 | // Otherwise, emit those as actual instructions. |
1542 | std::string text; |
1543 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
1544 | for (int p = 0; p < (int)processes.size(); ++p) { |
1545 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
1546 | text.append(s: "// OpModuleProcessed " ); |
1547 | text.append(str: processes[p]); |
1548 | text.append(s: "\n" ); |
1549 | } else |
1550 | builder.addModuleProcessed(p: processes[p]); |
1551 | } |
1552 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
1553 | text.append(s: "#line 1\n" ); |
1554 | text.append(str: glslangIntermediate->getSourceText()); |
1555 | builder.setSourceText(text); |
1556 | // Pass name and text for all included files |
1557 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
1558 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
1559 | builder.addInclude(name: iItr->first, text: iItr->second); |
1560 | } |
1561 | stdBuiltins = builder.import("GLSL.std.450" ); |
1562 | |
1563 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
1564 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
1565 | |
1566 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
1567 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
1568 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_physical_storage_buffer, incorporatedVersion: spv::Spv_1_5); |
1569 | builder.addCapability(cap: spv::CapabilityPhysicalStorageBufferAddressesEXT); |
1570 | } |
1571 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
1572 | memoryModel = spv::MemoryModelVulkanKHR; |
1573 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
1574 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_vulkan_memory_model, incorporatedVersion: spv::Spv_1_5); |
1575 | } |
1576 | builder.setMemoryModel(addr: addressingModel, mem: memoryModel); |
1577 | |
1578 | if (glslangIntermediate->usingVariablePointers()) { |
1579 | builder.addCapability(cap: spv::CapabilityVariablePointers); |
1580 | } |
1581 | |
1582 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
1583 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, name: glslangIntermediate->getEntryPointName().c_str()); |
1584 | |
1585 | // Add the source extensions |
1586 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
1587 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
1588 | builder.addSourceExtension(ext: it->c_str()); |
1589 | |
1590 | // Add the top-level modes for this shader. |
1591 | |
1592 | if (glslangIntermediate->getXfbMode()) { |
1593 | builder.addCapability(cap: spv::CapabilityTransformFeedback); |
1594 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeXfb); |
1595 | } |
1596 | |
1597 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
1598 | builder.addCapability(cap: spv::CapabilityRayTraversalPrimitiveCullingKHR); |
1599 | } |
1600 | |
1601 | #ifndef GLSLANG_WEB |
1602 | if (glslangIntermediate->getSubgroupUniformControlFlow()) { |
1603 | builder.addExtension(ext: spv::E_SPV_KHR_subgroup_uniform_control_flow); |
1604 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeSubgroupUniformControlFlowKHR); |
1605 | } |
1606 | #endif |
1607 | |
1608 | unsigned int mode; |
1609 | switch (glslangIntermediate->getStage()) { |
1610 | case EShLangVertex: |
1611 | builder.addCapability(cap: spv::CapabilityShader); |
1612 | break; |
1613 | |
1614 | case EShLangFragment: |
1615 | builder.addCapability(cap: spv::CapabilityShader); |
1616 | if (glslangIntermediate->getPixelCenterInteger()) |
1617 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModePixelCenterInteger); |
1618 | |
1619 | if (glslangIntermediate->getOriginUpperLeft()) |
1620 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOriginUpperLeft); |
1621 | else |
1622 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOriginLowerLeft); |
1623 | |
1624 | if (glslangIntermediate->getEarlyFragmentTests()) |
1625 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeEarlyFragmentTests); |
1626 | |
1627 | if (glslangIntermediate->getPostDepthCoverage()) { |
1628 | builder.addCapability(cap: spv::CapabilitySampleMaskPostDepthCoverage); |
1629 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModePostDepthCoverage); |
1630 | builder.addExtension(ext: spv::E_SPV_KHR_post_depth_coverage); |
1631 | } |
1632 | |
1633 | if (glslangIntermediate->isDepthReplacing()) |
1634 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeDepthReplacing); |
1635 | |
1636 | #ifndef GLSLANG_WEB |
1637 | |
1638 | switch(glslangIntermediate->getDepth()) { |
1639 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
1640 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
1641 | case glslang::EldUnchanged: mode = spv::ExecutionModeDepthUnchanged; break; |
1642 | default: mode = spv::ExecutionModeMax; break; |
1643 | } |
1644 | if (mode != spv::ExecutionModeMax) |
1645 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1646 | switch (glslangIntermediate->getInterlockOrdering()) { |
1647 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
1648 | break; |
1649 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
1650 | break; |
1651 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
1652 | break; |
1653 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
1654 | break; |
1655 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
1656 | break; |
1657 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
1658 | break; |
1659 | default: mode = spv::ExecutionModeMax; |
1660 | break; |
1661 | } |
1662 | if (mode != spv::ExecutionModeMax) { |
1663 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1664 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
1665 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
1666 | builder.addCapability(cap: spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
1667 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
1668 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
1669 | builder.addCapability(cap: spv::CapabilityFragmentShaderPixelInterlockEXT); |
1670 | } else { |
1671 | builder.addCapability(cap: spv::CapabilityFragmentShaderSampleInterlockEXT); |
1672 | } |
1673 | builder.addExtension(ext: spv::E_SPV_EXT_fragment_shader_interlock); |
1674 | } |
1675 | #endif |
1676 | break; |
1677 | |
1678 | case EShLangCompute: |
1679 | builder.addCapability(cap: spv::CapabilityShader); |
1680 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
1681 | std::vector<spv::Id> dimConstId; |
1682 | for (int dim = 0; dim < 3; ++dim) { |
1683 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
1684 | dimConstId.push_back(x: builder.makeUintConstant(u: glslangIntermediate->getLocalSize(dim), specConstant: specConst)); |
1685 | if (specConst) { |
1686 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
1687 | num: glslangIntermediate->getLocalSizeSpecId(dim)); |
1688 | } |
1689 | } |
1690 | builder.addExecutionModeId(shaderEntry, mode: spv::ExecutionModeLocalSizeId, operandIds: dimConstId); |
1691 | } else { |
1692 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeLocalSize, value1: glslangIntermediate->getLocalSize(dim: 0), |
1693 | value2: glslangIntermediate->getLocalSize(dim: 1), |
1694 | value3: glslangIntermediate->getLocalSize(dim: 2)); |
1695 | } |
1696 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
1697 | builder.addCapability(cap: spv::CapabilityComputeDerivativeGroupQuadsNV); |
1698 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeDerivativeGroupQuadsNV); |
1699 | builder.addExtension(ext: spv::E_SPV_NV_compute_shader_derivatives); |
1700 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
1701 | builder.addCapability(cap: spv::CapabilityComputeDerivativeGroupLinearNV); |
1702 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeDerivativeGroupLinearNV); |
1703 | builder.addExtension(ext: spv::E_SPV_NV_compute_shader_derivatives); |
1704 | } |
1705 | break; |
1706 | #ifndef GLSLANG_WEB |
1707 | case EShLangTessEvaluation: |
1708 | case EShLangTessControl: |
1709 | builder.addCapability(cap: spv::CapabilityTessellation); |
1710 | |
1711 | glslang::TLayoutGeometry primitive; |
1712 | |
1713 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
1714 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOutputVertices, |
1715 | value1: glslangIntermediate->getVertices()); |
1716 | primitive = glslangIntermediate->getOutputPrimitive(); |
1717 | } else { |
1718 | primitive = glslangIntermediate->getInputPrimitive(); |
1719 | } |
1720 | |
1721 | switch (primitive) { |
1722 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
1723 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
1724 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
1725 | default: mode = spv::ExecutionModeMax; break; |
1726 | } |
1727 | if (mode != spv::ExecutionModeMax) |
1728 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1729 | |
1730 | switch (glslangIntermediate->getVertexSpacing()) { |
1731 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
1732 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
1733 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
1734 | default: mode = spv::ExecutionModeMax; break; |
1735 | } |
1736 | if (mode != spv::ExecutionModeMax) |
1737 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1738 | |
1739 | switch (glslangIntermediate->getVertexOrder()) { |
1740 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
1741 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
1742 | default: mode = spv::ExecutionModeMax; break; |
1743 | } |
1744 | if (mode != spv::ExecutionModeMax) |
1745 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1746 | |
1747 | if (glslangIntermediate->getPointMode()) |
1748 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModePointMode); |
1749 | break; |
1750 | |
1751 | case EShLangGeometry: |
1752 | builder.addCapability(cap: spv::CapabilityGeometry); |
1753 | switch (glslangIntermediate->getInputPrimitive()) { |
1754 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
1755 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
1756 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
1757 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
1758 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
1759 | default: mode = spv::ExecutionModeMax; break; |
1760 | } |
1761 | if (mode != spv::ExecutionModeMax) |
1762 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1763 | |
1764 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeInvocations, value1: glslangIntermediate->getInvocations()); |
1765 | |
1766 | switch (glslangIntermediate->getOutputPrimitive()) { |
1767 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
1768 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
1769 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
1770 | default: mode = spv::ExecutionModeMax; break; |
1771 | } |
1772 | if (mode != spv::ExecutionModeMax) |
1773 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1774 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOutputVertices, value1: glslangIntermediate->getVertices()); |
1775 | break; |
1776 | |
1777 | case EShLangRayGen: |
1778 | case EShLangIntersect: |
1779 | case EShLangAnyHit: |
1780 | case EShLangClosestHit: |
1781 | case EShLangMiss: |
1782 | case EShLangCallable: |
1783 | { |
1784 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
1785 | if (extensions.find(x: "GL_NV_ray_tracing" ) == extensions.end()) { |
1786 | builder.addCapability(cap: spv::CapabilityRayTracingKHR); |
1787 | builder.addExtension(ext: "SPV_KHR_ray_tracing" ); |
1788 | } |
1789 | else { |
1790 | builder.addCapability(cap: spv::CapabilityRayTracingNV); |
1791 | builder.addExtension(ext: "SPV_NV_ray_tracing" ); |
1792 | } |
1793 | if (glslangIntermediate->getStage() != EShLangRayGen && glslangIntermediate->getStage() != EShLangCallable) |
1794 | { |
1795 | if (extensions.find(x: "GL_EXT_ray_cull_mask" ) != extensions.end()) { |
1796 | builder.addCapability(cap: spv::CapabilityRayCullMaskKHR); |
1797 | builder.addExtension(ext: "SPV_KHR_ray_cull_mask" ); |
1798 | } |
1799 | } |
1800 | break; |
1801 | } |
1802 | case EShLangTaskNV: |
1803 | case EShLangMeshNV: |
1804 | builder.addCapability(cap: spv::CapabilityMeshShadingNV); |
1805 | builder.addExtension(ext: spv::E_SPV_NV_mesh_shader); |
1806 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
1807 | std::vector<spv::Id> dimConstId; |
1808 | for (int dim = 0; dim < 3; ++dim) { |
1809 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
1810 | dimConstId.push_back(x: builder.makeUintConstant(u: glslangIntermediate->getLocalSize(dim), specConstant: specConst)); |
1811 | if (specConst) { |
1812 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
1813 | num: glslangIntermediate->getLocalSizeSpecId(dim)); |
1814 | } |
1815 | } |
1816 | builder.addExecutionModeId(shaderEntry, mode: spv::ExecutionModeLocalSizeId, operandIds: dimConstId); |
1817 | } else { |
1818 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeLocalSize, value1: glslangIntermediate->getLocalSize(dim: 0), |
1819 | value2: glslangIntermediate->getLocalSize(dim: 1), |
1820 | value3: glslangIntermediate->getLocalSize(dim: 2)); |
1821 | } |
1822 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
1823 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOutputVertices, |
1824 | value1: glslangIntermediate->getVertices()); |
1825 | builder.addExecutionMode(shaderEntry, mode: spv::ExecutionModeOutputPrimitivesNV, |
1826 | value1: glslangIntermediate->getPrimitives()); |
1827 | |
1828 | switch (glslangIntermediate->getOutputPrimitive()) { |
1829 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
1830 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
1831 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
1832 | default: mode = spv::ExecutionModeMax; break; |
1833 | } |
1834 | if (mode != spv::ExecutionModeMax) |
1835 | builder.addExecutionMode(shaderEntry, mode: (spv::ExecutionMode)mode); |
1836 | } |
1837 | break; |
1838 | #endif |
1839 | |
1840 | default: |
1841 | break; |
1842 | } |
1843 | |
1844 | #ifndef GLSLANG_WEB |
1845 | // |
1846 | // Add SPIR-V requirements (GL_EXT_spirv_intrinsics) |
1847 | // |
1848 | if (glslangIntermediate->hasSpirvRequirement()) { |
1849 | const glslang::TSpirvRequirement& spirvRequirement = glslangIntermediate->getSpirvRequirement(); |
1850 | |
1851 | // Add SPIR-V extension requirement |
1852 | for (auto& extension : spirvRequirement.extensions) |
1853 | builder.addExtension(ext: extension.c_str()); |
1854 | |
1855 | // Add SPIR-V capability requirement |
1856 | for (auto capability : spirvRequirement.capabilities) |
1857 | builder.addCapability(cap: static_cast<spv::Capability>(capability)); |
1858 | } |
1859 | |
1860 | // |
1861 | // Add SPIR-V execution mode qualifiers (GL_EXT_spirv_intrinsics) |
1862 | // |
1863 | if (glslangIntermediate->hasSpirvExecutionMode()) { |
1864 | const glslang::TSpirvExecutionMode spirvExecutionMode = glslangIntermediate->getSpirvExecutionMode(); |
1865 | |
1866 | // Add spirv_execution_mode |
1867 | for (auto& mode : spirvExecutionMode.modes) { |
1868 | if (!mode.second.empty()) { |
1869 | std::vector<unsigned> literals; |
1870 | TranslateLiterals(constants: mode.second, literals); |
1871 | builder.addExecutionMode(shaderEntry, mode: static_cast<spv::ExecutionMode>(mode.first), literals); |
1872 | } else |
1873 | builder.addExecutionMode(shaderEntry, mode: static_cast<spv::ExecutionMode>(mode.first)); |
1874 | } |
1875 | |
1876 | // Add spirv_execution_mode_id |
1877 | for (auto& modeId : spirvExecutionMode.modeIds) { |
1878 | std::vector<spv::Id> operandIds; |
1879 | assert(!modeId.second.empty()); |
1880 | for (auto extraOperand : modeId.second) { |
1881 | if (extraOperand->getType().getQualifier().isSpecConstant()) |
1882 | operandIds.push_back(x: getSymbolId(node: extraOperand->getAsSymbolNode())); |
1883 | else |
1884 | operandIds.push_back(x: createSpvConstant(*extraOperand)); |
1885 | } |
1886 | builder.addExecutionModeId(shaderEntry, mode: static_cast<spv::ExecutionMode>(modeId.first), operandIds); |
1887 | } |
1888 | } |
1889 | #endif |
1890 | } |
1891 | |
1892 | // Finish creating SPV, after the traversal is complete. |
1893 | void TGlslangToSpvTraverser::finishSpv() |
1894 | { |
1895 | // Finish the entry point function |
1896 | if (! entryPointTerminated) { |
1897 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
1898 | builder.leaveFunction(); |
1899 | } |
1900 | |
1901 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
1902 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
1903 | entryPoint->addIdOperand(id: *it); |
1904 | |
1905 | // Add capabilities, extensions, remove unneeded decorations, etc., |
1906 | // based on the resulting SPIR-V. |
1907 | // Note: WebGPU code generation must have the opportunity to aggressively |
1908 | // prune unreachable merge blocks and continue targets. |
1909 | builder.postProcess(); |
1910 | } |
1911 | |
1912 | // Write the SPV into 'out'. |
1913 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
1914 | { |
1915 | builder.dump(out); |
1916 | } |
1917 | |
1918 | // |
1919 | // Implement the traversal functions. |
1920 | // |
1921 | // Return true from interior nodes to have the external traversal |
1922 | // continue on to children. Return false if children were |
1923 | // already processed. |
1924 | // |
1925 | |
1926 | // |
1927 | // Symbols can turn into |
1928 | // - uniform/input reads |
1929 | // - output writes |
1930 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
1931 | // - something simple that degenerates into the last bullet |
1932 | // |
1933 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
1934 | { |
1935 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
1936 | if (symbol->getType().isStruct()) |
1937 | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
1938 | |
1939 | if (symbol->getType().getQualifier().isSpecConstant()) |
1940 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
1941 | |
1942 | #ifdef ENABLE_HLSL |
1943 | // Skip symbol handling if it is string-typed |
1944 | if (symbol->getBasicType() == glslang::EbtString) |
1945 | return; |
1946 | #endif |
1947 | |
1948 | // getSymbolId() will set up all the IO decorations on the first call. |
1949 | // Formal function parameters were mapped during makeFunctions(). |
1950 | spv::Id id = getSymbolId(node: symbol); |
1951 | |
1952 | if (builder.isPointer(resultId: id)) { |
1953 | if (!symbol->getType().getQualifier().isParamInput() && |
1954 | !symbol->getType().getQualifier().isParamOutput()) { |
1955 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
1956 | // Consider adding to the OpEntryPoint interface list. |
1957 | // Only looking at structures if they have at least one member. |
1958 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
1959 | spv::StorageClass sc = builder.getStorageClass(resultId: id); |
1960 | // Before SPIR-V 1.4, we only want to include Input and Output. |
1961 | // Starting with SPIR-V 1.4, we want all globals. |
1962 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(resultId: id)) || |
1963 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
1964 | iOSet.insert(x: id); |
1965 | } |
1966 | } |
1967 | } |
1968 | |
1969 | // If the SPIR-V type is required to be different than the AST type |
1970 | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
1971 | // translate now from the SPIR-V type to the AST type, for the consuming |
1972 | // operation. |
1973 | // Note this turns it from an l-value to an r-value. |
1974 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
1975 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
1976 | id = translateForcedType(object: id); |
1977 | } |
1978 | |
1979 | // Only process non-linkage-only nodes for generating actual static uses |
1980 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
1981 | // Prepare to generate code for the access |
1982 | |
1983 | // L-value chains will be computed left to right. We're on the symbol now, |
1984 | // which is the left-most part of the access chain, so now is "clear" time, |
1985 | // followed by setting the base. |
1986 | builder.clearAccessChain(); |
1987 | |
1988 | // For now, we consider all user variables as being in memory, so they are pointers, |
1989 | // except for |
1990 | // A) R-Value arguments to a function, which are an intermediate object. |
1991 | // See comments in handleUserFunctionCall(). |
1992 | // B) Specialization constants (normal constants don't even come in as a variable), |
1993 | // These are also pure R-values. |
1994 | // C) R-Values from type translation, see above call to translateForcedType() |
1995 | glslang::TQualifier qualifier = symbol->getQualifier(); |
1996 | if (qualifier.isSpecConstant() || rValueParameters.find(x: symbol->getId()) != rValueParameters.end() || |
1997 | !builder.isPointerType(typeId: builder.getTypeId(resultId: id))) |
1998 | builder.setAccessChainRValue(id); |
1999 | else |
2000 | builder.setAccessChainLValue(id); |
2001 | } |
2002 | |
2003 | #ifdef ENABLE_HLSL |
2004 | // Process linkage-only nodes for any special additional interface work. |
2005 | if (linkageOnly) { |
2006 | if (glslangIntermediate->getHlslFunctionality1()) { |
2007 | // Map implicit counter buffers to their originating buffers, which should have been |
2008 | // seen by now, given earlier pruning of unused counters, and preservation of order |
2009 | // of declaration. |
2010 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
2011 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
2012 | // Save possible originating buffers for counter buffers, keyed by |
2013 | // making the potential counter-buffer name. |
2014 | std::string keyName = symbol->getName().c_str(); |
2015 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
2016 | counterOriginator[keyName] = symbol; |
2017 | } else { |
2018 | // Handle a counter buffer, by finding the saved originating buffer. |
2019 | std::string keyName = symbol->getName().c_str(); |
2020 | auto it = counterOriginator.find(keyName); |
2021 | if (it != counterOriginator.end()) { |
2022 | id = getSymbolId(it->second); |
2023 | if (id != spv::NoResult) { |
2024 | spv::Id counterId = getSymbolId(symbol); |
2025 | if (counterId != spv::NoResult) { |
2026 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1" ); |
2027 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
2028 | } |
2029 | } |
2030 | } |
2031 | } |
2032 | } |
2033 | } |
2034 | } |
2035 | #endif |
2036 | } |
2037 | |
2038 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
2039 | { |
2040 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
2041 | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
2042 | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
2043 | } |
2044 | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
2045 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
2046 | } |
2047 | |
2048 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2049 | if (node->getType().getQualifier().isSpecConstant()) |
2050 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2051 | |
2052 | // First, handle special cases |
2053 | switch (node->getOp()) { |
2054 | case glslang::EOpAssign: |
2055 | case glslang::EOpAddAssign: |
2056 | case glslang::EOpSubAssign: |
2057 | case glslang::EOpMulAssign: |
2058 | case glslang::EOpVectorTimesMatrixAssign: |
2059 | case glslang::EOpVectorTimesScalarAssign: |
2060 | case glslang::EOpMatrixTimesScalarAssign: |
2061 | case glslang::EOpMatrixTimesMatrixAssign: |
2062 | case glslang::EOpDivAssign: |
2063 | case glslang::EOpModAssign: |
2064 | case glslang::EOpAndAssign: |
2065 | case glslang::EOpInclusiveOrAssign: |
2066 | case glslang::EOpExclusiveOrAssign: |
2067 | case glslang::EOpLeftShiftAssign: |
2068 | case glslang::EOpRightShiftAssign: |
2069 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
2070 | // where a is evaluated before b. For a simple assignment, GLSL |
2071 | // says to evaluate the left before the right. So, always, left |
2072 | // node then right node. |
2073 | { |
2074 | // get the left l-value, save it away |
2075 | builder.clearAccessChain(); |
2076 | node->getLeft()->traverse(this); |
2077 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
2078 | |
2079 | // evaluate the right |
2080 | builder.clearAccessChain(); |
2081 | node->getRight()->traverse(this); |
2082 | spv::Id rValue = accessChainLoad(type: node->getRight()->getType()); |
2083 | |
2084 | if (node->getOp() != glslang::EOpAssign) { |
2085 | // the left is also an r-value |
2086 | builder.setAccessChain(lValue); |
2087 | spv::Id leftRValue = accessChainLoad(type: node->getLeft()->getType()); |
2088 | |
2089 | // do the operation |
2090 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(type: node->getLeft()->getType()); |
2091 | coherentFlags |= TranslateCoherent(type: node->getRight()->getType()); |
2092 | OpDecorations decorations = { TranslatePrecisionDecoration(glslangPrecision: node->getOperationPrecision()), |
2093 | TranslateNoContractionDecoration(qualifier: node->getType().getQualifier()), |
2094 | TranslateNonUniformDecoration(coherentFlags) }; |
2095 | rValue = createBinaryOperation(op: node->getOp(), decorations, |
2096 | typeId: convertGlslangToSpvType(type: node->getType()), left: leftRValue, right: rValue, |
2097 | typeProxy: node->getType().getBasicType()); |
2098 | |
2099 | // these all need their counterparts in createBinaryOperation() |
2100 | assert(rValue != spv::NoResult); |
2101 | } |
2102 | |
2103 | // store the result |
2104 | builder.setAccessChain(lValue); |
2105 | multiTypeStore(node->getLeft()->getType(), rValue); |
2106 | |
2107 | // assignments are expressions having an rValue after they are evaluated... |
2108 | builder.clearAccessChain(); |
2109 | builder.setAccessChainRValue(rValue); |
2110 | } |
2111 | return false; |
2112 | case glslang::EOpIndexDirect: |
2113 | case glslang::EOpIndexDirectStruct: |
2114 | { |
2115 | // Structure, array, matrix, or vector indirection with statically known index. |
2116 | // Get the left part of the access chain. |
2117 | node->getLeft()->traverse(this); |
2118 | |
2119 | // Add the next element in the chain |
2120 | |
2121 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
2122 | if (! node->getLeft()->getType().isArray() && |
2123 | node->getLeft()->getType().isVector() && |
2124 | node->getOp() == glslang::EOpIndexDirect) { |
2125 | // Swizzle is uniform so propagate uniform into access chain |
2126 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(type: node->getLeft()->getType()); |
2127 | coherentFlags.nonUniform = 0; |
2128 | // This is essentially a hard-coded vector swizzle of size 1, |
2129 | // so short circuit the access-chain stuff with a swizzle. |
2130 | std::vector<unsigned> swizzle; |
2131 | swizzle.push_back(x: glslangIndex); |
2132 | int dummySize; |
2133 | builder.accessChainPushSwizzle(swizzle, preSwizzleBaseType: convertGlslangToSpvType(type: node->getLeft()->getType()), |
2134 | coherentFlags, |
2135 | alignment: glslangIntermediate->getBaseAlignmentScalar( |
2136 | node->getLeft()->getType(), size&: dummySize)); |
2137 | } else { |
2138 | |
2139 | // Load through a block reference is performed with a dot operator that |
2140 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
2141 | // do a load and reset the access chain. |
2142 | if (node->getLeft()->isReference() && |
2143 | !node->getLeft()->getType().isArray() && |
2144 | node->getOp() == glslang::EOpIndexDirectStruct) |
2145 | { |
2146 | spv::Id left = accessChainLoad(type: node->getLeft()->getType()); |
2147 | builder.clearAccessChain(); |
2148 | builder.setAccessChainLValue(left); |
2149 | } |
2150 | |
2151 | int spvIndex = glslangIndex; |
2152 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
2153 | node->getOp() == glslang::EOpIndexDirectStruct) |
2154 | { |
2155 | // This may be, e.g., an anonymous block-member selection, which generally need |
2156 | // index remapping due to hidden members in anonymous blocks. |
2157 | long long glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
2158 | if (memberRemapper.find(x: glslangId) != memberRemapper.end()) { |
2159 | std::vector<int>& remapper = memberRemapper[glslangId]; |
2160 | assert(remapper.size() > 0); |
2161 | spvIndex = remapper[glslangIndex]; |
2162 | } |
2163 | } |
2164 | |
2165 | // Struct reference propagates uniform lvalue |
2166 | spv::Builder::AccessChain::CoherentFlags coherentFlags = |
2167 | TranslateCoherent(type: node->getLeft()->getType()); |
2168 | coherentFlags.nonUniform = 0; |
2169 | |
2170 | // normal case for indexing array or structure or block |
2171 | builder.accessChainPush(offset: builder.makeIntConstant(i: spvIndex), |
2172 | coherentFlags, |
2173 | alignment: node->getLeft()->getType().getBufferReferenceAlignment()); |
2174 | |
2175 | // Add capabilities here for accessing PointSize and clip/cull distance. |
2176 | // We have deferred generation of associated capabilities until now. |
2177 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
2178 | declareUseOfStructMember(members: *(node->getLeft()->getType().getStruct()), glslangMember: glslangIndex); |
2179 | } |
2180 | } |
2181 | return false; |
2182 | case glslang::EOpIndexIndirect: |
2183 | { |
2184 | // Array, matrix, or vector indirection with variable index. |
2185 | // Will use native SPIR-V access-chain for and array indirection; |
2186 | // matrices are arrays of vectors, so will also work for a matrix. |
2187 | // Will use the access chain's 'component' for variable index into a vector. |
2188 | |
2189 | // This adapter is building access chains left to right. |
2190 | // Set up the access chain to the left. |
2191 | node->getLeft()->traverse(this); |
2192 | |
2193 | // save it so that computing the right side doesn't trash it |
2194 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
2195 | |
2196 | // compute the next index in the chain |
2197 | builder.clearAccessChain(); |
2198 | node->getRight()->traverse(this); |
2199 | spv::Id index = accessChainLoad(type: node->getRight()->getType()); |
2200 | |
2201 | addIndirectionIndexCapabilities(baseType: node->getLeft()->getType(), indexType: node->getRight()->getType()); |
2202 | |
2203 | // restore the saved access chain |
2204 | builder.setAccessChain(partial); |
2205 | |
2206 | // Only if index is nonUniform should we propagate nonUniform into access chain |
2207 | spv::Builder::AccessChain::CoherentFlags index_flags = TranslateCoherent(type: node->getRight()->getType()); |
2208 | spv::Builder::AccessChain::CoherentFlags coherent_flags = TranslateCoherent(type: node->getLeft()->getType()); |
2209 | coherent_flags.nonUniform = index_flags.nonUniform; |
2210 | |
2211 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
2212 | int dummySize; |
2213 | builder.accessChainPushComponent( |
2214 | component: index, preSwizzleBaseType: convertGlslangToSpvType(type: node->getLeft()->getType()), coherentFlags: coherent_flags, |
2215 | alignment: glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
2216 | size&: dummySize)); |
2217 | } else |
2218 | builder.accessChainPush(offset: index, coherentFlags: coherent_flags, |
2219 | alignment: node->getLeft()->getType().getBufferReferenceAlignment()); |
2220 | } |
2221 | return false; |
2222 | case glslang::EOpVectorSwizzle: |
2223 | { |
2224 | node->getLeft()->traverse(this); |
2225 | std::vector<unsigned> swizzle; |
2226 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
2227 | int dummySize; |
2228 | builder.accessChainPushSwizzle(swizzle, preSwizzleBaseType: convertGlslangToSpvType(type: node->getLeft()->getType()), |
2229 | coherentFlags: TranslateCoherent(type: node->getLeft()->getType()), |
2230 | alignment: glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
2231 | size&: dummySize)); |
2232 | } |
2233 | return false; |
2234 | case glslang::EOpMatrixSwizzle: |
2235 | logger->missingFunctionality(f: "matrix swizzle" ); |
2236 | return true; |
2237 | case glslang::EOpLogicalOr: |
2238 | case glslang::EOpLogicalAnd: |
2239 | { |
2240 | |
2241 | // These may require short circuiting, but can sometimes be done as straight |
2242 | // binary operations. The right operand must be short circuited if it has |
2243 | // side effects, and should probably be if it is complex. |
2244 | if (isTrivial(node: node->getRight()->getAsTyped())) |
2245 | break; // handle below as a normal binary operation |
2246 | // otherwise, we need to do dynamic short circuiting on the right operand |
2247 | spv::Id result = createShortCircuit(node->getOp(), left&: *node->getLeft()->getAsTyped(), |
2248 | right&: *node->getRight()->getAsTyped()); |
2249 | builder.clearAccessChain(); |
2250 | builder.setAccessChainRValue(result); |
2251 | } |
2252 | return false; |
2253 | default: |
2254 | break; |
2255 | } |
2256 | |
2257 | // Assume generic binary op... |
2258 | |
2259 | // get right operand |
2260 | builder.clearAccessChain(); |
2261 | node->getLeft()->traverse(this); |
2262 | spv::Id left = accessChainLoad(type: node->getLeft()->getType()); |
2263 | |
2264 | // get left operand |
2265 | builder.clearAccessChain(); |
2266 | node->getRight()->traverse(this); |
2267 | spv::Id right = accessChainLoad(type: node->getRight()->getType()); |
2268 | |
2269 | // get result |
2270 | OpDecorations decorations = { TranslatePrecisionDecoration(glslangPrecision: node->getOperationPrecision()), |
2271 | TranslateNoContractionDecoration(qualifier: node->getType().getQualifier()), |
2272 | TranslateNonUniformDecoration(qualifier: node->getType().getQualifier()) }; |
2273 | spv::Id result = createBinaryOperation(op: node->getOp(), decorations, |
2274 | typeId: convertGlslangToSpvType(type: node->getType()), left, right, |
2275 | typeProxy: node->getLeft()->getType().getBasicType()); |
2276 | |
2277 | builder.clearAccessChain(); |
2278 | if (! result) { |
2279 | logger->missingFunctionality(f: "unknown glslang binary operation" ); |
2280 | return true; // pick up a child as the place-holder result |
2281 | } else { |
2282 | builder.setAccessChainRValue(result); |
2283 | return false; |
2284 | } |
2285 | } |
2286 | |
2287 | spv::Id TGlslangToSpvTraverser::convertLoadedBoolInUniformToUint(const glslang::TType& type, |
2288 | spv::Id nominalTypeId, |
2289 | spv::Id loadedId) |
2290 | { |
2291 | if (builder.isScalarType(typeId: nominalTypeId)) { |
2292 | // Conversion for bool |
2293 | spv::Id boolType = builder.makeBoolType(); |
2294 | if (nominalTypeId != boolType) |
2295 | return builder.createBinOp(spv::OpINotEqual, typeId: boolType, operand1: loadedId, operand2: builder.makeUintConstant(u: 0)); |
2296 | } else if (builder.isVectorType(typeId: nominalTypeId)) { |
2297 | // Conversion for bvec |
2298 | int vecSize = builder.getNumTypeComponents(typeId: nominalTypeId); |
2299 | spv::Id bvecType = builder.makeVectorType(component: builder.makeBoolType(), size: vecSize); |
2300 | if (nominalTypeId != bvecType) |
2301 | loadedId = builder.createBinOp(spv::OpINotEqual, typeId: bvecType, operand1: loadedId, |
2302 | operand2: makeSmearedConstant(constant: builder.makeUintConstant(u: 0), vectorSize: vecSize)); |
2303 | } else if (builder.isArrayType(typeId: nominalTypeId)) { |
2304 | // Conversion for bool array |
2305 | spv::Id boolArrayTypeId = convertGlslangToSpvType(type); |
2306 | if (nominalTypeId != boolArrayTypeId) |
2307 | { |
2308 | // Use OpCopyLogical from SPIR-V 1.4 if available. |
2309 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) |
2310 | return builder.createUnaryOp(spv::OpCopyLogical, typeId: boolArrayTypeId, operand: loadedId); |
2311 | |
2312 | glslang::TType glslangElementType(type, 0); |
2313 | spv::Id elementNominalTypeId = builder.getContainedTypeId(typeId: nominalTypeId); |
2314 | std::vector<spv::Id> constituents; |
2315 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
2316 | // get the element |
2317 | spv::Id elementValue = builder.createCompositeExtract(composite: loadedId, typeId: elementNominalTypeId, index); |
2318 | |
2319 | // recursively convert it |
2320 | spv::Id elementConvertedValue = convertLoadedBoolInUniformToUint(type: glslangElementType, nominalTypeId: elementNominalTypeId, loadedId: elementValue); |
2321 | constituents.push_back(x: elementConvertedValue); |
2322 | } |
2323 | return builder.createCompositeConstruct(typeId: boolArrayTypeId, constituents); |
2324 | } |
2325 | } |
2326 | |
2327 | return loadedId; |
2328 | } |
2329 | |
2330 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
2331 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
2332 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
2333 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
2334 | const glslang::TType& glslangType) |
2335 | { |
2336 | switch(glslangBuiltIn) |
2337 | { |
2338 | case glslang::EbvSubGroupEqMask: |
2339 | case glslang::EbvSubGroupGeMask: |
2340 | case glslang::EbvSubGroupGtMask: |
2341 | case glslang::EbvSubGroupLeMask: |
2342 | case glslang::EbvSubGroupLtMask: { |
2343 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
2344 | if (glslangType.isVector()) |
2345 | break; |
2346 | spv::Id ivec4_type = builder.makeVectorType(component: builder.makeUintType(width: 32), size: 4); |
2347 | spv::Id uint64_type = builder.makeUintType(width: 64); |
2348 | std::pair<spv::Id, spv::Id> ret(ivec4_type, uint64_type); |
2349 | return ret; |
2350 | } |
2351 | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
2352 | // builtins. During visitBinary we insert a transpose |
2353 | case glslang::EbvWorldToObject3x4: |
2354 | case glslang::EbvObjectToWorld3x4: { |
2355 | spv::Id mat43 = builder.makeMatrixType(component: builder.makeFloatType(width: 32), cols: 4, rows: 3); |
2356 | spv::Id mat34 = builder.makeMatrixType(component: builder.makeFloatType(width: 32), cols: 3, rows: 4); |
2357 | std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
2358 | return ret; |
2359 | } |
2360 | default: |
2361 | break; |
2362 | } |
2363 | |
2364 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
2365 | return ret; |
2366 | } |
2367 | |
2368 | // For an object previously identified (see getForcedType() and forceType) |
2369 | // as needing type translations, do the translation needed for a load, turning |
2370 | // an L-value into in R-value. |
2371 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
2372 | { |
2373 | const auto forceIt = forceType.find(x: object); |
2374 | if (forceIt == forceType.end()) |
2375 | return object; |
2376 | |
2377 | spv::Id desiredTypeId = forceIt->second; |
2378 | spv::Id objectTypeId = builder.getTypeId(resultId: object); |
2379 | assert(builder.isPointerType(objectTypeId)); |
2380 | objectTypeId = builder.getContainedTypeId(typeId: objectTypeId); |
2381 | if (builder.isVectorType(typeId: objectTypeId) && |
2382 | builder.getScalarTypeWidth(typeId: builder.getContainedTypeId(typeId: objectTypeId)) == 32) { |
2383 | if (builder.getScalarTypeWidth(typeId: desiredTypeId) == 64) { |
2384 | // handle 32-bit v.xy* -> 64-bit |
2385 | builder.clearAccessChain(); |
2386 | builder.setAccessChainLValue(object); |
2387 | object = builder.accessChainLoad(precision: spv::NoPrecision, l_nonUniform: spv::DecorationMax, r_nonUniform: spv::DecorationMax, ResultType: objectTypeId); |
2388 | std::vector<spv::Id> components; |
2389 | components.push_back(x: builder.createCompositeExtract(composite: object, typeId: builder.getContainedTypeId(typeId: objectTypeId), index: 0)); |
2390 | components.push_back(x: builder.createCompositeExtract(composite: object, typeId: builder.getContainedTypeId(typeId: objectTypeId), index: 1)); |
2391 | |
2392 | spv::Id vecType = builder.makeVectorType(component: builder.getContainedTypeId(typeId: objectTypeId), size: 2); |
2393 | return builder.createUnaryOp(spv::OpBitcast, typeId: desiredTypeId, |
2394 | operand: builder.createCompositeConstruct(typeId: vecType, constituents: components)); |
2395 | } else { |
2396 | logger->missingFunctionality(f: "forcing 32-bit vector type to non 64-bit scalar" ); |
2397 | } |
2398 | } else if (builder.isMatrixType(typeId: objectTypeId)) { |
2399 | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
2400 | // and we insert a transpose after loading the original non-transposed builtins |
2401 | builder.clearAccessChain(); |
2402 | builder.setAccessChainLValue(object); |
2403 | object = builder.accessChainLoad(precision: spv::NoPrecision, l_nonUniform: spv::DecorationMax, r_nonUniform: spv::DecorationMax, ResultType: objectTypeId); |
2404 | return builder.createUnaryOp(spv::OpTranspose, typeId: desiredTypeId, operand: object); |
2405 | |
2406 | } else { |
2407 | logger->missingFunctionality(f: "forcing non 32-bit vector type" ); |
2408 | } |
2409 | |
2410 | return object; |
2411 | } |
2412 | |
2413 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
2414 | { |
2415 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
2416 | |
2417 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2418 | if (node->getType().getQualifier().isSpecConstant()) |
2419 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2420 | |
2421 | spv::Id result = spv::NoResult; |
2422 | |
2423 | // try texturing first |
2424 | result = createImageTextureFunctionCall(node); |
2425 | if (result != spv::NoResult) { |
2426 | builder.clearAccessChain(); |
2427 | builder.setAccessChainRValue(result); |
2428 | |
2429 | return false; // done with this node |
2430 | } |
2431 | |
2432 | // Non-texturing. |
2433 | |
2434 | if (node->getOp() == glslang::EOpArrayLength) { |
2435 | // Quite special; won't want to evaluate the operand. |
2436 | |
2437 | // Currently, the front-end does not allow .length() on an array until it is sized, |
2438 | // except for the last block membeor of an SSBO. |
2439 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
2440 | // size extracted. |
2441 | |
2442 | // Normal .length() would have been constant folded by the front-end. |
2443 | // So, this has to be block.lastMember.length(). |
2444 | // SPV wants "block" and member number as the operands, go get them. |
2445 | |
2446 | spv::Id length; |
2447 | if (node->getOperand()->getType().isCoopMat()) { |
2448 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2449 | |
2450 | spv::Id typeId = convertGlslangToSpvType(type: node->getOperand()->getType()); |
2451 | assert(builder.isCooperativeMatrixType(typeId)); |
2452 | |
2453 | length = builder.createCooperativeMatrixLength(type: typeId); |
2454 | } else { |
2455 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
2456 | block->traverse(this); |
2457 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
2458 | ->getConstArray()[0].getUConst(); |
2459 | length = builder.createArrayLength(base: builder.accessChainGetLValue(), member); |
2460 | } |
2461 | |
2462 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
2463 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
2464 | // AST expectation of a signed result. |
2465 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
2466 | if (builder.isInSpecConstCodeGenMode()) { |
2467 | length = builder.createBinOp(spv::OpIAdd, typeId: builder.makeIntType(width: 32), operand1: length, operand2: builder.makeIntConstant(i: 0)); |
2468 | } else { |
2469 | length = builder.createUnaryOp(spv::OpBitcast, typeId: builder.makeIntType(width: 32), operand: length); |
2470 | } |
2471 | } |
2472 | |
2473 | builder.clearAccessChain(); |
2474 | builder.setAccessChainRValue(length); |
2475 | |
2476 | return false; |
2477 | } |
2478 | |
2479 | // Start by evaluating the operand |
2480 | |
2481 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
2482 | // operate first on the swizzle base, then apply the swizzle. |
2483 | spv::Id invertedType = spv::NoType; |
2484 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
2485 | invertedType : convertGlslangToSpvType(type: node->getType()); }; |
2486 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
2487 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
2488 | |
2489 | builder.clearAccessChain(); |
2490 | TIntermNode *operandNode; |
2491 | if (invertedType != spv::NoType) |
2492 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
2493 | else |
2494 | operandNode = node->getOperand(); |
2495 | |
2496 | operandNode->traverse(this); |
2497 | |
2498 | spv::Id operand = spv::NoResult; |
2499 | |
2500 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
2501 | |
2502 | #ifndef GLSLANG_WEB |
2503 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
2504 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
2505 | node->getOp() == glslang::EOpAtomicCounter || |
2506 | (node->getOp() == glslang::EOpInterpolateAtCentroid && |
2507 | glslangIntermediate->getSource() != glslang::EShSourceHlsl) || |
2508 | node->getOp() == glslang::EOpRayQueryProceed || |
2509 | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
2510 | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
2511 | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
2512 | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
2513 | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
2514 | node->getOp() == glslang::EOpRayQueryTerminate || |
2515 | node->getOp() == glslang::EOpRayQueryConfirmIntersection || |
2516 | (node->getOp() == glslang::EOpSpirvInst && operandNode->getAsTyped()->getQualifier().isSpirvByReference())) { |
2517 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
2518 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
2519 | lvalueCoherentFlags |= TranslateCoherent(type: operandNode->getAsTyped()->getType()); |
2520 | } else if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
2521 | // Will be translated to a literal value, make a placeholder here |
2522 | operand = spv::NoResult; |
2523 | } else |
2524 | #endif |
2525 | { |
2526 | operand = accessChainLoad(type: node->getOperand()->getType()); |
2527 | } |
2528 | |
2529 | OpDecorations decorations = { TranslatePrecisionDecoration(glslangPrecision: node->getOperationPrecision()), |
2530 | TranslateNoContractionDecoration(qualifier: node->getType().getQualifier()), |
2531 | TranslateNonUniformDecoration(qualifier: node->getType().getQualifier()) }; |
2532 | |
2533 | // it could be a conversion |
2534 | if (! result) |
2535 | result = createConversion(op: node->getOp(), decorations, destTypeId: resultType(), operand, |
2536 | typeProxy: node->getOperand()->getBasicType()); |
2537 | |
2538 | // if not, then possibly an operation |
2539 | if (! result) |
2540 | result = createUnaryOperation(op: node->getOp(), decorations, typeId: resultType(), operand, |
2541 | typeProxy: node->getOperand()->getBasicType(), lvalueCoherentFlags); |
2542 | |
2543 | #ifndef GLSLANG_WEB |
2544 | // it could be attached to a SPIR-V intruction |
2545 | if (!result) { |
2546 | if (node->getOp() == glslang::EOpSpirvInst) { |
2547 | const auto& spirvInst = node->getSpirvInstruction(); |
2548 | if (spirvInst.set == "" ) { |
2549 | spv::IdImmediate idImmOp = {true, operand}; |
2550 | if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
2551 | // Translate the constant to a literal value |
2552 | std::vector<unsigned> literals; |
2553 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
2554 | constants.push_back(x: operandNode->getAsConstantUnion()); |
2555 | TranslateLiterals(constants, literals); |
2556 | idImmOp = {false, literals[0]}; |
2557 | } |
2558 | |
2559 | if (node->getBasicType() == glslang::EbtVoid) |
2560 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), operands: {idImmOp}); |
2561 | else |
2562 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), typeId: resultType(), operands: {idImmOp}); |
2563 | } else { |
2564 | result = builder.createBuiltinCall( |
2565 | resultType: resultType(), builtins: spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(name: spirvInst.set.c_str()), |
2566 | entryPoint: spirvInst.id, args: {operand}); |
2567 | } |
2568 | |
2569 | if (node->getBasicType() == glslang::EbtVoid) |
2570 | return false; // done with this node |
2571 | } |
2572 | } |
2573 | #endif |
2574 | |
2575 | if (result) { |
2576 | if (invertedType) { |
2577 | result = createInvertedSwizzle(precision: decorations.precision, *node->getOperand(), parentResult: result); |
2578 | decorations.addNonUniform(builder, t: result); |
2579 | } |
2580 | |
2581 | builder.clearAccessChain(); |
2582 | builder.setAccessChainRValue(result); |
2583 | |
2584 | return false; // done with this node |
2585 | } |
2586 | |
2587 | // it must be a special case, check... |
2588 | switch (node->getOp()) { |
2589 | case glslang::EOpPostIncrement: |
2590 | case glslang::EOpPostDecrement: |
2591 | case glslang::EOpPreIncrement: |
2592 | case glslang::EOpPreDecrement: |
2593 | { |
2594 | // we need the integer value "1" or the floating point "1.0" to add/subtract |
2595 | spv::Id one = 0; |
2596 | if (node->getBasicType() == glslang::EbtFloat) |
2597 | one = builder.makeFloatConstant(f: 1.0F); |
2598 | #ifndef GLSLANG_WEB |
2599 | else if (node->getBasicType() == glslang::EbtDouble) |
2600 | one = builder.makeDoubleConstant(d: 1.0); |
2601 | else if (node->getBasicType() == glslang::EbtFloat16) |
2602 | one = builder.makeFloat16Constant(f16: 1.0F); |
2603 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
2604 | one = builder.makeInt8Constant(i: 1); |
2605 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
2606 | one = builder.makeInt16Constant(i: 1); |
2607 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
2608 | one = builder.makeInt64Constant(i: 1); |
2609 | #endif |
2610 | else |
2611 | one = builder.makeIntConstant(i: 1); |
2612 | glslang::TOperator op; |
2613 | if (node->getOp() == glslang::EOpPreIncrement || |
2614 | node->getOp() == glslang::EOpPostIncrement) |
2615 | op = glslang::EOpAdd; |
2616 | else |
2617 | op = glslang::EOpSub; |
2618 | |
2619 | spv::Id result = createBinaryOperation(op, decorations, |
2620 | typeId: convertGlslangToSpvType(type: node->getType()), left: operand, right: one, |
2621 | typeProxy: node->getType().getBasicType()); |
2622 | assert(result != spv::NoResult); |
2623 | |
2624 | // The result of operation is always stored, but conditionally the |
2625 | // consumed result. The consumed result is always an r-value. |
2626 | builder.accessChainStore(rvalue: result, |
2627 | nonUniform: TranslateNonUniformDecoration(coherentFlags: builder.getAccessChain().coherentFlags)); |
2628 | builder.clearAccessChain(); |
2629 | if (node->getOp() == glslang::EOpPreIncrement || |
2630 | node->getOp() == glslang::EOpPreDecrement) |
2631 | builder.setAccessChainRValue(result); |
2632 | else |
2633 | builder.setAccessChainRValue(operand); |
2634 | } |
2635 | |
2636 | return false; |
2637 | |
2638 | #ifndef GLSLANG_WEB |
2639 | case glslang::EOpEmitStreamVertex: |
2640 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
2641 | return false; |
2642 | case glslang::EOpEndStreamPrimitive: |
2643 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
2644 | return false; |
2645 | case glslang::EOpRayQueryTerminate: |
2646 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
2647 | return false; |
2648 | case glslang::EOpRayQueryConfirmIntersection: |
2649 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
2650 | return false; |
2651 | #endif |
2652 | |
2653 | default: |
2654 | logger->missingFunctionality(f: "unknown glslang unary" ); |
2655 | return true; // pick up operand as placeholder result |
2656 | } |
2657 | } |
2658 | |
2659 | // Construct a composite object, recursively copying members if their types don't match |
2660 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
2661 | { |
2662 | for (int c = 0; c < (int)constituents.size(); ++c) { |
2663 | spv::Id& constituent = constituents[c]; |
2664 | spv::Id lType = builder.getContainedTypeId(typeId: resultTypeId, c); |
2665 | spv::Id rType = builder.getTypeId(resultId: constituent); |
2666 | if (lType != rType) { |
2667 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
2668 | constituent = builder.createUnaryOp(spv::OpCopyLogical, typeId: lType, operand: constituent); |
2669 | } else if (builder.isStructType(typeId: rType)) { |
2670 | std::vector<spv::Id> rTypeConstituents; |
2671 | int numrTypeConstituents = builder.getNumTypeConstituents(typeId: rType); |
2672 | for (int i = 0; i < numrTypeConstituents; ++i) { |
2673 | rTypeConstituents.push_back(x: builder.createCompositeExtract(composite: constituent, |
2674 | typeId: builder.getContainedTypeId(typeId: rType, i), index: i)); |
2675 | } |
2676 | constituents[c] = createCompositeConstruct(resultTypeId: lType, constituents: rTypeConstituents); |
2677 | } else { |
2678 | assert(builder.isArrayType(rType)); |
2679 | std::vector<spv::Id> rTypeConstituents; |
2680 | int numrTypeConstituents = builder.getNumTypeConstituents(typeId: rType); |
2681 | |
2682 | spv::Id elementRType = builder.getContainedTypeId(typeId: rType); |
2683 | for (int i = 0; i < numrTypeConstituents; ++i) { |
2684 | rTypeConstituents.push_back(x: builder.createCompositeExtract(composite: constituent, typeId: elementRType, index: i)); |
2685 | } |
2686 | constituents[c] = createCompositeConstruct(resultTypeId: lType, constituents: rTypeConstituents); |
2687 | } |
2688 | } |
2689 | } |
2690 | return builder.createCompositeConstruct(typeId: resultTypeId, constituents); |
2691 | } |
2692 | |
2693 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
2694 | { |
2695 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2696 | if (node->getType().getQualifier().isSpecConstant()) |
2697 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2698 | |
2699 | spv::Id result = spv::NoResult; |
2700 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
2701 | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
2702 | // SPIR-V, for an out parameter |
2703 | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
2704 | |
2705 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
2706 | invertedType : |
2707 | convertGlslangToSpvType(type: node->getType()); }; |
2708 | |
2709 | // try texturing |
2710 | result = createImageTextureFunctionCall(node); |
2711 | if (result != spv::NoResult) { |
2712 | builder.clearAccessChain(); |
2713 | builder.setAccessChainRValue(result); |
2714 | |
2715 | return false; |
2716 | } |
2717 | #ifndef GLSLANG_WEB |
2718 | else if (node->getOp() == glslang::EOpImageStore || |
2719 | node->getOp() == glslang::EOpImageStoreLod || |
2720 | node->getOp() == glslang::EOpImageAtomicStore) { |
2721 | // "imageStore" is a special case, which has no result |
2722 | return false; |
2723 | } |
2724 | #endif |
2725 | |
2726 | glslang::TOperator binOp = glslang::EOpNull; |
2727 | bool reduceComparison = true; |
2728 | bool isMatrix = false; |
2729 | bool noReturnValue = false; |
2730 | bool atomic = false; |
2731 | |
2732 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
2733 | |
2734 | assert(node->getOp()); |
2735 | |
2736 | spv::Decoration precision = TranslatePrecisionDecoration(glslangPrecision: node->getOperationPrecision()); |
2737 | |
2738 | switch (node->getOp()) { |
2739 | case glslang::EOpSequence: |
2740 | { |
2741 | if (preVisit) |
2742 | ++sequenceDepth; |
2743 | else |
2744 | --sequenceDepth; |
2745 | |
2746 | if (sequenceDepth == 1) { |
2747 | // If this is the parent node of all the functions, we want to see them |
2748 | // early, so all call points have actual SPIR-V functions to reference. |
2749 | // In all cases, still let the traverser visit the children for us. |
2750 | makeFunctions(node->getAsAggregate()->getSequence()); |
2751 | |
2752 | // Also, we want all globals initializers to go into the beginning of the entry point, before |
2753 | // anything else gets there, so visit out of order, doing them all now. |
2754 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
2755 | |
2756 | //Pre process linker objects for ray tracing stages |
2757 | if (glslangIntermediate->isRayTracingStage()) |
2758 | collectRayTracingLinkerObjects(); |
2759 | |
2760 | // Initializers are done, don't want to visit again, but functions and link objects need to be processed, |
2761 | // so do them manually. |
2762 | visitFunctions(node->getAsAggregate()->getSequence()); |
2763 | |
2764 | return false; |
2765 | } |
2766 | |
2767 | return true; |
2768 | } |
2769 | case glslang::EOpLinkerObjects: |
2770 | { |
2771 | if (visit == glslang::EvPreVisit) |
2772 | linkageOnly = true; |
2773 | else |
2774 | linkageOnly = false; |
2775 | |
2776 | return true; |
2777 | } |
2778 | case glslang::EOpComma: |
2779 | { |
2780 | // processing from left to right naturally leaves the right-most |
2781 | // lying around in the access chain |
2782 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
2783 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
2784 | glslangOperands[i]->traverse(this); |
2785 | |
2786 | return false; |
2787 | } |
2788 | case glslang::EOpFunction: |
2789 | if (visit == glslang::EvPreVisit) { |
2790 | if (isShaderEntryPoint(node)) { |
2791 | inEntryPoint = true; |
2792 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
2793 | currentFunction = shaderEntry; |
2794 | } else { |
2795 | handleFunctionEntry(node); |
2796 | } |
2797 | if (options.generateDebugInfo) { |
2798 | const auto& loc = node->getLoc(); |
2799 | currentFunction->setDebugLineInfo(fileName: builder.getSourceFile(), line: loc.line, column: loc.column); |
2800 | } |
2801 | } else { |
2802 | if (inEntryPoint) |
2803 | entryPointTerminated = true; |
2804 | builder.leaveFunction(); |
2805 | inEntryPoint = false; |
2806 | } |
2807 | |
2808 | return true; |
2809 | case glslang::EOpParameters: |
2810 | // Parameters will have been consumed by EOpFunction processing, but not |
2811 | // the body, so we still visited the function node's children, making this |
2812 | // child redundant. |
2813 | return false; |
2814 | case glslang::EOpFunctionCall: |
2815 | { |
2816 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
2817 | if (node->isUserDefined()) |
2818 | result = handleUserFunctionCall(node); |
2819 | if (result) { |
2820 | builder.clearAccessChain(); |
2821 | builder.setAccessChainRValue(result); |
2822 | } else |
2823 | logger->missingFunctionality(f: "missing user function; linker needs to catch that" ); |
2824 | |
2825 | return false; |
2826 | } |
2827 | case glslang::EOpConstructMat2x2: |
2828 | case glslang::EOpConstructMat2x3: |
2829 | case glslang::EOpConstructMat2x4: |
2830 | case glslang::EOpConstructMat3x2: |
2831 | case glslang::EOpConstructMat3x3: |
2832 | case glslang::EOpConstructMat3x4: |
2833 | case glslang::EOpConstructMat4x2: |
2834 | case glslang::EOpConstructMat4x3: |
2835 | case glslang::EOpConstructMat4x4: |
2836 | case glslang::EOpConstructDMat2x2: |
2837 | case glslang::EOpConstructDMat2x3: |
2838 | case glslang::EOpConstructDMat2x4: |
2839 | case glslang::EOpConstructDMat3x2: |
2840 | case glslang::EOpConstructDMat3x3: |
2841 | case glslang::EOpConstructDMat3x4: |
2842 | case glslang::EOpConstructDMat4x2: |
2843 | case glslang::EOpConstructDMat4x3: |
2844 | case glslang::EOpConstructDMat4x4: |
2845 | case glslang::EOpConstructIMat2x2: |
2846 | case glslang::EOpConstructIMat2x3: |
2847 | case glslang::EOpConstructIMat2x4: |
2848 | case glslang::EOpConstructIMat3x2: |
2849 | case glslang::EOpConstructIMat3x3: |
2850 | case glslang::EOpConstructIMat3x4: |
2851 | case glslang::EOpConstructIMat4x2: |
2852 | case glslang::EOpConstructIMat4x3: |
2853 | case glslang::EOpConstructIMat4x4: |
2854 | case glslang::EOpConstructUMat2x2: |
2855 | case glslang::EOpConstructUMat2x3: |
2856 | case glslang::EOpConstructUMat2x4: |
2857 | case glslang::EOpConstructUMat3x2: |
2858 | case glslang::EOpConstructUMat3x3: |
2859 | case glslang::EOpConstructUMat3x4: |
2860 | case glslang::EOpConstructUMat4x2: |
2861 | case glslang::EOpConstructUMat4x3: |
2862 | case glslang::EOpConstructUMat4x4: |
2863 | case glslang::EOpConstructBMat2x2: |
2864 | case glslang::EOpConstructBMat2x3: |
2865 | case glslang::EOpConstructBMat2x4: |
2866 | case glslang::EOpConstructBMat3x2: |
2867 | case glslang::EOpConstructBMat3x3: |
2868 | case glslang::EOpConstructBMat3x4: |
2869 | case glslang::EOpConstructBMat4x2: |
2870 | case glslang::EOpConstructBMat4x3: |
2871 | case glslang::EOpConstructBMat4x4: |
2872 | case glslang::EOpConstructF16Mat2x2: |
2873 | case glslang::EOpConstructF16Mat2x3: |
2874 | case glslang::EOpConstructF16Mat2x4: |
2875 | case glslang::EOpConstructF16Mat3x2: |
2876 | case glslang::EOpConstructF16Mat3x3: |
2877 | case glslang::EOpConstructF16Mat3x4: |
2878 | case glslang::EOpConstructF16Mat4x2: |
2879 | case glslang::EOpConstructF16Mat4x3: |
2880 | case glslang::EOpConstructF16Mat4x4: |
2881 | isMatrix = true; |
2882 | // fall through |
2883 | case glslang::EOpConstructFloat: |
2884 | case glslang::EOpConstructVec2: |
2885 | case glslang::EOpConstructVec3: |
2886 | case glslang::EOpConstructVec4: |
2887 | case glslang::EOpConstructDouble: |
2888 | case glslang::EOpConstructDVec2: |
2889 | case glslang::EOpConstructDVec3: |
2890 | case glslang::EOpConstructDVec4: |
2891 | case glslang::EOpConstructFloat16: |
2892 | case glslang::EOpConstructF16Vec2: |
2893 | case glslang::EOpConstructF16Vec3: |
2894 | case glslang::EOpConstructF16Vec4: |
2895 | case glslang::EOpConstructBool: |
2896 | case glslang::EOpConstructBVec2: |
2897 | case glslang::EOpConstructBVec3: |
2898 | case glslang::EOpConstructBVec4: |
2899 | case glslang::EOpConstructInt8: |
2900 | case glslang::EOpConstructI8Vec2: |
2901 | case glslang::EOpConstructI8Vec3: |
2902 | case glslang::EOpConstructI8Vec4: |
2903 | case glslang::EOpConstructUint8: |
2904 | case glslang::EOpConstructU8Vec2: |
2905 | case glslang::EOpConstructU8Vec3: |
2906 | case glslang::EOpConstructU8Vec4: |
2907 | case glslang::EOpConstructInt16: |
2908 | case glslang::EOpConstructI16Vec2: |
2909 | case glslang::EOpConstructI16Vec3: |
2910 | case glslang::EOpConstructI16Vec4: |
2911 | case glslang::EOpConstructUint16: |
2912 | case glslang::EOpConstructU16Vec2: |
2913 | case glslang::EOpConstructU16Vec3: |
2914 | case glslang::EOpConstructU16Vec4: |
2915 | case glslang::EOpConstructInt: |
2916 | case glslang::EOpConstructIVec2: |
2917 | case glslang::EOpConstructIVec3: |
2918 | case glslang::EOpConstructIVec4: |
2919 | case glslang::EOpConstructUint: |
2920 | case glslang::EOpConstructUVec2: |
2921 | case glslang::EOpConstructUVec3: |
2922 | case glslang::EOpConstructUVec4: |
2923 | case glslang::EOpConstructInt64: |
2924 | case glslang::EOpConstructI64Vec2: |
2925 | case glslang::EOpConstructI64Vec3: |
2926 | case glslang::EOpConstructI64Vec4: |
2927 | case glslang::EOpConstructUint64: |
2928 | case glslang::EOpConstructU64Vec2: |
2929 | case glslang::EOpConstructU64Vec3: |
2930 | case glslang::EOpConstructU64Vec4: |
2931 | case glslang::EOpConstructStruct: |
2932 | case glslang::EOpConstructTextureSampler: |
2933 | case glslang::EOpConstructReference: |
2934 | case glslang::EOpConstructCooperativeMatrix: |
2935 | { |
2936 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
2937 | std::vector<spv::Id> arguments; |
2938 | translateArguments(node: *node, arguments, lvalueCoherentFlags); |
2939 | spv::Id constructed; |
2940 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
2941 | constructed = builder.createOp(spv::OpSampledImage, typeId: resultType(), operands: arguments); |
2942 | else if (node->getOp() == glslang::EOpConstructStruct || |
2943 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
2944 | node->getType().isArray()) { |
2945 | std::vector<spv::Id> constituents; |
2946 | for (int c = 0; c < (int)arguments.size(); ++c) |
2947 | constituents.push_back(x: arguments[c]); |
2948 | constructed = createCompositeConstruct(resultTypeId: resultType(), constituents); |
2949 | } else if (isMatrix) |
2950 | constructed = builder.createMatrixConstructor(precision, sources: arguments, constructee: resultType()); |
2951 | else |
2952 | constructed = builder.createConstructor(precision, sources: arguments, resultTypeId: resultType()); |
2953 | |
2954 | if (node->getType().getQualifier().isNonUniform()) { |
2955 | builder.addDecoration(constructed, spv::DecorationNonUniformEXT); |
2956 | } |
2957 | |
2958 | builder.clearAccessChain(); |
2959 | builder.setAccessChainRValue(constructed); |
2960 | |
2961 | return false; |
2962 | } |
2963 | |
2964 | // These six are component-wise compares with component-wise results. |
2965 | // Forward on to createBinaryOperation(), requesting a vector result. |
2966 | case glslang::EOpLessThan: |
2967 | case glslang::EOpGreaterThan: |
2968 | case glslang::EOpLessThanEqual: |
2969 | case glslang::EOpGreaterThanEqual: |
2970 | case glslang::EOpVectorEqual: |
2971 | case glslang::EOpVectorNotEqual: |
2972 | { |
2973 | // Map the operation to a binary |
2974 | binOp = node->getOp(); |
2975 | reduceComparison = false; |
2976 | switch (node->getOp()) { |
2977 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
2978 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
2979 | default: binOp = node->getOp(); break; |
2980 | } |
2981 | |
2982 | break; |
2983 | } |
2984 | case glslang::EOpMul: |
2985 | // component-wise matrix multiply |
2986 | binOp = glslang::EOpMul; |
2987 | break; |
2988 | case glslang::EOpOuterProduct: |
2989 | // two vectors multiplied to make a matrix |
2990 | binOp = glslang::EOpOuterProduct; |
2991 | break; |
2992 | case glslang::EOpDot: |
2993 | { |
2994 | // for scalar dot product, use multiply |
2995 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
2996 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
2997 | binOp = glslang::EOpMul; |
2998 | break; |
2999 | } |
3000 | case glslang::EOpMod: |
3001 | // when an aggregate, this is the floating-point mod built-in function, |
3002 | // which can be emitted by the one in createBinaryOperation() |
3003 | binOp = glslang::EOpMod; |
3004 | break; |
3005 | |
3006 | case glslang::EOpEmitVertex: |
3007 | case glslang::EOpEndPrimitive: |
3008 | case glslang::EOpBarrier: |
3009 | case glslang::EOpMemoryBarrier: |
3010 | case glslang::EOpMemoryBarrierAtomicCounter: |
3011 | case glslang::EOpMemoryBarrierBuffer: |
3012 | case glslang::EOpMemoryBarrierImage: |
3013 | case glslang::EOpMemoryBarrierShared: |
3014 | case glslang::EOpGroupMemoryBarrier: |
3015 | case glslang::EOpDeviceMemoryBarrier: |
3016 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
3017 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
3018 | case glslang::EOpWorkgroupMemoryBarrier: |
3019 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
3020 | case glslang::EOpSubgroupBarrier: |
3021 | case glslang::EOpSubgroupMemoryBarrier: |
3022 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
3023 | case glslang::EOpSubgroupMemoryBarrierImage: |
3024 | case glslang::EOpSubgroupMemoryBarrierShared: |
3025 | noReturnValue = true; |
3026 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
3027 | break; |
3028 | |
3029 | case glslang::EOpAtomicAdd: |
3030 | case glslang::EOpAtomicSubtract: |
3031 | case glslang::EOpAtomicMin: |
3032 | case glslang::EOpAtomicMax: |
3033 | case glslang::EOpAtomicAnd: |
3034 | case glslang::EOpAtomicOr: |
3035 | case glslang::EOpAtomicXor: |
3036 | case glslang::EOpAtomicExchange: |
3037 | case glslang::EOpAtomicCompSwap: |
3038 | atomic = true; |
3039 | break; |
3040 | |
3041 | #ifndef GLSLANG_WEB |
3042 | case glslang::EOpAtomicStore: |
3043 | noReturnValue = true; |
3044 | // fallthrough |
3045 | case glslang::EOpAtomicLoad: |
3046 | atomic = true; |
3047 | break; |
3048 | |
3049 | case glslang::EOpAtomicCounterAdd: |
3050 | case glslang::EOpAtomicCounterSubtract: |
3051 | case glslang::EOpAtomicCounterMin: |
3052 | case glslang::EOpAtomicCounterMax: |
3053 | case glslang::EOpAtomicCounterAnd: |
3054 | case glslang::EOpAtomicCounterOr: |
3055 | case glslang::EOpAtomicCounterXor: |
3056 | case glslang::EOpAtomicCounterExchange: |
3057 | case glslang::EOpAtomicCounterCompSwap: |
3058 | builder.addExtension(ext: "SPV_KHR_shader_atomic_counter_ops" ); |
3059 | builder.addCapability(cap: spv::CapabilityAtomicStorageOps); |
3060 | atomic = true; |
3061 | break; |
3062 | |
3063 | case glslang::EOpAbsDifference: |
3064 | case glslang::EOpAddSaturate: |
3065 | case glslang::EOpSubSaturate: |
3066 | case glslang::EOpAverage: |
3067 | case glslang::EOpAverageRounded: |
3068 | case glslang::EOpMul32x16: |
3069 | builder.addCapability(cap: spv::CapabilityIntegerFunctions2INTEL); |
3070 | builder.addExtension(ext: "SPV_INTEL_shader_integer_functions2" ); |
3071 | binOp = node->getOp(); |
3072 | break; |
3073 | |
3074 | case glslang::EOpIgnoreIntersectionNV: |
3075 | case glslang::EOpTerminateRayNV: |
3076 | case glslang::EOpTraceNV: |
3077 | case glslang::EOpTraceRayMotionNV: |
3078 | case glslang::EOpTraceKHR: |
3079 | case glslang::EOpExecuteCallableNV: |
3080 | case glslang::EOpExecuteCallableKHR: |
3081 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
3082 | noReturnValue = true; |
3083 | break; |
3084 | case glslang::EOpRayQueryInitialize: |
3085 | case glslang::EOpRayQueryTerminate: |
3086 | case glslang::EOpRayQueryGenerateIntersection: |
3087 | case glslang::EOpRayQueryConfirmIntersection: |
3088 | builder.addExtension(ext: "SPV_KHR_ray_query" ); |
3089 | builder.addCapability(cap: spv::CapabilityRayQueryKHR); |
3090 | noReturnValue = true; |
3091 | break; |
3092 | case glslang::EOpRayQueryProceed: |
3093 | case glslang::EOpRayQueryGetIntersectionType: |
3094 | case glslang::EOpRayQueryGetRayTMin: |
3095 | case glslang::EOpRayQueryGetRayFlags: |
3096 | case glslang::EOpRayQueryGetIntersectionT: |
3097 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
3098 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
3099 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
3100 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
3101 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
3102 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
3103 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
3104 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
3105 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
3106 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
3107 | case glslang::EOpRayQueryGetWorldRayDirection: |
3108 | case glslang::EOpRayQueryGetWorldRayOrigin: |
3109 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
3110 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
3111 | builder.addExtension(ext: "SPV_KHR_ray_query" ); |
3112 | builder.addCapability(cap: spv::CapabilityRayQueryKHR); |
3113 | break; |
3114 | case glslang::EOpCooperativeMatrixLoad: |
3115 | case glslang::EOpCooperativeMatrixStore: |
3116 | noReturnValue = true; |
3117 | break; |
3118 | case glslang::EOpBeginInvocationInterlock: |
3119 | case glslang::EOpEndInvocationInterlock: |
3120 | builder.addExtension(ext: spv::E_SPV_EXT_fragment_shader_interlock); |
3121 | noReturnValue = true; |
3122 | break; |
3123 | #endif |
3124 | |
3125 | case glslang::EOpDebugPrintf: |
3126 | noReturnValue = true; |
3127 | break; |
3128 | |
3129 | default: |
3130 | break; |
3131 | } |
3132 | |
3133 | // |
3134 | // See if it maps to a regular operation. |
3135 | // |
3136 | if (binOp != glslang::EOpNull) { |
3137 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
3138 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
3139 | assert(left && right); |
3140 | |
3141 | builder.clearAccessChain(); |
3142 | left->traverse(this); |
3143 | spv::Id leftId = accessChainLoad(type: left->getType()); |
3144 | |
3145 | builder.clearAccessChain(); |
3146 | right->traverse(this); |
3147 | spv::Id rightId = accessChainLoad(type: right->getType()); |
3148 | |
3149 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3150 | OpDecorations decorations = { precision, |
3151 | TranslateNoContractionDecoration(qualifier: node->getType().getQualifier()), |
3152 | TranslateNonUniformDecoration(qualifier: node->getType().getQualifier()) }; |
3153 | result = createBinaryOperation(op: binOp, decorations, |
3154 | typeId: resultType(), left: leftId, right: rightId, |
3155 | typeProxy: left->getType().getBasicType(), reduceComparison); |
3156 | |
3157 | // code above should only make binOp that exists in createBinaryOperation |
3158 | assert(result != spv::NoResult); |
3159 | builder.clearAccessChain(); |
3160 | builder.setAccessChainRValue(result); |
3161 | |
3162 | return false; |
3163 | } |
3164 | |
3165 | // |
3166 | // Create the list of operands. |
3167 | // |
3168 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
3169 | std::vector<spv::Id> operands; |
3170 | std::vector<spv::IdImmediate> memoryAccessOperands; |
3171 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
3172 | // special case l-value operands; there are just a few |
3173 | bool lvalue = false; |
3174 | switch (node->getOp()) { |
3175 | case glslang::EOpModf: |
3176 | if (arg == 1) |
3177 | lvalue = true; |
3178 | break; |
3179 | |
3180 | case glslang::EOpRayQueryInitialize: |
3181 | case glslang::EOpRayQueryTerminate: |
3182 | case glslang::EOpRayQueryConfirmIntersection: |
3183 | case glslang::EOpRayQueryProceed: |
3184 | case glslang::EOpRayQueryGenerateIntersection: |
3185 | case glslang::EOpRayQueryGetIntersectionType: |
3186 | case glslang::EOpRayQueryGetIntersectionT: |
3187 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
3188 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
3189 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
3190 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
3191 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
3192 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
3193 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
3194 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
3195 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
3196 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
3197 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
3198 | if (arg == 0) |
3199 | lvalue = true; |
3200 | break; |
3201 | |
3202 | case glslang::EOpAtomicAdd: |
3203 | case glslang::EOpAtomicSubtract: |
3204 | case glslang::EOpAtomicMin: |
3205 | case glslang::EOpAtomicMax: |
3206 | case glslang::EOpAtomicAnd: |
3207 | case glslang::EOpAtomicOr: |
3208 | case glslang::EOpAtomicXor: |
3209 | case glslang::EOpAtomicExchange: |
3210 | case glslang::EOpAtomicCompSwap: |
3211 | if (arg == 0) |
3212 | lvalue = true; |
3213 | break; |
3214 | |
3215 | #ifndef GLSLANG_WEB |
3216 | case glslang::EOpFrexp: |
3217 | if (arg == 1) |
3218 | lvalue = true; |
3219 | break; |
3220 | case glslang::EOpInterpolateAtSample: |
3221 | case glslang::EOpInterpolateAtOffset: |
3222 | case glslang::EOpInterpolateAtVertex: |
3223 | if (arg == 0) { |
3224 | // If GLSL, use the address of the interpolant argument. |
3225 | // If HLSL, use an internal version of OpInterolates that takes |
3226 | // the rvalue of the interpolant. A fixup pass in spirv-opt |
3227 | // legalization will remove the OpLoad and convert to an lvalue. |
3228 | // Had to do this because legalization will only propagate a |
3229 | // builtin into an rvalue. |
3230 | lvalue = glslangIntermediate->getSource() != glslang::EShSourceHlsl; |
3231 | |
3232 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
3233 | // operate first on the swizzle base, then apply the swizzle. |
3234 | // That is, we transform |
3235 | // |
3236 | // interpolate(v.zy) -> interpolate(v).zy |
3237 | // |
3238 | if (glslangOperands[0]->getAsOperator() && |
3239 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
3240 | invertedType = convertGlslangToSpvType( |
3241 | type: glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
3242 | } |
3243 | break; |
3244 | case glslang::EOpAtomicLoad: |
3245 | case glslang::EOpAtomicStore: |
3246 | case glslang::EOpAtomicCounterAdd: |
3247 | case glslang::EOpAtomicCounterSubtract: |
3248 | case glslang::EOpAtomicCounterMin: |
3249 | case glslang::EOpAtomicCounterMax: |
3250 | case glslang::EOpAtomicCounterAnd: |
3251 | case glslang::EOpAtomicCounterOr: |
3252 | case glslang::EOpAtomicCounterXor: |
3253 | case glslang::EOpAtomicCounterExchange: |
3254 | case glslang::EOpAtomicCounterCompSwap: |
3255 | if (arg == 0) |
3256 | lvalue = true; |
3257 | break; |
3258 | case glslang::EOpAddCarry: |
3259 | case glslang::EOpSubBorrow: |
3260 | if (arg == 2) |
3261 | lvalue = true; |
3262 | break; |
3263 | case glslang::EOpUMulExtended: |
3264 | case glslang::EOpIMulExtended: |
3265 | if (arg >= 2) |
3266 | lvalue = true; |
3267 | break; |
3268 | case glslang::EOpCooperativeMatrixLoad: |
3269 | if (arg == 0 || arg == 1) |
3270 | lvalue = true; |
3271 | break; |
3272 | case glslang::EOpCooperativeMatrixStore: |
3273 | if (arg == 1) |
3274 | lvalue = true; |
3275 | break; |
3276 | case glslang::EOpSpirvInst: |
3277 | if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvByReference()) |
3278 | lvalue = true; |
3279 | break; |
3280 | #endif |
3281 | default: |
3282 | break; |
3283 | } |
3284 | builder.clearAccessChain(); |
3285 | if (invertedType != spv::NoType && arg == 0) |
3286 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
3287 | else |
3288 | glslangOperands[arg]->traverse(this); |
3289 | |
3290 | #ifndef GLSLANG_WEB |
3291 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
3292 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
3293 | |
3294 | if (arg == 1) { |
3295 | // fold "element" parameter into the access chain |
3296 | spv::Builder::AccessChain save = builder.getAccessChain(); |
3297 | builder.clearAccessChain(); |
3298 | glslangOperands[2]->traverse(this); |
3299 | |
3300 | spv::Id elementId = accessChainLoad(type: glslangOperands[2]->getAsTyped()->getType()); |
3301 | |
3302 | builder.setAccessChain(save); |
3303 | |
3304 | // Point to the first element of the array. |
3305 | builder.accessChainPush(offset: elementId, |
3306 | coherentFlags: TranslateCoherent(type: glslangOperands[arg]->getAsTyped()->getType()), |
3307 | alignment: glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
3308 | |
3309 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
3310 | unsigned int alignment = builder.getAccessChain().alignment; |
3311 | |
3312 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
3313 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
3314 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
3315 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
3316 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
3317 | if (builder.getStorageClass(resultId: builder.getAccessChain().base) == |
3318 | spv::StorageClassPhysicalStorageBufferEXT) { |
3319 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
3320 | } |
3321 | |
3322 | memoryAccessOperands.push_back(x: spv::IdImmediate(false, memoryAccess)); |
3323 | |
3324 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
3325 | memoryAccessOperands.push_back(x: spv::IdImmediate(false, alignment)); |
3326 | } |
3327 | |
3328 | if (memoryAccess & |
3329 | (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
3330 | memoryAccessOperands.push_back(x: spv::IdImmediate(true, |
3331 | builder.makeUintConstant(u: TranslateMemoryScope(coherentFlags)))); |
3332 | } |
3333 | } else if (arg == 2) { |
3334 | continue; |
3335 | } |
3336 | } |
3337 | #endif |
3338 | |
3339 | // for l-values, pass the address, for r-values, pass the value |
3340 | if (lvalue) { |
3341 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
3342 | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
3343 | // reduce to a simple access chain. So, we need a temporary vector to |
3344 | // receive the result, and must later swizzle that into the original |
3345 | // l-value. |
3346 | complexLvalues.push_back(x: builder.getAccessChain()); |
3347 | temporaryLvalues.push_back(x: builder.createVariable( |
3348 | precision: spv::NoPrecision, spv::StorageClassFunction, |
3349 | type: builder.accessChainGetInferredType(), name: "swizzleTemp" )); |
3350 | operands.push_back(x: temporaryLvalues.back()); |
3351 | } else { |
3352 | operands.push_back(x: builder.accessChainGetLValue()); |
3353 | } |
3354 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
3355 | lvalueCoherentFlags |= TranslateCoherent(type: glslangOperands[arg]->getAsTyped()->getType()); |
3356 | } else { |
3357 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3358 | glslang::TOperator glslangOp = node->getOp(); |
3359 | if (arg == 1 && |
3360 | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
3361 | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
3362 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
3363 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
3364 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
3365 | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
3366 | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
3367 | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
3368 | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
3369 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
3370 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
3371 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
3372 | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject |
3373 | )) { |
3374 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
3375 | operands.push_back(x: builder.makeIntConstant(i: cond ? 1 : 0)); |
3376 | } else if ((arg == 10 && glslangOp == glslang::EOpTraceKHR) || |
3377 | (arg == 11 && glslangOp == glslang::EOpTraceRayMotionNV) || |
3378 | (arg == 1 && glslangOp == glslang::EOpExecuteCallableKHR)) { |
3379 | const int opdNum = glslangOp == glslang::EOpTraceKHR ? 10 : (glslangOp == glslang::EOpTraceRayMotionNV ? 11 : 1); |
3380 | const int set = glslangOp == glslang::EOpExecuteCallableKHR ? 1 : 0; |
3381 | |
3382 | const int location = glslangOperands[opdNum]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
3383 | auto itNode = locationToSymbol[set].find(x: location); |
3384 | visitSymbol(symbol: itNode->second); |
3385 | spv::Id symId = getSymbolId(node: itNode->second); |
3386 | operands.push_back(x: symId); |
3387 | #ifndef GLSLANG_WEB |
3388 | } else if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
3389 | // Will be translated to a literal value, make a placeholder here |
3390 | operands.push_back(x: spv::NoResult); |
3391 | #endif |
3392 | } else { |
3393 | operands.push_back(x: accessChainLoad(type: glslangOperands[arg]->getAsTyped()->getType())); |
3394 | } |
3395 | } |
3396 | } |
3397 | |
3398 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3399 | #ifndef GLSLANG_WEB |
3400 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
3401 | std::vector<spv::IdImmediate> idImmOps; |
3402 | |
3403 | idImmOps.push_back(x: spv::IdImmediate(true, operands[1])); // buf |
3404 | idImmOps.push_back(x: spv::IdImmediate(true, operands[2])); // stride |
3405 | idImmOps.push_back(x: spv::IdImmediate(true, operands[3])); // colMajor |
3406 | idImmOps.insert(position: idImmOps.end(), first: memoryAccessOperands.begin(), last: memoryAccessOperands.end()); |
3407 | // get the pointee type |
3408 | spv::Id typeId = builder.getContainedTypeId(typeId: builder.getTypeId(resultId: operands[0])); |
3409 | assert(builder.isCooperativeMatrixType(typeId)); |
3410 | // do the op |
3411 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, operands: idImmOps); |
3412 | // store the result to the pointer (out param 'm') |
3413 | builder.createStore(rValue: result, lValue: operands[0]); |
3414 | result = 0; |
3415 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
3416 | std::vector<spv::IdImmediate> idImmOps; |
3417 | |
3418 | idImmOps.push_back(x: spv::IdImmediate(true, operands[1])); // buf |
3419 | idImmOps.push_back(x: spv::IdImmediate(true, operands[0])); // object |
3420 | idImmOps.push_back(x: spv::IdImmediate(true, operands[2])); // stride |
3421 | idImmOps.push_back(x: spv::IdImmediate(true, operands[3])); // colMajor |
3422 | idImmOps.insert(position: idImmOps.end(), first: memoryAccessOperands.begin(), last: memoryAccessOperands.end()); |
3423 | |
3424 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, operands: idImmOps); |
3425 | result = 0; |
3426 | } else |
3427 | #endif |
3428 | if (atomic) { |
3429 | // Handle all atomics |
3430 | glslang::TBasicType typeProxy = (node->getOp() == glslang::EOpAtomicStore) |
3431 | ? node->getSequence()[0]->getAsTyped()->getBasicType() : node->getBasicType(); |
3432 | result = createAtomicOperation(op: node->getOp(), precision, typeId: resultType(), operands, typeProxy, |
3433 | lvalueCoherentFlags); |
3434 | #ifndef GLSLANG_WEB |
3435 | } else if (node->getOp() == glslang::EOpSpirvInst) { |
3436 | const auto& spirvInst = node->getSpirvInstruction(); |
3437 | if (spirvInst.set == "" ) { |
3438 | std::vector<spv::IdImmediate> idImmOps; |
3439 | for (unsigned int i = 0; i < glslangOperands.size(); ++i) { |
3440 | if (glslangOperands[i]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
3441 | // Translate the constant to a literal value |
3442 | std::vector<unsigned> literals; |
3443 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
3444 | constants.push_back(x: glslangOperands[i]->getAsConstantUnion()); |
3445 | TranslateLiterals(constants, literals); |
3446 | idImmOps.push_back(x: {false, literals[0]}); |
3447 | } else |
3448 | idImmOps.push_back(x: {true, operands[i]}); |
3449 | } |
3450 | |
3451 | if (node->getBasicType() == glslang::EbtVoid) |
3452 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), operands: idImmOps); |
3453 | else |
3454 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), typeId: resultType(), operands: idImmOps); |
3455 | } else { |
3456 | result = builder.createBuiltinCall( |
3457 | resultType: resultType(), builtins: spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(name: spirvInst.set.c_str()), |
3458 | entryPoint: spirvInst.id, args: operands); |
3459 | } |
3460 | noReturnValue = node->getBasicType() == glslang::EbtVoid; |
3461 | #endif |
3462 | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
3463 | if (!nonSemanticDebugPrintf) { |
3464 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf" ); |
3465 | } |
3466 | result = builder.createBuiltinCall(resultType: builder.makeVoidType(), builtins: nonSemanticDebugPrintf, entryPoint: spv::NonSemanticDebugPrintfDebugPrintf, args: operands); |
3467 | builder.addExtension(ext: spv::E_SPV_KHR_non_semantic_info); |
3468 | } else { |
3469 | // Pass through to generic operations. |
3470 | switch (glslangOperands.size()) { |
3471 | case 0: |
3472 | result = createNoArgOperation(op: node->getOp(), precision, typeId: resultType()); |
3473 | break; |
3474 | case 1: |
3475 | { |
3476 | OpDecorations decorations = { precision, |
3477 | TranslateNoContractionDecoration(qualifier: node->getType().getQualifier()), |
3478 | TranslateNonUniformDecoration(qualifier: node->getType().getQualifier()) }; |
3479 | result = createUnaryOperation( |
3480 | op: node->getOp(), decorations, |
3481 | typeId: resultType(), operand: operands.front(), |
3482 | typeProxy: glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
3483 | } |
3484 | break; |
3485 | default: |
3486 | result = createMiscOperation(op: node->getOp(), precision, typeId: resultType(), operands, typeProxy: node->getBasicType()); |
3487 | break; |
3488 | } |
3489 | |
3490 | if (invertedType != spv::NoResult) |
3491 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), parentResult: result); |
3492 | |
3493 | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
3494 | builder.setAccessChain(complexLvalues[i]); |
3495 | builder.accessChainStore(rvalue: builder.createLoad(lValue: temporaryLvalues[i], precision: spv::NoPrecision), |
3496 | nonUniform: TranslateNonUniformDecoration(coherentFlags: complexLvalues[i].coherentFlags)); |
3497 | } |
3498 | } |
3499 | |
3500 | if (noReturnValue) |
3501 | return false; |
3502 | |
3503 | if (! result) { |
3504 | logger->missingFunctionality(f: "unknown glslang aggregate" ); |
3505 | return true; // pick up a child as a placeholder operand |
3506 | } else { |
3507 | builder.clearAccessChain(); |
3508 | builder.setAccessChainRValue(result); |
3509 | return false; |
3510 | } |
3511 | } |
3512 | |
3513 | // This path handles both if-then-else and ?: |
3514 | // The if-then-else has a node type of void, while |
3515 | // ?: has either a void or a non-void node type |
3516 | // |
3517 | // Leaving the result, when not void: |
3518 | // GLSL only has r-values as the result of a :?, but |
3519 | // if we have an l-value, that can be more efficient if it will |
3520 | // become the base of a complex r-value expression, because the |
3521 | // next layer copies r-values into memory to use the access-chain mechanism |
3522 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
3523 | { |
3524 | // see if OpSelect can handle it |
3525 | const auto isOpSelectable = [&]() { |
3526 | if (node->getBasicType() == glslang::EbtVoid) |
3527 | return false; |
3528 | // OpSelect can do all other types starting with SPV 1.4 |
3529 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
3530 | // pre-1.4, only scalars and vectors can be handled |
3531 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
3532 | return false; |
3533 | } |
3534 | return true; |
3535 | }; |
3536 | |
3537 | // See if it simple and safe, or required, to execute both sides. |
3538 | // Crucially, side effects must be either semantically required or avoided, |
3539 | // and there are performance trade-offs. |
3540 | // Return true if required or a good idea (and safe) to execute both sides, |
3541 | // false otherwise. |
3542 | const auto bothSidesPolicy = [&]() -> bool { |
3543 | // do we have both sides? |
3544 | if (node->getTrueBlock() == nullptr || |
3545 | node->getFalseBlock() == nullptr) |
3546 | return false; |
3547 | |
3548 | // required? (unless we write additional code to look for side effects |
3549 | // and make performance trade-offs if none are present) |
3550 | if (!node->getShortCircuit()) |
3551 | return true; |
3552 | |
3553 | // if not required to execute both, decide based on performance/practicality... |
3554 | |
3555 | if (!isOpSelectable()) |
3556 | return false; |
3557 | |
3558 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
3559 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
3560 | |
3561 | // return true if a single operand to ? : is okay for OpSelect |
3562 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
3563 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
3564 | }; |
3565 | |
3566 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
3567 | operandOkay(node->getFalseBlock()->getAsTyped()); |
3568 | }; |
3569 | |
3570 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
3571 | // emit the condition before doing anything with selection |
3572 | node->getCondition()->traverse(this); |
3573 | spv::Id condition = accessChainLoad(type: node->getCondition()->getType()); |
3574 | |
3575 | // Find a way of executing both sides and selecting the right result. |
3576 | const auto executeBothSides = [&]() -> void { |
3577 | // execute both sides |
3578 | node->getTrueBlock()->traverse(this); |
3579 | spv::Id trueValue = accessChainLoad(type: node->getTrueBlock()->getAsTyped()->getType()); |
3580 | node->getFalseBlock()->traverse(this); |
3581 | spv::Id falseValue = accessChainLoad(type: node->getTrueBlock()->getAsTyped()->getType()); |
3582 | |
3583 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3584 | |
3585 | // done if void |
3586 | if (node->getBasicType() == glslang::EbtVoid) |
3587 | return; |
3588 | |
3589 | // emit code to select between trueValue and falseValue |
3590 | |
3591 | // see if OpSelect can handle it |
3592 | if (isOpSelectable()) { |
3593 | // Emit OpSelect for this selection. |
3594 | |
3595 | // smear condition to vector, if necessary (AST is always scalar) |
3596 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
3597 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(resultId: trueValue)) { |
3598 | condition = builder.smearScalar(precision: spv::NoPrecision, scalarVal: condition, |
3599 | vectorType: builder.makeVectorType(component: builder.makeBoolType(), |
3600 | size: builder.getNumComponents(resultId: trueValue))); |
3601 | } |
3602 | |
3603 | // OpSelect |
3604 | result = builder.createTriOp(spv::OpSelect, |
3605 | typeId: convertGlslangToSpvType(type: node->getType()), operand1: condition, |
3606 | operand2: trueValue, operand3: falseValue); |
3607 | |
3608 | builder.clearAccessChain(); |
3609 | builder.setAccessChainRValue(result); |
3610 | } else { |
3611 | // We need control flow to select the result. |
3612 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
3613 | result = builder.createVariable(precision: TranslatePrecisionDecoration(type: node->getType()), |
3614 | spv::StorageClassFunction, type: convertGlslangToSpvType(type: node->getType())); |
3615 | |
3616 | // Selection control: |
3617 | const spv::SelectionControlMask control = TranslateSelectionControl(selectionNode: *node); |
3618 | |
3619 | // make an "if" based on the value created by the condition |
3620 | spv::Builder::If ifBuilder(condition, control, builder); |
3621 | |
3622 | // emit the "then" statement |
3623 | builder.createStore(rValue: trueValue, lValue: result); |
3624 | ifBuilder.makeBeginElse(); |
3625 | // emit the "else" statement |
3626 | builder.createStore(rValue: falseValue, lValue: result); |
3627 | |
3628 | // finish off the control flow |
3629 | ifBuilder.makeEndIf(); |
3630 | |
3631 | builder.clearAccessChain(); |
3632 | builder.setAccessChainLValue(result); |
3633 | } |
3634 | }; |
3635 | |
3636 | // Execute the one side needed, as per the condition |
3637 | const auto executeOneSide = [&]() { |
3638 | // Always emit control flow. |
3639 | if (node->getBasicType() != glslang::EbtVoid) { |
3640 | result = builder.createVariable(precision: TranslatePrecisionDecoration(type: node->getType()), spv::StorageClassFunction, |
3641 | type: convertGlslangToSpvType(type: node->getType())); |
3642 | } |
3643 | |
3644 | // Selection control: |
3645 | const spv::SelectionControlMask control = TranslateSelectionControl(selectionNode: *node); |
3646 | |
3647 | // make an "if" based on the value created by the condition |
3648 | spv::Builder::If ifBuilder(condition, control, builder); |
3649 | |
3650 | // emit the "then" statement |
3651 | if (node->getTrueBlock() != nullptr) { |
3652 | node->getTrueBlock()->traverse(this); |
3653 | if (result != spv::NoResult) |
3654 | builder.createStore(rValue: accessChainLoad(type: node->getTrueBlock()->getAsTyped()->getType()), lValue: result); |
3655 | } |
3656 | |
3657 | if (node->getFalseBlock() != nullptr) { |
3658 | ifBuilder.makeBeginElse(); |
3659 | // emit the "else" statement |
3660 | node->getFalseBlock()->traverse(this); |
3661 | if (result != spv::NoResult) |
3662 | builder.createStore(rValue: accessChainLoad(type: node->getFalseBlock()->getAsTyped()->getType()), lValue: result); |
3663 | } |
3664 | |
3665 | // finish off the control flow |
3666 | ifBuilder.makeEndIf(); |
3667 | |
3668 | if (result != spv::NoResult) { |
3669 | builder.clearAccessChain(); |
3670 | builder.setAccessChainLValue(result); |
3671 | } |
3672 | }; |
3673 | |
3674 | // Try for OpSelect (or a requirement to execute both sides) |
3675 | if (bothSidesPolicy()) { |
3676 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
3677 | if (node->getType().getQualifier().isSpecConstant()) |
3678 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
3679 | executeBothSides(); |
3680 | } else |
3681 | executeOneSide(); |
3682 | |
3683 | return false; |
3684 | } |
3685 | |
3686 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
3687 | { |
3688 | // emit and get the condition before doing anything with switch |
3689 | node->getCondition()->traverse(this); |
3690 | spv::Id selector = accessChainLoad(type: node->getCondition()->getAsTyped()->getType()); |
3691 | |
3692 | // Selection control: |
3693 | const spv::SelectionControlMask control = TranslateSwitchControl(switchNode: *node); |
3694 | |
3695 | // browse the children to sort out code segments |
3696 | int defaultSegment = -1; |
3697 | std::vector<TIntermNode*> codeSegments; |
3698 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
3699 | std::vector<int> caseValues; |
3700 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
3701 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
3702 | TIntermNode* child = *c; |
3703 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
3704 | defaultSegment = (int)codeSegments.size(); |
3705 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
3706 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
3707 | caseValues.push_back(x: child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
3708 | ->getConstArray()[0].getIConst()); |
3709 | } else |
3710 | codeSegments.push_back(x: child); |
3711 | } |
3712 | |
3713 | // handle the case where the last code segment is missing, due to no code |
3714 | // statements between the last case and the end of the switch statement |
3715 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
3716 | (int)codeSegments.size() == defaultSegment) |
3717 | codeSegments.push_back(x: nullptr); |
3718 | |
3719 | // make the switch statement |
3720 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
3721 | builder.makeSwitch(condition: selector, control, numSegments: (int)codeSegments.size(), caseValues, valueToSegment: valueIndexToSegment, defaultSegment, |
3722 | segmentBB&: segmentBlocks); |
3723 | |
3724 | // emit all the code in the segments |
3725 | breakForLoop.push(x: false); |
3726 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
3727 | builder.nextSwitchSegment(segmentBB&: segmentBlocks, segment: s); |
3728 | if (codeSegments[s]) |
3729 | codeSegments[s]->traverse(this); |
3730 | else |
3731 | builder.addSwitchBreak(); |
3732 | } |
3733 | breakForLoop.pop(); |
3734 | |
3735 | builder.endSwitch(segmentBB&: segmentBlocks); |
3736 | |
3737 | return false; |
3738 | } |
3739 | |
3740 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
3741 | { |
3742 | #ifndef GLSLANG_WEB |
3743 | if (node->getQualifier().isSpirvLiteral()) |
3744 | return; // Translated to a literal value, skip further processing |
3745 | #endif |
3746 | |
3747 | int nextConst = 0; |
3748 | spv::Id constant = createSpvConstantFromConstUnionArray(type: node->getType(), node->getConstArray(), nextConst, specConstant: false); |
3749 | |
3750 | builder.clearAccessChain(); |
3751 | builder.setAccessChainRValue(constant); |
3752 | } |
3753 | |
3754 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
3755 | { |
3756 | auto blocks = builder.makeNewLoop(); |
3757 | builder.createBranch(block: &blocks.head); |
3758 | |
3759 | // Loop control: |
3760 | std::vector<unsigned int> operands; |
3761 | const spv::LoopControlMask control = TranslateLoopControl(loopNode: *node, operands); |
3762 | |
3763 | // Spec requires back edges to target header blocks, and every header block |
3764 | // must dominate its merge block. Make a header block first to ensure these |
3765 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
3766 | // by a block-ending branch. But we don't want to put any other body/test |
3767 | // instructions in it, since the body/test may have arbitrary instructions, |
3768 | // including merges of its own. |
3769 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3770 | builder.setBuildPoint(&blocks.head); |
3771 | builder.createLoopMerge(mergeBlock: &blocks.merge, continueBlock: &blocks.continue_target, control, operands); |
3772 | if (node->testFirst() && node->getTest()) { |
3773 | spv::Block& test = builder.makeNewBlock(); |
3774 | builder.createBranch(block: &test); |
3775 | |
3776 | builder.setBuildPoint(&test); |
3777 | node->getTest()->traverse(this); |
3778 | spv::Id condition = accessChainLoad(type: node->getTest()->getType()); |
3779 | builder.createConditionalBranch(condition, thenBlock: &blocks.body, elseBlock: &blocks.merge); |
3780 | |
3781 | builder.setBuildPoint(&blocks.body); |
3782 | breakForLoop.push(x: true); |
3783 | if (node->getBody()) |
3784 | node->getBody()->traverse(this); |
3785 | builder.createBranch(block: &blocks.continue_target); |
3786 | breakForLoop.pop(); |
3787 | |
3788 | builder.setBuildPoint(&blocks.continue_target); |
3789 | if (node->getTerminal()) |
3790 | node->getTerminal()->traverse(this); |
3791 | builder.createBranch(block: &blocks.head); |
3792 | } else { |
3793 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3794 | builder.createBranch(block: &blocks.body); |
3795 | |
3796 | breakForLoop.push(x: true); |
3797 | builder.setBuildPoint(&blocks.body); |
3798 | if (node->getBody()) |
3799 | node->getBody()->traverse(this); |
3800 | builder.createBranch(block: &blocks.continue_target); |
3801 | breakForLoop.pop(); |
3802 | |
3803 | builder.setBuildPoint(&blocks.continue_target); |
3804 | if (node->getTerminal()) |
3805 | node->getTerminal()->traverse(this); |
3806 | if (node->getTest()) { |
3807 | node->getTest()->traverse(this); |
3808 | spv::Id condition = |
3809 | accessChainLoad(type: node->getTest()->getType()); |
3810 | builder.createConditionalBranch(condition, thenBlock: &blocks.head, elseBlock: &blocks.merge); |
3811 | } else { |
3812 | // TODO: unless there was a break/return/discard instruction |
3813 | // somewhere in the body, this is an infinite loop, so we should |
3814 | // issue a warning. |
3815 | builder.createBranch(block: &blocks.head); |
3816 | } |
3817 | } |
3818 | builder.setBuildPoint(&blocks.merge); |
3819 | builder.closeLoop(); |
3820 | return false; |
3821 | } |
3822 | |
3823 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
3824 | { |
3825 | if (node->getExpression()) |
3826 | node->getExpression()->traverse(this); |
3827 | |
3828 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
3829 | |
3830 | switch (node->getFlowOp()) { |
3831 | case glslang::EOpKill: |
3832 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
3833 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
3834 | builder.addCapability(cap: spv::CapabilityDemoteToHelperInvocation); |
3835 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
3836 | } else { |
3837 | builder.makeStatementTerminator(opcode: spv::OpTerminateInvocation, name: "post-terminate-invocation" ); |
3838 | } |
3839 | } else { |
3840 | builder.makeStatementTerminator(opcode: spv::OpKill, name: "post-discard" ); |
3841 | } |
3842 | break; |
3843 | case glslang::EOpTerminateInvocation: |
3844 | builder.addExtension(ext: spv::E_SPV_KHR_terminate_invocation); |
3845 | builder.makeStatementTerminator(opcode: spv::OpTerminateInvocation, name: "post-terminate-invocation" ); |
3846 | break; |
3847 | case glslang::EOpBreak: |
3848 | if (breakForLoop.top()) |
3849 | builder.createLoopExit(); |
3850 | else |
3851 | builder.addSwitchBreak(); |
3852 | break; |
3853 | case glslang::EOpContinue: |
3854 | builder.createLoopContinue(); |
3855 | break; |
3856 | case glslang::EOpReturn: |
3857 | if (node->getExpression() != nullptr) { |
3858 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
3859 | spv::Id returnId = accessChainLoad(type: glslangReturnType); |
3860 | if (builder.getTypeId(resultId: returnId) != currentFunction->getReturnType() || |
3861 | TranslatePrecisionDecoration(type: glslangReturnType) != currentFunction->getReturnPrecision()) { |
3862 | builder.clearAccessChain(); |
3863 | spv::Id copyId = builder.createVariable(precision: currentFunction->getReturnPrecision(), |
3864 | spv::StorageClassFunction, type: currentFunction->getReturnType()); |
3865 | builder.setAccessChainLValue(copyId); |
3866 | multiTypeStore(glslangReturnType, rValue: returnId); |
3867 | returnId = builder.createLoad(lValue: copyId, precision: currentFunction->getReturnPrecision()); |
3868 | } |
3869 | builder.makeReturn(implicit: false, retVal: returnId); |
3870 | } else |
3871 | builder.makeReturn(implicit: false); |
3872 | |
3873 | builder.clearAccessChain(); |
3874 | break; |
3875 | |
3876 | #ifndef GLSLANG_WEB |
3877 | case glslang::EOpDemote: |
3878 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
3879 | builder.addExtension(ext: spv::E_SPV_EXT_demote_to_helper_invocation); |
3880 | builder.addCapability(cap: spv::CapabilityDemoteToHelperInvocationEXT); |
3881 | break; |
3882 | case glslang::EOpTerminateRayKHR: |
3883 | builder.makeStatementTerminator(opcode: spv::OpTerminateRayKHR, name: "post-terminateRayKHR" ); |
3884 | break; |
3885 | case glslang::EOpIgnoreIntersectionKHR: |
3886 | builder.makeStatementTerminator(opcode: spv::OpIgnoreIntersectionKHR, name: "post-ignoreIntersectionKHR" ); |
3887 | break; |
3888 | #endif |
3889 | |
3890 | default: |
3891 | assert(0); |
3892 | break; |
3893 | } |
3894 | |
3895 | return false; |
3896 | } |
3897 | |
3898 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
3899 | { |
3900 | // First, steer off constants, which are not SPIR-V variables, but |
3901 | // can still have a mapping to a SPIR-V Id. |
3902 | // This includes specialization constants. |
3903 | if (node->getQualifier().isConstant()) { |
3904 | spv::Id result = createSpvConstant(*node); |
3905 | if (result != spv::NoResult) |
3906 | return result; |
3907 | } |
3908 | |
3909 | // Now, handle actual variables |
3910 | spv::StorageClass storageClass = TranslateStorageClass(type: node->getType()); |
3911 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(type: node->getType()) |
3912 | : forcedType; |
3913 | |
3914 | const bool contains16BitType = node->getType().contains16BitFloat() || |
3915 | node->getType().contains16BitInt(); |
3916 | if (contains16BitType) { |
3917 | switch (storageClass) { |
3918 | case spv::StorageClassInput: |
3919 | case spv::StorageClassOutput: |
3920 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_16bit_storage, incorporatedVersion: spv::Spv_1_3); |
3921 | builder.addCapability(cap: spv::CapabilityStorageInputOutput16); |
3922 | break; |
3923 | case spv::StorageClassUniform: |
3924 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_16bit_storage, incorporatedVersion: spv::Spv_1_3); |
3925 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
3926 | builder.addCapability(cap: spv::CapabilityStorageUniformBufferBlock16); |
3927 | else |
3928 | builder.addCapability(cap: spv::CapabilityStorageUniform16); |
3929 | break; |
3930 | #ifndef GLSLANG_WEB |
3931 | case spv::StorageClassPushConstant: |
3932 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_16bit_storage, incorporatedVersion: spv::Spv_1_3); |
3933 | builder.addCapability(cap: spv::CapabilityStoragePushConstant16); |
3934 | break; |
3935 | case spv::StorageClassStorageBuffer: |
3936 | case spv::StorageClassPhysicalStorageBufferEXT: |
3937 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_16bit_storage, incorporatedVersion: spv::Spv_1_3); |
3938 | builder.addCapability(cap: spv::CapabilityStorageUniformBufferBlock16); |
3939 | break; |
3940 | #endif |
3941 | default: |
3942 | if (storageClass == spv::StorageClassWorkgroup && |
3943 | node->getType().getBasicType() == glslang::EbtBlock) { |
3944 | builder.addCapability(cap: spv::CapabilityWorkgroupMemoryExplicitLayout16BitAccessKHR); |
3945 | break; |
3946 | } |
3947 | if (node->getType().contains16BitFloat()) |
3948 | builder.addCapability(cap: spv::CapabilityFloat16); |
3949 | if (node->getType().contains16BitInt()) |
3950 | builder.addCapability(cap: spv::CapabilityInt16); |
3951 | break; |
3952 | } |
3953 | } |
3954 | |
3955 | if (node->getType().contains8BitInt()) { |
3956 | if (storageClass == spv::StorageClassPushConstant) { |
3957 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_8bit_storage, incorporatedVersion: spv::Spv_1_5); |
3958 | builder.addCapability(cap: spv::CapabilityStoragePushConstant8); |
3959 | } else if (storageClass == spv::StorageClassUniform) { |
3960 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_8bit_storage, incorporatedVersion: spv::Spv_1_5); |
3961 | builder.addCapability(cap: spv::CapabilityUniformAndStorageBuffer8BitAccess); |
3962 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
3963 | builder.addIncorporatedExtension(ext: spv::E_SPV_KHR_8bit_storage, incorporatedVersion: spv::Spv_1_5); |
3964 | builder.addCapability(cap: spv::CapabilityStorageBuffer8BitAccess); |
3965 | } else if (storageClass == spv::StorageClassWorkgroup && |
3966 | node->getType().getBasicType() == glslang::EbtBlock) { |
3967 | builder.addCapability(cap: spv::CapabilityWorkgroupMemoryExplicitLayout8BitAccessKHR); |
3968 | } else { |
3969 | builder.addCapability(cap: spv::CapabilityInt8); |
3970 | } |
3971 | } |
3972 | |
3973 | const char* name = node->getName().c_str(); |
3974 | if (glslang::IsAnonymous(name)) |
3975 | name = "" ; |
3976 | |
3977 | spv::Id initializer = spv::NoResult; |
3978 | |
3979 | if (node->getType().getQualifier().storage == glslang::EvqUniform && !node->getConstArray().empty()) { |
3980 | int nextConst = 0; |
3981 | initializer = createSpvConstantFromConstUnionArray(type: node->getType(), |
3982 | node->getConstArray(), |
3983 | nextConst, |
3984 | specConstant: false /* specConst */); |
3985 | } else if (node->getType().getQualifier().isNullInit()) { |
3986 | initializer = builder.makeNullConstant(typeId: spvType); |
3987 | } |
3988 | |
3989 | return builder.createVariable(precision: spv::NoPrecision, storageClass, type: spvType, name, initializer); |
3990 | } |
3991 | |
3992 | // Return type Id of the sampled type. |
3993 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
3994 | { |
3995 | switch (sampler.type) { |
3996 | case glslang::EbtInt: return builder.makeIntType(width: 32); |
3997 | case glslang::EbtUint: return builder.makeUintType(width: 32); |
3998 | case glslang::EbtFloat: return builder.makeFloatType(width: 32); |
3999 | #ifndef GLSLANG_WEB |
4000 | case glslang::EbtFloat16: |
4001 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
4002 | builder.addCapability(cap: spv::CapabilityFloat16ImageAMD); |
4003 | return builder.makeFloatType(width: 16); |
4004 | case glslang::EbtInt64: |
4005 | builder.addExtension(ext: spv::E_SPV_EXT_shader_image_int64); |
4006 | builder.addCapability(cap: spv::CapabilityInt64ImageEXT); |
4007 | return builder.makeIntType(width: 64); |
4008 | case glslang::EbtUint64: |
4009 | builder.addExtension(ext: spv::E_SPV_EXT_shader_image_int64); |
4010 | builder.addCapability(cap: spv::CapabilityInt64ImageEXT); |
4011 | return builder.makeUintType(width: 64); |
4012 | #endif |
4013 | default: |
4014 | assert(0); |
4015 | return builder.makeFloatType(width: 32); |
4016 | } |
4017 | } |
4018 | |
4019 | // If node is a swizzle operation, return the type that should be used if |
4020 | // the swizzle base is first consumed by another operation, before the swizzle |
4021 | // is applied. |
4022 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
4023 | { |
4024 | if (node.getAsOperator() && |
4025 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
4026 | return convertGlslangToSpvType(type: node.getAsBinaryNode()->getLeft()->getType()); |
4027 | else |
4028 | return spv::NoType; |
4029 | } |
4030 | |
4031 | // When inverting a swizzle with a parent op, this function |
4032 | // will apply the swizzle operation to a completed parent operation. |
4033 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
4034 | spv::Id parentResult) |
4035 | { |
4036 | std::vector<unsigned> swizzle; |
4037 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
4038 | return builder.createRvalueSwizzle(precision, typeId: convertGlslangToSpvType(type: node.getType()), source: parentResult, channels: swizzle); |
4039 | } |
4040 | |
4041 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
4042 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
4043 | { |
4044 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
4045 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
4046 | swizzle.push_back(x: swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
4047 | } |
4048 | |
4049 | // Convert from a glslang type to an SPV type, by calling into a |
4050 | // recursive version of this function. This establishes the inherited |
4051 | // layout state rooted from the top-level type. |
4052 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
4053 | { |
4054 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), lastBufferBlockMember: false, forwardReferenceOnly); |
4055 | } |
4056 | |
4057 | // Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id. |
4058 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
4059 | // Mutually recursive with convertGlslangStructToSpvType(). |
4060 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
4061 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
4062 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
4063 | { |
4064 | spv::Id spvType = spv::NoResult; |
4065 | |
4066 | switch (type.getBasicType()) { |
4067 | case glslang::EbtVoid: |
4068 | spvType = builder.makeVoidType(); |
4069 | assert (! type.isArray()); |
4070 | break; |
4071 | case glslang::EbtBool: |
4072 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
4073 | // a 32-bit int where non-0 means true. |
4074 | if (explicitLayout != glslang::ElpNone) |
4075 | spvType = builder.makeUintType(width: 32); |
4076 | else |
4077 | spvType = builder.makeBoolType(); |
4078 | break; |
4079 | case glslang::EbtInt: |
4080 | spvType = builder.makeIntType(width: 32); |
4081 | break; |
4082 | case glslang::EbtUint: |
4083 | spvType = builder.makeUintType(width: 32); |
4084 | break; |
4085 | case glslang::EbtFloat: |
4086 | spvType = builder.makeFloatType(width: 32); |
4087 | break; |
4088 | #ifndef GLSLANG_WEB |
4089 | case glslang::EbtDouble: |
4090 | spvType = builder.makeFloatType(width: 64); |
4091 | break; |
4092 | case glslang::EbtFloat16: |
4093 | spvType = builder.makeFloatType(width: 16); |
4094 | break; |
4095 | case glslang::EbtInt8: |
4096 | spvType = builder.makeIntType(width: 8); |
4097 | break; |
4098 | case glslang::EbtUint8: |
4099 | spvType = builder.makeUintType(width: 8); |
4100 | break; |
4101 | case glslang::EbtInt16: |
4102 | spvType = builder.makeIntType(width: 16); |
4103 | break; |
4104 | case glslang::EbtUint16: |
4105 | spvType = builder.makeUintType(width: 16); |
4106 | break; |
4107 | case glslang::EbtInt64: |
4108 | spvType = builder.makeIntType(width: 64); |
4109 | break; |
4110 | case glslang::EbtUint64: |
4111 | spvType = builder.makeUintType(width: 64); |
4112 | break; |
4113 | case glslang::EbtAtomicUint: |
4114 | builder.addCapability(cap: spv::CapabilityAtomicStorage); |
4115 | spvType = builder.makeUintType(width: 32); |
4116 | break; |
4117 | case glslang::EbtAccStruct: |
4118 | switch (glslangIntermediate->getStage()) { |
4119 | case EShLangRayGen: |
4120 | case EShLangIntersect: |
4121 | case EShLangAnyHit: |
4122 | case EShLangClosestHit: |
4123 | case EShLangMiss: |
4124 | case EShLangCallable: |
4125 | // these all should have the RayTracingNV/KHR capability already |
4126 | break; |
4127 | default: |
4128 | { |
4129 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
4130 | if (extensions.find(x: "GL_EXT_ray_query" ) != extensions.end()) { |
4131 | builder.addExtension(ext: spv::E_SPV_KHR_ray_query); |
4132 | builder.addCapability(cap: spv::CapabilityRayQueryKHR); |
4133 | } |
4134 | } |
4135 | break; |
4136 | } |
4137 | spvType = builder.makeAccelerationStructureType(); |
4138 | break; |
4139 | case glslang::EbtRayQuery: |
4140 | { |
4141 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
4142 | if (extensions.find(x: "GL_EXT_ray_query" ) != extensions.end()) { |
4143 | builder.addExtension(ext: spv::E_SPV_KHR_ray_query); |
4144 | builder.addCapability(cap: spv::CapabilityRayQueryKHR); |
4145 | } |
4146 | spvType = builder.makeRayQueryType(); |
4147 | } |
4148 | break; |
4149 | case glslang::EbtReference: |
4150 | { |
4151 | // Make the forward pointer, then recurse to convert the structure type, then |
4152 | // patch up the forward pointer with a real pointer type. |
4153 | if (forwardPointers.find(x: type.getReferentType()) == forwardPointers.end()) { |
4154 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
4155 | forwardPointers[type.getReferentType()] = forwardId; |
4156 | } |
4157 | spvType = forwardPointers[type.getReferentType()]; |
4158 | if (!forwardReferenceOnly) { |
4159 | spv::Id referentType = convertGlslangToSpvType(type: *type.getReferentType()); |
4160 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
4161 | forwardPointerType: forwardPointers[type.getReferentType()], |
4162 | pointee: referentType); |
4163 | } |
4164 | } |
4165 | break; |
4166 | #endif |
4167 | case glslang::EbtSampler: |
4168 | { |
4169 | const glslang::TSampler& sampler = type.getSampler(); |
4170 | if (sampler.isPureSampler()) { |
4171 | spvType = builder.makeSamplerType(); |
4172 | } else { |
4173 | // an image is present, make its type |
4174 | spvType = builder.makeImageType(sampledType: getSampledType(sampler), TranslateDimensionality(sampler), |
4175 | depth: sampler.isShadow(), arrayed: sampler.isArrayed(), ms: sampler.isMultiSample(), |
4176 | sampled: sampler.isImageClass() ? 2 : 1, format: TranslateImageFormat(type)); |
4177 | if (sampler.isCombined()) { |
4178 | // already has both image and sampler, make the combined type |
4179 | spvType = builder.makeSampledImageType(imageType: spvType); |
4180 | } |
4181 | } |
4182 | } |
4183 | break; |
4184 | case glslang::EbtStruct: |
4185 | case glslang::EbtBlock: |
4186 | { |
4187 | // If we've seen this struct type, return it |
4188 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
4189 | |
4190 | // Try to share structs for different layouts, but not yet for other |
4191 | // kinds of qualification (primarily not yet including interpolant qualification). |
4192 | if (! HasNonLayoutQualifiers(type, qualifier)) |
4193 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
4194 | if (spvType != spv::NoResult) |
4195 | break; |
4196 | |
4197 | // else, we haven't seen it... |
4198 | if (type.getBasicType() == glslang::EbtBlock) |
4199 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(new_size: glslangMembers->size()); |
4200 | spvType = convertGlslangStructToSpvType(type, glslangStruct: glslangMembers, explicitLayout, qualifier); |
4201 | } |
4202 | break; |
4203 | case glslang::EbtString: |
4204 | // no type used for OpString |
4205 | return 0; |
4206 | #ifndef GLSLANG_WEB |
4207 | case glslang::EbtSpirvType: { |
4208 | // GL_EXT_spirv_intrinsics |
4209 | const auto& spirvType = type.getSpirvType(); |
4210 | const auto& spirvInst = spirvType.spirvInst; |
4211 | |
4212 | std::vector<spv::IdImmediate> operands; |
4213 | for (const auto& typeParam : spirvType.typeParams) { |
4214 | // Constant expression |
4215 | if (typeParam.constant->isLiteral()) { |
4216 | if (typeParam.constant->getBasicType() == glslang::EbtFloat) { |
4217 | float floatValue = static_cast<float>(typeParam.constant->getConstArray()[0].getDConst()); |
4218 | unsigned literal = *reinterpret_cast<unsigned*>(&floatValue); |
4219 | operands.push_back(x: {false, literal}); |
4220 | } else if (typeParam.constant->getBasicType() == glslang::EbtInt) { |
4221 | unsigned literal = typeParam.constant->getConstArray()[0].getIConst(); |
4222 | operands.push_back(x: {false, literal}); |
4223 | } else if (typeParam.constant->getBasicType() == glslang::EbtUint) { |
4224 | unsigned literal = typeParam.constant->getConstArray()[0].getUConst(); |
4225 | operands.push_back(x: {false, literal}); |
4226 | } else if (typeParam.constant->getBasicType() == glslang::EbtBool) { |
4227 | unsigned literal = typeParam.constant->getConstArray()[0].getBConst(); |
4228 | operands.push_back(x: {false, literal}); |
4229 | } else if (typeParam.constant->getBasicType() == glslang::EbtString) { |
4230 | auto str = typeParam.constant->getConstArray()[0].getSConst()->c_str(); |
4231 | unsigned literal = 0; |
4232 | char* literalPtr = reinterpret_cast<char*>(&literal); |
4233 | unsigned charCount = 0; |
4234 | char ch = 0; |
4235 | do { |
4236 | ch = *(str++); |
4237 | *(literalPtr++) = ch; |
4238 | ++charCount; |
4239 | if (charCount == 4) { |
4240 | operands.push_back(x: {false, literal}); |
4241 | literalPtr = reinterpret_cast<char*>(&literal); |
4242 | charCount = 0; |
4243 | } |
4244 | } while (ch != 0); |
4245 | |
4246 | // Partial literal is padded with 0 |
4247 | if (charCount > 0) { |
4248 | for (; charCount < 4; ++charCount) |
4249 | *(literalPtr++) = 0; |
4250 | operands.push_back(x: {false, literal}); |
4251 | } |
4252 | } else |
4253 | assert(0); // Unexpected type |
4254 | } else |
4255 | operands.push_back(x: {true, createSpvConstant(*typeParam.constant)}); |
4256 | } |
4257 | |
4258 | assert(spirvInst.set == "" ); // Currently, couldn't be extended instructions. |
4259 | spvType = builder.makeGenericType(opcode: static_cast<spv::Op>(spirvInst.id), operands); |
4260 | |
4261 | break; |
4262 | } |
4263 | #endif |
4264 | default: |
4265 | assert(0); |
4266 | break; |
4267 | } |
4268 | |
4269 | if (type.isMatrix()) |
4270 | spvType = builder.makeMatrixType(component: spvType, cols: type.getMatrixCols(), rows: type.getMatrixRows()); |
4271 | else { |
4272 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
4273 | if (type.getVectorSize() > 1) |
4274 | spvType = builder.makeVectorType(component: spvType, size: type.getVectorSize()); |
4275 | } |
4276 | |
4277 | if (type.isCoopMat()) { |
4278 | builder.addCapability(cap: spv::CapabilityCooperativeMatrixNV); |
4279 | builder.addExtension(ext: spv::E_SPV_NV_cooperative_matrix); |
4280 | if (type.getBasicType() == glslang::EbtFloat16) |
4281 | builder.addCapability(cap: spv::CapabilityFloat16); |
4282 | if (type.getBasicType() == glslang::EbtUint8 || |
4283 | type.getBasicType() == glslang::EbtInt8) { |
4284 | builder.addCapability(cap: spv::CapabilityInt8); |
4285 | } |
4286 | |
4287 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), dim: 1); |
4288 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), dim: 2); |
4289 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), dim: 3); |
4290 | |
4291 | spvType = builder.makeCooperativeMatrixType(component: spvType, scope, rows, cols); |
4292 | } |
4293 | |
4294 | if (type.isArray()) { |
4295 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
4296 | |
4297 | // Do all but the outer dimension |
4298 | if (type.getArraySizes()->getNumDims() > 1) { |
4299 | // We need to decorate array strides for types needing explicit layout, except blocks. |
4300 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
4301 | // Use a dummy glslang type for querying internal strides of |
4302 | // arrays of arrays, but using just a one-dimensional array. |
4303 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
4304 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
4305 | simpleArrayType.getArraySizes()->dereference(); |
4306 | |
4307 | // Will compute the higher-order strides here, rather than making a whole |
4308 | // pile of types and doing repetitive recursion on their contents. |
4309 | stride = getArrayStride(arrayType: simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
4310 | } |
4311 | |
4312 | // make the arrays |
4313 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
4314 | spvType = builder.makeArrayType(element: spvType, sizeId: makeArraySizeId(*type.getArraySizes(), dim), stride); |
4315 | if (stride > 0) |
4316 | builder.addDecoration(spvType, spv::DecorationArrayStride, num: stride); |
4317 | stride *= type.getArraySizes()->getDimSize(dim); |
4318 | } |
4319 | } else { |
4320 | // single-dimensional array, and don't yet have stride |
4321 | |
4322 | // We need to decorate array strides for types needing explicit layout, except blocks. |
4323 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
4324 | stride = getArrayStride(arrayType: type, explicitLayout, qualifier.layoutMatrix); |
4325 | } |
4326 | |
4327 | // Do the outer dimension, which might not be known for a runtime-sized array. |
4328 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
4329 | if (type.isSizedArray()) |
4330 | spvType = builder.makeArrayType(element: spvType, sizeId: makeArraySizeId(*type.getArraySizes(), dim: 0), stride); |
4331 | else { |
4332 | #ifndef GLSLANG_WEB |
4333 | if (!lastBufferBlockMember) { |
4334 | builder.addIncorporatedExtension(ext: "SPV_EXT_descriptor_indexing" , incorporatedVersion: spv::Spv_1_5); |
4335 | builder.addCapability(cap: spv::CapabilityRuntimeDescriptorArrayEXT); |
4336 | } |
4337 | #endif |
4338 | spvType = builder.makeRuntimeArray(element: spvType); |
4339 | } |
4340 | if (stride > 0) |
4341 | builder.addDecoration(spvType, spv::DecorationArrayStride, num: stride); |
4342 | } |
4343 | |
4344 | return spvType; |
4345 | } |
4346 | |
4347 | // TODO: this functionality should exist at a higher level, in creating the AST |
4348 | // |
4349 | // Identify interface members that don't have their required extension turned on. |
4350 | // |
4351 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
4352 | { |
4353 | #ifndef GLSLANG_WEB |
4354 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
4355 | |
4356 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
4357 | extensions.find(x: "GL_NV_stereo_view_rendering" ) == extensions.end()) |
4358 | return true; |
4359 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
4360 | extensions.find(x: "GL_NV_stereo_view_rendering" ) == extensions.end()) |
4361 | return true; |
4362 | |
4363 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
4364 | if (member.getFieldName() == "gl_ViewportMask" && |
4365 | extensions.find(x: "GL_NV_viewport_array2" ) == extensions.end()) |
4366 | return true; |
4367 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
4368 | extensions.find(x: "GL_NVX_multiview_per_view_attributes" ) == extensions.end()) |
4369 | return true; |
4370 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
4371 | extensions.find(x: "GL_NVX_multiview_per_view_attributes" ) == extensions.end()) |
4372 | return true; |
4373 | } |
4374 | #endif |
4375 | |
4376 | return false; |
4377 | }; |
4378 | |
4379 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
4380 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
4381 | // Mutually recursive with convertGlslangToSpvType(). |
4382 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
4383 | const glslang::TTypeList* glslangMembers, |
4384 | glslang::TLayoutPacking explicitLayout, |
4385 | const glslang::TQualifier& qualifier) |
4386 | { |
4387 | // Create a vector of struct types for SPIR-V to consume |
4388 | std::vector<spv::Id> spvMembers; |
4389 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
4390 | // except sometimes for blocks |
4391 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
4392 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
4393 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
4394 | if (glslangMember.hiddenMember()) { |
4395 | ++memberDelta; |
4396 | if (type.getBasicType() == glslang::EbtBlock) |
4397 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
4398 | } else { |
4399 | if (type.getBasicType() == glslang::EbtBlock) { |
4400 | if (filterMember(member: glslangMember)) { |
4401 | memberDelta++; |
4402 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
4403 | continue; |
4404 | } |
4405 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
4406 | } |
4407 | // modify just this child's view of the qualifier |
4408 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
4409 | InheritQualifiers(child&: memberQualifier, parent: qualifier); |
4410 | |
4411 | // manually inherit location |
4412 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
4413 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
4414 | |
4415 | // recurse |
4416 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
4417 | i == (int)glslangMembers->size() - 1; |
4418 | |
4419 | // Make forward pointers for any pointer members, and create a list of members to |
4420 | // convert to spirv types after creating the struct. |
4421 | if (glslangMember.isReference()) { |
4422 | if (forwardPointers.find(x: glslangMember.getReferentType()) == forwardPointers.end()) { |
4423 | deferredForwardPointers.push_back(x: std::make_pair(x: &glslangMember, y&: memberQualifier)); |
4424 | } |
4425 | spvMembers.push_back( |
4426 | x: convertGlslangToSpvType(type: glslangMember, explicitLayout, qualifier: memberQualifier, lastBufferBlockMember, |
4427 | forwardReferenceOnly: true)); |
4428 | } else { |
4429 | spvMembers.push_back( |
4430 | x: convertGlslangToSpvType(type: glslangMember, explicitLayout, qualifier: memberQualifier, lastBufferBlockMember, |
4431 | forwardReferenceOnly: false)); |
4432 | } |
4433 | } |
4434 | } |
4435 | |
4436 | // Make the SPIR-V type |
4437 | spv::Id spvType = builder.makeStructType(members: spvMembers, type.getTypeName().c_str()); |
4438 | if (! HasNonLayoutQualifiers(type, qualifier)) |
4439 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
4440 | |
4441 | // Decorate it |
4442 | decorateStructType(type, glslangStruct: glslangMembers, explicitLayout, qualifier, spvType); |
4443 | |
4444 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
4445 | auto it = deferredForwardPointers[i]; |
4446 | convertGlslangToSpvType(type: *it.first, explicitLayout, qualifier: it.second, lastBufferBlockMember: false); |
4447 | } |
4448 | |
4449 | return spvType; |
4450 | } |
4451 | |
4452 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
4453 | const glslang::TTypeList* glslangMembers, |
4454 | glslang::TLayoutPacking explicitLayout, |
4455 | const glslang::TQualifier& qualifier, |
4456 | spv::Id spvType) |
4457 | { |
4458 | // Name and decorate the non-hidden members |
4459 | int offset = -1; |
4460 | bool memberLocationInvalid = type.isArrayOfArrays() || |
4461 | (type.isArray() && (type.getQualifier().isArrayedIo(language: glslangIntermediate->getStage()) == false)); |
4462 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
4463 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
4464 | int member = i; |
4465 | if (type.getBasicType() == glslang::EbtBlock) { |
4466 | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
4467 | if (filterMember(member: glslangMember)) |
4468 | continue; |
4469 | } |
4470 | |
4471 | // modify just this child's view of the qualifier |
4472 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
4473 | InheritQualifiers(child&: memberQualifier, parent: qualifier); |
4474 | |
4475 | // using -1 above to indicate a hidden member |
4476 | if (member < 0) |
4477 | continue; |
4478 | |
4479 | builder.addMemberName(spvType, member, name: glslangMember.getFieldName().c_str()); |
4480 | builder.addMemberDecoration(spvType, member, |
4481 | TranslateLayoutDecoration(type: glslangMember, matrixLayout: memberQualifier.layoutMatrix)); |
4482 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(type: glslangMember)); |
4483 | // Add interpolation and auxiliary storage decorations only to |
4484 | // top-level members of Input and Output storage classes |
4485 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
4486 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
4487 | if (type.getBasicType() == glslang::EbtBlock || |
4488 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
4489 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(qualifier: memberQualifier)); |
4490 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(qualifier: memberQualifier)); |
4491 | #ifndef GLSLANG_WEB |
4492 | addMeshNVDecoration(id: spvType, member, qualifier: memberQualifier); |
4493 | #endif |
4494 | } |
4495 | } |
4496 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(qualifier: memberQualifier)); |
4497 | |
4498 | #ifndef GLSLANG_WEB |
4499 | if (type.getBasicType() == glslang::EbtBlock && |
4500 | qualifier.storage == glslang::EvqBuffer) { |
4501 | // Add memory decorations only to top-level members of shader storage block |
4502 | std::vector<spv::Decoration> memory; |
4503 | TranslateMemoryDecoration(qualifier: memberQualifier, memory, useVulkanMemoryModel: glslangIntermediate->usingVulkanMemoryModel()); |
4504 | for (unsigned int i = 0; i < memory.size(); ++i) |
4505 | builder.addMemberDecoration(spvType, member, memory[i]); |
4506 | } |
4507 | |
4508 | #endif |
4509 | |
4510 | // Location assignment was already completed correctly by the front end, |
4511 | // just track whether a member needs to be decorated. |
4512 | // Ignore member locations if the container is an array, as that's |
4513 | // ill-specified and decisions have been made to not allow this. |
4514 | if (!memberLocationInvalid && memberQualifier.hasLocation()) |
4515 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, num: memberQualifier.layoutLocation); |
4516 | |
4517 | // component, XFB, others |
4518 | if (glslangMember.getQualifier().hasComponent()) |
4519 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
4520 | num: glslangMember.getQualifier().layoutComponent); |
4521 | if (glslangMember.getQualifier().hasXfbOffset()) |
4522 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
4523 | num: glslangMember.getQualifier().layoutXfbOffset); |
4524 | else if (explicitLayout != glslang::ElpNone) { |
4525 | // figure out what to do with offset, which is accumulating |
4526 | int nextOffset; |
4527 | updateMemberOffset(structType: type, memberType: glslangMember, currentOffset&: offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
4528 | if (offset >= 0) |
4529 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, num: offset); |
4530 | offset = nextOffset; |
4531 | } |
4532 | |
4533 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
4534 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
4535 | num: getMatrixStride(matrixType: glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
4536 | |
4537 | // built-in variable decorations |
4538 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(builtIn: glslangMember.getQualifier().builtIn, memberDeclaration: true); |
4539 | if (builtIn != spv::BuiltInMax) |
4540 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, num: (int)builtIn); |
4541 | |
4542 | #ifndef GLSLANG_WEB |
4543 | // nonuniform |
4544 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(qualifier: glslangMember.getQualifier())); |
4545 | |
4546 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
4547 | builder.addExtension(ext: "SPV_GOOGLE_hlsl_functionality1" ); |
4548 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
4549 | memberQualifier.semanticName); |
4550 | } |
4551 | |
4552 | if (builtIn == spv::BuiltInLayer) { |
4553 | // SPV_NV_viewport_array2 extension |
4554 | if (glslangMember.getQualifier().layoutViewportRelative){ |
4555 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
4556 | builder.addCapability(cap: spv::CapabilityShaderViewportMaskNV); |
4557 | builder.addExtension(ext: spv::E_SPV_NV_viewport_array2); |
4558 | } |
4559 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
4560 | builder.addMemberDecoration(spvType, member, |
4561 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
4562 | num: glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
4563 | builder.addCapability(cap: spv::CapabilityShaderStereoViewNV); |
4564 | builder.addExtension(ext: spv::E_SPV_NV_stereo_view_rendering); |
4565 | } |
4566 | } |
4567 | if (glslangMember.getQualifier().layoutPassthrough) { |
4568 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
4569 | builder.addCapability(cap: spv::CapabilityGeometryShaderPassthroughNV); |
4570 | builder.addExtension(ext: spv::E_SPV_NV_geometry_shader_passthrough); |
4571 | } |
4572 | |
4573 | // |
4574 | // Add SPIR-V decorations for members (GL_EXT_spirv_intrinsics) |
4575 | // |
4576 | if (glslangMember.getQualifier().hasSprivDecorate()) { |
4577 | const glslang::TSpirvDecorate& spirvDecorate = glslangMember.getQualifier().getSpirvDecorate(); |
4578 | |
4579 | // Add spirv_decorate |
4580 | for (auto& decorate : spirvDecorate.decorates) { |
4581 | if (!decorate.second.empty()) { |
4582 | std::vector<unsigned> literals; |
4583 | TranslateLiterals(constants: decorate.second, literals); |
4584 | builder.addMemberDecoration(spvType, member, static_cast<spv::Decoration>(decorate.first), literals); |
4585 | } |
4586 | else |
4587 | builder.addMemberDecoration(spvType, member, static_cast<spv::Decoration>(decorate.first)); |
4588 | } |
4589 | |
4590 | // spirv_decorate_id not applied to members |
4591 | assert(spirvDecorate.decorateIds.empty()); |
4592 | |
4593 | // Add spirv_decorate_string |
4594 | for (auto& decorateString : spirvDecorate.decorateStrings) { |
4595 | std::vector<const char*> strings; |
4596 | assert(!decorateString.second.empty()); |
4597 | for (auto extraOperand : decorateString.second) { |
4598 | const char* string = extraOperand->getConstArray()[0].getSConst()->c_str(); |
4599 | strings.push_back(x: string); |
4600 | } |
4601 | builder.addDecoration(spvType, static_cast<spv::Decoration>(decorateString.first), strings); |
4602 | } |
4603 | } |
4604 | #endif |
4605 | } |
4606 | |
4607 | // Decorate the structure |
4608 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, matrixLayout: qualifier.layoutMatrix)); |
4609 | builder.addDecoration(spvType, TranslateBlockDecoration(type, useStorageBuffer: glslangIntermediate->usingStorageBuffer())); |
4610 | } |
4611 | |
4612 | // Turn the expression forming the array size into an id. |
4613 | // This is not quite trivial, because of specialization constants. |
4614 | // Sometimes, a raw constant is turned into an Id, and sometimes |
4615 | // a specialization constant expression is. |
4616 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
4617 | { |
4618 | // First, see if this is sized with a node, meaning a specialization constant: |
4619 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
4620 | if (specNode != nullptr) { |
4621 | builder.clearAccessChain(); |
4622 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
4623 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
4624 | specNode->traverse(this); |
4625 | return accessChainLoad(type: specNode->getAsTyped()->getType()); |
4626 | } |
4627 | |
4628 | // Otherwise, need a compile-time (front end) size, get it: |
4629 | int size = arraySizes.getDimSize(dim); |
4630 | assert(size > 0); |
4631 | return builder.makeUintConstant(u: size); |
4632 | } |
4633 | |
4634 | // Wrap the builder's accessChainLoad to: |
4635 | // - localize handling of RelaxedPrecision |
4636 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
4637 | // (avoids unnecessary work and possible type punning for structures) |
4638 | // - do conversion of concrete to abstract type |
4639 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
4640 | { |
4641 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
4642 | |
4643 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
4644 | coherentFlags |= TranslateCoherent(type); |
4645 | |
4646 | unsigned int alignment = builder.getAccessChain().alignment; |
4647 | alignment |= type.getBufferReferenceAlignment(); |
4648 | |
4649 | spv::Id loadedId = builder.accessChainLoad(precision: TranslatePrecisionDecoration(type), |
4650 | l_nonUniform: TranslateNonUniformDecoration(coherentFlags: builder.getAccessChain().coherentFlags), |
4651 | r_nonUniform: TranslateNonUniformDecoration(qualifier: type.getQualifier()), |
4652 | ResultType: nominalTypeId, |
4653 | memoryAccess: spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
4654 | scope: TranslateMemoryScope(coherentFlags), |
4655 | alignment); |
4656 | |
4657 | // Need to convert to abstract types when necessary |
4658 | if (type.getBasicType() == glslang::EbtBool) { |
4659 | loadedId = convertLoadedBoolInUniformToUint(type, nominalTypeId, loadedId); |
4660 | } |
4661 | |
4662 | return loadedId; |
4663 | } |
4664 | |
4665 | // Wrap the builder's accessChainStore to: |
4666 | // - do conversion of concrete to abstract type |
4667 | // |
4668 | // Implicitly uses the existing builder.accessChain as the storage target. |
4669 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
4670 | { |
4671 | // Need to convert to abstract types when necessary |
4672 | if (type.getBasicType() == glslang::EbtBool) { |
4673 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
4674 | |
4675 | if (builder.isScalarType(typeId: nominalTypeId)) { |
4676 | // Conversion for bool |
4677 | spv::Id boolType = builder.makeBoolType(); |
4678 | if (nominalTypeId != boolType) { |
4679 | // keep these outside arguments, for determinant order-of-evaluation |
4680 | spv::Id one = builder.makeUintConstant(u: 1); |
4681 | spv::Id zero = builder.makeUintConstant(u: 0); |
4682 | rvalue = builder.createTriOp(spv::OpSelect, typeId: nominalTypeId, operand1: rvalue, operand2: one, operand3: zero); |
4683 | } else if (builder.getTypeId(resultId: rvalue) != boolType) |
4684 | rvalue = builder.createBinOp(spv::OpINotEqual, typeId: boolType, operand1: rvalue, operand2: builder.makeUintConstant(u: 0)); |
4685 | } else if (builder.isVectorType(typeId: nominalTypeId)) { |
4686 | // Conversion for bvec |
4687 | int vecSize = builder.getNumTypeComponents(typeId: nominalTypeId); |
4688 | spv::Id bvecType = builder.makeVectorType(component: builder.makeBoolType(), size: vecSize); |
4689 | if (nominalTypeId != bvecType) { |
4690 | // keep these outside arguments, for determinant order-of-evaluation |
4691 | spv::Id one = makeSmearedConstant(constant: builder.makeUintConstant(u: 1), vectorSize: vecSize); |
4692 | spv::Id zero = makeSmearedConstant(constant: builder.makeUintConstant(u: 0), vectorSize: vecSize); |
4693 | rvalue = builder.createTriOp(spv::OpSelect, typeId: nominalTypeId, operand1: rvalue, operand2: one, operand3: zero); |
4694 | } else if (builder.getTypeId(resultId: rvalue) != bvecType) |
4695 | rvalue = builder.createBinOp(spv::OpINotEqual, typeId: bvecType, operand1: rvalue, |
4696 | operand2: makeSmearedConstant(constant: builder.makeUintConstant(u: 0), vectorSize: vecSize)); |
4697 | } |
4698 | } |
4699 | |
4700 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
4701 | coherentFlags |= TranslateCoherent(type); |
4702 | |
4703 | unsigned int alignment = builder.getAccessChain().alignment; |
4704 | alignment |= type.getBufferReferenceAlignment(); |
4705 | |
4706 | builder.accessChainStore(rvalue, nonUniform: TranslateNonUniformDecoration(coherentFlags: builder.getAccessChain().coherentFlags), |
4707 | memoryAccess: spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
4708 | ~spv::MemoryAccessMakePointerVisibleKHRMask), |
4709 | scope: TranslateMemoryScope(coherentFlags), alignment); |
4710 | } |
4711 | |
4712 | // For storing when types match at the glslang level, but not might match at the |
4713 | // SPIR-V level. |
4714 | // |
4715 | // This especially happens when a single glslang type expands to multiple |
4716 | // SPIR-V types, like a struct that is used in a member-undecorated way as well |
4717 | // as in a member-decorated way. |
4718 | // |
4719 | // NOTE: This function can handle any store request; if it's not special it |
4720 | // simplifies to a simple OpStore. |
4721 | // |
4722 | // Implicitly uses the existing builder.accessChain as the storage target. |
4723 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
4724 | { |
4725 | // we only do the complex path here if it's an aggregate |
4726 | if (! type.isStruct() && ! type.isArray()) { |
4727 | accessChainStore(type, rvalue: rValue); |
4728 | return; |
4729 | } |
4730 | |
4731 | // and, it has to be a case of type aliasing |
4732 | spv::Id rType = builder.getTypeId(resultId: rValue); |
4733 | spv::Id lValue = builder.accessChainGetLValue(); |
4734 | spv::Id lType = builder.getContainedTypeId(typeId: builder.getTypeId(resultId: lValue)); |
4735 | if (lType == rType) { |
4736 | accessChainStore(type, rvalue: rValue); |
4737 | return; |
4738 | } |
4739 | |
4740 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
4741 | // where the two types were the same type in GLSL. This requires member |
4742 | // by member copy, recursively. |
4743 | |
4744 | // SPIR-V 1.4 added an instruction to do help do this. |
4745 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
4746 | // However, bool in uniform space is changed to int, so |
4747 | // OpCopyLogical does not work for that. |
4748 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
4749 | bool rBool = builder.containsType(typeId: builder.getTypeId(resultId: rValue), typeOp: spv::OpTypeBool, width: 0); |
4750 | bool lBool = builder.containsType(typeId: lType, typeOp: spv::OpTypeBool, width: 0); |
4751 | if (lBool == rBool) { |
4752 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, typeId: lType, operand: rValue); |
4753 | accessChainStore(type, rvalue: logicalCopy); |
4754 | return; |
4755 | } |
4756 | } |
4757 | |
4758 | // If an array, copy element by element. |
4759 | if (type.isArray()) { |
4760 | glslang::TType glslangElementType(type, 0); |
4761 | spv::Id elementRType = builder.getContainedTypeId(typeId: rType); |
4762 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
4763 | // get the source member |
4764 | spv::Id elementRValue = builder.createCompositeExtract(composite: rValue, typeId: elementRType, index); |
4765 | |
4766 | // set up the target storage |
4767 | builder.clearAccessChain(); |
4768 | builder.setAccessChainLValue(lValue); |
4769 | builder.accessChainPush(offset: builder.makeIntConstant(i: index), coherentFlags: TranslateCoherent(type), |
4770 | alignment: type.getBufferReferenceAlignment()); |
4771 | |
4772 | // store the member |
4773 | multiTypeStore(type: glslangElementType, rValue: elementRValue); |
4774 | } |
4775 | } else { |
4776 | assert(type.isStruct()); |
4777 | |
4778 | // loop over structure members |
4779 | const glslang::TTypeList& members = *type.getStruct(); |
4780 | for (int m = 0; m < (int)members.size(); ++m) { |
4781 | const glslang::TType& glslangMemberType = *members[m].type; |
4782 | |
4783 | // get the source member |
4784 | spv::Id memberRType = builder.getContainedTypeId(typeId: rType, m); |
4785 | spv::Id memberRValue = builder.createCompositeExtract(composite: rValue, typeId: memberRType, index: m); |
4786 | |
4787 | // set up the target storage |
4788 | builder.clearAccessChain(); |
4789 | builder.setAccessChainLValue(lValue); |
4790 | builder.accessChainPush(offset: builder.makeIntConstant(i: m), coherentFlags: TranslateCoherent(type), |
4791 | alignment: type.getBufferReferenceAlignment()); |
4792 | |
4793 | // store the member |
4794 | multiTypeStore(type: glslangMemberType, rValue: memberRValue); |
4795 | } |
4796 | } |
4797 | } |
4798 | |
4799 | // Decide whether or not this type should be |
4800 | // decorated with offsets and strides, and if so |
4801 | // whether std140 or std430 rules should be applied. |
4802 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
4803 | { |
4804 | // has to be a block |
4805 | if (type.getBasicType() != glslang::EbtBlock) |
4806 | return glslang::ElpNone; |
4807 | |
4808 | // has to be a uniform or buffer block or task in/out blocks |
4809 | if (type.getQualifier().storage != glslang::EvqUniform && |
4810 | type.getQualifier().storage != glslang::EvqBuffer && |
4811 | type.getQualifier().storage != glslang::EvqShared && |
4812 | !type.getQualifier().isTaskMemory()) |
4813 | return glslang::ElpNone; |
4814 | |
4815 | // return the layout to use |
4816 | switch (type.getQualifier().layoutPacking) { |
4817 | case glslang::ElpStd140: |
4818 | case glslang::ElpStd430: |
4819 | case glslang::ElpScalar: |
4820 | return type.getQualifier().layoutPacking; |
4821 | default: |
4822 | return glslang::ElpNone; |
4823 | } |
4824 | } |
4825 | |
4826 | // Given an array type, returns the integer stride required for that array |
4827 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
4828 | glslang::TLayoutMatrix matrixLayout) |
4829 | { |
4830 | int size; |
4831 | int stride; |
4832 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, layoutPacking: explicitLayout, |
4833 | rowMajor: matrixLayout == glslang::ElmRowMajor); |
4834 | |
4835 | return stride; |
4836 | } |
4837 | |
4838 | // Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix |
4839 | // when used as a member of an interface block |
4840 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
4841 | glslang::TLayoutMatrix matrixLayout) |
4842 | { |
4843 | glslang::TType elementType; |
4844 | elementType.shallowCopy(copyOf: matrixType); |
4845 | elementType.clearArraySizes(); |
4846 | |
4847 | int size; |
4848 | int stride; |
4849 | glslangIntermediate->getMemberAlignment(elementType, size, stride, layoutPacking: explicitLayout, |
4850 | rowMajor: matrixLayout == glslang::ElmRowMajor); |
4851 | |
4852 | return stride; |
4853 | } |
4854 | |
4855 | // Given a member type of a struct, realign the current offset for it, and compute |
4856 | // the next (not yet aligned) offset for the next member, which will get aligned |
4857 | // on the next call. |
4858 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
4859 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
4860 | // -1 means a non-forced member offset (no decoration needed). |
4861 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
4862 | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
4863 | { |
4864 | // this will get a positive value when deemed necessary |
4865 | nextOffset = -1; |
4866 | |
4867 | // override anything in currentOffset with user-set offset |
4868 | if (memberType.getQualifier().hasOffset()) |
4869 | currentOffset = memberType.getQualifier().layoutOffset; |
4870 | |
4871 | // It could be that current linker usage in glslang updated all the layoutOffset, |
4872 | // in which case the following code does not matter. But, that's not quite right |
4873 | // once cross-compilation unit GLSL validation is done, as the original user |
4874 | // settings are needed in layoutOffset, and then the following will come into play. |
4875 | |
4876 | if (explicitLayout == glslang::ElpNone) { |
4877 | if (! memberType.getQualifier().hasOffset()) |
4878 | currentOffset = -1; |
4879 | |
4880 | return; |
4881 | } |
4882 | |
4883 | // Getting this far means we need explicit offsets |
4884 | if (currentOffset < 0) |
4885 | currentOffset = 0; |
4886 | |
4887 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
4888 | // but possibly not yet correctly aligned. |
4889 | |
4890 | int memberSize; |
4891 | int dummyStride; |
4892 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, size&: memberSize, stride&: dummyStride, layoutPacking: explicitLayout, |
4893 | rowMajor: matrixLayout == glslang::ElmRowMajor); |
4894 | |
4895 | // Adjust alignment for HLSL rules |
4896 | // TODO: make this consistent in early phases of code: |
4897 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
4898 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
4899 | // which is the most likely to rely on reflection, and least likely to rely implicit layouts |
4900 | if (glslangIntermediate->usingHlslOffsets() && |
4901 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare(s: "$Global" ) != 0) { |
4902 | int dummySize; |
4903 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, size&: dummySize); |
4904 | if (componentAlignment <= 4) |
4905 | memberAlignment = componentAlignment; |
4906 | } |
4907 | |
4908 | // Bump up to member alignment |
4909 | glslang::RoundToPow2(number&: currentOffset, powerOf2: memberAlignment); |
4910 | |
4911 | // Bump up to vec4 if there is a bad straddle |
4912 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(type: memberType, size: memberSize, |
4913 | offset: currentOffset)) |
4914 | glslang::RoundToPow2(number&: currentOffset, powerOf2: 16); |
4915 | |
4916 | nextOffset = currentOffset + memberSize; |
4917 | } |
4918 | |
4919 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
4920 | { |
4921 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
4922 | switch (glslangBuiltIn) |
4923 | { |
4924 | case glslang::EbvPointSize: |
4925 | #ifndef GLSLANG_WEB |
4926 | case glslang::EbvClipDistance: |
4927 | case glslang::EbvCullDistance: |
4928 | case glslang::EbvViewportMaskNV: |
4929 | case glslang::EbvSecondaryPositionNV: |
4930 | case glslang::EbvSecondaryViewportMaskNV: |
4931 | case glslang::EbvPositionPerViewNV: |
4932 | case glslang::EbvViewportMaskPerViewNV: |
4933 | case glslang::EbvTaskCountNV: |
4934 | case glslang::EbvPrimitiveCountNV: |
4935 | case glslang::EbvPrimitiveIndicesNV: |
4936 | case glslang::EbvClipDistancePerViewNV: |
4937 | case glslang::EbvCullDistancePerViewNV: |
4938 | case glslang::EbvLayerPerViewNV: |
4939 | case glslang::EbvMeshViewCountNV: |
4940 | case glslang::EbvMeshViewIndicesNV: |
4941 | #endif |
4942 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
4943 | // Alternately, we could just call this for any glslang built-in, since the |
4944 | // capability already guards against duplicates. |
4945 | TranslateBuiltInDecoration(builtIn: glslangBuiltIn, memberDeclaration: false); |
4946 | break; |
4947 | default: |
4948 | // Capabilities were already generated when the struct was declared. |
4949 | break; |
4950 | } |
4951 | } |
4952 | |
4953 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
4954 | { |
4955 | return node->getName().compare(s: glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
4956 | } |
4957 | |
4958 | // Does parameter need a place to keep writes, separate from the original? |
4959 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
4960 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
4961 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
4962 | { |
4963 | assert(qualifier == glslang::EvqIn || |
4964 | qualifier == glslang::EvqOut || |
4965 | qualifier == glslang::EvqInOut || |
4966 | qualifier == glslang::EvqUniform || |
4967 | qualifier == glslang::EvqConstReadOnly); |
4968 | return qualifier != glslang::EvqConstReadOnly && |
4969 | qualifier != glslang::EvqUniform; |
4970 | } |
4971 | |
4972 | // Is parameter pass-by-original? |
4973 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
4974 | bool implicitThisParam) |
4975 | { |
4976 | if (implicitThisParam) // implicit this |
4977 | return true; |
4978 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
4979 | return paramType.getBasicType() == glslang::EbtBlock; |
4980 | return paramType.containsOpaque() || // sampler, etc. |
4981 | #ifndef GLSLANG_WEB |
4982 | paramType.getQualifier().isSpirvByReference() || // spirv_by_reference |
4983 | #endif |
4984 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
4985 | } |
4986 | |
4987 | // Make all the functions, skeletally, without actually visiting their bodies. |
4988 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
4989 | { |
4990 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
4991 | bool useVulkanMemoryModel) { |
4992 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
4993 | if (paramPrecision != spv::NoPrecision) |
4994 | decorations.push_back(x: paramPrecision); |
4995 | TranslateMemoryDecoration(qualifier: type.getQualifier(), memory&: decorations, useVulkanMemoryModel); |
4996 | if (type.isReference()) { |
4997 | // Original and non-writable params pass the pointer directly and |
4998 | // use restrict/aliased, others are stored to a pointer in Function |
4999 | // memory and use RestrictPointer/AliasedPointer. |
5000 | if (originalParam(qualifier: type.getQualifier().storage, paramType: type, implicitThisParam: false) || |
5001 | !writableParam(qualifier: type.getQualifier().storage)) { |
5002 | decorations.push_back(x: type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
5003 | spv::DecorationAliased); |
5004 | } else { |
5005 | decorations.push_back(x: type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
5006 | spv::DecorationAliasedPointerEXT); |
5007 | } |
5008 | } |
5009 | }; |
5010 | |
5011 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
5012 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
5013 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(node: glslFunction)) |
5014 | continue; |
5015 | |
5016 | // We're on a user function. Set up the basic interface for the function now, |
5017 | // so that it's available to call. Translating the body will happen later. |
5018 | // |
5019 | // Typically (except for a "const in" parameter), an address will be passed to the |
5020 | // function. What it is an address of varies: |
5021 | // |
5022 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
5023 | // argument so that write needs to be to a copy, hence the address of a copy works. |
5024 | // |
5025 | // - "const in" parameters can just be the r-value, as no writes need occur. |
5026 | // |
5027 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
5028 | // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
5029 | |
5030 | std::vector<spv::Id> paramTypes; |
5031 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
5032 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
5033 | |
5034 | #ifdef ENABLE_HLSL |
5035 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
5036 | glslangIntermediate->implicitThisName; |
5037 | #else |
5038 | bool implicitThis = false; |
5039 | #endif |
5040 | |
5041 | paramDecorations.resize(new_size: parameters.size()); |
5042 | for (int p = 0; p < (int)parameters.size(); ++p) { |
5043 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
5044 | spv::Id typeId = convertGlslangToSpvType(type: paramType); |
5045 | if (originalParam(qualifier: paramType.getQualifier().storage, paramType, implicitThisParam: implicitThis && p == 0)) |
5046 | typeId = builder.makePointer(TranslateStorageClass(type: paramType), pointee: typeId); |
5047 | else if (writableParam(qualifier: paramType.getQualifier().storage)) |
5048 | typeId = builder.makePointer(spv::StorageClassFunction, pointee: typeId); |
5049 | else |
5050 | rValueParameters.insert(x: parameters[p]->getAsSymbolNode()->getId()); |
5051 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
5052 | paramTypes.push_back(x: typeId); |
5053 | } |
5054 | |
5055 | spv::Block* functionBlock; |
5056 | spv::Function *function = builder.makeFunctionEntry(precision: TranslatePrecisionDecoration(type: glslFunction->getType()), |
5057 | returnType: convertGlslangToSpvType(type: glslFunction->getType()), |
5058 | name: glslFunction->getName().c_str(), paramTypes, |
5059 | precisions: paramDecorations, entry: &functionBlock); |
5060 | if (implicitThis) |
5061 | function->setImplicitThis(); |
5062 | |
5063 | // Track function to emit/call later |
5064 | functionMap[glslFunction->getName().c_str()] = function; |
5065 | |
5066 | // Set the parameter id's |
5067 | for (int p = 0; p < (int)parameters.size(); ++p) { |
5068 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
5069 | // give a name too |
5070 | builder.addName(function->getParamId(p), name: parameters[p]->getAsSymbolNode()->getName().c_str()); |
5071 | |
5072 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
5073 | if (paramType.contains8BitInt()) |
5074 | builder.addCapability(cap: spv::CapabilityInt8); |
5075 | if (paramType.contains16BitInt()) |
5076 | builder.addCapability(cap: spv::CapabilityInt16); |
5077 | if (paramType.contains16BitFloat()) |
5078 | builder.addCapability(cap: spv::CapabilityFloat16); |
5079 | } |
5080 | } |
5081 | } |
5082 | |
5083 | // Process all the initializers, while skipping the functions and link objects |
5084 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
5085 | { |
5086 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
5087 | for (int i = 0; i < (int)initializers.size(); ++i) { |
5088 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
5089 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
5090 | glslang::EOpLinkerObjects) { |
5091 | |
5092 | // We're on a top-level node that's not a function. Treat as an initializer, whose |
5093 | // code goes into the beginning of the entry point. |
5094 | initializer->traverse(this); |
5095 | } |
5096 | } |
5097 | } |
5098 | // Walk over all linker objects to create a map for payload and callable data linker objects |
5099 | // and their location to be used during codegen for OpTraceKHR and OpExecuteCallableKHR |
5100 | // This is done here since it is possible that these linker objects are not be referenced in the AST |
5101 | void TGlslangToSpvTraverser::collectRayTracingLinkerObjects() |
5102 | { |
5103 | glslang::TIntermAggregate* linkerObjects = glslangIntermediate->findLinkerObjects(); |
5104 | for (auto& objSeq : linkerObjects->getSequence()) { |
5105 | auto objNode = objSeq->getAsSymbolNode(); |
5106 | if (objNode != nullptr) { |
5107 | if (objNode->getQualifier().hasLocation()) { |
5108 | unsigned int location = objNode->getQualifier().layoutLocation; |
5109 | auto st = objNode->getQualifier().storage; |
5110 | int set; |
5111 | switch (st) |
5112 | { |
5113 | case glslang::EvqPayload: |
5114 | case glslang::EvqPayloadIn: |
5115 | set = 0; |
5116 | break; |
5117 | case glslang::EvqCallableData: |
5118 | case glslang::EvqCallableDataIn: |
5119 | set = 1; |
5120 | break; |
5121 | |
5122 | default: |
5123 | set = -1; |
5124 | } |
5125 | if (set != -1) |
5126 | locationToSymbol[set].insert(x: std::make_pair(x&: location, y&: objNode)); |
5127 | } |
5128 | } |
5129 | } |
5130 | } |
5131 | // Process all the functions, while skipping initializers. |
5132 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
5133 | { |
5134 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
5135 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
5136 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
5137 | node->traverse(this); |
5138 | } |
5139 | } |
5140 | |
5141 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
5142 | { |
5143 | // SPIR-V functions should already be in the functionMap from the prepass |
5144 | // that called makeFunctions(). |
5145 | currentFunction = functionMap[node->getName().c_str()]; |
5146 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
5147 | builder.setBuildPoint(functionBlock); |
5148 | } |
5149 | |
5150 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
5151 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
5152 | { |
5153 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
5154 | |
5155 | glslang::TSampler sampler = {}; |
5156 | bool cubeCompare = false; |
5157 | #ifndef GLSLANG_WEB |
5158 | bool f16ShadowCompare = false; |
5159 | #endif |
5160 | if (node.isTexture() || node.isImage()) { |
5161 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
5162 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
5163 | #ifndef GLSLANG_WEB |
5164 | f16ShadowCompare = sampler.shadow && |
5165 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
5166 | #endif |
5167 | } |
5168 | |
5169 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
5170 | builder.clearAccessChain(); |
5171 | glslangArguments[i]->traverse(this); |
5172 | |
5173 | #ifndef GLSLANG_WEB |
5174 | // Special case l-value operands |
5175 | bool lvalue = false; |
5176 | switch (node.getOp()) { |
5177 | case glslang::EOpImageAtomicAdd: |
5178 | case glslang::EOpImageAtomicMin: |
5179 | case glslang::EOpImageAtomicMax: |
5180 | case glslang::EOpImageAtomicAnd: |
5181 | case glslang::EOpImageAtomicOr: |
5182 | case glslang::EOpImageAtomicXor: |
5183 | case glslang::EOpImageAtomicExchange: |
5184 | case glslang::EOpImageAtomicCompSwap: |
5185 | case glslang::EOpImageAtomicLoad: |
5186 | case glslang::EOpImageAtomicStore: |
5187 | if (i == 0) |
5188 | lvalue = true; |
5189 | break; |
5190 | case glslang::EOpSparseImageLoad: |
5191 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
5192 | lvalue = true; |
5193 | break; |
5194 | case glslang::EOpSparseTexture: |
5195 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
5196 | lvalue = true; |
5197 | break; |
5198 | case glslang::EOpSparseTextureClamp: |
5199 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
5200 | lvalue = true; |
5201 | break; |
5202 | case glslang::EOpSparseTextureLod: |
5203 | case glslang::EOpSparseTextureOffset: |
5204 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
5205 | lvalue = true; |
5206 | break; |
5207 | case glslang::EOpSparseTextureFetch: |
5208 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
5209 | lvalue = true; |
5210 | break; |
5211 | case glslang::EOpSparseTextureFetchOffset: |
5212 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
5213 | lvalue = true; |
5214 | break; |
5215 | case glslang::EOpSparseTextureLodOffset: |
5216 | case glslang::EOpSparseTextureGrad: |
5217 | case glslang::EOpSparseTextureOffsetClamp: |
5218 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
5219 | lvalue = true; |
5220 | break; |
5221 | case glslang::EOpSparseTextureGradOffset: |
5222 | case glslang::EOpSparseTextureGradClamp: |
5223 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
5224 | lvalue = true; |
5225 | break; |
5226 | case glslang::EOpSparseTextureGradOffsetClamp: |
5227 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
5228 | lvalue = true; |
5229 | break; |
5230 | case glslang::EOpSparseTextureGather: |
5231 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
5232 | lvalue = true; |
5233 | break; |
5234 | case glslang::EOpSparseTextureGatherOffset: |
5235 | case glslang::EOpSparseTextureGatherOffsets: |
5236 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
5237 | lvalue = true; |
5238 | break; |
5239 | case glslang::EOpSparseTextureGatherLod: |
5240 | if (i == 3) |
5241 | lvalue = true; |
5242 | break; |
5243 | case glslang::EOpSparseTextureGatherLodOffset: |
5244 | case glslang::EOpSparseTextureGatherLodOffsets: |
5245 | if (i == 4) |
5246 | lvalue = true; |
5247 | break; |
5248 | case glslang::EOpSparseImageLoadLod: |
5249 | if (i == 3) |
5250 | lvalue = true; |
5251 | break; |
5252 | case glslang::EOpImageSampleFootprintNV: |
5253 | if (i == 4) |
5254 | lvalue = true; |
5255 | break; |
5256 | case glslang::EOpImageSampleFootprintClampNV: |
5257 | case glslang::EOpImageSampleFootprintLodNV: |
5258 | if (i == 5) |
5259 | lvalue = true; |
5260 | break; |
5261 | case glslang::EOpImageSampleFootprintGradNV: |
5262 | if (i == 6) |
5263 | lvalue = true; |
5264 | break; |
5265 | case glslang::EOpImageSampleFootprintGradClampNV: |
5266 | if (i == 7) |
5267 | lvalue = true; |
5268 | break; |
5269 | default: |
5270 | break; |
5271 | } |
5272 | |
5273 | if (lvalue) { |
5274 | spv::Id lvalue_id = builder.accessChainGetLValue(); |
5275 | arguments.push_back(x: lvalue_id); |
5276 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
5277 | builder.addDecoration(lvalue_id, TranslateNonUniformDecoration(coherentFlags: lvalueCoherentFlags)); |
5278 | lvalueCoherentFlags |= TranslateCoherent(type: glslangArguments[i]->getAsTyped()->getType()); |
5279 | } else |
5280 | #endif |
5281 | arguments.push_back(x: accessChainLoad(type: glslangArguments[i]->getAsTyped()->getType())); |
5282 | } |
5283 | } |
5284 | |
5285 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
5286 | { |
5287 | builder.clearAccessChain(); |
5288 | node.getOperand()->traverse(this); |
5289 | arguments.push_back(x: accessChainLoad(type: node.getOperand()->getType())); |
5290 | } |
5291 | |
5292 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
5293 | { |
5294 | if (! node->isImage() && ! node->isTexture()) |
5295 | return spv::NoResult; |
5296 | |
5297 | builder.setLine(line: node->getLoc().line, filename: node->getLoc().getFilename()); |
5298 | |
5299 | // Process a GLSL texturing op (will be SPV image) |
5300 | |
5301 | const glslang::TType &imageType = node->getAsAggregate() |
5302 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
5303 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
5304 | const glslang::TSampler sampler = imageType.getSampler(); |
5305 | #ifdef GLSLANG_WEB |
5306 | const bool f16ShadowCompare = false; |
5307 | #else |
5308 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
5309 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
5310 | : false; |
5311 | #endif |
5312 | |
5313 | const auto signExtensionMask = [&]() { |
5314 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
5315 | if (sampler.type == glslang::EbtUint) |
5316 | return spv::ImageOperandsZeroExtendMask; |
5317 | else if (sampler.type == glslang::EbtInt) |
5318 | return spv::ImageOperandsSignExtendMask; |
5319 | } |
5320 | return spv::ImageOperandsMaskNone; |
5321 | }; |
5322 | |
5323 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
5324 | |
5325 | std::vector<spv::Id> arguments; |
5326 | if (node->getAsAggregate()) |
5327 | translateArguments(node: *node->getAsAggregate(), arguments, lvalueCoherentFlags); |
5328 | else |
5329 | translateArguments(node&: *node->getAsUnaryNode(), arguments); |
5330 | spv::Decoration precision = TranslatePrecisionDecoration(type: node->getType()); |
5331 | |
5332 | spv::Builder::TextureParameters params = { }; |
5333 | params.sampler = arguments[0]; |
5334 | |
5335 | glslang::TCrackedTextureOp cracked; |
5336 | node->crackTexture(sampler, cracked); |
5337 | |
5338 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
5339 | |
5340 | if (builder.isSampledImage(resultId: params.sampler) && |
5341 | ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
5342 | params.sampler = builder.createUnaryOp(spv::OpImage, typeId: builder.getImageType(resultId: params.sampler), operand: params.sampler); |
5343 | if (imageType.getQualifier().isNonUniform()) { |
5344 | builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT); |
5345 | } |
5346 | } |
5347 | // Check for queries |
5348 | if (cracked.query) { |
5349 | switch (node->getOp()) { |
5350 | case glslang::EOpImageQuerySize: |
5351 | case glslang::EOpTextureQuerySize: |
5352 | if (arguments.size() > 1) { |
5353 | params.lod = arguments[1]; |
5354 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
5355 | } else |
5356 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
5357 | #ifndef GLSLANG_WEB |
5358 | case glslang::EOpImageQuerySamples: |
5359 | case glslang::EOpTextureQuerySamples: |
5360 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
5361 | case glslang::EOpTextureQueryLod: |
5362 | params.coords = arguments[1]; |
5363 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
5364 | case glslang::EOpTextureQueryLevels: |
5365 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
5366 | case glslang::EOpSparseTexelsResident: |
5367 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, typeId: builder.makeBoolType(), operand: arguments[0]); |
5368 | #endif |
5369 | default: |
5370 | assert(0); |
5371 | break; |
5372 | } |
5373 | } |
5374 | |
5375 | int components = node->getType().getVectorSize(); |
5376 | |
5377 | if (node->getOp() == glslang::EOpImageLoad || |
5378 | node->getOp() == glslang::EOpImageLoadLod || |
5379 | node->getOp() == glslang::EOpTextureFetch || |
5380 | node->getOp() == glslang::EOpTextureFetchOffset) { |
5381 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
5382 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
5383 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
5384 | // here around e.g. which ones return scalars or other types. |
5385 | components = 4; |
5386 | } |
5387 | |
5388 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
5389 | |
5390 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(type: returnType); }; |
5391 | |
5392 | // Check for image functions other than queries |
5393 | if (node->isImage()) { |
5394 | std::vector<spv::IdImmediate> operands; |
5395 | auto opIt = arguments.begin(); |
5396 | spv::IdImmediate image = { true, *(opIt++) }; |
5397 | operands.push_back(x: image); |
5398 | |
5399 | // Handle subpass operations |
5400 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
5401 | // built-in function. |
5402 | if (cracked.subpass) { |
5403 | // add on the (0,0) coordinate |
5404 | spv::Id zero = builder.makeIntConstant(i: 0); |
5405 | std::vector<spv::Id> comps; |
5406 | comps.push_back(x: zero); |
5407 | comps.push_back(x: zero); |
5408 | spv::IdImmediate coord = { true, |
5409 | builder.makeCompositeConstant(type: builder.makeVectorType(component: builder.makeIntType(width: 32), size: 2), comps) }; |
5410 | operands.push_back(x: coord); |
5411 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
5412 | imageOperands.word = imageOperands.word | signExtensionMask(); |
5413 | if (sampler.isMultiSample()) { |
5414 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
5415 | } |
5416 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
5417 | operands.push_back(x: imageOperands); |
5418 | if (sampler.isMultiSample()) { |
5419 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
5420 | operands.push_back(x: imageOperand); |
5421 | } |
5422 | } |
5423 | spv::Id result = builder.createOp(spv::OpImageRead, typeId: resultType(), operands); |
5424 | builder.setPrecision(id: result, precision); |
5425 | return result; |
5426 | } |
5427 | |
5428 | spv::IdImmediate coord = { true, *(opIt++) }; |
5429 | operands.push_back(x: coord); |
5430 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
5431 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
5432 | if (sampler.isMultiSample()) { |
5433 | mask = mask | spv::ImageOperandsSampleMask; |
5434 | } |
5435 | if (cracked.lod) { |
5436 | builder.addExtension(ext: spv::E_SPV_AMD_shader_image_load_store_lod); |
5437 | builder.addCapability(cap: spv::CapabilityImageReadWriteLodAMD); |
5438 | mask = mask | spv::ImageOperandsLodMask; |
5439 | } |
5440 | mask = mask | TranslateImageOperands(coherentFlags: TranslateCoherent(type: imageType)); |
5441 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
5442 | mask = mask | signExtensionMask(); |
5443 | if (mask != spv::ImageOperandsMaskNone) { |
5444 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
5445 | operands.push_back(x: imageOperands); |
5446 | } |
5447 | if (mask & spv::ImageOperandsSampleMask) { |
5448 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5449 | operands.push_back(x: imageOperand); |
5450 | } |
5451 | if (mask & spv::ImageOperandsLodMask) { |
5452 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5453 | operands.push_back(x: imageOperand); |
5454 | } |
5455 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
5456 | spv::IdImmediate imageOperand = { true, |
5457 | builder.makeUintConstant(u: TranslateMemoryScope(coherentFlags: TranslateCoherent(type: imageType))) }; |
5458 | operands.push_back(x: imageOperand); |
5459 | } |
5460 | |
5461 | if (builder.getImageTypeFormat(typeId: builder.getImageType(resultId: operands.front().word)) == spv::ImageFormatUnknown) |
5462 | builder.addCapability(cap: spv::CapabilityStorageImageReadWithoutFormat); |
5463 | |
5464 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, typeId: resultType(), operands)); |
5465 | builder.setPrecision(id: result[0], precision); |
5466 | |
5467 | // If needed, add a conversion constructor to the proper size. |
5468 | if (components != node->getType().getVectorSize()) |
5469 | result[0] = builder.createConstructor(precision, sources: result, resultTypeId: convertGlslangToSpvType(type: node->getType())); |
5470 | |
5471 | return result[0]; |
5472 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
5473 | |
5474 | // Push the texel value before the operands |
5475 | if (sampler.isMultiSample() || cracked.lod) { |
5476 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
5477 | operands.push_back(x: texel); |
5478 | } else { |
5479 | spv::IdImmediate texel = { true, *opIt }; |
5480 | operands.push_back(x: texel); |
5481 | } |
5482 | |
5483 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
5484 | if (sampler.isMultiSample()) { |
5485 | mask = mask | spv::ImageOperandsSampleMask; |
5486 | } |
5487 | if (cracked.lod) { |
5488 | builder.addExtension(ext: spv::E_SPV_AMD_shader_image_load_store_lod); |
5489 | builder.addCapability(cap: spv::CapabilityImageReadWriteLodAMD); |
5490 | mask = mask | spv::ImageOperandsLodMask; |
5491 | } |
5492 | mask = mask | TranslateImageOperands(coherentFlags: TranslateCoherent(type: imageType)); |
5493 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
5494 | mask = mask | signExtensionMask(); |
5495 | if (mask != spv::ImageOperandsMaskNone) { |
5496 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
5497 | operands.push_back(x: imageOperands); |
5498 | } |
5499 | if (mask & spv::ImageOperandsSampleMask) { |
5500 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5501 | operands.push_back(x: imageOperand); |
5502 | } |
5503 | if (mask & spv::ImageOperandsLodMask) { |
5504 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5505 | operands.push_back(x: imageOperand); |
5506 | } |
5507 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
5508 | spv::IdImmediate imageOperand = { true, |
5509 | builder.makeUintConstant(u: TranslateMemoryScope(coherentFlags: TranslateCoherent(type: imageType))) }; |
5510 | operands.push_back(x: imageOperand); |
5511 | } |
5512 | |
5513 | builder.createNoResultOp(spv::OpImageWrite, operands); |
5514 | if (builder.getImageTypeFormat(typeId: builder.getImageType(resultId: operands.front().word)) == spv::ImageFormatUnknown) |
5515 | builder.addCapability(cap: spv::CapabilityStorageImageWriteWithoutFormat); |
5516 | return spv::NoResult; |
5517 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
5518 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
5519 | builder.addCapability(cap: spv::CapabilitySparseResidency); |
5520 | if (builder.getImageTypeFormat(typeId: builder.getImageType(resultId: operands.front().word)) == spv::ImageFormatUnknown) |
5521 | builder.addCapability(cap: spv::CapabilityStorageImageReadWithoutFormat); |
5522 | |
5523 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
5524 | if (sampler.isMultiSample()) { |
5525 | mask = mask | spv::ImageOperandsSampleMask; |
5526 | } |
5527 | if (cracked.lod) { |
5528 | builder.addExtension(ext: spv::E_SPV_AMD_shader_image_load_store_lod); |
5529 | builder.addCapability(cap: spv::CapabilityImageReadWriteLodAMD); |
5530 | |
5531 | mask = mask | spv::ImageOperandsLodMask; |
5532 | } |
5533 | mask = mask | TranslateImageOperands(coherentFlags: TranslateCoherent(type: imageType)); |
5534 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
5535 | mask = mask | signExtensionMask(); |
5536 | if (mask != spv::ImageOperandsMaskNone) { |
5537 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
5538 | operands.push_back(x: imageOperands); |
5539 | } |
5540 | if (mask & spv::ImageOperandsSampleMask) { |
5541 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5542 | operands.push_back(x: imageOperand); |
5543 | } |
5544 | if (mask & spv::ImageOperandsLodMask) { |
5545 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
5546 | operands.push_back(x: imageOperand); |
5547 | } |
5548 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
5549 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(u: TranslateMemoryScope( |
5550 | coherentFlags: TranslateCoherent(type: imageType))) }; |
5551 | operands.push_back(x: imageOperand); |
5552 | } |
5553 | |
5554 | // Create the return type that was a special structure |
5555 | spv::Id texelOut = *opIt; |
5556 | spv::Id typeId0 = resultType(); |
5557 | spv::Id typeId1 = builder.getDerefTypeId(resultId: texelOut); |
5558 | spv::Id resultTypeId = builder.makeStructResultType(type0: typeId0, type1: typeId1); |
5559 | |
5560 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, typeId: resultTypeId, operands); |
5561 | |
5562 | // Decode the return type |
5563 | builder.createStore(rValue: builder.createCompositeExtract(composite: resultId, typeId: typeId1, index: 1), lValue: texelOut); |
5564 | return builder.createCompositeExtract(composite: resultId, typeId: typeId0, index: 0); |
5565 | } else { |
5566 | // Process image atomic operations |
5567 | |
5568 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
5569 | // as the first source operand, is required by SPIR-V atomic operations. |
5570 | // For non-MS, the sample value should be 0 |
5571 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(u: 0) }; |
5572 | operands.push_back(x: sample); |
5573 | |
5574 | spv::Id resultTypeId; |
5575 | // imageAtomicStore has a void return type so base the pointer type on |
5576 | // the type of the value operand. |
5577 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
5578 | resultTypeId = builder.makePointer(spv::StorageClassImage, pointee: builder.getTypeId(resultId: *opIt)); |
5579 | } else { |
5580 | resultTypeId = builder.makePointer(spv::StorageClassImage, pointee: resultType()); |
5581 | } |
5582 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, typeId: resultTypeId, operands); |
5583 | if (imageType.getQualifier().nonUniform) { |
5584 | builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
5585 | } |
5586 | |
5587 | std::vector<spv::Id> operands; |
5588 | operands.push_back(x: pointer); |
5589 | for (; opIt != arguments.end(); ++opIt) |
5590 | operands.push_back(x: *opIt); |
5591 | |
5592 | return createAtomicOperation(op: node->getOp(), precision, typeId: resultType(), operands, typeProxy: node->getBasicType(), |
5593 | lvalueCoherentFlags); |
5594 | } |
5595 | } |
5596 | |
5597 | #ifndef GLSLANG_WEB |
5598 | // Check for fragment mask functions other than queries |
5599 | if (cracked.fragMask) { |
5600 | assert(sampler.ms); |
5601 | |
5602 | auto opIt = arguments.begin(); |
5603 | std::vector<spv::Id> operands; |
5604 | |
5605 | operands.push_back(x: params.sampler); |
5606 | ++opIt; |
5607 | |
5608 | if (sampler.isSubpass()) { |
5609 | // add on the (0,0) coordinate |
5610 | spv::Id zero = builder.makeIntConstant(i: 0); |
5611 | std::vector<spv::Id> comps; |
5612 | comps.push_back(x: zero); |
5613 | comps.push_back(x: zero); |
5614 | operands.push_back(x: builder.makeCompositeConstant( |
5615 | type: builder.makeVectorType(component: builder.makeIntType(width: 32), size: 2), comps)); |
5616 | } |
5617 | |
5618 | for (; opIt != arguments.end(); ++opIt) |
5619 | operands.push_back(x: *opIt); |
5620 | |
5621 | spv::Op fragMaskOp = spv::OpNop; |
5622 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
5623 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
5624 | else if (node->getOp() == glslang::EOpFragmentFetch) |
5625 | fragMaskOp = spv::OpFragmentFetchAMD; |
5626 | |
5627 | builder.addExtension(ext: spv::E_SPV_AMD_shader_fragment_mask); |
5628 | builder.addCapability(cap: spv::CapabilityFragmentMaskAMD); |
5629 | return builder.createOp(fragMaskOp, typeId: resultType(), operands); |
5630 | } |
5631 | #endif |
5632 | |
5633 | // Check for texture functions other than queries |
5634 | bool sparse = node->isSparseTexture(); |
5635 | bool = node->isImageFootprint(); |
5636 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
5637 | |
5638 | // check for bias argument |
5639 | bool bias = false; |
5640 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
5641 | int nonBiasArgCount = 2; |
5642 | if (cracked.gather) |
5643 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
5644 | |
5645 | if (f16ShadowCompare) |
5646 | ++nonBiasArgCount; |
5647 | if (cracked.offset) |
5648 | ++nonBiasArgCount; |
5649 | else if (cracked.offsets) |
5650 | ++nonBiasArgCount; |
5651 | if (cracked.grad) |
5652 | nonBiasArgCount += 2; |
5653 | if (cracked.lodClamp) |
5654 | ++nonBiasArgCount; |
5655 | if (sparse) |
5656 | ++nonBiasArgCount; |
5657 | if (imageFootprint) |
5658 | //Following three extra arguments |
5659 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
5660 | nonBiasArgCount += 3; |
5661 | if ((int)arguments.size() > nonBiasArgCount) |
5662 | bias = true; |
5663 | } |
5664 | |
5665 | #ifndef GLSLANG_WEB |
5666 | if (cracked.gather) { |
5667 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
5668 | if (bias || cracked.lod || |
5669 | sourceExtensions.find(x: glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
5670 | builder.addExtension(ext: spv::E_SPV_AMD_texture_gather_bias_lod); |
5671 | builder.addCapability(cap: spv::CapabilityImageGatherBiasLodAMD); |
5672 | } |
5673 | } |
5674 | #endif |
5675 | |
5676 | // set the rest of the arguments |
5677 | |
5678 | params.coords = arguments[1]; |
5679 | int = 0; |
5680 | bool noImplicitLod = false; |
5681 | |
5682 | // sort out where Dref is coming from |
5683 | if (cubeCompare || f16ShadowCompare) { |
5684 | params.Dref = arguments[2]; |
5685 | ++extraArgs; |
5686 | } else if (sampler.shadow && cracked.gather) { |
5687 | params.Dref = arguments[2]; |
5688 | ++extraArgs; |
5689 | } else if (sampler.shadow) { |
5690 | std::vector<spv::Id> indexes; |
5691 | int dRefComp; |
5692 | if (cracked.proj) |
5693 | dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref" |
5694 | else |
5695 | dRefComp = builder.getNumComponents(resultId: params.coords) - 1; |
5696 | indexes.push_back(x: dRefComp); |
5697 | params.Dref = builder.createCompositeExtract(composite: params.coords, |
5698 | typeId: builder.getScalarTypeId(typeId: builder.getTypeId(resultId: params.coords)), indexes); |
5699 | } |
5700 | |
5701 | // lod |
5702 | if (cracked.lod) { |
5703 | params.lod = arguments[2 + extraArgs]; |
5704 | ++extraArgs; |
5705 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
5706 | !(glslangIntermediate->getStage() == EShLangCompute && |
5707 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
5708 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
5709 | noImplicitLod = true; |
5710 | } |
5711 | |
5712 | // multisample |
5713 | if (sampler.isMultiSample()) { |
5714 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
5715 | ++extraArgs; |
5716 | } |
5717 | |
5718 | // gradient |
5719 | if (cracked.grad) { |
5720 | params.gradX = arguments[2 + extraArgs]; |
5721 | params.gradY = arguments[3 + extraArgs]; |
5722 | extraArgs += 2; |
5723 | } |
5724 | |
5725 | // offset and offsets |
5726 | if (cracked.offset) { |
5727 | params.offset = arguments[2 + extraArgs]; |
5728 | ++extraArgs; |
5729 | } else if (cracked.offsets) { |
5730 | params.offsets = arguments[2 + extraArgs]; |
5731 | ++extraArgs; |
5732 | } |
5733 | |
5734 | #ifndef GLSLANG_WEB |
5735 | // lod clamp |
5736 | if (cracked.lodClamp) { |
5737 | params.lodClamp = arguments[2 + extraArgs]; |
5738 | ++extraArgs; |
5739 | } |
5740 | // sparse |
5741 | if (sparse) { |
5742 | params.texelOut = arguments[2 + extraArgs]; |
5743 | ++extraArgs; |
5744 | } |
5745 | // gather component |
5746 | if (cracked.gather && ! sampler.shadow) { |
5747 | // default component is 0, if missing, otherwise an argument |
5748 | if (2 + extraArgs < (int)arguments.size()) { |
5749 | params.component = arguments[2 + extraArgs]; |
5750 | ++extraArgs; |
5751 | } else |
5752 | params.component = builder.makeIntConstant(i: 0); |
5753 | } |
5754 | spv::Id resultStruct = spv::NoResult; |
5755 | if (imageFootprint) { |
5756 | //Following three extra arguments |
5757 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
5758 | params.granularity = arguments[2 + extraArgs]; |
5759 | params.coarse = arguments[3 + extraArgs]; |
5760 | resultStruct = arguments[4 + extraArgs]; |
5761 | extraArgs += 3; |
5762 | } |
5763 | #endif |
5764 | // bias |
5765 | if (bias) { |
5766 | params.bias = arguments[2 + extraArgs]; |
5767 | ++extraArgs; |
5768 | } |
5769 | |
5770 | #ifndef GLSLANG_WEB |
5771 | if (imageFootprint) { |
5772 | builder.addExtension(ext: spv::E_SPV_NV_shader_image_footprint); |
5773 | builder.addCapability(cap: spv::CapabilityImageFootprintNV); |
5774 | |
5775 | |
5776 | //resultStructType(OpenGL type) contains 5 elements: |
5777 | //struct gl_TextureFootprint2DNV { |
5778 | // uvec2 anchor; |
5779 | // uvec2 offset; |
5780 | // uvec2 mask; |
5781 | // uint lod; |
5782 | // uint granularity; |
5783 | //}; |
5784 | //or |
5785 | //struct gl_TextureFootprint3DNV { |
5786 | // uvec3 anchor; |
5787 | // uvec3 offset; |
5788 | // uvec2 mask; |
5789 | // uint lod; |
5790 | // uint granularity; |
5791 | //}; |
5792 | spv::Id resultStructType = builder.getContainedTypeId(typeId: builder.getTypeId(resultId: resultStruct)); |
5793 | assert(builder.isStructType(resultStructType)); |
5794 | |
5795 | //resType (SPIR-V type) contains 6 elements: |
5796 | //Member 0 must be a Boolean type scalar(LOD), |
5797 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
5798 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
5799 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
5800 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
5801 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
5802 | std::vector<spv::Id> members; |
5803 | members.push_back(x: resultType()); |
5804 | for (int i = 0; i < 5; i++) { |
5805 | members.push_back(x: builder.getContainedTypeId(typeId: resultStructType, i)); |
5806 | } |
5807 | spv::Id resType = builder.makeStructType(members, "ResType" ); |
5808 | |
5809 | //call ImageFootprintNV |
5810 | spv::Id res = builder.createTextureCall(precision, resultType: resType, sparse, fetch: cracked.fetch, proj: cracked.proj, |
5811 | gather: cracked.gather, noImplicit: noImplicitLod, params, signExtensionMask()); |
5812 | |
5813 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
5814 | for (int i = 0; i < 5; i++) { |
5815 | builder.clearAccessChain(); |
5816 | builder.setAccessChainLValue(resultStruct); |
5817 | |
5818 | //Accessing to a struct we created, no coherent flag is set |
5819 | spv::Builder::AccessChain::CoherentFlags flags; |
5820 | flags.clear(); |
5821 | |
5822 | builder.accessChainPush(offset: builder.makeIntConstant(i), coherentFlags: flags, alignment: 0); |
5823 | builder.accessChainStore(rvalue: builder.createCompositeExtract(composite: res, typeId: builder.getContainedTypeId(typeId: resType, i+1), |
5824 | index: i+1), nonUniform: TranslateNonUniformDecoration(qualifier: imageType.getQualifier())); |
5825 | } |
5826 | return builder.createCompositeExtract(composite: res, typeId: resultType(), index: 0); |
5827 | } |
5828 | #endif |
5829 | |
5830 | // projective component (might not to move) |
5831 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
5832 | // are divided by the last component of P." |
5833 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
5834 | // unused components will appear after all used components." |
5835 | if (cracked.proj) { |
5836 | int projSourceComp = builder.getNumComponents(resultId: params.coords) - 1; |
5837 | int projTargetComp; |
5838 | switch (sampler.dim) { |
5839 | case glslang::Esd1D: projTargetComp = 1; break; |
5840 | case glslang::Esd2D: projTargetComp = 2; break; |
5841 | case glslang::EsdRect: projTargetComp = 2; break; |
5842 | default: projTargetComp = projSourceComp; break; |
5843 | } |
5844 | // copy the projective coordinate if we have to |
5845 | if (projTargetComp != projSourceComp) { |
5846 | spv::Id projComp = builder.createCompositeExtract(composite: params.coords, |
5847 | typeId: builder.getScalarTypeId(typeId: builder.getTypeId(resultId: params.coords)), index: projSourceComp); |
5848 | params.coords = builder.createCompositeInsert(object: projComp, composite: params.coords, |
5849 | typeId: builder.getTypeId(resultId: params.coords), index: projTargetComp); |
5850 | } |
5851 | } |
5852 | |
5853 | #ifndef GLSLANG_WEB |
5854 | // nonprivate |
5855 | if (imageType.getQualifier().nonprivate) { |
5856 | params.nonprivate = true; |
5857 | } |
5858 | |
5859 | // volatile |
5860 | if (imageType.getQualifier().volatil) { |
5861 | params.volatil = true; |
5862 | } |
5863 | #endif |
5864 | |
5865 | std::vector<spv::Id> result( 1, |
5866 | builder.createTextureCall(precision, resultType: resultType(), sparse, fetch: cracked.fetch, proj: cracked.proj, gather: cracked.gather, |
5867 | noImplicit: noImplicitLod, params, signExtensionMask()) |
5868 | ); |
5869 | |
5870 | if (components != node->getType().getVectorSize()) |
5871 | result[0] = builder.createConstructor(precision, sources: result, resultTypeId: convertGlslangToSpvType(type: node->getType())); |
5872 | |
5873 | return result[0]; |
5874 | } |
5875 | |
5876 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
5877 | { |
5878 | // Grab the function's pointer from the previously created function |
5879 | spv::Function* function = functionMap[node->getName().c_str()]; |
5880 | if (! function) |
5881 | return 0; |
5882 | |
5883 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
5884 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
5885 | |
5886 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
5887 | // |
5888 | // These imply we need a four step process: |
5889 | // 1. Evaluate the arguments |
5890 | // 2. Allocate and make copies of in, out, and inout arguments |
5891 | // 3. Make the call |
5892 | // 4. Copy back the results |
5893 | |
5894 | // 1. Evaluate the arguments and their types |
5895 | std::vector<spv::Builder::AccessChain> lValues; |
5896 | std::vector<spv::Id> rValues; |
5897 | std::vector<const glslang::TType*> argTypes; |
5898 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
5899 | argTypes.push_back(x: &glslangArgs[a]->getAsTyped()->getType()); |
5900 | // build l-value |
5901 | builder.clearAccessChain(); |
5902 | glslangArgs[a]->traverse(this); |
5903 | // keep outputs and pass-by-originals as l-values, evaluate others as r-values |
5904 | if (originalParam(qualifier: qualifiers[a], paramType: *argTypes[a], implicitThisParam: function->hasImplicitThis() && a == 0) || |
5905 | writableParam(qualifier: qualifiers[a])) { |
5906 | // save l-value |
5907 | lValues.push_back(x: builder.getAccessChain()); |
5908 | } else { |
5909 | // process r-value |
5910 | rValues.push_back(x: accessChainLoad(type: *argTypes.back())); |
5911 | } |
5912 | } |
5913 | |
5914 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
5915 | // copy the original into that space. |
5916 | // |
5917 | // Also, build up the list of actual arguments to pass in for the call |
5918 | int lValueCount = 0; |
5919 | int rValueCount = 0; |
5920 | std::vector<spv::Id> spvArgs; |
5921 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
5922 | spv::Id arg; |
5923 | if (originalParam(qualifier: qualifiers[a], paramType: *argTypes[a], implicitThisParam: function->hasImplicitThis() && a == 0)) { |
5924 | builder.setAccessChain(lValues[lValueCount]); |
5925 | arg = builder.accessChainGetLValue(); |
5926 | ++lValueCount; |
5927 | } else if (writableParam(qualifier: qualifiers[a])) { |
5928 | // need space to hold the copy |
5929 | arg = builder.createVariable(precision: function->getParamPrecision(param: a), spv::StorageClassFunction, |
5930 | type: builder.getContainedTypeId(typeId: function->getParamType(p: a)), name: "param" ); |
5931 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
5932 | // need to copy the input into output space |
5933 | builder.setAccessChain(lValues[lValueCount]); |
5934 | spv::Id copy = accessChainLoad(type: *argTypes[a]); |
5935 | builder.clearAccessChain(); |
5936 | builder.setAccessChainLValue(arg); |
5937 | multiTypeStore(type: *argTypes[a], rValue: copy); |
5938 | } |
5939 | ++lValueCount; |
5940 | } else { |
5941 | // process r-value, which involves a copy for a type mismatch |
5942 | if (function->getParamType(p: a) != builder.getTypeId(resultId: rValues[rValueCount]) || |
5943 | TranslatePrecisionDecoration(type: *argTypes[a]) != function->getParamPrecision(param: a)) |
5944 | { |
5945 | spv::Id argCopy = builder.createVariable(precision: function->getParamPrecision(param: a), spv::StorageClassFunction, type: function->getParamType(p: a), name: "arg" ); |
5946 | builder.clearAccessChain(); |
5947 | builder.setAccessChainLValue(argCopy); |
5948 | multiTypeStore(type: *argTypes[a], rValue: rValues[rValueCount]); |
5949 | arg = builder.createLoad(lValue: argCopy, precision: function->getParamPrecision(param: a)); |
5950 | } else |
5951 | arg = rValues[rValueCount]; |
5952 | ++rValueCount; |
5953 | } |
5954 | spvArgs.push_back(x: arg); |
5955 | } |
5956 | |
5957 | // 3. Make the call. |
5958 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
5959 | builder.setPrecision(id: result, precision: TranslatePrecisionDecoration(type: node->getType())); |
5960 | builder.addDecoration(result, TranslateNonUniformDecoration(qualifier: node->getType().getQualifier())); |
5961 | |
5962 | // 4. Copy back out an "out" arguments. |
5963 | lValueCount = 0; |
5964 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
5965 | if (originalParam(qualifier: qualifiers[a], paramType: *argTypes[a], implicitThisParam: function->hasImplicitThis() && a == 0)) |
5966 | ++lValueCount; |
5967 | else if (writableParam(qualifier: qualifiers[a])) { |
5968 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
5969 | spv::Id copy = builder.createLoad(lValue: spvArgs[a], precision: spv::NoPrecision); |
5970 | builder.addDecoration(copy, TranslateNonUniformDecoration(qualifier: argTypes[a]->getQualifier())); |
5971 | builder.setAccessChain(lValues[lValueCount]); |
5972 | multiTypeStore(type: *argTypes[a], rValue: copy); |
5973 | } |
5974 | ++lValueCount; |
5975 | } |
5976 | } |
5977 | |
5978 | return result; |
5979 | } |
5980 | |
5981 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
5982 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
5983 | spv::Id typeId, spv::Id left, spv::Id right, |
5984 | glslang::TBasicType typeProxy, bool reduceComparison) |
5985 | { |
5986 | bool isUnsigned = isTypeUnsignedInt(type: typeProxy); |
5987 | bool isFloat = isTypeFloat(type: typeProxy); |
5988 | bool isBool = typeProxy == glslang::EbtBool; |
5989 | |
5990 | spv::Op binOp = spv::OpNop; |
5991 | bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector? |
5992 | bool comparison = false; |
5993 | |
5994 | switch (op) { |
5995 | case glslang::EOpAdd: |
5996 | case glslang::EOpAddAssign: |
5997 | if (isFloat) |
5998 | binOp = spv::OpFAdd; |
5999 | else |
6000 | binOp = spv::OpIAdd; |
6001 | break; |
6002 | case glslang::EOpSub: |
6003 | case glslang::EOpSubAssign: |
6004 | if (isFloat) |
6005 | binOp = spv::OpFSub; |
6006 | else |
6007 | binOp = spv::OpISub; |
6008 | break; |
6009 | case glslang::EOpMul: |
6010 | case glslang::EOpMulAssign: |
6011 | if (isFloat) |
6012 | binOp = spv::OpFMul; |
6013 | else |
6014 | binOp = spv::OpIMul; |
6015 | break; |
6016 | case glslang::EOpVectorTimesScalar: |
6017 | case glslang::EOpVectorTimesScalarAssign: |
6018 | if (isFloat && (builder.isVector(resultId: left) || builder.isVector(resultId: right))) { |
6019 | if (builder.isVector(resultId: right)) |
6020 | std::swap(a&: left, b&: right); |
6021 | assert(builder.isScalar(right)); |
6022 | needMatchingVectors = false; |
6023 | binOp = spv::OpVectorTimesScalar; |
6024 | } else if (isFloat) |
6025 | binOp = spv::OpFMul; |
6026 | else |
6027 | binOp = spv::OpIMul; |
6028 | break; |
6029 | case glslang::EOpVectorTimesMatrix: |
6030 | case glslang::EOpVectorTimesMatrixAssign: |
6031 | binOp = spv::OpVectorTimesMatrix; |
6032 | break; |
6033 | case glslang::EOpMatrixTimesVector: |
6034 | binOp = spv::OpMatrixTimesVector; |
6035 | break; |
6036 | case glslang::EOpMatrixTimesScalar: |
6037 | case glslang::EOpMatrixTimesScalarAssign: |
6038 | binOp = spv::OpMatrixTimesScalar; |
6039 | break; |
6040 | case glslang::EOpMatrixTimesMatrix: |
6041 | case glslang::EOpMatrixTimesMatrixAssign: |
6042 | binOp = spv::OpMatrixTimesMatrix; |
6043 | break; |
6044 | case glslang::EOpOuterProduct: |
6045 | binOp = spv::OpOuterProduct; |
6046 | needMatchingVectors = false; |
6047 | break; |
6048 | |
6049 | case glslang::EOpDiv: |
6050 | case glslang::EOpDivAssign: |
6051 | if (isFloat) |
6052 | binOp = spv::OpFDiv; |
6053 | else if (isUnsigned) |
6054 | binOp = spv::OpUDiv; |
6055 | else |
6056 | binOp = spv::OpSDiv; |
6057 | break; |
6058 | case glslang::EOpMod: |
6059 | case glslang::EOpModAssign: |
6060 | if (isFloat) |
6061 | binOp = spv::OpFMod; |
6062 | else if (isUnsigned) |
6063 | binOp = spv::OpUMod; |
6064 | else |
6065 | binOp = spv::OpSMod; |
6066 | break; |
6067 | case glslang::EOpRightShift: |
6068 | case glslang::EOpRightShiftAssign: |
6069 | if (isUnsigned) |
6070 | binOp = spv::OpShiftRightLogical; |
6071 | else |
6072 | binOp = spv::OpShiftRightArithmetic; |
6073 | break; |
6074 | case glslang::EOpLeftShift: |
6075 | case glslang::EOpLeftShiftAssign: |
6076 | binOp = spv::OpShiftLeftLogical; |
6077 | break; |
6078 | case glslang::EOpAnd: |
6079 | case glslang::EOpAndAssign: |
6080 | binOp = spv::OpBitwiseAnd; |
6081 | break; |
6082 | case glslang::EOpLogicalAnd: |
6083 | needMatchingVectors = false; |
6084 | binOp = spv::OpLogicalAnd; |
6085 | break; |
6086 | case glslang::EOpInclusiveOr: |
6087 | case glslang::EOpInclusiveOrAssign: |
6088 | binOp = spv::OpBitwiseOr; |
6089 | break; |
6090 | case glslang::EOpLogicalOr: |
6091 | needMatchingVectors = false; |
6092 | binOp = spv::OpLogicalOr; |
6093 | break; |
6094 | case glslang::EOpExclusiveOr: |
6095 | case glslang::EOpExclusiveOrAssign: |
6096 | binOp = spv::OpBitwiseXor; |
6097 | break; |
6098 | case glslang::EOpLogicalXor: |
6099 | needMatchingVectors = false; |
6100 | binOp = spv::OpLogicalNotEqual; |
6101 | break; |
6102 | |
6103 | case glslang::EOpAbsDifference: |
6104 | binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
6105 | break; |
6106 | |
6107 | case glslang::EOpAddSaturate: |
6108 | binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
6109 | break; |
6110 | |
6111 | case glslang::EOpSubSaturate: |
6112 | binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
6113 | break; |
6114 | |
6115 | case glslang::EOpAverage: |
6116 | binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
6117 | break; |
6118 | |
6119 | case glslang::EOpAverageRounded: |
6120 | binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
6121 | break; |
6122 | |
6123 | case glslang::EOpMul32x16: |
6124 | binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
6125 | break; |
6126 | |
6127 | case glslang::EOpLessThan: |
6128 | case glslang::EOpGreaterThan: |
6129 | case glslang::EOpLessThanEqual: |
6130 | case glslang::EOpGreaterThanEqual: |
6131 | case glslang::EOpEqual: |
6132 | case glslang::EOpNotEqual: |
6133 | case glslang::EOpVectorEqual: |
6134 | case glslang::EOpVectorNotEqual: |
6135 | comparison = true; |
6136 | break; |
6137 | default: |
6138 | break; |
6139 | } |
6140 | |
6141 | // handle mapped binary operations (should be non-comparison) |
6142 | if (binOp != spv::OpNop) { |
6143 | assert(comparison == false); |
6144 | if (builder.isMatrix(resultId: left) || builder.isMatrix(resultId: right) || |
6145 | builder.isCooperativeMatrix(resultId: left) || builder.isCooperativeMatrix(resultId: right)) |
6146 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
6147 | |
6148 | // No matrix involved; make both operands be the same number of components, if needed |
6149 | if (needMatchingVectors) |
6150 | builder.promoteScalar(precision: decorations.precision, left, right); |
6151 | |
6152 | spv::Id result = builder.createBinOp(binOp, typeId, operand1: left, operand2: right); |
6153 | decorations.addNoContraction(builder, t: result); |
6154 | decorations.addNonUniform(builder, t: result); |
6155 | return builder.setPrecision(id: result, precision: decorations.precision); |
6156 | } |
6157 | |
6158 | if (! comparison) |
6159 | return 0; |
6160 | |
6161 | // Handle comparison instructions |
6162 | |
6163 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
6164 | && (builder.isVector(resultId: left) || builder.isMatrix(resultId: left) || builder.isAggregate(resultId: left))) { |
6165 | spv::Id result = builder.createCompositeCompare(precision: decorations.precision, left, right, op == glslang::EOpEqual); |
6166 | decorations.addNonUniform(builder, t: result); |
6167 | return result; |
6168 | } |
6169 | |
6170 | switch (op) { |
6171 | case glslang::EOpLessThan: |
6172 | if (isFloat) |
6173 | binOp = spv::OpFOrdLessThan; |
6174 | else if (isUnsigned) |
6175 | binOp = spv::OpULessThan; |
6176 | else |
6177 | binOp = spv::OpSLessThan; |
6178 | break; |
6179 | case glslang::EOpGreaterThan: |
6180 | if (isFloat) |
6181 | binOp = spv::OpFOrdGreaterThan; |
6182 | else if (isUnsigned) |
6183 | binOp = spv::OpUGreaterThan; |
6184 | else |
6185 | binOp = spv::OpSGreaterThan; |
6186 | break; |
6187 | case glslang::EOpLessThanEqual: |
6188 | if (isFloat) |
6189 | binOp = spv::OpFOrdLessThanEqual; |
6190 | else if (isUnsigned) |
6191 | binOp = spv::OpULessThanEqual; |
6192 | else |
6193 | binOp = spv::OpSLessThanEqual; |
6194 | break; |
6195 | case glslang::EOpGreaterThanEqual: |
6196 | if (isFloat) |
6197 | binOp = spv::OpFOrdGreaterThanEqual; |
6198 | else if (isUnsigned) |
6199 | binOp = spv::OpUGreaterThanEqual; |
6200 | else |
6201 | binOp = spv::OpSGreaterThanEqual; |
6202 | break; |
6203 | case glslang::EOpEqual: |
6204 | case glslang::EOpVectorEqual: |
6205 | if (isFloat) |
6206 | binOp = spv::OpFOrdEqual; |
6207 | else if (isBool) |
6208 | binOp = spv::OpLogicalEqual; |
6209 | else |
6210 | binOp = spv::OpIEqual; |
6211 | break; |
6212 | case glslang::EOpNotEqual: |
6213 | case glslang::EOpVectorNotEqual: |
6214 | if (isFloat) |
6215 | binOp = spv::OpFUnordNotEqual; |
6216 | else if (isBool) |
6217 | binOp = spv::OpLogicalNotEqual; |
6218 | else |
6219 | binOp = spv::OpINotEqual; |
6220 | break; |
6221 | default: |
6222 | break; |
6223 | } |
6224 | |
6225 | if (binOp != spv::OpNop) { |
6226 | spv::Id result = builder.createBinOp(binOp, typeId, operand1: left, operand2: right); |
6227 | decorations.addNoContraction(builder, t: result); |
6228 | decorations.addNonUniform(builder, t: result); |
6229 | return builder.setPrecision(id: result, precision: decorations.precision); |
6230 | } |
6231 | |
6232 | return 0; |
6233 | } |
6234 | |
6235 | // |
6236 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
6237 | // These can be any of: |
6238 | // |
6239 | // matrix * scalar |
6240 | // scalar * matrix |
6241 | // matrix * matrix linear algebraic |
6242 | // matrix * vector |
6243 | // vector * matrix |
6244 | // matrix * matrix componentwise |
6245 | // matrix op matrix op in {+, -, /} |
6246 | // matrix op scalar op in {+, -, /} |
6247 | // scalar op matrix op in {+, -, /} |
6248 | // |
6249 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
6250 | spv::Id left, spv::Id right) |
6251 | { |
6252 | bool firstClass = true; |
6253 | |
6254 | // First, handle first-class matrix operations (* and matrix/scalar) |
6255 | switch (op) { |
6256 | case spv::OpFDiv: |
6257 | if (builder.isMatrix(resultId: left) && builder.isScalar(resultId: right)) { |
6258 | // turn matrix / scalar into a multiply... |
6259 | spv::Id resultType = builder.getTypeId(resultId: right); |
6260 | right = builder.createBinOp(spv::OpFDiv, typeId: resultType, operand1: builder.makeFpConstant(type: resultType, d: 1.0), operand2: right); |
6261 | op = spv::OpMatrixTimesScalar; |
6262 | } else |
6263 | firstClass = false; |
6264 | break; |
6265 | case spv::OpMatrixTimesScalar: |
6266 | if (builder.isMatrix(resultId: right) || builder.isCooperativeMatrix(resultId: right)) |
6267 | std::swap(a&: left, b&: right); |
6268 | assert(builder.isScalar(right)); |
6269 | break; |
6270 | case spv::OpVectorTimesMatrix: |
6271 | assert(builder.isVector(left)); |
6272 | assert(builder.isMatrix(right)); |
6273 | break; |
6274 | case spv::OpMatrixTimesVector: |
6275 | assert(builder.isMatrix(left)); |
6276 | assert(builder.isVector(right)); |
6277 | break; |
6278 | case spv::OpMatrixTimesMatrix: |
6279 | assert(builder.isMatrix(left)); |
6280 | assert(builder.isMatrix(right)); |
6281 | break; |
6282 | default: |
6283 | firstClass = false; |
6284 | break; |
6285 | } |
6286 | |
6287 | if (builder.isCooperativeMatrix(resultId: left) || builder.isCooperativeMatrix(resultId: right)) |
6288 | firstClass = true; |
6289 | |
6290 | if (firstClass) { |
6291 | spv::Id result = builder.createBinOp(op, typeId, operand1: left, operand2: right); |
6292 | decorations.addNoContraction(builder, t: result); |
6293 | decorations.addNonUniform(builder, t: result); |
6294 | return builder.setPrecision(id: result, precision: decorations.precision); |
6295 | } |
6296 | |
6297 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
6298 | // The result type of all of them is the same type as the (a) matrix operand. |
6299 | // The algorithm is to: |
6300 | // - break the matrix(es) into vectors |
6301 | // - smear any scalar to a vector |
6302 | // - do vector operations |
6303 | // - make a matrix out the vector results |
6304 | switch (op) { |
6305 | case spv::OpFAdd: |
6306 | case spv::OpFSub: |
6307 | case spv::OpFDiv: |
6308 | case spv::OpFMod: |
6309 | case spv::OpFMul: |
6310 | { |
6311 | // one time set up... |
6312 | bool leftMat = builder.isMatrix(resultId: left); |
6313 | bool rightMat = builder.isMatrix(resultId: right); |
6314 | unsigned int numCols = leftMat ? builder.getNumColumns(resultId: left) : builder.getNumColumns(resultId: right); |
6315 | int numRows = leftMat ? builder.getNumRows(resultId: left) : builder.getNumRows(resultId: right); |
6316 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
6317 | spv::Id vecType = builder.makeVectorType(component: scalarType, size: numRows); |
6318 | std::vector<spv::Id> results; |
6319 | spv::Id smearVec = spv::NoResult; |
6320 | if (builder.isScalar(resultId: left)) |
6321 | smearVec = builder.smearScalar(precision: decorations.precision, scalarVal: left, vectorType: vecType); |
6322 | else if (builder.isScalar(resultId: right)) |
6323 | smearVec = builder.smearScalar(precision: decorations.precision, scalarVal: right, vectorType: vecType); |
6324 | |
6325 | // do each vector op |
6326 | for (unsigned int c = 0; c < numCols; ++c) { |
6327 | std::vector<unsigned int> indexes; |
6328 | indexes.push_back(x: c); |
6329 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( composite: left, typeId: vecType, indexes) : smearVec; |
6330 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(composite: right, typeId: vecType, indexes) : smearVec; |
6331 | spv::Id result = builder.createBinOp(op, typeId: vecType, operand1: leftVec, operand2: rightVec); |
6332 | decorations.addNoContraction(builder, t: result); |
6333 | decorations.addNonUniform(builder, t: result); |
6334 | results.push_back(x: builder.setPrecision(id: result, precision: decorations.precision)); |
6335 | } |
6336 | |
6337 | // put the pieces together |
6338 | spv::Id result = builder.setPrecision(id: builder.createCompositeConstruct(typeId, constituents: results), precision: decorations.precision); |
6339 | decorations.addNonUniform(builder, t: result); |
6340 | return result; |
6341 | } |
6342 | default: |
6343 | assert(0); |
6344 | return spv::NoResult; |
6345 | } |
6346 | } |
6347 | |
6348 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
6349 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
6350 | { |
6351 | spv::Op unaryOp = spv::OpNop; |
6352 | int extBuiltins = -1; |
6353 | int libCall = -1; |
6354 | bool isUnsigned = isTypeUnsignedInt(type: typeProxy); |
6355 | bool isFloat = isTypeFloat(type: typeProxy); |
6356 | |
6357 | switch (op) { |
6358 | case glslang::EOpNegative: |
6359 | if (isFloat) { |
6360 | unaryOp = spv::OpFNegate; |
6361 | if (builder.isMatrixType(typeId)) |
6362 | return createUnaryMatrixOperation(op: unaryOp, decorations, typeId, operand, typeProxy); |
6363 | } else |
6364 | unaryOp = spv::OpSNegate; |
6365 | break; |
6366 | |
6367 | case glslang::EOpLogicalNot: |
6368 | case glslang::EOpVectorLogicalNot: |
6369 | unaryOp = spv::OpLogicalNot; |
6370 | break; |
6371 | case glslang::EOpBitwiseNot: |
6372 | unaryOp = spv::OpNot; |
6373 | break; |
6374 | |
6375 | case glslang::EOpDeterminant: |
6376 | libCall = spv::GLSLstd450Determinant; |
6377 | break; |
6378 | case glslang::EOpMatrixInverse: |
6379 | libCall = spv::GLSLstd450MatrixInverse; |
6380 | break; |
6381 | case glslang::EOpTranspose: |
6382 | unaryOp = spv::OpTranspose; |
6383 | break; |
6384 | |
6385 | case glslang::EOpRadians: |
6386 | libCall = spv::GLSLstd450Radians; |
6387 | break; |
6388 | case glslang::EOpDegrees: |
6389 | libCall = spv::GLSLstd450Degrees; |
6390 | break; |
6391 | case glslang::EOpSin: |
6392 | libCall = spv::GLSLstd450Sin; |
6393 | break; |
6394 | case glslang::EOpCos: |
6395 | libCall = spv::GLSLstd450Cos; |
6396 | break; |
6397 | case glslang::EOpTan: |
6398 | libCall = spv::GLSLstd450Tan; |
6399 | break; |
6400 | case glslang::EOpAcos: |
6401 | libCall = spv::GLSLstd450Acos; |
6402 | break; |
6403 | case glslang::EOpAsin: |
6404 | libCall = spv::GLSLstd450Asin; |
6405 | break; |
6406 | case glslang::EOpAtan: |
6407 | libCall = spv::GLSLstd450Atan; |
6408 | break; |
6409 | |
6410 | case glslang::EOpAcosh: |
6411 | libCall = spv::GLSLstd450Acosh; |
6412 | break; |
6413 | case glslang::EOpAsinh: |
6414 | libCall = spv::GLSLstd450Asinh; |
6415 | break; |
6416 | case glslang::EOpAtanh: |
6417 | libCall = spv::GLSLstd450Atanh; |
6418 | break; |
6419 | case glslang::EOpTanh: |
6420 | libCall = spv::GLSLstd450Tanh; |
6421 | break; |
6422 | case glslang::EOpCosh: |
6423 | libCall = spv::GLSLstd450Cosh; |
6424 | break; |
6425 | case glslang::EOpSinh: |
6426 | libCall = spv::GLSLstd450Sinh; |
6427 | break; |
6428 | |
6429 | case glslang::EOpLength: |
6430 | libCall = spv::GLSLstd450Length; |
6431 | break; |
6432 | case glslang::EOpNormalize: |
6433 | libCall = spv::GLSLstd450Normalize; |
6434 | break; |
6435 | |
6436 | case glslang::EOpExp: |
6437 | libCall = spv::GLSLstd450Exp; |
6438 | break; |
6439 | case glslang::EOpLog: |
6440 | libCall = spv::GLSLstd450Log; |
6441 | break; |
6442 | case glslang::EOpExp2: |
6443 | libCall = spv::GLSLstd450Exp2; |
6444 | break; |
6445 | case glslang::EOpLog2: |
6446 | libCall = spv::GLSLstd450Log2; |
6447 | break; |
6448 | case glslang::EOpSqrt: |
6449 | libCall = spv::GLSLstd450Sqrt; |
6450 | break; |
6451 | case glslang::EOpInverseSqrt: |
6452 | libCall = spv::GLSLstd450InverseSqrt; |
6453 | break; |
6454 | |
6455 | case glslang::EOpFloor: |
6456 | libCall = spv::GLSLstd450Floor; |
6457 | break; |
6458 | case glslang::EOpTrunc: |
6459 | libCall = spv::GLSLstd450Trunc; |
6460 | break; |
6461 | case glslang::EOpRound: |
6462 | libCall = spv::GLSLstd450Round; |
6463 | break; |
6464 | case glslang::EOpRoundEven: |
6465 | libCall = spv::GLSLstd450RoundEven; |
6466 | break; |
6467 | case glslang::EOpCeil: |
6468 | libCall = spv::GLSLstd450Ceil; |
6469 | break; |
6470 | case glslang::EOpFract: |
6471 | libCall = spv::GLSLstd450Fract; |
6472 | break; |
6473 | |
6474 | case glslang::EOpIsNan: |
6475 | unaryOp = spv::OpIsNan; |
6476 | break; |
6477 | case glslang::EOpIsInf: |
6478 | unaryOp = spv::OpIsInf; |
6479 | break; |
6480 | case glslang::EOpIsFinite: |
6481 | unaryOp = spv::OpIsFinite; |
6482 | break; |
6483 | |
6484 | case glslang::EOpFloatBitsToInt: |
6485 | case glslang::EOpFloatBitsToUint: |
6486 | case glslang::EOpIntBitsToFloat: |
6487 | case glslang::EOpUintBitsToFloat: |
6488 | case glslang::EOpDoubleBitsToInt64: |
6489 | case glslang::EOpDoubleBitsToUint64: |
6490 | case glslang::EOpInt64BitsToDouble: |
6491 | case glslang::EOpUint64BitsToDouble: |
6492 | case glslang::EOpFloat16BitsToInt16: |
6493 | case glslang::EOpFloat16BitsToUint16: |
6494 | case glslang::EOpInt16BitsToFloat16: |
6495 | case glslang::EOpUint16BitsToFloat16: |
6496 | unaryOp = spv::OpBitcast; |
6497 | break; |
6498 | |
6499 | case glslang::EOpPackSnorm2x16: |
6500 | libCall = spv::GLSLstd450PackSnorm2x16; |
6501 | break; |
6502 | case glslang::EOpUnpackSnorm2x16: |
6503 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
6504 | break; |
6505 | case glslang::EOpPackUnorm2x16: |
6506 | libCall = spv::GLSLstd450PackUnorm2x16; |
6507 | break; |
6508 | case glslang::EOpUnpackUnorm2x16: |
6509 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
6510 | break; |
6511 | case glslang::EOpPackHalf2x16: |
6512 | libCall = spv::GLSLstd450PackHalf2x16; |
6513 | break; |
6514 | case glslang::EOpUnpackHalf2x16: |
6515 | libCall = spv::GLSLstd450UnpackHalf2x16; |
6516 | break; |
6517 | #ifndef GLSLANG_WEB |
6518 | case glslang::EOpPackSnorm4x8: |
6519 | libCall = spv::GLSLstd450PackSnorm4x8; |
6520 | break; |
6521 | case glslang::EOpUnpackSnorm4x8: |
6522 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
6523 | break; |
6524 | case glslang::EOpPackUnorm4x8: |
6525 | libCall = spv::GLSLstd450PackUnorm4x8; |
6526 | break; |
6527 | case glslang::EOpUnpackUnorm4x8: |
6528 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
6529 | break; |
6530 | case glslang::EOpPackDouble2x32: |
6531 | libCall = spv::GLSLstd450PackDouble2x32; |
6532 | break; |
6533 | case glslang::EOpUnpackDouble2x32: |
6534 | libCall = spv::GLSLstd450UnpackDouble2x32; |
6535 | break; |
6536 | #endif |
6537 | |
6538 | case glslang::EOpPackInt2x32: |
6539 | case glslang::EOpUnpackInt2x32: |
6540 | case glslang::EOpPackUint2x32: |
6541 | case glslang::EOpUnpackUint2x32: |
6542 | case glslang::EOpPack16: |
6543 | case glslang::EOpPack32: |
6544 | case glslang::EOpPack64: |
6545 | case glslang::EOpUnpack32: |
6546 | case glslang::EOpUnpack16: |
6547 | case glslang::EOpUnpack8: |
6548 | case glslang::EOpPackInt2x16: |
6549 | case glslang::EOpUnpackInt2x16: |
6550 | case glslang::EOpPackUint2x16: |
6551 | case glslang::EOpUnpackUint2x16: |
6552 | case glslang::EOpPackInt4x16: |
6553 | case glslang::EOpUnpackInt4x16: |
6554 | case glslang::EOpPackUint4x16: |
6555 | case glslang::EOpUnpackUint4x16: |
6556 | case glslang::EOpPackFloat2x16: |
6557 | case glslang::EOpUnpackFloat2x16: |
6558 | unaryOp = spv::OpBitcast; |
6559 | break; |
6560 | |
6561 | case glslang::EOpDPdx: |
6562 | unaryOp = spv::OpDPdx; |
6563 | break; |
6564 | case glslang::EOpDPdy: |
6565 | unaryOp = spv::OpDPdy; |
6566 | break; |
6567 | case glslang::EOpFwidth: |
6568 | unaryOp = spv::OpFwidth; |
6569 | break; |
6570 | |
6571 | case glslang::EOpAny: |
6572 | unaryOp = spv::OpAny; |
6573 | break; |
6574 | case glslang::EOpAll: |
6575 | unaryOp = spv::OpAll; |
6576 | break; |
6577 | |
6578 | case glslang::EOpAbs: |
6579 | if (isFloat) |
6580 | libCall = spv::GLSLstd450FAbs; |
6581 | else |
6582 | libCall = spv::GLSLstd450SAbs; |
6583 | break; |
6584 | case glslang::EOpSign: |
6585 | if (isFloat) |
6586 | libCall = spv::GLSLstd450FSign; |
6587 | else |
6588 | libCall = spv::GLSLstd450SSign; |
6589 | break; |
6590 | |
6591 | #ifndef GLSLANG_WEB |
6592 | case glslang::EOpDPdxFine: |
6593 | unaryOp = spv::OpDPdxFine; |
6594 | break; |
6595 | case glslang::EOpDPdyFine: |
6596 | unaryOp = spv::OpDPdyFine; |
6597 | break; |
6598 | case glslang::EOpFwidthFine: |
6599 | unaryOp = spv::OpFwidthFine; |
6600 | break; |
6601 | case glslang::EOpDPdxCoarse: |
6602 | unaryOp = spv::OpDPdxCoarse; |
6603 | break; |
6604 | case glslang::EOpDPdyCoarse: |
6605 | unaryOp = spv::OpDPdyCoarse; |
6606 | break; |
6607 | case glslang::EOpFwidthCoarse: |
6608 | unaryOp = spv::OpFwidthCoarse; |
6609 | break; |
6610 | case glslang::EOpRayQueryProceed: |
6611 | unaryOp = spv::OpRayQueryProceedKHR; |
6612 | break; |
6613 | case glslang::EOpRayQueryGetRayTMin: |
6614 | unaryOp = spv::OpRayQueryGetRayTMinKHR; |
6615 | break; |
6616 | case glslang::EOpRayQueryGetRayFlags: |
6617 | unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
6618 | break; |
6619 | case glslang::EOpRayQueryGetWorldRayOrigin: |
6620 | unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
6621 | break; |
6622 | case glslang::EOpRayQueryGetWorldRayDirection: |
6623 | unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
6624 | break; |
6625 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
6626 | unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
6627 | break; |
6628 | case glslang::EOpInterpolateAtCentroid: |
6629 | if (typeProxy == glslang::EbtFloat16) |
6630 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_half_float); |
6631 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
6632 | break; |
6633 | case glslang::EOpAtomicCounterIncrement: |
6634 | case glslang::EOpAtomicCounterDecrement: |
6635 | case glslang::EOpAtomicCounter: |
6636 | { |
6637 | // Handle all of the atomics in one place, in createAtomicOperation() |
6638 | std::vector<spv::Id> operands; |
6639 | operands.push_back(x: operand); |
6640 | return createAtomicOperation(op, precision: decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
6641 | } |
6642 | |
6643 | case glslang::EOpBitFieldReverse: |
6644 | unaryOp = spv::OpBitReverse; |
6645 | break; |
6646 | case glslang::EOpBitCount: |
6647 | unaryOp = spv::OpBitCount; |
6648 | break; |
6649 | case glslang::EOpFindLSB: |
6650 | libCall = spv::GLSLstd450FindILsb; |
6651 | break; |
6652 | case glslang::EOpFindMSB: |
6653 | if (isUnsigned) |
6654 | libCall = spv::GLSLstd450FindUMsb; |
6655 | else |
6656 | libCall = spv::GLSLstd450FindSMsb; |
6657 | break; |
6658 | |
6659 | case glslang::EOpCountLeadingZeros: |
6660 | builder.addCapability(cap: spv::CapabilityIntegerFunctions2INTEL); |
6661 | builder.addExtension(ext: "SPV_INTEL_shader_integer_functions2" ); |
6662 | unaryOp = spv::OpUCountLeadingZerosINTEL; |
6663 | break; |
6664 | |
6665 | case glslang::EOpCountTrailingZeros: |
6666 | builder.addCapability(cap: spv::CapabilityIntegerFunctions2INTEL); |
6667 | builder.addExtension(ext: "SPV_INTEL_shader_integer_functions2" ); |
6668 | unaryOp = spv::OpUCountTrailingZerosINTEL; |
6669 | break; |
6670 | |
6671 | case glslang::EOpBallot: |
6672 | case glslang::EOpReadFirstInvocation: |
6673 | case glslang::EOpAnyInvocation: |
6674 | case glslang::EOpAllInvocations: |
6675 | case glslang::EOpAllInvocationsEqual: |
6676 | case glslang::EOpMinInvocations: |
6677 | case glslang::EOpMaxInvocations: |
6678 | case glslang::EOpAddInvocations: |
6679 | case glslang::EOpMinInvocationsNonUniform: |
6680 | case glslang::EOpMaxInvocationsNonUniform: |
6681 | case glslang::EOpAddInvocationsNonUniform: |
6682 | case glslang::EOpMinInvocationsInclusiveScan: |
6683 | case glslang::EOpMaxInvocationsInclusiveScan: |
6684 | case glslang::EOpAddInvocationsInclusiveScan: |
6685 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
6686 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
6687 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
6688 | case glslang::EOpMinInvocationsExclusiveScan: |
6689 | case glslang::EOpMaxInvocationsExclusiveScan: |
6690 | case glslang::EOpAddInvocationsExclusiveScan: |
6691 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
6692 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
6693 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
6694 | { |
6695 | std::vector<spv::Id> operands; |
6696 | operands.push_back(x: operand); |
6697 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
6698 | } |
6699 | case glslang::EOpSubgroupAll: |
6700 | case glslang::EOpSubgroupAny: |
6701 | case glslang::EOpSubgroupAllEqual: |
6702 | case glslang::EOpSubgroupBroadcastFirst: |
6703 | case glslang::EOpSubgroupBallot: |
6704 | case glslang::EOpSubgroupInverseBallot: |
6705 | case glslang::EOpSubgroupBallotBitCount: |
6706 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
6707 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
6708 | case glslang::EOpSubgroupBallotFindLSB: |
6709 | case glslang::EOpSubgroupBallotFindMSB: |
6710 | case glslang::EOpSubgroupAdd: |
6711 | case glslang::EOpSubgroupMul: |
6712 | case glslang::EOpSubgroupMin: |
6713 | case glslang::EOpSubgroupMax: |
6714 | case glslang::EOpSubgroupAnd: |
6715 | case glslang::EOpSubgroupOr: |
6716 | case glslang::EOpSubgroupXor: |
6717 | case glslang::EOpSubgroupInclusiveAdd: |
6718 | case glslang::EOpSubgroupInclusiveMul: |
6719 | case glslang::EOpSubgroupInclusiveMin: |
6720 | case glslang::EOpSubgroupInclusiveMax: |
6721 | case glslang::EOpSubgroupInclusiveAnd: |
6722 | case glslang::EOpSubgroupInclusiveOr: |
6723 | case glslang::EOpSubgroupInclusiveXor: |
6724 | case glslang::EOpSubgroupExclusiveAdd: |
6725 | case glslang::EOpSubgroupExclusiveMul: |
6726 | case glslang::EOpSubgroupExclusiveMin: |
6727 | case glslang::EOpSubgroupExclusiveMax: |
6728 | case glslang::EOpSubgroupExclusiveAnd: |
6729 | case glslang::EOpSubgroupExclusiveOr: |
6730 | case glslang::EOpSubgroupExclusiveXor: |
6731 | case glslang::EOpSubgroupQuadSwapHorizontal: |
6732 | case glslang::EOpSubgroupQuadSwapVertical: |
6733 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
6734 | std::vector<spv::Id> operands; |
6735 | operands.push_back(x: operand); |
6736 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
6737 | } |
6738 | case glslang::EOpMbcnt: |
6739 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_ballot); |
6740 | libCall = spv::MbcntAMD; |
6741 | break; |
6742 | |
6743 | case glslang::EOpCubeFaceIndex: |
6744 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_gcn_shader); |
6745 | libCall = spv::CubeFaceIndexAMD; |
6746 | break; |
6747 | |
6748 | case glslang::EOpCubeFaceCoord: |
6749 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_gcn_shader); |
6750 | libCall = spv::CubeFaceCoordAMD; |
6751 | break; |
6752 | case glslang::EOpSubgroupPartition: |
6753 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
6754 | break; |
6755 | case glslang::EOpConstructReference: |
6756 | unaryOp = spv::OpBitcast; |
6757 | break; |
6758 | |
6759 | case glslang::EOpConvUint64ToAccStruct: |
6760 | case glslang::EOpConvUvec2ToAccStruct: |
6761 | unaryOp = spv::OpConvertUToAccelerationStructureKHR; |
6762 | break; |
6763 | #endif |
6764 | |
6765 | case glslang::EOpCopyObject: |
6766 | unaryOp = spv::OpCopyObject; |
6767 | break; |
6768 | |
6769 | default: |
6770 | return 0; |
6771 | } |
6772 | |
6773 | spv::Id id; |
6774 | if (libCall >= 0) { |
6775 | std::vector<spv::Id> args; |
6776 | args.push_back(x: operand); |
6777 | id = builder.createBuiltinCall(resultType: typeId, builtins: extBuiltins >= 0 ? extBuiltins : stdBuiltins, entryPoint: libCall, args); |
6778 | } else { |
6779 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
6780 | } |
6781 | |
6782 | decorations.addNoContraction(builder, t: id); |
6783 | decorations.addNonUniform(builder, t: id); |
6784 | return builder.setPrecision(id, precision: decorations.precision); |
6785 | } |
6786 | |
6787 | // Create a unary operation on a matrix |
6788 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
6789 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
6790 | { |
6791 | // Handle unary operations vector by vector. |
6792 | // The result type is the same type as the original type. |
6793 | // The algorithm is to: |
6794 | // - break the matrix into vectors |
6795 | // - apply the operation to each vector |
6796 | // - make a matrix out the vector results |
6797 | |
6798 | // get the types sorted out |
6799 | int numCols = builder.getNumColumns(resultId: operand); |
6800 | int numRows = builder.getNumRows(resultId: operand); |
6801 | spv::Id srcVecType = builder.makeVectorType(component: builder.getScalarTypeId(typeId: builder.getTypeId(resultId: operand)), size: numRows); |
6802 | spv::Id destVecType = builder.makeVectorType(component: builder.getScalarTypeId(typeId), size: numRows); |
6803 | std::vector<spv::Id> results; |
6804 | |
6805 | // do each vector op |
6806 | for (int c = 0; c < numCols; ++c) { |
6807 | std::vector<unsigned int> indexes; |
6808 | indexes.push_back(x: c); |
6809 | spv::Id srcVec = builder.createCompositeExtract(composite: operand, typeId: srcVecType, indexes); |
6810 | spv::Id destVec = builder.createUnaryOp(op, typeId: destVecType, operand: srcVec); |
6811 | decorations.addNoContraction(builder, t: destVec); |
6812 | decorations.addNonUniform(builder, t: destVec); |
6813 | results.push_back(x: builder.setPrecision(id: destVec, precision: decorations.precision)); |
6814 | } |
6815 | |
6816 | // put the pieces together |
6817 | spv::Id result = builder.setPrecision(id: builder.createCompositeConstruct(typeId, constituents: results), precision: decorations.precision); |
6818 | decorations.addNonUniform(builder, t: result); |
6819 | return result; |
6820 | } |
6821 | |
6822 | // For converting integers where both the bitwidth and the signedness could |
6823 | // change, but only do the width change here. The caller is still responsible |
6824 | // for the signedness conversion. |
6825 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
6826 | { |
6827 | // Get the result type width, based on the type to convert to. |
6828 | int width = 32; |
6829 | switch(op) { |
6830 | case glslang::EOpConvInt16ToUint8: |
6831 | case glslang::EOpConvIntToUint8: |
6832 | case glslang::EOpConvInt64ToUint8: |
6833 | case glslang::EOpConvUint16ToInt8: |
6834 | case glslang::EOpConvUintToInt8: |
6835 | case glslang::EOpConvUint64ToInt8: |
6836 | width = 8; |
6837 | break; |
6838 | case glslang::EOpConvInt8ToUint16: |
6839 | case glslang::EOpConvIntToUint16: |
6840 | case glslang::EOpConvInt64ToUint16: |
6841 | case glslang::EOpConvUint8ToInt16: |
6842 | case glslang::EOpConvUintToInt16: |
6843 | case glslang::EOpConvUint64ToInt16: |
6844 | width = 16; |
6845 | break; |
6846 | case glslang::EOpConvInt8ToUint: |
6847 | case glslang::EOpConvInt16ToUint: |
6848 | case glslang::EOpConvInt64ToUint: |
6849 | case glslang::EOpConvUint8ToInt: |
6850 | case glslang::EOpConvUint16ToInt: |
6851 | case glslang::EOpConvUint64ToInt: |
6852 | width = 32; |
6853 | break; |
6854 | case glslang::EOpConvInt8ToUint64: |
6855 | case glslang::EOpConvInt16ToUint64: |
6856 | case glslang::EOpConvIntToUint64: |
6857 | case glslang::EOpConvUint8ToInt64: |
6858 | case glslang::EOpConvUint16ToInt64: |
6859 | case glslang::EOpConvUintToInt64: |
6860 | width = 64; |
6861 | break; |
6862 | |
6863 | default: |
6864 | assert(false && "Default missing" ); |
6865 | break; |
6866 | } |
6867 | |
6868 | // Get the conversion operation and result type, |
6869 | // based on the target width, but the source type. |
6870 | spv::Id type = spv::NoType; |
6871 | spv::Op convOp = spv::OpNop; |
6872 | switch(op) { |
6873 | case glslang::EOpConvInt8ToUint16: |
6874 | case glslang::EOpConvInt8ToUint: |
6875 | case glslang::EOpConvInt8ToUint64: |
6876 | case glslang::EOpConvInt16ToUint8: |
6877 | case glslang::EOpConvInt16ToUint: |
6878 | case glslang::EOpConvInt16ToUint64: |
6879 | case glslang::EOpConvIntToUint8: |
6880 | case glslang::EOpConvIntToUint16: |
6881 | case glslang::EOpConvIntToUint64: |
6882 | case glslang::EOpConvInt64ToUint8: |
6883 | case glslang::EOpConvInt64ToUint16: |
6884 | case glslang::EOpConvInt64ToUint: |
6885 | convOp = spv::OpSConvert; |
6886 | type = builder.makeIntType(width); |
6887 | break; |
6888 | default: |
6889 | convOp = spv::OpUConvert; |
6890 | type = builder.makeUintType(width); |
6891 | break; |
6892 | } |
6893 | |
6894 | if (vectorSize > 0) |
6895 | type = builder.makeVectorType(component: type, size: vectorSize); |
6896 | |
6897 | return builder.createUnaryOp(convOp, typeId: type, operand); |
6898 | } |
6899 | |
6900 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
6901 | spv::Id operand, glslang::TBasicType typeProxy) |
6902 | { |
6903 | spv::Op convOp = spv::OpNop; |
6904 | spv::Id zero = 0; |
6905 | spv::Id one = 0; |
6906 | |
6907 | int vectorSize = builder.isVectorType(typeId: destType) ? builder.getNumTypeComponents(typeId: destType) : 0; |
6908 | |
6909 | switch (op) { |
6910 | case glslang::EOpConvIntToBool: |
6911 | case glslang::EOpConvUintToBool: |
6912 | zero = builder.makeUintConstant(u: 0); |
6913 | zero = makeSmearedConstant(constant: zero, vectorSize); |
6914 | return builder.createBinOp(spv::OpINotEqual, typeId: destType, operand1: operand, operand2: zero); |
6915 | case glslang::EOpConvFloatToBool: |
6916 | zero = builder.makeFloatConstant(f: 0.0F); |
6917 | zero = makeSmearedConstant(constant: zero, vectorSize); |
6918 | return builder.createBinOp(spv::OpFUnordNotEqual, typeId: destType, operand1: operand, operand2: zero); |
6919 | case glslang::EOpConvBoolToFloat: |
6920 | convOp = spv::OpSelect; |
6921 | zero = builder.makeFloatConstant(f: 0.0F); |
6922 | one = builder.makeFloatConstant(f: 1.0F); |
6923 | break; |
6924 | |
6925 | case glslang::EOpConvBoolToInt: |
6926 | case glslang::EOpConvBoolToInt64: |
6927 | #ifndef GLSLANG_WEB |
6928 | if (op == glslang::EOpConvBoolToInt64) { |
6929 | zero = builder.makeInt64Constant(i: 0); |
6930 | one = builder.makeInt64Constant(i: 1); |
6931 | } else |
6932 | #endif |
6933 | { |
6934 | zero = builder.makeIntConstant(i: 0); |
6935 | one = builder.makeIntConstant(i: 1); |
6936 | } |
6937 | |
6938 | convOp = spv::OpSelect; |
6939 | break; |
6940 | |
6941 | case glslang::EOpConvBoolToUint: |
6942 | case glslang::EOpConvBoolToUint64: |
6943 | #ifndef GLSLANG_WEB |
6944 | if (op == glslang::EOpConvBoolToUint64) { |
6945 | zero = builder.makeUint64Constant(u: 0); |
6946 | one = builder.makeUint64Constant(u: 1); |
6947 | } else |
6948 | #endif |
6949 | { |
6950 | zero = builder.makeUintConstant(u: 0); |
6951 | one = builder.makeUintConstant(u: 1); |
6952 | } |
6953 | |
6954 | convOp = spv::OpSelect; |
6955 | break; |
6956 | |
6957 | case glslang::EOpConvInt8ToFloat16: |
6958 | case glslang::EOpConvInt8ToFloat: |
6959 | case glslang::EOpConvInt8ToDouble: |
6960 | case glslang::EOpConvInt16ToFloat16: |
6961 | case glslang::EOpConvInt16ToFloat: |
6962 | case glslang::EOpConvInt16ToDouble: |
6963 | case glslang::EOpConvIntToFloat16: |
6964 | case glslang::EOpConvIntToFloat: |
6965 | case glslang::EOpConvIntToDouble: |
6966 | case glslang::EOpConvInt64ToFloat: |
6967 | case glslang::EOpConvInt64ToDouble: |
6968 | case glslang::EOpConvInt64ToFloat16: |
6969 | convOp = spv::OpConvertSToF; |
6970 | break; |
6971 | |
6972 | case glslang::EOpConvUint8ToFloat16: |
6973 | case glslang::EOpConvUint8ToFloat: |
6974 | case glslang::EOpConvUint8ToDouble: |
6975 | case glslang::EOpConvUint16ToFloat16: |
6976 | case glslang::EOpConvUint16ToFloat: |
6977 | case glslang::EOpConvUint16ToDouble: |
6978 | case glslang::EOpConvUintToFloat16: |
6979 | case glslang::EOpConvUintToFloat: |
6980 | case glslang::EOpConvUintToDouble: |
6981 | case glslang::EOpConvUint64ToFloat: |
6982 | case glslang::EOpConvUint64ToDouble: |
6983 | case glslang::EOpConvUint64ToFloat16: |
6984 | convOp = spv::OpConvertUToF; |
6985 | break; |
6986 | |
6987 | case glslang::EOpConvFloat16ToInt8: |
6988 | case glslang::EOpConvFloatToInt8: |
6989 | case glslang::EOpConvDoubleToInt8: |
6990 | case glslang::EOpConvFloat16ToInt16: |
6991 | case glslang::EOpConvFloatToInt16: |
6992 | case glslang::EOpConvDoubleToInt16: |
6993 | case glslang::EOpConvFloat16ToInt: |
6994 | case glslang::EOpConvFloatToInt: |
6995 | case glslang::EOpConvDoubleToInt: |
6996 | case glslang::EOpConvFloat16ToInt64: |
6997 | case glslang::EOpConvFloatToInt64: |
6998 | case glslang::EOpConvDoubleToInt64: |
6999 | convOp = spv::OpConvertFToS; |
7000 | break; |
7001 | |
7002 | case glslang::EOpConvUint8ToInt8: |
7003 | case glslang::EOpConvInt8ToUint8: |
7004 | case glslang::EOpConvUint16ToInt16: |
7005 | case glslang::EOpConvInt16ToUint16: |
7006 | case glslang::EOpConvUintToInt: |
7007 | case glslang::EOpConvIntToUint: |
7008 | case glslang::EOpConvUint64ToInt64: |
7009 | case glslang::EOpConvInt64ToUint64: |
7010 | if (builder.isInSpecConstCodeGenMode()) { |
7011 | // Build zero scalar or vector for OpIAdd. |
7012 | #ifndef GLSLANG_WEB |
7013 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
7014 | zero = builder.makeUint8Constant(u: 0); |
7015 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
7016 | zero = builder.makeUint16Constant(u: 0); |
7017 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
7018 | zero = builder.makeUint64Constant(u: 0); |
7019 | } else |
7020 | #endif |
7021 | { |
7022 | zero = builder.makeUintConstant(u: 0); |
7023 | } |
7024 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7025 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
7026 | // generating for OpSpecConstantOp instruction. |
7027 | return builder.createBinOp(spv::OpIAdd, typeId: destType, operand1: operand, operand2: zero); |
7028 | } |
7029 | // For normal run-time conversion instruction, use OpBitcast. |
7030 | convOp = spv::OpBitcast; |
7031 | break; |
7032 | |
7033 | case glslang::EOpConvFloat16ToUint8: |
7034 | case glslang::EOpConvFloatToUint8: |
7035 | case glslang::EOpConvDoubleToUint8: |
7036 | case glslang::EOpConvFloat16ToUint16: |
7037 | case glslang::EOpConvFloatToUint16: |
7038 | case glslang::EOpConvDoubleToUint16: |
7039 | case glslang::EOpConvFloat16ToUint: |
7040 | case glslang::EOpConvFloatToUint: |
7041 | case glslang::EOpConvDoubleToUint: |
7042 | case glslang::EOpConvFloatToUint64: |
7043 | case glslang::EOpConvDoubleToUint64: |
7044 | case glslang::EOpConvFloat16ToUint64: |
7045 | convOp = spv::OpConvertFToU; |
7046 | break; |
7047 | |
7048 | #ifndef GLSLANG_WEB |
7049 | case glslang::EOpConvInt8ToBool: |
7050 | case glslang::EOpConvUint8ToBool: |
7051 | zero = builder.makeUint8Constant(u: 0); |
7052 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7053 | return builder.createBinOp(spv::OpINotEqual, typeId: destType, operand1: operand, operand2: zero); |
7054 | case glslang::EOpConvInt16ToBool: |
7055 | case glslang::EOpConvUint16ToBool: |
7056 | zero = builder.makeUint16Constant(u: 0); |
7057 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7058 | return builder.createBinOp(spv::OpINotEqual, typeId: destType, operand1: operand, operand2: zero); |
7059 | case glslang::EOpConvInt64ToBool: |
7060 | case glslang::EOpConvUint64ToBool: |
7061 | zero = builder.makeUint64Constant(u: 0); |
7062 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7063 | return builder.createBinOp(spv::OpINotEqual, typeId: destType, operand1: operand, operand2: zero); |
7064 | case glslang::EOpConvDoubleToBool: |
7065 | zero = builder.makeDoubleConstant(d: 0.0); |
7066 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7067 | return builder.createBinOp(spv::OpFUnordNotEqual, typeId: destType, operand1: operand, operand2: zero); |
7068 | case glslang::EOpConvFloat16ToBool: |
7069 | zero = builder.makeFloat16Constant(f16: 0.0F); |
7070 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7071 | return builder.createBinOp(spv::OpFUnordNotEqual, typeId: destType, operand1: operand, operand2: zero); |
7072 | case glslang::EOpConvBoolToDouble: |
7073 | convOp = spv::OpSelect; |
7074 | zero = builder.makeDoubleConstant(d: 0.0); |
7075 | one = builder.makeDoubleConstant(d: 1.0); |
7076 | break; |
7077 | case glslang::EOpConvBoolToFloat16: |
7078 | convOp = spv::OpSelect; |
7079 | zero = builder.makeFloat16Constant(f16: 0.0F); |
7080 | one = builder.makeFloat16Constant(f16: 1.0F); |
7081 | break; |
7082 | case glslang::EOpConvBoolToInt8: |
7083 | zero = builder.makeInt8Constant(i: 0); |
7084 | one = builder.makeInt8Constant(i: 1); |
7085 | convOp = spv::OpSelect; |
7086 | break; |
7087 | case glslang::EOpConvBoolToUint8: |
7088 | zero = builder.makeUint8Constant(u: 0); |
7089 | one = builder.makeUint8Constant(u: 1); |
7090 | convOp = spv::OpSelect; |
7091 | break; |
7092 | case glslang::EOpConvBoolToInt16: |
7093 | zero = builder.makeInt16Constant(i: 0); |
7094 | one = builder.makeInt16Constant(i: 1); |
7095 | convOp = spv::OpSelect; |
7096 | break; |
7097 | case glslang::EOpConvBoolToUint16: |
7098 | zero = builder.makeUint16Constant(u: 0); |
7099 | one = builder.makeUint16Constant(u: 1); |
7100 | convOp = spv::OpSelect; |
7101 | break; |
7102 | case glslang::EOpConvDoubleToFloat: |
7103 | case glslang::EOpConvFloatToDouble: |
7104 | case glslang::EOpConvDoubleToFloat16: |
7105 | case glslang::EOpConvFloat16ToDouble: |
7106 | case glslang::EOpConvFloatToFloat16: |
7107 | case glslang::EOpConvFloat16ToFloat: |
7108 | convOp = spv::OpFConvert; |
7109 | if (builder.isMatrixType(typeId: destType)) |
7110 | return createUnaryMatrixOperation(op: convOp, decorations, typeId: destType, operand, typeProxy); |
7111 | break; |
7112 | |
7113 | case glslang::EOpConvInt8ToInt16: |
7114 | case glslang::EOpConvInt8ToInt: |
7115 | case glslang::EOpConvInt8ToInt64: |
7116 | case glslang::EOpConvInt16ToInt8: |
7117 | case glslang::EOpConvInt16ToInt: |
7118 | case glslang::EOpConvInt16ToInt64: |
7119 | case glslang::EOpConvIntToInt8: |
7120 | case glslang::EOpConvIntToInt16: |
7121 | case glslang::EOpConvIntToInt64: |
7122 | case glslang::EOpConvInt64ToInt8: |
7123 | case glslang::EOpConvInt64ToInt16: |
7124 | case glslang::EOpConvInt64ToInt: |
7125 | convOp = spv::OpSConvert; |
7126 | break; |
7127 | |
7128 | case glslang::EOpConvUint8ToUint16: |
7129 | case glslang::EOpConvUint8ToUint: |
7130 | case glslang::EOpConvUint8ToUint64: |
7131 | case glslang::EOpConvUint16ToUint8: |
7132 | case glslang::EOpConvUint16ToUint: |
7133 | case glslang::EOpConvUint16ToUint64: |
7134 | case glslang::EOpConvUintToUint8: |
7135 | case glslang::EOpConvUintToUint16: |
7136 | case glslang::EOpConvUintToUint64: |
7137 | case glslang::EOpConvUint64ToUint8: |
7138 | case glslang::EOpConvUint64ToUint16: |
7139 | case glslang::EOpConvUint64ToUint: |
7140 | convOp = spv::OpUConvert; |
7141 | break; |
7142 | |
7143 | case glslang::EOpConvInt8ToUint16: |
7144 | case glslang::EOpConvInt8ToUint: |
7145 | case glslang::EOpConvInt8ToUint64: |
7146 | case glslang::EOpConvInt16ToUint8: |
7147 | case glslang::EOpConvInt16ToUint: |
7148 | case glslang::EOpConvInt16ToUint64: |
7149 | case glslang::EOpConvIntToUint8: |
7150 | case glslang::EOpConvIntToUint16: |
7151 | case glslang::EOpConvIntToUint64: |
7152 | case glslang::EOpConvInt64ToUint8: |
7153 | case glslang::EOpConvInt64ToUint16: |
7154 | case glslang::EOpConvInt64ToUint: |
7155 | case glslang::EOpConvUint8ToInt16: |
7156 | case glslang::EOpConvUint8ToInt: |
7157 | case glslang::EOpConvUint8ToInt64: |
7158 | case glslang::EOpConvUint16ToInt8: |
7159 | case glslang::EOpConvUint16ToInt: |
7160 | case glslang::EOpConvUint16ToInt64: |
7161 | case glslang::EOpConvUintToInt8: |
7162 | case glslang::EOpConvUintToInt16: |
7163 | case glslang::EOpConvUintToInt64: |
7164 | case glslang::EOpConvUint64ToInt8: |
7165 | case glslang::EOpConvUint64ToInt16: |
7166 | case glslang::EOpConvUint64ToInt: |
7167 | // OpSConvert/OpUConvert + OpBitCast |
7168 | operand = createIntWidthConversion(op, operand, vectorSize); |
7169 | |
7170 | if (builder.isInSpecConstCodeGenMode()) { |
7171 | // Build zero scalar or vector for OpIAdd. |
7172 | switch(op) { |
7173 | case glslang::EOpConvInt16ToUint8: |
7174 | case glslang::EOpConvIntToUint8: |
7175 | case glslang::EOpConvInt64ToUint8: |
7176 | case glslang::EOpConvUint16ToInt8: |
7177 | case glslang::EOpConvUintToInt8: |
7178 | case glslang::EOpConvUint64ToInt8: |
7179 | zero = builder.makeUint8Constant(u: 0); |
7180 | break; |
7181 | case glslang::EOpConvInt8ToUint16: |
7182 | case glslang::EOpConvIntToUint16: |
7183 | case glslang::EOpConvInt64ToUint16: |
7184 | case glslang::EOpConvUint8ToInt16: |
7185 | case glslang::EOpConvUintToInt16: |
7186 | case glslang::EOpConvUint64ToInt16: |
7187 | zero = builder.makeUint16Constant(u: 0); |
7188 | break; |
7189 | case glslang::EOpConvInt8ToUint: |
7190 | case glslang::EOpConvInt16ToUint: |
7191 | case glslang::EOpConvInt64ToUint: |
7192 | case glslang::EOpConvUint8ToInt: |
7193 | case glslang::EOpConvUint16ToInt: |
7194 | case glslang::EOpConvUint64ToInt: |
7195 | zero = builder.makeUintConstant(u: 0); |
7196 | break; |
7197 | case glslang::EOpConvInt8ToUint64: |
7198 | case glslang::EOpConvInt16ToUint64: |
7199 | case glslang::EOpConvIntToUint64: |
7200 | case glslang::EOpConvUint8ToInt64: |
7201 | case glslang::EOpConvUint16ToInt64: |
7202 | case glslang::EOpConvUintToInt64: |
7203 | zero = builder.makeUint64Constant(u: 0); |
7204 | break; |
7205 | default: |
7206 | assert(false && "Default missing" ); |
7207 | break; |
7208 | } |
7209 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7210 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
7211 | // generating for OpSpecConstantOp instruction. |
7212 | return builder.createBinOp(spv::OpIAdd, typeId: destType, operand1: operand, operand2: zero); |
7213 | } |
7214 | // For normal run-time conversion instruction, use OpBitcast. |
7215 | convOp = spv::OpBitcast; |
7216 | break; |
7217 | case glslang::EOpConvUint64ToPtr: |
7218 | convOp = spv::OpConvertUToPtr; |
7219 | break; |
7220 | case glslang::EOpConvPtrToUint64: |
7221 | convOp = spv::OpConvertPtrToU; |
7222 | break; |
7223 | case glslang::EOpConvPtrToUvec2: |
7224 | case glslang::EOpConvUvec2ToPtr: |
7225 | convOp = spv::OpBitcast; |
7226 | break; |
7227 | #endif |
7228 | |
7229 | default: |
7230 | break; |
7231 | } |
7232 | |
7233 | spv::Id result = 0; |
7234 | if (convOp == spv::OpNop) |
7235 | return result; |
7236 | |
7237 | if (convOp == spv::OpSelect) { |
7238 | zero = makeSmearedConstant(constant: zero, vectorSize); |
7239 | one = makeSmearedConstant(constant: one, vectorSize); |
7240 | result = builder.createTriOp(convOp, typeId: destType, operand1: operand, operand2: one, operand3: zero); |
7241 | } else |
7242 | result = builder.createUnaryOp(convOp, typeId: destType, operand); |
7243 | |
7244 | result = builder.setPrecision(id: result, precision: decorations.precision); |
7245 | decorations.addNonUniform(builder, t: result); |
7246 | return result; |
7247 | } |
7248 | |
7249 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
7250 | { |
7251 | if (vectorSize == 0) |
7252 | return constant; |
7253 | |
7254 | spv::Id vectorTypeId = builder.makeVectorType(component: builder.getTypeId(resultId: constant), size: vectorSize); |
7255 | std::vector<spv::Id> components; |
7256 | for (int c = 0; c < vectorSize; ++c) |
7257 | components.push_back(x: constant); |
7258 | return builder.makeCompositeConstant(type: vectorTypeId, comps: components); |
7259 | } |
7260 | |
7261 | // For glslang ops that map to SPV atomic opCodes |
7262 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
7263 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
7264 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
7265 | { |
7266 | spv::Op opCode = spv::OpNop; |
7267 | |
7268 | switch (op) { |
7269 | case glslang::EOpAtomicAdd: |
7270 | case glslang::EOpImageAtomicAdd: |
7271 | case glslang::EOpAtomicCounterAdd: |
7272 | opCode = spv::OpAtomicIAdd; |
7273 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
7274 | opCode = spv::OpAtomicFAddEXT; |
7275 | builder.addExtension(ext: spv::E_SPV_EXT_shader_atomic_float_add); |
7276 | if (typeProxy == glslang::EbtFloat16) { |
7277 | builder.addExtension(ext: spv::E_SPV_EXT_shader_atomic_float16_add); |
7278 | builder.addCapability(cap: spv::CapabilityAtomicFloat16AddEXT); |
7279 | } else if (typeProxy == glslang::EbtFloat) { |
7280 | builder.addCapability(cap: spv::CapabilityAtomicFloat32AddEXT); |
7281 | } else { |
7282 | builder.addCapability(cap: spv::CapabilityAtomicFloat64AddEXT); |
7283 | } |
7284 | } |
7285 | break; |
7286 | case glslang::EOpAtomicSubtract: |
7287 | case glslang::EOpAtomicCounterSubtract: |
7288 | opCode = spv::OpAtomicISub; |
7289 | break; |
7290 | case glslang::EOpAtomicMin: |
7291 | case glslang::EOpImageAtomicMin: |
7292 | case glslang::EOpAtomicCounterMin: |
7293 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
7294 | opCode = spv::OpAtomicFMinEXT; |
7295 | builder.addExtension(ext: spv::E_SPV_EXT_shader_atomic_float_min_max); |
7296 | if (typeProxy == glslang::EbtFloat16) |
7297 | builder.addCapability(cap: spv::CapabilityAtomicFloat16MinMaxEXT); |
7298 | else if (typeProxy == glslang::EbtFloat) |
7299 | builder.addCapability(cap: spv::CapabilityAtomicFloat32MinMaxEXT); |
7300 | else |
7301 | builder.addCapability(cap: spv::CapabilityAtomicFloat64MinMaxEXT); |
7302 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
7303 | opCode = spv::OpAtomicUMin; |
7304 | } else { |
7305 | opCode = spv::OpAtomicSMin; |
7306 | } |
7307 | break; |
7308 | case glslang::EOpAtomicMax: |
7309 | case glslang::EOpImageAtomicMax: |
7310 | case glslang::EOpAtomicCounterMax: |
7311 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
7312 | opCode = spv::OpAtomicFMaxEXT; |
7313 | builder.addExtension(ext: spv::E_SPV_EXT_shader_atomic_float_min_max); |
7314 | if (typeProxy == glslang::EbtFloat16) |
7315 | builder.addCapability(cap: spv::CapabilityAtomicFloat16MinMaxEXT); |
7316 | else if (typeProxy == glslang::EbtFloat) |
7317 | builder.addCapability(cap: spv::CapabilityAtomicFloat32MinMaxEXT); |
7318 | else |
7319 | builder.addCapability(cap: spv::CapabilityAtomicFloat64MinMaxEXT); |
7320 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
7321 | opCode = spv::OpAtomicUMax; |
7322 | } else { |
7323 | opCode = spv::OpAtomicSMax; |
7324 | } |
7325 | break; |
7326 | case glslang::EOpAtomicAnd: |
7327 | case glslang::EOpImageAtomicAnd: |
7328 | case glslang::EOpAtomicCounterAnd: |
7329 | opCode = spv::OpAtomicAnd; |
7330 | break; |
7331 | case glslang::EOpAtomicOr: |
7332 | case glslang::EOpImageAtomicOr: |
7333 | case glslang::EOpAtomicCounterOr: |
7334 | opCode = spv::OpAtomicOr; |
7335 | break; |
7336 | case glslang::EOpAtomicXor: |
7337 | case glslang::EOpImageAtomicXor: |
7338 | case glslang::EOpAtomicCounterXor: |
7339 | opCode = spv::OpAtomicXor; |
7340 | break; |
7341 | case glslang::EOpAtomicExchange: |
7342 | case glslang::EOpImageAtomicExchange: |
7343 | case glslang::EOpAtomicCounterExchange: |
7344 | opCode = spv::OpAtomicExchange; |
7345 | break; |
7346 | case glslang::EOpAtomicCompSwap: |
7347 | case glslang::EOpImageAtomicCompSwap: |
7348 | case glslang::EOpAtomicCounterCompSwap: |
7349 | opCode = spv::OpAtomicCompareExchange; |
7350 | break; |
7351 | case glslang::EOpAtomicCounterIncrement: |
7352 | opCode = spv::OpAtomicIIncrement; |
7353 | break; |
7354 | case glslang::EOpAtomicCounterDecrement: |
7355 | opCode = spv::OpAtomicIDecrement; |
7356 | break; |
7357 | case glslang::EOpAtomicCounter: |
7358 | case glslang::EOpImageAtomicLoad: |
7359 | case glslang::EOpAtomicLoad: |
7360 | opCode = spv::OpAtomicLoad; |
7361 | break; |
7362 | case glslang::EOpAtomicStore: |
7363 | case glslang::EOpImageAtomicStore: |
7364 | opCode = spv::OpAtomicStore; |
7365 | break; |
7366 | default: |
7367 | assert(0); |
7368 | break; |
7369 | } |
7370 | |
7371 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
7372 | builder.addCapability(cap: spv::CapabilityInt64Atomics); |
7373 | |
7374 | // Sort out the operands |
7375 | // - mapping from glslang -> SPV |
7376 | // - there are extra SPV operands that are optional in glslang |
7377 | // - compare-exchange swaps the value and comparator |
7378 | // - compare-exchange has an extra memory semantics |
7379 | // - EOpAtomicCounterDecrement needs a post decrement |
7380 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
7381 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
7382 | spv::Id scopeId; |
7383 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
7384 | scopeId = builder.makeUintConstant(u: spv::ScopeQueueFamilyKHR); |
7385 | } else { |
7386 | scopeId = builder.makeUintConstant(u: spv::ScopeDevice); |
7387 | } |
7388 | // semantics default to relaxed |
7389 | spv::Id semanticsId = builder.makeUintConstant(u: lvalueCoherentFlags.isVolatile() && |
7390 | glslangIntermediate->usingVulkanMemoryModel() ? |
7391 | spv::MemorySemanticsVolatileMask : |
7392 | spv::MemorySemanticsMaskNone); |
7393 | spv::Id semanticsId2 = semanticsId; |
7394 | |
7395 | pointerId = operands[0]; |
7396 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
7397 | // no additional operands |
7398 | } else if (opCode == spv::OpAtomicCompareExchange) { |
7399 | compareId = operands[1]; |
7400 | valueId = operands[2]; |
7401 | if (operands.size() > 3) { |
7402 | scopeId = operands[3]; |
7403 | semanticsId = builder.makeUintConstant( |
7404 | u: builder.getConstantScalar(resultId: operands[4]) | builder.getConstantScalar(resultId: operands[5])); |
7405 | semanticsId2 = builder.makeUintConstant( |
7406 | u: builder.getConstantScalar(resultId: operands[6]) | builder.getConstantScalar(resultId: operands[7])); |
7407 | } |
7408 | } else if (opCode == spv::OpAtomicLoad) { |
7409 | if (operands.size() > 1) { |
7410 | scopeId = operands[1]; |
7411 | semanticsId = builder.makeUintConstant( |
7412 | u: builder.getConstantScalar(resultId: operands[2]) | builder.getConstantScalar(resultId: operands[3])); |
7413 | } |
7414 | } else { |
7415 | // atomic store or RMW |
7416 | valueId = operands[1]; |
7417 | if (operands.size() > 2) { |
7418 | scopeId = operands[2]; |
7419 | semanticsId = builder.makeUintConstant |
7420 | (u: builder.getConstantScalar(resultId: operands[3]) | builder.getConstantScalar(resultId: operands[4])); |
7421 | } |
7422 | } |
7423 | |
7424 | // Check for capabilities |
7425 | unsigned semanticsImmediate = builder.getConstantScalar(resultId: semanticsId) | builder.getConstantScalar(resultId: semanticsId2); |
7426 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
7427 | spv::MemorySemanticsMakeVisibleKHRMask | |
7428 | spv::MemorySemanticsOutputMemoryKHRMask | |
7429 | spv::MemorySemanticsVolatileMask)) { |
7430 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
7431 | } |
7432 | |
7433 | if (builder.getConstantScalar(resultId: scopeId) == spv::ScopeQueueFamily) { |
7434 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
7435 | } |
7436 | |
7437 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(resultId: scopeId) == spv::ScopeDevice) { |
7438 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
7439 | } |
7440 | |
7441 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
7442 | spvAtomicOperands.push_back(x: pointerId); |
7443 | spvAtomicOperands.push_back(x: scopeId); |
7444 | spvAtomicOperands.push_back(x: semanticsId); |
7445 | if (opCode == spv::OpAtomicCompareExchange) { |
7446 | spvAtomicOperands.push_back(x: semanticsId2); |
7447 | spvAtomicOperands.push_back(x: valueId); |
7448 | spvAtomicOperands.push_back(x: compareId); |
7449 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
7450 | spvAtomicOperands.push_back(x: valueId); |
7451 | } |
7452 | |
7453 | if (opCode == spv::OpAtomicStore) { |
7454 | builder.createNoResultOp(opCode, operands: spvAtomicOperands); |
7455 | return 0; |
7456 | } else { |
7457 | spv::Id resultId = builder.createOp(opCode, typeId, operands: spvAtomicOperands); |
7458 | |
7459 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
7460 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
7461 | if (op == glslang::EOpAtomicCounterDecrement) |
7462 | resultId = builder.createBinOp(spv::OpISub, typeId, operand1: resultId, operand2: builder.makeIntConstant(i: 1)); |
7463 | |
7464 | return resultId; |
7465 | } |
7466 | } |
7467 | |
7468 | // Create group invocation operations. |
7469 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
7470 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
7471 | { |
7472 | bool isUnsigned = isTypeUnsignedInt(type: typeProxy); |
7473 | bool isFloat = isTypeFloat(type: typeProxy); |
7474 | |
7475 | spv::Op opCode = spv::OpNop; |
7476 | std::vector<spv::IdImmediate> spvGroupOperands; |
7477 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
7478 | |
7479 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
7480 | op == glslang::EOpReadInvocation) { |
7481 | builder.addExtension(ext: spv::E_SPV_KHR_shader_ballot); |
7482 | builder.addCapability(cap: spv::CapabilitySubgroupBallotKHR); |
7483 | } else if (op == glslang::EOpAnyInvocation || |
7484 | op == glslang::EOpAllInvocations || |
7485 | op == glslang::EOpAllInvocationsEqual) { |
7486 | builder.addExtension(ext: spv::E_SPV_KHR_subgroup_vote); |
7487 | builder.addCapability(cap: spv::CapabilitySubgroupVoteKHR); |
7488 | } else { |
7489 | builder.addCapability(cap: spv::CapabilityGroups); |
7490 | if (op == glslang::EOpMinInvocationsNonUniform || |
7491 | op == glslang::EOpMaxInvocationsNonUniform || |
7492 | op == glslang::EOpAddInvocationsNonUniform || |
7493 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
7494 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
7495 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
7496 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
7497 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
7498 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
7499 | builder.addExtension(ext: spv::E_SPV_AMD_shader_ballot); |
7500 | |
7501 | switch (op) { |
7502 | case glslang::EOpMinInvocations: |
7503 | case glslang::EOpMaxInvocations: |
7504 | case glslang::EOpAddInvocations: |
7505 | case glslang::EOpMinInvocationsNonUniform: |
7506 | case glslang::EOpMaxInvocationsNonUniform: |
7507 | case glslang::EOpAddInvocationsNonUniform: |
7508 | groupOperation = spv::GroupOperationReduce; |
7509 | break; |
7510 | case glslang::EOpMinInvocationsInclusiveScan: |
7511 | case glslang::EOpMaxInvocationsInclusiveScan: |
7512 | case glslang::EOpAddInvocationsInclusiveScan: |
7513 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
7514 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
7515 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
7516 | groupOperation = spv::GroupOperationInclusiveScan; |
7517 | break; |
7518 | case glslang::EOpMinInvocationsExclusiveScan: |
7519 | case glslang::EOpMaxInvocationsExclusiveScan: |
7520 | case glslang::EOpAddInvocationsExclusiveScan: |
7521 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
7522 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
7523 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
7524 | groupOperation = spv::GroupOperationExclusiveScan; |
7525 | break; |
7526 | default: |
7527 | break; |
7528 | } |
7529 | spv::IdImmediate scope = { true, builder.makeUintConstant(u: spv::ScopeSubgroup) }; |
7530 | spvGroupOperands.push_back(x: scope); |
7531 | if (groupOperation != spv::GroupOperationMax) { |
7532 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
7533 | spvGroupOperands.push_back(x: groupOp); |
7534 | } |
7535 | } |
7536 | |
7537 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
7538 | spv::IdImmediate op = { true, *opIt }; |
7539 | spvGroupOperands.push_back(x: op); |
7540 | } |
7541 | |
7542 | switch (op) { |
7543 | case glslang::EOpAnyInvocation: |
7544 | opCode = spv::OpSubgroupAnyKHR; |
7545 | break; |
7546 | case glslang::EOpAllInvocations: |
7547 | opCode = spv::OpSubgroupAllKHR; |
7548 | break; |
7549 | case glslang::EOpAllInvocationsEqual: |
7550 | opCode = spv::OpSubgroupAllEqualKHR; |
7551 | break; |
7552 | case glslang::EOpReadInvocation: |
7553 | opCode = spv::OpSubgroupReadInvocationKHR; |
7554 | if (builder.isVectorType(typeId)) |
7555 | return CreateInvocationsVectorOperation(op: opCode, groupOperation, typeId, operands); |
7556 | break; |
7557 | case glslang::EOpReadFirstInvocation: |
7558 | opCode = spv::OpSubgroupFirstInvocationKHR; |
7559 | if (builder.isVectorType(typeId)) |
7560 | return CreateInvocationsVectorOperation(op: opCode, groupOperation, typeId, operands); |
7561 | break; |
7562 | case glslang::EOpBallot: |
7563 | { |
7564 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
7565 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
7566 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
7567 | // |
7568 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
7569 | // |
7570 | spv::Id uintType = builder.makeUintType(width: 32); |
7571 | spv::Id uvec4Type = builder.makeVectorType(component: uintType, size: 4); |
7572 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, typeId: uvec4Type, operands: spvGroupOperands); |
7573 | |
7574 | std::vector<spv::Id> components; |
7575 | components.push_back(x: builder.createCompositeExtract(composite: result, typeId: uintType, index: 0)); |
7576 | components.push_back(x: builder.createCompositeExtract(composite: result, typeId: uintType, index: 1)); |
7577 | |
7578 | spv::Id uvec2Type = builder.makeVectorType(component: uintType, size: 2); |
7579 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
7580 | operand: builder.createCompositeConstruct(typeId: uvec2Type, constituents: components)); |
7581 | } |
7582 | |
7583 | case glslang::EOpMinInvocations: |
7584 | case glslang::EOpMaxInvocations: |
7585 | case glslang::EOpAddInvocations: |
7586 | case glslang::EOpMinInvocationsInclusiveScan: |
7587 | case glslang::EOpMaxInvocationsInclusiveScan: |
7588 | case glslang::EOpAddInvocationsInclusiveScan: |
7589 | case glslang::EOpMinInvocationsExclusiveScan: |
7590 | case glslang::EOpMaxInvocationsExclusiveScan: |
7591 | case glslang::EOpAddInvocationsExclusiveScan: |
7592 | if (op == glslang::EOpMinInvocations || |
7593 | op == glslang::EOpMinInvocationsInclusiveScan || |
7594 | op == glslang::EOpMinInvocationsExclusiveScan) { |
7595 | if (isFloat) |
7596 | opCode = spv::OpGroupFMin; |
7597 | else { |
7598 | if (isUnsigned) |
7599 | opCode = spv::OpGroupUMin; |
7600 | else |
7601 | opCode = spv::OpGroupSMin; |
7602 | } |
7603 | } else if (op == glslang::EOpMaxInvocations || |
7604 | op == glslang::EOpMaxInvocationsInclusiveScan || |
7605 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
7606 | if (isFloat) |
7607 | opCode = spv::OpGroupFMax; |
7608 | else { |
7609 | if (isUnsigned) |
7610 | opCode = spv::OpGroupUMax; |
7611 | else |
7612 | opCode = spv::OpGroupSMax; |
7613 | } |
7614 | } else { |
7615 | if (isFloat) |
7616 | opCode = spv::OpGroupFAdd; |
7617 | else |
7618 | opCode = spv::OpGroupIAdd; |
7619 | } |
7620 | |
7621 | if (builder.isVectorType(typeId)) |
7622 | return CreateInvocationsVectorOperation(op: opCode, groupOperation, typeId, operands); |
7623 | |
7624 | break; |
7625 | case glslang::EOpMinInvocationsNonUniform: |
7626 | case glslang::EOpMaxInvocationsNonUniform: |
7627 | case glslang::EOpAddInvocationsNonUniform: |
7628 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
7629 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
7630 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
7631 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
7632 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
7633 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
7634 | if (op == glslang::EOpMinInvocationsNonUniform || |
7635 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
7636 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
7637 | if (isFloat) |
7638 | opCode = spv::OpGroupFMinNonUniformAMD; |
7639 | else { |
7640 | if (isUnsigned) |
7641 | opCode = spv::OpGroupUMinNonUniformAMD; |
7642 | else |
7643 | opCode = spv::OpGroupSMinNonUniformAMD; |
7644 | } |
7645 | } |
7646 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
7647 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
7648 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
7649 | if (isFloat) |
7650 | opCode = spv::OpGroupFMaxNonUniformAMD; |
7651 | else { |
7652 | if (isUnsigned) |
7653 | opCode = spv::OpGroupUMaxNonUniformAMD; |
7654 | else |
7655 | opCode = spv::OpGroupSMaxNonUniformAMD; |
7656 | } |
7657 | } |
7658 | else { |
7659 | if (isFloat) |
7660 | opCode = spv::OpGroupFAddNonUniformAMD; |
7661 | else |
7662 | opCode = spv::OpGroupIAddNonUniformAMD; |
7663 | } |
7664 | |
7665 | if (builder.isVectorType(typeId)) |
7666 | return CreateInvocationsVectorOperation(op: opCode, groupOperation, typeId, operands); |
7667 | |
7668 | break; |
7669 | default: |
7670 | logger->missingFunctionality(f: "invocation operation" ); |
7671 | return spv::NoResult; |
7672 | } |
7673 | |
7674 | assert(opCode != spv::OpNop); |
7675 | return builder.createOp(opCode, typeId, operands: spvGroupOperands); |
7676 | } |
7677 | |
7678 | // Create group invocation operations on a vector |
7679 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
7680 | spv::Id typeId, std::vector<spv::Id>& operands) |
7681 | { |
7682 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
7683 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
7684 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
7685 | op == spv::OpSubgroupReadInvocationKHR || op == spv::OpSubgroupFirstInvocationKHR || |
7686 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
7687 | op == spv::OpGroupSMinNonUniformAMD || |
7688 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
7689 | op == spv::OpGroupSMaxNonUniformAMD || |
7690 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
7691 | |
7692 | // Handle group invocation operations scalar by scalar. |
7693 | // The result type is the same type as the original type. |
7694 | // The algorithm is to: |
7695 | // - break the vector into scalars |
7696 | // - apply the operation to each scalar |
7697 | // - make a vector out the scalar results |
7698 | |
7699 | // get the types sorted out |
7700 | int numComponents = builder.getNumComponents(resultId: operands[0]); |
7701 | spv::Id scalarType = builder.getScalarTypeId(typeId: builder.getTypeId(resultId: operands[0])); |
7702 | std::vector<spv::Id> results; |
7703 | |
7704 | // do each scalar op |
7705 | for (int comp = 0; comp < numComponents; ++comp) { |
7706 | std::vector<unsigned int> indexes; |
7707 | indexes.push_back(x: comp); |
7708 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(composite: operands[0], typeId: scalarType, indexes) }; |
7709 | std::vector<spv::IdImmediate> spvGroupOperands; |
7710 | if (op == spv::OpSubgroupReadInvocationKHR) { |
7711 | spvGroupOperands.push_back(x: scalar); |
7712 | spv::IdImmediate operand = { true, operands[1] }; |
7713 | spvGroupOperands.push_back(x: operand); |
7714 | } else if (op == spv::OpSubgroupFirstInvocationKHR) { |
7715 | spvGroupOperands.push_back(x: scalar); |
7716 | } else if (op == spv::OpGroupBroadcast) { |
7717 | spv::IdImmediate scope = { true, builder.makeUintConstant(u: spv::ScopeSubgroup) }; |
7718 | spvGroupOperands.push_back(x: scope); |
7719 | spvGroupOperands.push_back(x: scalar); |
7720 | spv::IdImmediate operand = { true, operands[1] }; |
7721 | spvGroupOperands.push_back(x: operand); |
7722 | } else { |
7723 | spv::IdImmediate scope = { true, builder.makeUintConstant(u: spv::ScopeSubgroup) }; |
7724 | spvGroupOperands.push_back(x: scope); |
7725 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
7726 | spvGroupOperands.push_back(x: groupOp); |
7727 | spvGroupOperands.push_back(x: scalar); |
7728 | } |
7729 | |
7730 | results.push_back(x: builder.createOp(op, typeId: scalarType, operands: spvGroupOperands)); |
7731 | } |
7732 | |
7733 | // put the pieces together |
7734 | return builder.createCompositeConstruct(typeId, constituents: results); |
7735 | } |
7736 | |
7737 | // Create subgroup invocation operations. |
7738 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
7739 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
7740 | { |
7741 | // Add the required capabilities. |
7742 | switch (op) { |
7743 | case glslang::EOpSubgroupElect: |
7744 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7745 | break; |
7746 | case glslang::EOpSubgroupAll: |
7747 | case glslang::EOpSubgroupAny: |
7748 | case glslang::EOpSubgroupAllEqual: |
7749 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7750 | builder.addCapability(cap: spv::CapabilityGroupNonUniformVote); |
7751 | break; |
7752 | case glslang::EOpSubgroupBroadcast: |
7753 | case glslang::EOpSubgroupBroadcastFirst: |
7754 | case glslang::EOpSubgroupBallot: |
7755 | case glslang::EOpSubgroupInverseBallot: |
7756 | case glslang::EOpSubgroupBallotBitExtract: |
7757 | case glslang::EOpSubgroupBallotBitCount: |
7758 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
7759 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
7760 | case glslang::EOpSubgroupBallotFindLSB: |
7761 | case glslang::EOpSubgroupBallotFindMSB: |
7762 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7763 | builder.addCapability(cap: spv::CapabilityGroupNonUniformBallot); |
7764 | break; |
7765 | case glslang::EOpSubgroupShuffle: |
7766 | case glslang::EOpSubgroupShuffleXor: |
7767 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7768 | builder.addCapability(cap: spv::CapabilityGroupNonUniformShuffle); |
7769 | break; |
7770 | case glslang::EOpSubgroupShuffleUp: |
7771 | case glslang::EOpSubgroupShuffleDown: |
7772 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7773 | builder.addCapability(cap: spv::CapabilityGroupNonUniformShuffleRelative); |
7774 | break; |
7775 | case glslang::EOpSubgroupAdd: |
7776 | case glslang::EOpSubgroupMul: |
7777 | case glslang::EOpSubgroupMin: |
7778 | case glslang::EOpSubgroupMax: |
7779 | case glslang::EOpSubgroupAnd: |
7780 | case glslang::EOpSubgroupOr: |
7781 | case glslang::EOpSubgroupXor: |
7782 | case glslang::EOpSubgroupInclusiveAdd: |
7783 | case glslang::EOpSubgroupInclusiveMul: |
7784 | case glslang::EOpSubgroupInclusiveMin: |
7785 | case glslang::EOpSubgroupInclusiveMax: |
7786 | case glslang::EOpSubgroupInclusiveAnd: |
7787 | case glslang::EOpSubgroupInclusiveOr: |
7788 | case glslang::EOpSubgroupInclusiveXor: |
7789 | case glslang::EOpSubgroupExclusiveAdd: |
7790 | case glslang::EOpSubgroupExclusiveMul: |
7791 | case glslang::EOpSubgroupExclusiveMin: |
7792 | case glslang::EOpSubgroupExclusiveMax: |
7793 | case glslang::EOpSubgroupExclusiveAnd: |
7794 | case glslang::EOpSubgroupExclusiveOr: |
7795 | case glslang::EOpSubgroupExclusiveXor: |
7796 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7797 | builder.addCapability(cap: spv::CapabilityGroupNonUniformArithmetic); |
7798 | break; |
7799 | case glslang::EOpSubgroupClusteredAdd: |
7800 | case glslang::EOpSubgroupClusteredMul: |
7801 | case glslang::EOpSubgroupClusteredMin: |
7802 | case glslang::EOpSubgroupClusteredMax: |
7803 | case glslang::EOpSubgroupClusteredAnd: |
7804 | case glslang::EOpSubgroupClusteredOr: |
7805 | case glslang::EOpSubgroupClusteredXor: |
7806 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7807 | builder.addCapability(cap: spv::CapabilityGroupNonUniformClustered); |
7808 | break; |
7809 | case glslang::EOpSubgroupQuadBroadcast: |
7810 | case glslang::EOpSubgroupQuadSwapHorizontal: |
7811 | case glslang::EOpSubgroupQuadSwapVertical: |
7812 | case glslang::EOpSubgroupQuadSwapDiagonal: |
7813 | builder.addCapability(cap: spv::CapabilityGroupNonUniform); |
7814 | builder.addCapability(cap: spv::CapabilityGroupNonUniformQuad); |
7815 | break; |
7816 | case glslang::EOpSubgroupPartitionedAdd: |
7817 | case glslang::EOpSubgroupPartitionedMul: |
7818 | case glslang::EOpSubgroupPartitionedMin: |
7819 | case glslang::EOpSubgroupPartitionedMax: |
7820 | case glslang::EOpSubgroupPartitionedAnd: |
7821 | case glslang::EOpSubgroupPartitionedOr: |
7822 | case glslang::EOpSubgroupPartitionedXor: |
7823 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
7824 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
7825 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
7826 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
7827 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
7828 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
7829 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
7830 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
7831 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
7832 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
7833 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
7834 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
7835 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
7836 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
7837 | builder.addExtension(ext: spv::E_SPV_NV_shader_subgroup_partitioned); |
7838 | builder.addCapability(cap: spv::CapabilityGroupNonUniformPartitionedNV); |
7839 | break; |
7840 | default: assert(0 && "Unhandled subgroup operation!" ); |
7841 | } |
7842 | |
7843 | |
7844 | const bool isUnsigned = isTypeUnsignedInt(type: typeProxy); |
7845 | const bool isFloat = isTypeFloat(type: typeProxy); |
7846 | const bool isBool = typeProxy == glslang::EbtBool; |
7847 | |
7848 | spv::Op opCode = spv::OpNop; |
7849 | |
7850 | // Figure out which opcode to use. |
7851 | switch (op) { |
7852 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
7853 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
7854 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
7855 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
7856 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
7857 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
7858 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
7859 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
7860 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
7861 | case glslang::EOpSubgroupBallotBitCount: |
7862 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
7863 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
7864 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
7865 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
7866 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
7867 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
7868 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
7869 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
7870 | case glslang::EOpSubgroupAdd: |
7871 | case glslang::EOpSubgroupInclusiveAdd: |
7872 | case glslang::EOpSubgroupExclusiveAdd: |
7873 | case glslang::EOpSubgroupClusteredAdd: |
7874 | case glslang::EOpSubgroupPartitionedAdd: |
7875 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
7876 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
7877 | if (isFloat) { |
7878 | opCode = spv::OpGroupNonUniformFAdd; |
7879 | } else { |
7880 | opCode = spv::OpGroupNonUniformIAdd; |
7881 | } |
7882 | break; |
7883 | case glslang::EOpSubgroupMul: |
7884 | case glslang::EOpSubgroupInclusiveMul: |
7885 | case glslang::EOpSubgroupExclusiveMul: |
7886 | case glslang::EOpSubgroupClusteredMul: |
7887 | case glslang::EOpSubgroupPartitionedMul: |
7888 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
7889 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
7890 | if (isFloat) { |
7891 | opCode = spv::OpGroupNonUniformFMul; |
7892 | } else { |
7893 | opCode = spv::OpGroupNonUniformIMul; |
7894 | } |
7895 | break; |
7896 | case glslang::EOpSubgroupMin: |
7897 | case glslang::EOpSubgroupInclusiveMin: |
7898 | case glslang::EOpSubgroupExclusiveMin: |
7899 | case glslang::EOpSubgroupClusteredMin: |
7900 | case glslang::EOpSubgroupPartitionedMin: |
7901 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
7902 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
7903 | if (isFloat) { |
7904 | opCode = spv::OpGroupNonUniformFMin; |
7905 | } else if (isUnsigned) { |
7906 | opCode = spv::OpGroupNonUniformUMin; |
7907 | } else { |
7908 | opCode = spv::OpGroupNonUniformSMin; |
7909 | } |
7910 | break; |
7911 | case glslang::EOpSubgroupMax: |
7912 | case glslang::EOpSubgroupInclusiveMax: |
7913 | case glslang::EOpSubgroupExclusiveMax: |
7914 | case glslang::EOpSubgroupClusteredMax: |
7915 | case glslang::EOpSubgroupPartitionedMax: |
7916 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
7917 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
7918 | if (isFloat) { |
7919 | opCode = spv::OpGroupNonUniformFMax; |
7920 | } else if (isUnsigned) { |
7921 | opCode = spv::OpGroupNonUniformUMax; |
7922 | } else { |
7923 | opCode = spv::OpGroupNonUniformSMax; |
7924 | } |
7925 | break; |
7926 | case glslang::EOpSubgroupAnd: |
7927 | case glslang::EOpSubgroupInclusiveAnd: |
7928 | case glslang::EOpSubgroupExclusiveAnd: |
7929 | case glslang::EOpSubgroupClusteredAnd: |
7930 | case glslang::EOpSubgroupPartitionedAnd: |
7931 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
7932 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
7933 | if (isBool) { |
7934 | opCode = spv::OpGroupNonUniformLogicalAnd; |
7935 | } else { |
7936 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
7937 | } |
7938 | break; |
7939 | case glslang::EOpSubgroupOr: |
7940 | case glslang::EOpSubgroupInclusiveOr: |
7941 | case glslang::EOpSubgroupExclusiveOr: |
7942 | case glslang::EOpSubgroupClusteredOr: |
7943 | case glslang::EOpSubgroupPartitionedOr: |
7944 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
7945 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
7946 | if (isBool) { |
7947 | opCode = spv::OpGroupNonUniformLogicalOr; |
7948 | } else { |
7949 | opCode = spv::OpGroupNonUniformBitwiseOr; |
7950 | } |
7951 | break; |
7952 | case glslang::EOpSubgroupXor: |
7953 | case glslang::EOpSubgroupInclusiveXor: |
7954 | case glslang::EOpSubgroupExclusiveXor: |
7955 | case glslang::EOpSubgroupClusteredXor: |
7956 | case glslang::EOpSubgroupPartitionedXor: |
7957 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
7958 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
7959 | if (isBool) { |
7960 | opCode = spv::OpGroupNonUniformLogicalXor; |
7961 | } else { |
7962 | opCode = spv::OpGroupNonUniformBitwiseXor; |
7963 | } |
7964 | break; |
7965 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
7966 | case glslang::EOpSubgroupQuadSwapHorizontal: |
7967 | case glslang::EOpSubgroupQuadSwapVertical: |
7968 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
7969 | default: assert(0 && "Unhandled subgroup operation!" ); |
7970 | } |
7971 | |
7972 | // get the right Group Operation |
7973 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
7974 | switch (op) { |
7975 | default: |
7976 | break; |
7977 | case glslang::EOpSubgroupBallotBitCount: |
7978 | case glslang::EOpSubgroupAdd: |
7979 | case glslang::EOpSubgroupMul: |
7980 | case glslang::EOpSubgroupMin: |
7981 | case glslang::EOpSubgroupMax: |
7982 | case glslang::EOpSubgroupAnd: |
7983 | case glslang::EOpSubgroupOr: |
7984 | case glslang::EOpSubgroupXor: |
7985 | groupOperation = spv::GroupOperationReduce; |
7986 | break; |
7987 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
7988 | case glslang::EOpSubgroupInclusiveAdd: |
7989 | case glslang::EOpSubgroupInclusiveMul: |
7990 | case glslang::EOpSubgroupInclusiveMin: |
7991 | case glslang::EOpSubgroupInclusiveMax: |
7992 | case glslang::EOpSubgroupInclusiveAnd: |
7993 | case glslang::EOpSubgroupInclusiveOr: |
7994 | case glslang::EOpSubgroupInclusiveXor: |
7995 | groupOperation = spv::GroupOperationInclusiveScan; |
7996 | break; |
7997 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
7998 | case glslang::EOpSubgroupExclusiveAdd: |
7999 | case glslang::EOpSubgroupExclusiveMul: |
8000 | case glslang::EOpSubgroupExclusiveMin: |
8001 | case glslang::EOpSubgroupExclusiveMax: |
8002 | case glslang::EOpSubgroupExclusiveAnd: |
8003 | case glslang::EOpSubgroupExclusiveOr: |
8004 | case glslang::EOpSubgroupExclusiveXor: |
8005 | groupOperation = spv::GroupOperationExclusiveScan; |
8006 | break; |
8007 | case glslang::EOpSubgroupClusteredAdd: |
8008 | case glslang::EOpSubgroupClusteredMul: |
8009 | case glslang::EOpSubgroupClusteredMin: |
8010 | case glslang::EOpSubgroupClusteredMax: |
8011 | case glslang::EOpSubgroupClusteredAnd: |
8012 | case glslang::EOpSubgroupClusteredOr: |
8013 | case glslang::EOpSubgroupClusteredXor: |
8014 | groupOperation = spv::GroupOperationClusteredReduce; |
8015 | break; |
8016 | case glslang::EOpSubgroupPartitionedAdd: |
8017 | case glslang::EOpSubgroupPartitionedMul: |
8018 | case glslang::EOpSubgroupPartitionedMin: |
8019 | case glslang::EOpSubgroupPartitionedMax: |
8020 | case glslang::EOpSubgroupPartitionedAnd: |
8021 | case glslang::EOpSubgroupPartitionedOr: |
8022 | case glslang::EOpSubgroupPartitionedXor: |
8023 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
8024 | break; |
8025 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
8026 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
8027 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
8028 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
8029 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
8030 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
8031 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
8032 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
8033 | break; |
8034 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
8035 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
8036 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
8037 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
8038 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
8039 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
8040 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
8041 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
8042 | break; |
8043 | } |
8044 | |
8045 | // build the instruction |
8046 | std::vector<spv::IdImmediate> spvGroupOperands; |
8047 | |
8048 | // Every operation begins with the Execution Scope operand. |
8049 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(u: spv::ScopeSubgroup) }; |
8050 | spvGroupOperands.push_back(x: executionScope); |
8051 | |
8052 | // Next, for all operations that use a Group Operation, push that as an operand. |
8053 | if (groupOperation != spv::GroupOperationMax) { |
8054 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
8055 | spvGroupOperands.push_back(x: groupOperand); |
8056 | } |
8057 | |
8058 | // Push back the operands next. |
8059 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
8060 | spv::IdImmediate operand = { true, *opIt }; |
8061 | spvGroupOperands.push_back(x: operand); |
8062 | } |
8063 | |
8064 | // Some opcodes have additional operands. |
8065 | spv::Id directionId = spv::NoResult; |
8066 | switch (op) { |
8067 | default: break; |
8068 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(u: 0); break; |
8069 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(u: 1); break; |
8070 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(u: 2); break; |
8071 | } |
8072 | if (directionId != spv::NoResult) { |
8073 | spv::IdImmediate direction = { true, directionId }; |
8074 | spvGroupOperands.push_back(x: direction); |
8075 | } |
8076 | |
8077 | return builder.createOp(opCode, typeId, operands: spvGroupOperands); |
8078 | } |
8079 | |
8080 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
8081 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
8082 | { |
8083 | bool isUnsigned = isTypeUnsignedInt(type: typeProxy); |
8084 | bool isFloat = isTypeFloat(type: typeProxy); |
8085 | |
8086 | spv::Op opCode = spv::OpNop; |
8087 | int extBuiltins = -1; |
8088 | int libCall = -1; |
8089 | size_t consumedOperands = operands.size(); |
8090 | spv::Id typeId0 = 0; |
8091 | if (consumedOperands > 0) |
8092 | typeId0 = builder.getTypeId(resultId: operands[0]); |
8093 | spv::Id typeId1 = 0; |
8094 | if (consumedOperands > 1) |
8095 | typeId1 = builder.getTypeId(resultId: operands[1]); |
8096 | spv::Id frexpIntType = 0; |
8097 | |
8098 | switch (op) { |
8099 | case glslang::EOpMin: |
8100 | if (isFloat) |
8101 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
8102 | else if (isUnsigned) |
8103 | libCall = spv::GLSLstd450UMin; |
8104 | else |
8105 | libCall = spv::GLSLstd450SMin; |
8106 | builder.promoteScalar(precision, left&: operands.front(), right&: operands.back()); |
8107 | break; |
8108 | case glslang::EOpModf: |
8109 | libCall = spv::GLSLstd450Modf; |
8110 | break; |
8111 | case glslang::EOpMax: |
8112 | if (isFloat) |
8113 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
8114 | else if (isUnsigned) |
8115 | libCall = spv::GLSLstd450UMax; |
8116 | else |
8117 | libCall = spv::GLSLstd450SMax; |
8118 | builder.promoteScalar(precision, left&: operands.front(), right&: operands.back()); |
8119 | break; |
8120 | case glslang::EOpPow: |
8121 | libCall = spv::GLSLstd450Pow; |
8122 | break; |
8123 | case glslang::EOpDot: |
8124 | opCode = spv::OpDot; |
8125 | break; |
8126 | case glslang::EOpAtan: |
8127 | libCall = spv::GLSLstd450Atan2; |
8128 | break; |
8129 | |
8130 | case glslang::EOpClamp: |
8131 | if (isFloat) |
8132 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
8133 | else if (isUnsigned) |
8134 | libCall = spv::GLSLstd450UClamp; |
8135 | else |
8136 | libCall = spv::GLSLstd450SClamp; |
8137 | builder.promoteScalar(precision, left&: operands.front(), right&: operands[1]); |
8138 | builder.promoteScalar(precision, left&: operands.front(), right&: operands[2]); |
8139 | break; |
8140 | case glslang::EOpMix: |
8141 | if (! builder.isBoolType(typeId: builder.getScalarTypeId(typeId: builder.getTypeId(resultId: operands.back())))) { |
8142 | assert(isFloat); |
8143 | libCall = spv::GLSLstd450FMix; |
8144 | } else { |
8145 | opCode = spv::OpSelect; |
8146 | std::swap(a&: operands.front(), b&: operands.back()); |
8147 | } |
8148 | builder.promoteScalar(precision, left&: operands.front(), right&: operands.back()); |
8149 | break; |
8150 | case glslang::EOpStep: |
8151 | libCall = spv::GLSLstd450Step; |
8152 | builder.promoteScalar(precision, left&: operands.front(), right&: operands.back()); |
8153 | break; |
8154 | case glslang::EOpSmoothStep: |
8155 | libCall = spv::GLSLstd450SmoothStep; |
8156 | builder.promoteScalar(precision, left&: operands[0], right&: operands[2]); |
8157 | builder.promoteScalar(precision, left&: operands[1], right&: operands[2]); |
8158 | break; |
8159 | |
8160 | case glslang::EOpDistance: |
8161 | libCall = spv::GLSLstd450Distance; |
8162 | break; |
8163 | case glslang::EOpCross: |
8164 | libCall = spv::GLSLstd450Cross; |
8165 | break; |
8166 | case glslang::EOpFaceForward: |
8167 | libCall = spv::GLSLstd450FaceForward; |
8168 | break; |
8169 | case glslang::EOpReflect: |
8170 | libCall = spv::GLSLstd450Reflect; |
8171 | break; |
8172 | case glslang::EOpRefract: |
8173 | libCall = spv::GLSLstd450Refract; |
8174 | break; |
8175 | case glslang::EOpBarrier: |
8176 | { |
8177 | // This is for the extended controlBarrier function, with four operands. |
8178 | // The unextended barrier() goes through createNoArgOperation. |
8179 | assert(operands.size() == 4); |
8180 | unsigned int executionScope = builder.getConstantScalar(resultId: operands[0]); |
8181 | unsigned int memoryScope = builder.getConstantScalar(resultId: operands[1]); |
8182 | unsigned int semantics = builder.getConstantScalar(resultId: operands[2]) | builder.getConstantScalar(resultId: operands[3]); |
8183 | builder.createControlBarrier(execution: (spv::Scope)executionScope, memory: (spv::Scope)memoryScope, |
8184 | (spv::MemorySemanticsMask)semantics); |
8185 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
8186 | spv::MemorySemanticsMakeVisibleKHRMask | |
8187 | spv::MemorySemanticsOutputMemoryKHRMask | |
8188 | spv::MemorySemanticsVolatileMask)) { |
8189 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
8190 | } |
8191 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
8192 | memoryScope == spv::ScopeDevice)) { |
8193 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
8194 | } |
8195 | return 0; |
8196 | } |
8197 | break; |
8198 | case glslang::EOpMemoryBarrier: |
8199 | { |
8200 | // This is for the extended memoryBarrier function, with three operands. |
8201 | // The unextended memoryBarrier() goes through createNoArgOperation. |
8202 | assert(operands.size() == 3); |
8203 | unsigned int memoryScope = builder.getConstantScalar(resultId: operands[0]); |
8204 | unsigned int semantics = builder.getConstantScalar(resultId: operands[1]) | builder.getConstantScalar(resultId: operands[2]); |
8205 | builder.createMemoryBarrier(executionScope: (spv::Scope)memoryScope, memorySemantics: (spv::MemorySemanticsMask)semantics); |
8206 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
8207 | spv::MemorySemanticsMakeVisibleKHRMask | |
8208 | spv::MemorySemanticsOutputMemoryKHRMask | |
8209 | spv::MemorySemanticsVolatileMask)) { |
8210 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
8211 | } |
8212 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
8213 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
8214 | } |
8215 | return 0; |
8216 | } |
8217 | break; |
8218 | |
8219 | #ifndef GLSLANG_WEB |
8220 | case glslang::EOpInterpolateAtSample: |
8221 | if (typeProxy == glslang::EbtFloat16) |
8222 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_half_float); |
8223 | libCall = spv::GLSLstd450InterpolateAtSample; |
8224 | break; |
8225 | case glslang::EOpInterpolateAtOffset: |
8226 | if (typeProxy == glslang::EbtFloat16) |
8227 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_half_float); |
8228 | libCall = spv::GLSLstd450InterpolateAtOffset; |
8229 | break; |
8230 | case glslang::EOpAddCarry: |
8231 | opCode = spv::OpIAddCarry; |
8232 | typeId = builder.makeStructResultType(type0: typeId0, type1: typeId0); |
8233 | consumedOperands = 2; |
8234 | break; |
8235 | case glslang::EOpSubBorrow: |
8236 | opCode = spv::OpISubBorrow; |
8237 | typeId = builder.makeStructResultType(type0: typeId0, type1: typeId0); |
8238 | consumedOperands = 2; |
8239 | break; |
8240 | case glslang::EOpUMulExtended: |
8241 | opCode = spv::OpUMulExtended; |
8242 | typeId = builder.makeStructResultType(type0: typeId0, type1: typeId0); |
8243 | consumedOperands = 2; |
8244 | break; |
8245 | case glslang::EOpIMulExtended: |
8246 | opCode = spv::OpSMulExtended; |
8247 | typeId = builder.makeStructResultType(type0: typeId0, type1: typeId0); |
8248 | consumedOperands = 2; |
8249 | break; |
8250 | case glslang::EOpBitfieldExtract: |
8251 | if (isUnsigned) |
8252 | opCode = spv::OpBitFieldUExtract; |
8253 | else |
8254 | opCode = spv::OpBitFieldSExtract; |
8255 | break; |
8256 | case glslang::EOpBitfieldInsert: |
8257 | opCode = spv::OpBitFieldInsert; |
8258 | break; |
8259 | |
8260 | case glslang::EOpFma: |
8261 | libCall = spv::GLSLstd450Fma; |
8262 | break; |
8263 | case glslang::EOpFrexp: |
8264 | { |
8265 | libCall = spv::GLSLstd450FrexpStruct; |
8266 | assert(builder.isPointerType(typeId1)); |
8267 | typeId1 = builder.getContainedTypeId(typeId: typeId1); |
8268 | int width = builder.getScalarTypeWidth(typeId: typeId1); |
8269 | if (width == 16) |
8270 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
8271 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_int16); |
8272 | if (builder.getNumComponents(resultId: operands[0]) == 1) |
8273 | frexpIntType = builder.makeIntegerType(width, hasSign: true); |
8274 | else |
8275 | frexpIntType = builder.makeVectorType(component: builder.makeIntegerType(width, hasSign: true), |
8276 | size: builder.getNumComponents(resultId: operands[0])); |
8277 | typeId = builder.makeStructResultType(type0: typeId0, type1: frexpIntType); |
8278 | consumedOperands = 1; |
8279 | } |
8280 | break; |
8281 | case glslang::EOpLdexp: |
8282 | libCall = spv::GLSLstd450Ldexp; |
8283 | break; |
8284 | |
8285 | case glslang::EOpReadInvocation: |
8286 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
8287 | |
8288 | case glslang::EOpSubgroupBroadcast: |
8289 | case glslang::EOpSubgroupBallotBitExtract: |
8290 | case glslang::EOpSubgroupShuffle: |
8291 | case glslang::EOpSubgroupShuffleXor: |
8292 | case glslang::EOpSubgroupShuffleUp: |
8293 | case glslang::EOpSubgroupShuffleDown: |
8294 | case glslang::EOpSubgroupClusteredAdd: |
8295 | case glslang::EOpSubgroupClusteredMul: |
8296 | case glslang::EOpSubgroupClusteredMin: |
8297 | case glslang::EOpSubgroupClusteredMax: |
8298 | case glslang::EOpSubgroupClusteredAnd: |
8299 | case glslang::EOpSubgroupClusteredOr: |
8300 | case glslang::EOpSubgroupClusteredXor: |
8301 | case glslang::EOpSubgroupQuadBroadcast: |
8302 | case glslang::EOpSubgroupPartitionedAdd: |
8303 | case glslang::EOpSubgroupPartitionedMul: |
8304 | case glslang::EOpSubgroupPartitionedMin: |
8305 | case glslang::EOpSubgroupPartitionedMax: |
8306 | case glslang::EOpSubgroupPartitionedAnd: |
8307 | case glslang::EOpSubgroupPartitionedOr: |
8308 | case glslang::EOpSubgroupPartitionedXor: |
8309 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
8310 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
8311 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
8312 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
8313 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
8314 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
8315 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
8316 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
8317 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
8318 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
8319 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
8320 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
8321 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
8322 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
8323 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
8324 | |
8325 | case glslang::EOpSwizzleInvocations: |
8326 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_ballot); |
8327 | libCall = spv::SwizzleInvocationsAMD; |
8328 | break; |
8329 | case glslang::EOpSwizzleInvocationsMasked: |
8330 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_ballot); |
8331 | libCall = spv::SwizzleInvocationsMaskedAMD; |
8332 | break; |
8333 | case glslang::EOpWriteInvocation: |
8334 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_ballot); |
8335 | libCall = spv::WriteInvocationAMD; |
8336 | break; |
8337 | |
8338 | case glslang::EOpMin3: |
8339 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_trinary_minmax); |
8340 | if (isFloat) |
8341 | libCall = spv::FMin3AMD; |
8342 | else { |
8343 | if (isUnsigned) |
8344 | libCall = spv::UMin3AMD; |
8345 | else |
8346 | libCall = spv::SMin3AMD; |
8347 | } |
8348 | break; |
8349 | case glslang::EOpMax3: |
8350 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_trinary_minmax); |
8351 | if (isFloat) |
8352 | libCall = spv::FMax3AMD; |
8353 | else { |
8354 | if (isUnsigned) |
8355 | libCall = spv::UMax3AMD; |
8356 | else |
8357 | libCall = spv::SMax3AMD; |
8358 | } |
8359 | break; |
8360 | case glslang::EOpMid3: |
8361 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_trinary_minmax); |
8362 | if (isFloat) |
8363 | libCall = spv::FMid3AMD; |
8364 | else { |
8365 | if (isUnsigned) |
8366 | libCall = spv::UMid3AMD; |
8367 | else |
8368 | libCall = spv::SMid3AMD; |
8369 | } |
8370 | break; |
8371 | |
8372 | case glslang::EOpInterpolateAtVertex: |
8373 | if (typeProxy == glslang::EbtFloat16) |
8374 | builder.addExtension(ext: spv::E_SPV_AMD_gpu_shader_half_float); |
8375 | extBuiltins = getExtBuiltins(name: spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
8376 | libCall = spv::InterpolateAtVertexAMD; |
8377 | break; |
8378 | |
8379 | case glslang::EOpReportIntersection: |
8380 | typeId = builder.makeBoolType(); |
8381 | opCode = spv::OpReportIntersectionKHR; |
8382 | break; |
8383 | case glslang::EOpTraceNV: |
8384 | builder.createNoResultOp(spv::OpTraceNV, operands); |
8385 | return 0; |
8386 | case glslang::EOpTraceRayMotionNV: |
8387 | builder.addExtension(ext: spv::E_SPV_NV_ray_tracing_motion_blur); |
8388 | builder.addCapability(cap: spv::CapabilityRayTracingMotionBlurNV); |
8389 | builder.createNoResultOp(spv::OpTraceRayMotionNV, operands); |
8390 | return 0; |
8391 | case glslang::EOpTraceKHR: |
8392 | builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
8393 | return 0; |
8394 | case glslang::EOpExecuteCallableNV: |
8395 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
8396 | return 0; |
8397 | case glslang::EOpExecuteCallableKHR: |
8398 | builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
8399 | return 0; |
8400 | |
8401 | case glslang::EOpRayQueryInitialize: |
8402 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
8403 | return 0; |
8404 | case glslang::EOpRayQueryTerminate: |
8405 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
8406 | return 0; |
8407 | case glslang::EOpRayQueryGenerateIntersection: |
8408 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
8409 | return 0; |
8410 | case glslang::EOpRayQueryConfirmIntersection: |
8411 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
8412 | return 0; |
8413 | case glslang::EOpRayQueryProceed: |
8414 | typeId = builder.makeBoolType(); |
8415 | opCode = spv::OpRayQueryProceedKHR; |
8416 | break; |
8417 | case glslang::EOpRayQueryGetIntersectionType: |
8418 | typeId = builder.makeUintType(width: 32); |
8419 | opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
8420 | break; |
8421 | case glslang::EOpRayQueryGetRayTMin: |
8422 | typeId = builder.makeFloatType(width: 32); |
8423 | opCode = spv::OpRayQueryGetRayTMinKHR; |
8424 | break; |
8425 | case glslang::EOpRayQueryGetRayFlags: |
8426 | typeId = builder.makeIntType(width: 32); |
8427 | opCode = spv::OpRayQueryGetRayFlagsKHR; |
8428 | break; |
8429 | case glslang::EOpRayQueryGetIntersectionT: |
8430 | typeId = builder.makeFloatType(width: 32); |
8431 | opCode = spv::OpRayQueryGetIntersectionTKHR; |
8432 | break; |
8433 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
8434 | typeId = builder.makeIntType(width: 32); |
8435 | opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
8436 | break; |
8437 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
8438 | typeId = builder.makeIntType(width: 32); |
8439 | opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
8440 | break; |
8441 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
8442 | typeId = builder.makeUintType(width: 32); |
8443 | opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
8444 | break; |
8445 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
8446 | typeId = builder.makeIntType(width: 32); |
8447 | opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
8448 | break; |
8449 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
8450 | typeId = builder.makeIntType(width: 32); |
8451 | opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
8452 | break; |
8453 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
8454 | typeId = builder.makeVectorType(component: builder.makeFloatType(width: 32), size: 2); |
8455 | opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
8456 | break; |
8457 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
8458 | typeId = builder.makeBoolType(); |
8459 | opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
8460 | break; |
8461 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
8462 | typeId = builder.makeBoolType(); |
8463 | opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
8464 | break; |
8465 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
8466 | typeId = builder.makeVectorType(component: builder.makeFloatType(width: 32), size: 3); |
8467 | opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
8468 | break; |
8469 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
8470 | typeId = builder.makeVectorType(component: builder.makeFloatType(width: 32), size: 3); |
8471 | opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
8472 | break; |
8473 | case glslang::EOpRayQueryGetWorldRayDirection: |
8474 | typeId = builder.makeVectorType(component: builder.makeFloatType(width: 32), size: 3); |
8475 | opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
8476 | break; |
8477 | case glslang::EOpRayQueryGetWorldRayOrigin: |
8478 | typeId = builder.makeVectorType(component: builder.makeFloatType(width: 32), size: 3); |
8479 | opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
8480 | break; |
8481 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
8482 | typeId = builder.makeMatrixType(component: builder.makeFloatType(width: 32), cols: 4, rows: 3); |
8483 | opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
8484 | break; |
8485 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
8486 | typeId = builder.makeMatrixType(component: builder.makeFloatType(width: 32), cols: 4, rows: 3); |
8487 | opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
8488 | break; |
8489 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
8490 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
8491 | return 0; |
8492 | case glslang::EOpCooperativeMatrixMulAdd: |
8493 | opCode = spv::OpCooperativeMatrixMulAddNV; |
8494 | break; |
8495 | #endif // GLSLANG_WEB |
8496 | default: |
8497 | return 0; |
8498 | } |
8499 | |
8500 | spv::Id id = 0; |
8501 | if (libCall >= 0) { |
8502 | // Use an extended instruction from the standard library. |
8503 | // Construct the call arguments, without modifying the original operands vector. |
8504 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
8505 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
8506 | id = builder.createBuiltinCall(resultType: typeId, builtins: extBuiltins >= 0 ? extBuiltins : stdBuiltins, entryPoint: libCall, args: callArguments); |
8507 | } else if (opCode == spv::OpDot && !isFloat) { |
8508 | // int dot(int, int) |
8509 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
8510 | const int componentCount = builder.getNumComponents(resultId: operands[0]); |
8511 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, typeId: builder.getTypeId(resultId: operands[0]), operand1: operands[0], operand2: operands[1]); |
8512 | builder.setPrecision(id: mulOp, precision); |
8513 | id = builder.createCompositeExtract(composite: mulOp, typeId, index: 0); |
8514 | for (int i = 1; i < componentCount; ++i) { |
8515 | builder.setPrecision(id, precision); |
8516 | id = builder.createBinOp(spv::OpIAdd, typeId, operand1: id, operand2: builder.createCompositeExtract(composite: mulOp, typeId, index: i)); |
8517 | } |
8518 | } else { |
8519 | switch (consumedOperands) { |
8520 | case 0: |
8521 | // should all be handled by visitAggregate and createNoArgOperation |
8522 | assert(0); |
8523 | return 0; |
8524 | case 1: |
8525 | // should all be handled by createUnaryOperation |
8526 | assert(0); |
8527 | return 0; |
8528 | case 2: |
8529 | id = builder.createBinOp(opCode, typeId, operand1: operands[0], operand2: operands[1]); |
8530 | break; |
8531 | default: |
8532 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
8533 | assert(consumedOperands == operands.size()); |
8534 | id = builder.createOp(opCode, typeId, operands); |
8535 | break; |
8536 | } |
8537 | } |
8538 | |
8539 | #ifndef GLSLANG_WEB |
8540 | // Decode the return types that were structures |
8541 | switch (op) { |
8542 | case glslang::EOpAddCarry: |
8543 | case glslang::EOpSubBorrow: |
8544 | builder.createStore(rValue: builder.createCompositeExtract(composite: id, typeId: typeId0, index: 1), lValue: operands[2]); |
8545 | id = builder.createCompositeExtract(composite: id, typeId: typeId0, index: 0); |
8546 | break; |
8547 | case glslang::EOpUMulExtended: |
8548 | case glslang::EOpIMulExtended: |
8549 | builder.createStore(rValue: builder.createCompositeExtract(composite: id, typeId: typeId0, index: 0), lValue: operands[3]); |
8550 | builder.createStore(rValue: builder.createCompositeExtract(composite: id, typeId: typeId0, index: 1), lValue: operands[2]); |
8551 | break; |
8552 | case glslang::EOpFrexp: |
8553 | { |
8554 | assert(operands.size() == 2); |
8555 | if (builder.isFloatType(typeId: builder.getScalarTypeId(typeId: typeId1))) { |
8556 | // "exp" is floating-point type (from HLSL intrinsic) |
8557 | spv::Id member1 = builder.createCompositeExtract(composite: id, typeId: frexpIntType, index: 1); |
8558 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId: typeId1, operand: member1); |
8559 | builder.createStore(rValue: member1, lValue: operands[1]); |
8560 | } else |
8561 | // "exp" is integer type (from GLSL built-in function) |
8562 | builder.createStore(rValue: builder.createCompositeExtract(composite: id, typeId: frexpIntType, index: 1), lValue: operands[1]); |
8563 | id = builder.createCompositeExtract(composite: id, typeId: typeId0, index: 0); |
8564 | } |
8565 | break; |
8566 | default: |
8567 | break; |
8568 | } |
8569 | #endif |
8570 | |
8571 | return builder.setPrecision(id, precision); |
8572 | } |
8573 | |
8574 | // Intrinsics with no arguments (or no return value, and no precision). |
8575 | spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId) |
8576 | { |
8577 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
8578 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
8579 | spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
8580 | |
8581 | switch (op) { |
8582 | case glslang::EOpBarrier: |
8583 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
8584 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
8585 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeWorkgroup, |
8586 | spv::MemorySemanticsOutputMemoryKHRMask | |
8587 | spv::MemorySemanticsAcquireReleaseMask); |
8588 | builder.addCapability(cap: spv::CapabilityVulkanMemoryModelKHR); |
8589 | } else { |
8590 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
8591 | } |
8592 | } else { |
8593 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeWorkgroup, |
8594 | spv::MemorySemanticsWorkgroupMemoryMask | |
8595 | spv::MemorySemanticsAcquireReleaseMask); |
8596 | } |
8597 | return 0; |
8598 | case glslang::EOpMemoryBarrier: |
8599 | builder.createMemoryBarrier(executionScope: memoryBarrierScope, memorySemantics: spv::MemorySemanticsAllMemory | |
8600 | spv::MemorySemanticsAcquireReleaseMask); |
8601 | return 0; |
8602 | case glslang::EOpMemoryBarrierBuffer: |
8603 | builder.createMemoryBarrier(executionScope: memoryBarrierScope, memorySemantics: spv::MemorySemanticsUniformMemoryMask | |
8604 | spv::MemorySemanticsAcquireReleaseMask); |
8605 | return 0; |
8606 | case glslang::EOpMemoryBarrierShared: |
8607 | builder.createMemoryBarrier(executionScope: memoryBarrierScope, memorySemantics: spv::MemorySemanticsWorkgroupMemoryMask | |
8608 | spv::MemorySemanticsAcquireReleaseMask); |
8609 | return 0; |
8610 | case glslang::EOpGroupMemoryBarrier: |
8611 | builder.createMemoryBarrier(executionScope: spv::ScopeWorkgroup, memorySemantics: spv::MemorySemanticsAllMemory | |
8612 | spv::MemorySemanticsAcquireReleaseMask); |
8613 | return 0; |
8614 | #ifndef GLSLANG_WEB |
8615 | case glslang::EOpMemoryBarrierAtomicCounter: |
8616 | builder.createMemoryBarrier(executionScope: memoryBarrierScope, memorySemantics: spv::MemorySemanticsAtomicCounterMemoryMask | |
8617 | spv::MemorySemanticsAcquireReleaseMask); |
8618 | return 0; |
8619 | case glslang::EOpMemoryBarrierImage: |
8620 | builder.createMemoryBarrier(executionScope: memoryBarrierScope, memorySemantics: spv::MemorySemanticsImageMemoryMask | |
8621 | spv::MemorySemanticsAcquireReleaseMask); |
8622 | return 0; |
8623 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
8624 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeDevice, |
8625 | spv::MemorySemanticsAllMemory | |
8626 | spv::MemorySemanticsAcquireReleaseMask); |
8627 | return 0; |
8628 | case glslang::EOpDeviceMemoryBarrier: |
8629 | builder.createMemoryBarrier(executionScope: spv::ScopeDevice, memorySemantics: spv::MemorySemanticsUniformMemoryMask | |
8630 | spv::MemorySemanticsImageMemoryMask | |
8631 | spv::MemorySemanticsAcquireReleaseMask); |
8632 | return 0; |
8633 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
8634 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
8635 | spv::MemorySemanticsImageMemoryMask | |
8636 | spv::MemorySemanticsAcquireReleaseMask); |
8637 | return 0; |
8638 | case glslang::EOpWorkgroupMemoryBarrier: |
8639 | builder.createMemoryBarrier(executionScope: spv::ScopeWorkgroup, memorySemantics: spv::MemorySemanticsWorkgroupMemoryMask | |
8640 | spv::MemorySemanticsAcquireReleaseMask); |
8641 | return 0; |
8642 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
8643 | builder.createControlBarrier(execution: spv::ScopeWorkgroup, memory: spv::ScopeWorkgroup, |
8644 | spv::MemorySemanticsWorkgroupMemoryMask | |
8645 | spv::MemorySemanticsAcquireReleaseMask); |
8646 | return 0; |
8647 | case glslang::EOpSubgroupBarrier: |
8648 | builder.createControlBarrier(execution: spv::ScopeSubgroup, memory: spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
8649 | spv::MemorySemanticsAcquireReleaseMask); |
8650 | return spv::NoResult; |
8651 | case glslang::EOpSubgroupMemoryBarrier: |
8652 | builder.createMemoryBarrier(executionScope: spv::ScopeSubgroup, memorySemantics: spv::MemorySemanticsAllMemory | |
8653 | spv::MemorySemanticsAcquireReleaseMask); |
8654 | return spv::NoResult; |
8655 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
8656 | builder.createMemoryBarrier(executionScope: spv::ScopeSubgroup, memorySemantics: spv::MemorySemanticsUniformMemoryMask | |
8657 | spv::MemorySemanticsAcquireReleaseMask); |
8658 | return spv::NoResult; |
8659 | case glslang::EOpSubgroupMemoryBarrierImage: |
8660 | builder.createMemoryBarrier(executionScope: spv::ScopeSubgroup, memorySemantics: spv::MemorySemanticsImageMemoryMask | |
8661 | spv::MemorySemanticsAcquireReleaseMask); |
8662 | return spv::NoResult; |
8663 | case glslang::EOpSubgroupMemoryBarrierShared: |
8664 | builder.createMemoryBarrier(executionScope: spv::ScopeSubgroup, memorySemantics: spv::MemorySemanticsWorkgroupMemoryMask | |
8665 | spv::MemorySemanticsAcquireReleaseMask); |
8666 | return spv::NoResult; |
8667 | |
8668 | case glslang::EOpEmitVertex: |
8669 | builder.createNoResultOp(spv::OpEmitVertex); |
8670 | return 0; |
8671 | case glslang::EOpEndPrimitive: |
8672 | builder.createNoResultOp(spv::OpEndPrimitive); |
8673 | return 0; |
8674 | |
8675 | case glslang::EOpSubgroupElect: { |
8676 | std::vector<spv::Id> operands; |
8677 | return createSubgroupOperation(op, typeId, operands, typeProxy: glslang::EbtVoid); |
8678 | } |
8679 | case glslang::EOpTime: |
8680 | { |
8681 | std::vector<spv::Id> args; // Dummy arguments |
8682 | spv::Id id = builder.createBuiltinCall(resultType: typeId, builtins: getExtBuiltins(name: spv::E_SPV_AMD_gcn_shader), entryPoint: spv::TimeAMD, args); |
8683 | return builder.setPrecision(id, precision); |
8684 | } |
8685 | case glslang::EOpIgnoreIntersectionNV: |
8686 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
8687 | return 0; |
8688 | case glslang::EOpTerminateRayNV: |
8689 | builder.createNoResultOp(spv::OpTerminateRayNV); |
8690 | return 0; |
8691 | case glslang::EOpRayQueryInitialize: |
8692 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
8693 | return 0; |
8694 | case glslang::EOpRayQueryTerminate: |
8695 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
8696 | return 0; |
8697 | case glslang::EOpRayQueryGenerateIntersection: |
8698 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
8699 | return 0; |
8700 | case glslang::EOpRayQueryConfirmIntersection: |
8701 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
8702 | return 0; |
8703 | case glslang::EOpBeginInvocationInterlock: |
8704 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
8705 | return 0; |
8706 | case glslang::EOpEndInvocationInterlock: |
8707 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
8708 | return 0; |
8709 | |
8710 | case glslang::EOpIsHelperInvocation: |
8711 | { |
8712 | std::vector<spv::Id> args; // Dummy arguments |
8713 | builder.addExtension(ext: spv::E_SPV_EXT_demote_to_helper_invocation); |
8714 | builder.addCapability(cap: spv::CapabilityDemoteToHelperInvocationEXT); |
8715 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, operands: args); |
8716 | } |
8717 | |
8718 | case glslang::EOpReadClockSubgroupKHR: { |
8719 | std::vector<spv::Id> args; |
8720 | args.push_back(x: builder.makeUintConstant(u: spv::ScopeSubgroup)); |
8721 | builder.addExtension(ext: spv::E_SPV_KHR_shader_clock); |
8722 | builder.addCapability(cap: spv::CapabilityShaderClockKHR); |
8723 | return builder.createOp(spv::OpReadClockKHR, typeId, operands: args); |
8724 | } |
8725 | |
8726 | case glslang::EOpReadClockDeviceKHR: { |
8727 | std::vector<spv::Id> args; |
8728 | args.push_back(x: builder.makeUintConstant(u: spv::ScopeDevice)); |
8729 | builder.addExtension(ext: spv::E_SPV_KHR_shader_clock); |
8730 | builder.addCapability(cap: spv::CapabilityShaderClockKHR); |
8731 | return builder.createOp(spv::OpReadClockKHR, typeId, operands: args); |
8732 | } |
8733 | #endif |
8734 | default: |
8735 | break; |
8736 | } |
8737 | |
8738 | logger->missingFunctionality(f: "unknown operation with no arguments" ); |
8739 | |
8740 | return 0; |
8741 | } |
8742 | |
8743 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
8744 | { |
8745 | auto iter = symbolValues.find(x: symbol->getId()); |
8746 | spv::Id id; |
8747 | if (symbolValues.end() != iter) { |
8748 | id = iter->second; |
8749 | return id; |
8750 | } |
8751 | |
8752 | // it was not found, create it |
8753 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(builtIn: symbol->getQualifier().builtIn, memberDeclaration: false); |
8754 | auto forcedType = getForcedType(glslangBuiltIn: symbol->getQualifier().builtIn, glslangType: symbol->getType()); |
8755 | |
8756 | // There are pairs of symbols that map to the same SPIR-V built-in: |
8757 | // gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, and gl_WorldToObjectEXT |
8758 | // and gl_WorldToObject3x4EXT. SPIR-V forbids having two OpVariables |
8759 | // with the same BuiltIn in the same storage class, so we must re-use one. |
8760 | const bool mayNeedToReuseBuiltIn = |
8761 | builtIn == spv::BuiltInObjectToWorldKHR || |
8762 | builtIn == spv::BuiltInWorldToObjectKHR; |
8763 | |
8764 | if (mayNeedToReuseBuiltIn) { |
8765 | auto iter = builtInVariableIds.find(x: uint32_t(builtIn)); |
8766 | if (builtInVariableIds.end() != iter) { |
8767 | id = iter->second; |
8768 | symbolValues[symbol->getId()] = id; |
8769 | if (forcedType.second != spv::NoType) |
8770 | forceType[id] = forcedType.second; |
8771 | return id; |
8772 | } |
8773 | } |
8774 | |
8775 | id = createSpvVariable(node: symbol, forcedType: forcedType.first); |
8776 | |
8777 | if (mayNeedToReuseBuiltIn) { |
8778 | builtInVariableIds.insert(x: {uint32_t(builtIn), id}); |
8779 | } |
8780 | |
8781 | symbolValues[symbol->getId()] = id; |
8782 | if (forcedType.second != spv::NoType) |
8783 | forceType[id] = forcedType.second; |
8784 | |
8785 | if (symbol->getBasicType() != glslang::EbtBlock) { |
8786 | builder.addDecoration(id, TranslatePrecisionDecoration(type: symbol->getType())); |
8787 | builder.addDecoration(id, TranslateInterpolationDecoration(qualifier: symbol->getType().getQualifier())); |
8788 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(qualifier: symbol->getType().getQualifier())); |
8789 | #ifndef GLSLANG_WEB |
8790 | addMeshNVDecoration(id, /*member*/ -1, qualifier: symbol->getType().getQualifier()); |
8791 | if (symbol->getQualifier().hasComponent()) |
8792 | builder.addDecoration(id, spv::DecorationComponent, num: symbol->getQualifier().layoutComponent); |
8793 | if (symbol->getQualifier().hasIndex()) |
8794 | builder.addDecoration(id, spv::DecorationIndex, num: symbol->getQualifier().layoutIndex); |
8795 | #endif |
8796 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
8797 | builder.addDecoration(id, spv::DecorationSpecId, num: symbol->getType().getQualifier().layoutSpecConstantId); |
8798 | // atomic counters use this: |
8799 | if (symbol->getQualifier().hasOffset()) |
8800 | builder.addDecoration(id, spv::DecorationOffset, num: symbol->getQualifier().layoutOffset); |
8801 | } |
8802 | |
8803 | if (symbol->getQualifier().hasLocation()) { |
8804 | if (!(glslangIntermediate->isRayTracingStage() && glslangIntermediate->IsRequestedExtension(extension: glslang::E_GL_EXT_ray_tracing) |
8805 | && (builder.getStorageClass(resultId: id) == spv::StorageClassRayPayloadKHR || |
8806 | builder.getStorageClass(resultId: id) == spv::StorageClassIncomingRayPayloadKHR || |
8807 | builder.getStorageClass(resultId: id) == spv::StorageClassCallableDataKHR || |
8808 | builder.getStorageClass(resultId: id) == spv::StorageClassIncomingCallableDataKHR))) { |
8809 | // Location values are used to link TraceRayKHR and ExecuteCallableKHR to corresponding variables |
8810 | // but are not valid in SPIRV since they are supported only for Input/Output Storage classes. |
8811 | builder.addDecoration(id, spv::DecorationLocation, num: symbol->getQualifier().layoutLocation); |
8812 | } |
8813 | } |
8814 | |
8815 | builder.addDecoration(id, TranslateInvariantDecoration(qualifier: symbol->getType().getQualifier())); |
8816 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
8817 | builder.addCapability(cap: spv::CapabilityGeometryStreams); |
8818 | builder.addDecoration(id, spv::DecorationStream, num: symbol->getQualifier().layoutStream); |
8819 | } |
8820 | if (symbol->getQualifier().hasSet()) |
8821 | builder.addDecoration(id, spv::DecorationDescriptorSet, num: symbol->getQualifier().layoutSet); |
8822 | else if (IsDescriptorResource(type: symbol->getType())) { |
8823 | // default to 0 |
8824 | builder.addDecoration(id, spv::DecorationDescriptorSet, num: 0); |
8825 | } |
8826 | if (symbol->getQualifier().hasBinding()) |
8827 | builder.addDecoration(id, spv::DecorationBinding, num: symbol->getQualifier().layoutBinding); |
8828 | else if (IsDescriptorResource(type: symbol->getType())) { |
8829 | // default to 0 |
8830 | builder.addDecoration(id, spv::DecorationBinding, num: 0); |
8831 | } |
8832 | if (symbol->getQualifier().hasAttachment()) |
8833 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, num: symbol->getQualifier().layoutAttachment); |
8834 | if (glslangIntermediate->getXfbMode()) { |
8835 | builder.addCapability(cap: spv::CapabilityTransformFeedback); |
8836 | if (symbol->getQualifier().hasXfbBuffer()) { |
8837 | builder.addDecoration(id, spv::DecorationXfbBuffer, num: symbol->getQualifier().layoutXfbBuffer); |
8838 | unsigned stride = glslangIntermediate->getXfbStride(buffer: symbol->getQualifier().layoutXfbBuffer); |
8839 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
8840 | builder.addDecoration(id, spv::DecorationXfbStride, num: stride); |
8841 | } |
8842 | if (symbol->getQualifier().hasXfbOffset()) |
8843 | builder.addDecoration(id, spv::DecorationOffset, num: symbol->getQualifier().layoutXfbOffset); |
8844 | } |
8845 | |
8846 | // add built-in variable decoration |
8847 | if (builtIn != spv::BuiltInMax) { |
8848 | // WorkgroupSize deprecated in spirv1.6 |
8849 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6 || |
8850 | builtIn != spv::BuiltInWorkgroupSize) |
8851 | builder.addDecoration(id, spv::DecorationBuiltIn, num: (int)builtIn); |
8852 | } |
8853 | |
8854 | // Add volatile decoration to HelperInvocation for spirv1.6 and beyond |
8855 | if (builtIn == spv::BuiltInHelperInvocation && |
8856 | glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
8857 | builder.addDecoration(id, spv::DecorationVolatile); |
8858 | } |
8859 | |
8860 | #ifndef GLSLANG_WEB |
8861 | // Subgroup builtins which have input storage class are volatile for ray tracing stages. |
8862 | if (symbol->getType().isImage() || symbol->getQualifier().isPipeInput()) { |
8863 | std::vector<spv::Decoration> memory; |
8864 | TranslateMemoryDecoration(qualifier: symbol->getType().getQualifier(), memory, |
8865 | useVulkanMemoryModel: glslangIntermediate->usingVulkanMemoryModel()); |
8866 | for (unsigned int i = 0; i < memory.size(); ++i) |
8867 | builder.addDecoration(id, memory[i]); |
8868 | } |
8869 | |
8870 | if (builtIn == spv::BuiltInSampleMask) { |
8871 | spv::Decoration decoration; |
8872 | // GL_NV_sample_mask_override_coverage extension |
8873 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
8874 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
8875 | else |
8876 | decoration = (spv::Decoration)spv::DecorationMax; |
8877 | builder.addDecoration(id, decoration); |
8878 | if (decoration != spv::DecorationMax) { |
8879 | builder.addCapability(cap: spv::CapabilitySampleMaskOverrideCoverageNV); |
8880 | builder.addExtension(ext: spv::E_SPV_NV_sample_mask_override_coverage); |
8881 | } |
8882 | } |
8883 | else if (builtIn == spv::BuiltInLayer) { |
8884 | // SPV_NV_viewport_array2 extension |
8885 | if (symbol->getQualifier().layoutViewportRelative) { |
8886 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
8887 | builder.addCapability(cap: spv::CapabilityShaderViewportMaskNV); |
8888 | builder.addExtension(ext: spv::E_SPV_NV_viewport_array2); |
8889 | } |
8890 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
8891 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
8892 | num: symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
8893 | builder.addCapability(cap: spv::CapabilityShaderStereoViewNV); |
8894 | builder.addExtension(ext: spv::E_SPV_NV_stereo_view_rendering); |
8895 | } |
8896 | } |
8897 | |
8898 | if (symbol->getQualifier().layoutPassthrough) { |
8899 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
8900 | builder.addCapability(cap: spv::CapabilityGeometryShaderPassthroughNV); |
8901 | builder.addExtension(ext: spv::E_SPV_NV_geometry_shader_passthrough); |
8902 | } |
8903 | if (symbol->getQualifier().pervertexNV) { |
8904 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
8905 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricNV); |
8906 | builder.addExtension(ext: spv::E_SPV_NV_fragment_shader_barycentric); |
8907 | } |
8908 | |
8909 | if (symbol->getQualifier().pervertexEXT) { |
8910 | builder.addDecoration(id, spv::DecorationPerVertexKHR); |
8911 | builder.addCapability(cap: spv::CapabilityFragmentBarycentricKHR); |
8912 | builder.addExtension(ext: spv::E_SPV_KHR_fragment_shader_barycentric); |
8913 | } |
8914 | |
8915 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
8916 | builder.addExtension(ext: "SPV_GOOGLE_hlsl_functionality1" ); |
8917 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
8918 | symbol->getType().getQualifier().semanticName); |
8919 | } |
8920 | |
8921 | if (symbol->isReference()) { |
8922 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
8923 | spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
8924 | } |
8925 | |
8926 | // |
8927 | // Add SPIR-V decorations for structure (GL_EXT_spirv_intrinsics) |
8928 | // |
8929 | if (symbol->getType().getQualifier().hasSprivDecorate()) { |
8930 | const glslang::TSpirvDecorate& spirvDecorate = symbol->getType().getQualifier().getSpirvDecorate(); |
8931 | |
8932 | // Add spirv_decorate |
8933 | for (auto& decorate : spirvDecorate.decorates) { |
8934 | if (!decorate.second.empty()) { |
8935 | std::vector<unsigned> literals; |
8936 | TranslateLiterals(constants: decorate.second, literals); |
8937 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first), literals); |
8938 | } |
8939 | else |
8940 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first)); |
8941 | } |
8942 | |
8943 | // Add spirv_decorate_id |
8944 | for (auto& decorateId : spirvDecorate.decorateIds) { |
8945 | std::vector<spv::Id> operandIds; |
8946 | assert(!decorateId.second.empty()); |
8947 | for (auto extraOperand : decorateId.second) { |
8948 | if (extraOperand->getQualifier().isSpecConstant()) |
8949 | operandIds.push_back(x: getSymbolId(symbol: extraOperand->getAsSymbolNode())); |
8950 | else |
8951 | operandIds.push_back(x: createSpvConstant(*extraOperand)); |
8952 | } |
8953 | builder.addDecorationId(id, static_cast<spv::Decoration>(decorateId.first), operandIds); |
8954 | } |
8955 | |
8956 | // Add spirv_decorate_string |
8957 | for (auto& decorateString : spirvDecorate.decorateStrings) { |
8958 | std::vector<const char*> strings; |
8959 | assert(!decorateString.second.empty()); |
8960 | for (auto extraOperand : decorateString.second) { |
8961 | const char* string = extraOperand->getConstArray()[0].getSConst()->c_str(); |
8962 | strings.push_back(x: string); |
8963 | } |
8964 | builder.addDecoration(id, static_cast<spv::Decoration>(decorateString.first), strings); |
8965 | } |
8966 | } |
8967 | #endif |
8968 | |
8969 | return id; |
8970 | } |
8971 | |
8972 | #ifndef GLSLANG_WEB |
8973 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
8974 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
8975 | { |
8976 | if (member >= 0) { |
8977 | if (qualifier.perPrimitiveNV) { |
8978 | // Need to add capability/extension for fragment shader. |
8979 | // Mesh shader already adds this by default. |
8980 | if (glslangIntermediate->getStage() == EShLangFragment) { |
8981 | builder.addCapability(cap: spv::CapabilityMeshShadingNV); |
8982 | builder.addExtension(ext: spv::E_SPV_NV_mesh_shader); |
8983 | } |
8984 | builder.addMemberDecoration(id, member: (unsigned)member, spv::DecorationPerPrimitiveNV); |
8985 | } |
8986 | if (qualifier.perViewNV) |
8987 | builder.addMemberDecoration(id, member: (unsigned)member, spv::DecorationPerViewNV); |
8988 | if (qualifier.perTaskNV) |
8989 | builder.addMemberDecoration(id, member: (unsigned)member, spv::DecorationPerTaskNV); |
8990 | } else { |
8991 | if (qualifier.perPrimitiveNV) { |
8992 | // Need to add capability/extension for fragment shader. |
8993 | // Mesh shader already adds this by default. |
8994 | if (glslangIntermediate->getStage() == EShLangFragment) { |
8995 | builder.addCapability(cap: spv::CapabilityMeshShadingNV); |
8996 | builder.addExtension(ext: spv::E_SPV_NV_mesh_shader); |
8997 | } |
8998 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
8999 | } |
9000 | if (qualifier.perViewNV) |
9001 | builder.addDecoration(id, spv::DecorationPerViewNV); |
9002 | if (qualifier.perTaskNV) |
9003 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
9004 | } |
9005 | } |
9006 | #endif |
9007 | |
9008 | // Make a full tree of instructions to build a SPIR-V specialization constant, |
9009 | // or regular constant if possible. |
9010 | // |
9011 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
9012 | // |
9013 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
9014 | // regular constants, which themselves are trees that createSpvConstant() |
9015 | // recursively walks. So, this function walks the "top" of the tree: |
9016 | // - emit specialization constant-building instructions for specConstant |
9017 | // - when running into a non-spec-constant, switch to createSpvConstant() |
9018 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
9019 | { |
9020 | assert(node.getQualifier().isConstant()); |
9021 | |
9022 | // Handle front-end constants first (non-specialization constants). |
9023 | if (! node.getQualifier().specConstant) { |
9024 | // hand off to the non-spec-constant path |
9025 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
9026 | int nextConst = 0; |
9027 | return createSpvConstantFromConstUnionArray(type: node.getType(), node.getAsConstantUnion() ? |
9028 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
9029 | nextConst, specConstant: false); |
9030 | } |
9031 | |
9032 | // We now know we have a specialization constant to build |
9033 | |
9034 | // Extra capabilities may be needed. |
9035 | if (node.getType().contains8BitInt()) |
9036 | builder.addCapability(cap: spv::CapabilityInt8); |
9037 | if (node.getType().contains16BitFloat()) |
9038 | builder.addCapability(cap: spv::CapabilityFloat16); |
9039 | if (node.getType().contains16BitInt()) |
9040 | builder.addCapability(cap: spv::CapabilityInt16); |
9041 | if (node.getType().contains64BitInt()) |
9042 | builder.addCapability(cap: spv::CapabilityInt64); |
9043 | if (node.getType().containsDouble()) |
9044 | builder.addCapability(cap: spv::CapabilityFloat64); |
9045 | |
9046 | // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants, |
9047 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
9048 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
9049 | std::vector<spv::Id> dimConstId; |
9050 | for (int dim = 0; dim < 3; ++dim) { |
9051 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
9052 | dimConstId.push_back(x: builder.makeUintConstant(u: glslangIntermediate->getLocalSize(dim), specConstant: specConst)); |
9053 | if (specConst) { |
9054 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
9055 | num: glslangIntermediate->getLocalSizeSpecId(dim)); |
9056 | } |
9057 | } |
9058 | return builder.makeCompositeConstant(type: builder.makeVectorType(component: builder.makeUintType(width: 32), size: 3), comps: dimConstId, specConst: true); |
9059 | } |
9060 | |
9061 | // An AST node labelled as specialization constant should be a symbol node. |
9062 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
9063 | if (auto* sn = node.getAsSymbolNode()) { |
9064 | spv::Id result; |
9065 | if (auto* sub_tree = sn->getConstSubtree()) { |
9066 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
9067 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
9068 | // will set the builder into spec constant op instruction generating mode. |
9069 | sub_tree->traverse(this); |
9070 | result = accessChainLoad(type: sub_tree->getType()); |
9071 | } else if (auto* const_union_array = &sn->getConstArray()) { |
9072 | int nextConst = 0; |
9073 | result = createSpvConstantFromConstUnionArray(type: sn->getType(), *const_union_array, nextConst, specConstant: true); |
9074 | } else { |
9075 | logger->missingFunctionality(f: "Invalid initializer for spec onstant." ); |
9076 | return spv::NoResult; |
9077 | } |
9078 | builder.addName(result, name: sn->getName().c_str()); |
9079 | return result; |
9080 | } |
9081 | |
9082 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
9083 | // constant sub tree as initializer. |
9084 | logger->missingFunctionality(f: "Neither a front-end constant nor a spec constant." ); |
9085 | return spv::NoResult; |
9086 | } |
9087 | |
9088 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
9089 | // build the aggregate SPIR-V constant. |
9090 | // |
9091 | // If there are not enough elements present in 'consts', 0 will be substituted; |
9092 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
9093 | // |
9094 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
9095 | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
9096 | { |
9097 | // vector of constants for SPIR-V |
9098 | std::vector<spv::Id> spvConsts; |
9099 | |
9100 | // Type is used for struct and array constants |
9101 | spv::Id typeId = convertGlslangToSpvType(type: glslangType); |
9102 | |
9103 | if (glslangType.isArray()) { |
9104 | glslang::TType elementType(glslangType, 0); |
9105 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
9106 | spvConsts.push_back(x: createSpvConstantFromConstUnionArray(glslangType: elementType, consts, nextConst, specConstant: false)); |
9107 | } else if (glslangType.isMatrix()) { |
9108 | glslang::TType vectorType(glslangType, 0); |
9109 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
9110 | spvConsts.push_back(x: createSpvConstantFromConstUnionArray(glslangType: vectorType, consts, nextConst, specConstant: false)); |
9111 | } else if (glslangType.isCoopMat()) { |
9112 | glslang::TType componentType(glslangType.getBasicType()); |
9113 | spvConsts.push_back(x: createSpvConstantFromConstUnionArray(glslangType: componentType, consts, nextConst, specConstant: false)); |
9114 | } else if (glslangType.isStruct()) { |
9115 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
9116 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
9117 | spvConsts.push_back(x: createSpvConstantFromConstUnionArray(glslangType: *iter->type, consts, nextConst, specConstant: false)); |
9118 | } else if (glslangType.getVectorSize() > 1) { |
9119 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
9120 | bool zero = nextConst >= consts.size(); |
9121 | switch (glslangType.getBasicType()) { |
9122 | case glslang::EbtInt: |
9123 | spvConsts.push_back(x: builder.makeIntConstant(i: zero ? 0 : consts[nextConst].getIConst())); |
9124 | break; |
9125 | case glslang::EbtUint: |
9126 | spvConsts.push_back(x: builder.makeUintConstant(u: zero ? 0 : consts[nextConst].getUConst())); |
9127 | break; |
9128 | case glslang::EbtFloat: |
9129 | spvConsts.push_back(x: builder.makeFloatConstant(f: zero ? 0.0F : (float)consts[nextConst].getDConst())); |
9130 | break; |
9131 | case glslang::EbtBool: |
9132 | spvConsts.push_back(x: builder.makeBoolConstant(b: zero ? false : consts[nextConst].getBConst())); |
9133 | break; |
9134 | #ifndef GLSLANG_WEB |
9135 | case glslang::EbtInt8: |
9136 | builder.addCapability(cap: spv::CapabilityInt8); |
9137 | spvConsts.push_back(x: builder.makeInt8Constant(i: zero ? 0 : consts[nextConst].getI8Const())); |
9138 | break; |
9139 | case glslang::EbtUint8: |
9140 | builder.addCapability(cap: spv::CapabilityInt8); |
9141 | spvConsts.push_back(x: builder.makeUint8Constant(u: zero ? 0 : consts[nextConst].getU8Const())); |
9142 | break; |
9143 | case glslang::EbtInt16: |
9144 | builder.addCapability(cap: spv::CapabilityInt16); |
9145 | spvConsts.push_back(x: builder.makeInt16Constant(i: zero ? 0 : consts[nextConst].getI16Const())); |
9146 | break; |
9147 | case glslang::EbtUint16: |
9148 | builder.addCapability(cap: spv::CapabilityInt16); |
9149 | spvConsts.push_back(x: builder.makeUint16Constant(u: zero ? 0 : consts[nextConst].getU16Const())); |
9150 | break; |
9151 | case glslang::EbtInt64: |
9152 | spvConsts.push_back(x: builder.makeInt64Constant(i: zero ? 0 : consts[nextConst].getI64Const())); |
9153 | break; |
9154 | case glslang::EbtUint64: |
9155 | spvConsts.push_back(x: builder.makeUint64Constant(u: zero ? 0 : consts[nextConst].getU64Const())); |
9156 | break; |
9157 | case glslang::EbtDouble: |
9158 | spvConsts.push_back(x: builder.makeDoubleConstant(d: zero ? 0.0 : consts[nextConst].getDConst())); |
9159 | break; |
9160 | case glslang::EbtFloat16: |
9161 | builder.addCapability(cap: spv::CapabilityFloat16); |
9162 | spvConsts.push_back(x: builder.makeFloat16Constant(f16: zero ? 0.0F : (float)consts[nextConst].getDConst())); |
9163 | break; |
9164 | #endif |
9165 | default: |
9166 | assert(0); |
9167 | break; |
9168 | } |
9169 | ++nextConst; |
9170 | } |
9171 | } else { |
9172 | // we have a non-aggregate (scalar) constant |
9173 | bool zero = nextConst >= consts.size(); |
9174 | spv::Id scalar = 0; |
9175 | switch (glslangType.getBasicType()) { |
9176 | case glslang::EbtInt: |
9177 | scalar = builder.makeIntConstant(i: zero ? 0 : consts[nextConst].getIConst(), specConstant); |
9178 | break; |
9179 | case glslang::EbtUint: |
9180 | scalar = builder.makeUintConstant(u: zero ? 0 : consts[nextConst].getUConst(), specConstant); |
9181 | break; |
9182 | case glslang::EbtFloat: |
9183 | scalar = builder.makeFloatConstant(f: zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
9184 | break; |
9185 | case glslang::EbtBool: |
9186 | scalar = builder.makeBoolConstant(b: zero ? false : consts[nextConst].getBConst(), specConstant); |
9187 | break; |
9188 | #ifndef GLSLANG_WEB |
9189 | case glslang::EbtInt8: |
9190 | builder.addCapability(cap: spv::CapabilityInt8); |
9191 | scalar = builder.makeInt8Constant(i: zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
9192 | break; |
9193 | case glslang::EbtUint8: |
9194 | builder.addCapability(cap: spv::CapabilityInt8); |
9195 | scalar = builder.makeUint8Constant(u: zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
9196 | break; |
9197 | case glslang::EbtInt16: |
9198 | builder.addCapability(cap: spv::CapabilityInt16); |
9199 | scalar = builder.makeInt16Constant(i: zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
9200 | break; |
9201 | case glslang::EbtUint16: |
9202 | builder.addCapability(cap: spv::CapabilityInt16); |
9203 | scalar = builder.makeUint16Constant(u: zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
9204 | break; |
9205 | case glslang::EbtInt64: |
9206 | scalar = builder.makeInt64Constant(i: zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
9207 | break; |
9208 | case glslang::EbtUint64: |
9209 | scalar = builder.makeUint64Constant(u: zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
9210 | break; |
9211 | case glslang::EbtDouble: |
9212 | scalar = builder.makeDoubleConstant(d: zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
9213 | break; |
9214 | case glslang::EbtFloat16: |
9215 | builder.addCapability(cap: spv::CapabilityFloat16); |
9216 | scalar = builder.makeFloat16Constant(f16: zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
9217 | break; |
9218 | case glslang::EbtReference: |
9219 | scalar = builder.makeUint64Constant(u: zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
9220 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, operand: scalar); |
9221 | break; |
9222 | #endif |
9223 | case glslang::EbtString: |
9224 | scalar = builder.getStringId(str: consts[nextConst].getSConst()->c_str()); |
9225 | break; |
9226 | default: |
9227 | assert(0); |
9228 | break; |
9229 | } |
9230 | ++nextConst; |
9231 | return scalar; |
9232 | } |
9233 | |
9234 | return builder.makeCompositeConstant(type: typeId, comps: spvConsts); |
9235 | } |
9236 | |
9237 | // Return true if the node is a constant or symbol whose reading has no |
9238 | // non-trivial observable cost or effect. |
9239 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
9240 | { |
9241 | // don't know what this is |
9242 | if (node == nullptr) |
9243 | return false; |
9244 | |
9245 | // a constant is safe |
9246 | if (node->getAsConstantUnion() != nullptr) |
9247 | return true; |
9248 | |
9249 | // not a symbol means non-trivial |
9250 | if (node->getAsSymbolNode() == nullptr) |
9251 | return false; |
9252 | |
9253 | // a symbol, depends on what's being read |
9254 | switch (node->getType().getQualifier().storage) { |
9255 | case glslang::EvqTemporary: |
9256 | case glslang::EvqGlobal: |
9257 | case glslang::EvqIn: |
9258 | case glslang::EvqInOut: |
9259 | case glslang::EvqConst: |
9260 | case glslang::EvqConstReadOnly: |
9261 | case glslang::EvqUniform: |
9262 | return true; |
9263 | default: |
9264 | return false; |
9265 | } |
9266 | } |
9267 | |
9268 | // A node is trivial if it is a single operation with no side effects. |
9269 | // HLSL (and/or vectors) are always trivial, as it does not short circuit. |
9270 | // Otherwise, error on the side of saying non-trivial. |
9271 | // Return true if trivial. |
9272 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
9273 | { |
9274 | if (node == nullptr) |
9275 | return false; |
9276 | |
9277 | // count non scalars as trivial, as well as anything coming from HLSL |
9278 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
9279 | return true; |
9280 | |
9281 | // symbols and constants are trivial |
9282 | if (isTrivialLeaf(node)) |
9283 | return true; |
9284 | |
9285 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
9286 | |
9287 | // not a simple operation |
9288 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
9289 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
9290 | if (binaryNode == nullptr && unaryNode == nullptr) |
9291 | return false; |
9292 | |
9293 | // not on leaf nodes |
9294 | if (binaryNode && (! isTrivialLeaf(node: binaryNode->getLeft()) || ! isTrivialLeaf(node: binaryNode->getRight()))) |
9295 | return false; |
9296 | |
9297 | if (unaryNode && ! isTrivialLeaf(node: unaryNode->getOperand())) { |
9298 | return false; |
9299 | } |
9300 | |
9301 | switch (node->getAsOperator()->getOp()) { |
9302 | case glslang::EOpLogicalNot: |
9303 | case glslang::EOpConvIntToBool: |
9304 | case glslang::EOpConvUintToBool: |
9305 | case glslang::EOpConvFloatToBool: |
9306 | case glslang::EOpConvDoubleToBool: |
9307 | case glslang::EOpEqual: |
9308 | case glslang::EOpNotEqual: |
9309 | case glslang::EOpLessThan: |
9310 | case glslang::EOpGreaterThan: |
9311 | case glslang::EOpLessThanEqual: |
9312 | case glslang::EOpGreaterThanEqual: |
9313 | case glslang::EOpIndexDirect: |
9314 | case glslang::EOpIndexDirectStruct: |
9315 | case glslang::EOpLogicalXor: |
9316 | case glslang::EOpAny: |
9317 | case glslang::EOpAll: |
9318 | return true; |
9319 | default: |
9320 | return false; |
9321 | } |
9322 | } |
9323 | |
9324 | // Emit short-circuiting code, where 'right' is never evaluated unless |
9325 | // the left side is true (for &&) or false (for ||). |
9326 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
9327 | glslang::TIntermTyped& right) |
9328 | { |
9329 | spv::Id boolTypeId = builder.makeBoolType(); |
9330 | |
9331 | // emit left operand |
9332 | builder.clearAccessChain(); |
9333 | left.traverse(this); |
9334 | spv::Id leftId = accessChainLoad(type: left.getType()); |
9335 | |
9336 | // Operands to accumulate OpPhi operands |
9337 | std::vector<spv::Id> phiOperands; |
9338 | // accumulate left operand's phi information |
9339 | phiOperands.push_back(x: leftId); |
9340 | phiOperands.push_back(x: builder.getBuildPoint()->getId()); |
9341 | |
9342 | // Make the two kinds of operation symmetric with a "!" |
9343 | // || => emit "if (! left) result = right" |
9344 | // && => emit "if ( left) result = right" |
9345 | // |
9346 | // TODO: this runtime "not" for || could be avoided by adding functionality |
9347 | // to 'builder' to have an "else" without an "then" |
9348 | if (op == glslang::EOpLogicalOr) |
9349 | leftId = builder.createUnaryOp(spv::OpLogicalNot, typeId: boolTypeId, operand: leftId); |
9350 | |
9351 | // make an "if" based on the left value |
9352 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
9353 | |
9354 | // emit right operand as the "then" part of the "if" |
9355 | builder.clearAccessChain(); |
9356 | right.traverse(this); |
9357 | spv::Id rightId = accessChainLoad(type: right.getType()); |
9358 | |
9359 | // accumulate left operand's phi information |
9360 | phiOperands.push_back(x: rightId); |
9361 | phiOperands.push_back(x: builder.getBuildPoint()->getId()); |
9362 | |
9363 | // finish the "if" |
9364 | ifBuilder.makeEndIf(); |
9365 | |
9366 | // phi together the two results |
9367 | return builder.createOp(spv::OpPhi, typeId: boolTypeId, operands: phiOperands); |
9368 | } |
9369 | |
9370 | #ifndef GLSLANG_WEB |
9371 | // Return type Id of the imported set of extended instructions corresponds to the name. |
9372 | // Import this set if it has not been imported yet. |
9373 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
9374 | { |
9375 | if (extBuiltinMap.find(x: name) != extBuiltinMap.end()) |
9376 | return extBuiltinMap[name]; |
9377 | else { |
9378 | builder.addExtension(ext: name); |
9379 | spv::Id extBuiltins = builder.import(name); |
9380 | extBuiltinMap[name] = extBuiltins; |
9381 | return extBuiltins; |
9382 | } |
9383 | } |
9384 | #endif |
9385 | |
9386 | }; // end anonymous namespace |
9387 | |
9388 | namespace QtShaderTools { |
9389 | namespace glslang { |
9390 | |
9391 | void GetSpirvVersion(std::string& version) |
9392 | { |
9393 | const int bufSize = 100; |
9394 | char buf[bufSize]; |
9395 | snprintf(s: buf, maxlen: bufSize, format: "0x%08x, Revision %d" , spv::Version, spv::Revision); |
9396 | version = buf; |
9397 | } |
9398 | |
9399 | // For low-order part of the generator's magic number. Bump up |
9400 | // when there is a change in the style (e.g., if SSA form changes, |
9401 | // or a different instruction sequence to do something gets used). |
9402 | int GetSpirvGeneratorVersion() |
9403 | { |
9404 | // return 1; // start |
9405 | // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V |
9406 | // return 3; // change/correct barrier-instruction operands, to match memory model group decisions |
9407 | // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component |
9408 | // return 5; // make OpArrayLength result type be an int with signedness of 0 |
9409 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
9410 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
9411 | // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
9412 | // return 8; // switch to new dead block eliminator; use OpUnreachable |
9413 | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
9414 | return 10; // Generate OpFUnordNotEqual for != comparisons |
9415 | } |
9416 | |
9417 | // Write SPIR-V out to a binary file |
9418 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
9419 | { |
9420 | std::ofstream out; |
9421 | out.open(s: baseName, mode: std::ios::binary | std::ios::out); |
9422 | if (out.fail()) |
9423 | printf(format: "ERROR: Failed to open file: %s\n" , baseName); |
9424 | for (int i = 0; i < (int)spirv.size(); ++i) { |
9425 | unsigned int word = spirv[i]; |
9426 | out.write(s: (const char*)&word, n: 4); |
9427 | } |
9428 | out.close(); |
9429 | } |
9430 | |
9431 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
9432 | void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) |
9433 | { |
9434 | #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE) |
9435 | std::ofstream out; |
9436 | out.open(s: baseName, mode: std::ios::binary | std::ios::out); |
9437 | if (out.fail()) |
9438 | printf(format: "ERROR: Failed to open file: %s\n" , baseName); |
9439 | out << "\t// " << |
9440 | GetSpirvGeneratorVersion() << |
9441 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
9442 | GLSLANG_VERSION_FLAVOR << std::endl; |
9443 | if (varName != nullptr) { |
9444 | out << "\t #pragma once" << std::endl; |
9445 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
9446 | } |
9447 | const int WORDS_PER_LINE = 8; |
9448 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
9449 | out << "\t" ; |
9450 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
9451 | const unsigned int word = spirv[i + j]; |
9452 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
9453 | if (i + j + 1 < (int)spirv.size()) { |
9454 | out << "," ; |
9455 | } |
9456 | } |
9457 | out << std::endl; |
9458 | } |
9459 | if (varName != nullptr) { |
9460 | out << "};" ; |
9461 | out << std::endl; |
9462 | } |
9463 | out.close(); |
9464 | #endif |
9465 | } |
9466 | |
9467 | // |
9468 | // Set up the glslang traversal |
9469 | // |
9470 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
9471 | { |
9472 | spv::SpvBuildLogger logger; |
9473 | GlslangToSpv(intermediate, spirv, logger: &logger, options); |
9474 | } |
9475 | |
9476 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
9477 | spv::SpvBuildLogger* logger, SpvOptions* options) |
9478 | { |
9479 | TIntermNode* root = intermediate.getTreeRoot(); |
9480 | |
9481 | if (root == 0) |
9482 | return; |
9483 | |
9484 | SpvOptions defaultOptions; |
9485 | if (options == nullptr) |
9486 | options = &defaultOptions; |
9487 | |
9488 | GetThreadPoolAllocator().push(); |
9489 | |
9490 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
9491 | root->traverse(&it); |
9492 | it.finishSpv(); |
9493 | it.dumpSpv(out&: spirv); |
9494 | |
9495 | #if ENABLE_OPT |
9496 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
9497 | // eg. forward and remove memory writes of opaque types. |
9498 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
9499 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
9500 | SpirvToolsTransform(intermediate, spirv, logger, options); |
9501 | prelegalization = false; |
9502 | } |
9503 | else if (options->stripDebugInfo) { |
9504 | // Strip debug info even if optimization is disabled. |
9505 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
9506 | } |
9507 | |
9508 | if (options->validate) |
9509 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
9510 | |
9511 | if (options->disassemble) |
9512 | SpirvToolsDisassemble(std::cout, spirv); |
9513 | |
9514 | #endif |
9515 | |
9516 | GetThreadPoolAllocator().pop(); |
9517 | } |
9518 | |
9519 | }; // end namespace glslang |
9520 | }; // namespace QtShaderTools |
9521 | |