blob: f0bf3a6edbf0a54b84fa9ad7356d52590a610ad1 [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2014-2016 LunarG, Inc.
John Kessenich56364b62020-03-01 04:51:40 -07003// Copyright (C) 2015-2020 Google, Inc.
John Kessenich66011cb2018-03-06 16:12:04 -07004// Copyright (C) 2017 ARM Limited.
Torosdagli06c2eee2020-03-19 11:09:57 -04005// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06006//
John Kessenich927608b2017-01-06 12:34:14 -07007// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06008//
John Kessenich927608b2017-01-06 12:34:14 -07009// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions
11// are met:
John Kessenich140f3df2015-06-26 16:58:36 -060012//
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//
John Kessenich927608b2017-01-06 12:34:14 -070025// 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.
John Kessenich140f3df2015-06-26 16:58:36 -060037
38//
John Kessenich140f3df2015-06-26 16:58:36 -060039// Visit the nodes in the glslang intermediate tree representation to
40// translate them to SPIR-V.
41//
42
John Kessenich5e4b1242015-08-06 22:53:06 -060043#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060044#include "GlslangToSpv.h"
45#include "SpvBuilder.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046namespace spv {
Rex Xu51596642016-09-21 18:56:12 +080047 #include "GLSL.std.450.h"
48 #include "GLSL.ext.KHR.h"
Piers Daniell1c5443c2017-12-13 13:07:22 -070049 #include "GLSL.ext.EXT.h"
Rex Xu51596642016-09-21 18:56:12 +080050 #include "GLSL.ext.AMD.h"
chaoc0ad6a4e2016-12-19 16:29:34 -080051 #include "GLSL.ext.NV.h"
Jeff Bolz04d73732019-05-31 13:06:01 -050052 #include "NonSemanticDebugPrintf.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060053}
John Kessenich140f3df2015-06-26 16:58:36 -060054
55// Glslang includes
baldurk42169c52015-07-08 15:11:59 +020056#include "../glslang/MachineIndependent/localintermediate.h"
57#include "../glslang/MachineIndependent/SymbolTable.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060058#include "../glslang/Include/Common.h"
Ben Claytonfbe9a232020-06-17 11:17:19 +010059
60// Build-time generated includes
61#include "glslang/build_info.h"
John Kessenich140f3df2015-06-26 16:58:36 -060062
John Kessenich140f3df2015-06-26 16:58:36 -060063#include <fstream>
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050064#include <iomanip>
Lei Zhang17535f72016-05-04 15:55:59 -040065#include <list>
66#include <map>
67#include <stack>
68#include <string>
69#include <vector>
John Kessenich140f3df2015-06-26 16:58:36 -060070
71namespace {
72
qining4c912612016-04-01 10:35:16 -040073namespace {
74class SpecConstantOpModeGuard {
75public:
76 SpecConstantOpModeGuard(spv::Builder* builder)
77 : builder_(builder) {
78 previous_flag_ = builder->isInSpecConstCodeGenMode();
qining4c912612016-04-01 10:35:16 -040079 }
80 ~SpecConstantOpModeGuard() {
81 previous_flag_ ? builder_->setToSpecConstCodeGenMode()
82 : builder_->setToNormalCodeGenMode();
83 }
qining40887662016-04-03 22:20:42 -040084 void turnOnSpecConstantOpMode() {
85 builder_->setToSpecConstCodeGenMode();
86 }
qining4c912612016-04-01 10:35:16 -040087
88private:
89 spv::Builder* builder_;
90 bool previous_flag_;
91};
John Kessenichead86222018-03-28 18:01:20 -060092
93struct OpDecorations {
John Kessenichb9197c82019-08-11 07:41:45 -060094 public:
95 OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) :
96 precision(precision)
97#ifndef GLSLANG_WEB
98 ,
99 noContraction(noContraction),
100 nonUniform(nonUniform)
101#endif
102 { }
103
John Kessenichead86222018-03-28 18:01:20 -0600104 spv::Decoration precision;
John Kessenichb9197c82019-08-11 07:41:45 -0600105
106#ifdef GLSLANG_WEB
Ryan Harrison7c9accb2019-10-11 11:00:57 -0400107 void addNoContraction(spv::Builder&, spv::Id) const { }
108 void addNonUniform(spv::Builder&, spv::Id) const { }
John Kessenichb9197c82019-08-11 07:41:45 -0600109#else
Ryan Harrison7c9accb2019-10-11 11:00:57 -0400110 void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); }
111 void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); }
John Kessenichb9197c82019-08-11 07:41:45 -0600112 protected:
113 spv::Decoration noContraction;
114 spv::Decoration nonUniform;
115#endif
116
John Kessenichead86222018-03-28 18:01:20 -0600117};
118
119} // namespace
qining4c912612016-04-01 10:35:16 -0400120
John Kessenich140f3df2015-06-26 16:58:36 -0600121//
122// The main holder of information for translating glslang to SPIR-V.
123//
124// Derives from the AST walking base class.
125//
126class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
127public:
John Kessenich2b5ea9f2018-01-31 18:35:56 -0700128 TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger,
129 glslang::SpvOptions& options);
John Kessenichfca82622016-11-26 13:23:20 -0700130 virtual ~TGlslangToSpvTraverser() { }
John Kessenich140f3df2015-06-26 16:58:36 -0600131
132 bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*);
133 bool visitBinary(glslang::TVisit, glslang::TIntermBinary*);
134 void visitConstantUnion(glslang::TIntermConstantUnion*);
135 bool visitSelection(glslang::TVisit, glslang::TIntermSelection*);
136 bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*);
137 void visitSymbol(glslang::TIntermSymbol* symbol);
138 bool visitUnary(glslang::TVisit, glslang::TIntermUnary*);
139 bool visitLoop(glslang::TVisit, glslang::TIntermLoop*);
140 bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*);
141
John Kessenichfca82622016-11-26 13:23:20 -0700142 void finishSpv();
John Kessenich7ba63412015-12-20 17:37:07 -0700143 void dumpSpv(std::vector<unsigned int>& out);
John Kessenich140f3df2015-06-26 16:58:36 -0600144
145protected:
John Kessenich5d610ee2018-03-07 18:05:55 -0700146 TGlslangToSpvTraverser(TGlslangToSpvTraverser&);
147 TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&);
148
Rex Xu17ff3432016-10-14 17:41:45 +0800149 spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier);
Rex Xubbceed72016-05-21 09:40:44 +0800150 spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier);
John Kessenich5611c6d2018-04-05 11:25:02 -0600151 spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier);
greg-lunarg639f5462020-11-12 11:10:07 -0700152 spv::Decoration TranslateNonUniformDecoration(const spv::Builder::AccessChain::CoherentFlags& coherentFlags);
Jeff Bolz36831c92018-09-05 10:11:41 -0500153 spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type);
154 spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
155 spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
156 spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
David Netoa901ffe2016-06-08 14:11:40 +0100157 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
John Kessenich5d0fa972016-02-15 11:57:00 -0700158 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
John Kesseniche18fd202018-01-30 11:01:39 -0700159 spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const;
160 spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const;
John Kessenich1f4d0462019-01-12 17:31:41 +0700161 spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const;
John Kessenicha5c5fb62017-05-05 05:09:58 -0600162 spv::StorageClass TranslateStorageClass(const glslang::TType&);
John Kessenich5611c6d2018-04-05 11:25:02 -0600163 void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType);
John Kessenich9c14f772019-06-17 08:38:35 -0600164 spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType);
John Kessenich140f3df2015-06-26 16:58:36 -0600165 spv::Id getSampledType(const glslang::TSampler&);
John Kessenich8c8505c2016-07-26 12:50:38 -0600166 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
167 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
168 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600169 spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false);
John Kessenichead86222018-03-28 18:01:20 -0600170 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&,
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600171 bool lastBufferBlockMember, bool forwardReferenceOnly = false);
John Kessenich0e737842017-03-24 18:38:16 -0600172 bool filterMember(const glslang::TType& member);
John Kessenich6090df02016-06-30 21:18:02 -0600173 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
174 glslang::TLayoutPacking, const glslang::TQualifier&);
175 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
176 const glslang::TQualifier&, spv::Id);
John Kessenich6c292d32016-02-15 20:58:50 -0700177 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
John Kessenich32cfd492016-02-02 12:37:46 -0700178 spv::Id accessChainLoad(const glslang::TType& type);
Rex Xu27253232016-02-23 17:51:09 +0800179 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
John Kessenich4bf71552016-09-02 11:20:21 -0600180 void multiTypeStore(const glslang::TType&, spv::Id rValue);
John Kessenichf85e8062015-12-19 13:57:10 -0700181 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
John Kessenich3ac051e2015-12-20 11:29:16 -0700182 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
183 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
John Kessenich5d610ee2018-03-07 18:05:55 -0700184 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset,
185 int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
David Netoa901ffe2016-06-08 14:11:40 +0100186 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
John Kessenich140f3df2015-06-26 16:58:36 -0600187
John Kessenich6fccb3c2016-09-19 16:01:41 -0600188 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
John Kessenichd3ed90b2018-05-04 11:43:03 -0600189 bool writableParam(glslang::TStorageQualifier) const;
John Kessenichd41993d2017-09-10 15:21:05 -0600190 bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam);
John Kessenich140f3df2015-06-26 16:58:36 -0600191 void makeFunctions(const glslang::TIntermSequence&);
192 void makeGlobalInitializers(const glslang::TIntermSequence&);
Daniel Kochffccefd2020-11-23 15:41:27 -0500193 void collectRayTracingLinkerObjects();
John Kessenich140f3df2015-06-26 16:58:36 -0600194 void visitFunctions(const glslang::TIntermSequence&);
195 void handleFunctionEntry(const glslang::TIntermAggregate* node);
John Kessenich8985fc92020-03-03 10:25:07 -0700196 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments,
197 spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -0600198 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
199 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600200 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
201
John Kessenichead86222018-03-28 18:01:20 -0600202 spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right,
203 glslang::TBasicType typeProxy, bool reduceComparison = true);
204 spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right);
205 spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand,
John Kessenich8985fc92020-03-03 10:25:07 -0700206 glslang::TBasicType typeProxy,
207 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -0600208 spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand,
209 glslang::TBasicType typeProxy);
210 spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand,
211 glslang::TBasicType typeProxy);
John Kessenichad7645f2018-06-04 19:11:25 -0600212 spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize);
John Kessenich140f3df2015-06-26 16:58:36 -0600213 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
John Kessenich8985fc92020-03-03 10:25:07 -0700214 spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId,
215 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy,
216 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
217 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands,
218 glslang::TBasicType typeProxy);
219 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
220 spv::Id typeId, std::vector<spv::Id>& operands);
221 spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands,
222 glslang::TBasicType typeProxy);
223 spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId,
224 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +0800225 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600226 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
Chao Chen3c366992018-09-19 11:41:59 -0700227 void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier);
qining08408382016-03-21 09:51:37 -0400228 spv::Id createSpvConstant(const glslang::TIntermTyped&);
John Kessenich8985fc92020-03-03 10:25:07 -0700229 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&,
230 int& nextConst, bool specConstant);
John Kessenich7c1aa102015-10-15 13:29:11 -0600231 bool isTrivialLeaf(const glslang::TIntermTyped* node);
232 bool isTrivial(const glslang::TIntermTyped* node);
233 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
Rex Xu9d93a232016-05-05 12:30:44 +0800234 spv::Id getExtBuiltins(const char* name);
Daniel Kochdb32b242020-03-17 20:42:47 -0400235 std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&);
John Kessenich9c14f772019-06-17 08:38:35 -0600236 spv::Id translateForcedType(spv::Id object);
Jeff Bolz53134492019-06-25 13:31:10 -0500237 spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents);
John Kessenich140f3df2015-06-26 16:58:36 -0600238
John Kessenich121853f2017-05-31 17:11:16 -0600239 glslang::SpvOptions& options;
John Kessenich140f3df2015-06-26 16:58:36 -0600240 spv::Function* shaderEntry;
John Kesseniched33e052016-10-06 12:59:51 -0600241 spv::Function* currentFunction;
John Kessenich55e7d112015-11-15 21:33:39 -0700242 spv::Instruction* entryPoint;
John Kessenich140f3df2015-06-26 16:58:36 -0600243 int sequenceDepth;
244
Lei Zhang17535f72016-05-04 15:55:59 -0400245 spv::SpvBuildLogger* logger;
Lei Zhang09caf122016-05-02 18:11:54 -0400246
John Kessenich140f3df2015-06-26 16:58:36 -0600247 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
248 spv::Builder builder;
John Kessenich517fe7a2016-11-26 13:31:47 -0700249 bool inEntryPoint;
250 bool entryPointTerminated;
John Kessenich8985fc92020-03-03 10:25:07 -0700251 bool linkageOnly; // true when visiting the set of objects in the AST present only for
252 // establishing interface, whether or not they were statically used
John Kessenich59420fd2015-12-21 11:45:34 -0700253 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
John Kessenich140f3df2015-06-26 16:58:36 -0600254 const glslang::TIntermediate* glslangIntermediate;
John Kessenich605afc72019-06-17 23:33:09 -0600255 bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp
John Kessenich140f3df2015-06-26 16:58:36 -0600256 spv::Id stdBuiltins;
Jeff Bolz04d73732019-05-31 13:06:01 -0500257 spv::Id nonSemanticDebugPrintf;
Rex Xu77fe62f2021-02-26 17:25:45 +0800258 std::unordered_map<std::string, spv::Id> extBuiltinMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600259
Chow93b400f2020-11-12 15:54:16 +0800260 std::unordered_map<long long, spv::Id> symbolValues;
261 std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues,
John Kessenich8985fc92020-03-03 10:25:07 -0700262 // rather than a pointer
John Kessenich2f273362015-07-18 22:34:27 -0600263 std::unordered_map<std::string, spv::Function*> functionMap;
John Kessenich3ac051e2015-12-20 11:29:16 -0700264 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
John Kessenich5d610ee2018-03-07 18:05:55 -0700265 // for mapping glslang block indices to spv indices (e.g., due to hidden members):
Chow93b400f2020-11-12 15:54:16 +0800266 std::unordered_map<long long, std::vector<int>> memberRemapper;
Roy05a5b532020-01-03 16:21:34 +0800267 // for mapping glslang symbol struct to symbol Id
Chow93b400f2020-11-12 15:54:16 +0800268 std::unordered_map<const glslang::TTypeList*, long long> glslangTypeToIdMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600269 std::stack<bool> breakForLoop; // false means break for switch
John Kessenich5d610ee2018-03-07 18:05:55 -0700270 std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator;
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600271 // Map pointee types for EbtReference to their forward pointers
272 std::map<const glslang::TType *, spv::Id> forwardPointers;
John Kessenich9c14f772019-06-17 08:38:35 -0600273 // Type forcing, for when SPIR-V wants a different type than the AST,
274 // requiring local translation to and from SPIR-V type on every access.
275 // Maps <builtin-variable-id -> AST-required-type-id>
276 std::unordered_map<spv::Id, spv::Id> forceType;
Daniel Kochffccefd2020-11-23 15:41:27 -0500277
278 // Used later for generating OpTraceKHR/OpExecuteCallableKHR
279 std::unordered_map<unsigned int, glslang::TIntermSymbol *> locationToSymbol[2];
John Kessenich140f3df2015-06-26 16:58:36 -0600280};
281
282//
283// Helper functions for translating glslang representations to SPIR-V enumerants.
284//
285
286// Translate glslang profile to SPIR-V source language.
John Kessenich66e2faf2016-03-12 18:34:36 -0700287spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
John Kessenich140f3df2015-06-26 16:58:36 -0600288{
John Kessenich155d3512019-08-08 23:29:20 -0600289#ifdef GLSLANG_WEB
290 return spv::SourceLanguageESSL;
Shahbaz Youssefi1ef2e252020-07-03 15:42:53 -0400291#elif defined(GLSLANG_ANGLE)
292 return spv::SourceLanguageGLSL;
John Kessenich155d3512019-08-08 23:29:20 -0600293#endif
294
John Kessenich66e2faf2016-03-12 18:34:36 -0700295 switch (source) {
296 case glslang::EShSourceGlsl:
297 switch (profile) {
298 case ENoProfile:
299 case ECoreProfile:
300 case ECompatibilityProfile:
301 return spv::SourceLanguageGLSL;
302 case EEsProfile:
303 return spv::SourceLanguageESSL;
304 default:
305 return spv::SourceLanguageUnknown;
306 }
307 case glslang::EShSourceHlsl:
John Kessenich6fa17642017-04-07 15:33:08 -0600308 return spv::SourceLanguageHLSL;
John Kessenich140f3df2015-06-26 16:58:36 -0600309 default:
310 return spv::SourceLanguageUnknown;
311 }
312}
313
314// Translate glslang language (stage) to SPIR-V execution model.
315spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
316{
317 switch (stage) {
318 case EShLangVertex: return spv::ExecutionModelVertex;
John Kessenicha28f7a72019-08-06 07:00:58 -0600319 case EShLangFragment: return spv::ExecutionModelFragment;
John Kessenicha28f7a72019-08-06 07:00:58 -0600320 case EShLangCompute: return spv::ExecutionModelGLCompute;
John Kessenich51ed01c2019-10-10 11:40:11 -0600321#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -0600322 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
323 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
324 case EShLangGeometry: return spv::ExecutionModelGeometry;
Daniel Kochdb32b242020-03-17 20:42:47 -0400325 case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR;
326 case EShLangIntersect: return spv::ExecutionModelIntersectionKHR;
327 case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR;
328 case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR;
329 case EShLangMiss: return spv::ExecutionModelMissKHR;
330 case EShLangCallable: return spv::ExecutionModelCallableKHR;
Chao Chen3c366992018-09-19 11:41:59 -0700331 case EShLangTaskNV: return spv::ExecutionModelTaskNV;
332 case EShLangMeshNV: return spv::ExecutionModelMeshNV;
333#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600334 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700335 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600336 return spv::ExecutionModelFragment;
337 }
338}
339
John Kessenich140f3df2015-06-26 16:58:36 -0600340// Translate glslang sampler type to SPIR-V dimensionality.
341spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
342{
343 switch (sampler.dim) {
John Kessenich55e7d112015-11-15 21:33:39 -0700344 case glslang::Esd1D: return spv::Dim1D;
345 case glslang::Esd2D: return spv::Dim2D;
346 case glslang::Esd3D: return spv::Dim3D;
347 case glslang::EsdCube: return spv::DimCube;
348 case glslang::EsdRect: return spv::DimRect;
349 case glslang::EsdBuffer: return spv::DimBuffer;
John Kessenich6c292d32016-02-15 20:58:50 -0700350 case glslang::EsdSubpass: return spv::DimSubpassData;
John Kessenich140f3df2015-06-26 16:58:36 -0600351 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700352 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600353 return spv::Dim2D;
354 }
355}
356
John Kessenichf6640762016-08-01 19:44:00 -0600357// Translate glslang precision to SPIR-V precision decorations.
358spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
John Kessenich140f3df2015-06-26 16:58:36 -0600359{
John Kessenichf6640762016-08-01 19:44:00 -0600360 switch (glslangPrecision) {
John Kessenich61c47a92015-12-14 18:21:19 -0700361 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
John Kessenich5e4b1242015-08-06 22:53:06 -0600362 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
John Kessenich140f3df2015-06-26 16:58:36 -0600363 default:
364 return spv::NoPrecision;
365 }
366}
367
John Kessenichf6640762016-08-01 19:44:00 -0600368// Translate glslang type to SPIR-V precision decorations.
369spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
370{
371 return TranslatePrecisionDecoration(type.getQualifier().precision);
372}
373
John Kessenich140f3df2015-06-26 16:58:36 -0600374// Translate glslang type to SPIR-V block decorations.
John Kessenich67027182017-04-19 18:34:49 -0600375spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer)
John Kessenich140f3df2015-06-26 16:58:36 -0600376{
377 if (type.getBasicType() == glslang::EbtBlock) {
378 switch (type.getQualifier().storage) {
379 case glslang::EvqUniform: return spv::DecorationBlock;
John Kessenich67027182017-04-19 18:34:49 -0600380 case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock;
John Kessenich140f3df2015-06-26 16:58:36 -0600381 case glslang::EvqVaryingIn: return spv::DecorationBlock;
382 case glslang::EvqVaryingOut: return spv::DecorationBlock;
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -0700383 case glslang::EvqShared: return spv::DecorationBlock;
John Kessenicha28f7a72019-08-06 07:00:58 -0600384#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -0400385 case glslang::EvqPayload: return spv::DecorationBlock;
386 case glslang::EvqPayloadIn: return spv::DecorationBlock;
387 case glslang::EvqHitAttr: return spv::DecorationBlock;
388 case glslang::EvqCallableData: return spv::DecorationBlock;
389 case glslang::EvqCallableDataIn: return spv::DecorationBlock;
Chao Chenb50c02e2018-09-19 11:42:24 -0700390#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600391 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700392 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600393 break;
394 }
395 }
396
John Kessenich4016e382016-07-15 11:53:56 -0600397 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600398}
399
Rex Xu1da878f2016-02-21 20:59:01 +0800400// Translate glslang type to SPIR-V memory decorations.
John Kessenich8985fc92020-03-03 10:25:07 -0700401void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory,
402 bool useVulkanMemoryModel)
Rex Xu1da878f2016-02-21 20:59:01 +0800403{
Jeff Bolz36831c92018-09-05 10:11:41 -0500404 if (!useVulkanMemoryModel) {
John Kessenichf8d1d742019-10-21 06:55:11 -0600405 if (qualifier.isCoherent())
Jeff Bolz36831c92018-09-05 10:11:41 -0500406 memory.push_back(spv::DecorationCoherent);
John Kessenichf8d1d742019-10-21 06:55:11 -0600407 if (qualifier.isVolatile()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500408 memory.push_back(spv::DecorationVolatile);
409 memory.push_back(spv::DecorationCoherent);
410 }
John Kessenich14b85d32018-06-04 15:36:03 -0600411 }
John Kessenichf8d1d742019-10-21 06:55:11 -0600412 if (qualifier.isRestrict())
Rex Xu1da878f2016-02-21 20:59:01 +0800413 memory.push_back(spv::DecorationRestrict);
John Kessenichdeec1932019-08-13 08:00:30 -0600414 if (qualifier.isReadOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800415 memory.push_back(spv::DecorationNonWritable);
John Kessenichdeec1932019-08-13 08:00:30 -0600416 if (qualifier.isWriteOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800417 memory.push_back(spv::DecorationNonReadable);
418}
419
John Kessenich140f3df2015-06-26 16:58:36 -0600420// Translate glslang type to SPIR-V layout decorations.
John Kessenich3ac051e2015-12-20 11:29:16 -0700421spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
John Kessenich140f3df2015-06-26 16:58:36 -0600422{
423 if (type.isMatrix()) {
John Kessenich3ac051e2015-12-20 11:29:16 -0700424 switch (matrixLayout) {
John Kessenich140f3df2015-06-26 16:58:36 -0600425 case glslang::ElmRowMajor:
426 return spv::DecorationRowMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700427 case glslang::ElmColumnMajor:
John Kessenich140f3df2015-06-26 16:58:36 -0600428 return spv::DecorationColMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700429 default:
430 // opaque layouts don't need a majorness
John Kessenich4016e382016-07-15 11:53:56 -0600431 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600432 }
433 } else {
434 switch (type.getBasicType()) {
435 default:
John Kessenich4016e382016-07-15 11:53:56 -0600436 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600437 break;
438 case glslang::EbtBlock:
439 switch (type.getQualifier().storage) {
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -0700440 case glslang::EvqShared:
John Kessenich140f3df2015-06-26 16:58:36 -0600441 case glslang::EvqUniform:
442 case glslang::EvqBuffer:
443 switch (type.getQualifier().layoutPacking) {
444 case glslang::ElpShared: return spv::DecorationGLSLShared;
John Kessenich140f3df2015-06-26 16:58:36 -0600445 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
446 default:
John Kessenich4016e382016-07-15 11:53:56 -0600447 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600448 }
449 case glslang::EvqVaryingIn:
450 case glslang::EvqVaryingOut:
Chao Chen3c366992018-09-19 11:41:59 -0700451 if (type.getQualifier().isTaskMemory()) {
452 switch (type.getQualifier().layoutPacking) {
453 case glslang::ElpShared: return spv::DecorationGLSLShared;
454 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
455 default: break;
456 }
457 } else {
458 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
459 }
John Kessenich4016e382016-07-15 11:53:56 -0600460 return spv::DecorationMax;
John Kessenicha28f7a72019-08-06 07:00:58 -0600461#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -0400462 case glslang::EvqPayload:
463 case glslang::EvqPayloadIn:
464 case glslang::EvqHitAttr:
465 case glslang::EvqCallableData:
466 case glslang::EvqCallableDataIn:
Chao Chenb50c02e2018-09-19 11:42:24 -0700467 return spv::DecorationMax;
468#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600469 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700470 assert(0);
John Kessenich4016e382016-07-15 11:53:56 -0600471 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600472 }
473 }
474 }
475}
476
477// Translate glslang type to SPIR-V interpolation decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600478// Returns spv::DecorationMax when no decoration
John Kessenich55e7d112015-11-15 21:33:39 -0700479// should be applied.
Rex Xu17ff3432016-10-14 17:41:45 +0800480spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600481{
Rex Xubbceed72016-05-21 09:40:44 +0800482 if (qualifier.smooth)
John Kessenich55e7d112015-11-15 21:33:39 -0700483 // Smooth decoration doesn't exist in SPIR-V 1.0
John Kessenich4016e382016-07-15 11:53:56 -0600484 return spv::DecorationMax;
John Kessenich7015bd62019-08-01 03:28:08 -0600485 else if (qualifier.isNonPerspective())
John Kessenich55e7d112015-11-15 21:33:39 -0700486 return spv::DecorationNoPerspective;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700487 else if (qualifier.flat)
John Kessenich140f3df2015-06-26 16:58:36 -0600488 return spv::DecorationFlat;
John Kessenicha28f7a72019-08-06 07:00:58 -0600489 else if (qualifier.isExplicitInterpolation()) {
Rex Xu17ff3432016-10-14 17:41:45 +0800490 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
Rex Xu9d93a232016-05-05 12:30:44 +0800491 return spv::DecorationExplicitInterpAMD;
Rex Xu17ff3432016-10-14 17:41:45 +0800492 }
Rex Xubbceed72016-05-21 09:40:44 +0800493 else
John Kessenich4016e382016-07-15 11:53:56 -0600494 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800495}
496
497// Translate glslang type to SPIR-V auxiliary storage decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600498// Returns spv::DecorationMax when no decoration
Rex Xubbceed72016-05-21 09:40:44 +0800499// should be applied.
500spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
501{
John Kessenichb9197c82019-08-11 07:41:45 -0600502 if (qualifier.centroid)
John Kessenich140f3df2015-06-26 16:58:36 -0600503 return spv::DecorationCentroid;
John Kessenichb9197c82019-08-11 07:41:45 -0600504#ifndef GLSLANG_WEB
505 else if (qualifier.patch)
506 return spv::DecorationPatch;
John Kessenich5e801132016-02-15 11:09:46 -0700507 else if (qualifier.sample) {
508 builder.addCapability(spv::CapabilitySampleRateShading);
John Kessenich140f3df2015-06-26 16:58:36 -0600509 return spv::DecorationSample;
John Kessenichb9197c82019-08-11 07:41:45 -0600510 }
511#endif
512
513 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600514}
515
John Kessenich92187592016-02-01 13:45:25 -0700516// If glslang type is invariant, return SPIR-V invariant decoration.
John Kesseniche0b6cad2015-12-24 10:30:13 -0700517spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600518{
John Kesseniche0b6cad2015-12-24 10:30:13 -0700519 if (qualifier.invariant)
John Kessenich140f3df2015-06-26 16:58:36 -0600520 return spv::DecorationInvariant;
521 else
John Kessenich4016e382016-07-15 11:53:56 -0600522 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600523}
524
qining9220dbb2016-05-04 17:34:38 -0400525// If glslang type is noContraction, return SPIR-V NoContraction decoration.
526spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
527{
John Kessenichb9197c82019-08-11 07:41:45 -0600528#ifndef GLSLANG_WEB
John Kessenicha28f7a72019-08-06 07:00:58 -0600529 if (qualifier.isNoContraction())
qining9220dbb2016-05-04 17:34:38 -0400530 return spv::DecorationNoContraction;
531 else
John Kessenichb9197c82019-08-11 07:41:45 -0600532#endif
John Kessenich4016e382016-07-15 11:53:56 -0600533 return spv::DecorationMax;
qining9220dbb2016-05-04 17:34:38 -0400534}
535
John Kessenich5611c6d2018-04-05 11:25:02 -0600536// If glslang type is nonUniform, return SPIR-V NonUniform decoration.
537spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
538{
John Kessenichb9197c82019-08-11 07:41:45 -0600539#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -0600540 if (qualifier.isNonUniform()) {
John Kessenich8317e6c2019-08-18 23:58:08 -0600541 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -0600542 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
543 return spv::DecorationNonUniformEXT;
544 } else
John Kessenichb9197c82019-08-11 07:41:45 -0600545#endif
John Kessenich5611c6d2018-04-05 11:25:02 -0600546 return spv::DecorationMax;
547}
548
greg-lunarg639f5462020-11-12 11:10:07 -0700549// If lvalue flags contains nonUniform, return SPIR-V NonUniform decoration.
550spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(
551 const spv::Builder::AccessChain::CoherentFlags& coherentFlags)
552{
553#ifndef GLSLANG_WEB
554 if (coherentFlags.isNonUniform()) {
555 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
556 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
557 return spv::DecorationNonUniformEXT;
558 } else
559#endif
560 return spv::DecorationMax;
561}
562
John Kessenichb9197c82019-08-11 07:41:45 -0600563spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(
564 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500565{
Jeff Bolz36831c92018-09-05 10:11:41 -0500566 spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600567
568#ifndef GLSLANG_WEB
569 if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage)
570 return mask;
571
Daniel Kochdb32b242020-03-17 20:42:47 -0400572 if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500573 mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask |
574 spv::MemoryAccessMakePointerVisibleKHRMask;
575 }
Daniel Kochdb32b242020-03-17 20:42:47 -0400576
Jeff Bolz36831c92018-09-05 10:11:41 -0500577 if (coherentFlags.nonprivate) {
578 mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask;
579 }
580 if (coherentFlags.volatil) {
581 mask = mask | spv::MemoryAccessVolatileMask;
582 }
583 if (mask != spv::MemoryAccessMaskNone) {
584 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
585 }
John Kessenichb9197c82019-08-11 07:41:45 -0600586#endif
587
Jeff Bolz36831c92018-09-05 10:11:41 -0500588 return mask;
589}
590
John Kessenichb9197c82019-08-11 07:41:45 -0600591spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(
592 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500593{
Jeff Bolz36831c92018-09-05 10:11:41 -0500594 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600595
596#ifndef GLSLANG_WEB
597 if (!glslangIntermediate->usingVulkanMemoryModel())
598 return mask;
599
Jeff Bolz36831c92018-09-05 10:11:41 -0500600 if (coherentFlags.volatil ||
Daniel Kochdb32b242020-03-17 20:42:47 -0400601 coherentFlags.anyCoherent()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500602 mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask |
603 spv::ImageOperandsMakeTexelVisibleKHRMask;
604 }
605 if (coherentFlags.nonprivate) {
606 mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask;
607 }
608 if (coherentFlags.volatil) {
609 mask = mask | spv::ImageOperandsVolatileTexelKHRMask;
610 }
611 if (mask != spv::ImageOperandsMaskNone) {
612 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
613 }
John Kessenichb9197c82019-08-11 07:41:45 -0600614#endif
615
Jeff Bolz36831c92018-09-05 10:11:41 -0500616 return mask;
617}
618
619spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
620{
John Kessenichb9197c82019-08-11 07:41:45 -0600621 spv::Builder::AccessChain::CoherentFlags flags = {};
622#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -0500623 flags.coherent = type.getQualifier().coherent;
624 flags.devicecoherent = type.getQualifier().devicecoherent;
625 flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent;
626 // shared variables are implicitly workgroupcoherent in GLSL.
627 flags.workgroupcoherent = type.getQualifier().workgroupcoherent ||
628 type.getQualifier().storage == glslang::EvqShared;
629 flags.subgroupcoherent = type.getQualifier().subgroupcoherent;
Daniel Kochdb32b242020-03-17 20:42:47 -0400630 flags.shadercallcoherent = type.getQualifier().shadercallcoherent;
Jeff Bolz38cbad12019-03-05 14:40:07 -0600631 flags.volatil = type.getQualifier().volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500632 // *coherent variables are implicitly nonprivate in GLSL
633 flags.nonprivate = type.getQualifier().nonprivate ||
Daniel Kochdb32b242020-03-17 20:42:47 -0400634 flags.anyCoherent() ||
Jeff Bolz38cbad12019-03-05 14:40:07 -0600635 flags.volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500636 flags.isImage = type.getBasicType() == glslang::EbtSampler;
John Kessenichb9197c82019-08-11 07:41:45 -0600637#endif
greg-lunarg639f5462020-11-12 11:10:07 -0700638 flags.nonUniform = type.getQualifier().nonUniform;
Jeff Bolz36831c92018-09-05 10:11:41 -0500639 return flags;
640}
641
John Kessenichb9197c82019-08-11 07:41:45 -0600642spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(
643 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500644{
John Kessenichb9197c82019-08-11 07:41:45 -0600645 spv::Scope scope = spv::ScopeMax;
646
647#ifndef GLSLANG_WEB
Jeff Bolz38cbad12019-03-05 14:40:07 -0600648 if (coherentFlags.volatil || coherentFlags.coherent) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500649 // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model
650 scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
651 } else if (coherentFlags.devicecoherent) {
652 scope = spv::ScopeDevice;
653 } else if (coherentFlags.queuefamilycoherent) {
654 scope = spv::ScopeQueueFamilyKHR;
655 } else if (coherentFlags.workgroupcoherent) {
656 scope = spv::ScopeWorkgroup;
657 } else if (coherentFlags.subgroupcoherent) {
658 scope = spv::ScopeSubgroup;
Daniel Kochdb32b242020-03-17 20:42:47 -0400659 } else if (coherentFlags.shadercallcoherent) {
660 scope = spv::ScopeShaderCallKHR;
Jeff Bolz36831c92018-09-05 10:11:41 -0500661 }
662 if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) {
663 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
664 }
John Kessenichb9197c82019-08-11 07:41:45 -0600665#endif
666
Jeff Bolz36831c92018-09-05 10:11:41 -0500667 return scope;
668}
669
David Netoa901ffe2016-06-08 14:11:40 +0100670// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
671// associated capabilities when required. For some built-in variables, a capability
672// is generated only when using the variable in an executable instruction, but not when
673// just declaring a struct member variable with it. This is true for PointSize,
674// ClipDistance, and CullDistance.
John Kessenich8985fc92020-03-03 10:25:07 -0700675spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn,
676 bool memberDeclaration)
John Kessenich140f3df2015-06-26 16:58:36 -0600677{
678 switch (builtIn) {
John Kessenich92187592016-02-01 13:45:25 -0700679 case glslang::EbvPointSize:
John Kessenich155d3512019-08-08 23:29:20 -0600680#ifndef GLSLANG_WEB
John Kessenich78a45572016-07-08 14:05:15 -0600681 // Defer adding the capability until the built-in is actually used.
682 if (! memberDeclaration) {
683 switch (glslangIntermediate->getStage()) {
684 case EShLangGeometry:
685 builder.addCapability(spv::CapabilityGeometryPointSize);
686 break;
687 case EShLangTessControl:
688 case EShLangTessEvaluation:
689 builder.addCapability(spv::CapabilityTessellationPointSize);
690 break;
691 default:
692 break;
693 }
John Kessenich92187592016-02-01 13:45:25 -0700694 }
John Kessenich155d3512019-08-08 23:29:20 -0600695#endif
John Kessenich92187592016-02-01 13:45:25 -0700696 return spv::BuiltInPointSize;
697
John Kessenicha28f7a72019-08-06 07:00:58 -0600698 case glslang::EbvPosition: return spv::BuiltInPosition;
699 case glslang::EbvVertexId: return spv::BuiltInVertexId;
700 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
701 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
702 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
703
704 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
705 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
706 case glslang::EbvFace: return spv::BuiltInFrontFacing;
707 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
708
John Kessenich3dd1ce52019-10-17 07:08:40 -0600709 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
710 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
711 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
712 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
713 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
714 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
715
John Kessenicha28f7a72019-08-06 07:00:58 -0600716#ifndef GLSLANG_WEB
John Kessenichebb50532016-05-16 19:22:05 -0600717 // These *Distance capabilities logically belong here, but if the member is declared and
718 // then never used, consumers of SPIR-V prefer the capability not be declared.
719 // They are now generated when used, rather than here when declared.
720 // Potentially, the specification should be more clear what the minimum
721 // use needed is to trigger the capability.
722 //
John Kessenich92187592016-02-01 13:45:25 -0700723 case glslang::EbvClipDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100724 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800725 builder.addCapability(spv::CapabilityClipDistance);
John Kessenich92187592016-02-01 13:45:25 -0700726 return spv::BuiltInClipDistance;
727
728 case glslang::EbvCullDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100729 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800730 builder.addCapability(spv::CapabilityCullDistance);
John Kessenich92187592016-02-01 13:45:25 -0700731 return spv::BuiltInCullDistance;
732
733 case glslang::EbvViewportIndex:
David Netofb53f832020-11-12 15:00:16 -0500734 if (glslangIntermediate->getStage() == EShLangGeometry ||
735 glslangIntermediate->getStage() == EShLangFragment) {
736 builder.addCapability(spv::CapabilityMultiViewport);
737 }
John Kessenichba6a3c22017-09-13 13:22:50 -0600738 if (glslangIntermediate->getStage() == EShLangVertex ||
739 glslangIntermediate->getStage() == EShLangTessControl ||
740 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800741
Sidney Just56350ca2020-11-02 11:27:40 -0800742 if (builder.getSpvVersion() < spv::Spv_1_5) {
743 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
744 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
745 }
746 else
747 builder.addCapability(spv::CapabilityShaderViewportIndex);
Rex Xu5e317ff2017-03-16 23:02:39 +0800748 }
John Kessenich92187592016-02-01 13:45:25 -0700749 return spv::BuiltInViewportIndex;
750
John Kessenich5e801132016-02-15 11:09:46 -0700751 case glslang::EbvSampleId:
752 builder.addCapability(spv::CapabilitySampleRateShading);
753 return spv::BuiltInSampleId;
754
755 case glslang::EbvSamplePosition:
756 builder.addCapability(spv::CapabilitySampleRateShading);
757 return spv::BuiltInSamplePosition;
758
759 case glslang::EbvSampleMask:
John Kessenich5e801132016-02-15 11:09:46 -0700760 return spv::BuiltInSampleMask;
761
John Kessenich78a45572016-07-08 14:05:15 -0600762 case glslang::EbvLayer:
Chao Chen3c366992018-09-19 11:41:59 -0700763 if (glslangIntermediate->getStage() == EShLangMeshNV) {
764 return spv::BuiltInLayer;
765 }
David Netofb53f832020-11-12 15:00:16 -0500766 if (glslangIntermediate->getStage() == EShLangGeometry ||
767 glslangIntermediate->getStage() == EShLangFragment) {
768 builder.addCapability(spv::CapabilityGeometry);
769 }
John Kessenichba6a3c22017-09-13 13:22:50 -0600770 if (glslangIntermediate->getStage() == EShLangVertex ||
771 glslangIntermediate->getStage() == EShLangTessControl ||
772 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800773
Sidney Just56350ca2020-11-02 11:27:40 -0800774 if (builder.getSpvVersion() < spv::Spv_1_5) {
775 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
776 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
777 } else
778 builder.addCapability(spv::CapabilityShaderLayer);
Rex Xu5e317ff2017-03-16 23:02:39 +0800779 }
John Kessenich78a45572016-07-08 14:05:15 -0600780 return spv::BuiltInLayer;
781
John Kessenichda581a22015-10-14 14:10:30 -0600782 case glslang::EbvBaseVertex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600783 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800784 builder.addCapability(spv::CapabilityDrawParameters);
785 return spv::BuiltInBaseVertex;
786
John Kessenichda581a22015-10-14 14:10:30 -0600787 case glslang::EbvBaseInstance:
John Kessenich8317e6c2019-08-18 23:58:08 -0600788 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800789 builder.addCapability(spv::CapabilityDrawParameters);
790 return spv::BuiltInBaseInstance;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200791
John Kessenichda581a22015-10-14 14:10:30 -0600792 case glslang::EbvDrawId:
John Kessenich8317e6c2019-08-18 23:58:08 -0600793 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800794 builder.addCapability(spv::CapabilityDrawParameters);
795 return spv::BuiltInDrawIndex;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200796
797 case glslang::EbvPrimitiveId:
798 if (glslangIntermediate->getStage() == EShLangFragment)
799 builder.addCapability(spv::CapabilityGeometry);
800 return spv::BuiltInPrimitiveId;
801
Rex Xu37cdcee2017-06-29 17:46:34 +0800802 case glslang::EbvFragStencilRef:
Rex Xue8fdd792017-08-23 23:24:42 +0800803 builder.addExtension(spv::E_SPV_EXT_shader_stencil_export);
804 builder.addCapability(spv::CapabilityStencilExportEXT);
805 return spv::BuiltInFragStencilRefEXT;
Rex Xu37cdcee2017-06-29 17:46:34 +0800806
Chowa315b562020-07-02 15:50:36 +0800807 case glslang::EbvShadingRateKHR:
808 builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate);
809 builder.addCapability(spv::CapabilityFragmentShadingRateKHR);
810 return spv::BuiltInShadingRateKHR;
811
812 case glslang::EbvPrimitiveShadingRateKHR:
813 builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate);
814 builder.addCapability(spv::CapabilityFragmentShadingRateKHR);
815 return spv::BuiltInPrimitiveShadingRateKHR;
816
John Kessenich140f3df2015-06-26 16:58:36 -0600817 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
John Kessenich140f3df2015-06-26 16:58:36 -0600818 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
819 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
820 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
821 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
John Kessenich140f3df2015-06-26 16:58:36 -0600822 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
Rex Xu51596642016-09-21 18:56:12 +0800823
Rex Xu574ab042016-04-14 16:53:07 +0800824 case glslang::EbvSubGroupSize:
Rex Xu36876e62016-09-23 22:13:43 +0800825 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800826 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
827 return spv::BuiltInSubgroupSize;
828
Rex Xu574ab042016-04-14 16:53:07 +0800829 case glslang::EbvSubGroupInvocation:
Rex Xu36876e62016-09-23 22:13:43 +0800830 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800831 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
832 return spv::BuiltInSubgroupLocalInvocationId;
833
Rex Xu574ab042016-04-14 16:53:07 +0800834 case glslang::EbvSubGroupEqMask:
Rex Xu51596642016-09-21 18:56:12 +0800835 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
836 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600837 return spv::BuiltInSubgroupEqMask;
Rex Xu51596642016-09-21 18:56:12 +0800838
Rex Xu574ab042016-04-14 16:53:07 +0800839 case glslang::EbvSubGroupGeMask:
Rex Xu51596642016-09-21 18:56:12 +0800840 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
841 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600842 return spv::BuiltInSubgroupGeMask;
Rex Xu51596642016-09-21 18:56:12 +0800843
Rex Xu574ab042016-04-14 16:53:07 +0800844 case glslang::EbvSubGroupGtMask:
Rex Xu51596642016-09-21 18:56:12 +0800845 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
846 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600847 return spv::BuiltInSubgroupGtMask;
Rex Xu51596642016-09-21 18:56:12 +0800848
Rex Xu574ab042016-04-14 16:53:07 +0800849 case glslang::EbvSubGroupLeMask:
Rex Xu51596642016-09-21 18:56:12 +0800850 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
851 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600852 return spv::BuiltInSubgroupLeMask;
Rex Xu51596642016-09-21 18:56:12 +0800853
Rex Xu574ab042016-04-14 16:53:07 +0800854 case glslang::EbvSubGroupLtMask:
Rex Xu51596642016-09-21 18:56:12 +0800855 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
856 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600857 return spv::BuiltInSubgroupLtMask;
Rex Xu51596642016-09-21 18:56:12 +0800858
John Kessenich66011cb2018-03-06 16:12:04 -0700859 case glslang::EbvNumSubgroups:
860 builder.addCapability(spv::CapabilityGroupNonUniform);
861 return spv::BuiltInNumSubgroups;
862
863 case glslang::EbvSubgroupID:
864 builder.addCapability(spv::CapabilityGroupNonUniform);
865 return spv::BuiltInSubgroupId;
866
867 case glslang::EbvSubgroupSize2:
868 builder.addCapability(spv::CapabilityGroupNonUniform);
869 return spv::BuiltInSubgroupSize;
870
871 case glslang::EbvSubgroupInvocation2:
872 builder.addCapability(spv::CapabilityGroupNonUniform);
873 return spv::BuiltInSubgroupLocalInvocationId;
874
875 case glslang::EbvSubgroupEqMask2:
876 builder.addCapability(spv::CapabilityGroupNonUniform);
877 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
878 return spv::BuiltInSubgroupEqMask;
879
880 case glslang::EbvSubgroupGeMask2:
881 builder.addCapability(spv::CapabilityGroupNonUniform);
882 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
883 return spv::BuiltInSubgroupGeMask;
884
885 case glslang::EbvSubgroupGtMask2:
886 builder.addCapability(spv::CapabilityGroupNonUniform);
887 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
888 return spv::BuiltInSubgroupGtMask;
889
890 case glslang::EbvSubgroupLeMask2:
891 builder.addCapability(spv::CapabilityGroupNonUniform);
892 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
893 return spv::BuiltInSubgroupLeMask;
894
895 case glslang::EbvSubgroupLtMask2:
896 builder.addCapability(spv::CapabilityGroupNonUniform);
897 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
898 return spv::BuiltInSubgroupLtMask;
John Kessenich9c14f772019-06-17 08:38:35 -0600899
Rex Xu17ff3432016-10-14 17:41:45 +0800900 case glslang::EbvBaryCoordNoPersp:
901 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
902 return spv::BuiltInBaryCoordNoPerspAMD;
903
904 case glslang::EbvBaryCoordNoPerspCentroid:
905 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
906 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
907
908 case glslang::EbvBaryCoordNoPerspSample:
909 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
910 return spv::BuiltInBaryCoordNoPerspSampleAMD;
911
912 case glslang::EbvBaryCoordSmooth:
913 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
914 return spv::BuiltInBaryCoordSmoothAMD;
915
916 case glslang::EbvBaryCoordSmoothCentroid:
917 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
918 return spv::BuiltInBaryCoordSmoothCentroidAMD;
919
920 case glslang::EbvBaryCoordSmoothSample:
921 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
922 return spv::BuiltInBaryCoordSmoothSampleAMD;
923
924 case glslang::EbvBaryCoordPullModel:
925 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
926 return spv::BuiltInBaryCoordPullModelAMD;
chaoc771d89f2017-01-13 01:10:53 -0800927
John Kessenich6c8aaac2017-02-27 01:20:51 -0700928 case glslang::EbvDeviceIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600929 builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700930 builder.addCapability(spv::CapabilityDeviceGroup);
John Kessenich42e33c92017-02-27 01:50:28 -0700931 return spv::BuiltInDeviceIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700932
933 case glslang::EbvViewIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600934 builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700935 builder.addCapability(spv::CapabilityMultiView);
John Kessenich42e33c92017-02-27 01:50:28 -0700936 return spv::BuiltInViewIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700937
Daniel Koch5154db52018-11-26 10:01:58 -0500938 case glslang::EbvFragSizeEXT:
939 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
940 builder.addCapability(spv::CapabilityFragmentDensityEXT);
941 return spv::BuiltInFragSizeEXT;
942
943 case glslang::EbvFragInvocationCountEXT:
944 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
945 builder.addCapability(spv::CapabilityFragmentDensityEXT);
946 return spv::BuiltInFragInvocationCountEXT;
947
chaoc771d89f2017-01-13 01:10:53 -0800948 case glslang::EbvViewportMaskNV:
Rex Xu5e317ff2017-03-16 23:02:39 +0800949 if (!memberDeclaration) {
950 builder.addExtension(spv::E_SPV_NV_viewport_array2);
951 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
952 }
chaoc771d89f2017-01-13 01:10:53 -0800953 return spv::BuiltInViewportMaskNV;
954 case glslang::EbvSecondaryPositionNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800955 if (!memberDeclaration) {
956 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
957 builder.addCapability(spv::CapabilityShaderStereoViewNV);
958 }
chaoc771d89f2017-01-13 01:10:53 -0800959 return spv::BuiltInSecondaryPositionNV;
960 case glslang::EbvSecondaryViewportMaskNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800961 if (!memberDeclaration) {
962 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
963 builder.addCapability(spv::CapabilityShaderStereoViewNV);
964 }
chaoc771d89f2017-01-13 01:10:53 -0800965 return spv::BuiltInSecondaryViewportMaskNV;
chaocdf3956c2017-02-14 14:52:34 -0800966 case glslang::EbvPositionPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800967 if (!memberDeclaration) {
968 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
969 builder.addCapability(spv::CapabilityPerViewAttributesNV);
970 }
chaocdf3956c2017-02-14 14:52:34 -0800971 return spv::BuiltInPositionPerViewNV;
972 case glslang::EbvViewportMaskPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800973 if (!memberDeclaration) {
974 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
975 builder.addCapability(spv::CapabilityPerViewAttributesNV);
976 }
chaocdf3956c2017-02-14 14:52:34 -0800977 return spv::BuiltInViewportMaskPerViewNV;
Piers Daniell1c5443c2017-12-13 13:07:22 -0700978 case glslang::EbvFragFullyCoveredNV:
979 builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered);
980 builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT);
981 return spv::BuiltInFullyCoveredEXT;
Chao Chen5b2203d2018-09-19 11:43:21 -0700982 case glslang::EbvFragmentSizeNV:
983 builder.addExtension(spv::E_SPV_NV_shading_rate);
984 builder.addCapability(spv::CapabilityShadingRateNV);
985 return spv::BuiltInFragmentSizeNV;
986 case glslang::EbvInvocationsPerPixelNV:
987 builder.addExtension(spv::E_SPV_NV_shading_rate);
988 builder.addCapability(spv::CapabilityShadingRateNV);
989 return spv::BuiltInInvocationsPerPixelNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700990
Daniel Koch593a4e02019-05-27 16:46:31 -0400991 // ray tracing
Daniel Kochdb32b242020-03-17 20:42:47 -0400992 case glslang::EbvLaunchId:
993 return spv::BuiltInLaunchIdKHR;
994 case glslang::EbvLaunchSize:
995 return spv::BuiltInLaunchSizeKHR;
996 case glslang::EbvWorldRayOrigin:
997 return spv::BuiltInWorldRayOriginKHR;
998 case glslang::EbvWorldRayDirection:
999 return spv::BuiltInWorldRayDirectionKHR;
1000 case glslang::EbvObjectRayOrigin:
1001 return spv::BuiltInObjectRayOriginKHR;
1002 case glslang::EbvObjectRayDirection:
1003 return spv::BuiltInObjectRayDirectionKHR;
1004 case glslang::EbvRayTmin:
1005 return spv::BuiltInRayTminKHR;
1006 case glslang::EbvRayTmax:
1007 return spv::BuiltInRayTmaxKHR;
1008 case glslang::EbvInstanceCustomIndex:
1009 return spv::BuiltInInstanceCustomIndexKHR;
1010 case glslang::EbvHitT:
Daniel Koche11a2c82020-11-30 11:57:34 -05001011 {
1012 // this is a GLSL alias of RayTmax
1013 // in SPV_NV_ray_tracing it has a dedicated builtin
1014 // but in SPV_KHR_ray_tracing it gets mapped to RayTmax
1015 auto& extensions = glslangIntermediate->getRequestedExtensions();
1016 if (extensions.find("GL_NV_ray_tracing") != extensions.end()) {
1017 return spv::BuiltInHitTNV;
1018 } else {
1019 return spv::BuiltInRayTmaxKHR;
1020 }
1021 }
Daniel Kochdb32b242020-03-17 20:42:47 -04001022 case glslang::EbvHitKind:
1023 return spv::BuiltInHitKindKHR;
1024 case glslang::EbvObjectToWorld:
1025 case glslang::EbvObjectToWorld3x4:
1026 return spv::BuiltInObjectToWorldKHR;
1027 case glslang::EbvWorldToObject:
1028 case glslang::EbvWorldToObject3x4:
1029 return spv::BuiltInWorldToObjectKHR;
1030 case glslang::EbvIncomingRayFlags:
1031 return spv::BuiltInIncomingRayFlagsKHR;
1032 case glslang::EbvGeometryIndex:
1033 return spv::BuiltInRayGeometryIndexKHR;
Daniel Koch593a4e02019-05-27 16:46:31 -04001034
1035 // barycentrics
Chao Chen9eada4b2018-09-19 11:39:56 -07001036 case glslang::EbvBaryCoordNV:
1037 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
1038 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
1039 return spv::BuiltInBaryCoordNV;
1040 case glslang::EbvBaryCoordNoPerspNV:
1041 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
1042 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
1043 return spv::BuiltInBaryCoordNoPerspNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001044
1045 // mesh shaders
1046 case glslang::EbvTaskCountNV:
Chao Chen3c366992018-09-19 11:41:59 -07001047 return spv::BuiltInTaskCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001048 case glslang::EbvPrimitiveCountNV:
Chao Chen3c366992018-09-19 11:41:59 -07001049 return spv::BuiltInPrimitiveCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001050 case glslang::EbvPrimitiveIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -07001051 return spv::BuiltInPrimitiveIndicesNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001052 case glslang::EbvClipDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07001053 return spv::BuiltInClipDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001054 case glslang::EbvCullDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07001055 return spv::BuiltInCullDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001056 case glslang::EbvLayerPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07001057 return spv::BuiltInLayerPerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001058 case glslang::EbvMeshViewCountNV:
Chao Chen3c366992018-09-19 11:41:59 -07001059 return spv::BuiltInMeshViewCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001060 case glslang::EbvMeshViewIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -07001061 return spv::BuiltInMeshViewIndicesNV;
Daniel Koch2cb2f192019-06-04 08:43:32 -04001062
1063 // sm builtins
1064 case glslang::EbvWarpsPerSM:
1065 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1066 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1067 return spv::BuiltInWarpsPerSMNV;
1068 case glslang::EbvSMCount:
1069 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1070 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1071 return spv::BuiltInSMCountNV;
1072 case glslang::EbvWarpID:
1073 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1074 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1075 return spv::BuiltInWarpIDNV;
1076 case glslang::EbvSMID:
1077 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1078 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1079 return spv::BuiltInSMIDNV;
John Kessenicha28f7a72019-08-06 07:00:58 -06001080#endif
1081
Rex Xu3e783f92017-02-22 16:44:48 +08001082 default:
1083 return spv::BuiltInMax;
John Kessenich140f3df2015-06-26 16:58:36 -06001084 }
1085}
1086
Rex Xufc618912015-09-09 16:42:49 +08001087// Translate glslang image layout format to SPIR-V image format.
John Kessenich5d0fa972016-02-15 11:57:00 -07001088spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
Rex Xufc618912015-09-09 16:42:49 +08001089{
1090 assert(type.getBasicType() == glslang::EbtSampler);
1091
John Kessenichb9197c82019-08-11 07:41:45 -06001092#ifdef GLSLANG_WEB
1093 return spv::ImageFormatUnknown;
1094#endif
1095
John Kessenich5d0fa972016-02-15 11:57:00 -07001096 // Check for capabilities
John Kessenich7015bd62019-08-01 03:28:08 -06001097 switch (type.getQualifier().getFormat()) {
John Kessenich5d0fa972016-02-15 11:57:00 -07001098 case glslang::ElfRg32f:
1099 case glslang::ElfRg16f:
1100 case glslang::ElfR11fG11fB10f:
1101 case glslang::ElfR16f:
1102 case glslang::ElfRgba16:
1103 case glslang::ElfRgb10A2:
1104 case glslang::ElfRg16:
1105 case glslang::ElfRg8:
1106 case glslang::ElfR16:
1107 case glslang::ElfR8:
1108 case glslang::ElfRgba16Snorm:
1109 case glslang::ElfRg16Snorm:
1110 case glslang::ElfRg8Snorm:
1111 case glslang::ElfR16Snorm:
1112 case glslang::ElfR8Snorm:
1113
1114 case glslang::ElfRg32i:
1115 case glslang::ElfRg16i:
1116 case glslang::ElfRg8i:
1117 case glslang::ElfR16i:
1118 case glslang::ElfR8i:
1119
1120 case glslang::ElfRgb10a2ui:
1121 case glslang::ElfRg32ui:
1122 case glslang::ElfRg16ui:
1123 case glslang::ElfRg8ui:
1124 case glslang::ElfR16ui:
1125 case glslang::ElfR8ui:
1126 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
1127 break;
1128
Tobski8c1a3a02020-11-04 16:24:23 +00001129 case glslang::ElfR64ui:
1130 case glslang::ElfR64i:
1131 builder.addExtension(spv::E_SPV_EXT_shader_image_int64);
1132 builder.addCapability(spv::CapabilityInt64ImageEXT);
John Kessenich5d0fa972016-02-15 11:57:00 -07001133 default:
1134 break;
1135 }
1136
1137 // do the translation
John Kessenich7015bd62019-08-01 03:28:08 -06001138 switch (type.getQualifier().getFormat()) {
Rex Xufc618912015-09-09 16:42:49 +08001139 case glslang::ElfNone: return spv::ImageFormatUnknown;
1140 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
1141 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
1142 case glslang::ElfR32f: return spv::ImageFormatR32f;
1143 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
1144 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
1145 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
1146 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
1147 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
1148 case glslang::ElfR16f: return spv::ImageFormatR16f;
1149 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
1150 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
1151 case glslang::ElfRg16: return spv::ImageFormatRg16;
1152 case glslang::ElfRg8: return spv::ImageFormatRg8;
1153 case glslang::ElfR16: return spv::ImageFormatR16;
1154 case glslang::ElfR8: return spv::ImageFormatR8;
1155 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
1156 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
1157 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
1158 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
1159 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
1160 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
1161 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
1162 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
1163 case glslang::ElfR32i: return spv::ImageFormatR32i;
1164 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
1165 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
1166 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
1167 case glslang::ElfR16i: return spv::ImageFormatR16i;
1168 case glslang::ElfR8i: return spv::ImageFormatR8i;
1169 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
1170 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
1171 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
1172 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
1173 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
1174 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
1175 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
1176 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
1177 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
1178 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
Tobski8c1a3a02020-11-04 16:24:23 +00001179 case glslang::ElfR64ui: return spv::ImageFormatR64ui;
1180 case glslang::ElfR64i: return spv::ImageFormatR64i;
John Kessenich4016e382016-07-15 11:53:56 -06001181 default: return spv::ImageFormatMax;
Rex Xufc618912015-09-09 16:42:49 +08001182 }
1183}
1184
John Kessenich8985fc92020-03-03 10:25:07 -07001185spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(
1186 const glslang::TIntermSelection& selectionNode) const
Rex Xu57e65922017-07-04 23:23:40 +08001187{
John Kesseniche18fd202018-01-30 11:01:39 -07001188 if (selectionNode.getFlatten())
1189 return spv::SelectionControlFlattenMask;
1190 if (selectionNode.getDontFlatten())
1191 return spv::SelectionControlDontFlattenMask;
1192 return spv::SelectionControlMaskNone;
Rex Xu57e65922017-07-04 23:23:40 +08001193}
1194
John Kessenich8985fc92020-03-03 10:25:07 -07001195spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode)
1196 const
steve-lunargf1709e72017-05-02 20:14:50 -06001197{
John Kesseniche18fd202018-01-30 11:01:39 -07001198 if (switchNode.getFlatten())
1199 return spv::SelectionControlFlattenMask;
1200 if (switchNode.getDontFlatten())
1201 return spv::SelectionControlDontFlattenMask;
1202 return spv::SelectionControlMaskNone;
1203}
1204
John Kessenicha2858d92018-01-31 08:11:18 -07001205// return a non-0 dependency if the dependency argument must be set
1206spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
John Kessenich1f4d0462019-01-12 17:31:41 +07001207 std::vector<unsigned int>& operands) const
John Kesseniche18fd202018-01-30 11:01:39 -07001208{
1209 spv::LoopControlMask control = spv::LoopControlMaskNone;
1210
1211 if (loopNode.getDontUnroll())
1212 control = control | spv::LoopControlDontUnrollMask;
1213 if (loopNode.getUnroll())
1214 control = control | spv::LoopControlUnrollMask;
LoopDawg4425f242018-02-18 11:40:01 -07001215 if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite)
John Kessenicha2858d92018-01-31 08:11:18 -07001216 control = control | spv::LoopControlDependencyInfiniteMask;
1217 else if (loopNode.getLoopDependency() > 0) {
1218 control = control | spv::LoopControlDependencyLengthMask;
John Kessenich1f4d0462019-01-12 17:31:41 +07001219 operands.push_back((unsigned int)loopNode.getLoopDependency());
1220 }
1221 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
1222 if (loopNode.getMinIterations() > 0) {
1223 control = control | spv::LoopControlMinIterationsMask;
1224 operands.push_back(loopNode.getMinIterations());
1225 }
1226 if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) {
1227 control = control | spv::LoopControlMaxIterationsMask;
1228 operands.push_back(loopNode.getMaxIterations());
1229 }
1230 if (loopNode.getIterationMultiple() > 1) {
1231 control = control | spv::LoopControlIterationMultipleMask;
1232 operands.push_back(loopNode.getIterationMultiple());
1233 }
1234 if (loopNode.getPeelCount() > 0) {
1235 control = control | spv::LoopControlPeelCountMask;
1236 operands.push_back(loopNode.getPeelCount());
1237 }
1238 if (loopNode.getPartialCount() > 0) {
1239 control = control | spv::LoopControlPartialCountMask;
1240 operands.push_back(loopNode.getPartialCount());
1241 }
John Kessenicha2858d92018-01-31 08:11:18 -07001242 }
John Kesseniche18fd202018-01-30 11:01:39 -07001243
1244 return control;
steve-lunargf1709e72017-05-02 20:14:50 -06001245}
1246
John Kessenicha5c5fb62017-05-05 05:09:58 -06001247// Translate glslang type to SPIR-V storage class.
1248spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
1249{
Torosdagli06c2eee2020-03-19 11:09:57 -04001250 if (type.getBasicType() == glslang::EbtRayQuery)
Daniel Kochffccefd2020-11-23 15:41:27 -05001251 return spv::StorageClassPrivate;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001252 if (type.getQualifier().isPipeInput())
1253 return spv::StorageClassInput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001254 if (type.getQualifier().isPipeOutput())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001255 return spv::StorageClassOutput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001256
1257 if (glslangIntermediate->getSource() != glslang::EShSourceHlsl ||
John Kessenicha28f7a72019-08-06 07:00:58 -06001258 type.getQualifier().storage == glslang::EvqUniform) {
John Kessenichdeec1932019-08-13 08:00:30 -06001259 if (type.isAtomic())
John Kessenichbed4e4f2017-09-08 02:38:07 -06001260 return spv::StorageClassAtomicCounter;
1261 if (type.containsOpaque())
1262 return spv::StorageClassUniformConstant;
1263 }
1264
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001265 if (type.getQualifier().isUniformOrBuffer() &&
Daniel Kochdb32b242020-03-17 20:42:47 -04001266 type.getQualifier().isShaderRecord()) {
1267 return spv::StorageClassShaderRecordBufferKHR;
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001268 }
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001269
John Kessenichbed4e4f2017-09-08 02:38:07 -06001270 if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001271 builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3);
John Kessenicha5c5fb62017-05-05 05:09:58 -06001272 return spv::StorageClassStorageBuffer;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001273 }
1274
1275 if (type.getQualifier().isUniformOrBuffer()) {
John Kessenich7015bd62019-08-01 03:28:08 -06001276 if (type.getQualifier().isPushConstant())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001277 return spv::StorageClassPushConstant;
1278 if (type.getBasicType() == glslang::EbtBlock)
1279 return spv::StorageClassUniform;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001280 return spv::StorageClassUniformConstant;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001281 }
John Kessenichbed4e4f2017-09-08 02:38:07 -06001282
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -07001283 if (type.getQualifier().storage == glslang::EvqShared && type.getBasicType() == glslang::EbtBlock) {
1284 builder.addExtension(spv::E_SPV_KHR_workgroup_memory_explicit_layout);
1285 builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayoutKHR);
1286 return spv::StorageClassWorkgroup;
1287 }
1288
John Kessenichbed4e4f2017-09-08 02:38:07 -06001289 switch (type.getQualifier().storage) {
John Kessenichf8d1d742019-10-21 06:55:11 -06001290 case glslang::EvqGlobal: return spv::StorageClassPrivate;
1291 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
1292 case glslang::EvqTemporary: return spv::StorageClassFunction;
John Kessenichdeec1932019-08-13 08:00:30 -06001293 case glslang::EvqShared: return spv::StorageClassWorkgroup;
John Kessenich3dd1ce52019-10-17 07:08:40 -06001294#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -04001295 case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR;
1296 case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR;
1297 case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR;
1298 case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR;
1299 case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR;
Chao Chenb50c02e2018-09-19 11:42:24 -07001300#endif
John Kessenichbed4e4f2017-09-08 02:38:07 -06001301 default:
1302 assert(0);
1303 break;
1304 }
1305
1306 return spv::StorageClassFunction;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001307}
1308
John Kessenich5611c6d2018-04-05 11:25:02 -06001309// Add capabilities pertaining to how an array is indexed.
1310void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
1311 const glslang::TType& indexType)
1312{
John Kessenichb9197c82019-08-11 07:41:45 -06001313#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06001314 if (indexType.getQualifier().isNonUniform()) {
1315 // deal with an asserted non-uniform index
Jeff Bolzc140b962018-07-12 16:51:18 -05001316 // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration
John Kessenich5611c6d2018-04-05 11:25:02 -06001317 if (baseType.getBasicType() == glslang::EbtSampler) {
1318 if (baseType.getQualifier().hasAttachment())
1319 builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001320 else if (baseType.isImage() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001321 builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001322 else if (baseType.isTexture() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001323 builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT);
1324 else if (baseType.isImage())
1325 builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT);
1326 else if (baseType.isTexture())
1327 builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT);
1328 } else if (baseType.getBasicType() == glslang::EbtBlock) {
1329 if (baseType.getQualifier().storage == glslang::EvqBuffer)
1330 builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT);
1331 else if (baseType.getQualifier().storage == glslang::EvqUniform)
1332 builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT);
1333 }
1334 } else {
1335 // assume a dynamically uniform index
1336 if (baseType.getBasicType() == glslang::EbtSampler) {
Jeff Bolzc140b962018-07-12 16:51:18 -05001337 if (baseType.getQualifier().hasAttachment()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001338 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001339 builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001340 } else if (baseType.isImage() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001341 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001342 builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001343 } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001344 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001345 builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001346 }
John Kessenich5611c6d2018-04-05 11:25:02 -06001347 }
1348 }
John Kessenichb9197c82019-08-11 07:41:45 -06001349#endif
John Kessenich5611c6d2018-04-05 11:25:02 -06001350}
1351
qining25262b32016-05-06 17:25:16 -04001352// Return whether or not the given type is something that should be tied to a
John Kessenich6c292d32016-02-15 20:58:50 -07001353// descriptor set.
1354bool IsDescriptorResource(const glslang::TType& type)
1355{
John Kessenichf7497e22016-03-08 21:36:22 -07001356 // uniform and buffer blocks are included, unless it is a push_constant
John Kessenich6c292d32016-02-15 20:58:50 -07001357 if (type.getBasicType() == glslang::EbtBlock)
Chao Chenb50c02e2018-09-19 11:42:24 -07001358 return type.getQualifier().isUniformOrBuffer() &&
Daniel Kochdb32b242020-03-17 20:42:47 -04001359 ! type.getQualifier().isShaderRecord() &&
John Kessenich7015bd62019-08-01 03:28:08 -06001360 ! type.getQualifier().isPushConstant();
John Kessenich6c292d32016-02-15 20:58:50 -07001361
1362 // non block...
1363 // basically samplerXXX/subpass/sampler/texture are all included
1364 // if they are the global-scope-class, not the function parameter
1365 // (or local, if they ever exist) class.
alelenv999d4fd2020-06-01 23:24:41 -07001366 if (type.getBasicType() == glslang::EbtSampler ||
1367 type.getBasicType() == glslang::EbtAccStruct)
John Kessenich6c292d32016-02-15 20:58:50 -07001368 return type.getQualifier().isUniformOrBuffer();
1369
1370 // None of the above.
1371 return false;
1372}
1373
John Kesseniche0b6cad2015-12-24 10:30:13 -07001374void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
1375{
1376 if (child.layoutMatrix == glslang::ElmNone)
1377 child.layoutMatrix = parent.layoutMatrix;
1378
1379 if (parent.invariant)
1380 child.invariant = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001381 if (parent.flat)
1382 child.flat = true;
1383 if (parent.centroid)
1384 child.centroid = true;
John Kessenich7015bd62019-08-01 03:28:08 -06001385#ifndef GLSLANG_WEB
John Kesseniche0b6cad2015-12-24 10:30:13 -07001386 if (parent.nopersp)
1387 child.nopersp = true;
Rex Xu9d93a232016-05-05 12:30:44 +08001388 if (parent.explicitInterp)
1389 child.explicitInterp = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001390 if (parent.perPrimitiveNV)
1391 child.perPrimitiveNV = true;
1392 if (parent.perViewNV)
1393 child.perViewNV = true;
1394 if (parent.perTaskNV)
1395 child.perTaskNV = true;
John Kesseniche0b6cad2015-12-24 10:30:13 -07001396 if (parent.patch)
1397 child.patch = true;
1398 if (parent.sample)
1399 child.sample = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001400 if (parent.coherent)
1401 child.coherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001402 if (parent.devicecoherent)
1403 child.devicecoherent = true;
1404 if (parent.queuefamilycoherent)
1405 child.queuefamilycoherent = true;
1406 if (parent.workgroupcoherent)
1407 child.workgroupcoherent = true;
1408 if (parent.subgroupcoherent)
1409 child.subgroupcoherent = true;
Daniel Kochdb32b242020-03-17 20:42:47 -04001410 if (parent.shadercallcoherent)
1411 child.shadercallcoherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001412 if (parent.nonprivate)
1413 child.nonprivate = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001414 if (parent.volatil)
1415 child.volatil = true;
1416 if (parent.restrict)
1417 child.restrict = true;
1418 if (parent.readonly)
1419 child.readonly = true;
1420 if (parent.writeonly)
1421 child.writeonly = true;
Chao Chen3c366992018-09-19 11:41:59 -07001422#endif
greg-lunarg639f5462020-11-12 11:10:07 -07001423 if (parent.nonUniform)
1424 child.nonUniform = true;
John Kesseniche0b6cad2015-12-24 10:30:13 -07001425}
1426
John Kessenichf2b7f332016-09-01 17:05:23 -06001427bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001428{
John Kessenich7b9fa252016-01-21 18:56:57 -07001429 // This should list qualifiers that simultaneous satisfy:
John Kessenichf2b7f332016-09-01 17:05:23 -06001430 // - struct members might inherit from a struct declaration
1431 // (note that non-block structs don't explicitly inherit,
1432 // only implicitly, meaning no decoration involved)
1433 // - affect decorations on the struct members
1434 // (note smooth does not, and expecting something like volatile
1435 // to effect the whole object)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001436 // - are not part of the offset/st430/etc or row/column-major layout
John Kessenichf2b7f332016-09-01 17:05:23 -06001437 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
John Kesseniche0b6cad2015-12-24 10:30:13 -07001438}
1439
John Kessenich140f3df2015-06-26 16:58:36 -06001440//
1441// Implement the TGlslangToSpvTraverser class.
1442//
1443
John Kessenich8985fc92020-03-03 10:25:07 -07001444TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion,
1445 const glslang::TIntermediate* glslangIntermediate,
1446 spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) :
1447 TIntermTraverser(true, false, true),
1448 options(options),
1449 shaderEntry(nullptr), currentFunction(nullptr),
1450 sequenceDepth(0), logger(buildLogger),
1451 builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger),
1452 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
1453 glslangIntermediate(glslangIntermediate),
Jeff Bolz04d73732019-05-31 13:06:01 -05001454 nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()),
1455 nonSemanticDebugPrintf(0)
John Kessenich140f3df2015-06-26 16:58:36 -06001456{
1457 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
1458
1459 builder.clearAccessChain();
John Kessenich2a271162017-07-20 20:00:36 -06001460 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()),
1461 glslangIntermediate->getVersion());
1462
John Kessenich121853f2017-05-31 17:11:16 -06001463 if (options.generateDebugInfo) {
John Kesseniche485c7a2017-05-31 18:50:53 -06001464 builder.setEmitOpLines();
John Kessenich2a271162017-07-20 20:00:36 -06001465 builder.setSourceFile(glslangIntermediate->getSourceFile());
1466
1467 // Set the source shader's text. If for SPV version 1.0, include
1468 // a preamble in comments stating the OpModuleProcessed instructions.
1469 // Otherwise, emit those as actual instructions.
1470 std::string text;
1471 const std::vector<std::string>& processes = glslangIntermediate->getProcesses();
1472 for (int p = 0; p < (int)processes.size(); ++p) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001473 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) {
John Kessenich2a271162017-07-20 20:00:36 -06001474 text.append("// OpModuleProcessed ");
1475 text.append(processes[p]);
1476 text.append("\n");
1477 } else
1478 builder.addModuleProcessed(processes[p]);
1479 }
John Kessenich8717a5d2018-10-26 10:12:32 -06001480 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0)
John Kessenich2a271162017-07-20 20:00:36 -06001481 text.append("#line 1\n");
1482 text.append(glslangIntermediate->getSourceText());
1483 builder.setSourceText(text);
Greg Fischerd445bb22018-12-06 11:13:15 -07001484 // Pass name and text for all included files
1485 const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
1486 for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
1487 builder.addInclude(iItr->first, iItr->second);
John Kessenich121853f2017-05-31 17:11:16 -06001488 }
John Kessenich140f3df2015-06-26 16:58:36 -06001489 stdBuiltins = builder.import("GLSL.std.450");
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001490
1491 spv::AddressingModel addressingModel = spv::AddressingModelLogical;
1492 spv::MemoryModel memoryModel = spv::MemoryModelGLSL450;
1493
1494 if (glslangIntermediate->usingPhysicalStorageBuffer()) {
1495 addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT;
John Kessenich1ff0c182019-10-10 12:01:13 -06001496 builder.addIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, spv::Spv_1_5);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001497 builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT);
John Kessenich8985fc92020-03-03 10:25:07 -07001498 }
Jeff Bolz36831c92018-09-05 10:11:41 -05001499 if (glslangIntermediate->usingVulkanMemoryModel()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001500 memoryModel = spv::MemoryModelVulkanKHR;
1501 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
John Kessenich8317e6c2019-08-18 23:58:08 -06001502 builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
Jeff Bolz36831c92018-09-05 10:11:41 -05001503 }
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001504 builder.setMemoryModel(addressingModel, memoryModel);
1505
Jeff Bolz4605e2e2019-02-19 13:10:32 -06001506 if (glslangIntermediate->usingVariablePointers()) {
1507 builder.addCapability(spv::CapabilityVariablePointers);
1508 }
1509
John Kessenicheee9d532016-09-19 18:09:30 -06001510 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
1511 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001512
1513 // Add the source extensions
John Kessenich2f273362015-07-18 22:34:27 -06001514 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
1515 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
johnkslang01384722020-08-14 08:40:06 -06001516 builder.addSourceExtension(it->c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001517
1518 // Add the top-level modes for this shader.
1519
John Kessenich92187592016-02-01 13:45:25 -07001520 if (glslangIntermediate->getXfbMode()) {
1521 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06001522 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
John Kessenich92187592016-02-01 13:45:25 -07001523 }
John Kessenich140f3df2015-06-26 16:58:36 -06001524
alelenv59216d52020-05-21 04:38:41 -07001525 if (glslangIntermediate->getLayoutPrimitiveCulling()) {
Daniel Kochffccefd2020-11-23 15:41:27 -05001526 builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingKHR);
alelenv75de1962020-04-08 21:09:20 -07001527 }
1528
John Kessenich140f3df2015-06-26 16:58:36 -06001529 unsigned int mode;
1530 switch (glslangIntermediate->getStage()) {
1531 case EShLangVertex:
John Kessenich5e4b1242015-08-06 22:53:06 -06001532 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -06001533 break;
1534
John Kessenicha28f7a72019-08-06 07:00:58 -06001535 case EShLangFragment:
1536 builder.addCapability(spv::CapabilityShader);
1537 if (glslangIntermediate->getPixelCenterInteger())
1538 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
1539
1540 if (glslangIntermediate->getOriginUpperLeft())
1541 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
1542 else
1543 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
1544
1545 if (glslangIntermediate->getEarlyFragmentTests())
1546 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
1547
1548 if (glslangIntermediate->getPostDepthCoverage()) {
1549 builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage);
1550 builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage);
1551 builder.addExtension(spv::E_SPV_KHR_post_depth_coverage);
1552 }
1553
John Kessenichb9197c82019-08-11 07:41:45 -06001554 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
1555 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
1556
1557#ifndef GLSLANG_WEB
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04001558
John Kessenicha28f7a72019-08-06 07:00:58 -06001559 switch(glslangIntermediate->getDepth()) {
1560 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
1561 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
1562 default: mode = spv::ExecutionModeMax; break;
1563 }
1564 if (mode != spv::ExecutionModeMax)
1565 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kessenicha28f7a72019-08-06 07:00:58 -06001566 switch (glslangIntermediate->getInterlockOrdering()) {
John Kessenichf8d1d742019-10-21 06:55:11 -06001567 case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT;
1568 break;
1569 case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT;
1570 break;
1571 case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT;
1572 break;
1573 case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT;
1574 break;
1575 case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT;
1576 break;
1577 case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT;
1578 break;
1579 default: mode = spv::ExecutionModeMax;
1580 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06001581 }
1582 if (mode != spv::ExecutionModeMax) {
1583 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1584 if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT ||
1585 mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) {
1586 builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT);
1587 } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT ||
1588 mode == spv::ExecutionModePixelInterlockUnorderedEXT) {
1589 builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT);
1590 } else {
1591 builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT);
1592 }
1593 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
1594 }
John Kessenichb9197c82019-08-11 07:41:45 -06001595#endif
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04001596 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06001597
John Kessenicha28f7a72019-08-06 07:00:58 -06001598 case EShLangCompute:
1599 builder.addCapability(spv::CapabilityShader);
1600 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1601 glslangIntermediate->getLocalSize(1),
1602 glslangIntermediate->getLocalSize(2));
1603 if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) {
1604 builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV);
1605 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV);
1606 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1607 } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) {
1608 builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV);
1609 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV);
1610 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1611 }
1612 break;
John Kessenich51ed01c2019-10-10 11:40:11 -06001613#ifndef GLSLANG_WEB
steve-lunarge7412492017-03-23 11:56:07 -06001614 case EShLangTessEvaluation:
John Kessenich140f3df2015-06-26 16:58:36 -06001615 case EShLangTessControl:
John Kessenich5e4b1242015-08-06 22:53:06 -06001616 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -06001617
steve-lunarge7412492017-03-23 11:56:07 -06001618 glslang::TLayoutGeometry primitive;
1619
1620 if (glslangIntermediate->getStage() == EShLangTessControl) {
John Kessenich8985fc92020-03-03 10:25:07 -07001621 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices,
1622 glslangIntermediate->getVertices());
steve-lunarge7412492017-03-23 11:56:07 -06001623 primitive = glslangIntermediate->getOutputPrimitive();
1624 } else {
1625 primitive = glslangIntermediate->getInputPrimitive();
1626 }
1627
1628 switch (primitive) {
John Kessenich55e7d112015-11-15 21:33:39 -07001629 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1630 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
1631 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
John Kessenich4016e382016-07-15 11:53:56 -06001632 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001633 }
John Kessenich4016e382016-07-15 11:53:56 -06001634 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001635 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1636
John Kesseniche6903322015-10-13 16:29:02 -06001637 switch (glslangIntermediate->getVertexSpacing()) {
1638 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
1639 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
1640 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
John Kessenich4016e382016-07-15 11:53:56 -06001641 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001642 }
John Kessenich4016e382016-07-15 11:53:56 -06001643 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001644 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1645
1646 switch (glslangIntermediate->getVertexOrder()) {
1647 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
1648 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
John Kessenich4016e382016-07-15 11:53:56 -06001649 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001650 }
John Kessenich4016e382016-07-15 11:53:56 -06001651 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001652 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1653
1654 if (glslangIntermediate->getPointMode())
1655 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
John Kessenich140f3df2015-06-26 16:58:36 -06001656 break;
1657
1658 case EShLangGeometry:
John Kessenich5e4b1242015-08-06 22:53:06 -06001659 builder.addCapability(spv::CapabilityGeometry);
John Kessenich140f3df2015-06-26 16:58:36 -06001660 switch (glslangIntermediate->getInputPrimitive()) {
1661 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
1662 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
1663 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
John Kessenich55e7d112015-11-15 21:33:39 -07001664 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001665 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
John Kessenich4016e382016-07-15 11:53:56 -06001666 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001667 }
John Kessenich4016e382016-07-15 11:53:56 -06001668 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001669 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kesseniche6903322015-10-13 16:29:02 -06001670
John Kessenich140f3df2015-06-26 16:58:36 -06001671 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
1672
1673 switch (glslangIntermediate->getOutputPrimitive()) {
1674 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1675 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
1676 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
John Kessenich4016e382016-07-15 11:53:56 -06001677 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001678 }
John Kessenich4016e382016-07-15 11:53:56 -06001679 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001680 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1681 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1682 break;
1683
Daniel Kochdb32b242020-03-17 20:42:47 -04001684 case EShLangRayGen:
1685 case EShLangIntersect:
1686 case EShLangAnyHit:
1687 case EShLangClosestHit:
1688 case EShLangMiss:
1689 case EShLangCallable:
1690 {
1691 auto& extensions = glslangIntermediate->getRequestedExtensions();
1692 if (extensions.find("GL_NV_ray_tracing") == extensions.end()) {
Daniel Kochffccefd2020-11-23 15:41:27 -05001693 builder.addCapability(spv::CapabilityRayTracingKHR);
Daniel Kochdb32b242020-03-17 20:42:47 -04001694 builder.addExtension("SPV_KHR_ray_tracing");
1695 }
1696 else {
1697 builder.addCapability(spv::CapabilityRayTracingNV);
1698 builder.addExtension("SPV_NV_ray_tracing");
1699 }
Chao Chenb50c02e2018-09-19 11:42:24 -07001700 break;
Daniel Kochdb32b242020-03-17 20:42:47 -04001701 }
Chao Chen3c366992018-09-19 11:41:59 -07001702 case EShLangTaskNV:
1703 case EShLangMeshNV:
1704 builder.addCapability(spv::CapabilityMeshShadingNV);
1705 builder.addExtension(spv::E_SPV_NV_mesh_shader);
1706 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1707 glslangIntermediate->getLocalSize(1),
1708 glslangIntermediate->getLocalSize(2));
1709 if (glslangIntermediate->getStage() == EShLangMeshNV) {
John Kessenich8985fc92020-03-03 10:25:07 -07001710 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices,
1711 glslangIntermediate->getVertices());
1712 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV,
1713 glslangIntermediate->getPrimitives());
Chao Chen3c366992018-09-19 11:41:59 -07001714
1715 switch (glslangIntermediate->getOutputPrimitive()) {
1716 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1717 case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break;
1718 case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break;
1719 default: mode = spv::ExecutionModeMax; break;
1720 }
1721 if (mode != spv::ExecutionModeMax)
1722 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1723 }
1724 break;
1725#endif
1726
John Kessenich140f3df2015-06-26 16:58:36 -06001727 default:
1728 break;
1729 }
John Kessenich140f3df2015-06-26 16:58:36 -06001730}
1731
John Kessenichfca82622016-11-26 13:23:20 -07001732// Finish creating SPV, after the traversal is complete.
1733void TGlslangToSpvTraverser::finishSpv()
John Kessenich7ba63412015-12-20 17:37:07 -07001734{
John Kessenichf04c51b2018-08-03 15:56:12 -06001735 // Finish the entry point function
John Kessenich517fe7a2016-11-26 13:31:47 -07001736 if (! entryPointTerminated) {
John Kessenichfca82622016-11-26 13:23:20 -07001737 builder.setBuildPoint(shaderEntry->getLastBlock());
1738 builder.leaveFunction();
1739 }
1740
John Kessenich7ba63412015-12-20 17:37:07 -07001741 // finish off the entry-point SPV instruction by adding the Input/Output <id>
rdb32084e82016-02-23 22:17:38 +01001742 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
1743 entryPoint->addIdOperand(*it);
John Kessenich7ba63412015-12-20 17:37:07 -07001744
David Neto8c3d5b42019-10-21 14:50:31 -04001745 // Add capabilities, extensions, remove unneeded decorations, etc.,
John Kessenichf04c51b2018-08-03 15:56:12 -06001746 // based on the resulting SPIR-V.
David Neto8c3d5b42019-10-21 14:50:31 -04001747 // Note: WebGPU code generation must have the opportunity to aggressively
1748 // prune unreachable merge blocks and continue targets.
John Kessenichf04c51b2018-08-03 15:56:12 -06001749 builder.postProcess();
John Kessenich7ba63412015-12-20 17:37:07 -07001750}
1751
John Kessenichfca82622016-11-26 13:23:20 -07001752// Write the SPV into 'out'.
1753void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
John Kessenich140f3df2015-06-26 16:58:36 -06001754{
John Kessenichfca82622016-11-26 13:23:20 -07001755 builder.dump(out);
John Kessenich140f3df2015-06-26 16:58:36 -06001756}
1757
1758//
1759// Implement the traversal functions.
1760//
1761// Return true from interior nodes to have the external traversal
1762// continue on to children. Return false if children were
1763// already processed.
1764//
1765
1766//
qining25262b32016-05-06 17:25:16 -04001767// Symbols can turn into
John Kessenich140f3df2015-06-26 16:58:36 -06001768// - uniform/input reads
1769// - output writes
1770// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
1771// - something simple that degenerates into the last bullet
1772//
1773void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
1774{
qining75d1d802016-04-06 14:42:01 -04001775 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
Roy05a5b532020-01-03 16:21:34 +08001776 if (symbol->getType().isStruct())
1777 glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId();
1778
qining75d1d802016-04-06 14:42:01 -04001779 if (symbol->getType().getQualifier().isSpecConstant())
1780 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1781
Rex Xuf6e0fe82020-10-23 22:54:35 +08001782#ifdef ENABLE_HLSL
1783 // Skip symbol handling if it is string-typed
1784 if (symbol->getBasicType() == glslang::EbtString)
1785 return;
1786#endif
1787
John Kessenich140f3df2015-06-26 16:58:36 -06001788 // getSymbolId() will set up all the IO decorations on the first call.
1789 // Formal function parameters were mapped during makeFunctions().
1790 spv::Id id = getSymbolId(symbol);
John Kessenich7ba63412015-12-20 17:37:07 -07001791
John Kessenich7ba63412015-12-20 17:37:07 -07001792 if (builder.isPointer(id)) {
John Kessenichc30d3352020-06-10 07:15:24 -06001793 if (!symbol->getType().getQualifier().isParamInput() &&
1794 !symbol->getType().getQualifier().isParamOutput()) {
1795 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
1796 // Consider adding to the OpEntryPoint interface list.
1797 // Only looking at structures if they have at least one member.
1798 if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) {
1799 spv::StorageClass sc = builder.getStorageClass(id);
1800 // Before SPIR-V 1.4, we only want to include Input and Output.
1801 // Starting with SPIR-V 1.4, we want all globals.
John Kessenich4e13c902020-07-13 00:35:58 -06001802 if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) ||
John Kessenichc30d3352020-06-10 07:15:24 -06001803 (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) {
1804 iOSet.insert(id);
1805 }
John Kessenich7c7731e2019-01-04 16:47:06 +07001806 }
John Kessenich5f77d862017-09-19 11:09:59 -06001807 }
John Kessenich9c14f772019-06-17 08:38:35 -06001808
Daniel Kochdb32b242020-03-17 20:42:47 -04001809 // If the SPIR-V type is required to be different than the AST type
1810 // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices),
John Kessenich9c14f772019-06-17 08:38:35 -06001811 // translate now from the SPIR-V type to the AST type, for the consuming
1812 // operation.
1813 // Note this turns it from an l-value to an r-value.
1814 // Currently, all symbols needing this are inputs; avoid the map lookup when non-input.
1815 if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn)
1816 id = translateForcedType(id);
John Kessenich7ba63412015-12-20 17:37:07 -07001817 }
1818
1819 // Only process non-linkage-only nodes for generating actual static uses
John Kessenich6c292d32016-02-15 20:58:50 -07001820 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001821 // Prepare to generate code for the access
1822
1823 // L-value chains will be computed left to right. We're on the symbol now,
1824 // which is the left-most part of the access chain, so now is "clear" time,
1825 // followed by setting the base.
1826 builder.clearAccessChain();
1827
1828 // For now, we consider all user variables as being in memory, so they are pointers,
John Kessenich6c292d32016-02-15 20:58:50 -07001829 // except for
John Kessenich4bf71552016-09-02 11:20:21 -06001830 // A) R-Value arguments to a function, which are an intermediate object.
John Kessenich6c292d32016-02-15 20:58:50 -07001831 // See comments in handleUserFunctionCall().
John Kessenich4bf71552016-09-02 11:20:21 -06001832 // B) Specialization constants (normal constants don't even come in as a variable),
John Kessenich6c292d32016-02-15 20:58:50 -07001833 // These are also pure R-values.
John Kessenich9c14f772019-06-17 08:38:35 -06001834 // C) R-Values from type translation, see above call to translateForcedType()
John Kessenich6c292d32016-02-15 20:58:50 -07001835 glslang::TQualifier qualifier = symbol->getQualifier();
John Kessenich9c14f772019-06-17 08:38:35 -06001836 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() ||
1837 !builder.isPointerType(builder.getTypeId(id)))
John Kessenich140f3df2015-06-26 16:58:36 -06001838 builder.setAccessChainRValue(id);
1839 else
1840 builder.setAccessChainLValue(id);
1841 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001842
John Kessenichb9197c82019-08-11 07:41:45 -06001843#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001844 // Process linkage-only nodes for any special additional interface work.
1845 if (linkageOnly) {
1846 if (glslangIntermediate->getHlslFunctionality1()) {
1847 // Map implicit counter buffers to their originating buffers, which should have been
1848 // seen by now, given earlier pruning of unused counters, and preservation of order
1849 // of declaration.
1850 if (symbol->getType().getQualifier().isUniformOrBuffer()) {
1851 if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) {
1852 // Save possible originating buffers for counter buffers, keyed by
1853 // making the potential counter-buffer name.
1854 std::string keyName = symbol->getName().c_str();
1855 keyName = glslangIntermediate->addCounterBufferName(keyName);
1856 counterOriginator[keyName] = symbol;
1857 } else {
1858 // Handle a counter buffer, by finding the saved originating buffer.
1859 std::string keyName = symbol->getName().c_str();
1860 auto it = counterOriginator.find(keyName);
1861 if (it != counterOriginator.end()) {
1862 id = getSymbolId(it->second);
1863 if (id != spv::NoResult) {
1864 spv::Id counterId = getSymbolId(symbol);
John Kessenichf52b6382018-04-05 19:35:38 -06001865 if (counterId != spv::NoResult) {
1866 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
John Kessenich5d610ee2018-03-07 18:05:55 -07001867 builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId);
John Kessenichf52b6382018-04-05 19:35:38 -06001868 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001869 }
1870 }
1871 }
1872 }
1873 }
1874 }
John Kessenich155d3512019-08-08 23:29:20 -06001875#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001876}
1877
1878bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1879{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07001880 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Roy05a5b532020-01-03 16:21:34 +08001881 if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) {
1882 glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId();
1883 }
1884 if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) {
1885 glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId();
1886 }
John Kesseniche485c7a2017-05-31 18:50:53 -06001887
qining40887662016-04-03 22:20:42 -04001888 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1889 if (node->getType().getQualifier().isSpecConstant())
1890 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1891
John Kessenich140f3df2015-06-26 16:58:36 -06001892 // First, handle special cases
1893 switch (node->getOp()) {
1894 case glslang::EOpAssign:
1895 case glslang::EOpAddAssign:
1896 case glslang::EOpSubAssign:
1897 case glslang::EOpMulAssign:
1898 case glslang::EOpVectorTimesMatrixAssign:
1899 case glslang::EOpVectorTimesScalarAssign:
1900 case glslang::EOpMatrixTimesScalarAssign:
1901 case glslang::EOpMatrixTimesMatrixAssign:
1902 case glslang::EOpDivAssign:
1903 case glslang::EOpModAssign:
1904 case glslang::EOpAndAssign:
1905 case glslang::EOpInclusiveOrAssign:
1906 case glslang::EOpExclusiveOrAssign:
1907 case glslang::EOpLeftShiftAssign:
1908 case glslang::EOpRightShiftAssign:
1909 // A bin-op assign "a += b" means the same thing as "a = a + b"
1910 // where a is evaluated before b. For a simple assignment, GLSL
1911 // says to evaluate the left before the right. So, always, left
1912 // node then right node.
1913 {
1914 // get the left l-value, save it away
1915 builder.clearAccessChain();
1916 node->getLeft()->traverse(this);
1917 spv::Builder::AccessChain lValue = builder.getAccessChain();
1918
1919 // evaluate the right
1920 builder.clearAccessChain();
1921 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001922 spv::Id rValue = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001923
1924 if (node->getOp() != glslang::EOpAssign) {
1925 // the left is also an r-value
1926 builder.setAccessChain(lValue);
John Kessenich32cfd492016-02-02 12:37:46 -07001927 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001928
1929 // do the operation
greg-lunarg639f5462020-11-12 11:10:07 -07001930 spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType());
1931 coherentFlags |= TranslateCoherent(node->getRight()->getType());
John Kessenichead86222018-03-28 18:01:20 -06001932 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001933 TranslateNoContractionDecoration(node->getType().getQualifier()),
greg-lunarg639f5462020-11-12 11:10:07 -07001934 TranslateNonUniformDecoration(coherentFlags) };
John Kessenichead86222018-03-28 18:01:20 -06001935 rValue = createBinaryOperation(node->getOp(), decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06001936 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1937 node->getType().getBasicType());
1938
1939 // these all need their counterparts in createBinaryOperation()
John Kessenich55e7d112015-11-15 21:33:39 -07001940 assert(rValue != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001941 }
1942
1943 // store the result
1944 builder.setAccessChain(lValue);
Jeff Bolz36831c92018-09-05 10:11:41 -05001945 multiTypeStore(node->getLeft()->getType(), rValue);
John Kessenich140f3df2015-06-26 16:58:36 -06001946
1947 // assignments are expressions having an rValue after they are evaluated...
1948 builder.clearAccessChain();
1949 builder.setAccessChainRValue(rValue);
1950 }
1951 return false;
1952 case glslang::EOpIndexDirect:
1953 case glslang::EOpIndexDirectStruct:
1954 {
John Kessenich61a5ce12019-02-07 08:04:12 -07001955 // Structure, array, matrix, or vector indirection with statically known index.
John Kessenich140f3df2015-06-26 16:58:36 -06001956 // Get the left part of the access chain.
1957 node->getLeft()->traverse(this);
1958
1959 // Add the next element in the chain
1960
David Netoa901ffe2016-06-08 14:11:40 +01001961 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
John Kessenich140f3df2015-06-26 16:58:36 -06001962 if (! node->getLeft()->getType().isArray() &&
1963 node->getLeft()->getType().isVector() &&
1964 node->getOp() == glslang::EOpIndexDirect) {
greg-lunarg639f5462020-11-12 11:10:07 -07001965 // Swizzle is uniform so propagate uniform into access chain
1966 spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType());
1967 coherentFlags.nonUniform = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06001968 // This is essentially a hard-coded vector swizzle of size 1,
1969 // so short circuit the access-chain stuff with a swizzle.
1970 std::vector<unsigned> swizzle;
David Netoa901ffe2016-06-08 14:11:40 +01001971 swizzle.push_back(glslangIndex);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001972 int dummySize;
1973 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
greg-lunarg639f5462020-11-12 11:10:07 -07001974 coherentFlags,
John Kessenich8985fc92020-03-03 10:25:07 -07001975 glslangIntermediate->getBaseAlignmentScalar(
1976 node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001977 } else {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001978
1979 // Load through a block reference is performed with a dot operator that
1980 // is mapped to EOpIndexDirectStruct. When we get to the actual reference,
1981 // do a load and reset the access chain.
John Kessenich7015bd62019-08-01 03:28:08 -06001982 if (node->getLeft()->isReference() &&
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001983 !node->getLeft()->getType().isArray() &&
1984 node->getOp() == glslang::EOpIndexDirectStruct)
1985 {
1986 spv::Id left = accessChainLoad(node->getLeft()->getType());
1987 builder.clearAccessChain();
1988 builder.setAccessChainLValue(left);
1989 }
1990
David Netoa901ffe2016-06-08 14:11:40 +01001991 int spvIndex = glslangIndex;
1992 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1993 node->getOp() == glslang::EOpIndexDirectStruct)
1994 {
1995 // This may be, e.g., an anonymous block-member selection, which generally need
1996 // index remapping due to hidden members in anonymous blocks.
Chow93b400f2020-11-12 15:54:16 +08001997 long long glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()];
Roy05a5b532020-01-03 16:21:34 +08001998 if (memberRemapper.find(glslangId) != memberRemapper.end()) {
1999 std::vector<int>& remapper = memberRemapper[glslangId];
2000 assert(remapper.size() > 0);
2001 spvIndex = remapper[glslangIndex];
2002 }
David Netoa901ffe2016-06-08 14:11:40 +01002003 }
John Kessenichebb50532016-05-16 19:22:05 -06002004
greg-lunarg639f5462020-11-12 11:10:07 -07002005 // Struct reference propagates uniform lvalue
2006 spv::Builder::AccessChain::CoherentFlags coherentFlags =
2007 TranslateCoherent(node->getLeft()->getType());
2008 coherentFlags.nonUniform = 0;
2009
David Netoa901ffe2016-06-08 14:11:40 +01002010 // normal case for indexing array or structure or block
John Kessenich8985fc92020-03-03 10:25:07 -07002011 builder.accessChainPush(builder.makeIntConstant(spvIndex),
greg-lunarg639f5462020-11-12 11:10:07 -07002012 coherentFlags,
John Kessenich8985fc92020-03-03 10:25:07 -07002013 node->getLeft()->getType().getBufferReferenceAlignment());
David Netoa901ffe2016-06-08 14:11:40 +01002014
2015 // Add capabilities here for accessing PointSize and clip/cull distance.
2016 // We have deferred generation of associated capabilities until now.
John Kessenichebb50532016-05-16 19:22:05 -06002017 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
David Netoa901ffe2016-06-08 14:11:40 +01002018 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
John Kessenich140f3df2015-06-26 16:58:36 -06002019 }
2020 }
2021 return false;
2022 case glslang::EOpIndexIndirect:
2023 {
John Kessenich61a5ce12019-02-07 08:04:12 -07002024 // Array, matrix, or vector indirection with variable index.
2025 // Will use native SPIR-V access-chain for and array indirection;
John Kessenich140f3df2015-06-26 16:58:36 -06002026 // matrices are arrays of vectors, so will also work for a matrix.
2027 // Will use the access chain's 'component' for variable index into a vector.
2028
2029 // This adapter is building access chains left to right.
2030 // Set up the access chain to the left.
2031 node->getLeft()->traverse(this);
2032
2033 // save it so that computing the right side doesn't trash it
2034 spv::Builder::AccessChain partial = builder.getAccessChain();
2035
2036 // compute the next index in the chain
2037 builder.clearAccessChain();
2038 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002039 spv::Id index = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002040
John Kessenich5611c6d2018-04-05 11:25:02 -06002041 addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType());
2042
John Kessenich140f3df2015-06-26 16:58:36 -06002043 // restore the saved access chain
2044 builder.setAccessChain(partial);
2045
greg-lunarg639f5462020-11-12 11:10:07 -07002046 // Only if index is nonUniform should we propagate nonUniform into access chain
2047 spv::Builder::AccessChain::CoherentFlags index_flags = TranslateCoherent(node->getRight()->getType());
2048 spv::Builder::AccessChain::CoherentFlags coherent_flags = TranslateCoherent(node->getLeft()->getType());
2049 coherent_flags.nonUniform = index_flags.nonUniform;
2050
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002051 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) {
2052 int dummySize;
greg-lunarg639f5462020-11-12 11:10:07 -07002053 builder.accessChainPushComponent(
2054 index, convertGlslangToSpvType(node->getLeft()->getType()), coherent_flags,
John Kessenich8985fc92020-03-03 10:25:07 -07002055 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(),
2056 dummySize));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002057 } else
greg-lunarg639f5462020-11-12 11:10:07 -07002058 builder.accessChainPush(index, coherent_flags,
2059 node->getLeft()->getType().getBufferReferenceAlignment());
John Kessenich140f3df2015-06-26 16:58:36 -06002060 }
2061 return false;
2062 case glslang::EOpVectorSwizzle:
2063 {
2064 node->getLeft()->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06002065 std::vector<unsigned> swizzle;
John Kessenich8c8505c2016-07-26 12:50:38 -06002066 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002067 int dummySize;
2068 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
2069 TranslateCoherent(node->getLeft()->getType()),
John Kessenich8985fc92020-03-03 10:25:07 -07002070 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(),
2071 dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06002072 }
2073 return false;
John Kessenichfdf63472017-01-13 12:27:52 -07002074 case glslang::EOpMatrixSwizzle:
2075 logger->missingFunctionality("matrix swizzle");
2076 return true;
John Kessenich7c1aa102015-10-15 13:29:11 -06002077 case glslang::EOpLogicalOr:
2078 case glslang::EOpLogicalAnd:
2079 {
2080
2081 // These may require short circuiting, but can sometimes be done as straight
2082 // binary operations. The right operand must be short circuited if it has
2083 // side effects, and should probably be if it is complex.
2084 if (isTrivial(node->getRight()->getAsTyped()))
2085 break; // handle below as a normal binary operation
2086 // otherwise, we need to do dynamic short circuiting on the right operand
John Kessenich8985fc92020-03-03 10:25:07 -07002087 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(),
2088 *node->getRight()->getAsTyped());
John Kessenich7c1aa102015-10-15 13:29:11 -06002089 builder.clearAccessChain();
2090 builder.setAccessChainRValue(result);
2091 }
2092 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06002093 default:
2094 break;
2095 }
2096
2097 // Assume generic binary op...
2098
John Kessenich32cfd492016-02-02 12:37:46 -07002099 // get right operand
John Kessenich140f3df2015-06-26 16:58:36 -06002100 builder.clearAccessChain();
2101 node->getLeft()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002102 spv::Id left = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002103
John Kessenich32cfd492016-02-02 12:37:46 -07002104 // get left operand
John Kessenich140f3df2015-06-26 16:58:36 -06002105 builder.clearAccessChain();
2106 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002107 spv::Id right = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002108
John Kessenich32cfd492016-02-02 12:37:46 -07002109 // get result
John Kessenichead86222018-03-28 18:01:20 -06002110 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06002111 TranslateNoContractionDecoration(node->getType().getQualifier()),
2112 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002113 spv::Id result = createBinaryOperation(node->getOp(), decorations,
John Kessenich32cfd492016-02-02 12:37:46 -07002114 convertGlslangToSpvType(node->getType()), left, right,
2115 node->getLeft()->getType().getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06002116
John Kessenich50e57562015-12-21 21:21:11 -07002117 builder.clearAccessChain();
John Kessenich140f3df2015-06-26 16:58:36 -06002118 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04002119 logger->missingFunctionality("unknown glslang binary operation");
John Kessenich50e57562015-12-21 21:21:11 -07002120 return true; // pick up a child as the place-holder result
John Kessenich140f3df2015-06-26 16:58:36 -06002121 } else {
John Kessenich140f3df2015-06-26 16:58:36 -06002122 builder.setAccessChainRValue(result);
John Kessenich140f3df2015-06-26 16:58:36 -06002123 return false;
2124 }
John Kessenich140f3df2015-06-26 16:58:36 -06002125}
2126
John Kessenich9c14f772019-06-17 08:38:35 -06002127// Figure out what, if any, type changes are needed when accessing a specific built-in.
2128// Returns <the type SPIR-V requires for declarion, the type to translate to on use>.
2129// Also see comment for 'forceType', regarding tracking SPIR-V-required types.
Daniel Kochdb32b242020-03-17 20:42:47 -04002130std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn,
John Kessenich9c14f772019-06-17 08:38:35 -06002131 const glslang::TType& glslangType)
2132{
Daniel Kochdb32b242020-03-17 20:42:47 -04002133 switch(glslangBuiltIn)
John Kessenich9c14f772019-06-17 08:38:35 -06002134 {
Daniel Kochdb32b242020-03-17 20:42:47 -04002135 case glslang::EbvSubGroupEqMask:
2136 case glslang::EbvSubGroupGeMask:
2137 case glslang::EbvSubGroupGtMask:
2138 case glslang::EbvSubGroupLeMask:
2139 case glslang::EbvSubGroupLtMask: {
John Kessenich9c14f772019-06-17 08:38:35 -06002140 // these require changing a 64-bit scaler -> a vector of 32-bit components
2141 if (glslangType.isVector())
2142 break;
Daniel Kochffccefd2020-11-23 15:41:27 -05002143 spv::Id ivec4_type = builder.makeVectorType(builder.makeUintType(32), 4);
2144 spv::Id uint64_type = builder.makeUintType(64);
2145 std::pair<spv::Id, spv::Id> ret(ivec4_type, uint64_type);
John Kessenich9c14f772019-06-17 08:38:35 -06002146 return ret;
2147 }
Daniel Kochdb32b242020-03-17 20:42:47 -04002148 // There are no SPIR-V builtins defined for these and map onto original non-transposed
2149 // builtins. During visitBinary we insert a transpose
2150 case glslang::EbvWorldToObject3x4:
2151 case glslang::EbvObjectToWorld3x4: {
John Kessenichf80ef742020-07-14 01:44:35 -06002152 spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
2153 spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4);
2154 std::pair<spv::Id, spv::Id> ret(mat43, mat34);
Daniel Kochdb32b242020-03-17 20:42:47 -04002155 return ret;
2156 }
John Kessenich9c14f772019-06-17 08:38:35 -06002157 default:
2158 break;
2159 }
2160
2161 std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType);
2162 return ret;
2163}
2164
2165// For an object previously identified (see getForcedType() and forceType)
2166// as needing type translations, do the translation needed for a load, turning
2167// an L-value into in R-value.
2168spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object)
2169{
2170 const auto forceIt = forceType.find(object);
2171 if (forceIt == forceType.end())
2172 return object;
2173
2174 spv::Id desiredTypeId = forceIt->second;
2175 spv::Id objectTypeId = builder.getTypeId(object);
2176 assert(builder.isPointerType(objectTypeId));
2177 objectTypeId = builder.getContainedTypeId(objectTypeId);
2178 if (builder.isVectorType(objectTypeId) &&
2179 builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) {
2180 if (builder.getScalarTypeWidth(desiredTypeId) == 64) {
2181 // handle 32-bit v.xy* -> 64-bit
2182 builder.clearAccessChain();
2183 builder.setAccessChainLValue(object);
greg-lunarg639f5462020-11-12 11:10:07 -07002184 object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId);
John Kessenich9c14f772019-06-17 08:38:35 -06002185 std::vector<spv::Id> components;
2186 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0));
2187 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1));
2188
2189 spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2);
2190 return builder.createUnaryOp(spv::OpBitcast, desiredTypeId,
2191 builder.createCompositeConstruct(vecType, components));
2192 } else {
2193 logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar");
2194 }
Daniel Kochdb32b242020-03-17 20:42:47 -04002195 } else if (builder.isMatrixType(objectTypeId)) {
2196 // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject
2197 // and we insert a transpose after loading the original non-transposed builtins
2198 builder.clearAccessChain();
2199 builder.setAccessChainLValue(object);
greg-lunarg639f5462020-11-12 11:10:07 -07002200 object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId);
Daniel Kochdb32b242020-03-17 20:42:47 -04002201 return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object);
2202
2203 } else {
John Kessenich9c14f772019-06-17 08:38:35 -06002204 logger->missingFunctionality("forcing non 32-bit vector type");
2205 }
2206
2207 return object;
2208}
2209
John Kessenich140f3df2015-06-26 16:58:36 -06002210bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
2211{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002212 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002213
qining40887662016-04-03 22:20:42 -04002214 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2215 if (node->getType().getQualifier().isSpecConstant())
2216 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2217
John Kessenichfc51d282015-08-19 13:34:18 -06002218 spv::Id result = spv::NoResult;
2219
2220 // try texturing first
2221 result = createImageTextureFunctionCall(node);
2222 if (result != spv::NoResult) {
2223 builder.clearAccessChain();
2224 builder.setAccessChainRValue(result);
2225
2226 return false; // done with this node
2227 }
2228
2229 // Non-texturing.
John Kessenichc9a80832015-09-12 12:17:44 -06002230
2231 if (node->getOp() == glslang::EOpArrayLength) {
2232 // Quite special; won't want to evaluate the operand.
2233
John Kessenich5611c6d2018-04-05 11:25:02 -06002234 // Currently, the front-end does not allow .length() on an array until it is sized,
2235 // except for the last block membeor of an SSBO.
2236 // TODO: If this changes, link-time sized arrays might show up here, and need their
2237 // size extracted.
2238
John Kessenichc9a80832015-09-12 12:17:44 -06002239 // Normal .length() would have been constant folded by the front-end.
2240 // So, this has to be block.lastMember.length().
John Kessenichee21fc92015-09-21 21:50:29 -06002241 // SPV wants "block" and member number as the operands, go get them.
John Kessenichead86222018-03-28 18:01:20 -06002242
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002243 spv::Id length;
2244 if (node->getOperand()->getType().isCoopMat()) {
2245 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2246
2247 spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType());
2248 assert(builder.isCooperativeMatrixType(typeId));
2249
2250 length = builder.createCooperativeMatrixLength(typeId);
2251 } else {
2252 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
2253 block->traverse(this);
John Kessenich8985fc92020-03-03 10:25:07 -07002254 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()
2255 ->getConstArray()[0].getUConst();
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002256 length = builder.createArrayLength(builder.accessChainGetLValue(), member);
2257 }
John Kessenichc9a80832015-09-12 12:17:44 -06002258
John Kessenich8c869672018-11-28 07:01:37 -07002259 // GLSL semantics say the result of .length() is an int, while SPIR-V says
2260 // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's
2261 // AST expectation of a signed result.
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002262 if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) {
2263 if (builder.isInSpecConstCodeGenMode()) {
2264 length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0));
2265 } else {
2266 length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length);
2267 }
2268 }
John Kessenich8c869672018-11-28 07:01:37 -07002269
John Kessenichc9a80832015-09-12 12:17:44 -06002270 builder.clearAccessChain();
2271 builder.setAccessChainRValue(length);
2272
2273 return false;
2274 }
2275
John Kessenichfc51d282015-08-19 13:34:18 -06002276 // Start by evaluating the operand
2277
John Kessenich8c8505c2016-07-26 12:50:38 -06002278 // Does it need a swizzle inversion? If so, evaluation is inverted;
2279 // operate first on the swizzle base, then apply the swizzle.
2280 spv::Id invertedType = spv::NoType;
John Kessenich8985fc92020-03-03 10:25:07 -07002281 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ?
2282 invertedType : convertGlslangToSpvType(node->getType()); };
John Kessenich8c8505c2016-07-26 12:50:38 -06002283 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
2284 invertedType = getInvertedSwizzleType(*node->getOperand());
2285
John Kessenich140f3df2015-06-26 16:58:36 -06002286 builder.clearAccessChain();
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002287 TIntermNode *operandNode;
John Kessenich8c8505c2016-07-26 12:50:38 -06002288 if (invertedType != spv::NoType)
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002289 operandNode = node->getOperand()->getAsBinaryNode()->getLeft();
John Kessenich8c8505c2016-07-26 12:50:38 -06002290 else
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002291 operandNode = node->getOperand();
2292
2293 operandNode->traverse(this);
Rex Xu30f92582015-09-14 10:38:56 +08002294
Rex Xufc618912015-09-09 16:42:49 +08002295 spv::Id operand = spv::NoResult;
2296
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002297 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2298
John Kessenichfb4f2332019-08-09 03:49:15 -06002299#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08002300 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
2301 node->getOp() == glslang::EOpAtomicCounterDecrement ||
Rex Xu7a26c172015-12-08 17:12:09 +08002302 node->getOp() == glslang::EOpAtomicCounter ||
Neslisah Torosdagli7d37a682020-03-26 10:52:33 -04002303 node->getOp() == glslang::EOpInterpolateAtCentroid ||
2304 node->getOp() == glslang::EOpRayQueryProceed ||
2305 node->getOp() == glslang::EOpRayQueryGetRayTMin ||
2306 node->getOp() == glslang::EOpRayQueryGetRayFlags ||
2307 node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin ||
2308 node->getOp() == glslang::EOpRayQueryGetWorldRayDirection ||
2309 node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque ||
2310 node->getOp() == glslang::EOpRayQueryTerminate ||
2311 node->getOp() == glslang::EOpRayQueryConfirmIntersection) {
Rex Xufc618912015-09-09 16:42:49 +08002312 operand = builder.accessChainGetLValue(); // Special case l-value operands
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002313 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
2314 lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType());
2315 } else
John Kessenichfb4f2332019-08-09 03:49:15 -06002316#endif
2317 {
John Kessenich32cfd492016-02-02 12:37:46 -07002318 operand = accessChainLoad(node->getOperand()->getType());
John Kessenichfb4f2332019-08-09 03:49:15 -06002319 }
John Kessenich140f3df2015-06-26 16:58:36 -06002320
John Kessenichead86222018-03-28 18:01:20 -06002321 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06002322 TranslateNoContractionDecoration(node->getType().getQualifier()),
2323 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenich140f3df2015-06-26 16:58:36 -06002324
2325 // it could be a conversion
John Kessenichfc51d282015-08-19 13:34:18 -06002326 if (! result)
John Kessenich8985fc92020-03-03 10:25:07 -07002327 result = createConversion(node->getOp(), decorations, resultType(), operand,
2328 node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06002329
2330 // if not, then possibly an operation
2331 if (! result)
John Kessenich8985fc92020-03-03 10:25:07 -07002332 result = createUnaryOperation(node->getOp(), decorations, resultType(), operand,
2333 node->getOperand()->getBasicType(), lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002334
2335 if (result) {
John Kessenich5611c6d2018-04-05 11:25:02 -06002336 if (invertedType) {
John Kessenichead86222018-03-28 18:01:20 -06002337 result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result);
John Kessenichb9197c82019-08-11 07:41:45 -06002338 decorations.addNonUniform(builder, result);
John Kessenich5611c6d2018-04-05 11:25:02 -06002339 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002340
John Kessenich140f3df2015-06-26 16:58:36 -06002341 builder.clearAccessChain();
2342 builder.setAccessChainRValue(result);
2343
2344 return false; // done with this node
2345 }
2346
2347 // it must be a special case, check...
2348 switch (node->getOp()) {
2349 case glslang::EOpPostIncrement:
2350 case glslang::EOpPostDecrement:
2351 case glslang::EOpPreIncrement:
2352 case glslang::EOpPreDecrement:
2353 {
2354 // we need the integer value "1" or the floating point "1.0" to add/subtract
Rex Xu8ff43de2016-04-22 16:51:45 +08002355 spv::Id one = 0;
2356 if (node->getBasicType() == glslang::EbtFloat)
2357 one = builder.makeFloatConstant(1.0F);
John Kessenich39697cd2019-08-08 10:35:51 -06002358#ifndef GLSLANG_WEB
Rex Xuce31aea2016-07-29 16:13:04 +08002359 else if (node->getBasicType() == glslang::EbtDouble)
2360 one = builder.makeDoubleConstant(1.0);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002361 else if (node->getBasicType() == glslang::EbtFloat16)
2362 one = builder.makeFloat16Constant(1.0F);
John Kessenich66011cb2018-03-06 16:12:04 -07002363 else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8)
2364 one = builder.makeInt8Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08002365 else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16)
2366 one = builder.makeInt16Constant(1);
John Kessenich66011cb2018-03-06 16:12:04 -07002367 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
2368 one = builder.makeInt64Constant(1);
John Kessenich39697cd2019-08-08 10:35:51 -06002369#endif
Rex Xu8ff43de2016-04-22 16:51:45 +08002370 else
2371 one = builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06002372 glslang::TOperator op;
2373 if (node->getOp() == glslang::EOpPreIncrement ||
2374 node->getOp() == glslang::EOpPostIncrement)
2375 op = glslang::EOpAdd;
2376 else
2377 op = glslang::EOpSub;
2378
John Kessenichead86222018-03-28 18:01:20 -06002379 spv::Id result = createBinaryOperation(op, decorations,
Rex Xu8ff43de2016-04-22 16:51:45 +08002380 convertGlslangToSpvType(node->getType()), operand, one,
2381 node->getType().getBasicType());
John Kessenich55e7d112015-11-15 21:33:39 -07002382 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002383
2384 // The result of operation is always stored, but conditionally the
2385 // consumed result. The consumed result is always an r-value.
Bas Nieuwenhuizende949a22020-08-24 23:27:26 +02002386 builder.accessChainStore(result,
greg-lunarg639f5462020-11-12 11:10:07 -07002387 TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags));
John Kessenich140f3df2015-06-26 16:58:36 -06002388 builder.clearAccessChain();
2389 if (node->getOp() == glslang::EOpPreIncrement ||
2390 node->getOp() == glslang::EOpPreDecrement)
2391 builder.setAccessChainRValue(result);
2392 else
2393 builder.setAccessChainRValue(operand);
2394 }
2395
2396 return false;
2397
John Kessenich155d3512019-08-08 23:29:20 -06002398#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06002399 case glslang::EOpEmitStreamVertex:
2400 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
2401 return false;
2402 case glslang::EOpEndStreamPrimitive:
2403 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
2404 return false;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002405 case glslang::EOpRayQueryTerminate:
2406 builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand);
2407 return false;
2408 case glslang::EOpRayQueryConfirmIntersection:
2409 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand);
2410 return false;
John Kessenich155d3512019-08-08 23:29:20 -06002411#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002412
2413 default:
Lei Zhang17535f72016-05-04 15:55:59 -04002414 logger->missingFunctionality("unknown glslang unary");
John Kessenich50e57562015-12-21 21:21:11 -07002415 return true; // pick up operand as placeholder result
John Kessenich140f3df2015-06-26 16:58:36 -06002416 }
John Kessenich140f3df2015-06-26 16:58:36 -06002417}
2418
Jeff Bolz53134492019-06-25 13:31:10 -05002419// Construct a composite object, recursively copying members if their types don't match
2420spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents)
2421{
2422 for (int c = 0; c < (int)constituents.size(); ++c) {
2423 spv::Id& constituent = constituents[c];
2424 spv::Id lType = builder.getContainedTypeId(resultTypeId, c);
2425 spv::Id rType = builder.getTypeId(constituent);
2426 if (lType != rType) {
2427 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
2428 constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent);
2429 } else if (builder.isStructType(rType)) {
2430 std::vector<spv::Id> rTypeConstituents;
2431 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2432 for (int i = 0; i < numrTypeConstituents; ++i) {
John Kessenich8985fc92020-03-03 10:25:07 -07002433 rTypeConstituents.push_back(builder.createCompositeExtract(constituent,
2434 builder.getContainedTypeId(rType, i), i));
Jeff Bolz53134492019-06-25 13:31:10 -05002435 }
2436 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2437 } else {
2438 assert(builder.isArrayType(rType));
2439 std::vector<spv::Id> rTypeConstituents;
2440 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2441
2442 spv::Id elementRType = builder.getContainedTypeId(rType);
2443 for (int i = 0; i < numrTypeConstituents; ++i) {
2444 rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i));
2445 }
2446 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2447 }
2448 }
2449 }
2450 return builder.createCompositeConstruct(resultTypeId, constituents);
2451}
2452
John Kessenich140f3df2015-06-26 16:58:36 -06002453bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
2454{
qining27e04a02016-04-14 16:40:20 -04002455 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2456 if (node->getType().getQualifier().isSpecConstant())
2457 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2458
John Kessenichfc51d282015-08-19 13:34:18 -06002459 spv::Id result = spv::NoResult;
Cody Northrop4d2298b2020-04-13 21:59:49 -06002460 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
2461 std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for
2462 // SPIR-V, for an out parameter
2463 std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues
John Kessenichbbbd9a22020-03-03 07:21:37 -07002464
John Kessenich8985fc92020-03-03 10:25:07 -07002465 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ?
2466 invertedType :
2467 convertGlslangToSpvType(node->getType()); };
John Kessenichfc51d282015-08-19 13:34:18 -06002468
2469 // try texturing
2470 result = createImageTextureFunctionCall(node);
2471 if (result != spv::NoResult) {
2472 builder.clearAccessChain();
2473 builder.setAccessChainRValue(result);
2474
2475 return false;
John Kessenicha28f7a72019-08-06 07:00:58 -06002476 }
2477#ifndef GLSLANG_WEB
2478 else if (node->getOp() == glslang::EOpImageStore ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002479 node->getOp() == glslang::EOpImageStoreLod ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002480 node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufc618912015-09-09 16:42:49 +08002481 // "imageStore" is a special case, which has no result
2482 return false;
2483 }
John Kessenicha28f7a72019-08-06 07:00:58 -06002484#endif
John Kessenichfc51d282015-08-19 13:34:18 -06002485
John Kessenich140f3df2015-06-26 16:58:36 -06002486 glslang::TOperator binOp = glslang::EOpNull;
2487 bool reduceComparison = true;
2488 bool isMatrix = false;
2489 bool noReturnValue = false;
John Kessenich426394d2015-07-23 10:22:48 -06002490 bool atomic = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002491
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002492 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2493
John Kessenich140f3df2015-06-26 16:58:36 -06002494 assert(node->getOp());
2495
John Kessenichf6640762016-08-01 19:44:00 -06002496 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenich140f3df2015-06-26 16:58:36 -06002497
2498 switch (node->getOp()) {
2499 case glslang::EOpSequence:
2500 {
2501 if (preVisit)
2502 ++sequenceDepth;
2503 else
2504 --sequenceDepth;
2505
2506 if (sequenceDepth == 1) {
2507 // If this is the parent node of all the functions, we want to see them
2508 // early, so all call points have actual SPIR-V functions to reference.
2509 // In all cases, still let the traverser visit the children for us.
2510 makeFunctions(node->getAsAggregate()->getSequence());
2511
John Kessenich6fccb3c2016-09-19 16:01:41 -06002512 // Also, we want all globals initializers to go into the beginning of the entry point, before
John Kessenich140f3df2015-06-26 16:58:36 -06002513 // anything else gets there, so visit out of order, doing them all now.
2514 makeGlobalInitializers(node->getAsAggregate()->getSequence());
2515
Daniel Kochffccefd2020-11-23 15:41:27 -05002516 //Pre process linker objects for ray tracing stages
2517 if (glslangIntermediate->isRayTracingStage())
2518 collectRayTracingLinkerObjects();
2519
John Kessenich6a60c2f2016-12-08 21:01:59 -07002520 // Initializers are done, don't want to visit again, but functions and link objects need to be processed,
John Kessenich140f3df2015-06-26 16:58:36 -06002521 // so do them manually.
2522 visitFunctions(node->getAsAggregate()->getSequence());
2523
2524 return false;
2525 }
2526
2527 return true;
2528 }
2529 case glslang::EOpLinkerObjects:
2530 {
2531 if (visit == glslang::EvPreVisit)
2532 linkageOnly = true;
2533 else
2534 linkageOnly = false;
2535
2536 return true;
2537 }
2538 case glslang::EOpComma:
2539 {
2540 // processing from left to right naturally leaves the right-most
2541 // lying around in the access chain
2542 glslang::TIntermSequence& glslangOperands = node->getSequence();
2543 for (int i = 0; i < (int)glslangOperands.size(); ++i)
2544 glslangOperands[i]->traverse(this);
2545
2546 return false;
2547 }
2548 case glslang::EOpFunction:
2549 if (visit == glslang::EvPreVisit) {
John Kessenich6fccb3c2016-09-19 16:01:41 -06002550 if (isShaderEntryPoint(node)) {
John Kessenich517fe7a2016-11-26 13:31:47 -07002551 inEntryPoint = true;
John Kessenich140f3df2015-06-26 16:58:36 -06002552 builder.setBuildPoint(shaderEntry->getLastBlock());
John Kesseniched33e052016-10-06 12:59:51 -06002553 currentFunction = shaderEntry;
John Kessenich140f3df2015-06-26 16:58:36 -06002554 } else {
2555 handleFunctionEntry(node);
2556 }
2557 } else {
John Kessenich517fe7a2016-11-26 13:31:47 -07002558 if (inEntryPoint)
2559 entryPointTerminated = true;
John Kesseniche770b3e2015-09-14 20:58:02 -06002560 builder.leaveFunction();
John Kessenich517fe7a2016-11-26 13:31:47 -07002561 inEntryPoint = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002562 }
2563
2564 return true;
2565 case glslang::EOpParameters:
2566 // Parameters will have been consumed by EOpFunction processing, but not
2567 // the body, so we still visited the function node's children, making this
2568 // child redundant.
2569 return false;
2570 case glslang::EOpFunctionCall:
2571 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002572 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002573 if (node->isUserDefined())
2574 result = handleUserFunctionCall(node);
John Kessenich6c292d32016-02-15 20:58:50 -07002575 if (result) {
2576 builder.clearAccessChain();
2577 builder.setAccessChainRValue(result);
2578 } else
Lei Zhang17535f72016-05-04 15:55:59 -04002579 logger->missingFunctionality("missing user function; linker needs to catch that");
John Kessenich140f3df2015-06-26 16:58:36 -06002580
2581 return false;
2582 }
2583 case glslang::EOpConstructMat2x2:
2584 case glslang::EOpConstructMat2x3:
2585 case glslang::EOpConstructMat2x4:
2586 case glslang::EOpConstructMat3x2:
2587 case glslang::EOpConstructMat3x3:
2588 case glslang::EOpConstructMat3x4:
2589 case glslang::EOpConstructMat4x2:
2590 case glslang::EOpConstructMat4x3:
2591 case glslang::EOpConstructMat4x4:
2592 case glslang::EOpConstructDMat2x2:
2593 case glslang::EOpConstructDMat2x3:
2594 case glslang::EOpConstructDMat2x4:
2595 case glslang::EOpConstructDMat3x2:
2596 case glslang::EOpConstructDMat3x3:
2597 case glslang::EOpConstructDMat3x4:
2598 case glslang::EOpConstructDMat4x2:
2599 case glslang::EOpConstructDMat4x3:
2600 case glslang::EOpConstructDMat4x4:
LoopDawg174ccb82017-05-20 21:40:27 -06002601 case glslang::EOpConstructIMat2x2:
2602 case glslang::EOpConstructIMat2x3:
2603 case glslang::EOpConstructIMat2x4:
2604 case glslang::EOpConstructIMat3x2:
2605 case glslang::EOpConstructIMat3x3:
2606 case glslang::EOpConstructIMat3x4:
2607 case glslang::EOpConstructIMat4x2:
2608 case glslang::EOpConstructIMat4x3:
2609 case glslang::EOpConstructIMat4x4:
2610 case glslang::EOpConstructUMat2x2:
2611 case glslang::EOpConstructUMat2x3:
2612 case glslang::EOpConstructUMat2x4:
2613 case glslang::EOpConstructUMat3x2:
2614 case glslang::EOpConstructUMat3x3:
2615 case glslang::EOpConstructUMat3x4:
2616 case glslang::EOpConstructUMat4x2:
2617 case glslang::EOpConstructUMat4x3:
2618 case glslang::EOpConstructUMat4x4:
2619 case glslang::EOpConstructBMat2x2:
2620 case glslang::EOpConstructBMat2x3:
2621 case glslang::EOpConstructBMat2x4:
2622 case glslang::EOpConstructBMat3x2:
2623 case glslang::EOpConstructBMat3x3:
2624 case glslang::EOpConstructBMat3x4:
2625 case glslang::EOpConstructBMat4x2:
2626 case glslang::EOpConstructBMat4x3:
2627 case glslang::EOpConstructBMat4x4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002628 case glslang::EOpConstructF16Mat2x2:
2629 case glslang::EOpConstructF16Mat2x3:
2630 case glslang::EOpConstructF16Mat2x4:
2631 case glslang::EOpConstructF16Mat3x2:
2632 case glslang::EOpConstructF16Mat3x3:
2633 case glslang::EOpConstructF16Mat3x4:
2634 case glslang::EOpConstructF16Mat4x2:
2635 case glslang::EOpConstructF16Mat4x3:
2636 case glslang::EOpConstructF16Mat4x4:
John Kessenich140f3df2015-06-26 16:58:36 -06002637 isMatrix = true;
2638 // fall through
2639 case glslang::EOpConstructFloat:
2640 case glslang::EOpConstructVec2:
2641 case glslang::EOpConstructVec3:
2642 case glslang::EOpConstructVec4:
2643 case glslang::EOpConstructDouble:
2644 case glslang::EOpConstructDVec2:
2645 case glslang::EOpConstructDVec3:
2646 case glslang::EOpConstructDVec4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002647 case glslang::EOpConstructFloat16:
2648 case glslang::EOpConstructF16Vec2:
2649 case glslang::EOpConstructF16Vec3:
2650 case glslang::EOpConstructF16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002651 case glslang::EOpConstructBool:
2652 case glslang::EOpConstructBVec2:
2653 case glslang::EOpConstructBVec3:
2654 case glslang::EOpConstructBVec4:
John Kessenich66011cb2018-03-06 16:12:04 -07002655 case glslang::EOpConstructInt8:
2656 case glslang::EOpConstructI8Vec2:
2657 case glslang::EOpConstructI8Vec3:
2658 case glslang::EOpConstructI8Vec4:
2659 case glslang::EOpConstructUint8:
2660 case glslang::EOpConstructU8Vec2:
2661 case glslang::EOpConstructU8Vec3:
2662 case glslang::EOpConstructU8Vec4:
2663 case glslang::EOpConstructInt16:
2664 case glslang::EOpConstructI16Vec2:
2665 case glslang::EOpConstructI16Vec3:
2666 case glslang::EOpConstructI16Vec4:
2667 case glslang::EOpConstructUint16:
2668 case glslang::EOpConstructU16Vec2:
2669 case glslang::EOpConstructU16Vec3:
2670 case glslang::EOpConstructU16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002671 case glslang::EOpConstructInt:
2672 case glslang::EOpConstructIVec2:
2673 case glslang::EOpConstructIVec3:
2674 case glslang::EOpConstructIVec4:
2675 case glslang::EOpConstructUint:
2676 case glslang::EOpConstructUVec2:
2677 case glslang::EOpConstructUVec3:
2678 case glslang::EOpConstructUVec4:
Rex Xu8ff43de2016-04-22 16:51:45 +08002679 case glslang::EOpConstructInt64:
2680 case glslang::EOpConstructI64Vec2:
2681 case glslang::EOpConstructI64Vec3:
2682 case glslang::EOpConstructI64Vec4:
2683 case glslang::EOpConstructUint64:
2684 case glslang::EOpConstructU64Vec2:
2685 case glslang::EOpConstructU64Vec3:
2686 case glslang::EOpConstructU64Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002687 case glslang::EOpConstructStruct:
John Kessenich6c292d32016-02-15 20:58:50 -07002688 case glslang::EOpConstructTextureSampler:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002689 case glslang::EOpConstructReference:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002690 case glslang::EOpConstructCooperativeMatrix:
John Kessenich140f3df2015-06-26 16:58:36 -06002691 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002692 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002693 std::vector<spv::Id> arguments;
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002694 translateArguments(*node, arguments, lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002695 spv::Id constructed;
John Kessenich6c292d32016-02-15 20:58:50 -07002696 if (node->getOp() == glslang::EOpConstructTextureSampler)
John Kessenich8c8505c2016-07-26 12:50:38 -06002697 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002698 else if (node->getOp() == glslang::EOpConstructStruct ||
2699 node->getOp() == glslang::EOpConstructCooperativeMatrix ||
2700 node->getType().isArray()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002701 std::vector<spv::Id> constituents;
2702 for (int c = 0; c < (int)arguments.size(); ++c)
2703 constituents.push_back(arguments[c]);
Jeff Bolz53134492019-06-25 13:31:10 -05002704 constructed = createCompositeConstruct(resultType(), constituents);
John Kessenich55e7d112015-11-15 21:33:39 -07002705 } else if (isMatrix)
John Kessenich8c8505c2016-07-26 12:50:38 -06002706 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
John Kessenich55e7d112015-11-15 21:33:39 -07002707 else
John Kessenich8c8505c2016-07-26 12:50:38 -06002708 constructed = builder.createConstructor(precision, arguments, resultType());
John Kessenich140f3df2015-06-26 16:58:36 -06002709
Bas Nieuwenhuizenc9ffeec2020-09-03 03:09:39 +02002710 if (node->getType().getQualifier().isNonUniform()) {
2711 builder.addDecoration(constructed, spv::DecorationNonUniformEXT);
2712 }
2713
John Kessenich140f3df2015-06-26 16:58:36 -06002714 builder.clearAccessChain();
2715 builder.setAccessChainRValue(constructed);
2716
2717 return false;
2718 }
2719
2720 // These six are component-wise compares with component-wise results.
2721 // Forward on to createBinaryOperation(), requesting a vector result.
2722 case glslang::EOpLessThan:
2723 case glslang::EOpGreaterThan:
2724 case glslang::EOpLessThanEqual:
2725 case glslang::EOpGreaterThanEqual:
2726 case glslang::EOpVectorEqual:
2727 case glslang::EOpVectorNotEqual:
2728 {
2729 // Map the operation to a binary
2730 binOp = node->getOp();
2731 reduceComparison = false;
2732 switch (node->getOp()) {
2733 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
2734 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
2735 default: binOp = node->getOp(); break;
2736 }
2737
2738 break;
2739 }
2740 case glslang::EOpMul:
John Kessenich8c8505c2016-07-26 12:50:38 -06002741 // component-wise matrix multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002742 binOp = glslang::EOpMul;
2743 break;
2744 case glslang::EOpOuterProduct:
2745 // two vectors multiplied to make a matrix
2746 binOp = glslang::EOpOuterProduct;
2747 break;
2748 case glslang::EOpDot:
2749 {
qining25262b32016-05-06 17:25:16 -04002750 // for scalar dot product, use multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002751 glslang::TIntermSequence& glslangOperands = node->getSequence();
John Kessenich8d72f1a2016-05-20 12:06:03 -06002752 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
John Kessenich140f3df2015-06-26 16:58:36 -06002753 binOp = glslang::EOpMul;
2754 break;
2755 }
2756 case glslang::EOpMod:
2757 // when an aggregate, this is the floating-point mod built-in function,
2758 // which can be emitted by the one in createBinaryOperation()
2759 binOp = glslang::EOpMod;
2760 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002761
John Kessenich140f3df2015-06-26 16:58:36 -06002762 case glslang::EOpEmitVertex:
2763 case glslang::EOpEndPrimitive:
2764 case glslang::EOpBarrier:
2765 case glslang::EOpMemoryBarrier:
2766 case glslang::EOpMemoryBarrierAtomicCounter:
2767 case glslang::EOpMemoryBarrierBuffer:
2768 case glslang::EOpMemoryBarrierImage:
2769 case glslang::EOpMemoryBarrierShared:
2770 case glslang::EOpGroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07002771 case glslang::EOpDeviceMemoryBarrier:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002772 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07002773 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002774 case glslang::EOpWorkgroupMemoryBarrier:
2775 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich66011cb2018-03-06 16:12:04 -07002776 case glslang::EOpSubgroupBarrier:
2777 case glslang::EOpSubgroupMemoryBarrier:
2778 case glslang::EOpSubgroupMemoryBarrierBuffer:
2779 case glslang::EOpSubgroupMemoryBarrierImage:
2780 case glslang::EOpSubgroupMemoryBarrierShared:
John Kessenich140f3df2015-06-26 16:58:36 -06002781 noReturnValue = true;
2782 // These all have 0 operands and will naturally finish up in the code below for 0 operands
2783 break;
2784
John Kessenich426394d2015-07-23 10:22:48 -06002785 case glslang::EOpAtomicAdd:
2786 case glslang::EOpAtomicMin:
2787 case glslang::EOpAtomicMax:
2788 case glslang::EOpAtomicAnd:
2789 case glslang::EOpAtomicOr:
2790 case glslang::EOpAtomicXor:
2791 case glslang::EOpAtomicExchange:
2792 case glslang::EOpAtomicCompSwap:
2793 atomic = true;
2794 break;
2795
John Kesseniche5eee8f2019-10-18 01:03:11 -06002796#ifndef GLSLANG_WEB
2797 case glslang::EOpAtomicStore:
2798 noReturnValue = true;
2799 // fallthrough
2800 case glslang::EOpAtomicLoad:
2801 atomic = true;
2802 break;
2803
John Kessenich0d0c6d32017-07-23 16:08:26 -06002804 case glslang::EOpAtomicCounterAdd:
2805 case glslang::EOpAtomicCounterSubtract:
2806 case glslang::EOpAtomicCounterMin:
2807 case glslang::EOpAtomicCounterMax:
2808 case glslang::EOpAtomicCounterAnd:
2809 case glslang::EOpAtomicCounterOr:
2810 case glslang::EOpAtomicCounterXor:
2811 case glslang::EOpAtomicCounterExchange:
2812 case glslang::EOpAtomicCounterCompSwap:
2813 builder.addExtension("SPV_KHR_shader_atomic_counter_ops");
2814 builder.addCapability(spv::CapabilityAtomicStorageOps);
2815 atomic = true;
2816 break;
2817
Ian Romanickb3bd4022019-01-21 08:57:25 -08002818 case glslang::EOpAbsDifference:
2819 case glslang::EOpAddSaturate:
2820 case glslang::EOpSubSaturate:
2821 case glslang::EOpAverage:
2822 case glslang::EOpAverageRounded:
2823 case glslang::EOpMul32x16:
2824 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
2825 builder.addExtension("SPV_INTEL_shader_integer_functions2");
2826 binOp = node->getOp();
2827 break;
2828
Daniel Kochffccefd2020-11-23 15:41:27 -05002829 case glslang::EOpIgnoreIntersectionNV:
2830 case glslang::EOpTerminateRayNV:
2831 case glslang::EOpTraceNV:
2832 case glslang::EOpTraceKHR:
2833 case glslang::EOpExecuteCallableNV:
2834 case glslang::EOpExecuteCallableKHR:
Chao Chen3c366992018-09-19 11:41:59 -07002835 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
2836 noReturnValue = true;
2837 break;
Torosdagli06c2eee2020-03-19 11:09:57 -04002838 case glslang::EOpRayQueryInitialize:
2839 case glslang::EOpRayQueryTerminate:
2840 case glslang::EOpRayQueryGenerateIntersection:
2841 case glslang::EOpRayQueryConfirmIntersection:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002842 builder.addExtension("SPV_KHR_ray_query");
Daniel Kochffccefd2020-11-23 15:41:27 -05002843 builder.addCapability(spv::CapabilityRayQueryKHR);
Torosdagli06c2eee2020-03-19 11:09:57 -04002844 noReturnValue = true;
2845 break;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002846 case glslang::EOpRayQueryProceed:
2847 case glslang::EOpRayQueryGetIntersectionType:
2848 case glslang::EOpRayQueryGetRayTMin:
2849 case glslang::EOpRayQueryGetRayFlags:
2850 case glslang::EOpRayQueryGetIntersectionT:
2851 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
2852 case glslang::EOpRayQueryGetIntersectionInstanceId:
2853 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
2854 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
2855 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
2856 case glslang::EOpRayQueryGetIntersectionBarycentrics:
2857 case glslang::EOpRayQueryGetIntersectionFrontFace:
2858 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
2859 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
2860 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
2861 case glslang::EOpRayQueryGetWorldRayDirection:
2862 case glslang::EOpRayQueryGetWorldRayOrigin:
2863 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
2864 case glslang::EOpRayQueryGetIntersectionWorldToObject:
2865 builder.addExtension("SPV_KHR_ray_query");
Daniel Kochffccefd2020-11-23 15:41:27 -05002866 builder.addCapability(spv::CapabilityRayQueryKHR);
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002867 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002868 case glslang::EOpCooperativeMatrixLoad:
2869 case glslang::EOpCooperativeMatrixStore:
2870 noReturnValue = true;
2871 break;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05002872 case glslang::EOpBeginInvocationInterlock:
2873 case glslang::EOpEndInvocationInterlock:
2874 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
2875 noReturnValue = true;
2876 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002877#endif
Chao Chen3c366992018-09-19 11:41:59 -07002878
Jeff Bolz04d73732019-05-31 13:06:01 -05002879 case glslang::EOpDebugPrintf:
2880 noReturnValue = true;
2881 break;
2882
John Kessenich140f3df2015-06-26 16:58:36 -06002883 default:
2884 break;
2885 }
2886
2887 //
2888 // See if it maps to a regular operation.
2889 //
John Kessenich140f3df2015-06-26 16:58:36 -06002890 if (binOp != glslang::EOpNull) {
2891 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
2892 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
2893 assert(left && right);
2894
2895 builder.clearAccessChain();
2896 left->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002897 spv::Id leftId = accessChainLoad(left->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002898
2899 builder.clearAccessChain();
2900 right->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002901 spv::Id rightId = accessChainLoad(right->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002902
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002903 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichead86222018-03-28 18:01:20 -06002904 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002905 TranslateNoContractionDecoration(node->getType().getQualifier()),
2906 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002907 result = createBinaryOperation(binOp, decorations,
John Kessenich8c8505c2016-07-26 12:50:38 -06002908 resultType(), leftId, rightId,
John Kessenich140f3df2015-06-26 16:58:36 -06002909 left->getType().getBasicType(), reduceComparison);
2910
2911 // code above should only make binOp that exists in createBinaryOperation
John Kessenich55e7d112015-11-15 21:33:39 -07002912 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002913 builder.clearAccessChain();
2914 builder.setAccessChainRValue(result);
2915
2916 return false;
2917 }
2918
John Kessenich426394d2015-07-23 10:22:48 -06002919 //
2920 // Create the list of operands.
2921 //
John Kessenich140f3df2015-06-26 16:58:36 -06002922 glslang::TIntermSequence& glslangOperands = node->getSequence();
2923 std::vector<spv::Id> operands;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002924 std::vector<spv::IdImmediate> memoryAccessOperands;
John Kessenich140f3df2015-06-26 16:58:36 -06002925 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
John Kessenich140f3df2015-06-26 16:58:36 -06002926 // special case l-value operands; there are just a few
2927 bool lvalue = false;
2928 switch (node->getOp()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002929 case glslang::EOpModf:
2930 if (arg == 1)
2931 lvalue = true;
2932 break;
John Kesseniche5eee8f2019-10-18 01:03:11 -06002933
Torosdagli06c2eee2020-03-19 11:09:57 -04002934 case glslang::EOpRayQueryInitialize:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002935 case glslang::EOpRayQueryTerminate:
2936 case glslang::EOpRayQueryConfirmIntersection:
2937 case glslang::EOpRayQueryProceed:
Torosdagli06c2eee2020-03-19 11:09:57 -04002938 case glslang::EOpRayQueryGenerateIntersection:
2939 case glslang::EOpRayQueryGetIntersectionType:
2940 case glslang::EOpRayQueryGetIntersectionT:
2941 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
2942 case glslang::EOpRayQueryGetIntersectionInstanceId:
2943 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
2944 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
2945 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
2946 case glslang::EOpRayQueryGetIntersectionBarycentrics:
2947 case glslang::EOpRayQueryGetIntersectionFrontFace:
2948 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
2949 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
2950 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
2951 case glslang::EOpRayQueryGetIntersectionWorldToObject:
2952 if (arg == 0)
2953 lvalue = true;
2954 break;
2955
John Kesseniche5eee8f2019-10-18 01:03:11 -06002956 case glslang::EOpAtomicAdd:
2957 case glslang::EOpAtomicMin:
2958 case glslang::EOpAtomicMax:
2959 case glslang::EOpAtomicAnd:
2960 case glslang::EOpAtomicOr:
2961 case glslang::EOpAtomicXor:
2962 case glslang::EOpAtomicExchange:
2963 case glslang::EOpAtomicCompSwap:
2964 if (arg == 0)
2965 lvalue = true;
2966 break;
2967
John Kessenicha28f7a72019-08-06 07:00:58 -06002968#ifndef GLSLANG_WEB
2969 case glslang::EOpFrexp:
2970 if (arg == 1)
2971 lvalue = true;
2972 break;
Rex Xu7a26c172015-12-08 17:12:09 +08002973 case glslang::EOpInterpolateAtSample:
2974 case glslang::EOpInterpolateAtOffset:
Rex Xu9d93a232016-05-05 12:30:44 +08002975 case glslang::EOpInterpolateAtVertex:
John Kessenich8c8505c2016-07-26 12:50:38 -06002976 if (arg == 0) {
Rex Xu7a26c172015-12-08 17:12:09 +08002977 lvalue = true;
John Kessenich8c8505c2016-07-26 12:50:38 -06002978
2979 // Does it need a swizzle inversion? If so, evaluation is inverted;
2980 // operate first on the swizzle base, then apply the swizzle.
John Kessenichbbbd9a22020-03-03 07:21:37 -07002981 // That is, we transform
2982 //
2983 // interpolate(v.zy) -> interpolate(v).zy
2984 //
John Kessenichecba76f2017-01-06 00:34:48 -07002985 if (glslangOperands[0]->getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002986 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
John Kessenich8985fc92020-03-03 10:25:07 -07002987 invertedType = convertGlslangToSpvType(
2988 glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
John Kessenich8c8505c2016-07-26 12:50:38 -06002989 }
Rex Xu7a26c172015-12-08 17:12:09 +08002990 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05002991 case glslang::EOpAtomicLoad:
2992 case glslang::EOpAtomicStore:
John Kessenich0d0c6d32017-07-23 16:08:26 -06002993 case glslang::EOpAtomicCounterAdd:
2994 case glslang::EOpAtomicCounterSubtract:
2995 case glslang::EOpAtomicCounterMin:
2996 case glslang::EOpAtomicCounterMax:
2997 case glslang::EOpAtomicCounterAnd:
2998 case glslang::EOpAtomicCounterOr:
2999 case glslang::EOpAtomicCounterXor:
3000 case glslang::EOpAtomicCounterExchange:
3001 case glslang::EOpAtomicCounterCompSwap:
Rex Xud4782c12015-09-06 16:30:11 +08003002 if (arg == 0)
3003 lvalue = true;
3004 break;
John Kessenich55e7d112015-11-15 21:33:39 -07003005 case glslang::EOpAddCarry:
3006 case glslang::EOpSubBorrow:
3007 if (arg == 2)
3008 lvalue = true;
3009 break;
3010 case glslang::EOpUMulExtended:
3011 case glslang::EOpIMulExtended:
3012 if (arg >= 2)
3013 lvalue = true;
3014 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003015 case glslang::EOpCooperativeMatrixLoad:
3016 if (arg == 0 || arg == 1)
3017 lvalue = true;
3018 break;
3019 case glslang::EOpCooperativeMatrixStore:
3020 if (arg == 1)
3021 lvalue = true;
3022 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003023#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003024 default:
3025 break;
3026 }
John Kessenich8c8505c2016-07-26 12:50:38 -06003027 builder.clearAccessChain();
3028 if (invertedType != spv::NoType && arg == 0)
3029 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
3030 else
3031 glslangOperands[arg]->traverse(this);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003032
John Kessenichb9197c82019-08-11 07:41:45 -06003033#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003034 if (node->getOp() == glslang::EOpCooperativeMatrixLoad ||
3035 node->getOp() == glslang::EOpCooperativeMatrixStore) {
3036
3037 if (arg == 1) {
3038 // fold "element" parameter into the access chain
3039 spv::Builder::AccessChain save = builder.getAccessChain();
3040 builder.clearAccessChain();
3041 glslangOperands[2]->traverse(this);
3042
3043 spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType());
3044
3045 builder.setAccessChain(save);
3046
3047 // Point to the first element of the array.
John Kessenich8985fc92020-03-03 10:25:07 -07003048 builder.accessChainPush(elementId,
3049 TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()),
3050 glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment());
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003051
3052 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3053 unsigned int alignment = builder.getAccessChain().alignment;
3054
3055 int memoryAccess = TranslateMemoryAccess(coherentFlags);
3056 if (node->getOp() == glslang::EOpCooperativeMatrixLoad)
3057 memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask;
3058 if (node->getOp() == glslang::EOpCooperativeMatrixStore)
3059 memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask;
John Kessenich8985fc92020-03-03 10:25:07 -07003060 if (builder.getStorageClass(builder.getAccessChain().base) ==
3061 spv::StorageClassPhysicalStorageBufferEXT) {
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003062 memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
3063 }
3064
3065 memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess));
3066
3067 if (memoryAccess & spv::MemoryAccessAlignedMask) {
3068 memoryAccessOperands.push_back(spv::IdImmediate(false, alignment));
3069 }
3070
John Kessenich8985fc92020-03-03 10:25:07 -07003071 if (memoryAccess &
3072 (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) {
3073 memoryAccessOperands.push_back(spv::IdImmediate(true,
3074 builder.makeUintConstant(TranslateMemoryScope(coherentFlags))));
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003075 }
3076 } else if (arg == 2) {
3077 continue;
3078 }
3079 }
John Kessenichb9197c82019-08-11 07:41:45 -06003080#endif
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003081
John Kessenichbbbd9a22020-03-03 07:21:37 -07003082 // for l-values, pass the address, for r-values, pass the value
Jeff Bolz38a52fc2019-06-14 09:56:28 -05003083 if (lvalue) {
John Kessenichbbbd9a22020-03-03 07:21:37 -07003084 if (invertedType == spv::NoType && !builder.isSpvLvalue()) {
3085 // SPIR-V cannot represent an l-value containing a swizzle that doesn't
3086 // reduce to a simple access chain. So, we need a temporary vector to
3087 // receive the result, and must later swizzle that into the original
3088 // l-value.
Cody Northrop4d2298b2020-04-13 21:59:49 -06003089 complexLvalues.push_back(builder.getAccessChain());
John Kessenich435dd802020-06-30 01:27:08 -06003090 temporaryLvalues.push_back(builder.createVariable(
3091 spv::NoPrecision, spv::StorageClassFunction,
Cody Northrop4d2298b2020-04-13 21:59:49 -06003092 builder.accessChainGetInferredType(), "swizzleTemp"));
3093 operands.push_back(temporaryLvalues.back());
John Kessenichbbbd9a22020-03-03 07:21:37 -07003094 } else {
3095 operands.push_back(builder.accessChainGetLValue());
3096 }
Jeff Bolz38a52fc2019-06-14 09:56:28 -05003097 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
3098 lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType());
3099 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003100 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Torosdagli06c2eee2020-03-19 11:09:57 -04003101 glslang::TOperator glslangOp = node->getOp();
3102 if (arg == 1 &&
3103 (glslangOp == glslang::EOpRayQueryGetIntersectionType ||
3104 glslangOp == glslang::EOpRayQueryGetIntersectionT ||
3105 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex ||
3106 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId ||
3107 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset ||
3108 glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex ||
3109 glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex ||
3110 glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics ||
3111 glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace ||
3112 glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection ||
3113 glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin ||
3114 glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld ||
3115 glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject
3116 )) {
3117 bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst();
3118 operands.push_back(builder.makeIntConstant(cond ? 1 : 0));
Daniel Kochffccefd2020-11-23 15:41:27 -05003119 } else if ((arg == 10 && glslangOp == glslang::EOpTraceKHR) ||
3120 (arg == 1 && glslangOp == glslang::EOpExecuteCallableKHR)) {
3121 const int opdNum = glslangOp == glslang::EOpTraceKHR ? 10 : 1;
3122 const int set = glslangOp == glslang::EOpTraceKHR ? 0 : 1;
3123 const int location = glslangOperands[opdNum]->getAsConstantUnion()->getConstArray()[0].getUConst();
3124 auto itNode = locationToSymbol[set].find(location);
3125 visitSymbol(itNode->second);
3126 spv::Id symId = getSymbolId(itNode->second);
3127 operands.push_back(symId);
3128 } else {
Torosdagli06c2eee2020-03-19 11:09:57 -04003129 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
Daniel Kochffccefd2020-11-23 15:41:27 -05003130 }
John Kesseniche485c7a2017-05-31 18:50:53 -06003131 }
John Kessenich140f3df2015-06-26 16:58:36 -06003132 }
John Kessenich426394d2015-07-23 10:22:48 -06003133
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003134 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichb9197c82019-08-11 07:41:45 -06003135#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003136 if (node->getOp() == glslang::EOpCooperativeMatrixLoad) {
3137 std::vector<spv::IdImmediate> idImmOps;
3138
3139 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
3140 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
3141 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
3142 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
3143 // get the pointee type
3144 spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0]));
3145 assert(builder.isCooperativeMatrixType(typeId));
3146 // do the op
3147 spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps);
3148 // store the result to the pointer (out param 'm')
3149 builder.createStore(result, operands[0]);
3150 result = 0;
3151 } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) {
3152 std::vector<spv::IdImmediate> idImmOps;
3153
3154 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
3155 idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object
3156 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
3157 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
3158 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
3159
3160 builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps);
3161 result = 0;
John Kessenichb9197c82019-08-11 07:41:45 -06003162 } else
3163#endif
John Kesseniche5eee8f2019-10-18 01:03:11 -06003164 if (atomic) {
3165 // Handle all atomics
John Kessenich8985fc92020-03-03 10:25:07 -07003166 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(),
3167 lvalueCoherentFlags);
Jeff Bolz04d73732019-05-31 13:06:01 -05003168 } else if (node->getOp() == glslang::EOpDebugPrintf) {
3169 if (!nonSemanticDebugPrintf) {
3170 nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf");
3171 }
3172 result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands);
3173 builder.addExtension(spv::E_SPV_KHR_non_semantic_info);
John Kesseniche5eee8f2019-10-18 01:03:11 -06003174 } else {
John Kessenich426394d2015-07-23 10:22:48 -06003175 // Pass through to generic operations.
3176 switch (glslangOperands.size()) {
3177 case 0:
John Kessenich8c8505c2016-07-26 12:50:38 -06003178 result = createNoArgOperation(node->getOp(), precision, resultType());
John Kessenich426394d2015-07-23 10:22:48 -06003179 break;
3180 case 1:
John Kessenichead86222018-03-28 18:01:20 -06003181 {
3182 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06003183 TranslateNoContractionDecoration(node->getType().getQualifier()),
3184 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06003185 result = createUnaryOperation(
3186 node->getOp(), decorations,
3187 resultType(), operands.front(),
Jeff Bolz38a52fc2019-06-14 09:56:28 -05003188 glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -06003189 }
John Kessenich426394d2015-07-23 10:22:48 -06003190 break;
3191 default:
John Kessenich8c8505c2016-07-26 12:50:38 -06003192 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06003193 break;
3194 }
Cody Northrop4d2298b2020-04-13 21:59:49 -06003195
John Kessenichbbbd9a22020-03-03 07:21:37 -07003196 if (invertedType != spv::NoResult)
John Kessenich8c8505c2016-07-26 12:50:38 -06003197 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
Cody Northrop4d2298b2020-04-13 21:59:49 -06003198
3199 for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) {
3200 builder.setAccessChain(complexLvalues[i]);
greg-lunarg639f5462020-11-12 11:10:07 -07003201 builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision),
3202 TranslateNonUniformDecoration(complexLvalues[i].coherentFlags));
John Kessenichbbbd9a22020-03-03 07:21:37 -07003203 }
John Kessenich140f3df2015-06-26 16:58:36 -06003204 }
3205
3206 if (noReturnValue)
3207 return false;
3208
3209 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04003210 logger->missingFunctionality("unknown glslang aggregate");
John Kessenich50e57562015-12-21 21:21:11 -07003211 return true; // pick up a child as a placeholder operand
John Kessenich140f3df2015-06-26 16:58:36 -06003212 } else {
3213 builder.clearAccessChain();
3214 builder.setAccessChainRValue(result);
3215 return false;
3216 }
3217}
3218
John Kessenich433e9ff2017-01-26 20:31:11 -07003219// This path handles both if-then-else and ?:
3220// The if-then-else has a node type of void, while
3221// ?: has either a void or a non-void node type
3222//
3223// Leaving the result, when not void:
3224// GLSL only has r-values as the result of a :?, but
3225// if we have an l-value, that can be more efficient if it will
3226// become the base of a complex r-value expression, because the
3227// next layer copies r-values into memory to use the access-chain mechanism
John Kessenich140f3df2015-06-26 16:58:36 -06003228bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
3229{
John Kessenich0c1e71a2019-01-10 18:23:06 +07003230 // see if OpSelect can handle it
3231 const auto isOpSelectable = [&]() {
3232 if (node->getBasicType() == glslang::EbtVoid)
3233 return false;
3234 // OpSelect can do all other types starting with SPV 1.4
3235 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) {
3236 // pre-1.4, only scalars and vectors can be handled
3237 if ((!node->getType().isScalar() && !node->getType().isVector()))
3238 return false;
3239 }
3240 return true;
3241 };
3242
John Kessenich4bee5312018-02-20 21:29:05 -07003243 // See if it simple and safe, or required, to execute both sides.
3244 // Crucially, side effects must be either semantically required or avoided,
3245 // and there are performance trade-offs.
3246 // Return true if required or a good idea (and safe) to execute both sides,
3247 // false otherwise.
3248 const auto bothSidesPolicy = [&]() -> bool {
3249 // do we have both sides?
John Kessenich433e9ff2017-01-26 20:31:11 -07003250 if (node->getTrueBlock() == nullptr ||
3251 node->getFalseBlock() == nullptr)
3252 return false;
3253
John Kessenich4bee5312018-02-20 21:29:05 -07003254 // required? (unless we write additional code to look for side effects
3255 // and make performance trade-offs if none are present)
3256 if (!node->getShortCircuit())
3257 return true;
3258
3259 // if not required to execute both, decide based on performance/practicality...
3260
John Kessenich0c1e71a2019-01-10 18:23:06 +07003261 if (!isOpSelectable())
John Kessenich4bee5312018-02-20 21:29:05 -07003262 return false;
3263
John Kessenich433e9ff2017-01-26 20:31:11 -07003264 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
3265 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
3266
3267 // return true if a single operand to ? : is okay for OpSelect
3268 const auto operandOkay = [](glslang::TIntermTyped* node) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07003269 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
John Kessenich433e9ff2017-01-26 20:31:11 -07003270 };
3271
3272 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
3273 operandOkay(node->getFalseBlock()->getAsTyped());
3274 };
3275
John Kessenich4bee5312018-02-20 21:29:05 -07003276 spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue
3277 // emit the condition before doing anything with selection
3278 node->getCondition()->traverse(this);
3279 spv::Id condition = accessChainLoad(node->getCondition()->getType());
3280
3281 // Find a way of executing both sides and selecting the right result.
3282 const auto executeBothSides = [&]() -> void {
3283 // execute both sides
John Kessenich433e9ff2017-01-26 20:31:11 -07003284 node->getTrueBlock()->traverse(this);
3285 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
3286 node->getFalseBlock()->traverse(this);
3287 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
3288
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003289 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06003290
John Kessenich4bee5312018-02-20 21:29:05 -07003291 // done if void
3292 if (node->getBasicType() == glslang::EbtVoid)
3293 return;
John Kesseniche434ad92017-03-30 10:09:28 -06003294
John Kessenich4bee5312018-02-20 21:29:05 -07003295 // emit code to select between trueValue and falseValue
3296
3297 // see if OpSelect can handle it
John Kessenich0c1e71a2019-01-10 18:23:06 +07003298 if (isOpSelectable()) {
John Kessenich4bee5312018-02-20 21:29:05 -07003299 // Emit OpSelect for this selection.
3300
3301 // smear condition to vector, if necessary (AST is always scalar)
John Kessenich0c1e71a2019-01-10 18:23:06 +07003302 // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar
3303 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) {
John Kessenich4bee5312018-02-20 21:29:05 -07003304 condition = builder.smearScalar(spv::NoPrecision, condition,
3305 builder.makeVectorType(builder.makeBoolType(),
3306 builder.getNumComponents(trueValue)));
John Kessenich0c1e71a2019-01-10 18:23:06 +07003307 }
John Kessenich4bee5312018-02-20 21:29:05 -07003308
3309 // OpSelect
3310 result = builder.createTriOp(spv::OpSelect,
3311 convertGlslangToSpvType(node->getType()), condition,
3312 trueValue, falseValue);
3313
3314 builder.clearAccessChain();
3315 builder.setAccessChainRValue(result);
3316 } else {
3317 // We need control flow to select the result.
3318 // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path.
John Kessenich435dd802020-06-30 01:27:08 -06003319 result = builder.createVariable(TranslatePrecisionDecoration(node->getType()),
3320 spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
John Kessenich4bee5312018-02-20 21:29:05 -07003321
3322 // Selection control:
3323 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
3324
3325 // make an "if" based on the value created by the condition
3326 spv::Builder::If ifBuilder(condition, control, builder);
3327
3328 // emit the "then" statement
3329 builder.createStore(trueValue, result);
3330 ifBuilder.makeBeginElse();
3331 // emit the "else" statement
3332 builder.createStore(falseValue, result);
3333
3334 // finish off the control flow
3335 ifBuilder.makeEndIf();
3336
3337 builder.clearAccessChain();
3338 builder.setAccessChainLValue(result);
3339 }
John Kessenich433e9ff2017-01-26 20:31:11 -07003340 };
3341
John Kessenich4bee5312018-02-20 21:29:05 -07003342 // Execute the one side needed, as per the condition
3343 const auto executeOneSide = [&]() {
3344 // Always emit control flow.
John Kessenich435dd802020-06-30 01:27:08 -06003345 if (node->getBasicType() != glslang::EbtVoid) {
3346 result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction,
3347 convertGlslangToSpvType(node->getType()));
3348 }
John Kessenich433e9ff2017-01-26 20:31:11 -07003349
John Kessenich4bee5312018-02-20 21:29:05 -07003350 // Selection control:
3351 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
3352
3353 // make an "if" based on the value created by the condition
3354 spv::Builder::If ifBuilder(condition, control, builder);
3355
3356 // emit the "then" statement
3357 if (node->getTrueBlock() != nullptr) {
3358 node->getTrueBlock()->traverse(this);
3359 if (result != spv::NoResult)
3360 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
3361 }
3362
3363 if (node->getFalseBlock() != nullptr) {
3364 ifBuilder.makeBeginElse();
3365 // emit the "else" statement
3366 node->getFalseBlock()->traverse(this);
3367 if (result != spv::NoResult)
3368 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
3369 }
3370
3371 // finish off the control flow
3372 ifBuilder.makeEndIf();
3373
3374 if (result != spv::NoResult) {
3375 builder.clearAccessChain();
3376 builder.setAccessChainLValue(result);
3377 }
3378 };
3379
3380 // Try for OpSelect (or a requirement to execute both sides)
3381 if (bothSidesPolicy()) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07003382 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
3383 if (node->getType().getQualifier().isSpecConstant())
3384 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
John Kessenich4bee5312018-02-20 21:29:05 -07003385 executeBothSides();
3386 } else
3387 executeOneSide();
John Kessenich140f3df2015-06-26 16:58:36 -06003388
3389 return false;
3390}
3391
3392bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
3393{
3394 // emit and get the condition before doing anything with switch
3395 node->getCondition()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07003396 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06003397
Rex Xu57e65922017-07-04 23:23:40 +08003398 // Selection control:
John Kesseniche18fd202018-01-30 11:01:39 -07003399 const spv::SelectionControlMask control = TranslateSwitchControl(*node);
Rex Xu57e65922017-07-04 23:23:40 +08003400
John Kessenich140f3df2015-06-26 16:58:36 -06003401 // browse the children to sort out code segments
3402 int defaultSegment = -1;
3403 std::vector<TIntermNode*> codeSegments;
3404 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
3405 std::vector<int> caseValues;
3406 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
3407 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
3408 TIntermNode* child = *c;
3409 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
baldurkd76692d2015-07-12 11:32:58 +02003410 defaultSegment = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06003411 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
baldurkd76692d2015-07-12 11:32:58 +02003412 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
John Kessenich8985fc92020-03-03 10:25:07 -07003413 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()
3414 ->getConstArray()[0].getIConst());
John Kessenich140f3df2015-06-26 16:58:36 -06003415 } else
3416 codeSegments.push_back(child);
3417 }
3418
qining25262b32016-05-06 17:25:16 -04003419 // handle the case where the last code segment is missing, due to no code
John Kessenich140f3df2015-06-26 16:58:36 -06003420 // statements between the last case and the end of the switch statement
3421 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
3422 (int)codeSegments.size() == defaultSegment)
3423 codeSegments.push_back(nullptr);
3424
3425 // make the switch statement
3426 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
John Kessenich8985fc92020-03-03 10:25:07 -07003427 builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment,
3428 segmentBlocks);
John Kessenich140f3df2015-06-26 16:58:36 -06003429
3430 // emit all the code in the segments
3431 breakForLoop.push(false);
3432 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
3433 builder.nextSwitchSegment(segmentBlocks, s);
3434 if (codeSegments[s])
3435 codeSegments[s]->traverse(this);
3436 else
3437 builder.addSwitchBreak();
3438 }
3439 breakForLoop.pop();
3440
3441 builder.endSwitch(segmentBlocks);
3442
3443 return false;
3444}
3445
3446void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
3447{
3448 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04003449 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
John Kessenich140f3df2015-06-26 16:58:36 -06003450
3451 builder.clearAccessChain();
3452 builder.setAccessChainRValue(constant);
3453}
3454
3455bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
3456{
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003457 auto blocks = builder.makeNewLoop();
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003458 builder.createBranch(&blocks.head);
steve-lunargf1709e72017-05-02 20:14:50 -06003459
3460 // Loop control:
John Kessenich1f4d0462019-01-12 17:31:41 +07003461 std::vector<unsigned int> operands;
3462 const spv::LoopControlMask control = TranslateLoopControl(*node, operands);
steve-lunargf1709e72017-05-02 20:14:50 -06003463
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003464 // Spec requires back edges to target header blocks, and every header block
3465 // must dominate its merge block. Make a header block first to ensure these
3466 // conditions are met. By definition, it will contain OpLoopMerge, followed
3467 // by a block-ending branch. But we don't want to put any other body/test
3468 // instructions in it, since the body/test may have arbitrary instructions,
3469 // including merges of its own.
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003470 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003471 builder.setBuildPoint(&blocks.head);
John Kessenich1f4d0462019-01-12 17:31:41 +07003472 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003473 if (node->testFirst() && node->getTest()) {
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003474 spv::Block& test = builder.makeNewBlock();
3475 builder.createBranch(&test);
3476
3477 builder.setBuildPoint(&test);
John Kessenich140f3df2015-06-26 16:58:36 -06003478 node->getTest()->traverse(this);
John Kesseniche485c7a2017-05-31 18:50:53 -06003479 spv::Id condition = accessChainLoad(node->getTest()->getType());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003480 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
3481
3482 builder.setBuildPoint(&blocks.body);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003483 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003484 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003485 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003486 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003487 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003488
3489 builder.setBuildPoint(&blocks.continue_target);
3490 if (node->getTerminal())
3491 node->getTerminal()->traverse(this);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003492 builder.createBranch(&blocks.head);
David Netoc22f37c2015-07-15 16:21:26 -04003493 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003494 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003495 builder.createBranch(&blocks.body);
3496
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003497 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003498 builder.setBuildPoint(&blocks.body);
3499 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003500 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003501 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003502 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003503
3504 builder.setBuildPoint(&blocks.continue_target);
3505 if (node->getTerminal())
3506 node->getTerminal()->traverse(this);
3507 if (node->getTest()) {
3508 node->getTest()->traverse(this);
3509 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07003510 accessChainLoad(node->getTest()->getType());
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003511 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003512 } else {
Dejan Mircevskied55bcd2016-01-19 21:13:38 -05003513 // TODO: unless there was a break/return/discard instruction
3514 // somewhere in the body, this is an infinite loop, so we should
3515 // issue a warning.
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003516 builder.createBranch(&blocks.head);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003517 }
John Kessenich140f3df2015-06-26 16:58:36 -06003518 }
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003519 builder.setBuildPoint(&blocks.merge);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003520 builder.closeLoop();
John Kessenich140f3df2015-06-26 16:58:36 -06003521 return false;
3522}
3523
3524bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
3525{
3526 if (node->getExpression())
3527 node->getExpression()->traverse(this);
3528
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003529 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06003530
John Kessenich140f3df2015-06-26 16:58:36 -06003531 switch (node->getFlowOp()) {
3532 case glslang::EOpKill:
Daniel Kochffccefd2020-11-23 15:41:27 -05003533 builder.makeStatementTerminator(spv::OpKill, "post-discard");
John Kessenich140f3df2015-06-26 16:58:36 -06003534 break;
Jesse Hall74e8f052020-11-09 08:30:01 -08003535 case glslang::EOpTerminateInvocation:
3536 builder.addExtension(spv::E_SPV_KHR_terminate_invocation);
Daniel Kochffccefd2020-11-23 15:41:27 -05003537 builder.makeStatementTerminator(spv::OpTerminateInvocation, "post-terminate-invocation");
Jesse Hall74e8f052020-11-09 08:30:01 -08003538 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003539 case glslang::EOpBreak:
3540 if (breakForLoop.top())
3541 builder.createLoopExit();
3542 else
3543 builder.addSwitchBreak();
3544 break;
3545 case glslang::EOpContinue:
John Kessenich140f3df2015-06-26 16:58:36 -06003546 builder.createLoopContinue();
3547 break;
3548 case glslang::EOpReturn:
John Kessenich435dd802020-06-30 01:27:08 -06003549 if (node->getExpression() != nullptr) {
John Kesseniched33e052016-10-06 12:59:51 -06003550 const glslang::TType& glslangReturnType = node->getExpression()->getType();
3551 spv::Id returnId = accessChainLoad(glslangReturnType);
John Kessenich435dd802020-06-30 01:27:08 -06003552 if (builder.getTypeId(returnId) != currentFunction->getReturnType() ||
3553 TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) {
John Kesseniched33e052016-10-06 12:59:51 -06003554 builder.clearAccessChain();
John Kessenich435dd802020-06-30 01:27:08 -06003555 spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(),
3556 spv::StorageClassFunction, currentFunction->getReturnType());
John Kesseniched33e052016-10-06 12:59:51 -06003557 builder.setAccessChainLValue(copyId);
3558 multiTypeStore(glslangReturnType, returnId);
John Kessenich435dd802020-06-30 01:27:08 -06003559 returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision());
John Kesseniched33e052016-10-06 12:59:51 -06003560 }
3561 builder.makeReturn(false, returnId);
3562 } else
John Kesseniche770b3e2015-09-14 20:58:02 -06003563 builder.makeReturn(false);
John Kessenich140f3df2015-06-26 16:58:36 -06003564
3565 builder.clearAccessChain();
3566 break;
3567
John Kessenichb9197c82019-08-11 07:41:45 -06003568#ifndef GLSLANG_WEB
Jeff Bolzba6170b2019-07-01 09:23:23 -05003569 case glslang::EOpDemote:
3570 builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT);
3571 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
3572 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
3573 break;
Daniel Kochffccefd2020-11-23 15:41:27 -05003574 case glslang::EOpTerminateRayKHR:
3575 builder.makeStatementTerminator(spv::OpTerminateRayKHR, "post-terminateRayKHR");
3576 break;
3577 case glslang::EOpIgnoreIntersectionKHR:
3578 builder.makeStatementTerminator(spv::OpIgnoreIntersectionKHR, "post-ignoreIntersectionKHR");
3579 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003580#endif
Jeff Bolzba6170b2019-07-01 09:23:23 -05003581
John Kessenich140f3df2015-06-26 16:58:36 -06003582 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003583 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003584 break;
3585 }
3586
3587 return false;
3588}
3589
John Kessenich9c14f772019-06-17 08:38:35 -06003590spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType)
John Kessenich140f3df2015-06-26 16:58:36 -06003591{
qining25262b32016-05-06 17:25:16 -04003592 // First, steer off constants, which are not SPIR-V variables, but
John Kessenich140f3df2015-06-26 16:58:36 -06003593 // can still have a mapping to a SPIR-V Id.
John Kessenich55e7d112015-11-15 21:33:39 -07003594 // This includes specialization constants.
John Kessenich7cc0e282016-03-20 00:46:02 -06003595 if (node->getQualifier().isConstant()) {
Dan Sinclair12fcaa22018-11-13 09:17:44 -05003596 spv::Id result = createSpvConstant(*node);
3597 if (result != spv::NoResult)
3598 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06003599 }
3600
3601 // Now, handle actual variables
John Kessenicha5c5fb62017-05-05 05:09:58 -06003602 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
John Kessenich9c14f772019-06-17 08:38:35 -06003603 spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType())
3604 : forcedType;
John Kessenich140f3df2015-06-26 16:58:36 -06003605
John Kessenichb9197c82019-08-11 07:41:45 -06003606 const bool contains16BitType = node->getType().contains16BitFloat() ||
3607 node->getType().contains16BitInt();
Rex Xuf89ad982017-04-07 23:22:33 +08003608 if (contains16BitType) {
John Kessenich18310872018-05-14 22:08:53 -06003609 switch (storageClass) {
3610 case spv::StorageClassInput:
3611 case spv::StorageClassOutput:
John Kessenich8317e6c2019-08-18 23:58:08 -06003612 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003613 builder.addCapability(spv::CapabilityStorageInputOutput16);
John Kessenich18310872018-05-14 22:08:53 -06003614 break;
John Kessenich18310872018-05-14 22:08:53 -06003615 case spv::StorageClassUniform:
John Kessenich8317e6c2019-08-18 23:58:08 -06003616 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003617 if (node->getType().getQualifier().storage == glslang::EvqBuffer)
3618 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
John Kessenich18310872018-05-14 22:08:53 -06003619 else
3620 builder.addCapability(spv::CapabilityStorageUniform16);
3621 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003622#ifndef GLSLANG_WEB
3623 case spv::StorageClassPushConstant:
John Kessenich8317e6c2019-08-18 23:58:08 -06003624 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenichb9197c82019-08-11 07:41:45 -06003625 builder.addCapability(spv::CapabilityStoragePushConstant16);
3626 break;
John Kessenich18310872018-05-14 22:08:53 -06003627 case spv::StorageClassStorageBuffer:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003628 case spv::StorageClassPhysicalStorageBufferEXT:
John Kessenich8317e6c2019-08-18 23:58:08 -06003629 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenich18310872018-05-14 22:08:53 -06003630 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
3631 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003632#endif
John Kessenich18310872018-05-14 22:08:53 -06003633 default:
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -07003634 if (storageClass == spv::StorageClassWorkgroup &&
3635 node->getType().getBasicType() == glslang::EbtBlock) {
3636 builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout16BitAccessKHR);
3637 break;
3638 }
John Kessenichb9197c82019-08-11 07:41:45 -06003639 if (node->getType().contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003640 builder.addCapability(spv::CapabilityFloat16);
John Kessenichb9197c82019-08-11 07:41:45 -06003641 if (node->getType().contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003642 builder.addCapability(spv::CapabilityInt16);
John Kessenich18310872018-05-14 22:08:53 -06003643 break;
Rex Xuf89ad982017-04-07 23:22:33 +08003644 }
3645 }
Rex Xuf89ad982017-04-07 23:22:33 +08003646
John Kessenichb9197c82019-08-11 07:41:45 -06003647 if (node->getType().contains8BitInt()) {
John Kessenich312dcfb2018-07-03 13:19:51 -06003648 if (storageClass == spv::StorageClassPushConstant) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003649 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003650 builder.addCapability(spv::CapabilityStoragePushConstant8);
3651 } else if (storageClass == spv::StorageClassUniform) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003652 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003653 builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess);
Neil Henningb6b01f02018-10-23 15:02:29 +01003654 } else if (storageClass == spv::StorageClassStorageBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003655 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
Neil Henningb6b01f02018-10-23 15:02:29 +01003656 builder.addCapability(spv::CapabilityStorageBuffer8BitAccess);
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -07003657 } else if (storageClass == spv::StorageClassWorkgroup &&
3658 node->getType().getBasicType() == glslang::EbtBlock) {
3659 builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout8BitAccessKHR);
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003660 } else {
3661 builder.addCapability(spv::CapabilityInt8);
John Kessenich312dcfb2018-07-03 13:19:51 -06003662 }
3663 }
3664
John Kessenich140f3df2015-06-26 16:58:36 -06003665 const char* name = node->getName().c_str();
3666 if (glslang::IsAnonymous(name))
3667 name = "";
3668
Alejandro Piñeiroff6dcca2020-06-04 09:39:31 +02003669 spv::Id initializer = spv::NoResult;
3670
John Kessenichc739e032020-06-11 08:30:03 -06003671 if (node->getType().getQualifier().storage == glslang::EvqUniform && !node->getConstArray().empty()) {
3672 int nextConst = 0;
3673 initializer = createSpvConstantFromConstUnionArray(node->getType(),
3674 node->getConstArray(),
3675 nextConst,
3676 false /* specConst */);
3677 } else if (node->getType().getQualifier().isNullInit()) {
3678 initializer = builder.makeNullConstant(spvType);
Alejandro Piñeiroff6dcca2020-06-04 09:39:31 +02003679 }
3680
John Kessenich435dd802020-06-30 01:27:08 -06003681 return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer);
John Kessenich140f3df2015-06-26 16:58:36 -06003682}
3683
3684// Return type Id of the sampled type.
3685spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
3686{
3687 switch (sampler.type) {
John Kessenicha28f7a72019-08-06 07:00:58 -06003688 case glslang::EbtInt: return builder.makeIntType(32);
3689 case glslang::EbtUint: return builder.makeUintType(32);
John Kessenich140f3df2015-06-26 16:58:36 -06003690 case glslang::EbtFloat: return builder.makeFloatType(32);
John Kessenicha28f7a72019-08-06 07:00:58 -06003691#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08003692 case glslang::EbtFloat16:
3693 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch);
3694 builder.addCapability(spv::CapabilityFloat16ImageAMD);
3695 return builder.makeFloatType(16);
Tobski8c1a3a02020-11-04 16:24:23 +00003696 case glslang::EbtInt64: return builder.makeIntType(64);
3697 builder.addExtension(spv::E_SPV_EXT_shader_image_int64);
3698 builder.addCapability(spv::CapabilityFloat16ImageAMD);
3699 case glslang::EbtUint64: return builder.makeUintType(64);
3700 builder.addExtension(spv::E_SPV_EXT_shader_image_int64);
3701 builder.addCapability(spv::CapabilityFloat16ImageAMD);
Rex Xu1e5d7b02016-11-29 17:36:31 +08003702#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003703 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003704 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003705 return builder.makeFloatType(32);
3706 }
3707}
3708
John Kessenich8c8505c2016-07-26 12:50:38 -06003709// If node is a swizzle operation, return the type that should be used if
3710// the swizzle base is first consumed by another operation, before the swizzle
3711// is applied.
3712spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
3713{
John Kessenichecba76f2017-01-06 00:34:48 -07003714 if (node.getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06003715 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
3716 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
3717 else
3718 return spv::NoType;
3719}
3720
3721// When inverting a swizzle with a parent op, this function
3722// will apply the swizzle operation to a completed parent operation.
John Kessenich8985fc92020-03-03 10:25:07 -07003723spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node,
3724 spv::Id parentResult)
John Kessenich8c8505c2016-07-26 12:50:38 -06003725{
3726 std::vector<unsigned> swizzle;
3727 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
3728 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
3729}
3730
John Kessenich8c8505c2016-07-26 12:50:38 -06003731// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
3732void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
3733{
3734 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
3735 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
3736 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
3737}
3738
John Kessenich3ac051e2015-12-20 11:29:16 -07003739// Convert from a glslang type to an SPV type, by calling into a
3740// recursive version of this function. This establishes the inherited
3741// layout state rooted from the top-level type.
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003742spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
John Kessenich140f3df2015-06-26 16:58:36 -06003743{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003744 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
John Kessenich31ed4832015-09-09 17:51:38 -06003745}
3746
3747// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
John Kessenich7b9fa252016-01-21 18:56:57 -07003748// explicitLayout can be kept the same throughout the hierarchical recursive walk.
John Kessenich6090df02016-06-30 21:18:02 -06003749// Mutually recursive with convertGlslangStructToSpvType().
John Kessenichead86222018-03-28 18:01:20 -06003750spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003751 glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
3752 bool lastBufferBlockMember, bool forwardReferenceOnly)
John Kessenich31ed4832015-09-09 17:51:38 -06003753{
John Kesseniche0b6cad2015-12-24 10:30:13 -07003754 spv::Id spvType = spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06003755
3756 switch (type.getBasicType()) {
3757 case glslang::EbtVoid:
3758 spvType = builder.makeVoidType();
John Kessenich55e7d112015-11-15 21:33:39 -07003759 assert (! type.isArray());
John Kessenich140f3df2015-06-26 16:58:36 -06003760 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003761 case glslang::EbtBool:
John Kessenich103bef92016-02-08 21:38:15 -07003762 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
3763 // a 32-bit int where non-0 means true.
3764 if (explicitLayout != glslang::ElpNone)
3765 spvType = builder.makeUintType(32);
3766 else
3767 spvType = builder.makeBoolType();
John Kessenich140f3df2015-06-26 16:58:36 -06003768 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003769 case glslang::EbtInt:
3770 spvType = builder.makeIntType(32);
3771 break;
3772 case glslang::EbtUint:
3773 spvType = builder.makeUintType(32);
3774 break;
3775 case glslang::EbtFloat:
3776 spvType = builder.makeFloatType(32);
3777 break;
3778#ifndef GLSLANG_WEB
3779 case glslang::EbtDouble:
3780 spvType = builder.makeFloatType(64);
3781 break;
3782 case glslang::EbtFloat16:
3783 spvType = builder.makeFloatType(16);
3784 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003785 case glslang::EbtInt8:
John Kessenich66011cb2018-03-06 16:12:04 -07003786 spvType = builder.makeIntType(8);
3787 break;
3788 case glslang::EbtUint8:
John Kessenich66011cb2018-03-06 16:12:04 -07003789 spvType = builder.makeUintType(8);
3790 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003791 case glslang::EbtInt16:
John Kessenich66011cb2018-03-06 16:12:04 -07003792 spvType = builder.makeIntType(16);
3793 break;
3794 case glslang::EbtUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07003795 spvType = builder.makeUintType(16);
3796 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08003797 case glslang::EbtInt64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003798 spvType = builder.makeIntType(64);
3799 break;
3800 case glslang::EbtUint64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003801 spvType = builder.makeUintType(64);
3802 break;
John Kessenich426394d2015-07-23 10:22:48 -06003803 case glslang::EbtAtomicUint:
John Kessenich2d0cc782016-07-07 13:20:00 -06003804 builder.addCapability(spv::CapabilityAtomicStorage);
John Kessenich426394d2015-07-23 10:22:48 -06003805 spvType = builder.makeUintType(32);
3806 break;
Daniel Kochdb32b242020-03-17 20:42:47 -04003807 case glslang::EbtAccStruct:
Daniel Koch4d41da32020-11-24 23:06:16 -05003808 switch (glslangIntermediate->getStage()) {
3809 case EShLangRayGen:
3810 case EShLangIntersect:
3811 case EShLangAnyHit:
3812 case EShLangClosestHit:
3813 case EShLangMiss:
3814 case EShLangCallable:
3815 // these all should have the RayTracingNV/KHR capability already
3816 break;
3817 default:
3818 {
3819 auto& extensions = glslangIntermediate->getRequestedExtensions();
3820 if (extensions.find("GL_EXT_ray_query") != extensions.end()) {
3821 builder.addExtension(spv::E_SPV_KHR_ray_query);
3822 builder.addCapability(spv::CapabilityRayQueryKHR);
3823 }
3824 }
3825 break;
3826 }
Daniel Kochdb32b242020-03-17 20:42:47 -04003827 spvType = builder.makeAccelerationStructureType();
Chao Chenb50c02e2018-09-19 11:42:24 -07003828 break;
Torosdagli06c2eee2020-03-19 11:09:57 -04003829 case glslang::EbtRayQuery:
Daniel Koch4d41da32020-11-24 23:06:16 -05003830 {
3831 auto& extensions = glslangIntermediate->getRequestedExtensions();
3832 if (extensions.find("GL_EXT_ray_query") != extensions.end()) {
3833 builder.addExtension(spv::E_SPV_KHR_ray_query);
3834 builder.addCapability(spv::CapabilityRayQueryKHR);
3835 }
3836 spvType = builder.makeRayQueryType();
3837 }
Torosdagli06c2eee2020-03-19 11:09:57 -04003838 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003839 case glslang::EbtReference:
3840 {
3841 // Make the forward pointer, then recurse to convert the structure type, then
3842 // patch up the forward pointer with a real pointer type.
3843 if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) {
3844 spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT);
3845 forwardPointers[type.getReferentType()] = forwardId;
3846 }
3847 spvType = forwardPointers[type.getReferentType()];
3848 if (!forwardReferenceOnly) {
3849 spv::Id referentType = convertGlslangToSpvType(*type.getReferentType());
3850 builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT,
3851 forwardPointers[type.getReferentType()],
3852 referentType);
3853 }
3854 }
3855 break;
Chao Chenb50c02e2018-09-19 11:42:24 -07003856#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003857 case glslang::EbtSampler:
3858 {
3859 const glslang::TSampler& sampler = type.getSampler();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003860 if (sampler.isPureSampler()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003861 spvType = builder.makeSamplerType();
3862 } else {
3863 // an image is present, make its type
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003864 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler),
3865 sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(),
3866 sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type));
3867 if (sampler.isCombined()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003868 // already has both image and sampler, make the combined type
3869 spvType = builder.makeSampledImageType(spvType);
3870 }
John Kessenich55e7d112015-11-15 21:33:39 -07003871 }
John Kesseniche0b6cad2015-12-24 10:30:13 -07003872 }
John Kessenich140f3df2015-06-26 16:58:36 -06003873 break;
3874 case glslang::EbtStruct:
3875 case glslang::EbtBlock:
3876 {
3877 // If we've seen this struct type, return it
John Kessenich6090df02016-06-30 21:18:02 -06003878 const glslang::TTypeList* glslangMembers = type.getStruct();
John Kesseniche0b6cad2015-12-24 10:30:13 -07003879
3880 // Try to share structs for different layouts, but not yet for other
3881 // kinds of qualification (primarily not yet including interpolant qualification).
John Kessenichf2b7f332016-09-01 17:05:23 -06003882 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003883 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
John Kesseniche0b6cad2015-12-24 10:30:13 -07003884 if (spvType != spv::NoResult)
John Kessenich140f3df2015-06-26 16:58:36 -06003885 break;
3886
3887 // else, we haven't seen it...
John Kessenich140f3df2015-06-26 16:58:36 -06003888 if (type.getBasicType() == glslang::EbtBlock)
Roy05a5b532020-01-03 16:21:34 +08003889 memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size());
John Kessenich6090df02016-06-30 21:18:02 -06003890 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
John Kessenich140f3df2015-06-26 16:58:36 -06003891 }
3892 break;
Jeff Bolz04d73732019-05-31 13:06:01 -05003893 case glslang::EbtString:
3894 // no type used for OpString
3895 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06003896 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003897 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003898 break;
3899 }
3900
3901 if (type.isMatrix())
3902 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
3903 else {
3904 // If this variable has a vector element count greater than 1, create a SPIR-V vector
3905 if (type.getVectorSize() > 1)
3906 spvType = builder.makeVectorType(spvType, type.getVectorSize());
3907 }
3908
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003909 if (type.isCoopMat()) {
3910 builder.addCapability(spv::CapabilityCooperativeMatrixNV);
3911 builder.addExtension(spv::E_SPV_NV_cooperative_matrix);
3912 if (type.getBasicType() == glslang::EbtFloat16)
3913 builder.addCapability(spv::CapabilityFloat16);
Jeff Bolz387657e2019-08-22 20:28:00 -05003914 if (type.getBasicType() == glslang::EbtUint8 ||
3915 type.getBasicType() == glslang::EbtInt8) {
3916 builder.addCapability(spv::CapabilityInt8);
3917 }
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003918
3919 spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1);
3920 spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2);
3921 spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3);
3922
3923 spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols);
3924 }
3925
John Kessenich140f3df2015-06-26 16:58:36 -06003926 if (type.isArray()) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003927 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
3928
John Kessenichc9a80832015-09-12 12:17:44 -06003929 // Do all but the outer dimension
John Kessenichc9e0a422015-12-29 21:27:24 -07003930 if (type.getArraySizes()->getNumDims() > 1) {
John Kessenichf8842e52016-01-04 19:22:56 -07003931 // We need to decorate array strides for types needing explicit layout, except blocks.
3932 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003933 // Use a dummy glslang type for querying internal strides of
3934 // arrays of arrays, but using just a one-dimensional array.
3935 glslang::TType simpleArrayType(type, 0); // deference type of the array
John Kessenich859b0342018-03-26 00:38:53 -06003936 while (simpleArrayType.getArraySizes()->getNumDims() > 1)
3937 simpleArrayType.getArraySizes()->dereference();
John Kessenichc9e0a422015-12-29 21:27:24 -07003938
3939 // Will compute the higher-order strides here, rather than making a whole
3940 // pile of types and doing repetitive recursion on their contents.
3941 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
3942 }
John Kessenichf8842e52016-01-04 19:22:56 -07003943
3944 // make the arrays
John Kessenichc9e0a422015-12-29 21:27:24 -07003945 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
John Kessenich6c292d32016-02-15 20:58:50 -07003946 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
John Kessenichc9e0a422015-12-29 21:27:24 -07003947 if (stride > 0)
3948 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich6c292d32016-02-15 20:58:50 -07003949 stride *= type.getArraySizes()->getDimSize(dim);
John Kessenichc9e0a422015-12-29 21:27:24 -07003950 }
3951 } else {
3952 // single-dimensional array, and don't yet have stride
3953
John Kessenichf8842e52016-01-04 19:22:56 -07003954 // We need to decorate array strides for types needing explicit layout, except blocks.
John Kessenichc9e0a422015-12-29 21:27:24 -07003955 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
3956 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
John Kessenichc9a80832015-09-12 12:17:44 -06003957 }
John Kessenich31ed4832015-09-09 17:51:38 -06003958
John Kessenichead86222018-03-28 18:01:20 -06003959 // Do the outer dimension, which might not be known for a runtime-sized array.
3960 // (Unsized arrays that survive through linking will be runtime-sized arrays)
3961 if (type.isSizedArray())
John Kessenich6c292d32016-02-15 20:58:50 -07003962 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
John Kessenich5611c6d2018-04-05 11:25:02 -06003963 else {
John Kessenichb9197c82019-08-11 07:41:45 -06003964#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06003965 if (!lastBufferBlockMember) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003966 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06003967 builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT);
3968 }
John Kessenichb9197c82019-08-11 07:41:45 -06003969#endif
John Kessenich3dd1ce52019-10-17 07:08:40 -06003970 spvType = builder.makeRuntimeArray(spvType);
John Kessenich5611c6d2018-04-05 11:25:02 -06003971 }
John Kessenichc9e0a422015-12-29 21:27:24 -07003972 if (stride > 0)
3973 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich140f3df2015-06-26 16:58:36 -06003974 }
3975
3976 return spvType;
3977}
3978
John Kessenich0e737842017-03-24 18:38:16 -06003979// TODO: this functionality should exist at a higher level, in creating the AST
3980//
3981// Identify interface members that don't have their required extension turned on.
3982//
3983bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
3984{
John Kessenicha28f7a72019-08-06 07:00:58 -06003985#ifndef GLSLANG_WEB
John Kessenich0e737842017-03-24 18:38:16 -06003986 auto& extensions = glslangIntermediate->getRequestedExtensions();
3987
Rex Xubcf291a2017-03-29 23:01:36 +08003988 if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
3989 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3990 return true;
John Kessenich0e737842017-03-24 18:38:16 -06003991 if (member.getFieldName() == "gl_SecondaryPositionNV" &&
3992 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3993 return true;
Chao Chen3c366992018-09-19 11:41:59 -07003994
3995 if (glslangIntermediate->getStage() != EShLangMeshNV) {
3996 if (member.getFieldName() == "gl_ViewportMask" &&
3997 extensions.find("GL_NV_viewport_array2") == extensions.end())
3998 return true;
3999 if (member.getFieldName() == "gl_PositionPerViewNV" &&
4000 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
4001 return true;
4002 if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
4003 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
4004 return true;
4005 }
4006#endif
John Kessenich0e737842017-03-24 18:38:16 -06004007
4008 return false;
4009};
4010
John Kessenich6090df02016-06-30 21:18:02 -06004011// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
4012// explicitLayout can be kept the same throughout the hierarchical recursive walk.
4013// Mutually recursive with convertGlslangToSpvType().
4014spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
4015 const glslang::TTypeList* glslangMembers,
4016 glslang::TLayoutPacking explicitLayout,
4017 const glslang::TQualifier& qualifier)
4018{
4019 // Create a vector of struct types for SPIR-V to consume
4020 std::vector<spv::Id> spvMembers;
John Kessenich8985fc92020-03-03 10:25:07 -07004021 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0,
4022 // except sometimes for blocks
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004023 std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers;
John Kessenich6090df02016-06-30 21:18:02 -06004024 for (int i = 0; i < (int)glslangMembers->size(); i++) {
4025 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
4026 if (glslangMember.hiddenMember()) {
4027 ++memberDelta;
4028 if (type.getBasicType() == glslang::EbtBlock)
Roy05a5b532020-01-03 16:21:34 +08004029 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1;
John Kessenich6090df02016-06-30 21:18:02 -06004030 } else {
John Kessenich0e737842017-03-24 18:38:16 -06004031 if (type.getBasicType() == glslang::EbtBlock) {
Ashwin Lelec1e61d62019-07-22 12:36:38 -07004032 if (filterMember(glslangMember)) {
4033 memberDelta++;
Roy05a5b532020-01-03 16:21:34 +08004034 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1;
John Kessenich0e737842017-03-24 18:38:16 -06004035 continue;
Ashwin Lelec1e61d62019-07-22 12:36:38 -07004036 }
Roy05a5b532020-01-03 16:21:34 +08004037 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta;
John Kessenich0e737842017-03-24 18:38:16 -06004038 }
John Kessenich6090df02016-06-30 21:18:02 -06004039 // modify just this child's view of the qualifier
4040 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
4041 InheritQualifiers(memberQualifier, qualifier);
4042
John Kessenich7cdf3fc2017-06-04 13:22:39 -06004043 // manually inherit location
John Kessenich6090df02016-06-30 21:18:02 -06004044 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
John Kessenich7cdf3fc2017-06-04 13:22:39 -06004045 memberQualifier.layoutLocation = qualifier.layoutLocation;
John Kessenich6090df02016-06-30 21:18:02 -06004046
4047 // recurse
John Kessenichead86222018-03-28 18:01:20 -06004048 bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer &&
4049 i == (int)glslangMembers->size() - 1;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004050
4051 // Make forward pointers for any pointer members, and create a list of members to
4052 // convert to spirv types after creating the struct.
John Kessenich7015bd62019-08-01 03:28:08 -06004053 if (glslangMember.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004054 if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) {
4055 deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier));
4056 }
4057 spvMembers.push_back(
John Kessenich8985fc92020-03-03 10:25:07 -07004058 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember,
4059 true));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004060 } else {
4061 spvMembers.push_back(
John Kessenich8985fc92020-03-03 10:25:07 -07004062 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember,
4063 false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004064 }
John Kessenich6090df02016-06-30 21:18:02 -06004065 }
4066 }
4067
4068 // Make the SPIR-V type
4069 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
John Kessenichf2b7f332016-09-01 17:05:23 -06004070 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06004071 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
4072
4073 // Decorate it
4074 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
4075
John Kessenichd72f4882019-01-16 14:55:37 +07004076 for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004077 auto it = deferredForwardPointers[i];
4078 convertGlslangToSpvType(*it.first, explicitLayout, it.second, false);
4079 }
4080
John Kessenich6090df02016-06-30 21:18:02 -06004081 return spvType;
4082}
4083
4084void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
4085 const glslang::TTypeList* glslangMembers,
4086 glslang::TLayoutPacking explicitLayout,
4087 const glslang::TQualifier& qualifier,
4088 spv::Id spvType)
4089{
4090 // Name and decorate the non-hidden members
4091 int offset = -1;
4092 int locationOffset = 0; // for use within the members of this struct
Chow478b2322020-11-04 04:34:19 +08004093 bool memberLocationInvalid = type.isArrayOfArrays() ||
4094 (type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false));
John Kessenich6090df02016-06-30 21:18:02 -06004095 for (int i = 0; i < (int)glslangMembers->size(); i++) {
4096 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
4097 int member = i;
John Kessenich0e737842017-03-24 18:38:16 -06004098 if (type.getBasicType() == glslang::EbtBlock) {
Roy05a5b532020-01-03 16:21:34 +08004099 member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i];
John Kessenich0e737842017-03-24 18:38:16 -06004100 if (filterMember(glslangMember))
4101 continue;
4102 }
John Kessenich6090df02016-06-30 21:18:02 -06004103
4104 // modify just this child's view of the qualifier
4105 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
4106 InheritQualifiers(memberQualifier, qualifier);
4107
4108 // using -1 above to indicate a hidden member
John Kessenich5d610ee2018-03-07 18:05:55 -07004109 if (member < 0)
4110 continue;
4111
4112 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
4113 builder.addMemberDecoration(spvType, member,
4114 TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
4115 builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
4116 // Add interpolation and auxiliary storage decorations only to
4117 // top-level members of Input and Output storage classes
4118 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
4119 type.getQualifier().storage == glslang::EvqVaryingOut) {
4120 if (type.getBasicType() == glslang::EbtBlock ||
4121 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
4122 builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
4123 builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
John Kessenicha28f7a72019-08-06 07:00:58 -06004124#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07004125 addMeshNVDecoration(spvType, member, memberQualifier);
4126#endif
John Kessenich6090df02016-06-30 21:18:02 -06004127 }
John Kessenich5d610ee2018-03-07 18:05:55 -07004128 }
4129 builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
John Kessenich6090df02016-06-30 21:18:02 -06004130
John Kessenichb9197c82019-08-11 07:41:45 -06004131#ifndef GLSLANG_WEB
John Kessenich5d610ee2018-03-07 18:05:55 -07004132 if (type.getBasicType() == glslang::EbtBlock &&
4133 qualifier.storage == glslang::EvqBuffer) {
4134 // Add memory decorations only to top-level members of shader storage block
4135 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05004136 TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich5d610ee2018-03-07 18:05:55 -07004137 for (unsigned int i = 0; i < memory.size(); ++i)
4138 builder.addMemberDecoration(spvType, member, memory[i]);
4139 }
John Kessenichf8d1d742019-10-21 06:55:11 -06004140
John Kessenichb9197c82019-08-11 07:41:45 -06004141#endif
John Kessenich6090df02016-06-30 21:18:02 -06004142
John Kessenich5d610ee2018-03-07 18:05:55 -07004143 // Location assignment was already completed correctly by the front end,
4144 // just track whether a member needs to be decorated.
4145 // Ignore member locations if the container is an array, as that's
4146 // ill-specified and decisions have been made to not allow this.
Chow478b2322020-11-04 04:34:19 +08004147 if (!memberLocationInvalid && memberQualifier.hasLocation())
John Kessenich5d610ee2018-03-07 18:05:55 -07004148 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation);
John Kessenich6090df02016-06-30 21:18:02 -06004149
John Kessenich5d610ee2018-03-07 18:05:55 -07004150 if (qualifier.hasLocation()) // track for upcoming inheritance
4151 locationOffset += glslangIntermediate->computeTypeLocationSize(
4152 glslangMember, glslangIntermediate->getStage());
John Kessenich2f47bc92016-06-30 21:47:35 -06004153
John Kessenich5d610ee2018-03-07 18:05:55 -07004154 // component, XFB, others
4155 if (glslangMember.getQualifier().hasComponent())
4156 builder.addMemberDecoration(spvType, member, spv::DecorationComponent,
4157 glslangMember.getQualifier().layoutComponent);
4158 if (glslangMember.getQualifier().hasXfbOffset())
4159 builder.addMemberDecoration(spvType, member, spv::DecorationOffset,
4160 glslangMember.getQualifier().layoutXfbOffset);
4161 else if (explicitLayout != glslang::ElpNone) {
4162 // figure out what to do with offset, which is accumulating
4163 int nextOffset;
4164 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
4165 if (offset >= 0)
4166 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
4167 offset = nextOffset;
4168 }
John Kessenich6090df02016-06-30 21:18:02 -06004169
John Kessenich5d610ee2018-03-07 18:05:55 -07004170 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
4171 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride,
4172 getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
John Kessenich6090df02016-06-30 21:18:02 -06004173
John Kessenich5d610ee2018-03-07 18:05:55 -07004174 // built-in variable decorations
4175 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
4176 if (builtIn != spv::BuiltInMax)
4177 builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
chaoc771d89f2017-01-13 01:10:53 -08004178
John Kessenichb9197c82019-08-11 07:41:45 -06004179#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06004180 // nonuniform
4181 builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier()));
4182
John Kessenichead86222018-03-28 18:01:20 -06004183 if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) {
4184 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
4185 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
4186 memberQualifier.semanticName);
4187 }
4188
John Kessenich5d610ee2018-03-07 18:05:55 -07004189 if (builtIn == spv::BuiltInLayer) {
4190 // SPV_NV_viewport_array2 extension
4191 if (glslangMember.getQualifier().layoutViewportRelative){
4192 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
4193 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
4194 builder.addExtension(spv::E_SPV_NV_viewport_array2);
chaoc771d89f2017-01-13 01:10:53 -08004195 }
John Kessenich5d610ee2018-03-07 18:05:55 -07004196 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
4197 builder.addMemberDecoration(spvType, member,
4198 (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
4199 glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
4200 builder.addCapability(spv::CapabilityShaderStereoViewNV);
4201 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
chaocdf3956c2017-02-14 14:52:34 -08004202 }
John Kessenich5d610ee2018-03-07 18:05:55 -07004203 }
4204 if (glslangMember.getQualifier().layoutPassthrough) {
4205 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
4206 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
4207 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
4208 }
chaoc771d89f2017-01-13 01:10:53 -08004209#endif
John Kessenich6090df02016-06-30 21:18:02 -06004210 }
4211
4212 // Decorate the structure
John Kessenich5d610ee2018-03-07 18:05:55 -07004213 builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
4214 builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer()));
John Kessenich6090df02016-06-30 21:18:02 -06004215}
4216
John Kessenich6c292d32016-02-15 20:58:50 -07004217// Turn the expression forming the array size into an id.
4218// This is not quite trivial, because of specialization constants.
4219// Sometimes, a raw constant is turned into an Id, and sometimes
4220// a specialization constant expression is.
4221spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
4222{
4223 // First, see if this is sized with a node, meaning a specialization constant:
4224 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
4225 if (specNode != nullptr) {
4226 builder.clearAccessChain();
4227 specNode->traverse(this);
4228 return accessChainLoad(specNode->getAsTyped()->getType());
4229 }
qining25262b32016-05-06 17:25:16 -04004230
John Kessenich6c292d32016-02-15 20:58:50 -07004231 // Otherwise, need a compile-time (front end) size, get it:
4232 int size = arraySizes.getDimSize(dim);
4233 assert(size > 0);
4234 return builder.makeUintConstant(size);
4235}
4236
John Kessenich103bef92016-02-08 21:38:15 -07004237// Wrap the builder's accessChainLoad to:
4238// - localize handling of RelaxedPrecision
4239// - use the SPIR-V inferred type instead of another conversion of the glslang type
4240// (avoids unnecessary work and possible type punning for structures)
4241// - do conversion of concrete to abstract type
John Kessenich32cfd492016-02-02 12:37:46 -07004242spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
4243{
John Kessenich103bef92016-02-08 21:38:15 -07004244 spv::Id nominalTypeId = builder.accessChainGetInferredType();
Jeff Bolz36831c92018-09-05 10:11:41 -05004245
4246 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
4247 coherentFlags |= TranslateCoherent(type);
4248
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004249 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06004250 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004251
John Kessenich5611c6d2018-04-05 11:25:02 -06004252 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type),
greg-lunarg639f5462020-11-12 11:10:07 -07004253 TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags),
John Kessenich8985fc92020-03-03 10:25:07 -07004254 TranslateNonUniformDecoration(type.getQualifier()),
4255 nominalTypeId,
4256 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask),
4257 TranslateMemoryScope(coherentFlags),
4258 alignment);
John Kessenich103bef92016-02-08 21:38:15 -07004259
4260 // Need to convert to abstract types when necessary
Rex Xu27253232016-02-23 17:51:09 +08004261 if (type.getBasicType() == glslang::EbtBool) {
4262 if (builder.isScalarType(nominalTypeId)) {
4263 // Conversion for bool
4264 spv::Id boolType = builder.makeBoolType();
4265 if (nominalTypeId != boolType)
4266 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
4267 } else if (builder.isVectorType(nominalTypeId)) {
4268 // Conversion for bvec
4269 int vecSize = builder.getNumTypeComponents(nominalTypeId);
4270 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
4271 if (nominalTypeId != bvecType)
John Kessenich8985fc92020-03-03 10:25:07 -07004272 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId,
4273 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08004274 }
4275 }
John Kessenich103bef92016-02-08 21:38:15 -07004276
4277 return loadedId;
John Kessenich32cfd492016-02-02 12:37:46 -07004278}
4279
Rex Xu27253232016-02-23 17:51:09 +08004280// Wrap the builder's accessChainStore to:
4281// - do conversion of concrete to abstract type
John Kessenich4bf71552016-09-02 11:20:21 -06004282//
4283// Implicitly uses the existing builder.accessChain as the storage target.
Rex Xu27253232016-02-23 17:51:09 +08004284void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
4285{
4286 // Need to convert to abstract types when necessary
4287 if (type.getBasicType() == glslang::EbtBool) {
4288 spv::Id nominalTypeId = builder.accessChainGetInferredType();
4289
4290 if (builder.isScalarType(nominalTypeId)) {
4291 // Conversion for bool
4292 spv::Id boolType = builder.makeBoolType();
John Kessenichb6cabc42017-05-19 23:29:50 -06004293 if (nominalTypeId != boolType) {
4294 // keep these outside arguments, for determinant order-of-evaluation
4295 spv::Id one = builder.makeUintConstant(1);
4296 spv::Id zero = builder.makeUintConstant(0);
4297 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
4298 } else if (builder.getTypeId(rvalue) != boolType)
John Kessenich80f92a12017-05-19 23:00:13 -06004299 rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0));
Rex Xu27253232016-02-23 17:51:09 +08004300 } else if (builder.isVectorType(nominalTypeId)) {
4301 // Conversion for bvec
4302 int vecSize = builder.getNumTypeComponents(nominalTypeId);
4303 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
John Kessenichb6cabc42017-05-19 23:29:50 -06004304 if (nominalTypeId != bvecType) {
4305 // keep these outside arguments, for determinant order-of-evaluation
John Kessenich7b8c3862017-05-19 23:44:51 -06004306 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
4307 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
4308 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
John Kessenichb6cabc42017-05-19 23:29:50 -06004309 } else if (builder.getTypeId(rvalue) != bvecType)
John Kessenich80f92a12017-05-19 23:00:13 -06004310 rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue,
4311 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08004312 }
4313 }
4314
Jeff Bolz36831c92018-09-05 10:11:41 -05004315 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
4316 coherentFlags |= TranslateCoherent(type);
4317
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004318 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06004319 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004320
greg-lunarg639f5462020-11-12 11:10:07 -07004321 builder.accessChainStore(rvalue, TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags),
John Kessenich8985fc92020-03-03 10:25:07 -07004322 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) &
4323 ~spv::MemoryAccessMakePointerVisibleKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004324 TranslateMemoryScope(coherentFlags), alignment);
Rex Xu27253232016-02-23 17:51:09 +08004325}
4326
John Kessenich4bf71552016-09-02 11:20:21 -06004327// For storing when types match at the glslang level, but not might match at the
4328// SPIR-V level.
4329//
4330// This especially happens when a single glslang type expands to multiple
John Kesseniched33e052016-10-06 12:59:51 -06004331// SPIR-V types, like a struct that is used in a member-undecorated way as well
John Kessenich4bf71552016-09-02 11:20:21 -06004332// as in a member-decorated way.
4333//
4334// NOTE: This function can handle any store request; if it's not special it
4335// simplifies to a simple OpStore.
4336//
4337// Implicitly uses the existing builder.accessChain as the storage target.
4338void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
4339{
John Kessenichb3e24e42016-09-11 12:33:43 -06004340 // we only do the complex path here if it's an aggregate
4341 if (! type.isStruct() && ! type.isArray()) {
John Kessenich4bf71552016-09-02 11:20:21 -06004342 accessChainStore(type, rValue);
4343 return;
4344 }
4345
John Kessenichb3e24e42016-09-11 12:33:43 -06004346 // and, it has to be a case of type aliasing
John Kessenich4bf71552016-09-02 11:20:21 -06004347 spv::Id rType = builder.getTypeId(rValue);
4348 spv::Id lValue = builder.accessChainGetLValue();
4349 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
4350 if (lType == rType) {
4351 accessChainStore(type, rValue);
4352 return;
4353 }
4354
John Kessenichb3e24e42016-09-11 12:33:43 -06004355 // Recursively (as needed) copy an aggregate type to a different aggregate type,
John Kessenich4bf71552016-09-02 11:20:21 -06004356 // where the two types were the same type in GLSL. This requires member
4357 // by member copy, recursively.
4358
John Kessenichfbb6bdf2019-01-15 21:48:27 +07004359 // SPIR-V 1.4 added an instruction to do help do this.
4360 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
4361 // However, bool in uniform space is changed to int, so
4362 // OpCopyLogical does not work for that.
4363 // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules.
4364 bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0);
4365 bool lBool = builder.containsType(lType, spv::OpTypeBool, 0);
4366 if (lBool == rBool) {
4367 spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue);
4368 accessChainStore(type, logicalCopy);
4369 return;
4370 }
4371 }
4372
John Kessenichb3e24e42016-09-11 12:33:43 -06004373 // If an array, copy element by element.
4374 if (type.isArray()) {
4375 glslang::TType glslangElementType(type, 0);
4376 spv::Id elementRType = builder.getContainedTypeId(rType);
4377 for (int index = 0; index < type.getOuterArraySize(); ++index) {
4378 // get the source member
4379 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
John Kessenich4bf71552016-09-02 11:20:21 -06004380
John Kessenichb3e24e42016-09-11 12:33:43 -06004381 // set up the target storage
4382 builder.clearAccessChain();
4383 builder.setAccessChainLValue(lValue);
John Kessenich8985fc92020-03-03 10:25:07 -07004384 builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type),
4385 type.getBufferReferenceAlignment());
John Kessenich4bf71552016-09-02 11:20:21 -06004386
John Kessenichb3e24e42016-09-11 12:33:43 -06004387 // store the member
4388 multiTypeStore(glslangElementType, elementRValue);
4389 }
4390 } else {
4391 assert(type.isStruct());
John Kessenich4bf71552016-09-02 11:20:21 -06004392
John Kessenichb3e24e42016-09-11 12:33:43 -06004393 // loop over structure members
4394 const glslang::TTypeList& members = *type.getStruct();
4395 for (int m = 0; m < (int)members.size(); ++m) {
4396 const glslang::TType& glslangMemberType = *members[m].type;
4397
4398 // get the source member
4399 spv::Id memberRType = builder.getContainedTypeId(rType, m);
4400 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
4401
4402 // set up the target storage
4403 builder.clearAccessChain();
4404 builder.setAccessChainLValue(lValue);
John Kessenich8985fc92020-03-03 10:25:07 -07004405 builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type),
4406 type.getBufferReferenceAlignment());
John Kessenichb3e24e42016-09-11 12:33:43 -06004407
4408 // store the member
4409 multiTypeStore(glslangMemberType, memberRValue);
4410 }
John Kessenich4bf71552016-09-02 11:20:21 -06004411 }
4412}
4413
John Kessenichf85e8062015-12-19 13:57:10 -07004414// Decide whether or not this type should be
4415// decorated with offsets and strides, and if so
4416// whether std140 or std430 rules should be applied.
4417glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
John Kessenich31ed4832015-09-09 17:51:38 -06004418{
John Kessenichf85e8062015-12-19 13:57:10 -07004419 // has to be a block
4420 if (type.getBasicType() != glslang::EbtBlock)
4421 return glslang::ElpNone;
4422
Chao Chen3c366992018-09-19 11:41:59 -07004423 // has to be a uniform or buffer block or task in/out blocks
John Kessenichf85e8062015-12-19 13:57:10 -07004424 if (type.getQualifier().storage != glslang::EvqUniform &&
Chao Chen3c366992018-09-19 11:41:59 -07004425 type.getQualifier().storage != glslang::EvqBuffer &&
Caio Marcelo de Oliveira Filho4bfbf622020-06-02 16:58:51 -07004426 type.getQualifier().storage != glslang::EvqShared &&
Chao Chen3c366992018-09-19 11:41:59 -07004427 !type.getQualifier().isTaskMemory())
John Kessenichf85e8062015-12-19 13:57:10 -07004428 return glslang::ElpNone;
4429
4430 // return the layout to use
4431 switch (type.getQualifier().layoutPacking) {
4432 case glslang::ElpStd140:
4433 case glslang::ElpStd430:
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004434 case glslang::ElpScalar:
John Kessenichf85e8062015-12-19 13:57:10 -07004435 return type.getQualifier().layoutPacking;
4436 default:
4437 return glslang::ElpNone;
4438 }
John Kessenich31ed4832015-09-09 17:51:38 -06004439}
4440
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004441// Given an array type, returns the integer stride required for that array
John Kessenich8985fc92020-03-03 10:25:07 -07004442int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout,
4443 glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004444{
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004445 int size;
John Kessenich49987892015-12-29 17:11:44 -07004446 int stride;
John Kessenich8985fc92020-03-03 10:25:07 -07004447 glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout,
4448 matrixLayout == glslang::ElmRowMajor);
John Kesseniche721f492015-12-06 19:17:49 -07004449
4450 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004451}
4452
John Kessenich49987892015-12-29 17:11:44 -07004453// Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004454// when used as a member of an interface block
John Kessenich8985fc92020-03-03 10:25:07 -07004455int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout,
4456 glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004457{
John Kessenich49987892015-12-29 17:11:44 -07004458 glslang::TType elementType;
4459 elementType.shallowCopy(matrixType);
4460 elementType.clearArraySizes();
4461
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004462 int size;
John Kessenich49987892015-12-29 17:11:44 -07004463 int stride;
John Kessenich8985fc92020-03-03 10:25:07 -07004464 glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout,
4465 matrixLayout == glslang::ElmRowMajor);
John Kessenich49987892015-12-29 17:11:44 -07004466
4467 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004468}
4469
John Kessenich5e4b1242015-08-06 22:53:06 -06004470// Given a member type of a struct, realign the current offset for it, and compute
4471// the next (not yet aligned) offset for the next member, which will get aligned
4472// on the next call.
4473// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
4474// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
4475// -1 means a non-forced member offset (no decoration needed).
John Kessenich8985fc92020-03-03 10:25:07 -07004476void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType,
4477 int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
John Kessenich5e4b1242015-08-06 22:53:06 -06004478{
4479 // this will get a positive value when deemed necessary
4480 nextOffset = -1;
4481
John Kessenich5e4b1242015-08-06 22:53:06 -06004482 // override anything in currentOffset with user-set offset
4483 if (memberType.getQualifier().hasOffset())
4484 currentOffset = memberType.getQualifier().layoutOffset;
4485
4486 // It could be that current linker usage in glslang updated all the layoutOffset,
4487 // in which case the following code does not matter. But, that's not quite right
4488 // once cross-compilation unit GLSL validation is done, as the original user
4489 // settings are needed in layoutOffset, and then the following will come into play.
4490
John Kessenichf85e8062015-12-19 13:57:10 -07004491 if (explicitLayout == glslang::ElpNone) {
John Kessenich5e4b1242015-08-06 22:53:06 -06004492 if (! memberType.getQualifier().hasOffset())
4493 currentOffset = -1;
4494
4495 return;
4496 }
4497
John Kessenichf85e8062015-12-19 13:57:10 -07004498 // Getting this far means we need explicit offsets
John Kessenich5e4b1242015-08-06 22:53:06 -06004499 if (currentOffset < 0)
4500 currentOffset = 0;
qining25262b32016-05-06 17:25:16 -04004501
John Kessenich5e4b1242015-08-06 22:53:06 -06004502 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
4503 // but possibly not yet correctly aligned.
4504
4505 int memberSize;
John Kessenich49987892015-12-29 17:11:44 -07004506 int dummyStride;
John Kessenich8985fc92020-03-03 10:25:07 -07004507 int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout,
4508 matrixLayout == glslang::ElmRowMajor);
John Kessenich4f1403e2017-04-05 17:38:20 -06004509
4510 // Adjust alignment for HLSL rules
John Kessenich735d7e52017-07-13 11:39:16 -06004511 // TODO: make this consistent in early phases of code:
4512 // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
4513 // Until reflection is brought in sync with these adjustments, don't apply to $Global,
4514 // which is the most likely to rely on reflection, and least likely to rely implicit layouts
John Kesseniche7df8e02018-08-22 17:12:46 -06004515 if (glslangIntermediate->usingHlslOffsets() &&
John Kessenich735d7e52017-07-13 11:39:16 -06004516 ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
John Kessenich4f1403e2017-04-05 17:38:20 -06004517 int dummySize;
4518 int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
4519 if (componentAlignment <= 4)
4520 memberAlignment = componentAlignment;
4521 }
4522
4523 // Bump up to member alignment
John Kessenich5e4b1242015-08-06 22:53:06 -06004524 glslang::RoundToPow2(currentOffset, memberAlignment);
John Kessenich4f1403e2017-04-05 17:38:20 -06004525
4526 // Bump up to vec4 if there is a bad straddle
John Kessenich8985fc92020-03-03 10:25:07 -07004527 if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize,
4528 currentOffset))
John Kessenich4f1403e2017-04-05 17:38:20 -06004529 glslang::RoundToPow2(currentOffset, 16);
4530
John Kessenich5e4b1242015-08-06 22:53:06 -06004531 nextOffset = currentOffset + memberSize;
4532}
4533
David Netoa901ffe2016-06-08 14:11:40 +01004534void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
John Kessenichebb50532016-05-16 19:22:05 -06004535{
David Netoa901ffe2016-06-08 14:11:40 +01004536 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
4537 switch (glslangBuiltIn)
4538 {
John Kessenicha28f7a72019-08-06 07:00:58 -06004539 case glslang::EbvPointSize:
4540#ifndef GLSLANG_WEB
David Netoa901ffe2016-06-08 14:11:40 +01004541 case glslang::EbvClipDistance:
4542 case glslang::EbvCullDistance:
chaoc771d89f2017-01-13 01:10:53 -08004543 case glslang::EbvViewportMaskNV:
4544 case glslang::EbvSecondaryPositionNV:
4545 case glslang::EbvSecondaryViewportMaskNV:
chaocdf3956c2017-02-14 14:52:34 -08004546 case glslang::EbvPositionPerViewNV:
4547 case glslang::EbvViewportMaskPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07004548 case glslang::EbvTaskCountNV:
4549 case glslang::EbvPrimitiveCountNV:
4550 case glslang::EbvPrimitiveIndicesNV:
4551 case glslang::EbvClipDistancePerViewNV:
4552 case glslang::EbvCullDistancePerViewNV:
4553 case glslang::EbvLayerPerViewNV:
4554 case glslang::EbvMeshViewCountNV:
4555 case glslang::EbvMeshViewIndicesNV:
chaoc771d89f2017-01-13 01:10:53 -08004556#endif
David Netoa901ffe2016-06-08 14:11:40 +01004557 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
4558 // Alternately, we could just call this for any glslang built-in, since the
4559 // capability already guards against duplicates.
4560 TranslateBuiltInDecoration(glslangBuiltIn, false);
4561 break;
4562 default:
4563 // Capabilities were already generated when the struct was declared.
4564 break;
4565 }
John Kessenichebb50532016-05-16 19:22:05 -06004566}
4567
John Kessenich6fccb3c2016-09-19 16:01:41 -06004568bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
John Kessenich140f3df2015-06-26 16:58:36 -06004569{
John Kessenicheee9d532016-09-19 18:09:30 -06004570 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
John Kessenich140f3df2015-06-26 16:58:36 -06004571}
4572
John Kessenichd41993d2017-09-10 15:21:05 -06004573// Does parameter need a place to keep writes, separate from the original?
John Kessenich6a14f782017-12-04 02:48:10 -07004574// Assumes called after originalParam(), which filters out block/buffer/opaque-based
4575// qualifiers such that we should have only in/out/inout/constreadonly here.
John Kessenichd3ed90b2018-05-04 11:43:03 -06004576bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
John Kessenichd41993d2017-09-10 15:21:05 -06004577{
John Kessenich6a14f782017-12-04 02:48:10 -07004578 assert(qualifier == glslang::EvqIn ||
4579 qualifier == glslang::EvqOut ||
4580 qualifier == glslang::EvqInOut ||
rdbd8edfd82020-06-02 08:30:07 +02004581 qualifier == glslang::EvqUniform ||
John Kessenich6a14f782017-12-04 02:48:10 -07004582 qualifier == glslang::EvqConstReadOnly);
rdbd8edfd82020-06-02 08:30:07 +02004583 return qualifier != glslang::EvqConstReadOnly &&
4584 qualifier != glslang::EvqUniform;
John Kessenichd41993d2017-09-10 15:21:05 -06004585}
4586
4587// Is parameter pass-by-original?
4588bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
4589 bool implicitThisParam)
4590{
4591 if (implicitThisParam) // implicit this
4592 return true;
4593 if (glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich6a14f782017-12-04 02:48:10 -07004594 return paramType.getBasicType() == glslang::EbtBlock;
John Kessenichd41993d2017-09-10 15:21:05 -06004595 return paramType.containsOpaque() || // sampler, etc.
4596 (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO
4597}
4598
John Kessenich140f3df2015-06-26 16:58:36 -06004599// Make all the functions, skeletally, without actually visiting their bodies.
4600void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
4601{
John Kessenich8985fc92020-03-03 10:25:07 -07004602 const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type,
4603 bool useVulkanMemoryModel) {
John Kessenichfad62972017-07-18 02:35:46 -06004604 spv::Decoration paramPrecision = TranslatePrecisionDecoration(type);
4605 if (paramPrecision != spv::NoPrecision)
4606 decorations.push_back(paramPrecision);
Jeff Bolz36831c92018-09-05 10:11:41 -05004607 TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel);
John Kessenich7015bd62019-08-01 03:28:08 -06004608 if (type.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004609 // Original and non-writable params pass the pointer directly and
4610 // use restrict/aliased, others are stored to a pointer in Function
4611 // memory and use RestrictPointer/AliasedPointer.
4612 if (originalParam(type.getQualifier().storage, type, false) ||
4613 !writableParam(type.getQualifier().storage)) {
John Kessenichf8d1d742019-10-21 06:55:11 -06004614 decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict :
4615 spv::DecorationAliased);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004616 } else {
John Kessenichf8d1d742019-10-21 06:55:11 -06004617 decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT :
4618 spv::DecorationAliasedPointerEXT);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004619 }
4620 }
John Kessenichfad62972017-07-18 02:35:46 -06004621 };
4622
John Kessenich140f3df2015-06-26 16:58:36 -06004623 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4624 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
John Kessenich6fccb3c2016-09-19 16:01:41 -06004625 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
John Kessenich140f3df2015-06-26 16:58:36 -06004626 continue;
4627
4628 // We're on a user function. Set up the basic interface for the function now,
John Kessenich4bf71552016-09-02 11:20:21 -06004629 // so that it's available to call. Translating the body will happen later.
John Kessenich140f3df2015-06-26 16:58:36 -06004630 //
qining25262b32016-05-06 17:25:16 -04004631 // Typically (except for a "const in" parameter), an address will be passed to the
John Kessenich140f3df2015-06-26 16:58:36 -06004632 // function. What it is an address of varies:
4633 //
John Kessenich4bf71552016-09-02 11:20:21 -06004634 // - "in" parameters not marked as "const" can be written to without modifying the calling
4635 // argument so that write needs to be to a copy, hence the address of a copy works.
John Kessenich140f3df2015-06-26 16:58:36 -06004636 //
4637 // - "const in" parameters can just be the r-value, as no writes need occur.
4638 //
John Kessenich4bf71552016-09-02 11:20:21 -06004639 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
4640 // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy.
John Kessenich140f3df2015-06-26 16:58:36 -06004641
4642 std::vector<spv::Id> paramTypes;
John Kessenichfad62972017-07-18 02:35:46 -06004643 std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter
John Kessenich140f3df2015-06-26 16:58:36 -06004644 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
4645
John Kessenich155d3512019-08-08 23:29:20 -06004646#ifdef ENABLE_HLSL
John Kessenichfad62972017-07-18 02:35:46 -06004647 bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() ==
4648 glslangIntermediate->implicitThisName;
John Kessenich155d3512019-08-08 23:29:20 -06004649#else
4650 bool implicitThis = false;
4651#endif
John Kessenich37789792017-03-21 23:56:40 -06004652
John Kessenichfad62972017-07-18 02:35:46 -06004653 paramDecorations.resize(parameters.size());
John Kessenich140f3df2015-06-26 16:58:36 -06004654 for (int p = 0; p < (int)parameters.size(); ++p) {
4655 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
4656 spv::Id typeId = convertGlslangToSpvType(paramType);
John Kessenichd41993d2017-09-10 15:21:05 -06004657 if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0))
John Kessenicha5c5fb62017-05-05 05:09:58 -06004658 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
John Kessenichd41993d2017-09-10 15:21:05 -06004659 else if (writableParam(paramType.getQualifier().storage))
John Kessenich140f3df2015-06-26 16:58:36 -06004660 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
4661 else
John Kessenich4bf71552016-09-02 11:20:21 -06004662 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
Jeff Bolz36831c92018-09-05 10:11:41 -05004663 getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich140f3df2015-06-26 16:58:36 -06004664 paramTypes.push_back(typeId);
4665 }
4666
4667 spv::Block* functionBlock;
John Kessenich32cfd492016-02-02 12:37:46 -07004668 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
4669 convertGlslangToSpvType(glslFunction->getType()),
John Kessenichfad62972017-07-18 02:35:46 -06004670 glslFunction->getName().c_str(), paramTypes,
4671 paramDecorations, &functionBlock);
John Kessenich37789792017-03-21 23:56:40 -06004672 if (implicitThis)
4673 function->setImplicitThis();
John Kessenich140f3df2015-06-26 16:58:36 -06004674
4675 // Track function to emit/call later
4676 functionMap[glslFunction->getName().c_str()] = function;
4677
4678 // Set the parameter id's
4679 for (int p = 0; p < (int)parameters.size(); ++p) {
4680 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
4681 // give a name too
4682 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004683
4684 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
John Kessenichb9197c82019-08-11 07:41:45 -06004685 if (paramType.contains8BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004686 builder.addCapability(spv::CapabilityInt8);
John Kessenichb9197c82019-08-11 07:41:45 -06004687 if (paramType.contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004688 builder.addCapability(spv::CapabilityInt16);
John Kessenichb9197c82019-08-11 07:41:45 -06004689 if (paramType.contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004690 builder.addCapability(spv::CapabilityFloat16);
John Kessenich140f3df2015-06-26 16:58:36 -06004691 }
4692 }
4693}
4694
4695// Process all the initializers, while skipping the functions and link objects
4696void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
4697{
4698 builder.setBuildPoint(shaderEntry->getLastBlock());
4699 for (int i = 0; i < (int)initializers.size(); ++i) {
4700 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
John Kessenich8985fc92020-03-03 10:25:07 -07004701 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() !=
4702 glslang::EOpLinkerObjects) {
John Kessenich140f3df2015-06-26 16:58:36 -06004703
4704 // We're on a top-level node that's not a function. Treat as an initializer, whose
John Kessenich6fccb3c2016-09-19 16:01:41 -06004705 // code goes into the beginning of the entry point.
John Kessenich140f3df2015-06-26 16:58:36 -06004706 initializer->traverse(this);
4707 }
4708 }
4709}
Daniel Kochffccefd2020-11-23 15:41:27 -05004710// Walk over all linker objects to create a map for payload and callable data linker objects
4711// and their location to be used during codegen for OpTraceKHR and OpExecuteCallableKHR
4712// This is done here since it is possible that these linker objects are not be referenced in the AST
4713void TGlslangToSpvTraverser::collectRayTracingLinkerObjects()
4714{
4715 glslang::TIntermAggregate* linkerObjects = glslangIntermediate->findLinkerObjects();
4716 for (auto& objSeq : linkerObjects->getSequence()) {
4717 auto objNode = objSeq->getAsSymbolNode();
4718 if (objNode != nullptr) {
4719 if (objNode->getQualifier().hasLocation()) {
4720 unsigned int location = objNode->getQualifier().layoutLocation;
4721 auto st = objNode->getQualifier().storage;
4722 int set;
4723 switch (st)
4724 {
4725 case glslang::EvqPayload:
4726 case glslang::EvqPayloadIn:
4727 set = 0;
4728 break;
4729 case glslang::EvqCallableData:
4730 case glslang::EvqCallableDataIn:
4731 set = 1;
4732 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004733
Daniel Kochffccefd2020-11-23 15:41:27 -05004734 default:
4735 set = -1;
4736 }
4737 if (set != -1)
4738 locationToSymbol[set].insert(std::make_pair(location, objNode));
4739 }
4740 }
4741 }
4742}
John Kessenich140f3df2015-06-26 16:58:36 -06004743// Process all the functions, while skipping initializers.
4744void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
4745{
4746 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4747 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
John Kessenich6a60c2f2016-12-08 21:01:59 -07004748 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
John Kessenich140f3df2015-06-26 16:58:36 -06004749 node->traverse(this);
4750 }
4751}
4752
4753void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
4754{
qining25262b32016-05-06 17:25:16 -04004755 // SPIR-V functions should already be in the functionMap from the prepass
John Kessenich140f3df2015-06-26 16:58:36 -06004756 // that called makeFunctions().
John Kesseniched33e052016-10-06 12:59:51 -06004757 currentFunction = functionMap[node->getName().c_str()];
4758 spv::Block* functionBlock = currentFunction->getEntryBlock();
John Kessenich140f3df2015-06-26 16:58:36 -06004759 builder.setBuildPoint(functionBlock);
4760}
4761
John Kessenich8985fc92020-03-03 10:25:07 -07004762void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments,
4763 spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06004764{
Rex Xufc618912015-09-09 16:42:49 +08004765 const glslang::TIntermSequence& glslangArguments = node.getSequence();
Rex Xu48edadf2015-12-31 16:11:41 +08004766
4767 glslang::TSampler sampler = {};
4768 bool cubeCompare = false;
John Kessenicha28f7a72019-08-06 07:00:58 -06004769#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08004770 bool f16ShadowCompare = false;
4771#endif
Rex Xu5eafa472016-02-19 22:24:03 +08004772 if (node.isTexture() || node.isImage()) {
Rex Xu48edadf2015-12-31 16:11:41 +08004773 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
4774 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
John Kessenicha28f7a72019-08-06 07:00:58 -06004775#ifndef GLSLANG_WEB
John Kessenich8985fc92020-03-03 10:25:07 -07004776 f16ShadowCompare = sampler.shadow &&
4777 glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004778#endif
Rex Xu48edadf2015-12-31 16:11:41 +08004779 }
4780
John Kessenich140f3df2015-06-26 16:58:36 -06004781 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
4782 builder.clearAccessChain();
4783 glslangArguments[i]->traverse(this);
Rex Xufc618912015-09-09 16:42:49 +08004784
John Kessenicha28f7a72019-08-06 07:00:58 -06004785#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08004786 // Special case l-value operands
4787 bool lvalue = false;
4788 switch (node.getOp()) {
4789 case glslang::EOpImageAtomicAdd:
4790 case glslang::EOpImageAtomicMin:
4791 case glslang::EOpImageAtomicMax:
4792 case glslang::EOpImageAtomicAnd:
4793 case glslang::EOpImageAtomicOr:
4794 case glslang::EOpImageAtomicXor:
4795 case glslang::EOpImageAtomicExchange:
4796 case glslang::EOpImageAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05004797 case glslang::EOpImageAtomicLoad:
4798 case glslang::EOpImageAtomicStore:
Rex Xufc618912015-09-09 16:42:49 +08004799 if (i == 0)
4800 lvalue = true;
4801 break;
Rex Xu5eafa472016-02-19 22:24:03 +08004802 case glslang::EOpSparseImageLoad:
4803 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
4804 lvalue = true;
4805 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004806 case glslang::EOpSparseTexture:
4807 if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2))
4808 lvalue = true;
4809 break;
4810 case glslang::EOpSparseTextureClamp:
4811 if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3))
4812 lvalue = true;
4813 break;
4814 case glslang::EOpSparseTextureLod:
4815 case glslang::EOpSparseTextureOffset:
4816 if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3))
4817 lvalue = true;
4818 break;
Rex Xu48edadf2015-12-31 16:11:41 +08004819 case glslang::EOpSparseTextureFetch:
4820 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
4821 lvalue = true;
4822 break;
4823 case glslang::EOpSparseTextureFetchOffset:
4824 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
4825 lvalue = true;
4826 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004827 case glslang::EOpSparseTextureLodOffset:
4828 case glslang::EOpSparseTextureGrad:
4829 case glslang::EOpSparseTextureOffsetClamp:
4830 if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4))
4831 lvalue = true;
4832 break;
4833 case glslang::EOpSparseTextureGradOffset:
4834 case glslang::EOpSparseTextureGradClamp:
4835 if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5))
4836 lvalue = true;
4837 break;
4838 case glslang::EOpSparseTextureGradOffsetClamp:
4839 if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6))
4840 lvalue = true;
4841 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004842 case glslang::EOpSparseTextureGather:
Rex Xu48edadf2015-12-31 16:11:41 +08004843 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
4844 lvalue = true;
4845 break;
4846 case glslang::EOpSparseTextureGatherOffset:
4847 case glslang::EOpSparseTextureGatherOffsets:
4848 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
4849 lvalue = true;
4850 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004851 case glslang::EOpSparseTextureGatherLod:
4852 if (i == 3)
4853 lvalue = true;
4854 break;
4855 case glslang::EOpSparseTextureGatherLodOffset:
4856 case glslang::EOpSparseTextureGatherLodOffsets:
4857 if (i == 4)
4858 lvalue = true;
4859 break;
Rex Xu129799a2017-07-05 17:23:28 +08004860 case glslang::EOpSparseImageLoadLod:
4861 if (i == 3)
4862 lvalue = true;
4863 break;
Chao Chen3a137962018-09-19 11:41:27 -07004864 case glslang::EOpImageSampleFootprintNV:
4865 if (i == 4)
4866 lvalue = true;
4867 break;
4868 case glslang::EOpImageSampleFootprintClampNV:
4869 case glslang::EOpImageSampleFootprintLodNV:
4870 if (i == 5)
4871 lvalue = true;
4872 break;
4873 case glslang::EOpImageSampleFootprintGradNV:
4874 if (i == 6)
4875 lvalue = true;
4876 break;
4877 case glslang::EOpImageSampleFootprintGradClampNV:
4878 if (i == 7)
4879 lvalue = true;
4880 break;
Rex Xufc618912015-09-09 16:42:49 +08004881 default:
4882 break;
4883 }
4884
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004885 if (lvalue) {
greg-lunarg639f5462020-11-12 11:10:07 -07004886 spv::Id lvalue_id = builder.accessChainGetLValue();
4887 arguments.push_back(lvalue_id);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004888 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
greg-lunarg639f5462020-11-12 11:10:07 -07004889 builder.addDecoration(lvalue_id, TranslateNonUniformDecoration(lvalueCoherentFlags));
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004890 lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType());
4891 } else
John Kessenicha28f7a72019-08-06 07:00:58 -06004892#endif
John Kessenich32cfd492016-02-02 12:37:46 -07004893 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06004894 }
4895}
4896
John Kessenichfc51d282015-08-19 13:34:18 -06004897void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06004898{
John Kessenichfc51d282015-08-19 13:34:18 -06004899 builder.clearAccessChain();
4900 node.getOperand()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07004901 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
John Kessenichfc51d282015-08-19 13:34:18 -06004902}
John Kessenich140f3df2015-06-26 16:58:36 -06004903
John Kessenichfc51d282015-08-19 13:34:18 -06004904spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
4905{
John Kesseniche485c7a2017-05-31 18:50:53 -06004906 if (! node->isImage() && ! node->isTexture())
John Kessenichfc51d282015-08-19 13:34:18 -06004907 return spv::NoResult;
John Kesseniche485c7a2017-05-31 18:50:53 -06004908
greg-lunarg5d43c4a2018-12-07 17:36:33 -07004909 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06004910
John Kessenichfc51d282015-08-19 13:34:18 -06004911 // Process a GLSL texturing op (will be SPV image)
Jeff Bolz36831c92018-09-05 10:11:41 -05004912
John Kessenichf43c7392019-03-31 10:51:57 -06004913 const glslang::TType &imageType = node->getAsAggregate()
4914 ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType()
4915 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType();
Jeff Bolz36831c92018-09-05 10:11:41 -05004916 const glslang::TSampler sampler = imageType.getSampler();
John Kessenicha28f7a72019-08-06 07:00:58 -06004917#ifdef GLSLANG_WEB
4918 const bool f16ShadowCompare = false;
4919#else
Rex Xu1e5d7b02016-11-29 17:36:31 +08004920 bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate())
John Kessenichf43c7392019-03-31 10:51:57 -06004921 ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16
4922 : false;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004923#endif
4924
John Kessenichf43c7392019-03-31 10:51:57 -06004925 const auto signExtensionMask = [&]() {
4926 if (builder.getSpvVersion() >= spv::Spv_1_4) {
4927 if (sampler.type == glslang::EbtUint)
4928 return spv::ImageOperandsZeroExtendMask;
4929 else if (sampler.type == glslang::EbtInt)
4930 return spv::ImageOperandsSignExtendMask;
4931 }
4932 return spv::ImageOperandsMaskNone;
4933 };
4934
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004935 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
4936
John Kessenichfc51d282015-08-19 13:34:18 -06004937 std::vector<spv::Id> arguments;
4938 if (node->getAsAggregate())
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004939 translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06004940 else
4941 translateArguments(*node->getAsUnaryNode(), arguments);
John Kessenich12c155f2020-06-30 07:52:05 -06004942 spv::Decoration precision = TranslatePrecisionDecoration(node->getType());
John Kessenichfc51d282015-08-19 13:34:18 -06004943
4944 spv::Builder::TextureParameters params = { };
4945 params.sampler = arguments[0];
4946
Rex Xu04db3f52015-09-16 11:44:02 +08004947 glslang::TCrackedTextureOp cracked;
4948 node->crackTexture(sampler, cracked);
4949
amhagan05506bb2017-06-13 16:53:02 -04004950 const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint;
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004951
Bas Nieuwenhuizen58064312020-09-03 03:04:13 +02004952 if (builder.isSampledImage(params.sampler) &&
4953 ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) {
4954 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4955 if (imageType.getQualifier().isNonUniform()) {
4956 builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT);
4957 }
4958 }
John Kessenichfc51d282015-08-19 13:34:18 -06004959 // Check for queries
4960 if (cracked.query) {
4961 switch (node->getOp()) {
4962 case glslang::EOpImageQuerySize:
4963 case glslang::EOpTextureQuerySize:
John Kessenich140f3df2015-06-26 16:58:36 -06004964 if (arguments.size() > 1) {
4965 params.lod = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004966 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult);
John Kessenich140f3df2015-06-26 16:58:36 -06004967 } else
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004968 return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult);
John Kessenicha28f7a72019-08-06 07:00:58 -06004969#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06004970 case glslang::EOpImageQuerySamples:
4971 case glslang::EOpTextureQuerySamples:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004972 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004973 case glslang::EOpTextureQueryLod:
4974 params.coords = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004975 return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004976 case glslang::EOpTextureQueryLevels:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004977 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult);
Rex Xu48edadf2015-12-31 16:11:41 +08004978 case glslang::EOpSparseTexelsResident:
4979 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
John Kessenicha28f7a72019-08-06 07:00:58 -06004980#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004981 default:
4982 assert(0);
4983 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004984 }
John Kessenich140f3df2015-06-26 16:58:36 -06004985 }
4986
LoopDawg4425f242018-02-18 11:40:01 -07004987 int components = node->getType().getVectorSize();
4988
4989 if (node->getOp() == glslang::EOpTextureFetch) {
4990 // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed.
4991 // This will only happen through the HLSL path for operator[], so we do not have to handle e.g.
4992 // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic
4993 // here around e.g. which ones return scalars or other types.
4994 components = 4;
4995 }
4996
4997 glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components);
4998
4999 auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); };
5000
Rex Xufc618912015-09-09 16:42:49 +08005001 // Check for image functions other than queries
5002 if (node->isImage()) {
John Kessenich149afc32018-08-14 13:31:43 -06005003 std::vector<spv::IdImmediate> operands;
John Kessenich56bab042015-09-16 10:54:31 -06005004 auto opIt = arguments.begin();
John Kessenich149afc32018-08-14 13:31:43 -06005005 spv::IdImmediate image = { true, *(opIt++) };
5006 operands.push_back(image);
John Kessenich6c292d32016-02-15 20:58:50 -07005007
5008 // Handle subpass operations
5009 // TODO: GLSL should change to have the "MS" only on the type rather than the
5010 // built-in function.
5011 if (cracked.subpass) {
5012 // add on the (0,0) coordinate
5013 spv::Id zero = builder.makeIntConstant(0);
5014 std::vector<spv::Id> comps;
5015 comps.push_back(zero);
5016 comps.push_back(zero);
John Kessenich149afc32018-08-14 13:31:43 -06005017 spv::IdImmediate coord = { true,
5018 builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) };
5019 operands.push_back(coord);
John Kessenichf43c7392019-03-31 10:51:57 -06005020 spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone };
5021 imageOperands.word = imageOperands.word | signExtensionMask();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005022 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06005023 imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask;
5024 }
5025 if (imageOperands.word != spv::ImageOperandsMaskNone) {
John Kessenich149afc32018-08-14 13:31:43 -06005026 operands.push_back(imageOperands);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005027 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06005028 spv::IdImmediate imageOperand = { true, *(opIt++) };
5029 operands.push_back(imageOperand);
5030 }
John Kessenich6c292d32016-02-15 20:58:50 -07005031 }
John Kessenichfe4e5722017-10-19 02:07:30 -06005032 spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands);
5033 builder.setPrecision(result, precision);
5034 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07005035 }
5036
John Kessenich149afc32018-08-14 13:31:43 -06005037 spv::IdImmediate coord = { true, *(opIt++) };
5038 operands.push_back(coord);
Rex Xu129799a2017-07-05 17:23:28 +08005039 if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005040 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005041 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005042 mask = mask | spv::ImageOperandsSampleMask;
5043 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005044 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08005045 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
5046 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
Jeff Bolz36831c92018-09-05 10:11:41 -05005047 mask = mask | spv::ImageOperandsLodMask;
John Kessenich55e7d112015-11-15 21:33:39 -07005048 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005049 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
5050 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06005051 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06005052 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005053 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
5054 operands.push_back(imageOperands);
5055 }
5056 if (mask & spv::ImageOperandsSampleMask) {
5057 spv::IdImmediate imageOperand = { true, *opIt++ };
5058 operands.push_back(imageOperand);
5059 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005060 if (mask & spv::ImageOperandsLodMask) {
5061 spv::IdImmediate imageOperand = { true, *opIt++ };
5062 operands.push_back(imageOperand);
5063 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005064 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06005065 spv::IdImmediate imageOperand = { true,
5066 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05005067 operands.push_back(imageOperand);
5068 }
5069
John Kessenich149afc32018-08-14 13:31:43 -06005070 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07005071 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
John Kessenichfe4e5722017-10-19 02:07:30 -06005072
John Kessenich149afc32018-08-14 13:31:43 -06005073 std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands));
LoopDawg4425f242018-02-18 11:40:01 -07005074 builder.setPrecision(result[0], precision);
5075
5076 // If needed, add a conversion constructor to the proper size.
5077 if (components != node->getType().getVectorSize())
5078 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
5079
5080 return result[0];
Rex Xu129799a2017-07-05 17:23:28 +08005081 } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) {
Rex Xu129799a2017-07-05 17:23:28 +08005082
Jeff Bolz36831c92018-09-05 10:11:41 -05005083 // Push the texel value before the operands
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005084 if (sampler.isMultiSample() || cracked.lod) {
John Kessenich149afc32018-08-14 13:31:43 -06005085 spv::IdImmediate texel = { true, *(opIt + 1) };
5086 operands.push_back(texel);
John Kessenich149afc32018-08-14 13:31:43 -06005087 } else {
5088 spv::IdImmediate texel = { true, *opIt };
5089 operands.push_back(texel);
5090 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005091
5092 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005093 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005094 mask = mask | spv::ImageOperandsSampleMask;
5095 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005096 if (cracked.lod) {
5097 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
5098 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
5099 mask = mask | spv::ImageOperandsLodMask;
5100 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005101 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
5102 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06005103 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06005104 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005105 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
5106 operands.push_back(imageOperands);
5107 }
5108 if (mask & spv::ImageOperandsSampleMask) {
5109 spv::IdImmediate imageOperand = { true, *opIt++ };
5110 operands.push_back(imageOperand);
5111 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005112 if (mask & spv::ImageOperandsLodMask) {
5113 spv::IdImmediate imageOperand = { true, *opIt++ };
5114 operands.push_back(imageOperand);
5115 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005116 if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06005117 spv::IdImmediate imageOperand = { true,
5118 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05005119 operands.push_back(imageOperand);
5120 }
5121
John Kessenich56bab042015-09-16 10:54:31 -06005122 builder.createNoResultOp(spv::OpImageWrite, operands);
John Kessenich149afc32018-08-14 13:31:43 -06005123 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07005124 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
John Kessenich56bab042015-09-16 10:54:31 -06005125 return spv::NoResult;
John Kessenichf43c7392019-03-31 10:51:57 -06005126 } else if (node->getOp() == glslang::EOpSparseImageLoad ||
5127 node->getOp() == glslang::EOpSparseImageLoadLod) {
Rex Xu5eafa472016-02-19 22:24:03 +08005128 builder.addCapability(spv::CapabilitySparseResidency);
John Kessenich149afc32018-08-14 13:31:43 -06005129 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
Rex Xu5eafa472016-02-19 22:24:03 +08005130 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
5131
Jeff Bolz36831c92018-09-05 10:11:41 -05005132 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005133 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005134 mask = mask | spv::ImageOperandsSampleMask;
5135 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005136 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08005137 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
5138 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
5139
Jeff Bolz36831c92018-09-05 10:11:41 -05005140 mask = mask | spv::ImageOperandsLodMask;
5141 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005142 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
5143 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06005144 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06005145 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05005146 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
John Kessenich149afc32018-08-14 13:31:43 -06005147 operands.push_back(imageOperands);
Jeff Bolz36831c92018-09-05 10:11:41 -05005148 }
5149 if (mask & spv::ImageOperandsSampleMask) {
John Kessenich149afc32018-08-14 13:31:43 -06005150 spv::IdImmediate imageOperand = { true, *opIt++ };
5151 operands.push_back(imageOperand);
Jeff Bolz36831c92018-09-05 10:11:41 -05005152 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005153 if (mask & spv::ImageOperandsLodMask) {
5154 spv::IdImmediate imageOperand = { true, *opIt++ };
5155 operands.push_back(imageOperand);
5156 }
Jeff Bolz36831c92018-09-05 10:11:41 -05005157 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
John Kessenich8985fc92020-03-03 10:25:07 -07005158 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(
5159 TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05005160 operands.push_back(imageOperand);
Rex Xu5eafa472016-02-19 22:24:03 +08005161 }
5162
5163 // Create the return type that was a special structure
5164 spv::Id texelOut = *opIt;
John Kessenich8c8505c2016-07-26 12:50:38 -06005165 spv::Id typeId0 = resultType();
Rex Xu5eafa472016-02-19 22:24:03 +08005166 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
5167 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
5168
5169 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
5170
5171 // Decode the return type
5172 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
5173 return builder.createCompositeExtract(resultId, typeId0, 0);
John Kessenichcd261442016-01-22 09:54:12 -07005174 } else {
Rex Xu6b86d492015-09-16 17:48:22 +08005175 // Process image atomic operations
5176
5177 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
5178 // as the first source operand, is required by SPIR-V atomic operations.
John Kessenich149afc32018-08-14 13:31:43 -06005179 // For non-MS, the sample value should be 0
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005180 spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) };
John Kessenich149afc32018-08-14 13:31:43 -06005181 operands.push_back(sample);
John Kessenich140f3df2015-06-26 16:58:36 -06005182
Jeff Bolz36831c92018-09-05 10:11:41 -05005183 spv::Id resultTypeId;
5184 // imageAtomicStore has a void return type so base the pointer type on
5185 // the type of the value operand.
5186 if (node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufb18b6d2020-02-22 22:04:31 +08005187 resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt));
Jeff Bolz36831c92018-09-05 10:11:41 -05005188 } else {
5189 resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
5190 }
John Kessenich56bab042015-09-16 10:54:31 -06005191 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
Jeff Bolz39ffdaf2020-03-09 10:48:12 -05005192 if (imageType.getQualifier().nonUniform) {
5193 builder.addDecoration(pointer, spv::DecorationNonUniformEXT);
5194 }
Rex Xufc618912015-09-09 16:42:49 +08005195
5196 std::vector<spv::Id> operands;
5197 operands.push_back(pointer);
5198 for (; opIt != arguments.end(); ++opIt)
5199 operands.push_back(*opIt);
5200
John Kessenich8985fc92020-03-03 10:25:07 -07005201 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(),
5202 lvalueCoherentFlags);
Rex Xufc618912015-09-09 16:42:49 +08005203 }
5204 }
5205
John Kessenicha28f7a72019-08-06 07:00:58 -06005206#ifndef GLSLANG_WEB
amhagan05506bb2017-06-13 16:53:02 -04005207 // Check for fragment mask functions other than queries
5208 if (cracked.fragMask) {
5209 assert(sampler.ms);
5210
5211 auto opIt = arguments.begin();
5212 std::vector<spv::Id> operands;
5213
amhagan05506bb2017-06-13 16:53:02 -04005214 operands.push_back(params.sampler);
5215 ++opIt;
5216
5217 if (sampler.isSubpass()) {
5218 // add on the (0,0) coordinate
5219 spv::Id zero = builder.makeIntConstant(0);
5220 std::vector<spv::Id> comps;
5221 comps.push_back(zero);
5222 comps.push_back(zero);
John Kessenich8985fc92020-03-03 10:25:07 -07005223 operands.push_back(builder.makeCompositeConstant(
5224 builder.makeVectorType(builder.makeIntType(32), 2), comps));
amhagan05506bb2017-06-13 16:53:02 -04005225 }
5226
5227 for (; opIt != arguments.end(); ++opIt)
5228 operands.push_back(*opIt);
5229
5230 spv::Op fragMaskOp = spv::OpNop;
5231 if (node->getOp() == glslang::EOpFragmentMaskFetch)
5232 fragMaskOp = spv::OpFragmentMaskFetchAMD;
5233 else if (node->getOp() == glslang::EOpFragmentFetch)
5234 fragMaskOp = spv::OpFragmentFetchAMD;
5235
5236 builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask);
5237 builder.addCapability(spv::CapabilityFragmentMaskAMD);
5238 return builder.createOp(fragMaskOp, resultType(), operands);
5239 }
5240#endif
5241
Rex Xufc618912015-09-09 16:42:49 +08005242 // Check for texture functions other than queries
Rex Xu48edadf2015-12-31 16:11:41 +08005243 bool sparse = node->isSparseTexture();
Chao Chen3a137962018-09-19 11:41:27 -07005244 bool imageFootprint = node->isImageFootprint();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005245 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow();
Rex Xu71519fe2015-11-11 15:35:47 +08005246
John Kessenichfc51d282015-08-19 13:34:18 -06005247 // check for bias argument
5248 bool bias = false;
Rex Xu225e0fc2016-11-17 17:47:59 +08005249 if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06005250 int nonBiasArgCount = 2;
Rex Xu225e0fc2016-11-17 17:47:59 +08005251 if (cracked.gather)
5252 ++nonBiasArgCount; // comp argument should be present when bias argument is present
Rex Xu1e5d7b02016-11-29 17:36:31 +08005253
5254 if (f16ShadowCompare)
5255 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06005256 if (cracked.offset)
5257 ++nonBiasArgCount;
Rex Xu225e0fc2016-11-17 17:47:59 +08005258 else if (cracked.offsets)
5259 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06005260 if (cracked.grad)
5261 nonBiasArgCount += 2;
Rex Xu48edadf2015-12-31 16:11:41 +08005262 if (cracked.lodClamp)
5263 ++nonBiasArgCount;
5264 if (sparse)
5265 ++nonBiasArgCount;
Chao Chen3a137962018-09-19 11:41:27 -07005266 if (imageFootprint)
5267 //Following three extra arguments
5268 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
5269 nonBiasArgCount += 3;
John Kessenichfc51d282015-08-19 13:34:18 -06005270 if ((int)arguments.size() > nonBiasArgCount)
5271 bias = true;
5272 }
5273
John Kessenicha28f7a72019-08-06 07:00:58 -06005274#ifndef GLSLANG_WEB
Rex Xu225e0fc2016-11-17 17:47:59 +08005275 if (cracked.gather) {
5276 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
5277 if (bias || cracked.lod ||
5278 sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) {
5279 builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod);
Rex Xu301a2bc2017-06-14 23:09:39 +08005280 builder.addCapability(spv::CapabilityImageGatherBiasLodAMD);
Rex Xu225e0fc2016-11-17 17:47:59 +08005281 }
5282 }
5283#endif
5284
John Kessenichfc51d282015-08-19 13:34:18 -06005285 // set the rest of the arguments
John Kessenich55e7d112015-11-15 21:33:39 -07005286
John Kessenichfc51d282015-08-19 13:34:18 -06005287 params.coords = arguments[1];
5288 int extraArgs = 0;
John Kessenich019f08f2016-02-15 15:40:42 -07005289 bool noImplicitLod = false;
John Kessenich55e7d112015-11-15 21:33:39 -07005290
5291 // sort out where Dref is coming from
Rex Xu1e5d7b02016-11-29 17:36:31 +08005292 if (cubeCompare || f16ShadowCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06005293 params.Dref = arguments[2];
Rex Xu48edadf2015-12-31 16:11:41 +08005294 ++extraArgs;
5295 } else if (sampler.shadow && cracked.gather) {
John Kessenich55e7d112015-11-15 21:33:39 -07005296 params.Dref = arguments[2];
5297 ++extraArgs;
5298 } else if (sampler.shadow) {
John Kessenichfc51d282015-08-19 13:34:18 -06005299 std::vector<spv::Id> indexes;
John Kessenich76d4dfc2016-06-16 12:43:23 -06005300 int dRefComp;
John Kessenichfc51d282015-08-19 13:34:18 -06005301 if (cracked.proj)
John Kessenich76d4dfc2016-06-16 12:43:23 -06005302 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
John Kessenichfc51d282015-08-19 13:34:18 -06005303 else
John Kessenich76d4dfc2016-06-16 12:43:23 -06005304 dRefComp = builder.getNumComponents(params.coords) - 1;
5305 indexes.push_back(dRefComp);
John Kessenich8985fc92020-03-03 10:25:07 -07005306 params.Dref = builder.createCompositeExtract(params.coords,
5307 builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
John Kessenichfc51d282015-08-19 13:34:18 -06005308 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005309
5310 // lod
John Kessenichfc51d282015-08-19 13:34:18 -06005311 if (cracked.lod) {
LoopDawgef94b1a2017-07-24 18:45:37 -06005312 params.lod = arguments[2 + extraArgs];
John Kessenichfc51d282015-08-19 13:34:18 -06005313 ++extraArgs;
John Kessenichb9197c82019-08-11 07:41:45 -06005314 } else if (glslangIntermediate->getStage() != EShLangFragment &&
5315 !(glslangIntermediate->getStage() == EShLangCompute &&
5316 glslangIntermediate->hasLayoutDerivativeModeNone())) {
John Kessenich019f08f2016-02-15 15:40:42 -07005317 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
5318 noImplicitLod = true;
5319 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005320
5321 // multisample
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005322 if (sampler.isMultiSample()) {
LoopDawgef94b1a2017-07-24 18:45:37 -06005323 params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified
Rex Xu04db3f52015-09-16 11:44:02 +08005324 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06005325 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005326
5327 // gradient
John Kessenichfc51d282015-08-19 13:34:18 -06005328 if (cracked.grad) {
5329 params.gradX = arguments[2 + extraArgs];
5330 params.gradY = arguments[3 + extraArgs];
5331 extraArgs += 2;
5332 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005333
5334 // offset and offsets
John Kessenich55e7d112015-11-15 21:33:39 -07005335 if (cracked.offset) {
John Kessenichfc51d282015-08-19 13:34:18 -06005336 params.offset = arguments[2 + extraArgs];
5337 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07005338 } else if (cracked.offsets) {
5339 params.offsets = arguments[2 + extraArgs];
5340 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06005341 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005342
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005343#ifndef GLSLANG_WEB
John Kessenich76d4dfc2016-06-16 12:43:23 -06005344 // lod clamp
Rex Xu48edadf2015-12-31 16:11:41 +08005345 if (cracked.lodClamp) {
5346 params.lodClamp = arguments[2 + extraArgs];
5347 ++extraArgs;
5348 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005349 // sparse
Rex Xu48edadf2015-12-31 16:11:41 +08005350 if (sparse) {
5351 params.texelOut = arguments[2 + extraArgs];
5352 ++extraArgs;
5353 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005354 // gather component
John Kessenich55e7d112015-11-15 21:33:39 -07005355 if (cracked.gather && ! sampler.shadow) {
5356 // default component is 0, if missing, otherwise an argument
5357 if (2 + extraArgs < (int)arguments.size()) {
John Kessenich76d4dfc2016-06-16 12:43:23 -06005358 params.component = arguments[2 + extraArgs];
John Kessenich55e7d112015-11-15 21:33:39 -07005359 ++extraArgs;
Rex Xu225e0fc2016-11-17 17:47:59 +08005360 } else
John Kessenich76d4dfc2016-06-16 12:43:23 -06005361 params.component = builder.makeIntConstant(0);
Rex Xu225e0fc2016-11-17 17:47:59 +08005362 }
Chao Chen3a137962018-09-19 11:41:27 -07005363 spv::Id resultStruct = spv::NoResult;
5364 if (imageFootprint) {
5365 //Following three extra arguments
5366 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
5367 params.granularity = arguments[2 + extraArgs];
5368 params.coarse = arguments[3 + extraArgs];
5369 resultStruct = arguments[4 + extraArgs];
5370 extraArgs += 3;
5371 }
5372#endif
Rex Xu225e0fc2016-11-17 17:47:59 +08005373 // bias
5374 if (bias) {
5375 params.bias = arguments[2 + extraArgs];
5376 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07005377 }
John Kessenichfc51d282015-08-19 13:34:18 -06005378
John Kessenicha28f7a72019-08-06 07:00:58 -06005379#ifndef GLSLANG_WEB
Chao Chen3a137962018-09-19 11:41:27 -07005380 if (imageFootprint) {
5381 builder.addExtension(spv::E_SPV_NV_shader_image_footprint);
5382 builder.addCapability(spv::CapabilityImageFootprintNV);
5383
5384
5385 //resultStructType(OpenGL type) contains 5 elements:
5386 //struct gl_TextureFootprint2DNV {
5387 // uvec2 anchor;
5388 // uvec2 offset;
5389 // uvec2 mask;
5390 // uint lod;
5391 // uint granularity;
5392 //};
5393 //or
5394 //struct gl_TextureFootprint3DNV {
5395 // uvec3 anchor;
5396 // uvec3 offset;
5397 // uvec2 mask;
5398 // uint lod;
5399 // uint granularity;
5400 //};
5401 spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct));
5402 assert(builder.isStructType(resultStructType));
5403
5404 //resType (SPIR-V type) contains 6 elements:
5405 //Member 0 must be a Boolean type scalar(LOD),
5406 //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor),
5407 //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset),
5408 //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask),
5409 //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod),
5410 //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity).
5411 std::vector<spv::Id> members;
5412 members.push_back(resultType());
5413 for (int i = 0; i < 5; i++) {
5414 members.push_back(builder.getContainedTypeId(resultStructType, i));
5415 }
5416 spv::Id resType = builder.makeStructType(members, "ResType");
5417
5418 //call ImageFootprintNV
John Kessenichf43c7392019-03-31 10:51:57 -06005419 spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj,
5420 cracked.gather, noImplicitLod, params, signExtensionMask());
Chao Chen3a137962018-09-19 11:41:27 -07005421
5422 //copy resType (SPIR-V type) to resultStructType(OpenGL type)
5423 for (int i = 0; i < 5; i++) {
5424 builder.clearAccessChain();
5425 builder.setAccessChainLValue(resultStruct);
5426
5427 //Accessing to a struct we created, no coherent flag is set
5428 spv::Builder::AccessChain::CoherentFlags flags;
5429 flags.clear();
5430
Jeff Bolz9f2aec42019-01-06 17:58:04 -06005431 builder.accessChainPush(builder.makeIntConstant(i), flags, 0);
John Kessenich8985fc92020-03-03 10:25:07 -07005432 builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1),
Bas Nieuwenhuizende949a22020-08-24 23:27:26 +02005433 i+1), TranslateNonUniformDecoration(imageType.getQualifier()));
Chao Chen3a137962018-09-19 11:41:27 -07005434 }
5435 return builder.createCompositeExtract(res, resultType(), 0);
5436 }
5437#endif
5438
John Kessenich65336482016-06-16 14:06:26 -06005439 // projective component (might not to move)
5440 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
5441 // are divided by the last component of P."
5442 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
5443 // unused components will appear after all used components."
5444 if (cracked.proj) {
5445 int projSourceComp = builder.getNumComponents(params.coords) - 1;
5446 int projTargetComp;
5447 switch (sampler.dim) {
5448 case glslang::Esd1D: projTargetComp = 1; break;
5449 case glslang::Esd2D: projTargetComp = 2; break;
5450 case glslang::EsdRect: projTargetComp = 2; break;
5451 default: projTargetComp = projSourceComp; break;
5452 }
5453 // copy the projective coordinate if we have to
5454 if (projTargetComp != projSourceComp) {
John Kessenichecba76f2017-01-06 00:34:48 -07005455 spv::Id projComp = builder.createCompositeExtract(params.coords,
John Kessenich8985fc92020-03-03 10:25:07 -07005456 builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp);
John Kessenich65336482016-06-16 14:06:26 -06005457 params.coords = builder.createCompositeInsert(projComp, params.coords,
John Kessenich8985fc92020-03-03 10:25:07 -07005458 builder.getTypeId(params.coords), projTargetComp);
John Kessenich65336482016-06-16 14:06:26 -06005459 }
5460 }
5461
John Kessenichf8d1d742019-10-21 06:55:11 -06005462#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -05005463 // nonprivate
5464 if (imageType.getQualifier().nonprivate) {
5465 params.nonprivate = true;
5466 }
5467
5468 // volatile
5469 if (imageType.getQualifier().volatil) {
5470 params.volatil = true;
5471 }
John Kessenichf8d1d742019-10-21 06:55:11 -06005472#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05005473
St0fFa1184dd2018-04-09 21:08:14 +02005474 std::vector<spv::Id> result( 1,
John Kessenichf43c7392019-03-31 10:51:57 -06005475 builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather,
5476 noImplicitLod, params, signExtensionMask())
St0fFa1184dd2018-04-09 21:08:14 +02005477 );
LoopDawg4425f242018-02-18 11:40:01 -07005478
5479 if (components != node->getType().getVectorSize())
5480 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
5481
5482 return result[0];
John Kessenich140f3df2015-06-26 16:58:36 -06005483}
5484
5485spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
5486{
5487 // Grab the function's pointer from the previously created function
5488 spv::Function* function = functionMap[node->getName().c_str()];
5489 if (! function)
5490 return 0;
5491
5492 const glslang::TIntermSequence& glslangArgs = node->getSequence();
5493 const glslang::TQualifierList& qualifiers = node->getQualifierList();
5494
5495 // See comments in makeFunctions() for details about the semantics for parameter passing.
5496 //
5497 // These imply we need a four step process:
5498 // 1. Evaluate the arguments
5499 // 2. Allocate and make copies of in, out, and inout arguments
5500 // 3. Make the call
5501 // 4. Copy back the results
5502
John Kessenichd3ed90b2018-05-04 11:43:03 -06005503 // 1. Evaluate the arguments and their types
John Kessenich140f3df2015-06-26 16:58:36 -06005504 std::vector<spv::Builder::AccessChain> lValues;
5505 std::vector<spv::Id> rValues;
John Kessenich32cfd492016-02-02 12:37:46 -07005506 std::vector<const glslang::TType*> argTypes;
John Kessenich140f3df2015-06-26 16:58:36 -06005507 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005508 argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06005509 // build l-value
5510 builder.clearAccessChain();
5511 glslangArgs[a]->traverse(this);
John Kessenichd41993d2017-09-10 15:21:05 -06005512 // keep outputs and pass-by-originals as l-values, evaluate others as r-values
John Kessenichd3ed90b2018-05-04 11:43:03 -06005513 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) ||
John Kessenich6a14f782017-12-04 02:48:10 -07005514 writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005515 // save l-value
5516 lValues.push_back(builder.getAccessChain());
5517 } else {
5518 // process r-value
John Kessenich32cfd492016-02-02 12:37:46 -07005519 rValues.push_back(accessChainLoad(*argTypes.back()));
John Kessenich140f3df2015-06-26 16:58:36 -06005520 }
5521 }
5522
5523 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
5524 // copy the original into that space.
5525 //
5526 // Also, build up the list of actual arguments to pass in for the call
5527 int lValueCount = 0;
5528 int rValueCount = 0;
5529 std::vector<spv::Id> spvArgs;
5530 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
5531 spv::Id arg;
John Kessenichd3ed90b2018-05-04 11:43:03 -06005532 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07005533 builder.setAccessChain(lValues[lValueCount]);
5534 arg = builder.accessChainGetLValue();
5535 ++lValueCount;
John Kessenichd41993d2017-09-10 15:21:05 -06005536 } else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005537 // need space to hold the copy
John Kessenich435dd802020-06-30 01:27:08 -06005538 arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction,
John Kessenich8985fc92020-03-03 10:25:07 -07005539 builder.getContainedTypeId(function->getParamType(a)), "param");
John Kessenich140f3df2015-06-26 16:58:36 -06005540 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
5541 // need to copy the input into output space
5542 builder.setAccessChain(lValues[lValueCount]);
John Kessenich32cfd492016-02-02 12:37:46 -07005543 spv::Id copy = accessChainLoad(*argTypes[a]);
John Kessenich4bf71552016-09-02 11:20:21 -06005544 builder.clearAccessChain();
5545 builder.setAccessChainLValue(arg);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005546 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005547 }
5548 ++lValueCount;
5549 } else {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005550 // process r-value, which involves a copy for a type mismatch
John Kessenich4df10332020-06-26 08:37:06 -06005551 if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) ||
John Kessenich435dd802020-06-30 01:27:08 -06005552 TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a))
John Kessenich4df10332020-06-26 08:37:06 -06005553 {
John Kessenich435dd802020-06-30 01:27:08 -06005554 spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, function->getParamType(a), "arg");
John Kessenichd3ed90b2018-05-04 11:43:03 -06005555 builder.clearAccessChain();
5556 builder.setAccessChainLValue(argCopy);
5557 multiTypeStore(*argTypes[a], rValues[rValueCount]);
John Kessenich435dd802020-06-30 01:27:08 -06005558 arg = builder.createLoad(argCopy, function->getParamPrecision(a));
John Kessenichd3ed90b2018-05-04 11:43:03 -06005559 } else
5560 arg = rValues[rValueCount];
John Kessenich140f3df2015-06-26 16:58:36 -06005561 ++rValueCount;
5562 }
5563 spvArgs.push_back(arg);
5564 }
5565
5566 // 3. Make the call.
5567 spv::Id result = builder.createFunctionCall(function, spvArgs);
John Kessenich32cfd492016-02-02 12:37:46 -07005568 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
greg-lunarg639f5462020-11-12 11:10:07 -07005569 builder.addDecoration(result, TranslateNonUniformDecoration(node->getType().getQualifier()));
John Kessenich140f3df2015-06-26 16:58:36 -06005570
5571 // 4. Copy back out an "out" arguments.
5572 lValueCount = 0;
5573 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005574 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0))
John Kessenichd41993d2017-09-10 15:21:05 -06005575 ++lValueCount;
5576 else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005577 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
John Kessenich435dd802020-06-30 01:27:08 -06005578 spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision);
greg-lunarg639f5462020-11-12 11:10:07 -07005579 builder.addDecoration(copy, TranslateNonUniformDecoration(argTypes[a]->getQualifier()));
John Kessenich140f3df2015-06-26 16:58:36 -06005580 builder.setAccessChain(lValues[lValueCount]);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005581 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005582 }
5583 ++lValueCount;
5584 }
5585 }
5586
5587 return result;
5588}
5589
5590// Translate AST operation to SPV operation, already having SPV-based operands/types.
John Kessenichead86222018-03-28 18:01:20 -06005591spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06005592 spv::Id typeId, spv::Id left, spv::Id right,
5593 glslang::TBasicType typeProxy, bool reduceComparison)
5594{
John Kessenich66011cb2018-03-06 16:12:04 -07005595 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5596 bool isFloat = isTypeFloat(typeProxy);
Rex Xuc7d36562016-04-27 08:15:37 +08005597 bool isBool = typeProxy == glslang::EbtBool;
John Kessenich140f3df2015-06-26 16:58:36 -06005598
5599 spv::Op binOp = spv::OpNop;
John Kessenichec43d0a2015-07-04 17:17:31 -06005600 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
John Kessenich140f3df2015-06-26 16:58:36 -06005601 bool comparison = false;
5602
5603 switch (op) {
5604 case glslang::EOpAdd:
5605 case glslang::EOpAddAssign:
5606 if (isFloat)
5607 binOp = spv::OpFAdd;
5608 else
5609 binOp = spv::OpIAdd;
5610 break;
5611 case glslang::EOpSub:
5612 case glslang::EOpSubAssign:
5613 if (isFloat)
5614 binOp = spv::OpFSub;
5615 else
5616 binOp = spv::OpISub;
5617 break;
5618 case glslang::EOpMul:
5619 case glslang::EOpMulAssign:
5620 if (isFloat)
5621 binOp = spv::OpFMul;
5622 else
5623 binOp = spv::OpIMul;
5624 break;
5625 case glslang::EOpVectorTimesScalar:
5626 case glslang::EOpVectorTimesScalarAssign:
John Kessenich8d72f1a2016-05-20 12:06:03 -06005627 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
John Kessenichec43d0a2015-07-04 17:17:31 -06005628 if (builder.isVector(right))
5629 std::swap(left, right);
5630 assert(builder.isScalar(right));
5631 needMatchingVectors = false;
5632 binOp = spv::OpVectorTimesScalar;
t.jung697fdf02018-11-14 13:04:39 +01005633 } else if (isFloat)
5634 binOp = spv::OpFMul;
5635 else
John Kessenichec43d0a2015-07-04 17:17:31 -06005636 binOp = spv::OpIMul;
John Kessenich140f3df2015-06-26 16:58:36 -06005637 break;
5638 case glslang::EOpVectorTimesMatrix:
5639 case glslang::EOpVectorTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005640 binOp = spv::OpVectorTimesMatrix;
5641 break;
5642 case glslang::EOpMatrixTimesVector:
John Kessenich140f3df2015-06-26 16:58:36 -06005643 binOp = spv::OpMatrixTimesVector;
5644 break;
5645 case glslang::EOpMatrixTimesScalar:
5646 case glslang::EOpMatrixTimesScalarAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005647 binOp = spv::OpMatrixTimesScalar;
5648 break;
5649 case glslang::EOpMatrixTimesMatrix:
5650 case glslang::EOpMatrixTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005651 binOp = spv::OpMatrixTimesMatrix;
5652 break;
5653 case glslang::EOpOuterProduct:
5654 binOp = spv::OpOuterProduct;
John Kessenichec43d0a2015-07-04 17:17:31 -06005655 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005656 break;
5657
5658 case glslang::EOpDiv:
5659 case glslang::EOpDivAssign:
5660 if (isFloat)
5661 binOp = spv::OpFDiv;
5662 else if (isUnsigned)
5663 binOp = spv::OpUDiv;
5664 else
5665 binOp = spv::OpSDiv;
5666 break;
5667 case glslang::EOpMod:
5668 case glslang::EOpModAssign:
5669 if (isFloat)
5670 binOp = spv::OpFMod;
5671 else if (isUnsigned)
5672 binOp = spv::OpUMod;
5673 else
5674 binOp = spv::OpSMod;
5675 break;
5676 case glslang::EOpRightShift:
5677 case glslang::EOpRightShiftAssign:
5678 if (isUnsigned)
5679 binOp = spv::OpShiftRightLogical;
5680 else
5681 binOp = spv::OpShiftRightArithmetic;
5682 break;
5683 case glslang::EOpLeftShift:
5684 case glslang::EOpLeftShiftAssign:
5685 binOp = spv::OpShiftLeftLogical;
5686 break;
5687 case glslang::EOpAnd:
5688 case glslang::EOpAndAssign:
5689 binOp = spv::OpBitwiseAnd;
5690 break;
5691 case glslang::EOpLogicalAnd:
John Kessenichec43d0a2015-07-04 17:17:31 -06005692 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005693 binOp = spv::OpLogicalAnd;
5694 break;
5695 case glslang::EOpInclusiveOr:
5696 case glslang::EOpInclusiveOrAssign:
5697 binOp = spv::OpBitwiseOr;
5698 break;
5699 case glslang::EOpLogicalOr:
John Kessenichec43d0a2015-07-04 17:17:31 -06005700 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005701 binOp = spv::OpLogicalOr;
5702 break;
5703 case glslang::EOpExclusiveOr:
5704 case glslang::EOpExclusiveOrAssign:
5705 binOp = spv::OpBitwiseXor;
5706 break;
5707 case glslang::EOpLogicalXor:
John Kessenichec43d0a2015-07-04 17:17:31 -06005708 needMatchingVectors = false;
John Kessenich5e4b1242015-08-06 22:53:06 -06005709 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005710 break;
5711
Ian Romanickb3bd4022019-01-21 08:57:25 -08005712 case glslang::EOpAbsDifference:
5713 binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL;
5714 break;
5715
5716 case glslang::EOpAddSaturate:
5717 binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL;
5718 break;
5719
5720 case glslang::EOpSubSaturate:
5721 binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL;
5722 break;
5723
5724 case glslang::EOpAverage:
5725 binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL;
5726 break;
5727
5728 case glslang::EOpAverageRounded:
5729 binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL;
5730 break;
5731
5732 case glslang::EOpMul32x16:
5733 binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL;
5734 break;
5735
John Kessenich140f3df2015-06-26 16:58:36 -06005736 case glslang::EOpLessThan:
5737 case glslang::EOpGreaterThan:
5738 case glslang::EOpLessThanEqual:
5739 case glslang::EOpGreaterThanEqual:
5740 case glslang::EOpEqual:
5741 case glslang::EOpNotEqual:
5742 case glslang::EOpVectorEqual:
5743 case glslang::EOpVectorNotEqual:
5744 comparison = true;
5745 break;
5746 default:
5747 break;
5748 }
5749
John Kessenich7c1aa102015-10-15 13:29:11 -06005750 // handle mapped binary operations (should be non-comparison)
John Kessenich140f3df2015-06-26 16:58:36 -06005751 if (binOp != spv::OpNop) {
John Kessenich7c1aa102015-10-15 13:29:11 -06005752 assert(comparison == false);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005753 if (builder.isMatrix(left) || builder.isMatrix(right) ||
5754 builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
John Kessenichead86222018-03-28 18:01:20 -06005755 return createBinaryMatrixOperation(binOp, decorations, typeId, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005756
5757 // No matrix involved; make both operands be the same number of components, if needed
John Kessenichec43d0a2015-07-04 17:17:31 -06005758 if (needMatchingVectors)
John Kessenichead86222018-03-28 18:01:20 -06005759 builder.promoteScalar(decorations.precision, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005760
qining25262b32016-05-06 17:25:16 -04005761 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005762 decorations.addNoContraction(builder, result);
5763 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005764 return builder.setPrecision(result, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06005765 }
5766
5767 if (! comparison)
5768 return 0;
5769
John Kessenich7c1aa102015-10-15 13:29:11 -06005770 // Handle comparison instructions
John Kessenich140f3df2015-06-26 16:58:36 -06005771
John Kessenich4583b612016-08-07 19:14:22 -06005772 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
John Kessenichead86222018-03-28 18:01:20 -06005773 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) {
5774 spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual);
John Kessenichb9197c82019-08-11 07:41:45 -06005775 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005776 return result;
5777 }
John Kessenich140f3df2015-06-26 16:58:36 -06005778
5779 switch (op) {
5780 case glslang::EOpLessThan:
5781 if (isFloat)
5782 binOp = spv::OpFOrdLessThan;
5783 else if (isUnsigned)
5784 binOp = spv::OpULessThan;
5785 else
5786 binOp = spv::OpSLessThan;
5787 break;
5788 case glslang::EOpGreaterThan:
5789 if (isFloat)
5790 binOp = spv::OpFOrdGreaterThan;
5791 else if (isUnsigned)
5792 binOp = spv::OpUGreaterThan;
5793 else
5794 binOp = spv::OpSGreaterThan;
5795 break;
5796 case glslang::EOpLessThanEqual:
5797 if (isFloat)
5798 binOp = spv::OpFOrdLessThanEqual;
5799 else if (isUnsigned)
5800 binOp = spv::OpULessThanEqual;
5801 else
5802 binOp = spv::OpSLessThanEqual;
5803 break;
5804 case glslang::EOpGreaterThanEqual:
5805 if (isFloat)
5806 binOp = spv::OpFOrdGreaterThanEqual;
5807 else if (isUnsigned)
5808 binOp = spv::OpUGreaterThanEqual;
5809 else
5810 binOp = spv::OpSGreaterThanEqual;
5811 break;
5812 case glslang::EOpEqual:
5813 case glslang::EOpVectorEqual:
5814 if (isFloat)
5815 binOp = spv::OpFOrdEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005816 else if (isBool)
5817 binOp = spv::OpLogicalEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005818 else
5819 binOp = spv::OpIEqual;
5820 break;
5821 case glslang::EOpNotEqual:
5822 case glslang::EOpVectorNotEqual:
5823 if (isFloat)
Graeme Leese65ce5662020-06-05 13:32:51 +01005824 binOp = spv::OpFUnordNotEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005825 else if (isBool)
5826 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005827 else
5828 binOp = spv::OpINotEqual;
5829 break;
5830 default:
5831 break;
5832 }
5833
qining25262b32016-05-06 17:25:16 -04005834 if (binOp != spv::OpNop) {
5835 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005836 decorations.addNoContraction(builder, result);
5837 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005838 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005839 }
John Kessenich140f3df2015-06-26 16:58:36 -06005840
5841 return 0;
5842}
5843
John Kessenich04bb8a02015-12-12 12:28:14 -07005844//
5845// Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
5846// These can be any of:
5847//
5848// matrix * scalar
5849// scalar * matrix
5850// matrix * matrix linear algebraic
5851// matrix * vector
5852// vector * matrix
5853// matrix * matrix componentwise
5854// matrix op matrix op in {+, -, /}
5855// matrix op scalar op in {+, -, /}
5856// scalar op matrix op in {+, -, /}
5857//
John Kessenichead86222018-03-28 18:01:20 -06005858spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5859 spv::Id left, spv::Id right)
John Kessenich04bb8a02015-12-12 12:28:14 -07005860{
5861 bool firstClass = true;
5862
5863 // First, handle first-class matrix operations (* and matrix/scalar)
5864 switch (op) {
5865 case spv::OpFDiv:
5866 if (builder.isMatrix(left) && builder.isScalar(right)) {
5867 // turn matrix / scalar into a multiply...
Neil Robertseddb1312018-03-13 10:57:59 +01005868 spv::Id resultType = builder.getTypeId(right);
5869 right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right);
John Kessenich04bb8a02015-12-12 12:28:14 -07005870 op = spv::OpMatrixTimesScalar;
5871 } else
5872 firstClass = false;
5873 break;
5874 case spv::OpMatrixTimesScalar:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005875 if (builder.isMatrix(right) || builder.isCooperativeMatrix(right))
John Kessenich04bb8a02015-12-12 12:28:14 -07005876 std::swap(left, right);
5877 assert(builder.isScalar(right));
5878 break;
5879 case spv::OpVectorTimesMatrix:
5880 assert(builder.isVector(left));
5881 assert(builder.isMatrix(right));
5882 break;
5883 case spv::OpMatrixTimesVector:
5884 assert(builder.isMatrix(left));
5885 assert(builder.isVector(right));
5886 break;
5887 case spv::OpMatrixTimesMatrix:
5888 assert(builder.isMatrix(left));
5889 assert(builder.isMatrix(right));
5890 break;
5891 default:
5892 firstClass = false;
5893 break;
5894 }
5895
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005896 if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
5897 firstClass = true;
5898
qining25262b32016-05-06 17:25:16 -04005899 if (firstClass) {
5900 spv::Id result = builder.createBinOp(op, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005901 decorations.addNoContraction(builder, result);
5902 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005903 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005904 }
John Kessenich04bb8a02015-12-12 12:28:14 -07005905
LoopDawg592860c2016-06-09 08:57:35 -06005906 // Handle component-wise +, -, *, %, and / for all combinations of type.
John Kessenich04bb8a02015-12-12 12:28:14 -07005907 // The result type of all of them is the same type as the (a) matrix operand.
5908 // The algorithm is to:
5909 // - break the matrix(es) into vectors
5910 // - smear any scalar to a vector
5911 // - do vector operations
5912 // - make a matrix out the vector results
5913 switch (op) {
5914 case spv::OpFAdd:
5915 case spv::OpFSub:
5916 case spv::OpFDiv:
LoopDawg592860c2016-06-09 08:57:35 -06005917 case spv::OpFMod:
John Kessenich04bb8a02015-12-12 12:28:14 -07005918 case spv::OpFMul:
5919 {
5920 // one time set up...
5921 bool leftMat = builder.isMatrix(left);
5922 bool rightMat = builder.isMatrix(right);
5923 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
5924 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
5925 spv::Id scalarType = builder.getScalarTypeId(typeId);
5926 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
5927 std::vector<spv::Id> results;
5928 spv::Id smearVec = spv::NoResult;
5929 if (builder.isScalar(left))
John Kessenichead86222018-03-28 18:01:20 -06005930 smearVec = builder.smearScalar(decorations.precision, left, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005931 else if (builder.isScalar(right))
John Kessenichead86222018-03-28 18:01:20 -06005932 smearVec = builder.smearScalar(decorations.precision, right, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005933
5934 // do each vector op
5935 for (unsigned int c = 0; c < numCols; ++c) {
5936 std::vector<unsigned int> indexes;
5937 indexes.push_back(c);
5938 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
5939 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
qining25262b32016-05-06 17:25:16 -04005940 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
John Kessenichb9197c82019-08-11 07:41:45 -06005941 decorations.addNoContraction(builder, result);
5942 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005943 results.push_back(builder.setPrecision(result, decorations.precision));
John Kessenich04bb8a02015-12-12 12:28:14 -07005944 }
5945
5946 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005947 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06005948 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005949 return result;
John Kessenich04bb8a02015-12-12 12:28:14 -07005950 }
5951 default:
5952 assert(0);
5953 return spv::NoResult;
5954 }
5955}
5956
John Kessenichead86222018-03-28 18:01:20 -06005957spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
John Kessenich8985fc92020-03-03 10:25:07 -07005958 spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06005959{
5960 spv::Op unaryOp = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08005961 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06005962 int libCall = -1;
John Kessenich66011cb2018-03-06 16:12:04 -07005963 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5964 bool isFloat = isTypeFloat(typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06005965
5966 switch (op) {
5967 case glslang::EOpNegative:
John Kessenich7a53f762016-01-20 11:19:27 -07005968 if (isFloat) {
John Kessenich140f3df2015-06-26 16:58:36 -06005969 unaryOp = spv::OpFNegate;
John Kessenich7a53f762016-01-20 11:19:27 -07005970 if (builder.isMatrixType(typeId))
John Kessenichead86222018-03-28 18:01:20 -06005971 return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy);
John Kessenich7a53f762016-01-20 11:19:27 -07005972 } else
John Kessenich140f3df2015-06-26 16:58:36 -06005973 unaryOp = spv::OpSNegate;
5974 break;
5975
5976 case glslang::EOpLogicalNot:
5977 case glslang::EOpVectorLogicalNot:
John Kessenich5e4b1242015-08-06 22:53:06 -06005978 unaryOp = spv::OpLogicalNot;
5979 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005980 case glslang::EOpBitwiseNot:
5981 unaryOp = spv::OpNot;
5982 break;
John Kessenich5e4b1242015-08-06 22:53:06 -06005983
John Kessenich140f3df2015-06-26 16:58:36 -06005984 case glslang::EOpDeterminant:
John Kessenich5e4b1242015-08-06 22:53:06 -06005985 libCall = spv::GLSLstd450Determinant;
John Kessenich140f3df2015-06-26 16:58:36 -06005986 break;
5987 case glslang::EOpMatrixInverse:
John Kessenich5e4b1242015-08-06 22:53:06 -06005988 libCall = spv::GLSLstd450MatrixInverse;
John Kessenich140f3df2015-06-26 16:58:36 -06005989 break;
5990 case glslang::EOpTranspose:
5991 unaryOp = spv::OpTranspose;
5992 break;
5993
5994 case glslang::EOpRadians:
John Kessenich5e4b1242015-08-06 22:53:06 -06005995 libCall = spv::GLSLstd450Radians;
John Kessenich140f3df2015-06-26 16:58:36 -06005996 break;
5997 case glslang::EOpDegrees:
John Kessenich5e4b1242015-08-06 22:53:06 -06005998 libCall = spv::GLSLstd450Degrees;
John Kessenich140f3df2015-06-26 16:58:36 -06005999 break;
6000 case glslang::EOpSin:
John Kessenich5e4b1242015-08-06 22:53:06 -06006001 libCall = spv::GLSLstd450Sin;
John Kessenich140f3df2015-06-26 16:58:36 -06006002 break;
6003 case glslang::EOpCos:
John Kessenich5e4b1242015-08-06 22:53:06 -06006004 libCall = spv::GLSLstd450Cos;
John Kessenich140f3df2015-06-26 16:58:36 -06006005 break;
6006 case glslang::EOpTan:
John Kessenich5e4b1242015-08-06 22:53:06 -06006007 libCall = spv::GLSLstd450Tan;
John Kessenich140f3df2015-06-26 16:58:36 -06006008 break;
6009 case glslang::EOpAcos:
John Kessenich5e4b1242015-08-06 22:53:06 -06006010 libCall = spv::GLSLstd450Acos;
John Kessenich140f3df2015-06-26 16:58:36 -06006011 break;
6012 case glslang::EOpAsin:
John Kessenich5e4b1242015-08-06 22:53:06 -06006013 libCall = spv::GLSLstd450Asin;
John Kessenich140f3df2015-06-26 16:58:36 -06006014 break;
6015 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06006016 libCall = spv::GLSLstd450Atan;
John Kessenich140f3df2015-06-26 16:58:36 -06006017 break;
6018
6019 case glslang::EOpAcosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006020 libCall = spv::GLSLstd450Acosh;
John Kessenich140f3df2015-06-26 16:58:36 -06006021 break;
6022 case glslang::EOpAsinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006023 libCall = spv::GLSLstd450Asinh;
John Kessenich140f3df2015-06-26 16:58:36 -06006024 break;
6025 case glslang::EOpAtanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006026 libCall = spv::GLSLstd450Atanh;
John Kessenich140f3df2015-06-26 16:58:36 -06006027 break;
6028 case glslang::EOpTanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006029 libCall = spv::GLSLstd450Tanh;
John Kessenich140f3df2015-06-26 16:58:36 -06006030 break;
6031 case glslang::EOpCosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006032 libCall = spv::GLSLstd450Cosh;
John Kessenich140f3df2015-06-26 16:58:36 -06006033 break;
6034 case glslang::EOpSinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06006035 libCall = spv::GLSLstd450Sinh;
John Kessenich140f3df2015-06-26 16:58:36 -06006036 break;
6037
6038 case glslang::EOpLength:
John Kessenich5e4b1242015-08-06 22:53:06 -06006039 libCall = spv::GLSLstd450Length;
John Kessenich140f3df2015-06-26 16:58:36 -06006040 break;
6041 case glslang::EOpNormalize:
John Kessenich5e4b1242015-08-06 22:53:06 -06006042 libCall = spv::GLSLstd450Normalize;
John Kessenich140f3df2015-06-26 16:58:36 -06006043 break;
6044
6045 case glslang::EOpExp:
John Kessenich5e4b1242015-08-06 22:53:06 -06006046 libCall = spv::GLSLstd450Exp;
John Kessenich140f3df2015-06-26 16:58:36 -06006047 break;
6048 case glslang::EOpLog:
John Kessenich5e4b1242015-08-06 22:53:06 -06006049 libCall = spv::GLSLstd450Log;
John Kessenich140f3df2015-06-26 16:58:36 -06006050 break;
6051 case glslang::EOpExp2:
John Kessenich5e4b1242015-08-06 22:53:06 -06006052 libCall = spv::GLSLstd450Exp2;
John Kessenich140f3df2015-06-26 16:58:36 -06006053 break;
6054 case glslang::EOpLog2:
John Kessenich5e4b1242015-08-06 22:53:06 -06006055 libCall = spv::GLSLstd450Log2;
John Kessenich140f3df2015-06-26 16:58:36 -06006056 break;
6057 case glslang::EOpSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06006058 libCall = spv::GLSLstd450Sqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06006059 break;
6060 case glslang::EOpInverseSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06006061 libCall = spv::GLSLstd450InverseSqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06006062 break;
6063
6064 case glslang::EOpFloor:
John Kessenich5e4b1242015-08-06 22:53:06 -06006065 libCall = spv::GLSLstd450Floor;
John Kessenich140f3df2015-06-26 16:58:36 -06006066 break;
6067 case glslang::EOpTrunc:
John Kessenich5e4b1242015-08-06 22:53:06 -06006068 libCall = spv::GLSLstd450Trunc;
John Kessenich140f3df2015-06-26 16:58:36 -06006069 break;
6070 case glslang::EOpRound:
John Kessenich5e4b1242015-08-06 22:53:06 -06006071 libCall = spv::GLSLstd450Round;
John Kessenich140f3df2015-06-26 16:58:36 -06006072 break;
6073 case glslang::EOpRoundEven:
John Kessenich5e4b1242015-08-06 22:53:06 -06006074 libCall = spv::GLSLstd450RoundEven;
John Kessenich140f3df2015-06-26 16:58:36 -06006075 break;
6076 case glslang::EOpCeil:
John Kessenich5e4b1242015-08-06 22:53:06 -06006077 libCall = spv::GLSLstd450Ceil;
John Kessenich140f3df2015-06-26 16:58:36 -06006078 break;
6079 case glslang::EOpFract:
John Kessenich5e4b1242015-08-06 22:53:06 -06006080 libCall = spv::GLSLstd450Fract;
John Kessenich140f3df2015-06-26 16:58:36 -06006081 break;
6082
6083 case glslang::EOpIsNan:
6084 unaryOp = spv::OpIsNan;
6085 break;
6086 case glslang::EOpIsInf:
6087 unaryOp = spv::OpIsInf;
6088 break;
LoopDawg592860c2016-06-09 08:57:35 -06006089 case glslang::EOpIsFinite:
6090 unaryOp = spv::OpIsFinite;
6091 break;
John Kessenich140f3df2015-06-26 16:58:36 -06006092
Rex Xucbc426e2015-12-15 16:03:10 +08006093 case glslang::EOpFloatBitsToInt:
6094 case glslang::EOpFloatBitsToUint:
6095 case glslang::EOpIntBitsToFloat:
6096 case glslang::EOpUintBitsToFloat:
Rex Xu8ff43de2016-04-22 16:51:45 +08006097 case glslang::EOpDoubleBitsToInt64:
6098 case glslang::EOpDoubleBitsToUint64:
6099 case glslang::EOpInt64BitsToDouble:
6100 case glslang::EOpUint64BitsToDouble:
Rex Xucabbb782017-03-24 13:41:14 +08006101 case glslang::EOpFloat16BitsToInt16:
6102 case glslang::EOpFloat16BitsToUint16:
6103 case glslang::EOpInt16BitsToFloat16:
6104 case glslang::EOpUint16BitsToFloat16:
Rex Xucbc426e2015-12-15 16:03:10 +08006105 unaryOp = spv::OpBitcast;
6106 break;
6107
John Kessenich140f3df2015-06-26 16:58:36 -06006108 case glslang::EOpPackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006109 libCall = spv::GLSLstd450PackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006110 break;
6111 case glslang::EOpUnpackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006112 libCall = spv::GLSLstd450UnpackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006113 break;
6114 case glslang::EOpPackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006115 libCall = spv::GLSLstd450PackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006116 break;
6117 case glslang::EOpUnpackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006118 libCall = spv::GLSLstd450UnpackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006119 break;
6120 case glslang::EOpPackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006121 libCall = spv::GLSLstd450PackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006122 break;
6123 case glslang::EOpUnpackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06006124 libCall = spv::GLSLstd450UnpackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06006125 break;
John Kessenichb9197c82019-08-11 07:41:45 -06006126#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06006127 case glslang::EOpPackSnorm4x8:
6128 libCall = spv::GLSLstd450PackSnorm4x8;
6129 break;
6130 case glslang::EOpUnpackSnorm4x8:
6131 libCall = spv::GLSLstd450UnpackSnorm4x8;
6132 break;
6133 case glslang::EOpPackUnorm4x8:
6134 libCall = spv::GLSLstd450PackUnorm4x8;
6135 break;
6136 case glslang::EOpUnpackUnorm4x8:
6137 libCall = spv::GLSLstd450UnpackUnorm4x8;
6138 break;
6139 case glslang::EOpPackDouble2x32:
6140 libCall = spv::GLSLstd450PackDouble2x32;
6141 break;
6142 case glslang::EOpUnpackDouble2x32:
6143 libCall = spv::GLSLstd450UnpackDouble2x32;
6144 break;
John Kessenichb9197c82019-08-11 07:41:45 -06006145#endif
John Kessenich140f3df2015-06-26 16:58:36 -06006146
Rex Xu8ff43de2016-04-22 16:51:45 +08006147 case glslang::EOpPackInt2x32:
6148 case glslang::EOpUnpackInt2x32:
6149 case glslang::EOpPackUint2x32:
6150 case glslang::EOpUnpackUint2x32:
John Kessenich66011cb2018-03-06 16:12:04 -07006151 case glslang::EOpPack16:
6152 case glslang::EOpPack32:
6153 case glslang::EOpPack64:
6154 case glslang::EOpUnpack32:
6155 case glslang::EOpUnpack16:
6156 case glslang::EOpUnpack8:
Rex Xucabbb782017-03-24 13:41:14 +08006157 case glslang::EOpPackInt2x16:
6158 case glslang::EOpUnpackInt2x16:
6159 case glslang::EOpPackUint2x16:
6160 case glslang::EOpUnpackUint2x16:
6161 case glslang::EOpPackInt4x16:
6162 case glslang::EOpUnpackInt4x16:
6163 case glslang::EOpPackUint4x16:
6164 case glslang::EOpUnpackUint4x16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006165 case glslang::EOpPackFloat2x16:
6166 case glslang::EOpUnpackFloat2x16:
6167 unaryOp = spv::OpBitcast;
6168 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006169
John Kessenich140f3df2015-06-26 16:58:36 -06006170 case glslang::EOpDPdx:
6171 unaryOp = spv::OpDPdx;
6172 break;
6173 case glslang::EOpDPdy:
6174 unaryOp = spv::OpDPdy;
6175 break;
6176 case glslang::EOpFwidth:
6177 unaryOp = spv::OpFwidth;
6178 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06006179
John Kessenich140f3df2015-06-26 16:58:36 -06006180 case glslang::EOpAny:
6181 unaryOp = spv::OpAny;
6182 break;
6183 case glslang::EOpAll:
6184 unaryOp = spv::OpAll;
6185 break;
6186
6187 case glslang::EOpAbs:
John Kessenich5e4b1242015-08-06 22:53:06 -06006188 if (isFloat)
6189 libCall = spv::GLSLstd450FAbs;
6190 else
6191 libCall = spv::GLSLstd450SAbs;
John Kessenich140f3df2015-06-26 16:58:36 -06006192 break;
6193 case glslang::EOpSign:
John Kessenich5e4b1242015-08-06 22:53:06 -06006194 if (isFloat)
6195 libCall = spv::GLSLstd450FSign;
6196 else
6197 libCall = spv::GLSLstd450SSign;
John Kessenich140f3df2015-06-26 16:58:36 -06006198 break;
6199
John Kessenicha28f7a72019-08-06 07:00:58 -06006200#ifndef GLSLANG_WEB
6201 case glslang::EOpDPdxFine:
6202 unaryOp = spv::OpDPdxFine;
6203 break;
6204 case glslang::EOpDPdyFine:
6205 unaryOp = spv::OpDPdyFine;
6206 break;
6207 case glslang::EOpFwidthFine:
6208 unaryOp = spv::OpFwidthFine;
6209 break;
6210 case glslang::EOpDPdxCoarse:
6211 unaryOp = spv::OpDPdxCoarse;
6212 break;
6213 case glslang::EOpDPdyCoarse:
6214 unaryOp = spv::OpDPdyCoarse;
6215 break;
6216 case glslang::EOpFwidthCoarse:
6217 unaryOp = spv::OpFwidthCoarse;
6218 break;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04006219 case glslang::EOpRayQueryProceed:
6220 unaryOp = spv::OpRayQueryProceedKHR;
6221 break;
6222 case glslang::EOpRayQueryGetRayTMin:
6223 unaryOp = spv::OpRayQueryGetRayTMinKHR;
6224 break;
6225 case glslang::EOpRayQueryGetRayFlags:
6226 unaryOp = spv::OpRayQueryGetRayFlagsKHR;
6227 break;
6228 case glslang::EOpRayQueryGetWorldRayOrigin:
6229 unaryOp = spv::OpRayQueryGetWorldRayOriginKHR;
6230 break;
6231 case glslang::EOpRayQueryGetWorldRayDirection:
6232 unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR;
6233 break;
6234 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
6235 unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR;
6236 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06006237 case glslang::EOpInterpolateAtCentroid:
6238 if (typeProxy == glslang::EbtFloat16)
6239 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6240 libCall = spv::GLSLstd450InterpolateAtCentroid;
6241 break;
John Kessenichfc51d282015-08-19 13:34:18 -06006242 case glslang::EOpAtomicCounterIncrement:
6243 case glslang::EOpAtomicCounterDecrement:
6244 case glslang::EOpAtomicCounter:
6245 {
6246 // Handle all of the atomics in one place, in createAtomicOperation()
6247 std::vector<spv::Id> operands;
6248 operands.push_back(operand);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05006249 return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06006250 }
6251
John Kessenichfc51d282015-08-19 13:34:18 -06006252 case glslang::EOpBitFieldReverse:
6253 unaryOp = spv::OpBitReverse;
6254 break;
6255 case glslang::EOpBitCount:
6256 unaryOp = spv::OpBitCount;
6257 break;
6258 case glslang::EOpFindLSB:
John Kessenich55e7d112015-11-15 21:33:39 -07006259 libCall = spv::GLSLstd450FindILsb;
John Kessenichfc51d282015-08-19 13:34:18 -06006260 break;
6261 case glslang::EOpFindMSB:
John Kessenich55e7d112015-11-15 21:33:39 -07006262 if (isUnsigned)
6263 libCall = spv::GLSLstd450FindUMsb;
6264 else
6265 libCall = spv::GLSLstd450FindSMsb;
John Kessenichfc51d282015-08-19 13:34:18 -06006266 break;
6267
Ian Romanickb3bd4022019-01-21 08:57:25 -08006268 case glslang::EOpCountLeadingZeros:
6269 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
6270 builder.addExtension("SPV_INTEL_shader_integer_functions2");
6271 unaryOp = spv::OpUCountLeadingZerosINTEL;
6272 break;
6273
6274 case glslang::EOpCountTrailingZeros:
6275 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
6276 builder.addExtension("SPV_INTEL_shader_integer_functions2");
6277 unaryOp = spv::OpUCountTrailingZerosINTEL;
6278 break;
6279
Rex Xu574ab042016-04-14 16:53:07 +08006280 case glslang::EOpBallot:
6281 case glslang::EOpReadFirstInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08006282 case glslang::EOpAnyInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08006283 case glslang::EOpAllInvocations:
Rex Xu338b1852016-05-05 20:38:33 +08006284 case glslang::EOpAllInvocationsEqual:
Rex Xu9d93a232016-05-05 12:30:44 +08006285 case glslang::EOpMinInvocations:
6286 case glslang::EOpMaxInvocations:
6287 case glslang::EOpAddInvocations:
6288 case glslang::EOpMinInvocationsNonUniform:
6289 case glslang::EOpMaxInvocationsNonUniform:
6290 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08006291 case glslang::EOpMinInvocationsInclusiveScan:
6292 case glslang::EOpMaxInvocationsInclusiveScan:
6293 case glslang::EOpAddInvocationsInclusiveScan:
6294 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6295 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6296 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6297 case glslang::EOpMinInvocationsExclusiveScan:
6298 case glslang::EOpMaxInvocationsExclusiveScan:
6299 case glslang::EOpAddInvocationsExclusiveScan:
6300 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6301 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6302 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
Rex Xu51596642016-09-21 18:56:12 +08006303 {
6304 std::vector<spv::Id> operands;
6305 operands.push_back(operand);
6306 return createInvocationsOperation(op, typeId, operands, typeProxy);
6307 }
John Kessenich66011cb2018-03-06 16:12:04 -07006308 case glslang::EOpSubgroupAll:
6309 case glslang::EOpSubgroupAny:
6310 case glslang::EOpSubgroupAllEqual:
6311 case glslang::EOpSubgroupBroadcastFirst:
6312 case glslang::EOpSubgroupBallot:
6313 case glslang::EOpSubgroupInverseBallot:
6314 case glslang::EOpSubgroupBallotBitCount:
6315 case glslang::EOpSubgroupBallotInclusiveBitCount:
6316 case glslang::EOpSubgroupBallotExclusiveBitCount:
6317 case glslang::EOpSubgroupBallotFindLSB:
6318 case glslang::EOpSubgroupBallotFindMSB:
6319 case glslang::EOpSubgroupAdd:
6320 case glslang::EOpSubgroupMul:
6321 case glslang::EOpSubgroupMin:
6322 case glslang::EOpSubgroupMax:
6323 case glslang::EOpSubgroupAnd:
6324 case glslang::EOpSubgroupOr:
6325 case glslang::EOpSubgroupXor:
6326 case glslang::EOpSubgroupInclusiveAdd:
6327 case glslang::EOpSubgroupInclusiveMul:
6328 case glslang::EOpSubgroupInclusiveMin:
6329 case glslang::EOpSubgroupInclusiveMax:
6330 case glslang::EOpSubgroupInclusiveAnd:
6331 case glslang::EOpSubgroupInclusiveOr:
6332 case glslang::EOpSubgroupInclusiveXor:
6333 case glslang::EOpSubgroupExclusiveAdd:
6334 case glslang::EOpSubgroupExclusiveMul:
6335 case glslang::EOpSubgroupExclusiveMin:
6336 case glslang::EOpSubgroupExclusiveMax:
6337 case glslang::EOpSubgroupExclusiveAnd:
6338 case glslang::EOpSubgroupExclusiveOr:
6339 case glslang::EOpSubgroupExclusiveXor:
6340 case glslang::EOpSubgroupQuadSwapHorizontal:
6341 case glslang::EOpSubgroupQuadSwapVertical:
6342 case glslang::EOpSubgroupQuadSwapDiagonal: {
6343 std::vector<spv::Id> operands;
6344 operands.push_back(operand);
6345 return createSubgroupOperation(op, typeId, operands, typeProxy);
6346 }
Rex Xu9d93a232016-05-05 12:30:44 +08006347 case glslang::EOpMbcnt:
6348 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6349 libCall = spv::MbcntAMD;
6350 break;
6351
6352 case glslang::EOpCubeFaceIndex:
6353 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
6354 libCall = spv::CubeFaceIndexAMD;
6355 break;
6356
6357 case glslang::EOpCubeFaceCoord:
6358 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
6359 libCall = spv::CubeFaceCoordAMD;
6360 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006361 case glslang::EOpSubgroupPartition:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006362 unaryOp = spv::OpGroupNonUniformPartitionNV;
6363 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06006364 case glslang::EOpConstructReference:
6365 unaryOp = spv::OpBitcast;
6366 break;
Daniel Kochffccefd2020-11-23 15:41:27 -05006367
6368 case glslang::EOpConvUint64ToAccStruct:
6369 case glslang::EOpConvUvec2ToAccStruct:
6370 unaryOp = spv::OpConvertUToAccelerationStructureKHR;
6371 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06006372#endif
Jeff Bolz88220d52019-05-08 10:24:46 -05006373
6374 case glslang::EOpCopyObject:
6375 unaryOp = spv::OpCopyObject;
6376 break;
6377
John Kessenich140f3df2015-06-26 16:58:36 -06006378 default:
6379 return 0;
6380 }
6381
6382 spv::Id id;
6383 if (libCall >= 0) {
6384 std::vector<spv::Id> args;
6385 args.push_back(operand);
Rex Xu9d93a232016-05-05 12:30:44 +08006386 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
Rex Xu338b1852016-05-05 20:38:33 +08006387 } else {
John Kessenich91cef522016-05-05 16:45:40 -06006388 id = builder.createUnaryOp(unaryOp, typeId, operand);
Rex Xu338b1852016-05-05 20:38:33 +08006389 }
John Kessenich140f3df2015-06-26 16:58:36 -06006390
John Kessenichb9197c82019-08-11 07:41:45 -06006391 decorations.addNoContraction(builder, id);
6392 decorations.addNonUniform(builder, id);
John Kessenichead86222018-03-28 18:01:20 -06006393 return builder.setPrecision(id, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06006394}
6395
John Kessenich7a53f762016-01-20 11:19:27 -07006396// Create a unary operation on a matrix
John Kessenichead86222018-03-28 18:01:20 -06006397spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
6398 spv::Id operand, glslang::TBasicType /* typeProxy */)
John Kessenich7a53f762016-01-20 11:19:27 -07006399{
6400 // Handle unary operations vector by vector.
6401 // The result type is the same type as the original type.
6402 // The algorithm is to:
6403 // - break the matrix into vectors
6404 // - apply the operation to each vector
6405 // - make a matrix out the vector results
6406
6407 // get the types sorted out
6408 int numCols = builder.getNumColumns(operand);
6409 int numRows = builder.getNumRows(operand);
Rex Xuc1992e52016-05-17 18:57:18 +08006410 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
6411 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
John Kessenich7a53f762016-01-20 11:19:27 -07006412 std::vector<spv::Id> results;
6413
6414 // do each vector op
6415 for (int c = 0; c < numCols; ++c) {
6416 std::vector<unsigned int> indexes;
6417 indexes.push_back(c);
Rex Xuc1992e52016-05-17 18:57:18 +08006418 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
6419 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
John Kessenichb9197c82019-08-11 07:41:45 -06006420 decorations.addNoContraction(builder, destVec);
6421 decorations.addNonUniform(builder, destVec);
John Kessenichead86222018-03-28 18:01:20 -06006422 results.push_back(builder.setPrecision(destVec, decorations.precision));
John Kessenich7a53f762016-01-20 11:19:27 -07006423 }
6424
6425 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06006426 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06006427 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06006428 return result;
John Kessenich7a53f762016-01-20 11:19:27 -07006429}
6430
John Kessenichad7645f2018-06-04 19:11:25 -06006431// For converting integers where both the bitwidth and the signedness could
6432// change, but only do the width change here. The caller is still responsible
6433// for the signedness conversion.
6434spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize)
John Kessenich66011cb2018-03-06 16:12:04 -07006435{
John Kessenichad7645f2018-06-04 19:11:25 -06006436 // Get the result type width, based on the type to convert to.
6437 int width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07006438 switch(op) {
John Kessenichad7645f2018-06-04 19:11:25 -06006439 case glslang::EOpConvInt16ToUint8:
6440 case glslang::EOpConvIntToUint8:
6441 case glslang::EOpConvInt64ToUint8:
6442 case glslang::EOpConvUint16ToInt8:
6443 case glslang::EOpConvUintToInt8:
6444 case glslang::EOpConvUint64ToInt8:
6445 width = 8;
6446 break;
John Kessenich66011cb2018-03-06 16:12:04 -07006447 case glslang::EOpConvInt8ToUint16:
John Kessenichad7645f2018-06-04 19:11:25 -06006448 case glslang::EOpConvIntToUint16:
6449 case glslang::EOpConvInt64ToUint16:
6450 case glslang::EOpConvUint8ToInt16:
6451 case glslang::EOpConvUintToInt16:
6452 case glslang::EOpConvUint64ToInt16:
6453 width = 16;
John Kessenich66011cb2018-03-06 16:12:04 -07006454 break;
6455 case glslang::EOpConvInt8ToUint:
John Kessenichad7645f2018-06-04 19:11:25 -06006456 case glslang::EOpConvInt16ToUint:
6457 case glslang::EOpConvInt64ToUint:
6458 case glslang::EOpConvUint8ToInt:
6459 case glslang::EOpConvUint16ToInt:
6460 case glslang::EOpConvUint64ToInt:
6461 width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07006462 break;
6463 case glslang::EOpConvInt8ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006464 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006465 case glslang::EOpConvIntToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006466 case glslang::EOpConvUint8ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006467 case glslang::EOpConvUint16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006468 case glslang::EOpConvUintToInt64:
John Kessenichad7645f2018-06-04 19:11:25 -06006469 width = 64;
John Kessenich66011cb2018-03-06 16:12:04 -07006470 break;
6471
6472 default:
6473 assert(false && "Default missing");
6474 break;
6475 }
6476
John Kessenichad7645f2018-06-04 19:11:25 -06006477 // Get the conversion operation and result type,
6478 // based on the target width, but the source type.
6479 spv::Id type = spv::NoType;
6480 spv::Op convOp = spv::OpNop;
6481 switch(op) {
6482 case glslang::EOpConvInt8ToUint16:
6483 case glslang::EOpConvInt8ToUint:
6484 case glslang::EOpConvInt8ToUint64:
6485 case glslang::EOpConvInt16ToUint8:
6486 case glslang::EOpConvInt16ToUint:
6487 case glslang::EOpConvInt16ToUint64:
6488 case glslang::EOpConvIntToUint8:
6489 case glslang::EOpConvIntToUint16:
6490 case glslang::EOpConvIntToUint64:
6491 case glslang::EOpConvInt64ToUint8:
6492 case glslang::EOpConvInt64ToUint16:
6493 case glslang::EOpConvInt64ToUint:
6494 convOp = spv::OpSConvert;
6495 type = builder.makeIntType(width);
6496 break;
6497 default:
6498 convOp = spv::OpUConvert;
6499 type = builder.makeUintType(width);
6500 break;
6501 }
6502
John Kessenich66011cb2018-03-06 16:12:04 -07006503 if (vectorSize > 0)
6504 type = builder.makeVectorType(type, vectorSize);
6505
John Kessenichad7645f2018-06-04 19:11:25 -06006506 return builder.createUnaryOp(convOp, type, operand);
John Kessenich66011cb2018-03-06 16:12:04 -07006507}
6508
John Kessenichead86222018-03-28 18:01:20 -06006509spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
6510 spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06006511{
6512 spv::Op convOp = spv::OpNop;
6513 spv::Id zero = 0;
6514 spv::Id one = 0;
6515
6516 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
6517
6518 switch (op) {
John Kessenich66011cb2018-03-06 16:12:04 -07006519 case glslang::EOpConvIntToBool:
6520 case glslang::EOpConvUintToBool:
6521 zero = builder.makeUintConstant(0);
6522 zero = makeSmearedConstant(zero, vectorSize);
6523 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06006524 case glslang::EOpConvFloatToBool:
6525 zero = builder.makeFloatConstant(0.0F);
6526 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006527 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06006528 case glslang::EOpConvBoolToFloat:
6529 convOp = spv::OpSelect;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006530 zero = builder.makeFloatConstant(0.0F);
6531 one = builder.makeFloatConstant(1.0F);
John Kessenich140f3df2015-06-26 16:58:36 -06006532 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006533
John Kessenich140f3df2015-06-26 16:58:36 -06006534 case glslang::EOpConvBoolToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006535 case glslang::EOpConvBoolToInt64:
John Kessenichb9197c82019-08-11 07:41:45 -06006536#ifndef GLSLANG_WEB
6537 if (op == glslang::EOpConvBoolToInt64) {
Rex Xucabbb782017-03-24 13:41:14 +08006538 zero = builder.makeInt64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006539 one = builder.makeInt64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006540 } else
6541#endif
6542 {
6543 zero = builder.makeIntConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006544 one = builder.makeIntConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006545 }
Rex Xucabbb782017-03-24 13:41:14 +08006546
John Kessenich140f3df2015-06-26 16:58:36 -06006547 convOp = spv::OpSelect;
6548 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006549
John Kessenich140f3df2015-06-26 16:58:36 -06006550 case glslang::EOpConvBoolToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006551 case glslang::EOpConvBoolToUint64:
John Kessenichb9197c82019-08-11 07:41:45 -06006552#ifndef GLSLANG_WEB
6553 if (op == glslang::EOpConvBoolToUint64) {
Rex Xucabbb782017-03-24 13:41:14 +08006554 zero = builder.makeUint64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006555 one = builder.makeUint64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006556 } else
6557#endif
6558 {
6559 zero = builder.makeUintConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006560 one = builder.makeUintConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006561 }
Rex Xucabbb782017-03-24 13:41:14 +08006562
John Kessenich140f3df2015-06-26 16:58:36 -06006563 convOp = spv::OpSelect;
6564 break;
6565
John Kessenich66011cb2018-03-06 16:12:04 -07006566 case glslang::EOpConvInt8ToFloat16:
6567 case glslang::EOpConvInt8ToFloat:
6568 case glslang::EOpConvInt8ToDouble:
6569 case glslang::EOpConvInt16ToFloat16:
6570 case glslang::EOpConvInt16ToFloat:
6571 case glslang::EOpConvInt16ToDouble:
6572 case glslang::EOpConvIntToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006573 case glslang::EOpConvIntToFloat:
6574 case glslang::EOpConvIntToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006575 case glslang::EOpConvInt64ToFloat:
6576 case glslang::EOpConvInt64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006577 case glslang::EOpConvInt64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006578 convOp = spv::OpConvertSToF;
6579 break;
6580
John Kessenich66011cb2018-03-06 16:12:04 -07006581 case glslang::EOpConvUint8ToFloat16:
6582 case glslang::EOpConvUint8ToFloat:
6583 case glslang::EOpConvUint8ToDouble:
6584 case glslang::EOpConvUint16ToFloat16:
6585 case glslang::EOpConvUint16ToFloat:
6586 case glslang::EOpConvUint16ToDouble:
6587 case glslang::EOpConvUintToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006588 case glslang::EOpConvUintToFloat:
6589 case glslang::EOpConvUintToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006590 case glslang::EOpConvUint64ToFloat:
6591 case glslang::EOpConvUint64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006592 case glslang::EOpConvUint64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006593 convOp = spv::OpConvertUToF;
6594 break;
6595
John Kessenich66011cb2018-03-06 16:12:04 -07006596 case glslang::EOpConvFloat16ToInt8:
6597 case glslang::EOpConvFloatToInt8:
6598 case glslang::EOpConvDoubleToInt8:
6599 case glslang::EOpConvFloat16ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006600 case glslang::EOpConvFloatToInt16:
6601 case glslang::EOpConvDoubleToInt16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006602 case glslang::EOpConvFloat16ToInt:
John Kessenich66011cb2018-03-06 16:12:04 -07006603 case glslang::EOpConvFloatToInt:
6604 case glslang::EOpConvDoubleToInt:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006605 case glslang::EOpConvFloat16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006606 case glslang::EOpConvFloatToInt64:
6607 case glslang::EOpConvDoubleToInt64:
John Kessenich140f3df2015-06-26 16:58:36 -06006608 convOp = spv::OpConvertFToS;
6609 break;
6610
John Kessenich66011cb2018-03-06 16:12:04 -07006611 case glslang::EOpConvUint8ToInt8:
6612 case glslang::EOpConvInt8ToUint8:
6613 case glslang::EOpConvUint16ToInt16:
6614 case glslang::EOpConvInt16ToUint16:
John Kessenich140f3df2015-06-26 16:58:36 -06006615 case glslang::EOpConvUintToInt:
6616 case glslang::EOpConvIntToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006617 case glslang::EOpConvUint64ToInt64:
6618 case glslang::EOpConvInt64ToUint64:
qininge24aa5e2016-04-07 15:40:27 -04006619 if (builder.isInSpecConstCodeGenMode()) {
6620 // Build zero scalar or vector for OpIAdd.
John Kessenich39697cd2019-08-08 10:35:51 -06006621#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07006622 if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) {
6623 zero = builder.makeUint8Constant(0);
6624 } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) {
Rex Xucabbb782017-03-24 13:41:14 +08006625 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006626 } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) {
6627 zero = builder.makeUint64Constant(0);
John Kessenich39697cd2019-08-08 10:35:51 -06006628 } else
6629#endif
6630 {
Rex Xucabbb782017-03-24 13:41:14 +08006631 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006632 }
qining189b2032016-04-12 23:16:20 -04006633 zero = makeSmearedConstant(zero, vectorSize);
qininge24aa5e2016-04-07 15:40:27 -04006634 // Use OpIAdd, instead of OpBitcast to do the conversion when
6635 // generating for OpSpecConstantOp instruction.
6636 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6637 }
6638 // For normal run-time conversion instruction, use OpBitcast.
John Kessenich140f3df2015-06-26 16:58:36 -06006639 convOp = spv::OpBitcast;
6640 break;
6641
John Kessenich66011cb2018-03-06 16:12:04 -07006642 case glslang::EOpConvFloat16ToUint8:
6643 case glslang::EOpConvFloatToUint8:
6644 case glslang::EOpConvDoubleToUint8:
6645 case glslang::EOpConvFloat16ToUint16:
6646 case glslang::EOpConvFloatToUint16:
6647 case glslang::EOpConvDoubleToUint16:
6648 case glslang::EOpConvFloat16ToUint:
John Kessenich140f3df2015-06-26 16:58:36 -06006649 case glslang::EOpConvFloatToUint:
6650 case glslang::EOpConvDoubleToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006651 case glslang::EOpConvFloatToUint64:
6652 case glslang::EOpConvDoubleToUint64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006653 case glslang::EOpConvFloat16ToUint64:
John Kessenich140f3df2015-06-26 16:58:36 -06006654 convOp = spv::OpConvertFToU;
6655 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08006656
John Kessenich39697cd2019-08-08 10:35:51 -06006657#ifndef GLSLANG_WEB
6658 case glslang::EOpConvInt8ToBool:
6659 case glslang::EOpConvUint8ToBool:
6660 zero = builder.makeUint8Constant(0);
6661 zero = makeSmearedConstant(zero, vectorSize);
6662 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6663 case glslang::EOpConvInt16ToBool:
6664 case glslang::EOpConvUint16ToBool:
6665 zero = builder.makeUint16Constant(0);
6666 zero = makeSmearedConstant(zero, vectorSize);
6667 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6668 case glslang::EOpConvInt64ToBool:
6669 case glslang::EOpConvUint64ToBool:
6670 zero = builder.makeUint64Constant(0);
6671 zero = makeSmearedConstant(zero, vectorSize);
6672 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6673 case glslang::EOpConvDoubleToBool:
6674 zero = builder.makeDoubleConstant(0.0);
6675 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006676 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich39697cd2019-08-08 10:35:51 -06006677 case glslang::EOpConvFloat16ToBool:
6678 zero = builder.makeFloat16Constant(0.0F);
6679 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006680 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich39697cd2019-08-08 10:35:51 -06006681 case glslang::EOpConvBoolToDouble:
6682 convOp = spv::OpSelect;
6683 zero = builder.makeDoubleConstant(0.0);
6684 one = builder.makeDoubleConstant(1.0);
6685 break;
6686 case glslang::EOpConvBoolToFloat16:
6687 convOp = spv::OpSelect;
6688 zero = builder.makeFloat16Constant(0.0F);
6689 one = builder.makeFloat16Constant(1.0F);
6690 break;
6691 case glslang::EOpConvBoolToInt8:
6692 zero = builder.makeInt8Constant(0);
6693 one = builder.makeInt8Constant(1);
6694 convOp = spv::OpSelect;
6695 break;
6696 case glslang::EOpConvBoolToUint8:
6697 zero = builder.makeUint8Constant(0);
6698 one = builder.makeUint8Constant(1);
6699 convOp = spv::OpSelect;
6700 break;
6701 case glslang::EOpConvBoolToInt16:
6702 zero = builder.makeInt16Constant(0);
6703 one = builder.makeInt16Constant(1);
6704 convOp = spv::OpSelect;
6705 break;
6706 case glslang::EOpConvBoolToUint16:
6707 zero = builder.makeUint16Constant(0);
6708 one = builder.makeUint16Constant(1);
6709 convOp = spv::OpSelect;
6710 break;
6711 case glslang::EOpConvDoubleToFloat:
6712 case glslang::EOpConvFloatToDouble:
6713 case glslang::EOpConvDoubleToFloat16:
6714 case glslang::EOpConvFloat16ToDouble:
6715 case glslang::EOpConvFloatToFloat16:
6716 case glslang::EOpConvFloat16ToFloat:
6717 convOp = spv::OpFConvert;
6718 if (builder.isMatrixType(destType))
6719 return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy);
6720 break;
6721
John Kessenich66011cb2018-03-06 16:12:04 -07006722 case glslang::EOpConvInt8ToInt16:
6723 case glslang::EOpConvInt8ToInt:
6724 case glslang::EOpConvInt8ToInt64:
6725 case glslang::EOpConvInt16ToInt8:
Rex Xucabbb782017-03-24 13:41:14 +08006726 case glslang::EOpConvInt16ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006727 case glslang::EOpConvInt16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006728 case glslang::EOpConvIntToInt8:
6729 case glslang::EOpConvIntToInt16:
6730 case glslang::EOpConvIntToInt64:
6731 case glslang::EOpConvInt64ToInt8:
6732 case glslang::EOpConvInt64ToInt16:
6733 case glslang::EOpConvInt64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006734 convOp = spv::OpSConvert;
6735 break;
6736
John Kessenich66011cb2018-03-06 16:12:04 -07006737 case glslang::EOpConvUint8ToUint16:
6738 case glslang::EOpConvUint8ToUint:
6739 case glslang::EOpConvUint8ToUint64:
6740 case glslang::EOpConvUint16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006741 case glslang::EOpConvUint16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006742 case glslang::EOpConvUint16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006743 case glslang::EOpConvUintToUint8:
6744 case glslang::EOpConvUintToUint16:
6745 case glslang::EOpConvUintToUint64:
6746 case glslang::EOpConvUint64ToUint8:
6747 case glslang::EOpConvUint64ToUint16:
6748 case glslang::EOpConvUint64ToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006749 convOp = spv::OpUConvert;
6750 break;
6751
John Kessenich66011cb2018-03-06 16:12:04 -07006752 case glslang::EOpConvInt8ToUint16:
6753 case glslang::EOpConvInt8ToUint:
6754 case glslang::EOpConvInt8ToUint64:
6755 case glslang::EOpConvInt16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006756 case glslang::EOpConvInt16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006757 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006758 case glslang::EOpConvIntToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006759 case glslang::EOpConvIntToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006760 case glslang::EOpConvIntToUint64:
6761 case glslang::EOpConvInt64ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006762 case glslang::EOpConvInt64ToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006763 case glslang::EOpConvInt64ToUint:
6764 case glslang::EOpConvUint8ToInt16:
6765 case glslang::EOpConvUint8ToInt:
6766 case glslang::EOpConvUint8ToInt64:
6767 case glslang::EOpConvUint16ToInt8:
6768 case glslang::EOpConvUint16ToInt:
6769 case glslang::EOpConvUint16ToInt64:
6770 case glslang::EOpConvUintToInt8:
6771 case glslang::EOpConvUintToInt16:
6772 case glslang::EOpConvUintToInt64:
6773 case glslang::EOpConvUint64ToInt8:
6774 case glslang::EOpConvUint64ToInt16:
6775 case glslang::EOpConvUint64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006776 // OpSConvert/OpUConvert + OpBitCast
John Kessenichad7645f2018-06-04 19:11:25 -06006777 operand = createIntWidthConversion(op, operand, vectorSize);
Rex Xu8ff43de2016-04-22 16:51:45 +08006778
6779 if (builder.isInSpecConstCodeGenMode()) {
6780 // Build zero scalar or vector for OpIAdd.
John Kessenich66011cb2018-03-06 16:12:04 -07006781 switch(op) {
6782 case glslang::EOpConvInt16ToUint8:
6783 case glslang::EOpConvIntToUint8:
6784 case glslang::EOpConvInt64ToUint8:
6785 case glslang::EOpConvUint16ToInt8:
6786 case glslang::EOpConvUintToInt8:
6787 case glslang::EOpConvUint64ToInt8:
6788 zero = builder.makeUint8Constant(0);
6789 break;
6790 case glslang::EOpConvInt8ToUint16:
6791 case glslang::EOpConvIntToUint16:
6792 case glslang::EOpConvInt64ToUint16:
6793 case glslang::EOpConvUint8ToInt16:
6794 case glslang::EOpConvUintToInt16:
6795 case glslang::EOpConvUint64ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006796 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006797 break;
6798 case glslang::EOpConvInt8ToUint:
6799 case glslang::EOpConvInt16ToUint:
6800 case glslang::EOpConvInt64ToUint:
6801 case glslang::EOpConvUint8ToInt:
6802 case glslang::EOpConvUint16ToInt:
6803 case glslang::EOpConvUint64ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006804 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006805 break;
6806 case glslang::EOpConvInt8ToUint64:
6807 case glslang::EOpConvInt16ToUint64:
6808 case glslang::EOpConvIntToUint64:
6809 case glslang::EOpConvUint8ToInt64:
6810 case glslang::EOpConvUint16ToInt64:
6811 case glslang::EOpConvUintToInt64:
Rex Xucabbb782017-03-24 13:41:14 +08006812 zero = builder.makeUint64Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006813 break;
6814 default:
6815 assert(false && "Default missing");
6816 break;
6817 }
Rex Xu8ff43de2016-04-22 16:51:45 +08006818 zero = makeSmearedConstant(zero, vectorSize);
6819 // Use OpIAdd, instead of OpBitcast to do the conversion when
6820 // generating for OpSpecConstantOp instruction.
6821 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6822 }
6823 // For normal run-time conversion instruction, use OpBitcast.
6824 convOp = spv::OpBitcast;
6825 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06006826 case glslang::EOpConvUint64ToPtr:
6827 convOp = spv::OpConvertUToPtr;
6828 break;
6829 case glslang::EOpConvPtrToUint64:
6830 convOp = spv::OpConvertPtrToU;
6831 break;
John Kessenich90e402f2019-09-17 23:19:38 -06006832 case glslang::EOpConvPtrToUvec2:
6833 case glslang::EOpConvUvec2ToPtr:
John Kessenichee8e9c12019-10-10 20:54:21 -06006834 if (builder.isVector(operand))
6835 builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer,
6836 spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5);
John Kessenich90e402f2019-09-17 23:19:38 -06006837 convOp = spv::OpBitcast;
6838 break;
John Kessenich39697cd2019-08-08 10:35:51 -06006839#endif
6840
John Kessenich140f3df2015-06-26 16:58:36 -06006841 default:
6842 break;
6843 }
6844
6845 spv::Id result = 0;
6846 if (convOp == spv::OpNop)
6847 return result;
6848
6849 if (convOp == spv::OpSelect) {
6850 zero = makeSmearedConstant(zero, vectorSize);
6851 one = makeSmearedConstant(one, vectorSize);
6852 result = builder.createTriOp(convOp, destType, operand, one, zero);
6853 } else
6854 result = builder.createUnaryOp(convOp, destType, operand);
6855
John Kessenichead86222018-03-28 18:01:20 -06006856 result = builder.setPrecision(result, decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06006857 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06006858 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06006859}
6860
6861spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
6862{
6863 if (vectorSize == 0)
6864 return constant;
6865
6866 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
6867 std::vector<spv::Id> components;
6868 for (int c = 0; c < vectorSize; ++c)
6869 components.push_back(constant);
6870 return builder.makeCompositeConstant(vectorTypeId, components);
6871}
6872
John Kessenich426394d2015-07-23 10:22:48 -06006873// For glslang ops that map to SPV atomic opCodes
John Kessenich8985fc92020-03-03 10:25:07 -07006874spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/,
6875 spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy,
6876 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich426394d2015-07-23 10:22:48 -06006877{
6878 spv::Op opCode = spv::OpNop;
6879
6880 switch (op) {
6881 case glslang::EOpAtomicAdd:
Rex Xufc618912015-09-09 16:42:49 +08006882 case glslang::EOpImageAtomicAdd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006883 case glslang::EOpAtomicCounterAdd:
John Kessenich426394d2015-07-23 10:22:48 -06006884 opCode = spv::OpAtomicIAdd;
Vikram Kushwaha79b93922020-07-19 15:45:01 -07006885 if (typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) {
6886 opCode = spv::OpAtomicFAddEXT;
6887 builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add);
6888 if (typeProxy == glslang::EbtFloat)
6889 builder.addCapability(spv::CapabilityAtomicFloat32AddEXT);
6890 else
6891 builder.addCapability(spv::CapabilityAtomicFloat64AddEXT);
6892 }
John Kessenich426394d2015-07-23 10:22:48 -06006893 break;
John Kessenich0d0c6d32017-07-23 16:08:26 -06006894 case glslang::EOpAtomicCounterSubtract:
6895 opCode = spv::OpAtomicISub;
6896 break;
John Kessenich426394d2015-07-23 10:22:48 -06006897 case glslang::EOpAtomicMin:
Rex Xufc618912015-09-09 16:42:49 +08006898 case glslang::EOpImageAtomicMin:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006899 case glslang::EOpAtomicCounterMin:
John Kessenich8985fc92020-03-03 10:25:07 -07006900 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ?
6901 spv::OpAtomicUMin : spv::OpAtomicSMin;
John Kessenich426394d2015-07-23 10:22:48 -06006902 break;
6903 case glslang::EOpAtomicMax:
Rex Xufc618912015-09-09 16:42:49 +08006904 case glslang::EOpImageAtomicMax:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006905 case glslang::EOpAtomicCounterMax:
John Kessenich8985fc92020-03-03 10:25:07 -07006906 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ?
6907 spv::OpAtomicUMax : spv::OpAtomicSMax;
John Kessenich426394d2015-07-23 10:22:48 -06006908 break;
6909 case glslang::EOpAtomicAnd:
Rex Xufc618912015-09-09 16:42:49 +08006910 case glslang::EOpImageAtomicAnd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006911 case glslang::EOpAtomicCounterAnd:
John Kessenich426394d2015-07-23 10:22:48 -06006912 opCode = spv::OpAtomicAnd;
6913 break;
6914 case glslang::EOpAtomicOr:
Rex Xufc618912015-09-09 16:42:49 +08006915 case glslang::EOpImageAtomicOr:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006916 case glslang::EOpAtomicCounterOr:
John Kessenich426394d2015-07-23 10:22:48 -06006917 opCode = spv::OpAtomicOr;
6918 break;
6919 case glslang::EOpAtomicXor:
Rex Xufc618912015-09-09 16:42:49 +08006920 case glslang::EOpImageAtomicXor:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006921 case glslang::EOpAtomicCounterXor:
John Kessenich426394d2015-07-23 10:22:48 -06006922 opCode = spv::OpAtomicXor;
6923 break;
6924 case glslang::EOpAtomicExchange:
Rex Xufc618912015-09-09 16:42:49 +08006925 case glslang::EOpImageAtomicExchange:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006926 case glslang::EOpAtomicCounterExchange:
John Kessenich426394d2015-07-23 10:22:48 -06006927 opCode = spv::OpAtomicExchange;
6928 break;
6929 case glslang::EOpAtomicCompSwap:
Rex Xufc618912015-09-09 16:42:49 +08006930 case glslang::EOpImageAtomicCompSwap:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006931 case glslang::EOpAtomicCounterCompSwap:
John Kessenich426394d2015-07-23 10:22:48 -06006932 opCode = spv::OpAtomicCompareExchange;
6933 break;
6934 case glslang::EOpAtomicCounterIncrement:
6935 opCode = spv::OpAtomicIIncrement;
6936 break;
6937 case glslang::EOpAtomicCounterDecrement:
6938 opCode = spv::OpAtomicIDecrement;
6939 break;
6940 case glslang::EOpAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05006941 case glslang::EOpImageAtomicLoad:
6942 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06006943 opCode = spv::OpAtomicLoad;
6944 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05006945 case glslang::EOpAtomicStore:
6946 case glslang::EOpImageAtomicStore:
6947 opCode = spv::OpAtomicStore;
6948 break;
John Kessenich426394d2015-07-23 10:22:48 -06006949 default:
John Kessenich55e7d112015-11-15 21:33:39 -07006950 assert(0);
John Kessenich426394d2015-07-23 10:22:48 -06006951 break;
6952 }
6953
Rex Xue8fe8b02017-09-26 15:42:56 +08006954 if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64)
6955 builder.addCapability(spv::CapabilityInt64Atomics);
6956
John Kessenich426394d2015-07-23 10:22:48 -06006957 // Sort out the operands
6958 // - mapping from glslang -> SPV
Jeff Bolz36831c92018-09-05 10:11:41 -05006959 // - there are extra SPV operands that are optional in glslang
John Kessenich3e60a6f2015-09-14 22:45:16 -06006960 // - compare-exchange swaps the value and comparator
6961 // - compare-exchange has an extra memory semantics
John Kessenich48d6e792017-10-06 21:21:48 -06006962 // - EOpAtomicCounterDecrement needs a post decrement
Jeff Bolz36831c92018-09-05 10:11:41 -05006963 spv::Id pointerId = 0, compareId = 0, valueId = 0;
6964 // scope defaults to Device in the old model, QueueFamilyKHR in the new model
6965 spv::Id scopeId;
6966 if (glslangIntermediate->usingVulkanMemoryModel()) {
6967 scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR);
6968 } else {
6969 scopeId = builder.makeUintConstant(spv::ScopeDevice);
6970 }
6971 // semantics default to relaxed
John Kessenich8985fc92020-03-03 10:25:07 -07006972 spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() &&
6973 glslangIntermediate->usingVulkanMemoryModel() ?
John Kessenichb9197c82019-08-11 07:41:45 -06006974 spv::MemorySemanticsVolatileMask :
6975 spv::MemorySemanticsMaskNone);
Jeff Bolz36831c92018-09-05 10:11:41 -05006976 spv::Id semanticsId2 = semanticsId;
6977
6978 pointerId = operands[0];
6979 if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) {
6980 // no additional operands
6981 } else if (opCode == spv::OpAtomicCompareExchange) {
6982 compareId = operands[1];
6983 valueId = operands[2];
6984 if (operands.size() > 3) {
6985 scopeId = operands[3];
John Kessenich8985fc92020-03-03 10:25:07 -07006986 semanticsId = builder.makeUintConstant(
6987 builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5]));
6988 semanticsId2 = builder.makeUintConstant(
6989 builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7]));
Jeff Bolz36831c92018-09-05 10:11:41 -05006990 }
6991 } else if (opCode == spv::OpAtomicLoad) {
6992 if (operands.size() > 1) {
6993 scopeId = operands[1];
John Kessenich8985fc92020-03-03 10:25:07 -07006994 semanticsId = builder.makeUintConstant(
6995 builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]));
Jeff Bolz36831c92018-09-05 10:11:41 -05006996 }
6997 } else {
6998 // atomic store or RMW
6999 valueId = operands[1];
7000 if (operands.size() > 2) {
7001 scopeId = operands[2];
John Kessenich8985fc92020-03-03 10:25:07 -07007002 semanticsId = builder.makeUintConstant
7003 (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4]));
Jeff Bolz36831c92018-09-05 10:11:41 -05007004 }
Rex Xu04db3f52015-09-16 11:44:02 +08007005 }
John Kessenich426394d2015-07-23 10:22:48 -06007006
Jeff Bolz36831c92018-09-05 10:11:41 -05007007 // Check for capabilities
7008 unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05007009 if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask |
7010 spv::MemorySemanticsMakeVisibleKHRMask |
7011 spv::MemorySemanticsOutputMemoryKHRMask |
7012 spv::MemorySemanticsVolatileMask)) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007013 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7014 }
John Kessenich426394d2015-07-23 10:22:48 -06007015
Jeff Bolzbfd84a32021-01-27 13:14:34 -06007016 if (builder.getConstantScalar(scopeId) == spv::ScopeQueueFamily) {
7017 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7018 }
7019
Jeff Bolz36831c92018-09-05 10:11:41 -05007020 if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) {
7021 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7022 }
John Kessenich48d6e792017-10-06 21:21:48 -06007023
Jeff Bolz36831c92018-09-05 10:11:41 -05007024 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
7025 spvAtomicOperands.push_back(pointerId);
7026 spvAtomicOperands.push_back(scopeId);
7027 spvAtomicOperands.push_back(semanticsId);
7028 if (opCode == spv::OpAtomicCompareExchange) {
7029 spvAtomicOperands.push_back(semanticsId2);
7030 spvAtomicOperands.push_back(valueId);
7031 spvAtomicOperands.push_back(compareId);
7032 } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) {
7033 spvAtomicOperands.push_back(valueId);
7034 }
John Kessenich48d6e792017-10-06 21:21:48 -06007035
Jeff Bolz36831c92018-09-05 10:11:41 -05007036 if (opCode == spv::OpAtomicStore) {
7037 builder.createNoResultOp(opCode, spvAtomicOperands);
7038 return 0;
7039 } else {
7040 spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands);
7041
7042 // GLSL and HLSL atomic-counter decrement return post-decrement value,
7043 // while SPIR-V returns pre-decrement value. Translate between these semantics.
7044 if (op == glslang::EOpAtomicCounterDecrement)
7045 resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1));
7046
7047 return resultId;
7048 }
John Kessenich426394d2015-07-23 10:22:48 -06007049}
7050
John Kessenich91cef522016-05-05 16:45:40 -06007051// Create group invocation operations.
John Kessenich8985fc92020-03-03 10:25:07 -07007052spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId,
7053 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich91cef522016-05-05 16:45:40 -06007054{
John Kessenich66011cb2018-03-06 16:12:04 -07007055 bool isUnsigned = isTypeUnsignedInt(typeProxy);
7056 bool isFloat = isTypeFloat(typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +08007057
Rex Xu51596642016-09-21 18:56:12 +08007058 spv::Op opCode = spv::OpNop;
John Kessenich149afc32018-08-14 13:31:43 -06007059 std::vector<spv::IdImmediate> spvGroupOperands;
Rex Xu430ef402016-10-14 17:22:23 +08007060 spv::GroupOperation groupOperation = spv::GroupOperationMax;
7061
chaocf200da82016-12-20 12:44:35 -08007062 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
7063 op == glslang::EOpReadInvocation) {
Rex Xu51596642016-09-21 18:56:12 +08007064 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
7065 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08007066 } else if (op == glslang::EOpAnyInvocation ||
7067 op == glslang::EOpAllInvocations ||
7068 op == glslang::EOpAllInvocationsEqual) {
7069 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
7070 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
Rex Xu51596642016-09-21 18:56:12 +08007071 } else {
7072 builder.addCapability(spv::CapabilityGroups);
Rex Xu17ff3432016-10-14 17:41:45 +08007073 if (op == glslang::EOpMinInvocationsNonUniform ||
7074 op == glslang::EOpMaxInvocationsNonUniform ||
Rex Xu430ef402016-10-14 17:22:23 +08007075 op == glslang::EOpAddInvocationsNonUniform ||
7076 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
7077 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
7078 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
7079 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
7080 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
7081 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
Rex Xu17ff3432016-10-14 17:41:45 +08007082 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +08007083
Rex Xu430ef402016-10-14 17:22:23 +08007084 switch (op) {
7085 case glslang::EOpMinInvocations:
7086 case glslang::EOpMaxInvocations:
7087 case glslang::EOpAddInvocations:
7088 case glslang::EOpMinInvocationsNonUniform:
7089 case glslang::EOpMaxInvocationsNonUniform:
7090 case glslang::EOpAddInvocationsNonUniform:
7091 groupOperation = spv::GroupOperationReduce;
Rex Xu430ef402016-10-14 17:22:23 +08007092 break;
7093 case glslang::EOpMinInvocationsInclusiveScan:
7094 case glslang::EOpMaxInvocationsInclusiveScan:
7095 case glslang::EOpAddInvocationsInclusiveScan:
7096 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
7097 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
7098 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
7099 groupOperation = spv::GroupOperationInclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08007100 break;
7101 case glslang::EOpMinInvocationsExclusiveScan:
7102 case glslang::EOpMaxInvocationsExclusiveScan:
7103 case glslang::EOpAddInvocationsExclusiveScan:
7104 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
7105 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
7106 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
7107 groupOperation = spv::GroupOperationExclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08007108 break;
Mike Weiblen4e9e4002017-01-20 13:34:10 -07007109 default:
7110 break;
Rex Xu430ef402016-10-14 17:22:23 +08007111 }
John Kessenich149afc32018-08-14 13:31:43 -06007112 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7113 spvGroupOperands.push_back(scope);
7114 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06007115 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007116 spvGroupOperands.push_back(groupOp);
7117 }
Rex Xu51596642016-09-21 18:56:12 +08007118 }
7119
John Kessenich149afc32018-08-14 13:31:43 -06007120 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) {
7121 spv::IdImmediate op = { true, *opIt };
7122 spvGroupOperands.push_back(op);
7123 }
John Kessenich91cef522016-05-05 16:45:40 -06007124
7125 switch (op) {
7126 case glslang::EOpAnyInvocation:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08007127 opCode = spv::OpSubgroupAnyKHR;
Rex Xu51596642016-09-21 18:56:12 +08007128 break;
John Kessenich91cef522016-05-05 16:45:40 -06007129 case glslang::EOpAllInvocations:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08007130 opCode = spv::OpSubgroupAllKHR;
Rex Xu51596642016-09-21 18:56:12 +08007131 break;
John Kessenich91cef522016-05-05 16:45:40 -06007132 case glslang::EOpAllInvocationsEqual:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08007133 opCode = spv::OpSubgroupAllEqualKHR;
7134 break;
Rex Xu51596642016-09-21 18:56:12 +08007135 case glslang::EOpReadInvocation:
chaocf200da82016-12-20 12:44:35 -08007136 opCode = spv::OpSubgroupReadInvocationKHR;
Rex Xub7072052016-09-26 15:53:40 +08007137 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08007138 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08007139 break;
7140 case glslang::EOpReadFirstInvocation:
7141 opCode = spv::OpSubgroupFirstInvocationKHR;
7142 break;
7143 case glslang::EOpBallot:
7144 {
7145 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
7146 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
7147 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
7148 //
7149 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
7150 //
7151 spv::Id uintType = builder.makeUintType(32);
7152 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
7153 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
7154
7155 std::vector<spv::Id> components;
7156 components.push_back(builder.createCompositeExtract(result, uintType, 0));
7157 components.push_back(builder.createCompositeExtract(result, uintType, 1));
7158
7159 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
7160 return builder.createUnaryOp(spv::OpBitcast, typeId,
7161 builder.createCompositeConstruct(uvec2Type, components));
7162 }
7163
Rex Xu9d93a232016-05-05 12:30:44 +08007164 case glslang::EOpMinInvocations:
7165 case glslang::EOpMaxInvocations:
7166 case glslang::EOpAddInvocations:
Rex Xu430ef402016-10-14 17:22:23 +08007167 case glslang::EOpMinInvocationsInclusiveScan:
7168 case glslang::EOpMaxInvocationsInclusiveScan:
7169 case glslang::EOpAddInvocationsInclusiveScan:
7170 case glslang::EOpMinInvocationsExclusiveScan:
7171 case glslang::EOpMaxInvocationsExclusiveScan:
7172 case glslang::EOpAddInvocationsExclusiveScan:
7173 if (op == glslang::EOpMinInvocations ||
7174 op == glslang::EOpMinInvocationsInclusiveScan ||
7175 op == glslang::EOpMinInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08007176 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007177 opCode = spv::OpGroupFMin;
Rex Xu9d93a232016-05-05 12:30:44 +08007178 else {
7179 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007180 opCode = spv::OpGroupUMin;
Rex Xu9d93a232016-05-05 12:30:44 +08007181 else
Rex Xu51596642016-09-21 18:56:12 +08007182 opCode = spv::OpGroupSMin;
Rex Xu9d93a232016-05-05 12:30:44 +08007183 }
Rex Xu430ef402016-10-14 17:22:23 +08007184 } else if (op == glslang::EOpMaxInvocations ||
7185 op == glslang::EOpMaxInvocationsInclusiveScan ||
7186 op == glslang::EOpMaxInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08007187 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007188 opCode = spv::OpGroupFMax;
Rex Xu9d93a232016-05-05 12:30:44 +08007189 else {
7190 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007191 opCode = spv::OpGroupUMax;
Rex Xu9d93a232016-05-05 12:30:44 +08007192 else
Rex Xu51596642016-09-21 18:56:12 +08007193 opCode = spv::OpGroupSMax;
Rex Xu9d93a232016-05-05 12:30:44 +08007194 }
7195 } else {
7196 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007197 opCode = spv::OpGroupFAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08007198 else
Rex Xu51596642016-09-21 18:56:12 +08007199 opCode = spv::OpGroupIAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08007200 }
7201
Rex Xu2bbbe062016-08-23 15:41:05 +08007202 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08007203 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08007204
7205 break;
Rex Xu9d93a232016-05-05 12:30:44 +08007206 case glslang::EOpMinInvocationsNonUniform:
7207 case glslang::EOpMaxInvocationsNonUniform:
7208 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08007209 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
7210 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
7211 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
7212 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
7213 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
7214 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
7215 if (op == glslang::EOpMinInvocationsNonUniform ||
7216 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
7217 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08007218 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007219 opCode = spv::OpGroupFMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007220 else {
7221 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007222 opCode = spv::OpGroupUMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007223 else
Rex Xu51596642016-09-21 18:56:12 +08007224 opCode = spv::OpGroupSMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007225 }
7226 }
Rex Xu430ef402016-10-14 17:22:23 +08007227 else if (op == glslang::EOpMaxInvocationsNonUniform ||
7228 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
7229 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08007230 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007231 opCode = spv::OpGroupFMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007232 else {
7233 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007234 opCode = spv::OpGroupUMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007235 else
Rex Xu51596642016-09-21 18:56:12 +08007236 opCode = spv::OpGroupSMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007237 }
7238 }
7239 else {
7240 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007241 opCode = spv::OpGroupFAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007242 else
Rex Xu51596642016-09-21 18:56:12 +08007243 opCode = spv::OpGroupIAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007244 }
7245
Rex Xu2bbbe062016-08-23 15:41:05 +08007246 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08007247 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08007248
7249 break;
John Kessenich91cef522016-05-05 16:45:40 -06007250 default:
7251 logger->missingFunctionality("invocation operation");
7252 return spv::NoResult;
7253 }
Rex Xu51596642016-09-21 18:56:12 +08007254
7255 assert(opCode != spv::OpNop);
7256 return builder.createOp(opCode, typeId, spvGroupOperands);
John Kessenich91cef522016-05-05 16:45:40 -06007257}
7258
Rex Xu2bbbe062016-08-23 15:41:05 +08007259// Create group invocation operations on a vector
John Kessenich149afc32018-08-14 13:31:43 -06007260spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
7261 spv::Id typeId, std::vector<spv::Id>& operands)
Rex Xu2bbbe062016-08-23 15:41:05 +08007262{
7263 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
7264 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
Rex Xub7072052016-09-26 15:53:40 +08007265 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
chaocf200da82016-12-20 12:44:35 -08007266 op == spv::OpSubgroupReadInvocationKHR ||
John Kessenich8985fc92020-03-03 10:25:07 -07007267 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD ||
7268 op == spv::OpGroupSMinNonUniformAMD ||
7269 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD ||
7270 op == spv::OpGroupSMaxNonUniformAMD ||
Rex Xu2bbbe062016-08-23 15:41:05 +08007271 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
7272
7273 // Handle group invocation operations scalar by scalar.
7274 // The result type is the same type as the original type.
7275 // The algorithm is to:
7276 // - break the vector into scalars
7277 // - apply the operation to each scalar
7278 // - make a vector out the scalar results
7279
7280 // get the types sorted out
Rex Xub7072052016-09-26 15:53:40 +08007281 int numComponents = builder.getNumComponents(operands[0]);
7282 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
Rex Xu2bbbe062016-08-23 15:41:05 +08007283 std::vector<spv::Id> results;
7284
7285 // do each scalar op
7286 for (int comp = 0; comp < numComponents; ++comp) {
7287 std::vector<unsigned int> indexes;
7288 indexes.push_back(comp);
John Kessenich149afc32018-08-14 13:31:43 -06007289 spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) };
7290 std::vector<spv::IdImmediate> spvGroupOperands;
chaocf200da82016-12-20 12:44:35 -08007291 if (op == spv::OpSubgroupReadInvocationKHR) {
7292 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06007293 spv::IdImmediate operand = { true, operands[1] };
7294 spvGroupOperands.push_back(operand);
chaocf200da82016-12-20 12:44:35 -08007295 } else if (op == spv::OpGroupBroadcast) {
John Kessenich149afc32018-08-14 13:31:43 -06007296 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7297 spvGroupOperands.push_back(scope);
Rex Xub7072052016-09-26 15:53:40 +08007298 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06007299 spv::IdImmediate operand = { true, operands[1] };
7300 spvGroupOperands.push_back(operand);
Rex Xub7072052016-09-26 15:53:40 +08007301 } else {
John Kessenich149afc32018-08-14 13:31:43 -06007302 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7303 spvGroupOperands.push_back(scope);
John Kessenichd122a722018-09-18 03:43:30 -06007304 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007305 spvGroupOperands.push_back(groupOp);
Rex Xub7072052016-09-26 15:53:40 +08007306 spvGroupOperands.push_back(scalar);
7307 }
Rex Xu2bbbe062016-08-23 15:41:05 +08007308
Rex Xub7072052016-09-26 15:53:40 +08007309 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
Rex Xu2bbbe062016-08-23 15:41:05 +08007310 }
7311
7312 // put the pieces together
7313 return builder.createCompositeConstruct(typeId, results);
7314}
Rex Xu2bbbe062016-08-23 15:41:05 +08007315
John Kessenich66011cb2018-03-06 16:12:04 -07007316// Create subgroup invocation operations.
John Kessenich149afc32018-08-14 13:31:43 -06007317spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
7318 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich66011cb2018-03-06 16:12:04 -07007319{
7320 // Add the required capabilities.
7321 switch (op) {
7322 case glslang::EOpSubgroupElect:
7323 builder.addCapability(spv::CapabilityGroupNonUniform);
7324 break;
7325 case glslang::EOpSubgroupAll:
7326 case glslang::EOpSubgroupAny:
7327 case glslang::EOpSubgroupAllEqual:
7328 builder.addCapability(spv::CapabilityGroupNonUniform);
7329 builder.addCapability(spv::CapabilityGroupNonUniformVote);
7330 break;
7331 case glslang::EOpSubgroupBroadcast:
7332 case glslang::EOpSubgroupBroadcastFirst:
7333 case glslang::EOpSubgroupBallot:
7334 case glslang::EOpSubgroupInverseBallot:
7335 case glslang::EOpSubgroupBallotBitExtract:
7336 case glslang::EOpSubgroupBallotBitCount:
7337 case glslang::EOpSubgroupBallotInclusiveBitCount:
7338 case glslang::EOpSubgroupBallotExclusiveBitCount:
7339 case glslang::EOpSubgroupBallotFindLSB:
7340 case glslang::EOpSubgroupBallotFindMSB:
7341 builder.addCapability(spv::CapabilityGroupNonUniform);
7342 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
7343 break;
7344 case glslang::EOpSubgroupShuffle:
7345 case glslang::EOpSubgroupShuffleXor:
7346 builder.addCapability(spv::CapabilityGroupNonUniform);
7347 builder.addCapability(spv::CapabilityGroupNonUniformShuffle);
7348 break;
7349 case glslang::EOpSubgroupShuffleUp:
7350 case glslang::EOpSubgroupShuffleDown:
7351 builder.addCapability(spv::CapabilityGroupNonUniform);
7352 builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative);
7353 break;
7354 case glslang::EOpSubgroupAdd:
7355 case glslang::EOpSubgroupMul:
7356 case glslang::EOpSubgroupMin:
7357 case glslang::EOpSubgroupMax:
7358 case glslang::EOpSubgroupAnd:
7359 case glslang::EOpSubgroupOr:
7360 case glslang::EOpSubgroupXor:
7361 case glslang::EOpSubgroupInclusiveAdd:
7362 case glslang::EOpSubgroupInclusiveMul:
7363 case glslang::EOpSubgroupInclusiveMin:
7364 case glslang::EOpSubgroupInclusiveMax:
7365 case glslang::EOpSubgroupInclusiveAnd:
7366 case glslang::EOpSubgroupInclusiveOr:
7367 case glslang::EOpSubgroupInclusiveXor:
7368 case glslang::EOpSubgroupExclusiveAdd:
7369 case glslang::EOpSubgroupExclusiveMul:
7370 case glslang::EOpSubgroupExclusiveMin:
7371 case glslang::EOpSubgroupExclusiveMax:
7372 case glslang::EOpSubgroupExclusiveAnd:
7373 case glslang::EOpSubgroupExclusiveOr:
7374 case glslang::EOpSubgroupExclusiveXor:
7375 builder.addCapability(spv::CapabilityGroupNonUniform);
7376 builder.addCapability(spv::CapabilityGroupNonUniformArithmetic);
7377 break;
7378 case glslang::EOpSubgroupClusteredAdd:
7379 case glslang::EOpSubgroupClusteredMul:
7380 case glslang::EOpSubgroupClusteredMin:
7381 case glslang::EOpSubgroupClusteredMax:
7382 case glslang::EOpSubgroupClusteredAnd:
7383 case glslang::EOpSubgroupClusteredOr:
7384 case glslang::EOpSubgroupClusteredXor:
7385 builder.addCapability(spv::CapabilityGroupNonUniform);
7386 builder.addCapability(spv::CapabilityGroupNonUniformClustered);
7387 break;
7388 case glslang::EOpSubgroupQuadBroadcast:
7389 case glslang::EOpSubgroupQuadSwapHorizontal:
7390 case glslang::EOpSubgroupQuadSwapVertical:
7391 case glslang::EOpSubgroupQuadSwapDiagonal:
7392 builder.addCapability(spv::CapabilityGroupNonUniform);
7393 builder.addCapability(spv::CapabilityGroupNonUniformQuad);
7394 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007395 case glslang::EOpSubgroupPartitionedAdd:
7396 case glslang::EOpSubgroupPartitionedMul:
7397 case glslang::EOpSubgroupPartitionedMin:
7398 case glslang::EOpSubgroupPartitionedMax:
7399 case glslang::EOpSubgroupPartitionedAnd:
7400 case glslang::EOpSubgroupPartitionedOr:
7401 case glslang::EOpSubgroupPartitionedXor:
7402 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7403 case glslang::EOpSubgroupPartitionedInclusiveMul:
7404 case glslang::EOpSubgroupPartitionedInclusiveMin:
7405 case glslang::EOpSubgroupPartitionedInclusiveMax:
7406 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7407 case glslang::EOpSubgroupPartitionedInclusiveOr:
7408 case glslang::EOpSubgroupPartitionedInclusiveXor:
7409 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7410 case glslang::EOpSubgroupPartitionedExclusiveMul:
7411 case glslang::EOpSubgroupPartitionedExclusiveMin:
7412 case glslang::EOpSubgroupPartitionedExclusiveMax:
7413 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7414 case glslang::EOpSubgroupPartitionedExclusiveOr:
7415 case glslang::EOpSubgroupPartitionedExclusiveXor:
7416 builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned);
7417 builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV);
7418 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007419 default: assert(0 && "Unhandled subgroup operation!");
7420 }
7421
Jeff Bolzc5b669e2019-09-08 08:49:18 -05007422
7423 const bool isUnsigned = isTypeUnsignedInt(typeProxy);
7424 const bool isFloat = isTypeFloat(typeProxy);
John Kessenich66011cb2018-03-06 16:12:04 -07007425 const bool isBool = typeProxy == glslang::EbtBool;
7426
7427 spv::Op opCode = spv::OpNop;
7428
7429 // Figure out which opcode to use.
7430 switch (op) {
7431 case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break;
7432 case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break;
7433 case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break;
7434 case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break;
7435 case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break;
7436 case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break;
7437 case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break;
7438 case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break;
7439 case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break;
7440 case glslang::EOpSubgroupBallotBitCount:
7441 case glslang::EOpSubgroupBallotInclusiveBitCount:
7442 case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break;
7443 case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break;
7444 case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break;
7445 case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break;
7446 case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break;
7447 case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break;
7448 case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break;
7449 case glslang::EOpSubgroupAdd:
7450 case glslang::EOpSubgroupInclusiveAdd:
7451 case glslang::EOpSubgroupExclusiveAdd:
7452 case glslang::EOpSubgroupClusteredAdd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007453 case glslang::EOpSubgroupPartitionedAdd:
7454 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7455 case glslang::EOpSubgroupPartitionedExclusiveAdd:
John Kessenich66011cb2018-03-06 16:12:04 -07007456 if (isFloat) {
7457 opCode = spv::OpGroupNonUniformFAdd;
7458 } else {
7459 opCode = spv::OpGroupNonUniformIAdd;
7460 }
7461 break;
7462 case glslang::EOpSubgroupMul:
7463 case glslang::EOpSubgroupInclusiveMul:
7464 case glslang::EOpSubgroupExclusiveMul:
7465 case glslang::EOpSubgroupClusteredMul:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007466 case glslang::EOpSubgroupPartitionedMul:
7467 case glslang::EOpSubgroupPartitionedInclusiveMul:
7468 case glslang::EOpSubgroupPartitionedExclusiveMul:
John Kessenich66011cb2018-03-06 16:12:04 -07007469 if (isFloat) {
7470 opCode = spv::OpGroupNonUniformFMul;
7471 } else {
7472 opCode = spv::OpGroupNonUniformIMul;
7473 }
7474 break;
7475 case glslang::EOpSubgroupMin:
7476 case glslang::EOpSubgroupInclusiveMin:
7477 case glslang::EOpSubgroupExclusiveMin:
7478 case glslang::EOpSubgroupClusteredMin:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007479 case glslang::EOpSubgroupPartitionedMin:
7480 case glslang::EOpSubgroupPartitionedInclusiveMin:
7481 case glslang::EOpSubgroupPartitionedExclusiveMin:
John Kessenich66011cb2018-03-06 16:12:04 -07007482 if (isFloat) {
7483 opCode = spv::OpGroupNonUniformFMin;
7484 } else if (isUnsigned) {
7485 opCode = spv::OpGroupNonUniformUMin;
7486 } else {
7487 opCode = spv::OpGroupNonUniformSMin;
7488 }
7489 break;
7490 case glslang::EOpSubgroupMax:
7491 case glslang::EOpSubgroupInclusiveMax:
7492 case glslang::EOpSubgroupExclusiveMax:
7493 case glslang::EOpSubgroupClusteredMax:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007494 case glslang::EOpSubgroupPartitionedMax:
7495 case glslang::EOpSubgroupPartitionedInclusiveMax:
7496 case glslang::EOpSubgroupPartitionedExclusiveMax:
John Kessenich66011cb2018-03-06 16:12:04 -07007497 if (isFloat) {
7498 opCode = spv::OpGroupNonUniformFMax;
7499 } else if (isUnsigned) {
7500 opCode = spv::OpGroupNonUniformUMax;
7501 } else {
7502 opCode = spv::OpGroupNonUniformSMax;
7503 }
7504 break;
7505 case glslang::EOpSubgroupAnd:
7506 case glslang::EOpSubgroupInclusiveAnd:
7507 case glslang::EOpSubgroupExclusiveAnd:
7508 case glslang::EOpSubgroupClusteredAnd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007509 case glslang::EOpSubgroupPartitionedAnd:
7510 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7511 case glslang::EOpSubgroupPartitionedExclusiveAnd:
John Kessenich66011cb2018-03-06 16:12:04 -07007512 if (isBool) {
7513 opCode = spv::OpGroupNonUniformLogicalAnd;
7514 } else {
7515 opCode = spv::OpGroupNonUniformBitwiseAnd;
7516 }
7517 break;
7518 case glslang::EOpSubgroupOr:
7519 case glslang::EOpSubgroupInclusiveOr:
7520 case glslang::EOpSubgroupExclusiveOr:
7521 case glslang::EOpSubgroupClusteredOr:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007522 case glslang::EOpSubgroupPartitionedOr:
7523 case glslang::EOpSubgroupPartitionedInclusiveOr:
7524 case glslang::EOpSubgroupPartitionedExclusiveOr:
John Kessenich66011cb2018-03-06 16:12:04 -07007525 if (isBool) {
7526 opCode = spv::OpGroupNonUniformLogicalOr;
7527 } else {
7528 opCode = spv::OpGroupNonUniformBitwiseOr;
7529 }
7530 break;
7531 case glslang::EOpSubgroupXor:
7532 case glslang::EOpSubgroupInclusiveXor:
7533 case glslang::EOpSubgroupExclusiveXor:
7534 case glslang::EOpSubgroupClusteredXor:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007535 case glslang::EOpSubgroupPartitionedXor:
7536 case glslang::EOpSubgroupPartitionedInclusiveXor:
7537 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07007538 if (isBool) {
7539 opCode = spv::OpGroupNonUniformLogicalXor;
7540 } else {
7541 opCode = spv::OpGroupNonUniformBitwiseXor;
7542 }
7543 break;
7544 case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break;
7545 case glslang::EOpSubgroupQuadSwapHorizontal:
7546 case glslang::EOpSubgroupQuadSwapVertical:
7547 case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break;
7548 default: assert(0 && "Unhandled subgroup operation!");
7549 }
7550
John Kessenich149afc32018-08-14 13:31:43 -06007551 // get the right Group Operation
7552 spv::GroupOperation groupOperation = spv::GroupOperationMax;
John Kessenich66011cb2018-03-06 16:12:04 -07007553 switch (op) {
John Kessenich149afc32018-08-14 13:31:43 -06007554 default:
7555 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007556 case glslang::EOpSubgroupBallotBitCount:
7557 case glslang::EOpSubgroupAdd:
7558 case glslang::EOpSubgroupMul:
7559 case glslang::EOpSubgroupMin:
7560 case glslang::EOpSubgroupMax:
7561 case glslang::EOpSubgroupAnd:
7562 case glslang::EOpSubgroupOr:
7563 case glslang::EOpSubgroupXor:
John Kessenich149afc32018-08-14 13:31:43 -06007564 groupOperation = spv::GroupOperationReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07007565 break;
7566 case glslang::EOpSubgroupBallotInclusiveBitCount:
7567 case glslang::EOpSubgroupInclusiveAdd:
7568 case glslang::EOpSubgroupInclusiveMul:
7569 case glslang::EOpSubgroupInclusiveMin:
7570 case glslang::EOpSubgroupInclusiveMax:
7571 case glslang::EOpSubgroupInclusiveAnd:
7572 case glslang::EOpSubgroupInclusiveOr:
7573 case glslang::EOpSubgroupInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007574 groupOperation = spv::GroupOperationInclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07007575 break;
7576 case glslang::EOpSubgroupBallotExclusiveBitCount:
7577 case glslang::EOpSubgroupExclusiveAdd:
7578 case glslang::EOpSubgroupExclusiveMul:
7579 case glslang::EOpSubgroupExclusiveMin:
7580 case glslang::EOpSubgroupExclusiveMax:
7581 case glslang::EOpSubgroupExclusiveAnd:
7582 case glslang::EOpSubgroupExclusiveOr:
7583 case glslang::EOpSubgroupExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007584 groupOperation = spv::GroupOperationExclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07007585 break;
7586 case glslang::EOpSubgroupClusteredAdd:
7587 case glslang::EOpSubgroupClusteredMul:
7588 case glslang::EOpSubgroupClusteredMin:
7589 case glslang::EOpSubgroupClusteredMax:
7590 case glslang::EOpSubgroupClusteredAnd:
7591 case glslang::EOpSubgroupClusteredOr:
7592 case glslang::EOpSubgroupClusteredXor:
John Kessenich149afc32018-08-14 13:31:43 -06007593 groupOperation = spv::GroupOperationClusteredReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07007594 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007595 case glslang::EOpSubgroupPartitionedAdd:
7596 case glslang::EOpSubgroupPartitionedMul:
7597 case glslang::EOpSubgroupPartitionedMin:
7598 case glslang::EOpSubgroupPartitionedMax:
7599 case glslang::EOpSubgroupPartitionedAnd:
7600 case glslang::EOpSubgroupPartitionedOr:
7601 case glslang::EOpSubgroupPartitionedXor:
John Kessenich149afc32018-08-14 13:31:43 -06007602 groupOperation = spv::GroupOperationPartitionedReduceNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007603 break;
7604 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7605 case glslang::EOpSubgroupPartitionedInclusiveMul:
7606 case glslang::EOpSubgroupPartitionedInclusiveMin:
7607 case glslang::EOpSubgroupPartitionedInclusiveMax:
7608 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7609 case glslang::EOpSubgroupPartitionedInclusiveOr:
7610 case glslang::EOpSubgroupPartitionedInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007611 groupOperation = spv::GroupOperationPartitionedInclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007612 break;
7613 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7614 case glslang::EOpSubgroupPartitionedExclusiveMul:
7615 case glslang::EOpSubgroupPartitionedExclusiveMin:
7616 case glslang::EOpSubgroupPartitionedExclusiveMax:
7617 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7618 case glslang::EOpSubgroupPartitionedExclusiveOr:
7619 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007620 groupOperation = spv::GroupOperationPartitionedExclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007621 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007622 }
7623
John Kessenich149afc32018-08-14 13:31:43 -06007624 // build the instruction
7625 std::vector<spv::IdImmediate> spvGroupOperands;
7626
7627 // Every operation begins with the Execution Scope operand.
7628 spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7629 spvGroupOperands.push_back(executionScope);
7630
7631 // Next, for all operations that use a Group Operation, push that as an operand.
7632 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06007633 spv::IdImmediate groupOperand = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007634 spvGroupOperands.push_back(groupOperand);
7635 }
7636
John Kessenich66011cb2018-03-06 16:12:04 -07007637 // Push back the operands next.
John Kessenich149afc32018-08-14 13:31:43 -06007638 for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) {
7639 spv::IdImmediate operand = { true, *opIt };
7640 spvGroupOperands.push_back(operand);
John Kessenich66011cb2018-03-06 16:12:04 -07007641 }
7642
7643 // Some opcodes have additional operands.
John Kessenich149afc32018-08-14 13:31:43 -06007644 spv::Id directionId = spv::NoResult;
John Kessenich66011cb2018-03-06 16:12:04 -07007645 switch (op) {
7646 default: break;
John Kessenich149afc32018-08-14 13:31:43 -06007647 case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break;
7648 case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break;
7649 case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break;
7650 }
7651 if (directionId != spv::NoResult) {
7652 spv::IdImmediate direction = { true, directionId };
7653 spvGroupOperands.push_back(direction);
John Kessenich66011cb2018-03-06 16:12:04 -07007654 }
7655
7656 return builder.createOp(opCode, typeId, spvGroupOperands);
7657}
7658
John Kessenich8985fc92020-03-03 10:25:07 -07007659spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision,
7660 spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06007661{
John Kessenich66011cb2018-03-06 16:12:04 -07007662 bool isUnsigned = isTypeUnsignedInt(typeProxy);
7663 bool isFloat = isTypeFloat(typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -06007664
John Kessenich140f3df2015-06-26 16:58:36 -06007665 spv::Op opCode = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08007666 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06007667 int libCall = -1;
Mark Adams364c21c2016-01-06 13:41:02 -05007668 size_t consumedOperands = operands.size();
John Kessenich55e7d112015-11-15 21:33:39 -07007669 spv::Id typeId0 = 0;
7670 if (consumedOperands > 0)
7671 typeId0 = builder.getTypeId(operands[0]);
Rex Xu470026f2017-03-29 17:12:40 +08007672 spv::Id typeId1 = 0;
7673 if (consumedOperands > 1)
7674 typeId1 = builder.getTypeId(operands[1]);
John Kessenich55e7d112015-11-15 21:33:39 -07007675 spv::Id frexpIntType = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007676
7677 switch (op) {
7678 case glslang::EOpMin:
John Kessenich5e4b1242015-08-06 22:53:06 -06007679 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007680 libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin;
John Kessenich5e4b1242015-08-06 22:53:06 -06007681 else if (isUnsigned)
7682 libCall = spv::GLSLstd450UMin;
7683 else
7684 libCall = spv::GLSLstd450SMin;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007685 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007686 break;
7687 case glslang::EOpModf:
John Kessenich5e4b1242015-08-06 22:53:06 -06007688 libCall = spv::GLSLstd450Modf;
John Kessenich140f3df2015-06-26 16:58:36 -06007689 break;
7690 case glslang::EOpMax:
John Kessenich5e4b1242015-08-06 22:53:06 -06007691 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007692 libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax;
John Kessenich5e4b1242015-08-06 22:53:06 -06007693 else if (isUnsigned)
7694 libCall = spv::GLSLstd450UMax;
7695 else
7696 libCall = spv::GLSLstd450SMax;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007697 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007698 break;
7699 case glslang::EOpPow:
John Kessenich5e4b1242015-08-06 22:53:06 -06007700 libCall = spv::GLSLstd450Pow;
John Kessenich140f3df2015-06-26 16:58:36 -06007701 break;
7702 case glslang::EOpDot:
7703 opCode = spv::OpDot;
7704 break;
7705 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06007706 libCall = spv::GLSLstd450Atan2;
John Kessenich140f3df2015-06-26 16:58:36 -06007707 break;
7708
7709 case glslang::EOpClamp:
John Kessenich5e4b1242015-08-06 22:53:06 -06007710 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007711 libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp;
John Kessenich5e4b1242015-08-06 22:53:06 -06007712 else if (isUnsigned)
7713 libCall = spv::GLSLstd450UClamp;
7714 else
7715 libCall = spv::GLSLstd450SClamp;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007716 builder.promoteScalar(precision, operands.front(), operands[1]);
7717 builder.promoteScalar(precision, operands.front(), operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007718 break;
7719 case glslang::EOpMix:
Rex Xud715adc2016-03-15 12:08:31 +08007720 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
7721 assert(isFloat);
John Kessenich55e7d112015-11-15 21:33:39 -07007722 libCall = spv::GLSLstd450FMix;
Rex Xud715adc2016-03-15 12:08:31 +08007723 } else {
John Kessenich6c292d32016-02-15 20:58:50 -07007724 opCode = spv::OpSelect;
Rex Xud715adc2016-03-15 12:08:31 +08007725 std::swap(operands.front(), operands.back());
John Kessenich6c292d32016-02-15 20:58:50 -07007726 }
John Kesseniche7c83cf2015-12-13 13:34:37 -07007727 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007728 break;
7729 case glslang::EOpStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007730 libCall = spv::GLSLstd450Step;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007731 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007732 break;
7733 case glslang::EOpSmoothStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007734 libCall = spv::GLSLstd450SmoothStep;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007735 builder.promoteScalar(precision, operands[0], operands[2]);
7736 builder.promoteScalar(precision, operands[1], operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007737 break;
7738
7739 case glslang::EOpDistance:
John Kessenich5e4b1242015-08-06 22:53:06 -06007740 libCall = spv::GLSLstd450Distance;
John Kessenich140f3df2015-06-26 16:58:36 -06007741 break;
7742 case glslang::EOpCross:
John Kessenich5e4b1242015-08-06 22:53:06 -06007743 libCall = spv::GLSLstd450Cross;
John Kessenich140f3df2015-06-26 16:58:36 -06007744 break;
7745 case glslang::EOpFaceForward:
John Kessenich5e4b1242015-08-06 22:53:06 -06007746 libCall = spv::GLSLstd450FaceForward;
John Kessenich140f3df2015-06-26 16:58:36 -06007747 break;
7748 case glslang::EOpReflect:
John Kessenich5e4b1242015-08-06 22:53:06 -06007749 libCall = spv::GLSLstd450Reflect;
John Kessenich140f3df2015-06-26 16:58:36 -06007750 break;
7751 case glslang::EOpRefract:
John Kessenich5e4b1242015-08-06 22:53:06 -06007752 libCall = spv::GLSLstd450Refract;
John Kessenich140f3df2015-06-26 16:58:36 -06007753 break;
John Kessenich3dd1ce52019-10-17 07:08:40 -06007754 case glslang::EOpBarrier:
7755 {
7756 // This is for the extended controlBarrier function, with four operands.
7757 // The unextended barrier() goes through createNoArgOperation.
7758 assert(operands.size() == 4);
7759 unsigned int executionScope = builder.getConstantScalar(operands[0]);
7760 unsigned int memoryScope = builder.getConstantScalar(operands[1]);
7761 unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]);
John Kessenich8985fc92020-03-03 10:25:07 -07007762 builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope,
7763 (spv::MemorySemanticsMask)semantics);
John Kessenich3dd1ce52019-10-17 07:08:40 -06007764 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7765 spv::MemorySemanticsMakeVisibleKHRMask |
7766 spv::MemorySemanticsOutputMemoryKHRMask |
7767 spv::MemorySemanticsVolatileMask)) {
7768 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7769 }
John Kessenich8985fc92020-03-03 10:25:07 -07007770 if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice ||
7771 memoryScope == spv::ScopeDevice)) {
John Kessenich3dd1ce52019-10-17 07:08:40 -06007772 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7773 }
7774 return 0;
7775 }
7776 break;
7777 case glslang::EOpMemoryBarrier:
7778 {
7779 // This is for the extended memoryBarrier function, with three operands.
7780 // The unextended memoryBarrier() goes through createNoArgOperation.
7781 assert(operands.size() == 3);
7782 unsigned int memoryScope = builder.getConstantScalar(operands[0]);
7783 unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]);
7784 builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
7785 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7786 spv::MemorySemanticsMakeVisibleKHRMask |
7787 spv::MemorySemanticsOutputMemoryKHRMask |
7788 spv::MemorySemanticsVolatileMask)) {
7789 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7790 }
7791 if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) {
7792 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7793 }
7794 return 0;
7795 }
7796 break;
7797
John Kessenicha28f7a72019-08-06 07:00:58 -06007798#ifndef GLSLANG_WEB
Rex Xu7a26c172015-12-08 17:12:09 +08007799 case glslang::EOpInterpolateAtSample:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007800 if (typeProxy == glslang::EbtFloat16)
7801 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007802 libCall = spv::GLSLstd450InterpolateAtSample;
7803 break;
7804 case glslang::EOpInterpolateAtOffset:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007805 if (typeProxy == glslang::EbtFloat16)
7806 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007807 libCall = spv::GLSLstd450InterpolateAtOffset;
7808 break;
John Kessenich55e7d112015-11-15 21:33:39 -07007809 case glslang::EOpAddCarry:
7810 opCode = spv::OpIAddCarry;
7811 typeId = builder.makeStructResultType(typeId0, typeId0);
7812 consumedOperands = 2;
7813 break;
7814 case glslang::EOpSubBorrow:
7815 opCode = spv::OpISubBorrow;
7816 typeId = builder.makeStructResultType(typeId0, typeId0);
7817 consumedOperands = 2;
7818 break;
7819 case glslang::EOpUMulExtended:
7820 opCode = spv::OpUMulExtended;
7821 typeId = builder.makeStructResultType(typeId0, typeId0);
7822 consumedOperands = 2;
7823 break;
7824 case glslang::EOpIMulExtended:
7825 opCode = spv::OpSMulExtended;
7826 typeId = builder.makeStructResultType(typeId0, typeId0);
7827 consumedOperands = 2;
7828 break;
7829 case glslang::EOpBitfieldExtract:
7830 if (isUnsigned)
7831 opCode = spv::OpBitFieldUExtract;
7832 else
7833 opCode = spv::OpBitFieldSExtract;
7834 break;
7835 case glslang::EOpBitfieldInsert:
7836 opCode = spv::OpBitFieldInsert;
7837 break;
7838
7839 case glslang::EOpFma:
7840 libCall = spv::GLSLstd450Fma;
7841 break;
7842 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007843 {
7844 libCall = spv::GLSLstd450FrexpStruct;
7845 assert(builder.isPointerType(typeId1));
7846 typeId1 = builder.getContainedTypeId(typeId1);
Rex Xu470026f2017-03-29 17:12:40 +08007847 int width = builder.getScalarTypeWidth(typeId1);
Rex Xu7c88aff2018-04-11 16:56:50 +08007848 if (width == 16)
7849 // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16
7850 builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16);
Rex Xu470026f2017-03-29 17:12:40 +08007851 if (builder.getNumComponents(operands[0]) == 1)
7852 frexpIntType = builder.makeIntegerType(width, true);
7853 else
John Kessenich8985fc92020-03-03 10:25:07 -07007854 frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true),
7855 builder.getNumComponents(operands[0]));
Rex Xu470026f2017-03-29 17:12:40 +08007856 typeId = builder.makeStructResultType(typeId0, frexpIntType);
7857 consumedOperands = 1;
7858 }
John Kessenich55e7d112015-11-15 21:33:39 -07007859 break;
7860 case glslang::EOpLdexp:
7861 libCall = spv::GLSLstd450Ldexp;
7862 break;
7863
Rex Xu574ab042016-04-14 16:53:07 +08007864 case glslang::EOpReadInvocation:
Rex Xu51596642016-09-21 18:56:12 +08007865 return createInvocationsOperation(op, typeId, operands, typeProxy);
Rex Xu574ab042016-04-14 16:53:07 +08007866
John Kessenich66011cb2018-03-06 16:12:04 -07007867 case glslang::EOpSubgroupBroadcast:
7868 case glslang::EOpSubgroupBallotBitExtract:
7869 case glslang::EOpSubgroupShuffle:
7870 case glslang::EOpSubgroupShuffleXor:
7871 case glslang::EOpSubgroupShuffleUp:
7872 case glslang::EOpSubgroupShuffleDown:
7873 case glslang::EOpSubgroupClusteredAdd:
7874 case glslang::EOpSubgroupClusteredMul:
7875 case glslang::EOpSubgroupClusteredMin:
7876 case glslang::EOpSubgroupClusteredMax:
7877 case glslang::EOpSubgroupClusteredAnd:
7878 case glslang::EOpSubgroupClusteredOr:
7879 case glslang::EOpSubgroupClusteredXor:
7880 case glslang::EOpSubgroupQuadBroadcast:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007881 case glslang::EOpSubgroupPartitionedAdd:
7882 case glslang::EOpSubgroupPartitionedMul:
7883 case glslang::EOpSubgroupPartitionedMin:
7884 case glslang::EOpSubgroupPartitionedMax:
7885 case glslang::EOpSubgroupPartitionedAnd:
7886 case glslang::EOpSubgroupPartitionedOr:
7887 case glslang::EOpSubgroupPartitionedXor:
7888 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7889 case glslang::EOpSubgroupPartitionedInclusiveMul:
7890 case glslang::EOpSubgroupPartitionedInclusiveMin:
7891 case glslang::EOpSubgroupPartitionedInclusiveMax:
7892 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7893 case glslang::EOpSubgroupPartitionedInclusiveOr:
7894 case glslang::EOpSubgroupPartitionedInclusiveXor:
7895 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7896 case glslang::EOpSubgroupPartitionedExclusiveMul:
7897 case glslang::EOpSubgroupPartitionedExclusiveMin:
7898 case glslang::EOpSubgroupPartitionedExclusiveMax:
7899 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7900 case glslang::EOpSubgroupPartitionedExclusiveOr:
7901 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07007902 return createSubgroupOperation(op, typeId, operands, typeProxy);
7903
Rex Xu9d93a232016-05-05 12:30:44 +08007904 case glslang::EOpSwizzleInvocations:
7905 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7906 libCall = spv::SwizzleInvocationsAMD;
7907 break;
7908 case glslang::EOpSwizzleInvocationsMasked:
7909 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7910 libCall = spv::SwizzleInvocationsMaskedAMD;
7911 break;
7912 case glslang::EOpWriteInvocation:
7913 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7914 libCall = spv::WriteInvocationAMD;
7915 break;
7916
7917 case glslang::EOpMin3:
7918 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7919 if (isFloat)
7920 libCall = spv::FMin3AMD;
7921 else {
7922 if (isUnsigned)
7923 libCall = spv::UMin3AMD;
7924 else
7925 libCall = spv::SMin3AMD;
7926 }
7927 break;
7928 case glslang::EOpMax3:
7929 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7930 if (isFloat)
7931 libCall = spv::FMax3AMD;
7932 else {
7933 if (isUnsigned)
7934 libCall = spv::UMax3AMD;
7935 else
7936 libCall = spv::SMax3AMD;
7937 }
7938 break;
7939 case glslang::EOpMid3:
7940 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7941 if (isFloat)
7942 libCall = spv::FMid3AMD;
7943 else {
7944 if (isUnsigned)
7945 libCall = spv::UMid3AMD;
7946 else
7947 libCall = spv::SMid3AMD;
7948 }
7949 break;
7950
7951 case glslang::EOpInterpolateAtVertex:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007952 if (typeProxy == glslang::EbtFloat16)
7953 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu9d93a232016-05-05 12:30:44 +08007954 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
7955 libCall = spv::InterpolateAtVertexAMD;
7956 break;
Chao Chen3c366992018-09-19 11:41:59 -07007957
Daniel Kochdb32b242020-03-17 20:42:47 -04007958 case glslang::EOpReportIntersection:
Chao Chenb50c02e2018-09-19 11:42:24 -07007959 typeId = builder.makeBoolType();
Daniel Kochdb32b242020-03-17 20:42:47 -04007960 opCode = spv::OpReportIntersectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007961 break;
Daniel Kochffccefd2020-11-23 15:41:27 -05007962 case glslang::EOpTraceNV:
7963 builder.createNoResultOp(spv::OpTraceNV, operands);
7964 return 0;
7965 case glslang::EOpTraceKHR:
Daniel Kochdb32b242020-03-17 20:42:47 -04007966 builder.createNoResultOp(spv::OpTraceRayKHR, operands);
Ashwin Leleff1783d2018-10-22 16:41:44 -07007967 return 0;
Daniel Kochffccefd2020-11-23 15:41:27 -05007968 case glslang::EOpExecuteCallableNV:
7969 builder.createNoResultOp(spv::OpExecuteCallableNV, operands);
7970 return 0;
7971 case glslang::EOpExecuteCallableKHR:
Daniel Kochdb32b242020-03-17 20:42:47 -04007972 builder.createNoResultOp(spv::OpExecuteCallableKHR, operands);
Chao Chenb50c02e2018-09-19 11:42:24 -07007973 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007974
7975 case glslang::EOpRayQueryInitialize:
Torosdagli06c2eee2020-03-19 11:09:57 -04007976 builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands);
7977 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007978 case glslang::EOpRayQueryTerminate:
Torosdagli06c2eee2020-03-19 11:09:57 -04007979 builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands);
7980 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007981 case glslang::EOpRayQueryGenerateIntersection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007982 builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands);
7983 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007984 case glslang::EOpRayQueryConfirmIntersection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007985 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands);
7986 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007987 case glslang::EOpRayQueryProceed:
Torosdagli06c2eee2020-03-19 11:09:57 -04007988 typeId = builder.makeBoolType();
7989 opCode = spv::OpRayQueryProceedKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007990 break;
7991 case glslang::EOpRayQueryGetIntersectionType:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04007992 typeId = builder.makeUintType(32);
Torosdagli06c2eee2020-03-19 11:09:57 -04007993 opCode = spv::OpRayQueryGetIntersectionTypeKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007994 break;
7995 case glslang::EOpRayQueryGetRayTMin:
Torosdagli06c2eee2020-03-19 11:09:57 -04007996 typeId = builder.makeFloatType(32);
7997 opCode = spv::OpRayQueryGetRayTMinKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007998 break;
7999 case glslang::EOpRayQueryGetRayFlags:
Torosdagli06c2eee2020-03-19 11:09:57 -04008000 typeId = builder.makeIntType(32);
8001 opCode = spv::OpRayQueryGetRayFlagsKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008002 break;
8003 case glslang::EOpRayQueryGetIntersectionT:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04008004 typeId = builder.makeFloatType(32);
Torosdagli06c2eee2020-03-19 11:09:57 -04008005 opCode = spv::OpRayQueryGetIntersectionTKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008006 break;
8007 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04008008 typeId = builder.makeIntType(32);
8009 opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008010 break;
8011 case glslang::EOpRayQueryGetIntersectionInstanceId:
Torosdagli06c2eee2020-03-19 11:09:57 -04008012 typeId = builder.makeIntType(32);
8013 opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008014 break;
8015 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
Daniel Kochc0bcfaf2020-12-12 12:34:24 -05008016 typeId = builder.makeUintType(32);
Torosdagli06c2eee2020-03-19 11:09:57 -04008017 opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008018 break;
8019 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04008020 typeId = builder.makeIntType(32);
8021 opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008022 break;
8023 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04008024 typeId = builder.makeIntType(32);
8025 opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008026 break;
8027 case glslang::EOpRayQueryGetIntersectionBarycentrics:
Torosdagli06c2eee2020-03-19 11:09:57 -04008028 typeId = builder.makeVectorType(builder.makeFloatType(32), 2);
8029 opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008030 break;
8031 case glslang::EOpRayQueryGetIntersectionFrontFace:
Torosdagli06c2eee2020-03-19 11:09:57 -04008032 typeId = builder.makeBoolType();
8033 opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008034 break;
8035 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
Torosdagli06c2eee2020-03-19 11:09:57 -04008036 typeId = builder.makeBoolType();
8037 opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008038 break;
8039 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
Torosdagli06c2eee2020-03-19 11:09:57 -04008040 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
8041 opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008042 break;
8043 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
Torosdagli06c2eee2020-03-19 11:09:57 -04008044 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
8045 opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008046 break;
8047 case glslang::EOpRayQueryGetWorldRayDirection:
Torosdagli06c2eee2020-03-19 11:09:57 -04008048 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
8049 opCode = spv::OpRayQueryGetWorldRayDirectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008050 break;
8051 case glslang::EOpRayQueryGetWorldRayOrigin:
Torosdagli06c2eee2020-03-19 11:09:57 -04008052 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
8053 opCode = spv::OpRayQueryGetWorldRayOriginKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008054 break;
8055 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
Torosdagli06c2eee2020-03-19 11:09:57 -04008056 typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
Torosdagli06c2eee2020-03-19 11:09:57 -04008057 opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008058 break;
8059 case glslang::EOpRayQueryGetIntersectionWorldToObject:
Torosdagli06c2eee2020-03-19 11:09:57 -04008060 typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
Torosdagli06c2eee2020-03-19 11:09:57 -04008061 opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04008062 break;
Chao Chen3c366992018-09-19 11:41:59 -07008063 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
8064 builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands);
8065 return 0;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06008066 case glslang::EOpCooperativeMatrixMulAdd:
8067 opCode = spv::OpCooperativeMatrixMulAddNV;
8068 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06008069#endif // GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06008070 default:
8071 return 0;
8072 }
8073
8074 spv::Id id = 0;
John Kessenich2359bd02015-12-06 19:29:11 -07008075 if (libCall >= 0) {
David Neto8d63a3d2015-12-07 16:17:06 -05008076 // Use an extended instruction from the standard library.
8077 // Construct the call arguments, without modifying the original operands vector.
8078 // We might need the remaining arguments, e.g. in the EOpFrexp case.
8079 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
Rex Xu9d93a232016-05-05 12:30:44 +08008080 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
t.jungb16bea82018-11-15 10:21:36 +01008081 } else if (opCode == spv::OpDot && !isFloat) {
8082 // int dot(int, int)
8083 // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached
8084 const int componentCount = builder.getNumComponents(operands[0]);
8085 spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]);
8086 builder.setPrecision(mulOp, precision);
8087 id = builder.createCompositeExtract(mulOp, typeId, 0);
8088 for (int i = 1; i < componentCount; ++i) {
8089 builder.setPrecision(id, precision);
John Kessenich5de15a22019-12-26 10:56:54 -07008090 id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i));
t.jungb16bea82018-11-15 10:21:36 +01008091 }
John Kessenich2359bd02015-12-06 19:29:11 -07008092 } else {
John Kessenich55e7d112015-11-15 21:33:39 -07008093 switch (consumedOperands) {
John Kessenich140f3df2015-06-26 16:58:36 -06008094 case 0:
8095 // should all be handled by visitAggregate and createNoArgOperation
8096 assert(0);
8097 return 0;
8098 case 1:
8099 // should all be handled by createUnaryOperation
8100 assert(0);
8101 return 0;
8102 case 2:
8103 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
8104 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008105 default:
John Kessenich55e7d112015-11-15 21:33:39 -07008106 // anything 3 or over doesn't have l-value operands, so all should be consumed
8107 assert(consumedOperands == operands.size());
8108 id = builder.createOp(opCode, typeId, operands);
John Kessenich140f3df2015-06-26 16:58:36 -06008109 break;
8110 }
8111 }
8112
John Kessenichb9197c82019-08-11 07:41:45 -06008113#ifndef GLSLANG_WEB
John Kessenich55e7d112015-11-15 21:33:39 -07008114 // Decode the return types that were structures
8115 switch (op) {
8116 case glslang::EOpAddCarry:
8117 case glslang::EOpSubBorrow:
8118 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
8119 id = builder.createCompositeExtract(id, typeId0, 0);
8120 break;
8121 case glslang::EOpUMulExtended:
8122 case glslang::EOpIMulExtended:
8123 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
8124 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
8125 break;
8126 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08008127 {
8128 assert(operands.size() == 2);
8129 if (builder.isFloatType(builder.getScalarTypeId(typeId1))) {
8130 // "exp" is floating-point type (from HLSL intrinsic)
8131 spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1);
8132 member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1);
8133 builder.createStore(member1, operands[1]);
8134 } else
8135 // "exp" is integer type (from GLSL built-in function)
8136 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
8137 id = builder.createCompositeExtract(id, typeId0, 0);
8138 }
John Kessenich55e7d112015-11-15 21:33:39 -07008139 break;
8140 default:
8141 break;
8142 }
John Kessenichb9197c82019-08-11 07:41:45 -06008143#endif
John Kessenich55e7d112015-11-15 21:33:39 -07008144
John Kessenich32cfd492016-02-02 12:37:46 -07008145 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06008146}
8147
Rex Xu9d93a232016-05-05 12:30:44 +08008148// Intrinsics with no arguments (or no return value, and no precision).
8149spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
John Kessenich140f3df2015-06-26 16:58:36 -06008150{
Jeff Bolz36831c92018-09-05 10:11:41 -05008151 // GLSL memory barriers use queuefamily scope in new model, device scope in old model
John Kessenich8985fc92020-03-03 10:25:07 -07008152 spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ?
8153 spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
John Kessenich140f3df2015-06-26 16:58:36 -06008154
8155 switch (op) {
John Kessenich140f3df2015-06-26 16:58:36 -06008156 case glslang::EOpBarrier:
John Kessenich82979362017-12-11 04:02:24 -07008157 if (glslangIntermediate->getStage() == EShLangTessControl) {
Jeff Bolz36831c92018-09-05 10:11:41 -05008158 if (glslangIntermediate->usingVulkanMemoryModel()) {
8159 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
8160 spv::MemorySemanticsOutputMemoryKHRMask |
8161 spv::MemorySemanticsAcquireReleaseMask);
8162 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
8163 } else {
8164 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone);
8165 }
John Kessenich82979362017-12-11 04:02:24 -07008166 } else {
8167 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
8168 spv::MemorySemanticsWorkgroupMemoryMask |
8169 spv::MemorySemanticsAcquireReleaseMask);
8170 }
John Kessenich140f3df2015-06-26 16:58:36 -06008171 return 0;
8172 case glslang::EOpMemoryBarrier:
Jeff Bolz36831c92018-09-05 10:11:41 -05008173 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory |
8174 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06008175 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06008176 case glslang::EOpMemoryBarrierBuffer:
Jeff Bolz36831c92018-09-05 10:11:41 -05008177 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask |
8178 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06008179 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06008180 case glslang::EOpMemoryBarrierShared:
Jeff Bolz36831c92018-09-05 10:11:41 -05008181 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask |
8182 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06008183 return 0;
8184 case glslang::EOpGroupMemoryBarrier:
John Kessenich82979362017-12-11 04:02:24 -07008185 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory |
8186 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06008187 return 0;
John Kessenich3dd1ce52019-10-17 07:08:40 -06008188#ifndef GLSLANG_WEB
8189 case glslang::EOpMemoryBarrierAtomicCounter:
8190 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask |
8191 spv::MemorySemanticsAcquireReleaseMask);
8192 return 0;
8193 case glslang::EOpMemoryBarrierImage:
8194 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask |
8195 spv::MemorySemanticsAcquireReleaseMask);
8196 return 0;
LoopDawg6e72fdd2016-06-15 09:50:24 -06008197 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07008198 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice,
John Kessenich82979362017-12-11 04:02:24 -07008199 spv::MemorySemanticsAllMemory |
John Kessenich838d7af2017-12-12 22:50:53 -07008200 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008201 return 0;
John Kessenich838d7af2017-12-12 22:50:53 -07008202 case glslang::EOpDeviceMemoryBarrier:
8203 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
8204 spv::MemorySemanticsImageMemoryMask |
8205 spv::MemorySemanticsAcquireReleaseMask);
8206 return 0;
8207 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
8208 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
8209 spv::MemorySemanticsImageMemoryMask |
8210 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008211 return 0;
8212 case glslang::EOpWorkgroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07008213 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask |
8214 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008215 return 0;
8216 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07008217 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
8218 spv::MemorySemanticsWorkgroupMemoryMask |
8219 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008220 return 0;
John Kessenich66011cb2018-03-06 16:12:04 -07008221 case glslang::EOpSubgroupBarrier:
8222 builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
8223 spv::MemorySemanticsAcquireReleaseMask);
8224 return spv::NoResult;
8225 case glslang::EOpSubgroupMemoryBarrier:
8226 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
8227 spv::MemorySemanticsAcquireReleaseMask);
8228 return spv::NoResult;
8229 case glslang::EOpSubgroupMemoryBarrierBuffer:
8230 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask |
8231 spv::MemorySemanticsAcquireReleaseMask);
8232 return spv::NoResult;
8233 case glslang::EOpSubgroupMemoryBarrierImage:
8234 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask |
8235 spv::MemorySemanticsAcquireReleaseMask);
8236 return spv::NoResult;
8237 case glslang::EOpSubgroupMemoryBarrierShared:
8238 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask |
8239 spv::MemorySemanticsAcquireReleaseMask);
8240 return spv::NoResult;
John Kessenichf8d1d742019-10-21 06:55:11 -06008241
8242 case glslang::EOpEmitVertex:
8243 builder.createNoResultOp(spv::OpEmitVertex);
8244 return 0;
8245 case glslang::EOpEndPrimitive:
8246 builder.createNoResultOp(spv::OpEndPrimitive);
8247 return 0;
8248
John Kessenich66011cb2018-03-06 16:12:04 -07008249 case glslang::EOpSubgroupElect: {
8250 std::vector<spv::Id> operands;
8251 return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid);
8252 }
Rex Xu9d93a232016-05-05 12:30:44 +08008253 case glslang::EOpTime:
8254 {
8255 std::vector<spv::Id> args; // Dummy arguments
8256 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
8257 return builder.setPrecision(id, precision);
8258 }
Daniel Kochffccefd2020-11-23 15:41:27 -05008259 case glslang::EOpIgnoreIntersectionNV:
8260 builder.createNoResultOp(spv::OpIgnoreIntersectionNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07008261 return 0;
Daniel Kochffccefd2020-11-23 15:41:27 -05008262 case glslang::EOpTerminateRayNV:
8263 builder.createNoResultOp(spv::OpTerminateRayNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07008264 return 0;
Torosdagli06c2eee2020-03-19 11:09:57 -04008265 case glslang::EOpRayQueryInitialize:
8266 builder.createNoResultOp(spv::OpRayQueryInitializeKHR);
8267 return 0;
8268 case glslang::EOpRayQueryTerminate:
8269 builder.createNoResultOp(spv::OpRayQueryTerminateKHR);
8270 return 0;
8271 case glslang::EOpRayQueryGenerateIntersection:
8272 builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR);
8273 return 0;
8274 case glslang::EOpRayQueryConfirmIntersection:
8275 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR);
8276 return 0;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05008277 case glslang::EOpBeginInvocationInterlock:
8278 builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT);
8279 return 0;
8280 case glslang::EOpEndInvocationInterlock:
8281 builder.createNoResultOp(spv::OpEndInvocationInterlockEXT);
8282 return 0;
8283
Jeff Bolzba6170b2019-07-01 09:23:23 -05008284 case glslang::EOpIsHelperInvocation:
8285 {
8286 std::vector<spv::Id> args; // Dummy arguments
Rex Xubb7307b2019-07-15 14:57:20 +08008287 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
8288 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
8289 return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args);
Jeff Bolzba6170b2019-07-01 09:23:23 -05008290 }
8291
amhagan91fb0092019-07-10 21:14:38 -04008292 case glslang::EOpReadClockSubgroupKHR: {
8293 std::vector<spv::Id> args;
8294 args.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
8295 builder.addExtension(spv::E_SPV_KHR_shader_clock);
8296 builder.addCapability(spv::CapabilityShaderClockKHR);
8297 return builder.createOp(spv::OpReadClockKHR, typeId, args);
8298 }
8299
8300 case glslang::EOpReadClockDeviceKHR: {
8301 std::vector<spv::Id> args;
8302 args.push_back(builder.makeUintConstant(spv::ScopeDevice));
8303 builder.addExtension(spv::E_SPV_KHR_shader_clock);
8304 builder.addCapability(spv::CapabilityShaderClockKHR);
8305 return builder.createOp(spv::OpReadClockKHR, typeId, args);
8306 }
John Kessenich3dd1ce52019-10-17 07:08:40 -06008307#endif
John Kessenich140f3df2015-06-26 16:58:36 -06008308 default:
John Kessenich155d3512019-08-08 23:29:20 -06008309 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008310 }
John Kessenich155d3512019-08-08 23:29:20 -06008311
8312 logger->missingFunctionality("unknown operation with no arguments");
8313
8314 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06008315}
8316
8317spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
8318{
John Kessenich2f273362015-07-18 22:34:27 -06008319 auto iter = symbolValues.find(symbol->getId());
John Kessenich140f3df2015-06-26 16:58:36 -06008320 spv::Id id;
8321 if (symbolValues.end() != iter) {
8322 id = iter->second;
8323 return id;
8324 }
8325
8326 // it was not found, create it
John Kessenich9c14f772019-06-17 08:38:35 -06008327 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
Daniel Kochdb32b242020-03-17 20:42:47 -04008328 auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType());
John Kessenich9c14f772019-06-17 08:38:35 -06008329 id = createSpvVariable(symbol, forcedType.first);
John Kessenich140f3df2015-06-26 16:58:36 -06008330 symbolValues[symbol->getId()] = id;
John Kessenich9c14f772019-06-17 08:38:35 -06008331 if (forcedType.second != spv::NoType)
8332 forceType[id] = forcedType.second;
John Kessenich140f3df2015-06-26 16:58:36 -06008333
Rex Xuc884b4a2016-06-29 15:03:44 +08008334 if (symbol->getBasicType() != glslang::EbtBlock) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008335 builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
8336 builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
8337 builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
John Kessenicha28f7a72019-08-06 07:00:58 -06008338#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07008339 addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier());
John Kessenichb9197c82019-08-11 07:41:45 -06008340 if (symbol->getQualifier().hasComponent())
8341 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
8342 if (symbol->getQualifier().hasIndex())
8343 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
Chao Chen3c366992018-09-19 11:41:59 -07008344#endif
John Kessenich6c292d32016-02-15 20:58:50 -07008345 if (symbol->getType().getQualifier().hasSpecConstantId())
John Kessenich5d610ee2018-03-07 18:05:55 -07008346 builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
John Kessenich91e4aa52016-07-07 17:46:42 -06008347 // atomic counters use this:
8348 if (symbol->getQualifier().hasOffset())
8349 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06008350 }
8351
scygan2c864272016-05-18 18:09:17 +02008352 if (symbol->getQualifier().hasLocation())
8353 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
John Kessenich5d610ee2018-03-07 18:05:55 -07008354 builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
John Kessenichf2d8a5c2016-03-03 22:29:11 -07008355 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
John Kessenich92187592016-02-01 13:45:25 -07008356 builder.addCapability(spv::CapabilityGeometryStreams);
John Kessenich140f3df2015-06-26 16:58:36 -06008357 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
John Kessenich92187592016-02-01 13:45:25 -07008358 }
John Kessenich140f3df2015-06-26 16:58:36 -06008359 if (symbol->getQualifier().hasSet())
8360 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
John Kessenich6c292d32016-02-15 20:58:50 -07008361 else if (IsDescriptorResource(symbol->getType())) {
8362 // default to 0
8363 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
8364 }
John Kessenich140f3df2015-06-26 16:58:36 -06008365 if (symbol->getQualifier().hasBinding())
8366 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
Jeff Bolz0a93cfb2018-12-11 20:53:59 -06008367 else if (IsDescriptorResource(symbol->getType())) {
8368 // default to 0
8369 builder.addDecoration(id, spv::DecorationBinding, 0);
8370 }
John Kessenich6c292d32016-02-15 20:58:50 -07008371 if (symbol->getQualifier().hasAttachment())
8372 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
John Kessenich140f3df2015-06-26 16:58:36 -06008373 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07008374 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenichedaf5562017-12-15 06:21:46 -07008375 if (symbol->getQualifier().hasXfbBuffer()) {
John Kessenich140f3df2015-06-26 16:58:36 -06008376 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
John Kessenichedaf5562017-12-15 06:21:46 -07008377 unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer);
8378 if (stride != glslang::TQualifier::layoutXfbStrideEnd)
8379 builder.addDecoration(id, spv::DecorationXfbStride, stride);
8380 }
8381 if (symbol->getQualifier().hasXfbOffset())
8382 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06008383 }
8384
John Kessenichb9197c82019-08-11 07:41:45 -06008385 // add built-in variable decoration
8386 if (builtIn != spv::BuiltInMax) {
8387 builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
8388 }
8389
8390#ifndef GLSLANG_WEB
Daniel Kochffccefd2020-11-23 15:41:27 -05008391 // Subgroup builtins which have input storage class are volatile for ray tracing stages.
8392 if (symbol->getType().isImage() || symbol->getQualifier().isPipeInput()) {
Rex Xu1da878f2016-02-21 20:59:01 +08008393 std::vector<spv::Decoration> memory;
John Kessenich8985fc92020-03-03 10:25:07 -07008394 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory,
8395 glslangIntermediate->usingVulkanMemoryModel());
Rex Xu1da878f2016-02-21 20:59:01 +08008396 for (unsigned int i = 0; i < memory.size(); ++i)
John Kessenich5d610ee2018-03-07 18:05:55 -07008397 builder.addDecoration(id, memory[i]);
Rex Xu1da878f2016-02-21 20:59:01 +08008398 }
8399
chaoc0ad6a4e2016-12-19 16:29:34 -08008400 if (builtIn == spv::BuiltInSampleMask) {
8401 spv::Decoration decoration;
8402 // GL_NV_sample_mask_override_coverage extension
8403 if (glslangIntermediate->getLayoutOverrideCoverage())
chaoc771d89f2017-01-13 01:10:53 -08008404 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
chaoc0ad6a4e2016-12-19 16:29:34 -08008405 else
8406 decoration = (spv::Decoration)spv::DecorationMax;
John Kessenich5d610ee2018-03-07 18:05:55 -07008407 builder.addDecoration(id, decoration);
chaoc0ad6a4e2016-12-19 16:29:34 -08008408 if (decoration != spv::DecorationMax) {
Jason Macnakdbd4c3c2019-07-12 14:33:02 -07008409 builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV);
chaoc0ad6a4e2016-12-19 16:29:34 -08008410 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
8411 }
8412 }
chaoc771d89f2017-01-13 01:10:53 -08008413 else if (builtIn == spv::BuiltInLayer) {
8414 // SPV_NV_viewport_array2 extension
John Kessenichb41bff62017-08-11 13:07:17 -06008415 if (symbol->getQualifier().layoutViewportRelative) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008416 builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
chaoc771d89f2017-01-13 01:10:53 -08008417 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
8418 builder.addExtension(spv::E_SPV_NV_viewport_array2);
8419 }
John Kessenichb41bff62017-08-11 13:07:17 -06008420 if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008421 builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
8422 symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
chaoc771d89f2017-01-13 01:10:53 -08008423 builder.addCapability(spv::CapabilityShaderStereoViewNV);
8424 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
8425 }
8426 }
8427
chaoc6e5acae2016-12-20 13:28:52 -08008428 if (symbol->getQualifier().layoutPassthrough) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008429 builder.addDecoration(id, spv::DecorationPassthroughNV);
chaoc771d89f2017-01-13 01:10:53 -08008430 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
chaoc6e5acae2016-12-20 13:28:52 -08008431 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
8432 }
Chao Chen9eada4b2018-09-19 11:39:56 -07008433 if (symbol->getQualifier().pervertexNV) {
8434 builder.addDecoration(id, spv::DecorationPerVertexNV);
8435 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
8436 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
8437 }
chaoc0ad6a4e2016-12-19 16:29:34 -08008438
John Kessenich5d610ee2018-03-07 18:05:55 -07008439 if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) {
8440 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
8441 builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
8442 symbol->getType().getQualifier().semanticName);
8443 }
8444
John Kessenich7015bd62019-08-01 03:28:08 -06008445 if (symbol->isReference()) {
John Kessenich8985fc92020-03-03 10:25:07 -07008446 builder.addDecoration(id, symbol->getType().getQualifier().restrict ?
8447 spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008448 }
John Kessenichb9197c82019-08-11 07:41:45 -06008449#endif
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008450
John Kessenich140f3df2015-06-26 16:58:36 -06008451 return id;
8452}
8453
John Kessenicha28f7a72019-08-06 07:00:58 -06008454#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07008455// add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object
8456void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
8457{
8458 if (member >= 0) {
Sahil Parmar38772c02018-10-25 23:50:59 -07008459 if (qualifier.perPrimitiveNV) {
8460 // Need to add capability/extension for fragment shader.
8461 // Mesh shader already adds this by default.
8462 if (glslangIntermediate->getStage() == EShLangFragment) {
8463 builder.addCapability(spv::CapabilityMeshShadingNV);
8464 builder.addExtension(spv::E_SPV_NV_mesh_shader);
8465 }
Chao Chen3c366992018-09-19 11:41:59 -07008466 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07008467 }
Chao Chen3c366992018-09-19 11:41:59 -07008468 if (qualifier.perViewNV)
8469 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV);
8470 if (qualifier.perTaskNV)
8471 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV);
8472 } else {
Sahil Parmar38772c02018-10-25 23:50:59 -07008473 if (qualifier.perPrimitiveNV) {
8474 // Need to add capability/extension for fragment shader.
8475 // Mesh shader already adds this by default.
8476 if (glslangIntermediate->getStage() == EShLangFragment) {
8477 builder.addCapability(spv::CapabilityMeshShadingNV);
8478 builder.addExtension(spv::E_SPV_NV_mesh_shader);
8479 }
Chao Chen3c366992018-09-19 11:41:59 -07008480 builder.addDecoration(id, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07008481 }
Chao Chen3c366992018-09-19 11:41:59 -07008482 if (qualifier.perViewNV)
8483 builder.addDecoration(id, spv::DecorationPerViewNV);
8484 if (qualifier.perTaskNV)
8485 builder.addDecoration(id, spv::DecorationPerTaskNV);
8486 }
8487}
8488#endif
8489
John Kessenich55e7d112015-11-15 21:33:39 -07008490// Make a full tree of instructions to build a SPIR-V specialization constant,
John Kessenich6c292d32016-02-15 20:58:50 -07008491// or regular constant if possible.
John Kessenich55e7d112015-11-15 21:33:39 -07008492//
8493// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
8494//
8495// Recursively walk the nodes. The nodes form a tree whose leaves are
8496// regular constants, which themselves are trees that createSpvConstant()
8497// recursively walks. So, this function walks the "top" of the tree:
8498// - emit specialization constant-building instructions for specConstant
8499// - when running into a non-spec-constant, switch to createSpvConstant()
qining08408382016-03-21 09:51:37 -04008500spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
John Kessenich55e7d112015-11-15 21:33:39 -07008501{
John Kessenich7cc0e282016-03-20 00:46:02 -06008502 assert(node.getQualifier().isConstant());
John Kessenich55e7d112015-11-15 21:33:39 -07008503
qining4f4bb812016-04-03 23:55:17 -04008504 // Handle front-end constants first (non-specialization constants).
John Kessenich6c292d32016-02-15 20:58:50 -07008505 if (! node.getQualifier().specConstant) {
8506 // hand off to the non-spec-constant path
8507 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
8508 int nextConst = 0;
John Kessenich8985fc92020-03-03 10:25:07 -07008509 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ?
8510 node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
8511 nextConst, false);
John Kessenich6c292d32016-02-15 20:58:50 -07008512 }
8513
8514 // We now know we have a specialization constant to build
8515
Ricardo Garcia232ba0d2020-06-03 15:52:55 +02008516 // Extra capabilities may be needed.
8517 if (node.getType().contains8BitInt())
8518 builder.addCapability(spv::CapabilityInt8);
8519 if (node.getType().contains16BitFloat())
8520 builder.addCapability(spv::CapabilityFloat16);
8521 if (node.getType().contains16BitInt())
8522 builder.addCapability(spv::CapabilityInt16);
8523 if (node.getType().contains64BitInt())
8524 builder.addCapability(spv::CapabilityInt64);
8525 if (node.getType().containsDouble())
8526 builder.addCapability(spv::CapabilityFloat64);
8527
John Kessenichd94c0032016-05-30 19:29:40 -06008528 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
qining4f4bb812016-04-03 23:55:17 -04008529 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
8530 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
8531 std::vector<spv::Id> dimConstId;
8532 for (int dim = 0; dim < 3; ++dim) {
8533 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
8534 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
John Kessenich5d610ee2018-03-07 18:05:55 -07008535 if (specConst) {
8536 builder.addDecoration(dimConstId.back(), spv::DecorationSpecId,
8537 glslangIntermediate->getLocalSizeSpecId(dim));
8538 }
qining4f4bb812016-04-03 23:55:17 -04008539 }
8540 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
8541 }
8542
8543 // An AST node labelled as specialization constant should be a symbol node.
8544 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
8545 if (auto* sn = node.getAsSymbolNode()) {
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008546 spv::Id result;
qining4f4bb812016-04-03 23:55:17 -04008547 if (auto* sub_tree = sn->getConstSubtree()) {
qining27e04a02016-04-14 16:40:20 -04008548 // Traverse the constant constructor sub tree like generating normal run-time instructions.
8549 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
8550 // will set the builder into spec constant op instruction generating mode.
8551 sub_tree->traverse(this);
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008552 result = accessChainLoad(sub_tree->getType());
8553 } else if (auto* const_union_array = &sn->getConstArray()) {
qining4f4bb812016-04-03 23:55:17 -04008554 int nextConst = 0;
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008555 result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
Dan Sinclair70661b92018-11-12 13:56:52 -05008556 } else {
8557 logger->missingFunctionality("Invalid initializer for spec onstant.");
Dan Sinclair70661b92018-11-12 13:56:52 -05008558 return spv::NoResult;
John Kessenich6c292d32016-02-15 20:58:50 -07008559 }
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008560 builder.addName(result, sn->getName().c_str());
8561 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07008562 }
qining4f4bb812016-04-03 23:55:17 -04008563
8564 // Neither a front-end constant node, nor a specialization constant node with constant union array or
8565 // constant sub tree as initializer.
Lei Zhang17535f72016-05-04 15:55:59 -04008566 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
qining4f4bb812016-04-03 23:55:17 -04008567 return spv::NoResult;
John Kessenich55e7d112015-11-15 21:33:39 -07008568}
8569
John Kessenich140f3df2015-06-26 16:58:36 -06008570// Use 'consts' as the flattened glslang source of scalar constants to recursively
8571// build the aggregate SPIR-V constant.
8572//
8573// If there are not enough elements present in 'consts', 0 will be substituted;
8574// an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
8575//
John Kessenich8985fc92020-03-03 10:25:07 -07008576spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType,
8577 const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
John Kessenich140f3df2015-06-26 16:58:36 -06008578{
8579 // vector of constants for SPIR-V
8580 std::vector<spv::Id> spvConsts;
8581
8582 // Type is used for struct and array constants
8583 spv::Id typeId = convertGlslangToSpvType(glslangType);
8584
8585 if (glslangType.isArray()) {
John Kessenich65c78a02015-08-10 17:08:55 -06008586 glslang::TType elementType(glslangType, 0);
8587 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
qining08408382016-03-21 09:51:37 -04008588 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06008589 } else if (glslangType.isMatrix()) {
John Kessenich65c78a02015-08-10 17:08:55 -06008590 glslang::TType vectorType(glslangType, 0);
John Kessenich140f3df2015-06-26 16:58:36 -06008591 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
qining08408382016-03-21 09:51:37 -04008592 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
Jeff Bolz4605e2e2019-02-19 13:10:32 -06008593 } else if (glslangType.isCoopMat()) {
8594 glslang::TType componentType(glslangType.getBasicType());
8595 spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008596 } else if (glslangType.isStruct()) {
John Kessenich140f3df2015-06-26 16:58:36 -06008597 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
8598 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
qining08408382016-03-21 09:51:37 -04008599 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
John Kessenich8d72f1a2016-05-20 12:06:03 -06008600 } else if (glslangType.getVectorSize() > 1) {
John Kessenich140f3df2015-06-26 16:58:36 -06008601 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
8602 bool zero = nextConst >= consts.size();
8603 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06008604 case glslang::EbtInt:
8605 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
8606 break;
8607 case glslang::EbtUint:
8608 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
8609 break;
8610 case glslang::EbtFloat:
8611 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
8612 break;
8613 case glslang::EbtBool:
8614 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
8615 break;
8616#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07008617 case glslang::EbtInt8:
8618 spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const()));
8619 break;
8620 case glslang::EbtUint8:
8621 spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const()));
8622 break;
8623 case glslang::EbtInt16:
8624 spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const()));
8625 break;
8626 case glslang::EbtUint16:
8627 spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const()));
8628 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08008629 case glslang::EbtInt64:
8630 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
8631 break;
8632 case glslang::EbtUint64:
8633 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
8634 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008635 case glslang::EbtDouble:
8636 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
8637 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08008638 case glslang::EbtFloat16:
8639 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
8640 break;
John Kessenich39697cd2019-08-08 10:35:51 -06008641#endif
John Kessenich140f3df2015-06-26 16:58:36 -06008642 default:
John Kessenich55e7d112015-11-15 21:33:39 -07008643 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06008644 break;
8645 }
8646 ++nextConst;
8647 }
8648 } else {
8649 // we have a non-aggregate (scalar) constant
8650 bool zero = nextConst >= consts.size();
8651 spv::Id scalar = 0;
8652 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06008653 case glslang::EbtInt:
8654 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
8655 break;
8656 case glslang::EbtUint:
8657 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
8658 break;
8659 case glslang::EbtFloat:
8660 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
8661 break;
8662 case glslang::EbtBool:
8663 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
8664 break;
8665#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07008666 case glslang::EbtInt8:
8667 scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant);
8668 break;
8669 case glslang::EbtUint8:
8670 scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant);
8671 break;
8672 case glslang::EbtInt16:
8673 scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant);
8674 break;
8675 case glslang::EbtUint16:
8676 scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant);
8677 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08008678 case glslang::EbtInt64:
8679 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
8680 break;
8681 case glslang::EbtUint64:
8682 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
8683 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008684 case glslang::EbtDouble:
John Kessenich55e7d112015-11-15 21:33:39 -07008685 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06008686 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08008687 case glslang::EbtFloat16:
8688 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
8689 break;
Jeff Bolz3fd12322019-03-05 23:27:09 -06008690 case glslang::EbtReference:
8691 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
8692 scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar);
8693 break;
John Kessenich39697cd2019-08-08 10:35:51 -06008694#endif
Jeff Bolz04d73732019-05-31 13:06:01 -05008695 case glslang::EbtString:
8696 scalar = builder.getStringId(consts[nextConst].getSConst()->c_str());
8697 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008698 default:
John Kessenich55e7d112015-11-15 21:33:39 -07008699 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06008700 break;
8701 }
8702 ++nextConst;
8703 return scalar;
8704 }
8705
8706 return builder.makeCompositeConstant(typeId, spvConsts);
8707}
8708
John Kessenich7c1aa102015-10-15 13:29:11 -06008709// Return true if the node is a constant or symbol whose reading has no
8710// non-trivial observable cost or effect.
8711bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
8712{
8713 // don't know what this is
8714 if (node == nullptr)
8715 return false;
8716
8717 // a constant is safe
8718 if (node->getAsConstantUnion() != nullptr)
8719 return true;
8720
8721 // not a symbol means non-trivial
8722 if (node->getAsSymbolNode() == nullptr)
8723 return false;
8724
8725 // a symbol, depends on what's being read
8726 switch (node->getType().getQualifier().storage) {
8727 case glslang::EvqTemporary:
8728 case glslang::EvqGlobal:
8729 case glslang::EvqIn:
8730 case glslang::EvqInOut:
8731 case glslang::EvqConst:
8732 case glslang::EvqConstReadOnly:
8733 case glslang::EvqUniform:
8734 return true;
8735 default:
8736 return false;
8737 }
qining25262b32016-05-06 17:25:16 -04008738}
John Kessenich7c1aa102015-10-15 13:29:11 -06008739
8740// A node is trivial if it is a single operation with no side effects.
John Kessenich84cc15f2017-05-24 16:44:47 -06008741// HLSL (and/or vectors) are always trivial, as it does not short circuit.
John Kessenich0d2b4712017-05-19 20:19:00 -06008742// Otherwise, error on the side of saying non-trivial.
John Kessenich7c1aa102015-10-15 13:29:11 -06008743// Return true if trivial.
8744bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
8745{
8746 if (node == nullptr)
8747 return false;
8748
John Kessenich84cc15f2017-05-24 16:44:47 -06008749 // count non scalars as trivial, as well as anything coming from HLSL
8750 if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich0d2b4712017-05-19 20:19:00 -06008751 return true;
8752
John Kessenich7c1aa102015-10-15 13:29:11 -06008753 // symbols and constants are trivial
8754 if (isTrivialLeaf(node))
8755 return true;
8756
8757 // otherwise, it needs to be a simple operation or one or two leaf nodes
8758
8759 // not a simple operation
8760 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
8761 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
8762 if (binaryNode == nullptr && unaryNode == nullptr)
8763 return false;
8764
8765 // not on leaf nodes
8766 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
8767 return false;
8768
8769 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
8770 return false;
8771 }
8772
8773 switch (node->getAsOperator()->getOp()) {
8774 case glslang::EOpLogicalNot:
8775 case glslang::EOpConvIntToBool:
8776 case glslang::EOpConvUintToBool:
8777 case glslang::EOpConvFloatToBool:
8778 case glslang::EOpConvDoubleToBool:
8779 case glslang::EOpEqual:
8780 case glslang::EOpNotEqual:
8781 case glslang::EOpLessThan:
8782 case glslang::EOpGreaterThan:
8783 case glslang::EOpLessThanEqual:
8784 case glslang::EOpGreaterThanEqual:
8785 case glslang::EOpIndexDirect:
8786 case glslang::EOpIndexDirectStruct:
8787 case glslang::EOpLogicalXor:
8788 case glslang::EOpAny:
8789 case glslang::EOpAll:
8790 return true;
8791 default:
8792 return false;
8793 }
8794}
8795
8796// Emit short-circuiting code, where 'right' is never evaluated unless
8797// the left side is true (for &&) or false (for ||).
John Kessenich8985fc92020-03-03 10:25:07 -07008798spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left,
8799 glslang::TIntermTyped& right)
John Kessenich7c1aa102015-10-15 13:29:11 -06008800{
8801 spv::Id boolTypeId = builder.makeBoolType();
8802
8803 // emit left operand
8804 builder.clearAccessChain();
8805 left.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008806 spv::Id leftId = accessChainLoad(left.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008807
8808 // Operands to accumulate OpPhi operands
8809 std::vector<spv::Id> phiOperands;
8810 // accumulate left operand's phi information
8811 phiOperands.push_back(leftId);
8812 phiOperands.push_back(builder.getBuildPoint()->getId());
8813
8814 // Make the two kinds of operation symmetric with a "!"
8815 // || => emit "if (! left) result = right"
8816 // && => emit "if ( left) result = right"
8817 //
8818 // TODO: this runtime "not" for || could be avoided by adding functionality
8819 // to 'builder' to have an "else" without an "then"
8820 if (op == glslang::EOpLogicalOr)
8821 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
8822
8823 // make an "if" based on the left value
Rex Xu57e65922017-07-04 23:23:40 +08008824 spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder);
John Kessenich7c1aa102015-10-15 13:29:11 -06008825
8826 // emit right operand as the "then" part of the "if"
8827 builder.clearAccessChain();
8828 right.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008829 spv::Id rightId = accessChainLoad(right.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008830
8831 // accumulate left operand's phi information
8832 phiOperands.push_back(rightId);
8833 phiOperands.push_back(builder.getBuildPoint()->getId());
8834
8835 // finish the "if"
8836 ifBuilder.makeEndIf();
8837
8838 // phi together the two results
8839 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
8840}
8841
John Kessenicha28f7a72019-08-06 07:00:58 -06008842#ifndef GLSLANG_WEB
Rex Xu9d93a232016-05-05 12:30:44 +08008843// Return type Id of the imported set of extended instructions corresponds to the name.
8844// Import this set if it has not been imported yet.
8845spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
8846{
8847 if (extBuiltinMap.find(name) != extBuiltinMap.end())
8848 return extBuiltinMap[name];
8849 else {
Rex Xu51596642016-09-21 18:56:12 +08008850 builder.addExtension(name);
Rex Xu9d93a232016-05-05 12:30:44 +08008851 spv::Id extBuiltins = builder.import(name);
8852 extBuiltinMap[name] = extBuiltins;
8853 return extBuiltins;
8854 }
8855}
Frank Henigman541f7bb2018-01-16 00:18:26 -05008856#endif
Rex Xu9d93a232016-05-05 12:30:44 +08008857
John Kessenich140f3df2015-06-26 16:58:36 -06008858}; // end anonymous namespace
8859
8860namespace glslang {
8861
John Kessenich68d78fd2015-07-12 19:28:10 -06008862void GetSpirvVersion(std::string& version)
8863{
John Kessenich9e55f632015-07-15 10:03:39 -06008864 const int bufSize = 100;
John Kessenichf98ee232015-07-12 19:39:51 -06008865 char buf[bufSize];
John Kessenich55e7d112015-11-15 21:33:39 -07008866 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
John Kessenich68d78fd2015-07-12 19:28:10 -06008867 version = buf;
8868}
8869
John Kessenicha372a3e2017-11-02 22:32:14 -06008870// For low-order part of the generator's magic number. Bump up
8871// when there is a change in the style (e.g., if SSA form changes,
8872// or a different instruction sequence to do something gets used).
8873int GetSpirvGeneratorVersion()
8874{
John Kessenich3f0d4bc2017-12-16 23:46:37 -07008875 // return 1; // start
8876 // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V
John Kessenich71b5da62018-02-06 08:06:36 -07008877 // return 3; // change/correct barrier-instruction operands, to match memory model group decisions
John Kessenich0216f242018-03-03 11:47:07 -07008878 // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component
John Kessenichac370792018-03-07 11:24:50 -07008879 // return 5; // make OpArrayLength result type be an int with signedness of 0
John Kessenichd6c97552018-06-04 15:33:31 -06008880 // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code,
8881 // versions 4 and 6 each generate OpArrayLength as it has long been done
John Kessenich31c33702019-11-02 21:26:40 -06008882 // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent
John Kessenich3641ff72020-06-10 07:38:31 -06008883 // return 8; // switch to new dead block eliminator; use OpUnreachable
Graeme Leese060882f2020-06-22 11:03:46 +01008884 // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list
8885 return 10; // Generate OpFUnordNotEqual for != comparisons
John Kessenicha372a3e2017-11-02 22:32:14 -06008886}
8887
John Kessenich140f3df2015-06-26 16:58:36 -06008888// Write SPIR-V out to a binary file
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008889void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
John Kessenich140f3df2015-06-26 16:58:36 -06008890{
8891 std::ofstream out;
John Kessenich68d78fd2015-07-12 19:28:10 -06008892 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008893 if (out.fail())
8894 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenich140f3df2015-06-26 16:58:36 -06008895 for (int i = 0; i < (int)spirv.size(); ++i) {
8896 unsigned int word = spirv[i];
8897 out.write((const char*)&word, 4);
8898 }
8899 out.close();
8900}
8901
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008902// Write SPIR-V out to a text file with 32-bit hexadecimal words
Flavioaea3c892017-02-06 11:46:35 -08008903void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008904{
Shahbaz Youssefi1ef2e252020-07-03 15:42:53 -04008905#if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008906 std::ofstream out;
8907 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008908 if (out.fail())
8909 printf("ERROR: Failed to open file: %s\n", baseName);
Ben Claytonfbe9a232020-06-17 11:17:19 +01008910 out << "\t// " <<
8911 GetSpirvGeneratorVersion() <<
8912 GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH <<
8913 GLSLANG_VERSION_FLAVOR << std::endl;
Flavio15017db2017-02-15 14:29:33 -08008914 if (varName != nullptr) {
8915 out << "\t #pragma once" << std::endl;
8916 out << "const uint32_t " << varName << "[] = {" << std::endl;
8917 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008918 const int WORDS_PER_LINE = 8;
8919 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
8920 out << "\t";
8921 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
8922 const unsigned int word = spirv[i + j];
8923 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
8924 if (i + j + 1 < (int)spirv.size()) {
8925 out << ",";
8926 }
8927 }
8928 out << std::endl;
8929 }
Flavio15017db2017-02-15 14:29:33 -08008930 if (varName != nullptr) {
8931 out << "};";
johnkslangf881f082020-08-04 02:13:50 -06008932 out << std::endl;
Flavio15017db2017-02-15 14:29:33 -08008933 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008934 out.close();
John Kessenich155d3512019-08-08 23:29:20 -06008935#endif
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008936}
8937
John Kessenich140f3df2015-06-26 16:58:36 -06008938//
8939// Set up the glslang traversal
8940//
John Kessenich4e11b612018-08-30 16:56:59 -06008941void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
John Kessenich140f3df2015-06-26 16:58:36 -06008942{
Lei Zhang17535f72016-05-04 15:55:59 -04008943 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06008944 GlslangToSpv(intermediate, spirv, &logger, options);
Lei Zhang09caf122016-05-02 18:11:54 -04008945}
8946
John Kessenich4e11b612018-08-30 16:56:59 -06008947void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
John Kessenich121853f2017-05-31 17:11:16 -06008948 spv::SpvBuildLogger* logger, SpvOptions* options)
Lei Zhang09caf122016-05-02 18:11:54 -04008949{
John Kessenich140f3df2015-06-26 16:58:36 -06008950 TIntermNode* root = intermediate.getTreeRoot();
8951
8952 if (root == 0)
8953 return;
8954
John Kessenich4e11b612018-08-30 16:56:59 -06008955 SpvOptions defaultOptions;
John Kessenich121853f2017-05-31 17:11:16 -06008956 if (options == nullptr)
8957 options = &defaultOptions;
8958
John Kessenich4e11b612018-08-30 16:56:59 -06008959 GetThreadPoolAllocator().push();
John Kessenich140f3df2015-06-26 16:58:36 -06008960
John Kessenich2b5ea9f2018-01-31 18:35:56 -07008961 TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
John Kessenich140f3df2015-06-26 16:58:36 -06008962 root->traverse(&it);
John Kessenichfca82622016-11-26 13:23:20 -07008963 it.finishSpv();
John Kessenich140f3df2015-06-26 16:58:36 -06008964 it.dumpSpv(spirv);
8965
GregFfb03a552018-03-29 11:49:14 -06008966#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -06008967 // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
8968 // eg. forward and remove memory writes of opaque types.
Jeff Bolzfd556e32019-06-07 14:42:08 -05008969 bool prelegalization = intermediate.getSource() == EShSourceHlsl;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04008970 if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) {
8971 SpirvToolsTransform(intermediate, spirv, logger, options);
Jeff Bolzfd556e32019-06-07 14:42:08 -05008972 prelegalization = false;
8973 }
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04008974 else if (options->stripDebugInfo) {
8975 // Strip debug info even if optimization is disabled.
8976 SpirvToolsStripDebugInfo(intermediate, spirv, logger);
8977 }
John Kessenich717c80a2018-08-23 15:17:10 -06008978
John Kessenich4e11b612018-08-30 16:56:59 -06008979 if (options->validate)
Jeff Bolzfd556e32019-06-07 14:42:08 -05008980 SpirvToolsValidate(intermediate, spirv, logger, prelegalization);
John Kessenich4e11b612018-08-30 16:56:59 -06008981
John Kessenich717c80a2018-08-23 15:17:10 -06008982 if (options->disassemble)
John Kessenich4e11b612018-08-30 16:56:59 -06008983 SpirvToolsDisassemble(std::cout, spirv);
John Kessenich717c80a2018-08-23 15:17:10 -06008984
GregFcd1f1692017-09-21 18:40:22 -06008985#endif
8986
John Kessenich4e11b612018-08-30 16:56:59 -06008987 GetThreadPoolAllocator().pop();
John Kessenich140f3df2015-06-26 16:58:36 -06008988}
8989
8990}; // end namespace glslang