/src/shaderc/third_party/glslang/SPIRV/GlslangToSpv.cpp
Line | Count | Source |
1 | | // |
2 | | // Copyright (C) 2014-2016 LunarG, Inc. |
3 | | // Copyright (C) 2015-2020 Google, Inc. |
4 | | // Copyright (C) 2017, 2022-2025 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.hpp11" |
44 | | #include "GlslangToSpv.h" |
45 | | #include "SpvBuilder.h" |
46 | | #include "SpvTools.h" |
47 | | #include "spvUtil.h" |
48 | | |
49 | | namespace spv { |
50 | | #include "GLSL.std.450.h" |
51 | | #include "GLSL.ext.KHR.h" |
52 | | #include "GLSL.ext.EXT.h" |
53 | | #include "GLSL.ext.AMD.h" |
54 | | #include "GLSL.ext.NV.h" |
55 | | #include "GLSL.ext.ARM.h" |
56 | | #include "GLSL.ext.QCOM.h" |
57 | | #include "NonSemanticDebugPrintf.h" |
58 | | } |
59 | | |
60 | | // Glslang includes |
61 | | #include "../glslang/MachineIndependent/localintermediate.h" |
62 | | #include "../glslang/MachineIndependent/SymbolTable.h" |
63 | | #include "../glslang/Include/Common.h" |
64 | | |
65 | | // Build-time generated includes |
66 | | #include "glslang/build_info.h" |
67 | | |
68 | | #include <fstream> |
69 | | #include <iomanip> |
70 | | #include <list> |
71 | | #include <map> |
72 | | #include <optional> |
73 | | #include <stack> |
74 | | #include <string> |
75 | | #include <vector> |
76 | | |
77 | | namespace { |
78 | | |
79 | | namespace { |
80 | | class SpecConstantOpModeGuard { |
81 | | public: |
82 | | SpecConstantOpModeGuard(spv::Builder* builder) |
83 | 58.0k | : builder_(builder) { |
84 | 58.0k | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
85 | 58.0k | } |
86 | 58.0k | ~SpecConstantOpModeGuard() { |
87 | 58.0k | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
88 | 58.0k | : builder_->setToNormalCodeGenMode(); |
89 | 58.0k | } |
90 | 1.26k | void turnOnSpecConstantOpMode() { |
91 | 1.26k | builder_->setToSpecConstCodeGenMode(); |
92 | 1.26k | } |
93 | | |
94 | | private: |
95 | | spv::Builder* builder_; |
96 | | bool previous_flag_; |
97 | | }; |
98 | | |
99 | | struct OpDecorations { |
100 | | public: |
101 | | OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
102 | 6.96k | precision(precision) |
103 | | , |
104 | 6.96k | noContraction(noContraction), |
105 | 6.96k | nonUniform(nonUniform) |
106 | 6.96k | { } |
107 | | |
108 | | spv::Decoration precision; |
109 | | |
110 | 4.69k | void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); } |
111 | 6.75k | void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); } |
112 | | protected: |
113 | | spv::Decoration noContraction; |
114 | | spv::Decoration nonUniform; |
115 | | }; |
116 | | |
117 | | void addDerivativeGroupExecutionMode(spv::Builder& builder, const glslang::TIntermediate& intermediate, |
118 | | spv::Function* shaderEntry) |
119 | 0 | { |
120 | 0 | if (intermediate.getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
121 | 0 | if (intermediate.getLayoutDerivativeExtension() == glslang::EdgKHR) { |
122 | 0 | builder.addCapability(spv::Capability::ComputeDerivativeGroupQuadsKHR); |
123 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::DerivativeGroupQuadsKHR); |
124 | 0 | builder.addExtension(spv::E_SPV_KHR_compute_shader_derivatives); |
125 | 0 | } else { |
126 | 0 | builder.addCapability(spv::Capability::ComputeDerivativeGroupQuadsNV); |
127 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::DerivativeGroupQuadsNV); |
128 | 0 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
129 | 0 | } |
130 | 0 | } else if (intermediate.getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
131 | 0 | if (intermediate.getLayoutDerivativeExtension() == glslang::EdgKHR) { |
132 | 0 | builder.addCapability(spv::Capability::ComputeDerivativeGroupLinearKHR); |
133 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::DerivativeGroupLinearKHR); |
134 | 0 | builder.addExtension(spv::E_SPV_KHR_compute_shader_derivatives); |
135 | 0 | } else { |
136 | 0 | builder.addCapability(spv::Capability::ComputeDerivativeGroupLinearNV); |
137 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::DerivativeGroupLinearNV); |
138 | 0 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
139 | 0 | } |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | } // namespace |
144 | | |
145 | | // |
146 | | // The main holder of information for translating glslang to SPIR-V. |
147 | | // |
148 | | // Derives from the AST walking base class. |
149 | | // |
150 | | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
151 | | public: |
152 | | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
153 | | glslang::SpvOptions& options); |
154 | 370 | virtual ~TGlslangToSpvTraverser() { } |
155 | | |
156 | | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*) override; |
157 | | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*) override; |
158 | | void visitConstantUnion(glslang::TIntermConstantUnion*) override; |
159 | | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*) override; |
160 | | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*) override; |
161 | | void visitSymbol(glslang::TIntermSymbol* symbol) override; |
162 | | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*) override; |
163 | | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*) override; |
164 | | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*) override; |
165 | | bool visitVariableDecl(glslang::TVisit, glslang::TIntermVariableDecl*) override; |
166 | | |
167 | | void finishSpv(bool compileOnly); |
168 | | void dumpSpv(std::vector<unsigned int>& out); |
169 | | |
170 | | protected: |
171 | | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
172 | | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
173 | | |
174 | | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
175 | | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
176 | | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
177 | | spv::Decoration TranslateNonUniformDecoration(const spv::Builder::AccessChain::CoherentFlags& coherentFlags); |
178 | | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
179 | | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
180 | | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
181 | | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
182 | | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
183 | | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
184 | | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
185 | | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
186 | | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
187 | | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
188 | | void TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>&, std::vector<unsigned>&) const; |
189 | | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
190 | | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
191 | | spv::Id getSampledType(const glslang::TSampler&); |
192 | | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
193 | | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
194 | | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
195 | | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
196 | | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
197 | | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
198 | | void applySpirvDecorate(const glslang::TType& type, spv::Id id, std::optional<int> member); |
199 | | bool filterMember(const glslang::TType& member); |
200 | | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
201 | | glslang::TLayoutPacking, const glslang::TQualifier&); |
202 | | spv::LinkageType convertGlslangLinkageToSpv(glslang::TLinkType glslangLinkType); |
203 | | spv::Id decorateDescHeapType(const glslang::TType& type, spv::Id& memberBaseOffset, spv::Id& alignment, |
204 | | int& maxPlainDataAlignment); |
205 | | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
206 | | const glslang::TQualifier&, spv::Id, const std::vector<spv::Id>& spvMembers); |
207 | | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim, bool allowZero = false, bool boolType = false); |
208 | | spv::Id accessChainLoad(const glslang::TType& type); |
209 | | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
210 | | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
211 | | spv::Id convertLoadedBoolInUniformToUint(const glslang::TType& type, spv::Id nominalTypeId, spv::Id loadedId); |
212 | | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
213 | | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
214 | | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
215 | | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
216 | | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
217 | | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
218 | | |
219 | | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
220 | | bool writableParam(glslang::TStorageQualifier) const; |
221 | | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
222 | | void makeFunctions(const glslang::TIntermSequence&); |
223 | | void makeGlobalInitializers(const glslang::TIntermSequence&); |
224 | | void collectRayTracingLinkerObjects(); |
225 | | void visitFunctions(const glslang::TIntermSequence&); |
226 | | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
227 | | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
228 | | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
229 | | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
230 | | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
231 | | |
232 | | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
233 | | glslang::TBasicType typeProxy, bool reduceComparison = true); |
234 | | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
235 | | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
236 | | glslang::TBasicType typeProxy, |
237 | | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags, |
238 | | const glslang::TType &opType); |
239 | | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
240 | | glslang::TBasicType typeProxy); |
241 | | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
242 | | glslang::TBasicType resultBasicType, glslang::TBasicType operandBasicType); |
243 | | spv::Id createIntWidthConversion(spv::Id operand, int vectorSize, spv::Id destType, |
244 | | glslang::TBasicType resultBasicType, glslang::TBasicType operandBasicType); |
245 | | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
246 | | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
247 | | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
248 | | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags, |
249 | | const glslang::TType &opType); |
250 | | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
251 | | glslang::TBasicType typeProxy); |
252 | | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
253 | | spv::Id typeId, std::vector<spv::Id>& operands); |
254 | | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
255 | | glslang::TBasicType typeProxy); |
256 | | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
257 | | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
258 | | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
259 | | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
260 | | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
261 | | bool hasQCOMImageProceessingDecoration(spv::Id id, spv::Decoration decor); |
262 | | void addImageProcessingQCOMDecoration(spv::Id id, spv::Decoration decor); |
263 | | void addImageProcessing2QCOMDecoration(spv::Id id, bool isForGather); |
264 | | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
265 | | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
266 | | int& nextConst, bool specConstant); |
267 | | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
268 | | bool isTrivial(const glslang::TIntermTyped* node); |
269 | | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
270 | | spv::Id getExtBuiltins(const char* name); |
271 | | std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&); |
272 | | spv::Id translateForcedType(spv::Id object); |
273 | | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
274 | | void recordDescHeapAccessChainInfo(glslang::TIntermBinary* node); |
275 | | void createAbortEXT(const glslang::TIntermSequence &glslangOperands); |
276 | | |
277 | | glslang::SpvOptions& options; |
278 | | spv::Function* shaderEntry; |
279 | | spv::Function* currentFunction; |
280 | | spv::Instruction* entryPoint; |
281 | | int sequenceDepth; |
282 | | |
283 | | spv::SpvBuildLogger* logger; |
284 | | |
285 | | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
286 | | spv::Builder builder; |
287 | | bool inEntryPoint; |
288 | | bool entryPointTerminated; |
289 | | bool linkageOnly; // true when visiting the set of objects in the AST present only for |
290 | | // establishing interface, whether or not they were statically used |
291 | | std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
292 | | const glslang::TIntermediate* glslangIntermediate; |
293 | | bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
294 | | spv::Id stdBuiltins; |
295 | | spv::Id nonSemanticDebugPrintf; |
296 | | std::unordered_map<std::string, spv::Id> extBuiltinMap; |
297 | | |
298 | | std::unordered_map<long long, spv::Id> symbolValues; |
299 | | std::unordered_map<uint32_t, spv::Id> builtInVariableIds; |
300 | | std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues, |
301 | | // rather than a pointer |
302 | | std::unordered_map<std::string, spv::Function*> functionMap; |
303 | | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
304 | | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
305 | | std::unordered_map<long long, std::vector<int>> memberRemapper; |
306 | | // for mapping glslang symbol struct to symbol Id |
307 | | std::unordered_map<const glslang::TTypeList*, long long> glslangTypeToIdMap; |
308 | | std::stack<bool> breakForLoop; // false means break for switch |
309 | | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
310 | | // Map pointee types for EbtReference to their forward pointers |
311 | | std::map<const glslang::TType *, spv::Id> forwardPointers; |
312 | | // Type forcing, for when SPIR-V wants a different type than the AST, |
313 | | // requiring local translation to and from SPIR-V type on every access. |
314 | | // Maps <builtin-variable-id -> AST-required-type-id> |
315 | | std::unordered_map<spv::Id, spv::Id> forceType; |
316 | | // Used by Task shader while generating opearnds for OpEmitMeshTasksEXT |
317 | | spv::Id taskPayloadID; |
318 | | // Used later for generating OpTraceKHR/OpExecuteCallableKHR/OpHitObjectRecordHit*/OpHitObjectGetShaderBindingTableData |
319 | | std::unordered_map<unsigned int, glslang::TIntermSymbol *> locationToSymbol[4]; |
320 | | std::unordered_map<spv::Id, std::vector<spv::Decoration> > idToQCOMDecorations; |
321 | | // For nested or inner resource heap structure's alignment and offset records. |
322 | | typedef struct heapMetaData { |
323 | | spv::Id typeStride; |
324 | | spv::Id maxRsrcTypeAlignment; |
325 | | int maxPlainDataAlignment; |
326 | | } HeapMetaData; |
327 | | std::unordered_map<const glslang::TType*, HeapMetaData> heapStructureTypeMetaData; |
328 | | std::unordered_map<spv::Id, spv::Id> heapStructureTypeSize; |
329 | | std::vector<spv::Id> heapStructureMemberOffsets; |
330 | | }; |
331 | | |
332 | | // |
333 | | // Helper functions for translating glslang representations to SPIR-V enumerants. |
334 | | // |
335 | | |
336 | | // Translate glslang profile to SPIR-V source language. |
337 | | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
338 | 370 | { |
339 | 370 | switch (source) { |
340 | 370 | case glslang::EShSourceGlsl: |
341 | 370 | switch (profile) { |
342 | 0 | case ENoProfile: |
343 | 370 | case ECoreProfile: |
344 | 370 | case ECompatibilityProfile: |
345 | 370 | return spv::SourceLanguage::GLSL; |
346 | 0 | case EEsProfile: |
347 | 0 | return spv::SourceLanguage::ESSL; |
348 | 0 | default: |
349 | 0 | return spv::SourceLanguage::Unknown; |
350 | 370 | } |
351 | 0 | case glslang::EShSourceHlsl: |
352 | 0 | return spv::SourceLanguage::HLSL; |
353 | 0 | default: |
354 | 0 | return spv::SourceLanguage::Unknown; |
355 | 370 | } |
356 | 370 | } |
357 | | |
358 | | // Translate glslang language (stage) to SPIR-V execution model. |
359 | | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage, bool isMeshShaderEXT = false) |
360 | 370 | { |
361 | 370 | switch (stage) { |
362 | 339 | case EShLangVertex: return spv::ExecutionModel::Vertex; |
363 | 31 | case EShLangFragment: return spv::ExecutionModel::Fragment; |
364 | 0 | case EShLangCompute: return spv::ExecutionModel::GLCompute; |
365 | 0 | case EShLangTessControl: return spv::ExecutionModel::TessellationControl; |
366 | 0 | case EShLangTessEvaluation: return spv::ExecutionModel::TessellationEvaluation; |
367 | 0 | case EShLangGeometry: return spv::ExecutionModel::Geometry; |
368 | 0 | case EShLangRayGen: return spv::ExecutionModel::RayGenerationKHR; |
369 | 0 | case EShLangIntersect: return spv::ExecutionModel::IntersectionKHR; |
370 | 0 | case EShLangAnyHit: return spv::ExecutionModel::AnyHitKHR; |
371 | 0 | case EShLangClosestHit: return spv::ExecutionModel::ClosestHitKHR; |
372 | 0 | case EShLangMiss: return spv::ExecutionModel::MissKHR; |
373 | 0 | case EShLangCallable: return spv::ExecutionModel::CallableKHR; |
374 | 0 | case EShLangTask: return (isMeshShaderEXT)? spv::ExecutionModel::TaskEXT : spv::ExecutionModel::TaskNV; |
375 | 0 | case EShLangMesh: return (isMeshShaderEXT)? spv::ExecutionModel::MeshEXT : spv::ExecutionModel::MeshNV; |
376 | 0 | default: |
377 | 0 | assert(0); |
378 | 0 | return spv::ExecutionModel::Fragment; |
379 | 370 | } |
380 | 370 | } |
381 | | |
382 | | // Translate glslang sampler type to SPIR-V dimensionality. |
383 | | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
384 | 1.33k | { |
385 | 1.33k | switch (sampler.dim) { |
386 | 240 | case glslang::Esd1D: return spv::Dim::Dim1D; |
387 | 788 | case glslang::Esd2D: return spv::Dim::Dim2D; |
388 | 79 | case glslang::Esd3D: return spv::Dim::Dim3D; |
389 | 172 | case glslang::EsdCube: return spv::Dim::Cube; |
390 | 30 | case glslang::EsdRect: return spv::Dim::Rect; |
391 | 19 | case glslang::EsdBuffer: return spv::Dim::Buffer; |
392 | 6 | case glslang::EsdSubpass: return spv::Dim::SubpassData; |
393 | 0 | case glslang::EsdAttachmentEXT: return spv::Dim::TileImageDataEXT; |
394 | 0 | default: |
395 | 0 | assert(0); |
396 | 0 | return spv::Dim::Dim2D; |
397 | 1.33k | } |
398 | 1.33k | } |
399 | | |
400 | | // Translate glslang precision to SPIR-V precision decorations. |
401 | | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
402 | 61.0k | { |
403 | 61.0k | switch (glslangPrecision) { |
404 | 18 | case glslang::EpqLow: return spv::Decoration::RelaxedPrecision; |
405 | 18 | case glslang::EpqMedium: return spv::Decoration::RelaxedPrecision; |
406 | 61.0k | default: |
407 | 61.0k | return spv::NoPrecision; |
408 | 61.0k | } |
409 | 61.0k | } |
410 | | |
411 | | // Translate glslang type to SPIR-V precision decorations. |
412 | | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
413 | 42.5k | { |
414 | 42.5k | return TranslatePrecisionDecoration(type.getQualifier().precision); |
415 | 42.5k | } |
416 | | |
417 | | // Translate glslang type to SPIR-V block decorations. |
418 | | spv::Decoration TranslateBlockDecoration(const glslang::TStorageQualifier storage, bool useStorageBuffer) |
419 | 377 | { |
420 | 377 | switch (storage) { |
421 | 220 | case glslang::EvqUniform: return spv::Decoration::Block; |
422 | 157 | case glslang::EvqBuffer: return useStorageBuffer ? spv::Decoration::Block : spv::Decoration::BufferBlock; |
423 | 0 | case glslang::EvqVaryingIn: return spv::Decoration::Block; |
424 | 0 | case glslang::EvqVaryingOut: return spv::Decoration::Block; |
425 | 0 | case glslang::EvqShared: return spv::Decoration::Block; |
426 | 0 | case glslang::EvqPayload: return spv::Decoration::Block; |
427 | 0 | case glslang::EvqPayloadIn: return spv::Decoration::Block; |
428 | 0 | case glslang::EvqHitAttr: return spv::Decoration::Block; |
429 | 0 | case glslang::EvqCallableData: return spv::Decoration::Block; |
430 | 0 | case glslang::EvqCallableDataIn: return spv::Decoration::Block; |
431 | 0 | case glslang::EvqHitObjectAttrNV: return spv::Decoration::Block; |
432 | 0 | case glslang::EvqHitObjectAttrEXT: return spv::Decoration::Block; |
433 | 0 | case glslang::EvqResourceHeap: return spv::Decoration::Block; |
434 | 0 | case glslang::EvqSamplerHeap: return spv::Decoration::Block; |
435 | 0 | default: |
436 | 0 | assert(0); |
437 | 0 | break; |
438 | 377 | } |
439 | | |
440 | 0 | return spv::Decoration::Max; |
441 | 377 | } |
442 | | |
443 | | // Translate glslang type to SPIR-V memory decorations. |
444 | | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
445 | | bool useVulkanMemoryModel) |
446 | 5.58k | { |
447 | 5.58k | if (!useVulkanMemoryModel) { |
448 | 5.58k | if (qualifier.isVolatile()) { |
449 | 532 | memory.push_back(spv::Decoration::Volatile); |
450 | 532 | memory.push_back(spv::Decoration::Coherent); |
451 | 5.05k | } else if (qualifier.isCoherent()) { |
452 | 0 | memory.push_back(spv::Decoration::Coherent); |
453 | 0 | } |
454 | 5.58k | } |
455 | 5.58k | if (qualifier.isRestrict()) |
456 | 0 | memory.push_back(spv::Decoration::Restrict); |
457 | 5.58k | if (qualifier.isReadOnly()) |
458 | 0 | memory.push_back(spv::Decoration::NonWritable); |
459 | 5.58k | if (qualifier.isWriteOnly()) |
460 | 0 | memory.push_back(spv::Decoration::NonReadable); |
461 | 5.58k | } |
462 | | |
463 | | // Translate glslang type to SPIR-V layout decorations. |
464 | | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
465 | 2.54k | { |
466 | 2.54k | if (type.isMatrix()) { |
467 | 96 | switch (matrixLayout) { |
468 | 10 | case glslang::ElmRowMajor: |
469 | 10 | return spv::Decoration::RowMajor; |
470 | 32 | case glslang::ElmColumnMajor: |
471 | 32 | return spv::Decoration::ColMajor; |
472 | 54 | default: |
473 | | // opaque layouts don't need a majorness |
474 | 54 | return spv::Decoration::Max; |
475 | 96 | } |
476 | 2.45k | } else { |
477 | 2.45k | switch (type.getBasicType()) { |
478 | 2.07k | default: |
479 | 2.07k | return spv::Decoration::Max; |
480 | 0 | break; |
481 | 377 | case glslang::EbtBlock: |
482 | 377 | switch (type.getQualifier().storage) { |
483 | 0 | case glslang::EvqShared: |
484 | 220 | case glslang::EvqUniform: |
485 | 377 | case glslang::EvqBuffer: |
486 | 377 | switch (type.getQualifier().layoutPacking) { |
487 | 0 | case glslang::ElpShared: return spv::Decoration::GLSLShared; |
488 | 0 | case glslang::ElpPacked: return spv::Decoration::GLSLPacked; |
489 | 377 | default: |
490 | 377 | return spv::Decoration::Max; |
491 | 377 | } |
492 | 0 | case glslang::EvqVaryingIn: |
493 | 0 | case glslang::EvqVaryingOut: |
494 | 0 | if (type.getQualifier().isTaskMemory()) { |
495 | 0 | switch (type.getQualifier().layoutPacking) { |
496 | 0 | case glslang::ElpShared: return spv::Decoration::GLSLShared; |
497 | 0 | case glslang::ElpPacked: return spv::Decoration::GLSLPacked; |
498 | 0 | default: break; |
499 | 0 | } |
500 | 0 | } else { |
501 | 0 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
502 | 0 | } |
503 | 0 | return spv::Decoration::Max; |
504 | 0 | case glslang::EvqPayload: |
505 | 0 | case glslang::EvqPayloadIn: |
506 | 0 | case glslang::EvqHitAttr: |
507 | 0 | case glslang::EvqCallableData: |
508 | 0 | case glslang::EvqCallableDataIn: |
509 | 0 | case glslang::EvqHitObjectAttrNV: |
510 | 0 | case glslang::EvqHitObjectAttrEXT: |
511 | 0 | case glslang::EvqResourceHeap: |
512 | 0 | case glslang::EvqSamplerHeap: |
513 | 0 | return spv::Decoration::Max; |
514 | 0 | default: |
515 | 0 | assert(0); |
516 | 0 | return spv::Decoration::Max; |
517 | 377 | } |
518 | 2.45k | } |
519 | 2.45k | } |
520 | 2.54k | } |
521 | | |
522 | | // Translate glslang type to SPIR-V interpolation decorations. |
523 | | // Returns spv::Decoration::Max when no decoration |
524 | | // should be applied. |
525 | | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
526 | 4.35k | { |
527 | 4.35k | if (qualifier.smooth) |
528 | | // Smooth decoration doesn't exist in SPIR-V 1.0 |
529 | 190 | return spv::Decoration::Max; |
530 | 4.16k | else if (qualifier.isNonPerspective()) |
531 | 0 | return spv::Decoration::NoPerspective; |
532 | 4.16k | else if (qualifier.flat) |
533 | 18 | return spv::Decoration::Flat; |
534 | 4.14k | else if (qualifier.isExplicitInterpolation()) { |
535 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
536 | 0 | return spv::Decoration::ExplicitInterpAMD; |
537 | 0 | } |
538 | 4.14k | else |
539 | 4.14k | return spv::Decoration::Max; |
540 | 4.35k | } |
541 | | |
542 | | // Translate glslang type to SPIR-V auxiliary storage decorations. |
543 | | // Returns spv::Decoration::Max when no decoration |
544 | | // should be applied. |
545 | | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
546 | 4.35k | { |
547 | 4.35k | if (qualifier.centroid) |
548 | 0 | return spv::Decoration::Centroid; |
549 | 4.35k | else if (qualifier.patch) |
550 | 0 | return spv::Decoration::Patch; |
551 | 4.35k | else if (qualifier.sample) { |
552 | 0 | builder.addCapability(spv::Capability::SampleRateShading); |
553 | 0 | return spv::Decoration::Sample; |
554 | 0 | } |
555 | | |
556 | 4.35k | return spv::Decoration::Max; |
557 | 4.35k | } |
558 | | |
559 | | // If glslang type is invariant, return SPIR-V invariant decoration. |
560 | | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
561 | 6.66k | { |
562 | 6.66k | if (qualifier.invariant) |
563 | 0 | return spv::Decoration::Invariant; |
564 | 6.66k | else |
565 | 6.66k | return spv::Decoration::Max; |
566 | 6.66k | } |
567 | | |
568 | | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
569 | | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
570 | 6.96k | { |
571 | 6.96k | if (qualifier.isNoContraction()) |
572 | 0 | return spv::Decoration::NoContraction; |
573 | 6.96k | else |
574 | 6.96k | return spv::Decoration::Max; |
575 | 6.96k | } |
576 | | |
577 | | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
578 | | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
579 | 38.5k | { |
580 | 38.5k | if (qualifier.isNonUniform()) { |
581 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
582 | 0 | builder.addCapability(spv::Capability::ShaderNonUniformEXT); |
583 | | |
584 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
585 | 0 | if (extensions.find("GL_EXT_descriptor_heap") != extensions.end()) { |
586 | 0 | builder.addExtension("SPV_EXT_descriptor_heap"); |
587 | 0 | builder.addCapability(spv::Capability::DescriptorHeapEXT); |
588 | 0 | } |
589 | 0 | else { |
590 | 0 | return spv::Decoration::NonUniformEXT; |
591 | 0 | } |
592 | 0 | } |
593 | | |
594 | 38.5k | return spv::Decoration::Max; |
595 | 38.5k | } |
596 | | |
597 | | // If lvalue flags contains nonUniform, return SPIR-V NonUniform decoration. |
598 | | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration( |
599 | | const spv::Builder::AccessChain::CoherentFlags& coherentFlags) |
600 | 43.1k | { |
601 | 43.1k | if (coherentFlags.isNonUniform()) { |
602 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
603 | 0 | builder.addCapability(spv::Capability::ShaderNonUniformEXT); |
604 | | |
605 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
606 | 0 | if (extensions.find("GL_EXT_descriptor_heap") != extensions.end()) { |
607 | 0 | builder.addExtension("SPV_EXT_descriptor_heap"); |
608 | 0 | builder.addCapability(spv::Capability::DescriptorHeapEXT); |
609 | 0 | } |
610 | 0 | else { |
611 | 0 | return spv::Decoration::NonUniformEXT; |
612 | 0 | } |
613 | 0 | } |
614 | | |
615 | 43.1k | return spv::Decoration::Max; |
616 | 43.1k | } |
617 | | |
618 | | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
619 | | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
620 | 38.4k | { |
621 | 38.4k | spv::MemoryAccessMask mask = spv::MemoryAccessMask::MaskNone; |
622 | | |
623 | 38.4k | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
624 | 38.4k | return mask; |
625 | | |
626 | 0 | if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
627 | 0 | mask = mask | spv::MemoryAccessMask::MakePointerAvailableKHR | |
628 | 0 | spv::MemoryAccessMask::MakePointerVisibleKHR; |
629 | 0 | } |
630 | |
|
631 | 0 | if (coherentFlags.nonprivate) { |
632 | 0 | mask = mask | spv::MemoryAccessMask::NonPrivatePointerKHR; |
633 | 0 | } |
634 | 0 | if (coherentFlags.volatil) { |
635 | 0 | mask = mask | spv::MemoryAccessMask::Volatile; |
636 | 0 | } |
637 | 0 | if (coherentFlags.nontemporal) { |
638 | 0 | mask = mask | spv::MemoryAccessMask::Nontemporal; |
639 | 0 | } |
640 | 0 | if (mask != spv::MemoryAccessMask::MaskNone) { |
641 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
642 | 0 | } |
643 | |
|
644 | 0 | return mask; |
645 | 38.4k | } |
646 | | |
647 | | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
648 | | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
649 | 386 | { |
650 | 386 | spv::ImageOperandsMask mask = spv::ImageOperandsMask::MaskNone; |
651 | | |
652 | 386 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
653 | 386 | return mask; |
654 | | |
655 | 0 | if (coherentFlags.volatil || |
656 | 0 | coherentFlags.anyCoherent()) { |
657 | 0 | mask = mask | spv::ImageOperandsMask::MakeTexelAvailableKHR | |
658 | 0 | spv::ImageOperandsMask::MakeTexelVisibleKHR; |
659 | 0 | } |
660 | 0 | if (coherentFlags.nonprivate) { |
661 | 0 | mask = mask | spv::ImageOperandsMask::NonPrivateTexelKHR; |
662 | 0 | } |
663 | 0 | if (coherentFlags.volatil) { |
664 | 0 | mask = mask | spv::ImageOperandsMask::VolatileTexelKHR; |
665 | 0 | } |
666 | 0 | if (coherentFlags.nontemporal && builder.getSpvVersion() >= spv::Spv_1_6) { |
667 | 0 | mask = mask | spv::ImageOperandsMask::Nontemporal; |
668 | 0 | } |
669 | 0 | if (mask != spv::ImageOperandsMask::MaskNone) { |
670 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
671 | 0 | } |
672 | |
|
673 | 0 | return mask; |
674 | 386 | } |
675 | | |
676 | | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
677 | 53.3k | { |
678 | 53.3k | spv::Builder::AccessChain::CoherentFlags flags = {}; |
679 | 53.3k | flags.coherent = type.getQualifier().coherent; |
680 | 53.3k | flags.devicecoherent = type.getQualifier().devicecoherent; |
681 | 53.3k | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
682 | | // shared variables are implicitly workgroupcoherent in GLSL. |
683 | 53.3k | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
684 | 53.3k | type.getQualifier().storage == glslang::EvqShared; |
685 | 53.3k | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
686 | 53.3k | flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
687 | 53.3k | flags.volatil = type.getQualifier().volatil; |
688 | 53.3k | flags.nontemporal = type.getQualifier().nontemporal; |
689 | | // *coherent variables are implicitly nonprivate in GLSL |
690 | 53.3k | flags.nonprivate = type.getQualifier().nonprivate || |
691 | 53.3k | flags.anyCoherent() || |
692 | 52.2k | flags.volatil; |
693 | 53.3k | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
694 | 53.3k | flags.nonUniform = type.getQualifier().nonUniform; |
695 | 53.3k | return flags; |
696 | 53.3k | } |
697 | | |
698 | | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
699 | | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
700 | 38.4k | { |
701 | 38.4k | spv::Scope scope = spv::Scope::Max; |
702 | | |
703 | 38.4k | if (coherentFlags.volatil || coherentFlags.coherent) { |
704 | | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
705 | 0 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::Scope::QueueFamilyKHR : spv::Scope::Device; |
706 | 38.4k | } else if (coherentFlags.devicecoherent) { |
707 | 0 | scope = spv::Scope::Device; |
708 | 38.4k | } else if (coherentFlags.queuefamilycoherent) { |
709 | 0 | scope = spv::Scope::QueueFamilyKHR; |
710 | 38.4k | } else if (coherentFlags.workgroupcoherent) { |
711 | 0 | scope = spv::Scope::Workgroup; |
712 | 38.4k | } else if (coherentFlags.subgroupcoherent) { |
713 | 0 | scope = spv::Scope::Subgroup; |
714 | 38.4k | } else if (coherentFlags.shadercallcoherent) { |
715 | 0 | scope = spv::Scope::ShaderCallKHR; |
716 | 0 | } |
717 | 38.4k | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::Scope::Device) { |
718 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelDeviceScopeKHR); |
719 | 0 | } |
720 | | |
721 | 38.4k | return scope; |
722 | 38.4k | } |
723 | | |
724 | | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
725 | | // associated capabilities when required. For some built-in variables, a capability |
726 | | // is generated only when using the variable in an executable instruction, but not when |
727 | | // just declaring a struct member variable with it. This is true for PointSize, |
728 | | // ClipDistance, and CullDistance. |
729 | | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
730 | | bool memberDeclaration) |
731 | 6.66k | { |
732 | 6.66k | switch (builtIn) { |
733 | 0 | case glslang::EbvPointSize: |
734 | | // Defer adding the capability until the built-in is actually used. |
735 | 0 | if (! memberDeclaration) { |
736 | 0 | switch (glslangIntermediate->getStage()) { |
737 | 0 | case EShLangGeometry: |
738 | 0 | builder.addCapability(spv::Capability::GeometryPointSize); |
739 | 0 | break; |
740 | 0 | case EShLangTessControl: |
741 | 0 | case EShLangTessEvaluation: |
742 | 0 | builder.addCapability(spv::Capability::TessellationPointSize); |
743 | 0 | break; |
744 | 0 | default: |
745 | 0 | break; |
746 | 0 | } |
747 | 0 | } |
748 | 0 | return spv::BuiltIn::PointSize; |
749 | | |
750 | 0 | case glslang::EbvPosition: return spv::BuiltIn::Position; |
751 | 0 | case glslang::EbvVertexId: return spv::BuiltIn::VertexId; |
752 | 0 | case glslang::EbvInstanceId: return spv::BuiltIn::InstanceId; |
753 | 0 | case glslang::EbvVertexIndex: return spv::BuiltIn::VertexIndex; |
754 | 0 | case glslang::EbvInstanceIndex: return spv::BuiltIn::InstanceIndex; |
755 | | |
756 | 0 | case glslang::EbvFragCoord: return spv::BuiltIn::FragCoord; |
757 | 0 | case glslang::EbvPointCoord: return spv::BuiltIn::PointCoord; |
758 | 0 | case glslang::EbvFace: return spv::BuiltIn::FrontFacing; |
759 | 0 | case glslang::EbvFragDepth: return spv::BuiltIn::FragDepth; |
760 | | |
761 | 0 | case glslang::EbvNumWorkGroups: return spv::BuiltIn::NumWorkgroups; |
762 | 0 | case glslang::EbvWorkGroupSize: return spv::BuiltIn::WorkgroupSize; |
763 | 0 | case glslang::EbvWorkGroupId: return spv::BuiltIn::WorkgroupId; |
764 | 0 | case glslang::EbvLocalInvocationId: return spv::BuiltIn::LocalInvocationId; |
765 | 0 | case glslang::EbvLocalInvocationIndex: return spv::BuiltIn::LocalInvocationIndex; |
766 | 0 | case glslang::EbvGlobalInvocationId: return spv::BuiltIn::GlobalInvocationId; |
767 | | |
768 | | // These *Distance capabilities logically belong here, but if the member is declared and |
769 | | // then never used, consumers of SPIR-V prefer the capability not be declared. |
770 | | // They are now generated when used, rather than here when declared. |
771 | | // Potentially, the specification should be more clear what the minimum |
772 | | // use needed is to trigger the capability. |
773 | | // |
774 | 0 | case glslang::EbvClipDistance: |
775 | 0 | if (!memberDeclaration) |
776 | 0 | builder.addCapability(spv::Capability::ClipDistance); |
777 | 0 | return spv::BuiltIn::ClipDistance; |
778 | | |
779 | 0 | case glslang::EbvCullDistance: |
780 | 0 | if (!memberDeclaration) |
781 | 0 | builder.addCapability(spv::Capability::CullDistance); |
782 | 0 | return spv::BuiltIn::CullDistance; |
783 | | |
784 | 0 | case glslang::EbvViewportIndex: |
785 | 0 | if (glslangIntermediate->getStage() == EShLangGeometry || |
786 | 0 | glslangIntermediate->getStage() == EShLangFragment) { |
787 | 0 | builder.addCapability(spv::Capability::MultiViewport); |
788 | 0 | } |
789 | 0 | if (glslangIntermediate->getStage() == EShLangVertex || |
790 | 0 | glslangIntermediate->getStage() == EShLangTessControl || |
791 | 0 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
792 | |
|
793 | 0 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
794 | 0 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
795 | 0 | builder.addCapability(spv::Capability::ShaderViewportIndexLayerEXT); |
796 | 0 | } |
797 | 0 | else |
798 | 0 | builder.addCapability(spv::Capability::ShaderViewportIndex); |
799 | 0 | } |
800 | 0 | return spv::BuiltIn::ViewportIndex; |
801 | | |
802 | 0 | case glslang::EbvSampleId: |
803 | 0 | builder.addCapability(spv::Capability::SampleRateShading); |
804 | 0 | return spv::BuiltIn::SampleId; |
805 | | |
806 | 0 | case glslang::EbvSamplePosition: |
807 | 0 | builder.addCapability(spv::Capability::SampleRateShading); |
808 | 0 | return spv::BuiltIn::SamplePosition; |
809 | | |
810 | 0 | case glslang::EbvSampleMask: |
811 | 0 | return spv::BuiltIn::SampleMask; |
812 | | |
813 | 0 | case glslang::EbvLayer: |
814 | 0 | if (glslangIntermediate->getStage() == EShLangMesh) { |
815 | 0 | return spv::BuiltIn::Layer; |
816 | 0 | } |
817 | 0 | if (glslangIntermediate->getStage() == EShLangGeometry || |
818 | 0 | glslangIntermediate->getStage() == EShLangFragment) { |
819 | 0 | builder.addCapability(spv::Capability::Geometry); |
820 | 0 | } |
821 | 0 | if (glslangIntermediate->getStage() == EShLangVertex || |
822 | 0 | glslangIntermediate->getStage() == EShLangTessControl || |
823 | 0 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
824 | |
|
825 | 0 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
826 | 0 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
827 | 0 | builder.addCapability(spv::Capability::ShaderViewportIndexLayerEXT); |
828 | 0 | } else |
829 | 0 | builder.addCapability(spv::Capability::ShaderLayer); |
830 | 0 | } |
831 | 0 | return spv::BuiltIn::Layer; |
832 | | |
833 | 0 | case glslang::EbvBaseVertex: |
834 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
835 | 0 | builder.addCapability(spv::Capability::DrawParameters); |
836 | 0 | return spv::BuiltIn::BaseVertex; |
837 | | |
838 | 0 | case glslang::EbvBaseInstance: |
839 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
840 | 0 | builder.addCapability(spv::Capability::DrawParameters); |
841 | 0 | return spv::BuiltIn::BaseInstance; |
842 | | |
843 | 0 | case glslang::EbvDrawId: |
844 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
845 | 0 | builder.addCapability(spv::Capability::DrawParameters); |
846 | 0 | return spv::BuiltIn::DrawIndex; |
847 | | |
848 | 0 | case glslang::EbvPrimitiveId: |
849 | 0 | if (glslangIntermediate->getStage() == EShLangFragment) |
850 | 0 | builder.addCapability(spv::Capability::Geometry); |
851 | 0 | return spv::BuiltIn::PrimitiveId; |
852 | | |
853 | 0 | case glslang::EbvFragStencilRef: |
854 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
855 | 0 | builder.addCapability(spv::Capability::StencilExportEXT); |
856 | 0 | return spv::BuiltIn::FragStencilRefEXT; |
857 | | |
858 | 0 | case glslang::EbvShadingRateKHR: |
859 | 0 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
860 | 0 | builder.addCapability(spv::Capability::FragmentShadingRateKHR); |
861 | 0 | return spv::BuiltIn::ShadingRateKHR; |
862 | | |
863 | 0 | case glslang::EbvPrimitiveShadingRateKHR: |
864 | 0 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
865 | 0 | builder.addCapability(spv::Capability::FragmentShadingRateKHR); |
866 | 0 | return spv::BuiltIn::PrimitiveShadingRateKHR; |
867 | | |
868 | 0 | case glslang::EbvInvocationId: return spv::BuiltIn::InvocationId; |
869 | 0 | case glslang::EbvTessLevelInner: return spv::BuiltIn::TessLevelInner; |
870 | 0 | case glslang::EbvTessLevelOuter: return spv::BuiltIn::TessLevelOuter; |
871 | 0 | case glslang::EbvTessCoord: return spv::BuiltIn::TessCoord; |
872 | 0 | case glslang::EbvPatchVertices: return spv::BuiltIn::PatchVertices; |
873 | 0 | case glslang::EbvHelperInvocation: return spv::BuiltIn::HelperInvocation; |
874 | | |
875 | 0 | case glslang::EbvSubGroupSize: |
876 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
877 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
878 | 0 | return spv::BuiltIn::SubgroupSize; |
879 | | |
880 | 0 | case glslang::EbvSubGroupInvocation: |
881 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
882 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
883 | 0 | return spv::BuiltIn::SubgroupLocalInvocationId; |
884 | | |
885 | 0 | case glslang::EbvSubGroupEqMask: |
886 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
887 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
888 | 0 | return spv::BuiltIn::SubgroupEqMask; |
889 | | |
890 | 0 | case glslang::EbvSubGroupGeMask: |
891 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
892 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
893 | 0 | return spv::BuiltIn::SubgroupGeMask; |
894 | | |
895 | 0 | case glslang::EbvSubGroupGtMask: |
896 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
897 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
898 | 0 | return spv::BuiltIn::SubgroupGtMask; |
899 | | |
900 | 0 | case glslang::EbvSubGroupLeMask: |
901 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
902 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
903 | 0 | return spv::BuiltIn::SubgroupLeMask; |
904 | | |
905 | 0 | case glslang::EbvSubGroupLtMask: |
906 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
907 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
908 | 0 | return spv::BuiltIn::SubgroupLtMask; |
909 | | |
910 | 0 | case glslang::EbvNumSubgroups: |
911 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
912 | 0 | return spv::BuiltIn::NumSubgroups; |
913 | | |
914 | 0 | case glslang::EbvSubgroupID: |
915 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
916 | 0 | return spv::BuiltIn::SubgroupId; |
917 | | |
918 | 0 | case glslang::EbvSubgroupSize2: |
919 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
920 | 0 | return spv::BuiltIn::SubgroupSize; |
921 | | |
922 | 0 | case glslang::EbvSubgroupInvocation2: |
923 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
924 | 0 | return spv::BuiltIn::SubgroupLocalInvocationId; |
925 | | |
926 | 0 | case glslang::EbvSubgroupEqMask2: |
927 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
928 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
929 | 0 | return spv::BuiltIn::SubgroupEqMask; |
930 | | |
931 | 0 | case glslang::EbvSubgroupGeMask2: |
932 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
933 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
934 | 0 | return spv::BuiltIn::SubgroupGeMask; |
935 | | |
936 | 0 | case glslang::EbvSubgroupGtMask2: |
937 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
938 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
939 | 0 | return spv::BuiltIn::SubgroupGtMask; |
940 | | |
941 | 0 | case glslang::EbvSubgroupLeMask2: |
942 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
943 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
944 | 0 | return spv::BuiltIn::SubgroupLeMask; |
945 | | |
946 | 0 | case glslang::EbvSubgroupLtMask2: |
947 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
948 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
949 | 0 | return spv::BuiltIn::SubgroupLtMask; |
950 | | |
951 | 0 | case glslang::EbvBaryCoordNoPersp: |
952 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
953 | 0 | return spv::BuiltIn::BaryCoordNoPerspAMD; |
954 | | |
955 | 0 | case glslang::EbvBaryCoordNoPerspCentroid: |
956 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
957 | 0 | return spv::BuiltIn::BaryCoordNoPerspCentroidAMD; |
958 | | |
959 | 0 | case glslang::EbvBaryCoordNoPerspSample: |
960 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
961 | 0 | return spv::BuiltIn::BaryCoordNoPerspSampleAMD; |
962 | | |
963 | 0 | case glslang::EbvBaryCoordSmooth: |
964 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
965 | 0 | return spv::BuiltIn::BaryCoordSmoothAMD; |
966 | | |
967 | 0 | case glslang::EbvBaryCoordSmoothCentroid: |
968 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
969 | 0 | return spv::BuiltIn::BaryCoordSmoothCentroidAMD; |
970 | | |
971 | 0 | case glslang::EbvBaryCoordSmoothSample: |
972 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
973 | 0 | return spv::BuiltIn::BaryCoordSmoothSampleAMD; |
974 | | |
975 | 0 | case glslang::EbvBaryCoordPullModel: |
976 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
977 | 0 | return spv::BuiltIn::BaryCoordPullModelAMD; |
978 | | |
979 | 0 | case glslang::EbvDeviceIndex: |
980 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
981 | 0 | builder.addCapability(spv::Capability::DeviceGroup); |
982 | 0 | return spv::BuiltIn::DeviceIndex; |
983 | | |
984 | 0 | case glslang::EbvViewIndex: |
985 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
986 | 0 | builder.addCapability(spv::Capability::MultiView); |
987 | 0 | return spv::BuiltIn::ViewIndex; |
988 | | |
989 | 0 | case glslang::EbvFragSizeEXT: |
990 | 0 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
991 | 0 | builder.addCapability(spv::Capability::FragmentDensityEXT); |
992 | 0 | return spv::BuiltIn::FragSizeEXT; |
993 | | |
994 | 0 | case glslang::EbvFragInvocationCountEXT: |
995 | 0 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
996 | 0 | builder.addCapability(spv::Capability::FragmentDensityEXT); |
997 | 0 | return spv::BuiltIn::FragInvocationCountEXT; |
998 | | |
999 | 0 | case glslang::EbvViewportMaskNV: |
1000 | 0 | if (!memberDeclaration) { |
1001 | 0 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
1002 | 0 | builder.addCapability(spv::Capability::ShaderViewportMaskNV); |
1003 | 0 | } |
1004 | 0 | return spv::BuiltIn::ViewportMaskNV; |
1005 | 0 | case glslang::EbvSecondaryPositionNV: |
1006 | 0 | if (!memberDeclaration) { |
1007 | 0 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
1008 | 0 | builder.addCapability(spv::Capability::ShaderStereoViewNV); |
1009 | 0 | } |
1010 | 0 | return spv::BuiltIn::SecondaryPositionNV; |
1011 | 0 | case glslang::EbvSecondaryViewportMaskNV: |
1012 | 0 | if (!memberDeclaration) { |
1013 | 0 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
1014 | 0 | builder.addCapability(spv::Capability::ShaderStereoViewNV); |
1015 | 0 | } |
1016 | 0 | return spv::BuiltIn::SecondaryViewportMaskNV; |
1017 | 0 | case glslang::EbvPositionPerViewNV: |
1018 | 0 | if (!memberDeclaration) { |
1019 | 0 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
1020 | 0 | builder.addCapability(spv::Capability::PerViewAttributesNV); |
1021 | 0 | } |
1022 | 0 | return spv::BuiltIn::PositionPerViewNV; |
1023 | 0 | case glslang::EbvViewportMaskPerViewNV: |
1024 | 0 | if (!memberDeclaration) { |
1025 | 0 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
1026 | 0 | builder.addCapability(spv::Capability::PerViewAttributesNV); |
1027 | 0 | } |
1028 | 0 | return spv::BuiltIn::ViewportMaskPerViewNV; |
1029 | 0 | case glslang::EbvFragFullyCoveredNV: |
1030 | 0 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
1031 | 0 | builder.addCapability(spv::Capability::FragmentFullyCoveredEXT); |
1032 | 0 | return spv::BuiltIn::FullyCoveredEXT; |
1033 | 0 | case glslang::EbvFragmentSizeNV: |
1034 | 0 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
1035 | 0 | builder.addCapability(spv::Capability::ShadingRateNV); |
1036 | 0 | return spv::BuiltIn::FragmentSizeNV; |
1037 | 0 | case glslang::EbvInvocationsPerPixelNV: |
1038 | 0 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
1039 | 0 | builder.addCapability(spv::Capability::ShadingRateNV); |
1040 | 0 | return spv::BuiltIn::InvocationsPerPixelNV; |
1041 | | |
1042 | | // ray tracing |
1043 | 0 | case glslang::EbvLaunchId: |
1044 | 0 | return spv::BuiltIn::LaunchIdKHR; |
1045 | 0 | case glslang::EbvLaunchSize: |
1046 | 0 | return spv::BuiltIn::LaunchSizeKHR; |
1047 | 0 | case glslang::EbvWorldRayOrigin: |
1048 | 0 | return spv::BuiltIn::WorldRayOriginKHR; |
1049 | 0 | case glslang::EbvWorldRayDirection: |
1050 | 0 | return spv::BuiltIn::WorldRayDirectionKHR; |
1051 | 0 | case glslang::EbvObjectRayOrigin: |
1052 | 0 | return spv::BuiltIn::ObjectRayOriginKHR; |
1053 | 0 | case glslang::EbvObjectRayDirection: |
1054 | 0 | return spv::BuiltIn::ObjectRayDirectionKHR; |
1055 | 0 | case glslang::EbvRayTmin: |
1056 | 0 | return spv::BuiltIn::RayTminKHR; |
1057 | 0 | case glslang::EbvRayTmax: |
1058 | 0 | return spv::BuiltIn::RayTmaxKHR; |
1059 | 0 | case glslang::EbvCullMask: |
1060 | 0 | return spv::BuiltIn::CullMaskKHR; |
1061 | 0 | case glslang::EbvPositionFetch: |
1062 | 0 | return spv::BuiltIn::HitTriangleVertexPositionsKHR; |
1063 | 0 | case glslang::EbvInstanceCustomIndex: |
1064 | 0 | return spv::BuiltIn::InstanceCustomIndexKHR; |
1065 | 0 | case glslang::EbvHitKind: |
1066 | 0 | return spv::BuiltIn::HitKindKHR; |
1067 | 0 | case glslang::EbvObjectToWorld: |
1068 | 0 | case glslang::EbvObjectToWorld3x4: |
1069 | 0 | return spv::BuiltIn::ObjectToWorldKHR; |
1070 | 0 | case glslang::EbvWorldToObject: |
1071 | 0 | case glslang::EbvWorldToObject3x4: |
1072 | 0 | return spv::BuiltIn::WorldToObjectKHR; |
1073 | 0 | case glslang::EbvIncomingRayFlags: |
1074 | 0 | return spv::BuiltIn::IncomingRayFlagsKHR; |
1075 | 0 | case glslang::EbvGeometryIndex: |
1076 | 0 | return spv::BuiltIn::RayGeometryIndexKHR; |
1077 | 0 | case glslang::EbvCurrentRayTimeNV: |
1078 | 0 | builder.addExtension(spv::E_SPV_NV_ray_tracing_motion_blur); |
1079 | 0 | builder.addCapability(spv::Capability::RayTracingMotionBlurNV); |
1080 | 0 | return spv::BuiltIn::CurrentRayTimeNV; |
1081 | 0 | case glslang::EbvMicroTrianglePositionNV: |
1082 | 0 | builder.addCapability(spv::Capability::RayTracingDisplacementMicromapNV); |
1083 | 0 | builder.addExtension("SPV_NV_displacement_micromap"); |
1084 | 0 | return spv::BuiltIn::HitMicroTriangleVertexPositionsNV; |
1085 | 0 | case glslang::EbvMicroTriangleBaryNV: |
1086 | 0 | builder.addCapability(spv::Capability::RayTracingDisplacementMicromapNV); |
1087 | 0 | builder.addExtension("SPV_NV_displacement_micromap"); |
1088 | 0 | return spv::BuiltIn::HitMicroTriangleVertexBarycentricsNV; |
1089 | 0 | case glslang::EbvHitKindFrontFacingMicroTriangleNV: |
1090 | 0 | builder.addCapability(spv::Capability::RayTracingDisplacementMicromapNV); |
1091 | 0 | builder.addExtension("SPV_NV_displacement_micromap"); |
1092 | 0 | return spv::BuiltIn::HitKindFrontFacingMicroTriangleNV; |
1093 | 0 | case glslang::EbvHitKindBackFacingMicroTriangleNV: |
1094 | 0 | builder.addCapability(spv::Capability::RayTracingDisplacementMicromapNV); |
1095 | 0 | builder.addExtension("SPV_NV_displacement_micromap"); |
1096 | 0 | return spv::BuiltIn::HitKindBackFacingMicroTriangleNV; |
1097 | 0 | case glslang::EbvClusterIDNV: |
1098 | 0 | builder.addCapability(spv::Capability::RayTracingClusterAccelerationStructureNV); |
1099 | 0 | builder.addExtension("SPV_NV_cluster_acceleration_structure"); |
1100 | 0 | return spv::BuiltIn::ClusterIDNV; |
1101 | 0 | case glslang::EbvHitIsSphereNV: |
1102 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
1103 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1104 | 0 | return spv::BuiltIn::HitIsSphereNV; |
1105 | 0 | case glslang::EbvHitIsLSSNV: |
1106 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
1107 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1108 | 0 | return spv::BuiltIn::HitIsLSSNV; |
1109 | 0 | case glslang::EbvHitSpherePositionNV: |
1110 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
1111 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1112 | 0 | return spv::BuiltIn::HitSpherePositionNV; |
1113 | 0 | case glslang::EbvHitSphereRadiusNV: |
1114 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
1115 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1116 | 0 | return spv::BuiltIn::HitSphereRadiusNV; |
1117 | 0 | case glslang::EbvHitLSSPositionsNV: |
1118 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
1119 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1120 | 0 | return spv::BuiltIn::HitLSSPositionsNV; |
1121 | 0 | case glslang::EbvHitLSSRadiiNV: |
1122 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
1123 | 0 | builder.addExtension("SPV_NV_linear_swept_spheres"); |
1124 | 0 | return spv::BuiltIn::HitLSSRadiiNV; |
1125 | | |
1126 | | // barycentrics |
1127 | 0 | case glslang::EbvBaryCoordNV: |
1128 | 0 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
1129 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricNV); |
1130 | 0 | return spv::BuiltIn::BaryCoordNV; |
1131 | 0 | case glslang::EbvBaryCoordNoPerspNV: |
1132 | 0 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
1133 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricNV); |
1134 | 0 | return spv::BuiltIn::BaryCoordNoPerspNV; |
1135 | | |
1136 | 0 | case glslang::EbvBaryCoordEXT: |
1137 | 0 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
1138 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricKHR); |
1139 | 0 | return spv::BuiltIn::BaryCoordKHR; |
1140 | 0 | case glslang::EbvBaryCoordNoPerspEXT: |
1141 | 0 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
1142 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricKHR); |
1143 | 0 | return spv::BuiltIn::BaryCoordNoPerspKHR; |
1144 | | |
1145 | | // mesh shaders |
1146 | 0 | case glslang::EbvTaskCountNV: |
1147 | 0 | return spv::BuiltIn::TaskCountNV; |
1148 | 0 | case glslang::EbvPrimitiveCountNV: |
1149 | 0 | return spv::BuiltIn::PrimitiveCountNV; |
1150 | 0 | case glslang::EbvPrimitiveIndicesNV: |
1151 | 0 | return spv::BuiltIn::PrimitiveIndicesNV; |
1152 | 0 | case glslang::EbvClipDistancePerViewNV: |
1153 | 0 | return spv::BuiltIn::ClipDistancePerViewNV; |
1154 | 0 | case glslang::EbvCullDistancePerViewNV: |
1155 | 0 | return spv::BuiltIn::CullDistancePerViewNV; |
1156 | 0 | case glslang::EbvLayerPerViewNV: |
1157 | 0 | return spv::BuiltIn::LayerPerViewNV; |
1158 | 0 | case glslang::EbvMeshViewCountNV: |
1159 | 0 | return spv::BuiltIn::MeshViewCountNV; |
1160 | 0 | case glslang::EbvMeshViewIndicesNV: |
1161 | 0 | return spv::BuiltIn::MeshViewIndicesNV; |
1162 | | |
1163 | | // SPV_EXT_mesh_shader |
1164 | 0 | case glslang::EbvPrimitivePointIndicesEXT: |
1165 | 0 | return spv::BuiltIn::PrimitivePointIndicesEXT; |
1166 | 0 | case glslang::EbvPrimitiveLineIndicesEXT: |
1167 | 0 | return spv::BuiltIn::PrimitiveLineIndicesEXT; |
1168 | 0 | case glslang::EbvPrimitiveTriangleIndicesEXT: |
1169 | 0 | return spv::BuiltIn::PrimitiveTriangleIndicesEXT; |
1170 | 0 | case glslang::EbvCullPrimitiveEXT: |
1171 | 0 | return spv::BuiltIn::CullPrimitiveEXT; |
1172 | | |
1173 | | // sm builtins |
1174 | 0 | case glslang::EbvWarpsPerSM: |
1175 | 0 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
1176 | 0 | builder.addCapability(spv::Capability::ShaderSMBuiltinsNV); |
1177 | 0 | return spv::BuiltIn::WarpsPerSMNV; |
1178 | 0 | case glslang::EbvSMCount: |
1179 | 0 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
1180 | 0 | builder.addCapability(spv::Capability::ShaderSMBuiltinsNV); |
1181 | 0 | return spv::BuiltIn::SMCountNV; |
1182 | 0 | case glslang::EbvWarpID: |
1183 | 0 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
1184 | 0 | builder.addCapability(spv::Capability::ShaderSMBuiltinsNV); |
1185 | 0 | return spv::BuiltIn::WarpIDNV; |
1186 | 0 | case glslang::EbvSMID: |
1187 | 0 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
1188 | 0 | builder.addCapability(spv::Capability::ShaderSMBuiltinsNV); |
1189 | 0 | return spv::BuiltIn::SMIDNV; |
1190 | | |
1191 | | // ARM builtins |
1192 | 0 | case glslang::EbvCoreCountARM: |
1193 | 0 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
1194 | 0 | builder.addCapability(spv::Capability::CoreBuiltinsARM); |
1195 | 0 | return spv::BuiltIn::CoreCountARM; |
1196 | 0 | case glslang::EbvCoreIDARM: |
1197 | 0 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
1198 | 0 | builder.addCapability(spv::Capability::CoreBuiltinsARM); |
1199 | 0 | return spv::BuiltIn::CoreIDARM; |
1200 | 0 | case glslang::EbvCoreMaxIDARM: |
1201 | 0 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
1202 | 0 | builder.addCapability(spv::Capability::CoreBuiltinsARM); |
1203 | 0 | return spv::BuiltIn::CoreMaxIDARM; |
1204 | 0 | case glslang::EbvWarpIDARM: |
1205 | 0 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
1206 | 0 | builder.addCapability(spv::Capability::CoreBuiltinsARM); |
1207 | 0 | return spv::BuiltIn::WarpIDARM; |
1208 | 0 | case glslang::EbvWarpMaxIDARM: |
1209 | 0 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
1210 | 0 | builder.addCapability(spv::Capability::CoreBuiltinsARM); |
1211 | 0 | return spv::BuiltIn::WarpMaxIDARM; |
1212 | | |
1213 | | // QCOM builtins |
1214 | 0 | case glslang::EbvTileOffsetQCOM: |
1215 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1216 | 0 | return spv::BuiltIn::TileOffsetQCOM; |
1217 | 0 | case glslang::EbvTileDimensionQCOM: |
1218 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1219 | 0 | return spv::BuiltIn::TileDimensionQCOM; |
1220 | 0 | case glslang::EbvTileApronSizeQCOM: |
1221 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1222 | 0 | return spv::BuiltIn::TileApronSizeQCOM; |
1223 | | |
1224 | | // SPV_EXT_descriptor_heap |
1225 | 0 | case glslang::EbvSamplerHeapEXT: |
1226 | 0 | builder.addExtension(spv::E_SPV_EXT_descriptor_heap); |
1227 | 0 | builder.addExtension(spv::E_SPV_KHR_untyped_pointers); |
1228 | 0 | builder.addCapability(spv::Capability::DescriptorHeapEXT); |
1229 | 0 | builder.addCapability(spv::Capability::UntypedPointersKHR); |
1230 | | // Add SamplerHeapEXT Symbol for spv level. |
1231 | 0 | if (builtInVariableIds.find(uint32_t(spv::BuiltIn::SamplerHeapEXT)) == builtInVariableIds.end()) { |
1232 | 0 | spv::Id samplerHeapEXT = |
1233 | 0 | builder.createUntypedVariable(spv::NoPrecision, spv::StorageClass::UniformConstant, "sampler_heap"); |
1234 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) |
1235 | 0 | entryPoint->addIdOperand(samplerHeapEXT); |
1236 | 0 | builder.addDecoration(samplerHeapEXT, spv::Decoration::BuiltIn, (int)spv::BuiltIn::SamplerHeapEXT); |
1237 | 0 | builtInVariableIds.insert({uint32_t(spv::BuiltIn::SamplerHeapEXT), samplerHeapEXT}); |
1238 | 0 | } |
1239 | 0 | return spv::BuiltIn::SamplerHeapEXT; |
1240 | 0 | case glslang::EbvResourceHeapEXT: |
1241 | 0 | builder.addExtension(spv::E_SPV_EXT_descriptor_heap); |
1242 | 0 | builder.addExtension(spv::E_SPV_KHR_untyped_pointers); |
1243 | 0 | builder.addCapability(spv::Capability::DescriptorHeapEXT); |
1244 | 0 | builder.addCapability(spv::Capability::UntypedPointersKHR); |
1245 | | // Add ResourceHeapEXT Symbol for spv level. |
1246 | 0 | if (builtInVariableIds.find(uint32_t(spv::BuiltIn::ResourceHeapEXT)) == builtInVariableIds.end()) { |
1247 | 0 | spv::Id resourceHeapEXT = |
1248 | 0 | builder.createUntypedVariable(spv::NoPrecision, spv::StorageClass::UniformConstant, "resource_heap"); |
1249 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) |
1250 | 0 | entryPoint->addIdOperand(resourceHeapEXT); |
1251 | 0 | builder.addDecoration(resourceHeapEXT, spv::Decoration::BuiltIn, (int)spv::BuiltIn::ResourceHeapEXT); |
1252 | 0 | builtInVariableIds.insert({uint32_t(spv::BuiltIn::ResourceHeapEXT), resourceHeapEXT}); |
1253 | 0 | } |
1254 | 0 | return spv::BuiltIn::ResourceHeapEXT; |
1255 | | |
1256 | 6.66k | default: |
1257 | 6.66k | return spv::BuiltIn::Max; |
1258 | 6.66k | } |
1259 | 6.66k | } |
1260 | | |
1261 | | // Translate glslang image layout format to SPIR-V image format. |
1262 | | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
1263 | 1.33k | { |
1264 | 1.33k | assert(type.getBasicType() == glslang::EbtSampler); |
1265 | | |
1266 | | // Check for capabilities |
1267 | 1.33k | switch (type.getQualifier().getFormat()) { |
1268 | 0 | case glslang::ElfRg32f: |
1269 | 133 | case glslang::ElfRg16f: |
1270 | 133 | case glslang::ElfR11fG11fB10f: |
1271 | 133 | case glslang::ElfR16f: |
1272 | 133 | case glslang::ElfRgba16: |
1273 | 133 | case glslang::ElfRgb10A2: |
1274 | 133 | case glslang::ElfRg16: |
1275 | 133 | case glslang::ElfRg8: |
1276 | 133 | case glslang::ElfR16: |
1277 | 133 | case glslang::ElfR8: |
1278 | 133 | case glslang::ElfRgba16Snorm: |
1279 | 133 | case glslang::ElfRg16Snorm: |
1280 | 133 | case glslang::ElfRg8Snorm: |
1281 | 133 | case glslang::ElfR16Snorm: |
1282 | 133 | case glslang::ElfR8Snorm: |
1283 | | |
1284 | 133 | case glslang::ElfRg32i: |
1285 | 133 | case glslang::ElfRg16i: |
1286 | 133 | case glslang::ElfRg8i: |
1287 | 133 | case glslang::ElfR16i: |
1288 | 133 | case glslang::ElfR8i: |
1289 | | |
1290 | 133 | case glslang::ElfRgb10a2ui: |
1291 | 133 | case glslang::ElfRg32ui: |
1292 | 133 | case glslang::ElfRg16ui: |
1293 | 133 | case glslang::ElfRg8ui: |
1294 | 133 | case glslang::ElfR16ui: |
1295 | 133 | case glslang::ElfR8ui: |
1296 | 133 | builder.addCapability(spv::Capability::StorageImageExtendedFormats); |
1297 | 133 | break; |
1298 | | |
1299 | 86 | case glslang::ElfR64ui: |
1300 | 173 | case glslang::ElfR64i: |
1301 | 173 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
1302 | 173 | builder.addCapability(spv::Capability::Int64ImageEXT); |
1303 | 173 | break; |
1304 | 1.02k | default: |
1305 | 1.02k | break; |
1306 | 1.33k | } |
1307 | | |
1308 | | // do the translation |
1309 | 1.33k | switch (type.getQualifier().getFormat()) { |
1310 | 799 | case glslang::ElfNone: return spv::ImageFormat::Unknown; |
1311 | 27 | case glslang::ElfRgba32f: return spv::ImageFormat::Rgba32f; |
1312 | 166 | case glslang::ElfRgba16f: return spv::ImageFormat::Rgba16f; |
1313 | 0 | case glslang::ElfR32f: return spv::ImageFormat::R32f; |
1314 | 0 | case glslang::ElfRgba8: return spv::ImageFormat::Rgba8; |
1315 | 0 | case glslang::ElfRgba8Snorm: return spv::ImageFormat::Rgba8Snorm; |
1316 | 0 | case glslang::ElfRg32f: return spv::ImageFormat::Rg32f; |
1317 | 133 | case glslang::ElfRg16f: return spv::ImageFormat::Rg16f; |
1318 | 0 | case glslang::ElfR11fG11fB10f: return spv::ImageFormat::R11fG11fB10f; |
1319 | 0 | case glslang::ElfR16f: return spv::ImageFormat::R16f; |
1320 | 0 | case glslang::ElfRgba16: return spv::ImageFormat::Rgba16; |
1321 | 0 | case glslang::ElfRgb10A2: return spv::ImageFormat::Rgb10A2; |
1322 | 0 | case glslang::ElfRg16: return spv::ImageFormat::Rg16; |
1323 | 0 | case glslang::ElfRg8: return spv::ImageFormat::Rg8; |
1324 | 0 | case glslang::ElfR16: return spv::ImageFormat::R16; |
1325 | 0 | case glslang::ElfR8: return spv::ImageFormat::R8; |
1326 | 0 | case glslang::ElfRgba16Snorm: return spv::ImageFormat::Rgba16Snorm; |
1327 | 0 | case glslang::ElfRg16Snorm: return spv::ImageFormat::Rg16Snorm; |
1328 | 0 | case glslang::ElfRg8Snorm: return spv::ImageFormat::Rg8Snorm; |
1329 | 0 | case glslang::ElfR16Snorm: return spv::ImageFormat::R16Snorm; |
1330 | 0 | case glslang::ElfR8Snorm: return spv::ImageFormat::R8Snorm; |
1331 | 18 | case glslang::ElfRgba32i: return spv::ImageFormat::Rgba32i; |
1332 | 0 | case glslang::ElfRgba16i: return spv::ImageFormat::Rgba16i; |
1333 | 0 | case glslang::ElfRgba8i: return spv::ImageFormat::Rgba8i; |
1334 | 0 | case glslang::ElfR32i: return spv::ImageFormat::R32i; |
1335 | 0 | case glslang::ElfRg32i: return spv::ImageFormat::Rg32i; |
1336 | 0 | case glslang::ElfRg16i: return spv::ImageFormat::Rg16i; |
1337 | 0 | case glslang::ElfRg8i: return spv::ImageFormat::Rg8i; |
1338 | 0 | case glslang::ElfR16i: return spv::ImageFormat::R16i; |
1339 | 0 | case glslang::ElfR8i: return spv::ImageFormat::R8i; |
1340 | 18 | case glslang::ElfRgba32ui: return spv::ImageFormat::Rgba32ui; |
1341 | 0 | case glslang::ElfRgba16ui: return spv::ImageFormat::Rgba16ui; |
1342 | 0 | case glslang::ElfRgba8ui: return spv::ImageFormat::Rgba8ui; |
1343 | 0 | case glslang::ElfR32ui: return spv::ImageFormat::R32ui; |
1344 | 0 | case glslang::ElfRg32ui: return spv::ImageFormat::Rg32ui; |
1345 | 0 | case glslang::ElfRg16ui: return spv::ImageFormat::Rg16ui; |
1346 | 0 | case glslang::ElfRgb10a2ui: return spv::ImageFormat::Rgb10a2ui; |
1347 | 0 | case glslang::ElfRg8ui: return spv::ImageFormat::Rg8ui; |
1348 | 0 | case glslang::ElfR16ui: return spv::ImageFormat::R16ui; |
1349 | 0 | case glslang::ElfR8ui: return spv::ImageFormat::R8ui; |
1350 | 86 | case glslang::ElfR64ui: return spv::ImageFormat::R64ui; |
1351 | 87 | case glslang::ElfR64i: return spv::ImageFormat::R64i; |
1352 | 0 | default: return spv::ImageFormat::Max; |
1353 | 1.33k | } |
1354 | 1.33k | } |
1355 | | |
1356 | | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
1357 | | const glslang::TIntermSelection& selectionNode) const |
1358 | 401 | { |
1359 | 401 | if (selectionNode.getFlatten()) |
1360 | 36 | return spv::SelectionControlMask::Flatten; |
1361 | 365 | if (selectionNode.getDontFlatten()) |
1362 | 18 | return spv::SelectionControlMask::DontFlatten; |
1363 | 347 | return spv::SelectionControlMask::MaskNone; |
1364 | 365 | } |
1365 | | |
1366 | | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
1367 | | const |
1368 | 89 | { |
1369 | 89 | if (switchNode.getFlatten()) |
1370 | 0 | return spv::SelectionControlMask::Flatten; |
1371 | 89 | if (switchNode.getDontFlatten()) |
1372 | 36 | return spv::SelectionControlMask::DontFlatten; |
1373 | 53 | return spv::SelectionControlMask::MaskNone; |
1374 | 89 | } |
1375 | | |
1376 | | // return a non-0 dependency if the dependency argument must be set |
1377 | | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
1378 | | std::vector<unsigned int>& operands) const |
1379 | 242 | { |
1380 | 242 | spv::LoopControlMask control = spv::LoopControlMask::MaskNone; |
1381 | | |
1382 | 242 | if (loopNode.getDontUnroll()) |
1383 | 36 | control = control | spv::LoopControlMask::DontUnroll; |
1384 | 242 | if (loopNode.getUnroll()) |
1385 | 36 | control = control | spv::LoopControlMask::Unroll; |
1386 | 242 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
1387 | 36 | control = control | spv::LoopControlMask::DependencyInfinite; |
1388 | 206 | else if (loopNode.getLoopDependency() > 0) { |
1389 | 36 | control = control | spv::LoopControlMask::DependencyLength; |
1390 | 36 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
1391 | 36 | } |
1392 | 242 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
1393 | 0 | if (loopNode.getMinIterations() > 0) { |
1394 | 0 | control = control | spv::LoopControlMask::MinIterations; |
1395 | 0 | operands.push_back(loopNode.getMinIterations()); |
1396 | 0 | } |
1397 | 0 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
1398 | 0 | control = control | spv::LoopControlMask::MaxIterations; |
1399 | 0 | operands.push_back(loopNode.getMaxIterations()); |
1400 | 0 | } |
1401 | 0 | if (loopNode.getIterationMultiple() > 1) { |
1402 | 0 | control = control | spv::LoopControlMask::IterationMultiple; |
1403 | 0 | operands.push_back(loopNode.getIterationMultiple()); |
1404 | 0 | } |
1405 | 0 | if (loopNode.getPeelCount() > 0) { |
1406 | 0 | control = control | spv::LoopControlMask::PeelCount; |
1407 | 0 | operands.push_back(loopNode.getPeelCount()); |
1408 | 0 | } |
1409 | 0 | if (loopNode.getPartialCount() > 0) { |
1410 | 0 | control = control | spv::LoopControlMask::PartialCount; |
1411 | 0 | operands.push_back(loopNode.getPartialCount()); |
1412 | 0 | } |
1413 | 0 | } |
1414 | | |
1415 | 242 | return control; |
1416 | 242 | } |
1417 | | |
1418 | | // Translate glslang type to SPIR-V storage class. |
1419 | | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
1420 | 4.06k | { |
1421 | 4.06k | if (type.getBasicType() == glslang::EbtRayQuery || type.getBasicType() == glslang::EbtHitObjectNV |
1422 | 4.05k | || type.getBasicType() == glslang::EbtHitObjectEXT) |
1423 | 10 | return spv::StorageClass::Private; |
1424 | 4.05k | if (type.getQualifier().isSpirvByReference()) { |
1425 | 0 | if (type.getQualifier().isParamInput() || type.getQualifier().isParamOutput()) |
1426 | 0 | return spv::StorageClass::Function; |
1427 | 0 | } |
1428 | 4.05k | if (type.getQualifier().isPipeInput()) |
1429 | 303 | return spv::StorageClass::Input; |
1430 | 3.75k | if (type.getQualifier().isPipeOutput()) |
1431 | 150 | return spv::StorageClass::Output; |
1432 | 3.60k | if (type.getQualifier().storage == glslang::EvqTileImageEXT || type.isAttachmentEXT()) { |
1433 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
1434 | 0 | builder.addCapability(spv::Capability::TileImageColorReadAccessEXT); |
1435 | 0 | return spv::StorageClass::TileImageEXT; |
1436 | 0 | } |
1437 | | |
1438 | 3.60k | if (type.getQualifier().isTileAttachmentQCOM()) { |
1439 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1440 | 0 | builder.addCapability(spv::Capability::TileShadingQCOM); |
1441 | 0 | return spv::StorageClass::TileAttachmentQCOM; |
1442 | 0 | } |
1443 | | |
1444 | 3.60k | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
1445 | 3.60k | type.getQualifier().storage == glslang::EvqUniform) { |
1446 | 3.60k | if (type.isAtomic()) |
1447 | 0 | return spv::StorageClass::AtomicCounter; |
1448 | 3.60k | if (type.containsOpaque() && !glslangIntermediate->getBindlessMode()) { |
1449 | 1.21k | if (type.getQualifier().storage == glslang::EvqResourceHeap || |
1450 | 1.21k | type.getQualifier().storage == glslang::EvqSamplerHeap) |
1451 | 0 | return spv::StorageClass::Uniform; |
1452 | 1.21k | else |
1453 | 1.21k | return spv::StorageClass::UniformConstant; |
1454 | 1.21k | } |
1455 | 3.60k | } |
1456 | | |
1457 | 2.39k | if (type.getQualifier().isUniformOrBuffer() && |
1458 | 323 | type.getQualifier().isShaderRecord()) { |
1459 | 0 | return spv::StorageClass::ShaderRecordBufferKHR; |
1460 | 0 | } |
1461 | | |
1462 | 2.39k | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
1463 | 9 | builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3); |
1464 | 9 | return spv::StorageClass::StorageBuffer; |
1465 | 9 | } |
1466 | | |
1467 | 2.38k | if (type.getQualifier().isUniformOrBuffer()) { |
1468 | 314 | if (type.getQualifier().isPushConstant()) |
1469 | 9 | return spv::StorageClass::PushConstant; |
1470 | 305 | if (type.getBasicType() == glslang::EbtBlock || |
1471 | 0 | type.getQualifier().layoutDescriptorHeap) |
1472 | 305 | return spv::StorageClass::Uniform; |
1473 | 0 | return spv::StorageClass::UniformConstant; |
1474 | 305 | } |
1475 | | |
1476 | 2.07k | if (type.getQualifier().storage == glslang::EvqShared && type.getBasicType() == glslang::EbtBlock) { |
1477 | 0 | builder.addExtension(spv::E_SPV_KHR_workgroup_memory_explicit_layout); |
1478 | 0 | builder.addCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR); |
1479 | 0 | return spv::StorageClass::Workgroup; |
1480 | 0 | } |
1481 | | |
1482 | 2.07k | switch (type.getQualifier().storage) { |
1483 | 345 | case glslang::EvqGlobal: return spv::StorageClass::Private; |
1484 | 0 | case glslang::EvqConstReadOnly: return spv::StorageClass::Function; |
1485 | 1.72k | case glslang::EvqTemporary: return spv::StorageClass::Function; |
1486 | 0 | case glslang::EvqShared: return spv::StorageClass::Workgroup; |
1487 | 0 | case glslang::EvqPayload: return spv::StorageClass::RayPayloadKHR; |
1488 | 0 | case glslang::EvqPayloadIn: return spv::StorageClass::IncomingRayPayloadKHR; |
1489 | 0 | case glslang::EvqHitAttr: return spv::StorageClass::HitAttributeKHR; |
1490 | 0 | case glslang::EvqCallableData: return spv::StorageClass::CallableDataKHR; |
1491 | 0 | case glslang::EvqCallableDataIn: return spv::StorageClass::IncomingCallableDataKHR; |
1492 | 0 | case glslang::EvqtaskPayloadSharedEXT : return spv::StorageClass::TaskPayloadWorkgroupEXT; |
1493 | 0 | case glslang::EvqHitObjectAttrNV: return spv::StorageClass::HitObjectAttributeNV; |
1494 | 0 | case glslang::EvqHitObjectAttrEXT: return spv::StorageClass::HitObjectAttributeEXT; |
1495 | 0 | case glslang::EvqSpirvStorageClass: return static_cast<spv::StorageClass>(type.getQualifier().spirvStorageClass); |
1496 | 0 | default: |
1497 | 0 | assert(0); |
1498 | 0 | break; |
1499 | 2.07k | } |
1500 | | |
1501 | 0 | return spv::StorageClass::Function; |
1502 | 2.07k | } |
1503 | | |
1504 | | // Translate glslang constants to SPIR-V literals |
1505 | | void TGlslangToSpvTraverser::TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>& constants, |
1506 | | std::vector<unsigned>& literals) const |
1507 | 126 | { |
1508 | 126 | for (auto constant : constants) { |
1509 | 126 | if (constant->getBasicType() == glslang::EbtFloat) { |
1510 | 0 | float floatValue = static_cast<float>(constant->getConstArray()[0].getDConst()); |
1511 | 0 | unsigned literal; |
1512 | 0 | static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)"); |
1513 | 0 | memcpy(&literal, &floatValue, sizeof(literal)); |
1514 | 0 | literals.push_back(literal); |
1515 | 126 | } else if (constant->getBasicType() == glslang::EbtInt) { |
1516 | 126 | unsigned literal = constant->getConstArray()[0].getIConst(); |
1517 | 126 | literals.push_back(literal); |
1518 | 126 | } else if (constant->getBasicType() == glslang::EbtUint) { |
1519 | 0 | unsigned literal = constant->getConstArray()[0].getUConst(); |
1520 | 0 | literals.push_back(literal); |
1521 | 0 | } else if (constant->getBasicType() == glslang::EbtBool) { |
1522 | 0 | unsigned literal = constant->getConstArray()[0].getBConst(); |
1523 | 0 | literals.push_back(literal); |
1524 | 0 | } else if (constant->getBasicType() == glslang::EbtString) { |
1525 | 0 | auto str = constant->getConstArray()[0].getSConst()->c_str(); |
1526 | 0 | unsigned literal = 0; |
1527 | 0 | char* literalPtr = reinterpret_cast<char*>(&literal); |
1528 | 0 | unsigned charCount = 0; |
1529 | 0 | char ch = 0; |
1530 | 0 | do { |
1531 | 0 | ch = *(str++); |
1532 | 0 | *(literalPtr++) = ch; |
1533 | 0 | ++charCount; |
1534 | 0 | if (charCount == 4) { |
1535 | 0 | literals.push_back(literal); |
1536 | 0 | literalPtr = reinterpret_cast<char*>(&literal); |
1537 | 0 | charCount = 0; |
1538 | 0 | } |
1539 | 0 | } while (ch != 0); |
1540 | | |
1541 | | // Partial literal is padded with 0 |
1542 | 0 | if (charCount > 0) { |
1543 | 0 | for (; charCount < 4; ++charCount) |
1544 | 0 | *(literalPtr++) = 0; |
1545 | 0 | literals.push_back(literal); |
1546 | 0 | } |
1547 | 0 | } else |
1548 | 0 | assert(0); // Unexpected type |
1549 | 126 | } |
1550 | 126 | } |
1551 | | |
1552 | | // Add capabilities pertaining to how an array is indexed. |
1553 | | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
1554 | | const glslang::TType& indexType) |
1555 | 170 | { |
1556 | 170 | if (indexType.getQualifier().isNonUniform()) { |
1557 | | |
1558 | | // deal with an asserted non-uniform index |
1559 | | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
1560 | 0 | if (baseType.getBasicType() == glslang::EbtSampler) { |
1561 | 0 | if (baseType.getQualifier().hasAttachment()) |
1562 | 0 | builder.addCapability(spv::Capability::InputAttachmentArrayNonUniformIndexingEXT); |
1563 | 0 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
1564 | 0 | builder.addCapability(spv::Capability::StorageTexelBufferArrayNonUniformIndexingEXT); |
1565 | 0 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
1566 | 0 | builder.addCapability(spv::Capability::UniformTexelBufferArrayNonUniformIndexingEXT); |
1567 | 0 | else if (baseType.isImage()) |
1568 | 0 | builder.addCapability(spv::Capability::StorageImageArrayNonUniformIndexingEXT); |
1569 | 0 | else if (baseType.isTexture()) |
1570 | 0 | builder.addCapability(spv::Capability::SampledImageArrayNonUniformIndexingEXT); |
1571 | 0 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
1572 | 0 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
1573 | 0 | builder.addCapability(spv::Capability::StorageBufferArrayNonUniformIndexingEXT); |
1574 | 0 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
1575 | 0 | builder.addCapability(spv::Capability::UniformBufferArrayNonUniformIndexingEXT); |
1576 | 0 | } |
1577 | 170 | } else { |
1578 | | // assume a dynamically uniform index |
1579 | 170 | if (baseType.getBasicType() == glslang::EbtSampler) { |
1580 | 9 | if (baseType.getQualifier().hasAttachment()) { |
1581 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
1582 | 0 | builder.addCapability(spv::Capability::InputAttachmentArrayDynamicIndexingEXT); |
1583 | 9 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
1584 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
1585 | 0 | builder.addCapability(spv::Capability::StorageTexelBufferArrayDynamicIndexingEXT); |
1586 | 9 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
1587 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
1588 | 0 | builder.addCapability(spv::Capability::UniformTexelBufferArrayDynamicIndexingEXT); |
1589 | 0 | } |
1590 | 9 | } |
1591 | 170 | } |
1592 | 170 | } |
1593 | | |
1594 | | // Return whether or not the given type is something that should be tied to a |
1595 | | // descriptor set. |
1596 | | bool IsDescriptorResource(const glslang::TType& type) |
1597 | 6.95k | { |
1598 | | // uniform and buffer blocks are included, unless it is a push_constant |
1599 | 6.95k | if (type.getBasicType() == glslang::EbtBlock) |
1600 | 214 | return type.getQualifier().isUniformOrBuffer() && |
1601 | 214 | ! type.getQualifier().isShaderRecord() && |
1602 | 214 | ! type.getQualifier().isPushConstant(); |
1603 | | |
1604 | | // non block... |
1605 | | // basically samplerXXX/subpass/sampler/texture are all included |
1606 | | // if they are the global-scope-class, not the function parameter |
1607 | | // (or local, if they ever exist) class. |
1608 | 6.73k | if (type.getBasicType() == glslang::EbtSampler || |
1609 | 6.29k | type.getBasicType() == glslang::EbtAccStruct) |
1610 | 446 | return type.getQualifier().isUniformOrBuffer(); |
1611 | | |
1612 | | // Tensors are tied to a descriptor. |
1613 | 6.29k | if (type.isTensorARM()) |
1614 | 0 | return true; |
1615 | | |
1616 | | // None of the above. |
1617 | 6.29k | return false; |
1618 | 6.29k | } |
1619 | | |
1620 | | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
1621 | 3.97k | { |
1622 | 3.97k | if (child.layoutMatrix == glslang::ElmNone) |
1623 | 1.12k | child.layoutMatrix = parent.layoutMatrix; |
1624 | | |
1625 | 3.97k | if (parent.invariant) |
1626 | 0 | child.invariant = true; |
1627 | 3.97k | if (parent.flat) |
1628 | 0 | child.flat = true; |
1629 | 3.97k | if (parent.centroid) |
1630 | 0 | child.centroid = true; |
1631 | 3.97k | if (parent.nopersp) |
1632 | 0 | child.nopersp = true; |
1633 | 3.97k | if (parent.explicitInterp) |
1634 | 0 | child.explicitInterp = true; |
1635 | 3.97k | if (parent.perPrimitiveNV) |
1636 | 0 | child.perPrimitiveNV = true; |
1637 | 3.97k | if (parent.perViewNV) |
1638 | 0 | child.perViewNV = true; |
1639 | 3.97k | if (parent.perTaskNV) |
1640 | 0 | child.perTaskNV = true; |
1641 | 3.97k | if (parent.storage == glslang::EvqtaskPayloadSharedEXT) |
1642 | 0 | child.storage = glslang::EvqtaskPayloadSharedEXT; |
1643 | 3.97k | if (parent.patch) |
1644 | 0 | child.patch = true; |
1645 | 3.97k | if (parent.sample) |
1646 | 0 | child.sample = true; |
1647 | 3.97k | if (parent.coherent) |
1648 | 0 | child.coherent = true; |
1649 | 3.97k | if (parent.devicecoherent) |
1650 | 0 | child.devicecoherent = true; |
1651 | 3.97k | if (parent.queuefamilycoherent) |
1652 | 0 | child.queuefamilycoherent = true; |
1653 | 3.97k | if (parent.workgroupcoherent) |
1654 | 0 | child.workgroupcoherent = true; |
1655 | 3.97k | if (parent.subgroupcoherent) |
1656 | 0 | child.subgroupcoherent = true; |
1657 | 3.97k | if (parent.shadercallcoherent) |
1658 | 0 | child.shadercallcoherent = true; |
1659 | 3.97k | if (parent.nonprivate) |
1660 | 0 | child.nonprivate = true; |
1661 | 3.97k | if (parent.volatil) |
1662 | 0 | child.volatil = true; |
1663 | 3.97k | if (parent.nontemporal) |
1664 | 0 | child.nontemporal = true; |
1665 | 3.97k | if (parent.restrict) |
1666 | 0 | child.restrict = true; |
1667 | 3.97k | if (parent.readonly) |
1668 | 0 | child.readonly = true; |
1669 | 3.97k | if (parent.writeonly) |
1670 | 0 | child.writeonly = true; |
1671 | 3.97k | if (parent.nonUniform) |
1672 | 0 | child.nonUniform = true; |
1673 | 3.97k | } |
1674 | | |
1675 | | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
1676 | 1.87k | { |
1677 | | // This should list qualifiers that simultaneous satisfy: |
1678 | | // - struct members might inherit from a struct declaration |
1679 | | // (note that non-block structs don't explicitly inherit, |
1680 | | // only implicitly, meaning no decoration involved) |
1681 | | // - affect decorations on the struct members |
1682 | | // (note smooth does not, and expecting something like volatile |
1683 | | // to effect the whole object) |
1684 | | // - are not part of the offset/st430/etc or row/column-major layout |
1685 | 1.87k | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
1686 | 1.87k | } |
1687 | | |
1688 | | // |
1689 | | // Implement the TGlslangToSpvTraverser class. |
1690 | | // |
1691 | | |
1692 | | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
1693 | | const glslang::TIntermediate* glslangIntermediate, |
1694 | | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
1695 | 370 | TIntermTraverser(true, false, true), |
1696 | 370 | options(options), |
1697 | 370 | shaderEntry(nullptr), currentFunction(nullptr), |
1698 | 370 | sequenceDepth(0), logger(buildLogger), |
1699 | 370 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
1700 | 370 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
1701 | 370 | glslangIntermediate(glslangIntermediate), |
1702 | 370 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
1703 | 370 | nonSemanticDebugPrintf(0), |
1704 | 370 | taskPayloadID(0) |
1705 | 370 | { |
1706 | 370 | bool isMeshShaderExt = (glslangIntermediate->getRequestedExtensions().find(glslang::E_GL_EXT_mesh_shader) != |
1707 | 370 | glslangIntermediate->getRequestedExtensions().end()); |
1708 | 370 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage(), isMeshShaderExt); |
1709 | | |
1710 | 370 | builder.clearAccessChain(); |
1711 | 370 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
1712 | 370 | glslangIntermediate->getVersion()); |
1713 | | |
1714 | 370 | if (options.emitNonSemanticShaderDebugSource) |
1715 | 0 | this->options.emitNonSemanticShaderDebugInfo = true; |
1716 | 370 | if (options.emitNonSemanticShaderDebugInfo) |
1717 | 0 | this->options.generateDebugInfo = true; |
1718 | | |
1719 | 370 | if (this->options.generateDebugInfo) { |
1720 | 0 | if (this->options.emitNonSemanticShaderDebugInfo) { |
1721 | 0 | builder.setEmitNonSemanticShaderDebugInfo(this->options.emitNonSemanticShaderDebugSource); |
1722 | 0 | } |
1723 | 0 | else { |
1724 | 0 | builder.setEmitSpirvDebugInfo(); |
1725 | 0 | } |
1726 | 0 | builder.setDebugMainSourceFile(glslangIntermediate->getSourceFile()); |
1727 | | |
1728 | | // Set the source shader's text |
1729 | 0 | std::string text; |
1730 | | |
1731 | | // If for SPV version 1.0, include |
1732 | | // a preamble in comments stating the OpModuleProcessed instructions. |
1733 | | // Otherwise, emit those as actual instructions. |
1734 | | // |
1735 | | // ...Except when using ShaderDebugInfo we DON'T want these as it will mess up the line |
1736 | | // number, instead the user has opt'ed in for ShaderDebugInfo instead, so they will want |
1737 | | // to parse those instead |
1738 | | // https://github.com/KhronosGroup/glslang/issues/3863 |
1739 | 0 | const bool add_comments = |
1740 | 0 | glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && !options.emitNonSemanticShaderDebugSource; |
1741 | |
|
1742 | 0 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
1743 | 0 | for (int p = 0; p < (int)processes.size(); ++p) { |
1744 | 0 | if (add_comments) { |
1745 | 0 | text.append("// OpModuleProcessed "); |
1746 | 0 | text.append(processes[p]); |
1747 | 0 | text.append("\n"); |
1748 | 0 | } else if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_1) { |
1749 | | // OpModuleProcessed added in SPIR-V 1.1 |
1750 | 0 | builder.addModuleProcessed(processes[p]); |
1751 | 0 | } |
1752 | 0 | } |
1753 | 0 | if (add_comments && (int)processes.size() > 0) |
1754 | 0 | text.append("#line 1\n"); |
1755 | 0 | text.append(glslangIntermediate->getSourceText()); |
1756 | 0 | builder.setSourceText(text); |
1757 | | // Pass name and text for all included files |
1758 | 0 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
1759 | 0 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
1760 | 0 | builder.addInclude(iItr->first, iItr->second); |
1761 | 0 | } |
1762 | | |
1763 | 370 | builder.setUseReplicatedComposites(glslangIntermediate->usingReplicatedComposites()); |
1764 | | |
1765 | 370 | stdBuiltins = builder.import("GLSL.std.450"); |
1766 | | |
1767 | 370 | spv::AddressingModel addressingModel = spv::AddressingModel::Logical; |
1768 | 370 | spv::MemoryModel memoryModel = spv::MemoryModel::GLSL450; |
1769 | | |
1770 | 370 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
1771 | 27 | addressingModel = spv::AddressingModel::PhysicalStorageBuffer64EXT; |
1772 | 27 | builder.addIncorporatedExtension(spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
1773 | 27 | builder.addCapability(spv::Capability::PhysicalStorageBufferAddressesEXT); |
1774 | 27 | } |
1775 | 370 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
1776 | 0 | memoryModel = spv::MemoryModel::VulkanKHR; |
1777 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
1778 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5); |
1779 | 0 | } |
1780 | 370 | builder.setMemoryModel(addressingModel, memoryModel); |
1781 | | |
1782 | 370 | if (glslangIntermediate->usingVariablePointers()) { |
1783 | 0 | builder.addCapability(spv::Capability::VariablePointers); |
1784 | 0 | } |
1785 | | |
1786 | | // If not linking, there is no entry point |
1787 | 370 | if (!options.compileOnly) { |
1788 | 370 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
1789 | 370 | entryPoint = |
1790 | 370 | builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
1791 | 370 | } |
1792 | | |
1793 | | // Add the source extensions |
1794 | 370 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
1795 | 2.10k | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
1796 | 1.73k | builder.addSourceExtension(it->c_str()); |
1797 | | |
1798 | | // Add the top-level modes for this shader. |
1799 | | |
1800 | 370 | if (glslangIntermediate->getXfbMode()) { |
1801 | 0 | builder.addCapability(spv::Capability::TransformFeedback); |
1802 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::Xfb); |
1803 | 0 | } |
1804 | | |
1805 | 370 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
1806 | 0 | builder.addCapability(spv::Capability::RayTraversalPrimitiveCullingKHR); |
1807 | 0 | } |
1808 | | |
1809 | 370 | if (glslangIntermediate->getSubgroupUniformControlFlow()) { |
1810 | 0 | builder.addExtension(spv::E_SPV_KHR_subgroup_uniform_control_flow); |
1811 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::SubgroupUniformControlFlowKHR); |
1812 | 0 | } |
1813 | 370 | if (glslangIntermediate->getMaximallyReconverges()) { |
1814 | 0 | builder.addExtension(spv::E_SPV_KHR_maximal_reconvergence); |
1815 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::MaximallyReconvergesKHR); |
1816 | 0 | } |
1817 | | |
1818 | 370 | if (glslangIntermediate->getQuadDerivMode()) |
1819 | 0 | { |
1820 | 0 | builder.addCapability(spv::Capability::QuadControlKHR); |
1821 | 0 | builder.addExtension(spv::E_SPV_KHR_quad_control); |
1822 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::QuadDerivativesKHR); |
1823 | 0 | } |
1824 | | |
1825 | 370 | if (glslangIntermediate->getReqFullQuadsMode()) |
1826 | 0 | { |
1827 | 0 | builder.addCapability(spv::Capability::QuadControlKHR); |
1828 | 0 | builder.addExtension(spv::E_SPV_KHR_quad_control); |
1829 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::RequireFullQuadsKHR); |
1830 | 0 | } |
1831 | | |
1832 | 370 | if (glslangIntermediate->usingShader64BitIndexing()) |
1833 | 0 | { |
1834 | 0 | builder.addCapability(spv::Capability::Shader64BitIndexingEXT); |
1835 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_64bit_indexing); |
1836 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::Shader64BitIndexingEXT); |
1837 | 0 | } |
1838 | | |
1839 | 370 | spv::ExecutionMode mode; |
1840 | 370 | switch (glslangIntermediate->getStage()) { |
1841 | 339 | case EShLangVertex: |
1842 | 339 | builder.addCapability(spv::Capability::Shader); |
1843 | 339 | break; |
1844 | | |
1845 | 31 | case EShLangFragment: |
1846 | 31 | builder.addCapability(spv::Capability::Shader); |
1847 | 31 | if (glslangIntermediate->getPixelCenterInteger()) |
1848 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::PixelCenterInteger); |
1849 | | |
1850 | 31 | if (glslangIntermediate->getOriginUpperLeft()) |
1851 | 31 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OriginUpperLeft); |
1852 | 0 | else |
1853 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OriginLowerLeft); |
1854 | | |
1855 | 31 | if (glslangIntermediate->getEarlyFragmentTests()) |
1856 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::EarlyFragmentTests); |
1857 | | |
1858 | 31 | if (glslangIntermediate->getEarlyAndLateFragmentTestsAMD()) |
1859 | 0 | { |
1860 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::EarlyAndLateFragmentTestsAMD); |
1861 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_early_and_late_fragment_tests); |
1862 | 0 | } |
1863 | | |
1864 | 31 | if (glslangIntermediate->getPostDepthCoverage()) { |
1865 | 0 | builder.addCapability(spv::Capability::SampleMaskPostDepthCoverage); |
1866 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::PostDepthCoverage); |
1867 | 0 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
1868 | 0 | } |
1869 | | |
1870 | 31 | if (glslangIntermediate->getNonCoherentColorAttachmentReadEXT()) { |
1871 | 0 | builder.addCapability(spv::Capability::TileImageColorReadAccessEXT); |
1872 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::NonCoherentColorAttachmentReadEXT); |
1873 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
1874 | 0 | } |
1875 | | |
1876 | 31 | if (glslangIntermediate->getNonCoherentDepthAttachmentReadEXT()) { |
1877 | 0 | builder.addCapability(spv::Capability::TileImageDepthReadAccessEXT); |
1878 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::NonCoherentDepthAttachmentReadEXT); |
1879 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
1880 | 0 | } |
1881 | | |
1882 | 31 | if (glslangIntermediate->getNonCoherentStencilAttachmentReadEXT()) { |
1883 | 0 | builder.addCapability(spv::Capability::TileImageStencilReadAccessEXT); |
1884 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::NonCoherentStencilAttachmentReadEXT); |
1885 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
1886 | 0 | } |
1887 | | |
1888 | 31 | if (glslangIntermediate->getNonCoherentTileAttachmentReadQCOM()) { |
1889 | 0 | builder.addCapability(spv::Capability::TileShadingQCOM); |
1890 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::NonCoherentTileAttachmentReadQCOM); |
1891 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1892 | 0 | } |
1893 | | |
1894 | 31 | if (glslangIntermediate->isDepthReplacing()) |
1895 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::DepthReplacing); |
1896 | | |
1897 | 31 | if (glslangIntermediate->isStencilReplacing()) |
1898 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::StencilRefReplacingEXT); |
1899 | | |
1900 | 31 | switch(glslangIntermediate->getDepth()) { |
1901 | 0 | case glslang::EldGreater: mode = spv::ExecutionMode::DepthGreater; break; |
1902 | 0 | case glslang::EldLess: mode = spv::ExecutionMode::DepthLess; break; |
1903 | 0 | case glslang::EldUnchanged: mode = spv::ExecutionMode::DepthUnchanged; break; |
1904 | 31 | default: mode = spv::ExecutionMode::Max; break; |
1905 | 31 | } |
1906 | | |
1907 | 31 | if (mode != spv::ExecutionMode::Max) |
1908 | 0 | builder.addExecutionMode(shaderEntry, mode); |
1909 | | |
1910 | 31 | switch (glslangIntermediate->getStencil()) { |
1911 | 0 | case glslang::ElsRefUnchangedFrontAMD: mode = spv::ExecutionMode::StencilRefUnchangedFrontAMD; break; |
1912 | 0 | case glslang::ElsRefGreaterFrontAMD: mode = spv::ExecutionMode::StencilRefGreaterFrontAMD; break; |
1913 | 0 | case glslang::ElsRefLessFrontAMD: mode = spv::ExecutionMode::StencilRefLessFrontAMD; break; |
1914 | 0 | case glslang::ElsRefUnchangedBackAMD: mode = spv::ExecutionMode::StencilRefUnchangedBackAMD; break; |
1915 | 0 | case glslang::ElsRefGreaterBackAMD: mode = spv::ExecutionMode::StencilRefGreaterBackAMD; break; |
1916 | 0 | case glslang::ElsRefLessBackAMD: mode = spv::ExecutionMode::StencilRefLessBackAMD; break; |
1917 | 31 | default: mode = spv::ExecutionMode::Max; break; |
1918 | 31 | } |
1919 | | |
1920 | 31 | if (mode != spv::ExecutionMode::Max) |
1921 | 0 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
1922 | 31 | switch (glslangIntermediate->getInterlockOrdering()) { |
1923 | 0 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionMode::PixelInterlockOrderedEXT; |
1924 | 0 | break; |
1925 | 0 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionMode::PixelInterlockUnorderedEXT; |
1926 | 0 | break; |
1927 | 0 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionMode::SampleInterlockOrderedEXT; |
1928 | 0 | break; |
1929 | 0 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionMode::SampleInterlockUnorderedEXT; |
1930 | 0 | break; |
1931 | 0 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionMode::ShadingRateInterlockOrderedEXT; |
1932 | 0 | break; |
1933 | 0 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionMode::ShadingRateInterlockUnorderedEXT; |
1934 | 0 | break; |
1935 | 31 | default: mode = spv::ExecutionMode::Max; |
1936 | 31 | break; |
1937 | 31 | } |
1938 | 31 | if (mode != spv::ExecutionMode::Max) { |
1939 | 0 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
1940 | 0 | if (mode == spv::ExecutionMode::ShadingRateInterlockOrderedEXT || |
1941 | 0 | mode == spv::ExecutionMode::ShadingRateInterlockUnorderedEXT) { |
1942 | 0 | builder.addCapability(spv::Capability::FragmentShaderShadingRateInterlockEXT); |
1943 | 0 | } else if (mode == spv::ExecutionMode::PixelInterlockOrderedEXT || |
1944 | 0 | mode == spv::ExecutionMode::PixelInterlockUnorderedEXT) { |
1945 | 0 | builder.addCapability(spv::Capability::FragmentShaderPixelInterlockEXT); |
1946 | 0 | } else { |
1947 | 0 | builder.addCapability(spv::Capability::FragmentShaderSampleInterlockEXT); |
1948 | 0 | } |
1949 | 0 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
1950 | 0 | } |
1951 | 31 | break; |
1952 | | |
1953 | 0 | case EShLangCompute: { |
1954 | 0 | builder.addCapability(spv::Capability::Shader); |
1955 | 0 | bool needSizeId = false; |
1956 | 0 | for (int dim = 0; dim < 3; ++dim) { |
1957 | 0 | if ((glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet)) { |
1958 | 0 | needSizeId = true; |
1959 | 0 | break; |
1960 | 0 | } |
1961 | 0 | } |
1962 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6 && needSizeId) { |
1963 | 0 | std::vector<spv::Id> dimConstId; |
1964 | 0 | for (int dim = 0; dim < 3; ++dim) { |
1965 | 0 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
1966 | 0 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
1967 | 0 | if (specConst) { |
1968 | 0 | builder.addDecoration(dimConstId.back(), spv::Decoration::SpecId, |
1969 | 0 | glslangIntermediate->getLocalSizeSpecId(dim)); |
1970 | 0 | needSizeId = true; |
1971 | 0 | } |
1972 | 0 | } |
1973 | 0 | builder.addExecutionModeId(shaderEntry, spv::ExecutionMode::LocalSizeId, dimConstId); |
1974 | 0 | } else { |
1975 | 0 | if (glslangIntermediate->getTileShadingRateQCOM(0) >= 1 || glslangIntermediate->getTileShadingRateQCOM(1) >= 1 || glslangIntermediate->getTileShadingRateQCOM(2) >= 1) { |
1976 | 0 | auto rate_x = glslangIntermediate->getTileShadingRateQCOM(0); |
1977 | 0 | auto rate_y = glslangIntermediate->getTileShadingRateQCOM(1); |
1978 | 0 | auto rate_z = glslangIntermediate->getTileShadingRateQCOM(2); |
1979 | 0 | rate_x = ( rate_x == 0 ? 1 : rate_x ); |
1980 | 0 | rate_y = ( rate_y == 0 ? 1 : rate_y ); |
1981 | 0 | rate_z = ( rate_z == 0 ? 1 : rate_z ); |
1982 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::TileShadingRateQCOM, rate_x, rate_y, rate_z); |
1983 | 0 | } else { |
1984 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::LocalSize, glslangIntermediate->getLocalSize(0), |
1985 | 0 | glslangIntermediate->getLocalSize(1), |
1986 | 0 | glslangIntermediate->getLocalSize(2)); |
1987 | 0 | } |
1988 | 0 | } |
1989 | 0 | addDerivativeGroupExecutionMode(builder, *glslangIntermediate, shaderEntry); |
1990 | |
|
1991 | 0 | if (glslangIntermediate->getNonCoherentTileAttachmentReadQCOM()) { |
1992 | 0 | builder.addCapability(spv::Capability::TileShadingQCOM); |
1993 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::NonCoherentTileAttachmentReadQCOM); |
1994 | 0 | builder.addExtension(spv::E_SPV_QCOM_tile_shading); |
1995 | 0 | } |
1996 | |
|
1997 | 0 | break; |
1998 | 31 | } |
1999 | 0 | case EShLangTessEvaluation: |
2000 | 0 | case EShLangTessControl: |
2001 | 0 | builder.addCapability(spv::Capability::Tessellation); |
2002 | |
|
2003 | 0 | glslang::TLayoutGeometry primitive; |
2004 | |
|
2005 | 0 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
2006 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OutputVertices, |
2007 | 0 | glslangIntermediate->getVertices()); |
2008 | 0 | primitive = glslangIntermediate->getOutputPrimitive(); |
2009 | 0 | } else { |
2010 | 0 | primitive = glslangIntermediate->getInputPrimitive(); |
2011 | 0 | } |
2012 | |
|
2013 | 0 | switch (primitive) { |
2014 | 0 | case glslang::ElgTriangles: mode = spv::ExecutionMode::Triangles; break; |
2015 | 0 | case glslang::ElgQuads: mode = spv::ExecutionMode::Quads; break; |
2016 | 0 | case glslang::ElgIsolines: mode = spv::ExecutionMode::Isolines; break; |
2017 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2018 | 0 | } |
2019 | 0 | if (mode != spv::ExecutionMode::Max) |
2020 | 0 | builder.addExecutionMode(shaderEntry, mode); |
2021 | |
|
2022 | 0 | switch (glslangIntermediate->getVertexSpacing()) { |
2023 | 0 | case glslang::EvsEqual: mode = spv::ExecutionMode::SpacingEqual; break; |
2024 | 0 | case glslang::EvsFractionalEven: mode = spv::ExecutionMode::SpacingFractionalEven; break; |
2025 | 0 | case glslang::EvsFractionalOdd: mode = spv::ExecutionMode::SpacingFractionalOdd; break; |
2026 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2027 | 0 | } |
2028 | 0 | if (mode != spv::ExecutionMode::Max) |
2029 | 0 | builder.addExecutionMode(shaderEntry, mode); |
2030 | |
|
2031 | 0 | switch (glslangIntermediate->getVertexOrder()) { |
2032 | 0 | case glslang::EvoCw: mode = spv::ExecutionMode::VertexOrderCw; break; |
2033 | 0 | case glslang::EvoCcw: mode = spv::ExecutionMode::VertexOrderCcw; break; |
2034 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2035 | 0 | } |
2036 | 0 | if (mode != spv::ExecutionMode::Max) |
2037 | 0 | builder.addExecutionMode(shaderEntry, mode); |
2038 | |
|
2039 | 0 | if (glslangIntermediate->getPointMode()) |
2040 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::PointMode); |
2041 | 0 | break; |
2042 | | |
2043 | 0 | case EShLangGeometry: |
2044 | 0 | builder.addCapability(spv::Capability::Geometry); |
2045 | 0 | switch (glslangIntermediate->getInputPrimitive()) { |
2046 | 0 | case glslang::ElgPoints: mode = spv::ExecutionMode::InputPoints; break; |
2047 | 0 | case glslang::ElgLines: mode = spv::ExecutionMode::InputLines; break; |
2048 | 0 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionMode::InputLinesAdjacency; break; |
2049 | 0 | case glslang::ElgTriangles: mode = spv::ExecutionMode::Triangles; break; |
2050 | 0 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionMode::InputTrianglesAdjacency; break; |
2051 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2052 | 0 | } |
2053 | 0 | if (mode != spv::ExecutionMode::Max) |
2054 | 0 | builder.addExecutionMode(shaderEntry, mode); |
2055 | |
|
2056 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::Invocations, glslangIntermediate->getInvocations()); |
2057 | |
|
2058 | 0 | switch (glslangIntermediate->getOutputPrimitive()) { |
2059 | 0 | case glslang::ElgPoints: mode = spv::ExecutionMode::OutputPoints; break; |
2060 | 0 | case glslang::ElgLineStrip: mode = spv::ExecutionMode::OutputLineStrip; break; |
2061 | 0 | case glslang::ElgTriangleStrip: mode = spv::ExecutionMode::OutputTriangleStrip; break; |
2062 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2063 | 0 | } |
2064 | 0 | if (mode != spv::ExecutionMode::Max) |
2065 | 0 | builder.addExecutionMode(shaderEntry, mode); |
2066 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OutputVertices, glslangIntermediate->getVertices()); |
2067 | 0 | break; |
2068 | | |
2069 | 0 | case EShLangRayGen: |
2070 | 0 | case EShLangIntersect: |
2071 | 0 | case EShLangAnyHit: |
2072 | 0 | case EShLangClosestHit: |
2073 | 0 | case EShLangMiss: |
2074 | 0 | case EShLangCallable: |
2075 | 0 | { |
2076 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
2077 | 0 | if (extensions.find("GL_EXT_opacity_micromap") != extensions.end()) { |
2078 | 0 | builder.addCapability(spv::Capability::RayTracingOpacityMicromapEXT); |
2079 | 0 | builder.addExtension("SPV_EXT_opacity_micromap"); |
2080 | 0 | } |
2081 | 0 | if (extensions.find("GL_NV_ray_tracing") == extensions.end()) { |
2082 | 0 | builder.addCapability(spv::Capability::RayTracingKHR); |
2083 | 0 | builder.addExtension("SPV_KHR_ray_tracing"); |
2084 | 0 | } |
2085 | 0 | else { |
2086 | 0 | builder.addCapability(spv::Capability::RayTracingNV); |
2087 | 0 | builder.addExtension("SPV_NV_ray_tracing"); |
2088 | 0 | } |
2089 | 0 | if (glslangIntermediate->getStage() != EShLangRayGen && glslangIntermediate->getStage() != EShLangCallable) { |
2090 | 0 | if (extensions.find("GL_EXT_ray_cull_mask") != extensions.end()) { |
2091 | 0 | builder.addCapability(spv::Capability::RayCullMaskKHR); |
2092 | 0 | builder.addExtension("SPV_KHR_ray_cull_mask"); |
2093 | 0 | } |
2094 | 0 | if (extensions.find("GL_EXT_ray_tracing_position_fetch") != extensions.end()) { |
2095 | 0 | builder.addCapability(spv::Capability::RayTracingPositionFetchKHR); |
2096 | 0 | builder.addExtension("SPV_KHR_ray_tracing_position_fetch"); |
2097 | 0 | } |
2098 | 0 | } |
2099 | 0 | break; |
2100 | 0 | } |
2101 | 0 | case EShLangTask: |
2102 | 0 | case EShLangMesh: |
2103 | 0 | if(isMeshShaderExt) { |
2104 | 0 | builder.addCapability(spv::Capability::MeshShadingEXT); |
2105 | 0 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
2106 | 0 | } else { |
2107 | 0 | builder.addCapability(spv::Capability::MeshShadingNV); |
2108 | 0 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
2109 | 0 | } |
2110 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
2111 | 0 | std::vector<spv::Id> dimConstId; |
2112 | 0 | for (int dim = 0; dim < 3; ++dim) { |
2113 | 0 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
2114 | 0 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
2115 | 0 | if (specConst) { |
2116 | 0 | builder.addDecoration(dimConstId.back(), spv::Decoration::SpecId, |
2117 | 0 | glslangIntermediate->getLocalSizeSpecId(dim)); |
2118 | 0 | } |
2119 | 0 | } |
2120 | 0 | builder.addExecutionModeId(shaderEntry, spv::ExecutionMode::LocalSizeId, dimConstId); |
2121 | 0 | } else { |
2122 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::LocalSize, glslangIntermediate->getLocalSize(0), |
2123 | 0 | glslangIntermediate->getLocalSize(1), |
2124 | 0 | glslangIntermediate->getLocalSize(2)); |
2125 | 0 | } |
2126 | 0 | addDerivativeGroupExecutionMode(builder, *glslangIntermediate, shaderEntry); |
2127 | 0 | if (glslangIntermediate->getStage() == EShLangMesh) { |
2128 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OutputVertices, |
2129 | 0 | glslangIntermediate->getVertices()); |
2130 | 0 | builder.addExecutionMode(shaderEntry, spv::ExecutionMode::OutputPrimitivesNV, |
2131 | 0 | glslangIntermediate->getPrimitives()); |
2132 | |
|
2133 | 0 | switch (glslangIntermediate->getOutputPrimitive()) { |
2134 | 0 | case glslang::ElgPoints: mode = spv::ExecutionMode::OutputPoints; break; |
2135 | 0 | case glslang::ElgLines: mode = spv::ExecutionMode::OutputLinesNV; break; |
2136 | 0 | case glslang::ElgTriangles: mode = spv::ExecutionMode::OutputTrianglesNV; break; |
2137 | 0 | default: mode = spv::ExecutionMode::Max; break; |
2138 | 0 | } |
2139 | 0 | if (mode != spv::ExecutionMode::Max) |
2140 | 0 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
2141 | 0 | } |
2142 | 0 | break; |
2143 | | |
2144 | 0 | default: |
2145 | 0 | break; |
2146 | 370 | } |
2147 | | |
2148 | | // |
2149 | | // Add SPIR-V requirements (GL_EXT_spirv_intrinsics) |
2150 | | // |
2151 | 370 | if (glslangIntermediate->hasSpirvRequirement()) { |
2152 | 18 | const glslang::TSpirvRequirement& spirvRequirement = glslangIntermediate->getSpirvRequirement(); |
2153 | | |
2154 | | // Add SPIR-V extension requirement |
2155 | 18 | for (auto& extension : spirvRequirement.extensions) |
2156 | 18 | builder.addExtension(extension.c_str()); |
2157 | | |
2158 | | // Add SPIR-V capability requirement |
2159 | 18 | for (auto capability : spirvRequirement.capabilities) |
2160 | 0 | builder.addCapability(static_cast<spv::Capability>(capability)); |
2161 | 18 | } |
2162 | | |
2163 | | // |
2164 | | // Add SPIR-V execution mode qualifiers (GL_EXT_spirv_intrinsics) |
2165 | | // |
2166 | 370 | if (glslangIntermediate->hasSpirvExecutionMode()) { |
2167 | 0 | const glslang::TSpirvExecutionMode spirvExecutionMode = glslangIntermediate->getSpirvExecutionMode(); |
2168 | | |
2169 | | // Add spirv_execution_mode |
2170 | 0 | for (auto& mode : spirvExecutionMode.modes) { |
2171 | 0 | if (!mode.second.empty()) { |
2172 | 0 | std::vector<unsigned> literals; |
2173 | 0 | TranslateLiterals(mode.second, literals); |
2174 | 0 | builder.addExecutionMode(shaderEntry, static_cast<spv::ExecutionMode>(mode.first), literals); |
2175 | 0 | } else |
2176 | 0 | builder.addExecutionMode(shaderEntry, static_cast<spv::ExecutionMode>(mode.first)); |
2177 | 0 | } |
2178 | | |
2179 | | // Add spirv_execution_mode_id |
2180 | 0 | for (auto& modeId : spirvExecutionMode.modeIds) { |
2181 | 0 | std::vector<spv::Id> operandIds; |
2182 | 0 | assert(!modeId.second.empty()); |
2183 | 0 | for (auto extraOperand : modeId.second) { |
2184 | 0 | if (extraOperand->getType().getQualifier().isSpecConstant()) |
2185 | 0 | operandIds.push_back(getSymbolId(extraOperand->getAsSymbolNode())); |
2186 | 0 | else |
2187 | 0 | operandIds.push_back(createSpvConstant(*extraOperand)); |
2188 | 0 | } |
2189 | 0 | builder.addExecutionModeId(shaderEntry, static_cast<spv::ExecutionMode>(modeId.first), operandIds); |
2190 | 0 | } |
2191 | 0 | } |
2192 | 370 | } |
2193 | | |
2194 | | // Finish creating SPV, after the traversal is complete. |
2195 | | void TGlslangToSpvTraverser::finishSpv(bool compileOnly) |
2196 | 370 | { |
2197 | | // If not linking, an entry point is not expected |
2198 | 370 | if (!compileOnly) { |
2199 | | // Finish the entry point function |
2200 | 370 | if (!entryPointTerminated) { |
2201 | 0 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
2202 | 0 | builder.leaveFunction(); |
2203 | 0 | } |
2204 | | |
2205 | | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
2206 | 370 | entryPoint->reserveOperands(iOSet.size()); |
2207 | 370 | for (auto id : iOSet) |
2208 | 453 | entryPoint->addIdOperand(id); |
2209 | 370 | } |
2210 | | |
2211 | | // Add capabilities, extensions, remove unneeded decorations, etc., |
2212 | | // based on the resulting SPIR-V. |
2213 | | // Note: WebGPU code generation must have the opportunity to aggressively |
2214 | | // prune unreachable merge blocks and continue targets. |
2215 | 370 | builder.postProcess(compileOnly); |
2216 | 370 | } |
2217 | | |
2218 | | // Write the SPV into 'out'. |
2219 | | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
2220 | 370 | { |
2221 | 370 | builder.dump(out); |
2222 | 370 | } |
2223 | | |
2224 | | // |
2225 | | // Implement the traversal functions. |
2226 | | // |
2227 | | // Return true from interior nodes to have the external traversal |
2228 | | // continue on to children. Return false if children were |
2229 | | // already processed. |
2230 | | // |
2231 | | |
2232 | | // |
2233 | | // Symbols can turn into |
2234 | | // - uniform/input reads |
2235 | | // - output writes |
2236 | | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
2237 | | // - something simple that degenerates into the last bullet |
2238 | | // |
2239 | | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
2240 | 25.4k | { |
2241 | | // We update the line information even though no code might be generated here |
2242 | | // This is helpful to yield correct lines for control flow instructions |
2243 | 25.4k | if (!linkageOnly) { |
2244 | 22.1k | builder.setDebugSourceLocation(symbol->getLoc().line, symbol->getLoc().getFilename()); |
2245 | 22.1k | } |
2246 | | |
2247 | 25.4k | if (symbol->getBasicType() == glslang::EbtFunction) { |
2248 | 0 | return; |
2249 | 0 | } |
2250 | | |
2251 | 25.4k | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2252 | 25.4k | if (symbol->getType().isStruct()) |
2253 | 4.21k | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
2254 | | |
2255 | 25.4k | if (symbol->getType().getQualifier().isSpecConstant()) |
2256 | 918 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2257 | 25.4k | #ifdef ENABLE_HLSL |
2258 | | // Skip symbol handling if it is string-typed |
2259 | 25.4k | if (symbol->getBasicType() == glslang::EbtString) |
2260 | 0 | return; |
2261 | 25.4k | #endif |
2262 | | |
2263 | | // getSymbolId() will set up all the IO decorations on the first call. |
2264 | | // Formal function parameters were mapped during makeFunctions(). |
2265 | 25.4k | spv::Id id = getSymbolId(symbol); |
2266 | | |
2267 | 25.4k | if (symbol->getType().getQualifier().isTaskPayload()) |
2268 | 0 | taskPayloadID = id; // cache the taskPayloadID to be used it as operand for OpEmitMeshTasksEXT |
2269 | | |
2270 | 25.4k | if (builder.isPointer(id)) { |
2271 | 24.4k | if (!symbol->getType().getQualifier().isParamInput() && |
2272 | 24.1k | !symbol->getType().getQualifier().isParamOutput()) { |
2273 | | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
2274 | | // Consider adding to the OpEntryPoint interface list. |
2275 | | // Only looking at structures if they have at least one member. |
2276 | 24.1k | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
2277 | 24.1k | spv::StorageClass sc = builder.getStorageClass(id); |
2278 | | // Before SPIR-V 1.4, we only want to include Input and Output. |
2279 | | // Starting with SPIR-V 1.4, we want all globals. |
2280 | 24.1k | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalVariable(id)) || |
2281 | 24.1k | (sc == spv::StorageClass::Input || sc == spv::StorageClass::Output)) { |
2282 | 3.97k | iOSet.insert(id); |
2283 | 3.97k | } |
2284 | 24.1k | } |
2285 | 24.1k | } |
2286 | | |
2287 | | // If the SPIR-V type is required to be different than the AST type |
2288 | | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
2289 | | // translate now from the SPIR-V type to the AST type, for the consuming |
2290 | | // operation. |
2291 | | // Note this turns it from an l-value to an r-value. |
2292 | | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
2293 | 24.4k | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
2294 | 3.48k | id = translateForcedType(id); |
2295 | 24.4k | } |
2296 | | |
2297 | | // Only process non-linkage-only nodes for generating actual static uses |
2298 | 25.4k | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
2299 | | // Prepare to generate code for the access |
2300 | | |
2301 | | // L-value chains will be computed left to right. We're on the symbol now, |
2302 | | // which is the left-most part of the access chain, so now is "clear" time, |
2303 | | // followed by setting the base. |
2304 | 23.0k | builder.clearAccessChain(); |
2305 | | |
2306 | | // For now, we consider all user variables as being in memory, so they are pointers, |
2307 | | // except for |
2308 | | // A) R-Value arguments to a function, which are an intermediate object. |
2309 | | // See comments in handleUserFunctionCall(). |
2310 | | // B) Specialization constants (normal constants don't even come in as a variable), |
2311 | | // These are also pure R-values. |
2312 | | // C) R-Values from type translation, see above call to translateForcedType() |
2313 | 23.0k | glslang::TQualifier qualifier = symbol->getQualifier(); |
2314 | 23.0k | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
2315 | 22.1k | (!builder.isPointerType(builder.getTypeId(id)) && !builder.isUntypedPointer(id))) |
2316 | 918 | builder.setAccessChainRValue(id); |
2317 | 22.1k | else |
2318 | 22.1k | builder.setAccessChainLValue(id); |
2319 | 23.0k | } |
2320 | | |
2321 | 25.4k | #ifdef ENABLE_HLSL |
2322 | | // Process linkage-only nodes for any special additional interface work. |
2323 | 25.4k | if (linkageOnly) { |
2324 | 3.28k | if (glslangIntermediate->getHlslFunctionality1()) { |
2325 | | // Map implicit counter buffers to their originating buffers, which should have been |
2326 | | // seen by now, given earlier pruning of unused counters, and preservation of order |
2327 | | // of declaration. |
2328 | 656 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
2329 | 390 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
2330 | | // Save possible originating buffers for counter buffers, keyed by |
2331 | | // making the potential counter-buffer name. |
2332 | 390 | std::string keyName = symbol->getName().c_str(); |
2333 | 390 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
2334 | 390 | counterOriginator[keyName] = symbol; |
2335 | 390 | } else { |
2336 | | // Handle a counter buffer, by finding the saved originating buffer. |
2337 | 0 | std::string keyName = symbol->getName().c_str(); |
2338 | 0 | auto it = counterOriginator.find(keyName); |
2339 | 0 | if (it != counterOriginator.end()) { |
2340 | 0 | id = getSymbolId(it->second); |
2341 | 0 | if (id != spv::NoResult) { |
2342 | 0 | spv::Id counterId = getSymbolId(symbol); |
2343 | 0 | if (counterId != spv::NoResult) { |
2344 | 0 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
2345 | 0 | builder.addDecorationId(id, spv::Decoration::HlslCounterBufferGOOGLE, counterId); |
2346 | 0 | } |
2347 | 0 | } |
2348 | 0 | } |
2349 | 0 | } |
2350 | 390 | } |
2351 | 656 | } |
2352 | 3.28k | } |
2353 | 25.4k | #endif |
2354 | 25.4k | } |
2355 | | |
2356 | | // Create new untyped access chain instruction to descriptor heap, based on EXT_descriptor_heap extension. |
2357 | | void TGlslangToSpvTraverser::recordDescHeapAccessChainInfo(glslang::TIntermBinary* node) |
2358 | 0 | { |
2359 | | // EXT_descriptor_heap |
2360 | 0 | glslang::TType* nodeTy = node->getWritableType().clone(); |
2361 | | // For buffer/structure type, using its basic structure type: |
2362 | | // data ptr will be first loaded into a BufferPointerType before finally gets chained accessed. |
2363 | 0 | if (node->getLeft()->getQualifier().isUniformOrBuffer()) |
2364 | 0 | nodeTy = node->getLeft()->getType().clone(); |
2365 | 0 | if (nodeTy->isArray()) |
2366 | 0 | nodeTy->clearArraySizes(); |
2367 | 0 | spv::BuiltIn bt = spv::BuiltIn::Max; |
2368 | 0 | unsigned int firstArrIndex = 0; |
2369 | 0 | auto rsrcOffsetIdx = builder.isStructureHeapMember(builder.getTypeId(builder.getAccessChain().base), |
2370 | 0 | builder.getAccessChain().indexChain, 0, &bt, &firstArrIndex); |
2371 | 0 | spv::Id remappedBuiltin = bt == spv::BuiltIn::Max ? 0 : builtInVariableIds[unsigned(bt)]; |
2372 | 0 | nodeTy->getQualifier().layoutDescriptorInnerBlock = false; |
2373 | | // Extra dimension is not needed when translate storage and type. Real loading type is based on orignal AST nodes. |
2374 | 0 | builder.setAccessChainDescHeapInfo(TranslateStorageClass(*nodeTy), convertGlslangToSpvType(*nodeTy), |
2375 | 0 | nodeTy->getQualifier().layoutDescriptorStride, rsrcOffsetIdx, remappedBuiltin, firstArrIndex); |
2376 | 0 | } |
2377 | | |
2378 | | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
2379 | 14.8k | { |
2380 | 14.8k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
2381 | 14.8k | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
2382 | 3.82k | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
2383 | 3.82k | } |
2384 | 14.8k | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
2385 | 0 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
2386 | 0 | } |
2387 | | |
2388 | 14.8k | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2389 | 14.8k | if (node->getType().getQualifier().isSpecConstant()) |
2390 | 10 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2391 | | |
2392 | | // First, handle special cases |
2393 | 14.8k | switch (node->getOp()) { |
2394 | 4.03k | case glslang::EOpAssign: |
2395 | 6.47k | case glslang::EOpAddAssign: |
2396 | 6.48k | case glslang::EOpSubAssign: |
2397 | 6.49k | case glslang::EOpMulAssign: |
2398 | 6.49k | case glslang::EOpVectorTimesMatrixAssign: |
2399 | 6.51k | case glslang::EOpVectorTimesScalarAssign: |
2400 | 6.51k | case glslang::EOpMatrixTimesScalarAssign: |
2401 | 6.51k | case glslang::EOpMatrixTimesMatrixAssign: |
2402 | 6.52k | case glslang::EOpDivAssign: |
2403 | 6.53k | case glslang::EOpModAssign: |
2404 | 6.54k | case glslang::EOpAndAssign: |
2405 | 6.55k | case glslang::EOpInclusiveOrAssign: |
2406 | 6.56k | case glslang::EOpExclusiveOrAssign: |
2407 | 6.56k | case glslang::EOpLeftShiftAssign: |
2408 | 6.57k | case glslang::EOpRightShiftAssign: |
2409 | | // A bin-op assign "a += b" means the same thing as "a = a + b" |
2410 | | // where a is evaluated before b. For a simple assignment, GLSL |
2411 | | // says to evaluate the left before the right. So, always, left |
2412 | | // node then right node. |
2413 | 6.57k | { |
2414 | | // get the left l-value, save it away |
2415 | 6.57k | builder.clearAccessChain(); |
2416 | 6.57k | node->getLeft()->traverse(this); |
2417 | 6.57k | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
2418 | | |
2419 | | // evaluate the right |
2420 | 6.57k | builder.clearAccessChain(); |
2421 | 6.57k | node->getRight()->traverse(this); |
2422 | 6.57k | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
2423 | | |
2424 | | // reset line number for assignment |
2425 | 6.57k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
2426 | | |
2427 | 6.57k | if (node->getOp() != glslang::EOpAssign) { |
2428 | | // the left is also an r-value |
2429 | 2.53k | builder.setAccessChain(lValue); |
2430 | 2.53k | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
2431 | | |
2432 | | // do the operation |
2433 | 2.53k | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
2434 | 2.53k | coherentFlags |= TranslateCoherent(node->getRight()->getType()); |
2435 | 2.53k | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
2436 | 2.53k | TranslateNoContractionDecoration(node->getType().getQualifier()), |
2437 | 2.53k | TranslateNonUniformDecoration(coherentFlags) }; |
2438 | 2.53k | rValue = createBinaryOperation(node->getOp(), decorations, |
2439 | 2.53k | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
2440 | 2.53k | node->getType().getBasicType()); |
2441 | | |
2442 | | // these all need their counterparts in createBinaryOperation() |
2443 | 2.53k | assert(rValue != spv::NoResult); |
2444 | 2.53k | } |
2445 | | |
2446 | | // store the result |
2447 | 6.57k | builder.setAccessChain(lValue); |
2448 | 6.57k | multiTypeStore(node->getLeft()->getType(), rValue); |
2449 | | |
2450 | | // assignments are expressions having an rValue after they are evaluated... |
2451 | 6.57k | builder.clearAccessChain(); |
2452 | 6.57k | builder.setAccessChainRValue(rValue); |
2453 | 6.57k | } |
2454 | 6.57k | return false; |
2455 | 1.75k | case glslang::EOpIndexDirect: |
2456 | 5.66k | case glslang::EOpIndexDirectStruct: |
2457 | 5.66k | { |
2458 | | // Structure, array, matrix, or vector indirection with statically known index. |
2459 | | // Get the left part of the access chain. |
2460 | 5.66k | node->getLeft()->traverse(this); |
2461 | | |
2462 | | // Add the next element in the chain |
2463 | | |
2464 | 5.66k | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
2465 | 5.66k | if (! node->getLeft()->getType().isArray() && |
2466 | 5.48k | node->getLeft()->getType().isVector() && |
2467 | 1.50k | node->getOp() == glslang::EOpIndexDirect) { |
2468 | | // Swizzle is uniform so propagate uniform into access chain |
2469 | 1.50k | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
2470 | 1.50k | coherentFlags.nonUniform = 0; |
2471 | | // This is essentially a hard-coded vector swizzle of size 1, |
2472 | | // so short circuit the access-chain stuff with a swizzle. |
2473 | 1.50k | std::vector<unsigned> swizzle; |
2474 | 1.50k | swizzle.push_back(glslangIndex); |
2475 | 1.50k | int dummySize; |
2476 | 1.50k | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
2477 | 1.50k | coherentFlags, |
2478 | 1.50k | glslangIntermediate->getBaseAlignmentScalar( |
2479 | 1.50k | node->getLeft()->getType(), dummySize)); |
2480 | 4.16k | } else { |
2481 | | |
2482 | | // Load through a block reference is performed with a dot operator that |
2483 | | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
2484 | | // do a load and reset the access chain. |
2485 | 4.16k | if (node->getLeft()->isReference() && |
2486 | 81 | !node->getLeft()->getType().isArray() && |
2487 | 72 | node->getOp() == glslang::EOpIndexDirectStruct) |
2488 | 72 | { |
2489 | 72 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
2490 | 72 | builder.clearAccessChain(); |
2491 | 72 | builder.setAccessChainLValue(left); |
2492 | 72 | } |
2493 | | |
2494 | 4.16k | int spvIndex = glslangIndex; |
2495 | 4.16k | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
2496 | 3.60k | node->getOp() == glslang::EOpIndexDirectStruct) |
2497 | 3.60k | { |
2498 | | // This may be, e.g., an anonymous block-member selection, which generally need |
2499 | | // index remapping due to hidden members in anonymous blocks. |
2500 | 3.60k | long long glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
2501 | 3.60k | if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
2502 | 3.60k | std::vector<int>& remapper = memberRemapper[glslangId]; |
2503 | 3.60k | assert(remapper.size() > 0); |
2504 | 3.60k | spvIndex = remapper[glslangIndex]; |
2505 | 3.60k | } |
2506 | 3.60k | } |
2507 | | |
2508 | | // Struct reference propagates uniform lvalue |
2509 | 4.16k | spv::Builder::AccessChain::CoherentFlags coherentFlags = |
2510 | 4.16k | TranslateCoherent(node->getLeft()->getType()); |
2511 | 4.16k | coherentFlags.nonUniform = 0; |
2512 | | |
2513 | | // normal case for indexing array or structure or block |
2514 | 4.16k | if ((node->getRight()->getType().getBasicType() == glslang::EbtUint && glslangIntermediate->usingPromoteUint32Indices()) || |
2515 | 4.16k | node->getRight()->getType().contains64BitInt()) { |
2516 | 0 | int64_t idx = node->getRight()->getType().contains64BitInt() ? |
2517 | 0 | node->getRight()->getAsConstantUnion()->getConstArray()[0].getI64Const() : |
2518 | 0 | node->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
2519 | 0 | builder.accessChainPush(builder.makeInt64Constant(idx), |
2520 | 0 | coherentFlags, |
2521 | 0 | node->getLeft()->getType().getBufferReferenceAlignment()); |
2522 | |
|
2523 | 4.16k | } else { |
2524 | 4.16k | builder.accessChainPush(builder.makeIntConstant(spvIndex), |
2525 | 4.16k | coherentFlags, |
2526 | 4.16k | node->getLeft()->getType().getBufferReferenceAlignment()); |
2527 | 4.16k | } |
2528 | | // Add capabilities here for accessing PointSize and clip/cull distance. |
2529 | | // We have deferred generation of associated capabilities until now. |
2530 | 4.16k | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
2531 | 3.83k | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
2532 | | |
2533 | | // EXT_descriptor_heap |
2534 | | // Record untyped descriptor heap access info. |
2535 | 4.16k | if (node->getLeft()->getType().isBuiltIn() && |
2536 | 0 | (node->getLeft()->getQualifier().builtIn == glslang::EbvResourceHeapEXT || |
2537 | 0 | node->getLeft()->getQualifier().builtIn == glslang::EbvSamplerHeapEXT)) { |
2538 | 0 | recordDescHeapAccessChainInfo(node); |
2539 | 0 | } |
2540 | 4.16k | } |
2541 | 5.66k | } |
2542 | 5.66k | return false; |
2543 | 170 | case glslang::EOpIndexIndirect: |
2544 | 170 | { |
2545 | | // Array, matrix, or vector indirection with variable index. |
2546 | | // Will use native SPIR-V access-chain for and array indirection; |
2547 | | // matrices are arrays of vectors, so will also work for a matrix. |
2548 | | // Will use the access chain's 'component' for variable index into a vector. |
2549 | | |
2550 | | // This adapter is building access chains left to right. |
2551 | | // Set up the access chain to the left. |
2552 | 170 | node->getLeft()->traverse(this); |
2553 | | |
2554 | | // save it so that computing the right side doesn't trash it |
2555 | 170 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
2556 | | |
2557 | | // compute the next index in the chain |
2558 | 170 | builder.clearAccessChain(); |
2559 | 170 | node->getRight()->traverse(this); |
2560 | 170 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
2561 | | |
2562 | | // Zero-extend smaller unsigned integer types for array indexing. |
2563 | | // SPIR-V OpAccessChain treats indices as signed, so we need to zero-extend |
2564 | | // unsigned types to preserve their values (signed types are fine as-is). |
2565 | 170 | spv::Id indexType = builder.getTypeId(index); |
2566 | 170 | if (builder.isUintType(indexType) && builder.getScalarTypeWidth(indexType) < 32) { |
2567 | | // Zero-extend unsigned types to preserve their values |
2568 | 0 | spv::Id uintType = builder.makeUintType(32); |
2569 | 0 | index = builder.createUnaryOp(spv::Op::OpUConvert, uintType, index); |
2570 | 0 | } |
2571 | | |
2572 | 170 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
2573 | | |
2574 | | // restore the saved access chain |
2575 | 170 | builder.setAccessChain(partial); |
2576 | | |
2577 | | // Only if index is nonUniform should we propagate nonUniform into access chain |
2578 | 170 | spv::Builder::AccessChain::CoherentFlags index_flags = TranslateCoherent(node->getRight()->getType()); |
2579 | 170 | spv::Builder::AccessChain::CoherentFlags coherent_flags = TranslateCoherent(node->getLeft()->getType()); |
2580 | 170 | coherent_flags.nonUniform = index_flags.nonUniform; |
2581 | | |
2582 | 170 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
2583 | 0 | int dummySize; |
2584 | 0 | builder.accessChainPushComponent( |
2585 | 0 | index, convertGlslangToSpvType(node->getLeft()->getType()), coherent_flags, |
2586 | 0 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
2587 | 0 | dummySize)); |
2588 | 170 | } else { |
2589 | 170 | if (glslangIntermediate->usingPromoteUint32Indices() && |
2590 | 0 | node->getRight()->getType().getBasicType() == glslang::EbtUint) { |
2591 | 0 | index = createIntWidthConversion(index, 0, builder.makeIntegerType(64, true), glslang::EbtInt64, node->getRight()->getType().getBasicType()); |
2592 | 0 | } |
2593 | | |
2594 | 170 | builder.accessChainPush(index, coherent_flags, |
2595 | 170 | node->getLeft()->getType().getBufferReferenceAlignment()); |
2596 | 170 | } |
2597 | | |
2598 | | // EXT_descriptor_heap |
2599 | | // Record untyped descriptor heap access info. |
2600 | 170 | if (node->getLeft()->getType().isBuiltIn() && |
2601 | 0 | (node->getLeft()->getQualifier().builtIn == glslang::EbvResourceHeapEXT || |
2602 | 0 | node->getLeft()->getQualifier().builtIn == glslang::EbvSamplerHeapEXT)) { |
2603 | 0 | recordDescHeapAccessChainInfo(node); |
2604 | 0 | } |
2605 | 170 | } |
2606 | 170 | return false; |
2607 | 944 | case glslang::EOpVectorSwizzle: |
2608 | 944 | { |
2609 | 944 | node->getLeft()->traverse(this); |
2610 | 944 | std::vector<unsigned> swizzle; |
2611 | 944 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
2612 | 944 | int dummySize; |
2613 | 944 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
2614 | 944 | TranslateCoherent(node->getLeft()->getType()), |
2615 | 944 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
2616 | 944 | dummySize)); |
2617 | 944 | } |
2618 | 944 | return false; |
2619 | 0 | case glslang::EOpMatrixSwizzle: |
2620 | 0 | logger->missingFunctionality("matrix swizzle"); |
2621 | 0 | return true; |
2622 | 0 | case glslang::EOpLogicalOr: |
2623 | 62 | case glslang::EOpLogicalAnd: |
2624 | 62 | { |
2625 | | |
2626 | | // These may require short circuiting, but can sometimes be done as straight |
2627 | | // binary operations. The right operand must be short circuited if it has |
2628 | | // side effects, and should probably be if it is complex. |
2629 | 62 | if (isTrivial(node->getRight()->getAsTyped())) |
2630 | 0 | break; // handle below as a normal binary operation |
2631 | | // otherwise, we need to do dynamic short circuiting on the right operand |
2632 | 62 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
2633 | 62 | *node->getRight()->getAsTyped()); |
2634 | 62 | builder.clearAccessChain(); |
2635 | 62 | builder.setAccessChainRValue(result); |
2636 | 62 | } |
2637 | 0 | return false; |
2638 | 1.39k | default: |
2639 | 1.39k | break; |
2640 | 14.8k | } |
2641 | | |
2642 | | // Assume generic binary op... |
2643 | | |
2644 | | // get right operand |
2645 | 1.39k | builder.clearAccessChain(); |
2646 | 1.39k | node->getLeft()->traverse(this); |
2647 | 1.39k | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
2648 | | |
2649 | | // get left operand |
2650 | 1.39k | builder.clearAccessChain(); |
2651 | 1.39k | node->getRight()->traverse(this); |
2652 | 1.39k | spv::Id right = accessChainLoad(node->getRight()->getType()); |
2653 | | |
2654 | | // get result |
2655 | 1.39k | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
2656 | 1.39k | TranslateNoContractionDecoration(node->getType().getQualifier()), |
2657 | 1.39k | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
2658 | 1.39k | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
2659 | 1.39k | convertGlslangToSpvType(node->getType()), left, right, |
2660 | 1.39k | node->getLeft()->getType().getBasicType()); |
2661 | | |
2662 | 1.39k | builder.clearAccessChain(); |
2663 | 1.39k | if (! result) { |
2664 | 0 | logger->missingFunctionality("unknown glslang binary operation"); |
2665 | 0 | return true; // pick up a child as the place-holder result |
2666 | 1.39k | } else { |
2667 | 1.39k | builder.setAccessChainRValue(result); |
2668 | 1.39k | return false; |
2669 | 1.39k | } |
2670 | 1.39k | } |
2671 | | |
2672 | | spv::Id TGlslangToSpvTraverser::convertLoadedBoolInUniformToUint(const glslang::TType& type, |
2673 | | spv::Id nominalTypeId, |
2674 | | spv::Id loadedId) |
2675 | 1.18k | { |
2676 | 1.18k | if (builder.isScalarType(nominalTypeId)) { |
2677 | | // Conversion for bool |
2678 | 1.02k | spv::Id boolType = builder.makeBoolType(); |
2679 | 1.02k | if (nominalTypeId != boolType) |
2680 | 9 | return builder.createBinOp(spv::Op::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
2681 | 1.02k | } else if (builder.isVectorType(nominalTypeId)) { |
2682 | | // Conversion for bvec |
2683 | 152 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
2684 | 152 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
2685 | 152 | if (nominalTypeId != bvecType) |
2686 | 0 | loadedId = builder.createBinOp(spv::Op::OpINotEqual, bvecType, loadedId, |
2687 | 0 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
2688 | 152 | } else if (builder.isArrayType(nominalTypeId)) { |
2689 | | // Conversion for bool array |
2690 | 0 | spv::Id boolArrayTypeId = convertGlslangToSpvType(type); |
2691 | 0 | if (nominalTypeId != boolArrayTypeId) |
2692 | 0 | { |
2693 | | // Use OpCopyLogical from SPIR-V 1.4 if available. |
2694 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) |
2695 | 0 | return builder.createUnaryOp(spv::Op::OpCopyLogical, boolArrayTypeId, loadedId); |
2696 | | |
2697 | 0 | glslang::TType glslangElementType(type, 0); |
2698 | 0 | spv::Id elementNominalTypeId = builder.getContainedTypeId(nominalTypeId); |
2699 | 0 | std::vector<spv::Id> constituents; |
2700 | 0 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
2701 | | // get the element |
2702 | 0 | spv::Id elementValue = builder.createCompositeExtract(loadedId, elementNominalTypeId, index); |
2703 | | |
2704 | | // recursively convert it |
2705 | 0 | spv::Id elementConvertedValue = convertLoadedBoolInUniformToUint(glslangElementType, elementNominalTypeId, elementValue); |
2706 | 0 | constituents.push_back(elementConvertedValue); |
2707 | 0 | } |
2708 | 0 | return builder.createCompositeConstruct(boolArrayTypeId, constituents); |
2709 | 0 | } |
2710 | 0 | } |
2711 | | |
2712 | 1.17k | return loadedId; |
2713 | 1.18k | } |
2714 | | |
2715 | | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
2716 | | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
2717 | | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
2718 | | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
2719 | | const glslang::TType& glslangType) |
2720 | 4.67k | { |
2721 | 4.67k | switch(glslangBuiltIn) |
2722 | 4.67k | { |
2723 | 0 | case glslang::EbvSubGroupEqMask: |
2724 | 0 | case glslang::EbvSubGroupGeMask: |
2725 | 0 | case glslang::EbvSubGroupGtMask: |
2726 | 0 | case glslang::EbvSubGroupLeMask: |
2727 | 0 | case glslang::EbvSubGroupLtMask: { |
2728 | | // these require changing a 64-bit scaler -> a vector of 32-bit components |
2729 | 0 | if (glslangType.isVector()) |
2730 | 0 | break; |
2731 | 0 | spv::Id ivec4_type = builder.makeVectorType(builder.makeUintType(32), 4); |
2732 | 0 | spv::Id uint64_type = builder.makeUintType(64); |
2733 | 0 | std::pair<spv::Id, spv::Id> ret(ivec4_type, uint64_type); |
2734 | 0 | return ret; |
2735 | 0 | } |
2736 | | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
2737 | | // builtins. During visitBinary we insert a transpose |
2738 | 0 | case glslang::EbvWorldToObject3x4: |
2739 | 0 | case glslang::EbvObjectToWorld3x4: { |
2740 | 0 | spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
2741 | 0 | spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4); |
2742 | 0 | std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
2743 | 0 | return ret; |
2744 | 0 | } |
2745 | 4.67k | default: |
2746 | 4.67k | break; |
2747 | 4.67k | } |
2748 | | |
2749 | 4.67k | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
2750 | 4.67k | return ret; |
2751 | 4.67k | } |
2752 | | |
2753 | | // For an object previously identified (see getForcedType() and forceType) |
2754 | | // as needing type translations, do the translation needed for a load, turning |
2755 | | // an L-value into in R-value. |
2756 | | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
2757 | 3.48k | { |
2758 | 3.48k | const auto forceIt = forceType.find(object); |
2759 | 3.48k | if (forceIt == forceType.end()) |
2760 | 3.48k | return object; |
2761 | | |
2762 | 0 | spv::Id desiredTypeId = forceIt->second; |
2763 | 0 | spv::Id objectTypeId = builder.getTypeId(object); |
2764 | 0 | assert(builder.isPointerType(objectTypeId)); |
2765 | 0 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
2766 | 0 | if (builder.isVectorType(objectTypeId) && |
2767 | 0 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
2768 | 0 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
2769 | | // handle 32-bit v.xy* -> 64-bit |
2770 | 0 | builder.clearAccessChain(); |
2771 | 0 | builder.setAccessChainLValue(object); |
2772 | 0 | object = builder.accessChainLoad(spv::NoPrecision, spv::Decoration::Max, spv::Decoration::Max, objectTypeId); |
2773 | 0 | std::vector<spv::Id> components; |
2774 | 0 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
2775 | 0 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
2776 | |
|
2777 | 0 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
2778 | 0 | return builder.createUnaryOp(spv::Op::OpBitcast, desiredTypeId, |
2779 | 0 | builder.createCompositeConstruct(vecType, components)); |
2780 | 0 | } else { |
2781 | 0 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
2782 | 0 | } |
2783 | 0 | } else if (builder.isMatrixType(objectTypeId)) { |
2784 | | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
2785 | | // and we insert a transpose after loading the original non-transposed builtins |
2786 | 0 | builder.clearAccessChain(); |
2787 | 0 | builder.setAccessChainLValue(object); |
2788 | 0 | object = builder.accessChainLoad(spv::NoPrecision, spv::Decoration::Max, spv::Decoration::Max, objectTypeId); |
2789 | 0 | return builder.createUnaryOp(spv::Op::OpTranspose, desiredTypeId, object); |
2790 | |
|
2791 | 0 | } else { |
2792 | 0 | logger->missingFunctionality("forcing non 32-bit vector type"); |
2793 | 0 | } |
2794 | | |
2795 | 0 | return object; |
2796 | 0 | } |
2797 | | |
2798 | | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
2799 | 2.95k | { |
2800 | 2.95k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
2801 | | |
2802 | 2.95k | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
2803 | 2.95k | if (node->getType().getQualifier().isSpecConstant()) |
2804 | 336 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2805 | | |
2806 | 2.95k | spv::Id result = spv::NoResult; |
2807 | | |
2808 | | // try texturing first |
2809 | 2.95k | result = createImageTextureFunctionCall(node); |
2810 | 2.95k | if (result != spv::NoResult) { |
2811 | 63 | builder.clearAccessChain(); |
2812 | 63 | builder.setAccessChainRValue(result); |
2813 | | |
2814 | 63 | return false; // done with this node |
2815 | 63 | } |
2816 | | |
2817 | | // Non-texturing. |
2818 | | |
2819 | 2.89k | if (node->getOp() == glslang::EOpArrayLength) { |
2820 | | // Quite special; won't want to evaluate the operand. |
2821 | | |
2822 | | // Currently, the front-end does not allow .length() on an array until it is sized, |
2823 | | // except for the last block membeor of an SSBO. |
2824 | | // TODO: If this changes, link-time sized arrays might show up here, and need their |
2825 | | // size extracted. |
2826 | | |
2827 | | // Normal .length() would have been constant folded by the front-end. |
2828 | | // So, this has to be block.lastMember.length(). |
2829 | | // SPV wants "block" and member number as the operands, go get them. |
2830 | |
|
2831 | 0 | uint32_t bits = node->getType().contains64BitInt() ? 64 : 32; |
2832 | |
|
2833 | 0 | spv::Id length; |
2834 | 0 | if (node->getOperand()->getType().isCoopMat()) { |
2835 | 0 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
2836 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
2837 | |
|
2838 | 0 | if (node->getOperand()->getType().isCoopMatKHR()) { |
2839 | 0 | length = builder.createCooperativeMatrixLengthKHR(typeId); |
2840 | 0 | } else { |
2841 | 0 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
2842 | 0 | length = builder.createCooperativeMatrixLengthNV(typeId); |
2843 | 0 | } |
2844 | 0 | } else if (node->getOperand()->getType().isCoopVecOrLongVector() && |
2845 | 0 | !node->getOperand()->getType().isArray()) { |
2846 | | // Long/cooperative vectors support v.length() as a compile-time |
2847 | | // component count. For arrays of such vectors, .length() must use |
2848 | | // OpArrayLength and not the vector-component path. |
2849 | 0 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
2850 | 0 | if (builder.isCooperativeVectorType(typeId)) { |
2851 | 0 | length = builder.getCooperativeVectorNumComponents(typeId); |
2852 | 0 | } else { |
2853 | 0 | length = builder.makeIntConstant(builder.getNumTypeConstituents(typeId)); |
2854 | 0 | } |
2855 | 0 | } else { |
2856 | 0 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
2857 | 0 | block->traverse(this); |
2858 | 0 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
2859 | 0 | ->getConstArray()[0].getUConst(); |
2860 | 0 | length = builder.createArrayLength(builder.accessChainGetLValue(), member, bits); |
2861 | 0 | } |
2862 | | |
2863 | | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
2864 | | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
2865 | | // AST expectation of a signed result. |
2866 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
2867 | 0 | if (builder.isInSpecConstCodeGenMode()) { |
2868 | 0 | length = builder.createBinOp(spv::Op::OpIAdd, builder.makeIntType(bits), length, builder.makeIntConstant(0)); |
2869 | 0 | } else { |
2870 | 0 | length = builder.createUnaryOp(spv::Op::OpBitcast, builder.makeIntType(bits), length); |
2871 | 0 | } |
2872 | 0 | } |
2873 | |
|
2874 | 0 | builder.clearAccessChain(); |
2875 | 0 | builder.setAccessChainRValue(length); |
2876 | |
|
2877 | 0 | return false; |
2878 | 0 | } |
2879 | | |
2880 | | // Force variable declaration - Debug Mode Only |
2881 | 2.89k | if (node->getOp() == glslang::EOpDeclare) { |
2882 | 0 | builder.clearAccessChain(); |
2883 | 0 | node->getOperand()->traverse(this); |
2884 | 0 | builder.clearAccessChain(); |
2885 | 0 | return false; |
2886 | 0 | } |
2887 | | |
2888 | | // Start by evaluating the operand |
2889 | | |
2890 | | // Does it need a swizzle inversion? If so, evaluation is inverted; |
2891 | | // operate first on the swizzle base, then apply the swizzle. |
2892 | 2.89k | spv::Id invertedType = spv::NoType; |
2893 | 3.52k | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
2894 | 3.52k | invertedType : convertGlslangToSpvType(node->getType()); }; |
2895 | 2.89k | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
2896 | 0 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
2897 | | |
2898 | 2.89k | builder.clearAccessChain(); |
2899 | 2.89k | TIntermNode *operandNode; |
2900 | 2.89k | if (invertedType != spv::NoType) |
2901 | 0 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
2902 | 2.89k | else |
2903 | 2.89k | operandNode = node->getOperand(); |
2904 | | |
2905 | 2.89k | operandNode->traverse(this); |
2906 | | |
2907 | 2.89k | spv::Id operand = spv::NoResult; |
2908 | | |
2909 | 2.89k | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
2910 | | |
2911 | 2.89k | const auto hitObjectOpsWithLvalue = [](glslang::TOperator op) { |
2912 | 2.80k | switch(op) { |
2913 | 0 | case glslang::EOpReorderThreadNV: |
2914 | 0 | case glslang::EOpHitObjectGetCurrentTimeNV: |
2915 | 0 | case glslang::EOpHitObjectGetHitKindNV: |
2916 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
2917 | 0 | case glslang::EOpHitObjectGetGeometryIndexNV: |
2918 | 0 | case glslang::EOpHitObjectGetInstanceIdNV: |
2919 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
2920 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
2921 | 0 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
2922 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
2923 | 0 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
2924 | 0 | case glslang::EOpHitObjectGetWorldToObjectNV: |
2925 | 0 | case glslang::EOpHitObjectGetObjectToWorldNV: |
2926 | 0 | case glslang::EOpHitObjectGetRayTMaxNV: |
2927 | 0 | case glslang::EOpHitObjectGetRayTMinNV: |
2928 | 0 | case glslang::EOpHitObjectIsEmptyNV: |
2929 | 0 | case glslang::EOpHitObjectIsHitNV: |
2930 | 0 | case glslang::EOpHitObjectIsMissNV: |
2931 | 0 | case glslang::EOpHitObjectRecordEmptyNV: |
2932 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
2933 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
2934 | 0 | case glslang::EOpHitObjectGetClusterIdNV: |
2935 | 0 | case glslang::EOpHitObjectGetSpherePositionNV: |
2936 | 0 | case glslang::EOpHitObjectGetSphereRadiusNV: |
2937 | 0 | case glslang::EOpHitObjectIsSphereHitNV: |
2938 | 0 | case glslang::EOpHitObjectIsLSSHitNV: |
2939 | 0 | case glslang::EOpReorderThreadEXT: |
2940 | 0 | case glslang::EOpHitObjectGetCurrentTimeEXT: |
2941 | 0 | case glslang::EOpHitObjectGetHitKindEXT: |
2942 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexEXT: |
2943 | 0 | case glslang::EOpHitObjectGetGeometryIndexEXT: |
2944 | 0 | case glslang::EOpHitObjectGetInstanceIdEXT: |
2945 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexEXT: |
2946 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionEXT: |
2947 | 0 | case glslang::EOpHitObjectGetObjectRayOriginEXT: |
2948 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionEXT: |
2949 | 0 | case glslang::EOpHitObjectGetWorldRayOriginEXT: |
2950 | 0 | case glslang::EOpHitObjectGetWorldToObjectEXT: |
2951 | 0 | case glslang::EOpHitObjectGetObjectToWorldEXT: |
2952 | 0 | case glslang::EOpHitObjectGetRayTMaxEXT: |
2953 | 0 | case glslang::EOpHitObjectGetRayTMinEXT: |
2954 | 0 | case glslang::EOpHitObjectGetRayFlagsEXT: |
2955 | 0 | case glslang::EOpHitObjectIsEmptyEXT: |
2956 | 0 | case glslang::EOpHitObjectIsHitEXT: |
2957 | 0 | case glslang::EOpHitObjectIsMissEXT: |
2958 | 0 | case glslang::EOpHitObjectRecordEmptyEXT: |
2959 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexEXT: |
2960 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleEXT: |
2961 | 0 | return true; |
2962 | 2.80k | default: |
2963 | 2.80k | return false; |
2964 | 2.80k | } |
2965 | 2.80k | }; |
2966 | | |
2967 | 2.89k | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
2968 | 2.89k | node->getOp() == glslang::EOpAtomicCounterDecrement || |
2969 | 2.89k | node->getOp() == glslang::EOpAtomicCounter || |
2970 | 2.89k | (node->getOp() == glslang::EOpInterpolateAtCentroid && |
2971 | 0 | glslangIntermediate->getSource() != glslang::EShSourceHlsl) || |
2972 | 2.89k | node->getOp() == glslang::EOpRayQueryProceed || |
2973 | 2.88k | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
2974 | 2.87k | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
2975 | 2.86k | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
2976 | 2.85k | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
2977 | 2.84k | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
2978 | 2.83k | node->getOp() == glslang::EOpRayQueryTerminate || |
2979 | 2.81k | node->getOp() == glslang::EOpRayQueryConfirmIntersection || |
2980 | 2.80k | (node->getOp() == glslang::EOpSpirvInst && operandNode->getAsTyped()->getQualifier().isSpirvByReference()) || |
2981 | 2.80k | hitObjectOpsWithLvalue(node->getOp())) { |
2982 | 90 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
2983 | 90 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
2984 | 90 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
2985 | 2.80k | } else if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
2986 | | // Will be translated to a literal value, make a placeholder here |
2987 | 0 | operand = spv::NoResult; |
2988 | 2.80k | } else { |
2989 | 2.80k | operand = accessChainLoad(node->getOperand()->getType()); |
2990 | 2.80k | } |
2991 | | |
2992 | 2.89k | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
2993 | 2.89k | TranslateNoContractionDecoration(node->getType().getQualifier()), |
2994 | 2.89k | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
2995 | | |
2996 | | // it could be a conversion |
2997 | 2.89k | if (! result) { |
2998 | 2.89k | result = createConversion(node->getOp(), decorations, resultType(), operand, |
2999 | 2.89k | node->getType().getBasicType(), node->getOperand()->getBasicType()); |
3000 | 2.89k | if (result) { |
3001 | 2.25k | if (node->getType().isCoopMatKHR() && node->getOperand()->getAsTyped()->getType().isCoopMatKHR() && |
3002 | 0 | !node->getAsTyped()->getType().sameCoopMatUse(node->getOperand()->getAsTyped()->getType())) { |
3003 | | // Conversions that change use need CapabilityCooperativeMatrixConversionsNV |
3004 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionsNV); |
3005 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
3006 | 0 | } |
3007 | 2.25k | } |
3008 | 2.89k | } |
3009 | | |
3010 | | // if not, then possibly an operation |
3011 | 2.89k | if (! result) |
3012 | 634 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
3013 | 634 | node->getOperand()->getBasicType(), lvalueCoherentFlags, node->getType()); |
3014 | | |
3015 | | // it could be attached to a SPIR-V intruction |
3016 | 2.89k | if (!result) { |
3017 | 220 | if (node->getOp() == glslang::EOpSpirvInst) { |
3018 | 0 | const auto& spirvInst = node->getSpirvInstruction(); |
3019 | 0 | if (spirvInst.set == "") { |
3020 | 0 | spv::IdImmediate idImmOp = {true, operand}; |
3021 | 0 | if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
3022 | | // Translate the constant to a literal value |
3023 | 0 | std::vector<unsigned> literals; |
3024 | 0 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
3025 | 0 | constants.push_back(operandNode->getAsConstantUnion()); |
3026 | 0 | TranslateLiterals(constants, literals); |
3027 | 0 | idImmOp = {false, literals[0]}; |
3028 | 0 | } |
3029 | |
|
3030 | 0 | if (node->getBasicType() == glslang::EbtVoid) |
3031 | 0 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), {idImmOp}); |
3032 | 0 | else |
3033 | 0 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), resultType(), {idImmOp}); |
3034 | 0 | } else { |
3035 | 0 | result = builder.createBuiltinCall( |
3036 | 0 | resultType(), spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(spirvInst.set.c_str()), |
3037 | 0 | spirvInst.id, {operand}); |
3038 | 0 | } |
3039 | |
|
3040 | 0 | if (node->getBasicType() == glslang::EbtVoid) |
3041 | 0 | return false; // done with this node |
3042 | 0 | } |
3043 | 220 | } |
3044 | | |
3045 | 2.89k | if (result) { |
3046 | 2.67k | if (invertedType) { |
3047 | 0 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
3048 | 0 | decorations.addNonUniform(builder, result); |
3049 | 0 | } |
3050 | | |
3051 | 2.67k | builder.clearAccessChain(); |
3052 | 2.67k | builder.setAccessChainRValue(result); |
3053 | | |
3054 | 2.67k | return false; // done with this node |
3055 | 2.67k | } |
3056 | | |
3057 | | // it must be a special case, check... |
3058 | 220 | switch (node->getOp()) { |
3059 | 53 | case glslang::EOpPostIncrement: |
3060 | 62 | case glslang::EOpPostDecrement: |
3061 | 177 | case glslang::EOpPreIncrement: |
3062 | 190 | case glslang::EOpPreDecrement: |
3063 | 190 | { |
3064 | | // we need the integer value "1" or the floating point "1.0" to add/subtract |
3065 | 190 | spv::Id one = 0; |
3066 | 190 | if (node->getBasicType() == glslang::EbtFloat) |
3067 | 10 | one = builder.makeFloatConstant(1.0F); |
3068 | 180 | else if (node->getBasicType() == glslang::EbtDouble) |
3069 | 0 | one = builder.makeDoubleConstant(1.0); |
3070 | 180 | else if (node->getBasicType() == glslang::EbtFloat16) |
3071 | 0 | one = builder.makeFloat16Constant(1.0F); |
3072 | 180 | else if (node->getBasicType() == glslang::EbtBFloat16) |
3073 | 0 | one = builder.makeBFloat16Constant(1.0F); |
3074 | 180 | else if (node->getBasicType() == glslang::EbtFloatE5M2) |
3075 | 0 | one = builder.makeFloatE5M2Constant(1.0F); |
3076 | 180 | else if (node->getBasicType() == glslang::EbtFloatE4M3) |
3077 | 0 | one = builder.makeFloatE4M3Constant(1.0F); |
3078 | 180 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
3079 | 0 | one = builder.makeInt8Constant(1); |
3080 | 180 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
3081 | 35 | one = builder.makeInt16Constant(1); |
3082 | 145 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
3083 | 0 | one = builder.makeInt64Constant(1); |
3084 | 145 | else |
3085 | 145 | one = builder.makeIntConstant(1); |
3086 | 190 | glslang::TOperator op; |
3087 | 190 | if (node->getOp() == glslang::EOpPreIncrement || |
3088 | 75 | node->getOp() == glslang::EOpPostIncrement) |
3089 | 168 | op = glslang::EOpAdd; |
3090 | 22 | else |
3091 | 22 | op = glslang::EOpSub; |
3092 | | |
3093 | 190 | spv::Id result = createBinaryOperation(op, decorations, |
3094 | 190 | convertGlslangToSpvType(node->getType()), operand, one, |
3095 | 190 | node->getType().getBasicType()); |
3096 | 190 | assert(result != spv::NoResult); |
3097 | | |
3098 | | // The result of operation is always stored, but conditionally the |
3099 | | // consumed result. The consumed result is always an r-value. |
3100 | 190 | builder.accessChainStore(result, |
3101 | 190 | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags)); |
3102 | 190 | builder.clearAccessChain(); |
3103 | 190 | if (node->getOp() == glslang::EOpPreIncrement || |
3104 | 75 | node->getOp() == glslang::EOpPreDecrement) |
3105 | 128 | builder.setAccessChainRValue(result); |
3106 | 62 | else |
3107 | 62 | builder.setAccessChainRValue(operand); |
3108 | 190 | } |
3109 | | |
3110 | 190 | return false; |
3111 | | |
3112 | 0 | case glslang::EOpAssumeEXT: |
3113 | 0 | builder.addCapability(spv::Capability::ExpectAssumeKHR); |
3114 | 0 | builder.addExtension(spv::E_SPV_KHR_expect_assume); |
3115 | 0 | builder.createNoResultOp(spv::Op::OpAssumeTrueKHR, operand); |
3116 | 0 | return false; |
3117 | 0 | case glslang::EOpEmitStreamVertex: |
3118 | 0 | builder.createNoResultOp(spv::Op::OpEmitStreamVertex, operand); |
3119 | 0 | return false; |
3120 | 0 | case glslang::EOpEndStreamPrimitive: |
3121 | 0 | builder.createNoResultOp(spv::Op::OpEndStreamPrimitive, operand); |
3122 | 0 | return false; |
3123 | 20 | case glslang::EOpRayQueryTerminate: |
3124 | 20 | builder.createNoResultOp(spv::Op::OpRayQueryTerminateKHR, operand); |
3125 | 20 | return false; |
3126 | 10 | case glslang::EOpRayQueryConfirmIntersection: |
3127 | 10 | builder.createNoResultOp(spv::Op::OpRayQueryConfirmIntersectionKHR, operand); |
3128 | 10 | return false; |
3129 | 0 | case glslang::EOpReorderThreadNV: |
3130 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHitObjectNV, operand); |
3131 | 0 | return false; |
3132 | 0 | case glslang::EOpReorderThreadEXT: |
3133 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHitObjectEXT, operand); |
3134 | 0 | return false; |
3135 | 0 | case glslang::EOpHitObjectRecordEmptyNV: |
3136 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordEmptyNV, operand); |
3137 | 0 | return false; |
3138 | 0 | case glslang::EOpHitObjectRecordEmptyEXT: |
3139 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordEmptyEXT, operand); |
3140 | 0 | return false; |
3141 | | |
3142 | 0 | case glslang::EOpCreateTensorLayoutNV: |
3143 | 0 | result = builder.createOp(spv::Op::OpCreateTensorLayoutNV, resultType(), std::vector<spv::Id>{}); |
3144 | 0 | builder.clearAccessChain(); |
3145 | 0 | builder.setAccessChainRValue(result); |
3146 | 0 | return false; |
3147 | | |
3148 | 0 | case glslang::EOpCreateTensorViewNV: |
3149 | 0 | result = builder.createOp(spv::Op::OpCreateTensorViewNV, resultType(), std::vector<spv::Id>{}); |
3150 | 0 | builder.clearAccessChain(); |
3151 | 0 | builder.setAccessChainRValue(result); |
3152 | 0 | return false; |
3153 | | |
3154 | 0 | default: |
3155 | 0 | logger->missingFunctionality("unknown glslang unary"); |
3156 | 0 | return true; // pick up operand as placeholder result |
3157 | 220 | } |
3158 | 220 | } |
3159 | | |
3160 | | // Construct a composite object, recursively copying members if their types don't match |
3161 | | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
3162 | 9 | { |
3163 | 27 | for (int c = 0; c < (int)constituents.size(); ++c) { |
3164 | 18 | spv::Id& constituent = constituents[c]; |
3165 | 18 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
3166 | 18 | spv::Id rType = builder.getTypeId(constituent); |
3167 | 18 | if (lType != rType) { |
3168 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
3169 | 0 | constituent = builder.createUnaryOp(spv::Op::OpCopyLogical, lType, constituent); |
3170 | 0 | } else if (builder.isStructType(rType)) { |
3171 | 0 | std::vector<spv::Id> rTypeConstituents; |
3172 | 0 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
3173 | 0 | for (int i = 0; i < numrTypeConstituents; ++i) { |
3174 | 0 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
3175 | 0 | builder.getContainedTypeId(rType, i), i)); |
3176 | 0 | } |
3177 | 0 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
3178 | 0 | } else { |
3179 | 0 | assert(builder.isArrayType(rType)); |
3180 | 0 | std::vector<spv::Id> rTypeConstituents; |
3181 | 0 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
3182 | |
|
3183 | 0 | spv::Id elementRType = builder.getContainedTypeId(rType); |
3184 | 0 | for (int i = 0; i < numrTypeConstituents; ++i) { |
3185 | 0 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
3186 | 0 | } |
3187 | 0 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
3188 | 0 | } |
3189 | 0 | } |
3190 | 18 | } |
3191 | 9 | return builder.createCompositeConstruct(resultTypeId, constituents); |
3192 | 9 | } |
3193 | | |
3194 | | void TGlslangToSpvTraverser::createAbortEXT(const glslang::TIntermSequence &glslangOperands) |
3195 | 0 | { |
3196 | 0 | bool isEmptyMsg = |
3197 | 0 | glslangOperands.empty() || |
3198 | 0 | glslangOperands[0]->getAsConstantUnion()->getConstArray()[0].getSConst()->empty(); |
3199 | | // Add Capability and extensions. |
3200 | 0 | builder.addCapability(spv::Capability::AbortKHR); |
3201 | 0 | builder.addCapability(spv::Capability::ConstantDataKHR); |
3202 | 0 | builder.addExtension(spv::E_SPV_KHR_constant_data); |
3203 | 0 | builder.addExtension(spv::E_SPV_KHR_abort); |
3204 | |
|
3205 | 0 | const uint32_t formatSpecifiersSize = 4; |
3206 | 0 | const char* formatSpecifiers[formatSpecifiersSize] = {"%d", "%i", "%f", "%u"}; |
3207 | | // 1. Check whether message is empty or has format specifiers. |
3208 | 0 | const auto emptyMsg = glslang::TString("\0"); |
3209 | 0 | bool hasSpecifier = false; |
3210 | 0 | const glslang::TString* msg = |
3211 | 0 | isEmptyMsg ? &emptyMsg : glslangOperands[0]->getAsConstantUnion()->getConstArray()[0].getSConst(); |
3212 | 0 | if (!isEmptyMsg) { |
3213 | 0 | for (uint32_t i = 0; i < formatSpecifiersSize; i++) { |
3214 | 0 | if (!msg->empty() && msg->find(formatSpecifiers[i]) != std::string::npos) { |
3215 | 0 | hasSpecifier = true; |
3216 | 0 | break; |
3217 | 0 | } |
3218 | 0 | } |
3219 | 0 | } |
3220 | | // 2. Prepare to construct message struct variable, record members' types, data and offsets. |
3221 | 0 | std::vector<int> structMemberOffsets; |
3222 | 0 | std::vector<spv::Id> structMemberType; |
3223 | 0 | std::vector<spv::Id> structLoadMemberType; |
3224 | 0 | std::vector<spv::Id> structMemberData; |
3225 | 0 | structMemberOffsets.push_back(0); |
3226 | 0 | auto charType = builder.makeIntType(8); |
3227 | | // 2.1 Get string's length (if has specifier, be spec const). |
3228 | | // If not an empty string, \0 is the final character used for padding. |
3229 | 0 | unsigned int msgLen = isEmptyMsg ? 1 : msg->size() + 1; |
3230 | 0 | unsigned int paddingSize = (4 - msgLen % 4) % 4; |
3231 | 0 | msgLen = msgLen + paddingSize; |
3232 | 0 | spv::Id constLen = builder.makeUintConstant(msgLen); |
3233 | 0 | spv::Op constDataOp = spv::Op::OpConstantDataKHR; |
3234 | 0 | if (hasSpecifier) { |
3235 | 0 | constLen = builder.makeUintConstant(msgLen, true); |
3236 | 0 | constDataOp = spv::Op::OpSpecConstantDataKHR; |
3237 | 0 | } |
3238 | | // 2.2 Get string's array type (if specifier, be spec const). |
3239 | 0 | auto msgArrType = builder.makeArrayType(charType, constLen, 1); |
3240 | 0 | auto msgLoadArrType = builder.makeArrayType(charType, constLen, 1); |
3241 | | // 2.3 Add string constant data |
3242 | 0 | auto msgConstData = builder.createConstData(constDataOp, msgArrType, {msg->c_str()}); |
3243 | | // 2.4 Add decoration for this string. |
3244 | 0 | builder.addDecoration(msgArrType, spv::Decoration::UTFEncodedKHR); |
3245 | 0 | builder.addDecoration(msgLoadArrType, spv::Decoration::UTFEncodedKHR); |
3246 | | // 2.5 Collect data and type for construct an internal message structure member. |
3247 | 0 | structMemberType.push_back(msgArrType); |
3248 | 0 | structLoadMemberType.push_back(msgLoadArrType); |
3249 | 0 | structMemberOffsets.push_back(msgLen); |
3250 | 0 | structMemberData.push_back(msgConstData); |
3251 | | // 3. Add extra following arguments/variables' types in member structure. |
3252 | 0 | for (unsigned int i = 1; i < glslangOperands.size(); i++) { |
3253 | 0 | spv::Builder::AccessChain save = builder.getAccessChain(); |
3254 | 0 | builder.clearAccessChain(); |
3255 | 0 | auto width = GetNumBits(glslangOperands[i]->getAsTyped()->getBasicType()); |
3256 | 0 | structMemberOffsets.push_back(structMemberOffsets.back() + width / 8); |
3257 | 0 | glslangOperands[i]->traverse(this); |
3258 | 0 | structMemberData.push_back(accessChainLoad(glslangOperands[i]->getAsTyped()->getType())); |
3259 | 0 | spv::Id reservedOpType = builder.getTypeId(structMemberData.back()); |
3260 | 0 | structMemberType.push_back(reservedOpType); |
3261 | 0 | structLoadMemberType.push_back(reservedOpType); |
3262 | |
|
3263 | 0 | builder.setAccessChain(save); |
3264 | 0 | } |
3265 | 0 | structMemberOffsets.pop_back(); |
3266 | | // 4. Construct struct message variable, add abortExt instruction. |
3267 | 0 | auto structLoadType = builder.makeStructType(structLoadMemberType, {}, "abortMessageLoadType"); |
3268 | 0 | for (unsigned int i = 0; i < structMemberOffsets.size(); i++) |
3269 | 0 | builder.addMemberDecoration(structLoadType, i, spv::Decoration::Offset, structMemberOffsets[i]); |
3270 | 0 | auto structType = builder.makeStructType(structMemberType, {}, "abortMessage"); |
3271 | 0 | auto messageVar = builder.createCompositeConstruct(structType, structMemberData); |
3272 | 0 | builder.makeStatementTerminator(spv::Op::OpAbortKHR, {structLoadType, messageVar}, "post-abort"); |
3273 | 0 | } |
3274 | | |
3275 | | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
3276 | 14.8k | { |
3277 | 14.8k | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
3278 | 14.8k | if (node->getType().getQualifier().isSpecConstant()) |
3279 | 0 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
3280 | | |
3281 | 14.8k | spv::Id result = spv::NoResult; |
3282 | 14.8k | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
3283 | 14.8k | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
3284 | | // SPIR-V, for an out parameter |
3285 | 14.8k | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
3286 | 14.8k | spv::Builder::AccessChain tensorReadResultLValue = {}; |
3287 | 14.8k | tensorReadResultLValue.base = spv::NoResult; // deferred tensorReadARM out-arg store target |
3288 | | |
3289 | 14.8k | auto resultType = [&invertedType, &node, this](){ |
3290 | 2.69k | if (invertedType != spv::NoType) { |
3291 | 0 | return invertedType; |
3292 | 2.69k | } else { |
3293 | 2.69k | auto ret = convertGlslangToSpvType(node->getType()); |
3294 | | // convertGlslangToSpvType may clobber the debug location, reset it |
3295 | 2.69k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
3296 | 2.69k | return ret; |
3297 | 2.69k | } |
3298 | 2.69k | }; |
3299 | | |
3300 | | // try texturing |
3301 | 14.8k | result = createImageTextureFunctionCall(node); |
3302 | 14.8k | if (result != spv::NoResult) { |
3303 | 3.04k | builder.clearAccessChain(); |
3304 | 3.04k | builder.setAccessChainRValue(result); |
3305 | | |
3306 | 3.04k | return false; |
3307 | 11.7k | } else if (node->getOp() == glslang::EOpImageStore || |
3308 | 11.6k | node->getOp() == glslang::EOpImageStoreLod || |
3309 | 11.6k | node->getOp() == glslang::EOpImageAtomicStore) { |
3310 | | // "imageStore" is a special case, which has no result |
3311 | 139 | return false; |
3312 | 139 | } |
3313 | | |
3314 | 11.6k | glslang::TOperator binOp = glslang::EOpNull; |
3315 | 11.6k | bool reduceComparison = true; |
3316 | 11.6k | bool isMatrix = false; |
3317 | 11.6k | bool noReturnValue = false; |
3318 | 11.6k | bool atomic = false; |
3319 | | |
3320 | 11.6k | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
3321 | | |
3322 | 11.6k | assert(node->getOp()); |
3323 | | |
3324 | 11.6k | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
3325 | | |
3326 | 11.6k | switch (node->getOp()) { |
3327 | 0 | case glslang::EOpScope: |
3328 | 5.34k | case glslang::EOpSequence: |
3329 | 5.34k | { |
3330 | 5.34k | if (visit == glslang::EvPreVisit) { |
3331 | 2.85k | ++sequenceDepth; |
3332 | 2.85k | if (sequenceDepth == 1) { |
3333 | | // If this is the parent node of all the functions, we want to see them |
3334 | | // early, so all call points have actual SPIR-V functions to reference. |
3335 | | // In all cases, still let the traverser visit the children for us. |
3336 | 370 | makeFunctions(node->getAsAggregate()->getSequence()); |
3337 | | |
3338 | | // Global initializers is specific to the shader entry point, which does not exist in compile-only mode |
3339 | 370 | if (!options.compileOnly) { |
3340 | | // Also, we want all globals initializers to go into the beginning of the entry point, before |
3341 | | // anything else gets there, so visit out of order, doing them all now. |
3342 | 370 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
3343 | 370 | } |
3344 | | |
3345 | | //Pre process linker objects for ray tracing stages |
3346 | 370 | if (glslangIntermediate->isRayTracingStage()) |
3347 | 0 | collectRayTracingLinkerObjects(); |
3348 | | |
3349 | | // Initializers are done, don't want to visit again, but functions and link objects need to be processed, |
3350 | | // so do them manually. |
3351 | 370 | visitFunctions(node->getAsAggregate()->getSequence()); |
3352 | | |
3353 | 370 | return false; |
3354 | 2.48k | } else { |
3355 | 2.48k | if (node->getOp() == glslang::EOpScope) { |
3356 | 0 | auto loc = node->getLoc(); |
3357 | 0 | builder.enterLexicalBlock(loc.line, loc.column); |
3358 | 0 | } |
3359 | 2.48k | } |
3360 | 2.85k | } else { |
3361 | 2.48k | if (sequenceDepth > 1 && node->getOp() == glslang::EOpScope) |
3362 | 0 | builder.leaveLexicalBlock(); |
3363 | 2.48k | --sequenceDepth; |
3364 | 2.48k | } |
3365 | | |
3366 | 4.97k | return true; |
3367 | 5.34k | } |
3368 | 740 | case glslang::EOpLinkerObjects: |
3369 | 740 | { |
3370 | 740 | if (visit == glslang::EvPreVisit) |
3371 | 370 | linkageOnly = true; |
3372 | 370 | else |
3373 | 370 | linkageOnly = false; |
3374 | | |
3375 | 740 | return true; |
3376 | 5.34k | } |
3377 | 45 | case glslang::EOpComma: |
3378 | 45 | { |
3379 | | // processing from left to right naturally leaves the right-most |
3380 | | // lying around in the access chain |
3381 | 45 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
3382 | 135 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
3383 | 90 | glslangOperands[i]->traverse(this); |
3384 | | |
3385 | 45 | return false; |
3386 | 5.34k | } |
3387 | 1.45k | case glslang::EOpFunction: |
3388 | 1.45k | if (visit == glslang::EvPreVisit) { |
3389 | 725 | if (options.generateDebugInfo) { |
3390 | 0 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
3391 | 0 | } |
3392 | 725 | if (isShaderEntryPoint(node)) { |
3393 | 370 | inEntryPoint = true; |
3394 | 370 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
3395 | 370 | builder.enterFunction(shaderEntry); |
3396 | 370 | currentFunction = shaderEntry; |
3397 | 370 | } else { |
3398 | | // SPIR-V functions should already be in the functionMap from the prepass |
3399 | | // that called makeFunctions(). |
3400 | 355 | currentFunction = functionMap[node->getName().c_str()]; |
3401 | 355 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
3402 | 355 | builder.setBuildPoint(functionBlock); |
3403 | 355 | builder.enterFunction(currentFunction); |
3404 | 355 | } |
3405 | 725 | if (options.generateDebugInfo && !options.emitNonSemanticShaderDebugInfo) { |
3406 | 0 | const auto& loc = node->getLoc(); |
3407 | 0 | const char* sourceFileName = loc.getFilename(); |
3408 | 0 | spv::Id sourceFileId = sourceFileName ? builder.getStringId(sourceFileName) : builder.getMainFileId(); |
3409 | 0 | currentFunction->setDebugLineInfo(sourceFileId, loc.line, loc.column); |
3410 | 0 | } |
3411 | 725 | } else { |
3412 | | // Here we have finished visiting the function (post-visit). Finalize it. |
3413 | 725 | if (options.generateDebugInfo) { |
3414 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl && node->getSequence().size() > 1) { |
3415 | 0 | auto endLoc = node->getSequence()[1]->getAsAggregate()->getEndLoc(); |
3416 | 0 | builder.setDebugSourceLocation(endLoc.line, endLoc.getFilename()); |
3417 | 0 | } |
3418 | 0 | } |
3419 | 725 | if (inEntryPoint) |
3420 | 370 | entryPointTerminated = true; |
3421 | 725 | builder.leaveFunction(); |
3422 | 725 | inEntryPoint = false; |
3423 | 725 | currentFunction = nullptr; |
3424 | 725 | } |
3425 | | |
3426 | 1.45k | return true; |
3427 | 725 | case glslang::EOpParameters: |
3428 | | // Parameters will have been consumed by EOpFunction processing, but not |
3429 | | // the body, so we still visited the function node's children, making this |
3430 | | // child redundant. |
3431 | 725 | return false; |
3432 | 631 | case glslang::EOpFunctionCall: |
3433 | 631 | { |
3434 | 631 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
3435 | 631 | if (node->isUserDefined()) |
3436 | 631 | result = handleUserFunctionCall(node); |
3437 | 631 | if (result) { |
3438 | 631 | builder.clearAccessChain(); |
3439 | 631 | builder.setAccessChainRValue(result); |
3440 | 631 | } else |
3441 | 0 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
3442 | | |
3443 | 631 | return false; |
3444 | 5.34k | } |
3445 | 18 | case glslang::EOpConstructMat2x2: |
3446 | 18 | case glslang::EOpConstructMat2x3: |
3447 | 18 | case glslang::EOpConstructMat2x4: |
3448 | 18 | case glslang::EOpConstructMat3x2: |
3449 | 27 | case glslang::EOpConstructMat3x3: |
3450 | 27 | case glslang::EOpConstructMat3x4: |
3451 | 27 | case glslang::EOpConstructMat4x2: |
3452 | 27 | case glslang::EOpConstructMat4x3: |
3453 | 36 | case glslang::EOpConstructMat4x4: |
3454 | 36 | case glslang::EOpConstructDMat2x2: |
3455 | 36 | case glslang::EOpConstructDMat2x3: |
3456 | 36 | case glslang::EOpConstructDMat2x4: |
3457 | 36 | case glslang::EOpConstructDMat3x2: |
3458 | 36 | case glslang::EOpConstructDMat3x3: |
3459 | 36 | case glslang::EOpConstructDMat3x4: |
3460 | 36 | case glslang::EOpConstructDMat4x2: |
3461 | 36 | case glslang::EOpConstructDMat4x3: |
3462 | 36 | case glslang::EOpConstructDMat4x4: |
3463 | 36 | case glslang::EOpConstructIMat2x2: |
3464 | 36 | case glslang::EOpConstructIMat2x3: |
3465 | 36 | case glslang::EOpConstructIMat2x4: |
3466 | 36 | case glslang::EOpConstructIMat3x2: |
3467 | 36 | case glslang::EOpConstructIMat3x3: |
3468 | 36 | case glslang::EOpConstructIMat3x4: |
3469 | 36 | case glslang::EOpConstructIMat4x2: |
3470 | 36 | case glslang::EOpConstructIMat4x3: |
3471 | 36 | case glslang::EOpConstructIMat4x4: |
3472 | 36 | case glslang::EOpConstructUMat2x2: |
3473 | 36 | case glslang::EOpConstructUMat2x3: |
3474 | 36 | case glslang::EOpConstructUMat2x4: |
3475 | 36 | case glslang::EOpConstructUMat3x2: |
3476 | 36 | case glslang::EOpConstructUMat3x3: |
3477 | 36 | case glslang::EOpConstructUMat3x4: |
3478 | 36 | case glslang::EOpConstructUMat4x2: |
3479 | 36 | case glslang::EOpConstructUMat4x3: |
3480 | 36 | case glslang::EOpConstructUMat4x4: |
3481 | 36 | case glslang::EOpConstructBMat2x2: |
3482 | 36 | case glslang::EOpConstructBMat2x3: |
3483 | 36 | case glslang::EOpConstructBMat2x4: |
3484 | 36 | case glslang::EOpConstructBMat3x2: |
3485 | 36 | case glslang::EOpConstructBMat3x3: |
3486 | 36 | case glslang::EOpConstructBMat3x4: |
3487 | 36 | case glslang::EOpConstructBMat4x2: |
3488 | 36 | case glslang::EOpConstructBMat4x3: |
3489 | 36 | case glslang::EOpConstructBMat4x4: |
3490 | 36 | case glslang::EOpConstructF16Mat2x2: |
3491 | 36 | case glslang::EOpConstructF16Mat2x3: |
3492 | 36 | case glslang::EOpConstructF16Mat2x4: |
3493 | 36 | case glslang::EOpConstructF16Mat3x2: |
3494 | 36 | case glslang::EOpConstructF16Mat3x3: |
3495 | 36 | case glslang::EOpConstructF16Mat3x4: |
3496 | 36 | case glslang::EOpConstructF16Mat4x2: |
3497 | 36 | case glslang::EOpConstructF16Mat4x3: |
3498 | 36 | case glslang::EOpConstructF16Mat4x4: |
3499 | 36 | isMatrix = true; |
3500 | 36 | [[fallthrough]]; |
3501 | 45 | case glslang::EOpConstructFloat: |
3502 | 68 | case glslang::EOpConstructVec2: |
3503 | 142 | case glslang::EOpConstructVec3: |
3504 | 188 | case glslang::EOpConstructVec4: |
3505 | 188 | case glslang::EOpConstructDouble: |
3506 | 188 | case glslang::EOpConstructDVec2: |
3507 | 188 | case glslang::EOpConstructDVec3: |
3508 | 188 | case glslang::EOpConstructDVec4: |
3509 | 190 | case glslang::EOpConstructFloat16: |
3510 | 192 | case glslang::EOpConstructF16Vec2: |
3511 | 203 | case glslang::EOpConstructF16Vec3: |
3512 | 203 | case glslang::EOpConstructF16Vec4: |
3513 | 203 | case glslang::EOpConstructBFloat16: |
3514 | 203 | case glslang::EOpConstructBF16Vec2: |
3515 | 203 | case glslang::EOpConstructBF16Vec3: |
3516 | 203 | case glslang::EOpConstructBF16Vec4: |
3517 | 203 | case glslang::EOpConstructFloatE5M2: |
3518 | 203 | case glslang::EOpConstructFloatE5M2Vec2: |
3519 | 203 | case glslang::EOpConstructFloatE5M2Vec3: |
3520 | 203 | case glslang::EOpConstructFloatE5M2Vec4: |
3521 | 203 | case glslang::EOpConstructFloatE4M3: |
3522 | 203 | case glslang::EOpConstructFloatE4M3Vec2: |
3523 | 203 | case glslang::EOpConstructFloatE4M3Vec3: |
3524 | 203 | case glslang::EOpConstructFloatE4M3Vec4: |
3525 | 203 | case glslang::EOpConstructBool: |
3526 | 203 | case glslang::EOpConstructBVec2: |
3527 | 203 | case glslang::EOpConstructBVec3: |
3528 | 203 | case glslang::EOpConstructBVec4: |
3529 | 205 | case glslang::EOpConstructInt8: |
3530 | 207 | case glslang::EOpConstructI8Vec2: |
3531 | 209 | case glslang::EOpConstructI8Vec3: |
3532 | 209 | case glslang::EOpConstructI8Vec4: |
3533 | 211 | case glslang::EOpConstructUint8: |
3534 | 213 | case glslang::EOpConstructU8Vec2: |
3535 | 215 | case glslang::EOpConstructU8Vec3: |
3536 | 215 | case glslang::EOpConstructU8Vec4: |
3537 | 217 | case glslang::EOpConstructInt16: |
3538 | 291 | case glslang::EOpConstructI16Vec2: |
3539 | 293 | case glslang::EOpConstructI16Vec3: |
3540 | 302 | case glslang::EOpConstructI16Vec4: |
3541 | 304 | case glslang::EOpConstructUint16: |
3542 | 306 | case glslang::EOpConstructU16Vec2: |
3543 | 371 | case glslang::EOpConstructU16Vec3: |
3544 | 380 | case glslang::EOpConstructU16Vec4: |
3545 | 380 | case glslang::EOpConstructInt: |
3546 | 380 | case glslang::EOpConstructIVec2: |
3547 | 388 | case glslang::EOpConstructIVec3: |
3548 | 392 | case glslang::EOpConstructIVec4: |
3549 | 392 | case glslang::EOpConstructUint: |
3550 | 392 | case glslang::EOpConstructUVec2: |
3551 | 400 | case glslang::EOpConstructUVec3: |
3552 | 404 | case glslang::EOpConstructUVec4: |
3553 | 404 | case glslang::EOpConstructInt64: |
3554 | 404 | case glslang::EOpConstructI64Vec2: |
3555 | 404 | case glslang::EOpConstructI64Vec3: |
3556 | 404 | case glslang::EOpConstructI64Vec4: |
3557 | 404 | case glslang::EOpConstructUint64: |
3558 | 422 | case glslang::EOpConstructU64Vec2: |
3559 | 422 | case glslang::EOpConstructU64Vec3: |
3560 | 422 | case glslang::EOpConstructU64Vec4: |
3561 | 422 | case glslang::EOpConstructStruct: |
3562 | 671 | case glslang::EOpConstructTextureSampler: |
3563 | 680 | case glslang::EOpConstructReference: |
3564 | 680 | case glslang::EOpConstructCooperativeMatrixNV: |
3565 | 680 | case glslang::EOpConstructCooperativeMatrixKHR: |
3566 | 680 | case glslang::EOpConstructCooperativeVectorNV: |
3567 | 680 | case glslang::EOpConstructSaturated: |
3568 | 680 | { |
3569 | 680 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
3570 | 680 | std::vector<spv::Id> arguments; |
3571 | 680 | translateArguments(*node, arguments, lvalueCoherentFlags); |
3572 | 680 | spv::Id constructed; |
3573 | 680 | if (node->getOp() == glslang::EOpConstructTextureSampler) { |
3574 | 249 | const glslang::TType& texType = node->getSequence()[0]->getAsTyped()->getType(); |
3575 | 249 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6 && |
3576 | 0 | texType.getSampler().isBuffer()) { |
3577 | | // SamplerBuffer is not supported in spirv1.6 so |
3578 | | // `samplerBuffer(textureBuffer, sampler)` is a no-op |
3579 | | // and textureBuffer is the result going forward |
3580 | 0 | constructed = arguments[0]; |
3581 | 0 | } else |
3582 | 249 | constructed = builder.createOp(spv::Op::OpSampledImage, resultType(), arguments); |
3583 | 431 | } else if (node->getOp() == glslang::EOpConstructCooperativeMatrixKHR && |
3584 | 0 | node->getType().isCoopMatKHR() && node->getSequence()[0]->getAsTyped()->getType().isCoopMatKHR() && |
3585 | 0 | !node->getAsTyped()->getType().sameCoopMatUse(node->getSequence()[0]->getAsTyped()->getType())) { |
3586 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionsNV); |
3587 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
3588 | 0 | constructed = builder.createCooperativeMatrixConversion(resultType(), arguments[0]); |
3589 | 431 | } else if (node->getType().isCoopVecOrLongVector() && |
3590 | 0 | arguments.size() == 1 && |
3591 | 0 | builder.getTypeId(arguments[0]) == resultType()) { |
3592 | 0 | constructed = arguments[0]; |
3593 | 431 | } else if (node->getOp() == glslang::EOpConstructStruct || |
3594 | 431 | node->getOp() == glslang::EOpConstructCooperativeMatrixNV || |
3595 | 431 | node->getOp() == glslang::EOpConstructCooperativeMatrixKHR || |
3596 | 431 | node->getType().isArray() || |
3597 | | // Handle constructing coopvec from one component here, to avoid the component |
3598 | | // getting smeared |
3599 | 422 | (node->getType().hasSpecConstantVectorComponents() && arguments.size() == 1 && builder.isScalar(arguments[0]))) { |
3600 | 9 | std::vector<spv::Id> constituents; |
3601 | 27 | for (int c = 0; c < (int)arguments.size(); ++c) |
3602 | 18 | constituents.push_back(arguments[c]); |
3603 | 9 | constructed = createCompositeConstruct(resultType(), constituents); |
3604 | 422 | } else if (isMatrix) |
3605 | 36 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
3606 | 386 | else if (node->getOp() == glslang::EOpConstructSaturated) { |
3607 | 0 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
3608 | 0 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
3609 | 0 | TranslateNonUniformDecoration(lvalueCoherentFlags) }; |
3610 | |
|
3611 | 0 | constructed = createConversion(node->getOp(), decorations, resultType(), arguments[1], |
3612 | 0 | node->getType().getBasicType(), node->getSequence()[1]->getAsTyped()->getBasicType()); |
3613 | 0 | builder.addDecoration(constructed, spv::Decoration::SaturatedToLargestFloat8NormalConversionEXT); |
3614 | 0 | builder.createStore(constructed, arguments[0]); |
3615 | 0 | } |
3616 | 386 | else |
3617 | 386 | constructed = builder.createConstructor(precision, arguments, resultType()); |
3618 | | |
3619 | 680 | if (node->getType().getQualifier().isNonUniform()) { |
3620 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
3621 | 0 | if (extensions.find("GL_EXT_descriptor_heap") == extensions.end()) { |
3622 | 0 | builder.addDecoration(constructed, spv::Decoration::NonUniformEXT); |
3623 | 0 | } |
3624 | 0 | } |
3625 | | |
3626 | 680 | builder.clearAccessChain(); |
3627 | 680 | builder.setAccessChainRValue(constructed); |
3628 | | |
3629 | 680 | return false; |
3630 | 680 | } |
3631 | | |
3632 | | // These six are component-wise compares with component-wise results. |
3633 | | // Forward on to createBinaryOperation(), requesting a vector result. |
3634 | 18 | case glslang::EOpLessThan: |
3635 | 36 | case glslang::EOpGreaterThan: |
3636 | 53 | case glslang::EOpLessThanEqual: |
3637 | 71 | case glslang::EOpGreaterThanEqual: |
3638 | 89 | case glslang::EOpVectorEqual: |
3639 | 99 | case glslang::EOpVectorNotEqual: |
3640 | 99 | { |
3641 | | // Map the operation to a binary |
3642 | 99 | binOp = node->getOp(); |
3643 | 99 | reduceComparison = false; |
3644 | 99 | switch (node->getOp()) { |
3645 | 18 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
3646 | 10 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
3647 | 71 | default: binOp = node->getOp(); break; |
3648 | 99 | } |
3649 | | |
3650 | 99 | break; |
3651 | 99 | } |
3652 | 99 | case glslang::EOpMul: |
3653 | | // component-wise matrix multiply |
3654 | 0 | binOp = glslang::EOpMul; |
3655 | 0 | break; |
3656 | 0 | case glslang::EOpOuterProduct: |
3657 | | // two vectors multiplied to make a matrix |
3658 | 0 | binOp = glslang::EOpOuterProduct; |
3659 | 0 | break; |
3660 | 528 | case glslang::EOpDot: |
3661 | 528 | { |
3662 | | // for scalar dot product, use multiply |
3663 | 528 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
3664 | 528 | if (!glslangOperands[0]->getAsTyped()->getType().isLongVector() && |
3665 | 528 | glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
3666 | 0 | binOp = glslang::EOpMul; |
3667 | 528 | else if (isTypeFloat(node->getType().getBasicType())) |
3668 | 48 | binOp = glslang::EOpDot; |
3669 | 528 | break; |
3670 | 99 | } |
3671 | 0 | case glslang::EOpMod: |
3672 | | // when an aggregate, this is the floating-point mod built-in function, |
3673 | | // which can be emitted by the one in createBinaryOperation() |
3674 | 0 | binOp = glslang::EOpMod; |
3675 | 0 | break; |
3676 | | |
3677 | 0 | case glslang::EOpEmitVertex: |
3678 | 0 | case glslang::EOpEndPrimitive: |
3679 | 0 | case glslang::EOpBarrier: |
3680 | 0 | case glslang::EOpMemoryBarrier: |
3681 | 0 | case glslang::EOpMemoryBarrierAtomicCounter: |
3682 | 0 | case glslang::EOpMemoryBarrierBuffer: |
3683 | 0 | case glslang::EOpMemoryBarrierImage: |
3684 | 0 | case glslang::EOpMemoryBarrierShared: |
3685 | 0 | case glslang::EOpGroupMemoryBarrier: |
3686 | 0 | case glslang::EOpDeviceMemoryBarrier: |
3687 | 0 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
3688 | 0 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
3689 | 0 | case glslang::EOpWorkgroupMemoryBarrier: |
3690 | 0 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
3691 | 0 | case glslang::EOpSubgroupBarrier: |
3692 | 0 | case glslang::EOpSubgroupMemoryBarrier: |
3693 | 0 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
3694 | 0 | case glslang::EOpSubgroupMemoryBarrierImage: |
3695 | 0 | case glslang::EOpSubgroupMemoryBarrierShared: |
3696 | 0 | noReturnValue = true; |
3697 | | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
3698 | 0 | break; |
3699 | | |
3700 | 38 | case glslang::EOpAtomicAdd: |
3701 | 38 | case glslang::EOpAtomicSubtract: |
3702 | 76 | case glslang::EOpAtomicMin: |
3703 | 114 | case glslang::EOpAtomicMax: |
3704 | 114 | case glslang::EOpAtomicAnd: |
3705 | 114 | case glslang::EOpAtomicOr: |
3706 | 114 | case glslang::EOpAtomicXor: |
3707 | 152 | case glslang::EOpAtomicExchange: |
3708 | 152 | case glslang::EOpAtomicCompSwap: |
3709 | 152 | atomic = true; |
3710 | 152 | break; |
3711 | | |
3712 | 0 | case glslang::EOpAtomicStore: |
3713 | 0 | noReturnValue = true; |
3714 | 0 | [[fallthrough]]; |
3715 | 0 | case glslang::EOpAtomicLoad: |
3716 | 0 | atomic = true; |
3717 | 0 | break; |
3718 | | |
3719 | 0 | case glslang::EOpAtomicCounterAdd: |
3720 | 0 | case glslang::EOpAtomicCounterSubtract: |
3721 | 0 | case glslang::EOpAtomicCounterMin: |
3722 | 0 | case glslang::EOpAtomicCounterMax: |
3723 | 0 | case glslang::EOpAtomicCounterAnd: |
3724 | 0 | case glslang::EOpAtomicCounterOr: |
3725 | 0 | case glslang::EOpAtomicCounterXor: |
3726 | 0 | case glslang::EOpAtomicCounterExchange: |
3727 | 0 | case glslang::EOpAtomicCounterCompSwap: |
3728 | 0 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
3729 | 0 | builder.addCapability(spv::Capability::AtomicStorageOps); |
3730 | 0 | atomic = true; |
3731 | 0 | break; |
3732 | | |
3733 | 0 | case glslang::EOpAbsDifference: |
3734 | 0 | case glslang::EOpAddSaturate: |
3735 | 0 | case glslang::EOpSubSaturate: |
3736 | 0 | case glslang::EOpAverage: |
3737 | 0 | case glslang::EOpAverageRounded: |
3738 | 0 | case glslang::EOpMul32x16: |
3739 | 0 | builder.addCapability(spv::Capability::IntegerFunctions2INTEL); |
3740 | 0 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
3741 | 0 | binOp = node->getOp(); |
3742 | 0 | break; |
3743 | | |
3744 | 0 | case glslang::EOpExpectEXT: |
3745 | 0 | builder.addCapability(spv::Capability::ExpectAssumeKHR); |
3746 | 0 | builder.addExtension(spv::E_SPV_KHR_expect_assume); |
3747 | 0 | binOp = node->getOp(); |
3748 | 0 | break; |
3749 | | |
3750 | 0 | case glslang::EOpIgnoreIntersectionNV: |
3751 | 0 | case glslang::EOpTerminateRayNV: |
3752 | 0 | case glslang::EOpTraceNV: |
3753 | 0 | case glslang::EOpTraceRayMotionNV: |
3754 | 0 | case glslang::EOpTraceKHR: |
3755 | 0 | case glslang::EOpExecuteCallableNV: |
3756 | 0 | case glslang::EOpExecuteCallableKHR: |
3757 | 0 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
3758 | 0 | case glslang::EOpEmitMeshTasksEXT: |
3759 | 0 | case glslang::EOpSetMeshOutputsEXT: |
3760 | 0 | noReturnValue = true; |
3761 | 0 | break; |
3762 | 10 | case glslang::EOpRayQueryInitialize: |
3763 | 10 | case glslang::EOpRayQueryTerminate: |
3764 | 20 | case glslang::EOpRayQueryGenerateIntersection: |
3765 | 20 | case glslang::EOpRayQueryConfirmIntersection: |
3766 | 20 | builder.addExtension("SPV_KHR_ray_query"); |
3767 | 20 | builder.addCapability(spv::Capability::RayQueryKHR); |
3768 | 20 | noReturnValue = true; |
3769 | 20 | break; |
3770 | 0 | case glslang::EOpRayQueryProceed: |
3771 | 20 | case glslang::EOpRayQueryGetIntersectionType: |
3772 | 20 | case glslang::EOpRayQueryGetRayTMin: |
3773 | 20 | case glslang::EOpRayQueryGetRayFlags: |
3774 | 40 | case glslang::EOpRayQueryGetIntersectionT: |
3775 | 60 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
3776 | 80 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
3777 | 90 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
3778 | 100 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
3779 | 120 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
3780 | 140 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
3781 | 160 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
3782 | 160 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
3783 | 180 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
3784 | 200 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
3785 | 200 | case glslang::EOpRayQueryGetWorldRayDirection: |
3786 | 200 | case glslang::EOpRayQueryGetWorldRayOrigin: |
3787 | 220 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
3788 | 240 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
3789 | 240 | builder.addExtension("SPV_KHR_ray_query"); |
3790 | 240 | builder.addCapability(spv::Capability::RayQueryKHR); |
3791 | 240 | break; |
3792 | 0 | case glslang::EOpCooperativeMatrixLoad: |
3793 | 0 | case glslang::EOpCooperativeMatrixStore: |
3794 | 0 | case glslang::EOpCooperativeMatrixLoadNV: |
3795 | 0 | case glslang::EOpCooperativeMatrixStoreNV: |
3796 | 0 | case glslang::EOpCooperativeMatrixLoadTensorNV: |
3797 | 0 | case glslang::EOpCooperativeMatrixStoreTensorNV: |
3798 | 0 | case glslang::EOpCooperativeMatrixReduceNV: |
3799 | 0 | case glslang::EOpCooperativeMatrixPerElementOpNV: |
3800 | 0 | case glslang::EOpCooperativeMatrixTransposeNV: |
3801 | 0 | case glslang::EOpCooperativeVectorMatMulNV: |
3802 | 0 | case glslang::EOpCooperativeVectorMatMulAddNV: |
3803 | 0 | case glslang::EOpCooperativeVectorLoadNV: |
3804 | 0 | case glslang::EOpCooperativeVectorStoreNV: |
3805 | 0 | case glslang::EOpCooperativeVectorOuterProductAccumulateNV: |
3806 | 0 | case glslang::EOpCooperativeVectorReduceSumAccumulateNV: |
3807 | 0 | noReturnValue = true; |
3808 | 0 | break; |
3809 | 0 | case glslang::EOpBeginInvocationInterlock: |
3810 | 0 | case glslang::EOpEndInvocationInterlock: |
3811 | 0 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
3812 | 0 | noReturnValue = true; |
3813 | 0 | break; |
3814 | | |
3815 | 0 | case glslang::EOpHitObjectTraceRayNV: |
3816 | 0 | case glslang::EOpHitObjectTraceRayMotionNV: |
3817 | 0 | case glslang::EOpHitObjectGetAttributesNV: |
3818 | 0 | case glslang::EOpHitObjectExecuteShaderNV: |
3819 | 0 | case glslang::EOpHitObjectRecordEmptyNV: |
3820 | 0 | case glslang::EOpHitObjectRecordMissNV: |
3821 | 0 | case glslang::EOpHitObjectRecordMissMotionNV: |
3822 | 0 | case glslang::EOpHitObjectRecordHitNV: |
3823 | 0 | case glslang::EOpHitObjectRecordHitMotionNV: |
3824 | 0 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
3825 | 0 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
3826 | 0 | case glslang::EOpReorderThreadNV: |
3827 | 0 | noReturnValue = true; |
3828 | 0 | [[fallthrough]]; |
3829 | 0 | case glslang::EOpHitObjectIsEmptyNV: |
3830 | 0 | case glslang::EOpHitObjectIsMissNV: |
3831 | 0 | case glslang::EOpHitObjectIsHitNV: |
3832 | 0 | case glslang::EOpHitObjectGetRayTMinNV: |
3833 | 0 | case glslang::EOpHitObjectGetRayTMaxNV: |
3834 | 0 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
3835 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
3836 | 0 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
3837 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
3838 | 0 | case glslang::EOpHitObjectGetObjectToWorldNV: |
3839 | 0 | case glslang::EOpHitObjectGetWorldToObjectNV: |
3840 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
3841 | 0 | case glslang::EOpHitObjectGetInstanceIdNV: |
3842 | 0 | case glslang::EOpHitObjectGetGeometryIndexNV: |
3843 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
3844 | 0 | case glslang::EOpHitObjectGetHitKindNV: |
3845 | 0 | case glslang::EOpHitObjectGetCurrentTimeNV: |
3846 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
3847 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
3848 | 0 | builder.addExtension(spv::E_SPV_NV_shader_invocation_reorder); |
3849 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
3850 | 0 | break; |
3851 | | |
3852 | 0 | case glslang::EOpHitObjectGetLSSPositionsNV: |
3853 | 0 | case glslang::EOpHitObjectGetLSSRadiiNV: |
3854 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
3855 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
3856 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
3857 | 0 | noReturnValue = true; |
3858 | 0 | break; |
3859 | | |
3860 | 0 | case glslang::EOpRayQueryGetIntersectionLSSPositionsNV: |
3861 | 0 | case glslang::EOpRayQueryGetIntersectionLSSRadiiNV: |
3862 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
3863 | 0 | builder.addCapability(spv::Capability::RayQueryKHR); |
3864 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
3865 | 0 | noReturnValue = true; |
3866 | 0 | break; |
3867 | | |
3868 | 0 | case glslang::EOpRayQueryGetIntersectionSpherePositionNV: |
3869 | 0 | case glslang::EOpRayQueryGetIntersectionSphereRadiusNV: |
3870 | 0 | case glslang::EOpRayQueryIsSphereHitNV: |
3871 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
3872 | 0 | builder.addCapability(spv::Capability::RayQueryKHR); |
3873 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
3874 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
3875 | 0 | break; |
3876 | | |
3877 | 0 | case glslang::EOpRayQueryGetIntersectionLSSHitValueNV: |
3878 | 0 | case glslang::EOpRayQueryIsLSSHitNV: |
3879 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
3880 | 0 | builder.addCapability(spv::Capability::RayQueryKHR); |
3881 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
3882 | 0 | break; |
3883 | | |
3884 | 0 | case glslang::EOpHitObjectTraceRayEXT: |
3885 | 0 | case glslang::EOpHitObjectTraceRayMotionEXT: |
3886 | 0 | case glslang::EOpHitObjectGetAttributesEXT: |
3887 | 0 | case glslang::EOpHitObjectExecuteShaderEXT: |
3888 | 0 | case glslang::EOpHitObjectRecordEmptyEXT: |
3889 | 0 | case glslang::EOpHitObjectRecordMissEXT: |
3890 | 0 | case glslang::EOpHitObjectRecordMissMotionEXT: |
3891 | 0 | case glslang::EOpReorderThreadEXT: |
3892 | 0 | case glslang::EOpHitObjectSetShaderBindingTableRecordIndexEXT: |
3893 | 0 | case glslang::EOpHitObjectReorderExecuteEXT: |
3894 | 0 | case glslang::EOpHitObjectTraceReorderExecuteEXT: |
3895 | 0 | case glslang::EOpHitObjectTraceMotionReorderExecuteEXT: |
3896 | 0 | case glslang::EOpHitObjectRecordFromQueryEXT: |
3897 | 0 | case glslang::EOpHitObjectGetIntersectionTriangleVertexPositionsEXT: |
3898 | 0 | noReturnValue = true; |
3899 | 0 | [[fallthrough]]; |
3900 | 0 | case glslang::EOpHitObjectIsEmptyEXT: |
3901 | 0 | case glslang::EOpHitObjectIsMissEXT: |
3902 | 0 | case glslang::EOpHitObjectIsHitEXT: |
3903 | 0 | case glslang::EOpHitObjectGetRayTMinEXT: |
3904 | 0 | case glslang::EOpHitObjectGetRayTMaxEXT: |
3905 | 0 | case glslang::EOpHitObjectGetRayFlagsEXT: |
3906 | 0 | case glslang::EOpHitObjectGetObjectRayOriginEXT: |
3907 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionEXT: |
3908 | 0 | case glslang::EOpHitObjectGetWorldRayOriginEXT: |
3909 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionEXT: |
3910 | 0 | case glslang::EOpHitObjectGetObjectToWorldEXT: |
3911 | 0 | case glslang::EOpHitObjectGetWorldToObjectEXT: |
3912 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexEXT: |
3913 | 0 | case glslang::EOpHitObjectGetInstanceIdEXT: |
3914 | 0 | case glslang::EOpHitObjectGetGeometryIndexEXT: |
3915 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexEXT: |
3916 | 0 | case glslang::EOpHitObjectGetHitKindEXT: |
3917 | 0 | case glslang::EOpHitObjectGetCurrentTimeEXT: |
3918 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexEXT: |
3919 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleEXT: |
3920 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_invocation_reorder); |
3921 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderEXT); |
3922 | 0 | break; |
3923 | | |
3924 | 0 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
3925 | 0 | builder.addExtension(spv::E_SPV_KHR_ray_tracing_position_fetch); |
3926 | 0 | builder.addCapability(spv::Capability::RayQueryPositionFetchKHR); |
3927 | 0 | noReturnValue = true; |
3928 | 0 | break; |
3929 | 18 | case glslang::EOpImageSampleWeightedQCOM: |
3930 | 18 | builder.addCapability(spv::Capability::TextureSampleWeightedQCOM); |
3931 | 18 | builder.addExtension(spv::E_SPV_QCOM_image_processing); |
3932 | 18 | break; |
3933 | 18 | case glslang::EOpImageBoxFilterQCOM: |
3934 | 18 | builder.addCapability(spv::Capability::TextureBoxFilterQCOM); |
3935 | 18 | builder.addExtension(spv::E_SPV_QCOM_image_processing); |
3936 | 18 | break; |
3937 | 27 | case glslang::EOpImageBlockMatchSADQCOM: |
3938 | 54 | case glslang::EOpImageBlockMatchSSDQCOM: |
3939 | 54 | builder.addCapability(spv::Capability::TextureBlockMatchQCOM); |
3940 | 54 | builder.addExtension(spv::E_SPV_QCOM_image_processing); |
3941 | 54 | break; |
3942 | 0 | case glslang::EOpTensorWriteARM: |
3943 | 0 | noReturnValue = true; |
3944 | 0 | break; |
3945 | | |
3946 | 18 | case glslang::EOpImageBlockMatchWindowSSDQCOM: |
3947 | 36 | case glslang::EOpImageBlockMatchWindowSADQCOM: |
3948 | 36 | builder.addCapability(spv::Capability::TextureBlockMatchQCOM); |
3949 | 36 | builder.addExtension(spv::E_SPV_QCOM_image_processing); |
3950 | 36 | builder.addCapability(spv::Capability::TextureBlockMatch2QCOM); |
3951 | 36 | builder.addExtension(spv::E_SPV_QCOM_image_processing2); |
3952 | 36 | break; |
3953 | | |
3954 | 18 | case glslang::EOpImageBlockMatchGatherSSDQCOM: |
3955 | 36 | case glslang::EOpImageBlockMatchGatherSADQCOM: |
3956 | 36 | builder.addCapability(spv::Capability::TextureBlockMatchQCOM); |
3957 | 36 | builder.addExtension(spv::E_SPV_QCOM_image_processing); |
3958 | 36 | builder.addCapability(spv::Capability::TextureBlockMatch2QCOM); |
3959 | 36 | builder.addExtension(spv::E_SPV_QCOM_image_processing2); |
3960 | 36 | break; |
3961 | | |
3962 | 0 | case glslang::EOpFetchMicroTriangleVertexPositionNV: |
3963 | 0 | case glslang::EOpFetchMicroTriangleVertexBarycentricNV: |
3964 | 0 | builder.addExtension(spv::E_SPV_NV_displacement_micromap); |
3965 | 0 | builder.addCapability(spv::Capability::DisplacementMicromapNV); |
3966 | 0 | break; |
3967 | | |
3968 | 0 | case glslang::EOpRayQueryGetIntersectionClusterIdNV: |
3969 | 0 | builder.addExtension(spv::E_SPV_NV_cluster_acceleration_structure); |
3970 | 0 | builder.addCapability(spv::Capability::RayQueryKHR); |
3971 | 0 | builder.addCapability(spv::Capability::RayTracingClusterAccelerationStructureNV); |
3972 | 0 | break; |
3973 | | |
3974 | 0 | case glslang::EOpDebugPrintf: |
3975 | 0 | noReturnValue = true; |
3976 | 0 | break; |
3977 | | |
3978 | 818 | default: |
3979 | 818 | break; |
3980 | 11.6k | } |
3981 | | |
3982 | | // |
3983 | | // See if it maps to a regular operation. |
3984 | | // |
3985 | 2.01k | if (binOp != glslang::EOpNull) { |
3986 | 147 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
3987 | 147 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
3988 | 147 | assert(left && right); |
3989 | | |
3990 | 147 | builder.clearAccessChain(); |
3991 | 147 | left->traverse(this); |
3992 | 147 | spv::Id leftId = accessChainLoad(left->getType()); |
3993 | | |
3994 | 147 | builder.clearAccessChain(); |
3995 | 147 | right->traverse(this); |
3996 | 147 | spv::Id rightId = accessChainLoad(right->getType()); |
3997 | | |
3998 | 147 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
3999 | 147 | OpDecorations decorations = { precision, |
4000 | 147 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
4001 | 147 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
4002 | 147 | result = createBinaryOperation(binOp, decorations, |
4003 | 147 | resultType(), leftId, rightId, |
4004 | 147 | left->getType().getBasicType(), reduceComparison); |
4005 | | |
4006 | | // code above should only make binOp that exists in createBinaryOperation |
4007 | 147 | assert(result != spv::NoResult); |
4008 | 147 | builder.clearAccessChain(); |
4009 | 147 | builder.setAccessChainRValue(result); |
4010 | | |
4011 | 147 | return false; |
4012 | 147 | } |
4013 | | |
4014 | | // |
4015 | | // Create the list of operands. |
4016 | | // |
4017 | 1.87k | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
4018 | 1.87k | std::vector<spv::Id> operands; |
4019 | 1.87k | std::vector<spv::IdImmediate> memoryAccessOperands; |
4020 | 1.87k | if (node->getOp() == glslang::EOpAbortEXT) { |
4021 | 0 | createAbortEXT(glslangOperands); |
4022 | 0 | return false; |
4023 | 0 | } |
4024 | 6.70k | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
4025 | | // special case l-value operands; there are just a few |
4026 | 4.83k | bool lvalue = false; |
4027 | 4.83k | switch (node->getOp()) { |
4028 | 0 | case glslang::EOpModf: |
4029 | 0 | if (arg == 1) |
4030 | 0 | lvalue = true; |
4031 | 0 | break; |
4032 | | |
4033 | | |
4034 | | |
4035 | 0 | case glslang::EOpHitObjectRecordFromQueryEXT: |
4036 | 0 | case glslang::EOpHitObjectGetIntersectionTriangleVertexPositionsEXT: |
4037 | 0 | if (arg == 0 || arg == 1) |
4038 | 0 | lvalue = true; |
4039 | 0 | break; |
4040 | | |
4041 | 0 | case glslang::EOpHitObjectRecordHitNV: |
4042 | 0 | case glslang::EOpHitObjectRecordHitMotionNV: |
4043 | 0 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
4044 | 0 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
4045 | 0 | case glslang::EOpHitObjectTraceRayNV: |
4046 | 0 | case glslang::EOpHitObjectTraceRayMotionNV: |
4047 | 0 | case glslang::EOpHitObjectExecuteShaderNV: |
4048 | 0 | case glslang::EOpHitObjectRecordMissNV: |
4049 | 0 | case glslang::EOpHitObjectRecordMissMotionNV: |
4050 | 0 | case glslang::EOpHitObjectGetAttributesNV: |
4051 | 0 | case glslang::EOpHitObjectGetClusterIdNV: |
4052 | 0 | case glslang::EOpHitObjectTraceRayEXT: |
4053 | 0 | case glslang::EOpHitObjectTraceRayMotionEXT: |
4054 | 0 | case glslang::EOpHitObjectExecuteShaderEXT: |
4055 | 0 | case glslang::EOpHitObjectRecordMissEXT: |
4056 | 0 | case glslang::EOpHitObjectRecordMissMotionEXT: |
4057 | 0 | case glslang::EOpHitObjectGetAttributesEXT: |
4058 | 0 | case glslang::EOpHitObjectSetShaderBindingTableRecordIndexEXT: |
4059 | 0 | case glslang::EOpHitObjectReorderExecuteEXT: |
4060 | 0 | case glslang::EOpHitObjectTraceReorderExecuteEXT: |
4061 | 0 | case glslang::EOpHitObjectTraceMotionReorderExecuteEXT: |
4062 | 0 | if (arg == 0) |
4063 | 0 | lvalue = true; |
4064 | 0 | break; |
4065 | | |
4066 | 0 | case glslang::EOpHitObjectGetLSSPositionsNV: |
4067 | 0 | case glslang::EOpHitObjectGetLSSRadiiNV: |
4068 | 0 | lvalue = true; |
4069 | 0 | break; |
4070 | | |
4071 | 80 | case glslang::EOpRayQueryInitialize: |
4072 | 80 | case glslang::EOpRayQueryTerminate: |
4073 | 80 | case glslang::EOpRayQueryConfirmIntersection: |
4074 | 80 | case glslang::EOpRayQueryProceed: |
4075 | 100 | case glslang::EOpRayQueryGenerateIntersection: |
4076 | 140 | case glslang::EOpRayQueryGetIntersectionType: |
4077 | 180 | case glslang::EOpRayQueryGetIntersectionT: |
4078 | 220 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
4079 | 260 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
4080 | 280 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
4081 | 300 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
4082 | 340 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
4083 | 380 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
4084 | 420 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
4085 | 460 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
4086 | 500 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
4087 | 540 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
4088 | 580 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
4089 | 580 | case glslang::EOpRayQueryGetIntersectionClusterIdNV: |
4090 | 580 | case glslang::EOpRayQueryGetIntersectionSpherePositionNV: |
4091 | 580 | case glslang::EOpRayQueryGetIntersectionSphereRadiusNV: |
4092 | 580 | case glslang::EOpRayQueryGetIntersectionLSSHitValueNV: |
4093 | 580 | case glslang::EOpRayQueryIsSphereHitNV: |
4094 | 580 | case glslang::EOpRayQueryIsLSSHitNV: |
4095 | 580 | if (arg == 0) |
4096 | 260 | lvalue = true; |
4097 | 580 | break; |
4098 | | |
4099 | 76 | case glslang::EOpAtomicAdd: |
4100 | 76 | case glslang::EOpAtomicSubtract: |
4101 | 152 | case glslang::EOpAtomicMin: |
4102 | 228 | case glslang::EOpAtomicMax: |
4103 | 228 | case glslang::EOpAtomicAnd: |
4104 | 228 | case glslang::EOpAtomicOr: |
4105 | 228 | case glslang::EOpAtomicXor: |
4106 | 304 | case glslang::EOpAtomicExchange: |
4107 | 304 | case glslang::EOpAtomicCompSwap: |
4108 | 304 | if (arg == 0) |
4109 | 152 | lvalue = true; |
4110 | 304 | break; |
4111 | | |
4112 | 18 | case glslang::EOpFrexp: |
4113 | 18 | if (arg == 1) |
4114 | 9 | lvalue = true; |
4115 | 18 | break; |
4116 | 0 | case glslang::EOpInterpolateAtSample: |
4117 | 0 | case glslang::EOpInterpolateAtOffset: |
4118 | 0 | case glslang::EOpInterpolateAtVertex: |
4119 | 0 | if (arg == 0) { |
4120 | | // If GLSL, use the address of the interpolant argument. |
4121 | | // If HLSL, use an internal version of OpInterolates that takes |
4122 | | // the rvalue of the interpolant. A fixup pass in spirv-opt |
4123 | | // legalization will remove the OpLoad and convert to an lvalue. |
4124 | | // Had to do this because legalization will only propagate a |
4125 | | // builtin into an rvalue. |
4126 | 0 | lvalue = glslangIntermediate->getSource() != glslang::EShSourceHlsl; |
4127 | | |
4128 | | // Does it need a swizzle inversion? If so, evaluation is inverted; |
4129 | | // operate first on the swizzle base, then apply the swizzle. |
4130 | | // That is, we transform |
4131 | | // |
4132 | | // interpolate(v.zy) -> interpolate(v).zy |
4133 | | // |
4134 | 0 | if (glslangOperands[0]->getAsOperator() && |
4135 | 0 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
4136 | 0 | invertedType = convertGlslangToSpvType( |
4137 | 0 | glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
4138 | 0 | } |
4139 | 0 | break; |
4140 | 0 | case glslang::EOpAtomicLoad: |
4141 | 0 | case glslang::EOpAtomicStore: |
4142 | 0 | case glslang::EOpAtomicCounterAdd: |
4143 | 0 | case glslang::EOpAtomicCounterSubtract: |
4144 | 0 | case glslang::EOpAtomicCounterMin: |
4145 | 0 | case glslang::EOpAtomicCounterMax: |
4146 | 0 | case glslang::EOpAtomicCounterAnd: |
4147 | 0 | case glslang::EOpAtomicCounterOr: |
4148 | 0 | case glslang::EOpAtomicCounterXor: |
4149 | 0 | case glslang::EOpAtomicCounterExchange: |
4150 | 0 | case glslang::EOpAtomicCounterCompSwap: |
4151 | 0 | if (arg == 0) |
4152 | 0 | lvalue = true; |
4153 | 0 | break; |
4154 | 0 | case glslang::EOpAddCarry: |
4155 | 0 | case glslang::EOpSubBorrow: |
4156 | 0 | if (arg == 2) |
4157 | 0 | lvalue = true; |
4158 | 0 | break; |
4159 | 0 | case glslang::EOpUMulExtended: |
4160 | 0 | case glslang::EOpIMulExtended: |
4161 | 0 | if (arg >= 2) |
4162 | 0 | lvalue = true; |
4163 | 0 | break; |
4164 | 0 | case glslang::EOpCooperativeMatrixLoad: |
4165 | 0 | case glslang::EOpCooperativeMatrixLoadNV: |
4166 | 0 | case glslang::EOpCooperativeMatrixLoadTensorNV: |
4167 | 0 | case glslang::EOpCooperativeVectorLoadNV: |
4168 | 0 | if (arg == 0 || arg == 1) |
4169 | 0 | lvalue = true; |
4170 | 0 | break; |
4171 | 0 | case glslang::EOpCooperativeMatrixStore: |
4172 | 0 | case glslang::EOpCooperativeMatrixStoreNV: |
4173 | 0 | case glslang::EOpCooperativeMatrixStoreTensorNV: |
4174 | 0 | case glslang::EOpCooperativeVectorStoreNV: |
4175 | 0 | if (arg == 1) |
4176 | 0 | lvalue = true; |
4177 | 0 | break; |
4178 | 0 | case glslang::EOpCooperativeVectorMatMulNV: |
4179 | 0 | if (arg == 0 || arg == 3) |
4180 | 0 | lvalue = true; |
4181 | 0 | break; |
4182 | 0 | case glslang::EOpCooperativeVectorMatMulAddNV: |
4183 | 0 | if (arg == 0 || arg == 3 || arg == 6) |
4184 | 0 | lvalue = true; |
4185 | 0 | break; |
4186 | 0 | case glslang::EOpCooperativeVectorOuterProductAccumulateNV: |
4187 | 0 | if (arg == 2) |
4188 | 0 | lvalue = true; |
4189 | 0 | break; |
4190 | 0 | case glslang::EOpCooperativeVectorReduceSumAccumulateNV: |
4191 | 0 | if (arg == 1) |
4192 | 0 | lvalue = true; |
4193 | 0 | break; |
4194 | 0 | case glslang::EOpCooperativeMatrixReduceNV: |
4195 | 0 | case glslang::EOpCooperativeMatrixPerElementOpNV: |
4196 | 0 | case glslang::EOpCooperativeMatrixTransposeNV: |
4197 | 0 | if (arg == 0) |
4198 | 0 | lvalue = true; |
4199 | 0 | break; |
4200 | 36 | case glslang::EOpSpirvInst: |
4201 | 36 | if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvByReference()) |
4202 | 0 | lvalue = true; |
4203 | 36 | break; |
4204 | 0 | case glslang::EOpReorderThreadNV: |
4205 | 0 | case glslang::EOpReorderThreadEXT: |
4206 | | //Three variants of reorderThreadNV, two of them use hitObjectNV |
4207 | 0 | if (arg == 0 && glslangOperands.size() != 2) |
4208 | 0 | lvalue = true; |
4209 | 0 | break; |
4210 | 0 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
4211 | 0 | case glslang::EOpRayQueryGetIntersectionLSSPositionsNV: |
4212 | 0 | case glslang::EOpRayQueryGetIntersectionLSSRadiiNV: |
4213 | 0 | if (arg == 0 || arg == 2) |
4214 | 0 | lvalue = true; |
4215 | 0 | break; |
4216 | 0 | case glslang::EOpTensorReadARM: |
4217 | 0 | if (arg == 2) |
4218 | 0 | lvalue = true; |
4219 | 0 | break; |
4220 | 3.89k | default: |
4221 | 3.89k | break; |
4222 | 4.83k | } |
4223 | 4.83k | builder.clearAccessChain(); |
4224 | 4.83k | if (invertedType != spv::NoType && arg == 0) |
4225 | 0 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
4226 | 4.83k | else |
4227 | 4.83k | glslangOperands[arg]->traverse(this); |
4228 | | |
4229 | 4.83k | bool isCoopMat = node->getOp() == glslang::EOpCooperativeMatrixLoad || |
4230 | 4.83k | node->getOp() == glslang::EOpCooperativeMatrixStore || |
4231 | 4.83k | node->getOp() == glslang::EOpCooperativeMatrixLoadNV || |
4232 | 4.83k | node->getOp() == glslang::EOpCooperativeMatrixStoreNV || |
4233 | 4.83k | node->getOp() == glslang::EOpCooperativeMatrixLoadTensorNV || |
4234 | 4.83k | node->getOp() == glslang::EOpCooperativeMatrixStoreTensorNV; |
4235 | 4.83k | bool isCoopVec = node->getOp() == glslang::EOpCooperativeVectorLoadNV || |
4236 | 4.83k | node->getOp() == glslang::EOpCooperativeVectorStoreNV; |
4237 | 4.83k | if (isCoopMat || isCoopVec) { |
4238 | |
|
4239 | 0 | if (arg == 1) { |
4240 | 0 | spv::Builder::AccessChain::CoherentFlags coherentFlags {}; |
4241 | 0 | unsigned int alignment {}; |
4242 | 0 | if (isCoopMat) { |
4243 | | // fold "element" parameter into the access chain |
4244 | 0 | spv::Builder::AccessChain save = builder.getAccessChain(); |
4245 | 0 | builder.clearAccessChain(); |
4246 | 0 | glslangOperands[2]->traverse(this); |
4247 | |
|
4248 | 0 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
4249 | |
|
4250 | 0 | builder.setAccessChain(save); |
4251 | | |
4252 | | // Point to the first element of the array. |
4253 | 0 | builder.accessChainPush(elementId, |
4254 | 0 | TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
4255 | 0 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
4256 | 0 | coherentFlags = builder.getAccessChain().coherentFlags; |
4257 | 0 | alignment = builder.getAccessChain().alignment; |
4258 | 0 | } else { |
4259 | 0 | coherentFlags = builder.getAccessChain().coherentFlags; |
4260 | 0 | coherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
4261 | 0 | alignment = 16; |
4262 | 0 | } |
4263 | |
|
4264 | 0 | spv::MemoryAccessMask memoryAccess = TranslateMemoryAccess(coherentFlags); |
4265 | 0 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
4266 | 0 | node->getOp() == glslang::EOpCooperativeMatrixLoadNV || |
4267 | 0 | node->getOp() == glslang::EOpCooperativeMatrixLoadTensorNV || |
4268 | 0 | node->getOp() == glslang::EOpCooperativeVectorLoadNV) |
4269 | 0 | memoryAccess = (memoryAccess & ~spv::MemoryAccessMask::MakePointerAvailableKHR); |
4270 | 0 | if (node->getOp() == glslang::EOpCooperativeMatrixStore || |
4271 | 0 | node->getOp() == glslang::EOpCooperativeMatrixStoreNV || |
4272 | 0 | node->getOp() == glslang::EOpCooperativeMatrixStoreTensorNV || |
4273 | 0 | node->getOp() == glslang::EOpCooperativeVectorStoreNV) |
4274 | 0 | memoryAccess = (memoryAccess & ~spv::MemoryAccessMask::MakePointerVisibleKHR); |
4275 | 0 | if (builder.getStorageClass(builder.getAccessChain().base) == |
4276 | 0 | spv::StorageClass::PhysicalStorageBufferEXT) { |
4277 | 0 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessMask::Aligned); |
4278 | 0 | } |
4279 | |
|
4280 | 0 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
4281 | |
|
4282 | 0 | if (anySet(memoryAccess, spv::MemoryAccessMask::Aligned)) { |
4283 | 0 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
4284 | 0 | } |
4285 | |
|
4286 | 0 | if (anySet(memoryAccess, |
4287 | 0 | spv::MemoryAccessMask::MakePointerAvailableKHR | spv::MemoryAccessMask::MakePointerVisibleKHR)) { |
4288 | 0 | memoryAccessOperands.push_back(spv::IdImmediate(true, |
4289 | 0 | builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
4290 | 0 | } |
4291 | 0 | } else if (isCoopMat && arg == 2) { |
4292 | 0 | continue; |
4293 | 0 | } |
4294 | 0 | } |
4295 | | |
4296 | | // for l-values, pass the address, for r-values, pass the value |
4297 | 4.83k | if (lvalue) { |
4298 | 421 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
4299 | | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
4300 | | // reduce to a simple access chain. So, we need a temporary vector to |
4301 | | // receive the result, and must later swizzle that into the original |
4302 | | // l-value. |
4303 | 0 | complexLvalues.push_back(builder.getAccessChain()); |
4304 | 0 | temporaryLvalues.push_back(builder.createVariable( |
4305 | 0 | spv::NoPrecision, spv::StorageClass::Function, |
4306 | 0 | builder.accessChainGetInferredType(), "swizzleTemp")); |
4307 | 0 | operands.push_back(temporaryLvalues.back()); |
4308 | 421 | } else { |
4309 | 421 | if (node->getOp() == glslang::EOpTensorReadARM && arg == 2) { |
4310 | | // tensorReadARM stores the result after emitting the op, so keep the |
4311 | | // original l-value access chain and avoid materializing a transient |
4312 | | // pointer that may not preserve descriptor-heap indexing. |
4313 | 0 | tensorReadResultLValue = builder.getAccessChain(); |
4314 | | // Keep the operand slot so optional tensor operands keep their |
4315 | | // existing indices in the later lowering logic. |
4316 | 0 | operands.push_back(spv::NoResult); |
4317 | 421 | } else { |
4318 | 421 | operands.push_back(builder.accessChainGetLValue()); |
4319 | 421 | } |
4320 | 421 | } |
4321 | 421 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
4322 | 421 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
4323 | 4.41k | } else { |
4324 | 4.41k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
4325 | 4.41k | glslang::TOperator glslangOp = node->getOp(); |
4326 | 4.41k | if (arg == 1 && |
4327 | 1.86k | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
4328 | 1.84k | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
4329 | 1.82k | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
4330 | 1.80k | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
4331 | 1.78k | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
4332 | 1.77k | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
4333 | 1.76k | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
4334 | 1.74k | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
4335 | 1.72k | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
4336 | 1.70k | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
4337 | 1.68k | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
4338 | 1.66k | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
4339 | 1.64k | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject || |
4340 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT || |
4341 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionClusterIdNV || |
4342 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionSpherePositionNV || |
4343 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionSphereRadiusNV || |
4344 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionLSSHitValueNV || |
4345 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionLSSPositionsNV || |
4346 | 1.62k | glslangOp == glslang::EOpRayQueryGetIntersectionLSSRadiiNV || |
4347 | 1.62k | glslangOp == glslang::EOpRayQueryIsLSSHitNV || |
4348 | 1.62k | glslangOp == glslang::EOpRayQueryIsSphereHitNV |
4349 | 1.86k | )) { |
4350 | 240 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
4351 | 240 | operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
4352 | 4.17k | } else if ((arg == 10 && glslangOp == glslang::EOpTraceKHR) || |
4353 | 4.17k | (arg == 11 && glslangOp == glslang::EOpTraceRayMotionNV) || |
4354 | 4.17k | (arg == 1 && glslangOp == glslang::EOpExecuteCallableKHR) || |
4355 | 4.17k | (arg == 1 && glslangOp == glslang::EOpHitObjectExecuteShaderNV) || |
4356 | 4.17k | (arg == 1 && glslangOp == glslang::EOpHitObjectExecuteShaderEXT) || |
4357 | 4.17k | (arg == 11 && glslangOp == glslang::EOpHitObjectTraceRayNV) || |
4358 | 4.17k | (arg == 11 && glslangOp == glslang::EOpHitObjectTraceRayEXT) || |
4359 | 4.17k | (arg == 12 && glslangOp == glslang::EOpHitObjectTraceRayMotionNV) || |
4360 | 4.17k | (arg == 12 && glslangOp == glslang::EOpHitObjectTraceRayMotionEXT) || |
4361 | 4.17k | (arg == 12 && glslangOp == glslang::EOpHitObjectTraceMotionReorderExecuteEXT && glslangOperands.size() == 13) || |
4362 | 4.17k | (arg == 14 && glslangOp == glslang::EOpHitObjectTraceMotionReorderExecuteEXT && glslangOperands.size() == 15) || |
4363 | 4.17k | (arg == 11 && glslangOp == glslang::EOpHitObjectTraceReorderExecuteEXT && glslangOperands.size() == 12) || |
4364 | 4.17k | (arg == 13 && glslangOp == glslang::EOpHitObjectTraceReorderExecuteEXT && glslangOperands.size() == 14) || |
4365 | 4.17k | (arg == 1 && glslangOp == glslang::EOpHitObjectReorderExecuteEXT && glslangOperands.size() == 2) || |
4366 | 4.17k | (arg == 3 && glslangOp == glslang::EOpHitObjectReorderExecuteEXT && glslangOperands.size() == 4)) { |
4367 | 0 | const int set = glslangOp == glslang::EOpExecuteCallableKHR ? 1 : 0; |
4368 | 0 | const int location = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
4369 | 0 | auto itNode = locationToSymbol[set].find(location); |
4370 | 0 | visitSymbol(itNode->second); |
4371 | 0 | spv::Id symId = getSymbolId(itNode->second); |
4372 | 0 | operands.push_back(symId); |
4373 | 4.17k | } else if ((arg == 12 && glslangOp == glslang::EOpHitObjectRecordHitNV) || |
4374 | 4.17k | (arg == 13 && glslangOp == glslang::EOpHitObjectRecordHitMotionNV) || |
4375 | 4.17k | (arg == 11 && glslangOp == glslang::EOpHitObjectRecordHitWithIndexNV) || |
4376 | 4.17k | (arg == 12 && glslangOp == glslang::EOpHitObjectRecordHitWithIndexMotionNV) || |
4377 | 4.17k | (arg == 3 && glslangOp == glslang::EOpHitObjectRecordFromQueryEXT) || |
4378 | 4.17k | (arg == 1 && glslangOp == glslang::EOpHitObjectGetAttributesEXT) || |
4379 | 4.17k | (arg == 1 && glslangOp == glslang::EOpHitObjectGetAttributesNV)) { |
4380 | 0 | const int location = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
4381 | 0 | const int set = 2; |
4382 | 0 | auto itNode = locationToSymbol[set].find(location); |
4383 | 0 | visitSymbol(itNode->second); |
4384 | 0 | spv::Id symId = getSymbolId(itNode->second); |
4385 | 0 | operands.push_back(symId); |
4386 | 4.17k | } else if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
4387 | | // Will be translated to a literal value, make a placeholder here |
4388 | 0 | operands.push_back(spv::NoResult); |
4389 | 4.17k | } else if (glslangOperands[arg]->getAsTyped()->getBasicType() == glslang::EbtFunction) { |
4390 | 0 | spv::Function* function = functionMap[glslangOperands[arg]->getAsSymbolNode()->getMangledName().c_str()]; |
4391 | 0 | assert(function); |
4392 | 0 | operands.push_back(function->getId()); |
4393 | 4.17k | } else { |
4394 | 4.17k | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
4395 | 4.17k | } |
4396 | 4.41k | } |
4397 | 4.83k | } |
4398 | | |
4399 | 1.87k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
4400 | 1.87k | if (node->getOp() == glslang::EOpCooperativeMatrixLoadTensorNV) { |
4401 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4402 | |
|
4403 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixTensorAddressingNV); |
4404 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
4405 | |
|
4406 | 0 | spv::Id object = builder.createLoad(operands[0], spv::NoPrecision); |
4407 | |
|
4408 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // Pointer |
4409 | 0 | idImmOps.push_back(spv::IdImmediate(true, object)); // Object |
4410 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // tensorLayout |
4411 | |
|
4412 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); // memoryaccess |
4413 | | |
4414 | | // initialize tensor operands to zero, then OR in flags based on the operands |
4415 | 0 | size_t tensorOpIdx = idImmOps.size(); |
4416 | 0 | idImmOps.push_back(spv::IdImmediate(false, 0)); |
4417 | | |
4418 | | // Order in `operands` follows GLSL argument order: |
4419 | | // [optional tensorView] [optional decodeFunc] [optional decodeVectorFunc] |
4420 | | // The emitted tensor-addressing operand order is the same (bit order: |
4421 | | // TensorView 0x1, DecodeFunc 0x2, DecodeVectorFunc 0x4), so we just |
4422 | | // bin operands by source position. |
4423 | 0 | spv::Id viewId = 0; |
4424 | 0 | spv::Id scalarFuncId = 0; |
4425 | 0 | spv::Id vectorFuncId = 0; |
4426 | 0 | uint32_t i = 3; |
4427 | 0 | if (i < operands.size() && builder.isTensorView(operands[i])) |
4428 | 0 | viewId = operands[i++]; |
4429 | 0 | if (i < operands.size()) |
4430 | 0 | scalarFuncId = operands[i++]; |
4431 | 0 | if (i < operands.size()) |
4432 | 0 | vectorFuncId = operands[i++]; |
4433 | 0 | assert(i == operands.size()); |
4434 | |
|
4435 | 0 | if (viewId != 0) { |
4436 | 0 | addMask(idImmOps[tensorOpIdx].word, spv::TensorAddressingOperandsMask::TensorView); |
4437 | 0 | idImmOps.push_back(spv::IdImmediate(true, viewId)); |
4438 | 0 | } |
4439 | 0 | if (scalarFuncId != 0) { |
4440 | 0 | addMask(idImmOps[tensorOpIdx].word, spv::TensorAddressingOperandsMask::DecodeFunc); |
4441 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixBlockLoadsNV); |
4442 | 0 | idImmOps.push_back(spv::IdImmediate(true, scalarFuncId)); |
4443 | 0 | } |
4444 | 0 | if (vectorFuncId != 0) { |
4445 | 0 | addMask(idImmOps[tensorOpIdx].word, spv::TensorAddressingOperandsMask::DecodeVectorFunc); |
4446 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixDecodeVectorNV); |
4447 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix_decode_vector); |
4448 | 0 | idImmOps.push_back(spv::IdImmediate(true, vectorFuncId)); |
4449 | 0 | } |
4450 | | |
4451 | | // get the pointee type |
4452 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4453 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
4454 | | // do the op |
4455 | 0 | spv::Id result = builder.createOp(spv::Op::OpCooperativeMatrixLoadTensorNV, typeId, idImmOps); |
4456 | | // store the result to the pointer (out param 'm') |
4457 | 0 | builder.createStore(result, operands[0]); |
4458 | 0 | result = 0; |
4459 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
4460 | 1.87k | node->getOp() == glslang::EOpCooperativeMatrixLoadNV) { |
4461 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4462 | |
|
4463 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
4464 | 0 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
4465 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // matrixLayout |
4466 | 0 | auto layout = (spv::CooperativeMatrixLayout)builder.getConstantScalar(operands[3]); |
4467 | 0 | if (layout == spv::CooperativeMatrixLayout::RowBlockedInterleavedARM || |
4468 | 0 | layout == spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM) { |
4469 | 0 | builder.addExtension(spv::E_SPV_ARM_cooperative_matrix_layouts); |
4470 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixLayoutsARM); |
4471 | 0 | } |
4472 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
4473 | 0 | } else { |
4474 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
4475 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
4476 | 0 | } |
4477 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
4478 | | // get the pointee type |
4479 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4480 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
4481 | | // do the op |
4482 | 0 | spv::Id result = node->getOp() == glslang::EOpCooperativeMatrixLoad |
4483 | 0 | ? builder.createOp(spv::Op::OpCooperativeMatrixLoadKHR, typeId, idImmOps) |
4484 | 0 | : builder.createOp(spv::Op::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
4485 | | // store the result to the pointer (out param 'm') |
4486 | 0 | builder.createStore(result, operands[0]); |
4487 | 0 | result = 0; |
4488 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixStoreTensorNV) { |
4489 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4490 | |
|
4491 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
4492 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
4493 | |
|
4494 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixTensorAddressingNV); |
4495 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
4496 | |
|
4497 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // tensorLayout |
4498 | |
|
4499 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); // memoryaccess |
4500 | |
|
4501 | 0 | if (operands.size() > 3) { |
4502 | 0 | idImmOps.push_back(spv::IdImmediate(false, spv::TensorAddressingOperandsMask::TensorView)); |
4503 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // tensorView |
4504 | 0 | } else { |
4505 | 0 | idImmOps.push_back(spv::IdImmediate(false, 0)); |
4506 | 0 | } |
4507 | |
|
4508 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeMatrixStoreTensorNV, idImmOps); |
4509 | 0 | result = 0; |
4510 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore || |
4511 | 1.87k | node->getOp() == glslang::EOpCooperativeMatrixStoreNV) { |
4512 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4513 | |
|
4514 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
4515 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
4516 | 0 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
4517 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // matrixLayout |
4518 | 0 | auto layout = (spv::CooperativeMatrixLayout)builder.getConstantScalar(operands[3]); |
4519 | 0 | if (layout == spv::CooperativeMatrixLayout::RowBlockedInterleavedARM || |
4520 | 0 | layout == spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM) { |
4521 | 0 | builder.addExtension(spv::E_SPV_ARM_cooperative_matrix_layouts); |
4522 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixLayoutsARM); |
4523 | 0 | } |
4524 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
4525 | 0 | } else { |
4526 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
4527 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
4528 | 0 | } |
4529 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
4530 | |
|
4531 | 0 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
4532 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeMatrixStoreKHR, idImmOps); |
4533 | 0 | else |
4534 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeMatrixStoreNV, idImmOps); |
4535 | 0 | result = 0; |
4536 | 1.87k | } else if (node->getOp() == glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT) { |
4537 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4538 | |
|
4539 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // q |
4540 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // committed |
4541 | |
|
4542 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeVectorType(builder.makeFloatType(32), 3), |
4543 | 0 | builder.makeUintConstant(3), 0); |
4544 | | // do the op |
4545 | |
|
4546 | 0 | spv::Op spvOp = spv::Op::OpRayQueryGetIntersectionTriangleVertexPositionsKHR; |
4547 | |
|
4548 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4549 | | // store the result to the pointer (out param 'm') |
4550 | 0 | builder.createStore(result, operands[2]); |
4551 | 0 | result = 0; |
4552 | 1.87k | } else if (node->getOp() == glslang::EOpRayQueryGetIntersectionLSSPositionsNV) { |
4553 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4554 | |
|
4555 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // q |
4556 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // committed |
4557 | |
|
4558 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeVectorType(builder.makeFloatType(32), 3), |
4559 | 0 | builder.makeUintConstant(2), 0); |
4560 | | // do the op |
4561 | |
|
4562 | 0 | spv::Op spvOp = spv::Op::OpRayQueryGetIntersectionLSSPositionsNV; |
4563 | |
|
4564 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4565 | | // store the result to the pointer (out param 'm') |
4566 | 0 | builder.createStore(result, operands[2]); |
4567 | 0 | result = 0; |
4568 | 1.87k | } else if (node->getOp() == glslang::EOpRayQueryGetIntersectionLSSRadiiNV) { |
4569 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4570 | |
|
4571 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // q |
4572 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // committed |
4573 | |
|
4574 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeFloatType(32), |
4575 | 0 | builder.makeUintConstant(2), 0); |
4576 | | // do the op |
4577 | |
|
4578 | 0 | spv::Op spvOp = spv::Op::OpRayQueryGetIntersectionLSSRadiiNV; |
4579 | |
|
4580 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4581 | | // store the result to the pointer (out param 'm') |
4582 | 0 | builder.createStore(result, operands[2]); |
4583 | 0 | result = 0; |
4584 | 1.87k | } else if (node->getOp() == glslang::EOpHitObjectGetLSSPositionsNV) { |
4585 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4586 | |
|
4587 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // hitObject |
4588 | |
|
4589 | 0 | spv::Op spvOp = spv::Op::OpHitObjectGetLSSPositionsNV; |
4590 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeVectorType(builder.makeFloatType(32), 3), |
4591 | 0 | builder.makeUintConstant(2), 0); |
4592 | |
|
4593 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4594 | | // store the result to the pointer (out param 'm') |
4595 | 0 | builder.createStore(result, operands[1]); |
4596 | 0 | result = 0; |
4597 | 1.87k | } else if (node->getOp() == glslang::EOpHitObjectGetLSSRadiiNV) { |
4598 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4599 | |
|
4600 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // hitObject |
4601 | |
|
4602 | 0 | spv::Op spvOp = spv::Op::OpHitObjectGetLSSRadiiNV; |
4603 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeFloatType(32), |
4604 | 0 | builder.makeUintConstant(2), 0); |
4605 | |
|
4606 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4607 | | // store the result to the pointer (out param 'm') |
4608 | 0 | builder.createStore(result, operands[1]); |
4609 | 0 | result = 0; |
4610 | 1.87k | } else if (node->getOp() == glslang::EOpHitObjectGetIntersectionTriangleVertexPositionsEXT) { |
4611 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4612 | |
|
4613 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // hitObject |
4614 | |
|
4615 | 0 | spv::Op spvOp = spv::Op::OpHitObjectGetIntersectionTriangleVertexPositionsEXT; |
4616 | 0 | spv::Id typeId = builder.makeArrayType(builder.makeVectorType(builder.makeFloatType(32), 3), |
4617 | 0 | builder.makeUintConstant(3), 0); |
4618 | |
|
4619 | 0 | spv::Id result = builder.createOp(spvOp, typeId, idImmOps); |
4620 | | // store the result to the pointer (out param 'm') |
4621 | 0 | builder.createStore(result, operands[1]); |
4622 | 0 | result = 0; |
4623 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixMulAdd) { |
4624 | 0 | auto matrixOperands = spv::CooperativeMatrixOperandsMask::MaskNone; |
4625 | | |
4626 | | // If the optional operand is present, initialize matrixOperands to that value. |
4627 | 0 | if (glslangOperands.size() == 4 && glslangOperands[3]->getAsConstantUnion()) { |
4628 | 0 | matrixOperands = (spv::CooperativeMatrixOperandsMask)glslangOperands[3]->getAsConstantUnion()->getConstArray()[0].getIConst(); |
4629 | 0 | } |
4630 | | |
4631 | | // Determine Cooperative Matrix Operands bits from the signedness of the types. |
4632 | 0 | if (isTypeSignedInt(glslangOperands[0]->getAsTyped()->getBasicType())) |
4633 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixASignedComponentsKHR); |
4634 | 0 | if (isTypeSignedInt(glslangOperands[1]->getAsTyped()->getBasicType())) |
4635 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixBSignedComponentsKHR); |
4636 | 0 | if (isTypeSignedInt(glslangOperands[2]->getAsTyped()->getBasicType())) |
4637 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixCSignedComponentsKHR); |
4638 | 0 | if (isTypeSignedInt(node->getBasicType())) |
4639 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixResultSignedComponentsKHR); |
4640 | |
|
4641 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4642 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); |
4643 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); |
4644 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); |
4645 | 0 | if (matrixOperands != spv::CooperativeMatrixOperandsMask::MaskNone) |
4646 | 0 | idImmOps.push_back(spv::IdImmediate(false, matrixOperands)); |
4647 | |
|
4648 | 0 | result = builder.createOp(spv::Op::OpCooperativeMatrixMulAddKHR, resultType(), idImmOps); |
4649 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixReduceNV) { |
4650 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixReductionsNV); |
4651 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
4652 | |
|
4653 | 0 | spv::Op opcode = spv::Op::OpCooperativeMatrixReduceNV; |
4654 | 0 | unsigned mask = glslangOperands[2]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
4655 | |
|
4656 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4657 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
4658 | |
|
4659 | 0 | result = builder.createCooperativeMatrixReduce(opcode, typeId, operands[1], mask, operands[3]); |
4660 | | // store the result to the pointer (out param 'm') |
4661 | 0 | builder.createStore(result, operands[0]); |
4662 | 0 | result = 0; |
4663 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixPerElementOpNV) { |
4664 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixPerElementOperationsNV); |
4665 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
4666 | |
|
4667 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4668 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
4669 | |
|
4670 | 0 | result = builder.createCooperativeMatrixPerElementOp(typeId, operands); |
4671 | | // store the result to the pointer |
4672 | 0 | builder.createStore(result, operands[0]); |
4673 | 0 | result = 0; |
4674 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeMatrixTransposeNV) { |
4675 | |
|
4676 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionsNV); |
4677 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix2); |
4678 | |
|
4679 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4680 | 0 | assert(builder.isCooperativeMatrixType(typeId)); |
4681 | |
|
4682 | 0 | result = builder.createUnaryOp(spv::Op::OpCooperativeMatrixTransposeNV, typeId, operands[1]); |
4683 | | // store the result to the pointer |
4684 | 0 | builder.createStore(result, operands[0]); |
4685 | 0 | result = 0; |
4686 | 1.87k | } else if (node->getOp() == glslang::EOpBitCastArrayQCOM) { |
4687 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionQCOM); |
4688 | 0 | builder.addExtension(spv::E_SPV_QCOM_cooperative_matrix_conversion); |
4689 | 0 | result = builder.createUnaryOp(spv::Op::OpBitCastArrayQCOM, resultType(), operands[0]); |
4690 | 1.87k | } else if (node->getOp() == glslang::EOpCompositeConstructCoopMatQCOM) { |
4691 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionQCOM); |
4692 | 0 | builder.addExtension(spv::E_SPV_QCOM_cooperative_matrix_conversion); |
4693 | 0 | result = builder.createUnaryOp(spv::Op::OpCompositeConstructCoopMatQCOM, resultType(), operands[0]); |
4694 | 1.87k | } else if (node->getOp() == glslang::EOpCompositeExtractCoopMatQCOM) { |
4695 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionQCOM); |
4696 | 0 | builder.addExtension(spv::E_SPV_QCOM_cooperative_matrix_conversion); |
4697 | 0 | result = builder.createUnaryOp(spv::Op::OpCompositeExtractCoopMatQCOM, resultType(), operands[0]); |
4698 | 1.87k | } else if (node->getOp() == glslang::EOpExtractSubArrayQCOM) { |
4699 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixConversionQCOM); |
4700 | 0 | builder.addExtension(spv::E_SPV_QCOM_cooperative_matrix_conversion); |
4701 | |
|
4702 | 0 | std::vector<spv::Id> arguments { operands[0], operands[1] };; |
4703 | 0 | result = builder.createOp(spv::Op::OpExtractSubArrayQCOM, resultType(), arguments); |
4704 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeVectorMatMulNV || |
4705 | 1.87k | node->getOp() == glslang::EOpCooperativeVectorMatMulAddNV) { |
4706 | 0 | auto matrixOperands = spv::CooperativeMatrixOperandsMask::MaskNone; |
4707 | |
|
4708 | 0 | bool isMulAdd = node->getOp() == glslang::EOpCooperativeVectorMatMulAddNV; |
4709 | | |
4710 | | // Determine Cooperative Matrix Operands bits from the signedness of the types. |
4711 | |
|
4712 | 0 | if (isTypeSignedInt(glslangOperands[1]->getAsTyped()->getBasicType())) |
4713 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixBSignedComponentsKHR); |
4714 | 0 | if (isTypeSignedInt(glslangOperands[0]->getAsTyped()->getBasicType())) |
4715 | 0 | addMask(matrixOperands, spv::CooperativeMatrixOperandsMask::MatrixResultSignedComponentsKHR); |
4716 | |
|
4717 | 0 | uint32_t opIdx = 1; |
4718 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4719 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // Input |
4720 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // InputInterpretation |
4721 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // Matrix |
4722 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // MatrixOffset |
4723 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // MatrixInterpretation |
4724 | 0 | if (isMulAdd) { |
4725 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // Bias |
4726 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // BiasOffset |
4727 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // BiasInterpretation |
4728 | 0 | } |
4729 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // M |
4730 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // K |
4731 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // MemoryLayout |
4732 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // Transpose |
4733 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[opIdx++])); // MatrixStride |
4734 | 0 | if (matrixOperands != spv::CooperativeMatrixOperandsMask::MaskNone) |
4735 | 0 | idImmOps.push_back(spv::IdImmediate(false, matrixOperands)); // Cooperative Matrix Operands |
4736 | | |
4737 | | // get the pointee type |
4738 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4739 | 0 | assert(builder.isCooperativeVectorType(typeId)); |
4740 | | // do the op |
4741 | 0 | spv::Id result = builder.createOp(isMulAdd ? spv::Op::OpCooperativeVectorMatrixMulAddNV : spv::Op::OpCooperativeVectorMatrixMulNV, typeId, idImmOps); |
4742 | | // store the result to the pointer (out param 'res') |
4743 | 0 | builder.createStore(result, operands[0]); |
4744 | 0 | result = 0; |
4745 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeVectorLoadNV) { |
4746 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4747 | |
|
4748 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
4749 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // offset |
4750 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
4751 | | // get the pointee type |
4752 | 0 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
4753 | 0 | assert(builder.isCooperativeVectorType(typeId)); |
4754 | | // do the op |
4755 | 0 | spv::Id result = builder.createOp(spv::Op::OpCooperativeVectorLoadNV, typeId, idImmOps); |
4756 | | // store the result to the pointer (out param 'v') |
4757 | 0 | builder.createStore(result, operands[0]); |
4758 | 0 | result = 0; |
4759 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeVectorStoreNV) { |
4760 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4761 | |
|
4762 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
4763 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // offset |
4764 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
4765 | 0 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
4766 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeVectorStoreNV, idImmOps); |
4767 | 0 | result = 0; |
4768 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeVectorOuterProductAccumulateNV) { |
4769 | 0 | builder.addCapability(spv::Capability::CooperativeVectorTrainingNV); |
4770 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_vector); |
4771 | |
|
4772 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4773 | |
|
4774 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // Matrix |
4775 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // Offset |
4776 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // A |
4777 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // B |
4778 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[5])); // MemoryLayout |
4779 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[6])); // MatrixInterpretation |
4780 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[4])); // Stride |
4781 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeVectorOuterProductAccumulateNV, idImmOps); |
4782 | 0 | result = 0; |
4783 | 1.87k | } else if (node->getOp() == glslang::EOpCooperativeVectorReduceSumAccumulateNV) { |
4784 | 0 | builder.addCapability(spv::Capability::CooperativeVectorTrainingNV); |
4785 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_vector); |
4786 | |
|
4787 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4788 | |
|
4789 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // Buf |
4790 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // Offset |
4791 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // A |
4792 | 0 | builder.createNoResultOp(spv::Op::OpCooperativeVectorReduceSumAccumulateNV, idImmOps); |
4793 | 0 | result = 0; |
4794 | 1.87k | } else if (node->getOp() == glslang::EOpTensorReadARM || |
4795 | 1.87k | node->getOp() == glslang::EOpTensorWriteARM) { |
4796 | 0 | const bool isWrite = node->getOp() == glslang::EOpTensorWriteARM; |
4797 | 0 | const unsigned int tensorMinOperandCount = 3; |
4798 | 0 | assert(operands.size() >= tensorMinOperandCount); |
4799 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4800 | |
|
4801 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // tensor |
4802 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // coords |
4803 | 0 | if (isWrite) { |
4804 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // value |
4805 | 0 | } |
4806 | | |
4807 | | // Analyze the tensor operands |
4808 | 0 | spv::IdImmediate tensorOperands = { false, uint32_t(spv::TensorOperandsMask::MaskNone) }; |
4809 | 0 | bool pushExtraArg = false; |
4810 | 0 | if (operands.size() > tensorMinOperandCount) { |
4811 | 0 | auto enumVal = builder.getConstantScalar(operands[tensorMinOperandCount]); |
4812 | |
|
4813 | 0 | if (enumVal & uint32_t(spv::TensorOperandsMask::NontemporalARM)) { |
4814 | 0 | tensorOperands.word |= uint32_t(spv::TensorOperandsMask::NontemporalARM); |
4815 | 0 | } |
4816 | 0 | if (enumVal & uint32_t(spv::TensorOperandsMask::OutOfBoundsValueARM)) { |
4817 | 0 | tensorOperands.word |= uint32_t(spv::TensorOperandsMask::OutOfBoundsValueARM); |
4818 | 0 | assert(operands.size() >= tensorMinOperandCount + 2 && |
4819 | 0 | "TensorOperandsOutOfBoundsValueMask requires an additional value"); |
4820 | 0 | pushExtraArg = true; |
4821 | 0 | } |
4822 | 0 | } |
4823 | | |
4824 | | // Append optional tensor operands if the mask was non-zero. |
4825 | 0 | if (tensorOperands.word) { |
4826 | 0 | idImmOps.push_back(tensorOperands); |
4827 | 0 | if (pushExtraArg) |
4828 | 0 | idImmOps.push_back(spv::IdImmediate(true, operands[tensorMinOperandCount + 1])); |
4829 | 0 | } |
4830 | |
|
4831 | 0 | if (isWrite) { |
4832 | 0 | builder.createNoResultOp(spv::Op::OpTensorWriteARM, idImmOps); |
4833 | 0 | result = 0; |
4834 | 0 | } else { |
4835 | | // Use the result argument type as the OpTensorReadARM result type. |
4836 | 0 | const glslang::TType &resArgType = glslangOperands[2]->getAsTyped()->getType(); |
4837 | 0 | spv::Id retType = convertGlslangToSpvType(resArgType); |
4838 | 0 | result = builder.createOp(spv::Op::OpTensorReadARM, retType, idImmOps); |
4839 | | // Store the result to the result argument. |
4840 | 0 | assert(tensorReadResultLValue.base != spv::NoResult); |
4841 | 0 | builder.setAccessChain(tensorReadResultLValue); |
4842 | 0 | accessChainStore(resArgType, result); |
4843 | 0 | } |
4844 | 1.87k | } else if (node->getOp() == glslang::EOpTensorSizeARM) { |
4845 | | // Expected operands are (tensor, dimension) |
4846 | 0 | assert(operands.size() == 2); |
4847 | |
|
4848 | 0 | spv::Id tensorOp = operands[0]; |
4849 | 0 | spv::Id dimOp = operands[1]; |
4850 | 0 | assert(builder.isTensorTypeARM(builder.getTypeId(tensorOp)) && "operand #0 must be a tensor"); |
4851 | |
|
4852 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4853 | 0 | idImmOps.push_back(spv::IdImmediate(true, tensorOp)); |
4854 | 0 | idImmOps.push_back(spv::IdImmediate(true, dimOp)); |
4855 | 0 | result = builder.createOp(spv::Op::OpTensorQuerySizeARM, resultType(), idImmOps); |
4856 | 1.87k | } else if (atomic) { |
4857 | | // Handle all atomics |
4858 | 152 | glslang::TBasicType typeProxy = (node->getOp() == glslang::EOpAtomicStore) |
4859 | 152 | ? node->getSequence()[0]->getAsTyped()->getBasicType() : node->getBasicType(); |
4860 | 152 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, typeProxy, |
4861 | 152 | lvalueCoherentFlags, node->getType()); |
4862 | 1.72k | } else if (node->getOp() == glslang::EOpSpirvInst) { |
4863 | 18 | const auto& spirvInst = node->getSpirvInstruction(); |
4864 | 18 | if (spirvInst.set == "") { |
4865 | 0 | std::vector<spv::IdImmediate> idImmOps; |
4866 | 0 | for (unsigned int i = 0; i < glslangOperands.size(); ++i) { |
4867 | 0 | if (glslangOperands[i]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
4868 | | // Translate the constant to a literal value |
4869 | 0 | std::vector<unsigned> literals; |
4870 | 0 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
4871 | 0 | constants.push_back(glslangOperands[i]->getAsConstantUnion()); |
4872 | 0 | TranslateLiterals(constants, literals); |
4873 | 0 | idImmOps.push_back({false, literals[0]}); |
4874 | 0 | } else |
4875 | 0 | idImmOps.push_back({true, operands[i]}); |
4876 | 0 | } |
4877 | |
|
4878 | 0 | if (node->getBasicType() == glslang::EbtVoid) |
4879 | 0 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), idImmOps); |
4880 | 0 | else |
4881 | 0 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), resultType(), idImmOps); |
4882 | 18 | } else { |
4883 | 18 | result = builder.createBuiltinCall( |
4884 | 18 | resultType(), spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(spirvInst.set.c_str()), |
4885 | 18 | spirvInst.id, operands); |
4886 | 18 | } |
4887 | 18 | noReturnValue = node->getBasicType() == glslang::EbtVoid; |
4888 | 1.70k | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
4889 | 0 | if (!nonSemanticDebugPrintf) { |
4890 | 0 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf"); |
4891 | 0 | } |
4892 | 0 | result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
4893 | 0 | builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
4894 | 1.70k | } else { |
4895 | | // Pass through to generic operations. |
4896 | 1.70k | switch (glslangOperands.size()) { |
4897 | 0 | case 0: |
4898 | 0 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
4899 | 0 | break; |
4900 | 0 | case 1: |
4901 | 0 | { |
4902 | 0 | OpDecorations decorations = { precision, |
4903 | 0 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
4904 | 0 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
4905 | 0 | result = createUnaryOperation( |
4906 | 0 | node->getOp(), decorations, |
4907 | 0 | resultType(), operands.front(), |
4908 | 0 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags, node->getType()); |
4909 | 0 | } |
4910 | 0 | break; |
4911 | 1.70k | default: |
4912 | 1.70k | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
4913 | 1.70k | break; |
4914 | 1.70k | } |
4915 | | |
4916 | 1.70k | if (invertedType != spv::NoResult) |
4917 | 0 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
4918 | | |
4919 | 1.70k | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
4920 | 0 | builder.setAccessChain(complexLvalues[i]); |
4921 | 0 | builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision), |
4922 | 0 | TranslateNonUniformDecoration(complexLvalues[i].coherentFlags)); |
4923 | 0 | } |
4924 | 1.70k | } |
4925 | | |
4926 | 1.87k | if (noReturnValue) |
4927 | 20 | return false; |
4928 | | |
4929 | 1.85k | if (! result) { |
4930 | 0 | logger->missingFunctionality("unknown glslang aggregate"); |
4931 | 0 | return true; // pick up a child as a placeholder operand |
4932 | 1.85k | } else { |
4933 | 1.85k | builder.clearAccessChain(); |
4934 | 1.85k | builder.setAccessChainRValue(result); |
4935 | 1.85k | return false; |
4936 | 1.85k | } |
4937 | 1.85k | } |
4938 | | |
4939 | | // This path handles both if-then-else and ?: |
4940 | | // The if-then-else has a node type of void, while |
4941 | | // ?: has either a void or a non-void node type |
4942 | | // |
4943 | | // Leaving the result, when not void: |
4944 | | // GLSL only has r-values as the result of a :?, but |
4945 | | // if we have an l-value, that can be more efficient if it will |
4946 | | // become the base of a complex r-value expression, because the |
4947 | | // next layer copies r-values into memory to use the access-chain mechanism |
4948 | | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
4949 | 421 | { |
4950 | | // see if OpSelect can handle it |
4951 | 421 | const auto isOpSelectable = [&]() { |
4952 | 48 | if (node->getBasicType() == glslang::EbtVoid) |
4953 | 4 | return false; |
4954 | | // OpSelect can do all other types starting with SPV 1.4 |
4955 | 44 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
4956 | | // pre-1.4, only scalars and vectors can be handled |
4957 | 44 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
4958 | 0 | return false; |
4959 | 44 | } |
4960 | 44 | return true; |
4961 | 44 | }; |
4962 | | |
4963 | | // See if it simple and safe, or required, to execute both sides. |
4964 | | // Crucially, side effects must be either semantically required or avoided, |
4965 | | // and there are performance trade-offs. |
4966 | | // Return true if required or a good idea (and safe) to execute both sides, |
4967 | | // false otherwise. |
4968 | 421 | const auto bothSidesPolicy = [&]() -> bool { |
4969 | | // do we have both sides? |
4970 | 421 | if (node->getTrueBlock() == nullptr || |
4971 | 367 | node->getFalseBlock() == nullptr) |
4972 | 393 | return false; |
4973 | | |
4974 | | // required? (unless we write additional code to look for side effects |
4975 | | // and make performance trade-offs if none are present) |
4976 | 28 | if (!node->getShortCircuit()) |
4977 | 0 | return true; |
4978 | | |
4979 | | // if not required to execute both, decide based on performance/practicality... |
4980 | | |
4981 | 28 | if (!isOpSelectable()) |
4982 | 4 | return false; |
4983 | | |
4984 | 28 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
4985 | 24 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
4986 | | |
4987 | | // return true if a single operand to ? : is okay for OpSelect |
4988 | 46 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
4989 | 46 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
4990 | 46 | }; |
4991 | | |
4992 | 24 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
4993 | 22 | operandOkay(node->getFalseBlock()->getAsTyped()); |
4994 | 28 | }; |
4995 | | |
4996 | 421 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
4997 | | // emit the condition before doing anything with selection |
4998 | 421 | node->getCondition()->traverse(this); |
4999 | 421 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
5000 | | |
5001 | | // Find a way of executing both sides and selecting the right result. |
5002 | 421 | const auto executeBothSides = [&]() -> void { |
5003 | | // execute both sides |
5004 | 20 | spv::Id resultType = convertGlslangToSpvType(node->getType()); |
5005 | 20 | node->getTrueBlock()->traverse(this); |
5006 | 20 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
5007 | 20 | node->getFalseBlock()->traverse(this); |
5008 | 20 | spv::Id falseValue = accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()); |
5009 | | |
5010 | 20 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
5011 | | |
5012 | | // done if void |
5013 | 20 | if (node->getBasicType() == glslang::EbtVoid) |
5014 | 0 | return; |
5015 | | |
5016 | | // emit code to select between trueValue and falseValue |
5017 | | // see if OpSelect can handle the result type, and that the SPIR-V types |
5018 | | // of the inputs match the result type. |
5019 | 20 | if (isOpSelectable()) { |
5020 | | // Emit OpSelect for this selection. |
5021 | | |
5022 | | // smear condition to vector, if necessary (AST is always scalar) |
5023 | | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
5024 | 20 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
5025 | 0 | condition = builder.smearScalar(spv::NoPrecision, condition, |
5026 | 0 | builder.makeVectorType(builder.makeBoolType(), |
5027 | 0 | builder.getNumComponents(trueValue))); |
5028 | 0 | } |
5029 | | |
5030 | | // If the types do not match, it is because of mismatched decorations on aggregates. |
5031 | | // Since isOpSelectable only lets us get here for SPIR-V >= 1.4, we can use OpCopyObject |
5032 | | // to get matching types. |
5033 | 20 | if (builder.getTypeId(trueValue) != resultType) { |
5034 | 0 | trueValue = builder.createUnaryOp(spv::Op::OpCopyLogical, resultType, trueValue); |
5035 | 0 | } |
5036 | 20 | if (builder.getTypeId(falseValue) != resultType) { |
5037 | 0 | falseValue = builder.createUnaryOp(spv::Op::OpCopyLogical, resultType, falseValue); |
5038 | 0 | } |
5039 | | |
5040 | | // OpSelect |
5041 | 20 | result = builder.createTriOp(spv::Op::OpSelect, resultType, condition, trueValue, falseValue); |
5042 | | |
5043 | 20 | builder.clearAccessChain(); |
5044 | 20 | builder.setAccessChainRValue(result); |
5045 | 20 | } else { |
5046 | | // We need control flow to select the result. |
5047 | | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
5048 | 0 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
5049 | 0 | spv::StorageClass::Function, resultType); |
5050 | | |
5051 | | // Selection control: |
5052 | 0 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
5053 | | |
5054 | | // make an "if" based on the value created by the condition |
5055 | 0 | spv::Builder::If ifBuilder(condition, control, builder); |
5056 | | |
5057 | | // emit the "then" statement |
5058 | 0 | builder.clearAccessChain(); |
5059 | 0 | builder.setAccessChainLValue(result); |
5060 | 0 | multiTypeStore(node->getType(), trueValue); |
5061 | |
|
5062 | 0 | ifBuilder.makeBeginElse(); |
5063 | | // emit the "else" statement |
5064 | 0 | builder.clearAccessChain(); |
5065 | 0 | builder.setAccessChainLValue(result); |
5066 | 0 | multiTypeStore(node->getType(), falseValue); |
5067 | | |
5068 | | // finish off the control flow |
5069 | 0 | ifBuilder.makeEndIf(); |
5070 | |
|
5071 | 0 | builder.clearAccessChain(); |
5072 | 0 | builder.setAccessChainLValue(result); |
5073 | 0 | } |
5074 | 20 | }; |
5075 | | |
5076 | | // Execute the one side needed, as per the condition |
5077 | 421 | const auto executeOneSide = [&]() { |
5078 | | // Always emit control flow. |
5079 | 401 | if (node->getBasicType() != glslang::EbtVoid) { |
5080 | 4 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClass::Function, |
5081 | 4 | convertGlslangToSpvType(node->getType())); |
5082 | 4 | } |
5083 | | |
5084 | | // Selection control: |
5085 | 401 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
5086 | | |
5087 | | // make an "if" based on the value created by the condition |
5088 | 401 | spv::Builder::If ifBuilder(condition, control, builder); |
5089 | | |
5090 | | // emit the "then" statement |
5091 | 401 | if (node->getTrueBlock() != nullptr) { |
5092 | 347 | node->getTrueBlock()->traverse(this); |
5093 | 347 | if (result != spv::NoResult) { |
5094 | 4 | spv::Id load = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
5095 | | |
5096 | 4 | builder.clearAccessChain(); |
5097 | 4 | builder.setAccessChainLValue(result); |
5098 | 4 | multiTypeStore(node->getType(), load); |
5099 | 4 | } |
5100 | 347 | } |
5101 | | |
5102 | 401 | if (node->getFalseBlock() != nullptr) { |
5103 | 8 | ifBuilder.makeBeginElse(); |
5104 | | // emit the "else" statement |
5105 | 8 | node->getFalseBlock()->traverse(this); |
5106 | 8 | if (result != spv::NoResult) { |
5107 | 4 | spv::Id load = accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()); |
5108 | | |
5109 | 4 | builder.clearAccessChain(); |
5110 | 4 | builder.setAccessChainLValue(result); |
5111 | 4 | multiTypeStore(node->getType(), load); |
5112 | 4 | } |
5113 | 8 | } |
5114 | | |
5115 | | // finish off the control flow |
5116 | 401 | ifBuilder.makeEndIf(); |
5117 | | |
5118 | 401 | if (result != spv::NoResult) { |
5119 | 4 | builder.clearAccessChain(); |
5120 | 4 | builder.setAccessChainLValue(result); |
5121 | 4 | } |
5122 | 401 | }; |
5123 | | |
5124 | | // Try for OpSelect (or a requirement to execute both sides) |
5125 | 421 | if (bothSidesPolicy()) { |
5126 | 20 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
5127 | 20 | if (node->getType().getQualifier().isSpecConstant()) |
5128 | 0 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
5129 | 20 | executeBothSides(); |
5130 | 20 | } else |
5131 | 401 | executeOneSide(); |
5132 | | |
5133 | 421 | return false; |
5134 | 421 | } |
5135 | | |
5136 | | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
5137 | 89 | { |
5138 | | // emit and get the condition before doing anything with switch |
5139 | 89 | node->getCondition()->traverse(this); |
5140 | 89 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
5141 | | |
5142 | | // Selection control: |
5143 | 89 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
5144 | | |
5145 | | // browse the children to sort out code segments |
5146 | 89 | int defaultSegment = -1; |
5147 | 89 | std::vector<TIntermNode*> codeSegments; |
5148 | 89 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
5149 | 89 | std::vector<int> caseValues; |
5150 | 89 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
5151 | 437 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
5152 | 348 | TIntermNode* child = *c; |
5153 | 348 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
5154 | 2 | defaultSegment = (int)codeSegments.size(); |
5155 | 346 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
5156 | 172 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
5157 | 172 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
5158 | 172 | ->getConstArray()[0].getIConst()); |
5159 | 172 | } else |
5160 | 174 | codeSegments.push_back(child); |
5161 | 348 | } |
5162 | | |
5163 | | // handle the case where the last code segment is missing, due to no code |
5164 | | // statements between the last case and the end of the switch statement |
5165 | 89 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
5166 | 89 | (int)codeSegments.size() == defaultSegment) |
5167 | 0 | codeSegments.push_back(nullptr); |
5168 | | |
5169 | | // make the switch statement |
5170 | 89 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
5171 | 89 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
5172 | 89 | segmentBlocks); |
5173 | | |
5174 | | // emit all the code in the segments |
5175 | 89 | breakForLoop.push(false); |
5176 | 263 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
5177 | 174 | builder.nextSwitchSegment(segmentBlocks, s); |
5178 | 174 | if (codeSegments[s]) |
5179 | 174 | codeSegments[s]->traverse(this); |
5180 | 0 | else |
5181 | 0 | builder.addSwitchBreak(true); |
5182 | 174 | } |
5183 | 89 | breakForLoop.pop(); |
5184 | | |
5185 | 89 | builder.endSwitch(segmentBlocks); |
5186 | | |
5187 | 89 | return false; |
5188 | 89 | } |
5189 | | |
5190 | | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
5191 | 7.38k | { |
5192 | 7.38k | if (node->getQualifier().isSpirvLiteral()) |
5193 | 0 | return; // Translated to a literal value, skip further processing |
5194 | | |
5195 | 7.38k | int nextConst = 0; |
5196 | 7.38k | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
5197 | | |
5198 | 7.38k | builder.clearAccessChain(); |
5199 | 7.38k | builder.setAccessChainRValue(constant); |
5200 | 7.38k | } |
5201 | | |
5202 | | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
5203 | 242 | { |
5204 | 242 | auto blocks = builder.makeNewLoop(); |
5205 | 242 | builder.createBranch(true, &blocks.head); |
5206 | | |
5207 | | // Loop control: |
5208 | 242 | std::vector<unsigned int> operands; |
5209 | 242 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
5210 | | |
5211 | | // Spec requires back edges to target header blocks, and every header block |
5212 | | // must dominate its merge block. Make a header block first to ensure these |
5213 | | // conditions are met. By definition, it will contain OpLoopMerge, followed |
5214 | | // by a block-ending branch. But we don't want to put any other body/test |
5215 | | // instructions in it, since the body/test may have arbitrary instructions, |
5216 | | // including merges of its own. |
5217 | 242 | builder.setBuildPoint(&blocks.head); |
5218 | 242 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
5219 | 242 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
5220 | 242 | if (node->testFirst() && node->getTest()) { |
5221 | 188 | spv::Block& test = builder.makeNewBlock(); |
5222 | 188 | builder.createBranch(true, &test); |
5223 | | |
5224 | 188 | builder.setBuildPoint(&test); |
5225 | 188 | node->getTest()->traverse(this); |
5226 | 188 | spv::Id condition = accessChainLoad(node->getTestExpr()->getType()); |
5227 | 188 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
5228 | | |
5229 | 188 | builder.setBuildPoint(&blocks.body); |
5230 | 188 | breakForLoop.push(true); |
5231 | 188 | if (node->getBody()) |
5232 | 62 | node->getBody()->traverse(this); |
5233 | 188 | builder.createBranch(true, &blocks.continue_target); |
5234 | 188 | breakForLoop.pop(); |
5235 | | |
5236 | 188 | builder.setBuildPoint(&blocks.continue_target); |
5237 | 188 | if (node->getTerminal()) |
5238 | 124 | node->getTerminal()->traverse(this); |
5239 | 188 | builder.createBranch(true, &blocks.head); |
5240 | 188 | } else { |
5241 | 54 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
5242 | 54 | builder.createBranch(true, &blocks.body); |
5243 | | |
5244 | 54 | breakForLoop.push(true); |
5245 | 54 | builder.setBuildPoint(&blocks.body); |
5246 | 54 | if (node->getBody()) |
5247 | 0 | node->getBody()->traverse(this); |
5248 | 54 | builder.createBranch(true, &blocks.continue_target); |
5249 | 54 | breakForLoop.pop(); |
5250 | | |
5251 | 54 | builder.setBuildPoint(&blocks.continue_target); |
5252 | 54 | if (node->getTerminal()) |
5253 | 0 | node->getTerminal()->traverse(this); |
5254 | 54 | if (node->getTest()) { |
5255 | 36 | node->getTest()->traverse(this); |
5256 | 36 | spv::Id condition = |
5257 | 36 | accessChainLoad(node->getTestExpr()->getType()); |
5258 | 36 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
5259 | 36 | } else { |
5260 | | // TODO: unless there was a break/return/discard instruction |
5261 | | // somewhere in the body, this is an infinite loop, so we should |
5262 | | // issue a warning. |
5263 | 18 | builder.createBranch(true, &blocks.head); |
5264 | 18 | } |
5265 | 54 | } |
5266 | 242 | builder.setBuildPoint(&blocks.merge); |
5267 | 242 | builder.closeLoop(); |
5268 | 242 | return false; |
5269 | 242 | } |
5270 | | |
5271 | | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
5272 | 421 | { |
5273 | 421 | if (node->getExpression()) |
5274 | 234 | node->getExpression()->traverse(this); |
5275 | | |
5276 | 421 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
5277 | | |
5278 | 421 | switch (node->getFlowOp()) { |
5279 | 0 | case glslang::EOpKill: |
5280 | 0 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
5281 | 0 | builder.addCapability(spv::Capability::DemoteToHelperInvocation); |
5282 | 0 | builder.createNoResultOp(spv::Op::OpDemoteToHelperInvocationEXT); |
5283 | 0 | } else { |
5284 | 0 | builder.makeStatementTerminator(spv::Op::OpKill, "post-discard"); |
5285 | 0 | } |
5286 | 0 | break; |
5287 | 0 | case glslang::EOpTerminateInvocation: |
5288 | 0 | builder.addExtension(spv::E_SPV_KHR_terminate_invocation); |
5289 | 0 | builder.makeStatementTerminator(spv::Op::OpTerminateInvocation, "post-terminate-invocation"); |
5290 | 0 | break; |
5291 | 174 | case glslang::EOpBreak: |
5292 | 174 | if (breakForLoop.top()) |
5293 | 0 | builder.createLoopExit(); |
5294 | 174 | else |
5295 | 174 | builder.addSwitchBreak(false); |
5296 | 174 | break; |
5297 | 0 | case glslang::EOpContinue: |
5298 | 0 | builder.createLoopContinue(); |
5299 | 0 | break; |
5300 | 247 | case glslang::EOpReturn: |
5301 | 247 | if (node->getExpression() != nullptr) { |
5302 | 234 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
5303 | 234 | spv::Id returnId = accessChainLoad(glslangReturnType); |
5304 | 234 | if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
5305 | 234 | TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
5306 | 0 | builder.clearAccessChain(); |
5307 | 0 | spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
5308 | 0 | spv::StorageClass::Function, currentFunction->getReturnType()); |
5309 | 0 | builder.setAccessChainLValue(copyId); |
5310 | 0 | multiTypeStore(glslangReturnType, returnId); |
5311 | 0 | returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
5312 | 0 | } |
5313 | 234 | builder.makeReturn(false, returnId); |
5314 | 234 | } else |
5315 | 13 | builder.makeReturn(false); |
5316 | | |
5317 | 247 | builder.clearAccessChain(); |
5318 | 247 | break; |
5319 | | |
5320 | 0 | case glslang::EOpDemote: |
5321 | 0 | builder.createNoResultOp(spv::Op::OpDemoteToHelperInvocationEXT); |
5322 | 0 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
5323 | 0 | builder.addCapability(spv::Capability::DemoteToHelperInvocationEXT); |
5324 | 0 | break; |
5325 | 0 | case glslang::EOpTerminateRayKHR: |
5326 | 0 | builder.makeStatementTerminator(spv::Op::OpTerminateRayKHR, "post-terminateRayKHR"); |
5327 | 0 | break; |
5328 | 0 | case glslang::EOpIgnoreIntersectionKHR: |
5329 | 0 | builder.makeStatementTerminator(spv::Op::OpIgnoreIntersectionKHR, "post-ignoreIntersectionKHR"); |
5330 | 0 | break; |
5331 | | |
5332 | 0 | default: |
5333 | 0 | assert(0); |
5334 | 0 | break; |
5335 | 421 | } |
5336 | | |
5337 | 421 | return false; |
5338 | 421 | } |
5339 | | |
5340 | | bool TGlslangToSpvTraverser::visitVariableDecl(glslang::TVisit visit, glslang::TIntermVariableDecl* node) |
5341 | 0 | { |
5342 | 0 | if (visit == glslang::EvPreVisit) { |
5343 | 0 | builder.setDebugSourceLocation(node->getDeclSymbol()->getLoc().line, node->getDeclSymbol()->getLoc().getFilename()); |
5344 | | // We touch the symbol once here to create the debug info. |
5345 | 0 | getSymbolId(node->getDeclSymbol()); |
5346 | 0 | } |
5347 | |
|
5348 | 0 | return true; |
5349 | 0 | } |
5350 | | |
5351 | | |
5352 | | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
5353 | 4.67k | { |
5354 | | // First, steer off constants, which are not SPIR-V variables, but |
5355 | | // can still have a mapping to a SPIR-V Id. |
5356 | | // This includes specialization constants. |
5357 | 4.67k | if (node->getQualifier().isConstant()) { |
5358 | 611 | spv::Id result = createSpvConstant(*node); |
5359 | 611 | if (result != spv::NoResult) { |
5360 | 611 | auto name = node->getAsSymbolNode()->getAccessName().c_str(); |
5361 | 611 | auto typeId = convertGlslangToSpvType(node->getType()); |
5362 | 611 | builder.createConstVariable(typeId, name, result, currentFunction == nullptr); |
5363 | 611 | return result; |
5364 | 611 | } |
5365 | 611 | } |
5366 | | |
5367 | | // Now, handle actual variables |
5368 | 4.06k | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
5369 | 4.06k | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
5370 | 4.06k | : forcedType; |
5371 | | |
5372 | 4.06k | const bool contains16BitType = node->getType().contains16BitFloat() || |
5373 | 3.89k | node->getType().contains16BitInt(); |
5374 | 4.06k | if (contains16BitType) { |
5375 | 414 | switch (storageClass) { |
5376 | 28 | case spv::StorageClass::Input: |
5377 | 28 | case spv::StorageClass::Output: |
5378 | 28 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
5379 | 28 | builder.addCapability(spv::Capability::StorageInputOutput16); |
5380 | 28 | break; |
5381 | 97 | case spv::StorageClass::Uniform: |
5382 | 97 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
5383 | 97 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
5384 | 37 | builder.addCapability(spv::Capability::StorageUniformBufferBlock16); |
5385 | 60 | else |
5386 | 60 | builder.addCapability(spv::Capability::StorageUniform16); |
5387 | 97 | break; |
5388 | 0 | case spv::StorageClass::PushConstant: |
5389 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
5390 | 0 | builder.addCapability(spv::Capability::StoragePushConstant16); |
5391 | 0 | break; |
5392 | 0 | case spv::StorageClass::StorageBuffer: |
5393 | 0 | case spv::StorageClass::PhysicalStorageBufferEXT: |
5394 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
5395 | 0 | builder.addCapability(spv::Capability::StorageUniformBufferBlock16); |
5396 | 0 | break; |
5397 | 0 | case spv::StorageClass::TileAttachmentQCOM: |
5398 | 0 | builder.addCapability(spv::Capability::TileShadingQCOM); |
5399 | 0 | break; |
5400 | 289 | default: |
5401 | 289 | if (storageClass == spv::StorageClass::Workgroup && |
5402 | 0 | node->getType().getBasicType() == glslang::EbtBlock) { |
5403 | 0 | builder.addCapability(spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR); |
5404 | 0 | break; |
5405 | 0 | } |
5406 | 289 | if (node->getType().contains16BitFloat()) |
5407 | 134 | builder.addCapability(spv::Capability::Float16); |
5408 | 289 | if (node->getType().contains16BitInt()) |
5409 | 155 | builder.addCapability(spv::Capability::Int16); |
5410 | 289 | break; |
5411 | 414 | } |
5412 | 414 | } |
5413 | | |
5414 | 4.06k | if (node->getType().contains8BitInt()) { |
5415 | 103 | if (storageClass == spv::StorageClass::PushConstant) { |
5416 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
5417 | 0 | builder.addCapability(spv::Capability::StoragePushConstant8); |
5418 | 103 | } else if (storageClass == spv::StorageClass::Uniform) { |
5419 | 31 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
5420 | 31 | builder.addCapability(spv::Capability::UniformAndStorageBuffer8BitAccess); |
5421 | 72 | } else if (storageClass == spv::StorageClass::StorageBuffer) { |
5422 | 0 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
5423 | 0 | builder.addCapability(spv::Capability::StorageBuffer8BitAccess); |
5424 | 72 | } else if (storageClass == spv::StorageClass::Workgroup && |
5425 | 0 | node->getType().getBasicType() == glslang::EbtBlock) { |
5426 | 0 | builder.addCapability(spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR); |
5427 | 72 | } else { |
5428 | 72 | builder.addCapability(spv::Capability::Int8); |
5429 | 72 | } |
5430 | 103 | } |
5431 | | |
5432 | 4.06k | const char* name = node->getName().c_str(); |
5433 | 4.06k | if (glslang::IsAnonymous(name)) |
5434 | 152 | name = ""; |
5435 | | |
5436 | 4.06k | spv::Id initializer = spv::NoResult; |
5437 | | |
5438 | 4.06k | if (node->getType().getQualifier().storage == glslang::EvqUniform && !node->getConstArray().empty()) { |
5439 | 0 | int nextConst = 0; |
5440 | 0 | initializer = createSpvConstantFromConstUnionArray(node->getType(), |
5441 | 0 | node->getConstArray(), |
5442 | 0 | nextConst, |
5443 | 0 | false /* specConst */); |
5444 | 4.06k | } else if (node->getType().getQualifier().isNullInit()) { |
5445 | 0 | initializer = builder.makeNullConstant(spvType); |
5446 | 0 | } |
5447 | | |
5448 | 4.06k | spv::Id var = builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer, false); |
5449 | | |
5450 | 4.06k | if (options.emitNonSemanticShaderDebugInfo && storageClass != spv::StorageClass::Function) { |
5451 | | // Create variable alias for retargeted symbols if any. |
5452 | | // Notably, this is only applicable to built-in variables so that it is okay to only use name as the key. |
5453 | 0 | auto [itBegin, itEnd] = glslangIntermediate->getBuiltinAliasLookup().equal_range(name); |
5454 | 0 | for (auto it = itBegin; it != itEnd; ++it) { |
5455 | 0 | builder.createDebugGlobalVariable(builder.getDebugType(spvType), it->second.c_str(), var); |
5456 | 0 | } |
5457 | 0 | } |
5458 | | |
5459 | 4.06k | std::vector<spv::Decoration> topLevelDecorations; |
5460 | 4.06k | glslang::TQualifier typeQualifier = node->getType().getQualifier(); |
5461 | 4.06k | TranslateMemoryDecoration(typeQualifier, topLevelDecorations, glslangIntermediate->usingVulkanMemoryModel()); |
5462 | 4.06k | for (auto deco : topLevelDecorations) { |
5463 | 532 | builder.addDecoration(var, deco); |
5464 | 532 | } |
5465 | 4.06k | return var; |
5466 | 4.06k | } |
5467 | | |
5468 | | // Return type Id of the sampled type. |
5469 | | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
5470 | 1.33k | { |
5471 | 1.33k | switch (sampler.type) { |
5472 | 18 | case glslang::EbtInt: return builder.makeIntType(32); |
5473 | 18 | case glslang::EbtUint: return builder.makeUintType(32); |
5474 | 1.05k | case glslang::EbtFloat: return builder.makeFloatType(32); |
5475 | 72 | case glslang::EbtFloat16: |
5476 | 72 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
5477 | 72 | builder.addCapability(spv::Capability::Float16ImageAMD); |
5478 | 72 | return builder.makeFloatType(16); |
5479 | 87 | case glslang::EbtInt64: |
5480 | 87 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
5481 | 87 | builder.addCapability(spv::Capability::Int64ImageEXT); |
5482 | 87 | return builder.makeIntType(64); |
5483 | 86 | case glslang::EbtUint64: |
5484 | 86 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
5485 | 86 | builder.addCapability(spv::Capability::Int64ImageEXT); |
5486 | 86 | return builder.makeUintType(64); |
5487 | 0 | default: |
5488 | 0 | assert(0); |
5489 | 0 | return builder.makeFloatType(32); |
5490 | 1.33k | } |
5491 | 1.33k | } |
5492 | | |
5493 | | // If node is a swizzle operation, return the type that should be used if |
5494 | | // the swizzle base is first consumed by another operation, before the swizzle |
5495 | | // is applied. |
5496 | | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
5497 | 0 | { |
5498 | 0 | if (node.getAsOperator() && |
5499 | 0 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
5500 | 0 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
5501 | 0 | else |
5502 | 0 | return spv::NoType; |
5503 | 0 | } |
5504 | | |
5505 | | // When inverting a swizzle with a parent op, this function |
5506 | | // will apply the swizzle operation to a completed parent operation. |
5507 | | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
5508 | | spv::Id parentResult) |
5509 | 0 | { |
5510 | 0 | std::vector<unsigned> swizzle; |
5511 | 0 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
5512 | 0 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
5513 | 0 | } |
5514 | | |
5515 | | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
5516 | | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
5517 | 944 | { |
5518 | 944 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
5519 | 3.33k | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
5520 | 2.38k | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
5521 | 944 | } |
5522 | | |
5523 | | // Convert from a glslang type to an SPV type, by calling into a |
5524 | | // recursive version of this function. This establishes the inherited |
5525 | | // layout state rooted from the top-level type. |
5526 | | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
5527 | 31.7k | { |
5528 | 31.7k | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
5529 | 31.7k | } |
5530 | | |
5531 | | spv::LinkageType TGlslangToSpvTraverser::convertGlslangLinkageToSpv(glslang::TLinkType linkType) |
5532 | 355 | { |
5533 | 355 | switch (linkType) { |
5534 | 0 | case glslang::ELinkExport: |
5535 | 0 | return spv::LinkageType::Export; |
5536 | 355 | default: |
5537 | 355 | return spv::LinkageType::Max; |
5538 | 355 | } |
5539 | 355 | } |
5540 | | |
5541 | | // Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id. |
5542 | | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
5543 | | // Mutually recursive with convertGlslangStructToSpvType(). |
5544 | | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
5545 | | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
5546 | | bool lastBufferBlockMember, bool forwardReferenceOnly) |
5547 | 33.7k | { |
5548 | 33.7k | spv::Id spvType = spv::NoResult; |
5549 | | |
5550 | 33.7k | switch (type.getBasicType()) { |
5551 | 221 | case glslang::EbtVoid: |
5552 | 221 | spvType = builder.makeVoidType(); |
5553 | 221 | assert (! type.isArray()); |
5554 | 221 | break; |
5555 | 1.38k | case glslang::EbtBool: |
5556 | | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
5557 | | // a 32-bit int where non-0 means true. |
5558 | 1.38k | if (explicitLayout != glslang::ElpNone) |
5559 | 9 | spvType = builder.makeUintType(32); |
5560 | 1.37k | else |
5561 | 1.37k | spvType = builder.makeBoolType(); |
5562 | 1.38k | break; |
5563 | 7.55k | case glslang::EbtInt: |
5564 | 7.55k | spvType = builder.makeIntType(32); |
5565 | 7.55k | break; |
5566 | 2.20k | case glslang::EbtUint: |
5567 | 2.20k | spvType = builder.makeUintType(32); |
5568 | 2.20k | break; |
5569 | 6.90k | case glslang::EbtFloat: |
5570 | 6.90k | spvType = builder.makeFloatType(32); |
5571 | 6.90k | break; |
5572 | 81 | case glslang::EbtDouble: |
5573 | 81 | spvType = builder.makeFloatType(64); |
5574 | 81 | break; |
5575 | 5.19k | case glslang::EbtFloat16: |
5576 | 5.19k | spvType = builder.makeFloatType(16); |
5577 | 5.19k | break; |
5578 | 0 | case glslang::EbtBFloat16: |
5579 | 0 | spvType = builder.makeBFloat16Type(); |
5580 | 0 | break; |
5581 | 0 | case glslang::EbtFloatE5M2: |
5582 | 0 | spvType = builder.makeFloatE5M2Type(); |
5583 | 0 | break; |
5584 | 0 | case glslang::EbtFloatE4M3: |
5585 | 0 | spvType = builder.makeFloatE4M3Type(); |
5586 | 0 | break; |
5587 | 432 | case glslang::EbtInt8: |
5588 | 432 | spvType = builder.makeIntType(8); |
5589 | 432 | break; |
5590 | 461 | case glslang::EbtUint8: |
5591 | 461 | spvType = builder.makeUintType(8); |
5592 | 461 | break; |
5593 | 1.32k | case glslang::EbtInt16: |
5594 | 1.32k | spvType = builder.makeIntType(16); |
5595 | 1.32k | break; |
5596 | 1.59k | case glslang::EbtUint16: |
5597 | 1.59k | spvType = builder.makeUintType(16); |
5598 | 1.59k | break; |
5599 | 1.39k | case glslang::EbtInt64: |
5600 | 1.39k | spvType = builder.makeIntType(64); |
5601 | 1.39k | break; |
5602 | 1.78k | case glslang::EbtUint64: |
5603 | 1.78k | spvType = builder.makeUintType(64); |
5604 | 1.78k | break; |
5605 | 0 | case glslang::EbtAtomicUint: |
5606 | 0 | builder.addCapability(spv::Capability::AtomicStorage); |
5607 | 0 | spvType = builder.makeUintType(32); |
5608 | 0 | break; |
5609 | 10 | case glslang::EbtAccStruct: |
5610 | 10 | switch (glslangIntermediate->getStage()) { |
5611 | 0 | case EShLangRayGen: |
5612 | 0 | case EShLangIntersect: |
5613 | 0 | case EShLangAnyHit: |
5614 | 0 | case EShLangClosestHit: |
5615 | 0 | case EShLangMiss: |
5616 | 0 | case EShLangCallable: |
5617 | | // these all should have the RayTracingNV/KHR capability already |
5618 | 0 | break; |
5619 | 10 | default: |
5620 | 10 | { |
5621 | 10 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
5622 | 10 | if (extensions.find("GL_EXT_ray_query") != extensions.end()) { |
5623 | 10 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
5624 | 10 | builder.addCapability(spv::Capability::RayQueryKHR); |
5625 | 10 | } |
5626 | 10 | } |
5627 | 10 | break; |
5628 | 10 | } |
5629 | 10 | spvType = builder.makeAccelerationStructureType(); |
5630 | 10 | break; |
5631 | 10 | case glslang::EbtRayQuery: |
5632 | 10 | { |
5633 | 10 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
5634 | 10 | if (extensions.find("GL_EXT_ray_query") != extensions.end()) { |
5635 | 10 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
5636 | 10 | builder.addCapability(spv::Capability::RayQueryKHR); |
5637 | 10 | } |
5638 | 10 | spvType = builder.makeRayQueryType(); |
5639 | 10 | } |
5640 | 10 | break; |
5641 | 414 | case glslang::EbtReference: |
5642 | 414 | { |
5643 | | // Make the forward pointer, then recurse to convert the structure type, then |
5644 | | // patch up the forward pointer with a real pointer type. |
5645 | 414 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
5646 | 54 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClass::PhysicalStorageBufferEXT); |
5647 | 54 | forwardPointers[type.getReferentType()] = forwardId; |
5648 | 54 | } |
5649 | 414 | spvType = forwardPointers[type.getReferentType()]; |
5650 | 414 | if (!forwardReferenceOnly) { |
5651 | 387 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
5652 | 387 | builder.makePointerFromForwardPointer(spv::StorageClass::PhysicalStorageBufferEXT, |
5653 | 387 | forwardPointers[type.getReferentType()], |
5654 | 387 | referentType); |
5655 | 387 | } |
5656 | 414 | } |
5657 | 414 | break; |
5658 | 1.44k | case glslang::EbtSampler: |
5659 | 1.44k | { |
5660 | 1.44k | const glslang::TSampler& sampler = type.getSampler(); |
5661 | 1.44k | std::string debugName; |
5662 | | |
5663 | 1.44k | if (sampler.isPureSampler()) { |
5664 | 115 | if (options.emitNonSemanticShaderDebugInfo) { |
5665 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
5666 | 0 | debugName = sampler.getString(); |
5667 | 0 | } |
5668 | 0 | else { |
5669 | 0 | debugName = "type.sampler"; |
5670 | 0 | } |
5671 | 0 | } |
5672 | 115 | spvType = builder.makeSamplerType(debugName.c_str()); |
5673 | 1.33k | } else { |
5674 | | // an image is present, make its type |
5675 | 1.33k | if (options.emitNonSemanticShaderDebugInfo) { |
5676 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
5677 | 0 | debugName = sampler.removeCombined().getString(); |
5678 | 0 | } |
5679 | 0 | else { |
5680 | 0 | switch (sampler.dim) { |
5681 | 0 | case glslang::Esd1D: debugName = "type.1d.image"; break; |
5682 | 0 | case glslang::Esd2D: debugName = "type.2d.image"; break; |
5683 | 0 | case glslang::Esd3D: debugName = "type.3d.image"; break; |
5684 | 0 | case glslang::EsdCube: debugName = "type.cube.image"; break; |
5685 | 0 | default: debugName = "type.image"; break; |
5686 | 0 | } |
5687 | 0 | } |
5688 | 0 | } |
5689 | 1.33k | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
5690 | 1.33k | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
5691 | 1.33k | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type), debugName.c_str()); |
5692 | 1.33k | if (sampler.isCombined() && |
5693 | 503 | (!sampler.isBuffer() || glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6)) { |
5694 | | // Already has both image and sampler, make the combined type. Only combine sampler to |
5695 | | // buffer if before SPIR-V 1.6. |
5696 | 503 | if (options.emitNonSemanticShaderDebugInfo) { |
5697 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
5698 | 0 | debugName = sampler.getString(); |
5699 | 0 | } |
5700 | 0 | else { |
5701 | 0 | debugName = "type.sampled.image"; |
5702 | 0 | } |
5703 | 0 | } |
5704 | 503 | spvType = builder.makeSampledImageType(spvType, debugName.c_str()); |
5705 | 503 | } |
5706 | 1.33k | } |
5707 | 1.44k | } |
5708 | 1.44k | break; |
5709 | 1.44k | case glslang::EbtStruct: |
5710 | 1.31k | case glslang::EbtBlock: |
5711 | 1.31k | { |
5712 | | // If it's an inner declared heap buffer or uniform block, using OpTypeBufferEXT |
5713 | | // to convert it to an untyped type. |
5714 | 1.31k | if (type.getQualifier().isUniformOrBuffer() && |
5715 | 829 | type.getQualifier().layoutDescriptorHeap && type.getQualifier().layoutDescriptorInnerBlock) { |
5716 | 0 | spvType = builder.makeUntypedPointer(TranslateStorageClass(type), true); |
5717 | 0 | break; |
5718 | 0 | } |
5719 | | |
5720 | | // If we've seen this struct type, return it |
5721 | 1.31k | const glslang::TTypeList* glslangMembers = type.getStruct(); |
5722 | | |
5723 | | // Try to share structs for different layouts, but not yet for other |
5724 | | // kinds of qualification (primarily not yet including interpolant qualification). |
5725 | 1.31k | if (! HasNonLayoutQualifiers(type, qualifier)) |
5726 | 1.31k | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
5727 | 1.31k | if (spvType != spv::NoResult) |
5728 | 753 | break; |
5729 | | |
5730 | | // else, we haven't seen it... |
5731 | 559 | if (type.getBasicType() == glslang::EbtBlock) |
5732 | 377 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
5733 | 559 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
5734 | 559 | } |
5735 | 0 | break; |
5736 | 0 | case glslang::EbtString: |
5737 | | // no type used for OpString |
5738 | 0 | return 0; |
5739 | | |
5740 | 0 | case glslang::EbtHitObjectNV: { |
5741 | 0 | builder.addExtension(spv::E_SPV_NV_shader_invocation_reorder); |
5742 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
5743 | 0 | spvType = builder.makeHitObjectNVType(); |
5744 | 0 | } |
5745 | 0 | break; |
5746 | | |
5747 | 0 | case glslang::EbtHitObjectEXT: { |
5748 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_invocation_reorder); |
5749 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderEXT); |
5750 | 0 | spvType = builder.makeHitObjectEXTType(); |
5751 | 0 | } |
5752 | 0 | break; |
5753 | 0 | case glslang::EbtSpirvType: { |
5754 | | // GL_EXT_spirv_intrinsics |
5755 | 0 | const auto& spirvType = type.getSpirvType(); |
5756 | 0 | const auto& spirvInst = spirvType.spirvInst; |
5757 | |
|
5758 | 0 | std::vector<spv::IdImmediate> operands; |
5759 | 0 | for (const auto& typeParam : spirvType.typeParams) { |
5760 | 0 | if (typeParam.getAsConstant() != nullptr) { |
5761 | | // Constant expression |
5762 | 0 | auto constant = typeParam.getAsConstant(); |
5763 | 0 | if (constant->isLiteral()) { |
5764 | 0 | if (constant->getBasicType() == glslang::EbtFloat) { |
5765 | 0 | float floatValue = static_cast<float>(constant->getConstArray()[0].getDConst()); |
5766 | 0 | unsigned literal; |
5767 | 0 | static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)"); |
5768 | 0 | memcpy(&literal, &floatValue, sizeof(literal)); |
5769 | 0 | operands.push_back({false, literal}); |
5770 | 0 | } else if (constant->getBasicType() == glslang::EbtInt) { |
5771 | 0 | unsigned literal = constant->getConstArray()[0].getIConst(); |
5772 | 0 | operands.push_back({false, literal}); |
5773 | 0 | } else if (constant->getBasicType() == glslang::EbtUint) { |
5774 | 0 | unsigned literal = constant->getConstArray()[0].getUConst(); |
5775 | 0 | operands.push_back({false, literal}); |
5776 | 0 | } else if (constant->getBasicType() == glslang::EbtBool) { |
5777 | 0 | unsigned literal = constant->getConstArray()[0].getBConst(); |
5778 | 0 | operands.push_back({false, literal}); |
5779 | 0 | } else if (constant->getBasicType() == glslang::EbtString) { |
5780 | 0 | auto str = constant->getConstArray()[0].getSConst()->c_str(); |
5781 | 0 | unsigned literal = 0; |
5782 | 0 | char* literalPtr = reinterpret_cast<char*>(&literal); |
5783 | 0 | unsigned charCount = 0; |
5784 | 0 | char ch = 0; |
5785 | 0 | do { |
5786 | 0 | ch = *(str++); |
5787 | 0 | *(literalPtr++) = ch; |
5788 | 0 | ++charCount; |
5789 | 0 | if (charCount == 4) { |
5790 | 0 | operands.push_back({false, literal}); |
5791 | 0 | literalPtr = reinterpret_cast<char*>(&literal); |
5792 | 0 | charCount = 0; |
5793 | 0 | } |
5794 | 0 | } while (ch != 0); |
5795 | | |
5796 | | // Partial literal is padded with 0 |
5797 | 0 | if (charCount > 0) { |
5798 | 0 | for (; charCount < 4; ++charCount) |
5799 | 0 | *(literalPtr++) = 0; |
5800 | 0 | operands.push_back({false, literal}); |
5801 | 0 | } |
5802 | 0 | } else |
5803 | 0 | assert(0); // Unexpected type |
5804 | 0 | } else |
5805 | 0 | operands.push_back({true, createSpvConstant(*constant)}); |
5806 | 0 | } else { |
5807 | | // Type specifier |
5808 | 0 | assert(typeParam.getAsType() != nullptr); |
5809 | 0 | operands.push_back({true, convertGlslangToSpvType(*typeParam.getAsType())}); |
5810 | 0 | } |
5811 | 0 | } |
5812 | |
|
5813 | 0 | assert(spirvInst.set == ""); // Currently, couldn't be extended instructions. |
5814 | 0 | spvType = builder.makeGenericType(static_cast<spv::Op>(spirvInst.id), operands); |
5815 | |
|
5816 | 0 | break; |
5817 | 1.31k | } |
5818 | 0 | case glslang::EbtTensorLayoutNV: |
5819 | 0 | { |
5820 | 0 | builder.addCapability(spv::Capability::TensorAddressingNV); |
5821 | 0 | builder.addExtension(spv::E_SPV_NV_tensor_addressing); |
5822 | |
|
5823 | 0 | std::vector<spv::IdImmediate> operands; |
5824 | 0 | for (uint32_t i = 0; i < 2; ++i) { |
5825 | 0 | operands.push_back({true, makeArraySizeId(*type.getTypeParameters()->arraySizes, i, true)}); |
5826 | 0 | } |
5827 | 0 | spvType = builder.makeGenericType(spv::Op::OpTypeTensorLayoutNV, operands); |
5828 | 0 | break; |
5829 | 1.31k | } |
5830 | 0 | case glslang::EbtTensorViewNV: |
5831 | 0 | { |
5832 | 0 | builder.addCapability(spv::Capability::TensorAddressingNV); |
5833 | 0 | builder.addExtension(spv::E_SPV_NV_tensor_addressing); |
5834 | |
|
5835 | 0 | uint32_t dim = type.getTypeParameters()->arraySizes->getDimSize(0); |
5836 | 0 | assert(dim >= 1 && dim <= 5); |
5837 | 0 | std::vector<spv::IdImmediate> operands; |
5838 | 0 | for (uint32_t i = 0; i < dim + 2; ++i) { |
5839 | 0 | operands.push_back({true, makeArraySizeId(*type.getTypeParameters()->arraySizes, i, true, i==1)}); |
5840 | 0 | } |
5841 | 0 | spvType = builder.makeGenericType(spv::Op::OpTypeTensorViewNV, operands); |
5842 | 0 | break; |
5843 | 1.31k | } |
5844 | 0 | default: |
5845 | 0 | assert(0); |
5846 | 0 | break; |
5847 | 33.7k | } |
5848 | | |
5849 | 33.7k | if (type.isMatrix()) |
5850 | 416 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
5851 | 33.3k | else { |
5852 | | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
5853 | 33.3k | if (type.getVectorSize() > 1) |
5854 | 16.8k | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
5855 | 33.3k | } |
5856 | | |
5857 | 33.7k | if (type.isCoopMatNV()) { |
5858 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixNV); |
5859 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
5860 | |
|
5861 | 0 | if (type.getBasicType() == glslang::EbtFloat16) |
5862 | 0 | builder.addCapability(spv::Capability::Float16); |
5863 | 0 | if (type.getBasicType() == glslang::EbtUint8 || |
5864 | 0 | type.getBasicType() == glslang::EbtInt8) { |
5865 | 0 | builder.addCapability(spv::Capability::Int8); |
5866 | 0 | } |
5867 | |
|
5868 | 0 | spv::Id scope = makeArraySizeId(*type.getTypeParameters()->arraySizes, 1); |
5869 | 0 | spv::Id rows = makeArraySizeId(*type.getTypeParameters()->arraySizes, 2); |
5870 | 0 | spv::Id cols = makeArraySizeId(*type.getTypeParameters()->arraySizes, 3); |
5871 | |
|
5872 | 0 | spvType = builder.makeCooperativeMatrixTypeNV(spvType, scope, rows, cols); |
5873 | 0 | } |
5874 | | |
5875 | 33.7k | if (type.isCoopMatKHR()) { |
5876 | 0 | builder.addCapability(spv::Capability::CooperativeMatrixKHR); |
5877 | 0 | builder.addExtension(spv::E_SPV_KHR_cooperative_matrix); |
5878 | |
|
5879 | 0 | if (type.getBasicType() == glslang::EbtBFloat16) { |
5880 | 0 | builder.addExtension(spv::E_SPV_KHR_bfloat16); |
5881 | 0 | builder.addCapability(spv::Capability::BFloat16CooperativeMatrixKHR); |
5882 | 0 | } |
5883 | |
|
5884 | 0 | if (type.getBasicType() == glslang::EbtFloatE5M2 || type.getBasicType() == glslang::EbtFloatE4M3) { |
5885 | 0 | builder.addExtension(spv::E_SPV_EXT_float8); |
5886 | 0 | builder.addCapability(spv::Capability::Float8CooperativeMatrixEXT); |
5887 | 0 | } |
5888 | |
|
5889 | 0 | if (type.getBasicType() == glslang::EbtFloat16) |
5890 | 0 | builder.addCapability(spv::Capability::Float16); |
5891 | 0 | if (type.getBasicType() == glslang::EbtUint8 || type.getBasicType() == glslang::EbtInt8) { |
5892 | 0 | builder.addCapability(spv::Capability::Int8); |
5893 | 0 | } |
5894 | |
|
5895 | 0 | spv::Id scope = makeArraySizeId(*type.getTypeParameters()->arraySizes, 0); |
5896 | 0 | spv::Id rows = makeArraySizeId(*type.getTypeParameters()->arraySizes, 1); |
5897 | 0 | spv::Id cols = makeArraySizeId(*type.getTypeParameters()->arraySizes, 2); |
5898 | 0 | spv::Id use = makeArraySizeId(*type.getTypeParameters()->arraySizes, 3, true); |
5899 | |
|
5900 | 0 | spvType = builder.makeCooperativeMatrixTypeKHR(spvType, scope, rows, cols, use); |
5901 | 0 | } |
5902 | 33.7k | else if (type.isTensorARM()) { |
5903 | 0 | builder.addCapability(spv::Capability::TensorsARM); |
5904 | 0 | builder.addExtension(spv::E_SPV_ARM_tensors); |
5905 | 0 | if (type.getBasicType() == glslang::EbtInt8 || type.getBasicType() == glslang::EbtUint8) { |
5906 | 0 | builder.addCapability(spv::Capability::Int8); |
5907 | 0 | } else if (type.getBasicType() == glslang::EbtInt16 || |
5908 | 0 | type.getBasicType() == glslang::EbtUint16) { |
5909 | 0 | builder.addCapability(spv::Capability::Int16); |
5910 | 0 | } else if (type.getBasicType() == glslang::EbtInt64 || |
5911 | 0 | type.getBasicType() == glslang::EbtUint64) { |
5912 | 0 | builder.addCapability(spv::Capability::Int64); |
5913 | 0 | } else if (type.getBasicType() == glslang::EbtFloat16) { |
5914 | 0 | builder.addCapability(spv::Capability::Float16); |
5915 | 0 | } |
5916 | |
|
5917 | 0 | spv::Id rank = makeArraySizeId(*type.getTypeParameters()->arraySizes, 0); |
5918 | |
|
5919 | 0 | spvType = builder.makeTensorTypeARM(spvType, rank); |
5920 | 0 | } |
5921 | | |
5922 | 33.7k | if (type.isCoopVecNV()) { |
5923 | 0 | builder.addCapability(spv::Capability::CooperativeVectorNV); |
5924 | 0 | builder.addExtension(spv::E_SPV_NV_cooperative_vector); |
5925 | |
|
5926 | 0 | if (type.getBasicType() == glslang::EbtFloat16) |
5927 | 0 | builder.addCapability(spv::Capability::Float16); |
5928 | 0 | if (type.getBasicType() == glslang::EbtUint8 || type.getBasicType() == glslang::EbtInt8) { |
5929 | 0 | builder.addCapability(spv::Capability::Int8); |
5930 | 0 | } |
5931 | |
|
5932 | 0 | spv::Id components = makeArraySizeId(*type.getTypeParameters()->arraySizes, 0); |
5933 | |
|
5934 | 0 | spvType = builder.makeCooperativeVectorTypeNV(spvType, components); |
5935 | 0 | } |
5936 | | |
5937 | 33.7k | if (type.isLongVector()) { |
5938 | | // SPIR-V LongVectorEXT not needed when component count is literal 2–4. |
5939 | 0 | const bool needLongVectorCap = type.hasSpecConstantVectorComponents() || |
5940 | 0 | (type.getTypeParameters()->arraySizes->getDimSize(0) < 2 || |
5941 | 0 | type.getTypeParameters()->arraySizes->getDimSize(0) > 4); |
5942 | 0 | if (needLongVectorCap) { |
5943 | 0 | builder.addCapability(spv::Capability::LongVectorEXT); |
5944 | 0 | builder.addExtension(spv::E_SPV_EXT_long_vector); |
5945 | 0 | } |
5946 | |
|
5947 | 0 | if (type.getBasicType() == glslang::EbtFloat16) |
5948 | 0 | builder.addCapability(spv::Capability::Float16); |
5949 | 0 | if (type.getBasicType() == glslang::EbtUint8 || type.getBasicType() == glslang::EbtInt8) { |
5950 | 0 | builder.addCapability(spv::Capability::Int8); |
5951 | 0 | } |
5952 | |
|
5953 | 0 | if (type.hasSpecConstantVectorComponents()) { |
5954 | 0 | spv::Id components = makeArraySizeId(*type.getTypeParameters()->arraySizes, 0); |
5955 | 0 | spvType = builder.makeCooperativeVectorTypeNV(spvType, components); |
5956 | 0 | } else { |
5957 | 0 | spvType = builder.makeVectorType(spvType, type.getTypeParameters()->arraySizes->getDimSize(0)); |
5958 | 0 | } |
5959 | 0 | } |
5960 | | |
5961 | 33.7k | if (type.isArray()) { |
5962 | 385 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
5963 | | |
5964 | | // Do all but the outer dimension |
5965 | 385 | if (type.getArraySizes()->getNumDims() > 1) { |
5966 | | // We need to decorate array strides for types needing explicit layout, except blocks. |
5967 | 0 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
5968 | | // Use a dummy glslang type for querying internal strides of |
5969 | | // arrays of arrays, but using just a one-dimensional array. |
5970 | 0 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
5971 | 0 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
5972 | 0 | simpleArrayType.getArraySizes()->dereference(); |
5973 | | |
5974 | | // Will compute the higher-order strides here, rather than making a whole |
5975 | | // pile of types and doing repetitive recursion on their contents. |
5976 | 0 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
5977 | 0 | } |
5978 | | |
5979 | | // make the arrays |
5980 | 0 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
5981 | 0 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
5982 | 0 | if (stride > 0) |
5983 | 0 | builder.addDecoration(spvType, spv::Decoration::ArrayStride, stride); |
5984 | 0 | stride *= type.getArraySizes()->getDimSize(dim); |
5985 | 0 | } |
5986 | 385 | } else { |
5987 | | // single-dimensional array, and don't yet have stride |
5988 | | |
5989 | | // We need to decorate array strides for types needing explicit layout, except blocks. |
5990 | 385 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
5991 | 133 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
5992 | 385 | } |
5993 | | |
5994 | 385 | if (type.getQualifier().storage == glslang::EvqResourceHeap || |
5995 | 385 | type.getQualifier().storage == glslang::EvqSamplerHeap) { |
5996 | 0 | auto structSize = heapStructureTypeSize[spvType]; |
5997 | 0 | spvType = builder.makeRuntimeArray(spvType); |
5998 | 0 | builder.addDecorationId(spvType, spv::Decoration::ArrayStrideIdEXT, structSize); |
5999 | 0 | } |
6000 | | // Do the outer dimension, which might not be known for a runtime-sized array. |
6001 | | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
6002 | 385 | else if (type.isSizedArray()) |
6003 | 361 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
6004 | 24 | else { |
6005 | | // If we see an runtime array in a buffer_reference, it is not a descriptor |
6006 | 24 | if (!lastBufferBlockMember && type.getBasicType() != glslang::EbtReference) { |
6007 | 0 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
6008 | 0 | builder.addCapability(spv::Capability::RuntimeDescriptorArrayEXT); |
6009 | 0 | } |
6010 | 24 | spvType = builder.makeRuntimeArray(spvType); |
6011 | 24 | } |
6012 | 385 | if (stride > 0) |
6013 | 133 | builder.addDecoration(spvType, spv::Decoration::ArrayStride, stride); |
6014 | 385 | } |
6015 | | |
6016 | 33.7k | return spvType; |
6017 | 33.7k | } |
6018 | | |
6019 | | // Apply SPIR-V decorations to the SPIR-V object (provided by SPIR-V ID). If member index is provided, the |
6020 | | // decorations are applied to this member. |
6021 | | void TGlslangToSpvTraverser::applySpirvDecorate(const glslang::TType& type, spv::Id id, std::optional<int> member) |
6022 | 144 | { |
6023 | 144 | assert(type.getQualifier().hasSpirvDecorate()); |
6024 | | |
6025 | 144 | const glslang::TSpirvDecorate& spirvDecorate = type.getQualifier().getSpirvDecorate(); |
6026 | | |
6027 | | // Add spirv_decorate |
6028 | 144 | for (auto& decorate : spirvDecorate.decorates) { |
6029 | 144 | if (!decorate.second.empty()) { |
6030 | 126 | std::vector<unsigned> literals; |
6031 | 126 | TranslateLiterals(decorate.second, literals); |
6032 | 126 | if (member.has_value()) |
6033 | 0 | builder.addMemberDecoration(id, *member, static_cast<spv::Decoration>(decorate.first), literals); |
6034 | 126 | else |
6035 | 126 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first), literals); |
6036 | 126 | } else { |
6037 | 18 | if (member.has_value()) |
6038 | 0 | builder.addMemberDecoration(id, *member, static_cast<spv::Decoration>(decorate.first)); |
6039 | 18 | else |
6040 | 18 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first)); |
6041 | 18 | } |
6042 | 144 | } |
6043 | | |
6044 | | // Add spirv_decorate_id |
6045 | 144 | if (member.has_value()) { |
6046 | | // spirv_decorate_id not applied to members |
6047 | 0 | assert(spirvDecorate.decorateIds.empty()); |
6048 | 144 | } else { |
6049 | 144 | for (auto& decorateId : spirvDecorate.decorateIds) { |
6050 | 0 | std::vector<spv::Id> operandIds; |
6051 | 0 | assert(!decorateId.second.empty()); |
6052 | 0 | for (auto extraOperand : decorateId.second) { |
6053 | 0 | if (extraOperand->getQualifier().isFrontEndConstant()) |
6054 | 0 | operandIds.push_back(createSpvConstant(*extraOperand)); |
6055 | 0 | else |
6056 | 0 | operandIds.push_back(getSymbolId(extraOperand->getAsSymbolNode())); |
6057 | 0 | } |
6058 | 0 | builder.addDecorationId(id, static_cast<spv::Decoration>(decorateId.first), operandIds); |
6059 | 0 | } |
6060 | 144 | } |
6061 | | |
6062 | | // Add spirv_decorate_string |
6063 | 144 | for (auto& decorateString : spirvDecorate.decorateStrings) { |
6064 | 0 | std::vector<const char*> strings; |
6065 | 0 | assert(!decorateString.second.empty()); |
6066 | 0 | for (auto extraOperand : decorateString.second) { |
6067 | 0 | const char* string = extraOperand->getConstArray()[0].getSConst()->c_str(); |
6068 | 0 | strings.push_back(string); |
6069 | 0 | } |
6070 | 0 | if (member.has_value()) |
6071 | 0 | builder.addMemberDecoration(id, *member, static_cast<spv::Decoration>(decorateString.first), strings); |
6072 | 0 | else |
6073 | 0 | builder.addDecoration(id, static_cast<spv::Decoration>(decorateString.first), strings); |
6074 | 0 | } |
6075 | 144 | } |
6076 | | |
6077 | | // TODO: this functionality should exist at a higher level, in creating the AST |
6078 | | // |
6079 | | // Identify interface members that don't have their required extension turned on. |
6080 | | // |
6081 | | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
6082 | 2.76k | { |
6083 | 2.76k | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
6084 | | |
6085 | 2.76k | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
6086 | 0 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
6087 | 0 | return true; |
6088 | 2.76k | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
6089 | 0 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
6090 | 0 | return true; |
6091 | | |
6092 | 2.76k | if (glslangIntermediate->getStage() == EShLangMesh) { |
6093 | 0 | if (member.getFieldName() == "gl_PrimitiveShadingRateEXT" && |
6094 | 0 | extensions.find("GL_EXT_fragment_shading_rate") == extensions.end()) |
6095 | 0 | return true; |
6096 | 0 | } |
6097 | | |
6098 | 2.76k | if (glslangIntermediate->getStage() != EShLangMesh) { |
6099 | 2.76k | if (member.getFieldName() == "gl_ViewportMask" && |
6100 | 0 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
6101 | 0 | return true; |
6102 | 2.76k | if (member.getFieldName() == "gl_PositionPerViewNV" && |
6103 | 0 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
6104 | 0 | return true; |
6105 | 2.76k | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
6106 | 0 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
6107 | 0 | return true; |
6108 | 2.76k | } |
6109 | | |
6110 | 2.76k | return false; |
6111 | 2.76k | } |
6112 | | |
6113 | | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
6114 | | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
6115 | | // Mutually recursive with convertGlslangToSpvType(). |
6116 | | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
6117 | | const glslang::TTypeList* glslangMembers, |
6118 | | glslang::TLayoutPacking explicitLayout, |
6119 | | const glslang::TQualifier& qualifier) |
6120 | 559 | { |
6121 | | // Create a vector of struct types for SPIR-V to consume |
6122 | 559 | std::vector<spv::Id> spvMembers; |
6123 | 559 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
6124 | | // except sometimes for blocks |
6125 | 559 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
6126 | 559 | std::vector<spv::StructMemberDebugInfo> memberDebugInfo; |
6127 | 2.54k | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
6128 | 1.98k | auto& glslangMember = (*glslangMembers)[i]; |
6129 | 1.98k | if (glslangMember.type->hiddenMember()) { |
6130 | 0 | ++memberDelta; |
6131 | 0 | if (type.getBasicType() == glslang::EbtBlock) |
6132 | 0 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
6133 | 1.98k | } else { |
6134 | 1.98k | if (type.getBasicType() == glslang::EbtBlock) { |
6135 | 1.38k | if (filterMember(*glslangMember.type)) { |
6136 | 0 | memberDelta++; |
6137 | 0 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
6138 | 0 | continue; |
6139 | 0 | } |
6140 | 1.38k | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
6141 | 1.38k | } |
6142 | | // modify just this child's view of the qualifier |
6143 | 1.98k | glslang::TQualifier memberQualifier = glslangMember.type->getQualifier(); |
6144 | 1.98k | InheritQualifiers(memberQualifier, qualifier); |
6145 | | |
6146 | | // manually inherit location |
6147 | 1.98k | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
6148 | 0 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
6149 | | |
6150 | | // recurse |
6151 | 1.98k | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
6152 | 520 | i == (int)glslangMembers->size() - 1; |
6153 | | |
6154 | | // Make forward pointers for any pointer members. |
6155 | 1.98k | if (glslangMember.type->isReference() && |
6156 | 27 | forwardPointers.find(glslangMember.type->getReferentType()) == forwardPointers.end()) { |
6157 | 27 | deferredForwardPointers.push_back(std::make_pair(glslangMember.type, memberQualifier)); |
6158 | 27 | } |
6159 | | |
6160 | | // Create the member type. |
6161 | 1.98k | auto const spvMember = convertGlslangToSpvType(*glslangMember.type, explicitLayout, memberQualifier, lastBufferBlockMember, |
6162 | 1.98k | glslangMember.type->isReference()); |
6163 | 1.98k | spvMembers.push_back(spvMember); |
6164 | | |
6165 | | // Update the builder with the type's location so that we can create debug types for the structure members. |
6166 | | // There doesn't exist a "clean" entry point for this information to be passed along to the builder so, for now, |
6167 | | // it is stored in the builder and consumed during the construction of composite debug types. |
6168 | | // TODO: This probably warrants further investigation. This approach was decided to be the least ugly of the |
6169 | | // quick and dirty approaches that were tried. |
6170 | | // Advantages of this approach: |
6171 | | // + Relatively clean. No direct calls into debug type system. |
6172 | | // + Handles nested recursive structures. |
6173 | | // Disadvantages of this approach: |
6174 | | // + Not as clean as desired. Traverser queries/sets persistent state. This is fragile. |
6175 | | // + Table lookup during creation of composite debug types. This really shouldn't be necessary. |
6176 | 1.98k | if(options.emitNonSemanticShaderDebugInfo) { |
6177 | 0 | spv::StructMemberDebugInfo debugInfo{}; |
6178 | 0 | debugInfo.name = glslangMember.type->getFieldName(); |
6179 | 0 | debugInfo.line = glslangMember.loc.line; |
6180 | 0 | debugInfo.column = glslangMember.loc.column; |
6181 | | |
6182 | | // Per the GLSL spec, bool variables inside of a uniform or buffer block are generated as uint. |
6183 | | // But for debug info, we want to represent them as bool because that is the original type in |
6184 | | // the source code. The bool type can be nested within a vector or a multidimensional array, |
6185 | | // so we must construct the chain of types up from the scalar bool. |
6186 | 0 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl && explicitLayout != glslang::ElpNone && |
6187 | 0 | glslangMember.type->getBasicType() == glslang::EbtBool) { |
6188 | 0 | auto typeId = builder.makeBoolType(); |
6189 | 0 | if (glslangMember.type->isVector()) { |
6190 | 0 | typeId = builder.makeVectorType(typeId, glslangMember.type->getVectorSize()); |
6191 | 0 | } |
6192 | 0 | if (glslangMember.type->isArray()) { |
6193 | 0 | const auto* arraySizes = glslangMember.type->getArraySizes(); |
6194 | 0 | int dims = arraySizes->getNumDims(); |
6195 | 0 | for (int i = dims - 1; i >= 0; --i) { |
6196 | 0 | spv::Id size = builder.makeIntConstant(arraySizes->getDimSize(i)); |
6197 | 0 | typeId = builder.makeArrayType(typeId, size, 0); |
6198 | 0 | } |
6199 | 0 | } |
6200 | 0 | debugInfo.debugTypeOverride = builder.getDebugType(typeId); |
6201 | 0 | } else if (glslangMember.type->getQualifier().builtIn != glslang::EbvNone) { |
6202 | | // TODO - The built-in currently are not provide the correct line/column and spirv-val will validate these when using shaderDebugInfo |
6203 | | // |
6204 | | // There is a larger issue because even defining the builtIn such as |
6205 | | // |
6206 | | // out gl_PerVertex { |
6207 | | // vec4 gl_Position; |
6208 | | // }; |
6209 | | // |
6210 | | // in the shader also doesn't produce the correct line/column |
6211 | | // So for now, provide zero, as that is a valid value here |
6212 | 0 | debugInfo.line = 0; |
6213 | 0 | debugInfo.column = 0; |
6214 | 0 | } |
6215 | |
|
6216 | 0 | memberDebugInfo.push_back(debugInfo); |
6217 | 0 | } |
6218 | 1.98k | } |
6219 | 1.98k | } |
6220 | | |
6221 | | // For those whose storage is ResourceHeap or SamplerHeap, we needs to recursively add |
6222 | | // member decorator for inner structure member declaration with spec Constant. |
6223 | 559 | spv::Id heapStructSize = 0; |
6224 | 559 | if (qualifier.storage == glslang::EvqResourceHeap || qualifier.storage == glslang::EvqSamplerHeap || |
6225 | 559 | qualifier.layoutDescriptorInnerBlock) { |
6226 | 0 | auto memberBaseOffset = builder.makeUintConstant(0); |
6227 | 0 | int maxPlainDataTypeAlignment = 0; |
6228 | 0 | spv::Id descTypeMaxAlignment = 0; |
6229 | 0 | auto structLastMemberSize = |
6230 | 0 | decorateDescHeapType(type, memberBaseOffset, descTypeMaxAlignment, maxPlainDataTypeAlignment); |
6231 | 0 | auto structureUnaligned = builder.createSpecConstantOp( |
6232 | 0 | spv::Op::OpIAdd, builder.makeUintType(32), {memberBaseOffset, structLastMemberSize}, {}); |
6233 | 0 | auto structurePadding = builder.createSpecConstantOp( |
6234 | 0 | spv::Op::OpUMod, builder.makeUintType(32), {descTypeMaxAlignment, structureUnaligned}, {}); |
6235 | 0 | heapStructSize = builder.createSpecConstantOp( |
6236 | 0 | spv::Op::OpIAdd, builder.makeUintType(32), {structureUnaligned, structurePadding}, {}); |
6237 | 0 | HeapMetaData meta = {heapStructSize, descTypeMaxAlignment, maxPlainDataTypeAlignment}; |
6238 | 0 | heapStructureTypeMetaData[&type] = meta; |
6239 | 0 | } |
6240 | | |
6241 | | // Make the SPIR-V type |
6242 | 559 | spv::Id spvType = builder.makeStructType(spvMembers, memberDebugInfo, type.getTypeName().c_str(), false); |
6243 | 559 | if (! HasNonLayoutQualifiers(type, qualifier)) |
6244 | 559 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
6245 | | |
6246 | | // Keep decoration data order after spv structure type. |
6247 | 559 | if (qualifier.storage == glslang::EvqResourceHeap || qualifier.storage == glslang::EvqSamplerHeap || |
6248 | 559 | qualifier.layoutDescriptorInnerBlock) { |
6249 | 0 | heapStructureTypeSize[spvType] = heapStructSize; |
6250 | 0 | for (unsigned int i = 0; i < spvMembers.size(); i++) { |
6251 | 0 | builder.addMemberDecorationIdEXT(spvType, i, spv::Decoration::OffsetIdEXT, {heapStructureMemberOffsets[i]}); |
6252 | 0 | } |
6253 | 0 | heapStructureMemberOffsets.clear(); |
6254 | 0 | } |
6255 | | |
6256 | | // Decorate it |
6257 | 559 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType, spvMembers); |
6258 | | |
6259 | 586 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
6260 | 27 | auto it = deferredForwardPointers[i]; |
6261 | 27 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
6262 | 27 | } |
6263 | | |
6264 | 559 | return spvType; |
6265 | 559 | } |
6266 | | |
6267 | | // Return not aligned size of current type. |
6268 | | spv::Id TGlslangToSpvTraverser::decorateDescHeapType( |
6269 | | const glslang::TType& type, |
6270 | | spv::Id &memberBaseOffset, |
6271 | | spv::Id &descTypeAlignment, |
6272 | | int& maxPlainDataAlignment) |
6273 | 0 | { |
6274 | 0 | glslang::TLayoutPacking explicitLayout = glslang::ElpScalar; |
6275 | 0 | auto selectMaxRsrcTyAlign = [&](spv::Id typeSize) { |
6276 | 0 | if (descTypeAlignment != 0) { |
6277 | 0 | auto compareInst = builder.createSpecConstantOp(spv::Op::OpUGreaterThan, builder.makeBoolType(), |
6278 | 0 | {descTypeAlignment, typeSize}, {}); |
6279 | 0 | auto selectMaxAlign = builder.createSpecConstantOp(spv::Op::OpSelect, builder.makeUintType(32), |
6280 | 0 | {compareInst, descTypeAlignment, typeSize}, {}); |
6281 | 0 | descTypeAlignment = selectMaxAlign; |
6282 | 0 | } else |
6283 | 0 | descTypeAlignment = typeSize; |
6284 | 0 | }; |
6285 | | |
6286 | | // Get or calculate following info ordered. |
6287 | | // Member Type | Type size | Type alignment within current structure | Type offset. |
6288 | | // PS. resource blocks declared within a structure will not be processed as a normal declaration before, |
6289 | | // and its' size is dependent on constantSizeOfEXT op, so we won't trigger it as a normal structure here. |
6290 | 0 | bool isArray = type.isArray(); |
6291 | 0 | if (type.isStruct() && !type.getQualifier().layoutDescriptorInnerBlock) { |
6292 | | // 1. Structure type is processed from inner to higher level. |
6293 | | // If member is a sturcture, it will be processed earlier, |
6294 | | // parent structure could just load early results. |
6295 | 0 | if (heapStructureTypeMetaData.find(&type) != heapStructureTypeMetaData.end()) { |
6296 | 0 | auto metadata = heapStructureTypeMetaData[&type]; |
6297 | 0 | selectMaxRsrcTyAlign(metadata.maxRsrcTypeAlignment); |
6298 | 0 | maxPlainDataAlignment = std::max(metadata.maxPlainDataAlignment, maxPlainDataAlignment); |
6299 | 0 | return metadata.typeStride; |
6300 | 0 | } |
6301 | | // For other nested structure, it would be declared before its parent, |
6302 | | // so it should be directly returned a result in above check. |
6303 | 0 | const glslang::TTypeList* structTyList = type.getStruct(); |
6304 | 0 | spv::Id previousTypeSize = 0; |
6305 | 0 | auto currentOffset = memberBaseOffset; |
6306 | 0 | for (int i = 0; i < (int)structTyList->size(); i++) { |
6307 | | // Get offset : |
6308 | | // OffsetsBase = default ? 0 : OffsetInLayout; |
6309 | | // OffsetsBase = OffsetsBase + previousOffset + previousPadding. |
6310 | 0 | const glslang::TType& memberTy = *(*structTyList)[i].type; |
6311 | 0 | spv::Id typeSize = decorateDescHeapType(memberTy, currentOffset, descTypeAlignment, maxPlainDataAlignment); |
6312 | | // 2. Get each member's unaligned offset, padding and aligned offset. |
6313 | | // As this function is processed recursively, from bottom to upper, so in here, |
6314 | | // non-structure member's alignment should have been known. |
6315 | 0 | if (i != 0) { |
6316 | 0 | auto offsetNotAligned = builder.createSpecConstantOp(spv::Op::OpIAdd, builder.makeUintType(32), |
6317 | 0 | {currentOffset, previousTypeSize}, {}); |
6318 | 0 | auto offsetPadding = builder.createSpecConstantOp(spv::Op::OpUMod, builder.makeUintType(32), |
6319 | 0 | {typeSize, offsetNotAligned}, {}); |
6320 | 0 | currentOffset = builder.createSpecConstantOp(spv::Op::OpIAdd, builder.makeUintType(32), |
6321 | 0 | {offsetNotAligned, offsetPadding}, {}); |
6322 | 0 | } |
6323 | 0 | heapStructureMemberOffsets.push_back(currentOffset); |
6324 | 0 | previousTypeSize = typeSize; |
6325 | 0 | } |
6326 | | // 3. Structure level, get max resource type's alignment, max plain data alignment and final |
6327 | | // structure inner alignment. |
6328 | | // Get compared result for alignment within whole structure. |
6329 | 0 | spv::Id maxPlainAlignmentSize = builder.makeIntConstant(maxPlainDataAlignment); |
6330 | | |
6331 | | // Select biggest alignemnt size between desc types and plain old types. |
6332 | 0 | selectMaxRsrcTyAlign(maxPlainAlignmentSize); |
6333 | 0 | memberBaseOffset = currentOffset; |
6334 | | |
6335 | | // Now, return generates size of the MaterialData type |
6336 | | // Get structure level unaligned offset, padding and final offset outside. |
6337 | 0 | return previousTypeSize; |
6338 | 0 | } |
6339 | | |
6340 | | // Following are normal types, structures/blocks have been processed in above part. |
6341 | | // Normal type or member type within a structure. |
6342 | 0 | bool useConstantSizeOf = |
6343 | 0 | ((type.getQualifier().isUniformOrBuffer() && type.getQualifier().layoutDescriptorHeap) || |
6344 | 0 | type.isImage() || type.isTexture() || type.isTensorARM() || type.getBasicType() == glslang::EbtAccStruct); |
6345 | | |
6346 | | // Get single type and layout info. |
6347 | 0 | int elemCurrentOffset, elemAlignedSize; |
6348 | 0 | int memberSize, dummyStride, typeAlignment; |
6349 | 0 | spv::Id spvType = 0; |
6350 | 0 | if (isArray) { |
6351 | 0 | glslang::TType elemTy(type, 0); |
6352 | 0 | elemTy.clearArraySizes(); |
6353 | 0 | if (!useConstantSizeOf) { |
6354 | 0 | typeAlignment = glslangIntermediate->getMemberAlignment( |
6355 | 0 | elemTy, memberSize, dummyStride, glslang::ElpScalar, elemTy.getQualifier().layoutMatrix == glslang::ElmRowMajor); |
6356 | 0 | updateMemberOffset(elemTy, elemTy, elemCurrentOffset, elemAlignedSize, explicitLayout, elemTy.getQualifier().layoutMatrix); |
6357 | 0 | elemAlignedSize *= type.getCumulativeArraySize(); |
6358 | 0 | } |
6359 | 0 | spvType = convertGlslangToSpvType(elemTy); |
6360 | 0 | } else { |
6361 | 0 | if (!useConstantSizeOf) { |
6362 | 0 | typeAlignment = glslangIntermediate->getMemberAlignment( |
6363 | 0 | type, memberSize, dummyStride, glslang::ElpScalar, type.getQualifier().layoutMatrix == glslang::ElmRowMajor); |
6364 | 0 | updateMemberOffset(type, type, elemCurrentOffset, elemAlignedSize, explicitLayout, type.getQualifier().layoutMatrix); |
6365 | 0 | } |
6366 | 0 | spvType = convertGlslangToSpvType(type); |
6367 | 0 | } |
6368 | | |
6369 | | // Get alignment and type size. |
6370 | 0 | spv::Id typeSize = 0; |
6371 | 0 | if (useConstantSizeOf) { |
6372 | 0 | typeSize = builder.createConstantSizeOfEXT(spvType); |
6373 | | // For resource type, needs to check current's largest alignment to select. |
6374 | | // New parent structure's alignment is equal to: |
6375 | | // maxInnerAlignment = currentAlignment > maxInnerAlignment |
6376 | | // ? currentAlignment |
6377 | | // : maxInnerAlignment; |
6378 | 0 | selectMaxRsrcTyAlign(typeSize); |
6379 | 0 | } else { |
6380 | 0 | typeSize = builder.makeIntConstant(elemAlignedSize); |
6381 | 0 | maxPlainDataAlignment = std::max(typeAlignment, maxPlainDataAlignment); |
6382 | 0 | } |
6383 | |
|
6384 | 0 | return typeSize; |
6385 | 0 | } |
6386 | | |
6387 | | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
6388 | | const glslang::TTypeList* glslangMembers, |
6389 | | glslang::TLayoutPacking explicitLayout, |
6390 | | const glslang::TQualifier& qualifier, |
6391 | | spv::Id spvType, |
6392 | | const std::vector<spv::Id>& spvMembers) |
6393 | 559 | { |
6394 | | // Name and decorate the non-hidden members |
6395 | 559 | int offset = -1; |
6396 | 559 | bool memberLocationInvalid = type.isArrayOfArrays() || |
6397 | 559 | (type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false)); |
6398 | 2.54k | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
6399 | 1.98k | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
6400 | 1.98k | int member = i; |
6401 | 1.98k | if (type.getBasicType() == glslang::EbtBlock) { |
6402 | 1.38k | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
6403 | 1.38k | if (filterMember(glslangMember)) |
6404 | 0 | continue; |
6405 | 1.38k | } |
6406 | | |
6407 | | // modify just this child's view of the qualifier |
6408 | 1.98k | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
6409 | 1.98k | InheritQualifiers(memberQualifier, qualifier); |
6410 | | |
6411 | | // using -1 above to indicate a hidden member |
6412 | 1.98k | if (member < 0) |
6413 | 0 | continue; |
6414 | | |
6415 | 1.98k | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
6416 | 1.98k | builder.addMemberDecoration(spvType, member, |
6417 | 1.98k | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
6418 | 1.98k | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
6419 | | // Add interpolation and auxiliary storage decorations only to |
6420 | | // top-level members of Input and Output storage classes |
6421 | 1.98k | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
6422 | 1.98k | type.getQualifier().storage == glslang::EvqVaryingOut) { |
6423 | 0 | if (type.getBasicType() == glslang::EbtBlock || |
6424 | 0 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
6425 | 0 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
6426 | 0 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
6427 | 0 | addMeshNVDecoration(spvType, member, memberQualifier); |
6428 | 0 | } |
6429 | 0 | } |
6430 | 1.98k | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
6431 | | |
6432 | 1.98k | if (type.getBasicType() == glslang::EbtBlock && |
6433 | 1.38k | qualifier.storage == glslang::EvqBuffer) { |
6434 | | // Add memory decorations only to top-level members of shader storage block |
6435 | 380 | std::vector<spv::Decoration> memory; |
6436 | 380 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
6437 | 380 | for (unsigned int i = 0; i < memory.size(); ++i) |
6438 | 0 | builder.addMemberDecoration(spvType, member, memory[i]); |
6439 | 380 | } |
6440 | | |
6441 | | // Location assignment was already completed correctly by the front end, |
6442 | | // just track whether a member needs to be decorated. |
6443 | | // Ignore member locations if the container is an array, as that's |
6444 | | // ill-specified and decisions have been made to not allow this. |
6445 | 1.98k | if (!memberLocationInvalid && memberQualifier.hasLocation()) |
6446 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::Location, memberQualifier.layoutLocation); |
6447 | | |
6448 | | // component, XFB, others |
6449 | 1.98k | if (glslangMember.getQualifier().hasComponent()) |
6450 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::Component, |
6451 | 0 | glslangMember.getQualifier().layoutComponent); |
6452 | 1.98k | if (glslangMember.getQualifier().hasXfbOffset()) |
6453 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::Offset, |
6454 | 0 | glslangMember.getQualifier().layoutXfbOffset); |
6455 | 1.98k | else if (glslangMember.getQualifier().hasMemberOffset()) { |
6456 | 0 | builder.addExtension(spv::E_SPV_NV_push_constant_bank); |
6457 | 0 | builder.addCapability(spv::Capability::PushConstantBanksNV); |
6458 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::MemberOffsetNV, |
6459 | 0 | glslangMember.getQualifier().layoutMemberOffset); |
6460 | 1.98k | } else if (explicitLayout != glslang::ElpNone) { |
6461 | | // figure out what to do with offset, which is accumulating |
6462 | 1.64k | int nextOffset; |
6463 | 1.64k | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
6464 | 1.64k | if (offset >= 0) |
6465 | 1.64k | builder.addMemberDecoration(spvType, member, spv::Decoration::Offset, offset); |
6466 | 1.64k | offset = nextOffset; |
6467 | 1.64k | } |
6468 | | |
6469 | 1.98k | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
6470 | 42 | builder.addMemberDecoration(spvType, member, spv::Decoration::MatrixStride, |
6471 | 42 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
6472 | | |
6473 | | // built-in variable decorations |
6474 | 1.98k | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
6475 | | // built-in decoration is used to detect AST nodes, but won't be decorated on member variables. |
6476 | 1.98k | if (builtIn != spv::BuiltIn::Max && |
6477 | 0 | builtIn != spv::BuiltIn::ResourceHeapEXT && builtIn != spv::BuiltIn::SamplerHeapEXT) |
6478 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::BuiltIn, (int)builtIn); |
6479 | | |
6480 | | // nonuniform |
6481 | 1.98k | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
6482 | | |
6483 | 1.98k | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
6484 | 0 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
6485 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::HlslSemanticGOOGLE, |
6486 | 0 | memberQualifier.semanticName); |
6487 | 0 | } |
6488 | | |
6489 | 1.98k | if (builtIn == spv::BuiltIn::Layer) { |
6490 | | // SPV_NV_viewport_array2 extension |
6491 | 0 | if (glslangMember.getQualifier().layoutViewportRelative){ |
6492 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::ViewportRelativeNV); |
6493 | 0 | builder.addCapability(spv::Capability::ShaderViewportMaskNV); |
6494 | 0 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
6495 | 0 | } |
6496 | 0 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
6497 | 0 | builder.addMemberDecoration(spvType, member, |
6498 | 0 | spv::Decoration::SecondaryViewportRelativeNV, |
6499 | 0 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
6500 | 0 | builder.addCapability(spv::Capability::ShaderStereoViewNV); |
6501 | 0 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
6502 | 0 | } |
6503 | 0 | } |
6504 | 1.98k | if (glslangMember.getQualifier().layoutPassthrough) { |
6505 | 0 | builder.addMemberDecoration(spvType, member, spv::Decoration::PassthroughNV); |
6506 | 0 | builder.addCapability(spv::Capability::GeometryShaderPassthroughNV); |
6507 | 0 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
6508 | 0 | } |
6509 | | |
6510 | | // Add SPIR-V decorations (GL_EXT_spirv_intrinsics) |
6511 | 1.98k | if (glslangMember.getQualifier().hasSpirvDecorate()) |
6512 | 0 | applySpirvDecorate(glslangMember, spvType, member); |
6513 | 1.98k | } |
6514 | | |
6515 | | // Decorate the structure |
6516 | 559 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
6517 | 559 | const auto basicType = type.getBasicType(); |
6518 | 559 | const auto typeStorageQualifier = type.getQualifier().storage; |
6519 | 559 | if (basicType == glslang::EbtBlock) { |
6520 | 377 | builder.addDecoration(spvType, TranslateBlockDecoration(typeStorageQualifier, glslangIntermediate->usingStorageBuffer())); |
6521 | 377 | } else if (basicType == glslang::EbtStruct && glslangIntermediate->getSpv().vulkan > 0) { |
6522 | 182 | const auto hasRuntimeArray = !spvMembers.empty() && builder.getOpCode(spvMembers.back()) == spv::Op::OpTypeRuntimeArray; |
6523 | 182 | if (hasRuntimeArray) { |
6524 | 0 | builder.addDecoration(spvType, TranslateBlockDecoration(typeStorageQualifier, glslangIntermediate->usingStorageBuffer())); |
6525 | 0 | } |
6526 | 182 | } |
6527 | | |
6528 | 559 | if (qualifier.hasHitObjectShaderRecordNV()) |
6529 | 0 | builder.addDecoration(spvType, spv::Decoration::HitObjectShaderRecordBufferNV); |
6530 | | |
6531 | 559 | if (qualifier.hasBank()) { |
6532 | 0 | builder.addExtension(spv::E_SPV_NV_push_constant_bank); |
6533 | 0 | builder.addCapability(spv::Capability::PushConstantBanksNV); |
6534 | 0 | builder.addDecoration(spvType, spv::Decoration::BankNV, qualifier.layoutBank); |
6535 | 0 | } |
6536 | | |
6537 | 559 | if (qualifier.hasHitObjectShaderRecordEXT()) |
6538 | 0 | builder.addDecoration(spvType, spv::Decoration::HitObjectShaderRecordBufferEXT); |
6539 | 559 | } |
6540 | | |
6541 | | // Turn the expression forming the array size into an id. |
6542 | | // This is not quite trivial, because of specialization constants. |
6543 | | // Sometimes, a raw constant is turned into an Id, and sometimes |
6544 | | // a specialization constant expression is. |
6545 | | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim, bool allowZero, bool boolType) |
6546 | 361 | { |
6547 | | // First, see if this is sized with a node, meaning a specialization constant: |
6548 | 361 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
6549 | 361 | if (specNode != nullptr) { |
6550 | 0 | builder.clearAccessChain(); |
6551 | 0 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
6552 | 0 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
6553 | 0 | specNode->traverse(this); |
6554 | 0 | return accessChainLoad(specNode->getAsTyped()->getType()); |
6555 | 0 | } |
6556 | | |
6557 | | // Otherwise, need a compile-time (front end) size, get it: |
6558 | 361 | int size = arraySizes.getDimSize(dim); |
6559 | | |
6560 | 361 | if (!allowZero) |
6561 | 361 | assert(size > 0); |
6562 | | |
6563 | 361 | if (boolType) { |
6564 | 0 | return builder.makeBoolConstant(size); |
6565 | 361 | } else { |
6566 | 361 | return builder.makeUintConstant(size); |
6567 | 361 | } |
6568 | 361 | } |
6569 | | |
6570 | | // Wrap the builder's accessChainLoad to: |
6571 | | // - localize handling of RelaxedPrecision |
6572 | | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
6573 | | // (avoids unnecessary work and possible type punning for structures) |
6574 | | // - do conversion of concrete to abstract type |
6575 | | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
6576 | 31.4k | { |
6577 | 31.4k | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
6578 | | |
6579 | 31.4k | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
6580 | 31.4k | coherentFlags |= TranslateCoherent(type); |
6581 | | |
6582 | 31.4k | spv::MemoryAccessMask accessMask = spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMask::MakePointerAvailableKHR); |
6583 | | // If the value being loaded is HelperInvocation, SPIR-V 1.6 is being generated (so that |
6584 | | // SPV_EXT_demote_to_helper_invocation is in core) and the memory model is in use, add |
6585 | | // the Volatile MemoryAccess semantic. |
6586 | 31.4k | if (type.getQualifier().builtIn == glslang::EbvHelperInvocation && |
6587 | 0 | glslangIntermediate->usingVulkanMemoryModel() && |
6588 | 0 | glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
6589 | 0 | accessMask = spv::MemoryAccessMask(accessMask | spv::MemoryAccessMask::Volatile); |
6590 | 0 | } |
6591 | | |
6592 | 31.4k | unsigned int alignment = builder.getAccessChain().alignment; |
6593 | 31.4k | alignment |= type.getBufferReferenceAlignment(); |
6594 | | |
6595 | 31.4k | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
6596 | 31.4k | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
6597 | 31.4k | TranslateNonUniformDecoration(type.getQualifier()), |
6598 | 31.4k | nominalTypeId, |
6599 | 31.4k | accessMask, |
6600 | 31.4k | TranslateMemoryScope(coherentFlags), |
6601 | 31.4k | alignment); |
6602 | | |
6603 | | // Need to convert to abstract types when necessary |
6604 | 31.4k | if (type.getBasicType() == glslang::EbtBool) { |
6605 | 1.18k | loadedId = convertLoadedBoolInUniformToUint(type, nominalTypeId, loadedId); |
6606 | 1.18k | } |
6607 | | |
6608 | 31.4k | if (!builder.getAccessChain().descHeapInfo.descHeapInstId.empty()) { |
6609 | 0 | for (auto heapInst : builder.getAccessChain().descHeapInfo.descHeapInstId) |
6610 | 0 | heapInst->setTypeId(convertGlslangToSpvType(type)); |
6611 | 0 | builder.clearAccessChain(); |
6612 | 0 | } |
6613 | | |
6614 | 31.4k | return loadedId; |
6615 | 31.4k | } |
6616 | | |
6617 | | // Wrap the builder's accessChainStore to: |
6618 | | // - do conversion of concrete to abstract type |
6619 | | // |
6620 | | // Implicitly uses the existing builder.accessChain as the storage target. |
6621 | | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
6622 | 6.96k | { |
6623 | | // Need to convert to abstract types when necessary |
6624 | 6.96k | if (type.getBasicType() == glslang::EbtBool) { |
6625 | 254 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
6626 | | |
6627 | 254 | if (builder.isScalarType(nominalTypeId)) { |
6628 | | // Conversion for bool |
6629 | 137 | spv::Id boolType = builder.makeBoolType(); |
6630 | 137 | if (nominalTypeId != boolType) { |
6631 | | // keep these outside arguments, for determinant order-of-evaluation |
6632 | 0 | spv::Id one = builder.makeUintConstant(1); |
6633 | 0 | spv::Id zero = builder.makeUintConstant(0); |
6634 | 0 | rvalue = builder.createTriOp(spv::Op::OpSelect, nominalTypeId, rvalue, one, zero); |
6635 | 137 | } else if (builder.getTypeId(rvalue) != boolType) |
6636 | 0 | rvalue = builder.createBinOp(spv::Op::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
6637 | 137 | } else if (builder.isVectorType(nominalTypeId)) { |
6638 | | // Conversion for bvec |
6639 | 117 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
6640 | 117 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
6641 | 117 | if (nominalTypeId != bvecType) { |
6642 | | // keep these outside arguments, for determinant order-of-evaluation |
6643 | 0 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
6644 | 0 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
6645 | 0 | rvalue = builder.createTriOp(spv::Op::OpSelect, nominalTypeId, rvalue, one, zero); |
6646 | 117 | } else if (builder.getTypeId(rvalue) != bvecType) |
6647 | 0 | rvalue = builder.createBinOp(spv::Op::OpINotEqual, bvecType, rvalue, |
6648 | 0 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
6649 | 117 | } |
6650 | 254 | } |
6651 | | |
6652 | 6.96k | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
6653 | 6.96k | coherentFlags |= TranslateCoherent(type); |
6654 | | |
6655 | 6.96k | unsigned int alignment = builder.getAccessChain().alignment; |
6656 | 6.96k | alignment |= type.getBufferReferenceAlignment(); |
6657 | | |
6658 | 6.96k | builder.accessChainStore(rvalue, TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
6659 | 6.96k | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
6660 | 6.96k | ~spv::MemoryAccessMask::MakePointerVisibleKHR), |
6661 | 6.96k | TranslateMemoryScope(coherentFlags), alignment); |
6662 | 6.96k | } |
6663 | | |
6664 | | // For storing when types match at the glslang level, but not might match at the |
6665 | | // SPIR-V level. |
6666 | | // |
6667 | | // This especially happens when a single glslang type expands to multiple |
6668 | | // SPIR-V types, like a struct that is used in a member-undecorated way as well |
6669 | | // as in a member-decorated way. |
6670 | | // |
6671 | | // NOTE: This function can handle any store request; if it's not special it |
6672 | | // simplifies to a simple OpStore. |
6673 | | // |
6674 | | // Implicitly uses the existing builder.accessChain as the storage target. |
6675 | | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
6676 | 6.96k | { |
6677 | | // we only do the complex path here if it's an aggregate |
6678 | 6.96k | if (! type.isStruct() && ! type.isArray()) { |
6679 | 6.78k | accessChainStore(type, rValue); |
6680 | 6.78k | return; |
6681 | 6.78k | } |
6682 | | |
6683 | | // and, it has to be a case of type aliasing |
6684 | 183 | spv::Id rType = builder.getTypeId(rValue); |
6685 | 183 | spv::Id lValue = builder.accessChainGetLValue(); |
6686 | 183 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
6687 | 183 | if (lType == rType) { |
6688 | 181 | accessChainStore(type, rValue); |
6689 | 181 | return; |
6690 | 181 | } |
6691 | | |
6692 | | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
6693 | | // where the two types were the same type in GLSL. This requires member |
6694 | | // by member copy, recursively. |
6695 | | |
6696 | | // SPIR-V 1.4 added an instruction to do help do this. |
6697 | 2 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
6698 | | // However, bool in uniform space is changed to int, so |
6699 | | // OpCopyLogical does not work for that. |
6700 | | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
6701 | 0 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::Op::OpTypeBool, 0); |
6702 | 0 | bool lBool = builder.containsType(lType, spv::Op::OpTypeBool, 0); |
6703 | 0 | if (lBool == rBool) { |
6704 | 0 | spv::Id logicalCopy = builder.createUnaryOp(spv::Op::OpCopyLogical, lType, rValue); |
6705 | 0 | accessChainStore(type, logicalCopy); |
6706 | 0 | return; |
6707 | 0 | } |
6708 | 0 | } |
6709 | | |
6710 | | // If an array, copy element by element. |
6711 | 2 | if (type.isArray()) { |
6712 | 0 | glslang::TType glslangElementType(type, 0); |
6713 | 0 | spv::Id elementRType = builder.getContainedTypeId(rType); |
6714 | 0 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
6715 | | // get the source member |
6716 | 0 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
6717 | | |
6718 | | // set up the target storage |
6719 | 0 | builder.clearAccessChain(); |
6720 | 0 | builder.setAccessChainLValue(lValue); |
6721 | 0 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
6722 | 0 | type.getBufferReferenceAlignment()); |
6723 | | |
6724 | | // store the member |
6725 | 0 | multiTypeStore(glslangElementType, elementRValue); |
6726 | 0 | } |
6727 | 2 | } else { |
6728 | 2 | assert(type.isStruct()); |
6729 | | |
6730 | | // loop over structure members |
6731 | 2 | const glslang::TTypeList& members = *type.getStruct(); |
6732 | 4 | for (int m = 0; m < (int)members.size(); ++m) { |
6733 | 2 | const glslang::TType& glslangMemberType = *members[m].type; |
6734 | | |
6735 | | // get the source member |
6736 | 2 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
6737 | 2 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
6738 | | |
6739 | | // set up the target storage |
6740 | 2 | builder.clearAccessChain(); |
6741 | 2 | builder.setAccessChainLValue(lValue); |
6742 | 2 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
6743 | 2 | type.getBufferReferenceAlignment()); |
6744 | | |
6745 | | // store the member |
6746 | 2 | multiTypeStore(glslangMemberType, memberRValue); |
6747 | 2 | } |
6748 | 2 | } |
6749 | 2 | } |
6750 | | |
6751 | | // Decide whether or not this type should be |
6752 | | // decorated with offsets and strides, and if so |
6753 | | // whether std140 or std430 rules should be applied. |
6754 | | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
6755 | 31.7k | { |
6756 | | // has to be a block |
6757 | 31.7k | if (type.getBasicType() != glslang::EbtBlock) |
6758 | 31.0k | return glslang::ElpNone; |
6759 | | |
6760 | | // has to be a uniform or buffer block or task in/out blocks |
6761 | 710 | if (type.getQualifier().storage != glslang::EvqUniform && |
6762 | 490 | type.getQualifier().storage != glslang::EvqBuffer && |
6763 | 0 | type.getQualifier().storage != glslang::EvqShared && |
6764 | 0 | !type.getQualifier().isTaskMemory()) |
6765 | 0 | return glslang::ElpNone; |
6766 | | |
6767 | | // return the layout to use |
6768 | 710 | switch (type.getQualifier().layoutPacking) { |
6769 | 220 | case glslang::ElpStd140: |
6770 | 710 | case glslang::ElpStd430: |
6771 | 710 | case glslang::ElpScalar: |
6772 | 710 | return type.getQualifier().layoutPacking; |
6773 | 0 | default: |
6774 | 0 | return glslang::ElpNone; |
6775 | 710 | } |
6776 | 710 | } |
6777 | | |
6778 | | // Given an array type, returns the integer stride required for that array |
6779 | | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
6780 | | glslang::TLayoutMatrix matrixLayout) |
6781 | 133 | { |
6782 | 133 | int size; |
6783 | 133 | int stride; |
6784 | 133 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
6785 | 133 | matrixLayout == glslang::ElmRowMajor); |
6786 | | |
6787 | 133 | return stride; |
6788 | 133 | } |
6789 | | |
6790 | | // Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix |
6791 | | // when used as a member of an interface block |
6792 | | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
6793 | | glslang::TLayoutMatrix matrixLayout) |
6794 | 42 | { |
6795 | 42 | glslang::TType elementType; |
6796 | 42 | elementType.shallowCopy(matrixType); |
6797 | 42 | elementType.clearArraySizes(); |
6798 | | |
6799 | 42 | int size; |
6800 | 42 | int stride; |
6801 | 42 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
6802 | 42 | matrixLayout == glslang::ElmRowMajor); |
6803 | | |
6804 | 42 | return stride; |
6805 | 42 | } |
6806 | | |
6807 | | // Given a member type of a struct, realign the current offset for it, and compute |
6808 | | // the next (not yet aligned) offset for the next member, which will get aligned |
6809 | | // on the next call. |
6810 | | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
6811 | | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
6812 | | // -1 means a non-forced member offset (no decoration needed). |
6813 | | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
6814 | | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
6815 | 1.64k | { |
6816 | | // this will get a positive value when deemed necessary |
6817 | 1.64k | nextOffset = -1; |
6818 | | |
6819 | | // override anything in currentOffset with user-set offset |
6820 | 1.64k | if (memberType.getQualifier().hasOffset()) |
6821 | 1.02k | currentOffset = memberType.getQualifier().layoutOffset; |
6822 | | |
6823 | | // It could be that current linker usage in glslang updated all the layoutOffset, |
6824 | | // in which case the following code does not matter. But, that's not quite right |
6825 | | // once cross-compilation unit GLSL validation is done, as the original user |
6826 | | // settings are needed in layoutOffset, and then the following will come into play. |
6827 | | |
6828 | 1.64k | if (explicitLayout == glslang::ElpNone) { |
6829 | 0 | if (! memberType.getQualifier().hasOffset()) |
6830 | 0 | currentOffset = -1; |
6831 | |
|
6832 | 0 | return; |
6833 | 0 | } |
6834 | | |
6835 | | // Getting this far means we need explicit offsets |
6836 | 1.64k | if (currentOffset < 0) |
6837 | 272 | currentOffset = 0; |
6838 | | |
6839 | | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
6840 | | // but possibly not yet correctly aligned. |
6841 | | |
6842 | 1.64k | int memberSize; |
6843 | 1.64k | int dummyStride; |
6844 | 1.64k | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
6845 | 1.64k | matrixLayout == glslang::ElmRowMajor); |
6846 | | |
6847 | 1.64k | bool isVectorLike = memberType.isVector(); |
6848 | 1.64k | if (memberType.isMatrix()) { |
6849 | 42 | if (matrixLayout == glslang::ElmRowMajor) |
6850 | 10 | isVectorLike = memberType.getMatrixRows() == 1; |
6851 | 32 | else |
6852 | 32 | isVectorLike = memberType.getMatrixCols() == 1; |
6853 | 42 | } |
6854 | | |
6855 | | // Adjust alignment for HLSL rules |
6856 | | // TODO: make this consistent in early phases of code: |
6857 | | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
6858 | | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
6859 | | // which is the most likely to rely on reflection, and least likely to rely implicit layouts |
6860 | 1.64k | if (glslangIntermediate->usingHlslOffsets() && |
6861 | 662 | ! memberType.isStruct() && structType.getTypeName().compare("$Global") != 0) { |
6862 | 552 | int componentSize; |
6863 | 552 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, componentSize); |
6864 | 552 | if (! memberType.isArray() && isVectorLike && componentAlignment <= 4) |
6865 | 246 | memberAlignment = componentAlignment; |
6866 | | |
6867 | | // Don't add unnecessary padding after this member |
6868 | | // (undo std140 bumping size to a mutliple of vec4) |
6869 | 552 | if (explicitLayout == glslang::ElpStd140) { |
6870 | 330 | if (memberType.isMatrix()) { |
6871 | 2 | if (matrixLayout == glslang::ElmRowMajor) |
6872 | 0 | memberSize -= componentSize * (4 - memberType.getMatrixCols()); |
6873 | 2 | else |
6874 | 2 | memberSize -= componentSize * (4 - memberType.getMatrixRows()); |
6875 | 328 | } else if (memberType.isArray()) |
6876 | 44 | memberSize -= componentSize * (4 - memberType.getVectorSize()); |
6877 | 330 | } |
6878 | 552 | } |
6879 | | |
6880 | | // Bump up to member alignment |
6881 | 1.64k | glslang::RoundToPow2(currentOffset, memberAlignment); |
6882 | | |
6883 | | // Bump up to vec4 if there is a bad straddle |
6884 | 1.64k | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
6885 | 1.64k | currentOffset, isVectorLike)) |
6886 | 4 | glslang::RoundToPow2(currentOffset, 16); |
6887 | | |
6888 | 1.64k | nextOffset = currentOffset + memberSize; |
6889 | 1.64k | } |
6890 | | |
6891 | | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
6892 | 3.83k | { |
6893 | 3.83k | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
6894 | 3.83k | switch (glslangBuiltIn) |
6895 | 3.83k | { |
6896 | 0 | case glslang::EbvPointSize: |
6897 | 0 | case glslang::EbvClipDistance: |
6898 | 0 | case glslang::EbvCullDistance: |
6899 | 0 | case glslang::EbvViewportMaskNV: |
6900 | 0 | case glslang::EbvSecondaryPositionNV: |
6901 | 0 | case glslang::EbvSecondaryViewportMaskNV: |
6902 | 0 | case glslang::EbvPositionPerViewNV: |
6903 | 0 | case glslang::EbvViewportMaskPerViewNV: |
6904 | 0 | case glslang::EbvTaskCountNV: |
6905 | 0 | case glslang::EbvPrimitiveCountNV: |
6906 | 0 | case glslang::EbvPrimitiveIndicesNV: |
6907 | 0 | case glslang::EbvClipDistancePerViewNV: |
6908 | 0 | case glslang::EbvCullDistancePerViewNV: |
6909 | 0 | case glslang::EbvLayerPerViewNV: |
6910 | 0 | case glslang::EbvMeshViewCountNV: |
6911 | 0 | case glslang::EbvMeshViewIndicesNV: |
6912 | | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
6913 | | // Alternately, we could just call this for any glslang built-in, since the |
6914 | | // capability already guards against duplicates. |
6915 | 0 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
6916 | 0 | break; |
6917 | 3.83k | default: |
6918 | | // Capabilities were already generated when the struct was declared. |
6919 | 3.83k | break; |
6920 | 3.83k | } |
6921 | 3.83k | } |
6922 | | |
6923 | | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
6924 | 1.45k | { |
6925 | 1.45k | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
6926 | 1.45k | } |
6927 | | |
6928 | | // Does parameter need a place to keep writes, separate from the original? |
6929 | | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
6930 | | // qualifiers such that we should have only in/out/inout/constreadonly here. |
6931 | | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
6932 | 1.42k | { |
6933 | 1.42k | assert(qualifier == glslang::EvqIn || |
6934 | 1.42k | qualifier == glslang::EvqOut || |
6935 | 1.42k | qualifier == glslang::EvqInOut || |
6936 | 1.42k | qualifier == glslang::EvqUniform || |
6937 | 1.42k | qualifier == glslang::EvqConstReadOnly); |
6938 | 1.42k | return qualifier != glslang::EvqConstReadOnly && |
6939 | 1.42k | qualifier != glslang::EvqUniform; |
6940 | 1.42k | } |
6941 | | |
6942 | | // Is parameter pass-by-original? |
6943 | | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
6944 | | bool implicitThisParam) |
6945 | 1.42k | { |
6946 | 1.42k | if (implicitThisParam) // implicit this |
6947 | 0 | return true; |
6948 | 1.42k | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
6949 | 0 | return paramType.getBasicType() == glslang::EbtBlock; |
6950 | 1.42k | return (paramType.containsOpaque() && !glslangIntermediate->getBindlessMode()) || // sampler, etc. |
6951 | 1.42k | paramType.getQualifier().isSpirvByReference() || // spirv_by_reference |
6952 | 1.42k | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
6953 | 1.42k | } |
6954 | | |
6955 | | // Make all the functions, skeletally, without actually visiting their bodies. |
6956 | | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
6957 | 370 | { |
6958 | 370 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
6959 | 370 | bool useVulkanMemoryModel) { |
6960 | 296 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
6961 | 296 | if (paramPrecision != spv::NoPrecision) |
6962 | 0 | decorations.push_back(paramPrecision); |
6963 | 296 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
6964 | 296 | if (type.isReference()) { |
6965 | | // Original and non-writable params pass the pointer directly and |
6966 | | // use restrict/aliased, others are stored to a pointer in Function |
6967 | | // memory and use RestrictPointer/AliasedPointer. |
6968 | 0 | if (originalParam(type.getQualifier().storage, type, false) || |
6969 | 0 | !writableParam(type.getQualifier().storage)) { |
6970 | | // TranslateMemoryDecoration added Restrict decoration already. |
6971 | 0 | if (!type.getQualifier().isRestrict()) { |
6972 | 0 | decorations.push_back(spv::Decoration::Aliased); |
6973 | 0 | } |
6974 | 0 | } else { |
6975 | 0 | decorations.push_back(type.getQualifier().isRestrict() ? spv::Decoration::RestrictPointerEXT : |
6976 | 0 | spv::Decoration::AliasedPointerEXT); |
6977 | 0 | } |
6978 | 0 | } |
6979 | 296 | }; |
6980 | | |
6981 | 1.55k | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
6982 | 1.18k | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
6983 | 1.18k | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction) |
6984 | 463 | continue; |
6985 | | |
6986 | 725 | builder.setDebugSourceLocation(glslFunction->getLoc().line, glslFunction->getLoc().getFilename()); |
6987 | | |
6988 | 725 | if (isShaderEntryPoint(glslFunction)) { |
6989 | | // For HLSL, the entry function is actually a compiler generated function to resolve the difference of |
6990 | | // entry function signature between HLSL and SPIR-V. So we don't emit debug information for that. |
6991 | 370 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl) { |
6992 | 370 | builder.setupFunctionDebugInfo(shaderEntry, glslangIntermediate->getEntryPointMangledName().c_str(), |
6993 | 370 | std::vector<spv::Id>(), // main function has no param |
6994 | 370 | std::vector<char const*>()); |
6995 | 370 | } |
6996 | 370 | continue; |
6997 | 370 | } |
6998 | | // We're on a user function. Set up the basic interface for the function now, |
6999 | | // so that it's available to call. Translating the body will happen later. |
7000 | | // |
7001 | | // Typically (except for a "const in" parameter), an address will be passed to the |
7002 | | // function. What it is an address of varies: |
7003 | | // |
7004 | | // - "in" parameters not marked as "const" can be written to without modifying the calling |
7005 | | // argument so that write needs to be to a copy, hence the address of a copy works. |
7006 | | // |
7007 | | // - "const in" parameters can just be the r-value, as no writes need occur. |
7008 | | // |
7009 | | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
7010 | | // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
7011 | | |
7012 | 355 | std::vector<spv::Id> paramTypes; |
7013 | 355 | std::vector<char const*> paramNames; |
7014 | 355 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
7015 | 355 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
7016 | | |
7017 | 355 | #ifdef ENABLE_HLSL |
7018 | 355 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
7019 | 135 | glslangIntermediate->implicitThisName; |
7020 | | #else |
7021 | | bool implicitThis = false; |
7022 | | #endif |
7023 | | |
7024 | 355 | paramDecorations.resize(parameters.size()); |
7025 | 651 | for (int p = 0; p < (int)parameters.size(); ++p) { |
7026 | 296 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
7027 | 296 | spv::Id typeId = convertGlslangToSpvType(paramType); |
7028 | 296 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
7029 | 0 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
7030 | 296 | else if (writableParam(paramType.getQualifier().storage)) |
7031 | 296 | typeId = builder.makePointer(spv::StorageClass::Function, typeId); |
7032 | 0 | else |
7033 | 0 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
7034 | 296 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
7035 | 296 | paramTypes.push_back(typeId); |
7036 | 296 | } |
7037 | | |
7038 | 355 | for (auto const parameter:parameters) { |
7039 | 296 | paramNames.push_back(parameter->getAsSymbolNode()->getName().c_str()); |
7040 | 296 | } |
7041 | | |
7042 | 355 | spv::Block* functionBlock; |
7043 | 355 | spv::Function* function = builder.makeFunctionEntry( |
7044 | 355 | TranslatePrecisionDecoration(glslFunction->getType()), convertGlslangToSpvType(glslFunction->getType()), |
7045 | 355 | glslFunction->getName().c_str(), convertGlslangLinkageToSpv(glslFunction->getLinkType()), paramTypes, |
7046 | 355 | paramDecorations, &functionBlock); |
7047 | 355 | builder.setupFunctionDebugInfo(function, glslFunction->getName().c_str(), paramTypes, paramNames); |
7048 | 355 | if (implicitThis) |
7049 | 0 | function->setImplicitThis(); |
7050 | | |
7051 | | // Track function to emit/call later |
7052 | 355 | functionMap[glslFunction->getName().c_str()] = function; |
7053 | | |
7054 | | // Set the parameter id's |
7055 | 651 | for (int p = 0; p < (int)parameters.size(); ++p) { |
7056 | 296 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
7057 | | // give a name too |
7058 | 296 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
7059 | | |
7060 | 296 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
7061 | 296 | if (paramType.contains8BitInt()) |
7062 | 0 | builder.addCapability(spv::Capability::Int8); |
7063 | 296 | if (paramType.contains16BitInt()) |
7064 | 0 | builder.addCapability(spv::Capability::Int16); |
7065 | 296 | if (paramType.contains16BitFloat()) |
7066 | 1 | builder.addCapability(spv::Capability::Float16); |
7067 | 296 | } |
7068 | 355 | } |
7069 | 370 | } |
7070 | | |
7071 | | // Process all the initializers, while skipping the functions and link objects |
7072 | | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
7073 | 370 | { |
7074 | 370 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
7075 | 1.55k | for (int i = 0; i < (int)initializers.size(); ++i) { |
7076 | 1.18k | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
7077 | 1.18k | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
7078 | 463 | glslang::EOpLinkerObjects) { |
7079 | | |
7080 | | // We're on a top-level node that's not a function. Treat as an initializer, whose |
7081 | | // code goes into the beginning of the entry point. |
7082 | 93 | initializer->traverse(this); |
7083 | 93 | } |
7084 | 1.18k | } |
7085 | 370 | } |
7086 | | // Walk over all linker objects to create a map for payload and callable data linker objects |
7087 | | // and their location to be used during codegen for OpTraceKHR and OpExecuteCallableKHR |
7088 | | // This is done here since it is possible that these linker objects are not be referenced in the AST |
7089 | | void TGlslangToSpvTraverser::collectRayTracingLinkerObjects() |
7090 | 0 | { |
7091 | 0 | glslang::TIntermAggregate* linkerObjects = glslangIntermediate->findLinkerObjects(); |
7092 | 0 | for (auto& objSeq : linkerObjects->getSequence()) { |
7093 | 0 | auto objNode = objSeq->getAsSymbolNode(); |
7094 | 0 | if (objNode != nullptr) { |
7095 | 0 | if (objNode->getQualifier().hasLocation()) { |
7096 | 0 | unsigned int location = objNode->getQualifier().layoutLocation; |
7097 | 0 | auto st = objNode->getQualifier().storage; |
7098 | 0 | int set; |
7099 | 0 | switch (st) |
7100 | 0 | { |
7101 | 0 | case glslang::EvqPayload: |
7102 | 0 | case glslang::EvqPayloadIn: |
7103 | 0 | set = 0; |
7104 | 0 | break; |
7105 | 0 | case glslang::EvqCallableData: |
7106 | 0 | case glslang::EvqCallableDataIn: |
7107 | 0 | set = 1; |
7108 | 0 | break; |
7109 | | |
7110 | 0 | case glslang::EvqHitObjectAttrNV: |
7111 | 0 | case glslang::EvqHitObjectAttrEXT: |
7112 | 0 | set = 2; |
7113 | 0 | break; |
7114 | | |
7115 | 0 | default: |
7116 | 0 | set = -1; |
7117 | 0 | } |
7118 | 0 | if (set != -1) |
7119 | 0 | locationToSymbol[set].insert(std::make_pair(location, objNode)); |
7120 | 0 | } |
7121 | 0 | } |
7122 | 0 | } |
7123 | 0 | } |
7124 | | // Process all the functions, while skipping initializers. |
7125 | | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
7126 | 370 | { |
7127 | 1.55k | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
7128 | 1.18k | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
7129 | 1.18k | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
7130 | 1.09k | node->traverse(this); |
7131 | 1.18k | } |
7132 | 370 | } |
7133 | | |
7134 | | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
7135 | | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
7136 | 3.86k | { |
7137 | 3.86k | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
7138 | | |
7139 | 3.86k | glslang::TSampler sampler = {}; |
7140 | 3.86k | bool cubeCompare = false; |
7141 | 3.86k | bool f16ShadowCompare = false; |
7142 | 3.86k | if (node.isTexture() || node.isImage()) { |
7143 | 3.18k | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
7144 | 3.18k | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
7145 | 3.18k | f16ShadowCompare = sampler.shadow && |
7146 | 474 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
7147 | 3.18k | } |
7148 | | |
7149 | 16.2k | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
7150 | 12.4k | builder.clearAccessChain(); |
7151 | 12.4k | glslangArguments[i]->traverse(this); |
7152 | | |
7153 | | // Special case l-value operands |
7154 | 12.4k | bool lvalue = false; |
7155 | 12.4k | switch (node.getOp()) { |
7156 | 978 | case glslang::EOpImageAtomicAdd: |
7157 | 1.95k | case glslang::EOpImageAtomicMin: |
7158 | 2.93k | case glslang::EOpImageAtomicMax: |
7159 | 3.11k | case glslang::EOpImageAtomicAnd: |
7160 | 3.31k | case glslang::EOpImageAtomicOr: |
7161 | 3.51k | case glslang::EOpImageAtomicXor: |
7162 | 4.49k | case glslang::EOpImageAtomicExchange: |
7163 | 4.75k | case glslang::EOpImageAtomicCompSwap: |
7164 | 4.85k | case glslang::EOpImageAtomicLoad: |
7165 | 4.98k | case glslang::EOpImageAtomicStore: |
7166 | 4.98k | if (i == 0) |
7167 | 1.42k | lvalue = true; |
7168 | 4.98k | break; |
7169 | 258 | case glslang::EOpSparseImageLoad: |
7170 | 258 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
7171 | 84 | lvalue = true; |
7172 | 258 | break; |
7173 | 147 | case glslang::EOpSparseTexture: |
7174 | 147 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
7175 | 44 | lvalue = true; |
7176 | 147 | break; |
7177 | 158 | case glslang::EOpSparseTextureClamp: |
7178 | 158 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
7179 | 36 | lvalue = true; |
7180 | 158 | break; |
7181 | 97 | case glslang::EOpSparseTextureLod: |
7182 | 216 | case glslang::EOpSparseTextureOffset: |
7183 | 216 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
7184 | 52 | lvalue = true; |
7185 | 216 | break; |
7186 | 69 | case glslang::EOpSparseTextureFetch: |
7187 | 69 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
7188 | 18 | lvalue = true; |
7189 | 69 | break; |
7190 | 57 | case glslang::EOpSparseTextureFetchOffset: |
7191 | 57 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
7192 | 12 | lvalue = true; |
7193 | 57 | break; |
7194 | 81 | case glslang::EOpSparseTextureLodOffset: |
7195 | 285 | case glslang::EOpSparseTextureGrad: |
7196 | 391 | case glslang::EOpSparseTextureOffsetClamp: |
7197 | 391 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
7198 | 76 | lvalue = true; |
7199 | 391 | break; |
7200 | 171 | case glslang::EOpSparseTextureGradOffset: |
7201 | 171 | case glslang::EOpSparseTextureGradClamp: |
7202 | 171 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
7203 | 28 | lvalue = true; |
7204 | 171 | break; |
7205 | 142 | case glslang::EOpSparseTextureGradOffsetClamp: |
7206 | 142 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
7207 | 20 | lvalue = true; |
7208 | 142 | break; |
7209 | 164 | case glslang::EOpSparseTextureGather: |
7210 | 164 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
7211 | 40 | lvalue = true; |
7212 | 164 | break; |
7213 | 122 | case glslang::EOpSparseTextureGatherOffset: |
7214 | 244 | case glslang::EOpSparseTextureGatherOffsets: |
7215 | 244 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
7216 | 48 | lvalue = true; |
7217 | 244 | break; |
7218 | 80 | case glslang::EOpSparseTextureGatherLod: |
7219 | 80 | if (i == 3) |
7220 | 16 | lvalue = true; |
7221 | 80 | break; |
7222 | 48 | case glslang::EOpSparseTextureGatherLodOffset: |
7223 | 96 | case glslang::EOpSparseTextureGatherLodOffsets: |
7224 | 96 | if (i == 4) |
7225 | 16 | lvalue = true; |
7226 | 96 | break; |
7227 | 144 | case glslang::EOpSparseImageLoadLod: |
7228 | 144 | if (i == 3) |
7229 | 36 | lvalue = true; |
7230 | 144 | break; |
7231 | 0 | case glslang::EOpImageSampleFootprintNV: |
7232 | 0 | if (i == 4) |
7233 | 0 | lvalue = true; |
7234 | 0 | break; |
7235 | 0 | case glslang::EOpImageSampleFootprintClampNV: |
7236 | 0 | case glslang::EOpImageSampleFootprintLodNV: |
7237 | 0 | if (i == 5) |
7238 | 0 | lvalue = true; |
7239 | 0 | break; |
7240 | 0 | case glslang::EOpImageSampleFootprintGradNV: |
7241 | 0 | if (i == 6) |
7242 | 0 | lvalue = true; |
7243 | 0 | break; |
7244 | 0 | case glslang::EOpImageSampleFootprintGradClampNV: |
7245 | 0 | if (i == 7) |
7246 | 0 | lvalue = true; |
7247 | 0 | break; |
7248 | 0 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
7249 | 0 | case glslang::EOpRayQueryGetIntersectionLSSPositionsNV: |
7250 | 0 | case glslang::EOpRayQueryGetIntersectionLSSRadiiNV: |
7251 | 0 | if (i == 2) |
7252 | 0 | lvalue = true; |
7253 | 0 | break; |
7254 | 0 | case glslang::EOpConstructSaturated: |
7255 | 0 | if (i == 0) |
7256 | 0 | lvalue = true; |
7257 | 0 | break; |
7258 | 5.08k | default: |
7259 | 5.08k | break; |
7260 | 12.4k | } |
7261 | | |
7262 | 12.4k | if (lvalue) { |
7263 | 1.95k | spv::Id lvalue_id = builder.accessChainGetLValue(); |
7264 | 1.95k | arguments.push_back(lvalue_id); |
7265 | 1.95k | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
7266 | 1.95k | builder.addDecoration(lvalue_id, TranslateNonUniformDecoration(lvalueCoherentFlags)); |
7267 | 1.95k | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
7268 | 10.4k | } else { |
7269 | 10.4k | if (i > 0 && |
7270 | 8.01k | glslangArguments[i]->getAsSymbolNode() && glslangArguments[i-1]->getAsSymbolNode() && |
7271 | 2.50k | glslangArguments[i]->getAsSymbolNode()->getId() == glslangArguments[i-1]->getAsSymbolNode()->getId()) { |
7272 | | // Reuse the id if possible |
7273 | 304 | arguments.push_back(arguments[i-1]); |
7274 | 10.1k | } else { |
7275 | 10.1k | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
7276 | 10.1k | } |
7277 | 10.4k | } |
7278 | 12.4k | } |
7279 | 3.86k | } |
7280 | | |
7281 | | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
7282 | 63 | { |
7283 | 63 | builder.clearAccessChain(); |
7284 | 63 | node.getOperand()->traverse(this); |
7285 | 63 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
7286 | 63 | } |
7287 | | |
7288 | | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
7289 | 17.7k | { |
7290 | 17.7k | if (! node->isImage() && ! node->isTexture()) |
7291 | 14.5k | return spv::NoResult; |
7292 | | |
7293 | 3.25k | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
7294 | | |
7295 | | // Process a GLSL texturing op (will be SPV image) |
7296 | | |
7297 | 3.25k | const glslang::TType &imageType = node->getAsAggregate() |
7298 | 3.25k | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
7299 | 3.25k | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
7300 | 3.25k | const glslang::TSampler sampler = imageType.getSampler(); |
7301 | 3.25k | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
7302 | 3.25k | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
7303 | 3.25k | : false; |
7304 | | |
7305 | 3.25k | const auto signExtensionMask = [&]() { |
7306 | 1.66k | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
7307 | 0 | if (sampler.type == glslang::EbtUint) |
7308 | 0 | return spv::ImageOperandsMask::ZeroExtend; |
7309 | 0 | else if (sampler.type == glslang::EbtInt) |
7310 | 0 | return spv::ImageOperandsMask::SignExtend; |
7311 | 0 | } |
7312 | 1.66k | return spv::ImageOperandsMask::MaskNone; |
7313 | 1.66k | }; |
7314 | | |
7315 | 3.25k | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
7316 | | |
7317 | 3.25k | std::vector<spv::Id> arguments; |
7318 | 3.25k | if (node->getAsAggregate()) |
7319 | 3.18k | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
7320 | 63 | else |
7321 | 63 | translateArguments(*node->getAsUnaryNode(), arguments); |
7322 | 3.25k | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
7323 | | |
7324 | 3.25k | spv::Builder::TextureParameters params = { }; |
7325 | 3.25k | params.sampler = arguments[0]; |
7326 | | |
7327 | 3.25k | glslang::TCrackedTextureOp cracked; |
7328 | 3.25k | node->crackTexture(sampler, cracked); |
7329 | | |
7330 | 3.25k | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
7331 | | |
7332 | 3.25k | if (builder.isSampledImage(params.sampler) && |
7333 | 1.43k | ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
7334 | 186 | params.sampler = builder.createUnaryOp(spv::Op::OpImage, builder.getImageType(params.sampler), params.sampler); |
7335 | 186 | if (imageType.getQualifier().isNonUniform()) { |
7336 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
7337 | 0 | if (extensions.find("GL_EXT_descriptor_heap") == extensions.end()) { |
7338 | 0 | builder.addDecoration(params.sampler, spv::Decoration::NonUniformEXT); |
7339 | 0 | } |
7340 | 0 | } |
7341 | 186 | } |
7342 | | // Check for queries |
7343 | 3.25k | if (cracked.query) { |
7344 | 163 | switch (node->getOp()) { |
7345 | 0 | case glslang::EOpImageQuerySize: |
7346 | 66 | case glslang::EOpTextureQuerySize: |
7347 | 66 | if (arguments.size() > 1) { |
7348 | 51 | params.lod = arguments[1]; |
7349 | 51 | return builder.createTextureQueryCall(spv::Op::OpImageQuerySizeLod, params, isUnsignedResult); |
7350 | 51 | } else |
7351 | 15 | return builder.createTextureQueryCall(spv::Op::OpImageQuerySize, params, isUnsignedResult); |
7352 | 0 | case glslang::EOpImageQuerySamples: |
7353 | 6 | case glslang::EOpTextureQuerySamples: |
7354 | 6 | return builder.createTextureQueryCall(spv::Op::OpImageQuerySamples, params, isUnsignedResult); |
7355 | 52 | case glslang::EOpTextureQueryLod: |
7356 | 52 | params.coords = arguments[1]; |
7357 | 52 | return builder.createTextureQueryCall(spv::Op::OpImageQueryLod, params, isUnsignedResult); |
7358 | 39 | case glslang::EOpTextureQueryLevels: |
7359 | 39 | return builder.createTextureQueryCall(spv::Op::OpImageQueryLevels, params, isUnsignedResult); |
7360 | 0 | case glslang::EOpSparseTexelsResident: |
7361 | 0 | return builder.createUnaryOp(spv::Op::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
7362 | 0 | default: |
7363 | 0 | assert(0); |
7364 | 0 | break; |
7365 | 163 | } |
7366 | 163 | } |
7367 | | |
7368 | 3.08k | int components = node->getType().getVectorSize(); |
7369 | | |
7370 | 3.08k | if (node->getOp() == glslang::EOpImageLoad || |
7371 | 2.99k | node->getOp() == glslang::EOpImageLoadLod || |
7372 | 2.94k | node->getOp() == glslang::EOpTextureFetch || |
7373 | 2.91k | node->getOp() == glslang::EOpTextureFetchOffset) { |
7374 | | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
7375 | | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
7376 | | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
7377 | | // here around e.g. which ones return scalars or other types. |
7378 | 192 | components = 4; |
7379 | 192 | } |
7380 | | |
7381 | 3.08k | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
7382 | | |
7383 | 4.37k | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
7384 | | |
7385 | | // Check for image functions other than queries |
7386 | 3.08k | if (node->isImage()) { |
7387 | 1.81k | std::vector<spv::IdImmediate> operands; |
7388 | 1.81k | auto opIt = arguments.begin(); |
7389 | 1.81k | spv::IdImmediate image = { true, *(opIt++) }; |
7390 | 1.81k | operands.push_back(image); |
7391 | | |
7392 | | // Handle subpass operations |
7393 | | // TODO: GLSL should change to have the "MS" only on the type rather than the |
7394 | | // built-in function. |
7395 | 1.81k | if (cracked.subpass) { |
7396 | | // add on the (0,0) coordinate |
7397 | 6 | spv::Id zero = builder.makeIntConstant(0); |
7398 | 6 | std::vector<spv::Id> comps; |
7399 | 6 | comps.push_back(zero); |
7400 | 6 | comps.push_back(zero); |
7401 | 6 | spv::IdImmediate coord = { true, |
7402 | 6 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
7403 | 6 | operands.push_back(coord); |
7404 | 6 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMask::MaskNone }; |
7405 | 6 | imageOperands.word = imageOperands.word | (unsigned)signExtensionMask(); |
7406 | 6 | if (sampler.isMultiSample()) { |
7407 | 3 | imageOperands.word = imageOperands.word | (unsigned)spv::ImageOperandsMask::Sample; |
7408 | 3 | } |
7409 | 6 | if (imageOperands.word != (unsigned)spv::ImageOperandsMask::MaskNone) { |
7410 | 3 | operands.push_back(imageOperands); |
7411 | 3 | if (sampler.isMultiSample()) { |
7412 | 3 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
7413 | 3 | operands.push_back(imageOperand); |
7414 | 3 | } |
7415 | 3 | } |
7416 | 6 | spv::Id result = builder.createOp(spv::Op::OpImageRead, resultType(), operands); |
7417 | 6 | builder.setPrecision(result, precision); |
7418 | 6 | return result; |
7419 | 6 | } |
7420 | | |
7421 | 1.81k | if (cracked.attachmentEXT) { |
7422 | 0 | if (opIt != arguments.end()) { |
7423 | 0 | spv::IdImmediate sample = { true, *opIt }; |
7424 | 0 | operands.push_back(sample); |
7425 | 0 | } |
7426 | 0 | spv::Id result = builder.createOp(spv::Op::OpColorAttachmentReadEXT, resultType(), operands); |
7427 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
7428 | 0 | builder.setPrecision(result, precision); |
7429 | 0 | return result; |
7430 | 0 | } |
7431 | | |
7432 | 1.81k | spv::IdImmediate coord = { true, *(opIt++) }; |
7433 | 1.81k | operands.push_back(coord); |
7434 | 1.81k | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
7435 | 147 | spv::ImageOperandsMask mask = spv::ImageOperandsMask::MaskNone; |
7436 | 147 | if (sampler.isMultiSample()) { |
7437 | 16 | mask = mask | spv::ImageOperandsMask::Sample; |
7438 | 16 | } |
7439 | 147 | if (cracked.lod) { |
7440 | 54 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
7441 | 54 | builder.addCapability(spv::Capability::ImageReadWriteLodAMD); |
7442 | 54 | mask = mask | spv::ImageOperandsMask::Lod; |
7443 | 54 | } |
7444 | 147 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
7445 | 147 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMask::MakeTexelAvailableKHR); |
7446 | 147 | mask = mask | signExtensionMask(); |
7447 | 147 | if (mask != spv::ImageOperandsMask::MaskNone) { |
7448 | 70 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
7449 | 70 | operands.push_back(imageOperands); |
7450 | 70 | } |
7451 | 147 | if (anySet(mask, spv::ImageOperandsMask::Sample)) { |
7452 | 16 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7453 | 16 | operands.push_back(imageOperand); |
7454 | 16 | } |
7455 | 147 | if (anySet(mask, spv::ImageOperandsMask::Lod)) { |
7456 | 54 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7457 | 54 | operands.push_back(imageOperand); |
7458 | 54 | } |
7459 | 147 | if (anySet(mask, spv::ImageOperandsMask::MakeTexelVisibleKHR)) { |
7460 | 0 | spv::IdImmediate imageOperand = { true, |
7461 | 0 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
7462 | 0 | operands.push_back(imageOperand); |
7463 | 0 | } |
7464 | | |
7465 | 147 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormat::Unknown) |
7466 | 0 | builder.addCapability(spv::Capability::StorageImageReadWithoutFormat); |
7467 | | |
7468 | 147 | std::vector<spv::Id> result(1, builder.createOp(spv::Op::OpImageRead, resultType(), operands)); |
7469 | 147 | builder.setPrecision(result[0], precision); |
7470 | | |
7471 | | // If needed, add a conversion constructor to the proper size. |
7472 | 147 | if (components != node->getType().getVectorSize()) |
7473 | 0 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
7474 | | |
7475 | 147 | return result[0]; |
7476 | 1.66k | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
7477 | | |
7478 | | // Push the texel value before the operands |
7479 | 119 | if (sampler.isMultiSample() || cracked.lod) { |
7480 | 52 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
7481 | 52 | operands.push_back(texel); |
7482 | 67 | } else { |
7483 | 67 | spv::IdImmediate texel = { true, *opIt }; |
7484 | 67 | operands.push_back(texel); |
7485 | 67 | } |
7486 | | |
7487 | 119 | spv::ImageOperandsMask mask = spv::ImageOperandsMask::MaskNone; |
7488 | 119 | if (sampler.isMultiSample()) { |
7489 | 16 | mask = mask | spv::ImageOperandsMask::Sample; |
7490 | 16 | } |
7491 | 119 | if (cracked.lod) { |
7492 | 36 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
7493 | 36 | builder.addCapability(spv::Capability::ImageReadWriteLodAMD); |
7494 | 36 | mask = mask | spv::ImageOperandsMask::Lod; |
7495 | 36 | } |
7496 | 119 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
7497 | 119 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMask::MakeTexelVisibleKHR); |
7498 | 119 | mask = mask | signExtensionMask(); |
7499 | 119 | if (mask != spv::ImageOperandsMask::MaskNone) { |
7500 | 52 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
7501 | 52 | operands.push_back(imageOperands); |
7502 | 52 | } |
7503 | 119 | if (anySet(mask, spv::ImageOperandsMask::Sample)) { |
7504 | 16 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7505 | 16 | operands.push_back(imageOperand); |
7506 | 16 | } |
7507 | 119 | if (anySet(mask, spv::ImageOperandsMask::Lod)) { |
7508 | 36 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7509 | 36 | operands.push_back(imageOperand); |
7510 | 36 | } |
7511 | 119 | if (anySet(mask, spv::ImageOperandsMask::MakeTexelAvailableKHR)) { |
7512 | 0 | spv::IdImmediate imageOperand = { true, |
7513 | 0 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
7514 | 0 | operands.push_back(imageOperand); |
7515 | 0 | } |
7516 | | |
7517 | 119 | builder.createNoResultOp(spv::Op::OpImageWrite, operands); |
7518 | 119 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormat::Unknown) |
7519 | 0 | builder.addCapability(spv::Capability::StorageImageWriteWithoutFormat); |
7520 | 119 | return spv::NoResult; |
7521 | 1.54k | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
7522 | 1.46k | node->getOp() == glslang::EOpSparseImageLoadLod) { |
7523 | 120 | builder.addCapability(spv::Capability::SparseResidency); |
7524 | 120 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormat::Unknown) |
7525 | 0 | builder.addCapability(spv::Capability::StorageImageReadWithoutFormat); |
7526 | | |
7527 | 120 | spv::ImageOperandsMask mask = spv::ImageOperandsMask::MaskNone; |
7528 | 120 | if (sampler.isMultiSample()) { |
7529 | 6 | mask = mask | spv::ImageOperandsMask::Sample; |
7530 | 6 | } |
7531 | 120 | if (cracked.lod) { |
7532 | 36 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
7533 | 36 | builder.addCapability(spv::Capability::ImageReadWriteLodAMD); |
7534 | | |
7535 | 36 | mask = mask | spv::ImageOperandsMask::Lod; |
7536 | 36 | } |
7537 | 120 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
7538 | 120 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMask::MakeTexelAvailableKHR); |
7539 | 120 | mask = mask | signExtensionMask(); |
7540 | 120 | if (mask != spv::ImageOperandsMask::MaskNone) { |
7541 | 42 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
7542 | 42 | operands.push_back(imageOperands); |
7543 | 42 | } |
7544 | 120 | if (anySet(mask, spv::ImageOperandsMask::Sample)) { |
7545 | 6 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7546 | 6 | operands.push_back(imageOperand); |
7547 | 6 | } |
7548 | 120 | if (anySet(mask, spv::ImageOperandsMask::Lod)) { |
7549 | 36 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
7550 | 36 | operands.push_back(imageOperand); |
7551 | 36 | } |
7552 | 120 | if (anySet(mask, spv::ImageOperandsMask::MakeTexelVisibleKHR)) { |
7553 | 0 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
7554 | 0 | TranslateCoherent(imageType))) }; |
7555 | 0 | operands.push_back(imageOperand); |
7556 | 0 | } |
7557 | | |
7558 | | // Create the return type that was a special structure |
7559 | 120 | spv::Id texelOut = *opIt; |
7560 | 120 | spv::Id typeId0 = resultType(); |
7561 | 120 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
7562 | 120 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
7563 | | |
7564 | 120 | spv::Id resultId = builder.createOp(spv::Op::OpImageSparseRead, resultTypeId, operands); |
7565 | | |
7566 | | // Decode the return type |
7567 | 120 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
7568 | 120 | return builder.createCompositeExtract(resultId, typeId0, 0); |
7569 | 1.42k | } else { |
7570 | | // Process image atomic operations |
7571 | | |
7572 | | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
7573 | | // as the first source operand, is required by SPIR-V atomic operations. |
7574 | | // For non-MS, the sample value should be 0 |
7575 | 1.42k | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
7576 | 1.42k | operands.push_back(sample); |
7577 | | |
7578 | 1.42k | spv::Id resultTypeId; |
7579 | 1.42k | glslang::TBasicType typeProxy = node->getBasicType(); |
7580 | | // imageAtomicStore has a void return type so base the pointer type on |
7581 | | // the type of the value operand. |
7582 | 1.42k | if (node->getOp() == glslang::EOpImageAtomicStore) { |
7583 | 20 | resultTypeId = builder.makePointer(spv::StorageClass::Image, builder.getTypeId(*opIt)); |
7584 | 20 | typeProxy = node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler().type; |
7585 | 1.40k | } else { |
7586 | 1.40k | resultTypeId = builder.makePointer(spv::StorageClass::Image, resultType()); |
7587 | 1.40k | } |
7588 | | |
7589 | | // EXT_descriptor_heap |
7590 | | // For image atomic parameter, using untyped image texel pointer to carry on type metadata. |
7591 | 1.42k | spv::Op imgTexelOp = spv::Op::OpImageTexelPointer; |
7592 | 1.42k | if (node->getQualifier().isUsedByAtomic() && |
7593 | 0 | imageType.getQualifier().builtIn == glslang::EbvResourceHeapEXT) { |
7594 | 0 | operands.insert(operands.begin(), {true, convertGlslangToSpvType(imageType)}); |
7595 | 0 | imgTexelOp = spv::Op::OpUntypedImageTexelPointerEXT; |
7596 | 0 | resultTypeId = builder.makeUntypedPointer(spv::StorageClass::Image); |
7597 | 0 | } |
7598 | | |
7599 | 1.42k | spv::Id pointer = builder.createOp(imgTexelOp, resultTypeId, operands); |
7600 | 1.42k | if (imageType.getQualifier().nonUniform) { |
7601 | 0 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
7602 | 0 | if (extensions.find("GL_EXT_descriptor_heap") == extensions.end()) { |
7603 | 0 | builder.addDecoration(pointer, spv::Decoration::NonUniformEXT); |
7604 | 0 | } |
7605 | 0 | } |
7606 | | |
7607 | 1.42k | std::vector<spv::Id> operands; |
7608 | 1.42k | operands.push_back(pointer); |
7609 | 3.50k | for (; opIt != arguments.end(); ++opIt) |
7610 | 2.08k | operands.push_back(*opIt); |
7611 | | |
7612 | 1.42k | return createAtomicOperation(node->getOp(), precision, resultType(), operands, typeProxy, |
7613 | 1.42k | lvalueCoherentFlags, node->getType()); |
7614 | 1.42k | } |
7615 | 1.81k | } |
7616 | | |
7617 | | // Check for fragment mask functions other than queries |
7618 | 1.27k | if (cracked.fragMask) { |
7619 | 0 | assert(sampler.ms); |
7620 | |
|
7621 | 0 | auto opIt = arguments.begin(); |
7622 | 0 | std::vector<spv::Id> operands; |
7623 | |
|
7624 | 0 | operands.push_back(params.sampler); |
7625 | 0 | ++opIt; |
7626 | |
|
7627 | 0 | if (sampler.isSubpass()) { |
7628 | | // add on the (0,0) coordinate |
7629 | 0 | spv::Id zero = builder.makeIntConstant(0); |
7630 | 0 | std::vector<spv::Id> comps; |
7631 | 0 | comps.push_back(zero); |
7632 | 0 | comps.push_back(zero); |
7633 | 0 | operands.push_back(builder.makeCompositeConstant( |
7634 | 0 | builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
7635 | 0 | } |
7636 | |
|
7637 | 0 | for (; opIt != arguments.end(); ++opIt) |
7638 | 0 | operands.push_back(*opIt); |
7639 | |
|
7640 | 0 | spv::Op fragMaskOp = spv::Op::OpNop; |
7641 | 0 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
7642 | 0 | fragMaskOp = spv::Op::OpFragmentMaskFetchAMD; |
7643 | 0 | else if (node->getOp() == glslang::EOpFragmentFetch) |
7644 | 0 | fragMaskOp = spv::Op::OpFragmentFetchAMD; |
7645 | |
|
7646 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
7647 | 0 | builder.addCapability(spv::Capability::FragmentMaskAMD); |
7648 | 0 | return builder.createOp(fragMaskOp, resultType(), operands); |
7649 | 0 | } |
7650 | | |
7651 | | // Check for texture functions other than queries |
7652 | 1.27k | bool sparse = node->isSparseTexture(); |
7653 | 1.27k | bool imageFootprint = node->isImageFootprint(); |
7654 | 1.27k | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
7655 | | |
7656 | | // check for bias argument |
7657 | 1.27k | bool bias = false; |
7658 | 1.27k | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
7659 | 660 | int nonBiasArgCount = 2; |
7660 | 660 | if (cracked.gather) |
7661 | 168 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
7662 | | |
7663 | 660 | if (f16ShadowCompare) |
7664 | 61 | ++nonBiasArgCount; |
7665 | 660 | if (cracked.offset) |
7666 | 176 | ++nonBiasArgCount; |
7667 | 484 | else if (cracked.offsets) |
7668 | 48 | ++nonBiasArgCount; |
7669 | 660 | if (cracked.grad) |
7670 | 0 | nonBiasArgCount += 2; |
7671 | 660 | if (cracked.lodClamp) |
7672 | 136 | ++nonBiasArgCount; |
7673 | 660 | if (sparse) |
7674 | 204 | ++nonBiasArgCount; |
7675 | 660 | if (imageFootprint) |
7676 | | //Following three extra arguments |
7677 | | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
7678 | 0 | nonBiasArgCount += 3; |
7679 | 660 | if ((int)arguments.size() > nonBiasArgCount) |
7680 | 94 | bias = true; |
7681 | 660 | } |
7682 | | |
7683 | 1.27k | if (cracked.gather) { |
7684 | 240 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
7685 | 240 | if (bias || cracked.lod || |
7686 | 240 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
7687 | 240 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
7688 | 240 | builder.addCapability(spv::Capability::ImageGatherBiasLodAMD); |
7689 | 240 | } |
7690 | 240 | } |
7691 | | |
7692 | | // set the rest of the arguments |
7693 | | |
7694 | 1.27k | params.coords = arguments[1]; |
7695 | 1.27k | int extraArgs = 0; |
7696 | 1.27k | bool noImplicitLod = false; |
7697 | | |
7698 | | // sort out where Dref is coming from |
7699 | 1.27k | if (cubeCompare || f16ShadowCompare) { |
7700 | 129 | params.Dref = arguments[2]; |
7701 | 129 | ++extraArgs; |
7702 | 1.14k | } else if (sampler.shadow && cracked.gather) { |
7703 | 60 | params.Dref = arguments[2]; |
7704 | 60 | ++extraArgs; |
7705 | 1.08k | } else if (sampler.shadow) { |
7706 | 243 | std::vector<spv::Id> indexes; |
7707 | 243 | int dRefComp; |
7708 | 243 | if (cracked.proj) |
7709 | 33 | dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref" |
7710 | 210 | else |
7711 | 210 | dRefComp = builder.getNumComponents(params.coords) - 1; |
7712 | 243 | indexes.push_back(dRefComp); |
7713 | 243 | params.Dref = builder.createCompositeExtract(params.coords, |
7714 | 243 | builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
7715 | 243 | } |
7716 | | |
7717 | | // lod |
7718 | 1.27k | if (cracked.lod) { |
7719 | 252 | params.lod = arguments[2 + extraArgs]; |
7720 | 252 | ++extraArgs; |
7721 | 1.01k | } else if (glslangIntermediate->getStage() != EShLangFragment && |
7722 | 76 | !(glslangIntermediate->getStage() == EShLangCompute && |
7723 | 76 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
7724 | | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
7725 | 76 | noImplicitLod = true; |
7726 | 76 | } |
7727 | | |
7728 | | // multisample |
7729 | 1.27k | if (sampler.isMultiSample()) { |
7730 | 12 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
7731 | 12 | ++extraArgs; |
7732 | 12 | } |
7733 | | |
7734 | | // gradient |
7735 | 1.27k | if (cracked.grad) { |
7736 | 304 | params.gradX = arguments[2 + extraArgs]; |
7737 | 304 | params.gradY = arguments[3 + extraArgs]; |
7738 | 304 | extraArgs += 2; |
7739 | 304 | } |
7740 | | |
7741 | | // offset and offsets |
7742 | 1.27k | if (cracked.offset) { |
7743 | 466 | params.offset = arguments[2 + extraArgs]; |
7744 | 466 | ++extraArgs; |
7745 | 805 | } else if (cracked.offsets) { |
7746 | 64 | params.offsets = arguments[2 + extraArgs]; |
7747 | 64 | ++extraArgs; |
7748 | 64 | } |
7749 | | |
7750 | | // lod clamp |
7751 | 1.27k | if (cracked.lodClamp) { |
7752 | 200 | params.lodClamp = arguments[2 + extraArgs]; |
7753 | 200 | ++extraArgs; |
7754 | 200 | } |
7755 | | // sparse |
7756 | 1.27k | if (sparse) { |
7757 | 406 | params.texelOut = arguments[2 + extraArgs]; |
7758 | 406 | ++extraArgs; |
7759 | 406 | } |
7760 | | // gather component |
7761 | 1.27k | if (cracked.gather && ! sampler.shadow) { |
7762 | | // default component is 0, if missing, otherwise an argument |
7763 | 152 | if (2 + extraArgs < (int)arguments.size()) { |
7764 | 152 | params.component = arguments[2 + extraArgs]; |
7765 | 152 | ++extraArgs; |
7766 | 152 | } else |
7767 | 0 | params.component = builder.makeIntConstant(0); |
7768 | 152 | } |
7769 | 1.27k | spv::Id resultStruct = spv::NoResult; |
7770 | 1.27k | if (imageFootprint) { |
7771 | | //Following three extra arguments |
7772 | | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
7773 | 0 | params.granularity = arguments[2 + extraArgs]; |
7774 | 0 | params.coarse = arguments[3 + extraArgs]; |
7775 | 0 | resultStruct = arguments[4 + extraArgs]; |
7776 | 0 | extraArgs += 3; |
7777 | 0 | } |
7778 | | |
7779 | | // bias |
7780 | 1.27k | if (bias) { |
7781 | 94 | params.bias = arguments[2 + extraArgs]; |
7782 | 94 | ++extraArgs; |
7783 | 94 | } |
7784 | | |
7785 | 1.27k | if (imageFootprint) { |
7786 | 0 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
7787 | 0 | builder.addCapability(spv::Capability::ImageFootprintNV); |
7788 | | |
7789 | | |
7790 | | //resultStructType(OpenGL type) contains 5 elements: |
7791 | | //struct gl_TextureFootprint2DNV { |
7792 | | // uvec2 anchor; |
7793 | | // uvec2 offset; |
7794 | | // uvec2 mask; |
7795 | | // uint lod; |
7796 | | // uint granularity; |
7797 | | //}; |
7798 | | //or |
7799 | | //struct gl_TextureFootprint3DNV { |
7800 | | // uvec3 anchor; |
7801 | | // uvec3 offset; |
7802 | | // uvec2 mask; |
7803 | | // uint lod; |
7804 | | // uint granularity; |
7805 | | //}; |
7806 | 0 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
7807 | 0 | assert(builder.isStructType(resultStructType)); |
7808 | | |
7809 | | //resType (SPIR-V type) contains 6 elements: |
7810 | | //Member 0 must be a Boolean type scalar(LOD), |
7811 | | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
7812 | | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
7813 | | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
7814 | | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
7815 | | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
7816 | 0 | std::vector<spv::Id> members; |
7817 | 0 | members.push_back(resultType()); |
7818 | 0 | for (int i = 0; i < 5; i++) { |
7819 | 0 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
7820 | 0 | } |
7821 | 0 | spv::Id resType = builder.makeStructType(members, {}, "ResType"); |
7822 | | |
7823 | | //call ImageFootprintNV |
7824 | 0 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
7825 | 0 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
7826 | | |
7827 | | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
7828 | 0 | for (int i = 0; i < 5; i++) { |
7829 | 0 | builder.clearAccessChain(); |
7830 | 0 | builder.setAccessChainLValue(resultStruct); |
7831 | | |
7832 | | //Accessing to a struct we created, no coherent flag is set |
7833 | 0 | spv::Builder::AccessChain::CoherentFlags flags; |
7834 | 0 | flags.clear(); |
7835 | |
|
7836 | 0 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
7837 | 0 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
7838 | 0 | i+1), TranslateNonUniformDecoration(imageType.getQualifier())); |
7839 | 0 | } |
7840 | 0 | return builder.createCompositeExtract(res, resultType(), 0); |
7841 | 0 | } |
7842 | | |
7843 | | // projective component (might not to move) |
7844 | | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
7845 | | // are divided by the last component of P." |
7846 | | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
7847 | | // unused components will appear after all used components." |
7848 | 1.27k | if (cracked.proj) { |
7849 | 148 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
7850 | 148 | int projTargetComp; |
7851 | 148 | switch (sampler.dim) { |
7852 | 48 | case glslang::Esd1D: projTargetComp = 1; break; |
7853 | 48 | case glslang::Esd2D: projTargetComp = 2; break; |
7854 | 36 | case glslang::EsdRect: projTargetComp = 2; break; |
7855 | 16 | default: projTargetComp = projSourceComp; break; |
7856 | 148 | } |
7857 | | // copy the projective coordinate if we have to |
7858 | 148 | if (projTargetComp != projSourceComp) { |
7859 | 80 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
7860 | 80 | builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
7861 | 80 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
7862 | 80 | builder.getTypeId(params.coords), projTargetComp); |
7863 | 80 | } |
7864 | 148 | } |
7865 | | |
7866 | | // nonprivate |
7867 | 1.27k | if (imageType.getQualifier().nonprivate) { |
7868 | 0 | params.nonprivate = true; |
7869 | 0 | } |
7870 | | |
7871 | | // volatile |
7872 | 1.27k | if (imageType.getQualifier().volatil) { |
7873 | 0 | params.volatil = true; |
7874 | 0 | } |
7875 | | |
7876 | 1.27k | if (imageType.getQualifier().nontemporal) { |
7877 | 0 | params.nontemporal = true; |
7878 | 0 | } |
7879 | | |
7880 | 1.27k | std::vector<spv::Id> result( 1, |
7881 | 1.27k | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
7882 | 1.27k | noImplicitLod, params, signExtensionMask()) |
7883 | 1.27k | ); |
7884 | | |
7885 | 1.27k | if (components != node->getType().getVectorSize()) |
7886 | 0 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
7887 | | |
7888 | 1.27k | return result[0]; |
7889 | 1.27k | } |
7890 | | |
7891 | | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
7892 | 631 | { |
7893 | | // Grab the function's pointer from the previously created function |
7894 | 631 | spv::Function* function = functionMap[node->getName().c_str()]; |
7895 | 631 | if (! function) |
7896 | 0 | return 0; |
7897 | | |
7898 | 631 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
7899 | 631 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
7900 | | |
7901 | | // See comments in makeFunctions() for details about the semantics for parameter passing. |
7902 | | // |
7903 | | // These imply we need a four step process: |
7904 | | // 1. Evaluate the arguments |
7905 | | // 2. Allocate and make copies of in, out, and inout arguments |
7906 | | // 3. Make the call |
7907 | | // 4. Copy back the results |
7908 | | |
7909 | | // 1. Evaluate the arguments and their types |
7910 | 631 | std::vector<spv::Builder::AccessChain> lValues; |
7911 | 631 | std::vector<spv::Id> rValues; |
7912 | 631 | std::vector<const glslang::TType*> argTypes; |
7913 | 1.00k | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
7914 | 376 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
7915 | | // build l-value |
7916 | 376 | builder.clearAccessChain(); |
7917 | 376 | glslangArgs[a]->traverse(this); |
7918 | | // keep outputs and pass-by-originals as l-values, evaluate others as r-values |
7919 | 376 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
7920 | 376 | writableParam(qualifiers[a])) { |
7921 | | // save l-value |
7922 | 376 | lValues.push_back(builder.getAccessChain()); |
7923 | 376 | } else { |
7924 | | // process r-value |
7925 | 0 | rValues.push_back(accessChainLoad(*argTypes.back())); |
7926 | 0 | } |
7927 | 376 | } |
7928 | | |
7929 | | // Reset source location to the function call location after argument evaluation |
7930 | 631 | builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename()); |
7931 | | |
7932 | | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
7933 | | // copy the original into that space. |
7934 | | // |
7935 | | // Also, build up the list of actual arguments to pass in for the call |
7936 | 631 | int lValueCount = 0; |
7937 | 631 | int rValueCount = 0; |
7938 | 631 | std::vector<spv::Id> spvArgs; |
7939 | 1.00k | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
7940 | 376 | spv::Id arg; |
7941 | 376 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
7942 | 0 | builder.setAccessChain(lValues[lValueCount]); |
7943 | 0 | arg = builder.accessChainGetLValue(); |
7944 | 0 | ++lValueCount; |
7945 | 376 | } else if (writableParam(qualifiers[a])) { |
7946 | | // need space to hold the copy |
7947 | 376 | arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClass::Function, |
7948 | 376 | builder.getContainedTypeId(function->getParamType(a)), "param"); |
7949 | 376 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
7950 | | // need to copy the input into output space |
7951 | 376 | builder.setAccessChain(lValues[lValueCount]); |
7952 | 376 | spv::Id copy = accessChainLoad(*argTypes[a]); |
7953 | 376 | builder.clearAccessChain(); |
7954 | 376 | builder.setAccessChainLValue(arg); |
7955 | 376 | multiTypeStore(*argTypes[a], copy); |
7956 | 376 | } |
7957 | 376 | ++lValueCount; |
7958 | 376 | } else { |
7959 | | // process r-value, which involves a copy for a type mismatch |
7960 | 0 | if (function->getParamType(a) != builder.getTypeId(rValues[rValueCount]) || |
7961 | 0 | TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
7962 | 0 | { |
7963 | 0 | spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClass::Function, function->getParamType(a), "arg"); |
7964 | 0 | builder.clearAccessChain(); |
7965 | 0 | builder.setAccessChainLValue(argCopy); |
7966 | 0 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
7967 | 0 | arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
7968 | 0 | } else |
7969 | 0 | arg = rValues[rValueCount]; |
7970 | 0 | ++rValueCount; |
7971 | 0 | } |
7972 | 376 | spvArgs.push_back(arg); |
7973 | 376 | } |
7974 | | |
7975 | | // 3. Make the call. |
7976 | 631 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
7977 | 631 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
7978 | 631 | builder.addDecoration(result, TranslateNonUniformDecoration(node->getType().getQualifier())); |
7979 | | |
7980 | | // 4. Copy back out an "out" arguments. |
7981 | 631 | lValueCount = 0; |
7982 | 1.00k | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
7983 | 376 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
7984 | 0 | ++lValueCount; |
7985 | 376 | else if (writableParam(qualifiers[a])) { |
7986 | 376 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
7987 | 0 | spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
7988 | 0 | builder.addDecoration(copy, TranslateNonUniformDecoration(argTypes[a]->getQualifier())); |
7989 | 0 | builder.setAccessChain(lValues[lValueCount]); |
7990 | 0 | multiTypeStore(*argTypes[a], copy); |
7991 | 0 | } |
7992 | 376 | ++lValueCount; |
7993 | 376 | } |
7994 | 376 | } |
7995 | | |
7996 | 631 | return result; |
7997 | 631 | } |
7998 | | |
7999 | | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
8000 | | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
8001 | | spv::Id typeId, spv::Id left, spv::Id right, |
8002 | | glslang::TBasicType typeProxy, bool reduceComparison) |
8003 | 4.26k | { |
8004 | 4.26k | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
8005 | 4.26k | bool isFloat = isTypeFloat(typeProxy); |
8006 | 4.26k | bool isBool = typeProxy == glslang::EbtBool; |
8007 | | |
8008 | 4.26k | spv::Op binOp = spv::Op::OpNop; |
8009 | 4.26k | bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector? |
8010 | 4.26k | bool comparison = false; |
8011 | | |
8012 | 4.26k | switch (op) { |
8013 | 615 | case glslang::EOpAdd: |
8014 | 3.05k | case glslang::EOpAddAssign: |
8015 | 3.05k | if (isFloat) |
8016 | 2.44k | binOp = spv::Op::OpFAdd; |
8017 | 609 | else |
8018 | 609 | binOp = spv::Op::OpIAdd; |
8019 | 3.05k | break; |
8020 | 109 | case glslang::EOpSub: |
8021 | 120 | case glslang::EOpSubAssign: |
8022 | 120 | if (isFloat) |
8023 | 56 | binOp = spv::Op::OpFSub; |
8024 | 64 | else |
8025 | 64 | binOp = spv::Op::OpISub; |
8026 | 120 | break; |
8027 | 172 | case glslang::EOpMul: |
8028 | 181 | case glslang::EOpMulAssign: |
8029 | 181 | if (isFloat) |
8030 | 100 | binOp = spv::Op::OpFMul; |
8031 | 81 | else |
8032 | 81 | binOp = spv::Op::OpIMul; |
8033 | 181 | break; |
8034 | 65 | case glslang::EOpVectorTimesScalar: |
8035 | 82 | case glslang::EOpVectorTimesScalarAssign: |
8036 | 82 | if (isFloat && (builder.isVector(left) || builder.isVector(right) || builder.isCooperativeVector(left) || builder.isCooperativeVector(right))) { |
8037 | 73 | if (builder.isVector(right) || builder.isCooperativeVector(right)) |
8038 | 4 | std::swap(left, right); |
8039 | 73 | assert(builder.isScalar(right)); |
8040 | 73 | needMatchingVectors = false; |
8041 | 73 | binOp = spv::Op::OpVectorTimesScalar; |
8042 | 73 | } else if (isFloat) { |
8043 | 0 | binOp = spv::Op::OpFMul; |
8044 | 9 | } else if (builder.isCooperativeVector(left) || builder.isCooperativeVector(right)) { |
8045 | 0 | if (builder.isCooperativeVector(right)) |
8046 | 0 | std::swap(left, right); |
8047 | 0 | assert(builder.isScalar(right)); |
8048 | | // Construct a cooperative vector from the scalar |
8049 | 0 | right = builder.createCompositeConstruct(builder.getTypeId(left), { right }); |
8050 | 0 | binOp = spv::Op::OpIMul; |
8051 | 9 | } else { |
8052 | 9 | binOp = spv::Op::OpIMul; |
8053 | 9 | } |
8054 | 82 | break; |
8055 | 0 | case glslang::EOpVectorTimesMatrix: |
8056 | 0 | case glslang::EOpVectorTimesMatrixAssign: |
8057 | 0 | binOp = spv::Op::OpVectorTimesMatrix; |
8058 | 0 | break; |
8059 | 13 | case glslang::EOpMatrixTimesVector: |
8060 | 13 | binOp = spv::Op::OpMatrixTimesVector; |
8061 | 13 | break; |
8062 | 0 | case glslang::EOpMatrixTimesScalar: |
8063 | 0 | case glslang::EOpMatrixTimesScalarAssign: |
8064 | 0 | binOp = spv::Op::OpMatrixTimesScalar; |
8065 | 0 | break; |
8066 | 0 | case glslang::EOpMatrixTimesMatrix: |
8067 | 0 | case glslang::EOpMatrixTimesMatrixAssign: |
8068 | 0 | binOp = spv::Op::OpMatrixTimesMatrix; |
8069 | 0 | break; |
8070 | 0 | case glslang::EOpOuterProduct: |
8071 | 0 | binOp = spv::Op::OpOuterProduct; |
8072 | 0 | needMatchingVectors = false; |
8073 | 0 | break; |
8074 | 48 | case glslang::EOpDot: |
8075 | 48 | if (typeProxy == glslang::EbtBFloat16) { |
8076 | 0 | builder.addExtension(spv::E_SPV_KHR_bfloat16); |
8077 | 0 | builder.addCapability(spv::Capability::BFloat16DotProductKHR); |
8078 | 0 | } |
8079 | 48 | binOp = spv::Op::OpDot; |
8080 | 48 | break; |
8081 | | |
8082 | 58 | case glslang::EOpDiv: |
8083 | 67 | case glslang::EOpDivAssign: |
8084 | 67 | if (isFloat) |
8085 | 49 | binOp = spv::Op::OpFDiv; |
8086 | 18 | else if (isUnsigned) |
8087 | 9 | binOp = spv::Op::OpUDiv; |
8088 | 9 | else |
8089 | 9 | binOp = spv::Op::OpSDiv; |
8090 | 67 | break; |
8091 | 18 | case glslang::EOpMod: |
8092 | 27 | case glslang::EOpModAssign: |
8093 | 27 | if (isFloat) |
8094 | 0 | binOp = spv::Op::OpFMod; |
8095 | 27 | else if (isUnsigned) |
8096 | 18 | binOp = spv::Op::OpUMod; |
8097 | 9 | else |
8098 | 9 | binOp = spv::Op::OpSMod; |
8099 | 27 | break; |
8100 | 0 | case glslang::EOpRightShift: |
8101 | 9 | case glslang::EOpRightShiftAssign: |
8102 | 9 | if (isUnsigned) |
8103 | 0 | binOp = spv::Op::OpShiftRightLogical; |
8104 | 9 | else |
8105 | 9 | binOp = spv::Op::OpShiftRightArithmetic; |
8106 | 9 | break; |
8107 | 18 | case glslang::EOpLeftShift: |
8108 | 27 | case glslang::EOpLeftShiftAssign: |
8109 | 27 | binOp = spv::Op::OpShiftLeftLogical; |
8110 | 27 | break; |
8111 | 9 | case glslang::EOpAnd: |
8112 | 18 | case glslang::EOpAndAssign: |
8113 | 18 | binOp = spv::Op::OpBitwiseAnd; |
8114 | 18 | break; |
8115 | 0 | case glslang::EOpLogicalAnd: |
8116 | 0 | needMatchingVectors = false; |
8117 | 0 | binOp = spv::Op::OpLogicalAnd; |
8118 | 0 | break; |
8119 | 9 | case glslang::EOpInclusiveOr: |
8120 | 18 | case glslang::EOpInclusiveOrAssign: |
8121 | 18 | binOp = spv::Op::OpBitwiseOr; |
8122 | 18 | break; |
8123 | 0 | case glslang::EOpLogicalOr: |
8124 | 0 | needMatchingVectors = false; |
8125 | 0 | binOp = spv::Op::OpLogicalOr; |
8126 | 0 | break; |
8127 | 9 | case glslang::EOpExclusiveOr: |
8128 | 18 | case glslang::EOpExclusiveOrAssign: |
8129 | 18 | binOp = spv::Op::OpBitwiseXor; |
8130 | 18 | break; |
8131 | 0 | case glslang::EOpLogicalXor: |
8132 | 0 | needMatchingVectors = false; |
8133 | 0 | binOp = spv::Op::OpLogicalNotEqual; |
8134 | 0 | break; |
8135 | | |
8136 | 0 | case glslang::EOpAbsDifference: |
8137 | 0 | binOp = isUnsigned ? spv::Op::OpAbsUSubINTEL : spv::Op::OpAbsISubINTEL; |
8138 | 0 | break; |
8139 | | |
8140 | 0 | case glslang::EOpAddSaturate: |
8141 | 0 | binOp = isUnsigned ? spv::Op::OpUAddSatINTEL : spv::Op::OpIAddSatINTEL; |
8142 | 0 | break; |
8143 | | |
8144 | 0 | case glslang::EOpSubSaturate: |
8145 | 0 | binOp = isUnsigned ? spv::Op::OpUSubSatINTEL : spv::Op::OpISubSatINTEL; |
8146 | 0 | break; |
8147 | | |
8148 | 0 | case glslang::EOpAverage: |
8149 | 0 | binOp = isUnsigned ? spv::Op::OpUAverageINTEL : spv::Op::OpIAverageINTEL; |
8150 | 0 | break; |
8151 | | |
8152 | 0 | case glslang::EOpAverageRounded: |
8153 | 0 | binOp = isUnsigned ? spv::Op::OpUAverageRoundedINTEL : spv::Op::OpIAverageRoundedINTEL; |
8154 | 0 | break; |
8155 | | |
8156 | 0 | case glslang::EOpMul32x16: |
8157 | 0 | binOp = isUnsigned ? spv::Op::OpUMul32x16INTEL : spv::Op::OpIMul32x16INTEL; |
8158 | 0 | break; |
8159 | | |
8160 | 0 | case glslang::EOpExpectEXT: |
8161 | 0 | binOp = spv::Op::OpExpectKHR; |
8162 | 0 | break; |
8163 | | |
8164 | 166 | case glslang::EOpLessThan: |
8165 | 411 | case glslang::EOpGreaterThan: |
8166 | 461 | case glslang::EOpLessThanEqual: |
8167 | 488 | case glslang::EOpGreaterThanEqual: |
8168 | 547 | case glslang::EOpEqual: |
8169 | 556 | case glslang::EOpNotEqual: |
8170 | 574 | case glslang::EOpVectorEqual: |
8171 | 584 | case glslang::EOpVectorNotEqual: |
8172 | 584 | comparison = true; |
8173 | 584 | break; |
8174 | 0 | default: |
8175 | 0 | break; |
8176 | 4.26k | } |
8177 | | |
8178 | | // handle mapped binary operations (should be non-comparison) |
8179 | 4.26k | if (binOp != spv::Op::OpNop) { |
8180 | 3.68k | assert(comparison == false); |
8181 | 3.68k | if (builder.isMatrix(left) || builder.isMatrix(right) || |
8182 | 3.66k | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
8183 | 13 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
8184 | | |
8185 | | // No matrix involved; make both operands be the same number of components, if needed |
8186 | 3.66k | if (needMatchingVectors) |
8187 | 3.59k | builder.promoteScalar(decorations.precision, left, right); |
8188 | | |
8189 | 3.66k | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
8190 | 3.66k | decorations.addNoContraction(builder, result); |
8191 | 3.66k | decorations.addNonUniform(builder, result); |
8192 | 3.66k | return builder.setPrecision(result, decorations.precision); |
8193 | 3.68k | } |
8194 | | |
8195 | 584 | if (! comparison) |
8196 | 0 | return 0; |
8197 | | |
8198 | | // Handle comparison instructions |
8199 | | |
8200 | 584 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
8201 | 68 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
8202 | 0 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
8203 | 0 | decorations.addNonUniform(builder, result); |
8204 | 0 | return result; |
8205 | 0 | } |
8206 | | |
8207 | 584 | switch (op) { |
8208 | 166 | case glslang::EOpLessThan: |
8209 | 166 | if (isFloat) |
8210 | 39 | binOp = spv::Op::OpFOrdLessThan; |
8211 | 127 | else if (isUnsigned) |
8212 | 18 | binOp = spv::Op::OpULessThan; |
8213 | 109 | else |
8214 | 109 | binOp = spv::Op::OpSLessThan; |
8215 | 166 | break; |
8216 | 245 | case glslang::EOpGreaterThan: |
8217 | 245 | if (isFloat) |
8218 | 102 | binOp = spv::Op::OpFOrdGreaterThan; |
8219 | 143 | else if (isUnsigned) |
8220 | 38 | binOp = spv::Op::OpUGreaterThan; |
8221 | 105 | else |
8222 | 105 | binOp = spv::Op::OpSGreaterThan; |
8223 | 245 | break; |
8224 | 50 | case glslang::EOpLessThanEqual: |
8225 | 50 | if (isFloat) |
8226 | 0 | binOp = spv::Op::OpFOrdLessThanEqual; |
8227 | 50 | else if (isUnsigned) |
8228 | 8 | binOp = spv::Op::OpULessThanEqual; |
8229 | 42 | else |
8230 | 42 | binOp = spv::Op::OpSLessThanEqual; |
8231 | 50 | break; |
8232 | 27 | case glslang::EOpGreaterThanEqual: |
8233 | 27 | if (isFloat) |
8234 | 0 | binOp = spv::Op::OpFOrdGreaterThanEqual; |
8235 | 27 | else if (isUnsigned) |
8236 | 18 | binOp = spv::Op::OpUGreaterThanEqual; |
8237 | 9 | else |
8238 | 9 | binOp = spv::Op::OpSGreaterThanEqual; |
8239 | 27 | break; |
8240 | 59 | case glslang::EOpEqual: |
8241 | 77 | case glslang::EOpVectorEqual: |
8242 | 77 | if (isFloat) |
8243 | 50 | binOp = spv::Op::OpFOrdEqual; |
8244 | 27 | else if (isBool) |
8245 | 0 | binOp = spv::Op::OpLogicalEqual; |
8246 | 27 | else |
8247 | 27 | binOp = spv::Op::OpIEqual; |
8248 | 77 | break; |
8249 | 9 | case glslang::EOpNotEqual: |
8250 | 19 | case glslang::EOpVectorNotEqual: |
8251 | 19 | if (isFloat) |
8252 | 0 | binOp = spv::Op::OpFUnordNotEqual; |
8253 | 19 | else if (isBool) |
8254 | 0 | binOp = spv::Op::OpLogicalNotEqual; |
8255 | 19 | else |
8256 | 19 | binOp = spv::Op::OpINotEqual; |
8257 | 19 | break; |
8258 | 0 | default: |
8259 | 0 | break; |
8260 | 584 | } |
8261 | | |
8262 | 584 | if (binOp != spv::Op::OpNop) { |
8263 | 584 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
8264 | 584 | decorations.addNoContraction(builder, result); |
8265 | 584 | decorations.addNonUniform(builder, result); |
8266 | 584 | return builder.setPrecision(result, decorations.precision); |
8267 | 584 | } |
8268 | | |
8269 | 0 | return 0; |
8270 | 584 | } |
8271 | | |
8272 | | // |
8273 | | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
8274 | | // These can be any of: |
8275 | | // |
8276 | | // matrix * scalar |
8277 | | // scalar * matrix |
8278 | | // matrix * matrix linear algebraic |
8279 | | // matrix * vector |
8280 | | // vector * matrix |
8281 | | // matrix * matrix componentwise |
8282 | | // matrix op matrix op in {+, -, /} |
8283 | | // matrix op scalar op in {+, -, /} |
8284 | | // scalar op matrix op in {+, -, /} |
8285 | | // |
8286 | | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
8287 | | spv::Id left, spv::Id right) |
8288 | 13 | { |
8289 | 13 | bool firstClass = true; |
8290 | | |
8291 | | // First, handle first-class matrix operations (* and matrix/scalar) |
8292 | 13 | switch (op) { |
8293 | 0 | case spv::Op::OpFDiv: |
8294 | 0 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
8295 | | // turn matrix / scalar into a multiply... |
8296 | 0 | spv::Id resultType = builder.getTypeId(right); |
8297 | 0 | right = builder.createBinOp(spv::Op::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
8298 | 0 | op = spv::Op::OpMatrixTimesScalar; |
8299 | 0 | } else |
8300 | 0 | firstClass = false; |
8301 | 0 | break; |
8302 | 0 | case spv::Op::OpMatrixTimesScalar: |
8303 | 0 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
8304 | 0 | std::swap(left, right); |
8305 | 0 | assert(builder.isScalar(right)); |
8306 | 0 | break; |
8307 | 0 | case spv::Op::OpVectorTimesMatrix: |
8308 | 0 | assert(builder.isVector(left)); |
8309 | 0 | assert(builder.isMatrix(right)); |
8310 | 0 | break; |
8311 | 13 | case spv::Op::OpMatrixTimesVector: |
8312 | 13 | assert(builder.isMatrix(left)); |
8313 | 13 | assert(builder.isVector(right)); |
8314 | 13 | break; |
8315 | 0 | case spv::Op::OpMatrixTimesMatrix: |
8316 | 0 | assert(builder.isMatrix(left)); |
8317 | 0 | assert(builder.isMatrix(right)); |
8318 | 0 | break; |
8319 | 0 | default: |
8320 | 0 | firstClass = false; |
8321 | 0 | break; |
8322 | 13 | } |
8323 | | |
8324 | 13 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
8325 | 0 | firstClass = true; |
8326 | | |
8327 | 13 | if (firstClass) { |
8328 | 13 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
8329 | 13 | decorations.addNoContraction(builder, result); |
8330 | 13 | decorations.addNonUniform(builder, result); |
8331 | 13 | return builder.setPrecision(result, decorations.precision); |
8332 | 13 | } |
8333 | | |
8334 | | // Handle component-wise +, -, *, %, and / for all combinations of type. |
8335 | | // The result type of all of them is the same type as the (a) matrix operand. |
8336 | | // The algorithm is to: |
8337 | | // - break the matrix(es) into vectors |
8338 | | // - smear any scalar to a vector |
8339 | | // - do vector operations |
8340 | | // - make a matrix out the vector results |
8341 | 0 | switch (op) { |
8342 | 0 | case spv::Op::OpFAdd: |
8343 | 0 | case spv::Op::OpFSub: |
8344 | 0 | case spv::Op::OpFDiv: |
8345 | 0 | case spv::Op::OpFMod: |
8346 | 0 | case spv::Op::OpFMul: |
8347 | 0 | { |
8348 | | // one time set up... |
8349 | 0 | bool leftMat = builder.isMatrix(left); |
8350 | 0 | bool rightMat = builder.isMatrix(right); |
8351 | 0 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
8352 | 0 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
8353 | 0 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
8354 | 0 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
8355 | 0 | std::vector<spv::Id> results; |
8356 | 0 | spv::Id smearVec = spv::NoResult; |
8357 | 0 | if (builder.isScalar(left)) |
8358 | 0 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
8359 | 0 | else if (builder.isScalar(right)) |
8360 | 0 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
8361 | | |
8362 | | // do each vector op |
8363 | 0 | for (unsigned int c = 0; c < numCols; ++c) { |
8364 | 0 | std::vector<unsigned int> indexes; |
8365 | 0 | indexes.push_back(c); |
8366 | 0 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
8367 | 0 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
8368 | 0 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
8369 | 0 | decorations.addNoContraction(builder, result); |
8370 | 0 | decorations.addNonUniform(builder, result); |
8371 | 0 | results.push_back(builder.setPrecision(result, decorations.precision)); |
8372 | 0 | } |
8373 | | |
8374 | | // put the pieces together |
8375 | 0 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
8376 | 0 | decorations.addNonUniform(builder, result); |
8377 | 0 | return result; |
8378 | 0 | } |
8379 | 0 | default: |
8380 | 0 | assert(0); |
8381 | 0 | return spv::NoResult; |
8382 | 0 | } |
8383 | 0 | } |
8384 | | |
8385 | | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
8386 | | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags, |
8387 | | const glslang::TType &opType) |
8388 | 634 | { |
8389 | 634 | spv::Op unaryOp = spv::Op::OpNop; |
8390 | 634 | int extBuiltins = -1; |
8391 | 634 | int libCall = -1; |
8392 | 634 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
8393 | 634 | bool isFloat = isTypeFloat(typeProxy); |
8394 | | |
8395 | 634 | switch (op) { |
8396 | 101 | case glslang::EOpNegative: |
8397 | 101 | if (isFloat) { |
8398 | 13 | unaryOp = spv::Op::OpFNegate; |
8399 | 13 | if (builder.isMatrixType(typeId)) |
8400 | 0 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
8401 | 13 | } else |
8402 | 88 | unaryOp = spv::Op::OpSNegate; |
8403 | 101 | break; |
8404 | | |
8405 | 101 | case glslang::EOpLogicalNot: |
8406 | 0 | case glslang::EOpVectorLogicalNot: |
8407 | 0 | unaryOp = spv::Op::OpLogicalNot; |
8408 | 0 | break; |
8409 | 9 | case glslang::EOpBitwiseNot: |
8410 | 9 | unaryOp = spv::Op::OpNot; |
8411 | 9 | break; |
8412 | | |
8413 | 0 | case glslang::EOpDeterminant: |
8414 | 0 | libCall = spv::GLSLstd450Determinant; |
8415 | 0 | break; |
8416 | 0 | case glslang::EOpMatrixInverse: |
8417 | 0 | libCall = spv::GLSLstd450MatrixInverse; |
8418 | 0 | break; |
8419 | 40 | case glslang::EOpTranspose: |
8420 | 40 | unaryOp = spv::Op::OpTranspose; |
8421 | 40 | break; |
8422 | | |
8423 | 0 | case glslang::EOpRadians: |
8424 | 0 | libCall = spv::GLSLstd450Radians; |
8425 | 0 | break; |
8426 | 0 | case glslang::EOpDegrees: |
8427 | 0 | libCall = spv::GLSLstd450Degrees; |
8428 | 0 | break; |
8429 | 2 | case glslang::EOpSin: |
8430 | 2 | libCall = spv::GLSLstd450Sin; |
8431 | 2 | break; |
8432 | 2 | case glslang::EOpCos: |
8433 | 2 | libCall = spv::GLSLstd450Cos; |
8434 | 2 | break; |
8435 | 0 | case glslang::EOpTan: |
8436 | 0 | libCall = spv::GLSLstd450Tan; |
8437 | 0 | break; |
8438 | 0 | case glslang::EOpAcos: |
8439 | 0 | libCall = spv::GLSLstd450Acos; |
8440 | 0 | break; |
8441 | 0 | case glslang::EOpAsin: |
8442 | 0 | libCall = spv::GLSLstd450Asin; |
8443 | 0 | break; |
8444 | 0 | case glslang::EOpAtan: |
8445 | 0 | libCall = spv::GLSLstd450Atan; |
8446 | 0 | break; |
8447 | | |
8448 | 0 | case glslang::EOpAcosh: |
8449 | 0 | libCall = spv::GLSLstd450Acosh; |
8450 | 0 | break; |
8451 | 0 | case glslang::EOpAsinh: |
8452 | 0 | libCall = spv::GLSLstd450Asinh; |
8453 | 0 | break; |
8454 | 0 | case glslang::EOpAtanh: |
8455 | 0 | libCall = spv::GLSLstd450Atanh; |
8456 | 0 | break; |
8457 | 0 | case glslang::EOpTanh: |
8458 | 0 | libCall = spv::GLSLstd450Tanh; |
8459 | 0 | break; |
8460 | 0 | case glslang::EOpCosh: |
8461 | 0 | libCall = spv::GLSLstd450Cosh; |
8462 | 0 | break; |
8463 | 0 | case glslang::EOpSinh: |
8464 | 0 | libCall = spv::GLSLstd450Sinh; |
8465 | 0 | break; |
8466 | | |
8467 | 13 | case glslang::EOpLength: |
8468 | 13 | libCall = spv::GLSLstd450Length; |
8469 | 13 | break; |
8470 | 52 | case glslang::EOpNormalize: |
8471 | 52 | libCall = spv::GLSLstd450Normalize; |
8472 | 52 | break; |
8473 | | |
8474 | 0 | case glslang::EOpExp: |
8475 | 0 | libCall = spv::GLSLstd450Exp; |
8476 | 0 | break; |
8477 | 0 | case glslang::EOpLog: |
8478 | 0 | libCall = spv::GLSLstd450Log; |
8479 | 0 | break; |
8480 | 0 | case glslang::EOpExp2: |
8481 | 0 | libCall = spv::GLSLstd450Exp2; |
8482 | 0 | break; |
8483 | 0 | case glslang::EOpLog2: |
8484 | 0 | libCall = spv::GLSLstd450Log2; |
8485 | 0 | break; |
8486 | 0 | case glslang::EOpSqrt: |
8487 | 0 | libCall = spv::GLSLstd450Sqrt; |
8488 | 0 | break; |
8489 | 0 | case glslang::EOpInverseSqrt: |
8490 | 0 | libCall = spv::GLSLstd450InverseSqrt; |
8491 | 0 | break; |
8492 | | |
8493 | 0 | case glslang::EOpFloor: |
8494 | 0 | libCall = spv::GLSLstd450Floor; |
8495 | 0 | break; |
8496 | 0 | case glslang::EOpTrunc: |
8497 | 0 | libCall = spv::GLSLstd450Trunc; |
8498 | 0 | break; |
8499 | 0 | case glslang::EOpRound: |
8500 | 0 | libCall = spv::GLSLstd450Round; |
8501 | 0 | break; |
8502 | 0 | case glslang::EOpRoundEven: |
8503 | 0 | libCall = spv::GLSLstd450RoundEven; |
8504 | 0 | break; |
8505 | 0 | case glslang::EOpCeil: |
8506 | 0 | libCall = spv::GLSLstd450Ceil; |
8507 | 0 | break; |
8508 | 0 | case glslang::EOpFract: |
8509 | 0 | libCall = spv::GLSLstd450Fract; |
8510 | 0 | break; |
8511 | | |
8512 | 0 | case glslang::EOpIsNan: |
8513 | 0 | unaryOp = spv::Op::OpIsNan; |
8514 | 0 | break; |
8515 | 0 | case glslang::EOpIsInf: |
8516 | 0 | unaryOp = spv::Op::OpIsInf; |
8517 | 0 | break; |
8518 | 0 | case glslang::EOpIsFinite: |
8519 | 0 | unaryOp = spv::Op::OpIsFinite; |
8520 | 0 | break; |
8521 | | |
8522 | 0 | case glslang::EOpFloatBitsToInt: |
8523 | 0 | case glslang::EOpFloatBitsToUint: |
8524 | 0 | case glslang::EOpIntBitsToFloat: |
8525 | 0 | case glslang::EOpUintBitsToFloat: |
8526 | 0 | case glslang::EOpDoubleBitsToInt64: |
8527 | 0 | case glslang::EOpDoubleBitsToUint64: |
8528 | 0 | case glslang::EOpInt64BitsToDouble: |
8529 | 0 | case glslang::EOpUint64BitsToDouble: |
8530 | 9 | case glslang::EOpFloat16BitsToInt16: |
8531 | 18 | case glslang::EOpFloat16BitsToUint16: |
8532 | 27 | case glslang::EOpInt16BitsToFloat16: |
8533 | 36 | case glslang::EOpUint16BitsToFloat16: |
8534 | 36 | unaryOp = spv::Op::OpBitcast; |
8535 | 36 | break; |
8536 | | |
8537 | 0 | case glslang::EOpPackSnorm2x16: |
8538 | 0 | libCall = spv::GLSLstd450PackSnorm2x16; |
8539 | 0 | break; |
8540 | 0 | case glslang::EOpUnpackSnorm2x16: |
8541 | 0 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
8542 | 0 | break; |
8543 | 0 | case glslang::EOpPackUnorm2x16: |
8544 | 0 | libCall = spv::GLSLstd450PackUnorm2x16; |
8545 | 0 | break; |
8546 | 0 | case glslang::EOpUnpackUnorm2x16: |
8547 | 0 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
8548 | 0 | break; |
8549 | 0 | case glslang::EOpPackHalf2x16: |
8550 | 0 | libCall = spv::GLSLstd450PackHalf2x16; |
8551 | 0 | break; |
8552 | 0 | case glslang::EOpUnpackHalf2x16: |
8553 | 0 | libCall = spv::GLSLstd450UnpackHalf2x16; |
8554 | 0 | break; |
8555 | 0 | case glslang::EOpPackSnorm4x8: |
8556 | 0 | libCall = spv::GLSLstd450PackSnorm4x8; |
8557 | 0 | break; |
8558 | 0 | case glslang::EOpUnpackSnorm4x8: |
8559 | 0 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
8560 | 0 | break; |
8561 | 0 | case glslang::EOpPackUnorm4x8: |
8562 | 0 | libCall = spv::GLSLstd450PackUnorm4x8; |
8563 | 0 | break; |
8564 | 0 | case glslang::EOpUnpackUnorm4x8: |
8565 | 0 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
8566 | 0 | break; |
8567 | 0 | case glslang::EOpPackDouble2x32: |
8568 | 0 | libCall = spv::GLSLstd450PackDouble2x32; |
8569 | 0 | break; |
8570 | 0 | case glslang::EOpUnpackDouble2x32: |
8571 | 0 | libCall = spv::GLSLstd450UnpackDouble2x32; |
8572 | 0 | break; |
8573 | | |
8574 | 0 | case glslang::EOpPackInt2x32: |
8575 | 0 | case glslang::EOpUnpackInt2x32: |
8576 | 0 | case glslang::EOpPackUint2x32: |
8577 | 0 | case glslang::EOpUnpackUint2x32: |
8578 | 0 | case glslang::EOpPack16: |
8579 | 0 | case glslang::EOpPack32: |
8580 | 0 | case glslang::EOpPack64: |
8581 | 0 | case glslang::EOpUnpack32: |
8582 | 0 | case glslang::EOpUnpack16: |
8583 | 0 | case glslang::EOpUnpack8: |
8584 | 9 | case glslang::EOpPackInt2x16: |
8585 | 18 | case glslang::EOpUnpackInt2x16: |
8586 | 27 | case glslang::EOpPackUint2x16: |
8587 | 36 | case glslang::EOpUnpackUint2x16: |
8588 | 45 | case glslang::EOpPackInt4x16: |
8589 | 54 | case glslang::EOpUnpackInt4x16: |
8590 | 63 | case glslang::EOpPackUint4x16: |
8591 | 72 | case glslang::EOpUnpackUint4x16: |
8592 | 72 | case glslang::EOpPackFloat2x16: |
8593 | 72 | case glslang::EOpUnpackFloat2x16: |
8594 | 72 | unaryOp = spv::Op::OpBitcast; |
8595 | 72 | break; |
8596 | | |
8597 | 0 | case glslang::EOpDPdx: |
8598 | 0 | if (typeProxy == glslang::EbtFloat16) |
8599 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8600 | 0 | unaryOp = spv::Op::OpDPdx; |
8601 | 0 | break; |
8602 | 0 | case glslang::EOpDPdy: |
8603 | 0 | if (typeProxy == glslang::EbtFloat16) |
8604 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8605 | 0 | unaryOp = spv::Op::OpDPdy; |
8606 | 0 | break; |
8607 | 0 | case glslang::EOpFwidth: |
8608 | 0 | if (typeProxy == glslang::EbtFloat16) |
8609 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8610 | 0 | unaryOp = spv::Op::OpFwidth; |
8611 | 0 | break; |
8612 | | |
8613 | 0 | case glslang::EOpAny: |
8614 | 0 | unaryOp = spv::Op::OpAny; |
8615 | 0 | break; |
8616 | 0 | case glslang::EOpAll: |
8617 | 0 | unaryOp = spv::Op::OpAll; |
8618 | 0 | break; |
8619 | | |
8620 | 18 | case glslang::EOpAbs: |
8621 | 18 | if (isFloat) |
8622 | 9 | libCall = spv::GLSLstd450FAbs; |
8623 | 9 | else |
8624 | 9 | libCall = spv::GLSLstd450SAbs; |
8625 | 18 | break; |
8626 | 9 | case glslang::EOpSign: |
8627 | 9 | if (isFloat) |
8628 | 0 | libCall = spv::GLSLstd450FSign; |
8629 | 9 | else |
8630 | 9 | libCall = spv::GLSLstd450SSign; |
8631 | 9 | break; |
8632 | | |
8633 | 0 | case glslang::EOpDPdxFine: |
8634 | 0 | if (typeProxy == glslang::EbtFloat16) |
8635 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8636 | 0 | unaryOp = spv::Op::OpDPdxFine; |
8637 | 0 | break; |
8638 | 0 | case glslang::EOpDPdyFine: |
8639 | 0 | if (typeProxy == glslang::EbtFloat16) |
8640 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8641 | 0 | unaryOp = spv::Op::OpDPdyFine; |
8642 | 0 | break; |
8643 | 0 | case glslang::EOpFwidthFine: |
8644 | 0 | if (typeProxy == glslang::EbtFloat16) |
8645 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8646 | 0 | unaryOp = spv::Op::OpFwidthFine; |
8647 | 0 | break; |
8648 | 0 | case glslang::EOpDPdxCoarse: |
8649 | 0 | if (typeProxy == glslang::EbtFloat16) |
8650 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8651 | 0 | unaryOp = spv::Op::OpDPdxCoarse; |
8652 | 0 | break; |
8653 | 0 | case glslang::EOpDPdyCoarse: |
8654 | 0 | if (typeProxy == glslang::EbtFloat16) |
8655 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8656 | 0 | unaryOp = spv::Op::OpDPdyCoarse; |
8657 | 0 | break; |
8658 | 0 | case glslang::EOpFwidthCoarse: |
8659 | 0 | if (typeProxy == glslang::EbtFloat16) |
8660 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8661 | 0 | unaryOp = spv::Op::OpFwidthCoarse; |
8662 | 0 | break; |
8663 | 10 | case glslang::EOpRayQueryProceed: |
8664 | 10 | unaryOp = spv::Op::OpRayQueryProceedKHR; |
8665 | 10 | break; |
8666 | 10 | case glslang::EOpRayQueryGetRayTMin: |
8667 | 10 | unaryOp = spv::Op::OpRayQueryGetRayTMinKHR; |
8668 | 10 | break; |
8669 | 10 | case glslang::EOpRayQueryGetRayFlags: |
8670 | 10 | unaryOp = spv::Op::OpRayQueryGetRayFlagsKHR; |
8671 | 10 | break; |
8672 | 10 | case glslang::EOpRayQueryGetWorldRayOrigin: |
8673 | 10 | unaryOp = spv::Op::OpRayQueryGetWorldRayOriginKHR; |
8674 | 10 | break; |
8675 | 10 | case glslang::EOpRayQueryGetWorldRayDirection: |
8676 | 10 | unaryOp = spv::Op::OpRayQueryGetWorldRayDirectionKHR; |
8677 | 10 | break; |
8678 | 10 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
8679 | 10 | unaryOp = spv::Op::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
8680 | 10 | break; |
8681 | 0 | case glslang::EOpInterpolateAtCentroid: |
8682 | 0 | if (typeProxy == glslang::EbtFloat16) |
8683 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
8684 | 0 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
8685 | 0 | break; |
8686 | 0 | case glslang::EOpAtomicCounterIncrement: |
8687 | 0 | case glslang::EOpAtomicCounterDecrement: |
8688 | 0 | case glslang::EOpAtomicCounter: |
8689 | 0 | { |
8690 | | // Handle all of the atomics in one place, in createAtomicOperation() |
8691 | 0 | std::vector<spv::Id> operands; |
8692 | 0 | operands.push_back(operand); |
8693 | 0 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags, opType); |
8694 | 0 | } |
8695 | | |
8696 | 0 | case glslang::EOpBitFieldReverse: |
8697 | 0 | unaryOp = spv::Op::OpBitReverse; |
8698 | 0 | break; |
8699 | 0 | case glslang::EOpBitCount: |
8700 | 0 | unaryOp = spv::Op::OpBitCount; |
8701 | 0 | break; |
8702 | 0 | case glslang::EOpFindLSB: |
8703 | 0 | libCall = spv::GLSLstd450FindILsb; |
8704 | 0 | break; |
8705 | 0 | case glslang::EOpFindMSB: |
8706 | 0 | if (isUnsigned) |
8707 | 0 | libCall = spv::GLSLstd450FindUMsb; |
8708 | 0 | else |
8709 | 0 | libCall = spv::GLSLstd450FindSMsb; |
8710 | 0 | break; |
8711 | | |
8712 | 0 | case glslang::EOpCountLeadingZeros: |
8713 | 0 | builder.addCapability(spv::Capability::IntegerFunctions2INTEL); |
8714 | 0 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
8715 | 0 | unaryOp = spv::Op::OpUCountLeadingZerosINTEL; |
8716 | 0 | break; |
8717 | | |
8718 | 0 | case glslang::EOpCountTrailingZeros: |
8719 | 0 | builder.addCapability(spv::Capability::IntegerFunctions2INTEL); |
8720 | 0 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
8721 | 0 | unaryOp = spv::Op::OpUCountTrailingZerosINTEL; |
8722 | 0 | break; |
8723 | | |
8724 | 0 | case glslang::EOpBallot: |
8725 | 0 | case glslang::EOpReadFirstInvocation: |
8726 | 0 | case glslang::EOpAnyInvocation: |
8727 | 0 | case glslang::EOpAllInvocations: |
8728 | 0 | case glslang::EOpAllInvocationsEqual: |
8729 | 0 | case glslang::EOpMinInvocations: |
8730 | 0 | case glslang::EOpMaxInvocations: |
8731 | 0 | case glslang::EOpAddInvocations: |
8732 | 0 | case glslang::EOpMinInvocationsNonUniform: |
8733 | 0 | case glslang::EOpMaxInvocationsNonUniform: |
8734 | 0 | case glslang::EOpAddInvocationsNonUniform: |
8735 | 0 | case glslang::EOpMinInvocationsInclusiveScan: |
8736 | 0 | case glslang::EOpMaxInvocationsInclusiveScan: |
8737 | 0 | case glslang::EOpAddInvocationsInclusiveScan: |
8738 | 0 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
8739 | 0 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
8740 | 0 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
8741 | 0 | case glslang::EOpMinInvocationsExclusiveScan: |
8742 | 0 | case glslang::EOpMaxInvocationsExclusiveScan: |
8743 | 0 | case glslang::EOpAddInvocationsExclusiveScan: |
8744 | 0 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
8745 | 0 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
8746 | 0 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
8747 | 0 | { |
8748 | 0 | std::vector<spv::Id> operands; |
8749 | 0 | operands.push_back(operand); |
8750 | 0 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
8751 | 0 | } |
8752 | 0 | case glslang::EOpSubgroupAll: |
8753 | 0 | case glslang::EOpSubgroupAny: |
8754 | 0 | case glslang::EOpSubgroupAllEqual: |
8755 | 0 | case glslang::EOpSubgroupBroadcastFirst: |
8756 | 0 | case glslang::EOpSubgroupBallot: |
8757 | 0 | case glslang::EOpSubgroupInverseBallot: |
8758 | 0 | case glslang::EOpSubgroupBallotBitCount: |
8759 | 0 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
8760 | 0 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
8761 | 0 | case glslang::EOpSubgroupBallotFindLSB: |
8762 | 0 | case glslang::EOpSubgroupBallotFindMSB: |
8763 | 0 | case glslang::EOpSubgroupAdd: |
8764 | 0 | case glslang::EOpSubgroupMul: |
8765 | 0 | case glslang::EOpSubgroupMin: |
8766 | 0 | case glslang::EOpSubgroupMax: |
8767 | 0 | case glslang::EOpSubgroupAnd: |
8768 | 0 | case glslang::EOpSubgroupOr: |
8769 | 0 | case glslang::EOpSubgroupXor: |
8770 | 0 | case glslang::EOpSubgroupInclusiveAdd: |
8771 | 0 | case glslang::EOpSubgroupInclusiveMul: |
8772 | 0 | case glslang::EOpSubgroupInclusiveMin: |
8773 | 0 | case glslang::EOpSubgroupInclusiveMax: |
8774 | 0 | case glslang::EOpSubgroupInclusiveAnd: |
8775 | 0 | case glslang::EOpSubgroupInclusiveOr: |
8776 | 0 | case glslang::EOpSubgroupInclusiveXor: |
8777 | 0 | case glslang::EOpSubgroupExclusiveAdd: |
8778 | 0 | case glslang::EOpSubgroupExclusiveMul: |
8779 | 0 | case glslang::EOpSubgroupExclusiveMin: |
8780 | 0 | case glslang::EOpSubgroupExclusiveMax: |
8781 | 0 | case glslang::EOpSubgroupExclusiveAnd: |
8782 | 0 | case glslang::EOpSubgroupExclusiveOr: |
8783 | 0 | case glslang::EOpSubgroupExclusiveXor: |
8784 | 0 | case glslang::EOpSubgroupQuadSwapHorizontal: |
8785 | 0 | case glslang::EOpSubgroupQuadSwapVertical: |
8786 | 0 | case glslang::EOpSubgroupQuadSwapDiagonal: |
8787 | 0 | case glslang::EOpSubgroupQuadAll: |
8788 | 0 | case glslang::EOpSubgroupQuadAny: { |
8789 | 0 | std::vector<spv::Id> operands; |
8790 | 0 | operands.push_back(operand); |
8791 | 0 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
8792 | 0 | } |
8793 | 0 | case glslang::EOpMbcnt: |
8794 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
8795 | 0 | libCall = spv::MbcntAMD; |
8796 | 0 | break; |
8797 | | |
8798 | 0 | case glslang::EOpCubeFaceIndex: |
8799 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
8800 | 0 | libCall = spv::CubeFaceIndexAMD; |
8801 | 0 | break; |
8802 | | |
8803 | 0 | case glslang::EOpCubeFaceCoord: |
8804 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
8805 | 0 | libCall = spv::CubeFaceCoordAMD; |
8806 | 0 | break; |
8807 | 0 | case glslang::EOpSubgroupPartition: |
8808 | 0 | unaryOp = spv::Op::OpGroupNonUniformPartitionNV; |
8809 | 0 | break; |
8810 | 0 | case glslang::EOpConstructReference: |
8811 | 0 | unaryOp = spv::Op::OpBitcast; |
8812 | 0 | break; |
8813 | | |
8814 | 0 | case glslang::EOpConvUint64ToAccStruct: |
8815 | 0 | case glslang::EOpConvUvec2ToAccStruct: |
8816 | 0 | unaryOp = spv::Op::OpConvertUToAccelerationStructureKHR; |
8817 | 0 | break; |
8818 | | |
8819 | 0 | case glslang::EOpHitObjectIsEmptyNV: |
8820 | 0 | unaryOp = spv::Op::OpHitObjectIsEmptyNV; |
8821 | 0 | break; |
8822 | | |
8823 | 0 | case glslang::EOpHitObjectIsEmptyEXT: |
8824 | 0 | unaryOp = spv::Op::OpHitObjectIsEmptyEXT; |
8825 | 0 | break; |
8826 | | |
8827 | 0 | case glslang::EOpHitObjectIsMissNV: |
8828 | 0 | unaryOp = spv::Op::OpHitObjectIsMissNV; |
8829 | 0 | break; |
8830 | | |
8831 | 0 | case glslang::EOpHitObjectIsMissEXT: |
8832 | 0 | unaryOp = spv::Op::OpHitObjectIsMissEXT; |
8833 | 0 | break; |
8834 | | |
8835 | 0 | case glslang::EOpHitObjectIsHitNV: |
8836 | 0 | unaryOp = spv::Op::OpHitObjectIsHitNV; |
8837 | 0 | break; |
8838 | | |
8839 | 0 | case glslang::EOpHitObjectIsHitEXT: |
8840 | 0 | unaryOp = spv::Op::OpHitObjectIsHitEXT; |
8841 | 0 | break; |
8842 | | |
8843 | 0 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
8844 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectRayOriginNV; |
8845 | 0 | break; |
8846 | | |
8847 | 0 | case glslang::EOpHitObjectGetObjectRayOriginEXT: |
8848 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectRayOriginEXT; |
8849 | 0 | break; |
8850 | | |
8851 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
8852 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectRayDirectionNV; |
8853 | 0 | break; |
8854 | | |
8855 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionEXT: |
8856 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectRayDirectionEXT; |
8857 | 0 | break; |
8858 | | |
8859 | 0 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
8860 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldRayOriginNV; |
8861 | 0 | break; |
8862 | | |
8863 | 0 | case glslang::EOpHitObjectGetWorldRayOriginEXT: |
8864 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldRayOriginEXT; |
8865 | 0 | break; |
8866 | | |
8867 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
8868 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldRayDirectionNV; |
8869 | 0 | break; |
8870 | | |
8871 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionEXT: |
8872 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldRayDirectionEXT; |
8873 | 0 | break; |
8874 | | |
8875 | 0 | case glslang::EOpHitObjectGetObjectToWorldNV: |
8876 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectToWorldNV; |
8877 | 0 | break; |
8878 | | |
8879 | 0 | case glslang::EOpHitObjectGetObjectToWorldEXT: |
8880 | 0 | unaryOp = spv::Op::OpHitObjectGetObjectToWorldEXT; |
8881 | 0 | break; |
8882 | | |
8883 | 0 | case glslang::EOpHitObjectGetWorldToObjectNV: |
8884 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldToObjectNV; |
8885 | 0 | break; |
8886 | | |
8887 | 0 | case glslang::EOpHitObjectGetWorldToObjectEXT: |
8888 | 0 | unaryOp = spv::Op::OpHitObjectGetWorldToObjectEXT; |
8889 | 0 | break; |
8890 | | |
8891 | 0 | case glslang::EOpHitObjectGetRayTMinNV: |
8892 | 0 | unaryOp = spv::Op::OpHitObjectGetRayTMinNV; |
8893 | 0 | break; |
8894 | | |
8895 | 0 | case glslang::EOpHitObjectGetRayTMinEXT: |
8896 | 0 | unaryOp = spv::Op::OpHitObjectGetRayTMinEXT; |
8897 | 0 | break; |
8898 | | |
8899 | 0 | case glslang::EOpHitObjectGetRayTMaxNV: |
8900 | 0 | unaryOp = spv::Op::OpHitObjectGetRayTMaxNV; |
8901 | 0 | break; |
8902 | | |
8903 | 0 | case glslang::EOpHitObjectGetRayTMaxEXT: |
8904 | 0 | unaryOp = spv::Op::OpHitObjectGetRayTMaxEXT; |
8905 | 0 | break; |
8906 | | |
8907 | 0 | case glslang::EOpHitObjectGetRayFlagsEXT: |
8908 | 0 | unaryOp = spv::Op::OpHitObjectGetRayFlagsEXT; |
8909 | 0 | break; |
8910 | | |
8911 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
8912 | 0 | unaryOp = spv::Op::OpHitObjectGetPrimitiveIndexNV; |
8913 | 0 | break; |
8914 | | |
8915 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexEXT: |
8916 | 0 | unaryOp = spv::Op::OpHitObjectGetPrimitiveIndexEXT; |
8917 | 0 | break; |
8918 | | |
8919 | 0 | case glslang::EOpHitObjectGetInstanceIdNV: |
8920 | 0 | unaryOp = spv::Op::OpHitObjectGetInstanceIdNV; |
8921 | 0 | break; |
8922 | | |
8923 | 0 | case glslang::EOpHitObjectGetInstanceIdEXT: |
8924 | 0 | unaryOp = spv::Op::OpHitObjectGetInstanceIdEXT; |
8925 | 0 | break; |
8926 | | |
8927 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
8928 | 0 | unaryOp = spv::Op::OpHitObjectGetInstanceCustomIndexNV; |
8929 | 0 | break; |
8930 | | |
8931 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexEXT: |
8932 | 0 | unaryOp = spv::Op::OpHitObjectGetInstanceCustomIndexEXT; |
8933 | 0 | break; |
8934 | | |
8935 | 0 | case glslang::EOpHitObjectGetGeometryIndexNV: |
8936 | 0 | unaryOp = spv::Op::OpHitObjectGetGeometryIndexNV; |
8937 | 0 | break; |
8938 | | |
8939 | 0 | case glslang::EOpHitObjectGetGeometryIndexEXT: |
8940 | 0 | unaryOp = spv::Op::OpHitObjectGetGeometryIndexEXT; |
8941 | 0 | break; |
8942 | | |
8943 | 0 | case glslang::EOpHitObjectGetHitKindNV: |
8944 | 0 | unaryOp = spv::Op::OpHitObjectGetHitKindNV; |
8945 | 0 | break; |
8946 | | |
8947 | 0 | case glslang::EOpHitObjectGetHitKindEXT: |
8948 | 0 | unaryOp = spv::Op::OpHitObjectGetHitKindEXT; |
8949 | 0 | break; |
8950 | | |
8951 | 0 | case glslang::EOpHitObjectGetCurrentTimeNV: |
8952 | 0 | unaryOp = spv::Op::OpHitObjectGetCurrentTimeNV; |
8953 | 0 | break; |
8954 | | |
8955 | 0 | case glslang::EOpHitObjectGetCurrentTimeEXT: |
8956 | 0 | unaryOp = spv::Op::OpHitObjectGetCurrentTimeEXT; |
8957 | 0 | break; |
8958 | | |
8959 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
8960 | 0 | unaryOp = spv::Op::OpHitObjectGetShaderBindingTableRecordIndexNV; |
8961 | 0 | break; |
8962 | | |
8963 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexEXT: |
8964 | 0 | unaryOp = spv::Op::OpHitObjectGetShaderBindingTableRecordIndexEXT; |
8965 | 0 | break; |
8966 | | |
8967 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
8968 | 0 | unaryOp = spv::Op::OpHitObjectGetShaderRecordBufferHandleNV; |
8969 | 0 | break; |
8970 | | |
8971 | 0 | case glslang::EOpHitObjectGetClusterIdNV: |
8972 | 0 | unaryOp = spv::Op::OpHitObjectGetClusterIdNV; |
8973 | 0 | builder.addExtension(spv::E_SPV_NV_cluster_acceleration_structure); |
8974 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
8975 | 0 | builder.addCapability(spv::Capability::RayTracingClusterAccelerationStructureNV); |
8976 | 0 | break; |
8977 | | |
8978 | 0 | case glslang::EOpHitObjectGetSpherePositionNV: |
8979 | 0 | unaryOp = spv::Op::OpHitObjectGetSpherePositionNV; |
8980 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
8981 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
8982 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
8983 | 0 | break; |
8984 | | |
8985 | 0 | case glslang::EOpHitObjectGetSphereRadiusNV: |
8986 | 0 | unaryOp = spv::Op::OpHitObjectGetSphereRadiusNV; |
8987 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
8988 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
8989 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
8990 | 0 | break; |
8991 | | |
8992 | 0 | case glslang::EOpHitObjectIsSphereHitNV: |
8993 | 0 | unaryOp = spv::Op::OpHitObjectIsSphereHitNV; |
8994 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
8995 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
8996 | 0 | builder.addCapability(spv::Capability::RayTracingSpheresGeometryNV); |
8997 | 0 | break; |
8998 | | |
8999 | 0 | case glslang::EOpHitObjectIsLSSHitNV: |
9000 | 0 | unaryOp = spv::Op::OpHitObjectIsLSSHitNV; |
9001 | 0 | builder.addExtension(spv::E_SPV_NV_linear_swept_spheres); |
9002 | 0 | builder.addCapability(spv::Capability::ShaderInvocationReorderNV); |
9003 | 0 | builder.addCapability(spv::Capability::RayTracingLinearSweptSpheresGeometryNV); |
9004 | 0 | break; |
9005 | | |
9006 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleEXT: |
9007 | 0 | unaryOp = spv::Op::OpHitObjectGetShaderRecordBufferHandleEXT; |
9008 | 0 | break; |
9009 | | |
9010 | 0 | case glslang::EOpFetchMicroTriangleVertexPositionNV: |
9011 | 0 | unaryOp = spv::Op::OpFetchMicroTriangleVertexPositionNV; |
9012 | 0 | break; |
9013 | | |
9014 | 0 | case glslang::EOpFetchMicroTriangleVertexBarycentricNV: |
9015 | 0 | unaryOp = spv::Op::OpFetchMicroTriangleVertexBarycentricNV; |
9016 | 0 | break; |
9017 | | |
9018 | 0 | case glslang::EOpCopyObject: |
9019 | 0 | unaryOp = spv::Op::OpCopyObject; |
9020 | 0 | break; |
9021 | | |
9022 | 0 | case glslang::EOpDepthAttachmentReadEXT: |
9023 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
9024 | 0 | builder.addCapability(spv::Capability::TileImageDepthReadAccessEXT); |
9025 | 0 | unaryOp = spv::Op::OpDepthAttachmentReadEXT; |
9026 | 0 | decorations.precision = spv::NoPrecision; |
9027 | 0 | break; |
9028 | 0 | case glslang::EOpStencilAttachmentReadEXT: |
9029 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
9030 | 0 | builder.addCapability(spv::Capability::TileImageStencilReadAccessEXT); |
9031 | 0 | unaryOp = spv::Op::OpStencilAttachmentReadEXT; |
9032 | 0 | decorations.precision = spv::Decoration::RelaxedPrecision; |
9033 | 0 | break; |
9034 | | |
9035 | 220 | default: |
9036 | 220 | return 0; |
9037 | 634 | } |
9038 | | |
9039 | 414 | spv::Id id; |
9040 | 414 | if (libCall >= 0) { |
9041 | 96 | std::vector<spv::Id> args; |
9042 | 96 | args.push_back(operand); |
9043 | 96 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
9044 | 318 | } else { |
9045 | 318 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
9046 | 318 | } |
9047 | | |
9048 | 414 | decorations.addNoContraction(builder, id); |
9049 | 414 | decorations.addNonUniform(builder, id); |
9050 | 414 | return builder.setPrecision(id, decorations.precision); |
9051 | 634 | } |
9052 | | |
9053 | | // Create a unary operation on a matrix |
9054 | | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
9055 | | spv::Id operand, glslang::TBasicType /* typeProxy */) |
9056 | 9 | { |
9057 | | // Handle unary operations vector by vector. |
9058 | | // The result type is the same type as the original type. |
9059 | | // The algorithm is to: |
9060 | | // - break the matrix into vectors |
9061 | | // - apply the operation to each vector |
9062 | | // - make a matrix out the vector results |
9063 | | |
9064 | | // get the types sorted out |
9065 | 9 | int numCols = builder.getNumColumns(operand); |
9066 | 9 | int numRows = builder.getNumRows(operand); |
9067 | 9 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
9068 | 9 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
9069 | 9 | std::vector<spv::Id> results; |
9070 | | |
9071 | | // do each vector op |
9072 | 27 | for (int c = 0; c < numCols; ++c) { |
9073 | 18 | std::vector<unsigned int> indexes; |
9074 | 18 | indexes.push_back(c); |
9075 | 18 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
9076 | 18 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
9077 | 18 | decorations.addNoContraction(builder, destVec); |
9078 | 18 | decorations.addNonUniform(builder, destVec); |
9079 | 18 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
9080 | 18 | } |
9081 | | |
9082 | | // put the pieces together |
9083 | 9 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
9084 | 9 | decorations.addNonUniform(builder, result); |
9085 | 9 | return result; |
9086 | 9 | } |
9087 | | |
9088 | | // For converting integers where both the bitwidth and the signedness could |
9089 | | // change, but only do the width change here. The caller is still responsible |
9090 | | // for the signedness conversion. |
9091 | | // destType is the final type that will be converted to, but this function |
9092 | | // may only be doing part of that conversion. |
9093 | | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(spv::Id operand, int vectorSize, spv::Id destType, |
9094 | | glslang::TBasicType resultBasicType, glslang::TBasicType operandBasicType) |
9095 | 191 | { |
9096 | | // Get the result type width, based on the type to convert to. |
9097 | 191 | int width = GetNumBits(resultBasicType); |
9098 | | |
9099 | | // Get the conversion operation and result type, |
9100 | | // based on the target width, but the source type. |
9101 | 191 | spv::Id type = spv::NoType; |
9102 | 191 | spv::Op convOp = spv::Op::OpNop; |
9103 | 191 | if (isTypeSignedInt(operandBasicType)) { |
9104 | 100 | convOp = spv::Op::OpSConvert; |
9105 | 100 | type = builder.makeIntType(width); |
9106 | 100 | } else { |
9107 | 91 | convOp = spv::Op::OpUConvert; |
9108 | 91 | type = builder.makeUintType(width); |
9109 | 91 | } |
9110 | | |
9111 | 191 | if (builder.getOpCode(destType) == spv::Op::OpTypeCooperativeVectorNV) { |
9112 | 0 | type = builder.makeCooperativeVectorTypeNV(type, builder.getCooperativeVectorNumComponents(destType)); |
9113 | 191 | } else if (vectorSize > 0) |
9114 | 71 | type = builder.makeVectorType(type, vectorSize); |
9115 | 120 | else if (builder.getOpCode(destType) == spv::Op::OpTypeCooperativeMatrixKHR || |
9116 | 120 | builder.getOpCode(destType) == spv::Op::OpTypeCooperativeMatrixNV) { |
9117 | |
|
9118 | 0 | type = builder.makeCooperativeMatrixTypeWithSameShape(type, destType); |
9119 | 0 | } |
9120 | | |
9121 | 191 | return builder.createUnaryOp(convOp, type, operand); |
9122 | 191 | } |
9123 | | |
9124 | | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
9125 | | spv::Id operand, glslang::TBasicType resultBasicType, glslang::TBasicType operandBasicType) |
9126 | 2.89k | { |
9127 | 2.89k | spv::Op convOp = spv::Op::OpNop; |
9128 | 2.89k | spv::Id zero = 0; |
9129 | 2.89k | spv::Id one = 0; |
9130 | | |
9131 | 2.89k | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
9132 | | |
9133 | 2.89k | if (IsOpNumericConv(op) || op == glslang::EOpConstructSaturated) { |
9134 | 1.89k | if (isTypeSignedInt(operandBasicType) && isTypeFloat(resultBasicType)) { |
9135 | 131 | convOp = spv::Op::OpConvertSToF; |
9136 | 131 | } |
9137 | 1.89k | if (isTypeUnsignedInt(operandBasicType) && isTypeFloat(resultBasicType)) { |
9138 | 99 | convOp = spv::Op::OpConvertUToF; |
9139 | 99 | } |
9140 | 1.89k | if (isTypeFloat(operandBasicType) && isTypeSignedInt(resultBasicType)) { |
9141 | 324 | convOp = spv::Op::OpConvertFToS; |
9142 | 324 | } |
9143 | 1.89k | if (isTypeFloat(operandBasicType) && isTypeUnsignedInt(resultBasicType)) { |
9144 | 333 | convOp = spv::Op::OpConvertFToU; |
9145 | 333 | } |
9146 | 1.89k | if (isTypeSignedInt(operandBasicType) && isTypeSignedInt(resultBasicType)) { |
9147 | 231 | convOp = spv::Op::OpSConvert; |
9148 | 231 | } |
9149 | 1.89k | if (isTypeUnsignedInt(operandBasicType) && isTypeUnsignedInt(resultBasicType)) { |
9150 | 169 | convOp = spv::Op::OpUConvert; |
9151 | 169 | } |
9152 | 1.89k | if (isTypeFloat(operandBasicType) && isTypeFloat(resultBasicType)) { |
9153 | 63 | convOp = spv::Op::OpFConvert; |
9154 | 63 | if (builder.isMatrixType(destType)) |
9155 | 9 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, operandBasicType); |
9156 | 63 | } |
9157 | 1.89k | if (isTypeInt(operandBasicType) && isTypeInt(resultBasicType) && |
9158 | 836 | isTypeUnsignedInt(operandBasicType) != isTypeUnsignedInt(resultBasicType)) { |
9159 | | |
9160 | 436 | if (GetNumBits(operandBasicType) != GetNumBits(resultBasicType)) { |
9161 | | // OpSConvert/OpUConvert + OpBitCast |
9162 | 191 | operand = createIntWidthConversion(operand, vectorSize, destType, resultBasicType, operandBasicType); |
9163 | 191 | } |
9164 | | |
9165 | 436 | if (builder.isInSpecConstCodeGenMode()) { |
9166 | 149 | uint32_t bits = GetNumBits(resultBasicType); |
9167 | 149 | spv::Id zeroType = builder.makeUintType(bits); |
9168 | 149 | if (bits == 64) { |
9169 | 57 | zero = builder.makeInt64Constant(zeroType, 0, false); |
9170 | 92 | } else { |
9171 | 92 | zero = builder.makeIntConstant(zeroType, 0, false); |
9172 | 92 | } |
9173 | 149 | zero = makeSmearedConstant(zero, vectorSize); |
9174 | | // Use OpIAdd, instead of OpBitcast to do the conversion when |
9175 | | // generating for OpSpecConstantOp instruction. |
9176 | 149 | return builder.createBinOp(spv::Op::OpIAdd, destType, operand, zero); |
9177 | 149 | } |
9178 | | // For normal run-time conversion instruction, use OpBitcast. |
9179 | 287 | convOp = spv::Op::OpBitcast; |
9180 | 287 | } |
9181 | 1.74k | if (resultBasicType == glslang::EbtBool) { |
9182 | 56 | uint32_t bits = GetNumBits(operandBasicType); |
9183 | 56 | if (isTypeInt(operandBasicType)) { |
9184 | 56 | spv::Id zeroType = builder.makeUintType(bits); |
9185 | 56 | if (bits == 64) { |
9186 | 20 | zero = builder.makeInt64Constant(zeroType, 0, false); |
9187 | 36 | } else { |
9188 | 36 | zero = builder.makeIntConstant(zeroType, 0, false); |
9189 | 36 | } |
9190 | 56 | zero = makeSmearedConstant(zero, vectorSize); |
9191 | 56 | return builder.createBinOp(spv::Op::OpINotEqual, destType, operand, zero); |
9192 | 56 | } else { |
9193 | 0 | assert(isTypeFloat(operandBasicType)); |
9194 | 0 | if (bits == 64) { |
9195 | 0 | zero = builder.makeDoubleConstant(0.0); |
9196 | 0 | } else if (bits == 32) { |
9197 | 0 | zero = builder.makeFloatConstant(0.0); |
9198 | 0 | } else { |
9199 | 0 | assert(bits == 16); |
9200 | 0 | zero = builder.makeFloat16Constant(0.0); |
9201 | 0 | } |
9202 | 0 | zero = makeSmearedConstant(zero, vectorSize); |
9203 | 0 | return builder.createBinOp(spv::Op::OpFUnordNotEqual, destType, operand, zero); |
9204 | 0 | } |
9205 | 56 | } |
9206 | 1.68k | if (operandBasicType == glslang::EbtBool) { |
9207 | 57 | uint32_t bits = GetNumBits(resultBasicType); |
9208 | 57 | convOp = spv::Op::OpSelect; |
9209 | 57 | if (isTypeInt(resultBasicType)) { |
9210 | 55 | spv::Id zeroType = isTypeSignedInt(resultBasicType) ? builder.makeIntType(bits) : builder.makeUintType(bits); |
9211 | 55 | if (bits == 64) { |
9212 | 20 | zero = builder.makeInt64Constant(zeroType, 0, false); |
9213 | 20 | one = builder.makeInt64Constant(zeroType, 1, false); |
9214 | 35 | } else { |
9215 | 35 | zero = builder.makeIntConstant(zeroType, 0, false); |
9216 | 35 | one = builder.makeIntConstant(zeroType, 1, false); |
9217 | 35 | } |
9218 | 55 | } else { |
9219 | 2 | assert(isTypeFloat(resultBasicType)); |
9220 | 2 | if (bits == 64) { |
9221 | 0 | zero = builder.makeDoubleConstant(0.0); |
9222 | 0 | one = builder.makeDoubleConstant(1.0); |
9223 | 2 | } else if (bits == 32) { |
9224 | 2 | zero = builder.makeFloatConstant(0.0); |
9225 | 2 | one = builder.makeFloatConstant(1.0); |
9226 | 2 | } else { |
9227 | 0 | assert(bits == 16); |
9228 | 0 | zero = builder.makeFloat16Constant(0.0); |
9229 | 0 | one = builder.makeFloat16Constant(1.0); |
9230 | 0 | } |
9231 | 2 | } |
9232 | 57 | } |
9233 | 1.68k | } |
9234 | | |
9235 | 2.67k | if (convOp == spv::Op::OpNop) { |
9236 | 994 | switch (op) { |
9237 | 171 | case glslang::EOpConvUint64ToPtr: |
9238 | 171 | convOp = spv::Op::OpConvertUToPtr; |
9239 | 171 | break; |
9240 | 180 | case glslang::EOpConvPtrToUint64: |
9241 | 180 | convOp = spv::Op::OpConvertPtrToU; |
9242 | 180 | break; |
9243 | 0 | case glslang::EOpConvPtrToUvec2: |
9244 | 9 | case glslang::EOpConvUvec2ToPtr: |
9245 | 9 | convOp = spv::Op::OpBitcast; |
9246 | 9 | break; |
9247 | | |
9248 | 634 | default: |
9249 | 634 | break; |
9250 | 994 | } |
9251 | 994 | } |
9252 | | |
9253 | 2.67k | spv::Id result = 0; |
9254 | 2.67k | if (convOp == spv::Op::OpNop) |
9255 | 634 | return result; |
9256 | | |
9257 | 2.04k | if (convOp == spv::Op::OpSelect) { |
9258 | 57 | zero = makeSmearedConstant(zero, vectorSize); |
9259 | 57 | one = makeSmearedConstant(one, vectorSize); |
9260 | 57 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
9261 | 57 | } else |
9262 | 1.98k | result = builder.createUnaryOp(convOp, destType, operand); |
9263 | | |
9264 | 2.04k | result = builder.setPrecision(result, decorations.precision); |
9265 | 2.04k | decorations.addNonUniform(builder, result); |
9266 | 2.04k | return result; |
9267 | 2.67k | } |
9268 | | |
9269 | | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
9270 | 319 | { |
9271 | 319 | if (vectorSize == 0) |
9272 | 267 | return constant; |
9273 | | |
9274 | 52 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
9275 | 52 | std::vector<spv::Id> components; |
9276 | 156 | for (int c = 0; c < vectorSize; ++c) |
9277 | 104 | components.push_back(constant); |
9278 | 52 | return builder.makeCompositeConstant(vectorTypeId, components); |
9279 | 319 | } |
9280 | | |
9281 | | // For glslang ops that map to SPV atomic opCodes |
9282 | | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
9283 | | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
9284 | | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags, const glslang::TType &opType) |
9285 | 1.57k | { |
9286 | 1.57k | spv::Op opCode = spv::Op::OpNop; |
9287 | | |
9288 | 1.57k | switch (op) { |
9289 | 38 | case glslang::EOpAtomicAdd: |
9290 | 344 | case glslang::EOpImageAtomicAdd: |
9291 | 344 | case glslang::EOpAtomicCounterAdd: |
9292 | 344 | opCode = spv::Op::OpAtomicIAdd; |
9293 | 344 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
9294 | 304 | opCode = spv::Op::OpAtomicFAddEXT; |
9295 | 304 | if (typeProxy == glslang::EbtFloat16 && |
9296 | 304 | (opType.getVectorSize() == 2 || opType.getVectorSize() == 4)) { |
9297 | 304 | builder.addExtension(spv::E_SPV_NV_shader_atomic_fp16_vector); |
9298 | 304 | builder.addCapability(spv::Capability::AtomicFloat16VectorNV); |
9299 | 304 | } else { |
9300 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add); |
9301 | 0 | if (typeProxy == glslang::EbtFloat16) { |
9302 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float16_add); |
9303 | 0 | builder.addCapability(spv::Capability::AtomicFloat16AddEXT); |
9304 | 0 | } else if (typeProxy == glslang::EbtFloat) { |
9305 | 0 | builder.addCapability(spv::Capability::AtomicFloat32AddEXT); |
9306 | 0 | } else { |
9307 | 0 | builder.addCapability(spv::Capability::AtomicFloat64AddEXT); |
9308 | 0 | } |
9309 | 0 | } |
9310 | 304 | } |
9311 | 344 | break; |
9312 | 0 | case glslang::EOpAtomicSubtract: |
9313 | 0 | case glslang::EOpAtomicCounterSubtract: |
9314 | 0 | opCode = spv::Op::OpAtomicISub; |
9315 | 0 | break; |
9316 | 38 | case glslang::EOpAtomicMin: |
9317 | 344 | case glslang::EOpImageAtomicMin: |
9318 | 344 | case glslang::EOpAtomicCounterMin: |
9319 | 344 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
9320 | 304 | opCode = spv::Op::OpAtomicFMinEXT; |
9321 | 304 | if (typeProxy == glslang::EbtFloat16 && |
9322 | 304 | (opType.getVectorSize() == 2 || opType.getVectorSize() == 4)) { |
9323 | 304 | builder.addExtension(spv::E_SPV_NV_shader_atomic_fp16_vector); |
9324 | 304 | builder.addCapability(spv::Capability::AtomicFloat16VectorNV); |
9325 | 304 | } else { |
9326 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_min_max); |
9327 | 0 | if (typeProxy == glslang::EbtFloat16) |
9328 | 0 | builder.addCapability(spv::Capability::AtomicFloat16MinMaxEXT); |
9329 | 0 | else if (typeProxy == glslang::EbtFloat) |
9330 | 0 | builder.addCapability(spv::Capability::AtomicFloat32MinMaxEXT); |
9331 | 0 | else |
9332 | 0 | builder.addCapability(spv::Capability::AtomicFloat64MinMaxEXT); |
9333 | 0 | } |
9334 | 304 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
9335 | 20 | opCode = spv::Op::OpAtomicUMin; |
9336 | 20 | } else { |
9337 | 20 | opCode = spv::Op::OpAtomicSMin; |
9338 | 20 | } |
9339 | 344 | break; |
9340 | 38 | case glslang::EOpAtomicMax: |
9341 | 344 | case glslang::EOpImageAtomicMax: |
9342 | 344 | case glslang::EOpAtomicCounterMax: |
9343 | 344 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
9344 | 304 | opCode = spv::Op::OpAtomicFMaxEXT; |
9345 | 304 | if (typeProxy == glslang::EbtFloat16 && |
9346 | 304 | (opType.getVectorSize() == 2 || opType.getVectorSize() == 4)) { |
9347 | 304 | builder.addExtension(spv::E_SPV_NV_shader_atomic_fp16_vector); |
9348 | 304 | builder.addCapability(spv::Capability::AtomicFloat16VectorNV); |
9349 | 304 | } else { |
9350 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_min_max); |
9351 | 0 | if (typeProxy == glslang::EbtFloat16) |
9352 | 0 | builder.addCapability(spv::Capability::AtomicFloat16MinMaxEXT); |
9353 | 0 | else if (typeProxy == glslang::EbtFloat) |
9354 | 0 | builder.addCapability(spv::Capability::AtomicFloat32MinMaxEXT); |
9355 | 0 | else |
9356 | 0 | builder.addCapability(spv::Capability::AtomicFloat64MinMaxEXT); |
9357 | 0 | } |
9358 | 304 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
9359 | 20 | opCode = spv::Op::OpAtomicUMax; |
9360 | 20 | } else { |
9361 | 20 | opCode = spv::Op::OpAtomicSMax; |
9362 | 20 | } |
9363 | 344 | break; |
9364 | 0 | case glslang::EOpAtomicAnd: |
9365 | 40 | case glslang::EOpImageAtomicAnd: |
9366 | 40 | case glslang::EOpAtomicCounterAnd: |
9367 | 40 | opCode = spv::Op::OpAtomicAnd; |
9368 | 40 | break; |
9369 | 0 | case glslang::EOpAtomicOr: |
9370 | 40 | case glslang::EOpImageAtomicOr: |
9371 | 40 | case glslang::EOpAtomicCounterOr: |
9372 | 40 | opCode = spv::Op::OpAtomicOr; |
9373 | 40 | break; |
9374 | 0 | case glslang::EOpAtomicXor: |
9375 | 40 | case glslang::EOpImageAtomicXor: |
9376 | 40 | case glslang::EOpAtomicCounterXor: |
9377 | 40 | opCode = spv::Op::OpAtomicXor; |
9378 | 40 | break; |
9379 | 38 | case glslang::EOpAtomicExchange: |
9380 | 344 | case glslang::EOpImageAtomicExchange: |
9381 | 344 | case glslang::EOpAtomicCounterExchange: |
9382 | 344 | if ((typeProxy == glslang::EbtFloat16) && |
9383 | 304 | (opType.getVectorSize() == 2 || opType.getVectorSize() == 4)) { |
9384 | 304 | builder.addExtension(spv::E_SPV_NV_shader_atomic_fp16_vector); |
9385 | 304 | builder.addCapability(spv::Capability::AtomicFloat16VectorNV); |
9386 | 304 | } |
9387 | | |
9388 | 344 | opCode = spv::Op::OpAtomicExchange; |
9389 | 344 | break; |
9390 | 0 | case glslang::EOpAtomicCompSwap: |
9391 | 40 | case glslang::EOpImageAtomicCompSwap: |
9392 | 40 | case glslang::EOpAtomicCounterCompSwap: |
9393 | 40 | opCode = spv::Op::OpAtomicCompareExchange; |
9394 | 40 | break; |
9395 | 0 | case glslang::EOpAtomicCounterIncrement: |
9396 | 0 | opCode = spv::Op::OpAtomicIIncrement; |
9397 | 0 | break; |
9398 | 0 | case glslang::EOpAtomicCounterDecrement: |
9399 | 0 | opCode = spv::Op::OpAtomicIDecrement; |
9400 | 0 | break; |
9401 | 0 | case glslang::EOpAtomicCounter: |
9402 | 20 | case glslang::EOpImageAtomicLoad: |
9403 | 20 | case glslang::EOpAtomicLoad: |
9404 | 20 | opCode = spv::Op::OpAtomicLoad; |
9405 | 20 | break; |
9406 | 0 | case glslang::EOpAtomicStore: |
9407 | 20 | case glslang::EOpImageAtomicStore: |
9408 | 20 | opCode = spv::Op::OpAtomicStore; |
9409 | 20 | break; |
9410 | 0 | default: |
9411 | 0 | assert(0); |
9412 | 0 | break; |
9413 | 1.57k | } |
9414 | | |
9415 | 1.57k | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
9416 | 360 | builder.addCapability(spv::Capability::Int64Atomics); |
9417 | | |
9418 | | // Sort out the operands |
9419 | | // - mapping from glslang -> SPV |
9420 | | // - there are extra SPV operands that are optional in glslang |
9421 | | // - compare-exchange swaps the value and comparator |
9422 | | // - compare-exchange has an extra memory semantics |
9423 | | // - EOpAtomicCounterDecrement needs a post decrement |
9424 | 1.57k | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
9425 | | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
9426 | 1.57k | spv::Id scopeId; |
9427 | 1.57k | if (glslangIntermediate->usingVulkanMemoryModel()) { |
9428 | 0 | scopeId = builder.makeUintConstant(spv::Scope::QueueFamilyKHR); |
9429 | 1.57k | } else { |
9430 | 1.57k | scopeId = builder.makeUintConstant(spv::Scope::Device); |
9431 | 1.57k | } |
9432 | | // semantics default to relaxed |
9433 | 1.57k | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
9434 | 1.06k | glslangIntermediate->usingVulkanMemoryModel() ? |
9435 | 0 | spv::MemorySemanticsMask::Volatile : |
9436 | 1.57k | spv::MemorySemanticsMask::MaskNone); |
9437 | 1.57k | spv::Id semanticsId2 = semanticsId; |
9438 | | |
9439 | 1.57k | pointerId = operands[0]; |
9440 | 1.57k | if (opCode == spv::Op::OpAtomicIIncrement || opCode == spv::Op::OpAtomicIDecrement) { |
9441 | | // no additional operands |
9442 | 1.57k | } else if (opCode == spv::Op::OpAtomicCompareExchange) { |
9443 | 40 | compareId = operands[1]; |
9444 | 40 | valueId = operands[2]; |
9445 | 40 | if (operands.size() > 3) { |
9446 | 20 | scopeId = operands[3]; |
9447 | 20 | semanticsId = builder.makeUintConstant( |
9448 | 20 | builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
9449 | 20 | semanticsId2 = builder.makeUintConstant( |
9450 | 20 | builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
9451 | 20 | } |
9452 | 1.53k | } else if (opCode == spv::Op::OpAtomicLoad) { |
9453 | 20 | if (operands.size() > 1) { |
9454 | 20 | scopeId = operands[1]; |
9455 | 20 | semanticsId = builder.makeUintConstant( |
9456 | 20 | builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
9457 | 20 | } |
9458 | 1.51k | } else { |
9459 | | // atomic store or RMW |
9460 | 1.51k | valueId = operands[1]; |
9461 | 1.51k | if (operands.size() > 2) { |
9462 | 160 | scopeId = operands[2]; |
9463 | 160 | semanticsId = builder.makeUintConstant |
9464 | 160 | (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
9465 | 160 | } |
9466 | 1.51k | } |
9467 | | |
9468 | | // Check for capabilities |
9469 | 1.57k | auto const semanticsImmediate = (spv::MemorySemanticsMask)(builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2)); |
9470 | 1.57k | if (anySet(semanticsImmediate, spv::MemorySemanticsMask::MakeAvailableKHR | |
9471 | 1.57k | spv::MemorySemanticsMask::MakeVisibleKHR | |
9472 | 1.57k | spv::MemorySemanticsMask::OutputMemoryKHR | |
9473 | 1.57k | spv::MemorySemanticsMask::Volatile)) { |
9474 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
9475 | 0 | } |
9476 | | |
9477 | 1.57k | auto const scope = (spv::Scope)builder.getConstantScalar(scopeId); |
9478 | 1.57k | if (scope == spv::Scope::QueueFamily) { |
9479 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
9480 | 0 | } |
9481 | | |
9482 | 1.57k | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::Scope::Device) { |
9483 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelDeviceScopeKHR); |
9484 | 0 | } |
9485 | | |
9486 | 1.57k | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
9487 | 1.57k | spvAtomicOperands.reserve(6); |
9488 | 1.57k | spvAtomicOperands.push_back(pointerId); |
9489 | 1.57k | spvAtomicOperands.push_back(scopeId); |
9490 | 1.57k | spvAtomicOperands.push_back(semanticsId); |
9491 | 1.57k | if (opCode == spv::Op::OpAtomicCompareExchange) { |
9492 | 40 | spvAtomicOperands.push_back(semanticsId2); |
9493 | 40 | spvAtomicOperands.push_back(valueId); |
9494 | 40 | spvAtomicOperands.push_back(compareId); |
9495 | 1.53k | } else if (opCode != spv::Op::OpAtomicLoad && opCode != spv::Op::OpAtomicIIncrement && opCode != spv::Op::OpAtomicIDecrement) { |
9496 | 1.51k | spvAtomicOperands.push_back(valueId); |
9497 | 1.51k | } |
9498 | | |
9499 | 1.57k | if (opCode == spv::Op::OpAtomicStore) { |
9500 | 20 | builder.createNoResultOp(opCode, spvAtomicOperands); |
9501 | 20 | return 0; |
9502 | 1.55k | } else { |
9503 | 1.55k | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
9504 | | |
9505 | | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
9506 | | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
9507 | 1.55k | if (op == glslang::EOpAtomicCounterDecrement) |
9508 | 0 | resultId = builder.createBinOp(spv::Op::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
9509 | | |
9510 | 1.55k | return resultId; |
9511 | 1.55k | } |
9512 | 1.57k | } |
9513 | | |
9514 | | // Create group invocation operations. |
9515 | | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
9516 | | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
9517 | 0 | { |
9518 | 0 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
9519 | 0 | bool isFloat = isTypeFloat(typeProxy); |
9520 | |
|
9521 | 0 | spv::Op opCode = spv::Op::OpNop; |
9522 | 0 | std::vector<spv::IdImmediate> spvGroupOperands; |
9523 | 0 | spv::GroupOperation groupOperation = spv::GroupOperation::Max; |
9524 | |
|
9525 | 0 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
9526 | 0 | op == glslang::EOpReadInvocation) { |
9527 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
9528 | 0 | builder.addCapability(spv::Capability::SubgroupBallotKHR); |
9529 | 0 | } else if (op == glslang::EOpAnyInvocation || |
9530 | 0 | op == glslang::EOpAllInvocations || |
9531 | 0 | op == glslang::EOpAllInvocationsEqual) { |
9532 | 0 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
9533 | 0 | builder.addCapability(spv::Capability::SubgroupVoteKHR); |
9534 | 0 | } else { |
9535 | 0 | builder.addCapability(spv::Capability::Groups); |
9536 | 0 | if (op == glslang::EOpMinInvocationsNonUniform || |
9537 | 0 | op == glslang::EOpMaxInvocationsNonUniform || |
9538 | 0 | op == glslang::EOpAddInvocationsNonUniform || |
9539 | 0 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
9540 | 0 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
9541 | 0 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
9542 | 0 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
9543 | 0 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
9544 | 0 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
9545 | 0 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
9546 | |
|
9547 | 0 | switch (op) { |
9548 | 0 | case glslang::EOpMinInvocations: |
9549 | 0 | case glslang::EOpMaxInvocations: |
9550 | 0 | case glslang::EOpAddInvocations: |
9551 | 0 | case glslang::EOpMinInvocationsNonUniform: |
9552 | 0 | case glslang::EOpMaxInvocationsNonUniform: |
9553 | 0 | case glslang::EOpAddInvocationsNonUniform: |
9554 | 0 | groupOperation = spv::GroupOperation::Reduce; |
9555 | 0 | break; |
9556 | 0 | case glslang::EOpMinInvocationsInclusiveScan: |
9557 | 0 | case glslang::EOpMaxInvocationsInclusiveScan: |
9558 | 0 | case glslang::EOpAddInvocationsInclusiveScan: |
9559 | 0 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
9560 | 0 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
9561 | 0 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
9562 | 0 | groupOperation = spv::GroupOperation::InclusiveScan; |
9563 | 0 | break; |
9564 | 0 | case glslang::EOpMinInvocationsExclusiveScan: |
9565 | 0 | case glslang::EOpMaxInvocationsExclusiveScan: |
9566 | 0 | case glslang::EOpAddInvocationsExclusiveScan: |
9567 | 0 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
9568 | 0 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
9569 | 0 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
9570 | 0 | groupOperation = spv::GroupOperation::ExclusiveScan; |
9571 | 0 | break; |
9572 | 0 | default: |
9573 | 0 | break; |
9574 | 0 | } |
9575 | 0 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::Scope::Subgroup) }; |
9576 | 0 | spvGroupOperands.push_back(scope); |
9577 | 0 | if (groupOperation != spv::GroupOperation::Max) { |
9578 | 0 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
9579 | 0 | spvGroupOperands.push_back(groupOp); |
9580 | 0 | } |
9581 | 0 | } |
9582 | | |
9583 | 0 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
9584 | 0 | spv::IdImmediate op = { true, *opIt }; |
9585 | 0 | spvGroupOperands.push_back(op); |
9586 | 0 | } |
9587 | |
|
9588 | 0 | switch (op) { |
9589 | 0 | case glslang::EOpAnyInvocation: |
9590 | 0 | opCode = spv::Op::OpSubgroupAnyKHR; |
9591 | 0 | break; |
9592 | 0 | case glslang::EOpAllInvocations: |
9593 | 0 | opCode = spv::Op::OpSubgroupAllKHR; |
9594 | 0 | break; |
9595 | 0 | case glslang::EOpAllInvocationsEqual: |
9596 | 0 | opCode = spv::Op::OpSubgroupAllEqualKHR; |
9597 | 0 | break; |
9598 | 0 | case glslang::EOpReadInvocation: |
9599 | 0 | opCode = spv::Op::OpSubgroupReadInvocationKHR; |
9600 | 0 | if (builder.isVectorType(typeId)) |
9601 | 0 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
9602 | 0 | break; |
9603 | 0 | case glslang::EOpReadFirstInvocation: |
9604 | 0 | opCode = spv::Op::OpSubgroupFirstInvocationKHR; |
9605 | 0 | if (builder.isVectorType(typeId)) |
9606 | 0 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
9607 | 0 | break; |
9608 | 0 | case glslang::EOpBallot: |
9609 | 0 | { |
9610 | | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
9611 | | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
9612 | | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
9613 | | // |
9614 | | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
9615 | | // |
9616 | 0 | spv::Id uintType = builder.makeUintType(32); |
9617 | 0 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
9618 | 0 | spv::Id result = builder.createOp(spv::Op::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
9619 | |
|
9620 | 0 | std::vector<spv::Id> components; |
9621 | 0 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
9622 | 0 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
9623 | |
|
9624 | 0 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
9625 | 0 | return builder.createUnaryOp(spv::Op::OpBitcast, typeId, |
9626 | 0 | builder.createCompositeConstruct(uvec2Type, components)); |
9627 | 0 | } |
9628 | | |
9629 | 0 | case glslang::EOpMinInvocations: |
9630 | 0 | case glslang::EOpMaxInvocations: |
9631 | 0 | case glslang::EOpAddInvocations: |
9632 | 0 | case glslang::EOpMinInvocationsInclusiveScan: |
9633 | 0 | case glslang::EOpMaxInvocationsInclusiveScan: |
9634 | 0 | case glslang::EOpAddInvocationsInclusiveScan: |
9635 | 0 | case glslang::EOpMinInvocationsExclusiveScan: |
9636 | 0 | case glslang::EOpMaxInvocationsExclusiveScan: |
9637 | 0 | case glslang::EOpAddInvocationsExclusiveScan: |
9638 | 0 | if (op == glslang::EOpMinInvocations || |
9639 | 0 | op == glslang::EOpMinInvocationsInclusiveScan || |
9640 | 0 | op == glslang::EOpMinInvocationsExclusiveScan) { |
9641 | 0 | if (isFloat) |
9642 | 0 | opCode = spv::Op::OpGroupFMin; |
9643 | 0 | else { |
9644 | 0 | if (isUnsigned) |
9645 | 0 | opCode = spv::Op::OpGroupUMin; |
9646 | 0 | else |
9647 | 0 | opCode = spv::Op::OpGroupSMin; |
9648 | 0 | } |
9649 | 0 | } else if (op == glslang::EOpMaxInvocations || |
9650 | 0 | op == glslang::EOpMaxInvocationsInclusiveScan || |
9651 | 0 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
9652 | 0 | if (isFloat) |
9653 | 0 | opCode = spv::Op::OpGroupFMax; |
9654 | 0 | else { |
9655 | 0 | if (isUnsigned) |
9656 | 0 | opCode = spv::Op::OpGroupUMax; |
9657 | 0 | else |
9658 | 0 | opCode = spv::Op::OpGroupSMax; |
9659 | 0 | } |
9660 | 0 | } else { |
9661 | 0 | if (isFloat) |
9662 | 0 | opCode = spv::Op::OpGroupFAdd; |
9663 | 0 | else |
9664 | 0 | opCode = spv::Op::OpGroupIAdd; |
9665 | 0 | } |
9666 | |
|
9667 | 0 | if (builder.isVectorType(typeId)) |
9668 | 0 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
9669 | | |
9670 | 0 | break; |
9671 | 0 | case glslang::EOpMinInvocationsNonUniform: |
9672 | 0 | case glslang::EOpMaxInvocationsNonUniform: |
9673 | 0 | case glslang::EOpAddInvocationsNonUniform: |
9674 | 0 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
9675 | 0 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
9676 | 0 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
9677 | 0 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
9678 | 0 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
9679 | 0 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
9680 | 0 | if (op == glslang::EOpMinInvocationsNonUniform || |
9681 | 0 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
9682 | 0 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
9683 | 0 | if (isFloat) |
9684 | 0 | opCode = spv::Op::OpGroupFMinNonUniformAMD; |
9685 | 0 | else { |
9686 | 0 | if (isUnsigned) |
9687 | 0 | opCode = spv::Op::OpGroupUMinNonUniformAMD; |
9688 | 0 | else |
9689 | 0 | opCode = spv::Op::OpGroupSMinNonUniformAMD; |
9690 | 0 | } |
9691 | 0 | } |
9692 | 0 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
9693 | 0 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
9694 | 0 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
9695 | 0 | if (isFloat) |
9696 | 0 | opCode = spv::Op::OpGroupFMaxNonUniformAMD; |
9697 | 0 | else { |
9698 | 0 | if (isUnsigned) |
9699 | 0 | opCode = spv::Op::OpGroupUMaxNonUniformAMD; |
9700 | 0 | else |
9701 | 0 | opCode = spv::Op::OpGroupSMaxNonUniformAMD; |
9702 | 0 | } |
9703 | 0 | } |
9704 | 0 | else { |
9705 | 0 | if (isFloat) |
9706 | 0 | opCode = spv::Op::OpGroupFAddNonUniformAMD; |
9707 | 0 | else |
9708 | 0 | opCode = spv::Op::OpGroupIAddNonUniformAMD; |
9709 | 0 | } |
9710 | |
|
9711 | 0 | if (builder.isVectorType(typeId)) |
9712 | 0 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
9713 | | |
9714 | 0 | break; |
9715 | 0 | default: |
9716 | 0 | logger->missingFunctionality("invocation operation"); |
9717 | 0 | return spv::NoResult; |
9718 | 0 | } |
9719 | | |
9720 | 0 | assert(opCode != spv::Op::OpNop); |
9721 | 0 | return builder.createOp(opCode, typeId, spvGroupOperands); |
9722 | 0 | } |
9723 | | |
9724 | | // Create group invocation operations on a vector |
9725 | | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
9726 | | spv::Id typeId, std::vector<spv::Id>& operands) |
9727 | 0 | { |
9728 | 0 | assert(op == spv::Op::OpGroupFMin || op == spv::Op::OpGroupUMin || op == spv::Op::OpGroupSMin || |
9729 | 0 | op == spv::Op::OpGroupFMax || op == spv::Op::OpGroupUMax || op == spv::Op::OpGroupSMax || |
9730 | 0 | op == spv::Op::OpGroupFAdd || op == spv::Op::OpGroupIAdd || op == spv::Op::OpGroupBroadcast || |
9731 | 0 | op == spv::Op::OpSubgroupReadInvocationKHR || op == spv::Op::OpSubgroupFirstInvocationKHR || |
9732 | 0 | op == spv::Op::OpGroupFMinNonUniformAMD || op == spv::Op::OpGroupUMinNonUniformAMD || |
9733 | 0 | op == spv::Op::OpGroupSMinNonUniformAMD || |
9734 | 0 | op == spv::Op::OpGroupFMaxNonUniformAMD || op == spv::Op::OpGroupUMaxNonUniformAMD || |
9735 | 0 | op == spv::Op::OpGroupSMaxNonUniformAMD || |
9736 | 0 | op == spv::Op::OpGroupFAddNonUniformAMD || op == spv::Op::OpGroupIAddNonUniformAMD); |
9737 | | |
9738 | | // Handle group invocation operations scalar by scalar. |
9739 | | // The result type is the same type as the original type. |
9740 | | // The algorithm is to: |
9741 | | // - break the vector into scalars |
9742 | | // - apply the operation to each scalar |
9743 | | // - make a vector out the scalar results |
9744 | | |
9745 | | // get the types sorted out |
9746 | 0 | int numComponents = builder.getNumComponents(operands[0]); |
9747 | 0 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
9748 | 0 | std::vector<spv::Id> results; |
9749 | | |
9750 | | // do each scalar op |
9751 | 0 | for (int comp = 0; comp < numComponents; ++comp) { |
9752 | 0 | std::vector<unsigned int> indexes; |
9753 | 0 | indexes.push_back(comp); |
9754 | 0 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
9755 | 0 | std::vector<spv::IdImmediate> spvGroupOperands; |
9756 | 0 | if (op == spv::Op::OpSubgroupReadInvocationKHR) { |
9757 | 0 | spvGroupOperands.push_back(scalar); |
9758 | 0 | spv::IdImmediate operand = { true, operands[1] }; |
9759 | 0 | spvGroupOperands.push_back(operand); |
9760 | 0 | } else if (op == spv::Op::OpSubgroupFirstInvocationKHR) { |
9761 | 0 | spvGroupOperands.push_back(scalar); |
9762 | 0 | } else if (op == spv::Op::OpGroupBroadcast) { |
9763 | 0 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::Scope::Subgroup) }; |
9764 | 0 | spvGroupOperands.push_back(scope); |
9765 | 0 | spvGroupOperands.push_back(scalar); |
9766 | 0 | spv::IdImmediate operand = { true, operands[1] }; |
9767 | 0 | spvGroupOperands.push_back(operand); |
9768 | 0 | } else { |
9769 | 0 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::Scope::Subgroup) }; |
9770 | 0 | spvGroupOperands.push_back(scope); |
9771 | 0 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
9772 | 0 | spvGroupOperands.push_back(groupOp); |
9773 | 0 | spvGroupOperands.push_back(scalar); |
9774 | 0 | } |
9775 | |
|
9776 | 0 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
9777 | 0 | } |
9778 | | |
9779 | | // put the pieces together |
9780 | 0 | return builder.createCompositeConstruct(typeId, results); |
9781 | 0 | } |
9782 | | |
9783 | | // Create subgroup invocation operations. |
9784 | | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
9785 | | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
9786 | 0 | { |
9787 | | // Add the required capabilities. |
9788 | 0 | switch (op) { |
9789 | 0 | case glslang::EOpSubgroupElect: |
9790 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9791 | 0 | break; |
9792 | 0 | case glslang::EOpSubgroupQuadAll: |
9793 | 0 | case glslang::EOpSubgroupQuadAny: |
9794 | 0 | builder.addExtension(spv::E_SPV_KHR_quad_control); |
9795 | 0 | builder.addCapability(spv::Capability::QuadControlKHR); |
9796 | 0 | [[fallthrough]]; |
9797 | 0 | case glslang::EOpSubgroupAll: |
9798 | 0 | case glslang::EOpSubgroupAny: |
9799 | 0 | case glslang::EOpSubgroupAllEqual: |
9800 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9801 | 0 | builder.addCapability(spv::Capability::GroupNonUniformVote); |
9802 | 0 | break; |
9803 | 0 | case glslang::EOpSubgroupBroadcast: |
9804 | 0 | case glslang::EOpSubgroupBroadcastFirst: |
9805 | 0 | case glslang::EOpSubgroupBallot: |
9806 | 0 | case glslang::EOpSubgroupInverseBallot: |
9807 | 0 | case glslang::EOpSubgroupBallotBitExtract: |
9808 | 0 | case glslang::EOpSubgroupBallotBitCount: |
9809 | 0 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
9810 | 0 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
9811 | 0 | case glslang::EOpSubgroupBallotFindLSB: |
9812 | 0 | case glslang::EOpSubgroupBallotFindMSB: |
9813 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9814 | 0 | builder.addCapability(spv::Capability::GroupNonUniformBallot); |
9815 | 0 | break; |
9816 | 0 | case glslang::EOpSubgroupRotate: |
9817 | 0 | case glslang::EOpSubgroupClusteredRotate: |
9818 | 0 | builder.addExtension(spv::E_SPV_KHR_subgroup_rotate); |
9819 | 0 | builder.addCapability(spv::Capability::GroupNonUniformRotateKHR); |
9820 | 0 | break; |
9821 | 0 | case glslang::EOpSubgroupShuffle: |
9822 | 0 | case glslang::EOpSubgroupShuffleXor: |
9823 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9824 | 0 | builder.addCapability(spv::Capability::GroupNonUniformShuffle); |
9825 | 0 | break; |
9826 | 0 | case glslang::EOpSubgroupShuffleUp: |
9827 | 0 | case glslang::EOpSubgroupShuffleDown: |
9828 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9829 | 0 | builder.addCapability(spv::Capability::GroupNonUniformShuffleRelative); |
9830 | 0 | break; |
9831 | 0 | case glslang::EOpSubgroupAdd: |
9832 | 0 | case glslang::EOpSubgroupMul: |
9833 | 0 | case glslang::EOpSubgroupMin: |
9834 | 0 | case glslang::EOpSubgroupMax: |
9835 | 0 | case glslang::EOpSubgroupAnd: |
9836 | 0 | case glslang::EOpSubgroupOr: |
9837 | 0 | case glslang::EOpSubgroupXor: |
9838 | 0 | case glslang::EOpSubgroupInclusiveAdd: |
9839 | 0 | case glslang::EOpSubgroupInclusiveMul: |
9840 | 0 | case glslang::EOpSubgroupInclusiveMin: |
9841 | 0 | case glslang::EOpSubgroupInclusiveMax: |
9842 | 0 | case glslang::EOpSubgroupInclusiveAnd: |
9843 | 0 | case glslang::EOpSubgroupInclusiveOr: |
9844 | 0 | case glslang::EOpSubgroupInclusiveXor: |
9845 | 0 | case glslang::EOpSubgroupExclusiveAdd: |
9846 | 0 | case glslang::EOpSubgroupExclusiveMul: |
9847 | 0 | case glslang::EOpSubgroupExclusiveMin: |
9848 | 0 | case glslang::EOpSubgroupExclusiveMax: |
9849 | 0 | case glslang::EOpSubgroupExclusiveAnd: |
9850 | 0 | case glslang::EOpSubgroupExclusiveOr: |
9851 | 0 | case glslang::EOpSubgroupExclusiveXor: |
9852 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9853 | 0 | builder.addCapability(spv::Capability::GroupNonUniformArithmetic); |
9854 | 0 | break; |
9855 | 0 | case glslang::EOpSubgroupClusteredAdd: |
9856 | 0 | case glslang::EOpSubgroupClusteredMul: |
9857 | 0 | case glslang::EOpSubgroupClusteredMin: |
9858 | 0 | case glslang::EOpSubgroupClusteredMax: |
9859 | 0 | case glslang::EOpSubgroupClusteredAnd: |
9860 | 0 | case glslang::EOpSubgroupClusteredOr: |
9861 | 0 | case glslang::EOpSubgroupClusteredXor: |
9862 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9863 | 0 | builder.addCapability(spv::Capability::GroupNonUniformClustered); |
9864 | 0 | break; |
9865 | 0 | case glslang::EOpSubgroupQuadBroadcast: |
9866 | 0 | case glslang::EOpSubgroupQuadSwapHorizontal: |
9867 | 0 | case glslang::EOpSubgroupQuadSwapVertical: |
9868 | 0 | case glslang::EOpSubgroupQuadSwapDiagonal: |
9869 | 0 | builder.addCapability(spv::Capability::GroupNonUniform); |
9870 | 0 | builder.addCapability(spv::Capability::GroupNonUniformQuad); |
9871 | 0 | break; |
9872 | 0 | case glslang::EOpSubgroupPartitionedAdd: |
9873 | 0 | case glslang::EOpSubgroupPartitionedMul: |
9874 | 0 | case glslang::EOpSubgroupPartitionedMin: |
9875 | 0 | case glslang::EOpSubgroupPartitionedMax: |
9876 | 0 | case glslang::EOpSubgroupPartitionedAnd: |
9877 | 0 | case glslang::EOpSubgroupPartitionedOr: |
9878 | 0 | case glslang::EOpSubgroupPartitionedXor: |
9879 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
9880 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
9881 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
9882 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
9883 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
9884 | 0 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
9885 | 0 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
9886 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
9887 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
9888 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
9889 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
9890 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
9891 | 0 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
9892 | 0 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
9893 | 0 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
9894 | 0 | builder.addCapability(spv::Capability::GroupNonUniformPartitionedNV); |
9895 | 0 | break; |
9896 | 0 | default: assert(0 && "Unhandled subgroup operation!"); |
9897 | 0 | } |
9898 | | |
9899 | | |
9900 | 0 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
9901 | 0 | const bool isFloat = isTypeFloat(typeProxy); |
9902 | 0 | const bool isBool = typeProxy == glslang::EbtBool; |
9903 | |
|
9904 | 0 | spv::Op opCode = spv::Op::OpNop; |
9905 | | |
9906 | | // Figure out which opcode to use. |
9907 | 0 | switch (op) { |
9908 | 0 | case glslang::EOpSubgroupElect: opCode = spv::Op::OpGroupNonUniformElect; break; |
9909 | 0 | case glslang::EOpSubgroupQuadAll: opCode = spv::Op::OpGroupNonUniformQuadAllKHR; break; |
9910 | 0 | case glslang::EOpSubgroupAll: opCode = spv::Op::OpGroupNonUniformAll; break; |
9911 | 0 | case glslang::EOpSubgroupQuadAny: opCode = spv::Op::OpGroupNonUniformQuadAnyKHR; break; |
9912 | 0 | case glslang::EOpSubgroupAny: opCode = spv::Op::OpGroupNonUniformAny; break; |
9913 | 0 | case glslang::EOpSubgroupAllEqual: opCode = spv::Op::OpGroupNonUniformAllEqual; break; |
9914 | 0 | case glslang::EOpSubgroupBroadcast: opCode = spv::Op::OpGroupNonUniformBroadcast; break; |
9915 | 0 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::Op::OpGroupNonUniformBroadcastFirst; break; |
9916 | 0 | case glslang::EOpSubgroupBallot: opCode = spv::Op::OpGroupNonUniformBallot; break; |
9917 | 0 | case glslang::EOpSubgroupInverseBallot: opCode = spv::Op::OpGroupNonUniformInverseBallot; break; |
9918 | 0 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::Op::OpGroupNonUniformBallotBitExtract; break; |
9919 | 0 | case glslang::EOpSubgroupBallotBitCount: |
9920 | 0 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
9921 | 0 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::Op::OpGroupNonUniformBallotBitCount; break; |
9922 | 0 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::Op::OpGroupNonUniformBallotFindLSB; break; |
9923 | 0 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::Op::OpGroupNonUniformBallotFindMSB; break; |
9924 | 0 | case glslang::EOpSubgroupShuffle: opCode = spv::Op::OpGroupNonUniformShuffle; break; |
9925 | 0 | case glslang::EOpSubgroupShuffleXor: opCode = spv::Op::OpGroupNonUniformShuffleXor; break; |
9926 | 0 | case glslang::EOpSubgroupShuffleUp: opCode = spv::Op::OpGroupNonUniformShuffleUp; break; |
9927 | 0 | case glslang::EOpSubgroupShuffleDown: opCode = spv::Op::OpGroupNonUniformShuffleDown; break; |
9928 | 0 | case glslang::EOpSubgroupRotate: |
9929 | 0 | case glslang::EOpSubgroupClusteredRotate: opCode = spv::Op::OpGroupNonUniformRotateKHR; break; |
9930 | 0 | case glslang::EOpSubgroupAdd: |
9931 | 0 | case glslang::EOpSubgroupInclusiveAdd: |
9932 | 0 | case glslang::EOpSubgroupExclusiveAdd: |
9933 | 0 | case glslang::EOpSubgroupClusteredAdd: |
9934 | 0 | case glslang::EOpSubgroupPartitionedAdd: |
9935 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
9936 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
9937 | 0 | if (isFloat) { |
9938 | 0 | opCode = spv::Op::OpGroupNonUniformFAdd; |
9939 | 0 | } else { |
9940 | 0 | opCode = spv::Op::OpGroupNonUniformIAdd; |
9941 | 0 | } |
9942 | 0 | break; |
9943 | 0 | case glslang::EOpSubgroupMul: |
9944 | 0 | case glslang::EOpSubgroupInclusiveMul: |
9945 | 0 | case glslang::EOpSubgroupExclusiveMul: |
9946 | 0 | case glslang::EOpSubgroupClusteredMul: |
9947 | 0 | case glslang::EOpSubgroupPartitionedMul: |
9948 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
9949 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
9950 | 0 | if (isFloat) { |
9951 | 0 | opCode = spv::Op::OpGroupNonUniformFMul; |
9952 | 0 | } else { |
9953 | 0 | opCode = spv::Op::OpGroupNonUniformIMul; |
9954 | 0 | } |
9955 | 0 | break; |
9956 | 0 | case glslang::EOpSubgroupMin: |
9957 | 0 | case glslang::EOpSubgroupInclusiveMin: |
9958 | 0 | case glslang::EOpSubgroupExclusiveMin: |
9959 | 0 | case glslang::EOpSubgroupClusteredMin: |
9960 | 0 | case glslang::EOpSubgroupPartitionedMin: |
9961 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
9962 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
9963 | 0 | if (isFloat) { |
9964 | 0 | opCode = spv::Op::OpGroupNonUniformFMin; |
9965 | 0 | } else if (isUnsigned) { |
9966 | 0 | opCode = spv::Op::OpGroupNonUniformUMin; |
9967 | 0 | } else { |
9968 | 0 | opCode = spv::Op::OpGroupNonUniformSMin; |
9969 | 0 | } |
9970 | 0 | break; |
9971 | 0 | case glslang::EOpSubgroupMax: |
9972 | 0 | case glslang::EOpSubgroupInclusiveMax: |
9973 | 0 | case glslang::EOpSubgroupExclusiveMax: |
9974 | 0 | case glslang::EOpSubgroupClusteredMax: |
9975 | 0 | case glslang::EOpSubgroupPartitionedMax: |
9976 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
9977 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
9978 | 0 | if (isFloat) { |
9979 | 0 | opCode = spv::Op::OpGroupNonUniformFMax; |
9980 | 0 | } else if (isUnsigned) { |
9981 | 0 | opCode = spv::Op::OpGroupNonUniformUMax; |
9982 | 0 | } else { |
9983 | 0 | opCode = spv::Op::OpGroupNonUniformSMax; |
9984 | 0 | } |
9985 | 0 | break; |
9986 | 0 | case glslang::EOpSubgroupAnd: |
9987 | 0 | case glslang::EOpSubgroupInclusiveAnd: |
9988 | 0 | case glslang::EOpSubgroupExclusiveAnd: |
9989 | 0 | case glslang::EOpSubgroupClusteredAnd: |
9990 | 0 | case glslang::EOpSubgroupPartitionedAnd: |
9991 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
9992 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
9993 | 0 | if (isBool) { |
9994 | 0 | opCode = spv::Op::OpGroupNonUniformLogicalAnd; |
9995 | 0 | } else { |
9996 | 0 | opCode = spv::Op::OpGroupNonUniformBitwiseAnd; |
9997 | 0 | } |
9998 | 0 | break; |
9999 | 0 | case glslang::EOpSubgroupOr: |
10000 | 0 | case glslang::EOpSubgroupInclusiveOr: |
10001 | 0 | case glslang::EOpSubgroupExclusiveOr: |
10002 | 0 | case glslang::EOpSubgroupClusteredOr: |
10003 | 0 | case glslang::EOpSubgroupPartitionedOr: |
10004 | 0 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
10005 | 0 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
10006 | 0 | if (isBool) { |
10007 | 0 | opCode = spv::Op::OpGroupNonUniformLogicalOr; |
10008 | 0 | } else { |
10009 | 0 | opCode = spv::Op::OpGroupNonUniformBitwiseOr; |
10010 | 0 | } |
10011 | 0 | break; |
10012 | 0 | case glslang::EOpSubgroupXor: |
10013 | 0 | case glslang::EOpSubgroupInclusiveXor: |
10014 | 0 | case glslang::EOpSubgroupExclusiveXor: |
10015 | 0 | case glslang::EOpSubgroupClusteredXor: |
10016 | 0 | case glslang::EOpSubgroupPartitionedXor: |
10017 | 0 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
10018 | 0 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
10019 | 0 | if (isBool) { |
10020 | 0 | opCode = spv::Op::OpGroupNonUniformLogicalXor; |
10021 | 0 | } else { |
10022 | 0 | opCode = spv::Op::OpGroupNonUniformBitwiseXor; |
10023 | 0 | } |
10024 | 0 | break; |
10025 | 0 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::Op::OpGroupNonUniformQuadBroadcast; break; |
10026 | 0 | case glslang::EOpSubgroupQuadSwapHorizontal: |
10027 | 0 | case glslang::EOpSubgroupQuadSwapVertical: |
10028 | 0 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::Op::OpGroupNonUniformQuadSwap; break; |
10029 | 0 | default: assert(0 && "Unhandled subgroup operation!"); |
10030 | 0 | } |
10031 | | |
10032 | | // get the right Group Operation |
10033 | 0 | spv::GroupOperation groupOperation = spv::GroupOperation::Max; |
10034 | 0 | switch (op) { |
10035 | 0 | default: |
10036 | 0 | break; |
10037 | 0 | case glslang::EOpSubgroupBallotBitCount: |
10038 | 0 | case glslang::EOpSubgroupAdd: |
10039 | 0 | case glslang::EOpSubgroupMul: |
10040 | 0 | case glslang::EOpSubgroupMin: |
10041 | 0 | case glslang::EOpSubgroupMax: |
10042 | 0 | case glslang::EOpSubgroupAnd: |
10043 | 0 | case glslang::EOpSubgroupOr: |
10044 | 0 | case glslang::EOpSubgroupXor: |
10045 | 0 | groupOperation = spv::GroupOperation::Reduce; |
10046 | 0 | break; |
10047 | 0 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
10048 | 0 | case glslang::EOpSubgroupInclusiveAdd: |
10049 | 0 | case glslang::EOpSubgroupInclusiveMul: |
10050 | 0 | case glslang::EOpSubgroupInclusiveMin: |
10051 | 0 | case glslang::EOpSubgroupInclusiveMax: |
10052 | 0 | case glslang::EOpSubgroupInclusiveAnd: |
10053 | 0 | case glslang::EOpSubgroupInclusiveOr: |
10054 | 0 | case glslang::EOpSubgroupInclusiveXor: |
10055 | 0 | groupOperation = spv::GroupOperation::InclusiveScan; |
10056 | 0 | break; |
10057 | 0 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
10058 | 0 | case glslang::EOpSubgroupExclusiveAdd: |
10059 | 0 | case glslang::EOpSubgroupExclusiveMul: |
10060 | 0 | case glslang::EOpSubgroupExclusiveMin: |
10061 | 0 | case glslang::EOpSubgroupExclusiveMax: |
10062 | 0 | case glslang::EOpSubgroupExclusiveAnd: |
10063 | 0 | case glslang::EOpSubgroupExclusiveOr: |
10064 | 0 | case glslang::EOpSubgroupExclusiveXor: |
10065 | 0 | groupOperation = spv::GroupOperation::ExclusiveScan; |
10066 | 0 | break; |
10067 | 0 | case glslang::EOpSubgroupClusteredAdd: |
10068 | 0 | case glslang::EOpSubgroupClusteredMul: |
10069 | 0 | case glslang::EOpSubgroupClusteredMin: |
10070 | 0 | case glslang::EOpSubgroupClusteredMax: |
10071 | 0 | case glslang::EOpSubgroupClusteredAnd: |
10072 | 0 | case glslang::EOpSubgroupClusteredOr: |
10073 | 0 | case glslang::EOpSubgroupClusteredXor: |
10074 | 0 | groupOperation = spv::GroupOperation::ClusteredReduce; |
10075 | 0 | break; |
10076 | 0 | case glslang::EOpSubgroupPartitionedAdd: |
10077 | 0 | case glslang::EOpSubgroupPartitionedMul: |
10078 | 0 | case glslang::EOpSubgroupPartitionedMin: |
10079 | 0 | case glslang::EOpSubgroupPartitionedMax: |
10080 | 0 | case glslang::EOpSubgroupPartitionedAnd: |
10081 | 0 | case glslang::EOpSubgroupPartitionedOr: |
10082 | 0 | case glslang::EOpSubgroupPartitionedXor: |
10083 | 0 | groupOperation = spv::GroupOperation::PartitionedReduceNV; |
10084 | 0 | break; |
10085 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
10086 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
10087 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
10088 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
10089 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
10090 | 0 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
10091 | 0 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
10092 | 0 | groupOperation = spv::GroupOperation::PartitionedInclusiveScanNV; |
10093 | 0 | break; |
10094 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
10095 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
10096 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
10097 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
10098 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
10099 | 0 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
10100 | 0 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
10101 | 0 | groupOperation = spv::GroupOperation::PartitionedExclusiveScanNV; |
10102 | 0 | break; |
10103 | 0 | } |
10104 | | |
10105 | | // build the instruction |
10106 | 0 | std::vector<spv::IdImmediate> spvGroupOperands; |
10107 | | |
10108 | | // Every operation begins with the Execution Scope operand. |
10109 | 0 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::Scope::Subgroup) }; |
10110 | | // All other ops need the execution scope. Quad Control Ops don't need scope, it's always Quad. |
10111 | 0 | if (opCode != spv::Op::OpGroupNonUniformQuadAllKHR && opCode != spv::Op::OpGroupNonUniformQuadAnyKHR) { |
10112 | 0 | spvGroupOperands.push_back(executionScope); |
10113 | 0 | } |
10114 | | |
10115 | | // Next, for all operations that use a Group Operation, push that as an operand. |
10116 | 0 | if (groupOperation != spv::GroupOperation::Max) { |
10117 | 0 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
10118 | 0 | spvGroupOperands.push_back(groupOperand); |
10119 | 0 | } |
10120 | | |
10121 | | // Push back the operands next. |
10122 | 0 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
10123 | 0 | spv::IdImmediate operand = { true, *opIt }; |
10124 | 0 | spvGroupOperands.push_back(operand); |
10125 | 0 | } |
10126 | | |
10127 | | // Some opcodes have additional operands. |
10128 | 0 | spv::Id directionId = spv::NoResult; |
10129 | 0 | switch (op) { |
10130 | 0 | default: break; |
10131 | 0 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
10132 | 0 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
10133 | 0 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
10134 | 0 | } |
10135 | 0 | if (directionId != spv::NoResult) { |
10136 | 0 | spv::IdImmediate direction = { true, directionId }; |
10137 | 0 | spvGroupOperands.push_back(direction); |
10138 | 0 | } |
10139 | |
|
10140 | 0 | return builder.createOp(opCode, typeId, spvGroupOperands); |
10141 | 0 | } |
10142 | | |
10143 | | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
10144 | | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
10145 | 1.70k | { |
10146 | 1.70k | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
10147 | 1.70k | bool isFloat = isTypeFloat(typeProxy); |
10148 | | |
10149 | 1.70k | spv::Op opCode = spv::Op::OpNop; |
10150 | 1.70k | int extBuiltins = -1; |
10151 | 1.70k | int libCall = -1; |
10152 | 1.70k | size_t consumedOperands = operands.size(); |
10153 | 1.70k | spv::Id typeId0 = 0; |
10154 | 1.70k | if (consumedOperands > 0) |
10155 | 1.70k | typeId0 = builder.getTypeId(operands[0]); |
10156 | 1.70k | spv::Id typeId1 = 0; |
10157 | 1.70k | if (consumedOperands > 1) |
10158 | 1.70k | typeId1 = builder.getTypeId(operands[1]); |
10159 | 1.70k | spv::Id frexpIntType = 0; |
10160 | | |
10161 | 1.70k | switch (op) { |
10162 | 36 | case glslang::EOpMin: |
10163 | 36 | if (isFloat) |
10164 | 0 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
10165 | 36 | else if (isUnsigned) |
10166 | 18 | libCall = spv::GLSLstd450UMin; |
10167 | 18 | else |
10168 | 18 | libCall = spv::GLSLstd450SMin; |
10169 | 36 | builder.promoteScalar(precision, operands.front(), operands.back()); |
10170 | 36 | break; |
10171 | 0 | case glslang::EOpModf: |
10172 | 0 | { |
10173 | 0 | libCall = spv::GLSLstd450ModfStruct; |
10174 | 0 | assert(builder.isFloatType(builder.getScalarTypeId(typeId0))); |
10175 | | // The returned struct has two members of the same type as the first argument |
10176 | 0 | typeId = builder.makeStructResultType(typeId0, typeId0); |
10177 | 0 | consumedOperands = 1; |
10178 | 0 | } |
10179 | 0 | break; |
10180 | 62 | case glslang::EOpMax: |
10181 | 62 | if (isFloat) |
10182 | 26 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
10183 | 36 | else if (isUnsigned) |
10184 | 18 | libCall = spv::GLSLstd450UMax; |
10185 | 18 | else |
10186 | 18 | libCall = spv::GLSLstd450SMax; |
10187 | 62 | builder.promoteScalar(precision, operands.front(), operands.back()); |
10188 | 62 | break; |
10189 | 13 | case glslang::EOpPow: |
10190 | 13 | libCall = spv::GLSLstd450Pow; |
10191 | 13 | break; |
10192 | 480 | case glslang::EOpDot: |
10193 | 520 | case glslang::EOpDotPackedEXT: |
10194 | 1.00k | case glslang::EOpDotAccSatEXT: |
10195 | 1.04k | case glslang::EOpDotPackedAccSatEXT: |
10196 | 1.04k | { |
10197 | 1.04k | if (builder.isFloatType(builder.getScalarTypeId(typeId0)) || |
10198 | | // HLSL supports dot(int,int) which is just a multiply |
10199 | 1.04k | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
10200 | 0 | if (typeProxy == glslang::EbtBFloat16) { |
10201 | 0 | builder.addExtension(spv::E_SPV_KHR_bfloat16); |
10202 | 0 | builder.addCapability(spv::Capability::BFloat16DotProductKHR); |
10203 | 0 | } |
10204 | 0 | opCode = spv::Op::OpDot; |
10205 | 1.04k | } else { |
10206 | 1.04k | builder.addExtension(spv::E_SPV_KHR_integer_dot_product); |
10207 | 1.04k | builder.addCapability(spv::Capability::DotProductKHR); |
10208 | 1.04k | const unsigned int vectorSize = builder.getNumComponents(operands[0]); |
10209 | 1.04k | if (op == glslang::EOpDotPackedEXT || op == glslang::EOpDotPackedAccSatEXT) { |
10210 | 80 | builder.addCapability(spv::Capability::DotProductInput4x8BitPackedKHR); |
10211 | 960 | } else if (vectorSize == 4 && builder.getScalarTypeWidth(typeId0) == 8) { |
10212 | 80 | builder.addCapability(spv::Capability::DotProductInput4x8BitKHR); |
10213 | 880 | } else { |
10214 | 880 | builder.addCapability(spv::Capability::DotProductInputAllKHR); |
10215 | 880 | } |
10216 | 1.04k | const bool type0isSigned = builder.isIntType(builder.getScalarTypeId(typeId0)); |
10217 | 1.04k | const bool type1isSigned = builder.isIntType(builder.getScalarTypeId(typeId1)); |
10218 | 1.04k | const bool accSat = (op == glslang::EOpDotAccSatEXT || op == glslang::EOpDotPackedAccSatEXT); |
10219 | 1.04k | if (!type0isSigned && !type1isSigned) { |
10220 | 260 | opCode = accSat ? spv::Op::OpUDotAccSatKHR : spv::Op::OpUDotKHR; |
10221 | 780 | } else if (type0isSigned && type1isSigned) { |
10222 | 260 | opCode = accSat ? spv::Op::OpSDotAccSatKHR : spv::Op::OpSDotKHR; |
10223 | 520 | } else { |
10224 | 520 | opCode = accSat ? spv::Op::OpSUDotAccSatKHR : spv::Op::OpSUDotKHR; |
10225 | | // the spir-v opcode assumes the operands to be "signed, unsigned" in that order, so swap if needed |
10226 | 520 | if (type1isSigned) { |
10227 | 260 | std::swap(operands[0], operands[1]); |
10228 | 260 | } |
10229 | 520 | } |
10230 | 1.04k | std::vector<spv::IdImmediate> operands2; |
10231 | 2.60k | for (auto &o : operands) { |
10232 | 2.60k | operands2.push_back({true, o}); |
10233 | 2.60k | } |
10234 | 1.04k | if (op == glslang::EOpDotPackedEXT || op == glslang::EOpDotPackedAccSatEXT) { |
10235 | 80 | operands2.push_back({false, 0}); |
10236 | 80 | } |
10237 | 1.04k | return builder.createOp(opCode, typeId, operands2); |
10238 | 1.04k | } |
10239 | 1.04k | } |
10240 | 0 | break; |
10241 | 0 | case glslang::EOpAtan: |
10242 | 0 | libCall = spv::GLSLstd450Atan2; |
10243 | 0 | break; |
10244 | | |
10245 | 36 | case glslang::EOpClamp: |
10246 | 36 | if (isFloat) |
10247 | 0 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
10248 | 36 | else if (isUnsigned) |
10249 | 18 | libCall = spv::GLSLstd450UClamp; |
10250 | 18 | else |
10251 | 18 | libCall = spv::GLSLstd450SClamp; |
10252 | 36 | builder.promoteScalar(precision, operands.front(), operands[1]); |
10253 | 36 | builder.promoteScalar(precision, operands.front(), operands[2]); |
10254 | 36 | break; |
10255 | 36 | case glslang::EOpMix: |
10256 | 36 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
10257 | 0 | assert(isFloat); |
10258 | 0 | libCall = spv::GLSLstd450FMix; |
10259 | 36 | } else { |
10260 | 36 | opCode = spv::Op::OpSelect; |
10261 | 36 | std::swap(operands.front(), operands.back()); |
10262 | 36 | } |
10263 | 36 | builder.promoteScalar(precision, operands.front(), operands.back()); |
10264 | 36 | break; |
10265 | 0 | case glslang::EOpStep: |
10266 | 0 | libCall = spv::GLSLstd450Step; |
10267 | 0 | builder.promoteScalar(precision, operands.front(), operands.back()); |
10268 | 0 | break; |
10269 | 26 | case glslang::EOpSmoothStep: |
10270 | 26 | libCall = spv::GLSLstd450SmoothStep; |
10271 | 26 | builder.promoteScalar(precision, operands[0], operands[2]); |
10272 | 26 | builder.promoteScalar(precision, operands[1], operands[2]); |
10273 | 26 | break; |
10274 | | |
10275 | 0 | case glslang::EOpDistance: |
10276 | 0 | libCall = spv::GLSLstd450Distance; |
10277 | 0 | break; |
10278 | 0 | case glslang::EOpCross: |
10279 | 0 | libCall = spv::GLSLstd450Cross; |
10280 | 0 | break; |
10281 | 0 | case glslang::EOpFaceForward: |
10282 | 0 | libCall = spv::GLSLstd450FaceForward; |
10283 | 0 | break; |
10284 | 13 | case glslang::EOpReflect: |
10285 | 13 | libCall = spv::GLSLstd450Reflect; |
10286 | 13 | break; |
10287 | 0 | case glslang::EOpRefract: |
10288 | 0 | libCall = spv::GLSLstd450Refract; |
10289 | 0 | break; |
10290 | 0 | case glslang::EOpBarrier: |
10291 | 0 | { |
10292 | | // This is for the extended controlBarrier function, with four operands. |
10293 | | // The unextended barrier() goes through createNoArgOperation. |
10294 | 0 | assert(operands.size() == 4); |
10295 | 0 | auto const executionScope = (spv::Scope)builder.getConstantScalar(operands[0]); |
10296 | 0 | auto const memoryScope = (spv::Scope)builder.getConstantScalar(operands[1]); |
10297 | 0 | auto const semantics = (spv::MemorySemanticsMask)(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
10298 | 0 | builder.createControlBarrier(executionScope, memoryScope, |
10299 | 0 | semantics); |
10300 | 0 | if (anySet(semantics, spv::MemorySemanticsMask::MakeAvailableKHR | |
10301 | 0 | spv::MemorySemanticsMask::MakeVisibleKHR | |
10302 | 0 | spv::MemorySemanticsMask::OutputMemoryKHR | |
10303 | 0 | spv::MemorySemanticsMask::Volatile)) { |
10304 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
10305 | 0 | } |
10306 | 0 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::Scope::Device || |
10307 | 0 | memoryScope == spv::Scope::Device)) { |
10308 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelDeviceScopeKHR); |
10309 | 0 | } |
10310 | 0 | return 0; |
10311 | 1.04k | } |
10312 | 0 | break; |
10313 | 0 | case glslang::EOpMemoryBarrier: |
10314 | 0 | { |
10315 | | // This is for the extended memoryBarrier function, with three operands. |
10316 | | // The unextended memoryBarrier() goes through createNoArgOperation. |
10317 | 0 | assert(operands.size() == 3); |
10318 | 0 | auto const memoryScope = (spv::Scope)builder.getConstantScalar(operands[0]); |
10319 | 0 | auto const semantics = (spv::MemorySemanticsMask)(builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2])); |
10320 | 0 | builder.createMemoryBarrier(memoryScope, semantics); |
10321 | 0 | if (anySet(semantics, spv::MemorySemanticsMask::MakeAvailableKHR | |
10322 | 0 | spv::MemorySemanticsMask::MakeVisibleKHR | |
10323 | 0 | spv::MemorySemanticsMask::OutputMemoryKHR | |
10324 | 0 | spv::MemorySemanticsMask::Volatile)) { |
10325 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
10326 | 0 | } |
10327 | 0 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::Scope::Device) { |
10328 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelDeviceScopeKHR); |
10329 | 0 | } |
10330 | 0 | return 0; |
10331 | 1.04k | } |
10332 | 0 | break; |
10333 | | |
10334 | 0 | case glslang::EOpInterpolateAtSample: |
10335 | 0 | if (typeProxy == glslang::EbtFloat16) |
10336 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
10337 | 0 | libCall = spv::GLSLstd450InterpolateAtSample; |
10338 | 0 | break; |
10339 | 0 | case glslang::EOpInterpolateAtOffset: |
10340 | 0 | if (typeProxy == glslang::EbtFloat16) |
10341 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
10342 | 0 | libCall = spv::GLSLstd450InterpolateAtOffset; |
10343 | 0 | break; |
10344 | 0 | case glslang::EOpAddCarry: |
10345 | 0 | opCode = spv::Op::OpIAddCarry; |
10346 | 0 | typeId = builder.makeStructResultType(typeId0, typeId0); |
10347 | 0 | consumedOperands = 2; |
10348 | 0 | break; |
10349 | 0 | case glslang::EOpSubBorrow: |
10350 | 0 | opCode = spv::Op::OpISubBorrow; |
10351 | 0 | typeId = builder.makeStructResultType(typeId0, typeId0); |
10352 | 0 | consumedOperands = 2; |
10353 | 0 | break; |
10354 | 0 | case glslang::EOpUMulExtended: |
10355 | 0 | opCode = spv::Op::OpUMulExtended; |
10356 | 0 | typeId = builder.makeStructResultType(typeId0, typeId0); |
10357 | 0 | consumedOperands = 2; |
10358 | 0 | break; |
10359 | 0 | case glslang::EOpIMulExtended: |
10360 | 0 | opCode = spv::Op::OpSMulExtended; |
10361 | 0 | typeId = builder.makeStructResultType(typeId0, typeId0); |
10362 | 0 | consumedOperands = 2; |
10363 | 0 | break; |
10364 | 0 | case glslang::EOpBitfieldExtract: |
10365 | 0 | if (isUnsigned) |
10366 | 0 | opCode = spv::Op::OpBitFieldUExtract; |
10367 | 0 | else |
10368 | 0 | opCode = spv::Op::OpBitFieldSExtract; |
10369 | 0 | break; |
10370 | 0 | case glslang::EOpBitfieldInsert: |
10371 | 0 | opCode = spv::Op::OpBitFieldInsert; |
10372 | 0 | break; |
10373 | | |
10374 | 0 | case glslang::EOpFma: |
10375 | 0 | libCall = spv::GLSLstd450Fma; |
10376 | 0 | break; |
10377 | 9 | case glslang::EOpFrexp: |
10378 | 9 | { |
10379 | 9 | libCall = spv::GLSLstd450FrexpStruct; |
10380 | 9 | assert(builder.isPointerType(typeId1)); |
10381 | 9 | typeId1 = builder.getContainedTypeId(typeId1); |
10382 | 9 | int width = builder.getScalarTypeWidth(typeId1); |
10383 | 9 | if (width == 16) |
10384 | | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
10385 | 9 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
10386 | 9 | if (builder.getNumComponents(operands[0]) == 1) |
10387 | 0 | frexpIntType = builder.makeIntegerType(width, true); |
10388 | 9 | else if (builder.isCooperativeVector(operands[0])) |
10389 | 0 | frexpIntType = builder.makeCooperativeVectorTypeNV(builder.makeIntegerType(width, true), |
10390 | 0 | builder.getCooperativeVectorNumComponents(builder.getTypeId(operands[0]))); |
10391 | 9 | else |
10392 | 9 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
10393 | 9 | builder.getNumComponents(operands[0])); |
10394 | 9 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
10395 | 9 | consumedOperands = 1; |
10396 | 9 | } |
10397 | 9 | break; |
10398 | 9 | case glslang::EOpLdexp: |
10399 | 9 | libCall = spv::GLSLstd450Ldexp; |
10400 | 9 | break; |
10401 | | |
10402 | 0 | case glslang::EOpReadInvocation: |
10403 | 0 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
10404 | | |
10405 | 0 | case glslang::EOpSubgroupBroadcast: |
10406 | 0 | case glslang::EOpSubgroupBallotBitExtract: |
10407 | 0 | case glslang::EOpSubgroupShuffle: |
10408 | 0 | case glslang::EOpSubgroupShuffleXor: |
10409 | 0 | case glslang::EOpSubgroupShuffleUp: |
10410 | 0 | case glslang::EOpSubgroupShuffleDown: |
10411 | 0 | case glslang::EOpSubgroupRotate: |
10412 | 0 | case glslang::EOpSubgroupClusteredRotate: |
10413 | 0 | case glslang::EOpSubgroupClusteredAdd: |
10414 | 0 | case glslang::EOpSubgroupClusteredMul: |
10415 | 0 | case glslang::EOpSubgroupClusteredMin: |
10416 | 0 | case glslang::EOpSubgroupClusteredMax: |
10417 | 0 | case glslang::EOpSubgroupClusteredAnd: |
10418 | 0 | case glslang::EOpSubgroupClusteredOr: |
10419 | 0 | case glslang::EOpSubgroupClusteredXor: |
10420 | 0 | case glslang::EOpSubgroupQuadBroadcast: |
10421 | 0 | case glslang::EOpSubgroupPartitionedAdd: |
10422 | 0 | case glslang::EOpSubgroupPartitionedMul: |
10423 | 0 | case glslang::EOpSubgroupPartitionedMin: |
10424 | 0 | case glslang::EOpSubgroupPartitionedMax: |
10425 | 0 | case glslang::EOpSubgroupPartitionedAnd: |
10426 | 0 | case glslang::EOpSubgroupPartitionedOr: |
10427 | 0 | case glslang::EOpSubgroupPartitionedXor: |
10428 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
10429 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
10430 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
10431 | 0 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
10432 | 0 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
10433 | 0 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
10434 | 0 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
10435 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
10436 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
10437 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
10438 | 0 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
10439 | 0 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
10440 | 0 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
10441 | 0 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
10442 | 0 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
10443 | | |
10444 | 0 | case glslang::EOpSwizzleInvocations: |
10445 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
10446 | 0 | libCall = spv::SwizzleInvocationsAMD; |
10447 | 0 | break; |
10448 | 0 | case glslang::EOpSwizzleInvocationsMasked: |
10449 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
10450 | 0 | libCall = spv::SwizzleInvocationsMaskedAMD; |
10451 | 0 | break; |
10452 | 0 | case glslang::EOpWriteInvocation: |
10453 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
10454 | 0 | libCall = spv::WriteInvocationAMD; |
10455 | 0 | break; |
10456 | | |
10457 | 0 | case glslang::EOpMin3: |
10458 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
10459 | 0 | if (isFloat) |
10460 | 0 | libCall = spv::FMin3AMD; |
10461 | 0 | else { |
10462 | 0 | if (isUnsigned) |
10463 | 0 | libCall = spv::UMin3AMD; |
10464 | 0 | else |
10465 | 0 | libCall = spv::SMin3AMD; |
10466 | 0 | } |
10467 | 0 | break; |
10468 | 0 | case glslang::EOpMax3: |
10469 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
10470 | 0 | if (isFloat) |
10471 | 0 | libCall = spv::FMax3AMD; |
10472 | 0 | else { |
10473 | 0 | if (isUnsigned) |
10474 | 0 | libCall = spv::UMax3AMD; |
10475 | 0 | else |
10476 | 0 | libCall = spv::SMax3AMD; |
10477 | 0 | } |
10478 | 0 | break; |
10479 | 0 | case glslang::EOpMid3: |
10480 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
10481 | 0 | if (isFloat) |
10482 | 0 | libCall = spv::FMid3AMD; |
10483 | 0 | else { |
10484 | 0 | if (isUnsigned) |
10485 | 0 | libCall = spv::UMid3AMD; |
10486 | 0 | else |
10487 | 0 | libCall = spv::SMid3AMD; |
10488 | 0 | } |
10489 | 0 | break; |
10490 | | |
10491 | 0 | case glslang::EOpInterpolateAtVertex: |
10492 | 0 | if (typeProxy == glslang::EbtFloat16) |
10493 | 0 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
10494 | 0 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
10495 | 0 | libCall = spv::InterpolateAtVertexAMD; |
10496 | 0 | break; |
10497 | | |
10498 | 0 | case glslang::EOpReportIntersection: |
10499 | 0 | typeId = builder.makeBoolType(); |
10500 | 0 | opCode = spv::Op::OpReportIntersectionKHR; |
10501 | 0 | break; |
10502 | 0 | case glslang::EOpTraceNV: |
10503 | 0 | builder.createNoResultOp(spv::Op::OpTraceNV, operands); |
10504 | 0 | return 0; |
10505 | 0 | case glslang::EOpTraceRayMotionNV: |
10506 | 0 | builder.addExtension(spv::E_SPV_NV_ray_tracing_motion_blur); |
10507 | 0 | builder.addCapability(spv::Capability::RayTracingMotionBlurNV); |
10508 | 0 | builder.createNoResultOp(spv::Op::OpTraceRayMotionNV, operands); |
10509 | 0 | return 0; |
10510 | 0 | case glslang::EOpTraceKHR: |
10511 | 0 | builder.createNoResultOp(spv::Op::OpTraceRayKHR, operands); |
10512 | 0 | return 0; |
10513 | 0 | case glslang::EOpExecuteCallableNV: |
10514 | 0 | builder.createNoResultOp(spv::Op::OpExecuteCallableNV, operands); |
10515 | 0 | return 0; |
10516 | 0 | case glslang::EOpExecuteCallableKHR: |
10517 | 0 | builder.createNoResultOp(spv::Op::OpExecuteCallableKHR, operands); |
10518 | 0 | return 0; |
10519 | | |
10520 | 10 | case glslang::EOpRayQueryInitialize: |
10521 | 10 | builder.createNoResultOp(spv::Op::OpRayQueryInitializeKHR, operands); |
10522 | 10 | return 0; |
10523 | 0 | case glslang::EOpRayQueryTerminate: |
10524 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryTerminateKHR, operands); |
10525 | 0 | return 0; |
10526 | 10 | case glslang::EOpRayQueryGenerateIntersection: |
10527 | 10 | builder.createNoResultOp(spv::Op::OpRayQueryGenerateIntersectionKHR, operands); |
10528 | 10 | return 0; |
10529 | 0 | case glslang::EOpRayQueryConfirmIntersection: |
10530 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryConfirmIntersectionKHR, operands); |
10531 | 0 | return 0; |
10532 | 0 | case glslang::EOpRayQueryProceed: |
10533 | 0 | typeId = builder.makeBoolType(); |
10534 | 0 | opCode = spv::Op::OpRayQueryProceedKHR; |
10535 | 0 | break; |
10536 | 20 | case glslang::EOpRayQueryGetIntersectionType: |
10537 | 20 | typeId = builder.makeUintType(32); |
10538 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionTypeKHR; |
10539 | 20 | break; |
10540 | 0 | case glslang::EOpRayQueryGetRayTMin: |
10541 | 0 | typeId = builder.makeFloatType(32); |
10542 | 0 | opCode = spv::Op::OpRayQueryGetRayTMinKHR; |
10543 | 0 | break; |
10544 | 0 | case glslang::EOpRayQueryGetRayFlags: |
10545 | 0 | typeId = builder.makeIntType(32); |
10546 | 0 | opCode = spv::Op::OpRayQueryGetRayFlagsKHR; |
10547 | 0 | break; |
10548 | 20 | case glslang::EOpRayQueryGetIntersectionT: |
10549 | 20 | typeId = builder.makeFloatType(32); |
10550 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionTKHR; |
10551 | 20 | break; |
10552 | 20 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
10553 | 20 | typeId = builder.makeIntType(32); |
10554 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
10555 | 20 | break; |
10556 | 20 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
10557 | 20 | typeId = builder.makeIntType(32); |
10558 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionInstanceIdKHR; |
10559 | 20 | break; |
10560 | 10 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
10561 | 10 | typeId = builder.makeUintType(32); |
10562 | 10 | opCode = spv::Op::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
10563 | 10 | break; |
10564 | 10 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
10565 | 10 | typeId = builder.makeIntType(32); |
10566 | 10 | opCode = spv::Op::OpRayQueryGetIntersectionGeometryIndexKHR; |
10567 | 10 | break; |
10568 | 20 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
10569 | 20 | typeId = builder.makeIntType(32); |
10570 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
10571 | 20 | break; |
10572 | 20 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
10573 | 20 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
10574 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionBarycentricsKHR; |
10575 | 20 | break; |
10576 | 20 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
10577 | 20 | typeId = builder.makeBoolType(); |
10578 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionFrontFaceKHR; |
10579 | 20 | break; |
10580 | 0 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
10581 | 0 | typeId = builder.makeBoolType(); |
10582 | 0 | opCode = spv::Op::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
10583 | 0 | break; |
10584 | 20 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
10585 | 20 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10586 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
10587 | 20 | break; |
10588 | 20 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
10589 | 20 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10590 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionObjectRayOriginKHR; |
10591 | 20 | break; |
10592 | 0 | case glslang::EOpRayQueryGetWorldRayDirection: |
10593 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10594 | 0 | opCode = spv::Op::OpRayQueryGetWorldRayDirectionKHR; |
10595 | 0 | break; |
10596 | 0 | case glslang::EOpRayQueryGetWorldRayOrigin: |
10597 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10598 | 0 | opCode = spv::Op::OpRayQueryGetWorldRayOriginKHR; |
10599 | 0 | break; |
10600 | 20 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
10601 | 20 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10602 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionObjectToWorldKHR; |
10603 | 20 | break; |
10604 | 0 | case glslang::EOpRayQueryGetIntersectionClusterIdNV: |
10605 | 0 | typeId = builder.makeIntegerType(32, 1); |
10606 | 0 | opCode = spv::Op::OpRayQueryGetClusterIdNV; |
10607 | 0 | break; |
10608 | 20 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
10609 | 20 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10610 | 20 | opCode = spv::Op::OpRayQueryGetIntersectionWorldToObjectKHR; |
10611 | 20 | break; |
10612 | 0 | case glslang::EOpRayQueryGetIntersectionSpherePositionNV: |
10613 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10614 | 0 | opCode = spv::Op::OpRayQueryGetIntersectionSpherePositionNV; |
10615 | 0 | break; |
10616 | 0 | case glslang::EOpRayQueryGetIntersectionSphereRadiusNV: |
10617 | 0 | typeId = builder.makeFloatType(32); |
10618 | 0 | opCode = spv::Op::OpRayQueryGetIntersectionSphereRadiusNV; |
10619 | 0 | break; |
10620 | 0 | case glslang::EOpRayQueryGetIntersectionLSSHitValueNV: |
10621 | 0 | typeId = builder.makeFloatType(32); |
10622 | 0 | opCode = spv::Op::OpRayQueryGetIntersectionLSSHitValueNV; |
10623 | 0 | break; |
10624 | 0 | case glslang::EOpRayQueryIsSphereHitNV: |
10625 | 0 | typeId = builder.makeBoolType(); |
10626 | 0 | opCode = spv::Op::OpRayQueryIsSphereHitNV; |
10627 | 0 | break; |
10628 | 0 | case glslang::EOpRayQueryIsLSSHitNV: |
10629 | 0 | typeId = builder.makeBoolType(); |
10630 | 0 | opCode = spv::Op::OpRayQueryIsLSSHitNV; |
10631 | 0 | break; |
10632 | 0 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
10633 | 0 | builder.createNoResultOp(spv::Op::OpWritePackedPrimitiveIndices4x8NV, operands); |
10634 | 0 | return 0; |
10635 | 0 | case glslang::EOpEmitMeshTasksEXT: |
10636 | 0 | if (taskPayloadID) |
10637 | 0 | operands.push_back(taskPayloadID); |
10638 | | // As per SPV_EXT_mesh_shader make it a terminating instruction in the current block |
10639 | 0 | builder.makeStatementTerminator(spv::Op::OpEmitMeshTasksEXT, operands, "post-OpEmitMeshTasksEXT"); |
10640 | 0 | return 0; |
10641 | 0 | case glslang::EOpSetMeshOutputsEXT: |
10642 | 0 | builder.createNoResultOp(spv::Op::OpSetMeshOutputsEXT, operands); |
10643 | 0 | return 0; |
10644 | 0 | case glslang::EOpCooperativeMatrixMulAddNV: |
10645 | 0 | opCode = spv::Op::OpCooperativeMatrixMulAddNV; |
10646 | 0 | break; |
10647 | 0 | case glslang::EOpHitObjectTraceRayNV: |
10648 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceRayNV, operands); |
10649 | 0 | return 0; |
10650 | 0 | case glslang::EOpHitObjectTraceRayEXT: |
10651 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceRayEXT, operands); |
10652 | 0 | return 0; |
10653 | 0 | case glslang::EOpHitObjectTraceRayMotionNV: |
10654 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceRayMotionNV, operands); |
10655 | 0 | return 0; |
10656 | 0 | case glslang::EOpHitObjectTraceRayMotionEXT: |
10657 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceRayMotionEXT, operands); |
10658 | 0 | return 0; |
10659 | 0 | case glslang::EOpHitObjectRecordHitNV: |
10660 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordHitNV, operands); |
10661 | 0 | return 0; |
10662 | 0 | case glslang::EOpHitObjectRecordHitMotionNV: |
10663 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordHitMotionNV, operands); |
10664 | 0 | return 0; |
10665 | 0 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
10666 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordHitWithIndexNV, operands); |
10667 | 0 | return 0; |
10668 | 0 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
10669 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordHitWithIndexMotionNV, operands); |
10670 | 0 | return 0; |
10671 | 0 | case glslang::EOpHitObjectRecordMissNV: |
10672 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordMissNV, operands); |
10673 | 0 | return 0; |
10674 | 0 | case glslang::EOpHitObjectRecordMissEXT: |
10675 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordMissEXT, operands); |
10676 | 0 | return 0; |
10677 | 0 | case glslang::EOpHitObjectRecordMissMotionNV: |
10678 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordMissMotionNV, operands); |
10679 | 0 | return 0; |
10680 | 0 | case glslang::EOpHitObjectRecordMissMotionEXT: |
10681 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordMissMotionEXT, operands); |
10682 | 0 | return 0; |
10683 | 0 | case glslang::EOpHitObjectExecuteShaderNV: |
10684 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectExecuteShaderNV, operands); |
10685 | 0 | return 0; |
10686 | 0 | case glslang::EOpHitObjectExecuteShaderEXT: |
10687 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectExecuteShaderEXT, operands); |
10688 | 0 | return 0; |
10689 | 0 | case glslang::EOpHitObjectIsEmptyNV: |
10690 | 0 | typeId = builder.makeBoolType(); |
10691 | 0 | opCode = spv::Op::OpHitObjectIsEmptyNV; |
10692 | 0 | break; |
10693 | 0 | case glslang::EOpHitObjectIsEmptyEXT: |
10694 | 0 | typeId = builder.makeBoolType(); |
10695 | 0 | opCode = spv::Op::OpHitObjectIsEmptyEXT; |
10696 | 0 | break; |
10697 | 0 | case glslang::EOpHitObjectIsMissNV: |
10698 | 0 | typeId = builder.makeBoolType(); |
10699 | 0 | opCode = spv::Op::OpHitObjectIsMissNV; |
10700 | 0 | break; |
10701 | 0 | case glslang::EOpHitObjectIsMissEXT: |
10702 | 0 | typeId = builder.makeBoolType(); |
10703 | 0 | opCode = spv::Op::OpHitObjectIsMissEXT; |
10704 | 0 | break; |
10705 | 0 | case glslang::EOpHitObjectIsHitNV: |
10706 | 0 | typeId = builder.makeBoolType(); |
10707 | 0 | opCode = spv::Op::OpHitObjectIsHitNV; |
10708 | 0 | break; |
10709 | 0 | case glslang::EOpHitObjectIsSphereHitNV: |
10710 | 0 | typeId = builder.makeBoolType(); |
10711 | 0 | opCode = spv::Op::OpHitObjectIsSphereHitNV; |
10712 | 0 | break; |
10713 | 0 | case glslang::EOpHitObjectIsLSSHitNV: |
10714 | 0 | typeId = builder.makeBoolType(); |
10715 | 0 | opCode = spv::Op::OpHitObjectIsLSSHitNV; |
10716 | 0 | break; |
10717 | 0 | case glslang::EOpHitObjectIsHitEXT: |
10718 | 0 | typeId = builder.makeBoolType(); |
10719 | 0 | opCode = spv::Op::OpHitObjectIsHitEXT; |
10720 | 0 | break; |
10721 | 0 | case glslang::EOpHitObjectGetRayTMinNV: |
10722 | 0 | typeId = builder.makeFloatType(32); |
10723 | 0 | opCode = spv::Op::OpHitObjectGetRayTMinNV; |
10724 | 0 | break; |
10725 | 0 | case glslang::EOpHitObjectGetRayTMinEXT: |
10726 | 0 | typeId = builder.makeFloatType(32); |
10727 | 0 | opCode = spv::Op::OpHitObjectGetRayTMinEXT; |
10728 | 0 | break; |
10729 | 0 | case glslang::EOpHitObjectGetRayTMaxNV: |
10730 | 0 | typeId = builder.makeFloatType(32); |
10731 | 0 | opCode = spv::Op::OpHitObjectGetRayTMaxNV; |
10732 | 0 | break; |
10733 | 0 | case glslang::EOpHitObjectGetRayTMaxEXT: |
10734 | 0 | typeId = builder.makeFloatType(32); |
10735 | 0 | opCode = spv::Op::OpHitObjectGetRayTMaxEXT; |
10736 | 0 | break; |
10737 | 0 | case glslang::EOpHitObjectGetRayFlagsEXT: |
10738 | 0 | typeId = builder.makeIntegerType(32, 0); |
10739 | 0 | opCode = spv::Op::OpHitObjectGetRayFlagsEXT; |
10740 | 0 | break; |
10741 | 0 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
10742 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10743 | 0 | opCode = spv::Op::OpHitObjectGetObjectRayOriginNV; |
10744 | 0 | break; |
10745 | 0 | case glslang::EOpHitObjectGetObjectRayOriginEXT: |
10746 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10747 | 0 | opCode = spv::Op::OpHitObjectGetObjectRayOriginEXT; |
10748 | 0 | break; |
10749 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
10750 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10751 | 0 | opCode = spv::Op::OpHitObjectGetObjectRayDirectionNV; |
10752 | 0 | break; |
10753 | 0 | case glslang::EOpHitObjectGetObjectRayDirectionEXT: |
10754 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10755 | 0 | opCode = spv::Op::OpHitObjectGetObjectRayDirectionEXT; |
10756 | 0 | break; |
10757 | 0 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
10758 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10759 | 0 | opCode = spv::Op::OpHitObjectGetWorldRayOriginNV; |
10760 | 0 | break; |
10761 | 0 | case glslang::EOpHitObjectGetWorldRayOriginEXT: |
10762 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10763 | 0 | opCode = spv::Op::OpHitObjectGetWorldRayOriginEXT; |
10764 | 0 | break; |
10765 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
10766 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10767 | 0 | opCode = spv::Op::OpHitObjectGetWorldRayDirectionNV; |
10768 | 0 | break; |
10769 | 0 | case glslang::EOpHitObjectGetWorldRayDirectionEXT: |
10770 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10771 | 0 | opCode = spv::Op::OpHitObjectGetWorldRayDirectionEXT; |
10772 | 0 | break; |
10773 | 0 | case glslang::EOpHitObjectGetWorldToObjectNV: |
10774 | 0 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10775 | 0 | opCode = spv::Op::OpHitObjectGetWorldToObjectNV; |
10776 | 0 | break; |
10777 | 0 | case glslang::EOpHitObjectGetWorldToObjectEXT: |
10778 | 0 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10779 | 0 | opCode = spv::Op::OpHitObjectGetWorldToObjectEXT; |
10780 | 0 | break; |
10781 | 0 | case glslang::EOpHitObjectGetObjectToWorldNV: |
10782 | 0 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10783 | 0 | opCode = spv::Op::OpHitObjectGetObjectToWorldNV; |
10784 | 0 | break; |
10785 | 0 | case glslang::EOpHitObjectGetObjectToWorldEXT: |
10786 | 0 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
10787 | 0 | opCode = spv::Op::OpHitObjectGetObjectToWorldEXT; |
10788 | 0 | break; |
10789 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
10790 | 0 | typeId = builder.makeIntegerType(32, 1); |
10791 | 0 | opCode = spv::Op::OpHitObjectGetInstanceCustomIndexNV; |
10792 | 0 | break; |
10793 | 0 | case glslang::EOpHitObjectGetInstanceCustomIndexEXT: |
10794 | 0 | typeId = builder.makeIntegerType(32, 1); |
10795 | 0 | opCode = spv::Op::OpHitObjectGetInstanceCustomIndexEXT; |
10796 | 0 | break; |
10797 | 0 | case glslang::EOpHitObjectGetInstanceIdNV: |
10798 | 0 | typeId = builder.makeIntegerType(32, 1); |
10799 | 0 | opCode = spv::Op::OpHitObjectGetInstanceIdNV; |
10800 | 0 | break; |
10801 | 0 | case glslang::EOpHitObjectGetInstanceIdEXT: |
10802 | 0 | typeId = builder.makeIntegerType(32, 1); |
10803 | 0 | opCode = spv::Op::OpHitObjectGetInstanceIdEXT; |
10804 | 0 | break; |
10805 | 0 | case glslang::EOpHitObjectGetGeometryIndexNV: |
10806 | 0 | typeId = builder.makeIntegerType(32, 1); |
10807 | 0 | opCode = spv::Op::OpHitObjectGetGeometryIndexNV; |
10808 | 0 | break; |
10809 | 0 | case glslang::EOpHitObjectGetGeometryIndexEXT: |
10810 | 0 | typeId = builder.makeIntegerType(32, 1); |
10811 | 0 | opCode = spv::Op::OpHitObjectGetGeometryIndexEXT; |
10812 | 0 | break; |
10813 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
10814 | 0 | typeId = builder.makeIntegerType(32, 1); |
10815 | 0 | opCode = spv::Op::OpHitObjectGetPrimitiveIndexNV; |
10816 | 0 | break; |
10817 | 0 | case glslang::EOpHitObjectGetPrimitiveIndexEXT: |
10818 | 0 | typeId = builder.makeIntegerType(32, 1); |
10819 | 0 | opCode = spv::Op::OpHitObjectGetPrimitiveIndexEXT; |
10820 | 0 | break; |
10821 | 0 | case glslang::EOpHitObjectGetHitKindNV: |
10822 | 0 | typeId = builder.makeIntegerType(32, 0); |
10823 | 0 | opCode = spv::Op::OpHitObjectGetHitKindNV; |
10824 | 0 | break; |
10825 | 0 | case glslang::EOpHitObjectGetHitKindEXT: |
10826 | 0 | typeId = builder.makeIntegerType(32, 0); |
10827 | 0 | opCode = spv::Op::OpHitObjectGetHitKindEXT; |
10828 | 0 | break; |
10829 | 0 | case glslang::EOpHitObjectGetCurrentTimeNV: |
10830 | 0 | typeId = builder.makeFloatType(32); |
10831 | 0 | opCode = spv::Op::OpHitObjectGetCurrentTimeNV; |
10832 | 0 | break; |
10833 | 0 | case glslang::EOpHitObjectGetCurrentTimeEXT: |
10834 | 0 | typeId = builder.makeFloatType(32); |
10835 | 0 | opCode = spv::Op::OpHitObjectGetCurrentTimeEXT; |
10836 | 0 | break; |
10837 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
10838 | 0 | typeId = builder.makeIntegerType(32, 0); |
10839 | 0 | opCode = spv::Op::OpHitObjectGetShaderBindingTableRecordIndexNV; |
10840 | 0 | return 0; |
10841 | 0 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexEXT: |
10842 | 0 | typeId = builder.makeIntegerType(32, 0); |
10843 | 0 | opCode = spv::Op::OpHitObjectGetShaderBindingTableRecordIndexEXT; |
10844 | 0 | return 0; |
10845 | 0 | case glslang::EOpHitObjectGetAttributesNV: |
10846 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectGetAttributesNV, operands); |
10847 | 0 | return 0; |
10848 | 0 | case glslang::EOpHitObjectGetAttributesEXT: |
10849 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectGetAttributesEXT, operands); |
10850 | 0 | return 0; |
10851 | 0 | case glslang::EOpHitObjectRecordFromQueryEXT: |
10852 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectRecordFromQueryEXT, operands); |
10853 | 0 | return 0; |
10854 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
10855 | 0 | typeId = builder.makeVectorType(builder.makeUintType(32), 2); |
10856 | 0 | opCode = spv::Op::OpHitObjectGetShaderRecordBufferHandleNV; |
10857 | 0 | break; |
10858 | 0 | case glslang::EOpHitObjectGetClusterIdNV: |
10859 | 0 | typeId = builder.makeIntegerType(32, 1); |
10860 | 0 | opCode = spv::Op::OpHitObjectGetClusterIdNV; |
10861 | 0 | break; |
10862 | 0 | case glslang::EOpHitObjectGetShaderRecordBufferHandleEXT: |
10863 | 0 | typeId = builder.makeVectorType(builder.makeUintType(32), 2); |
10864 | 0 | opCode = spv::Op::OpHitObjectGetShaderRecordBufferHandleEXT; |
10865 | 0 | break; |
10866 | 0 | case glslang::EOpHitObjectSetShaderBindingTableRecordIndexEXT: |
10867 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectSetShaderBindingTableRecordIndexEXT, operands); |
10868 | 0 | return 0; |
10869 | 0 | case glslang::EOpReorderThreadNV: { |
10870 | 0 | if (operands.size() == 2) { |
10871 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHintNV, operands); |
10872 | 0 | } else { |
10873 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHitObjectNV, operands); |
10874 | 0 | } |
10875 | 0 | return 0; |
10876 | 0 | } |
10877 | 0 | case glslang::EOpReorderThreadEXT: { |
10878 | 0 | if (operands.size() == 2) { |
10879 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHintEXT, operands); |
10880 | 0 | } else { |
10881 | 0 | builder.createNoResultOp(spv::Op::OpReorderThreadWithHitObjectEXT, operands); |
10882 | 0 | } |
10883 | 0 | return 0; |
10884 | 0 | } |
10885 | | |
10886 | 0 | case glslang::EOpHitObjectReorderExecuteEXT: { |
10887 | 0 | if (operands.size() == 2) { |
10888 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectReorderExecuteShaderEXT, operands); |
10889 | 0 | } else { |
10890 | | // GLSL intrinsic is |
10891 | | // hitObjectReorderExecuteEXT(hitObjectEXT hitObject, uint hint, uint bits,int payload) while |
10892 | | // SPIRV is hitObject id , payload id, optional hint id, optional bits id hence reorder operands |
10893 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectReorderExecuteShaderEXT, {operands[0], operands[3], operands[1], operands[2]}); |
10894 | 0 | } |
10895 | 0 | return 0; |
10896 | 0 | } |
10897 | | |
10898 | 0 | case glslang::EOpHitObjectTraceReorderExecuteEXT: { |
10899 | 0 | if (operands.size() == 12) { |
10900 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceReorderExecuteEXT, operands); |
10901 | 0 | } else { |
10902 | 0 | std::vector<spv::Id> argOperands; |
10903 | 0 | std::copy(operands.begin(), operands.begin() + 11, std::back_inserter(argOperands)); |
10904 | 0 | argOperands.push_back(operands[13]); |
10905 | 0 | argOperands.push_back(operands[11]); |
10906 | 0 | argOperands.push_back(operands[12]); |
10907 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceReorderExecuteEXT, argOperands); |
10908 | 0 | } |
10909 | 0 | return 0; |
10910 | 0 | } |
10911 | 0 | case glslang::EOpHitObjectTraceMotionReorderExecuteEXT: { |
10912 | 0 | if (operands.size() == 13) { |
10913 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceMotionReorderExecuteEXT, operands); |
10914 | 0 | } else { |
10915 | 0 | std::vector<spv::Id> argOperands; |
10916 | 0 | std::copy(operands.begin(), operands.begin() + 12, std::back_inserter(argOperands)); |
10917 | 0 | argOperands.push_back(operands[14]); |
10918 | 0 | argOperands.push_back(operands[12]); |
10919 | 0 | argOperands.push_back(operands[13]); |
10920 | 0 | builder.createNoResultOp(spv::Op::OpHitObjectTraceMotionReorderExecuteEXT, argOperands); |
10921 | 0 | } |
10922 | 0 | return 0; |
10923 | 0 | } |
10924 | 18 | case glslang::EOpImageSampleWeightedQCOM: |
10925 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10926 | 18 | opCode = spv::Op::OpImageSampleWeightedQCOM; |
10927 | 18 | addImageProcessingQCOMDecoration(operands[2], spv::Decoration::WeightTextureQCOM); |
10928 | 18 | break; |
10929 | 18 | case glslang::EOpImageBoxFilterQCOM: |
10930 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10931 | 18 | opCode = spv::Op::OpImageBoxFilterQCOM; |
10932 | 18 | break; |
10933 | 27 | case glslang::EOpImageBlockMatchSADQCOM: |
10934 | 27 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10935 | 27 | opCode = spv::Op::OpImageBlockMatchSADQCOM; |
10936 | 27 | addImageProcessingQCOMDecoration(operands[0], spv::Decoration::BlockMatchTextureQCOM); |
10937 | 27 | addImageProcessingQCOMDecoration(operands[2], spv::Decoration::BlockMatchTextureQCOM); |
10938 | 27 | break; |
10939 | 27 | case glslang::EOpImageBlockMatchSSDQCOM: |
10940 | 27 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10941 | 27 | opCode = spv::Op::OpImageBlockMatchSSDQCOM; |
10942 | 27 | addImageProcessingQCOMDecoration(operands[0], spv::Decoration::BlockMatchTextureQCOM); |
10943 | 27 | addImageProcessingQCOMDecoration(operands[2], spv::Decoration::BlockMatchTextureQCOM); |
10944 | 27 | break; |
10945 | | |
10946 | 0 | case glslang::EOpFetchMicroTriangleVertexBarycentricNV: |
10947 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
10948 | 0 | opCode = spv::Op::OpFetchMicroTriangleVertexBarycentricNV; |
10949 | 0 | break; |
10950 | | |
10951 | 0 | case glslang::EOpFetchMicroTriangleVertexPositionNV: |
10952 | 0 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
10953 | 0 | opCode = spv::Op::OpFetchMicroTriangleVertexPositionNV; |
10954 | 0 | break; |
10955 | | |
10956 | 18 | case glslang::EOpImageBlockMatchWindowSSDQCOM: |
10957 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10958 | 18 | opCode = spv::Op::OpImageBlockMatchWindowSSDQCOM; |
10959 | 18 | addImageProcessing2QCOMDecoration(operands[0], false); |
10960 | 18 | addImageProcessing2QCOMDecoration(operands[2], false); |
10961 | 18 | break; |
10962 | 18 | case glslang::EOpImageBlockMatchWindowSADQCOM: |
10963 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10964 | 18 | opCode = spv::Op::OpImageBlockMatchWindowSADQCOM; |
10965 | 18 | addImageProcessing2QCOMDecoration(operands[0], false); |
10966 | 18 | addImageProcessing2QCOMDecoration(operands[2], false); |
10967 | 18 | break; |
10968 | 18 | case glslang::EOpImageBlockMatchGatherSSDQCOM: |
10969 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10970 | 18 | opCode = spv::Op::OpImageBlockMatchGatherSSDQCOM; |
10971 | 18 | addImageProcessing2QCOMDecoration(operands[0], true); |
10972 | 18 | addImageProcessing2QCOMDecoration(operands[2], true); |
10973 | 18 | break; |
10974 | 18 | case glslang::EOpImageBlockMatchGatherSADQCOM: |
10975 | 18 | typeId = builder.makeVectorType(builder.makeFloatType(32), 4); |
10976 | 18 | opCode = spv::Op::OpImageBlockMatchGatherSADQCOM; |
10977 | 18 | addImageProcessing2QCOMDecoration(operands[0], true); |
10978 | 18 | addImageProcessing2QCOMDecoration(operands[2], true); |
10979 | 18 | break; |
10980 | 0 | case glslang::EOpCreateTensorLayoutNV: |
10981 | 0 | return builder.createOp(spv::Op::OpCreateTensorLayoutNV, typeId, std::vector<spv::Id>{}); |
10982 | 0 | case glslang::EOpCreateTensorViewNV: |
10983 | 0 | return builder.createOp(spv::Op::OpCreateTensorViewNV, typeId, std::vector<spv::Id>{}); |
10984 | 0 | case glslang::EOpTensorLayoutSetBlockSizeNV: |
10985 | 0 | opCode = spv::Op::OpTensorLayoutSetBlockSizeNV; |
10986 | 0 | break; |
10987 | 0 | case glslang::EOpTensorLayoutSetDimensionNV: |
10988 | 0 | opCode = spv::Op::OpTensorLayoutSetDimensionNV; |
10989 | 0 | break; |
10990 | 0 | case glslang::EOpTensorLayoutSetStrideNV: |
10991 | 0 | opCode = spv::Op::OpTensorLayoutSetStrideNV; |
10992 | 0 | break; |
10993 | 0 | case glslang::EOpTensorLayoutSliceNV: |
10994 | 0 | opCode = spv::Op::OpTensorLayoutSliceNV; |
10995 | 0 | break; |
10996 | 0 | case glslang::EOpTensorLayoutSetClampValueNV: |
10997 | 0 | opCode = spv::Op::OpTensorLayoutSetClampValueNV; |
10998 | 0 | break; |
10999 | 0 | case glslang::EOpTensorViewSetDimensionNV: |
11000 | 0 | opCode = spv::Op::OpTensorViewSetDimensionNV; |
11001 | 0 | break; |
11002 | 0 | case glslang::EOpTensorViewSetStrideNV: |
11003 | 0 | opCode = spv::Op::OpTensorViewSetStrideNV; |
11004 | 0 | break; |
11005 | 0 | case glslang::EOpTensorViewSetClipNV: |
11006 | 0 | opCode = spv::Op::OpTensorViewSetClipNV; |
11007 | 0 | break; |
11008 | 0 | default: |
11009 | 0 | return 0; |
11010 | 1.70k | } |
11011 | | |
11012 | 642 | spv::Id id = 0; |
11013 | 642 | if (libCall >= 0) { |
11014 | | // Use an extended instruction from the standard library. |
11015 | | // Construct the call arguments, without modifying the original operands vector. |
11016 | | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
11017 | 204 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
11018 | 204 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
11019 | 438 | } else if (opCode == spv::Op::OpDot && !isFloat) { |
11020 | | // int dot(int, int) |
11021 | | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
11022 | 0 | const int componentCount = builder.getNumComponents(operands[0]); |
11023 | 0 | spv::Id mulOp = builder.createBinOp(spv::Op::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
11024 | 0 | builder.setPrecision(mulOp, precision); |
11025 | 0 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
11026 | 0 | for (int i = 1; i < componentCount; ++i) { |
11027 | 0 | builder.setPrecision(id, precision); |
11028 | 0 | id = builder.createBinOp(spv::Op::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
11029 | 0 | } |
11030 | 438 | } else { |
11031 | 438 | switch (consumedOperands) { |
11032 | 0 | case 0: |
11033 | | // should all be handled by visitAggregate and createNoArgOperation |
11034 | 0 | assert(0); |
11035 | 0 | return 0; |
11036 | 0 | case 1: |
11037 | | // should all be handled by createUnaryOperation |
11038 | 0 | assert(0); |
11039 | 0 | return 0; |
11040 | 240 | case 2: |
11041 | 240 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
11042 | 240 | break; |
11043 | 198 | default: |
11044 | | // anything 3 or over doesn't have l-value operands, so all should be consumed |
11045 | 198 | assert(consumedOperands == operands.size()); |
11046 | 198 | id = builder.createOp(opCode, typeId, operands); |
11047 | 198 | break; |
11048 | 438 | } |
11049 | 438 | } |
11050 | | |
11051 | | // Decode the return types that were structures |
11052 | 642 | switch (op) { |
11053 | 0 | case glslang::EOpAddCarry: |
11054 | 0 | case glslang::EOpSubBorrow: |
11055 | 0 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
11056 | 0 | id = builder.createCompositeExtract(id, typeId0, 0); |
11057 | 0 | break; |
11058 | 0 | case glslang::EOpUMulExtended: |
11059 | 0 | case glslang::EOpIMulExtended: |
11060 | 0 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
11061 | 0 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
11062 | 0 | break; |
11063 | 0 | case glslang::EOpModf: |
11064 | 0 | { |
11065 | 0 | assert(operands.size() == 2); |
11066 | 0 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[1]); |
11067 | 0 | id = builder.createCompositeExtract(id, typeId0, 0); |
11068 | 0 | } |
11069 | 0 | break; |
11070 | 9 | case glslang::EOpFrexp: |
11071 | 9 | { |
11072 | 9 | assert(operands.size() == 2); |
11073 | 9 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
11074 | | // "exp" is floating-point type (from HLSL intrinsic) |
11075 | 0 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
11076 | 0 | member1 = builder.createUnaryOp(spv::Op::OpConvertSToF, typeId1, member1); |
11077 | 0 | builder.createStore(member1, operands[1]); |
11078 | 0 | } else |
11079 | | // "exp" is integer type (from GLSL built-in function) |
11080 | 9 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
11081 | 9 | id = builder.createCompositeExtract(id, typeId0, 0); |
11082 | 9 | } |
11083 | 9 | break; |
11084 | 633 | default: |
11085 | 633 | break; |
11086 | 642 | } |
11087 | | |
11088 | 642 | return builder.setPrecision(id, precision); |
11089 | 642 | } |
11090 | | |
11091 | | // Intrinsics with no arguments (or no return value, and no precision). |
11092 | | spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId) |
11093 | 0 | { |
11094 | | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
11095 | 0 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
11096 | 0 | spv::Scope::QueueFamilyKHR : spv::Scope::Device; |
11097 | |
|
11098 | 0 | switch (op) { |
11099 | 0 | case glslang::EOpBarrier: |
11100 | 0 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
11101 | 0 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
11102 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Workgroup, |
11103 | 0 | spv::MemorySemanticsMask::OutputMemoryKHR | |
11104 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11105 | 0 | builder.addCapability(spv::Capability::VulkanMemoryModelKHR); |
11106 | 0 | } else { |
11107 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Invocation, spv::MemorySemanticsMask::MaskNone); |
11108 | 0 | } |
11109 | 0 | } else { |
11110 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Workgroup, |
11111 | 0 | spv::MemorySemanticsMask::WorkgroupMemory | |
11112 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11113 | 0 | } |
11114 | 0 | return 0; |
11115 | 0 | case glslang::EOpMemoryBarrier: |
11116 | 0 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
11117 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11118 | 0 | return 0; |
11119 | 0 | case glslang::EOpMemoryBarrierBuffer: |
11120 | 0 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsMask::UniformMemory | |
11121 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11122 | 0 | return 0; |
11123 | 0 | case glslang::EOpMemoryBarrierShared: |
11124 | 0 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsMask::WorkgroupMemory | |
11125 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11126 | 0 | return 0; |
11127 | 0 | case glslang::EOpGroupMemoryBarrier: |
11128 | 0 | builder.createMemoryBarrier(spv::Scope::Workgroup, spv::MemorySemanticsAllMemory | |
11129 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11130 | 0 | return 0; |
11131 | 0 | case glslang::EOpMemoryBarrierAtomicCounter: |
11132 | 0 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsMask::AtomicCounterMemory | |
11133 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11134 | 0 | return 0; |
11135 | 0 | case glslang::EOpMemoryBarrierImage: |
11136 | 0 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsMask::ImageMemory | |
11137 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11138 | 0 | return 0; |
11139 | 0 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
11140 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Device, |
11141 | 0 | spv::MemorySemanticsAllMemory | |
11142 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11143 | 0 | return 0; |
11144 | 0 | case glslang::EOpDeviceMemoryBarrier: |
11145 | 0 | builder.createMemoryBarrier(spv::Scope::Device, spv::MemorySemanticsMask::UniformMemory | |
11146 | 0 | spv::MemorySemanticsMask::ImageMemory | |
11147 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11148 | 0 | return 0; |
11149 | 0 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
11150 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Device, spv::MemorySemanticsMask::UniformMemory | |
11151 | 0 | spv::MemorySemanticsMask::ImageMemory | |
11152 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11153 | 0 | return 0; |
11154 | 0 | case glslang::EOpWorkgroupMemoryBarrier: |
11155 | 0 | builder.createMemoryBarrier(spv::Scope::Workgroup, spv::MemorySemanticsMask::WorkgroupMemory | |
11156 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11157 | 0 | return 0; |
11158 | 0 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
11159 | 0 | builder.createControlBarrier(spv::Scope::Workgroup, spv::Scope::Workgroup, |
11160 | 0 | spv::MemorySemanticsMask::WorkgroupMemory | |
11161 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11162 | 0 | return 0; |
11163 | 0 | case glslang::EOpSubgroupBarrier: |
11164 | 0 | builder.createControlBarrier(spv::Scope::Subgroup, spv::Scope::Subgroup, spv::MemorySemanticsAllMemory | |
11165 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11166 | 0 | return spv::NoResult; |
11167 | 0 | case glslang::EOpSubgroupMemoryBarrier: |
11168 | 0 | builder.createMemoryBarrier(spv::Scope::Subgroup, spv::MemorySemanticsAllMemory | |
11169 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11170 | 0 | return spv::NoResult; |
11171 | 0 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
11172 | 0 | builder.createMemoryBarrier(spv::Scope::Subgroup, spv::MemorySemanticsMask::UniformMemory | |
11173 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11174 | 0 | return spv::NoResult; |
11175 | 0 | case glslang::EOpSubgroupMemoryBarrierImage: |
11176 | 0 | builder.createMemoryBarrier(spv::Scope::Subgroup, spv::MemorySemanticsMask::ImageMemory | |
11177 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11178 | 0 | return spv::NoResult; |
11179 | 0 | case glslang::EOpSubgroupMemoryBarrierShared: |
11180 | 0 | builder.createMemoryBarrier(spv::Scope::Subgroup, spv::MemorySemanticsMask::WorkgroupMemory | |
11181 | 0 | spv::MemorySemanticsMask::AcquireRelease); |
11182 | 0 | return spv::NoResult; |
11183 | | |
11184 | 0 | case glslang::EOpEmitVertex: |
11185 | 0 | builder.createNoResultOp(spv::Op::OpEmitVertex); |
11186 | 0 | return 0; |
11187 | 0 | case glslang::EOpEndPrimitive: |
11188 | 0 | builder.createNoResultOp(spv::Op::OpEndPrimitive); |
11189 | 0 | return 0; |
11190 | | |
11191 | 0 | case glslang::EOpSubgroupElect: { |
11192 | 0 | std::vector<spv::Id> operands; |
11193 | 0 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
11194 | 0 | } |
11195 | 0 | case glslang::EOpTime: |
11196 | 0 | { |
11197 | 0 | std::vector<spv::Id> args; // Dummy arguments |
11198 | 0 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
11199 | 0 | return builder.setPrecision(id, precision); |
11200 | 0 | } |
11201 | 0 | case glslang::EOpIgnoreIntersectionNV: |
11202 | 0 | builder.createNoResultOp(spv::Op::OpIgnoreIntersectionNV); |
11203 | 0 | return 0; |
11204 | 0 | case glslang::EOpTerminateRayNV: |
11205 | 0 | builder.createNoResultOp(spv::Op::OpTerminateRayNV); |
11206 | 0 | return 0; |
11207 | 0 | case glslang::EOpRayQueryInitialize: |
11208 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryInitializeKHR); |
11209 | 0 | return 0; |
11210 | 0 | case glslang::EOpRayQueryTerminate: |
11211 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryTerminateKHR); |
11212 | 0 | return 0; |
11213 | 0 | case glslang::EOpRayQueryGenerateIntersection: |
11214 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryGenerateIntersectionKHR); |
11215 | 0 | return 0; |
11216 | 0 | case glslang::EOpRayQueryConfirmIntersection: |
11217 | 0 | builder.createNoResultOp(spv::Op::OpRayQueryConfirmIntersectionKHR); |
11218 | 0 | return 0; |
11219 | 0 | case glslang::EOpBeginInvocationInterlock: |
11220 | 0 | builder.createNoResultOp(spv::Op::OpBeginInvocationInterlockEXT); |
11221 | 0 | return 0; |
11222 | 0 | case glslang::EOpEndInvocationInterlock: |
11223 | 0 | builder.createNoResultOp(spv::Op::OpEndInvocationInterlockEXT); |
11224 | 0 | return 0; |
11225 | | |
11226 | 0 | case glslang::EOpIsHelperInvocation: |
11227 | 0 | { |
11228 | 0 | std::vector<spv::Id> args; // Dummy arguments |
11229 | 0 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
11230 | 0 | builder.addCapability(spv::Capability::DemoteToHelperInvocationEXT); |
11231 | 0 | return builder.createOp(spv::Op::OpIsHelperInvocationEXT, typeId, args); |
11232 | 0 | } |
11233 | | |
11234 | 0 | case glslang::EOpReadClockSubgroupKHR: { |
11235 | 0 | std::vector<spv::Id> args; |
11236 | 0 | args.push_back(builder.makeUintConstant(spv::Scope::Subgroup)); |
11237 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
11238 | 0 | builder.addCapability(spv::Capability::ShaderClockKHR); |
11239 | 0 | return builder.createOp(spv::Op::OpReadClockKHR, typeId, args); |
11240 | 0 | } |
11241 | | |
11242 | 0 | case glslang::EOpReadClockDeviceKHR: { |
11243 | 0 | std::vector<spv::Id> args; |
11244 | 0 | args.push_back(builder.makeUintConstant(spv::Scope::Device)); |
11245 | 0 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
11246 | 0 | builder.addCapability(spv::Capability::ShaderClockKHR); |
11247 | 0 | return builder.createOp(spv::Op::OpReadClockKHR, typeId, args); |
11248 | 0 | } |
11249 | 0 | case glslang::EOpStencilAttachmentReadEXT: |
11250 | 0 | case glslang::EOpDepthAttachmentReadEXT: |
11251 | 0 | { |
11252 | 0 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
11253 | |
|
11254 | 0 | spv::Decoration precision; |
11255 | 0 | spv::Op spv_op; |
11256 | 0 | if (op == glslang::EOpStencilAttachmentReadEXT) |
11257 | 0 | { |
11258 | 0 | precision = spv::Decoration::RelaxedPrecision; |
11259 | 0 | spv_op = spv::Op::OpStencilAttachmentReadEXT; |
11260 | 0 | builder.addCapability(spv::Capability::TileImageStencilReadAccessEXT); |
11261 | 0 | } |
11262 | 0 | else |
11263 | 0 | { |
11264 | 0 | precision = spv::NoPrecision; |
11265 | 0 | spv_op = spv::Op::OpDepthAttachmentReadEXT; |
11266 | 0 | builder.addCapability(spv::Capability::TileImageDepthReadAccessEXT); |
11267 | 0 | } |
11268 | |
|
11269 | 0 | std::vector<spv::Id> args; // Dummy args |
11270 | 0 | spv::Id result = builder.createOp(spv_op, typeId, args); |
11271 | 0 | return builder.setPrecision(result, precision); |
11272 | 0 | } |
11273 | 0 | default: |
11274 | 0 | break; |
11275 | 0 | } |
11276 | | |
11277 | 0 | logger->missingFunctionality("unknown operation with no arguments"); |
11278 | |
|
11279 | 0 | return 0; |
11280 | 0 | } |
11281 | | |
11282 | | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
11283 | 25.4k | { |
11284 | 25.4k | auto iter = symbolValues.find(symbol->getId()); |
11285 | 25.4k | spv::Id id; |
11286 | 25.4k | if (symbolValues.end() != iter) { |
11287 | 20.7k | id = iter->second; |
11288 | 20.7k | return id; |
11289 | 20.7k | } |
11290 | | |
11291 | | // it was not found, create it |
11292 | 4.67k | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
11293 | 4.67k | auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
11294 | | |
11295 | | // There are pairs of symbols that map to the same SPIR-V built-in: |
11296 | | // gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, and gl_WorldToObjectEXT |
11297 | | // and gl_WorldToObject3x4EXT. SPIR-V forbids having two OpVariables |
11298 | | // with the same BuiltIn in the same storage class, so we must re-use one. |
11299 | 4.67k | const bool mayNeedToReuseBuiltIn = |
11300 | 4.67k | builtIn == spv::BuiltIn::ObjectToWorldKHR || |
11301 | 4.67k | builtIn == spv::BuiltIn::WorldToObjectKHR; |
11302 | | |
11303 | | // EXT_descriptor_heap |
11304 | 4.67k | const bool needToRemapDescHeap = |
11305 | 4.67k | builtIn == spv::BuiltIn::ResourceHeapEXT || builtIn == spv::BuiltIn::SamplerHeapEXT; |
11306 | | |
11307 | 4.67k | if (mayNeedToReuseBuiltIn || needToRemapDescHeap) { |
11308 | 0 | auto iter = builtInVariableIds.find(uint32_t(builtIn)); |
11309 | 0 | if (builtInVariableIds.end() != iter) { |
11310 | 0 | id = iter->second; |
11311 | 0 | symbolValues[symbol->getId()] = id; |
11312 | 0 | if (forcedType.second != spv::NoType) |
11313 | 0 | forceType[id] = forcedType.second; |
11314 | 0 | return id; |
11315 | 0 | } |
11316 | 0 | } |
11317 | | |
11318 | 4.67k | if (symbol->getBasicType() == glslang::EbtFunction) { |
11319 | 0 | return 0; |
11320 | 0 | } |
11321 | | |
11322 | 4.67k | id = createSpvVariable(symbol, forcedType.first); |
11323 | | |
11324 | 4.67k | if (mayNeedToReuseBuiltIn) { |
11325 | 0 | builtInVariableIds.insert({uint32_t(builtIn), id}); |
11326 | 0 | } |
11327 | | |
11328 | 4.67k | symbolValues[symbol->getId()] = id; |
11329 | 4.67k | if (forcedType.second != spv::NoType) |
11330 | 0 | forceType[id] = forcedType.second; |
11331 | | |
11332 | 4.67k | if (symbol->getBasicType() != glslang::EbtBlock) { |
11333 | 4.35k | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
11334 | 4.35k | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
11335 | 4.35k | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
11336 | 4.35k | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
11337 | 4.35k | if (symbol->getQualifier().hasComponent()) |
11338 | 0 | builder.addDecoration(id, spv::Decoration::Component, symbol->getQualifier().layoutComponent); |
11339 | 4.35k | if (symbol->getQualifier().hasIndex()) |
11340 | 0 | builder.addDecoration(id, spv::Decoration::Index, symbol->getQualifier().layoutIndex); |
11341 | 4.35k | if (symbol->getType().getQualifier().hasSpecConstantId()) |
11342 | 236 | builder.addDecoration(id, spv::Decoration::SpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
11343 | | // atomic counters use this: |
11344 | 4.35k | if (symbol->getQualifier().hasOffset()) |
11345 | 0 | builder.addDecoration(id, spv::Decoration::Offset, symbol->getQualifier().layoutOffset); |
11346 | 4.35k | } |
11347 | | |
11348 | 4.67k | if (symbol->getQualifier().hasLocation()) { |
11349 | 327 | if (!(glslangIntermediate->isRayTracingStage() && |
11350 | 0 | (glslangIntermediate->IsRequestedExtension(glslang::E_GL_EXT_ray_tracing) || |
11351 | 0 | glslangIntermediate->IsRequestedExtension(glslang::E_GL_NV_shader_invocation_reorder) || |
11352 | 0 | glslangIntermediate->IsRequestedExtension(glslang::E_GL_EXT_shader_invocation_reorder)) |
11353 | 0 | && (builder.getStorageClass(id) == spv::StorageClass::RayPayloadKHR || |
11354 | 0 | builder.getStorageClass(id) == spv::StorageClass::IncomingRayPayloadKHR || |
11355 | 0 | builder.getStorageClass(id) == spv::StorageClass::CallableDataKHR || |
11356 | 0 | builder.getStorageClass(id) == spv::StorageClass::IncomingCallableDataKHR || |
11357 | 0 | builder.getStorageClass(id) == spv::StorageClass::HitObjectAttributeEXT || |
11358 | 327 | builder.getStorageClass(id) == spv::StorageClass::HitObjectAttributeNV))) { |
11359 | | // Location values are used to link TraceRayKHR/ExecuteCallableKHR/HitObjectGetAttributesNV |
11360 | | // to corresponding variables but are not valid in SPIRV since they are supported only |
11361 | | // for Input/Output Storage classes. |
11362 | 327 | builder.addDecoration(id, spv::Decoration::Location, symbol->getQualifier().layoutLocation); |
11363 | 327 | } |
11364 | 327 | } |
11365 | | |
11366 | 4.67k | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
11367 | 4.67k | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
11368 | 0 | builder.addCapability(spv::Capability::GeometryStreams); |
11369 | 0 | builder.addDecoration(id, spv::Decoration::Stream, symbol->getQualifier().layoutStream); |
11370 | 0 | } |
11371 | 4.67k | if (symbol->getQualifier().hasSet()) |
11372 | 894 | builder.addDecoration(id, spv::Decoration::DescriptorSet, symbol->getQualifier().layoutSet); |
11373 | 3.78k | else if (IsDescriptorResource(symbol->getType())) { |
11374 | | // default to 0 |
11375 | 630 | builder.addDecoration(id, spv::Decoration::DescriptorSet, 0); |
11376 | 630 | } |
11377 | 4.67k | if (symbol->getQualifier().hasBinding()) |
11378 | 1.51k | builder.addDecoration(id, spv::Decoration::Binding, symbol->getQualifier().layoutBinding); |
11379 | 3.16k | else if (IsDescriptorResource(symbol->getType())) { |
11380 | | // default to 0 |
11381 | 12 | builder.addDecoration(id, spv::Decoration::Binding, 0); |
11382 | 12 | } |
11383 | 4.67k | if (symbol->getQualifier().hasAttachment()) |
11384 | 6 | builder.addDecoration(id, spv::Decoration::InputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
11385 | 4.67k | if (glslangIntermediate->getXfbMode()) { |
11386 | 0 | builder.addCapability(spv::Capability::TransformFeedback); |
11387 | 0 | if (symbol->getQualifier().hasXfbBuffer()) { |
11388 | 0 | builder.addDecoration(id, spv::Decoration::XfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
11389 | 0 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
11390 | 0 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
11391 | 0 | builder.addDecoration(id, spv::Decoration::XfbStride, stride); |
11392 | 0 | } |
11393 | 0 | if (symbol->getQualifier().hasXfbOffset()) |
11394 | 0 | builder.addDecoration(id, spv::Decoration::Offset, symbol->getQualifier().layoutXfbOffset); |
11395 | 0 | } |
11396 | | |
11397 | | // add built-in variable decoration |
11398 | 4.67k | if (builtIn != spv::BuiltIn::Max) { |
11399 | | // WorkgroupSize deprecated in spirv1.6 |
11400 | 0 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6 || |
11401 | 0 | builtIn != spv::BuiltIn::WorkgroupSize) |
11402 | 0 | builder.addDecoration(id, spv::Decoration::BuiltIn, (int)builtIn); |
11403 | 0 | } |
11404 | | |
11405 | | // Add volatile decoration to HelperInvocation for spirv1.6 and beyond |
11406 | 4.67k | if (builtIn == spv::BuiltIn::HelperInvocation && |
11407 | 0 | !glslangIntermediate->usingVulkanMemoryModel() && |
11408 | 0 | glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
11409 | 0 | builder.addDecoration(id, spv::Decoration::Volatile); |
11410 | 0 | } |
11411 | | |
11412 | | // Subgroup builtins which have input storage class are volatile for ray tracing stages. |
11413 | 4.67k | if (symbol->getType().isImage() || symbol->getQualifier().isPipeInput()) { |
11414 | 838 | std::vector<spv::Decoration> memory; |
11415 | 838 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
11416 | 838 | glslangIntermediate->usingVulkanMemoryModel()); |
11417 | 1.37k | for (unsigned int i = 0; i < memory.size(); ++i) |
11418 | 532 | builder.addDecoration(id, memory[i]); |
11419 | 838 | } |
11420 | | |
11421 | 4.67k | if (builtIn == spv::BuiltIn::SampleMask) { |
11422 | 0 | spv::Decoration decoration; |
11423 | | // GL_NV_sample_mask_override_coverage extension |
11424 | 0 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
11425 | 0 | decoration = spv::Decoration::OverrideCoverageNV; |
11426 | 0 | else |
11427 | 0 | decoration = spv::Decoration::Max; |
11428 | 0 | builder.addDecoration(id, decoration); |
11429 | 0 | if (decoration != spv::Decoration::Max) { |
11430 | 0 | builder.addCapability(spv::Capability::SampleMaskOverrideCoverageNV); |
11431 | 0 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
11432 | 0 | } |
11433 | 0 | } |
11434 | 4.67k | else if (builtIn == spv::BuiltIn::Layer) { |
11435 | | // SPV_NV_viewport_array2 extension |
11436 | 0 | if (symbol->getQualifier().layoutViewportRelative) { |
11437 | 0 | builder.addDecoration(id, spv::Decoration::ViewportRelativeNV); |
11438 | 0 | builder.addCapability(spv::Capability::ShaderViewportMaskNV); |
11439 | 0 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
11440 | 0 | } |
11441 | 0 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
11442 | 0 | builder.addDecoration(id, spv::Decoration::SecondaryViewportRelativeNV, |
11443 | 0 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
11444 | 0 | builder.addCapability(spv::Capability::ShaderStereoViewNV); |
11445 | 0 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
11446 | 0 | } |
11447 | 0 | } |
11448 | | |
11449 | 4.67k | if (symbol->getQualifier().layoutPassthrough) { |
11450 | 0 | builder.addDecoration(id, spv::Decoration::PassthroughNV); |
11451 | 0 | builder.addCapability(spv::Capability::GeometryShaderPassthroughNV); |
11452 | 0 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
11453 | 0 | } |
11454 | 4.67k | if (symbol->getQualifier().pervertexNV) { |
11455 | 0 | builder.addDecoration(id, spv::Decoration::PerVertexNV); |
11456 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricNV); |
11457 | 0 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
11458 | 0 | } |
11459 | | |
11460 | 4.67k | if (symbol->getQualifier().pervertexEXT) { |
11461 | 0 | builder.addDecoration(id, spv::Decoration::PerVertexKHR); |
11462 | 0 | builder.addCapability(spv::Capability::FragmentBarycentricKHR); |
11463 | 0 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
11464 | 0 | } |
11465 | | |
11466 | 4.67k | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
11467 | 0 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
11468 | 0 | builder.addDecoration(id, spv::Decoration::HlslSemanticGOOGLE, |
11469 | 0 | symbol->getType().getQualifier().semanticName); |
11470 | 0 | } |
11471 | | |
11472 | 4.67k | if (symbol->isReference()) { |
11473 | 108 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
11474 | 108 | spv::Decoration::RestrictPointerEXT : spv::Decoration::AliasedPointerEXT); |
11475 | 108 | } |
11476 | | |
11477 | | // Add SPIR-V decorations (GL_EXT_spirv_intrinsics) |
11478 | 4.67k | if (symbol->getType().getQualifier().hasSpirvDecorate()) |
11479 | 144 | applySpirvDecorate(symbol->getType(), id, {}); |
11480 | | |
11481 | 4.67k | if (symbol->getQualifier().hasBank()) { |
11482 | 0 | builder.addExtension(spv::E_SPV_NV_push_constant_bank); |
11483 | 0 | builder.addCapability(spv::Capability::PushConstantBanksNV); |
11484 | 0 | builder.addDecoration(id, spv::Decoration::BankNV, symbol->getQualifier().layoutBank); |
11485 | 0 | } |
11486 | | |
11487 | 4.67k | if (symbol->getQualifier().hasMemberOffset()) { |
11488 | 0 | builder.addExtension(spv::E_SPV_NV_push_constant_bank); |
11489 | 0 | builder.addCapability(spv::Capability::PushConstantBanksNV); |
11490 | 0 | builder.addDecoration(id, spv::Decoration::MemberOffsetNV, symbol->getQualifier().layoutMemberOffset); |
11491 | 0 | } |
11492 | | |
11493 | 4.67k | return id; |
11494 | 4.67k | } |
11495 | | |
11496 | | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
11497 | | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
11498 | 4.35k | { |
11499 | 4.35k | bool isMeshShaderExt = (glslangIntermediate->getRequestedExtensions().find(glslang::E_GL_EXT_mesh_shader) != |
11500 | 4.35k | glslangIntermediate->getRequestedExtensions().end()); |
11501 | | |
11502 | 4.35k | if (member >= 0) { |
11503 | 0 | if (qualifier.perPrimitiveNV) { |
11504 | | // Need to add capability/extension for fragment shader. |
11505 | | // Mesh shader already adds this by default. |
11506 | 0 | if (glslangIntermediate->getStage() == EShLangFragment) { |
11507 | 0 | if(isMeshShaderExt) { |
11508 | 0 | builder.addCapability(spv::Capability::MeshShadingEXT); |
11509 | 0 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
11510 | 0 | } else { |
11511 | 0 | builder.addCapability(spv::Capability::MeshShadingNV); |
11512 | 0 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
11513 | 0 | } |
11514 | 0 | } |
11515 | 0 | builder.addMemberDecoration(id, (unsigned)member, spv::Decoration::PerPrimitiveNV); |
11516 | 0 | } |
11517 | 0 | if (qualifier.perViewNV) |
11518 | 0 | builder.addMemberDecoration(id, (unsigned)member, spv::Decoration::PerViewNV); |
11519 | 0 | if (qualifier.perTaskNV) |
11520 | 0 | builder.addMemberDecoration(id, (unsigned)member, spv::Decoration::PerTaskNV); |
11521 | 4.35k | } else { |
11522 | 4.35k | if (qualifier.perPrimitiveNV) { |
11523 | | // Need to add capability/extension for fragment shader. |
11524 | | // Mesh shader already adds this by default. |
11525 | 0 | if (glslangIntermediate->getStage() == EShLangFragment) { |
11526 | 0 | if(isMeshShaderExt) { |
11527 | 0 | builder.addCapability(spv::Capability::MeshShadingEXT); |
11528 | 0 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
11529 | 0 | } else { |
11530 | 0 | builder.addCapability(spv::Capability::MeshShadingNV); |
11531 | 0 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
11532 | 0 | } |
11533 | 0 | } |
11534 | 0 | builder.addDecoration(id, spv::Decoration::PerPrimitiveNV); |
11535 | 0 | } |
11536 | 4.35k | if (qualifier.perViewNV) |
11537 | 0 | builder.addDecoration(id, spv::Decoration::PerViewNV); |
11538 | 4.35k | if (qualifier.perTaskNV) |
11539 | 0 | builder.addDecoration(id, spv::Decoration::PerTaskNV); |
11540 | 4.35k | } |
11541 | 4.35k | } |
11542 | | |
11543 | | bool TGlslangToSpvTraverser::hasQCOMImageProceessingDecoration(spv::Id id, spv::Decoration decor) |
11544 | 342 | { |
11545 | 342 | std::vector<spv::Decoration> &decoVec = idToQCOMDecorations[id]; |
11546 | 342 | for ( auto d : decoVec ) { |
11547 | 54 | if ( d == decor ) |
11548 | 18 | return true; |
11549 | 54 | } |
11550 | 324 | return false; |
11551 | 342 | } |
11552 | | |
11553 | | void TGlslangToSpvTraverser::addImageProcessingQCOMDecoration(spv::Id id, spv::Decoration decor) |
11554 | 198 | { |
11555 | 198 | spv::Op opc = builder.getOpCode(id); |
11556 | 198 | if (opc == spv::Op::OpSampledImage) { |
11557 | 99 | id = builder.getIdOperand(id, 0); |
11558 | 99 | opc = builder.getOpCode(id); |
11559 | 99 | } |
11560 | | |
11561 | 198 | if (opc == spv::Op::OpLoad) { |
11562 | 198 | spv::Id texid = builder.getIdOperand(id, 0); |
11563 | 198 | if (!hasQCOMImageProceessingDecoration(texid, decor)) {// |
11564 | 198 | builder.addDecoration(texid, decor); |
11565 | 198 | idToQCOMDecorations[texid].push_back(decor); |
11566 | 198 | } |
11567 | 198 | } |
11568 | 198 | } |
11569 | | |
11570 | | void TGlslangToSpvTraverser::addImageProcessing2QCOMDecoration(spv::Id id, bool isForGather) |
11571 | 144 | { |
11572 | 144 | if (isForGather) { |
11573 | 72 | return addImageProcessingQCOMDecoration(id, spv::Decoration::BlockMatchTextureQCOM); |
11574 | 72 | } |
11575 | | |
11576 | 72 | auto addDecor = |
11577 | 144 | [this](spv::Id id, spv::Decoration decor) { |
11578 | 144 | spv::Op tsopc = this->builder.getOpCode(id); |
11579 | 144 | if (tsopc == spv::Op::OpLoad) { |
11580 | 144 | spv::Id tsid = this->builder.getIdOperand(id, 0); |
11581 | 144 | if (this->glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
11582 | 0 | assert(iOSet.count(tsid) > 0); |
11583 | 0 | } |
11584 | 144 | if (!hasQCOMImageProceessingDecoration(tsid, decor)) { |
11585 | 126 | this->builder.addDecoration(tsid, decor); |
11586 | 126 | idToQCOMDecorations[tsid].push_back(decor); |
11587 | 126 | } |
11588 | 144 | } |
11589 | 144 | }; |
11590 | | |
11591 | 72 | spv::Op opc = builder.getOpCode(id); |
11592 | 72 | bool isInterfaceObject = (opc != spv::Op::OpSampledImage); |
11593 | | |
11594 | 72 | if (!isInterfaceObject) { |
11595 | 36 | addDecor(builder.getIdOperand(id, 0), spv::Decoration::BlockMatchTextureQCOM); |
11596 | 36 | addDecor(builder.getIdOperand(id, 1), spv::Decoration::BlockMatchSamplerQCOM); |
11597 | 36 | } else { |
11598 | 36 | addDecor(id, spv::Decoration::BlockMatchTextureQCOM); |
11599 | 36 | addDecor(id, spv::Decoration::BlockMatchSamplerQCOM); |
11600 | 36 | } |
11601 | 72 | } |
11602 | | |
11603 | | // Make a full tree of instructions to build a SPIR-V specialization constant, |
11604 | | // or regular constant if possible. |
11605 | | // |
11606 | | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
11607 | | // |
11608 | | // Recursively walk the nodes. The nodes form a tree whose leaves are |
11609 | | // regular constants, which themselves are trees that createSpvConstant() |
11610 | | // recursively walks. So, this function walks the "top" of the tree: |
11611 | | // - emit specialization constant-building instructions for specConstant |
11612 | | // - when running into a non-spec-constant, switch to createSpvConstant() |
11613 | | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
11614 | 611 | { |
11615 | 611 | assert(node.getQualifier().isConstant()); |
11616 | | |
11617 | | // Handle front-end constants first (non-specialization constants). |
11618 | 611 | if (! node.getQualifier().specConstant) { |
11619 | | // hand off to the non-spec-constant path |
11620 | 39 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
11621 | 39 | int nextConst = 0; |
11622 | 39 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
11623 | 39 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
11624 | 39 | nextConst, false); |
11625 | 39 | } |
11626 | | |
11627 | | // We now know we have a specialization constant to build |
11628 | | |
11629 | | // Extra capabilities may be needed. |
11630 | 572 | if (node.getType().contains8BitInt()) |
11631 | 21 | builder.addCapability(spv::Capability::Int8); |
11632 | 572 | if (node.getType().contains16BitFloat()) |
11633 | 0 | builder.addCapability(spv::Capability::Float16); |
11634 | 572 | if (node.getType().contains16BitInt()) |
11635 | 168 | builder.addCapability(spv::Capability::Int16); |
11636 | 572 | if (node.getType().contains64BitInt()) |
11637 | 162 | builder.addCapability(spv::Capability::Int64); |
11638 | 572 | if (node.getType().containsDouble()) |
11639 | 0 | builder.addCapability(spv::Capability::Float64); |
11640 | | |
11641 | | // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants, |
11642 | | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
11643 | 572 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
11644 | 0 | std::vector<spv::Id> dimConstId; |
11645 | 0 | for (int dim = 0; dim < 3; ++dim) { |
11646 | 0 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
11647 | 0 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
11648 | 0 | if (specConst) { |
11649 | 0 | builder.addDecoration(dimConstId.back(), spv::Decoration::SpecId, |
11650 | 0 | glslangIntermediate->getLocalSizeSpecId(dim)); |
11651 | 0 | } |
11652 | 0 | } |
11653 | 0 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
11654 | 0 | } |
11655 | | |
11656 | | // An AST node labelled as specialization constant should be a symbol node. |
11657 | | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
11658 | 572 | if (auto* sn = node.getAsSymbolNode()) { |
11659 | 572 | spv::Id result; |
11660 | 572 | if (auto* sub_tree = sn->getConstSubtree()) { |
11661 | | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
11662 | | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
11663 | | // will set the builder into spec constant op instruction generating mode. |
11664 | 346 | sub_tree->traverse(this); |
11665 | 346 | result = accessChainLoad(sub_tree->getType()); |
11666 | 346 | } else if (auto* const_union_array = &sn->getConstArray()) { |
11667 | 226 | int nextConst = 0; |
11668 | 226 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
11669 | 226 | } else { |
11670 | 0 | logger->missingFunctionality("Invalid initializer for spec constant."); |
11671 | 0 | return spv::NoResult; |
11672 | 0 | } |
11673 | 572 | builder.addName(result, sn->getName().c_str()); |
11674 | 572 | return result; |
11675 | 572 | } |
11676 | | |
11677 | | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
11678 | | // constant sub tree as initializer. |
11679 | 0 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
11680 | 0 | return spv::NoResult; |
11681 | 572 | } |
11682 | | |
11683 | | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
11684 | | // build the aggregate SPIR-V constant. |
11685 | | // |
11686 | | // If there are not enough elements present in 'consts', 0 will be substituted; |
11687 | | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
11688 | | // |
11689 | | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
11690 | | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
11691 | 8.81k | { |
11692 | | // vector of constants for SPIR-V |
11693 | 8.81k | std::vector<spv::Id> spvConsts; |
11694 | | |
11695 | | // Type is used for struct and array constants |
11696 | 8.81k | spv::Id typeId = convertGlslangToSpvType(glslangType); |
11697 | | |
11698 | 8.81k | if (glslangType.isArray()) { |
11699 | 121 | glslang::TType elementType(glslangType, 0); |
11700 | 568 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
11701 | 447 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
11702 | 8.69k | } else if (glslangType.isMatrix()) { |
11703 | 72 | glslang::TType vectorType(glslangType, 0); |
11704 | 252 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
11705 | 180 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
11706 | 8.61k | } else if (glslangType.isCoopMat()) { |
11707 | 0 | glslang::TType componentType(glslangType.getBasicType()); |
11708 | 0 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
11709 | 8.61k | } else if (glslangType.isStruct()) { |
11710 | 216 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
11711 | 747 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
11712 | 531 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
11713 | 8.40k | } else if (glslangType.getVectorSize() > 1 || glslangType.isCoopVecOrLongVector()) { |
11714 | 4.30k | unsigned int numComponents = glslangType.isCoopVecOrLongVector() ? glslangType.getTypeParameters()->arraySizes->getDimSize(0) : glslangType.getVectorSize(); |
11715 | 16.3k | for (unsigned int i = 0; i < numComponents; ++i) { |
11716 | 12.0k | bool zero = nextConst >= consts.size(); |
11717 | 12.0k | switch (glslangType.getBasicType()) { |
11718 | 4.77k | case glslang::EbtInt: |
11719 | 4.77k | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
11720 | 4.77k | break; |
11721 | 881 | case glslang::EbtUint: |
11722 | 881 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
11723 | 881 | break; |
11724 | 1.16k | case glslang::EbtFloat: |
11725 | 1.16k | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
11726 | 1.16k | break; |
11727 | 45 | case glslang::EbtBool: |
11728 | 45 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
11729 | 45 | break; |
11730 | 720 | case glslang::EbtInt8: |
11731 | 720 | builder.addCapability(spv::Capability::Int8); |
11732 | 720 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
11733 | 720 | break; |
11734 | 720 | case glslang::EbtUint8: |
11735 | 720 | builder.addCapability(spv::Capability::Int8); |
11736 | 720 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
11737 | 720 | break; |
11738 | 756 | case glslang::EbtInt16: |
11739 | 756 | builder.addCapability(spv::Capability::Int16); |
11740 | 756 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
11741 | 756 | break; |
11742 | 774 | case glslang::EbtUint16: |
11743 | 774 | builder.addCapability(spv::Capability::Int16); |
11744 | 774 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
11745 | 774 | break; |
11746 | 720 | case glslang::EbtInt64: |
11747 | 720 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
11748 | 720 | break; |
11749 | 720 | case glslang::EbtUint64: |
11750 | 720 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
11751 | 720 | break; |
11752 | 0 | case glslang::EbtDouble: |
11753 | 0 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
11754 | 0 | break; |
11755 | 738 | case glslang::EbtFloat16: |
11756 | 738 | builder.addCapability(spv::Capability::Float16); |
11757 | 738 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
11758 | 738 | break; |
11759 | 0 | case glslang::EbtBFloat16: |
11760 | 0 | spvConsts.push_back(builder.makeBFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
11761 | 0 | break; |
11762 | 0 | case glslang::EbtFloatE5M2: |
11763 | 0 | spvConsts.push_back(builder.makeFloatE5M2Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
11764 | 0 | break; |
11765 | 0 | case glslang::EbtFloatE4M3: |
11766 | 0 | spvConsts.push_back(builder.makeFloatE4M3Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
11767 | 0 | break; |
11768 | 0 | default: |
11769 | 0 | assert(0); |
11770 | 0 | break; |
11771 | 12.0k | } |
11772 | 12.0k | ++nextConst; |
11773 | 12.0k | } |
11774 | 4.30k | } else { |
11775 | | // we have a non-aggregate (scalar) constant |
11776 | 4.09k | bool zero = nextConst >= consts.size(); |
11777 | 4.09k | spv::Id scalar = 0; |
11778 | 4.09k | switch (glslangType.getBasicType()) { |
11779 | 2.03k | case glslang::EbtInt: |
11780 | 2.03k | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
11781 | 2.03k | break; |
11782 | 233 | case glslang::EbtUint: |
11783 | 233 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
11784 | 233 | break; |
11785 | 752 | case glslang::EbtFloat: |
11786 | 752 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
11787 | 752 | break; |
11788 | 422 | case glslang::EbtBool: |
11789 | 422 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
11790 | 422 | break; |
11791 | 28 | case glslang::EbtInt8: |
11792 | 28 | builder.addCapability(spv::Capability::Int8); |
11793 | 28 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
11794 | 28 | break; |
11795 | 29 | case glslang::EbtUint8: |
11796 | 29 | builder.addCapability(spv::Capability::Int8); |
11797 | 29 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
11798 | 29 | break; |
11799 | 85 | case glslang::EbtInt16: |
11800 | 85 | builder.addCapability(spv::Capability::Int16); |
11801 | 85 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
11802 | 85 | break; |
11803 | 84 | case glslang::EbtUint16: |
11804 | 84 | builder.addCapability(spv::Capability::Int16); |
11805 | 84 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
11806 | 84 | break; |
11807 | 76 | case glslang::EbtInt64: |
11808 | 76 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
11809 | 76 | break; |
11810 | 276 | case glslang::EbtUint64: |
11811 | 276 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
11812 | 276 | break; |
11813 | 18 | case glslang::EbtDouble: |
11814 | 18 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
11815 | 18 | break; |
11816 | 18 | case glslang::EbtFloat16: |
11817 | 18 | builder.addCapability(spv::Capability::Float16); |
11818 | 18 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
11819 | 18 | break; |
11820 | 0 | case glslang::EbtBFloat16: |
11821 | 0 | scalar = builder.makeBFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
11822 | 0 | break; |
11823 | 0 | case glslang::EbtFloatE5M2: |
11824 | 0 | scalar = builder.makeFloatE5M2Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
11825 | 0 | break; |
11826 | 0 | case glslang::EbtFloatE4M3: |
11827 | 0 | scalar = builder.makeFloatE4M3Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
11828 | 0 | break; |
11829 | 45 | case glslang::EbtReference: |
11830 | 45 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
11831 | 45 | scalar = builder.createUnaryOp(spv::Op::OpBitcast, typeId, scalar); |
11832 | 45 | break; |
11833 | 0 | case glslang::EbtString: |
11834 | 0 | scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
11835 | 0 | break; |
11836 | 0 | default: |
11837 | 0 | assert(0); |
11838 | 0 | break; |
11839 | 4.09k | } |
11840 | 4.09k | ++nextConst; |
11841 | 4.09k | return scalar; |
11842 | 4.09k | } |
11843 | | |
11844 | 4.71k | return builder.makeCompositeConstant(typeId, spvConsts); |
11845 | 8.81k | } |
11846 | | |
11847 | | // Return true if the node is a constant or symbol whose reading has no |
11848 | | // non-trivial observable cost or effect. |
11849 | | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
11850 | 101 | { |
11851 | | // don't know what this is |
11852 | 101 | if (node == nullptr) |
11853 | 0 | return false; |
11854 | | |
11855 | | // a constant is safe |
11856 | 101 | if (node->getAsConstantUnion() != nullptr) |
11857 | 0 | return true; |
11858 | | |
11859 | | // not a symbol means non-trivial |
11860 | 101 | if (node->getAsSymbolNode() == nullptr) |
11861 | 88 | return false; |
11862 | | |
11863 | | // a symbol, depends on what's being read |
11864 | 13 | switch (node->getType().getQualifier().storage) { |
11865 | 13 | case glslang::EvqTemporary: |
11866 | 13 | case glslang::EvqGlobal: |
11867 | 13 | case glslang::EvqIn: |
11868 | 13 | case glslang::EvqInOut: |
11869 | 13 | case glslang::EvqConst: |
11870 | 13 | case glslang::EvqConstReadOnly: |
11871 | 13 | case glslang::EvqUniform: |
11872 | 13 | return true; |
11873 | 0 | default: |
11874 | 0 | return false; |
11875 | 13 | } |
11876 | 13 | } |
11877 | | |
11878 | | // A node is trivial if it is a single operation with no side effects. |
11879 | | // HLSL (and/or vectors) are always trivial, as it does not short circuit. |
11880 | | // Otherwise, error on the side of saying non-trivial. |
11881 | | // Return true if trivial. |
11882 | | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
11883 | 62 | { |
11884 | 62 | if (node == nullptr) |
11885 | 0 | return false; |
11886 | | |
11887 | | // count non scalars as trivial, as well as anything coming from HLSL |
11888 | 62 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
11889 | 0 | return true; |
11890 | | |
11891 | | // symbols and constants are trivial |
11892 | 62 | if (isTrivialLeaf(node)) |
11893 | 0 | return true; |
11894 | | |
11895 | | // otherwise, it needs to be a simple operation or one or two leaf nodes |
11896 | | |
11897 | | // not a simple operation |
11898 | 62 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
11899 | 62 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
11900 | 62 | if (binaryNode == nullptr && unaryNode == nullptr) |
11901 | 36 | return false; |
11902 | | |
11903 | | // not on leaf nodes |
11904 | 26 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
11905 | 26 | return false; |
11906 | | |
11907 | 0 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
11908 | 0 | return false; |
11909 | 0 | } |
11910 | | |
11911 | 0 | if (IsOpNumericConv(node->getAsOperator()->getOp()) && |
11912 | 0 | node->getType().getBasicType() == glslang::EbtBool) { |
11913 | 0 | return true; |
11914 | 0 | } |
11915 | | |
11916 | 0 | switch (node->getAsOperator()->getOp()) { |
11917 | 0 | case glslang::EOpLogicalNot: |
11918 | 0 | case glslang::EOpEqual: |
11919 | 0 | case glslang::EOpNotEqual: |
11920 | 0 | case glslang::EOpLessThan: |
11921 | 0 | case glslang::EOpGreaterThan: |
11922 | 0 | case glslang::EOpLessThanEqual: |
11923 | 0 | case glslang::EOpGreaterThanEqual: |
11924 | 0 | case glslang::EOpIndexDirect: |
11925 | 0 | case glslang::EOpIndexDirectStruct: |
11926 | 0 | case glslang::EOpLogicalXor: |
11927 | 0 | case glslang::EOpAny: |
11928 | 0 | case glslang::EOpAll: |
11929 | 0 | return true; |
11930 | 0 | default: |
11931 | 0 | return false; |
11932 | 0 | } |
11933 | 0 | } |
11934 | | |
11935 | | // Emit short-circuiting code, where 'right' is never evaluated unless |
11936 | | // the left side is true (for &&) or false (for ||). |
11937 | | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
11938 | | glslang::TIntermTyped& right) |
11939 | 62 | { |
11940 | 62 | spv::Id boolTypeId = builder.makeBoolType(); |
11941 | | |
11942 | | // emit left operand |
11943 | 62 | builder.clearAccessChain(); |
11944 | 62 | left.traverse(this); |
11945 | 62 | spv::Id leftId = accessChainLoad(left.getType()); |
11946 | | |
11947 | | // Operands to accumulate OpPhi operands |
11948 | 62 | std::vector<spv::Id> phiOperands; |
11949 | 62 | phiOperands.reserve(4); |
11950 | | // accumulate left operand's phi information |
11951 | 62 | phiOperands.push_back(leftId); |
11952 | 62 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
11953 | | |
11954 | | // Make the two kinds of operation symmetric with a "!" |
11955 | | // || => emit "if (! left) result = right" |
11956 | | // && => emit "if ( left) result = right" |
11957 | | // |
11958 | | // TODO: this runtime "not" for || could be avoided by adding functionality |
11959 | | // to 'builder' to have an "else" without an "then" |
11960 | 62 | if (op == glslang::EOpLogicalOr) |
11961 | 0 | leftId = builder.createUnaryOp(spv::Op::OpLogicalNot, boolTypeId, leftId); |
11962 | | |
11963 | | // make an "if" based on the left value |
11964 | 62 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMask::MaskNone, builder); |
11965 | | |
11966 | | // emit right operand as the "then" part of the "if" |
11967 | 62 | builder.clearAccessChain(); |
11968 | 62 | right.traverse(this); |
11969 | 62 | spv::Id rightId = accessChainLoad(right.getType()); |
11970 | | |
11971 | | // accumulate left operand's phi information |
11972 | 62 | phiOperands.push_back(rightId); |
11973 | 62 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
11974 | | |
11975 | | // finish the "if" |
11976 | 62 | ifBuilder.makeEndIf(); |
11977 | | |
11978 | | // phi together the two results |
11979 | 62 | return builder.createOp(spv::Op::OpPhi, boolTypeId, phiOperands); |
11980 | 62 | } |
11981 | | |
11982 | | // Return type Id of the imported set of extended instructions corresponds to the name. |
11983 | | // Import this set if it has not been imported yet. |
11984 | | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
11985 | 18 | { |
11986 | 18 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
11987 | 0 | return extBuiltinMap[name]; |
11988 | 18 | else { |
11989 | 18 | spv::Id extBuiltins = builder.import(name); |
11990 | 18 | extBuiltinMap[name] = extBuiltins; |
11991 | 18 | return extBuiltins; |
11992 | 18 | } |
11993 | 18 | } |
11994 | | |
11995 | | } // end anonymous namespace |
11996 | | |
11997 | | namespace glslang { |
11998 | | |
11999 | | void GetSpirvVersion(std::string& version) |
12000 | 0 | { |
12001 | 0 | const int bufSize = 100; |
12002 | 0 | char buf[bufSize]; |
12003 | 0 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
12004 | 0 | version = buf; |
12005 | 0 | } |
12006 | | |
12007 | | // For low-order part of the generator's magic number. Bump up |
12008 | | // when there is a change in the style (e.g., if SSA form changes, |
12009 | | // or a different instruction sequence to do something gets used). |
12010 | | int GetSpirvGeneratorVersion() |
12011 | 370 | { |
12012 | | // return 1; // start |
12013 | | // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V |
12014 | | // return 3; // change/correct barrier-instruction operands, to match memory model group decisions |
12015 | | // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component |
12016 | | // return 5; // make OpArrayLength result type be an int with signedness of 0 |
12017 | | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
12018 | | // versions 4 and 6 each generate OpArrayLength as it has long been done |
12019 | | // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
12020 | | // return 8; // switch to new dead block eliminator; use OpUnreachable |
12021 | | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
12022 | | // return 10; // Generate OpFUnordNotEqual for != comparisons |
12023 | 370 | return 11; // Make OpEmitMeshTasksEXT a terminal instruction |
12024 | 370 | } |
12025 | | |
12026 | | // Write SPIR-V out to a binary file |
12027 | | bool OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
12028 | 0 | { |
12029 | 0 | std::ofstream out; |
12030 | 0 | out.open(baseName, std::ios::binary | std::ios::out); |
12031 | 0 | if (out.fail()) { |
12032 | 0 | printf("ERROR: Failed to open file: %s\n", baseName); |
12033 | 0 | return false; |
12034 | 0 | } |
12035 | 0 | for (int i = 0; i < (int)spirv.size(); ++i) { |
12036 | 0 | unsigned int word = spirv[i]; |
12037 | 0 | out.write((const char*)&word, 4); |
12038 | 0 | } |
12039 | 0 | out.close(); |
12040 | 0 | return true; |
12041 | 0 | } |
12042 | | |
12043 | | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
12044 | | bool OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) |
12045 | 0 | { |
12046 | 0 | std::ofstream out; |
12047 | 0 | out.open(baseName, std::ios::binary | std::ios::out); |
12048 | 0 | if (out.fail()) { |
12049 | 0 | printf("ERROR: Failed to open file: %s\n", baseName); |
12050 | 0 | return false; |
12051 | 0 | } |
12052 | 0 | out << "\t// " << |
12053 | 0 | GetSpirvGeneratorVersion() << |
12054 | 0 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
12055 | 0 | GLSLANG_VERSION_FLAVOR << std::endl; |
12056 | 0 | if (varName != nullptr) { |
12057 | 0 | out << "\t #pragma once" << std::endl; |
12058 | 0 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
12059 | 0 | } |
12060 | 0 | const int WORDS_PER_LINE = 8; |
12061 | 0 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
12062 | 0 | out << "\t"; |
12063 | 0 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
12064 | 0 | const unsigned int word = spirv[i + j]; |
12065 | 0 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
12066 | 0 | if (i + j + 1 < (int)spirv.size()) { |
12067 | 0 | out << ","; |
12068 | 0 | } |
12069 | 0 | } |
12070 | 0 | out << std::endl; |
12071 | 0 | } |
12072 | 0 | if (varName != nullptr) { |
12073 | 0 | out << "};"; |
12074 | 0 | out << std::endl; |
12075 | 0 | } |
12076 | 0 | out.close(); |
12077 | 0 | return true; |
12078 | 0 | } |
12079 | | |
12080 | | // |
12081 | | // Set up the glslang traversal |
12082 | | // |
12083 | | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
12084 | 370 | { |
12085 | 370 | spv::SpvBuildLogger logger; |
12086 | 370 | GlslangToSpv(intermediate, spirv, &logger, options); |
12087 | 370 | } |
12088 | | |
12089 | | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
12090 | | spv::SpvBuildLogger* logger, SpvOptions* options) |
12091 | 370 | { |
12092 | 370 | TIntermNode* root = intermediate.getTreeRoot(); |
12093 | | |
12094 | 370 | if (root == nullptr) |
12095 | 0 | return; |
12096 | | |
12097 | 370 | SpvOptions defaultOptions; |
12098 | 370 | if (options == nullptr) |
12099 | 0 | options = &defaultOptions; |
12100 | | |
12101 | 370 | GetThreadPoolAllocator().push(); |
12102 | | |
12103 | 370 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
12104 | 370 | root->traverse(&it); |
12105 | 370 | it.finishSpv(options->compileOnly); |
12106 | 370 | it.dumpSpv(spirv); |
12107 | | |
12108 | 370 | #if ENABLE_OPT |
12109 | | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
12110 | | // eg. forward and remove memory writes of opaque types. |
12111 | 370 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
12112 | 370 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
12113 | 0 | SpirvToolsTransform(intermediate, spirv, logger, options); |
12114 | 0 | prelegalization = false; |
12115 | 0 | } |
12116 | 370 | else if (options->stripDebugInfo) { |
12117 | | // Strip debug info even if optimization is disabled. |
12118 | 0 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
12119 | 0 | } |
12120 | | |
12121 | 370 | if (options->validate) |
12122 | 0 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
12123 | | |
12124 | 370 | if (options->disassemble) |
12125 | 0 | SpirvToolsDisassemble(std::cout, spirv); |
12126 | | |
12127 | 370 | #endif |
12128 | | |
12129 | 370 | GetThreadPoolAllocator().pop(); |
12130 | 370 | } |
12131 | | |
12132 | | } // end namespace glslang |