blob: 5a39c1e3151d763d7fe4d5b73e71e316f46c56d1 [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);
Jeff Bolz36831c92018-09-05 10:11:41 -0500152 spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type);
153 spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
154 spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
155 spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
David Netoa901ffe2016-06-08 14:11:40 +0100156 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
John Kessenich5d0fa972016-02-15 11:57:00 -0700157 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
John Kesseniche18fd202018-01-30 11:01:39 -0700158 spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const;
159 spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const;
John Kessenich1f4d0462019-01-12 17:31:41 +0700160 spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const;
John Kessenicha5c5fb62017-05-05 05:09:58 -0600161 spv::StorageClass TranslateStorageClass(const glslang::TType&);
John Kessenich5611c6d2018-04-05 11:25:02 -0600162 void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType);
John Kessenich9c14f772019-06-17 08:38:35 -0600163 spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType);
John Kessenich140f3df2015-06-26 16:58:36 -0600164 spv::Id getSampledType(const glslang::TSampler&);
John Kessenich8c8505c2016-07-26 12:50:38 -0600165 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
166 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
167 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600168 spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false);
John Kessenichead86222018-03-28 18:01:20 -0600169 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&,
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600170 bool lastBufferBlockMember, bool forwardReferenceOnly = false);
John Kessenich0e737842017-03-24 18:38:16 -0600171 bool filterMember(const glslang::TType& member);
John Kessenich6090df02016-06-30 21:18:02 -0600172 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
173 glslang::TLayoutPacking, const glslang::TQualifier&);
174 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
175 const glslang::TQualifier&, spv::Id);
John Kessenich6c292d32016-02-15 20:58:50 -0700176 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
John Kessenich32cfd492016-02-02 12:37:46 -0700177 spv::Id accessChainLoad(const glslang::TType& type);
Rex Xu27253232016-02-23 17:51:09 +0800178 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
John Kessenich4bf71552016-09-02 11:20:21 -0600179 void multiTypeStore(const glslang::TType&, spv::Id rValue);
John Kessenichf85e8062015-12-19 13:57:10 -0700180 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
John Kessenich3ac051e2015-12-20 11:29:16 -0700181 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
182 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
John Kessenich5d610ee2018-03-07 18:05:55 -0700183 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset,
184 int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
David Netoa901ffe2016-06-08 14:11:40 +0100185 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
John Kessenich140f3df2015-06-26 16:58:36 -0600186
John Kessenich6fccb3c2016-09-19 16:01:41 -0600187 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
John Kessenichd3ed90b2018-05-04 11:43:03 -0600188 bool writableParam(glslang::TStorageQualifier) const;
John Kessenichd41993d2017-09-10 15:21:05 -0600189 bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam);
John Kessenich140f3df2015-06-26 16:58:36 -0600190 void makeFunctions(const glslang::TIntermSequence&);
191 void makeGlobalInitializers(const glslang::TIntermSequence&);
192 void visitFunctions(const glslang::TIntermSequence&);
193 void handleFunctionEntry(const glslang::TIntermAggregate* node);
John Kessenich8985fc92020-03-03 10:25:07 -0700194 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments,
195 spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -0600196 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
197 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600198 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
199
John Kessenichead86222018-03-28 18:01:20 -0600200 spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right,
201 glslang::TBasicType typeProxy, bool reduceComparison = true);
202 spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right);
203 spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand,
John Kessenich8985fc92020-03-03 10:25:07 -0700204 glslang::TBasicType typeProxy,
205 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -0600206 spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand,
207 glslang::TBasicType typeProxy);
208 spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand,
209 glslang::TBasicType typeProxy);
John Kessenichad7645f2018-06-04 19:11:25 -0600210 spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize);
John Kessenich140f3df2015-06-26 16:58:36 -0600211 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
John Kessenich8985fc92020-03-03 10:25:07 -0700212 spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId,
213 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy,
214 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
215 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands,
216 glslang::TBasicType typeProxy);
217 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
218 spv::Id typeId, std::vector<spv::Id>& operands);
219 spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands,
220 glslang::TBasicType typeProxy);
221 spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId,
222 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +0800223 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600224 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
Chao Chen3c366992018-09-19 11:41:59 -0700225 void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier);
qining08408382016-03-21 09:51:37 -0400226 spv::Id createSpvConstant(const glslang::TIntermTyped&);
John Kessenich8985fc92020-03-03 10:25:07 -0700227 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&,
228 int& nextConst, bool specConstant);
John Kessenich7c1aa102015-10-15 13:29:11 -0600229 bool isTrivialLeaf(const glslang::TIntermTyped* node);
230 bool isTrivial(const glslang::TIntermTyped* node);
231 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
Rex Xu9d93a232016-05-05 12:30:44 +0800232 spv::Id getExtBuiltins(const char* name);
Daniel Kochdb32b242020-03-17 20:42:47 -0400233 std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&);
John Kessenich9c14f772019-06-17 08:38:35 -0600234 spv::Id translateForcedType(spv::Id object);
Jeff Bolz53134492019-06-25 13:31:10 -0500235 spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents);
John Kessenich140f3df2015-06-26 16:58:36 -0600236
John Kessenich121853f2017-05-31 17:11:16 -0600237 glslang::SpvOptions& options;
John Kessenich140f3df2015-06-26 16:58:36 -0600238 spv::Function* shaderEntry;
John Kesseniched33e052016-10-06 12:59:51 -0600239 spv::Function* currentFunction;
John Kessenich55e7d112015-11-15 21:33:39 -0700240 spv::Instruction* entryPoint;
John Kessenich140f3df2015-06-26 16:58:36 -0600241 int sequenceDepth;
242
Lei Zhang17535f72016-05-04 15:55:59 -0400243 spv::SpvBuildLogger* logger;
Lei Zhang09caf122016-05-02 18:11:54 -0400244
John Kessenich140f3df2015-06-26 16:58:36 -0600245 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
246 spv::Builder builder;
John Kessenich517fe7a2016-11-26 13:31:47 -0700247 bool inEntryPoint;
248 bool entryPointTerminated;
John Kessenich8985fc92020-03-03 10:25:07 -0700249 bool linkageOnly; // true when visiting the set of objects in the AST present only for
250 // establishing interface, whether or not they were statically used
John Kessenich59420fd2015-12-21 11:45:34 -0700251 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
John Kessenich140f3df2015-06-26 16:58:36 -0600252 const glslang::TIntermediate* glslangIntermediate;
John Kessenich605afc72019-06-17 23:33:09 -0600253 bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp
John Kessenich140f3df2015-06-26 16:58:36 -0600254 spv::Id stdBuiltins;
Jeff Bolz04d73732019-05-31 13:06:01 -0500255 spv::Id nonSemanticDebugPrintf;
Rex Xu9d93a232016-05-05 12:30:44 +0800256 std::unordered_map<const char*, spv::Id> extBuiltinMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600257
John Kessenich2f273362015-07-18 22:34:27 -0600258 std::unordered_map<int, spv::Id> symbolValues;
John Kessenich8985fc92020-03-03 10:25:07 -0700259 std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues,
260 // rather than a pointer
John Kessenich2f273362015-07-18 22:34:27 -0600261 std::unordered_map<std::string, spv::Function*> functionMap;
John Kessenich3ac051e2015-12-20 11:29:16 -0700262 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
John Kessenich5d610ee2018-03-07 18:05:55 -0700263 // for mapping glslang block indices to spv indices (e.g., due to hidden members):
Roy05a5b532020-01-03 16:21:34 +0800264 std::unordered_map<int, std::vector<int>> memberRemapper;
265 // for mapping glslang symbol struct to symbol Id
266 std::unordered_map<const glslang::TTypeList*, int> glslangTypeToIdMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600267 std::stack<bool> breakForLoop; // false means break for switch
John Kessenich5d610ee2018-03-07 18:05:55 -0700268 std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator;
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600269 // Map pointee types for EbtReference to their forward pointers
270 std::map<const glslang::TType *, spv::Id> forwardPointers;
John Kessenich9c14f772019-06-17 08:38:35 -0600271 // Type forcing, for when SPIR-V wants a different type than the AST,
272 // requiring local translation to and from SPIR-V type on every access.
273 // Maps <builtin-variable-id -> AST-required-type-id>
274 std::unordered_map<spv::Id, spv::Id> forceType;
John Kessenich140f3df2015-06-26 16:58:36 -0600275};
276
277//
278// Helper functions for translating glslang representations to SPIR-V enumerants.
279//
280
281// Translate glslang profile to SPIR-V source language.
John Kessenich66e2faf2016-03-12 18:34:36 -0700282spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
John Kessenich140f3df2015-06-26 16:58:36 -0600283{
John Kessenich155d3512019-08-08 23:29:20 -0600284#ifdef GLSLANG_WEB
285 return spv::SourceLanguageESSL;
Shahbaz Youssefi1ef2e252020-07-03 15:42:53 -0400286#elif defined(GLSLANG_ANGLE)
287 return spv::SourceLanguageGLSL;
John Kessenich155d3512019-08-08 23:29:20 -0600288#endif
289
John Kessenich66e2faf2016-03-12 18:34:36 -0700290 switch (source) {
291 case glslang::EShSourceGlsl:
292 switch (profile) {
293 case ENoProfile:
294 case ECoreProfile:
295 case ECompatibilityProfile:
296 return spv::SourceLanguageGLSL;
297 case EEsProfile:
298 return spv::SourceLanguageESSL;
299 default:
300 return spv::SourceLanguageUnknown;
301 }
302 case glslang::EShSourceHlsl:
John Kessenich6fa17642017-04-07 15:33:08 -0600303 return spv::SourceLanguageHLSL;
John Kessenich140f3df2015-06-26 16:58:36 -0600304 default:
305 return spv::SourceLanguageUnknown;
306 }
307}
308
309// Translate glslang language (stage) to SPIR-V execution model.
310spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
311{
312 switch (stage) {
313 case EShLangVertex: return spv::ExecutionModelVertex;
John Kessenicha28f7a72019-08-06 07:00:58 -0600314 case EShLangFragment: return spv::ExecutionModelFragment;
John Kessenicha28f7a72019-08-06 07:00:58 -0600315 case EShLangCompute: return spv::ExecutionModelGLCompute;
John Kessenich51ed01c2019-10-10 11:40:11 -0600316#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -0600317 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
318 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
319 case EShLangGeometry: return spv::ExecutionModelGeometry;
Daniel Kochdb32b242020-03-17 20:42:47 -0400320 case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR;
321 case EShLangIntersect: return spv::ExecutionModelIntersectionKHR;
322 case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR;
323 case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR;
324 case EShLangMiss: return spv::ExecutionModelMissKHR;
325 case EShLangCallable: return spv::ExecutionModelCallableKHR;
Chao Chen3c366992018-09-19 11:41:59 -0700326 case EShLangTaskNV: return spv::ExecutionModelTaskNV;
327 case EShLangMeshNV: return spv::ExecutionModelMeshNV;
328#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600329 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700330 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600331 return spv::ExecutionModelFragment;
332 }
333}
334
John Kessenich140f3df2015-06-26 16:58:36 -0600335// Translate glslang sampler type to SPIR-V dimensionality.
336spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
337{
338 switch (sampler.dim) {
John Kessenich55e7d112015-11-15 21:33:39 -0700339 case glslang::Esd1D: return spv::Dim1D;
340 case glslang::Esd2D: return spv::Dim2D;
341 case glslang::Esd3D: return spv::Dim3D;
342 case glslang::EsdCube: return spv::DimCube;
343 case glslang::EsdRect: return spv::DimRect;
344 case glslang::EsdBuffer: return spv::DimBuffer;
John Kessenich6c292d32016-02-15 20:58:50 -0700345 case glslang::EsdSubpass: return spv::DimSubpassData;
John Kessenich140f3df2015-06-26 16:58:36 -0600346 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700347 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600348 return spv::Dim2D;
349 }
350}
351
John Kessenichf6640762016-08-01 19:44:00 -0600352// Translate glslang precision to SPIR-V precision decorations.
353spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
John Kessenich140f3df2015-06-26 16:58:36 -0600354{
John Kessenichf6640762016-08-01 19:44:00 -0600355 switch (glslangPrecision) {
John Kessenich61c47a92015-12-14 18:21:19 -0700356 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
John Kessenich5e4b1242015-08-06 22:53:06 -0600357 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
John Kessenich140f3df2015-06-26 16:58:36 -0600358 default:
359 return spv::NoPrecision;
360 }
361}
362
John Kessenichf6640762016-08-01 19:44:00 -0600363// Translate glslang type to SPIR-V precision decorations.
364spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
365{
366 return TranslatePrecisionDecoration(type.getQualifier().precision);
367}
368
John Kessenich140f3df2015-06-26 16:58:36 -0600369// Translate glslang type to SPIR-V block decorations.
John Kessenich67027182017-04-19 18:34:49 -0600370spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer)
John Kessenich140f3df2015-06-26 16:58:36 -0600371{
372 if (type.getBasicType() == glslang::EbtBlock) {
373 switch (type.getQualifier().storage) {
374 case glslang::EvqUniform: return spv::DecorationBlock;
John Kessenich67027182017-04-19 18:34:49 -0600375 case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock;
John Kessenich140f3df2015-06-26 16:58:36 -0600376 case glslang::EvqVaryingIn: return spv::DecorationBlock;
377 case glslang::EvqVaryingOut: return spv::DecorationBlock;
John Kessenicha28f7a72019-08-06 07:00:58 -0600378#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -0400379 case glslang::EvqPayload: return spv::DecorationBlock;
380 case glslang::EvqPayloadIn: return spv::DecorationBlock;
381 case glslang::EvqHitAttr: return spv::DecorationBlock;
382 case glslang::EvqCallableData: return spv::DecorationBlock;
383 case glslang::EvqCallableDataIn: return spv::DecorationBlock;
Chao Chenb50c02e2018-09-19 11:42:24 -0700384#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600385 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700386 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600387 break;
388 }
389 }
390
John Kessenich4016e382016-07-15 11:53:56 -0600391 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600392}
393
Rex Xu1da878f2016-02-21 20:59:01 +0800394// Translate glslang type to SPIR-V memory decorations.
John Kessenich8985fc92020-03-03 10:25:07 -0700395void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory,
396 bool useVulkanMemoryModel)
Rex Xu1da878f2016-02-21 20:59:01 +0800397{
Jeff Bolz36831c92018-09-05 10:11:41 -0500398 if (!useVulkanMemoryModel) {
John Kessenichf8d1d742019-10-21 06:55:11 -0600399 if (qualifier.isCoherent())
Jeff Bolz36831c92018-09-05 10:11:41 -0500400 memory.push_back(spv::DecorationCoherent);
John Kessenichf8d1d742019-10-21 06:55:11 -0600401 if (qualifier.isVolatile()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500402 memory.push_back(spv::DecorationVolatile);
403 memory.push_back(spv::DecorationCoherent);
404 }
John Kessenich14b85d32018-06-04 15:36:03 -0600405 }
John Kessenichf8d1d742019-10-21 06:55:11 -0600406 if (qualifier.isRestrict())
Rex Xu1da878f2016-02-21 20:59:01 +0800407 memory.push_back(spv::DecorationRestrict);
John Kessenichdeec1932019-08-13 08:00:30 -0600408 if (qualifier.isReadOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800409 memory.push_back(spv::DecorationNonWritable);
John Kessenichdeec1932019-08-13 08:00:30 -0600410 if (qualifier.isWriteOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800411 memory.push_back(spv::DecorationNonReadable);
412}
413
John Kessenich140f3df2015-06-26 16:58:36 -0600414// Translate glslang type to SPIR-V layout decorations.
John Kessenich3ac051e2015-12-20 11:29:16 -0700415spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
John Kessenich140f3df2015-06-26 16:58:36 -0600416{
417 if (type.isMatrix()) {
John Kessenich3ac051e2015-12-20 11:29:16 -0700418 switch (matrixLayout) {
John Kessenich140f3df2015-06-26 16:58:36 -0600419 case glslang::ElmRowMajor:
420 return spv::DecorationRowMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700421 case glslang::ElmColumnMajor:
John Kessenich140f3df2015-06-26 16:58:36 -0600422 return spv::DecorationColMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700423 default:
424 // opaque layouts don't need a majorness
John Kessenich4016e382016-07-15 11:53:56 -0600425 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600426 }
427 } else {
428 switch (type.getBasicType()) {
429 default:
John Kessenich4016e382016-07-15 11:53:56 -0600430 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600431 break;
432 case glslang::EbtBlock:
433 switch (type.getQualifier().storage) {
434 case glslang::EvqUniform:
435 case glslang::EvqBuffer:
436 switch (type.getQualifier().layoutPacking) {
437 case glslang::ElpShared: return spv::DecorationGLSLShared;
John Kessenich140f3df2015-06-26 16:58:36 -0600438 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
439 default:
John Kessenich4016e382016-07-15 11:53:56 -0600440 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600441 }
442 case glslang::EvqVaryingIn:
443 case glslang::EvqVaryingOut:
Chao Chen3c366992018-09-19 11:41:59 -0700444 if (type.getQualifier().isTaskMemory()) {
445 switch (type.getQualifier().layoutPacking) {
446 case glslang::ElpShared: return spv::DecorationGLSLShared;
447 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
448 default: break;
449 }
450 } else {
451 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
452 }
John Kessenich4016e382016-07-15 11:53:56 -0600453 return spv::DecorationMax;
John Kessenicha28f7a72019-08-06 07:00:58 -0600454#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -0400455 case glslang::EvqPayload:
456 case glslang::EvqPayloadIn:
457 case glslang::EvqHitAttr:
458 case glslang::EvqCallableData:
459 case glslang::EvqCallableDataIn:
Chao Chenb50c02e2018-09-19 11:42:24 -0700460 return spv::DecorationMax;
461#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600462 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700463 assert(0);
John Kessenich4016e382016-07-15 11:53:56 -0600464 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600465 }
466 }
467 }
468}
469
470// Translate glslang type to SPIR-V interpolation decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600471// Returns spv::DecorationMax when no decoration
John Kessenich55e7d112015-11-15 21:33:39 -0700472// should be applied.
Rex Xu17ff3432016-10-14 17:41:45 +0800473spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600474{
Rex Xubbceed72016-05-21 09:40:44 +0800475 if (qualifier.smooth)
John Kessenich55e7d112015-11-15 21:33:39 -0700476 // Smooth decoration doesn't exist in SPIR-V 1.0
John Kessenich4016e382016-07-15 11:53:56 -0600477 return spv::DecorationMax;
John Kessenich7015bd62019-08-01 03:28:08 -0600478 else if (qualifier.isNonPerspective())
John Kessenich55e7d112015-11-15 21:33:39 -0700479 return spv::DecorationNoPerspective;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700480 else if (qualifier.flat)
John Kessenich140f3df2015-06-26 16:58:36 -0600481 return spv::DecorationFlat;
John Kessenicha28f7a72019-08-06 07:00:58 -0600482 else if (qualifier.isExplicitInterpolation()) {
Rex Xu17ff3432016-10-14 17:41:45 +0800483 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
Rex Xu9d93a232016-05-05 12:30:44 +0800484 return spv::DecorationExplicitInterpAMD;
Rex Xu17ff3432016-10-14 17:41:45 +0800485 }
Rex Xubbceed72016-05-21 09:40:44 +0800486 else
John Kessenich4016e382016-07-15 11:53:56 -0600487 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800488}
489
490// Translate glslang type to SPIR-V auxiliary storage decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600491// Returns spv::DecorationMax when no decoration
Rex Xubbceed72016-05-21 09:40:44 +0800492// should be applied.
493spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
494{
John Kessenichb9197c82019-08-11 07:41:45 -0600495 if (qualifier.centroid)
John Kessenich140f3df2015-06-26 16:58:36 -0600496 return spv::DecorationCentroid;
John Kessenichb9197c82019-08-11 07:41:45 -0600497#ifndef GLSLANG_WEB
498 else if (qualifier.patch)
499 return spv::DecorationPatch;
John Kessenich5e801132016-02-15 11:09:46 -0700500 else if (qualifier.sample) {
501 builder.addCapability(spv::CapabilitySampleRateShading);
John Kessenich140f3df2015-06-26 16:58:36 -0600502 return spv::DecorationSample;
John Kessenichb9197c82019-08-11 07:41:45 -0600503 }
504#endif
505
506 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600507}
508
John Kessenich92187592016-02-01 13:45:25 -0700509// If glslang type is invariant, return SPIR-V invariant decoration.
John Kesseniche0b6cad2015-12-24 10:30:13 -0700510spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600511{
John Kesseniche0b6cad2015-12-24 10:30:13 -0700512 if (qualifier.invariant)
John Kessenich140f3df2015-06-26 16:58:36 -0600513 return spv::DecorationInvariant;
514 else
John Kessenich4016e382016-07-15 11:53:56 -0600515 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600516}
517
qining9220dbb2016-05-04 17:34:38 -0400518// If glslang type is noContraction, return SPIR-V NoContraction decoration.
519spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
520{
John Kessenichb9197c82019-08-11 07:41:45 -0600521#ifndef GLSLANG_WEB
John Kessenicha28f7a72019-08-06 07:00:58 -0600522 if (qualifier.isNoContraction())
qining9220dbb2016-05-04 17:34:38 -0400523 return spv::DecorationNoContraction;
524 else
John Kessenichb9197c82019-08-11 07:41:45 -0600525#endif
John Kessenich4016e382016-07-15 11:53:56 -0600526 return spv::DecorationMax;
qining9220dbb2016-05-04 17:34:38 -0400527}
528
John Kessenich5611c6d2018-04-05 11:25:02 -0600529// If glslang type is nonUniform, return SPIR-V NonUniform decoration.
530spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
531{
John Kessenichb9197c82019-08-11 07:41:45 -0600532#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -0600533 if (qualifier.isNonUniform()) {
John Kessenich8317e6c2019-08-18 23:58:08 -0600534 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -0600535 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
536 return spv::DecorationNonUniformEXT;
537 } else
John Kessenichb9197c82019-08-11 07:41:45 -0600538#endif
John Kessenich5611c6d2018-04-05 11:25:02 -0600539 return spv::DecorationMax;
540}
541
John Kessenichb9197c82019-08-11 07:41:45 -0600542spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(
543 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500544{
Jeff Bolz36831c92018-09-05 10:11:41 -0500545 spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600546
547#ifndef GLSLANG_WEB
548 if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage)
549 return mask;
550
Daniel Kochdb32b242020-03-17 20:42:47 -0400551 if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500552 mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask |
553 spv::MemoryAccessMakePointerVisibleKHRMask;
554 }
Daniel Kochdb32b242020-03-17 20:42:47 -0400555
Jeff Bolz36831c92018-09-05 10:11:41 -0500556 if (coherentFlags.nonprivate) {
557 mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask;
558 }
559 if (coherentFlags.volatil) {
560 mask = mask | spv::MemoryAccessVolatileMask;
561 }
562 if (mask != spv::MemoryAccessMaskNone) {
563 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
564 }
John Kessenichb9197c82019-08-11 07:41:45 -0600565#endif
566
Jeff Bolz36831c92018-09-05 10:11:41 -0500567 return mask;
568}
569
John Kessenichb9197c82019-08-11 07:41:45 -0600570spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(
571 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500572{
Jeff Bolz36831c92018-09-05 10:11:41 -0500573 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600574
575#ifndef GLSLANG_WEB
576 if (!glslangIntermediate->usingVulkanMemoryModel())
577 return mask;
578
Jeff Bolz36831c92018-09-05 10:11:41 -0500579 if (coherentFlags.volatil ||
Daniel Kochdb32b242020-03-17 20:42:47 -0400580 coherentFlags.anyCoherent()) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500581 mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask |
582 spv::ImageOperandsMakeTexelVisibleKHRMask;
583 }
584 if (coherentFlags.nonprivate) {
585 mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask;
586 }
587 if (coherentFlags.volatil) {
588 mask = mask | spv::ImageOperandsVolatileTexelKHRMask;
589 }
590 if (mask != spv::ImageOperandsMaskNone) {
591 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
592 }
John Kessenichb9197c82019-08-11 07:41:45 -0600593#endif
594
Jeff Bolz36831c92018-09-05 10:11:41 -0500595 return mask;
596}
597
598spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
599{
John Kessenichb9197c82019-08-11 07:41:45 -0600600 spv::Builder::AccessChain::CoherentFlags flags = {};
601#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -0500602 flags.coherent = type.getQualifier().coherent;
603 flags.devicecoherent = type.getQualifier().devicecoherent;
604 flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent;
605 // shared variables are implicitly workgroupcoherent in GLSL.
606 flags.workgroupcoherent = type.getQualifier().workgroupcoherent ||
607 type.getQualifier().storage == glslang::EvqShared;
608 flags.subgroupcoherent = type.getQualifier().subgroupcoherent;
Daniel Kochdb32b242020-03-17 20:42:47 -0400609 flags.shadercallcoherent = type.getQualifier().shadercallcoherent;
Jeff Bolz38cbad12019-03-05 14:40:07 -0600610 flags.volatil = type.getQualifier().volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500611 // *coherent variables are implicitly nonprivate in GLSL
612 flags.nonprivate = type.getQualifier().nonprivate ||
Daniel Kochdb32b242020-03-17 20:42:47 -0400613 flags.anyCoherent() ||
Jeff Bolz38cbad12019-03-05 14:40:07 -0600614 flags.volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500615 flags.isImage = type.getBasicType() == glslang::EbtSampler;
John Kessenichb9197c82019-08-11 07:41:45 -0600616#endif
Jeff Bolz36831c92018-09-05 10:11:41 -0500617 return flags;
618}
619
John Kessenichb9197c82019-08-11 07:41:45 -0600620spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(
621 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500622{
John Kessenichb9197c82019-08-11 07:41:45 -0600623 spv::Scope scope = spv::ScopeMax;
624
625#ifndef GLSLANG_WEB
Jeff Bolz38cbad12019-03-05 14:40:07 -0600626 if (coherentFlags.volatil || coherentFlags.coherent) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500627 // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model
628 scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
629 } else if (coherentFlags.devicecoherent) {
630 scope = spv::ScopeDevice;
631 } else if (coherentFlags.queuefamilycoherent) {
632 scope = spv::ScopeQueueFamilyKHR;
633 } else if (coherentFlags.workgroupcoherent) {
634 scope = spv::ScopeWorkgroup;
635 } else if (coherentFlags.subgroupcoherent) {
636 scope = spv::ScopeSubgroup;
Daniel Kochdb32b242020-03-17 20:42:47 -0400637 } else if (coherentFlags.shadercallcoherent) {
638 scope = spv::ScopeShaderCallKHR;
Jeff Bolz36831c92018-09-05 10:11:41 -0500639 }
640 if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) {
641 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
642 }
John Kessenichb9197c82019-08-11 07:41:45 -0600643#endif
644
Jeff Bolz36831c92018-09-05 10:11:41 -0500645 return scope;
646}
647
David Netoa901ffe2016-06-08 14:11:40 +0100648// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
649// associated capabilities when required. For some built-in variables, a capability
650// is generated only when using the variable in an executable instruction, but not when
651// just declaring a struct member variable with it. This is true for PointSize,
652// ClipDistance, and CullDistance.
John Kessenich8985fc92020-03-03 10:25:07 -0700653spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn,
654 bool memberDeclaration)
John Kessenich140f3df2015-06-26 16:58:36 -0600655{
656 switch (builtIn) {
John Kessenich92187592016-02-01 13:45:25 -0700657 case glslang::EbvPointSize:
John Kessenich155d3512019-08-08 23:29:20 -0600658#ifndef GLSLANG_WEB
John Kessenich78a45572016-07-08 14:05:15 -0600659 // Defer adding the capability until the built-in is actually used.
660 if (! memberDeclaration) {
661 switch (glslangIntermediate->getStage()) {
662 case EShLangGeometry:
663 builder.addCapability(spv::CapabilityGeometryPointSize);
664 break;
665 case EShLangTessControl:
666 case EShLangTessEvaluation:
667 builder.addCapability(spv::CapabilityTessellationPointSize);
668 break;
669 default:
670 break;
671 }
John Kessenich92187592016-02-01 13:45:25 -0700672 }
John Kessenich155d3512019-08-08 23:29:20 -0600673#endif
John Kessenich92187592016-02-01 13:45:25 -0700674 return spv::BuiltInPointSize;
675
John Kessenicha28f7a72019-08-06 07:00:58 -0600676 case glslang::EbvPosition: return spv::BuiltInPosition;
677 case glslang::EbvVertexId: return spv::BuiltInVertexId;
678 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
679 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
680 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
681
682 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
683 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
684 case glslang::EbvFace: return spv::BuiltInFrontFacing;
685 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
686
John Kessenich3dd1ce52019-10-17 07:08:40 -0600687 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
688 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
689 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
690 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
691 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
692 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
693
John Kessenicha28f7a72019-08-06 07:00:58 -0600694#ifndef GLSLANG_WEB
John Kessenichebb50532016-05-16 19:22:05 -0600695 // These *Distance capabilities logically belong here, but if the member is declared and
696 // then never used, consumers of SPIR-V prefer the capability not be declared.
697 // They are now generated when used, rather than here when declared.
698 // Potentially, the specification should be more clear what the minimum
699 // use needed is to trigger the capability.
700 //
John Kessenich92187592016-02-01 13:45:25 -0700701 case glslang::EbvClipDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100702 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800703 builder.addCapability(spv::CapabilityClipDistance);
John Kessenich92187592016-02-01 13:45:25 -0700704 return spv::BuiltInClipDistance;
705
706 case glslang::EbvCullDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100707 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800708 builder.addCapability(spv::CapabilityCullDistance);
John Kessenich92187592016-02-01 13:45:25 -0700709 return spv::BuiltInCullDistance;
710
711 case glslang::EbvViewportIndex:
John Kessenichba6a3c22017-09-13 13:22:50 -0600712 builder.addCapability(spv::CapabilityMultiViewport);
713 if (glslangIntermediate->getStage() == EShLangVertex ||
714 glslangIntermediate->getStage() == EShLangTessControl ||
715 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800716
John Kessenich8317e6c2019-08-18 23:58:08 -0600717 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
John Kessenichba6a3c22017-09-13 13:22:50 -0600718 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800719 }
John Kessenich92187592016-02-01 13:45:25 -0700720 return spv::BuiltInViewportIndex;
721
John Kessenich5e801132016-02-15 11:09:46 -0700722 case glslang::EbvSampleId:
723 builder.addCapability(spv::CapabilitySampleRateShading);
724 return spv::BuiltInSampleId;
725
726 case glslang::EbvSamplePosition:
727 builder.addCapability(spv::CapabilitySampleRateShading);
728 return spv::BuiltInSamplePosition;
729
730 case glslang::EbvSampleMask:
John Kessenich5e801132016-02-15 11:09:46 -0700731 return spv::BuiltInSampleMask;
732
John Kessenich78a45572016-07-08 14:05:15 -0600733 case glslang::EbvLayer:
Chao Chen3c366992018-09-19 11:41:59 -0700734 if (glslangIntermediate->getStage() == EShLangMeshNV) {
735 return spv::BuiltInLayer;
736 }
John Kessenichba6a3c22017-09-13 13:22:50 -0600737 builder.addCapability(spv::CapabilityGeometry);
738 if (glslangIntermediate->getStage() == EShLangVertex ||
739 glslangIntermediate->getStage() == EShLangTessControl ||
740 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800741
John Kessenich8317e6c2019-08-18 23:58:08 -0600742 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
John Kessenichba6a3c22017-09-13 13:22:50 -0600743 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800744 }
John Kessenich78a45572016-07-08 14:05:15 -0600745 return spv::BuiltInLayer;
746
John Kessenichda581a22015-10-14 14:10:30 -0600747 case glslang::EbvBaseVertex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600748 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800749 builder.addCapability(spv::CapabilityDrawParameters);
750 return spv::BuiltInBaseVertex;
751
John Kessenichda581a22015-10-14 14:10:30 -0600752 case glslang::EbvBaseInstance:
John Kessenich8317e6c2019-08-18 23:58:08 -0600753 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800754 builder.addCapability(spv::CapabilityDrawParameters);
755 return spv::BuiltInBaseInstance;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200756
John Kessenichda581a22015-10-14 14:10:30 -0600757 case glslang::EbvDrawId:
John Kessenich8317e6c2019-08-18 23:58:08 -0600758 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800759 builder.addCapability(spv::CapabilityDrawParameters);
760 return spv::BuiltInDrawIndex;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200761
762 case glslang::EbvPrimitiveId:
763 if (glslangIntermediate->getStage() == EShLangFragment)
764 builder.addCapability(spv::CapabilityGeometry);
765 return spv::BuiltInPrimitiveId;
766
Rex Xu37cdcee2017-06-29 17:46:34 +0800767 case glslang::EbvFragStencilRef:
Rex Xue8fdd792017-08-23 23:24:42 +0800768 builder.addExtension(spv::E_SPV_EXT_shader_stencil_export);
769 builder.addCapability(spv::CapabilityStencilExportEXT);
770 return spv::BuiltInFragStencilRefEXT;
Rex Xu37cdcee2017-06-29 17:46:34 +0800771
John Kessenich140f3df2015-06-26 16:58:36 -0600772 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
John Kessenich140f3df2015-06-26 16:58:36 -0600773 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
774 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
775 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
776 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
John Kessenich140f3df2015-06-26 16:58:36 -0600777 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
Rex Xu51596642016-09-21 18:56:12 +0800778
Rex Xu574ab042016-04-14 16:53:07 +0800779 case glslang::EbvSubGroupSize:
Rex Xu36876e62016-09-23 22:13:43 +0800780 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800781 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
782 return spv::BuiltInSubgroupSize;
783
Rex Xu574ab042016-04-14 16:53:07 +0800784 case glslang::EbvSubGroupInvocation:
Rex Xu36876e62016-09-23 22:13:43 +0800785 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800786 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
787 return spv::BuiltInSubgroupLocalInvocationId;
788
Rex Xu574ab042016-04-14 16:53:07 +0800789 case glslang::EbvSubGroupEqMask:
Rex Xu51596642016-09-21 18:56:12 +0800790 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
791 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600792 return spv::BuiltInSubgroupEqMask;
Rex Xu51596642016-09-21 18:56:12 +0800793
Rex Xu574ab042016-04-14 16:53:07 +0800794 case glslang::EbvSubGroupGeMask:
Rex Xu51596642016-09-21 18:56:12 +0800795 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
796 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600797 return spv::BuiltInSubgroupGeMask;
Rex Xu51596642016-09-21 18:56:12 +0800798
Rex Xu574ab042016-04-14 16:53:07 +0800799 case glslang::EbvSubGroupGtMask:
Rex Xu51596642016-09-21 18:56:12 +0800800 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
801 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600802 return spv::BuiltInSubgroupGtMask;
Rex Xu51596642016-09-21 18:56:12 +0800803
Rex Xu574ab042016-04-14 16:53:07 +0800804 case glslang::EbvSubGroupLeMask:
Rex Xu51596642016-09-21 18:56:12 +0800805 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
806 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600807 return spv::BuiltInSubgroupLeMask;
Rex Xu51596642016-09-21 18:56:12 +0800808
Rex Xu574ab042016-04-14 16:53:07 +0800809 case glslang::EbvSubGroupLtMask:
Rex Xu51596642016-09-21 18:56:12 +0800810 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
811 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600812 return spv::BuiltInSubgroupLtMask;
Rex Xu51596642016-09-21 18:56:12 +0800813
John Kessenich66011cb2018-03-06 16:12:04 -0700814 case glslang::EbvNumSubgroups:
815 builder.addCapability(spv::CapabilityGroupNonUniform);
816 return spv::BuiltInNumSubgroups;
817
818 case glslang::EbvSubgroupID:
819 builder.addCapability(spv::CapabilityGroupNonUniform);
820 return spv::BuiltInSubgroupId;
821
822 case glslang::EbvSubgroupSize2:
823 builder.addCapability(spv::CapabilityGroupNonUniform);
824 return spv::BuiltInSubgroupSize;
825
826 case glslang::EbvSubgroupInvocation2:
827 builder.addCapability(spv::CapabilityGroupNonUniform);
828 return spv::BuiltInSubgroupLocalInvocationId;
829
830 case glslang::EbvSubgroupEqMask2:
831 builder.addCapability(spv::CapabilityGroupNonUniform);
832 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
833 return spv::BuiltInSubgroupEqMask;
834
835 case glslang::EbvSubgroupGeMask2:
836 builder.addCapability(spv::CapabilityGroupNonUniform);
837 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
838 return spv::BuiltInSubgroupGeMask;
839
840 case glslang::EbvSubgroupGtMask2:
841 builder.addCapability(spv::CapabilityGroupNonUniform);
842 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
843 return spv::BuiltInSubgroupGtMask;
844
845 case glslang::EbvSubgroupLeMask2:
846 builder.addCapability(spv::CapabilityGroupNonUniform);
847 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
848 return spv::BuiltInSubgroupLeMask;
849
850 case glslang::EbvSubgroupLtMask2:
851 builder.addCapability(spv::CapabilityGroupNonUniform);
852 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
853 return spv::BuiltInSubgroupLtMask;
John Kessenich9c14f772019-06-17 08:38:35 -0600854
Rex Xu17ff3432016-10-14 17:41:45 +0800855 case glslang::EbvBaryCoordNoPersp:
856 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
857 return spv::BuiltInBaryCoordNoPerspAMD;
858
859 case glslang::EbvBaryCoordNoPerspCentroid:
860 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
861 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
862
863 case glslang::EbvBaryCoordNoPerspSample:
864 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
865 return spv::BuiltInBaryCoordNoPerspSampleAMD;
866
867 case glslang::EbvBaryCoordSmooth:
868 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
869 return spv::BuiltInBaryCoordSmoothAMD;
870
871 case glslang::EbvBaryCoordSmoothCentroid:
872 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
873 return spv::BuiltInBaryCoordSmoothCentroidAMD;
874
875 case glslang::EbvBaryCoordSmoothSample:
876 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
877 return spv::BuiltInBaryCoordSmoothSampleAMD;
878
879 case glslang::EbvBaryCoordPullModel:
880 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
881 return spv::BuiltInBaryCoordPullModelAMD;
chaoc771d89f2017-01-13 01:10:53 -0800882
John Kessenich6c8aaac2017-02-27 01:20:51 -0700883 case glslang::EbvDeviceIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600884 builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700885 builder.addCapability(spv::CapabilityDeviceGroup);
John Kessenich42e33c92017-02-27 01:50:28 -0700886 return spv::BuiltInDeviceIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700887
888 case glslang::EbvViewIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600889 builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700890 builder.addCapability(spv::CapabilityMultiView);
John Kessenich42e33c92017-02-27 01:50:28 -0700891 return spv::BuiltInViewIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700892
Daniel Koch5154db52018-11-26 10:01:58 -0500893 case glslang::EbvFragSizeEXT:
894 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
895 builder.addCapability(spv::CapabilityFragmentDensityEXT);
896 return spv::BuiltInFragSizeEXT;
897
898 case glslang::EbvFragInvocationCountEXT:
899 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
900 builder.addCapability(spv::CapabilityFragmentDensityEXT);
901 return spv::BuiltInFragInvocationCountEXT;
902
chaoc771d89f2017-01-13 01:10:53 -0800903 case glslang::EbvViewportMaskNV:
Rex Xu5e317ff2017-03-16 23:02:39 +0800904 if (!memberDeclaration) {
905 builder.addExtension(spv::E_SPV_NV_viewport_array2);
906 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
907 }
chaoc771d89f2017-01-13 01:10:53 -0800908 return spv::BuiltInViewportMaskNV;
909 case glslang::EbvSecondaryPositionNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800910 if (!memberDeclaration) {
911 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
912 builder.addCapability(spv::CapabilityShaderStereoViewNV);
913 }
chaoc771d89f2017-01-13 01:10:53 -0800914 return spv::BuiltInSecondaryPositionNV;
915 case glslang::EbvSecondaryViewportMaskNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800916 if (!memberDeclaration) {
917 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
918 builder.addCapability(spv::CapabilityShaderStereoViewNV);
919 }
chaoc771d89f2017-01-13 01:10:53 -0800920 return spv::BuiltInSecondaryViewportMaskNV;
chaocdf3956c2017-02-14 14:52:34 -0800921 case glslang::EbvPositionPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800922 if (!memberDeclaration) {
923 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
924 builder.addCapability(spv::CapabilityPerViewAttributesNV);
925 }
chaocdf3956c2017-02-14 14:52:34 -0800926 return spv::BuiltInPositionPerViewNV;
927 case glslang::EbvViewportMaskPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800928 if (!memberDeclaration) {
929 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
930 builder.addCapability(spv::CapabilityPerViewAttributesNV);
931 }
chaocdf3956c2017-02-14 14:52:34 -0800932 return spv::BuiltInViewportMaskPerViewNV;
Piers Daniell1c5443c2017-12-13 13:07:22 -0700933 case glslang::EbvFragFullyCoveredNV:
934 builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered);
935 builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT);
936 return spv::BuiltInFullyCoveredEXT;
Chao Chen5b2203d2018-09-19 11:43:21 -0700937 case glslang::EbvFragmentSizeNV:
938 builder.addExtension(spv::E_SPV_NV_shading_rate);
939 builder.addCapability(spv::CapabilityShadingRateNV);
940 return spv::BuiltInFragmentSizeNV;
941 case glslang::EbvInvocationsPerPixelNV:
942 builder.addExtension(spv::E_SPV_NV_shading_rate);
943 builder.addCapability(spv::CapabilityShadingRateNV);
944 return spv::BuiltInInvocationsPerPixelNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700945
Daniel Koch593a4e02019-05-27 16:46:31 -0400946 // ray tracing
Daniel Kochdb32b242020-03-17 20:42:47 -0400947 case glslang::EbvLaunchId:
948 return spv::BuiltInLaunchIdKHR;
949 case glslang::EbvLaunchSize:
950 return spv::BuiltInLaunchSizeKHR;
951 case glslang::EbvWorldRayOrigin:
952 return spv::BuiltInWorldRayOriginKHR;
953 case glslang::EbvWorldRayDirection:
954 return spv::BuiltInWorldRayDirectionKHR;
955 case glslang::EbvObjectRayOrigin:
956 return spv::BuiltInObjectRayOriginKHR;
957 case glslang::EbvObjectRayDirection:
958 return spv::BuiltInObjectRayDirectionKHR;
959 case glslang::EbvRayTmin:
960 return spv::BuiltInRayTminKHR;
961 case glslang::EbvRayTmax:
962 return spv::BuiltInRayTmaxKHR;
963 case glslang::EbvInstanceCustomIndex:
964 return spv::BuiltInInstanceCustomIndexKHR;
965 case glslang::EbvHitT:
966 return spv::BuiltInHitTKHR;
967 case glslang::EbvHitKind:
968 return spv::BuiltInHitKindKHR;
969 case glslang::EbvObjectToWorld:
970 case glslang::EbvObjectToWorld3x4:
971 return spv::BuiltInObjectToWorldKHR;
972 case glslang::EbvWorldToObject:
973 case glslang::EbvWorldToObject3x4:
974 return spv::BuiltInWorldToObjectKHR;
975 case glslang::EbvIncomingRayFlags:
976 return spv::BuiltInIncomingRayFlagsKHR;
977 case glslang::EbvGeometryIndex:
978 return spv::BuiltInRayGeometryIndexKHR;
Daniel Koch593a4e02019-05-27 16:46:31 -0400979
980 // barycentrics
Chao Chen9eada4b2018-09-19 11:39:56 -0700981 case glslang::EbvBaryCoordNV:
982 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
983 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
984 return spv::BuiltInBaryCoordNV;
985 case glslang::EbvBaryCoordNoPerspNV:
986 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
987 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
988 return spv::BuiltInBaryCoordNoPerspNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400989
990 // mesh shaders
991 case glslang::EbvTaskCountNV:
Chao Chen3c366992018-09-19 11:41:59 -0700992 return spv::BuiltInTaskCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400993 case glslang::EbvPrimitiveCountNV:
Chao Chen3c366992018-09-19 11:41:59 -0700994 return spv::BuiltInPrimitiveCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400995 case glslang::EbvPrimitiveIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -0700996 return spv::BuiltInPrimitiveIndicesNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400997 case glslang::EbvClipDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -0700998 return spv::BuiltInClipDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400999 case glslang::EbvCullDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07001000 return spv::BuiltInCullDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001001 case glslang::EbvLayerPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07001002 return spv::BuiltInLayerPerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001003 case glslang::EbvMeshViewCountNV:
Chao Chen3c366992018-09-19 11:41:59 -07001004 return spv::BuiltInMeshViewCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -04001005 case glslang::EbvMeshViewIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -07001006 return spv::BuiltInMeshViewIndicesNV;
Daniel Koch2cb2f192019-06-04 08:43:32 -04001007
1008 // sm builtins
1009 case glslang::EbvWarpsPerSM:
1010 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1011 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1012 return spv::BuiltInWarpsPerSMNV;
1013 case glslang::EbvSMCount:
1014 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1015 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1016 return spv::BuiltInSMCountNV;
1017 case glslang::EbvWarpID:
1018 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1019 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1020 return spv::BuiltInWarpIDNV;
1021 case glslang::EbvSMID:
1022 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1023 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1024 return spv::BuiltInSMIDNV;
John Kessenicha28f7a72019-08-06 07:00:58 -06001025#endif
1026
Rex Xu3e783f92017-02-22 16:44:48 +08001027 default:
1028 return spv::BuiltInMax;
John Kessenich140f3df2015-06-26 16:58:36 -06001029 }
1030}
1031
Rex Xufc618912015-09-09 16:42:49 +08001032// Translate glslang image layout format to SPIR-V image format.
John Kessenich5d0fa972016-02-15 11:57:00 -07001033spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
Rex Xufc618912015-09-09 16:42:49 +08001034{
1035 assert(type.getBasicType() == glslang::EbtSampler);
1036
John Kessenichb9197c82019-08-11 07:41:45 -06001037#ifdef GLSLANG_WEB
1038 return spv::ImageFormatUnknown;
1039#endif
1040
John Kessenich5d0fa972016-02-15 11:57:00 -07001041 // Check for capabilities
John Kessenich7015bd62019-08-01 03:28:08 -06001042 switch (type.getQualifier().getFormat()) {
John Kessenich5d0fa972016-02-15 11:57:00 -07001043 case glslang::ElfRg32f:
1044 case glslang::ElfRg16f:
1045 case glslang::ElfR11fG11fB10f:
1046 case glslang::ElfR16f:
1047 case glslang::ElfRgba16:
1048 case glslang::ElfRgb10A2:
1049 case glslang::ElfRg16:
1050 case glslang::ElfRg8:
1051 case glslang::ElfR16:
1052 case glslang::ElfR8:
1053 case glslang::ElfRgba16Snorm:
1054 case glslang::ElfRg16Snorm:
1055 case glslang::ElfRg8Snorm:
1056 case glslang::ElfR16Snorm:
1057 case glslang::ElfR8Snorm:
1058
1059 case glslang::ElfRg32i:
1060 case glslang::ElfRg16i:
1061 case glslang::ElfRg8i:
1062 case glslang::ElfR16i:
1063 case glslang::ElfR8i:
1064
1065 case glslang::ElfRgb10a2ui:
1066 case glslang::ElfRg32ui:
1067 case glslang::ElfRg16ui:
1068 case glslang::ElfRg8ui:
1069 case glslang::ElfR16ui:
1070 case glslang::ElfR8ui:
1071 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
1072 break;
1073
1074 default:
1075 break;
1076 }
1077
1078 // do the translation
John Kessenich7015bd62019-08-01 03:28:08 -06001079 switch (type.getQualifier().getFormat()) {
Rex Xufc618912015-09-09 16:42:49 +08001080 case glslang::ElfNone: return spv::ImageFormatUnknown;
1081 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
1082 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
1083 case glslang::ElfR32f: return spv::ImageFormatR32f;
1084 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
1085 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
1086 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
1087 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
1088 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
1089 case glslang::ElfR16f: return spv::ImageFormatR16f;
1090 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
1091 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
1092 case glslang::ElfRg16: return spv::ImageFormatRg16;
1093 case glslang::ElfRg8: return spv::ImageFormatRg8;
1094 case glslang::ElfR16: return spv::ImageFormatR16;
1095 case glslang::ElfR8: return spv::ImageFormatR8;
1096 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
1097 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
1098 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
1099 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
1100 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
1101 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
1102 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
1103 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
1104 case glslang::ElfR32i: return spv::ImageFormatR32i;
1105 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
1106 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
1107 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
1108 case glslang::ElfR16i: return spv::ImageFormatR16i;
1109 case glslang::ElfR8i: return spv::ImageFormatR8i;
1110 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
1111 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
1112 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
1113 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
1114 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
1115 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
1116 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
1117 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
1118 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
1119 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
John Kessenich4016e382016-07-15 11:53:56 -06001120 default: return spv::ImageFormatMax;
Rex Xufc618912015-09-09 16:42:49 +08001121 }
1122}
1123
John Kessenich8985fc92020-03-03 10:25:07 -07001124spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(
1125 const glslang::TIntermSelection& selectionNode) const
Rex Xu57e65922017-07-04 23:23:40 +08001126{
John Kesseniche18fd202018-01-30 11:01:39 -07001127 if (selectionNode.getFlatten())
1128 return spv::SelectionControlFlattenMask;
1129 if (selectionNode.getDontFlatten())
1130 return spv::SelectionControlDontFlattenMask;
1131 return spv::SelectionControlMaskNone;
Rex Xu57e65922017-07-04 23:23:40 +08001132}
1133
John Kessenich8985fc92020-03-03 10:25:07 -07001134spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode)
1135 const
steve-lunargf1709e72017-05-02 20:14:50 -06001136{
John Kesseniche18fd202018-01-30 11:01:39 -07001137 if (switchNode.getFlatten())
1138 return spv::SelectionControlFlattenMask;
1139 if (switchNode.getDontFlatten())
1140 return spv::SelectionControlDontFlattenMask;
1141 return spv::SelectionControlMaskNone;
1142}
1143
John Kessenicha2858d92018-01-31 08:11:18 -07001144// return a non-0 dependency if the dependency argument must be set
1145spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
John Kessenich1f4d0462019-01-12 17:31:41 +07001146 std::vector<unsigned int>& operands) const
John Kesseniche18fd202018-01-30 11:01:39 -07001147{
1148 spv::LoopControlMask control = spv::LoopControlMaskNone;
1149
1150 if (loopNode.getDontUnroll())
1151 control = control | spv::LoopControlDontUnrollMask;
1152 if (loopNode.getUnroll())
1153 control = control | spv::LoopControlUnrollMask;
LoopDawg4425f242018-02-18 11:40:01 -07001154 if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite)
John Kessenicha2858d92018-01-31 08:11:18 -07001155 control = control | spv::LoopControlDependencyInfiniteMask;
1156 else if (loopNode.getLoopDependency() > 0) {
1157 control = control | spv::LoopControlDependencyLengthMask;
John Kessenich1f4d0462019-01-12 17:31:41 +07001158 operands.push_back((unsigned int)loopNode.getLoopDependency());
1159 }
1160 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
1161 if (loopNode.getMinIterations() > 0) {
1162 control = control | spv::LoopControlMinIterationsMask;
1163 operands.push_back(loopNode.getMinIterations());
1164 }
1165 if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) {
1166 control = control | spv::LoopControlMaxIterationsMask;
1167 operands.push_back(loopNode.getMaxIterations());
1168 }
1169 if (loopNode.getIterationMultiple() > 1) {
1170 control = control | spv::LoopControlIterationMultipleMask;
1171 operands.push_back(loopNode.getIterationMultiple());
1172 }
1173 if (loopNode.getPeelCount() > 0) {
1174 control = control | spv::LoopControlPeelCountMask;
1175 operands.push_back(loopNode.getPeelCount());
1176 }
1177 if (loopNode.getPartialCount() > 0) {
1178 control = control | spv::LoopControlPartialCountMask;
1179 operands.push_back(loopNode.getPartialCount());
1180 }
John Kessenicha2858d92018-01-31 08:11:18 -07001181 }
John Kesseniche18fd202018-01-30 11:01:39 -07001182
1183 return control;
steve-lunargf1709e72017-05-02 20:14:50 -06001184}
1185
John Kessenicha5c5fb62017-05-05 05:09:58 -06001186// Translate glslang type to SPIR-V storage class.
1187spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
1188{
Torosdagli06c2eee2020-03-19 11:09:57 -04001189 if (type.getBasicType() == glslang::EbtRayQuery)
Neslisah Torosdagli054b5e32020-03-26 12:24:31 -04001190 return spv::StorageClassFunction;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001191 if (type.getQualifier().isPipeInput())
1192 return spv::StorageClassInput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001193 if (type.getQualifier().isPipeOutput())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001194 return spv::StorageClassOutput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001195
1196 if (glslangIntermediate->getSource() != glslang::EShSourceHlsl ||
John Kessenicha28f7a72019-08-06 07:00:58 -06001197 type.getQualifier().storage == glslang::EvqUniform) {
John Kessenichdeec1932019-08-13 08:00:30 -06001198 if (type.isAtomic())
John Kessenichbed4e4f2017-09-08 02:38:07 -06001199 return spv::StorageClassAtomicCounter;
1200 if (type.containsOpaque())
1201 return spv::StorageClassUniformConstant;
1202 }
1203
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001204 if (type.getQualifier().isUniformOrBuffer() &&
Daniel Kochdb32b242020-03-17 20:42:47 -04001205 type.getQualifier().isShaderRecord()) {
1206 return spv::StorageClassShaderRecordBufferKHR;
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001207 }
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001208
John Kessenichbed4e4f2017-09-08 02:38:07 -06001209 if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001210 builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3);
John Kessenicha5c5fb62017-05-05 05:09:58 -06001211 return spv::StorageClassStorageBuffer;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001212 }
1213
1214 if (type.getQualifier().isUniformOrBuffer()) {
John Kessenich7015bd62019-08-01 03:28:08 -06001215 if (type.getQualifier().isPushConstant())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001216 return spv::StorageClassPushConstant;
1217 if (type.getBasicType() == glslang::EbtBlock)
1218 return spv::StorageClassUniform;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001219 return spv::StorageClassUniformConstant;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001220 }
John Kessenichbed4e4f2017-09-08 02:38:07 -06001221
1222 switch (type.getQualifier().storage) {
John Kessenichf8d1d742019-10-21 06:55:11 -06001223 case glslang::EvqGlobal: return spv::StorageClassPrivate;
1224 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
1225 case glslang::EvqTemporary: return spv::StorageClassFunction;
John Kessenichdeec1932019-08-13 08:00:30 -06001226 case glslang::EvqShared: return spv::StorageClassWorkgroup;
John Kessenich3dd1ce52019-10-17 07:08:40 -06001227#ifndef GLSLANG_WEB
Daniel Kochdb32b242020-03-17 20:42:47 -04001228 case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR;
1229 case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR;
1230 case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR;
1231 case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR;
1232 case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR;
Chao Chenb50c02e2018-09-19 11:42:24 -07001233#endif
John Kessenichbed4e4f2017-09-08 02:38:07 -06001234 default:
1235 assert(0);
1236 break;
1237 }
1238
1239 return spv::StorageClassFunction;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001240}
1241
John Kessenich5611c6d2018-04-05 11:25:02 -06001242// Add capabilities pertaining to how an array is indexed.
1243void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
1244 const glslang::TType& indexType)
1245{
John Kessenichb9197c82019-08-11 07:41:45 -06001246#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06001247 if (indexType.getQualifier().isNonUniform()) {
1248 // deal with an asserted non-uniform index
Jeff Bolzc140b962018-07-12 16:51:18 -05001249 // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration
John Kessenich5611c6d2018-04-05 11:25:02 -06001250 if (baseType.getBasicType() == glslang::EbtSampler) {
1251 if (baseType.getQualifier().hasAttachment())
1252 builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001253 else if (baseType.isImage() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001254 builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001255 else if (baseType.isTexture() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001256 builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT);
1257 else if (baseType.isImage())
1258 builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT);
1259 else if (baseType.isTexture())
1260 builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT);
1261 } else if (baseType.getBasicType() == glslang::EbtBlock) {
1262 if (baseType.getQualifier().storage == glslang::EvqBuffer)
1263 builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT);
1264 else if (baseType.getQualifier().storage == glslang::EvqUniform)
1265 builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT);
1266 }
1267 } else {
1268 // assume a dynamically uniform index
1269 if (baseType.getBasicType() == glslang::EbtSampler) {
Jeff Bolzc140b962018-07-12 16:51:18 -05001270 if (baseType.getQualifier().hasAttachment()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001271 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001272 builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001273 } else if (baseType.isImage() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001274 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001275 builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001276 } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001277 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001278 builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001279 }
John Kessenich5611c6d2018-04-05 11:25:02 -06001280 }
1281 }
John Kessenichb9197c82019-08-11 07:41:45 -06001282#endif
John Kessenich5611c6d2018-04-05 11:25:02 -06001283}
1284
qining25262b32016-05-06 17:25:16 -04001285// Return whether or not the given type is something that should be tied to a
John Kessenich6c292d32016-02-15 20:58:50 -07001286// descriptor set.
1287bool IsDescriptorResource(const glslang::TType& type)
1288{
John Kessenichf7497e22016-03-08 21:36:22 -07001289 // uniform and buffer blocks are included, unless it is a push_constant
John Kessenich6c292d32016-02-15 20:58:50 -07001290 if (type.getBasicType() == glslang::EbtBlock)
Chao Chenb50c02e2018-09-19 11:42:24 -07001291 return type.getQualifier().isUniformOrBuffer() &&
Daniel Kochdb32b242020-03-17 20:42:47 -04001292 ! type.getQualifier().isShaderRecord() &&
John Kessenich7015bd62019-08-01 03:28:08 -06001293 ! type.getQualifier().isPushConstant();
John Kessenich6c292d32016-02-15 20:58:50 -07001294
1295 // non block...
1296 // basically samplerXXX/subpass/sampler/texture are all included
1297 // if they are the global-scope-class, not the function parameter
1298 // (or local, if they ever exist) class.
alelenv999d4fd2020-06-01 23:24:41 -07001299 if (type.getBasicType() == glslang::EbtSampler ||
1300 type.getBasicType() == glslang::EbtAccStruct)
John Kessenich6c292d32016-02-15 20:58:50 -07001301 return type.getQualifier().isUniformOrBuffer();
1302
1303 // None of the above.
1304 return false;
1305}
1306
John Kesseniche0b6cad2015-12-24 10:30:13 -07001307void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
1308{
1309 if (child.layoutMatrix == glslang::ElmNone)
1310 child.layoutMatrix = parent.layoutMatrix;
1311
1312 if (parent.invariant)
1313 child.invariant = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001314 if (parent.flat)
1315 child.flat = true;
1316 if (parent.centroid)
1317 child.centroid = true;
John Kessenich7015bd62019-08-01 03:28:08 -06001318#ifndef GLSLANG_WEB
John Kesseniche0b6cad2015-12-24 10:30:13 -07001319 if (parent.nopersp)
1320 child.nopersp = true;
Rex Xu9d93a232016-05-05 12:30:44 +08001321 if (parent.explicitInterp)
1322 child.explicitInterp = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001323 if (parent.perPrimitiveNV)
1324 child.perPrimitiveNV = true;
1325 if (parent.perViewNV)
1326 child.perViewNV = true;
1327 if (parent.perTaskNV)
1328 child.perTaskNV = true;
John Kesseniche0b6cad2015-12-24 10:30:13 -07001329 if (parent.patch)
1330 child.patch = true;
1331 if (parent.sample)
1332 child.sample = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001333 if (parent.coherent)
1334 child.coherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001335 if (parent.devicecoherent)
1336 child.devicecoherent = true;
1337 if (parent.queuefamilycoherent)
1338 child.queuefamilycoherent = true;
1339 if (parent.workgroupcoherent)
1340 child.workgroupcoherent = true;
1341 if (parent.subgroupcoherent)
1342 child.subgroupcoherent = true;
Daniel Kochdb32b242020-03-17 20:42:47 -04001343 if (parent.shadercallcoherent)
1344 child.shadercallcoherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001345 if (parent.nonprivate)
1346 child.nonprivate = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001347 if (parent.volatil)
1348 child.volatil = true;
1349 if (parent.restrict)
1350 child.restrict = true;
1351 if (parent.readonly)
1352 child.readonly = true;
1353 if (parent.writeonly)
1354 child.writeonly = true;
Chao Chen3c366992018-09-19 11:41:59 -07001355#endif
John Kesseniche0b6cad2015-12-24 10:30:13 -07001356}
1357
John Kessenichf2b7f332016-09-01 17:05:23 -06001358bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001359{
John Kessenich7b9fa252016-01-21 18:56:57 -07001360 // This should list qualifiers that simultaneous satisfy:
John Kessenichf2b7f332016-09-01 17:05:23 -06001361 // - struct members might inherit from a struct declaration
1362 // (note that non-block structs don't explicitly inherit,
1363 // only implicitly, meaning no decoration involved)
1364 // - affect decorations on the struct members
1365 // (note smooth does not, and expecting something like volatile
1366 // to effect the whole object)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001367 // - are not part of the offset/st430/etc or row/column-major layout
John Kessenichf2b7f332016-09-01 17:05:23 -06001368 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
John Kesseniche0b6cad2015-12-24 10:30:13 -07001369}
1370
John Kessenich140f3df2015-06-26 16:58:36 -06001371//
1372// Implement the TGlslangToSpvTraverser class.
1373//
1374
John Kessenich8985fc92020-03-03 10:25:07 -07001375TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion,
1376 const glslang::TIntermediate* glslangIntermediate,
1377 spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) :
1378 TIntermTraverser(true, false, true),
1379 options(options),
1380 shaderEntry(nullptr), currentFunction(nullptr),
1381 sequenceDepth(0), logger(buildLogger),
1382 builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger),
1383 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
1384 glslangIntermediate(glslangIntermediate),
Jeff Bolz04d73732019-05-31 13:06:01 -05001385 nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()),
1386 nonSemanticDebugPrintf(0)
John Kessenich140f3df2015-06-26 16:58:36 -06001387{
1388 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
1389
1390 builder.clearAccessChain();
John Kessenich2a271162017-07-20 20:00:36 -06001391 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()),
1392 glslangIntermediate->getVersion());
1393
John Kessenich121853f2017-05-31 17:11:16 -06001394 if (options.generateDebugInfo) {
John Kesseniche485c7a2017-05-31 18:50:53 -06001395 builder.setEmitOpLines();
John Kessenich2a271162017-07-20 20:00:36 -06001396 builder.setSourceFile(glslangIntermediate->getSourceFile());
1397
1398 // Set the source shader's text. If for SPV version 1.0, include
1399 // a preamble in comments stating the OpModuleProcessed instructions.
1400 // Otherwise, emit those as actual instructions.
1401 std::string text;
1402 const std::vector<std::string>& processes = glslangIntermediate->getProcesses();
1403 for (int p = 0; p < (int)processes.size(); ++p) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001404 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) {
John Kessenich2a271162017-07-20 20:00:36 -06001405 text.append("// OpModuleProcessed ");
1406 text.append(processes[p]);
1407 text.append("\n");
1408 } else
1409 builder.addModuleProcessed(processes[p]);
1410 }
John Kessenich8717a5d2018-10-26 10:12:32 -06001411 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0)
John Kessenich2a271162017-07-20 20:00:36 -06001412 text.append("#line 1\n");
1413 text.append(glslangIntermediate->getSourceText());
1414 builder.setSourceText(text);
Greg Fischerd445bb22018-12-06 11:13:15 -07001415 // Pass name and text for all included files
1416 const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
1417 for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
1418 builder.addInclude(iItr->first, iItr->second);
John Kessenich121853f2017-05-31 17:11:16 -06001419 }
John Kessenich140f3df2015-06-26 16:58:36 -06001420 stdBuiltins = builder.import("GLSL.std.450");
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001421
1422 spv::AddressingModel addressingModel = spv::AddressingModelLogical;
1423 spv::MemoryModel memoryModel = spv::MemoryModelGLSL450;
1424
1425 if (glslangIntermediate->usingPhysicalStorageBuffer()) {
1426 addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT;
John Kessenich1ff0c182019-10-10 12:01:13 -06001427 builder.addIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, spv::Spv_1_5);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001428 builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT);
John Kessenich8985fc92020-03-03 10:25:07 -07001429 }
Jeff Bolz36831c92018-09-05 10:11:41 -05001430 if (glslangIntermediate->usingVulkanMemoryModel()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001431 memoryModel = spv::MemoryModelVulkanKHR;
1432 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
John Kessenich8317e6c2019-08-18 23:58:08 -06001433 builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
Jeff Bolz36831c92018-09-05 10:11:41 -05001434 }
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001435 builder.setMemoryModel(addressingModel, memoryModel);
1436
Jeff Bolz4605e2e2019-02-19 13:10:32 -06001437 if (glslangIntermediate->usingVariablePointers()) {
1438 builder.addCapability(spv::CapabilityVariablePointers);
1439 }
1440
John Kessenicheee9d532016-09-19 18:09:30 -06001441 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
1442 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001443
1444 // Add the source extensions
John Kessenich2f273362015-07-18 22:34:27 -06001445 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
1446 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
Pankaj Mistry2a8ead22020-04-26 17:52:31 -07001447 builder.addSourceExtension(it->first.c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001448
1449 // Add the top-level modes for this shader.
1450
John Kessenich92187592016-02-01 13:45:25 -07001451 if (glslangIntermediate->getXfbMode()) {
1452 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06001453 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
John Kessenich92187592016-02-01 13:45:25 -07001454 }
John Kessenich140f3df2015-06-26 16:58:36 -06001455
alelenv59216d52020-05-21 04:38:41 -07001456 if (glslangIntermediate->getLayoutPrimitiveCulling()) {
alelenv75de1962020-04-08 21:09:20 -07001457 builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingProvisionalKHR);
1458 }
1459
John Kessenich140f3df2015-06-26 16:58:36 -06001460 unsigned int mode;
1461 switch (glslangIntermediate->getStage()) {
1462 case EShLangVertex:
John Kessenich5e4b1242015-08-06 22:53:06 -06001463 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -06001464 break;
1465
John Kessenicha28f7a72019-08-06 07:00:58 -06001466 case EShLangFragment:
1467 builder.addCapability(spv::CapabilityShader);
1468 if (glslangIntermediate->getPixelCenterInteger())
1469 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
1470
1471 if (glslangIntermediate->getOriginUpperLeft())
1472 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
1473 else
1474 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
1475
1476 if (glslangIntermediate->getEarlyFragmentTests())
1477 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
1478
1479 if (glslangIntermediate->getPostDepthCoverage()) {
1480 builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage);
1481 builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage);
1482 builder.addExtension(spv::E_SPV_KHR_post_depth_coverage);
1483 }
1484
John Kessenichb9197c82019-08-11 07:41:45 -06001485 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
1486 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
1487
1488#ifndef GLSLANG_WEB
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04001489
John Kessenicha28f7a72019-08-06 07:00:58 -06001490 switch(glslangIntermediate->getDepth()) {
1491 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
1492 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
1493 default: mode = spv::ExecutionModeMax; break;
1494 }
1495 if (mode != spv::ExecutionModeMax)
1496 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kessenicha28f7a72019-08-06 07:00:58 -06001497 switch (glslangIntermediate->getInterlockOrdering()) {
John Kessenichf8d1d742019-10-21 06:55:11 -06001498 case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT;
1499 break;
1500 case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT;
1501 break;
1502 case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT;
1503 break;
1504 case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT;
1505 break;
1506 case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT;
1507 break;
1508 case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT;
1509 break;
1510 default: mode = spv::ExecutionModeMax;
1511 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06001512 }
1513 if (mode != spv::ExecutionModeMax) {
1514 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1515 if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT ||
1516 mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) {
1517 builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT);
1518 } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT ||
1519 mode == spv::ExecutionModePixelInterlockUnorderedEXT) {
1520 builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT);
1521 } else {
1522 builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT);
1523 }
1524 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
1525 }
John Kessenichb9197c82019-08-11 07:41:45 -06001526#endif
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04001527 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06001528
John Kessenicha28f7a72019-08-06 07:00:58 -06001529 case EShLangCompute:
1530 builder.addCapability(spv::CapabilityShader);
1531 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1532 glslangIntermediate->getLocalSize(1),
1533 glslangIntermediate->getLocalSize(2));
1534 if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) {
1535 builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV);
1536 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV);
1537 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1538 } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) {
1539 builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV);
1540 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV);
1541 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1542 }
1543 break;
John Kessenich51ed01c2019-10-10 11:40:11 -06001544#ifndef GLSLANG_WEB
steve-lunarge7412492017-03-23 11:56:07 -06001545 case EShLangTessEvaluation:
John Kessenich140f3df2015-06-26 16:58:36 -06001546 case EShLangTessControl:
John Kessenich5e4b1242015-08-06 22:53:06 -06001547 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -06001548
steve-lunarge7412492017-03-23 11:56:07 -06001549 glslang::TLayoutGeometry primitive;
1550
1551 if (glslangIntermediate->getStage() == EShLangTessControl) {
John Kessenich8985fc92020-03-03 10:25:07 -07001552 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices,
1553 glslangIntermediate->getVertices());
steve-lunarge7412492017-03-23 11:56:07 -06001554 primitive = glslangIntermediate->getOutputPrimitive();
1555 } else {
1556 primitive = glslangIntermediate->getInputPrimitive();
1557 }
1558
1559 switch (primitive) {
John Kessenich55e7d112015-11-15 21:33:39 -07001560 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1561 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
1562 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
John Kessenich4016e382016-07-15 11:53:56 -06001563 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001564 }
John Kessenich4016e382016-07-15 11:53:56 -06001565 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001566 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1567
John Kesseniche6903322015-10-13 16:29:02 -06001568 switch (glslangIntermediate->getVertexSpacing()) {
1569 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
1570 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
1571 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
John Kessenich4016e382016-07-15 11:53:56 -06001572 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001573 }
John Kessenich4016e382016-07-15 11:53:56 -06001574 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001575 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1576
1577 switch (glslangIntermediate->getVertexOrder()) {
1578 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
1579 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
John Kessenich4016e382016-07-15 11:53:56 -06001580 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001581 }
John Kessenich4016e382016-07-15 11:53:56 -06001582 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001583 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1584
1585 if (glslangIntermediate->getPointMode())
1586 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
John Kessenich140f3df2015-06-26 16:58:36 -06001587 break;
1588
1589 case EShLangGeometry:
John Kessenich5e4b1242015-08-06 22:53:06 -06001590 builder.addCapability(spv::CapabilityGeometry);
John Kessenich140f3df2015-06-26 16:58:36 -06001591 switch (glslangIntermediate->getInputPrimitive()) {
1592 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
1593 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
1594 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
John Kessenich55e7d112015-11-15 21:33:39 -07001595 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001596 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
John Kessenich4016e382016-07-15 11:53:56 -06001597 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001598 }
John Kessenich4016e382016-07-15 11:53:56 -06001599 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001600 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kesseniche6903322015-10-13 16:29:02 -06001601
John Kessenich140f3df2015-06-26 16:58:36 -06001602 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
1603
1604 switch (glslangIntermediate->getOutputPrimitive()) {
1605 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1606 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
1607 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
John Kessenich4016e382016-07-15 11:53:56 -06001608 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001609 }
John Kessenich4016e382016-07-15 11:53:56 -06001610 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001611 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1612 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1613 break;
1614
Daniel Kochdb32b242020-03-17 20:42:47 -04001615 case EShLangRayGen:
1616 case EShLangIntersect:
1617 case EShLangAnyHit:
1618 case EShLangClosestHit:
1619 case EShLangMiss:
1620 case EShLangCallable:
1621 {
1622 auto& extensions = glslangIntermediate->getRequestedExtensions();
1623 if (extensions.find("GL_NV_ray_tracing") == extensions.end()) {
1624 builder.addCapability(spv::CapabilityRayTracingProvisionalKHR);
1625 builder.addExtension("SPV_KHR_ray_tracing");
1626 }
1627 else {
1628 builder.addCapability(spv::CapabilityRayTracingNV);
1629 builder.addExtension("SPV_NV_ray_tracing");
1630 }
Chao Chenb50c02e2018-09-19 11:42:24 -07001631 break;
Daniel Kochdb32b242020-03-17 20:42:47 -04001632 }
Chao Chen3c366992018-09-19 11:41:59 -07001633 case EShLangTaskNV:
1634 case EShLangMeshNV:
1635 builder.addCapability(spv::CapabilityMeshShadingNV);
1636 builder.addExtension(spv::E_SPV_NV_mesh_shader);
1637 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1638 glslangIntermediate->getLocalSize(1),
1639 glslangIntermediate->getLocalSize(2));
1640 if (glslangIntermediate->getStage() == EShLangMeshNV) {
John Kessenich8985fc92020-03-03 10:25:07 -07001641 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices,
1642 glslangIntermediate->getVertices());
1643 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV,
1644 glslangIntermediate->getPrimitives());
Chao Chen3c366992018-09-19 11:41:59 -07001645
1646 switch (glslangIntermediate->getOutputPrimitive()) {
1647 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1648 case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break;
1649 case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break;
1650 default: mode = spv::ExecutionModeMax; break;
1651 }
1652 if (mode != spv::ExecutionModeMax)
1653 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1654 }
1655 break;
1656#endif
1657
John Kessenich140f3df2015-06-26 16:58:36 -06001658 default:
1659 break;
1660 }
John Kessenich140f3df2015-06-26 16:58:36 -06001661}
1662
John Kessenichfca82622016-11-26 13:23:20 -07001663// Finish creating SPV, after the traversal is complete.
1664void TGlslangToSpvTraverser::finishSpv()
John Kessenich7ba63412015-12-20 17:37:07 -07001665{
John Kessenichf04c51b2018-08-03 15:56:12 -06001666 // Finish the entry point function
John Kessenich517fe7a2016-11-26 13:31:47 -07001667 if (! entryPointTerminated) {
John Kessenichfca82622016-11-26 13:23:20 -07001668 builder.setBuildPoint(shaderEntry->getLastBlock());
1669 builder.leaveFunction();
1670 }
1671
John Kessenich7ba63412015-12-20 17:37:07 -07001672 // finish off the entry-point SPV instruction by adding the Input/Output <id>
rdb32084e82016-02-23 22:17:38 +01001673 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
1674 entryPoint->addIdOperand(*it);
John Kessenich7ba63412015-12-20 17:37:07 -07001675
David Neto8c3d5b42019-10-21 14:50:31 -04001676 // Add capabilities, extensions, remove unneeded decorations, etc.,
John Kessenichf04c51b2018-08-03 15:56:12 -06001677 // based on the resulting SPIR-V.
David Neto8c3d5b42019-10-21 14:50:31 -04001678 // Note: WebGPU code generation must have the opportunity to aggressively
1679 // prune unreachable merge blocks and continue targets.
John Kessenichf04c51b2018-08-03 15:56:12 -06001680 builder.postProcess();
John Kessenich7ba63412015-12-20 17:37:07 -07001681}
1682
John Kessenichfca82622016-11-26 13:23:20 -07001683// Write the SPV into 'out'.
1684void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
John Kessenich140f3df2015-06-26 16:58:36 -06001685{
John Kessenichfca82622016-11-26 13:23:20 -07001686 builder.dump(out);
John Kessenich140f3df2015-06-26 16:58:36 -06001687}
1688
1689//
1690// Implement the traversal functions.
1691//
1692// Return true from interior nodes to have the external traversal
1693// continue on to children. Return false if children were
1694// already processed.
1695//
1696
1697//
qining25262b32016-05-06 17:25:16 -04001698// Symbols can turn into
John Kessenich140f3df2015-06-26 16:58:36 -06001699// - uniform/input reads
1700// - output writes
1701// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
1702// - something simple that degenerates into the last bullet
1703//
1704void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
1705{
qining75d1d802016-04-06 14:42:01 -04001706 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
Roy05a5b532020-01-03 16:21:34 +08001707 if (symbol->getType().isStruct())
1708 glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId();
1709
qining75d1d802016-04-06 14:42:01 -04001710 if (symbol->getType().getQualifier().isSpecConstant())
1711 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1712
John Kessenich140f3df2015-06-26 16:58:36 -06001713 // getSymbolId() will set up all the IO decorations on the first call.
1714 // Formal function parameters were mapped during makeFunctions().
1715 spv::Id id = getSymbolId(symbol);
John Kessenich7ba63412015-12-20 17:37:07 -07001716
John Kessenich7ba63412015-12-20 17:37:07 -07001717 if (builder.isPointer(id)) {
John Kessenichc30d3352020-06-10 07:15:24 -06001718 if (!symbol->getType().getQualifier().isParamInput() &&
1719 !symbol->getType().getQualifier().isParamOutput()) {
1720 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
1721 // Consider adding to the OpEntryPoint interface list.
1722 // Only looking at structures if they have at least one member.
1723 if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) {
1724 spv::StorageClass sc = builder.getStorageClass(id);
1725 // Before SPIR-V 1.4, we only want to include Input and Output.
1726 // Starting with SPIR-V 1.4, we want all globals.
John Kessenich4e13c902020-07-13 00:35:58 -06001727 if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) ||
John Kessenichc30d3352020-06-10 07:15:24 -06001728 (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) {
1729 iOSet.insert(id);
1730 }
John Kessenich7c7731e2019-01-04 16:47:06 +07001731 }
John Kessenich5f77d862017-09-19 11:09:59 -06001732 }
John Kessenich9c14f772019-06-17 08:38:35 -06001733
Daniel Kochdb32b242020-03-17 20:42:47 -04001734 // If the SPIR-V type is required to be different than the AST type
1735 // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices),
John Kessenich9c14f772019-06-17 08:38:35 -06001736 // translate now from the SPIR-V type to the AST type, for the consuming
1737 // operation.
1738 // Note this turns it from an l-value to an r-value.
1739 // Currently, all symbols needing this are inputs; avoid the map lookup when non-input.
1740 if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn)
1741 id = translateForcedType(id);
John Kessenich7ba63412015-12-20 17:37:07 -07001742 }
1743
1744 // Only process non-linkage-only nodes for generating actual static uses
John Kessenich6c292d32016-02-15 20:58:50 -07001745 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001746 // Prepare to generate code for the access
1747
1748 // L-value chains will be computed left to right. We're on the symbol now,
1749 // which is the left-most part of the access chain, so now is "clear" time,
1750 // followed by setting the base.
1751 builder.clearAccessChain();
1752
1753 // For now, we consider all user variables as being in memory, so they are pointers,
John Kessenich6c292d32016-02-15 20:58:50 -07001754 // except for
John Kessenich4bf71552016-09-02 11:20:21 -06001755 // A) R-Value arguments to a function, which are an intermediate object.
John Kessenich6c292d32016-02-15 20:58:50 -07001756 // See comments in handleUserFunctionCall().
John Kessenich4bf71552016-09-02 11:20:21 -06001757 // B) Specialization constants (normal constants don't even come in as a variable),
John Kessenich6c292d32016-02-15 20:58:50 -07001758 // These are also pure R-values.
John Kessenich9c14f772019-06-17 08:38:35 -06001759 // C) R-Values from type translation, see above call to translateForcedType()
John Kessenich6c292d32016-02-15 20:58:50 -07001760 glslang::TQualifier qualifier = symbol->getQualifier();
John Kessenich9c14f772019-06-17 08:38:35 -06001761 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() ||
1762 !builder.isPointerType(builder.getTypeId(id)))
John Kessenich140f3df2015-06-26 16:58:36 -06001763 builder.setAccessChainRValue(id);
1764 else
1765 builder.setAccessChainLValue(id);
1766 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001767
John Kessenichb9197c82019-08-11 07:41:45 -06001768#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001769 // Process linkage-only nodes for any special additional interface work.
1770 if (linkageOnly) {
1771 if (glslangIntermediate->getHlslFunctionality1()) {
1772 // Map implicit counter buffers to their originating buffers, which should have been
1773 // seen by now, given earlier pruning of unused counters, and preservation of order
1774 // of declaration.
1775 if (symbol->getType().getQualifier().isUniformOrBuffer()) {
1776 if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) {
1777 // Save possible originating buffers for counter buffers, keyed by
1778 // making the potential counter-buffer name.
1779 std::string keyName = symbol->getName().c_str();
1780 keyName = glslangIntermediate->addCounterBufferName(keyName);
1781 counterOriginator[keyName] = symbol;
1782 } else {
1783 // Handle a counter buffer, by finding the saved originating buffer.
1784 std::string keyName = symbol->getName().c_str();
1785 auto it = counterOriginator.find(keyName);
1786 if (it != counterOriginator.end()) {
1787 id = getSymbolId(it->second);
1788 if (id != spv::NoResult) {
1789 spv::Id counterId = getSymbolId(symbol);
John Kessenichf52b6382018-04-05 19:35:38 -06001790 if (counterId != spv::NoResult) {
1791 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
John Kessenich5d610ee2018-03-07 18:05:55 -07001792 builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId);
John Kessenichf52b6382018-04-05 19:35:38 -06001793 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001794 }
1795 }
1796 }
1797 }
1798 }
1799 }
John Kessenich155d3512019-08-08 23:29:20 -06001800#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001801}
1802
1803bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1804{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07001805 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Roy05a5b532020-01-03 16:21:34 +08001806 if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) {
1807 glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId();
1808 }
1809 if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) {
1810 glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId();
1811 }
John Kesseniche485c7a2017-05-31 18:50:53 -06001812
qining40887662016-04-03 22:20:42 -04001813 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1814 if (node->getType().getQualifier().isSpecConstant())
1815 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1816
John Kessenich140f3df2015-06-26 16:58:36 -06001817 // First, handle special cases
1818 switch (node->getOp()) {
1819 case glslang::EOpAssign:
1820 case glslang::EOpAddAssign:
1821 case glslang::EOpSubAssign:
1822 case glslang::EOpMulAssign:
1823 case glslang::EOpVectorTimesMatrixAssign:
1824 case glslang::EOpVectorTimesScalarAssign:
1825 case glslang::EOpMatrixTimesScalarAssign:
1826 case glslang::EOpMatrixTimesMatrixAssign:
1827 case glslang::EOpDivAssign:
1828 case glslang::EOpModAssign:
1829 case glslang::EOpAndAssign:
1830 case glslang::EOpInclusiveOrAssign:
1831 case glslang::EOpExclusiveOrAssign:
1832 case glslang::EOpLeftShiftAssign:
1833 case glslang::EOpRightShiftAssign:
1834 // A bin-op assign "a += b" means the same thing as "a = a + b"
1835 // where a is evaluated before b. For a simple assignment, GLSL
1836 // says to evaluate the left before the right. So, always, left
1837 // node then right node.
1838 {
1839 // get the left l-value, save it away
1840 builder.clearAccessChain();
1841 node->getLeft()->traverse(this);
1842 spv::Builder::AccessChain lValue = builder.getAccessChain();
1843
1844 // evaluate the right
1845 builder.clearAccessChain();
1846 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001847 spv::Id rValue = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001848
1849 if (node->getOp() != glslang::EOpAssign) {
1850 // the left is also an r-value
1851 builder.setAccessChain(lValue);
John Kessenich32cfd492016-02-02 12:37:46 -07001852 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001853
1854 // do the operation
John Kessenichead86222018-03-28 18:01:20 -06001855 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001856 TranslateNoContractionDecoration(node->getType().getQualifier()),
1857 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06001858 rValue = createBinaryOperation(node->getOp(), decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06001859 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1860 node->getType().getBasicType());
1861
1862 // these all need their counterparts in createBinaryOperation()
John Kessenich55e7d112015-11-15 21:33:39 -07001863 assert(rValue != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001864 }
1865
1866 // store the result
1867 builder.setAccessChain(lValue);
Jeff Bolz36831c92018-09-05 10:11:41 -05001868 multiTypeStore(node->getLeft()->getType(), rValue);
John Kessenich140f3df2015-06-26 16:58:36 -06001869
1870 // assignments are expressions having an rValue after they are evaluated...
1871 builder.clearAccessChain();
1872 builder.setAccessChainRValue(rValue);
1873 }
1874 return false;
1875 case glslang::EOpIndexDirect:
1876 case glslang::EOpIndexDirectStruct:
1877 {
John Kessenich61a5ce12019-02-07 08:04:12 -07001878 // Structure, array, matrix, or vector indirection with statically known index.
John Kessenich140f3df2015-06-26 16:58:36 -06001879 // Get the left part of the access chain.
1880 node->getLeft()->traverse(this);
1881
1882 // Add the next element in the chain
1883
David Netoa901ffe2016-06-08 14:11:40 +01001884 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
John Kessenich140f3df2015-06-26 16:58:36 -06001885 if (! node->getLeft()->getType().isArray() &&
1886 node->getLeft()->getType().isVector() &&
1887 node->getOp() == glslang::EOpIndexDirect) {
1888 // This is essentially a hard-coded vector swizzle of size 1,
1889 // so short circuit the access-chain stuff with a swizzle.
1890 std::vector<unsigned> swizzle;
David Netoa901ffe2016-06-08 14:11:40 +01001891 swizzle.push_back(glslangIndex);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001892 int dummySize;
1893 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1894 TranslateCoherent(node->getLeft()->getType()),
John Kessenich8985fc92020-03-03 10:25:07 -07001895 glslangIntermediate->getBaseAlignmentScalar(
1896 node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001897 } else {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001898
1899 // Load through a block reference is performed with a dot operator that
1900 // is mapped to EOpIndexDirectStruct. When we get to the actual reference,
1901 // do a load and reset the access chain.
John Kessenich7015bd62019-08-01 03:28:08 -06001902 if (node->getLeft()->isReference() &&
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001903 !node->getLeft()->getType().isArray() &&
1904 node->getOp() == glslang::EOpIndexDirectStruct)
1905 {
1906 spv::Id left = accessChainLoad(node->getLeft()->getType());
1907 builder.clearAccessChain();
1908 builder.setAccessChainLValue(left);
1909 }
1910
David Netoa901ffe2016-06-08 14:11:40 +01001911 int spvIndex = glslangIndex;
1912 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1913 node->getOp() == glslang::EOpIndexDirectStruct)
1914 {
1915 // This may be, e.g., an anonymous block-member selection, which generally need
1916 // index remapping due to hidden members in anonymous blocks.
Roy05a5b532020-01-03 16:21:34 +08001917 int glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()];
1918 if (memberRemapper.find(glslangId) != memberRemapper.end()) {
1919 std::vector<int>& remapper = memberRemapper[glslangId];
1920 assert(remapper.size() > 0);
1921 spvIndex = remapper[glslangIndex];
1922 }
David Netoa901ffe2016-06-08 14:11:40 +01001923 }
John Kessenichebb50532016-05-16 19:22:05 -06001924
David Netoa901ffe2016-06-08 14:11:40 +01001925 // normal case for indexing array or structure or block
John Kessenich8985fc92020-03-03 10:25:07 -07001926 builder.accessChainPush(builder.makeIntConstant(spvIndex),
1927 TranslateCoherent(node->getLeft()->getType()),
1928 node->getLeft()->getType().getBufferReferenceAlignment());
David Netoa901ffe2016-06-08 14:11:40 +01001929
1930 // Add capabilities here for accessing PointSize and clip/cull distance.
1931 // We have deferred generation of associated capabilities until now.
John Kessenichebb50532016-05-16 19:22:05 -06001932 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
David Netoa901ffe2016-06-08 14:11:40 +01001933 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
John Kessenich140f3df2015-06-26 16:58:36 -06001934 }
1935 }
1936 return false;
1937 case glslang::EOpIndexIndirect:
1938 {
John Kessenich61a5ce12019-02-07 08:04:12 -07001939 // Array, matrix, or vector indirection with variable index.
1940 // Will use native SPIR-V access-chain for and array indirection;
John Kessenich140f3df2015-06-26 16:58:36 -06001941 // matrices are arrays of vectors, so will also work for a matrix.
1942 // Will use the access chain's 'component' for variable index into a vector.
1943
1944 // This adapter is building access chains left to right.
1945 // Set up the access chain to the left.
1946 node->getLeft()->traverse(this);
1947
1948 // save it so that computing the right side doesn't trash it
1949 spv::Builder::AccessChain partial = builder.getAccessChain();
1950
1951 // compute the next index in the chain
1952 builder.clearAccessChain();
1953 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001954 spv::Id index = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001955
John Kessenich5611c6d2018-04-05 11:25:02 -06001956 addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType());
1957
John Kessenich140f3df2015-06-26 16:58:36 -06001958 // restore the saved access chain
1959 builder.setAccessChain(partial);
1960
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001961 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) {
1962 int dummySize;
1963 builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()),
1964 TranslateCoherent(node->getLeft()->getType()),
John Kessenich8985fc92020-03-03 10:25:07 -07001965 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(),
1966 dummySize));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001967 } else
John Kessenich8985fc92020-03-03 10:25:07 -07001968 builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()),
1969 node->getLeft()->getType().getBufferReferenceAlignment());
John Kessenich140f3df2015-06-26 16:58:36 -06001970 }
1971 return false;
1972 case glslang::EOpVectorSwizzle:
1973 {
1974 node->getLeft()->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06001975 std::vector<unsigned> swizzle;
John Kessenich8c8505c2016-07-26 12:50:38 -06001976 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001977 int dummySize;
1978 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1979 TranslateCoherent(node->getLeft()->getType()),
John Kessenich8985fc92020-03-03 10:25:07 -07001980 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(),
1981 dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001982 }
1983 return false;
John Kessenichfdf63472017-01-13 12:27:52 -07001984 case glslang::EOpMatrixSwizzle:
1985 logger->missingFunctionality("matrix swizzle");
1986 return true;
John Kessenich7c1aa102015-10-15 13:29:11 -06001987 case glslang::EOpLogicalOr:
1988 case glslang::EOpLogicalAnd:
1989 {
1990
1991 // These may require short circuiting, but can sometimes be done as straight
1992 // binary operations. The right operand must be short circuited if it has
1993 // side effects, and should probably be if it is complex.
1994 if (isTrivial(node->getRight()->getAsTyped()))
1995 break; // handle below as a normal binary operation
1996 // otherwise, we need to do dynamic short circuiting on the right operand
John Kessenich8985fc92020-03-03 10:25:07 -07001997 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(),
1998 *node->getRight()->getAsTyped());
John Kessenich7c1aa102015-10-15 13:29:11 -06001999 builder.clearAccessChain();
2000 builder.setAccessChainRValue(result);
2001 }
2002 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06002003 default:
2004 break;
2005 }
2006
2007 // Assume generic binary op...
2008
John Kessenich32cfd492016-02-02 12:37:46 -07002009 // get right operand
John Kessenich140f3df2015-06-26 16:58:36 -06002010 builder.clearAccessChain();
2011 node->getLeft()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002012 spv::Id left = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002013
John Kessenich32cfd492016-02-02 12:37:46 -07002014 // get left operand
John Kessenich140f3df2015-06-26 16:58:36 -06002015 builder.clearAccessChain();
2016 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002017 spv::Id right = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002018
John Kessenich32cfd492016-02-02 12:37:46 -07002019 // get result
John Kessenichead86222018-03-28 18:01:20 -06002020 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06002021 TranslateNoContractionDecoration(node->getType().getQualifier()),
2022 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002023 spv::Id result = createBinaryOperation(node->getOp(), decorations,
John Kessenich32cfd492016-02-02 12:37:46 -07002024 convertGlslangToSpvType(node->getType()), left, right,
2025 node->getLeft()->getType().getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06002026
John Kessenich50e57562015-12-21 21:21:11 -07002027 builder.clearAccessChain();
John Kessenich140f3df2015-06-26 16:58:36 -06002028 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04002029 logger->missingFunctionality("unknown glslang binary operation");
John Kessenich50e57562015-12-21 21:21:11 -07002030 return true; // pick up a child as the place-holder result
John Kessenich140f3df2015-06-26 16:58:36 -06002031 } else {
John Kessenich140f3df2015-06-26 16:58:36 -06002032 builder.setAccessChainRValue(result);
John Kessenich140f3df2015-06-26 16:58:36 -06002033 return false;
2034 }
John Kessenich140f3df2015-06-26 16:58:36 -06002035}
2036
John Kessenich9c14f772019-06-17 08:38:35 -06002037// Figure out what, if any, type changes are needed when accessing a specific built-in.
2038// Returns <the type SPIR-V requires for declarion, the type to translate to on use>.
2039// Also see comment for 'forceType', regarding tracking SPIR-V-required types.
Daniel Kochdb32b242020-03-17 20:42:47 -04002040std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn,
John Kessenich9c14f772019-06-17 08:38:35 -06002041 const glslang::TType& glslangType)
2042{
Daniel Kochdb32b242020-03-17 20:42:47 -04002043 switch(glslangBuiltIn)
John Kessenich9c14f772019-06-17 08:38:35 -06002044 {
Daniel Kochdb32b242020-03-17 20:42:47 -04002045 case glslang::EbvSubGroupEqMask:
2046 case glslang::EbvSubGroupGeMask:
2047 case glslang::EbvSubGroupGtMask:
2048 case glslang::EbvSubGroupLeMask:
2049 case glslang::EbvSubGroupLtMask: {
John Kessenich9c14f772019-06-17 08:38:35 -06002050 // these require changing a 64-bit scaler -> a vector of 32-bit components
2051 if (glslangType.isVector())
2052 break;
2053 std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4),
2054 builder.makeUintType(64));
2055 return ret;
2056 }
Daniel Kochdb32b242020-03-17 20:42:47 -04002057 // There are no SPIR-V builtins defined for these and map onto original non-transposed
2058 // builtins. During visitBinary we insert a transpose
2059 case glslang::EbvWorldToObject3x4:
2060 case glslang::EbvObjectToWorld3x4: {
John Kessenichf80ef742020-07-14 01:44:35 -06002061 spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
2062 spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4);
2063 std::pair<spv::Id, spv::Id> ret(mat43, mat34);
Daniel Kochdb32b242020-03-17 20:42:47 -04002064 return ret;
2065 }
John Kessenich9c14f772019-06-17 08:38:35 -06002066 default:
2067 break;
2068 }
2069
2070 std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType);
2071 return ret;
2072}
2073
2074// For an object previously identified (see getForcedType() and forceType)
2075// as needing type translations, do the translation needed for a load, turning
2076// an L-value into in R-value.
2077spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object)
2078{
2079 const auto forceIt = forceType.find(object);
2080 if (forceIt == forceType.end())
2081 return object;
2082
2083 spv::Id desiredTypeId = forceIt->second;
2084 spv::Id objectTypeId = builder.getTypeId(object);
2085 assert(builder.isPointerType(objectTypeId));
2086 objectTypeId = builder.getContainedTypeId(objectTypeId);
2087 if (builder.isVectorType(objectTypeId) &&
2088 builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) {
2089 if (builder.getScalarTypeWidth(desiredTypeId) == 64) {
2090 // handle 32-bit v.xy* -> 64-bit
2091 builder.clearAccessChain();
2092 builder.setAccessChainLValue(object);
2093 object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId);
2094 std::vector<spv::Id> components;
2095 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0));
2096 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1));
2097
2098 spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2);
2099 return builder.createUnaryOp(spv::OpBitcast, desiredTypeId,
2100 builder.createCompositeConstruct(vecType, components));
2101 } else {
2102 logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar");
2103 }
Daniel Kochdb32b242020-03-17 20:42:47 -04002104 } else if (builder.isMatrixType(objectTypeId)) {
2105 // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject
2106 // and we insert a transpose after loading the original non-transposed builtins
2107 builder.clearAccessChain();
2108 builder.setAccessChainLValue(object);
2109 object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId);
2110 return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object);
2111
2112 } else {
John Kessenich9c14f772019-06-17 08:38:35 -06002113 logger->missingFunctionality("forcing non 32-bit vector type");
2114 }
2115
2116 return object;
2117}
2118
John Kessenich140f3df2015-06-26 16:58:36 -06002119bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
2120{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002121 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002122
qining40887662016-04-03 22:20:42 -04002123 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2124 if (node->getType().getQualifier().isSpecConstant())
2125 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2126
John Kessenichfc51d282015-08-19 13:34:18 -06002127 spv::Id result = spv::NoResult;
2128
2129 // try texturing first
2130 result = createImageTextureFunctionCall(node);
2131 if (result != spv::NoResult) {
2132 builder.clearAccessChain();
2133 builder.setAccessChainRValue(result);
2134
2135 return false; // done with this node
2136 }
2137
2138 // Non-texturing.
John Kessenichc9a80832015-09-12 12:17:44 -06002139
2140 if (node->getOp() == glslang::EOpArrayLength) {
2141 // Quite special; won't want to evaluate the operand.
2142
John Kessenich5611c6d2018-04-05 11:25:02 -06002143 // Currently, the front-end does not allow .length() on an array until it is sized,
2144 // except for the last block membeor of an SSBO.
2145 // TODO: If this changes, link-time sized arrays might show up here, and need their
2146 // size extracted.
2147
John Kessenichc9a80832015-09-12 12:17:44 -06002148 // Normal .length() would have been constant folded by the front-end.
2149 // So, this has to be block.lastMember.length().
John Kessenichee21fc92015-09-21 21:50:29 -06002150 // SPV wants "block" and member number as the operands, go get them.
John Kessenichead86222018-03-28 18:01:20 -06002151
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002152 spv::Id length;
2153 if (node->getOperand()->getType().isCoopMat()) {
2154 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2155
2156 spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType());
2157 assert(builder.isCooperativeMatrixType(typeId));
2158
2159 length = builder.createCooperativeMatrixLength(typeId);
2160 } else {
2161 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
2162 block->traverse(this);
John Kessenich8985fc92020-03-03 10:25:07 -07002163 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()
2164 ->getConstArray()[0].getUConst();
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002165 length = builder.createArrayLength(builder.accessChainGetLValue(), member);
2166 }
John Kessenichc9a80832015-09-12 12:17:44 -06002167
John Kessenich8c869672018-11-28 07:01:37 -07002168 // GLSL semantics say the result of .length() is an int, while SPIR-V says
2169 // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's
2170 // AST expectation of a signed result.
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002171 if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) {
2172 if (builder.isInSpecConstCodeGenMode()) {
2173 length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0));
2174 } else {
2175 length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length);
2176 }
2177 }
John Kessenich8c869672018-11-28 07:01:37 -07002178
John Kessenichc9a80832015-09-12 12:17:44 -06002179 builder.clearAccessChain();
2180 builder.setAccessChainRValue(length);
2181
2182 return false;
2183 }
2184
John Kessenichfc51d282015-08-19 13:34:18 -06002185 // Start by evaluating the operand
2186
John Kessenich8c8505c2016-07-26 12:50:38 -06002187 // Does it need a swizzle inversion? If so, evaluation is inverted;
2188 // operate first on the swizzle base, then apply the swizzle.
2189 spv::Id invertedType = spv::NoType;
John Kessenich8985fc92020-03-03 10:25:07 -07002190 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ?
2191 invertedType : convertGlslangToSpvType(node->getType()); };
John Kessenich8c8505c2016-07-26 12:50:38 -06002192 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
2193 invertedType = getInvertedSwizzleType(*node->getOperand());
2194
John Kessenich140f3df2015-06-26 16:58:36 -06002195 builder.clearAccessChain();
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002196 TIntermNode *operandNode;
John Kessenich8c8505c2016-07-26 12:50:38 -06002197 if (invertedType != spv::NoType)
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002198 operandNode = node->getOperand()->getAsBinaryNode()->getLeft();
John Kessenich8c8505c2016-07-26 12:50:38 -06002199 else
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002200 operandNode = node->getOperand();
2201
2202 operandNode->traverse(this);
Rex Xu30f92582015-09-14 10:38:56 +08002203
Rex Xufc618912015-09-09 16:42:49 +08002204 spv::Id operand = spv::NoResult;
2205
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002206 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2207
John Kessenichfb4f2332019-08-09 03:49:15 -06002208#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08002209 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
2210 node->getOp() == glslang::EOpAtomicCounterDecrement ||
Rex Xu7a26c172015-12-08 17:12:09 +08002211 node->getOp() == glslang::EOpAtomicCounter ||
Neslisah Torosdagli7d37a682020-03-26 10:52:33 -04002212 node->getOp() == glslang::EOpInterpolateAtCentroid ||
2213 node->getOp() == glslang::EOpRayQueryProceed ||
2214 node->getOp() == glslang::EOpRayQueryGetRayTMin ||
2215 node->getOp() == glslang::EOpRayQueryGetRayFlags ||
2216 node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin ||
2217 node->getOp() == glslang::EOpRayQueryGetWorldRayDirection ||
2218 node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque ||
2219 node->getOp() == glslang::EOpRayQueryTerminate ||
2220 node->getOp() == glslang::EOpRayQueryConfirmIntersection) {
Rex Xufc618912015-09-09 16:42:49 +08002221 operand = builder.accessChainGetLValue(); // Special case l-value operands
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002222 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
2223 lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType());
2224 } else
John Kessenichfb4f2332019-08-09 03:49:15 -06002225#endif
2226 {
John Kessenich32cfd492016-02-02 12:37:46 -07002227 operand = accessChainLoad(node->getOperand()->getType());
John Kessenichfb4f2332019-08-09 03:49:15 -06002228 }
John Kessenich140f3df2015-06-26 16:58:36 -06002229
John Kessenichead86222018-03-28 18:01:20 -06002230 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06002231 TranslateNoContractionDecoration(node->getType().getQualifier()),
2232 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenich140f3df2015-06-26 16:58:36 -06002233
2234 // it could be a conversion
John Kessenichfc51d282015-08-19 13:34:18 -06002235 if (! result)
John Kessenich8985fc92020-03-03 10:25:07 -07002236 result = createConversion(node->getOp(), decorations, resultType(), operand,
2237 node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06002238
2239 // if not, then possibly an operation
2240 if (! result)
John Kessenich8985fc92020-03-03 10:25:07 -07002241 result = createUnaryOperation(node->getOp(), decorations, resultType(), operand,
2242 node->getOperand()->getBasicType(), lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002243
2244 if (result) {
John Kessenich5611c6d2018-04-05 11:25:02 -06002245 if (invertedType) {
John Kessenichead86222018-03-28 18:01:20 -06002246 result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result);
John Kessenichb9197c82019-08-11 07:41:45 -06002247 decorations.addNonUniform(builder, result);
John Kessenich5611c6d2018-04-05 11:25:02 -06002248 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002249
John Kessenich140f3df2015-06-26 16:58:36 -06002250 builder.clearAccessChain();
2251 builder.setAccessChainRValue(result);
2252
2253 return false; // done with this node
2254 }
2255
2256 // it must be a special case, check...
2257 switch (node->getOp()) {
2258 case glslang::EOpPostIncrement:
2259 case glslang::EOpPostDecrement:
2260 case glslang::EOpPreIncrement:
2261 case glslang::EOpPreDecrement:
2262 {
2263 // we need the integer value "1" or the floating point "1.0" to add/subtract
Rex Xu8ff43de2016-04-22 16:51:45 +08002264 spv::Id one = 0;
2265 if (node->getBasicType() == glslang::EbtFloat)
2266 one = builder.makeFloatConstant(1.0F);
John Kessenich39697cd2019-08-08 10:35:51 -06002267#ifndef GLSLANG_WEB
Rex Xuce31aea2016-07-29 16:13:04 +08002268 else if (node->getBasicType() == glslang::EbtDouble)
2269 one = builder.makeDoubleConstant(1.0);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002270 else if (node->getBasicType() == glslang::EbtFloat16)
2271 one = builder.makeFloat16Constant(1.0F);
John Kessenich66011cb2018-03-06 16:12:04 -07002272 else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8)
2273 one = builder.makeInt8Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08002274 else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16)
2275 one = builder.makeInt16Constant(1);
John Kessenich66011cb2018-03-06 16:12:04 -07002276 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
2277 one = builder.makeInt64Constant(1);
John Kessenich39697cd2019-08-08 10:35:51 -06002278#endif
Rex Xu8ff43de2016-04-22 16:51:45 +08002279 else
2280 one = builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06002281 glslang::TOperator op;
2282 if (node->getOp() == glslang::EOpPreIncrement ||
2283 node->getOp() == glslang::EOpPostIncrement)
2284 op = glslang::EOpAdd;
2285 else
2286 op = glslang::EOpSub;
2287
John Kessenichead86222018-03-28 18:01:20 -06002288 spv::Id result = createBinaryOperation(op, decorations,
Rex Xu8ff43de2016-04-22 16:51:45 +08002289 convertGlslangToSpvType(node->getType()), operand, one,
2290 node->getType().getBasicType());
John Kessenich55e7d112015-11-15 21:33:39 -07002291 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002292
2293 // The result of operation is always stored, but conditionally the
2294 // consumed result. The consumed result is always an r-value.
2295 builder.accessChainStore(result);
2296 builder.clearAccessChain();
2297 if (node->getOp() == glslang::EOpPreIncrement ||
2298 node->getOp() == glslang::EOpPreDecrement)
2299 builder.setAccessChainRValue(result);
2300 else
2301 builder.setAccessChainRValue(operand);
2302 }
2303
2304 return false;
2305
John Kessenich155d3512019-08-08 23:29:20 -06002306#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06002307 case glslang::EOpEmitStreamVertex:
2308 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
2309 return false;
2310 case glslang::EOpEndStreamPrimitive:
2311 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
2312 return false;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002313 case glslang::EOpRayQueryTerminate:
2314 builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand);
2315 return false;
2316 case glslang::EOpRayQueryConfirmIntersection:
2317 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand);
2318 return false;
John Kessenich155d3512019-08-08 23:29:20 -06002319#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002320
2321 default:
Lei Zhang17535f72016-05-04 15:55:59 -04002322 logger->missingFunctionality("unknown glslang unary");
John Kessenich50e57562015-12-21 21:21:11 -07002323 return true; // pick up operand as placeholder result
John Kessenich140f3df2015-06-26 16:58:36 -06002324 }
John Kessenich140f3df2015-06-26 16:58:36 -06002325}
2326
Jeff Bolz53134492019-06-25 13:31:10 -05002327// Construct a composite object, recursively copying members if their types don't match
2328spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents)
2329{
2330 for (int c = 0; c < (int)constituents.size(); ++c) {
2331 spv::Id& constituent = constituents[c];
2332 spv::Id lType = builder.getContainedTypeId(resultTypeId, c);
2333 spv::Id rType = builder.getTypeId(constituent);
2334 if (lType != rType) {
2335 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
2336 constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent);
2337 } else if (builder.isStructType(rType)) {
2338 std::vector<spv::Id> rTypeConstituents;
2339 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2340 for (int i = 0; i < numrTypeConstituents; ++i) {
John Kessenich8985fc92020-03-03 10:25:07 -07002341 rTypeConstituents.push_back(builder.createCompositeExtract(constituent,
2342 builder.getContainedTypeId(rType, i), i));
Jeff Bolz53134492019-06-25 13:31:10 -05002343 }
2344 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2345 } else {
2346 assert(builder.isArrayType(rType));
2347 std::vector<spv::Id> rTypeConstituents;
2348 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2349
2350 spv::Id elementRType = builder.getContainedTypeId(rType);
2351 for (int i = 0; i < numrTypeConstituents; ++i) {
2352 rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i));
2353 }
2354 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2355 }
2356 }
2357 }
2358 return builder.createCompositeConstruct(resultTypeId, constituents);
2359}
2360
John Kessenich140f3df2015-06-26 16:58:36 -06002361bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
2362{
qining27e04a02016-04-14 16:40:20 -04002363 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2364 if (node->getType().getQualifier().isSpecConstant())
2365 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2366
John Kessenichfc51d282015-08-19 13:34:18 -06002367 spv::Id result = spv::NoResult;
Cody Northrop4d2298b2020-04-13 21:59:49 -06002368 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
2369 std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for
2370 // SPIR-V, for an out parameter
2371 std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues
John Kessenichbbbd9a22020-03-03 07:21:37 -07002372
John Kessenich8985fc92020-03-03 10:25:07 -07002373 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ?
2374 invertedType :
2375 convertGlslangToSpvType(node->getType()); };
John Kessenichfc51d282015-08-19 13:34:18 -06002376
2377 // try texturing
2378 result = createImageTextureFunctionCall(node);
2379 if (result != spv::NoResult) {
2380 builder.clearAccessChain();
2381 builder.setAccessChainRValue(result);
2382
2383 return false;
John Kessenicha28f7a72019-08-06 07:00:58 -06002384 }
2385#ifndef GLSLANG_WEB
2386 else if (node->getOp() == glslang::EOpImageStore ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002387 node->getOp() == glslang::EOpImageStoreLod ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002388 node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufc618912015-09-09 16:42:49 +08002389 // "imageStore" is a special case, which has no result
2390 return false;
2391 }
John Kessenicha28f7a72019-08-06 07:00:58 -06002392#endif
John Kessenichfc51d282015-08-19 13:34:18 -06002393
John Kessenich140f3df2015-06-26 16:58:36 -06002394 glslang::TOperator binOp = glslang::EOpNull;
2395 bool reduceComparison = true;
2396 bool isMatrix = false;
2397 bool noReturnValue = false;
John Kessenich426394d2015-07-23 10:22:48 -06002398 bool atomic = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002399
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002400 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2401
John Kessenich140f3df2015-06-26 16:58:36 -06002402 assert(node->getOp());
2403
John Kessenichf6640762016-08-01 19:44:00 -06002404 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenich140f3df2015-06-26 16:58:36 -06002405
2406 switch (node->getOp()) {
2407 case glslang::EOpSequence:
2408 {
2409 if (preVisit)
2410 ++sequenceDepth;
2411 else
2412 --sequenceDepth;
2413
2414 if (sequenceDepth == 1) {
2415 // If this is the parent node of all the functions, we want to see them
2416 // early, so all call points have actual SPIR-V functions to reference.
2417 // In all cases, still let the traverser visit the children for us.
2418 makeFunctions(node->getAsAggregate()->getSequence());
2419
John Kessenich6fccb3c2016-09-19 16:01:41 -06002420 // Also, we want all globals initializers to go into the beginning of the entry point, before
John Kessenich140f3df2015-06-26 16:58:36 -06002421 // anything else gets there, so visit out of order, doing them all now.
2422 makeGlobalInitializers(node->getAsAggregate()->getSequence());
2423
John Kessenich6a60c2f2016-12-08 21:01:59 -07002424 // 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 -06002425 // so do them manually.
2426 visitFunctions(node->getAsAggregate()->getSequence());
2427
2428 return false;
2429 }
2430
2431 return true;
2432 }
2433 case glslang::EOpLinkerObjects:
2434 {
2435 if (visit == glslang::EvPreVisit)
2436 linkageOnly = true;
2437 else
2438 linkageOnly = false;
2439
2440 return true;
2441 }
2442 case glslang::EOpComma:
2443 {
2444 // processing from left to right naturally leaves the right-most
2445 // lying around in the access chain
2446 glslang::TIntermSequence& glslangOperands = node->getSequence();
2447 for (int i = 0; i < (int)glslangOperands.size(); ++i)
2448 glslangOperands[i]->traverse(this);
2449
2450 return false;
2451 }
2452 case glslang::EOpFunction:
2453 if (visit == glslang::EvPreVisit) {
John Kessenich6fccb3c2016-09-19 16:01:41 -06002454 if (isShaderEntryPoint(node)) {
John Kessenich517fe7a2016-11-26 13:31:47 -07002455 inEntryPoint = true;
John Kessenich140f3df2015-06-26 16:58:36 -06002456 builder.setBuildPoint(shaderEntry->getLastBlock());
John Kesseniched33e052016-10-06 12:59:51 -06002457 currentFunction = shaderEntry;
John Kessenich140f3df2015-06-26 16:58:36 -06002458 } else {
2459 handleFunctionEntry(node);
2460 }
2461 } else {
John Kessenich517fe7a2016-11-26 13:31:47 -07002462 if (inEntryPoint)
2463 entryPointTerminated = true;
John Kesseniche770b3e2015-09-14 20:58:02 -06002464 builder.leaveFunction();
John Kessenich517fe7a2016-11-26 13:31:47 -07002465 inEntryPoint = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002466 }
2467
2468 return true;
2469 case glslang::EOpParameters:
2470 // Parameters will have been consumed by EOpFunction processing, but not
2471 // the body, so we still visited the function node's children, making this
2472 // child redundant.
2473 return false;
2474 case glslang::EOpFunctionCall:
2475 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002476 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002477 if (node->isUserDefined())
2478 result = handleUserFunctionCall(node);
John Kessenich6c292d32016-02-15 20:58:50 -07002479 if (result) {
2480 builder.clearAccessChain();
2481 builder.setAccessChainRValue(result);
2482 } else
Lei Zhang17535f72016-05-04 15:55:59 -04002483 logger->missingFunctionality("missing user function; linker needs to catch that");
John Kessenich140f3df2015-06-26 16:58:36 -06002484
2485 return false;
2486 }
2487 case glslang::EOpConstructMat2x2:
2488 case glslang::EOpConstructMat2x3:
2489 case glslang::EOpConstructMat2x4:
2490 case glslang::EOpConstructMat3x2:
2491 case glslang::EOpConstructMat3x3:
2492 case glslang::EOpConstructMat3x4:
2493 case glslang::EOpConstructMat4x2:
2494 case glslang::EOpConstructMat4x3:
2495 case glslang::EOpConstructMat4x4:
2496 case glslang::EOpConstructDMat2x2:
2497 case glslang::EOpConstructDMat2x3:
2498 case glslang::EOpConstructDMat2x4:
2499 case glslang::EOpConstructDMat3x2:
2500 case glslang::EOpConstructDMat3x3:
2501 case glslang::EOpConstructDMat3x4:
2502 case glslang::EOpConstructDMat4x2:
2503 case glslang::EOpConstructDMat4x3:
2504 case glslang::EOpConstructDMat4x4:
LoopDawg174ccb82017-05-20 21:40:27 -06002505 case glslang::EOpConstructIMat2x2:
2506 case glslang::EOpConstructIMat2x3:
2507 case glslang::EOpConstructIMat2x4:
2508 case glslang::EOpConstructIMat3x2:
2509 case glslang::EOpConstructIMat3x3:
2510 case glslang::EOpConstructIMat3x4:
2511 case glslang::EOpConstructIMat4x2:
2512 case glslang::EOpConstructIMat4x3:
2513 case glslang::EOpConstructIMat4x4:
2514 case glslang::EOpConstructUMat2x2:
2515 case glslang::EOpConstructUMat2x3:
2516 case glslang::EOpConstructUMat2x4:
2517 case glslang::EOpConstructUMat3x2:
2518 case glslang::EOpConstructUMat3x3:
2519 case glslang::EOpConstructUMat3x4:
2520 case glslang::EOpConstructUMat4x2:
2521 case glslang::EOpConstructUMat4x3:
2522 case glslang::EOpConstructUMat4x4:
2523 case glslang::EOpConstructBMat2x2:
2524 case glslang::EOpConstructBMat2x3:
2525 case glslang::EOpConstructBMat2x4:
2526 case glslang::EOpConstructBMat3x2:
2527 case glslang::EOpConstructBMat3x3:
2528 case glslang::EOpConstructBMat3x4:
2529 case glslang::EOpConstructBMat4x2:
2530 case glslang::EOpConstructBMat4x3:
2531 case glslang::EOpConstructBMat4x4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002532 case glslang::EOpConstructF16Mat2x2:
2533 case glslang::EOpConstructF16Mat2x3:
2534 case glslang::EOpConstructF16Mat2x4:
2535 case glslang::EOpConstructF16Mat3x2:
2536 case glslang::EOpConstructF16Mat3x3:
2537 case glslang::EOpConstructF16Mat3x4:
2538 case glslang::EOpConstructF16Mat4x2:
2539 case glslang::EOpConstructF16Mat4x3:
2540 case glslang::EOpConstructF16Mat4x4:
John Kessenich140f3df2015-06-26 16:58:36 -06002541 isMatrix = true;
2542 // fall through
2543 case glslang::EOpConstructFloat:
2544 case glslang::EOpConstructVec2:
2545 case glslang::EOpConstructVec3:
2546 case glslang::EOpConstructVec4:
2547 case glslang::EOpConstructDouble:
2548 case glslang::EOpConstructDVec2:
2549 case glslang::EOpConstructDVec3:
2550 case glslang::EOpConstructDVec4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002551 case glslang::EOpConstructFloat16:
2552 case glslang::EOpConstructF16Vec2:
2553 case glslang::EOpConstructF16Vec3:
2554 case glslang::EOpConstructF16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002555 case glslang::EOpConstructBool:
2556 case glslang::EOpConstructBVec2:
2557 case glslang::EOpConstructBVec3:
2558 case glslang::EOpConstructBVec4:
John Kessenich66011cb2018-03-06 16:12:04 -07002559 case glslang::EOpConstructInt8:
2560 case glslang::EOpConstructI8Vec2:
2561 case glslang::EOpConstructI8Vec3:
2562 case glslang::EOpConstructI8Vec4:
2563 case glslang::EOpConstructUint8:
2564 case glslang::EOpConstructU8Vec2:
2565 case glslang::EOpConstructU8Vec3:
2566 case glslang::EOpConstructU8Vec4:
2567 case glslang::EOpConstructInt16:
2568 case glslang::EOpConstructI16Vec2:
2569 case glslang::EOpConstructI16Vec3:
2570 case glslang::EOpConstructI16Vec4:
2571 case glslang::EOpConstructUint16:
2572 case glslang::EOpConstructU16Vec2:
2573 case glslang::EOpConstructU16Vec3:
2574 case glslang::EOpConstructU16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002575 case glslang::EOpConstructInt:
2576 case glslang::EOpConstructIVec2:
2577 case glslang::EOpConstructIVec3:
2578 case glslang::EOpConstructIVec4:
2579 case glslang::EOpConstructUint:
2580 case glslang::EOpConstructUVec2:
2581 case glslang::EOpConstructUVec3:
2582 case glslang::EOpConstructUVec4:
Rex Xu8ff43de2016-04-22 16:51:45 +08002583 case glslang::EOpConstructInt64:
2584 case glslang::EOpConstructI64Vec2:
2585 case glslang::EOpConstructI64Vec3:
2586 case glslang::EOpConstructI64Vec4:
2587 case glslang::EOpConstructUint64:
2588 case glslang::EOpConstructU64Vec2:
2589 case glslang::EOpConstructU64Vec3:
2590 case glslang::EOpConstructU64Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002591 case glslang::EOpConstructStruct:
John Kessenich6c292d32016-02-15 20:58:50 -07002592 case glslang::EOpConstructTextureSampler:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002593 case glslang::EOpConstructReference:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002594 case glslang::EOpConstructCooperativeMatrix:
John Kessenich140f3df2015-06-26 16:58:36 -06002595 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002596 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002597 std::vector<spv::Id> arguments;
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002598 translateArguments(*node, arguments, lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002599 spv::Id constructed;
John Kessenich6c292d32016-02-15 20:58:50 -07002600 if (node->getOp() == glslang::EOpConstructTextureSampler)
John Kessenich8c8505c2016-07-26 12:50:38 -06002601 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002602 else if (node->getOp() == glslang::EOpConstructStruct ||
2603 node->getOp() == glslang::EOpConstructCooperativeMatrix ||
2604 node->getType().isArray()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002605 std::vector<spv::Id> constituents;
2606 for (int c = 0; c < (int)arguments.size(); ++c)
2607 constituents.push_back(arguments[c]);
Jeff Bolz53134492019-06-25 13:31:10 -05002608 constructed = createCompositeConstruct(resultType(), constituents);
John Kessenich55e7d112015-11-15 21:33:39 -07002609 } else if (isMatrix)
John Kessenich8c8505c2016-07-26 12:50:38 -06002610 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
John Kessenich55e7d112015-11-15 21:33:39 -07002611 else
John Kessenich8c8505c2016-07-26 12:50:38 -06002612 constructed = builder.createConstructor(precision, arguments, resultType());
John Kessenich140f3df2015-06-26 16:58:36 -06002613
2614 builder.clearAccessChain();
2615 builder.setAccessChainRValue(constructed);
2616
2617 return false;
2618 }
2619
2620 // These six are component-wise compares with component-wise results.
2621 // Forward on to createBinaryOperation(), requesting a vector result.
2622 case glslang::EOpLessThan:
2623 case glslang::EOpGreaterThan:
2624 case glslang::EOpLessThanEqual:
2625 case glslang::EOpGreaterThanEqual:
2626 case glslang::EOpVectorEqual:
2627 case glslang::EOpVectorNotEqual:
2628 {
2629 // Map the operation to a binary
2630 binOp = node->getOp();
2631 reduceComparison = false;
2632 switch (node->getOp()) {
2633 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
2634 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
2635 default: binOp = node->getOp(); break;
2636 }
2637
2638 break;
2639 }
2640 case glslang::EOpMul:
John Kessenich8c8505c2016-07-26 12:50:38 -06002641 // component-wise matrix multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002642 binOp = glslang::EOpMul;
2643 break;
2644 case glslang::EOpOuterProduct:
2645 // two vectors multiplied to make a matrix
2646 binOp = glslang::EOpOuterProduct;
2647 break;
2648 case glslang::EOpDot:
2649 {
qining25262b32016-05-06 17:25:16 -04002650 // for scalar dot product, use multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002651 glslang::TIntermSequence& glslangOperands = node->getSequence();
John Kessenich8d72f1a2016-05-20 12:06:03 -06002652 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
John Kessenich140f3df2015-06-26 16:58:36 -06002653 binOp = glslang::EOpMul;
2654 break;
2655 }
2656 case glslang::EOpMod:
2657 // when an aggregate, this is the floating-point mod built-in function,
2658 // which can be emitted by the one in createBinaryOperation()
2659 binOp = glslang::EOpMod;
2660 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002661
John Kessenich140f3df2015-06-26 16:58:36 -06002662 case glslang::EOpEmitVertex:
2663 case glslang::EOpEndPrimitive:
2664 case glslang::EOpBarrier:
2665 case glslang::EOpMemoryBarrier:
2666 case glslang::EOpMemoryBarrierAtomicCounter:
2667 case glslang::EOpMemoryBarrierBuffer:
2668 case glslang::EOpMemoryBarrierImage:
2669 case glslang::EOpMemoryBarrierShared:
2670 case glslang::EOpGroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07002671 case glslang::EOpDeviceMemoryBarrier:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002672 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07002673 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002674 case glslang::EOpWorkgroupMemoryBarrier:
2675 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich66011cb2018-03-06 16:12:04 -07002676 case glslang::EOpSubgroupBarrier:
2677 case glslang::EOpSubgroupMemoryBarrier:
2678 case glslang::EOpSubgroupMemoryBarrierBuffer:
2679 case glslang::EOpSubgroupMemoryBarrierImage:
2680 case glslang::EOpSubgroupMemoryBarrierShared:
John Kessenich140f3df2015-06-26 16:58:36 -06002681 noReturnValue = true;
2682 // These all have 0 operands and will naturally finish up in the code below for 0 operands
2683 break;
2684
John Kessenich426394d2015-07-23 10:22:48 -06002685 case glslang::EOpAtomicAdd:
2686 case glslang::EOpAtomicMin:
2687 case glslang::EOpAtomicMax:
2688 case glslang::EOpAtomicAnd:
2689 case glslang::EOpAtomicOr:
2690 case glslang::EOpAtomicXor:
2691 case glslang::EOpAtomicExchange:
2692 case glslang::EOpAtomicCompSwap:
2693 atomic = true;
2694 break;
2695
John Kesseniche5eee8f2019-10-18 01:03:11 -06002696#ifndef GLSLANG_WEB
2697 case glslang::EOpAtomicStore:
2698 noReturnValue = true;
2699 // fallthrough
2700 case glslang::EOpAtomicLoad:
2701 atomic = true;
2702 break;
2703
John Kessenich0d0c6d32017-07-23 16:08:26 -06002704 case glslang::EOpAtomicCounterAdd:
2705 case glslang::EOpAtomicCounterSubtract:
2706 case glslang::EOpAtomicCounterMin:
2707 case glslang::EOpAtomicCounterMax:
2708 case glslang::EOpAtomicCounterAnd:
2709 case glslang::EOpAtomicCounterOr:
2710 case glslang::EOpAtomicCounterXor:
2711 case glslang::EOpAtomicCounterExchange:
2712 case glslang::EOpAtomicCounterCompSwap:
2713 builder.addExtension("SPV_KHR_shader_atomic_counter_ops");
2714 builder.addCapability(spv::CapabilityAtomicStorageOps);
2715 atomic = true;
2716 break;
2717
Ian Romanickb3bd4022019-01-21 08:57:25 -08002718 case glslang::EOpAbsDifference:
2719 case glslang::EOpAddSaturate:
2720 case glslang::EOpSubSaturate:
2721 case glslang::EOpAverage:
2722 case glslang::EOpAverageRounded:
2723 case glslang::EOpMul32x16:
2724 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
2725 builder.addExtension("SPV_INTEL_shader_integer_functions2");
2726 binOp = node->getOp();
2727 break;
2728
Daniel Kochdb32b242020-03-17 20:42:47 -04002729 case glslang::EOpIgnoreIntersection:
2730 case glslang::EOpTerminateRay:
2731 case glslang::EOpTrace:
2732 case glslang::EOpExecuteCallable:
Chao Chen3c366992018-09-19 11:41:59 -07002733 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
2734 noReturnValue = true;
2735 break;
Torosdagli06c2eee2020-03-19 11:09:57 -04002736 case glslang::EOpRayQueryInitialize:
2737 case glslang::EOpRayQueryTerminate:
2738 case glslang::EOpRayQueryGenerateIntersection:
2739 case glslang::EOpRayQueryConfirmIntersection:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002740 builder.addExtension("SPV_KHR_ray_query");
2741 builder.addCapability(spv::CapabilityRayQueryProvisionalKHR);
Torosdagli06c2eee2020-03-19 11:09:57 -04002742 noReturnValue = true;
2743 break;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002744 case glslang::EOpRayQueryProceed:
2745 case glslang::EOpRayQueryGetIntersectionType:
2746 case glslang::EOpRayQueryGetRayTMin:
2747 case glslang::EOpRayQueryGetRayFlags:
2748 case glslang::EOpRayQueryGetIntersectionT:
2749 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
2750 case glslang::EOpRayQueryGetIntersectionInstanceId:
2751 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
2752 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
2753 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
2754 case glslang::EOpRayQueryGetIntersectionBarycentrics:
2755 case glslang::EOpRayQueryGetIntersectionFrontFace:
2756 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
2757 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
2758 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
2759 case glslang::EOpRayQueryGetWorldRayDirection:
2760 case glslang::EOpRayQueryGetWorldRayOrigin:
2761 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
2762 case glslang::EOpRayQueryGetIntersectionWorldToObject:
2763 builder.addExtension("SPV_KHR_ray_query");
2764 builder.addCapability(spv::CapabilityRayQueryProvisionalKHR);
2765 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002766 case glslang::EOpCooperativeMatrixLoad:
2767 case glslang::EOpCooperativeMatrixStore:
2768 noReturnValue = true;
2769 break;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05002770 case glslang::EOpBeginInvocationInterlock:
2771 case glslang::EOpEndInvocationInterlock:
2772 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
2773 noReturnValue = true;
2774 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002775#endif
Chao Chen3c366992018-09-19 11:41:59 -07002776
Jeff Bolz04d73732019-05-31 13:06:01 -05002777 case glslang::EOpDebugPrintf:
2778 noReturnValue = true;
2779 break;
2780
John Kessenich140f3df2015-06-26 16:58:36 -06002781 default:
2782 break;
2783 }
2784
2785 //
2786 // See if it maps to a regular operation.
2787 //
John Kessenich140f3df2015-06-26 16:58:36 -06002788 if (binOp != glslang::EOpNull) {
2789 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
2790 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
2791 assert(left && right);
2792
2793 builder.clearAccessChain();
2794 left->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002795 spv::Id leftId = accessChainLoad(left->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002796
2797 builder.clearAccessChain();
2798 right->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002799 spv::Id rightId = accessChainLoad(right->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002800
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002801 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichead86222018-03-28 18:01:20 -06002802 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002803 TranslateNoContractionDecoration(node->getType().getQualifier()),
2804 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002805 result = createBinaryOperation(binOp, decorations,
John Kessenich8c8505c2016-07-26 12:50:38 -06002806 resultType(), leftId, rightId,
John Kessenich140f3df2015-06-26 16:58:36 -06002807 left->getType().getBasicType(), reduceComparison);
2808
2809 // code above should only make binOp that exists in createBinaryOperation
John Kessenich55e7d112015-11-15 21:33:39 -07002810 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002811 builder.clearAccessChain();
2812 builder.setAccessChainRValue(result);
2813
2814 return false;
2815 }
2816
John Kessenich426394d2015-07-23 10:22:48 -06002817 //
2818 // Create the list of operands.
2819 //
John Kessenich140f3df2015-06-26 16:58:36 -06002820 glslang::TIntermSequence& glslangOperands = node->getSequence();
2821 std::vector<spv::Id> operands;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002822 std::vector<spv::IdImmediate> memoryAccessOperands;
John Kessenich140f3df2015-06-26 16:58:36 -06002823 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
John Kessenich140f3df2015-06-26 16:58:36 -06002824 // special case l-value operands; there are just a few
2825 bool lvalue = false;
2826 switch (node->getOp()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002827 case glslang::EOpModf:
2828 if (arg == 1)
2829 lvalue = true;
2830 break;
John Kesseniche5eee8f2019-10-18 01:03:11 -06002831
Torosdagli06c2eee2020-03-19 11:09:57 -04002832 case glslang::EOpRayQueryInitialize:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04002833 case glslang::EOpRayQueryTerminate:
2834 case glslang::EOpRayQueryConfirmIntersection:
2835 case glslang::EOpRayQueryProceed:
Torosdagli06c2eee2020-03-19 11:09:57 -04002836 case glslang::EOpRayQueryGenerateIntersection:
2837 case glslang::EOpRayQueryGetIntersectionType:
2838 case glslang::EOpRayQueryGetIntersectionT:
2839 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
2840 case glslang::EOpRayQueryGetIntersectionInstanceId:
2841 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
2842 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
2843 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
2844 case glslang::EOpRayQueryGetIntersectionBarycentrics:
2845 case glslang::EOpRayQueryGetIntersectionFrontFace:
2846 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
2847 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
2848 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
2849 case glslang::EOpRayQueryGetIntersectionWorldToObject:
2850 if (arg == 0)
2851 lvalue = true;
2852 break;
2853
John Kesseniche5eee8f2019-10-18 01:03:11 -06002854 case glslang::EOpAtomicAdd:
2855 case glslang::EOpAtomicMin:
2856 case glslang::EOpAtomicMax:
2857 case glslang::EOpAtomicAnd:
2858 case glslang::EOpAtomicOr:
2859 case glslang::EOpAtomicXor:
2860 case glslang::EOpAtomicExchange:
2861 case glslang::EOpAtomicCompSwap:
2862 if (arg == 0)
2863 lvalue = true;
2864 break;
2865
John Kessenicha28f7a72019-08-06 07:00:58 -06002866#ifndef GLSLANG_WEB
2867 case glslang::EOpFrexp:
2868 if (arg == 1)
2869 lvalue = true;
2870 break;
Rex Xu7a26c172015-12-08 17:12:09 +08002871 case glslang::EOpInterpolateAtSample:
2872 case glslang::EOpInterpolateAtOffset:
Rex Xu9d93a232016-05-05 12:30:44 +08002873 case glslang::EOpInterpolateAtVertex:
John Kessenich8c8505c2016-07-26 12:50:38 -06002874 if (arg == 0) {
Rex Xu7a26c172015-12-08 17:12:09 +08002875 lvalue = true;
John Kessenich8c8505c2016-07-26 12:50:38 -06002876
2877 // Does it need a swizzle inversion? If so, evaluation is inverted;
2878 // operate first on the swizzle base, then apply the swizzle.
John Kessenichbbbd9a22020-03-03 07:21:37 -07002879 // That is, we transform
2880 //
2881 // interpolate(v.zy) -> interpolate(v).zy
2882 //
John Kessenichecba76f2017-01-06 00:34:48 -07002883 if (glslangOperands[0]->getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002884 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
John Kessenich8985fc92020-03-03 10:25:07 -07002885 invertedType = convertGlslangToSpvType(
2886 glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
John Kessenich8c8505c2016-07-26 12:50:38 -06002887 }
Rex Xu7a26c172015-12-08 17:12:09 +08002888 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05002889 case glslang::EOpAtomicLoad:
2890 case glslang::EOpAtomicStore:
John Kessenich0d0c6d32017-07-23 16:08:26 -06002891 case glslang::EOpAtomicCounterAdd:
2892 case glslang::EOpAtomicCounterSubtract:
2893 case glslang::EOpAtomicCounterMin:
2894 case glslang::EOpAtomicCounterMax:
2895 case glslang::EOpAtomicCounterAnd:
2896 case glslang::EOpAtomicCounterOr:
2897 case glslang::EOpAtomicCounterXor:
2898 case glslang::EOpAtomicCounterExchange:
2899 case glslang::EOpAtomicCounterCompSwap:
Rex Xud4782c12015-09-06 16:30:11 +08002900 if (arg == 0)
2901 lvalue = true;
2902 break;
John Kessenich55e7d112015-11-15 21:33:39 -07002903 case glslang::EOpAddCarry:
2904 case glslang::EOpSubBorrow:
2905 if (arg == 2)
2906 lvalue = true;
2907 break;
2908 case glslang::EOpUMulExtended:
2909 case glslang::EOpIMulExtended:
2910 if (arg >= 2)
2911 lvalue = true;
2912 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002913 case glslang::EOpCooperativeMatrixLoad:
2914 if (arg == 0 || arg == 1)
2915 lvalue = true;
2916 break;
2917 case glslang::EOpCooperativeMatrixStore:
2918 if (arg == 1)
2919 lvalue = true;
2920 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002921#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002922 default:
2923 break;
2924 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002925 builder.clearAccessChain();
2926 if (invertedType != spv::NoType && arg == 0)
2927 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
2928 else
2929 glslangOperands[arg]->traverse(this);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002930
John Kessenichb9197c82019-08-11 07:41:45 -06002931#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002932 if (node->getOp() == glslang::EOpCooperativeMatrixLoad ||
2933 node->getOp() == glslang::EOpCooperativeMatrixStore) {
2934
2935 if (arg == 1) {
2936 // fold "element" parameter into the access chain
2937 spv::Builder::AccessChain save = builder.getAccessChain();
2938 builder.clearAccessChain();
2939 glslangOperands[2]->traverse(this);
2940
2941 spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType());
2942
2943 builder.setAccessChain(save);
2944
2945 // Point to the first element of the array.
John Kessenich8985fc92020-03-03 10:25:07 -07002946 builder.accessChainPush(elementId,
2947 TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()),
2948 glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment());
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002949
2950 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
2951 unsigned int alignment = builder.getAccessChain().alignment;
2952
2953 int memoryAccess = TranslateMemoryAccess(coherentFlags);
2954 if (node->getOp() == glslang::EOpCooperativeMatrixLoad)
2955 memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask;
2956 if (node->getOp() == glslang::EOpCooperativeMatrixStore)
2957 memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask;
John Kessenich8985fc92020-03-03 10:25:07 -07002958 if (builder.getStorageClass(builder.getAccessChain().base) ==
2959 spv::StorageClassPhysicalStorageBufferEXT) {
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002960 memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2961 }
2962
2963 memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess));
2964
2965 if (memoryAccess & spv::MemoryAccessAlignedMask) {
2966 memoryAccessOperands.push_back(spv::IdImmediate(false, alignment));
2967 }
2968
John Kessenich8985fc92020-03-03 10:25:07 -07002969 if (memoryAccess &
2970 (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) {
2971 memoryAccessOperands.push_back(spv::IdImmediate(true,
2972 builder.makeUintConstant(TranslateMemoryScope(coherentFlags))));
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002973 }
2974 } else if (arg == 2) {
2975 continue;
2976 }
2977 }
John Kessenichb9197c82019-08-11 07:41:45 -06002978#endif
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002979
John Kessenichbbbd9a22020-03-03 07:21:37 -07002980 // for l-values, pass the address, for r-values, pass the value
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002981 if (lvalue) {
John Kessenichbbbd9a22020-03-03 07:21:37 -07002982 if (invertedType == spv::NoType && !builder.isSpvLvalue()) {
2983 // SPIR-V cannot represent an l-value containing a swizzle that doesn't
2984 // reduce to a simple access chain. So, we need a temporary vector to
2985 // receive the result, and must later swizzle that into the original
2986 // l-value.
Cody Northrop4d2298b2020-04-13 21:59:49 -06002987 complexLvalues.push_back(builder.getAccessChain());
John Kessenich435dd802020-06-30 01:27:08 -06002988 temporaryLvalues.push_back(builder.createVariable(
2989 spv::NoPrecision, spv::StorageClassFunction,
Cody Northrop4d2298b2020-04-13 21:59:49 -06002990 builder.accessChainGetInferredType(), "swizzleTemp"));
2991 operands.push_back(temporaryLvalues.back());
John Kessenichbbbd9a22020-03-03 07:21:37 -07002992 } else {
2993 operands.push_back(builder.accessChainGetLValue());
2994 }
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002995 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
2996 lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType());
2997 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002998 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Torosdagli06c2eee2020-03-19 11:09:57 -04002999 glslang::TOperator glslangOp = node->getOp();
3000 if (arg == 1 &&
3001 (glslangOp == glslang::EOpRayQueryGetIntersectionType ||
3002 glslangOp == glslang::EOpRayQueryGetIntersectionT ||
3003 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex ||
3004 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId ||
3005 glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset ||
3006 glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex ||
3007 glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex ||
3008 glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics ||
3009 glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace ||
3010 glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection ||
3011 glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin ||
3012 glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld ||
3013 glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject
3014 )) {
3015 bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst();
3016 operands.push_back(builder.makeIntConstant(cond ? 1 : 0));
3017 }
3018 else {
3019 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
3020 }
3021
John Kesseniche485c7a2017-05-31 18:50:53 -06003022 }
John Kessenich140f3df2015-06-26 16:58:36 -06003023 }
John Kessenich426394d2015-07-23 10:22:48 -06003024
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003025 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichb9197c82019-08-11 07:41:45 -06003026#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003027 if (node->getOp() == glslang::EOpCooperativeMatrixLoad) {
3028 std::vector<spv::IdImmediate> idImmOps;
3029
3030 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
3031 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
3032 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
3033 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
3034 // get the pointee type
3035 spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0]));
3036 assert(builder.isCooperativeMatrixType(typeId));
3037 // do the op
3038 spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps);
3039 // store the result to the pointer (out param 'm')
3040 builder.createStore(result, operands[0]);
3041 result = 0;
3042 } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) {
3043 std::vector<spv::IdImmediate> idImmOps;
3044
3045 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
3046 idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object
3047 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
3048 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
3049 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
3050
3051 builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps);
3052 result = 0;
John Kessenichb9197c82019-08-11 07:41:45 -06003053 } else
3054#endif
John Kesseniche5eee8f2019-10-18 01:03:11 -06003055 if (atomic) {
3056 // Handle all atomics
John Kessenich8985fc92020-03-03 10:25:07 -07003057 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(),
3058 lvalueCoherentFlags);
Jeff Bolz04d73732019-05-31 13:06:01 -05003059 } else if (node->getOp() == glslang::EOpDebugPrintf) {
3060 if (!nonSemanticDebugPrintf) {
3061 nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf");
3062 }
3063 result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands);
3064 builder.addExtension(spv::E_SPV_KHR_non_semantic_info);
John Kesseniche5eee8f2019-10-18 01:03:11 -06003065 } else {
John Kessenich426394d2015-07-23 10:22:48 -06003066 // Pass through to generic operations.
3067 switch (glslangOperands.size()) {
3068 case 0:
John Kessenich8c8505c2016-07-26 12:50:38 -06003069 result = createNoArgOperation(node->getOp(), precision, resultType());
John Kessenich426394d2015-07-23 10:22:48 -06003070 break;
3071 case 1:
John Kessenichead86222018-03-28 18:01:20 -06003072 {
3073 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06003074 TranslateNoContractionDecoration(node->getType().getQualifier()),
3075 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06003076 result = createUnaryOperation(
3077 node->getOp(), decorations,
3078 resultType(), operands.front(),
Jeff Bolz38a52fc2019-06-14 09:56:28 -05003079 glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -06003080 }
John Kessenich426394d2015-07-23 10:22:48 -06003081 break;
3082 default:
John Kessenich8c8505c2016-07-26 12:50:38 -06003083 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06003084 break;
3085 }
Cody Northrop4d2298b2020-04-13 21:59:49 -06003086
John Kessenichbbbd9a22020-03-03 07:21:37 -07003087 if (invertedType != spv::NoResult)
John Kessenich8c8505c2016-07-26 12:50:38 -06003088 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
Cody Northrop4d2298b2020-04-13 21:59:49 -06003089
3090 for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) {
3091 builder.setAccessChain(complexLvalues[i]);
John Kessenich435dd802020-06-30 01:27:08 -06003092 builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision));
John Kessenichbbbd9a22020-03-03 07:21:37 -07003093 }
John Kessenich140f3df2015-06-26 16:58:36 -06003094 }
3095
3096 if (noReturnValue)
3097 return false;
3098
3099 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04003100 logger->missingFunctionality("unknown glslang aggregate");
John Kessenich50e57562015-12-21 21:21:11 -07003101 return true; // pick up a child as a placeholder operand
John Kessenich140f3df2015-06-26 16:58:36 -06003102 } else {
3103 builder.clearAccessChain();
3104 builder.setAccessChainRValue(result);
3105 return false;
3106 }
3107}
3108
John Kessenich433e9ff2017-01-26 20:31:11 -07003109// This path handles both if-then-else and ?:
3110// The if-then-else has a node type of void, while
3111// ?: has either a void or a non-void node type
3112//
3113// Leaving the result, when not void:
3114// GLSL only has r-values as the result of a :?, but
3115// if we have an l-value, that can be more efficient if it will
3116// become the base of a complex r-value expression, because the
3117// next layer copies r-values into memory to use the access-chain mechanism
John Kessenich140f3df2015-06-26 16:58:36 -06003118bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
3119{
John Kessenich0c1e71a2019-01-10 18:23:06 +07003120 // see if OpSelect can handle it
3121 const auto isOpSelectable = [&]() {
3122 if (node->getBasicType() == glslang::EbtVoid)
3123 return false;
3124 // OpSelect can do all other types starting with SPV 1.4
3125 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) {
3126 // pre-1.4, only scalars and vectors can be handled
3127 if ((!node->getType().isScalar() && !node->getType().isVector()))
3128 return false;
3129 }
3130 return true;
3131 };
3132
John Kessenich4bee5312018-02-20 21:29:05 -07003133 // See if it simple and safe, or required, to execute both sides.
3134 // Crucially, side effects must be either semantically required or avoided,
3135 // and there are performance trade-offs.
3136 // Return true if required or a good idea (and safe) to execute both sides,
3137 // false otherwise.
3138 const auto bothSidesPolicy = [&]() -> bool {
3139 // do we have both sides?
John Kessenich433e9ff2017-01-26 20:31:11 -07003140 if (node->getTrueBlock() == nullptr ||
3141 node->getFalseBlock() == nullptr)
3142 return false;
3143
John Kessenich4bee5312018-02-20 21:29:05 -07003144 // required? (unless we write additional code to look for side effects
3145 // and make performance trade-offs if none are present)
3146 if (!node->getShortCircuit())
3147 return true;
3148
3149 // if not required to execute both, decide based on performance/practicality...
3150
John Kessenich0c1e71a2019-01-10 18:23:06 +07003151 if (!isOpSelectable())
John Kessenich4bee5312018-02-20 21:29:05 -07003152 return false;
3153
John Kessenich433e9ff2017-01-26 20:31:11 -07003154 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
3155 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
3156
3157 // return true if a single operand to ? : is okay for OpSelect
3158 const auto operandOkay = [](glslang::TIntermTyped* node) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07003159 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
John Kessenich433e9ff2017-01-26 20:31:11 -07003160 };
3161
3162 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
3163 operandOkay(node->getFalseBlock()->getAsTyped());
3164 };
3165
John Kessenich4bee5312018-02-20 21:29:05 -07003166 spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue
3167 // emit the condition before doing anything with selection
3168 node->getCondition()->traverse(this);
3169 spv::Id condition = accessChainLoad(node->getCondition()->getType());
3170
3171 // Find a way of executing both sides and selecting the right result.
3172 const auto executeBothSides = [&]() -> void {
3173 // execute both sides
John Kessenich433e9ff2017-01-26 20:31:11 -07003174 node->getTrueBlock()->traverse(this);
3175 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
3176 node->getFalseBlock()->traverse(this);
3177 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
3178
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003179 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06003180
John Kessenich4bee5312018-02-20 21:29:05 -07003181 // done if void
3182 if (node->getBasicType() == glslang::EbtVoid)
3183 return;
John Kesseniche434ad92017-03-30 10:09:28 -06003184
John Kessenich4bee5312018-02-20 21:29:05 -07003185 // emit code to select between trueValue and falseValue
3186
3187 // see if OpSelect can handle it
John Kessenich0c1e71a2019-01-10 18:23:06 +07003188 if (isOpSelectable()) {
John Kessenich4bee5312018-02-20 21:29:05 -07003189 // Emit OpSelect for this selection.
3190
3191 // smear condition to vector, if necessary (AST is always scalar)
John Kessenich0c1e71a2019-01-10 18:23:06 +07003192 // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar
3193 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) {
John Kessenich4bee5312018-02-20 21:29:05 -07003194 condition = builder.smearScalar(spv::NoPrecision, condition,
3195 builder.makeVectorType(builder.makeBoolType(),
3196 builder.getNumComponents(trueValue)));
John Kessenich0c1e71a2019-01-10 18:23:06 +07003197 }
John Kessenich4bee5312018-02-20 21:29:05 -07003198
3199 // OpSelect
3200 result = builder.createTriOp(spv::OpSelect,
3201 convertGlslangToSpvType(node->getType()), condition,
3202 trueValue, falseValue);
3203
3204 builder.clearAccessChain();
3205 builder.setAccessChainRValue(result);
3206 } else {
3207 // We need control flow to select the result.
3208 // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path.
John Kessenich435dd802020-06-30 01:27:08 -06003209 result = builder.createVariable(TranslatePrecisionDecoration(node->getType()),
3210 spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
John Kessenich4bee5312018-02-20 21:29:05 -07003211
3212 // Selection control:
3213 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
3214
3215 // make an "if" based on the value created by the condition
3216 spv::Builder::If ifBuilder(condition, control, builder);
3217
3218 // emit the "then" statement
3219 builder.createStore(trueValue, result);
3220 ifBuilder.makeBeginElse();
3221 // emit the "else" statement
3222 builder.createStore(falseValue, result);
3223
3224 // finish off the control flow
3225 ifBuilder.makeEndIf();
3226
3227 builder.clearAccessChain();
3228 builder.setAccessChainLValue(result);
3229 }
John Kessenich433e9ff2017-01-26 20:31:11 -07003230 };
3231
John Kessenich4bee5312018-02-20 21:29:05 -07003232 // Execute the one side needed, as per the condition
3233 const auto executeOneSide = [&]() {
3234 // Always emit control flow.
John Kessenich435dd802020-06-30 01:27:08 -06003235 if (node->getBasicType() != glslang::EbtVoid) {
3236 result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction,
3237 convertGlslangToSpvType(node->getType()));
3238 }
John Kessenich433e9ff2017-01-26 20:31:11 -07003239
John Kessenich4bee5312018-02-20 21:29:05 -07003240 // Selection control:
3241 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
3242
3243 // make an "if" based on the value created by the condition
3244 spv::Builder::If ifBuilder(condition, control, builder);
3245
3246 // emit the "then" statement
3247 if (node->getTrueBlock() != nullptr) {
3248 node->getTrueBlock()->traverse(this);
3249 if (result != spv::NoResult)
3250 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
3251 }
3252
3253 if (node->getFalseBlock() != nullptr) {
3254 ifBuilder.makeBeginElse();
3255 // emit the "else" statement
3256 node->getFalseBlock()->traverse(this);
3257 if (result != spv::NoResult)
3258 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
3259 }
3260
3261 // finish off the control flow
3262 ifBuilder.makeEndIf();
3263
3264 if (result != spv::NoResult) {
3265 builder.clearAccessChain();
3266 builder.setAccessChainLValue(result);
3267 }
3268 };
3269
3270 // Try for OpSelect (or a requirement to execute both sides)
3271 if (bothSidesPolicy()) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07003272 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
3273 if (node->getType().getQualifier().isSpecConstant())
3274 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
John Kessenich4bee5312018-02-20 21:29:05 -07003275 executeBothSides();
3276 } else
3277 executeOneSide();
John Kessenich140f3df2015-06-26 16:58:36 -06003278
3279 return false;
3280}
3281
3282bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
3283{
3284 // emit and get the condition before doing anything with switch
3285 node->getCondition()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07003286 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06003287
Rex Xu57e65922017-07-04 23:23:40 +08003288 // Selection control:
John Kesseniche18fd202018-01-30 11:01:39 -07003289 const spv::SelectionControlMask control = TranslateSwitchControl(*node);
Rex Xu57e65922017-07-04 23:23:40 +08003290
John Kessenich140f3df2015-06-26 16:58:36 -06003291 // browse the children to sort out code segments
3292 int defaultSegment = -1;
3293 std::vector<TIntermNode*> codeSegments;
3294 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
3295 std::vector<int> caseValues;
3296 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
3297 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
3298 TIntermNode* child = *c;
3299 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
baldurkd76692d2015-07-12 11:32:58 +02003300 defaultSegment = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06003301 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
baldurkd76692d2015-07-12 11:32:58 +02003302 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
John Kessenich8985fc92020-03-03 10:25:07 -07003303 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()
3304 ->getConstArray()[0].getIConst());
John Kessenich140f3df2015-06-26 16:58:36 -06003305 } else
3306 codeSegments.push_back(child);
3307 }
3308
qining25262b32016-05-06 17:25:16 -04003309 // handle the case where the last code segment is missing, due to no code
John Kessenich140f3df2015-06-26 16:58:36 -06003310 // statements between the last case and the end of the switch statement
3311 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
3312 (int)codeSegments.size() == defaultSegment)
3313 codeSegments.push_back(nullptr);
3314
3315 // make the switch statement
3316 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
John Kessenich8985fc92020-03-03 10:25:07 -07003317 builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment,
3318 segmentBlocks);
John Kessenich140f3df2015-06-26 16:58:36 -06003319
3320 // emit all the code in the segments
3321 breakForLoop.push(false);
3322 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
3323 builder.nextSwitchSegment(segmentBlocks, s);
3324 if (codeSegments[s])
3325 codeSegments[s]->traverse(this);
3326 else
3327 builder.addSwitchBreak();
3328 }
3329 breakForLoop.pop();
3330
3331 builder.endSwitch(segmentBlocks);
3332
3333 return false;
3334}
3335
3336void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
3337{
3338 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04003339 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
John Kessenich140f3df2015-06-26 16:58:36 -06003340
3341 builder.clearAccessChain();
3342 builder.setAccessChainRValue(constant);
3343}
3344
3345bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
3346{
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003347 auto blocks = builder.makeNewLoop();
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003348 builder.createBranch(&blocks.head);
steve-lunargf1709e72017-05-02 20:14:50 -06003349
3350 // Loop control:
John Kessenich1f4d0462019-01-12 17:31:41 +07003351 std::vector<unsigned int> operands;
3352 const spv::LoopControlMask control = TranslateLoopControl(*node, operands);
steve-lunargf1709e72017-05-02 20:14:50 -06003353
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003354 // Spec requires back edges to target header blocks, and every header block
3355 // must dominate its merge block. Make a header block first to ensure these
3356 // conditions are met. By definition, it will contain OpLoopMerge, followed
3357 // by a block-ending branch. But we don't want to put any other body/test
3358 // instructions in it, since the body/test may have arbitrary instructions,
3359 // including merges of its own.
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003360 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003361 builder.setBuildPoint(&blocks.head);
John Kessenich1f4d0462019-01-12 17:31:41 +07003362 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003363 if (node->testFirst() && node->getTest()) {
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003364 spv::Block& test = builder.makeNewBlock();
3365 builder.createBranch(&test);
3366
3367 builder.setBuildPoint(&test);
John Kessenich140f3df2015-06-26 16:58:36 -06003368 node->getTest()->traverse(this);
John Kesseniche485c7a2017-05-31 18:50:53 -06003369 spv::Id condition = accessChainLoad(node->getTest()->getType());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003370 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
3371
3372 builder.setBuildPoint(&blocks.body);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003373 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003374 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003375 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003376 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003377 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003378
3379 builder.setBuildPoint(&blocks.continue_target);
3380 if (node->getTerminal())
3381 node->getTerminal()->traverse(this);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003382 builder.createBranch(&blocks.head);
David Netoc22f37c2015-07-15 16:21:26 -04003383 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003384 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003385 builder.createBranch(&blocks.body);
3386
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003387 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003388 builder.setBuildPoint(&blocks.body);
3389 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003390 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003391 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003392 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003393
3394 builder.setBuildPoint(&blocks.continue_target);
3395 if (node->getTerminal())
3396 node->getTerminal()->traverse(this);
3397 if (node->getTest()) {
3398 node->getTest()->traverse(this);
3399 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07003400 accessChainLoad(node->getTest()->getType());
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003401 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003402 } else {
Dejan Mircevskied55bcd2016-01-19 21:13:38 -05003403 // TODO: unless there was a break/return/discard instruction
3404 // somewhere in the body, this is an infinite loop, so we should
3405 // issue a warning.
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003406 builder.createBranch(&blocks.head);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003407 }
John Kessenich140f3df2015-06-26 16:58:36 -06003408 }
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003409 builder.setBuildPoint(&blocks.merge);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003410 builder.closeLoop();
John Kessenich140f3df2015-06-26 16:58:36 -06003411 return false;
3412}
3413
3414bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
3415{
3416 if (node->getExpression())
3417 node->getExpression()->traverse(this);
3418
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003419 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06003420
John Kessenich140f3df2015-06-26 16:58:36 -06003421 switch (node->getFlowOp()) {
3422 case glslang::EOpKill:
3423 builder.makeDiscard();
3424 break;
3425 case glslang::EOpBreak:
3426 if (breakForLoop.top())
3427 builder.createLoopExit();
3428 else
3429 builder.addSwitchBreak();
3430 break;
3431 case glslang::EOpContinue:
John Kessenich140f3df2015-06-26 16:58:36 -06003432 builder.createLoopContinue();
3433 break;
3434 case glslang::EOpReturn:
John Kessenich435dd802020-06-30 01:27:08 -06003435 if (node->getExpression() != nullptr) {
John Kesseniched33e052016-10-06 12:59:51 -06003436 const glslang::TType& glslangReturnType = node->getExpression()->getType();
3437 spv::Id returnId = accessChainLoad(glslangReturnType);
John Kessenich435dd802020-06-30 01:27:08 -06003438 if (builder.getTypeId(returnId) != currentFunction->getReturnType() ||
3439 TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) {
John Kesseniched33e052016-10-06 12:59:51 -06003440 builder.clearAccessChain();
John Kessenich435dd802020-06-30 01:27:08 -06003441 spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(),
3442 spv::StorageClassFunction, currentFunction->getReturnType());
John Kesseniched33e052016-10-06 12:59:51 -06003443 builder.setAccessChainLValue(copyId);
3444 multiTypeStore(glslangReturnType, returnId);
John Kessenich435dd802020-06-30 01:27:08 -06003445 returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision());
John Kesseniched33e052016-10-06 12:59:51 -06003446 }
3447 builder.makeReturn(false, returnId);
3448 } else
John Kesseniche770b3e2015-09-14 20:58:02 -06003449 builder.makeReturn(false);
John Kessenich140f3df2015-06-26 16:58:36 -06003450
3451 builder.clearAccessChain();
3452 break;
3453
John Kessenichb9197c82019-08-11 07:41:45 -06003454#ifndef GLSLANG_WEB
Jeff Bolzba6170b2019-07-01 09:23:23 -05003455 case glslang::EOpDemote:
3456 builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT);
3457 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
3458 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
3459 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003460#endif
Jeff Bolzba6170b2019-07-01 09:23:23 -05003461
John Kessenich140f3df2015-06-26 16:58:36 -06003462 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003463 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003464 break;
3465 }
3466
3467 return false;
3468}
3469
John Kessenich9c14f772019-06-17 08:38:35 -06003470spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType)
John Kessenich140f3df2015-06-26 16:58:36 -06003471{
qining25262b32016-05-06 17:25:16 -04003472 // First, steer off constants, which are not SPIR-V variables, but
John Kessenich140f3df2015-06-26 16:58:36 -06003473 // can still have a mapping to a SPIR-V Id.
John Kessenich55e7d112015-11-15 21:33:39 -07003474 // This includes specialization constants.
John Kessenich7cc0e282016-03-20 00:46:02 -06003475 if (node->getQualifier().isConstant()) {
Dan Sinclair12fcaa22018-11-13 09:17:44 -05003476 spv::Id result = createSpvConstant(*node);
3477 if (result != spv::NoResult)
3478 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06003479 }
3480
3481 // Now, handle actual variables
John Kessenicha5c5fb62017-05-05 05:09:58 -06003482 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
John Kessenich9c14f772019-06-17 08:38:35 -06003483 spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType())
3484 : forcedType;
John Kessenich140f3df2015-06-26 16:58:36 -06003485
John Kessenichb9197c82019-08-11 07:41:45 -06003486 const bool contains16BitType = node->getType().contains16BitFloat() ||
3487 node->getType().contains16BitInt();
Rex Xuf89ad982017-04-07 23:22:33 +08003488 if (contains16BitType) {
John Kessenich18310872018-05-14 22:08:53 -06003489 switch (storageClass) {
3490 case spv::StorageClassInput:
3491 case spv::StorageClassOutput:
John Kessenich8317e6c2019-08-18 23:58:08 -06003492 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003493 builder.addCapability(spv::CapabilityStorageInputOutput16);
John Kessenich18310872018-05-14 22:08:53 -06003494 break;
John Kessenich18310872018-05-14 22:08:53 -06003495 case spv::StorageClassUniform:
John Kessenich8317e6c2019-08-18 23:58:08 -06003496 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003497 if (node->getType().getQualifier().storage == glslang::EvqBuffer)
3498 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
John Kessenich18310872018-05-14 22:08:53 -06003499 else
3500 builder.addCapability(spv::CapabilityStorageUniform16);
3501 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003502#ifndef GLSLANG_WEB
3503 case spv::StorageClassPushConstant:
John Kessenich8317e6c2019-08-18 23:58:08 -06003504 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenichb9197c82019-08-11 07:41:45 -06003505 builder.addCapability(spv::CapabilityStoragePushConstant16);
3506 break;
John Kessenich18310872018-05-14 22:08:53 -06003507 case spv::StorageClassStorageBuffer:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003508 case spv::StorageClassPhysicalStorageBufferEXT:
John Kessenich8317e6c2019-08-18 23:58:08 -06003509 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenich18310872018-05-14 22:08:53 -06003510 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
3511 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003512#endif
John Kessenich18310872018-05-14 22:08:53 -06003513 default:
John Kessenichb9197c82019-08-11 07:41:45 -06003514 if (node->getType().contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003515 builder.addCapability(spv::CapabilityFloat16);
John Kessenichb9197c82019-08-11 07:41:45 -06003516 if (node->getType().contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003517 builder.addCapability(spv::CapabilityInt16);
John Kessenich18310872018-05-14 22:08:53 -06003518 break;
Rex Xuf89ad982017-04-07 23:22:33 +08003519 }
3520 }
Rex Xuf89ad982017-04-07 23:22:33 +08003521
John Kessenichb9197c82019-08-11 07:41:45 -06003522 if (node->getType().contains8BitInt()) {
John Kessenich312dcfb2018-07-03 13:19:51 -06003523 if (storageClass == spv::StorageClassPushConstant) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003524 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003525 builder.addCapability(spv::CapabilityStoragePushConstant8);
3526 } else if (storageClass == spv::StorageClassUniform) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003527 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003528 builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess);
Neil Henningb6b01f02018-10-23 15:02:29 +01003529 } else if (storageClass == spv::StorageClassStorageBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003530 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
Neil Henningb6b01f02018-10-23 15:02:29 +01003531 builder.addCapability(spv::CapabilityStorageBuffer8BitAccess);
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003532 } else {
3533 builder.addCapability(spv::CapabilityInt8);
John Kessenich312dcfb2018-07-03 13:19:51 -06003534 }
3535 }
3536
John Kessenich140f3df2015-06-26 16:58:36 -06003537 const char* name = node->getName().c_str();
3538 if (glslang::IsAnonymous(name))
3539 name = "";
3540
Alejandro Piñeiroff6dcca2020-06-04 09:39:31 +02003541 spv::Id initializer = spv::NoResult;
3542
3543 if (node->getType().getQualifier().storage == glslang::EvqUniform &&
3544 !node->getConstArray().empty()) {
3545 int nextConst = 0;
3546 initializer = createSpvConstantFromConstUnionArray(node->getType(),
3547 node->getConstArray(),
3548 nextConst,
3549 false /* specConst */);
3550 }
3551
John Kessenich435dd802020-06-30 01:27:08 -06003552 return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer);
John Kessenich140f3df2015-06-26 16:58:36 -06003553}
3554
3555// Return type Id of the sampled type.
3556spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
3557{
3558 switch (sampler.type) {
John Kessenicha28f7a72019-08-06 07:00:58 -06003559 case glslang::EbtInt: return builder.makeIntType(32);
3560 case glslang::EbtUint: return builder.makeUintType(32);
John Kessenich140f3df2015-06-26 16:58:36 -06003561 case glslang::EbtFloat: return builder.makeFloatType(32);
John Kessenicha28f7a72019-08-06 07:00:58 -06003562#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08003563 case glslang::EbtFloat16:
3564 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch);
3565 builder.addCapability(spv::CapabilityFloat16ImageAMD);
3566 return builder.makeFloatType(16);
3567#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003568 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003569 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003570 return builder.makeFloatType(32);
3571 }
3572}
3573
John Kessenich8c8505c2016-07-26 12:50:38 -06003574// If node is a swizzle operation, return the type that should be used if
3575// the swizzle base is first consumed by another operation, before the swizzle
3576// is applied.
3577spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
3578{
John Kessenichecba76f2017-01-06 00:34:48 -07003579 if (node.getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06003580 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
3581 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
3582 else
3583 return spv::NoType;
3584}
3585
3586// When inverting a swizzle with a parent op, this function
3587// will apply the swizzle operation to a completed parent operation.
John Kessenich8985fc92020-03-03 10:25:07 -07003588spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node,
3589 spv::Id parentResult)
John Kessenich8c8505c2016-07-26 12:50:38 -06003590{
3591 std::vector<unsigned> swizzle;
3592 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
3593 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
3594}
3595
John Kessenich8c8505c2016-07-26 12:50:38 -06003596// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
3597void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
3598{
3599 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
3600 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
3601 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
3602}
3603
John Kessenich3ac051e2015-12-20 11:29:16 -07003604// Convert from a glslang type to an SPV type, by calling into a
3605// recursive version of this function. This establishes the inherited
3606// layout state rooted from the top-level type.
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003607spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
John Kessenich140f3df2015-06-26 16:58:36 -06003608{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003609 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
John Kessenich31ed4832015-09-09 17:51:38 -06003610}
3611
3612// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
John Kessenich7b9fa252016-01-21 18:56:57 -07003613// explicitLayout can be kept the same throughout the hierarchical recursive walk.
John Kessenich6090df02016-06-30 21:18:02 -06003614// Mutually recursive with convertGlslangStructToSpvType().
John Kessenichead86222018-03-28 18:01:20 -06003615spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003616 glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
3617 bool lastBufferBlockMember, bool forwardReferenceOnly)
John Kessenich31ed4832015-09-09 17:51:38 -06003618{
John Kesseniche0b6cad2015-12-24 10:30:13 -07003619 spv::Id spvType = spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06003620
3621 switch (type.getBasicType()) {
3622 case glslang::EbtVoid:
3623 spvType = builder.makeVoidType();
John Kessenich55e7d112015-11-15 21:33:39 -07003624 assert (! type.isArray());
John Kessenich140f3df2015-06-26 16:58:36 -06003625 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003626 case glslang::EbtBool:
John Kessenich103bef92016-02-08 21:38:15 -07003627 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
3628 // a 32-bit int where non-0 means true.
3629 if (explicitLayout != glslang::ElpNone)
3630 spvType = builder.makeUintType(32);
3631 else
3632 spvType = builder.makeBoolType();
John Kessenich140f3df2015-06-26 16:58:36 -06003633 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003634 case glslang::EbtInt:
3635 spvType = builder.makeIntType(32);
3636 break;
3637 case glslang::EbtUint:
3638 spvType = builder.makeUintType(32);
3639 break;
3640 case glslang::EbtFloat:
3641 spvType = builder.makeFloatType(32);
3642 break;
3643#ifndef GLSLANG_WEB
3644 case glslang::EbtDouble:
3645 spvType = builder.makeFloatType(64);
3646 break;
3647 case glslang::EbtFloat16:
3648 spvType = builder.makeFloatType(16);
3649 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003650 case glslang::EbtInt8:
John Kessenich66011cb2018-03-06 16:12:04 -07003651 spvType = builder.makeIntType(8);
3652 break;
3653 case glslang::EbtUint8:
John Kessenich66011cb2018-03-06 16:12:04 -07003654 spvType = builder.makeUintType(8);
3655 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003656 case glslang::EbtInt16:
John Kessenich66011cb2018-03-06 16:12:04 -07003657 spvType = builder.makeIntType(16);
3658 break;
3659 case glslang::EbtUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07003660 spvType = builder.makeUintType(16);
3661 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08003662 case glslang::EbtInt64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003663 spvType = builder.makeIntType(64);
3664 break;
3665 case glslang::EbtUint64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003666 spvType = builder.makeUintType(64);
3667 break;
John Kessenich426394d2015-07-23 10:22:48 -06003668 case glslang::EbtAtomicUint:
John Kessenich2d0cc782016-07-07 13:20:00 -06003669 builder.addCapability(spv::CapabilityAtomicStorage);
John Kessenich426394d2015-07-23 10:22:48 -06003670 spvType = builder.makeUintType(32);
3671 break;
Daniel Kochdb32b242020-03-17 20:42:47 -04003672 case glslang::EbtAccStruct:
3673 spvType = builder.makeAccelerationStructureType();
Chao Chenb50c02e2018-09-19 11:42:24 -07003674 break;
Torosdagli06c2eee2020-03-19 11:09:57 -04003675 case glslang::EbtRayQuery:
3676 spvType = builder.makeRayQueryType();
3677 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003678 case glslang::EbtReference:
3679 {
3680 // Make the forward pointer, then recurse to convert the structure type, then
3681 // patch up the forward pointer with a real pointer type.
3682 if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) {
3683 spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT);
3684 forwardPointers[type.getReferentType()] = forwardId;
3685 }
3686 spvType = forwardPointers[type.getReferentType()];
3687 if (!forwardReferenceOnly) {
3688 spv::Id referentType = convertGlslangToSpvType(*type.getReferentType());
3689 builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT,
3690 forwardPointers[type.getReferentType()],
3691 referentType);
3692 }
3693 }
3694 break;
Chao Chenb50c02e2018-09-19 11:42:24 -07003695#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003696 case glslang::EbtSampler:
3697 {
3698 const glslang::TSampler& sampler = type.getSampler();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003699 if (sampler.isPureSampler()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003700 spvType = builder.makeSamplerType();
3701 } else {
3702 // an image is present, make its type
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003703 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler),
3704 sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(),
3705 sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type));
3706 if (sampler.isCombined()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003707 // already has both image and sampler, make the combined type
3708 spvType = builder.makeSampledImageType(spvType);
3709 }
John Kessenich55e7d112015-11-15 21:33:39 -07003710 }
John Kesseniche0b6cad2015-12-24 10:30:13 -07003711 }
John Kessenich140f3df2015-06-26 16:58:36 -06003712 break;
3713 case glslang::EbtStruct:
3714 case glslang::EbtBlock:
3715 {
3716 // If we've seen this struct type, return it
John Kessenich6090df02016-06-30 21:18:02 -06003717 const glslang::TTypeList* glslangMembers = type.getStruct();
John Kesseniche0b6cad2015-12-24 10:30:13 -07003718
3719 // Try to share structs for different layouts, but not yet for other
3720 // kinds of qualification (primarily not yet including interpolant qualification).
John Kessenichf2b7f332016-09-01 17:05:23 -06003721 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003722 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
John Kesseniche0b6cad2015-12-24 10:30:13 -07003723 if (spvType != spv::NoResult)
John Kessenich140f3df2015-06-26 16:58:36 -06003724 break;
3725
3726 // else, we haven't seen it...
John Kessenich140f3df2015-06-26 16:58:36 -06003727 if (type.getBasicType() == glslang::EbtBlock)
Roy05a5b532020-01-03 16:21:34 +08003728 memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size());
John Kessenich6090df02016-06-30 21:18:02 -06003729 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
John Kessenich140f3df2015-06-26 16:58:36 -06003730 }
3731 break;
Jeff Bolz04d73732019-05-31 13:06:01 -05003732 case glslang::EbtString:
3733 // no type used for OpString
3734 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06003735 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003736 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003737 break;
3738 }
3739
3740 if (type.isMatrix())
3741 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
3742 else {
3743 // If this variable has a vector element count greater than 1, create a SPIR-V vector
3744 if (type.getVectorSize() > 1)
3745 spvType = builder.makeVectorType(spvType, type.getVectorSize());
3746 }
3747
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003748 if (type.isCoopMat()) {
3749 builder.addCapability(spv::CapabilityCooperativeMatrixNV);
3750 builder.addExtension(spv::E_SPV_NV_cooperative_matrix);
3751 if (type.getBasicType() == glslang::EbtFloat16)
3752 builder.addCapability(spv::CapabilityFloat16);
Jeff Bolz387657e2019-08-22 20:28:00 -05003753 if (type.getBasicType() == glslang::EbtUint8 ||
3754 type.getBasicType() == glslang::EbtInt8) {
3755 builder.addCapability(spv::CapabilityInt8);
3756 }
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003757
3758 spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1);
3759 spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2);
3760 spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3);
3761
3762 spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols);
3763 }
3764
John Kessenich140f3df2015-06-26 16:58:36 -06003765 if (type.isArray()) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003766 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
3767
John Kessenichc9a80832015-09-12 12:17:44 -06003768 // Do all but the outer dimension
John Kessenichc9e0a422015-12-29 21:27:24 -07003769 if (type.getArraySizes()->getNumDims() > 1) {
John Kessenichf8842e52016-01-04 19:22:56 -07003770 // We need to decorate array strides for types needing explicit layout, except blocks.
3771 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003772 // Use a dummy glslang type for querying internal strides of
3773 // arrays of arrays, but using just a one-dimensional array.
3774 glslang::TType simpleArrayType(type, 0); // deference type of the array
John Kessenich859b0342018-03-26 00:38:53 -06003775 while (simpleArrayType.getArraySizes()->getNumDims() > 1)
3776 simpleArrayType.getArraySizes()->dereference();
John Kessenichc9e0a422015-12-29 21:27:24 -07003777
3778 // Will compute the higher-order strides here, rather than making a whole
3779 // pile of types and doing repetitive recursion on their contents.
3780 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
3781 }
John Kessenichf8842e52016-01-04 19:22:56 -07003782
3783 // make the arrays
John Kessenichc9e0a422015-12-29 21:27:24 -07003784 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
John Kessenich6c292d32016-02-15 20:58:50 -07003785 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
John Kessenichc9e0a422015-12-29 21:27:24 -07003786 if (stride > 0)
3787 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich6c292d32016-02-15 20:58:50 -07003788 stride *= type.getArraySizes()->getDimSize(dim);
John Kessenichc9e0a422015-12-29 21:27:24 -07003789 }
3790 } else {
3791 // single-dimensional array, and don't yet have stride
3792
John Kessenichf8842e52016-01-04 19:22:56 -07003793 // We need to decorate array strides for types needing explicit layout, except blocks.
John Kessenichc9e0a422015-12-29 21:27:24 -07003794 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
3795 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
John Kessenichc9a80832015-09-12 12:17:44 -06003796 }
John Kessenich31ed4832015-09-09 17:51:38 -06003797
John Kessenichead86222018-03-28 18:01:20 -06003798 // Do the outer dimension, which might not be known for a runtime-sized array.
3799 // (Unsized arrays that survive through linking will be runtime-sized arrays)
3800 if (type.isSizedArray())
John Kessenich6c292d32016-02-15 20:58:50 -07003801 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
John Kessenich5611c6d2018-04-05 11:25:02 -06003802 else {
John Kessenichb9197c82019-08-11 07:41:45 -06003803#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06003804 if (!lastBufferBlockMember) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003805 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06003806 builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT);
3807 }
John Kessenichb9197c82019-08-11 07:41:45 -06003808#endif
John Kessenich3dd1ce52019-10-17 07:08:40 -06003809 spvType = builder.makeRuntimeArray(spvType);
John Kessenich5611c6d2018-04-05 11:25:02 -06003810 }
John Kessenichc9e0a422015-12-29 21:27:24 -07003811 if (stride > 0)
3812 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich140f3df2015-06-26 16:58:36 -06003813 }
3814
3815 return spvType;
3816}
3817
John Kessenich0e737842017-03-24 18:38:16 -06003818// TODO: this functionality should exist at a higher level, in creating the AST
3819//
3820// Identify interface members that don't have their required extension turned on.
3821//
3822bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
3823{
John Kessenicha28f7a72019-08-06 07:00:58 -06003824#ifndef GLSLANG_WEB
John Kessenich0e737842017-03-24 18:38:16 -06003825 auto& extensions = glslangIntermediate->getRequestedExtensions();
3826
Rex Xubcf291a2017-03-29 23:01:36 +08003827 if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
3828 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3829 return true;
John Kessenich0e737842017-03-24 18:38:16 -06003830 if (member.getFieldName() == "gl_SecondaryPositionNV" &&
3831 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3832 return true;
Chao Chen3c366992018-09-19 11:41:59 -07003833
3834 if (glslangIntermediate->getStage() != EShLangMeshNV) {
3835 if (member.getFieldName() == "gl_ViewportMask" &&
3836 extensions.find("GL_NV_viewport_array2") == extensions.end())
3837 return true;
3838 if (member.getFieldName() == "gl_PositionPerViewNV" &&
3839 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3840 return true;
3841 if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
3842 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3843 return true;
3844 }
3845#endif
John Kessenich0e737842017-03-24 18:38:16 -06003846
3847 return false;
3848};
3849
John Kessenich6090df02016-06-30 21:18:02 -06003850// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
3851// explicitLayout can be kept the same throughout the hierarchical recursive walk.
3852// Mutually recursive with convertGlslangToSpvType().
3853spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
3854 const glslang::TTypeList* glslangMembers,
3855 glslang::TLayoutPacking explicitLayout,
3856 const glslang::TQualifier& qualifier)
3857{
3858 // Create a vector of struct types for SPIR-V to consume
3859 std::vector<spv::Id> spvMembers;
John Kessenich8985fc92020-03-03 10:25:07 -07003860 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0,
3861 // except sometimes for blocks
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003862 std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers;
John Kessenich6090df02016-06-30 21:18:02 -06003863 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3864 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3865 if (glslangMember.hiddenMember()) {
3866 ++memberDelta;
3867 if (type.getBasicType() == glslang::EbtBlock)
Roy05a5b532020-01-03 16:21:34 +08003868 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1;
John Kessenich6090df02016-06-30 21:18:02 -06003869 } else {
John Kessenich0e737842017-03-24 18:38:16 -06003870 if (type.getBasicType() == glslang::EbtBlock) {
Ashwin Lelec1e61d62019-07-22 12:36:38 -07003871 if (filterMember(glslangMember)) {
3872 memberDelta++;
Roy05a5b532020-01-03 16:21:34 +08003873 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1;
John Kessenich0e737842017-03-24 18:38:16 -06003874 continue;
Ashwin Lelec1e61d62019-07-22 12:36:38 -07003875 }
Roy05a5b532020-01-03 16:21:34 +08003876 memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta;
John Kessenich0e737842017-03-24 18:38:16 -06003877 }
John Kessenich6090df02016-06-30 21:18:02 -06003878 // modify just this child's view of the qualifier
3879 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3880 InheritQualifiers(memberQualifier, qualifier);
3881
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003882 // manually inherit location
John Kessenich6090df02016-06-30 21:18:02 -06003883 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003884 memberQualifier.layoutLocation = qualifier.layoutLocation;
John Kessenich6090df02016-06-30 21:18:02 -06003885
3886 // recurse
John Kessenichead86222018-03-28 18:01:20 -06003887 bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer &&
3888 i == (int)glslangMembers->size() - 1;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003889
3890 // Make forward pointers for any pointer members, and create a list of members to
3891 // convert to spirv types after creating the struct.
John Kessenich7015bd62019-08-01 03:28:08 -06003892 if (glslangMember.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003893 if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) {
3894 deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier));
3895 }
3896 spvMembers.push_back(
John Kessenich8985fc92020-03-03 10:25:07 -07003897 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember,
3898 true));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003899 } else {
3900 spvMembers.push_back(
John Kessenich8985fc92020-03-03 10:25:07 -07003901 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember,
3902 false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003903 }
John Kessenich6090df02016-06-30 21:18:02 -06003904 }
3905 }
3906
3907 // Make the SPIR-V type
3908 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
John Kessenichf2b7f332016-09-01 17:05:23 -06003909 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003910 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
3911
3912 // Decorate it
3913 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
3914
John Kessenichd72f4882019-01-16 14:55:37 +07003915 for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003916 auto it = deferredForwardPointers[i];
3917 convertGlslangToSpvType(*it.first, explicitLayout, it.second, false);
3918 }
3919
John Kessenich6090df02016-06-30 21:18:02 -06003920 return spvType;
3921}
3922
3923void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
3924 const glslang::TTypeList* glslangMembers,
3925 glslang::TLayoutPacking explicitLayout,
3926 const glslang::TQualifier& qualifier,
3927 spv::Id spvType)
3928{
3929 // Name and decorate the non-hidden members
3930 int offset = -1;
3931 int locationOffset = 0; // for use within the members of this struct
3932 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3933 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3934 int member = i;
John Kessenich0e737842017-03-24 18:38:16 -06003935 if (type.getBasicType() == glslang::EbtBlock) {
Roy05a5b532020-01-03 16:21:34 +08003936 member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i];
John Kessenich0e737842017-03-24 18:38:16 -06003937 if (filterMember(glslangMember))
3938 continue;
3939 }
John Kessenich6090df02016-06-30 21:18:02 -06003940
3941 // modify just this child's view of the qualifier
3942 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3943 InheritQualifiers(memberQualifier, qualifier);
3944
3945 // using -1 above to indicate a hidden member
John Kessenich5d610ee2018-03-07 18:05:55 -07003946 if (member < 0)
3947 continue;
3948
3949 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
3950 builder.addMemberDecoration(spvType, member,
3951 TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
3952 builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
3953 // Add interpolation and auxiliary storage decorations only to
3954 // top-level members of Input and Output storage classes
3955 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
3956 type.getQualifier().storage == glslang::EvqVaryingOut) {
3957 if (type.getBasicType() == glslang::EbtBlock ||
3958 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
3959 builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
3960 builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
John Kessenicha28f7a72019-08-06 07:00:58 -06003961#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07003962 addMeshNVDecoration(spvType, member, memberQualifier);
3963#endif
John Kessenich6090df02016-06-30 21:18:02 -06003964 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003965 }
3966 builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
John Kessenich6090df02016-06-30 21:18:02 -06003967
John Kessenichb9197c82019-08-11 07:41:45 -06003968#ifndef GLSLANG_WEB
John Kessenich5d610ee2018-03-07 18:05:55 -07003969 if (type.getBasicType() == glslang::EbtBlock &&
3970 qualifier.storage == glslang::EvqBuffer) {
3971 // Add memory decorations only to top-level members of shader storage block
3972 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05003973 TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich5d610ee2018-03-07 18:05:55 -07003974 for (unsigned int i = 0; i < memory.size(); ++i)
3975 builder.addMemberDecoration(spvType, member, memory[i]);
3976 }
John Kessenichf8d1d742019-10-21 06:55:11 -06003977
John Kessenichb9197c82019-08-11 07:41:45 -06003978#endif
John Kessenich6090df02016-06-30 21:18:02 -06003979
John Kessenich5d610ee2018-03-07 18:05:55 -07003980 // Location assignment was already completed correctly by the front end,
3981 // just track whether a member needs to be decorated.
3982 // Ignore member locations if the container is an array, as that's
3983 // ill-specified and decisions have been made to not allow this.
3984 if (! type.isArray() && memberQualifier.hasLocation())
3985 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation);
John Kessenich6090df02016-06-30 21:18:02 -06003986
John Kessenich5d610ee2018-03-07 18:05:55 -07003987 if (qualifier.hasLocation()) // track for upcoming inheritance
3988 locationOffset += glslangIntermediate->computeTypeLocationSize(
3989 glslangMember, glslangIntermediate->getStage());
John Kessenich2f47bc92016-06-30 21:47:35 -06003990
John Kessenich5d610ee2018-03-07 18:05:55 -07003991 // component, XFB, others
3992 if (glslangMember.getQualifier().hasComponent())
3993 builder.addMemberDecoration(spvType, member, spv::DecorationComponent,
3994 glslangMember.getQualifier().layoutComponent);
3995 if (glslangMember.getQualifier().hasXfbOffset())
3996 builder.addMemberDecoration(spvType, member, spv::DecorationOffset,
3997 glslangMember.getQualifier().layoutXfbOffset);
3998 else if (explicitLayout != glslang::ElpNone) {
3999 // figure out what to do with offset, which is accumulating
4000 int nextOffset;
4001 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
4002 if (offset >= 0)
4003 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
4004 offset = nextOffset;
4005 }
John Kessenich6090df02016-06-30 21:18:02 -06004006
John Kessenich5d610ee2018-03-07 18:05:55 -07004007 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
4008 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride,
4009 getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
John Kessenich6090df02016-06-30 21:18:02 -06004010
John Kessenich5d610ee2018-03-07 18:05:55 -07004011 // built-in variable decorations
4012 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
4013 if (builtIn != spv::BuiltInMax)
4014 builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
chaoc771d89f2017-01-13 01:10:53 -08004015
John Kessenichb9197c82019-08-11 07:41:45 -06004016#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06004017 // nonuniform
4018 builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier()));
4019
John Kessenichead86222018-03-28 18:01:20 -06004020 if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) {
4021 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
4022 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
4023 memberQualifier.semanticName);
4024 }
4025
John Kessenich5d610ee2018-03-07 18:05:55 -07004026 if (builtIn == spv::BuiltInLayer) {
4027 // SPV_NV_viewport_array2 extension
4028 if (glslangMember.getQualifier().layoutViewportRelative){
4029 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
4030 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
4031 builder.addExtension(spv::E_SPV_NV_viewport_array2);
chaoc771d89f2017-01-13 01:10:53 -08004032 }
John Kessenich5d610ee2018-03-07 18:05:55 -07004033 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
4034 builder.addMemberDecoration(spvType, member,
4035 (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
4036 glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
4037 builder.addCapability(spv::CapabilityShaderStereoViewNV);
4038 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
chaocdf3956c2017-02-14 14:52:34 -08004039 }
John Kessenich5d610ee2018-03-07 18:05:55 -07004040 }
4041 if (glslangMember.getQualifier().layoutPassthrough) {
4042 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
4043 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
4044 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
4045 }
chaoc771d89f2017-01-13 01:10:53 -08004046#endif
John Kessenich6090df02016-06-30 21:18:02 -06004047 }
4048
4049 // Decorate the structure
John Kessenich5d610ee2018-03-07 18:05:55 -07004050 builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
4051 builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer()));
John Kessenich6090df02016-06-30 21:18:02 -06004052}
4053
John Kessenich6c292d32016-02-15 20:58:50 -07004054// Turn the expression forming the array size into an id.
4055// This is not quite trivial, because of specialization constants.
4056// Sometimes, a raw constant is turned into an Id, and sometimes
4057// a specialization constant expression is.
4058spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
4059{
4060 // First, see if this is sized with a node, meaning a specialization constant:
4061 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
4062 if (specNode != nullptr) {
4063 builder.clearAccessChain();
4064 specNode->traverse(this);
4065 return accessChainLoad(specNode->getAsTyped()->getType());
4066 }
qining25262b32016-05-06 17:25:16 -04004067
John Kessenich6c292d32016-02-15 20:58:50 -07004068 // Otherwise, need a compile-time (front end) size, get it:
4069 int size = arraySizes.getDimSize(dim);
4070 assert(size > 0);
4071 return builder.makeUintConstant(size);
4072}
4073
John Kessenich103bef92016-02-08 21:38:15 -07004074// Wrap the builder's accessChainLoad to:
4075// - localize handling of RelaxedPrecision
4076// - use the SPIR-V inferred type instead of another conversion of the glslang type
4077// (avoids unnecessary work and possible type punning for structures)
4078// - do conversion of concrete to abstract type
John Kessenich32cfd492016-02-02 12:37:46 -07004079spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
4080{
John Kessenich103bef92016-02-08 21:38:15 -07004081 spv::Id nominalTypeId = builder.accessChainGetInferredType();
Jeff Bolz36831c92018-09-05 10:11:41 -05004082
4083 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
4084 coherentFlags |= TranslateCoherent(type);
4085
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004086 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06004087 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004088
John Kessenich5611c6d2018-04-05 11:25:02 -06004089 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type),
John Kessenich8985fc92020-03-03 10:25:07 -07004090 TranslateNonUniformDecoration(type.getQualifier()),
4091 nominalTypeId,
4092 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask),
4093 TranslateMemoryScope(coherentFlags),
4094 alignment);
John Kessenich103bef92016-02-08 21:38:15 -07004095
4096 // Need to convert to abstract types when necessary
Rex Xu27253232016-02-23 17:51:09 +08004097 if (type.getBasicType() == glslang::EbtBool) {
4098 if (builder.isScalarType(nominalTypeId)) {
4099 // Conversion for bool
4100 spv::Id boolType = builder.makeBoolType();
4101 if (nominalTypeId != boolType)
4102 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
4103 } else if (builder.isVectorType(nominalTypeId)) {
4104 // Conversion for bvec
4105 int vecSize = builder.getNumTypeComponents(nominalTypeId);
4106 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
4107 if (nominalTypeId != bvecType)
John Kessenich8985fc92020-03-03 10:25:07 -07004108 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId,
4109 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08004110 }
4111 }
John Kessenich103bef92016-02-08 21:38:15 -07004112
4113 return loadedId;
John Kessenich32cfd492016-02-02 12:37:46 -07004114}
4115
Rex Xu27253232016-02-23 17:51:09 +08004116// Wrap the builder's accessChainStore to:
4117// - do conversion of concrete to abstract type
John Kessenich4bf71552016-09-02 11:20:21 -06004118//
4119// Implicitly uses the existing builder.accessChain as the storage target.
Rex Xu27253232016-02-23 17:51:09 +08004120void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
4121{
4122 // Need to convert to abstract types when necessary
4123 if (type.getBasicType() == glslang::EbtBool) {
4124 spv::Id nominalTypeId = builder.accessChainGetInferredType();
4125
4126 if (builder.isScalarType(nominalTypeId)) {
4127 // Conversion for bool
4128 spv::Id boolType = builder.makeBoolType();
John Kessenichb6cabc42017-05-19 23:29:50 -06004129 if (nominalTypeId != boolType) {
4130 // keep these outside arguments, for determinant order-of-evaluation
4131 spv::Id one = builder.makeUintConstant(1);
4132 spv::Id zero = builder.makeUintConstant(0);
4133 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
4134 } else if (builder.getTypeId(rvalue) != boolType)
John Kessenich80f92a12017-05-19 23:00:13 -06004135 rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0));
Rex Xu27253232016-02-23 17:51:09 +08004136 } else if (builder.isVectorType(nominalTypeId)) {
4137 // Conversion for bvec
4138 int vecSize = builder.getNumTypeComponents(nominalTypeId);
4139 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
John Kessenichb6cabc42017-05-19 23:29:50 -06004140 if (nominalTypeId != bvecType) {
4141 // keep these outside arguments, for determinant order-of-evaluation
John Kessenich7b8c3862017-05-19 23:44:51 -06004142 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
4143 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
4144 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
John Kessenichb6cabc42017-05-19 23:29:50 -06004145 } else if (builder.getTypeId(rvalue) != bvecType)
John Kessenich80f92a12017-05-19 23:00:13 -06004146 rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue,
4147 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08004148 }
4149 }
4150
Jeff Bolz36831c92018-09-05 10:11:41 -05004151 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
4152 coherentFlags |= TranslateCoherent(type);
4153
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004154 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06004155 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004156
Jeff Bolz36831c92018-09-05 10:11:41 -05004157 builder.accessChainStore(rvalue,
John Kessenich8985fc92020-03-03 10:25:07 -07004158 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) &
4159 ~spv::MemoryAccessMakePointerVisibleKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004160 TranslateMemoryScope(coherentFlags), alignment);
Rex Xu27253232016-02-23 17:51:09 +08004161}
4162
John Kessenich4bf71552016-09-02 11:20:21 -06004163// For storing when types match at the glslang level, but not might match at the
4164// SPIR-V level.
4165//
4166// This especially happens when a single glslang type expands to multiple
John Kesseniched33e052016-10-06 12:59:51 -06004167// SPIR-V types, like a struct that is used in a member-undecorated way as well
John Kessenich4bf71552016-09-02 11:20:21 -06004168// as in a member-decorated way.
4169//
4170// NOTE: This function can handle any store request; if it's not special it
4171// simplifies to a simple OpStore.
4172//
4173// Implicitly uses the existing builder.accessChain as the storage target.
4174void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
4175{
John Kessenichb3e24e42016-09-11 12:33:43 -06004176 // we only do the complex path here if it's an aggregate
4177 if (! type.isStruct() && ! type.isArray()) {
John Kessenich4bf71552016-09-02 11:20:21 -06004178 accessChainStore(type, rValue);
4179 return;
4180 }
4181
John Kessenichb3e24e42016-09-11 12:33:43 -06004182 // and, it has to be a case of type aliasing
John Kessenich4bf71552016-09-02 11:20:21 -06004183 spv::Id rType = builder.getTypeId(rValue);
4184 spv::Id lValue = builder.accessChainGetLValue();
4185 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
4186 if (lType == rType) {
4187 accessChainStore(type, rValue);
4188 return;
4189 }
4190
John Kessenichb3e24e42016-09-11 12:33:43 -06004191 // Recursively (as needed) copy an aggregate type to a different aggregate type,
John Kessenich4bf71552016-09-02 11:20:21 -06004192 // where the two types were the same type in GLSL. This requires member
4193 // by member copy, recursively.
4194
John Kessenichfbb6bdf2019-01-15 21:48:27 +07004195 // SPIR-V 1.4 added an instruction to do help do this.
4196 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
4197 // However, bool in uniform space is changed to int, so
4198 // OpCopyLogical does not work for that.
4199 // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules.
4200 bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0);
4201 bool lBool = builder.containsType(lType, spv::OpTypeBool, 0);
4202 if (lBool == rBool) {
4203 spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue);
4204 accessChainStore(type, logicalCopy);
4205 return;
4206 }
4207 }
4208
John Kessenichb3e24e42016-09-11 12:33:43 -06004209 // If an array, copy element by element.
4210 if (type.isArray()) {
4211 glslang::TType glslangElementType(type, 0);
4212 spv::Id elementRType = builder.getContainedTypeId(rType);
4213 for (int index = 0; index < type.getOuterArraySize(); ++index) {
4214 // get the source member
4215 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
John Kessenich4bf71552016-09-02 11:20:21 -06004216
John Kessenichb3e24e42016-09-11 12:33:43 -06004217 // set up the target storage
4218 builder.clearAccessChain();
4219 builder.setAccessChainLValue(lValue);
John Kessenich8985fc92020-03-03 10:25:07 -07004220 builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type),
4221 type.getBufferReferenceAlignment());
John Kessenich4bf71552016-09-02 11:20:21 -06004222
John Kessenichb3e24e42016-09-11 12:33:43 -06004223 // store the member
4224 multiTypeStore(glslangElementType, elementRValue);
4225 }
4226 } else {
4227 assert(type.isStruct());
John Kessenich4bf71552016-09-02 11:20:21 -06004228
John Kessenichb3e24e42016-09-11 12:33:43 -06004229 // loop over structure members
4230 const glslang::TTypeList& members = *type.getStruct();
4231 for (int m = 0; m < (int)members.size(); ++m) {
4232 const glslang::TType& glslangMemberType = *members[m].type;
4233
4234 // get the source member
4235 spv::Id memberRType = builder.getContainedTypeId(rType, m);
4236 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
4237
4238 // set up the target storage
4239 builder.clearAccessChain();
4240 builder.setAccessChainLValue(lValue);
John Kessenich8985fc92020-03-03 10:25:07 -07004241 builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type),
4242 type.getBufferReferenceAlignment());
John Kessenichb3e24e42016-09-11 12:33:43 -06004243
4244 // store the member
4245 multiTypeStore(glslangMemberType, memberRValue);
4246 }
John Kessenich4bf71552016-09-02 11:20:21 -06004247 }
4248}
4249
John Kessenichf85e8062015-12-19 13:57:10 -07004250// Decide whether or not this type should be
4251// decorated with offsets and strides, and if so
4252// whether std140 or std430 rules should be applied.
4253glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
John Kessenich31ed4832015-09-09 17:51:38 -06004254{
John Kessenichf85e8062015-12-19 13:57:10 -07004255 // has to be a block
4256 if (type.getBasicType() != glslang::EbtBlock)
4257 return glslang::ElpNone;
4258
Chao Chen3c366992018-09-19 11:41:59 -07004259 // has to be a uniform or buffer block or task in/out blocks
John Kessenichf85e8062015-12-19 13:57:10 -07004260 if (type.getQualifier().storage != glslang::EvqUniform &&
Chao Chen3c366992018-09-19 11:41:59 -07004261 type.getQualifier().storage != glslang::EvqBuffer &&
4262 !type.getQualifier().isTaskMemory())
John Kessenichf85e8062015-12-19 13:57:10 -07004263 return glslang::ElpNone;
4264
4265 // return the layout to use
4266 switch (type.getQualifier().layoutPacking) {
4267 case glslang::ElpStd140:
4268 case glslang::ElpStd430:
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004269 case glslang::ElpScalar:
John Kessenichf85e8062015-12-19 13:57:10 -07004270 return type.getQualifier().layoutPacking;
4271 default:
4272 return glslang::ElpNone;
4273 }
John Kessenich31ed4832015-09-09 17:51:38 -06004274}
4275
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004276// Given an array type, returns the integer stride required for that array
John Kessenich8985fc92020-03-03 10:25:07 -07004277int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout,
4278 glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004279{
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004280 int size;
John Kessenich49987892015-12-29 17:11:44 -07004281 int stride;
John Kessenich8985fc92020-03-03 10:25:07 -07004282 glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout,
4283 matrixLayout == glslang::ElmRowMajor);
John Kesseniche721f492015-12-06 19:17:49 -07004284
4285 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004286}
4287
John Kessenich49987892015-12-29 17:11:44 -07004288// 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 -07004289// when used as a member of an interface block
John Kessenich8985fc92020-03-03 10:25:07 -07004290int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout,
4291 glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004292{
John Kessenich49987892015-12-29 17:11:44 -07004293 glslang::TType elementType;
4294 elementType.shallowCopy(matrixType);
4295 elementType.clearArraySizes();
4296
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004297 int size;
John Kessenich49987892015-12-29 17:11:44 -07004298 int stride;
John Kessenich8985fc92020-03-03 10:25:07 -07004299 glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout,
4300 matrixLayout == glslang::ElmRowMajor);
John Kessenich49987892015-12-29 17:11:44 -07004301
4302 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004303}
4304
John Kessenich5e4b1242015-08-06 22:53:06 -06004305// Given a member type of a struct, realign the current offset for it, and compute
4306// the next (not yet aligned) offset for the next member, which will get aligned
4307// on the next call.
4308// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
4309// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
4310// -1 means a non-forced member offset (no decoration needed).
John Kessenich8985fc92020-03-03 10:25:07 -07004311void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType,
4312 int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
John Kessenich5e4b1242015-08-06 22:53:06 -06004313{
4314 // this will get a positive value when deemed necessary
4315 nextOffset = -1;
4316
John Kessenich5e4b1242015-08-06 22:53:06 -06004317 // override anything in currentOffset with user-set offset
4318 if (memberType.getQualifier().hasOffset())
4319 currentOffset = memberType.getQualifier().layoutOffset;
4320
4321 // It could be that current linker usage in glslang updated all the layoutOffset,
4322 // in which case the following code does not matter. But, that's not quite right
4323 // once cross-compilation unit GLSL validation is done, as the original user
4324 // settings are needed in layoutOffset, and then the following will come into play.
4325
John Kessenichf85e8062015-12-19 13:57:10 -07004326 if (explicitLayout == glslang::ElpNone) {
John Kessenich5e4b1242015-08-06 22:53:06 -06004327 if (! memberType.getQualifier().hasOffset())
4328 currentOffset = -1;
4329
4330 return;
4331 }
4332
John Kessenichf85e8062015-12-19 13:57:10 -07004333 // Getting this far means we need explicit offsets
John Kessenich5e4b1242015-08-06 22:53:06 -06004334 if (currentOffset < 0)
4335 currentOffset = 0;
qining25262b32016-05-06 17:25:16 -04004336
John Kessenich5e4b1242015-08-06 22:53:06 -06004337 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
4338 // but possibly not yet correctly aligned.
4339
4340 int memberSize;
John Kessenich49987892015-12-29 17:11:44 -07004341 int dummyStride;
John Kessenich8985fc92020-03-03 10:25:07 -07004342 int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout,
4343 matrixLayout == glslang::ElmRowMajor);
John Kessenich4f1403e2017-04-05 17:38:20 -06004344
4345 // Adjust alignment for HLSL rules
John Kessenich735d7e52017-07-13 11:39:16 -06004346 // TODO: make this consistent in early phases of code:
4347 // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
4348 // Until reflection is brought in sync with these adjustments, don't apply to $Global,
4349 // which is the most likely to rely on reflection, and least likely to rely implicit layouts
John Kesseniche7df8e02018-08-22 17:12:46 -06004350 if (glslangIntermediate->usingHlslOffsets() &&
John Kessenich735d7e52017-07-13 11:39:16 -06004351 ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
John Kessenich4f1403e2017-04-05 17:38:20 -06004352 int dummySize;
4353 int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
4354 if (componentAlignment <= 4)
4355 memberAlignment = componentAlignment;
4356 }
4357
4358 // Bump up to member alignment
John Kessenich5e4b1242015-08-06 22:53:06 -06004359 glslang::RoundToPow2(currentOffset, memberAlignment);
John Kessenich4f1403e2017-04-05 17:38:20 -06004360
4361 // Bump up to vec4 if there is a bad straddle
John Kessenich8985fc92020-03-03 10:25:07 -07004362 if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize,
4363 currentOffset))
John Kessenich4f1403e2017-04-05 17:38:20 -06004364 glslang::RoundToPow2(currentOffset, 16);
4365
John Kessenich5e4b1242015-08-06 22:53:06 -06004366 nextOffset = currentOffset + memberSize;
4367}
4368
David Netoa901ffe2016-06-08 14:11:40 +01004369void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
John Kessenichebb50532016-05-16 19:22:05 -06004370{
David Netoa901ffe2016-06-08 14:11:40 +01004371 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
4372 switch (glslangBuiltIn)
4373 {
John Kessenicha28f7a72019-08-06 07:00:58 -06004374 case glslang::EbvPointSize:
4375#ifndef GLSLANG_WEB
David Netoa901ffe2016-06-08 14:11:40 +01004376 case glslang::EbvClipDistance:
4377 case glslang::EbvCullDistance:
chaoc771d89f2017-01-13 01:10:53 -08004378 case glslang::EbvViewportMaskNV:
4379 case glslang::EbvSecondaryPositionNV:
4380 case glslang::EbvSecondaryViewportMaskNV:
chaocdf3956c2017-02-14 14:52:34 -08004381 case glslang::EbvPositionPerViewNV:
4382 case glslang::EbvViewportMaskPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07004383 case glslang::EbvTaskCountNV:
4384 case glslang::EbvPrimitiveCountNV:
4385 case glslang::EbvPrimitiveIndicesNV:
4386 case glslang::EbvClipDistancePerViewNV:
4387 case glslang::EbvCullDistancePerViewNV:
4388 case glslang::EbvLayerPerViewNV:
4389 case glslang::EbvMeshViewCountNV:
4390 case glslang::EbvMeshViewIndicesNV:
chaoc771d89f2017-01-13 01:10:53 -08004391#endif
David Netoa901ffe2016-06-08 14:11:40 +01004392 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
4393 // Alternately, we could just call this for any glslang built-in, since the
4394 // capability already guards against duplicates.
4395 TranslateBuiltInDecoration(glslangBuiltIn, false);
4396 break;
4397 default:
4398 // Capabilities were already generated when the struct was declared.
4399 break;
4400 }
John Kessenichebb50532016-05-16 19:22:05 -06004401}
4402
John Kessenich6fccb3c2016-09-19 16:01:41 -06004403bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
John Kessenich140f3df2015-06-26 16:58:36 -06004404{
John Kessenicheee9d532016-09-19 18:09:30 -06004405 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
John Kessenich140f3df2015-06-26 16:58:36 -06004406}
4407
John Kessenichd41993d2017-09-10 15:21:05 -06004408// Does parameter need a place to keep writes, separate from the original?
John Kessenich6a14f782017-12-04 02:48:10 -07004409// Assumes called after originalParam(), which filters out block/buffer/opaque-based
4410// qualifiers such that we should have only in/out/inout/constreadonly here.
John Kessenichd3ed90b2018-05-04 11:43:03 -06004411bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
John Kessenichd41993d2017-09-10 15:21:05 -06004412{
John Kessenich6a14f782017-12-04 02:48:10 -07004413 assert(qualifier == glslang::EvqIn ||
4414 qualifier == glslang::EvqOut ||
4415 qualifier == glslang::EvqInOut ||
rdbd8edfd82020-06-02 08:30:07 +02004416 qualifier == glslang::EvqUniform ||
John Kessenich6a14f782017-12-04 02:48:10 -07004417 qualifier == glslang::EvqConstReadOnly);
rdbd8edfd82020-06-02 08:30:07 +02004418 return qualifier != glslang::EvqConstReadOnly &&
4419 qualifier != glslang::EvqUniform;
John Kessenichd41993d2017-09-10 15:21:05 -06004420}
4421
4422// Is parameter pass-by-original?
4423bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
4424 bool implicitThisParam)
4425{
4426 if (implicitThisParam) // implicit this
4427 return true;
4428 if (glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich6a14f782017-12-04 02:48:10 -07004429 return paramType.getBasicType() == glslang::EbtBlock;
John Kessenichd41993d2017-09-10 15:21:05 -06004430 return paramType.containsOpaque() || // sampler, etc.
4431 (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO
4432}
4433
John Kessenich140f3df2015-06-26 16:58:36 -06004434// Make all the functions, skeletally, without actually visiting their bodies.
4435void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
4436{
John Kessenich8985fc92020-03-03 10:25:07 -07004437 const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type,
4438 bool useVulkanMemoryModel) {
John Kessenichfad62972017-07-18 02:35:46 -06004439 spv::Decoration paramPrecision = TranslatePrecisionDecoration(type);
4440 if (paramPrecision != spv::NoPrecision)
4441 decorations.push_back(paramPrecision);
Jeff Bolz36831c92018-09-05 10:11:41 -05004442 TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel);
John Kessenich7015bd62019-08-01 03:28:08 -06004443 if (type.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004444 // Original and non-writable params pass the pointer directly and
4445 // use restrict/aliased, others are stored to a pointer in Function
4446 // memory and use RestrictPointer/AliasedPointer.
4447 if (originalParam(type.getQualifier().storage, type, false) ||
4448 !writableParam(type.getQualifier().storage)) {
John Kessenichf8d1d742019-10-21 06:55:11 -06004449 decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict :
4450 spv::DecorationAliased);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004451 } else {
John Kessenichf8d1d742019-10-21 06:55:11 -06004452 decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT :
4453 spv::DecorationAliasedPointerEXT);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004454 }
4455 }
John Kessenichfad62972017-07-18 02:35:46 -06004456 };
4457
John Kessenich140f3df2015-06-26 16:58:36 -06004458 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4459 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
John Kessenich6fccb3c2016-09-19 16:01:41 -06004460 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
John Kessenich140f3df2015-06-26 16:58:36 -06004461 continue;
4462
4463 // We're on a user function. Set up the basic interface for the function now,
John Kessenich4bf71552016-09-02 11:20:21 -06004464 // so that it's available to call. Translating the body will happen later.
John Kessenich140f3df2015-06-26 16:58:36 -06004465 //
qining25262b32016-05-06 17:25:16 -04004466 // Typically (except for a "const in" parameter), an address will be passed to the
John Kessenich140f3df2015-06-26 16:58:36 -06004467 // function. What it is an address of varies:
4468 //
John Kessenich4bf71552016-09-02 11:20:21 -06004469 // - "in" parameters not marked as "const" can be written to without modifying the calling
4470 // argument so that write needs to be to a copy, hence the address of a copy works.
John Kessenich140f3df2015-06-26 16:58:36 -06004471 //
4472 // - "const in" parameters can just be the r-value, as no writes need occur.
4473 //
John Kessenich4bf71552016-09-02 11:20:21 -06004474 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
4475 // 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 -06004476
4477 std::vector<spv::Id> paramTypes;
John Kessenichfad62972017-07-18 02:35:46 -06004478 std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter
John Kessenich140f3df2015-06-26 16:58:36 -06004479 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
4480
John Kessenich155d3512019-08-08 23:29:20 -06004481#ifdef ENABLE_HLSL
John Kessenichfad62972017-07-18 02:35:46 -06004482 bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() ==
4483 glslangIntermediate->implicitThisName;
John Kessenich155d3512019-08-08 23:29:20 -06004484#else
4485 bool implicitThis = false;
4486#endif
John Kessenich37789792017-03-21 23:56:40 -06004487
John Kessenichfad62972017-07-18 02:35:46 -06004488 paramDecorations.resize(parameters.size());
John Kessenich140f3df2015-06-26 16:58:36 -06004489 for (int p = 0; p < (int)parameters.size(); ++p) {
4490 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
4491 spv::Id typeId = convertGlslangToSpvType(paramType);
John Kessenichd41993d2017-09-10 15:21:05 -06004492 if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0))
John Kessenicha5c5fb62017-05-05 05:09:58 -06004493 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
John Kessenichd41993d2017-09-10 15:21:05 -06004494 else if (writableParam(paramType.getQualifier().storage))
John Kessenich140f3df2015-06-26 16:58:36 -06004495 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
4496 else
John Kessenich4bf71552016-09-02 11:20:21 -06004497 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
Jeff Bolz36831c92018-09-05 10:11:41 -05004498 getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich140f3df2015-06-26 16:58:36 -06004499 paramTypes.push_back(typeId);
4500 }
4501
4502 spv::Block* functionBlock;
John Kessenich32cfd492016-02-02 12:37:46 -07004503 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
4504 convertGlslangToSpvType(glslFunction->getType()),
John Kessenichfad62972017-07-18 02:35:46 -06004505 glslFunction->getName().c_str(), paramTypes,
4506 paramDecorations, &functionBlock);
John Kessenich37789792017-03-21 23:56:40 -06004507 if (implicitThis)
4508 function->setImplicitThis();
John Kessenich140f3df2015-06-26 16:58:36 -06004509
4510 // Track function to emit/call later
4511 functionMap[glslFunction->getName().c_str()] = function;
4512
4513 // Set the parameter id's
4514 for (int p = 0; p < (int)parameters.size(); ++p) {
4515 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
4516 // give a name too
4517 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004518
4519 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
John Kessenichb9197c82019-08-11 07:41:45 -06004520 if (paramType.contains8BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004521 builder.addCapability(spv::CapabilityInt8);
John Kessenichb9197c82019-08-11 07:41:45 -06004522 if (paramType.contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004523 builder.addCapability(spv::CapabilityInt16);
John Kessenichb9197c82019-08-11 07:41:45 -06004524 if (paramType.contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004525 builder.addCapability(spv::CapabilityFloat16);
John Kessenich140f3df2015-06-26 16:58:36 -06004526 }
4527 }
4528}
4529
4530// Process all the initializers, while skipping the functions and link objects
4531void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
4532{
4533 builder.setBuildPoint(shaderEntry->getLastBlock());
4534 for (int i = 0; i < (int)initializers.size(); ++i) {
4535 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
John Kessenich8985fc92020-03-03 10:25:07 -07004536 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() !=
4537 glslang::EOpLinkerObjects) {
John Kessenich140f3df2015-06-26 16:58:36 -06004538
4539 // We're on a top-level node that's not a function. Treat as an initializer, whose
John Kessenich6fccb3c2016-09-19 16:01:41 -06004540 // code goes into the beginning of the entry point.
John Kessenich140f3df2015-06-26 16:58:36 -06004541 initializer->traverse(this);
4542 }
4543 }
4544}
4545
4546// Process all the functions, while skipping initializers.
4547void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
4548{
4549 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4550 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
John Kessenich6a60c2f2016-12-08 21:01:59 -07004551 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
John Kessenich140f3df2015-06-26 16:58:36 -06004552 node->traverse(this);
4553 }
4554}
4555
4556void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
4557{
qining25262b32016-05-06 17:25:16 -04004558 // SPIR-V functions should already be in the functionMap from the prepass
John Kessenich140f3df2015-06-26 16:58:36 -06004559 // that called makeFunctions().
John Kesseniched33e052016-10-06 12:59:51 -06004560 currentFunction = functionMap[node->getName().c_str()];
4561 spv::Block* functionBlock = currentFunction->getEntryBlock();
John Kessenich140f3df2015-06-26 16:58:36 -06004562 builder.setBuildPoint(functionBlock);
4563}
4564
John Kessenich8985fc92020-03-03 10:25:07 -07004565void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments,
4566 spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06004567{
Rex Xufc618912015-09-09 16:42:49 +08004568 const glslang::TIntermSequence& glslangArguments = node.getSequence();
Rex Xu48edadf2015-12-31 16:11:41 +08004569
4570 glslang::TSampler sampler = {};
4571 bool cubeCompare = false;
John Kessenicha28f7a72019-08-06 07:00:58 -06004572#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08004573 bool f16ShadowCompare = false;
4574#endif
Rex Xu5eafa472016-02-19 22:24:03 +08004575 if (node.isTexture() || node.isImage()) {
Rex Xu48edadf2015-12-31 16:11:41 +08004576 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
4577 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
John Kessenicha28f7a72019-08-06 07:00:58 -06004578#ifndef GLSLANG_WEB
John Kessenich8985fc92020-03-03 10:25:07 -07004579 f16ShadowCompare = sampler.shadow &&
4580 glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004581#endif
Rex Xu48edadf2015-12-31 16:11:41 +08004582 }
4583
John Kessenich140f3df2015-06-26 16:58:36 -06004584 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
4585 builder.clearAccessChain();
4586 glslangArguments[i]->traverse(this);
Rex Xufc618912015-09-09 16:42:49 +08004587
John Kessenicha28f7a72019-08-06 07:00:58 -06004588#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08004589 // Special case l-value operands
4590 bool lvalue = false;
4591 switch (node.getOp()) {
4592 case glslang::EOpImageAtomicAdd:
4593 case glslang::EOpImageAtomicMin:
4594 case glslang::EOpImageAtomicMax:
4595 case glslang::EOpImageAtomicAnd:
4596 case glslang::EOpImageAtomicOr:
4597 case glslang::EOpImageAtomicXor:
4598 case glslang::EOpImageAtomicExchange:
4599 case glslang::EOpImageAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05004600 case glslang::EOpImageAtomicLoad:
4601 case glslang::EOpImageAtomicStore:
Rex Xufc618912015-09-09 16:42:49 +08004602 if (i == 0)
4603 lvalue = true;
4604 break;
Rex Xu5eafa472016-02-19 22:24:03 +08004605 case glslang::EOpSparseImageLoad:
4606 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
4607 lvalue = true;
4608 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004609 case glslang::EOpSparseTexture:
4610 if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2))
4611 lvalue = true;
4612 break;
4613 case glslang::EOpSparseTextureClamp:
4614 if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3))
4615 lvalue = true;
4616 break;
4617 case glslang::EOpSparseTextureLod:
4618 case glslang::EOpSparseTextureOffset:
4619 if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3))
4620 lvalue = true;
4621 break;
Rex Xu48edadf2015-12-31 16:11:41 +08004622 case glslang::EOpSparseTextureFetch:
4623 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
4624 lvalue = true;
4625 break;
4626 case glslang::EOpSparseTextureFetchOffset:
4627 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
4628 lvalue = true;
4629 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004630 case glslang::EOpSparseTextureLodOffset:
4631 case glslang::EOpSparseTextureGrad:
4632 case glslang::EOpSparseTextureOffsetClamp:
4633 if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4))
4634 lvalue = true;
4635 break;
4636 case glslang::EOpSparseTextureGradOffset:
4637 case glslang::EOpSparseTextureGradClamp:
4638 if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5))
4639 lvalue = true;
4640 break;
4641 case glslang::EOpSparseTextureGradOffsetClamp:
4642 if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6))
4643 lvalue = true;
4644 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004645 case glslang::EOpSparseTextureGather:
Rex Xu48edadf2015-12-31 16:11:41 +08004646 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
4647 lvalue = true;
4648 break;
4649 case glslang::EOpSparseTextureGatherOffset:
4650 case glslang::EOpSparseTextureGatherOffsets:
4651 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
4652 lvalue = true;
4653 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004654 case glslang::EOpSparseTextureGatherLod:
4655 if (i == 3)
4656 lvalue = true;
4657 break;
4658 case glslang::EOpSparseTextureGatherLodOffset:
4659 case glslang::EOpSparseTextureGatherLodOffsets:
4660 if (i == 4)
4661 lvalue = true;
4662 break;
Rex Xu129799a2017-07-05 17:23:28 +08004663 case glslang::EOpSparseImageLoadLod:
4664 if (i == 3)
4665 lvalue = true;
4666 break;
Chao Chen3a137962018-09-19 11:41:27 -07004667 case glslang::EOpImageSampleFootprintNV:
4668 if (i == 4)
4669 lvalue = true;
4670 break;
4671 case glslang::EOpImageSampleFootprintClampNV:
4672 case glslang::EOpImageSampleFootprintLodNV:
4673 if (i == 5)
4674 lvalue = true;
4675 break;
4676 case glslang::EOpImageSampleFootprintGradNV:
4677 if (i == 6)
4678 lvalue = true;
4679 break;
4680 case glslang::EOpImageSampleFootprintGradClampNV:
4681 if (i == 7)
4682 lvalue = true;
4683 break;
Rex Xufc618912015-09-09 16:42:49 +08004684 default:
4685 break;
4686 }
4687
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004688 if (lvalue) {
Rex Xufc618912015-09-09 16:42:49 +08004689 arguments.push_back(builder.accessChainGetLValue());
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004690 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
4691 lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType());
4692 } else
John Kessenicha28f7a72019-08-06 07:00:58 -06004693#endif
John Kessenich32cfd492016-02-02 12:37:46 -07004694 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06004695 }
4696}
4697
John Kessenichfc51d282015-08-19 13:34:18 -06004698void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06004699{
John Kessenichfc51d282015-08-19 13:34:18 -06004700 builder.clearAccessChain();
4701 node.getOperand()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07004702 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
John Kessenichfc51d282015-08-19 13:34:18 -06004703}
John Kessenich140f3df2015-06-26 16:58:36 -06004704
John Kessenichfc51d282015-08-19 13:34:18 -06004705spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
4706{
John Kesseniche485c7a2017-05-31 18:50:53 -06004707 if (! node->isImage() && ! node->isTexture())
John Kessenichfc51d282015-08-19 13:34:18 -06004708 return spv::NoResult;
John Kesseniche485c7a2017-05-31 18:50:53 -06004709
greg-lunarg5d43c4a2018-12-07 17:36:33 -07004710 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06004711
John Kessenichfc51d282015-08-19 13:34:18 -06004712 // Process a GLSL texturing op (will be SPV image)
Jeff Bolz36831c92018-09-05 10:11:41 -05004713
John Kessenichf43c7392019-03-31 10:51:57 -06004714 const glslang::TType &imageType = node->getAsAggregate()
4715 ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType()
4716 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType();
Jeff Bolz36831c92018-09-05 10:11:41 -05004717 const glslang::TSampler sampler = imageType.getSampler();
John Kessenicha28f7a72019-08-06 07:00:58 -06004718#ifdef GLSLANG_WEB
4719 const bool f16ShadowCompare = false;
4720#else
Rex Xu1e5d7b02016-11-29 17:36:31 +08004721 bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate())
John Kessenichf43c7392019-03-31 10:51:57 -06004722 ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16
4723 : false;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004724#endif
4725
John Kessenichf43c7392019-03-31 10:51:57 -06004726 const auto signExtensionMask = [&]() {
4727 if (builder.getSpvVersion() >= spv::Spv_1_4) {
4728 if (sampler.type == glslang::EbtUint)
4729 return spv::ImageOperandsZeroExtendMask;
4730 else if (sampler.type == glslang::EbtInt)
4731 return spv::ImageOperandsSignExtendMask;
4732 }
4733 return spv::ImageOperandsMaskNone;
4734 };
4735
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004736 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
4737
John Kessenichfc51d282015-08-19 13:34:18 -06004738 std::vector<spv::Id> arguments;
4739 if (node->getAsAggregate())
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004740 translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06004741 else
4742 translateArguments(*node->getAsUnaryNode(), arguments);
John Kessenich12c155f2020-06-30 07:52:05 -06004743 spv::Decoration precision = TranslatePrecisionDecoration(node->getType());
John Kessenichfc51d282015-08-19 13:34:18 -06004744
4745 spv::Builder::TextureParameters params = { };
4746 params.sampler = arguments[0];
4747
Rex Xu04db3f52015-09-16 11:44:02 +08004748 glslang::TCrackedTextureOp cracked;
4749 node->crackTexture(sampler, cracked);
4750
amhagan05506bb2017-06-13 16:53:02 -04004751 const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint;
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004752
John Kessenichfc51d282015-08-19 13:34:18 -06004753 // Check for queries
4754 if (cracked.query) {
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004755 // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first
4756 if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler))
John Kessenich33661452015-12-08 19:32:47 -07004757 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004758
John Kessenichfc51d282015-08-19 13:34:18 -06004759 switch (node->getOp()) {
4760 case glslang::EOpImageQuerySize:
4761 case glslang::EOpTextureQuerySize:
John Kessenich140f3df2015-06-26 16:58:36 -06004762 if (arguments.size() > 1) {
4763 params.lod = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004764 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult);
John Kessenich140f3df2015-06-26 16:58:36 -06004765 } else
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004766 return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult);
John Kessenicha28f7a72019-08-06 07:00:58 -06004767#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06004768 case glslang::EOpImageQuerySamples:
4769 case glslang::EOpTextureQuerySamples:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004770 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004771 case glslang::EOpTextureQueryLod:
4772 params.coords = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004773 return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004774 case glslang::EOpTextureQueryLevels:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004775 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult);
Rex Xu48edadf2015-12-31 16:11:41 +08004776 case glslang::EOpSparseTexelsResident:
4777 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
John Kessenicha28f7a72019-08-06 07:00:58 -06004778#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004779 default:
4780 assert(0);
4781 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004782 }
John Kessenich140f3df2015-06-26 16:58:36 -06004783 }
4784
LoopDawg4425f242018-02-18 11:40:01 -07004785 int components = node->getType().getVectorSize();
4786
4787 if (node->getOp() == glslang::EOpTextureFetch) {
4788 // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed.
4789 // This will only happen through the HLSL path for operator[], so we do not have to handle e.g.
4790 // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic
4791 // here around e.g. which ones return scalars or other types.
4792 components = 4;
4793 }
4794
4795 glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components);
4796
4797 auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); };
4798
Rex Xufc618912015-09-09 16:42:49 +08004799 // Check for image functions other than queries
4800 if (node->isImage()) {
John Kessenich149afc32018-08-14 13:31:43 -06004801 std::vector<spv::IdImmediate> operands;
John Kessenich56bab042015-09-16 10:54:31 -06004802 auto opIt = arguments.begin();
John Kessenich149afc32018-08-14 13:31:43 -06004803 spv::IdImmediate image = { true, *(opIt++) };
4804 operands.push_back(image);
John Kessenich6c292d32016-02-15 20:58:50 -07004805
4806 // Handle subpass operations
4807 // TODO: GLSL should change to have the "MS" only on the type rather than the
4808 // built-in function.
4809 if (cracked.subpass) {
4810 // add on the (0,0) coordinate
4811 spv::Id zero = builder.makeIntConstant(0);
4812 std::vector<spv::Id> comps;
4813 comps.push_back(zero);
4814 comps.push_back(zero);
John Kessenich149afc32018-08-14 13:31:43 -06004815 spv::IdImmediate coord = { true,
4816 builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) };
4817 operands.push_back(coord);
John Kessenichf43c7392019-03-31 10:51:57 -06004818 spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone };
4819 imageOperands.word = imageOperands.word | signExtensionMask();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004820 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06004821 imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask;
4822 }
4823 if (imageOperands.word != spv::ImageOperandsMaskNone) {
John Kessenich149afc32018-08-14 13:31:43 -06004824 operands.push_back(imageOperands);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004825 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06004826 spv::IdImmediate imageOperand = { true, *(opIt++) };
4827 operands.push_back(imageOperand);
4828 }
John Kessenich6c292d32016-02-15 20:58:50 -07004829 }
John Kessenichfe4e5722017-10-19 02:07:30 -06004830 spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands);
4831 builder.setPrecision(result, precision);
4832 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07004833 }
4834
John Kessenich149afc32018-08-14 13:31:43 -06004835 spv::IdImmediate coord = { true, *(opIt++) };
4836 operands.push_back(coord);
Rex Xu129799a2017-07-05 17:23:28 +08004837 if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004838 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004839 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004840 mask = mask | spv::ImageOperandsSampleMask;
4841 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004842 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004843 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4844 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
Jeff Bolz36831c92018-09-05 10:11:41 -05004845 mask = mask | spv::ImageOperandsLodMask;
John Kessenich55e7d112015-11-15 21:33:39 -07004846 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004847 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4848 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004849 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004850 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004851 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4852 operands.push_back(imageOperands);
4853 }
4854 if (mask & spv::ImageOperandsSampleMask) {
4855 spv::IdImmediate imageOperand = { true, *opIt++ };
4856 operands.push_back(imageOperand);
4857 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004858 if (mask & spv::ImageOperandsLodMask) {
4859 spv::IdImmediate imageOperand = { true, *opIt++ };
4860 operands.push_back(imageOperand);
4861 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004862 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06004863 spv::IdImmediate imageOperand = { true,
4864 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05004865 operands.push_back(imageOperand);
4866 }
4867
John Kessenich149afc32018-08-14 13:31:43 -06004868 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004869 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
John Kessenichfe4e5722017-10-19 02:07:30 -06004870
John Kessenich149afc32018-08-14 13:31:43 -06004871 std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands));
LoopDawg4425f242018-02-18 11:40:01 -07004872 builder.setPrecision(result[0], precision);
4873
4874 // If needed, add a conversion constructor to the proper size.
4875 if (components != node->getType().getVectorSize())
4876 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4877
4878 return result[0];
Rex Xu129799a2017-07-05 17:23:28 +08004879 } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) {
Rex Xu129799a2017-07-05 17:23:28 +08004880
Jeff Bolz36831c92018-09-05 10:11:41 -05004881 // Push the texel value before the operands
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004882 if (sampler.isMultiSample() || cracked.lod) {
John Kessenich149afc32018-08-14 13:31:43 -06004883 spv::IdImmediate texel = { true, *(opIt + 1) };
4884 operands.push_back(texel);
John Kessenich149afc32018-08-14 13:31:43 -06004885 } else {
4886 spv::IdImmediate texel = { true, *opIt };
4887 operands.push_back(texel);
4888 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004889
4890 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004891 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004892 mask = mask | spv::ImageOperandsSampleMask;
4893 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004894 if (cracked.lod) {
4895 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4896 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4897 mask = mask | spv::ImageOperandsLodMask;
4898 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004899 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4900 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004901 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004902 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004903 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4904 operands.push_back(imageOperands);
4905 }
4906 if (mask & spv::ImageOperandsSampleMask) {
4907 spv::IdImmediate imageOperand = { true, *opIt++ };
4908 operands.push_back(imageOperand);
4909 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004910 if (mask & spv::ImageOperandsLodMask) {
4911 spv::IdImmediate imageOperand = { true, *opIt++ };
4912 operands.push_back(imageOperand);
4913 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004914 if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06004915 spv::IdImmediate imageOperand = { true,
4916 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05004917 operands.push_back(imageOperand);
4918 }
4919
John Kessenich56bab042015-09-16 10:54:31 -06004920 builder.createNoResultOp(spv::OpImageWrite, operands);
John Kessenich149afc32018-08-14 13:31:43 -06004921 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004922 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
John Kessenich56bab042015-09-16 10:54:31 -06004923 return spv::NoResult;
John Kessenichf43c7392019-03-31 10:51:57 -06004924 } else if (node->getOp() == glslang::EOpSparseImageLoad ||
4925 node->getOp() == glslang::EOpSparseImageLoadLod) {
Rex Xu5eafa472016-02-19 22:24:03 +08004926 builder.addCapability(spv::CapabilitySparseResidency);
John Kessenich149afc32018-08-14 13:31:43 -06004927 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
Rex Xu5eafa472016-02-19 22:24:03 +08004928 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
4929
Jeff Bolz36831c92018-09-05 10:11:41 -05004930 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004931 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004932 mask = mask | spv::ImageOperandsSampleMask;
4933 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004934 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004935 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4936 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4937
Jeff Bolz36831c92018-09-05 10:11:41 -05004938 mask = mask | spv::ImageOperandsLodMask;
4939 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004940 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4941 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004942 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004943 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004944 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
John Kessenich149afc32018-08-14 13:31:43 -06004945 operands.push_back(imageOperands);
Jeff Bolz36831c92018-09-05 10:11:41 -05004946 }
4947 if (mask & spv::ImageOperandsSampleMask) {
John Kessenich149afc32018-08-14 13:31:43 -06004948 spv::IdImmediate imageOperand = { true, *opIt++ };
4949 operands.push_back(imageOperand);
Jeff Bolz36831c92018-09-05 10:11:41 -05004950 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004951 if (mask & spv::ImageOperandsLodMask) {
4952 spv::IdImmediate imageOperand = { true, *opIt++ };
4953 operands.push_back(imageOperand);
4954 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004955 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
John Kessenich8985fc92020-03-03 10:25:07 -07004956 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(
4957 TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05004958 operands.push_back(imageOperand);
Rex Xu5eafa472016-02-19 22:24:03 +08004959 }
4960
4961 // Create the return type that was a special structure
4962 spv::Id texelOut = *opIt;
John Kessenich8c8505c2016-07-26 12:50:38 -06004963 spv::Id typeId0 = resultType();
Rex Xu5eafa472016-02-19 22:24:03 +08004964 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
4965 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
4966
4967 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
4968
4969 // Decode the return type
4970 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
4971 return builder.createCompositeExtract(resultId, typeId0, 0);
John Kessenichcd261442016-01-22 09:54:12 -07004972 } else {
Rex Xu6b86d492015-09-16 17:48:22 +08004973 // Process image atomic operations
4974
4975 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
4976 // as the first source operand, is required by SPIR-V atomic operations.
John Kessenich149afc32018-08-14 13:31:43 -06004977 // For non-MS, the sample value should be 0
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004978 spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) };
John Kessenich149afc32018-08-14 13:31:43 -06004979 operands.push_back(sample);
John Kessenich140f3df2015-06-26 16:58:36 -06004980
Jeff Bolz36831c92018-09-05 10:11:41 -05004981 spv::Id resultTypeId;
4982 // imageAtomicStore has a void return type so base the pointer type on
4983 // the type of the value operand.
4984 if (node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufb18b6d2020-02-22 22:04:31 +08004985 resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt));
Jeff Bolz36831c92018-09-05 10:11:41 -05004986 } else {
4987 resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
4988 }
John Kessenich56bab042015-09-16 10:54:31 -06004989 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
Jeff Bolz39ffdaf2020-03-09 10:48:12 -05004990 if (imageType.getQualifier().nonUniform) {
4991 builder.addDecoration(pointer, spv::DecorationNonUniformEXT);
4992 }
Rex Xufc618912015-09-09 16:42:49 +08004993
4994 std::vector<spv::Id> operands;
4995 operands.push_back(pointer);
4996 for (; opIt != arguments.end(); ++opIt)
4997 operands.push_back(*opIt);
4998
John Kessenich8985fc92020-03-03 10:25:07 -07004999 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(),
5000 lvalueCoherentFlags);
Rex Xufc618912015-09-09 16:42:49 +08005001 }
5002 }
5003
John Kessenicha28f7a72019-08-06 07:00:58 -06005004#ifndef GLSLANG_WEB
amhagan05506bb2017-06-13 16:53:02 -04005005 // Check for fragment mask functions other than queries
5006 if (cracked.fragMask) {
5007 assert(sampler.ms);
5008
5009 auto opIt = arguments.begin();
5010 std::vector<spv::Id> operands;
5011
5012 // Extract the image if necessary
5013 if (builder.isSampledImage(params.sampler))
5014 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
5015
5016 operands.push_back(params.sampler);
5017 ++opIt;
5018
5019 if (sampler.isSubpass()) {
5020 // add on the (0,0) coordinate
5021 spv::Id zero = builder.makeIntConstant(0);
5022 std::vector<spv::Id> comps;
5023 comps.push_back(zero);
5024 comps.push_back(zero);
John Kessenich8985fc92020-03-03 10:25:07 -07005025 operands.push_back(builder.makeCompositeConstant(
5026 builder.makeVectorType(builder.makeIntType(32), 2), comps));
amhagan05506bb2017-06-13 16:53:02 -04005027 }
5028
5029 for (; opIt != arguments.end(); ++opIt)
5030 operands.push_back(*opIt);
5031
5032 spv::Op fragMaskOp = spv::OpNop;
5033 if (node->getOp() == glslang::EOpFragmentMaskFetch)
5034 fragMaskOp = spv::OpFragmentMaskFetchAMD;
5035 else if (node->getOp() == glslang::EOpFragmentFetch)
5036 fragMaskOp = spv::OpFragmentFetchAMD;
5037
5038 builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask);
5039 builder.addCapability(spv::CapabilityFragmentMaskAMD);
5040 return builder.createOp(fragMaskOp, resultType(), operands);
5041 }
5042#endif
5043
Rex Xufc618912015-09-09 16:42:49 +08005044 // Check for texture functions other than queries
Rex Xu48edadf2015-12-31 16:11:41 +08005045 bool sparse = node->isSparseTexture();
Chao Chen3a137962018-09-19 11:41:27 -07005046 bool imageFootprint = node->isImageFootprint();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005047 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow();
Rex Xu71519fe2015-11-11 15:35:47 +08005048
John Kessenichfc51d282015-08-19 13:34:18 -06005049 // check for bias argument
5050 bool bias = false;
Rex Xu225e0fc2016-11-17 17:47:59 +08005051 if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06005052 int nonBiasArgCount = 2;
Rex Xu225e0fc2016-11-17 17:47:59 +08005053 if (cracked.gather)
5054 ++nonBiasArgCount; // comp argument should be present when bias argument is present
Rex Xu1e5d7b02016-11-29 17:36:31 +08005055
5056 if (f16ShadowCompare)
5057 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06005058 if (cracked.offset)
5059 ++nonBiasArgCount;
Rex Xu225e0fc2016-11-17 17:47:59 +08005060 else if (cracked.offsets)
5061 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06005062 if (cracked.grad)
5063 nonBiasArgCount += 2;
Rex Xu48edadf2015-12-31 16:11:41 +08005064 if (cracked.lodClamp)
5065 ++nonBiasArgCount;
5066 if (sparse)
5067 ++nonBiasArgCount;
Chao Chen3a137962018-09-19 11:41:27 -07005068 if (imageFootprint)
5069 //Following three extra arguments
5070 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
5071 nonBiasArgCount += 3;
John Kessenichfc51d282015-08-19 13:34:18 -06005072 if ((int)arguments.size() > nonBiasArgCount)
5073 bias = true;
5074 }
5075
John Kessenicha5c33d62016-06-02 23:45:21 -06005076 // See if the sampler param should really be just the SPV image part
5077 if (cracked.fetch) {
5078 // a fetch needs to have the image extracted first
5079 if (builder.isSampledImage(params.sampler))
5080 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
5081 }
5082
John Kessenicha28f7a72019-08-06 07:00:58 -06005083#ifndef GLSLANG_WEB
Rex Xu225e0fc2016-11-17 17:47:59 +08005084 if (cracked.gather) {
5085 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
5086 if (bias || cracked.lod ||
5087 sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) {
5088 builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod);
Rex Xu301a2bc2017-06-14 23:09:39 +08005089 builder.addCapability(spv::CapabilityImageGatherBiasLodAMD);
Rex Xu225e0fc2016-11-17 17:47:59 +08005090 }
5091 }
5092#endif
5093
John Kessenichfc51d282015-08-19 13:34:18 -06005094 // set the rest of the arguments
John Kessenich55e7d112015-11-15 21:33:39 -07005095
John Kessenichfc51d282015-08-19 13:34:18 -06005096 params.coords = arguments[1];
5097 int extraArgs = 0;
John Kessenich019f08f2016-02-15 15:40:42 -07005098 bool noImplicitLod = false;
John Kessenich55e7d112015-11-15 21:33:39 -07005099
5100 // sort out where Dref is coming from
Rex Xu1e5d7b02016-11-29 17:36:31 +08005101 if (cubeCompare || f16ShadowCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06005102 params.Dref = arguments[2];
Rex Xu48edadf2015-12-31 16:11:41 +08005103 ++extraArgs;
5104 } else if (sampler.shadow && cracked.gather) {
John Kessenich55e7d112015-11-15 21:33:39 -07005105 params.Dref = arguments[2];
5106 ++extraArgs;
5107 } else if (sampler.shadow) {
John Kessenichfc51d282015-08-19 13:34:18 -06005108 std::vector<spv::Id> indexes;
John Kessenich76d4dfc2016-06-16 12:43:23 -06005109 int dRefComp;
John Kessenichfc51d282015-08-19 13:34:18 -06005110 if (cracked.proj)
John Kessenich76d4dfc2016-06-16 12:43:23 -06005111 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
John Kessenichfc51d282015-08-19 13:34:18 -06005112 else
John Kessenich76d4dfc2016-06-16 12:43:23 -06005113 dRefComp = builder.getNumComponents(params.coords) - 1;
5114 indexes.push_back(dRefComp);
John Kessenich8985fc92020-03-03 10:25:07 -07005115 params.Dref = builder.createCompositeExtract(params.coords,
5116 builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
John Kessenichfc51d282015-08-19 13:34:18 -06005117 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005118
5119 // lod
John Kessenichfc51d282015-08-19 13:34:18 -06005120 if (cracked.lod) {
LoopDawgef94b1a2017-07-24 18:45:37 -06005121 params.lod = arguments[2 + extraArgs];
John Kessenichfc51d282015-08-19 13:34:18 -06005122 ++extraArgs;
John Kessenichb9197c82019-08-11 07:41:45 -06005123 } else if (glslangIntermediate->getStage() != EShLangFragment &&
5124 !(glslangIntermediate->getStage() == EShLangCompute &&
5125 glslangIntermediate->hasLayoutDerivativeModeNone())) {
John Kessenich019f08f2016-02-15 15:40:42 -07005126 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
5127 noImplicitLod = true;
5128 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005129
5130 // multisample
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005131 if (sampler.isMultiSample()) {
LoopDawgef94b1a2017-07-24 18:45:37 -06005132 params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified
Rex Xu04db3f52015-09-16 11:44:02 +08005133 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06005134 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005135
5136 // gradient
John Kessenichfc51d282015-08-19 13:34:18 -06005137 if (cracked.grad) {
5138 params.gradX = arguments[2 + extraArgs];
5139 params.gradY = arguments[3 + extraArgs];
5140 extraArgs += 2;
5141 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005142
5143 // offset and offsets
John Kessenich55e7d112015-11-15 21:33:39 -07005144 if (cracked.offset) {
John Kessenichfc51d282015-08-19 13:34:18 -06005145 params.offset = arguments[2 + extraArgs];
5146 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07005147 } else if (cracked.offsets) {
5148 params.offsets = arguments[2 + extraArgs];
5149 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06005150 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005151
John Kessenich3e4b6ff2019-08-08 01:15:24 -06005152#ifndef GLSLANG_WEB
John Kessenich76d4dfc2016-06-16 12:43:23 -06005153 // lod clamp
Rex Xu48edadf2015-12-31 16:11:41 +08005154 if (cracked.lodClamp) {
5155 params.lodClamp = arguments[2 + extraArgs];
5156 ++extraArgs;
5157 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005158 // sparse
Rex Xu48edadf2015-12-31 16:11:41 +08005159 if (sparse) {
5160 params.texelOut = arguments[2 + extraArgs];
5161 ++extraArgs;
5162 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06005163 // gather component
John Kessenich55e7d112015-11-15 21:33:39 -07005164 if (cracked.gather && ! sampler.shadow) {
5165 // default component is 0, if missing, otherwise an argument
5166 if (2 + extraArgs < (int)arguments.size()) {
John Kessenich76d4dfc2016-06-16 12:43:23 -06005167 params.component = arguments[2 + extraArgs];
John Kessenich55e7d112015-11-15 21:33:39 -07005168 ++extraArgs;
Rex Xu225e0fc2016-11-17 17:47:59 +08005169 } else
John Kessenich76d4dfc2016-06-16 12:43:23 -06005170 params.component = builder.makeIntConstant(0);
Rex Xu225e0fc2016-11-17 17:47:59 +08005171 }
Chao Chen3a137962018-09-19 11:41:27 -07005172 spv::Id resultStruct = spv::NoResult;
5173 if (imageFootprint) {
5174 //Following three extra arguments
5175 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
5176 params.granularity = arguments[2 + extraArgs];
5177 params.coarse = arguments[3 + extraArgs];
5178 resultStruct = arguments[4 + extraArgs];
5179 extraArgs += 3;
5180 }
5181#endif
Rex Xu225e0fc2016-11-17 17:47:59 +08005182 // bias
5183 if (bias) {
5184 params.bias = arguments[2 + extraArgs];
5185 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07005186 }
John Kessenichfc51d282015-08-19 13:34:18 -06005187
John Kessenicha28f7a72019-08-06 07:00:58 -06005188#ifndef GLSLANG_WEB
Chao Chen3a137962018-09-19 11:41:27 -07005189 if (imageFootprint) {
5190 builder.addExtension(spv::E_SPV_NV_shader_image_footprint);
5191 builder.addCapability(spv::CapabilityImageFootprintNV);
5192
5193
5194 //resultStructType(OpenGL type) contains 5 elements:
5195 //struct gl_TextureFootprint2DNV {
5196 // uvec2 anchor;
5197 // uvec2 offset;
5198 // uvec2 mask;
5199 // uint lod;
5200 // uint granularity;
5201 //};
5202 //or
5203 //struct gl_TextureFootprint3DNV {
5204 // uvec3 anchor;
5205 // uvec3 offset;
5206 // uvec2 mask;
5207 // uint lod;
5208 // uint granularity;
5209 //};
5210 spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct));
5211 assert(builder.isStructType(resultStructType));
5212
5213 //resType (SPIR-V type) contains 6 elements:
5214 //Member 0 must be a Boolean type scalar(LOD),
5215 //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor),
5216 //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset),
5217 //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask),
5218 //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod),
5219 //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity).
5220 std::vector<spv::Id> members;
5221 members.push_back(resultType());
5222 for (int i = 0; i < 5; i++) {
5223 members.push_back(builder.getContainedTypeId(resultStructType, i));
5224 }
5225 spv::Id resType = builder.makeStructType(members, "ResType");
5226
5227 //call ImageFootprintNV
John Kessenichf43c7392019-03-31 10:51:57 -06005228 spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj,
5229 cracked.gather, noImplicitLod, params, signExtensionMask());
Chao Chen3a137962018-09-19 11:41:27 -07005230
5231 //copy resType (SPIR-V type) to resultStructType(OpenGL type)
5232 for (int i = 0; i < 5; i++) {
5233 builder.clearAccessChain();
5234 builder.setAccessChainLValue(resultStruct);
5235
5236 //Accessing to a struct we created, no coherent flag is set
5237 spv::Builder::AccessChain::CoherentFlags flags;
5238 flags.clear();
5239
Jeff Bolz9f2aec42019-01-06 17:58:04 -06005240 builder.accessChainPush(builder.makeIntConstant(i), flags, 0);
John Kessenich8985fc92020-03-03 10:25:07 -07005241 builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1),
5242 i+1));
Chao Chen3a137962018-09-19 11:41:27 -07005243 }
5244 return builder.createCompositeExtract(res, resultType(), 0);
5245 }
5246#endif
5247
John Kessenich65336482016-06-16 14:06:26 -06005248 // projective component (might not to move)
5249 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
5250 // are divided by the last component of P."
5251 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
5252 // unused components will appear after all used components."
5253 if (cracked.proj) {
5254 int projSourceComp = builder.getNumComponents(params.coords) - 1;
5255 int projTargetComp;
5256 switch (sampler.dim) {
5257 case glslang::Esd1D: projTargetComp = 1; break;
5258 case glslang::Esd2D: projTargetComp = 2; break;
5259 case glslang::EsdRect: projTargetComp = 2; break;
5260 default: projTargetComp = projSourceComp; break;
5261 }
5262 // copy the projective coordinate if we have to
5263 if (projTargetComp != projSourceComp) {
John Kessenichecba76f2017-01-06 00:34:48 -07005264 spv::Id projComp = builder.createCompositeExtract(params.coords,
John Kessenich8985fc92020-03-03 10:25:07 -07005265 builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp);
John Kessenich65336482016-06-16 14:06:26 -06005266 params.coords = builder.createCompositeInsert(projComp, params.coords,
John Kessenich8985fc92020-03-03 10:25:07 -07005267 builder.getTypeId(params.coords), projTargetComp);
John Kessenich65336482016-06-16 14:06:26 -06005268 }
5269 }
5270
John Kessenichf8d1d742019-10-21 06:55:11 -06005271#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -05005272 // nonprivate
5273 if (imageType.getQualifier().nonprivate) {
5274 params.nonprivate = true;
5275 }
5276
5277 // volatile
5278 if (imageType.getQualifier().volatil) {
5279 params.volatil = true;
5280 }
John Kessenichf8d1d742019-10-21 06:55:11 -06005281#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05005282
St0fFa1184dd2018-04-09 21:08:14 +02005283 std::vector<spv::Id> result( 1,
John Kessenichf43c7392019-03-31 10:51:57 -06005284 builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather,
5285 noImplicitLod, params, signExtensionMask())
St0fFa1184dd2018-04-09 21:08:14 +02005286 );
LoopDawg4425f242018-02-18 11:40:01 -07005287
5288 if (components != node->getType().getVectorSize())
5289 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
5290
5291 return result[0];
John Kessenich140f3df2015-06-26 16:58:36 -06005292}
5293
5294spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
5295{
5296 // Grab the function's pointer from the previously created function
5297 spv::Function* function = functionMap[node->getName().c_str()];
5298 if (! function)
5299 return 0;
5300
5301 const glslang::TIntermSequence& glslangArgs = node->getSequence();
5302 const glslang::TQualifierList& qualifiers = node->getQualifierList();
5303
5304 // See comments in makeFunctions() for details about the semantics for parameter passing.
5305 //
5306 // These imply we need a four step process:
5307 // 1. Evaluate the arguments
5308 // 2. Allocate and make copies of in, out, and inout arguments
5309 // 3. Make the call
5310 // 4. Copy back the results
5311
John Kessenichd3ed90b2018-05-04 11:43:03 -06005312 // 1. Evaluate the arguments and their types
John Kessenich140f3df2015-06-26 16:58:36 -06005313 std::vector<spv::Builder::AccessChain> lValues;
5314 std::vector<spv::Id> rValues;
John Kessenich32cfd492016-02-02 12:37:46 -07005315 std::vector<const glslang::TType*> argTypes;
John Kessenich140f3df2015-06-26 16:58:36 -06005316 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005317 argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06005318 // build l-value
5319 builder.clearAccessChain();
5320 glslangArgs[a]->traverse(this);
John Kessenichd41993d2017-09-10 15:21:05 -06005321 // keep outputs and pass-by-originals as l-values, evaluate others as r-values
John Kessenichd3ed90b2018-05-04 11:43:03 -06005322 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) ||
John Kessenich6a14f782017-12-04 02:48:10 -07005323 writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005324 // save l-value
5325 lValues.push_back(builder.getAccessChain());
5326 } else {
5327 // process r-value
John Kessenich32cfd492016-02-02 12:37:46 -07005328 rValues.push_back(accessChainLoad(*argTypes.back()));
John Kessenich140f3df2015-06-26 16:58:36 -06005329 }
5330 }
5331
5332 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
5333 // copy the original into that space.
5334 //
5335 // Also, build up the list of actual arguments to pass in for the call
5336 int lValueCount = 0;
5337 int rValueCount = 0;
5338 std::vector<spv::Id> spvArgs;
5339 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
5340 spv::Id arg;
John Kessenichd3ed90b2018-05-04 11:43:03 -06005341 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07005342 builder.setAccessChain(lValues[lValueCount]);
5343 arg = builder.accessChainGetLValue();
5344 ++lValueCount;
John Kessenichd41993d2017-09-10 15:21:05 -06005345 } else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005346 // need space to hold the copy
John Kessenich435dd802020-06-30 01:27:08 -06005347 arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction,
John Kessenich8985fc92020-03-03 10:25:07 -07005348 builder.getContainedTypeId(function->getParamType(a)), "param");
John Kessenich140f3df2015-06-26 16:58:36 -06005349 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
5350 // need to copy the input into output space
5351 builder.setAccessChain(lValues[lValueCount]);
John Kessenich32cfd492016-02-02 12:37:46 -07005352 spv::Id copy = accessChainLoad(*argTypes[a]);
John Kessenich4bf71552016-09-02 11:20:21 -06005353 builder.clearAccessChain();
5354 builder.setAccessChainLValue(arg);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005355 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005356 }
5357 ++lValueCount;
5358 } else {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005359 // process r-value, which involves a copy for a type mismatch
John Kessenich4df10332020-06-26 08:37:06 -06005360 if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) ||
John Kessenich435dd802020-06-30 01:27:08 -06005361 TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a))
John Kessenich4df10332020-06-26 08:37:06 -06005362 {
John Kessenich435dd802020-06-30 01:27:08 -06005363 spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, function->getParamType(a), "arg");
John Kessenichd3ed90b2018-05-04 11:43:03 -06005364 builder.clearAccessChain();
5365 builder.setAccessChainLValue(argCopy);
5366 multiTypeStore(*argTypes[a], rValues[rValueCount]);
John Kessenich435dd802020-06-30 01:27:08 -06005367 arg = builder.createLoad(argCopy, function->getParamPrecision(a));
John Kessenichd3ed90b2018-05-04 11:43:03 -06005368 } else
5369 arg = rValues[rValueCount];
John Kessenich140f3df2015-06-26 16:58:36 -06005370 ++rValueCount;
5371 }
5372 spvArgs.push_back(arg);
5373 }
5374
5375 // 3. Make the call.
5376 spv::Id result = builder.createFunctionCall(function, spvArgs);
John Kessenich32cfd492016-02-02 12:37:46 -07005377 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06005378
5379 // 4. Copy back out an "out" arguments.
5380 lValueCount = 0;
5381 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005382 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0))
John Kessenichd41993d2017-09-10 15:21:05 -06005383 ++lValueCount;
5384 else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005385 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
John Kessenich435dd802020-06-30 01:27:08 -06005386 spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision);
John Kessenich140f3df2015-06-26 16:58:36 -06005387 builder.setAccessChain(lValues[lValueCount]);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005388 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005389 }
5390 ++lValueCount;
5391 }
5392 }
5393
5394 return result;
5395}
5396
5397// Translate AST operation to SPV operation, already having SPV-based operands/types.
John Kessenichead86222018-03-28 18:01:20 -06005398spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06005399 spv::Id typeId, spv::Id left, spv::Id right,
5400 glslang::TBasicType typeProxy, bool reduceComparison)
5401{
John Kessenich66011cb2018-03-06 16:12:04 -07005402 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5403 bool isFloat = isTypeFloat(typeProxy);
Rex Xuc7d36562016-04-27 08:15:37 +08005404 bool isBool = typeProxy == glslang::EbtBool;
John Kessenich140f3df2015-06-26 16:58:36 -06005405
5406 spv::Op binOp = spv::OpNop;
John Kessenichec43d0a2015-07-04 17:17:31 -06005407 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
John Kessenich140f3df2015-06-26 16:58:36 -06005408 bool comparison = false;
5409
5410 switch (op) {
5411 case glslang::EOpAdd:
5412 case glslang::EOpAddAssign:
5413 if (isFloat)
5414 binOp = spv::OpFAdd;
5415 else
5416 binOp = spv::OpIAdd;
5417 break;
5418 case glslang::EOpSub:
5419 case glslang::EOpSubAssign:
5420 if (isFloat)
5421 binOp = spv::OpFSub;
5422 else
5423 binOp = spv::OpISub;
5424 break;
5425 case glslang::EOpMul:
5426 case glslang::EOpMulAssign:
5427 if (isFloat)
5428 binOp = spv::OpFMul;
5429 else
5430 binOp = spv::OpIMul;
5431 break;
5432 case glslang::EOpVectorTimesScalar:
5433 case glslang::EOpVectorTimesScalarAssign:
John Kessenich8d72f1a2016-05-20 12:06:03 -06005434 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
John Kessenichec43d0a2015-07-04 17:17:31 -06005435 if (builder.isVector(right))
5436 std::swap(left, right);
5437 assert(builder.isScalar(right));
5438 needMatchingVectors = false;
5439 binOp = spv::OpVectorTimesScalar;
t.jung697fdf02018-11-14 13:04:39 +01005440 } else if (isFloat)
5441 binOp = spv::OpFMul;
5442 else
John Kessenichec43d0a2015-07-04 17:17:31 -06005443 binOp = spv::OpIMul;
John Kessenich140f3df2015-06-26 16:58:36 -06005444 break;
5445 case glslang::EOpVectorTimesMatrix:
5446 case glslang::EOpVectorTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005447 binOp = spv::OpVectorTimesMatrix;
5448 break;
5449 case glslang::EOpMatrixTimesVector:
John Kessenich140f3df2015-06-26 16:58:36 -06005450 binOp = spv::OpMatrixTimesVector;
5451 break;
5452 case glslang::EOpMatrixTimesScalar:
5453 case glslang::EOpMatrixTimesScalarAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005454 binOp = spv::OpMatrixTimesScalar;
5455 break;
5456 case glslang::EOpMatrixTimesMatrix:
5457 case glslang::EOpMatrixTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005458 binOp = spv::OpMatrixTimesMatrix;
5459 break;
5460 case glslang::EOpOuterProduct:
5461 binOp = spv::OpOuterProduct;
John Kessenichec43d0a2015-07-04 17:17:31 -06005462 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005463 break;
5464
5465 case glslang::EOpDiv:
5466 case glslang::EOpDivAssign:
5467 if (isFloat)
5468 binOp = spv::OpFDiv;
5469 else if (isUnsigned)
5470 binOp = spv::OpUDiv;
5471 else
5472 binOp = spv::OpSDiv;
5473 break;
5474 case glslang::EOpMod:
5475 case glslang::EOpModAssign:
5476 if (isFloat)
5477 binOp = spv::OpFMod;
5478 else if (isUnsigned)
5479 binOp = spv::OpUMod;
5480 else
5481 binOp = spv::OpSMod;
5482 break;
5483 case glslang::EOpRightShift:
5484 case glslang::EOpRightShiftAssign:
5485 if (isUnsigned)
5486 binOp = spv::OpShiftRightLogical;
5487 else
5488 binOp = spv::OpShiftRightArithmetic;
5489 break;
5490 case glslang::EOpLeftShift:
5491 case glslang::EOpLeftShiftAssign:
5492 binOp = spv::OpShiftLeftLogical;
5493 break;
5494 case glslang::EOpAnd:
5495 case glslang::EOpAndAssign:
5496 binOp = spv::OpBitwiseAnd;
5497 break;
5498 case glslang::EOpLogicalAnd:
John Kessenichec43d0a2015-07-04 17:17:31 -06005499 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005500 binOp = spv::OpLogicalAnd;
5501 break;
5502 case glslang::EOpInclusiveOr:
5503 case glslang::EOpInclusiveOrAssign:
5504 binOp = spv::OpBitwiseOr;
5505 break;
5506 case glslang::EOpLogicalOr:
John Kessenichec43d0a2015-07-04 17:17:31 -06005507 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005508 binOp = spv::OpLogicalOr;
5509 break;
5510 case glslang::EOpExclusiveOr:
5511 case glslang::EOpExclusiveOrAssign:
5512 binOp = spv::OpBitwiseXor;
5513 break;
5514 case glslang::EOpLogicalXor:
John Kessenichec43d0a2015-07-04 17:17:31 -06005515 needMatchingVectors = false;
John Kessenich5e4b1242015-08-06 22:53:06 -06005516 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005517 break;
5518
Ian Romanickb3bd4022019-01-21 08:57:25 -08005519 case glslang::EOpAbsDifference:
5520 binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL;
5521 break;
5522
5523 case glslang::EOpAddSaturate:
5524 binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL;
5525 break;
5526
5527 case glslang::EOpSubSaturate:
5528 binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL;
5529 break;
5530
5531 case glslang::EOpAverage:
5532 binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL;
5533 break;
5534
5535 case glslang::EOpAverageRounded:
5536 binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL;
5537 break;
5538
5539 case glslang::EOpMul32x16:
5540 binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL;
5541 break;
5542
John Kessenich140f3df2015-06-26 16:58:36 -06005543 case glslang::EOpLessThan:
5544 case glslang::EOpGreaterThan:
5545 case glslang::EOpLessThanEqual:
5546 case glslang::EOpGreaterThanEqual:
5547 case glslang::EOpEqual:
5548 case glslang::EOpNotEqual:
5549 case glslang::EOpVectorEqual:
5550 case glslang::EOpVectorNotEqual:
5551 comparison = true;
5552 break;
5553 default:
5554 break;
5555 }
5556
John Kessenich7c1aa102015-10-15 13:29:11 -06005557 // handle mapped binary operations (should be non-comparison)
John Kessenich140f3df2015-06-26 16:58:36 -06005558 if (binOp != spv::OpNop) {
John Kessenich7c1aa102015-10-15 13:29:11 -06005559 assert(comparison == false);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005560 if (builder.isMatrix(left) || builder.isMatrix(right) ||
5561 builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
John Kessenichead86222018-03-28 18:01:20 -06005562 return createBinaryMatrixOperation(binOp, decorations, typeId, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005563
5564 // No matrix involved; make both operands be the same number of components, if needed
John Kessenichec43d0a2015-07-04 17:17:31 -06005565 if (needMatchingVectors)
John Kessenichead86222018-03-28 18:01:20 -06005566 builder.promoteScalar(decorations.precision, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005567
qining25262b32016-05-06 17:25:16 -04005568 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005569 decorations.addNoContraction(builder, result);
5570 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005571 return builder.setPrecision(result, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06005572 }
5573
5574 if (! comparison)
5575 return 0;
5576
John Kessenich7c1aa102015-10-15 13:29:11 -06005577 // Handle comparison instructions
John Kessenich140f3df2015-06-26 16:58:36 -06005578
John Kessenich4583b612016-08-07 19:14:22 -06005579 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
John Kessenichead86222018-03-28 18:01:20 -06005580 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) {
5581 spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual);
John Kessenichb9197c82019-08-11 07:41:45 -06005582 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005583 return result;
5584 }
John Kessenich140f3df2015-06-26 16:58:36 -06005585
5586 switch (op) {
5587 case glslang::EOpLessThan:
5588 if (isFloat)
5589 binOp = spv::OpFOrdLessThan;
5590 else if (isUnsigned)
5591 binOp = spv::OpULessThan;
5592 else
5593 binOp = spv::OpSLessThan;
5594 break;
5595 case glslang::EOpGreaterThan:
5596 if (isFloat)
5597 binOp = spv::OpFOrdGreaterThan;
5598 else if (isUnsigned)
5599 binOp = spv::OpUGreaterThan;
5600 else
5601 binOp = spv::OpSGreaterThan;
5602 break;
5603 case glslang::EOpLessThanEqual:
5604 if (isFloat)
5605 binOp = spv::OpFOrdLessThanEqual;
5606 else if (isUnsigned)
5607 binOp = spv::OpULessThanEqual;
5608 else
5609 binOp = spv::OpSLessThanEqual;
5610 break;
5611 case glslang::EOpGreaterThanEqual:
5612 if (isFloat)
5613 binOp = spv::OpFOrdGreaterThanEqual;
5614 else if (isUnsigned)
5615 binOp = spv::OpUGreaterThanEqual;
5616 else
5617 binOp = spv::OpSGreaterThanEqual;
5618 break;
5619 case glslang::EOpEqual:
5620 case glslang::EOpVectorEqual:
5621 if (isFloat)
5622 binOp = spv::OpFOrdEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005623 else if (isBool)
5624 binOp = spv::OpLogicalEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005625 else
5626 binOp = spv::OpIEqual;
5627 break;
5628 case glslang::EOpNotEqual:
5629 case glslang::EOpVectorNotEqual:
5630 if (isFloat)
Graeme Leese65ce5662020-06-05 13:32:51 +01005631 binOp = spv::OpFUnordNotEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005632 else if (isBool)
5633 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005634 else
5635 binOp = spv::OpINotEqual;
5636 break;
5637 default:
5638 break;
5639 }
5640
qining25262b32016-05-06 17:25:16 -04005641 if (binOp != spv::OpNop) {
5642 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005643 decorations.addNoContraction(builder, result);
5644 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005645 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005646 }
John Kessenich140f3df2015-06-26 16:58:36 -06005647
5648 return 0;
5649}
5650
John Kessenich04bb8a02015-12-12 12:28:14 -07005651//
5652// Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
5653// These can be any of:
5654//
5655// matrix * scalar
5656// scalar * matrix
5657// matrix * matrix linear algebraic
5658// matrix * vector
5659// vector * matrix
5660// matrix * matrix componentwise
5661// matrix op matrix op in {+, -, /}
5662// matrix op scalar op in {+, -, /}
5663// scalar op matrix op in {+, -, /}
5664//
John Kessenichead86222018-03-28 18:01:20 -06005665spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5666 spv::Id left, spv::Id right)
John Kessenich04bb8a02015-12-12 12:28:14 -07005667{
5668 bool firstClass = true;
5669
5670 // First, handle first-class matrix operations (* and matrix/scalar)
5671 switch (op) {
5672 case spv::OpFDiv:
5673 if (builder.isMatrix(left) && builder.isScalar(right)) {
5674 // turn matrix / scalar into a multiply...
Neil Robertseddb1312018-03-13 10:57:59 +01005675 spv::Id resultType = builder.getTypeId(right);
5676 right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right);
John Kessenich04bb8a02015-12-12 12:28:14 -07005677 op = spv::OpMatrixTimesScalar;
5678 } else
5679 firstClass = false;
5680 break;
5681 case spv::OpMatrixTimesScalar:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005682 if (builder.isMatrix(right) || builder.isCooperativeMatrix(right))
John Kessenich04bb8a02015-12-12 12:28:14 -07005683 std::swap(left, right);
5684 assert(builder.isScalar(right));
5685 break;
5686 case spv::OpVectorTimesMatrix:
5687 assert(builder.isVector(left));
5688 assert(builder.isMatrix(right));
5689 break;
5690 case spv::OpMatrixTimesVector:
5691 assert(builder.isMatrix(left));
5692 assert(builder.isVector(right));
5693 break;
5694 case spv::OpMatrixTimesMatrix:
5695 assert(builder.isMatrix(left));
5696 assert(builder.isMatrix(right));
5697 break;
5698 default:
5699 firstClass = false;
5700 break;
5701 }
5702
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005703 if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
5704 firstClass = true;
5705
qining25262b32016-05-06 17:25:16 -04005706 if (firstClass) {
5707 spv::Id result = builder.createBinOp(op, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005708 decorations.addNoContraction(builder, result);
5709 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005710 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005711 }
John Kessenich04bb8a02015-12-12 12:28:14 -07005712
LoopDawg592860c2016-06-09 08:57:35 -06005713 // Handle component-wise +, -, *, %, and / for all combinations of type.
John Kessenich04bb8a02015-12-12 12:28:14 -07005714 // The result type of all of them is the same type as the (a) matrix operand.
5715 // The algorithm is to:
5716 // - break the matrix(es) into vectors
5717 // - smear any scalar to a vector
5718 // - do vector operations
5719 // - make a matrix out the vector results
5720 switch (op) {
5721 case spv::OpFAdd:
5722 case spv::OpFSub:
5723 case spv::OpFDiv:
LoopDawg592860c2016-06-09 08:57:35 -06005724 case spv::OpFMod:
John Kessenich04bb8a02015-12-12 12:28:14 -07005725 case spv::OpFMul:
5726 {
5727 // one time set up...
5728 bool leftMat = builder.isMatrix(left);
5729 bool rightMat = builder.isMatrix(right);
5730 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
5731 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
5732 spv::Id scalarType = builder.getScalarTypeId(typeId);
5733 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
5734 std::vector<spv::Id> results;
5735 spv::Id smearVec = spv::NoResult;
5736 if (builder.isScalar(left))
John Kessenichead86222018-03-28 18:01:20 -06005737 smearVec = builder.smearScalar(decorations.precision, left, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005738 else if (builder.isScalar(right))
John Kessenichead86222018-03-28 18:01:20 -06005739 smearVec = builder.smearScalar(decorations.precision, right, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005740
5741 // do each vector op
5742 for (unsigned int c = 0; c < numCols; ++c) {
5743 std::vector<unsigned int> indexes;
5744 indexes.push_back(c);
5745 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
5746 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
qining25262b32016-05-06 17:25:16 -04005747 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
John Kessenichb9197c82019-08-11 07:41:45 -06005748 decorations.addNoContraction(builder, result);
5749 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005750 results.push_back(builder.setPrecision(result, decorations.precision));
John Kessenich04bb8a02015-12-12 12:28:14 -07005751 }
5752
5753 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005754 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06005755 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005756 return result;
John Kessenich04bb8a02015-12-12 12:28:14 -07005757 }
5758 default:
5759 assert(0);
5760 return spv::NoResult;
5761 }
5762}
5763
John Kessenichead86222018-03-28 18:01:20 -06005764spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
John Kessenich8985fc92020-03-03 10:25:07 -07005765 spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06005766{
5767 spv::Op unaryOp = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08005768 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06005769 int libCall = -1;
John Kessenich66011cb2018-03-06 16:12:04 -07005770 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5771 bool isFloat = isTypeFloat(typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06005772
5773 switch (op) {
5774 case glslang::EOpNegative:
John Kessenich7a53f762016-01-20 11:19:27 -07005775 if (isFloat) {
John Kessenich140f3df2015-06-26 16:58:36 -06005776 unaryOp = spv::OpFNegate;
John Kessenich7a53f762016-01-20 11:19:27 -07005777 if (builder.isMatrixType(typeId))
John Kessenichead86222018-03-28 18:01:20 -06005778 return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy);
John Kessenich7a53f762016-01-20 11:19:27 -07005779 } else
John Kessenich140f3df2015-06-26 16:58:36 -06005780 unaryOp = spv::OpSNegate;
5781 break;
5782
5783 case glslang::EOpLogicalNot:
5784 case glslang::EOpVectorLogicalNot:
John Kessenich5e4b1242015-08-06 22:53:06 -06005785 unaryOp = spv::OpLogicalNot;
5786 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005787 case glslang::EOpBitwiseNot:
5788 unaryOp = spv::OpNot;
5789 break;
John Kessenich5e4b1242015-08-06 22:53:06 -06005790
John Kessenich140f3df2015-06-26 16:58:36 -06005791 case glslang::EOpDeterminant:
John Kessenich5e4b1242015-08-06 22:53:06 -06005792 libCall = spv::GLSLstd450Determinant;
John Kessenich140f3df2015-06-26 16:58:36 -06005793 break;
5794 case glslang::EOpMatrixInverse:
John Kessenich5e4b1242015-08-06 22:53:06 -06005795 libCall = spv::GLSLstd450MatrixInverse;
John Kessenich140f3df2015-06-26 16:58:36 -06005796 break;
5797 case glslang::EOpTranspose:
5798 unaryOp = spv::OpTranspose;
5799 break;
5800
5801 case glslang::EOpRadians:
John Kessenich5e4b1242015-08-06 22:53:06 -06005802 libCall = spv::GLSLstd450Radians;
John Kessenich140f3df2015-06-26 16:58:36 -06005803 break;
5804 case glslang::EOpDegrees:
John Kessenich5e4b1242015-08-06 22:53:06 -06005805 libCall = spv::GLSLstd450Degrees;
John Kessenich140f3df2015-06-26 16:58:36 -06005806 break;
5807 case glslang::EOpSin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005808 libCall = spv::GLSLstd450Sin;
John Kessenich140f3df2015-06-26 16:58:36 -06005809 break;
5810 case glslang::EOpCos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005811 libCall = spv::GLSLstd450Cos;
John Kessenich140f3df2015-06-26 16:58:36 -06005812 break;
5813 case glslang::EOpTan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005814 libCall = spv::GLSLstd450Tan;
John Kessenich140f3df2015-06-26 16:58:36 -06005815 break;
5816 case glslang::EOpAcos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005817 libCall = spv::GLSLstd450Acos;
John Kessenich140f3df2015-06-26 16:58:36 -06005818 break;
5819 case glslang::EOpAsin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005820 libCall = spv::GLSLstd450Asin;
John Kessenich140f3df2015-06-26 16:58:36 -06005821 break;
5822 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005823 libCall = spv::GLSLstd450Atan;
John Kessenich140f3df2015-06-26 16:58:36 -06005824 break;
5825
5826 case glslang::EOpAcosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005827 libCall = spv::GLSLstd450Acosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005828 break;
5829 case glslang::EOpAsinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005830 libCall = spv::GLSLstd450Asinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005831 break;
5832 case glslang::EOpAtanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005833 libCall = spv::GLSLstd450Atanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005834 break;
5835 case glslang::EOpTanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005836 libCall = spv::GLSLstd450Tanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005837 break;
5838 case glslang::EOpCosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005839 libCall = spv::GLSLstd450Cosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005840 break;
5841 case glslang::EOpSinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005842 libCall = spv::GLSLstd450Sinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005843 break;
5844
5845 case glslang::EOpLength:
John Kessenich5e4b1242015-08-06 22:53:06 -06005846 libCall = spv::GLSLstd450Length;
John Kessenich140f3df2015-06-26 16:58:36 -06005847 break;
5848 case glslang::EOpNormalize:
John Kessenich5e4b1242015-08-06 22:53:06 -06005849 libCall = spv::GLSLstd450Normalize;
John Kessenich140f3df2015-06-26 16:58:36 -06005850 break;
5851
5852 case glslang::EOpExp:
John Kessenich5e4b1242015-08-06 22:53:06 -06005853 libCall = spv::GLSLstd450Exp;
John Kessenich140f3df2015-06-26 16:58:36 -06005854 break;
5855 case glslang::EOpLog:
John Kessenich5e4b1242015-08-06 22:53:06 -06005856 libCall = spv::GLSLstd450Log;
John Kessenich140f3df2015-06-26 16:58:36 -06005857 break;
5858 case glslang::EOpExp2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005859 libCall = spv::GLSLstd450Exp2;
John Kessenich140f3df2015-06-26 16:58:36 -06005860 break;
5861 case glslang::EOpLog2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005862 libCall = spv::GLSLstd450Log2;
John Kessenich140f3df2015-06-26 16:58:36 -06005863 break;
5864 case glslang::EOpSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005865 libCall = spv::GLSLstd450Sqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005866 break;
5867 case glslang::EOpInverseSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005868 libCall = spv::GLSLstd450InverseSqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005869 break;
5870
5871 case glslang::EOpFloor:
John Kessenich5e4b1242015-08-06 22:53:06 -06005872 libCall = spv::GLSLstd450Floor;
John Kessenich140f3df2015-06-26 16:58:36 -06005873 break;
5874 case glslang::EOpTrunc:
John Kessenich5e4b1242015-08-06 22:53:06 -06005875 libCall = spv::GLSLstd450Trunc;
John Kessenich140f3df2015-06-26 16:58:36 -06005876 break;
5877 case glslang::EOpRound:
John Kessenich5e4b1242015-08-06 22:53:06 -06005878 libCall = spv::GLSLstd450Round;
John Kessenich140f3df2015-06-26 16:58:36 -06005879 break;
5880 case glslang::EOpRoundEven:
John Kessenich5e4b1242015-08-06 22:53:06 -06005881 libCall = spv::GLSLstd450RoundEven;
John Kessenich140f3df2015-06-26 16:58:36 -06005882 break;
5883 case glslang::EOpCeil:
John Kessenich5e4b1242015-08-06 22:53:06 -06005884 libCall = spv::GLSLstd450Ceil;
John Kessenich140f3df2015-06-26 16:58:36 -06005885 break;
5886 case glslang::EOpFract:
John Kessenich5e4b1242015-08-06 22:53:06 -06005887 libCall = spv::GLSLstd450Fract;
John Kessenich140f3df2015-06-26 16:58:36 -06005888 break;
5889
5890 case glslang::EOpIsNan:
5891 unaryOp = spv::OpIsNan;
5892 break;
5893 case glslang::EOpIsInf:
5894 unaryOp = spv::OpIsInf;
5895 break;
LoopDawg592860c2016-06-09 08:57:35 -06005896 case glslang::EOpIsFinite:
5897 unaryOp = spv::OpIsFinite;
5898 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005899
Rex Xucbc426e2015-12-15 16:03:10 +08005900 case glslang::EOpFloatBitsToInt:
5901 case glslang::EOpFloatBitsToUint:
5902 case glslang::EOpIntBitsToFloat:
5903 case glslang::EOpUintBitsToFloat:
Rex Xu8ff43de2016-04-22 16:51:45 +08005904 case glslang::EOpDoubleBitsToInt64:
5905 case glslang::EOpDoubleBitsToUint64:
5906 case glslang::EOpInt64BitsToDouble:
5907 case glslang::EOpUint64BitsToDouble:
Rex Xucabbb782017-03-24 13:41:14 +08005908 case glslang::EOpFloat16BitsToInt16:
5909 case glslang::EOpFloat16BitsToUint16:
5910 case glslang::EOpInt16BitsToFloat16:
5911 case glslang::EOpUint16BitsToFloat16:
Rex Xucbc426e2015-12-15 16:03:10 +08005912 unaryOp = spv::OpBitcast;
5913 break;
5914
John Kessenich140f3df2015-06-26 16:58:36 -06005915 case glslang::EOpPackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005916 libCall = spv::GLSLstd450PackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005917 break;
5918 case glslang::EOpUnpackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005919 libCall = spv::GLSLstd450UnpackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005920 break;
5921 case glslang::EOpPackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005922 libCall = spv::GLSLstd450PackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005923 break;
5924 case glslang::EOpUnpackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005925 libCall = spv::GLSLstd450UnpackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005926 break;
5927 case glslang::EOpPackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005928 libCall = spv::GLSLstd450PackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005929 break;
5930 case glslang::EOpUnpackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005931 libCall = spv::GLSLstd450UnpackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005932 break;
John Kessenichb9197c82019-08-11 07:41:45 -06005933#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06005934 case glslang::EOpPackSnorm4x8:
5935 libCall = spv::GLSLstd450PackSnorm4x8;
5936 break;
5937 case glslang::EOpUnpackSnorm4x8:
5938 libCall = spv::GLSLstd450UnpackSnorm4x8;
5939 break;
5940 case glslang::EOpPackUnorm4x8:
5941 libCall = spv::GLSLstd450PackUnorm4x8;
5942 break;
5943 case glslang::EOpUnpackUnorm4x8:
5944 libCall = spv::GLSLstd450UnpackUnorm4x8;
5945 break;
5946 case glslang::EOpPackDouble2x32:
5947 libCall = spv::GLSLstd450PackDouble2x32;
5948 break;
5949 case glslang::EOpUnpackDouble2x32:
5950 libCall = spv::GLSLstd450UnpackDouble2x32;
5951 break;
John Kessenichb9197c82019-08-11 07:41:45 -06005952#endif
John Kessenich140f3df2015-06-26 16:58:36 -06005953
Rex Xu8ff43de2016-04-22 16:51:45 +08005954 case glslang::EOpPackInt2x32:
5955 case glslang::EOpUnpackInt2x32:
5956 case glslang::EOpPackUint2x32:
5957 case glslang::EOpUnpackUint2x32:
John Kessenich66011cb2018-03-06 16:12:04 -07005958 case glslang::EOpPack16:
5959 case glslang::EOpPack32:
5960 case glslang::EOpPack64:
5961 case glslang::EOpUnpack32:
5962 case glslang::EOpUnpack16:
5963 case glslang::EOpUnpack8:
Rex Xucabbb782017-03-24 13:41:14 +08005964 case glslang::EOpPackInt2x16:
5965 case glslang::EOpUnpackInt2x16:
5966 case glslang::EOpPackUint2x16:
5967 case glslang::EOpUnpackUint2x16:
5968 case glslang::EOpPackInt4x16:
5969 case glslang::EOpUnpackInt4x16:
5970 case glslang::EOpPackUint4x16:
5971 case glslang::EOpUnpackUint4x16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005972 case glslang::EOpPackFloat2x16:
5973 case glslang::EOpUnpackFloat2x16:
5974 unaryOp = spv::OpBitcast;
5975 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005976
John Kessenich140f3df2015-06-26 16:58:36 -06005977 case glslang::EOpDPdx:
5978 unaryOp = spv::OpDPdx;
5979 break;
5980 case glslang::EOpDPdy:
5981 unaryOp = spv::OpDPdy;
5982 break;
5983 case glslang::EOpFwidth:
5984 unaryOp = spv::OpFwidth;
5985 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06005986
John Kessenich140f3df2015-06-26 16:58:36 -06005987 case glslang::EOpAny:
5988 unaryOp = spv::OpAny;
5989 break;
5990 case glslang::EOpAll:
5991 unaryOp = spv::OpAll;
5992 break;
5993
5994 case glslang::EOpAbs:
John Kessenich5e4b1242015-08-06 22:53:06 -06005995 if (isFloat)
5996 libCall = spv::GLSLstd450FAbs;
5997 else
5998 libCall = spv::GLSLstd450SAbs;
John Kessenich140f3df2015-06-26 16:58:36 -06005999 break;
6000 case glslang::EOpSign:
John Kessenich5e4b1242015-08-06 22:53:06 -06006001 if (isFloat)
6002 libCall = spv::GLSLstd450FSign;
6003 else
6004 libCall = spv::GLSLstd450SSign;
John Kessenich140f3df2015-06-26 16:58:36 -06006005 break;
6006
John Kessenicha28f7a72019-08-06 07:00:58 -06006007#ifndef GLSLANG_WEB
6008 case glslang::EOpDPdxFine:
6009 unaryOp = spv::OpDPdxFine;
6010 break;
6011 case glslang::EOpDPdyFine:
6012 unaryOp = spv::OpDPdyFine;
6013 break;
6014 case glslang::EOpFwidthFine:
6015 unaryOp = spv::OpFwidthFine;
6016 break;
6017 case glslang::EOpDPdxCoarse:
6018 unaryOp = spv::OpDPdxCoarse;
6019 break;
6020 case glslang::EOpDPdyCoarse:
6021 unaryOp = spv::OpDPdyCoarse;
6022 break;
6023 case glslang::EOpFwidthCoarse:
6024 unaryOp = spv::OpFwidthCoarse;
6025 break;
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04006026 case glslang::EOpRayQueryProceed:
6027 unaryOp = spv::OpRayQueryProceedKHR;
6028 break;
6029 case glslang::EOpRayQueryGetRayTMin:
6030 unaryOp = spv::OpRayQueryGetRayTMinKHR;
6031 break;
6032 case glslang::EOpRayQueryGetRayFlags:
6033 unaryOp = spv::OpRayQueryGetRayFlagsKHR;
6034 break;
6035 case glslang::EOpRayQueryGetWorldRayOrigin:
6036 unaryOp = spv::OpRayQueryGetWorldRayOriginKHR;
6037 break;
6038 case glslang::EOpRayQueryGetWorldRayDirection:
6039 unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR;
6040 break;
6041 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
6042 unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR;
6043 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06006044 case glslang::EOpInterpolateAtCentroid:
6045 if (typeProxy == glslang::EbtFloat16)
6046 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6047 libCall = spv::GLSLstd450InterpolateAtCentroid;
6048 break;
John Kessenichfc51d282015-08-19 13:34:18 -06006049 case glslang::EOpAtomicCounterIncrement:
6050 case glslang::EOpAtomicCounterDecrement:
6051 case glslang::EOpAtomicCounter:
6052 {
6053 // Handle all of the atomics in one place, in createAtomicOperation()
6054 std::vector<spv::Id> operands;
6055 operands.push_back(operand);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05006056 return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06006057 }
6058
John Kessenichfc51d282015-08-19 13:34:18 -06006059 case glslang::EOpBitFieldReverse:
6060 unaryOp = spv::OpBitReverse;
6061 break;
6062 case glslang::EOpBitCount:
6063 unaryOp = spv::OpBitCount;
6064 break;
6065 case glslang::EOpFindLSB:
John Kessenich55e7d112015-11-15 21:33:39 -07006066 libCall = spv::GLSLstd450FindILsb;
John Kessenichfc51d282015-08-19 13:34:18 -06006067 break;
6068 case glslang::EOpFindMSB:
John Kessenich55e7d112015-11-15 21:33:39 -07006069 if (isUnsigned)
6070 libCall = spv::GLSLstd450FindUMsb;
6071 else
6072 libCall = spv::GLSLstd450FindSMsb;
John Kessenichfc51d282015-08-19 13:34:18 -06006073 break;
6074
Ian Romanickb3bd4022019-01-21 08:57:25 -08006075 case glslang::EOpCountLeadingZeros:
6076 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
6077 builder.addExtension("SPV_INTEL_shader_integer_functions2");
6078 unaryOp = spv::OpUCountLeadingZerosINTEL;
6079 break;
6080
6081 case glslang::EOpCountTrailingZeros:
6082 builder.addCapability(spv::CapabilityIntegerFunctions2INTEL);
6083 builder.addExtension("SPV_INTEL_shader_integer_functions2");
6084 unaryOp = spv::OpUCountTrailingZerosINTEL;
6085 break;
6086
Rex Xu574ab042016-04-14 16:53:07 +08006087 case glslang::EOpBallot:
6088 case glslang::EOpReadFirstInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08006089 case glslang::EOpAnyInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08006090 case glslang::EOpAllInvocations:
Rex Xu338b1852016-05-05 20:38:33 +08006091 case glslang::EOpAllInvocationsEqual:
Rex Xu9d93a232016-05-05 12:30:44 +08006092 case glslang::EOpMinInvocations:
6093 case glslang::EOpMaxInvocations:
6094 case glslang::EOpAddInvocations:
6095 case glslang::EOpMinInvocationsNonUniform:
6096 case glslang::EOpMaxInvocationsNonUniform:
6097 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08006098 case glslang::EOpMinInvocationsInclusiveScan:
6099 case glslang::EOpMaxInvocationsInclusiveScan:
6100 case glslang::EOpAddInvocationsInclusiveScan:
6101 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6102 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6103 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6104 case glslang::EOpMinInvocationsExclusiveScan:
6105 case glslang::EOpMaxInvocationsExclusiveScan:
6106 case glslang::EOpAddInvocationsExclusiveScan:
6107 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6108 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6109 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
Rex Xu51596642016-09-21 18:56:12 +08006110 {
6111 std::vector<spv::Id> operands;
6112 operands.push_back(operand);
6113 return createInvocationsOperation(op, typeId, operands, typeProxy);
6114 }
John Kessenich66011cb2018-03-06 16:12:04 -07006115 case glslang::EOpSubgroupAll:
6116 case glslang::EOpSubgroupAny:
6117 case glslang::EOpSubgroupAllEqual:
6118 case glslang::EOpSubgroupBroadcastFirst:
6119 case glslang::EOpSubgroupBallot:
6120 case glslang::EOpSubgroupInverseBallot:
6121 case glslang::EOpSubgroupBallotBitCount:
6122 case glslang::EOpSubgroupBallotInclusiveBitCount:
6123 case glslang::EOpSubgroupBallotExclusiveBitCount:
6124 case glslang::EOpSubgroupBallotFindLSB:
6125 case glslang::EOpSubgroupBallotFindMSB:
6126 case glslang::EOpSubgroupAdd:
6127 case glslang::EOpSubgroupMul:
6128 case glslang::EOpSubgroupMin:
6129 case glslang::EOpSubgroupMax:
6130 case glslang::EOpSubgroupAnd:
6131 case glslang::EOpSubgroupOr:
6132 case glslang::EOpSubgroupXor:
6133 case glslang::EOpSubgroupInclusiveAdd:
6134 case glslang::EOpSubgroupInclusiveMul:
6135 case glslang::EOpSubgroupInclusiveMin:
6136 case glslang::EOpSubgroupInclusiveMax:
6137 case glslang::EOpSubgroupInclusiveAnd:
6138 case glslang::EOpSubgroupInclusiveOr:
6139 case glslang::EOpSubgroupInclusiveXor:
6140 case glslang::EOpSubgroupExclusiveAdd:
6141 case glslang::EOpSubgroupExclusiveMul:
6142 case glslang::EOpSubgroupExclusiveMin:
6143 case glslang::EOpSubgroupExclusiveMax:
6144 case glslang::EOpSubgroupExclusiveAnd:
6145 case glslang::EOpSubgroupExclusiveOr:
6146 case glslang::EOpSubgroupExclusiveXor:
6147 case glslang::EOpSubgroupQuadSwapHorizontal:
6148 case glslang::EOpSubgroupQuadSwapVertical:
6149 case glslang::EOpSubgroupQuadSwapDiagonal: {
6150 std::vector<spv::Id> operands;
6151 operands.push_back(operand);
6152 return createSubgroupOperation(op, typeId, operands, typeProxy);
6153 }
Rex Xu9d93a232016-05-05 12:30:44 +08006154 case glslang::EOpMbcnt:
6155 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6156 libCall = spv::MbcntAMD;
6157 break;
6158
6159 case glslang::EOpCubeFaceIndex:
6160 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
6161 libCall = spv::CubeFaceIndexAMD;
6162 break;
6163
6164 case glslang::EOpCubeFaceCoord:
6165 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
6166 libCall = spv::CubeFaceCoordAMD;
6167 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006168 case glslang::EOpSubgroupPartition:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006169 unaryOp = spv::OpGroupNonUniformPartitionNV;
6170 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06006171 case glslang::EOpConstructReference:
6172 unaryOp = spv::OpBitcast;
6173 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06006174#endif
Jeff Bolz88220d52019-05-08 10:24:46 -05006175
6176 case glslang::EOpCopyObject:
6177 unaryOp = spv::OpCopyObject;
6178 break;
6179
John Kessenich140f3df2015-06-26 16:58:36 -06006180 default:
6181 return 0;
6182 }
6183
6184 spv::Id id;
6185 if (libCall >= 0) {
6186 std::vector<spv::Id> args;
6187 args.push_back(operand);
Rex Xu9d93a232016-05-05 12:30:44 +08006188 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
Rex Xu338b1852016-05-05 20:38:33 +08006189 } else {
John Kessenich91cef522016-05-05 16:45:40 -06006190 id = builder.createUnaryOp(unaryOp, typeId, operand);
Rex Xu338b1852016-05-05 20:38:33 +08006191 }
John Kessenich140f3df2015-06-26 16:58:36 -06006192
John Kessenichb9197c82019-08-11 07:41:45 -06006193 decorations.addNoContraction(builder, id);
6194 decorations.addNonUniform(builder, id);
John Kessenichead86222018-03-28 18:01:20 -06006195 return builder.setPrecision(id, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06006196}
6197
John Kessenich7a53f762016-01-20 11:19:27 -07006198// Create a unary operation on a matrix
John Kessenichead86222018-03-28 18:01:20 -06006199spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
6200 spv::Id operand, glslang::TBasicType /* typeProxy */)
John Kessenich7a53f762016-01-20 11:19:27 -07006201{
6202 // Handle unary operations vector by vector.
6203 // The result type is the same type as the original type.
6204 // The algorithm is to:
6205 // - break the matrix into vectors
6206 // - apply the operation to each vector
6207 // - make a matrix out the vector results
6208
6209 // get the types sorted out
6210 int numCols = builder.getNumColumns(operand);
6211 int numRows = builder.getNumRows(operand);
Rex Xuc1992e52016-05-17 18:57:18 +08006212 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
6213 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
John Kessenich7a53f762016-01-20 11:19:27 -07006214 std::vector<spv::Id> results;
6215
6216 // do each vector op
6217 for (int c = 0; c < numCols; ++c) {
6218 std::vector<unsigned int> indexes;
6219 indexes.push_back(c);
Rex Xuc1992e52016-05-17 18:57:18 +08006220 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
6221 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
John Kessenichb9197c82019-08-11 07:41:45 -06006222 decorations.addNoContraction(builder, destVec);
6223 decorations.addNonUniform(builder, destVec);
John Kessenichead86222018-03-28 18:01:20 -06006224 results.push_back(builder.setPrecision(destVec, decorations.precision));
John Kessenich7a53f762016-01-20 11:19:27 -07006225 }
6226
6227 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06006228 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06006229 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06006230 return result;
John Kessenich7a53f762016-01-20 11:19:27 -07006231}
6232
John Kessenichad7645f2018-06-04 19:11:25 -06006233// For converting integers where both the bitwidth and the signedness could
6234// change, but only do the width change here. The caller is still responsible
6235// for the signedness conversion.
6236spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize)
John Kessenich66011cb2018-03-06 16:12:04 -07006237{
John Kessenichad7645f2018-06-04 19:11:25 -06006238 // Get the result type width, based on the type to convert to.
6239 int width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07006240 switch(op) {
John Kessenichad7645f2018-06-04 19:11:25 -06006241 case glslang::EOpConvInt16ToUint8:
6242 case glslang::EOpConvIntToUint8:
6243 case glslang::EOpConvInt64ToUint8:
6244 case glslang::EOpConvUint16ToInt8:
6245 case glslang::EOpConvUintToInt8:
6246 case glslang::EOpConvUint64ToInt8:
6247 width = 8;
6248 break;
John Kessenich66011cb2018-03-06 16:12:04 -07006249 case glslang::EOpConvInt8ToUint16:
John Kessenichad7645f2018-06-04 19:11:25 -06006250 case glslang::EOpConvIntToUint16:
6251 case glslang::EOpConvInt64ToUint16:
6252 case glslang::EOpConvUint8ToInt16:
6253 case glslang::EOpConvUintToInt16:
6254 case glslang::EOpConvUint64ToInt16:
6255 width = 16;
John Kessenich66011cb2018-03-06 16:12:04 -07006256 break;
6257 case glslang::EOpConvInt8ToUint:
John Kessenichad7645f2018-06-04 19:11:25 -06006258 case glslang::EOpConvInt16ToUint:
6259 case glslang::EOpConvInt64ToUint:
6260 case glslang::EOpConvUint8ToInt:
6261 case glslang::EOpConvUint16ToInt:
6262 case glslang::EOpConvUint64ToInt:
6263 width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07006264 break;
6265 case glslang::EOpConvInt8ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006266 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006267 case glslang::EOpConvIntToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006268 case glslang::EOpConvUint8ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006269 case glslang::EOpConvUint16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006270 case glslang::EOpConvUintToInt64:
John Kessenichad7645f2018-06-04 19:11:25 -06006271 width = 64;
John Kessenich66011cb2018-03-06 16:12:04 -07006272 break;
6273
6274 default:
6275 assert(false && "Default missing");
6276 break;
6277 }
6278
John Kessenichad7645f2018-06-04 19:11:25 -06006279 // Get the conversion operation and result type,
6280 // based on the target width, but the source type.
6281 spv::Id type = spv::NoType;
6282 spv::Op convOp = spv::OpNop;
6283 switch(op) {
6284 case glslang::EOpConvInt8ToUint16:
6285 case glslang::EOpConvInt8ToUint:
6286 case glslang::EOpConvInt8ToUint64:
6287 case glslang::EOpConvInt16ToUint8:
6288 case glslang::EOpConvInt16ToUint:
6289 case glslang::EOpConvInt16ToUint64:
6290 case glslang::EOpConvIntToUint8:
6291 case glslang::EOpConvIntToUint16:
6292 case glslang::EOpConvIntToUint64:
6293 case glslang::EOpConvInt64ToUint8:
6294 case glslang::EOpConvInt64ToUint16:
6295 case glslang::EOpConvInt64ToUint:
6296 convOp = spv::OpSConvert;
6297 type = builder.makeIntType(width);
6298 break;
6299 default:
6300 convOp = spv::OpUConvert;
6301 type = builder.makeUintType(width);
6302 break;
6303 }
6304
John Kessenich66011cb2018-03-06 16:12:04 -07006305 if (vectorSize > 0)
6306 type = builder.makeVectorType(type, vectorSize);
6307
John Kessenichad7645f2018-06-04 19:11:25 -06006308 return builder.createUnaryOp(convOp, type, operand);
John Kessenich66011cb2018-03-06 16:12:04 -07006309}
6310
John Kessenichead86222018-03-28 18:01:20 -06006311spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
6312 spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06006313{
6314 spv::Op convOp = spv::OpNop;
6315 spv::Id zero = 0;
6316 spv::Id one = 0;
6317
6318 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
6319
6320 switch (op) {
John Kessenich66011cb2018-03-06 16:12:04 -07006321 case glslang::EOpConvIntToBool:
6322 case glslang::EOpConvUintToBool:
6323 zero = builder.makeUintConstant(0);
6324 zero = makeSmearedConstant(zero, vectorSize);
6325 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06006326 case glslang::EOpConvFloatToBool:
6327 zero = builder.makeFloatConstant(0.0F);
6328 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006329 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06006330 case glslang::EOpConvBoolToFloat:
6331 convOp = spv::OpSelect;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006332 zero = builder.makeFloatConstant(0.0F);
6333 one = builder.makeFloatConstant(1.0F);
John Kessenich140f3df2015-06-26 16:58:36 -06006334 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006335
John Kessenich140f3df2015-06-26 16:58:36 -06006336 case glslang::EOpConvBoolToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006337 case glslang::EOpConvBoolToInt64:
John Kessenichb9197c82019-08-11 07:41:45 -06006338#ifndef GLSLANG_WEB
6339 if (op == glslang::EOpConvBoolToInt64) {
Rex Xucabbb782017-03-24 13:41:14 +08006340 zero = builder.makeInt64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006341 one = builder.makeInt64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006342 } else
6343#endif
6344 {
6345 zero = builder.makeIntConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006346 one = builder.makeIntConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006347 }
Rex Xucabbb782017-03-24 13:41:14 +08006348
John Kessenich140f3df2015-06-26 16:58:36 -06006349 convOp = spv::OpSelect;
6350 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006351
John Kessenich140f3df2015-06-26 16:58:36 -06006352 case glslang::EOpConvBoolToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006353 case glslang::EOpConvBoolToUint64:
John Kessenichb9197c82019-08-11 07:41:45 -06006354#ifndef GLSLANG_WEB
6355 if (op == glslang::EOpConvBoolToUint64) {
Rex Xucabbb782017-03-24 13:41:14 +08006356 zero = builder.makeUint64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006357 one = builder.makeUint64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006358 } else
6359#endif
6360 {
6361 zero = builder.makeUintConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006362 one = builder.makeUintConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006363 }
Rex Xucabbb782017-03-24 13:41:14 +08006364
John Kessenich140f3df2015-06-26 16:58:36 -06006365 convOp = spv::OpSelect;
6366 break;
6367
John Kessenich66011cb2018-03-06 16:12:04 -07006368 case glslang::EOpConvInt8ToFloat16:
6369 case glslang::EOpConvInt8ToFloat:
6370 case glslang::EOpConvInt8ToDouble:
6371 case glslang::EOpConvInt16ToFloat16:
6372 case glslang::EOpConvInt16ToFloat:
6373 case glslang::EOpConvInt16ToDouble:
6374 case glslang::EOpConvIntToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006375 case glslang::EOpConvIntToFloat:
6376 case glslang::EOpConvIntToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006377 case glslang::EOpConvInt64ToFloat:
6378 case glslang::EOpConvInt64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006379 case glslang::EOpConvInt64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006380 convOp = spv::OpConvertSToF;
6381 break;
6382
John Kessenich66011cb2018-03-06 16:12:04 -07006383 case glslang::EOpConvUint8ToFloat16:
6384 case glslang::EOpConvUint8ToFloat:
6385 case glslang::EOpConvUint8ToDouble:
6386 case glslang::EOpConvUint16ToFloat16:
6387 case glslang::EOpConvUint16ToFloat:
6388 case glslang::EOpConvUint16ToDouble:
6389 case glslang::EOpConvUintToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006390 case glslang::EOpConvUintToFloat:
6391 case glslang::EOpConvUintToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006392 case glslang::EOpConvUint64ToFloat:
6393 case glslang::EOpConvUint64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006394 case glslang::EOpConvUint64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006395 convOp = spv::OpConvertUToF;
6396 break;
6397
John Kessenich66011cb2018-03-06 16:12:04 -07006398 case glslang::EOpConvFloat16ToInt8:
6399 case glslang::EOpConvFloatToInt8:
6400 case glslang::EOpConvDoubleToInt8:
6401 case glslang::EOpConvFloat16ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006402 case glslang::EOpConvFloatToInt16:
6403 case glslang::EOpConvDoubleToInt16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006404 case glslang::EOpConvFloat16ToInt:
John Kessenich66011cb2018-03-06 16:12:04 -07006405 case glslang::EOpConvFloatToInt:
6406 case glslang::EOpConvDoubleToInt:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006407 case glslang::EOpConvFloat16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006408 case glslang::EOpConvFloatToInt64:
6409 case glslang::EOpConvDoubleToInt64:
John Kessenich140f3df2015-06-26 16:58:36 -06006410 convOp = spv::OpConvertFToS;
6411 break;
6412
John Kessenich66011cb2018-03-06 16:12:04 -07006413 case glslang::EOpConvUint8ToInt8:
6414 case glslang::EOpConvInt8ToUint8:
6415 case glslang::EOpConvUint16ToInt16:
6416 case glslang::EOpConvInt16ToUint16:
John Kessenich140f3df2015-06-26 16:58:36 -06006417 case glslang::EOpConvUintToInt:
6418 case glslang::EOpConvIntToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006419 case glslang::EOpConvUint64ToInt64:
6420 case glslang::EOpConvInt64ToUint64:
qininge24aa5e2016-04-07 15:40:27 -04006421 if (builder.isInSpecConstCodeGenMode()) {
6422 // Build zero scalar or vector for OpIAdd.
John Kessenich39697cd2019-08-08 10:35:51 -06006423#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07006424 if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) {
6425 zero = builder.makeUint8Constant(0);
6426 } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) {
Rex Xucabbb782017-03-24 13:41:14 +08006427 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006428 } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) {
6429 zero = builder.makeUint64Constant(0);
John Kessenich39697cd2019-08-08 10:35:51 -06006430 } else
6431#endif
6432 {
Rex Xucabbb782017-03-24 13:41:14 +08006433 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006434 }
qining189b2032016-04-12 23:16:20 -04006435 zero = makeSmearedConstant(zero, vectorSize);
qininge24aa5e2016-04-07 15:40:27 -04006436 // Use OpIAdd, instead of OpBitcast to do the conversion when
6437 // generating for OpSpecConstantOp instruction.
6438 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6439 }
6440 // For normal run-time conversion instruction, use OpBitcast.
John Kessenich140f3df2015-06-26 16:58:36 -06006441 convOp = spv::OpBitcast;
6442 break;
6443
John Kessenich66011cb2018-03-06 16:12:04 -07006444 case glslang::EOpConvFloat16ToUint8:
6445 case glslang::EOpConvFloatToUint8:
6446 case glslang::EOpConvDoubleToUint8:
6447 case glslang::EOpConvFloat16ToUint16:
6448 case glslang::EOpConvFloatToUint16:
6449 case glslang::EOpConvDoubleToUint16:
6450 case glslang::EOpConvFloat16ToUint:
John Kessenich140f3df2015-06-26 16:58:36 -06006451 case glslang::EOpConvFloatToUint:
6452 case glslang::EOpConvDoubleToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006453 case glslang::EOpConvFloatToUint64:
6454 case glslang::EOpConvDoubleToUint64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006455 case glslang::EOpConvFloat16ToUint64:
John Kessenich140f3df2015-06-26 16:58:36 -06006456 convOp = spv::OpConvertFToU;
6457 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08006458
John Kessenich39697cd2019-08-08 10:35:51 -06006459#ifndef GLSLANG_WEB
6460 case glslang::EOpConvInt8ToBool:
6461 case glslang::EOpConvUint8ToBool:
6462 zero = builder.makeUint8Constant(0);
6463 zero = makeSmearedConstant(zero, vectorSize);
6464 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6465 case glslang::EOpConvInt16ToBool:
6466 case glslang::EOpConvUint16ToBool:
6467 zero = builder.makeUint16Constant(0);
6468 zero = makeSmearedConstant(zero, vectorSize);
6469 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6470 case glslang::EOpConvInt64ToBool:
6471 case glslang::EOpConvUint64ToBool:
6472 zero = builder.makeUint64Constant(0);
6473 zero = makeSmearedConstant(zero, vectorSize);
6474 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6475 case glslang::EOpConvDoubleToBool:
6476 zero = builder.makeDoubleConstant(0.0);
6477 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006478 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich39697cd2019-08-08 10:35:51 -06006479 case glslang::EOpConvFloat16ToBool:
6480 zero = builder.makeFloat16Constant(0.0F);
6481 zero = makeSmearedConstant(zero, vectorSize);
Graeme Leese65ce5662020-06-05 13:32:51 +01006482 return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero);
John Kessenich39697cd2019-08-08 10:35:51 -06006483 case glslang::EOpConvBoolToDouble:
6484 convOp = spv::OpSelect;
6485 zero = builder.makeDoubleConstant(0.0);
6486 one = builder.makeDoubleConstant(1.0);
6487 break;
6488 case glslang::EOpConvBoolToFloat16:
6489 convOp = spv::OpSelect;
6490 zero = builder.makeFloat16Constant(0.0F);
6491 one = builder.makeFloat16Constant(1.0F);
6492 break;
6493 case glslang::EOpConvBoolToInt8:
6494 zero = builder.makeInt8Constant(0);
6495 one = builder.makeInt8Constant(1);
6496 convOp = spv::OpSelect;
6497 break;
6498 case glslang::EOpConvBoolToUint8:
6499 zero = builder.makeUint8Constant(0);
6500 one = builder.makeUint8Constant(1);
6501 convOp = spv::OpSelect;
6502 break;
6503 case glslang::EOpConvBoolToInt16:
6504 zero = builder.makeInt16Constant(0);
6505 one = builder.makeInt16Constant(1);
6506 convOp = spv::OpSelect;
6507 break;
6508 case glslang::EOpConvBoolToUint16:
6509 zero = builder.makeUint16Constant(0);
6510 one = builder.makeUint16Constant(1);
6511 convOp = spv::OpSelect;
6512 break;
6513 case glslang::EOpConvDoubleToFloat:
6514 case glslang::EOpConvFloatToDouble:
6515 case glslang::EOpConvDoubleToFloat16:
6516 case glslang::EOpConvFloat16ToDouble:
6517 case glslang::EOpConvFloatToFloat16:
6518 case glslang::EOpConvFloat16ToFloat:
6519 convOp = spv::OpFConvert;
6520 if (builder.isMatrixType(destType))
6521 return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy);
6522 break;
6523
John Kessenich66011cb2018-03-06 16:12:04 -07006524 case glslang::EOpConvInt8ToInt16:
6525 case glslang::EOpConvInt8ToInt:
6526 case glslang::EOpConvInt8ToInt64:
6527 case glslang::EOpConvInt16ToInt8:
Rex Xucabbb782017-03-24 13:41:14 +08006528 case glslang::EOpConvInt16ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006529 case glslang::EOpConvInt16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006530 case glslang::EOpConvIntToInt8:
6531 case glslang::EOpConvIntToInt16:
6532 case glslang::EOpConvIntToInt64:
6533 case glslang::EOpConvInt64ToInt8:
6534 case glslang::EOpConvInt64ToInt16:
6535 case glslang::EOpConvInt64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006536 convOp = spv::OpSConvert;
6537 break;
6538
John Kessenich66011cb2018-03-06 16:12:04 -07006539 case glslang::EOpConvUint8ToUint16:
6540 case glslang::EOpConvUint8ToUint:
6541 case glslang::EOpConvUint8ToUint64:
6542 case glslang::EOpConvUint16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006543 case glslang::EOpConvUint16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006544 case glslang::EOpConvUint16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006545 case glslang::EOpConvUintToUint8:
6546 case glslang::EOpConvUintToUint16:
6547 case glslang::EOpConvUintToUint64:
6548 case glslang::EOpConvUint64ToUint8:
6549 case glslang::EOpConvUint64ToUint16:
6550 case glslang::EOpConvUint64ToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006551 convOp = spv::OpUConvert;
6552 break;
6553
John Kessenich66011cb2018-03-06 16:12:04 -07006554 case glslang::EOpConvInt8ToUint16:
6555 case glslang::EOpConvInt8ToUint:
6556 case glslang::EOpConvInt8ToUint64:
6557 case glslang::EOpConvInt16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006558 case glslang::EOpConvInt16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006559 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006560 case glslang::EOpConvIntToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006561 case glslang::EOpConvIntToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006562 case glslang::EOpConvIntToUint64:
6563 case glslang::EOpConvInt64ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006564 case glslang::EOpConvInt64ToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006565 case glslang::EOpConvInt64ToUint:
6566 case glslang::EOpConvUint8ToInt16:
6567 case glslang::EOpConvUint8ToInt:
6568 case glslang::EOpConvUint8ToInt64:
6569 case glslang::EOpConvUint16ToInt8:
6570 case glslang::EOpConvUint16ToInt:
6571 case glslang::EOpConvUint16ToInt64:
6572 case glslang::EOpConvUintToInt8:
6573 case glslang::EOpConvUintToInt16:
6574 case glslang::EOpConvUintToInt64:
6575 case glslang::EOpConvUint64ToInt8:
6576 case glslang::EOpConvUint64ToInt16:
6577 case glslang::EOpConvUint64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006578 // OpSConvert/OpUConvert + OpBitCast
John Kessenichad7645f2018-06-04 19:11:25 -06006579 operand = createIntWidthConversion(op, operand, vectorSize);
Rex Xu8ff43de2016-04-22 16:51:45 +08006580
6581 if (builder.isInSpecConstCodeGenMode()) {
6582 // Build zero scalar or vector for OpIAdd.
John Kessenich66011cb2018-03-06 16:12:04 -07006583 switch(op) {
6584 case glslang::EOpConvInt16ToUint8:
6585 case glslang::EOpConvIntToUint8:
6586 case glslang::EOpConvInt64ToUint8:
6587 case glslang::EOpConvUint16ToInt8:
6588 case glslang::EOpConvUintToInt8:
6589 case glslang::EOpConvUint64ToInt8:
6590 zero = builder.makeUint8Constant(0);
6591 break;
6592 case glslang::EOpConvInt8ToUint16:
6593 case glslang::EOpConvIntToUint16:
6594 case glslang::EOpConvInt64ToUint16:
6595 case glslang::EOpConvUint8ToInt16:
6596 case glslang::EOpConvUintToInt16:
6597 case glslang::EOpConvUint64ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006598 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006599 break;
6600 case glslang::EOpConvInt8ToUint:
6601 case glslang::EOpConvInt16ToUint:
6602 case glslang::EOpConvInt64ToUint:
6603 case glslang::EOpConvUint8ToInt:
6604 case glslang::EOpConvUint16ToInt:
6605 case glslang::EOpConvUint64ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006606 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006607 break;
6608 case glslang::EOpConvInt8ToUint64:
6609 case glslang::EOpConvInt16ToUint64:
6610 case glslang::EOpConvIntToUint64:
6611 case glslang::EOpConvUint8ToInt64:
6612 case glslang::EOpConvUint16ToInt64:
6613 case glslang::EOpConvUintToInt64:
Rex Xucabbb782017-03-24 13:41:14 +08006614 zero = builder.makeUint64Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006615 break;
6616 default:
6617 assert(false && "Default missing");
6618 break;
6619 }
Rex Xu8ff43de2016-04-22 16:51:45 +08006620 zero = makeSmearedConstant(zero, vectorSize);
6621 // Use OpIAdd, instead of OpBitcast to do the conversion when
6622 // generating for OpSpecConstantOp instruction.
6623 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6624 }
6625 // For normal run-time conversion instruction, use OpBitcast.
6626 convOp = spv::OpBitcast;
6627 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06006628 case glslang::EOpConvUint64ToPtr:
6629 convOp = spv::OpConvertUToPtr;
6630 break;
6631 case glslang::EOpConvPtrToUint64:
6632 convOp = spv::OpConvertPtrToU;
6633 break;
John Kessenich90e402f2019-09-17 23:19:38 -06006634 case glslang::EOpConvPtrToUvec2:
6635 case glslang::EOpConvUvec2ToPtr:
John Kessenichee8e9c12019-10-10 20:54:21 -06006636 if (builder.isVector(operand))
6637 builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer,
6638 spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5);
John Kessenich90e402f2019-09-17 23:19:38 -06006639 convOp = spv::OpBitcast;
6640 break;
John Kessenich39697cd2019-08-08 10:35:51 -06006641#endif
6642
John Kessenich140f3df2015-06-26 16:58:36 -06006643 default:
6644 break;
6645 }
6646
6647 spv::Id result = 0;
6648 if (convOp == spv::OpNop)
6649 return result;
6650
6651 if (convOp == spv::OpSelect) {
6652 zero = makeSmearedConstant(zero, vectorSize);
6653 one = makeSmearedConstant(one, vectorSize);
6654 result = builder.createTriOp(convOp, destType, operand, one, zero);
6655 } else
6656 result = builder.createUnaryOp(convOp, destType, operand);
6657
John Kessenichead86222018-03-28 18:01:20 -06006658 result = builder.setPrecision(result, decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06006659 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06006660 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06006661}
6662
6663spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
6664{
6665 if (vectorSize == 0)
6666 return constant;
6667
6668 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
6669 std::vector<spv::Id> components;
6670 for (int c = 0; c < vectorSize; ++c)
6671 components.push_back(constant);
6672 return builder.makeCompositeConstant(vectorTypeId, components);
6673}
6674
John Kessenich426394d2015-07-23 10:22:48 -06006675// For glslang ops that map to SPV atomic opCodes
John Kessenich8985fc92020-03-03 10:25:07 -07006676spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/,
6677 spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy,
6678 const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich426394d2015-07-23 10:22:48 -06006679{
6680 spv::Op opCode = spv::OpNop;
6681
6682 switch (op) {
6683 case glslang::EOpAtomicAdd:
Rex Xufc618912015-09-09 16:42:49 +08006684 case glslang::EOpImageAtomicAdd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006685 case glslang::EOpAtomicCounterAdd:
John Kessenich426394d2015-07-23 10:22:48 -06006686 opCode = spv::OpAtomicIAdd;
Vikram Kushwaha79b93922020-07-19 15:45:01 -07006687 if (typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) {
6688 opCode = spv::OpAtomicFAddEXT;
6689 builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add);
6690 if (typeProxy == glslang::EbtFloat)
6691 builder.addCapability(spv::CapabilityAtomicFloat32AddEXT);
6692 else
6693 builder.addCapability(spv::CapabilityAtomicFloat64AddEXT);
6694 }
John Kessenich426394d2015-07-23 10:22:48 -06006695 break;
John Kessenich0d0c6d32017-07-23 16:08:26 -06006696 case glslang::EOpAtomicCounterSubtract:
6697 opCode = spv::OpAtomicISub;
6698 break;
John Kessenich426394d2015-07-23 10:22:48 -06006699 case glslang::EOpAtomicMin:
Rex Xufc618912015-09-09 16:42:49 +08006700 case glslang::EOpImageAtomicMin:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006701 case glslang::EOpAtomicCounterMin:
John Kessenich8985fc92020-03-03 10:25:07 -07006702 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ?
6703 spv::OpAtomicUMin : spv::OpAtomicSMin;
John Kessenich426394d2015-07-23 10:22:48 -06006704 break;
6705 case glslang::EOpAtomicMax:
Rex Xufc618912015-09-09 16:42:49 +08006706 case glslang::EOpImageAtomicMax:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006707 case glslang::EOpAtomicCounterMax:
John Kessenich8985fc92020-03-03 10:25:07 -07006708 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ?
6709 spv::OpAtomicUMax : spv::OpAtomicSMax;
John Kessenich426394d2015-07-23 10:22:48 -06006710 break;
6711 case glslang::EOpAtomicAnd:
Rex Xufc618912015-09-09 16:42:49 +08006712 case glslang::EOpImageAtomicAnd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006713 case glslang::EOpAtomicCounterAnd:
John Kessenich426394d2015-07-23 10:22:48 -06006714 opCode = spv::OpAtomicAnd;
6715 break;
6716 case glslang::EOpAtomicOr:
Rex Xufc618912015-09-09 16:42:49 +08006717 case glslang::EOpImageAtomicOr:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006718 case glslang::EOpAtomicCounterOr:
John Kessenich426394d2015-07-23 10:22:48 -06006719 opCode = spv::OpAtomicOr;
6720 break;
6721 case glslang::EOpAtomicXor:
Rex Xufc618912015-09-09 16:42:49 +08006722 case glslang::EOpImageAtomicXor:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006723 case glslang::EOpAtomicCounterXor:
John Kessenich426394d2015-07-23 10:22:48 -06006724 opCode = spv::OpAtomicXor;
6725 break;
6726 case glslang::EOpAtomicExchange:
Rex Xufc618912015-09-09 16:42:49 +08006727 case glslang::EOpImageAtomicExchange:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006728 case glslang::EOpAtomicCounterExchange:
John Kessenich426394d2015-07-23 10:22:48 -06006729 opCode = spv::OpAtomicExchange;
6730 break;
6731 case glslang::EOpAtomicCompSwap:
Rex Xufc618912015-09-09 16:42:49 +08006732 case glslang::EOpImageAtomicCompSwap:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006733 case glslang::EOpAtomicCounterCompSwap:
John Kessenich426394d2015-07-23 10:22:48 -06006734 opCode = spv::OpAtomicCompareExchange;
6735 break;
6736 case glslang::EOpAtomicCounterIncrement:
6737 opCode = spv::OpAtomicIIncrement;
6738 break;
6739 case glslang::EOpAtomicCounterDecrement:
6740 opCode = spv::OpAtomicIDecrement;
6741 break;
6742 case glslang::EOpAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05006743 case glslang::EOpImageAtomicLoad:
6744 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06006745 opCode = spv::OpAtomicLoad;
6746 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05006747 case glslang::EOpAtomicStore:
6748 case glslang::EOpImageAtomicStore:
6749 opCode = spv::OpAtomicStore;
6750 break;
John Kessenich426394d2015-07-23 10:22:48 -06006751 default:
John Kessenich55e7d112015-11-15 21:33:39 -07006752 assert(0);
John Kessenich426394d2015-07-23 10:22:48 -06006753 break;
6754 }
6755
Rex Xue8fe8b02017-09-26 15:42:56 +08006756 if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64)
6757 builder.addCapability(spv::CapabilityInt64Atomics);
6758
John Kessenich426394d2015-07-23 10:22:48 -06006759 // Sort out the operands
6760 // - mapping from glslang -> SPV
Jeff Bolz36831c92018-09-05 10:11:41 -05006761 // - there are extra SPV operands that are optional in glslang
John Kessenich3e60a6f2015-09-14 22:45:16 -06006762 // - compare-exchange swaps the value and comparator
6763 // - compare-exchange has an extra memory semantics
John Kessenich48d6e792017-10-06 21:21:48 -06006764 // - EOpAtomicCounterDecrement needs a post decrement
Jeff Bolz36831c92018-09-05 10:11:41 -05006765 spv::Id pointerId = 0, compareId = 0, valueId = 0;
6766 // scope defaults to Device in the old model, QueueFamilyKHR in the new model
6767 spv::Id scopeId;
6768 if (glslangIntermediate->usingVulkanMemoryModel()) {
6769 scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR);
6770 } else {
6771 scopeId = builder.makeUintConstant(spv::ScopeDevice);
6772 }
6773 // semantics default to relaxed
John Kessenich8985fc92020-03-03 10:25:07 -07006774 spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() &&
6775 glslangIntermediate->usingVulkanMemoryModel() ?
John Kessenichb9197c82019-08-11 07:41:45 -06006776 spv::MemorySemanticsVolatileMask :
6777 spv::MemorySemanticsMaskNone);
Jeff Bolz36831c92018-09-05 10:11:41 -05006778 spv::Id semanticsId2 = semanticsId;
6779
6780 pointerId = operands[0];
6781 if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) {
6782 // no additional operands
6783 } else if (opCode == spv::OpAtomicCompareExchange) {
6784 compareId = operands[1];
6785 valueId = operands[2];
6786 if (operands.size() > 3) {
6787 scopeId = operands[3];
John Kessenich8985fc92020-03-03 10:25:07 -07006788 semanticsId = builder.makeUintConstant(
6789 builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5]));
6790 semanticsId2 = builder.makeUintConstant(
6791 builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7]));
Jeff Bolz36831c92018-09-05 10:11:41 -05006792 }
6793 } else if (opCode == spv::OpAtomicLoad) {
6794 if (operands.size() > 1) {
6795 scopeId = operands[1];
John Kessenich8985fc92020-03-03 10:25:07 -07006796 semanticsId = builder.makeUintConstant(
6797 builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]));
Jeff Bolz36831c92018-09-05 10:11:41 -05006798 }
6799 } else {
6800 // atomic store or RMW
6801 valueId = operands[1];
6802 if (operands.size() > 2) {
6803 scopeId = operands[2];
John Kessenich8985fc92020-03-03 10:25:07 -07006804 semanticsId = builder.makeUintConstant
6805 (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4]));
Jeff Bolz36831c92018-09-05 10:11:41 -05006806 }
Rex Xu04db3f52015-09-16 11:44:02 +08006807 }
John Kessenich426394d2015-07-23 10:22:48 -06006808
Jeff Bolz36831c92018-09-05 10:11:41 -05006809 // Check for capabilities
6810 unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05006811 if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask |
6812 spv::MemorySemanticsMakeVisibleKHRMask |
6813 spv::MemorySemanticsOutputMemoryKHRMask |
6814 spv::MemorySemanticsVolatileMask)) {
Jeff Bolz36831c92018-09-05 10:11:41 -05006815 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6816 }
John Kessenich426394d2015-07-23 10:22:48 -06006817
Jeff Bolz36831c92018-09-05 10:11:41 -05006818 if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) {
6819 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6820 }
John Kessenich48d6e792017-10-06 21:21:48 -06006821
Jeff Bolz36831c92018-09-05 10:11:41 -05006822 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
6823 spvAtomicOperands.push_back(pointerId);
6824 spvAtomicOperands.push_back(scopeId);
6825 spvAtomicOperands.push_back(semanticsId);
6826 if (opCode == spv::OpAtomicCompareExchange) {
6827 spvAtomicOperands.push_back(semanticsId2);
6828 spvAtomicOperands.push_back(valueId);
6829 spvAtomicOperands.push_back(compareId);
6830 } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) {
6831 spvAtomicOperands.push_back(valueId);
6832 }
John Kessenich48d6e792017-10-06 21:21:48 -06006833
Jeff Bolz36831c92018-09-05 10:11:41 -05006834 if (opCode == spv::OpAtomicStore) {
6835 builder.createNoResultOp(opCode, spvAtomicOperands);
6836 return 0;
6837 } else {
6838 spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands);
6839
6840 // GLSL and HLSL atomic-counter decrement return post-decrement value,
6841 // while SPIR-V returns pre-decrement value. Translate between these semantics.
6842 if (op == glslang::EOpAtomicCounterDecrement)
6843 resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1));
6844
6845 return resultId;
6846 }
John Kessenich426394d2015-07-23 10:22:48 -06006847}
6848
John Kessenich91cef522016-05-05 16:45:40 -06006849// Create group invocation operations.
John Kessenich8985fc92020-03-03 10:25:07 -07006850spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId,
6851 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich91cef522016-05-05 16:45:40 -06006852{
John Kessenich66011cb2018-03-06 16:12:04 -07006853 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6854 bool isFloat = isTypeFloat(typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +08006855
Rex Xu51596642016-09-21 18:56:12 +08006856 spv::Op opCode = spv::OpNop;
John Kessenich149afc32018-08-14 13:31:43 -06006857 std::vector<spv::IdImmediate> spvGroupOperands;
Rex Xu430ef402016-10-14 17:22:23 +08006858 spv::GroupOperation groupOperation = spv::GroupOperationMax;
6859
chaocf200da82016-12-20 12:44:35 -08006860 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
6861 op == glslang::EOpReadInvocation) {
Rex Xu51596642016-09-21 18:56:12 +08006862 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
6863 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006864 } else if (op == glslang::EOpAnyInvocation ||
6865 op == glslang::EOpAllInvocations ||
6866 op == glslang::EOpAllInvocationsEqual) {
6867 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
6868 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
Rex Xu51596642016-09-21 18:56:12 +08006869 } else {
6870 builder.addCapability(spv::CapabilityGroups);
Rex Xu17ff3432016-10-14 17:41:45 +08006871 if (op == glslang::EOpMinInvocationsNonUniform ||
6872 op == glslang::EOpMaxInvocationsNonUniform ||
Rex Xu430ef402016-10-14 17:22:23 +08006873 op == glslang::EOpAddInvocationsNonUniform ||
6874 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6875 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6876 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
6877 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
6878 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
6879 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
Rex Xu17ff3432016-10-14 17:41:45 +08006880 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +08006881
Rex Xu430ef402016-10-14 17:22:23 +08006882 switch (op) {
6883 case glslang::EOpMinInvocations:
6884 case glslang::EOpMaxInvocations:
6885 case glslang::EOpAddInvocations:
6886 case glslang::EOpMinInvocationsNonUniform:
6887 case glslang::EOpMaxInvocationsNonUniform:
6888 case glslang::EOpAddInvocationsNonUniform:
6889 groupOperation = spv::GroupOperationReduce;
Rex Xu430ef402016-10-14 17:22:23 +08006890 break;
6891 case glslang::EOpMinInvocationsInclusiveScan:
6892 case glslang::EOpMaxInvocationsInclusiveScan:
6893 case glslang::EOpAddInvocationsInclusiveScan:
6894 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6895 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6896 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6897 groupOperation = spv::GroupOperationInclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006898 break;
6899 case glslang::EOpMinInvocationsExclusiveScan:
6900 case glslang::EOpMaxInvocationsExclusiveScan:
6901 case glslang::EOpAddInvocationsExclusiveScan:
6902 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6903 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6904 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6905 groupOperation = spv::GroupOperationExclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006906 break;
Mike Weiblen4e9e4002017-01-20 13:34:10 -07006907 default:
6908 break;
Rex Xu430ef402016-10-14 17:22:23 +08006909 }
John Kessenich149afc32018-08-14 13:31:43 -06006910 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6911 spvGroupOperands.push_back(scope);
6912 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06006913 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006914 spvGroupOperands.push_back(groupOp);
6915 }
Rex Xu51596642016-09-21 18:56:12 +08006916 }
6917
John Kessenich149afc32018-08-14 13:31:43 -06006918 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) {
6919 spv::IdImmediate op = { true, *opIt };
6920 spvGroupOperands.push_back(op);
6921 }
John Kessenich91cef522016-05-05 16:45:40 -06006922
6923 switch (op) {
6924 case glslang::EOpAnyInvocation:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006925 opCode = spv::OpSubgroupAnyKHR;
Rex Xu51596642016-09-21 18:56:12 +08006926 break;
John Kessenich91cef522016-05-05 16:45:40 -06006927 case glslang::EOpAllInvocations:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006928 opCode = spv::OpSubgroupAllKHR;
Rex Xu51596642016-09-21 18:56:12 +08006929 break;
John Kessenich91cef522016-05-05 16:45:40 -06006930 case glslang::EOpAllInvocationsEqual:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006931 opCode = spv::OpSubgroupAllEqualKHR;
6932 break;
Rex Xu51596642016-09-21 18:56:12 +08006933 case glslang::EOpReadInvocation:
chaocf200da82016-12-20 12:44:35 -08006934 opCode = spv::OpSubgroupReadInvocationKHR;
Rex Xub7072052016-09-26 15:53:40 +08006935 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006936 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006937 break;
6938 case glslang::EOpReadFirstInvocation:
6939 opCode = spv::OpSubgroupFirstInvocationKHR;
6940 break;
6941 case glslang::EOpBallot:
6942 {
6943 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
6944 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
6945 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
6946 //
6947 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
6948 //
6949 spv::Id uintType = builder.makeUintType(32);
6950 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
6951 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
6952
6953 std::vector<spv::Id> components;
6954 components.push_back(builder.createCompositeExtract(result, uintType, 0));
6955 components.push_back(builder.createCompositeExtract(result, uintType, 1));
6956
6957 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
6958 return builder.createUnaryOp(spv::OpBitcast, typeId,
6959 builder.createCompositeConstruct(uvec2Type, components));
6960 }
6961
Rex Xu9d93a232016-05-05 12:30:44 +08006962 case glslang::EOpMinInvocations:
6963 case glslang::EOpMaxInvocations:
6964 case glslang::EOpAddInvocations:
Rex Xu430ef402016-10-14 17:22:23 +08006965 case glslang::EOpMinInvocationsInclusiveScan:
6966 case glslang::EOpMaxInvocationsInclusiveScan:
6967 case glslang::EOpAddInvocationsInclusiveScan:
6968 case glslang::EOpMinInvocationsExclusiveScan:
6969 case glslang::EOpMaxInvocationsExclusiveScan:
6970 case glslang::EOpAddInvocationsExclusiveScan:
6971 if (op == glslang::EOpMinInvocations ||
6972 op == glslang::EOpMinInvocationsInclusiveScan ||
6973 op == glslang::EOpMinInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006974 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006975 opCode = spv::OpGroupFMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006976 else {
6977 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006978 opCode = spv::OpGroupUMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006979 else
Rex Xu51596642016-09-21 18:56:12 +08006980 opCode = spv::OpGroupSMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006981 }
Rex Xu430ef402016-10-14 17:22:23 +08006982 } else if (op == glslang::EOpMaxInvocations ||
6983 op == glslang::EOpMaxInvocationsInclusiveScan ||
6984 op == glslang::EOpMaxInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006985 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006986 opCode = spv::OpGroupFMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006987 else {
6988 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006989 opCode = spv::OpGroupUMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006990 else
Rex Xu51596642016-09-21 18:56:12 +08006991 opCode = spv::OpGroupSMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006992 }
6993 } else {
6994 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006995 opCode = spv::OpGroupFAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006996 else
Rex Xu51596642016-09-21 18:56:12 +08006997 opCode = spv::OpGroupIAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006998 }
6999
Rex Xu2bbbe062016-08-23 15:41:05 +08007000 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08007001 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08007002
7003 break;
Rex Xu9d93a232016-05-05 12:30:44 +08007004 case glslang::EOpMinInvocationsNonUniform:
7005 case glslang::EOpMaxInvocationsNonUniform:
7006 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08007007 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
7008 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
7009 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
7010 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
7011 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
7012 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
7013 if (op == glslang::EOpMinInvocationsNonUniform ||
7014 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
7015 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08007016 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007017 opCode = spv::OpGroupFMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007018 else {
7019 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007020 opCode = spv::OpGroupUMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007021 else
Rex Xu51596642016-09-21 18:56:12 +08007022 opCode = spv::OpGroupSMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007023 }
7024 }
Rex Xu430ef402016-10-14 17:22:23 +08007025 else if (op == glslang::EOpMaxInvocationsNonUniform ||
7026 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
7027 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08007028 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007029 opCode = spv::OpGroupFMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007030 else {
7031 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08007032 opCode = spv::OpGroupUMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007033 else
Rex Xu51596642016-09-21 18:56:12 +08007034 opCode = spv::OpGroupSMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007035 }
7036 }
7037 else {
7038 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08007039 opCode = spv::OpGroupFAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007040 else
Rex Xu51596642016-09-21 18:56:12 +08007041 opCode = spv::OpGroupIAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08007042 }
7043
Rex Xu2bbbe062016-08-23 15:41:05 +08007044 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08007045 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08007046
7047 break;
John Kessenich91cef522016-05-05 16:45:40 -06007048 default:
7049 logger->missingFunctionality("invocation operation");
7050 return spv::NoResult;
7051 }
Rex Xu51596642016-09-21 18:56:12 +08007052
7053 assert(opCode != spv::OpNop);
7054 return builder.createOp(opCode, typeId, spvGroupOperands);
John Kessenich91cef522016-05-05 16:45:40 -06007055}
7056
Rex Xu2bbbe062016-08-23 15:41:05 +08007057// Create group invocation operations on a vector
John Kessenich149afc32018-08-14 13:31:43 -06007058spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
7059 spv::Id typeId, std::vector<spv::Id>& operands)
Rex Xu2bbbe062016-08-23 15:41:05 +08007060{
7061 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
7062 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
Rex Xub7072052016-09-26 15:53:40 +08007063 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
chaocf200da82016-12-20 12:44:35 -08007064 op == spv::OpSubgroupReadInvocationKHR ||
John Kessenich8985fc92020-03-03 10:25:07 -07007065 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD ||
7066 op == spv::OpGroupSMinNonUniformAMD ||
7067 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD ||
7068 op == spv::OpGroupSMaxNonUniformAMD ||
Rex Xu2bbbe062016-08-23 15:41:05 +08007069 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
7070
7071 // Handle group invocation operations scalar by scalar.
7072 // The result type is the same type as the original type.
7073 // The algorithm is to:
7074 // - break the vector into scalars
7075 // - apply the operation to each scalar
7076 // - make a vector out the scalar results
7077
7078 // get the types sorted out
Rex Xub7072052016-09-26 15:53:40 +08007079 int numComponents = builder.getNumComponents(operands[0]);
7080 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
Rex Xu2bbbe062016-08-23 15:41:05 +08007081 std::vector<spv::Id> results;
7082
7083 // do each scalar op
7084 for (int comp = 0; comp < numComponents; ++comp) {
7085 std::vector<unsigned int> indexes;
7086 indexes.push_back(comp);
John Kessenich149afc32018-08-14 13:31:43 -06007087 spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) };
7088 std::vector<spv::IdImmediate> spvGroupOperands;
chaocf200da82016-12-20 12:44:35 -08007089 if (op == spv::OpSubgroupReadInvocationKHR) {
7090 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06007091 spv::IdImmediate operand = { true, operands[1] };
7092 spvGroupOperands.push_back(operand);
chaocf200da82016-12-20 12:44:35 -08007093 } else if (op == spv::OpGroupBroadcast) {
John Kessenich149afc32018-08-14 13:31:43 -06007094 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7095 spvGroupOperands.push_back(scope);
Rex Xub7072052016-09-26 15:53:40 +08007096 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06007097 spv::IdImmediate operand = { true, operands[1] };
7098 spvGroupOperands.push_back(operand);
Rex Xub7072052016-09-26 15:53:40 +08007099 } else {
John Kessenich149afc32018-08-14 13:31:43 -06007100 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7101 spvGroupOperands.push_back(scope);
John Kessenichd122a722018-09-18 03:43:30 -06007102 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007103 spvGroupOperands.push_back(groupOp);
Rex Xub7072052016-09-26 15:53:40 +08007104 spvGroupOperands.push_back(scalar);
7105 }
Rex Xu2bbbe062016-08-23 15:41:05 +08007106
Rex Xub7072052016-09-26 15:53:40 +08007107 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
Rex Xu2bbbe062016-08-23 15:41:05 +08007108 }
7109
7110 // put the pieces together
7111 return builder.createCompositeConstruct(typeId, results);
7112}
Rex Xu2bbbe062016-08-23 15:41:05 +08007113
John Kessenich66011cb2018-03-06 16:12:04 -07007114// Create subgroup invocation operations.
John Kessenich149afc32018-08-14 13:31:43 -06007115spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
7116 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich66011cb2018-03-06 16:12:04 -07007117{
7118 // Add the required capabilities.
7119 switch (op) {
7120 case glslang::EOpSubgroupElect:
7121 builder.addCapability(spv::CapabilityGroupNonUniform);
7122 break;
7123 case glslang::EOpSubgroupAll:
7124 case glslang::EOpSubgroupAny:
7125 case glslang::EOpSubgroupAllEqual:
7126 builder.addCapability(spv::CapabilityGroupNonUniform);
7127 builder.addCapability(spv::CapabilityGroupNonUniformVote);
7128 break;
7129 case glslang::EOpSubgroupBroadcast:
7130 case glslang::EOpSubgroupBroadcastFirst:
7131 case glslang::EOpSubgroupBallot:
7132 case glslang::EOpSubgroupInverseBallot:
7133 case glslang::EOpSubgroupBallotBitExtract:
7134 case glslang::EOpSubgroupBallotBitCount:
7135 case glslang::EOpSubgroupBallotInclusiveBitCount:
7136 case glslang::EOpSubgroupBallotExclusiveBitCount:
7137 case glslang::EOpSubgroupBallotFindLSB:
7138 case glslang::EOpSubgroupBallotFindMSB:
7139 builder.addCapability(spv::CapabilityGroupNonUniform);
7140 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
7141 break;
7142 case glslang::EOpSubgroupShuffle:
7143 case glslang::EOpSubgroupShuffleXor:
7144 builder.addCapability(spv::CapabilityGroupNonUniform);
7145 builder.addCapability(spv::CapabilityGroupNonUniformShuffle);
7146 break;
7147 case glslang::EOpSubgroupShuffleUp:
7148 case glslang::EOpSubgroupShuffleDown:
7149 builder.addCapability(spv::CapabilityGroupNonUniform);
7150 builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative);
7151 break;
7152 case glslang::EOpSubgroupAdd:
7153 case glslang::EOpSubgroupMul:
7154 case glslang::EOpSubgroupMin:
7155 case glslang::EOpSubgroupMax:
7156 case glslang::EOpSubgroupAnd:
7157 case glslang::EOpSubgroupOr:
7158 case glslang::EOpSubgroupXor:
7159 case glslang::EOpSubgroupInclusiveAdd:
7160 case glslang::EOpSubgroupInclusiveMul:
7161 case glslang::EOpSubgroupInclusiveMin:
7162 case glslang::EOpSubgroupInclusiveMax:
7163 case glslang::EOpSubgroupInclusiveAnd:
7164 case glslang::EOpSubgroupInclusiveOr:
7165 case glslang::EOpSubgroupInclusiveXor:
7166 case glslang::EOpSubgroupExclusiveAdd:
7167 case glslang::EOpSubgroupExclusiveMul:
7168 case glslang::EOpSubgroupExclusiveMin:
7169 case glslang::EOpSubgroupExclusiveMax:
7170 case glslang::EOpSubgroupExclusiveAnd:
7171 case glslang::EOpSubgroupExclusiveOr:
7172 case glslang::EOpSubgroupExclusiveXor:
7173 builder.addCapability(spv::CapabilityGroupNonUniform);
7174 builder.addCapability(spv::CapabilityGroupNonUniformArithmetic);
7175 break;
7176 case glslang::EOpSubgroupClusteredAdd:
7177 case glslang::EOpSubgroupClusteredMul:
7178 case glslang::EOpSubgroupClusteredMin:
7179 case glslang::EOpSubgroupClusteredMax:
7180 case glslang::EOpSubgroupClusteredAnd:
7181 case glslang::EOpSubgroupClusteredOr:
7182 case glslang::EOpSubgroupClusteredXor:
7183 builder.addCapability(spv::CapabilityGroupNonUniform);
7184 builder.addCapability(spv::CapabilityGroupNonUniformClustered);
7185 break;
7186 case glslang::EOpSubgroupQuadBroadcast:
7187 case glslang::EOpSubgroupQuadSwapHorizontal:
7188 case glslang::EOpSubgroupQuadSwapVertical:
7189 case glslang::EOpSubgroupQuadSwapDiagonal:
7190 builder.addCapability(spv::CapabilityGroupNonUniform);
7191 builder.addCapability(spv::CapabilityGroupNonUniformQuad);
7192 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007193 case glslang::EOpSubgroupPartitionedAdd:
7194 case glslang::EOpSubgroupPartitionedMul:
7195 case glslang::EOpSubgroupPartitionedMin:
7196 case glslang::EOpSubgroupPartitionedMax:
7197 case glslang::EOpSubgroupPartitionedAnd:
7198 case glslang::EOpSubgroupPartitionedOr:
7199 case glslang::EOpSubgroupPartitionedXor:
7200 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7201 case glslang::EOpSubgroupPartitionedInclusiveMul:
7202 case glslang::EOpSubgroupPartitionedInclusiveMin:
7203 case glslang::EOpSubgroupPartitionedInclusiveMax:
7204 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7205 case glslang::EOpSubgroupPartitionedInclusiveOr:
7206 case glslang::EOpSubgroupPartitionedInclusiveXor:
7207 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7208 case glslang::EOpSubgroupPartitionedExclusiveMul:
7209 case glslang::EOpSubgroupPartitionedExclusiveMin:
7210 case glslang::EOpSubgroupPartitionedExclusiveMax:
7211 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7212 case glslang::EOpSubgroupPartitionedExclusiveOr:
7213 case glslang::EOpSubgroupPartitionedExclusiveXor:
7214 builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned);
7215 builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV);
7216 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007217 default: assert(0 && "Unhandled subgroup operation!");
7218 }
7219
Jeff Bolzc5b669e2019-09-08 08:49:18 -05007220
7221 const bool isUnsigned = isTypeUnsignedInt(typeProxy);
7222 const bool isFloat = isTypeFloat(typeProxy);
John Kessenich66011cb2018-03-06 16:12:04 -07007223 const bool isBool = typeProxy == glslang::EbtBool;
7224
7225 spv::Op opCode = spv::OpNop;
7226
7227 // Figure out which opcode to use.
7228 switch (op) {
7229 case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break;
7230 case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break;
7231 case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break;
7232 case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break;
7233 case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break;
7234 case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break;
7235 case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break;
7236 case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break;
7237 case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break;
7238 case glslang::EOpSubgroupBallotBitCount:
7239 case glslang::EOpSubgroupBallotInclusiveBitCount:
7240 case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break;
7241 case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break;
7242 case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break;
7243 case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break;
7244 case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break;
7245 case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break;
7246 case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break;
7247 case glslang::EOpSubgroupAdd:
7248 case glslang::EOpSubgroupInclusiveAdd:
7249 case glslang::EOpSubgroupExclusiveAdd:
7250 case glslang::EOpSubgroupClusteredAdd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007251 case glslang::EOpSubgroupPartitionedAdd:
7252 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7253 case glslang::EOpSubgroupPartitionedExclusiveAdd:
John Kessenich66011cb2018-03-06 16:12:04 -07007254 if (isFloat) {
7255 opCode = spv::OpGroupNonUniformFAdd;
7256 } else {
7257 opCode = spv::OpGroupNonUniformIAdd;
7258 }
7259 break;
7260 case glslang::EOpSubgroupMul:
7261 case glslang::EOpSubgroupInclusiveMul:
7262 case glslang::EOpSubgroupExclusiveMul:
7263 case glslang::EOpSubgroupClusteredMul:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007264 case glslang::EOpSubgroupPartitionedMul:
7265 case glslang::EOpSubgroupPartitionedInclusiveMul:
7266 case glslang::EOpSubgroupPartitionedExclusiveMul:
John Kessenich66011cb2018-03-06 16:12:04 -07007267 if (isFloat) {
7268 opCode = spv::OpGroupNonUniformFMul;
7269 } else {
7270 opCode = spv::OpGroupNonUniformIMul;
7271 }
7272 break;
7273 case glslang::EOpSubgroupMin:
7274 case glslang::EOpSubgroupInclusiveMin:
7275 case glslang::EOpSubgroupExclusiveMin:
7276 case glslang::EOpSubgroupClusteredMin:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007277 case glslang::EOpSubgroupPartitionedMin:
7278 case glslang::EOpSubgroupPartitionedInclusiveMin:
7279 case glslang::EOpSubgroupPartitionedExclusiveMin:
John Kessenich66011cb2018-03-06 16:12:04 -07007280 if (isFloat) {
7281 opCode = spv::OpGroupNonUniformFMin;
7282 } else if (isUnsigned) {
7283 opCode = spv::OpGroupNonUniformUMin;
7284 } else {
7285 opCode = spv::OpGroupNonUniformSMin;
7286 }
7287 break;
7288 case glslang::EOpSubgroupMax:
7289 case glslang::EOpSubgroupInclusiveMax:
7290 case glslang::EOpSubgroupExclusiveMax:
7291 case glslang::EOpSubgroupClusteredMax:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007292 case glslang::EOpSubgroupPartitionedMax:
7293 case glslang::EOpSubgroupPartitionedInclusiveMax:
7294 case glslang::EOpSubgroupPartitionedExclusiveMax:
John Kessenich66011cb2018-03-06 16:12:04 -07007295 if (isFloat) {
7296 opCode = spv::OpGroupNonUniformFMax;
7297 } else if (isUnsigned) {
7298 opCode = spv::OpGroupNonUniformUMax;
7299 } else {
7300 opCode = spv::OpGroupNonUniformSMax;
7301 }
7302 break;
7303 case glslang::EOpSubgroupAnd:
7304 case glslang::EOpSubgroupInclusiveAnd:
7305 case glslang::EOpSubgroupExclusiveAnd:
7306 case glslang::EOpSubgroupClusteredAnd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007307 case glslang::EOpSubgroupPartitionedAnd:
7308 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7309 case glslang::EOpSubgroupPartitionedExclusiveAnd:
John Kessenich66011cb2018-03-06 16:12:04 -07007310 if (isBool) {
7311 opCode = spv::OpGroupNonUniformLogicalAnd;
7312 } else {
7313 opCode = spv::OpGroupNonUniformBitwiseAnd;
7314 }
7315 break;
7316 case glslang::EOpSubgroupOr:
7317 case glslang::EOpSubgroupInclusiveOr:
7318 case glslang::EOpSubgroupExclusiveOr:
7319 case glslang::EOpSubgroupClusteredOr:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007320 case glslang::EOpSubgroupPartitionedOr:
7321 case glslang::EOpSubgroupPartitionedInclusiveOr:
7322 case glslang::EOpSubgroupPartitionedExclusiveOr:
John Kessenich66011cb2018-03-06 16:12:04 -07007323 if (isBool) {
7324 opCode = spv::OpGroupNonUniformLogicalOr;
7325 } else {
7326 opCode = spv::OpGroupNonUniformBitwiseOr;
7327 }
7328 break;
7329 case glslang::EOpSubgroupXor:
7330 case glslang::EOpSubgroupInclusiveXor:
7331 case glslang::EOpSubgroupExclusiveXor:
7332 case glslang::EOpSubgroupClusteredXor:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007333 case glslang::EOpSubgroupPartitionedXor:
7334 case glslang::EOpSubgroupPartitionedInclusiveXor:
7335 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07007336 if (isBool) {
7337 opCode = spv::OpGroupNonUniformLogicalXor;
7338 } else {
7339 opCode = spv::OpGroupNonUniformBitwiseXor;
7340 }
7341 break;
7342 case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break;
7343 case glslang::EOpSubgroupQuadSwapHorizontal:
7344 case glslang::EOpSubgroupQuadSwapVertical:
7345 case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break;
7346 default: assert(0 && "Unhandled subgroup operation!");
7347 }
7348
John Kessenich149afc32018-08-14 13:31:43 -06007349 // get the right Group Operation
7350 spv::GroupOperation groupOperation = spv::GroupOperationMax;
John Kessenich66011cb2018-03-06 16:12:04 -07007351 switch (op) {
John Kessenich149afc32018-08-14 13:31:43 -06007352 default:
7353 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007354 case glslang::EOpSubgroupBallotBitCount:
7355 case glslang::EOpSubgroupAdd:
7356 case glslang::EOpSubgroupMul:
7357 case glslang::EOpSubgroupMin:
7358 case glslang::EOpSubgroupMax:
7359 case glslang::EOpSubgroupAnd:
7360 case glslang::EOpSubgroupOr:
7361 case glslang::EOpSubgroupXor:
John Kessenich149afc32018-08-14 13:31:43 -06007362 groupOperation = spv::GroupOperationReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07007363 break;
7364 case glslang::EOpSubgroupBallotInclusiveBitCount:
7365 case glslang::EOpSubgroupInclusiveAdd:
7366 case glslang::EOpSubgroupInclusiveMul:
7367 case glslang::EOpSubgroupInclusiveMin:
7368 case glslang::EOpSubgroupInclusiveMax:
7369 case glslang::EOpSubgroupInclusiveAnd:
7370 case glslang::EOpSubgroupInclusiveOr:
7371 case glslang::EOpSubgroupInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007372 groupOperation = spv::GroupOperationInclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07007373 break;
7374 case glslang::EOpSubgroupBallotExclusiveBitCount:
7375 case glslang::EOpSubgroupExclusiveAdd:
7376 case glslang::EOpSubgroupExclusiveMul:
7377 case glslang::EOpSubgroupExclusiveMin:
7378 case glslang::EOpSubgroupExclusiveMax:
7379 case glslang::EOpSubgroupExclusiveAnd:
7380 case glslang::EOpSubgroupExclusiveOr:
7381 case glslang::EOpSubgroupExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007382 groupOperation = spv::GroupOperationExclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07007383 break;
7384 case glslang::EOpSubgroupClusteredAdd:
7385 case glslang::EOpSubgroupClusteredMul:
7386 case glslang::EOpSubgroupClusteredMin:
7387 case glslang::EOpSubgroupClusteredMax:
7388 case glslang::EOpSubgroupClusteredAnd:
7389 case glslang::EOpSubgroupClusteredOr:
7390 case glslang::EOpSubgroupClusteredXor:
John Kessenich149afc32018-08-14 13:31:43 -06007391 groupOperation = spv::GroupOperationClusteredReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07007392 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007393 case glslang::EOpSubgroupPartitionedAdd:
7394 case glslang::EOpSubgroupPartitionedMul:
7395 case glslang::EOpSubgroupPartitionedMin:
7396 case glslang::EOpSubgroupPartitionedMax:
7397 case glslang::EOpSubgroupPartitionedAnd:
7398 case glslang::EOpSubgroupPartitionedOr:
7399 case glslang::EOpSubgroupPartitionedXor:
John Kessenich149afc32018-08-14 13:31:43 -06007400 groupOperation = spv::GroupOperationPartitionedReduceNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007401 break;
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:
John Kessenich149afc32018-08-14 13:31:43 -06007409 groupOperation = spv::GroupOperationPartitionedInclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007410 break;
7411 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7412 case glslang::EOpSubgroupPartitionedExclusiveMul:
7413 case glslang::EOpSubgroupPartitionedExclusiveMin:
7414 case glslang::EOpSubgroupPartitionedExclusiveMax:
7415 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7416 case glslang::EOpSubgroupPartitionedExclusiveOr:
7417 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007418 groupOperation = spv::GroupOperationPartitionedExclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007419 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007420 }
7421
John Kessenich149afc32018-08-14 13:31:43 -06007422 // build the instruction
7423 std::vector<spv::IdImmediate> spvGroupOperands;
7424
7425 // Every operation begins with the Execution Scope operand.
7426 spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7427 spvGroupOperands.push_back(executionScope);
7428
7429 // Next, for all operations that use a Group Operation, push that as an operand.
7430 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06007431 spv::IdImmediate groupOperand = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007432 spvGroupOperands.push_back(groupOperand);
7433 }
7434
John Kessenich66011cb2018-03-06 16:12:04 -07007435 // Push back the operands next.
John Kessenich149afc32018-08-14 13:31:43 -06007436 for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) {
7437 spv::IdImmediate operand = { true, *opIt };
7438 spvGroupOperands.push_back(operand);
John Kessenich66011cb2018-03-06 16:12:04 -07007439 }
7440
7441 // Some opcodes have additional operands.
John Kessenich149afc32018-08-14 13:31:43 -06007442 spv::Id directionId = spv::NoResult;
John Kessenich66011cb2018-03-06 16:12:04 -07007443 switch (op) {
7444 default: break;
John Kessenich149afc32018-08-14 13:31:43 -06007445 case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break;
7446 case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break;
7447 case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break;
7448 }
7449 if (directionId != spv::NoResult) {
7450 spv::IdImmediate direction = { true, directionId };
7451 spvGroupOperands.push_back(direction);
John Kessenich66011cb2018-03-06 16:12:04 -07007452 }
7453
7454 return builder.createOp(opCode, typeId, spvGroupOperands);
7455}
7456
John Kessenich8985fc92020-03-03 10:25:07 -07007457spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision,
7458 spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06007459{
John Kessenich66011cb2018-03-06 16:12:04 -07007460 bool isUnsigned = isTypeUnsignedInt(typeProxy);
7461 bool isFloat = isTypeFloat(typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -06007462
John Kessenich140f3df2015-06-26 16:58:36 -06007463 spv::Op opCode = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08007464 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06007465 int libCall = -1;
Mark Adams364c21c2016-01-06 13:41:02 -05007466 size_t consumedOperands = operands.size();
John Kessenich55e7d112015-11-15 21:33:39 -07007467 spv::Id typeId0 = 0;
7468 if (consumedOperands > 0)
7469 typeId0 = builder.getTypeId(operands[0]);
Rex Xu470026f2017-03-29 17:12:40 +08007470 spv::Id typeId1 = 0;
7471 if (consumedOperands > 1)
7472 typeId1 = builder.getTypeId(operands[1]);
John Kessenich55e7d112015-11-15 21:33:39 -07007473 spv::Id frexpIntType = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007474
7475 switch (op) {
7476 case glslang::EOpMin:
John Kessenich5e4b1242015-08-06 22:53:06 -06007477 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007478 libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin;
John Kessenich5e4b1242015-08-06 22:53:06 -06007479 else if (isUnsigned)
7480 libCall = spv::GLSLstd450UMin;
7481 else
7482 libCall = spv::GLSLstd450SMin;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007483 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007484 break;
7485 case glslang::EOpModf:
John Kessenich5e4b1242015-08-06 22:53:06 -06007486 libCall = spv::GLSLstd450Modf;
John Kessenich140f3df2015-06-26 16:58:36 -06007487 break;
7488 case glslang::EOpMax:
John Kessenich5e4b1242015-08-06 22:53:06 -06007489 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007490 libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax;
John Kessenich5e4b1242015-08-06 22:53:06 -06007491 else if (isUnsigned)
7492 libCall = spv::GLSLstd450UMax;
7493 else
7494 libCall = spv::GLSLstd450SMax;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007495 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007496 break;
7497 case glslang::EOpPow:
John Kessenich5e4b1242015-08-06 22:53:06 -06007498 libCall = spv::GLSLstd450Pow;
John Kessenich140f3df2015-06-26 16:58:36 -06007499 break;
7500 case glslang::EOpDot:
7501 opCode = spv::OpDot;
7502 break;
7503 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06007504 libCall = spv::GLSLstd450Atan2;
John Kessenich140f3df2015-06-26 16:58:36 -06007505 break;
7506
7507 case glslang::EOpClamp:
John Kessenich5e4b1242015-08-06 22:53:06 -06007508 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007509 libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp;
John Kessenich5e4b1242015-08-06 22:53:06 -06007510 else if (isUnsigned)
7511 libCall = spv::GLSLstd450UClamp;
7512 else
7513 libCall = spv::GLSLstd450SClamp;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007514 builder.promoteScalar(precision, operands.front(), operands[1]);
7515 builder.promoteScalar(precision, operands.front(), operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007516 break;
7517 case glslang::EOpMix:
Rex Xud715adc2016-03-15 12:08:31 +08007518 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
7519 assert(isFloat);
John Kessenich55e7d112015-11-15 21:33:39 -07007520 libCall = spv::GLSLstd450FMix;
Rex Xud715adc2016-03-15 12:08:31 +08007521 } else {
John Kessenich6c292d32016-02-15 20:58:50 -07007522 opCode = spv::OpSelect;
Rex Xud715adc2016-03-15 12:08:31 +08007523 std::swap(operands.front(), operands.back());
John Kessenich6c292d32016-02-15 20:58:50 -07007524 }
John Kesseniche7c83cf2015-12-13 13:34:37 -07007525 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007526 break;
7527 case glslang::EOpStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007528 libCall = spv::GLSLstd450Step;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007529 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007530 break;
7531 case glslang::EOpSmoothStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007532 libCall = spv::GLSLstd450SmoothStep;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007533 builder.promoteScalar(precision, operands[0], operands[2]);
7534 builder.promoteScalar(precision, operands[1], operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007535 break;
7536
7537 case glslang::EOpDistance:
John Kessenich5e4b1242015-08-06 22:53:06 -06007538 libCall = spv::GLSLstd450Distance;
John Kessenich140f3df2015-06-26 16:58:36 -06007539 break;
7540 case glslang::EOpCross:
John Kessenich5e4b1242015-08-06 22:53:06 -06007541 libCall = spv::GLSLstd450Cross;
John Kessenich140f3df2015-06-26 16:58:36 -06007542 break;
7543 case glslang::EOpFaceForward:
John Kessenich5e4b1242015-08-06 22:53:06 -06007544 libCall = spv::GLSLstd450FaceForward;
John Kessenich140f3df2015-06-26 16:58:36 -06007545 break;
7546 case glslang::EOpReflect:
John Kessenich5e4b1242015-08-06 22:53:06 -06007547 libCall = spv::GLSLstd450Reflect;
John Kessenich140f3df2015-06-26 16:58:36 -06007548 break;
7549 case glslang::EOpRefract:
John Kessenich5e4b1242015-08-06 22:53:06 -06007550 libCall = spv::GLSLstd450Refract;
John Kessenich140f3df2015-06-26 16:58:36 -06007551 break;
John Kessenich3dd1ce52019-10-17 07:08:40 -06007552 case glslang::EOpBarrier:
7553 {
7554 // This is for the extended controlBarrier function, with four operands.
7555 // The unextended barrier() goes through createNoArgOperation.
7556 assert(operands.size() == 4);
7557 unsigned int executionScope = builder.getConstantScalar(operands[0]);
7558 unsigned int memoryScope = builder.getConstantScalar(operands[1]);
7559 unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]);
John Kessenich8985fc92020-03-03 10:25:07 -07007560 builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope,
7561 (spv::MemorySemanticsMask)semantics);
John Kessenich3dd1ce52019-10-17 07:08:40 -06007562 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7563 spv::MemorySemanticsMakeVisibleKHRMask |
7564 spv::MemorySemanticsOutputMemoryKHRMask |
7565 spv::MemorySemanticsVolatileMask)) {
7566 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7567 }
John Kessenich8985fc92020-03-03 10:25:07 -07007568 if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice ||
7569 memoryScope == spv::ScopeDevice)) {
John Kessenich3dd1ce52019-10-17 07:08:40 -06007570 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7571 }
7572 return 0;
7573 }
7574 break;
7575 case glslang::EOpMemoryBarrier:
7576 {
7577 // This is for the extended memoryBarrier function, with three operands.
7578 // The unextended memoryBarrier() goes through createNoArgOperation.
7579 assert(operands.size() == 3);
7580 unsigned int memoryScope = builder.getConstantScalar(operands[0]);
7581 unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]);
7582 builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
7583 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7584 spv::MemorySemanticsMakeVisibleKHRMask |
7585 spv::MemorySemanticsOutputMemoryKHRMask |
7586 spv::MemorySemanticsVolatileMask)) {
7587 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7588 }
7589 if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) {
7590 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7591 }
7592 return 0;
7593 }
7594 break;
7595
John Kessenicha28f7a72019-08-06 07:00:58 -06007596#ifndef GLSLANG_WEB
Rex Xu7a26c172015-12-08 17:12:09 +08007597 case glslang::EOpInterpolateAtSample:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007598 if (typeProxy == glslang::EbtFloat16)
7599 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007600 libCall = spv::GLSLstd450InterpolateAtSample;
7601 break;
7602 case glslang::EOpInterpolateAtOffset:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007603 if (typeProxy == glslang::EbtFloat16)
7604 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007605 libCall = spv::GLSLstd450InterpolateAtOffset;
7606 break;
John Kessenich55e7d112015-11-15 21:33:39 -07007607 case glslang::EOpAddCarry:
7608 opCode = spv::OpIAddCarry;
7609 typeId = builder.makeStructResultType(typeId0, typeId0);
7610 consumedOperands = 2;
7611 break;
7612 case glslang::EOpSubBorrow:
7613 opCode = spv::OpISubBorrow;
7614 typeId = builder.makeStructResultType(typeId0, typeId0);
7615 consumedOperands = 2;
7616 break;
7617 case glslang::EOpUMulExtended:
7618 opCode = spv::OpUMulExtended;
7619 typeId = builder.makeStructResultType(typeId0, typeId0);
7620 consumedOperands = 2;
7621 break;
7622 case glslang::EOpIMulExtended:
7623 opCode = spv::OpSMulExtended;
7624 typeId = builder.makeStructResultType(typeId0, typeId0);
7625 consumedOperands = 2;
7626 break;
7627 case glslang::EOpBitfieldExtract:
7628 if (isUnsigned)
7629 opCode = spv::OpBitFieldUExtract;
7630 else
7631 opCode = spv::OpBitFieldSExtract;
7632 break;
7633 case glslang::EOpBitfieldInsert:
7634 opCode = spv::OpBitFieldInsert;
7635 break;
7636
7637 case glslang::EOpFma:
7638 libCall = spv::GLSLstd450Fma;
7639 break;
7640 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007641 {
7642 libCall = spv::GLSLstd450FrexpStruct;
7643 assert(builder.isPointerType(typeId1));
7644 typeId1 = builder.getContainedTypeId(typeId1);
Rex Xu470026f2017-03-29 17:12:40 +08007645 int width = builder.getScalarTypeWidth(typeId1);
Rex Xu7c88aff2018-04-11 16:56:50 +08007646 if (width == 16)
7647 // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16
7648 builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16);
Rex Xu470026f2017-03-29 17:12:40 +08007649 if (builder.getNumComponents(operands[0]) == 1)
7650 frexpIntType = builder.makeIntegerType(width, true);
7651 else
John Kessenich8985fc92020-03-03 10:25:07 -07007652 frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true),
7653 builder.getNumComponents(operands[0]));
Rex Xu470026f2017-03-29 17:12:40 +08007654 typeId = builder.makeStructResultType(typeId0, frexpIntType);
7655 consumedOperands = 1;
7656 }
John Kessenich55e7d112015-11-15 21:33:39 -07007657 break;
7658 case glslang::EOpLdexp:
7659 libCall = spv::GLSLstd450Ldexp;
7660 break;
7661
Rex Xu574ab042016-04-14 16:53:07 +08007662 case glslang::EOpReadInvocation:
Rex Xu51596642016-09-21 18:56:12 +08007663 return createInvocationsOperation(op, typeId, operands, typeProxy);
Rex Xu574ab042016-04-14 16:53:07 +08007664
John Kessenich66011cb2018-03-06 16:12:04 -07007665 case glslang::EOpSubgroupBroadcast:
7666 case glslang::EOpSubgroupBallotBitExtract:
7667 case glslang::EOpSubgroupShuffle:
7668 case glslang::EOpSubgroupShuffleXor:
7669 case glslang::EOpSubgroupShuffleUp:
7670 case glslang::EOpSubgroupShuffleDown:
7671 case glslang::EOpSubgroupClusteredAdd:
7672 case glslang::EOpSubgroupClusteredMul:
7673 case glslang::EOpSubgroupClusteredMin:
7674 case glslang::EOpSubgroupClusteredMax:
7675 case glslang::EOpSubgroupClusteredAnd:
7676 case glslang::EOpSubgroupClusteredOr:
7677 case glslang::EOpSubgroupClusteredXor:
7678 case glslang::EOpSubgroupQuadBroadcast:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007679 case glslang::EOpSubgroupPartitionedAdd:
7680 case glslang::EOpSubgroupPartitionedMul:
7681 case glslang::EOpSubgroupPartitionedMin:
7682 case glslang::EOpSubgroupPartitionedMax:
7683 case glslang::EOpSubgroupPartitionedAnd:
7684 case glslang::EOpSubgroupPartitionedOr:
7685 case glslang::EOpSubgroupPartitionedXor:
7686 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7687 case glslang::EOpSubgroupPartitionedInclusiveMul:
7688 case glslang::EOpSubgroupPartitionedInclusiveMin:
7689 case glslang::EOpSubgroupPartitionedInclusiveMax:
7690 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7691 case glslang::EOpSubgroupPartitionedInclusiveOr:
7692 case glslang::EOpSubgroupPartitionedInclusiveXor:
7693 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7694 case glslang::EOpSubgroupPartitionedExclusiveMul:
7695 case glslang::EOpSubgroupPartitionedExclusiveMin:
7696 case glslang::EOpSubgroupPartitionedExclusiveMax:
7697 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7698 case glslang::EOpSubgroupPartitionedExclusiveOr:
7699 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07007700 return createSubgroupOperation(op, typeId, operands, typeProxy);
7701
Rex Xu9d93a232016-05-05 12:30:44 +08007702 case glslang::EOpSwizzleInvocations:
7703 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7704 libCall = spv::SwizzleInvocationsAMD;
7705 break;
7706 case glslang::EOpSwizzleInvocationsMasked:
7707 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7708 libCall = spv::SwizzleInvocationsMaskedAMD;
7709 break;
7710 case glslang::EOpWriteInvocation:
7711 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7712 libCall = spv::WriteInvocationAMD;
7713 break;
7714
7715 case glslang::EOpMin3:
7716 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7717 if (isFloat)
7718 libCall = spv::FMin3AMD;
7719 else {
7720 if (isUnsigned)
7721 libCall = spv::UMin3AMD;
7722 else
7723 libCall = spv::SMin3AMD;
7724 }
7725 break;
7726 case glslang::EOpMax3:
7727 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7728 if (isFloat)
7729 libCall = spv::FMax3AMD;
7730 else {
7731 if (isUnsigned)
7732 libCall = spv::UMax3AMD;
7733 else
7734 libCall = spv::SMax3AMD;
7735 }
7736 break;
7737 case glslang::EOpMid3:
7738 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7739 if (isFloat)
7740 libCall = spv::FMid3AMD;
7741 else {
7742 if (isUnsigned)
7743 libCall = spv::UMid3AMD;
7744 else
7745 libCall = spv::SMid3AMD;
7746 }
7747 break;
7748
7749 case glslang::EOpInterpolateAtVertex:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007750 if (typeProxy == glslang::EbtFloat16)
7751 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu9d93a232016-05-05 12:30:44 +08007752 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
7753 libCall = spv::InterpolateAtVertexAMD;
7754 break;
Chao Chen3c366992018-09-19 11:41:59 -07007755
Daniel Kochdb32b242020-03-17 20:42:47 -04007756 case glslang::EOpReportIntersection:
Chao Chenb50c02e2018-09-19 11:42:24 -07007757 typeId = builder.makeBoolType();
Daniel Kochdb32b242020-03-17 20:42:47 -04007758 opCode = spv::OpReportIntersectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007759 break;
Daniel Kochdb32b242020-03-17 20:42:47 -04007760 case glslang::EOpTrace:
Daniel Kochdb32b242020-03-17 20:42:47 -04007761 builder.createNoResultOp(spv::OpTraceRayKHR, operands);
Ashwin Leleff1783d2018-10-22 16:41:44 -07007762 return 0;
Daniel Kochdb32b242020-03-17 20:42:47 -04007763 case glslang::EOpExecuteCallable:
Daniel Kochdb32b242020-03-17 20:42:47 -04007764 builder.createNoResultOp(spv::OpExecuteCallableKHR, operands);
Chao Chenb50c02e2018-09-19 11:42:24 -07007765 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007766
7767 case glslang::EOpRayQueryInitialize:
Torosdagli06c2eee2020-03-19 11:09:57 -04007768 builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands);
7769 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007770 case glslang::EOpRayQueryTerminate:
Torosdagli06c2eee2020-03-19 11:09:57 -04007771 builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands);
7772 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007773 case glslang::EOpRayQueryGenerateIntersection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007774 builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands);
7775 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007776 case glslang::EOpRayQueryConfirmIntersection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007777 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands);
7778 return 0;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007779 case glslang::EOpRayQueryProceed:
Torosdagli06c2eee2020-03-19 11:09:57 -04007780 typeId = builder.makeBoolType();
7781 opCode = spv::OpRayQueryProceedKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007782 break;
7783 case glslang::EOpRayQueryGetIntersectionType:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04007784 typeId = builder.makeUintType(32);
Torosdagli06c2eee2020-03-19 11:09:57 -04007785 opCode = spv::OpRayQueryGetIntersectionTypeKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007786 break;
7787 case glslang::EOpRayQueryGetRayTMin:
Torosdagli06c2eee2020-03-19 11:09:57 -04007788 typeId = builder.makeFloatType(32);
7789 opCode = spv::OpRayQueryGetRayTMinKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007790 break;
7791 case glslang::EOpRayQueryGetRayFlags:
Torosdagli06c2eee2020-03-19 11:09:57 -04007792 typeId = builder.makeIntType(32);
7793 opCode = spv::OpRayQueryGetRayFlagsKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007794 break;
7795 case glslang::EOpRayQueryGetIntersectionT:
Neslisah Torosdagli50a72282020-03-20 18:23:27 -04007796 typeId = builder.makeFloatType(32);
Torosdagli06c2eee2020-03-19 11:09:57 -04007797 opCode = spv::OpRayQueryGetIntersectionTKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007798 break;
7799 case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04007800 typeId = builder.makeIntType(32);
7801 opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007802 break;
7803 case glslang::EOpRayQueryGetIntersectionInstanceId:
Torosdagli06c2eee2020-03-19 11:09:57 -04007804 typeId = builder.makeIntType(32);
7805 opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007806 break;
7807 case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:
Torosdagli06c2eee2020-03-19 11:09:57 -04007808 typeId = builder.makeIntType(32);
7809 opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007810 break;
7811 case glslang::EOpRayQueryGetIntersectionGeometryIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04007812 typeId = builder.makeIntType(32);
7813 opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007814 break;
7815 case glslang::EOpRayQueryGetIntersectionPrimitiveIndex:
Torosdagli06c2eee2020-03-19 11:09:57 -04007816 typeId = builder.makeIntType(32);
7817 opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007818 break;
7819 case glslang::EOpRayQueryGetIntersectionBarycentrics:
Torosdagli06c2eee2020-03-19 11:09:57 -04007820 typeId = builder.makeVectorType(builder.makeFloatType(32), 2);
7821 opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007822 break;
7823 case glslang::EOpRayQueryGetIntersectionFrontFace:
Torosdagli06c2eee2020-03-19 11:09:57 -04007824 typeId = builder.makeBoolType();
7825 opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007826 break;
7827 case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque:
Torosdagli06c2eee2020-03-19 11:09:57 -04007828 typeId = builder.makeBoolType();
7829 opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007830 break;
7831 case glslang::EOpRayQueryGetIntersectionObjectRayDirection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007832 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
7833 opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007834 break;
7835 case glslang::EOpRayQueryGetIntersectionObjectRayOrigin:
Torosdagli06c2eee2020-03-19 11:09:57 -04007836 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
7837 opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007838 break;
7839 case glslang::EOpRayQueryGetWorldRayDirection:
Torosdagli06c2eee2020-03-19 11:09:57 -04007840 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
7841 opCode = spv::OpRayQueryGetWorldRayDirectionKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007842 break;
7843 case glslang::EOpRayQueryGetWorldRayOrigin:
Torosdagli06c2eee2020-03-19 11:09:57 -04007844 typeId = builder.makeVectorType(builder.makeFloatType(32), 3);
7845 opCode = spv::OpRayQueryGetWorldRayOriginKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007846 break;
7847 case glslang::EOpRayQueryGetIntersectionObjectToWorld:
Torosdagli06c2eee2020-03-19 11:09:57 -04007848 typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
Torosdagli06c2eee2020-03-19 11:09:57 -04007849 opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007850 break;
7851 case glslang::EOpRayQueryGetIntersectionWorldToObject:
Torosdagli06c2eee2020-03-19 11:09:57 -04007852 typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3);
Torosdagli06c2eee2020-03-19 11:09:57 -04007853 opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR;
Neslisah Torosdaglicea93842020-03-25 08:13:32 -04007854 break;
Chao Chen3c366992018-09-19 11:41:59 -07007855 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
7856 builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands);
7857 return 0;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06007858 case glslang::EOpCooperativeMatrixMulAdd:
7859 opCode = spv::OpCooperativeMatrixMulAddNV;
7860 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06007861#endif // GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06007862 default:
7863 return 0;
7864 }
7865
7866 spv::Id id = 0;
John Kessenich2359bd02015-12-06 19:29:11 -07007867 if (libCall >= 0) {
David Neto8d63a3d2015-12-07 16:17:06 -05007868 // Use an extended instruction from the standard library.
7869 // Construct the call arguments, without modifying the original operands vector.
7870 // We might need the remaining arguments, e.g. in the EOpFrexp case.
7871 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
Rex Xu9d93a232016-05-05 12:30:44 +08007872 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
t.jungb16bea82018-11-15 10:21:36 +01007873 } else if (opCode == spv::OpDot && !isFloat) {
7874 // int dot(int, int)
7875 // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached
7876 const int componentCount = builder.getNumComponents(operands[0]);
7877 spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]);
7878 builder.setPrecision(mulOp, precision);
7879 id = builder.createCompositeExtract(mulOp, typeId, 0);
7880 for (int i = 1; i < componentCount; ++i) {
7881 builder.setPrecision(id, precision);
John Kessenich5de15a22019-12-26 10:56:54 -07007882 id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i));
t.jungb16bea82018-11-15 10:21:36 +01007883 }
John Kessenich2359bd02015-12-06 19:29:11 -07007884 } else {
John Kessenich55e7d112015-11-15 21:33:39 -07007885 switch (consumedOperands) {
John Kessenich140f3df2015-06-26 16:58:36 -06007886 case 0:
7887 // should all be handled by visitAggregate and createNoArgOperation
7888 assert(0);
7889 return 0;
7890 case 1:
7891 // should all be handled by createUnaryOperation
7892 assert(0);
7893 return 0;
7894 case 2:
7895 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
7896 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007897 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007898 // anything 3 or over doesn't have l-value operands, so all should be consumed
7899 assert(consumedOperands == operands.size());
7900 id = builder.createOp(opCode, typeId, operands);
John Kessenich140f3df2015-06-26 16:58:36 -06007901 break;
7902 }
7903 }
7904
John Kessenichb9197c82019-08-11 07:41:45 -06007905#ifndef GLSLANG_WEB
John Kessenich55e7d112015-11-15 21:33:39 -07007906 // Decode the return types that were structures
7907 switch (op) {
7908 case glslang::EOpAddCarry:
7909 case glslang::EOpSubBorrow:
7910 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7911 id = builder.createCompositeExtract(id, typeId0, 0);
7912 break;
7913 case glslang::EOpUMulExtended:
7914 case glslang::EOpIMulExtended:
7915 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
7916 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7917 break;
7918 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007919 {
7920 assert(operands.size() == 2);
7921 if (builder.isFloatType(builder.getScalarTypeId(typeId1))) {
7922 // "exp" is floating-point type (from HLSL intrinsic)
7923 spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1);
7924 member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1);
7925 builder.createStore(member1, operands[1]);
7926 } else
7927 // "exp" is integer type (from GLSL built-in function)
7928 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
7929 id = builder.createCompositeExtract(id, typeId0, 0);
7930 }
John Kessenich55e7d112015-11-15 21:33:39 -07007931 break;
7932 default:
7933 break;
7934 }
John Kessenichb9197c82019-08-11 07:41:45 -06007935#endif
John Kessenich55e7d112015-11-15 21:33:39 -07007936
John Kessenich32cfd492016-02-02 12:37:46 -07007937 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06007938}
7939
Rex Xu9d93a232016-05-05 12:30:44 +08007940// Intrinsics with no arguments (or no return value, and no precision).
7941spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
John Kessenich140f3df2015-06-26 16:58:36 -06007942{
Jeff Bolz36831c92018-09-05 10:11:41 -05007943 // GLSL memory barriers use queuefamily scope in new model, device scope in old model
John Kessenich8985fc92020-03-03 10:25:07 -07007944 spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ?
7945 spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
John Kessenich140f3df2015-06-26 16:58:36 -06007946
7947 switch (op) {
John Kessenich140f3df2015-06-26 16:58:36 -06007948 case glslang::EOpBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007949 if (glslangIntermediate->getStage() == EShLangTessControl) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007950 if (glslangIntermediate->usingVulkanMemoryModel()) {
7951 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7952 spv::MemorySemanticsOutputMemoryKHRMask |
7953 spv::MemorySemanticsAcquireReleaseMask);
7954 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7955 } else {
7956 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone);
7957 }
John Kessenich82979362017-12-11 04:02:24 -07007958 } else {
7959 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7960 spv::MemorySemanticsWorkgroupMemoryMask |
7961 spv::MemorySemanticsAcquireReleaseMask);
7962 }
John Kessenich140f3df2015-06-26 16:58:36 -06007963 return 0;
7964 case glslang::EOpMemoryBarrier:
Jeff Bolz36831c92018-09-05 10:11:41 -05007965 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory |
7966 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007967 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007968 case glslang::EOpMemoryBarrierBuffer:
Jeff Bolz36831c92018-09-05 10:11:41 -05007969 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask |
7970 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007971 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007972 case glslang::EOpMemoryBarrierShared:
Jeff Bolz36831c92018-09-05 10:11:41 -05007973 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask |
7974 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007975 return 0;
7976 case glslang::EOpGroupMemoryBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007977 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory |
7978 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007979 return 0;
John Kessenich3dd1ce52019-10-17 07:08:40 -06007980#ifndef GLSLANG_WEB
7981 case glslang::EOpMemoryBarrierAtomicCounter:
7982 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask |
7983 spv::MemorySemanticsAcquireReleaseMask);
7984 return 0;
7985 case glslang::EOpMemoryBarrierImage:
7986 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask |
7987 spv::MemorySemanticsAcquireReleaseMask);
7988 return 0;
LoopDawg6e72fdd2016-06-15 09:50:24 -06007989 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07007990 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice,
John Kessenich82979362017-12-11 04:02:24 -07007991 spv::MemorySemanticsAllMemory |
John Kessenich838d7af2017-12-12 22:50:53 -07007992 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007993 return 0;
John Kessenich838d7af2017-12-12 22:50:53 -07007994 case glslang::EOpDeviceMemoryBarrier:
7995 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7996 spv::MemorySemanticsImageMemoryMask |
7997 spv::MemorySemanticsAcquireReleaseMask);
7998 return 0;
7999 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
8000 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
8001 spv::MemorySemanticsImageMemoryMask |
8002 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008003 return 0;
8004 case glslang::EOpWorkgroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07008005 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask |
8006 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008007 return 0;
8008 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07008009 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
8010 spv::MemorySemanticsWorkgroupMemoryMask |
8011 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06008012 return 0;
John Kessenich66011cb2018-03-06 16:12:04 -07008013 case glslang::EOpSubgroupBarrier:
8014 builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
8015 spv::MemorySemanticsAcquireReleaseMask);
8016 return spv::NoResult;
8017 case glslang::EOpSubgroupMemoryBarrier:
8018 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
8019 spv::MemorySemanticsAcquireReleaseMask);
8020 return spv::NoResult;
8021 case glslang::EOpSubgroupMemoryBarrierBuffer:
8022 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask |
8023 spv::MemorySemanticsAcquireReleaseMask);
8024 return spv::NoResult;
8025 case glslang::EOpSubgroupMemoryBarrierImage:
8026 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask |
8027 spv::MemorySemanticsAcquireReleaseMask);
8028 return spv::NoResult;
8029 case glslang::EOpSubgroupMemoryBarrierShared:
8030 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask |
8031 spv::MemorySemanticsAcquireReleaseMask);
8032 return spv::NoResult;
John Kessenichf8d1d742019-10-21 06:55:11 -06008033
8034 case glslang::EOpEmitVertex:
8035 builder.createNoResultOp(spv::OpEmitVertex);
8036 return 0;
8037 case glslang::EOpEndPrimitive:
8038 builder.createNoResultOp(spv::OpEndPrimitive);
8039 return 0;
8040
John Kessenich66011cb2018-03-06 16:12:04 -07008041 case glslang::EOpSubgroupElect: {
8042 std::vector<spv::Id> operands;
8043 return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid);
8044 }
Rex Xu9d93a232016-05-05 12:30:44 +08008045 case glslang::EOpTime:
8046 {
8047 std::vector<spv::Id> args; // Dummy arguments
8048 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
8049 return builder.setPrecision(id, precision);
8050 }
Daniel Kochdb32b242020-03-17 20:42:47 -04008051 case glslang::EOpIgnoreIntersection:
8052 builder.createNoResultOp(spv::OpIgnoreIntersectionKHR);
Chao Chenb50c02e2018-09-19 11:42:24 -07008053 return 0;
Daniel Kochdb32b242020-03-17 20:42:47 -04008054 case glslang::EOpTerminateRay:
8055 builder.createNoResultOp(spv::OpTerminateRayKHR);
Chao Chenb50c02e2018-09-19 11:42:24 -07008056 return 0;
Torosdagli06c2eee2020-03-19 11:09:57 -04008057 case glslang::EOpRayQueryInitialize:
8058 builder.createNoResultOp(spv::OpRayQueryInitializeKHR);
8059 return 0;
8060 case glslang::EOpRayQueryTerminate:
8061 builder.createNoResultOp(spv::OpRayQueryTerminateKHR);
8062 return 0;
8063 case glslang::EOpRayQueryGenerateIntersection:
8064 builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR);
8065 return 0;
8066 case glslang::EOpRayQueryConfirmIntersection:
8067 builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR);
8068 return 0;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05008069 case glslang::EOpBeginInvocationInterlock:
8070 builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT);
8071 return 0;
8072 case glslang::EOpEndInvocationInterlock:
8073 builder.createNoResultOp(spv::OpEndInvocationInterlockEXT);
8074 return 0;
8075
Jeff Bolzba6170b2019-07-01 09:23:23 -05008076 case glslang::EOpIsHelperInvocation:
8077 {
8078 std::vector<spv::Id> args; // Dummy arguments
Rex Xubb7307b2019-07-15 14:57:20 +08008079 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
8080 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
8081 return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args);
Jeff Bolzba6170b2019-07-01 09:23:23 -05008082 }
8083
amhagan91fb0092019-07-10 21:14:38 -04008084 case glslang::EOpReadClockSubgroupKHR: {
8085 std::vector<spv::Id> args;
8086 args.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
8087 builder.addExtension(spv::E_SPV_KHR_shader_clock);
8088 builder.addCapability(spv::CapabilityShaderClockKHR);
8089 return builder.createOp(spv::OpReadClockKHR, typeId, args);
8090 }
8091
8092 case glslang::EOpReadClockDeviceKHR: {
8093 std::vector<spv::Id> args;
8094 args.push_back(builder.makeUintConstant(spv::ScopeDevice));
8095 builder.addExtension(spv::E_SPV_KHR_shader_clock);
8096 builder.addCapability(spv::CapabilityShaderClockKHR);
8097 return builder.createOp(spv::OpReadClockKHR, typeId, args);
8098 }
John Kessenich3dd1ce52019-10-17 07:08:40 -06008099#endif
John Kessenich140f3df2015-06-26 16:58:36 -06008100 default:
John Kessenich155d3512019-08-08 23:29:20 -06008101 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008102 }
John Kessenich155d3512019-08-08 23:29:20 -06008103
8104 logger->missingFunctionality("unknown operation with no arguments");
8105
8106 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06008107}
8108
8109spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
8110{
John Kessenich2f273362015-07-18 22:34:27 -06008111 auto iter = symbolValues.find(symbol->getId());
John Kessenich140f3df2015-06-26 16:58:36 -06008112 spv::Id id;
8113 if (symbolValues.end() != iter) {
8114 id = iter->second;
8115 return id;
8116 }
8117
8118 // it was not found, create it
John Kessenich9c14f772019-06-17 08:38:35 -06008119 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
Daniel Kochdb32b242020-03-17 20:42:47 -04008120 auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType());
John Kessenich9c14f772019-06-17 08:38:35 -06008121 id = createSpvVariable(symbol, forcedType.first);
John Kessenich140f3df2015-06-26 16:58:36 -06008122 symbolValues[symbol->getId()] = id;
John Kessenich9c14f772019-06-17 08:38:35 -06008123 if (forcedType.second != spv::NoType)
8124 forceType[id] = forcedType.second;
John Kessenich140f3df2015-06-26 16:58:36 -06008125
Rex Xuc884b4a2016-06-29 15:03:44 +08008126 if (symbol->getBasicType() != glslang::EbtBlock) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008127 builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
8128 builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
8129 builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
John Kessenicha28f7a72019-08-06 07:00:58 -06008130#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07008131 addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier());
John Kessenichb9197c82019-08-11 07:41:45 -06008132 if (symbol->getQualifier().hasComponent())
8133 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
8134 if (symbol->getQualifier().hasIndex())
8135 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
Chao Chen3c366992018-09-19 11:41:59 -07008136#endif
John Kessenich6c292d32016-02-15 20:58:50 -07008137 if (symbol->getType().getQualifier().hasSpecConstantId())
John Kessenich5d610ee2018-03-07 18:05:55 -07008138 builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
John Kessenich91e4aa52016-07-07 17:46:42 -06008139 // atomic counters use this:
8140 if (symbol->getQualifier().hasOffset())
8141 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06008142 }
8143
scygan2c864272016-05-18 18:09:17 +02008144 if (symbol->getQualifier().hasLocation())
8145 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
John Kessenich5d610ee2018-03-07 18:05:55 -07008146 builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
John Kessenichf2d8a5c2016-03-03 22:29:11 -07008147 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
John Kessenich92187592016-02-01 13:45:25 -07008148 builder.addCapability(spv::CapabilityGeometryStreams);
John Kessenich140f3df2015-06-26 16:58:36 -06008149 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
John Kessenich92187592016-02-01 13:45:25 -07008150 }
John Kessenich140f3df2015-06-26 16:58:36 -06008151 if (symbol->getQualifier().hasSet())
8152 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
John Kessenich6c292d32016-02-15 20:58:50 -07008153 else if (IsDescriptorResource(symbol->getType())) {
8154 // default to 0
8155 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
8156 }
John Kessenich140f3df2015-06-26 16:58:36 -06008157 if (symbol->getQualifier().hasBinding())
8158 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
Jeff Bolz0a93cfb2018-12-11 20:53:59 -06008159 else if (IsDescriptorResource(symbol->getType())) {
8160 // default to 0
8161 builder.addDecoration(id, spv::DecorationBinding, 0);
8162 }
John Kessenich6c292d32016-02-15 20:58:50 -07008163 if (symbol->getQualifier().hasAttachment())
8164 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
John Kessenich140f3df2015-06-26 16:58:36 -06008165 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07008166 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenichedaf5562017-12-15 06:21:46 -07008167 if (symbol->getQualifier().hasXfbBuffer()) {
John Kessenich140f3df2015-06-26 16:58:36 -06008168 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
John Kessenichedaf5562017-12-15 06:21:46 -07008169 unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer);
8170 if (stride != glslang::TQualifier::layoutXfbStrideEnd)
8171 builder.addDecoration(id, spv::DecorationXfbStride, stride);
8172 }
8173 if (symbol->getQualifier().hasXfbOffset())
8174 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06008175 }
8176
John Kessenichb9197c82019-08-11 07:41:45 -06008177 // add built-in variable decoration
8178 if (builtIn != spv::BuiltInMax) {
8179 builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
8180 }
8181
8182#ifndef GLSLANG_WEB
Rex Xu1da878f2016-02-21 20:59:01 +08008183 if (symbol->getType().isImage()) {
8184 std::vector<spv::Decoration> memory;
John Kessenich8985fc92020-03-03 10:25:07 -07008185 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory,
8186 glslangIntermediate->usingVulkanMemoryModel());
Rex Xu1da878f2016-02-21 20:59:01 +08008187 for (unsigned int i = 0; i < memory.size(); ++i)
John Kessenich5d610ee2018-03-07 18:05:55 -07008188 builder.addDecoration(id, memory[i]);
Rex Xu1da878f2016-02-21 20:59:01 +08008189 }
8190
John Kessenich5611c6d2018-04-05 11:25:02 -06008191 // nonuniform
8192 builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier()));
8193
chaoc0ad6a4e2016-12-19 16:29:34 -08008194 if (builtIn == spv::BuiltInSampleMask) {
8195 spv::Decoration decoration;
8196 // GL_NV_sample_mask_override_coverage extension
8197 if (glslangIntermediate->getLayoutOverrideCoverage())
chaoc771d89f2017-01-13 01:10:53 -08008198 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
chaoc0ad6a4e2016-12-19 16:29:34 -08008199 else
8200 decoration = (spv::Decoration)spv::DecorationMax;
John Kessenich5d610ee2018-03-07 18:05:55 -07008201 builder.addDecoration(id, decoration);
chaoc0ad6a4e2016-12-19 16:29:34 -08008202 if (decoration != spv::DecorationMax) {
Jason Macnakdbd4c3c2019-07-12 14:33:02 -07008203 builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV);
chaoc0ad6a4e2016-12-19 16:29:34 -08008204 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
8205 }
8206 }
chaoc771d89f2017-01-13 01:10:53 -08008207 else if (builtIn == spv::BuiltInLayer) {
8208 // SPV_NV_viewport_array2 extension
John Kessenichb41bff62017-08-11 13:07:17 -06008209 if (symbol->getQualifier().layoutViewportRelative) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008210 builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
chaoc771d89f2017-01-13 01:10:53 -08008211 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
8212 builder.addExtension(spv::E_SPV_NV_viewport_array2);
8213 }
John Kessenichb41bff62017-08-11 13:07:17 -06008214 if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008215 builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
8216 symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
chaoc771d89f2017-01-13 01:10:53 -08008217 builder.addCapability(spv::CapabilityShaderStereoViewNV);
8218 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
8219 }
8220 }
8221
chaoc6e5acae2016-12-20 13:28:52 -08008222 if (symbol->getQualifier().layoutPassthrough) {
John Kessenich5d610ee2018-03-07 18:05:55 -07008223 builder.addDecoration(id, spv::DecorationPassthroughNV);
chaoc771d89f2017-01-13 01:10:53 -08008224 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
chaoc6e5acae2016-12-20 13:28:52 -08008225 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
8226 }
Chao Chen9eada4b2018-09-19 11:39:56 -07008227 if (symbol->getQualifier().pervertexNV) {
8228 builder.addDecoration(id, spv::DecorationPerVertexNV);
8229 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
8230 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
8231 }
chaoc0ad6a4e2016-12-19 16:29:34 -08008232
John Kessenich5d610ee2018-03-07 18:05:55 -07008233 if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) {
8234 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
8235 builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
8236 symbol->getType().getQualifier().semanticName);
8237 }
8238
John Kessenich7015bd62019-08-01 03:28:08 -06008239 if (symbol->isReference()) {
John Kessenich8985fc92020-03-03 10:25:07 -07008240 builder.addDecoration(id, symbol->getType().getQualifier().restrict ?
8241 spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008242 }
John Kessenichb9197c82019-08-11 07:41:45 -06008243#endif
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008244
John Kessenich140f3df2015-06-26 16:58:36 -06008245 return id;
8246}
8247
John Kessenicha28f7a72019-08-06 07:00:58 -06008248#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07008249// add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object
8250void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
8251{
8252 if (member >= 0) {
Sahil Parmar38772c02018-10-25 23:50:59 -07008253 if (qualifier.perPrimitiveNV) {
8254 // Need to add capability/extension for fragment shader.
8255 // Mesh shader already adds this by default.
8256 if (glslangIntermediate->getStage() == EShLangFragment) {
8257 builder.addCapability(spv::CapabilityMeshShadingNV);
8258 builder.addExtension(spv::E_SPV_NV_mesh_shader);
8259 }
Chao Chen3c366992018-09-19 11:41:59 -07008260 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07008261 }
Chao Chen3c366992018-09-19 11:41:59 -07008262 if (qualifier.perViewNV)
8263 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV);
8264 if (qualifier.perTaskNV)
8265 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV);
8266 } else {
Sahil Parmar38772c02018-10-25 23:50:59 -07008267 if (qualifier.perPrimitiveNV) {
8268 // Need to add capability/extension for fragment shader.
8269 // Mesh shader already adds this by default.
8270 if (glslangIntermediate->getStage() == EShLangFragment) {
8271 builder.addCapability(spv::CapabilityMeshShadingNV);
8272 builder.addExtension(spv::E_SPV_NV_mesh_shader);
8273 }
Chao Chen3c366992018-09-19 11:41:59 -07008274 builder.addDecoration(id, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07008275 }
Chao Chen3c366992018-09-19 11:41:59 -07008276 if (qualifier.perViewNV)
8277 builder.addDecoration(id, spv::DecorationPerViewNV);
8278 if (qualifier.perTaskNV)
8279 builder.addDecoration(id, spv::DecorationPerTaskNV);
8280 }
8281}
8282#endif
8283
John Kessenich55e7d112015-11-15 21:33:39 -07008284// Make a full tree of instructions to build a SPIR-V specialization constant,
John Kessenich6c292d32016-02-15 20:58:50 -07008285// or regular constant if possible.
John Kessenich55e7d112015-11-15 21:33:39 -07008286//
8287// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
8288//
8289// Recursively walk the nodes. The nodes form a tree whose leaves are
8290// regular constants, which themselves are trees that createSpvConstant()
8291// recursively walks. So, this function walks the "top" of the tree:
8292// - emit specialization constant-building instructions for specConstant
8293// - when running into a non-spec-constant, switch to createSpvConstant()
qining08408382016-03-21 09:51:37 -04008294spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
John Kessenich55e7d112015-11-15 21:33:39 -07008295{
John Kessenich7cc0e282016-03-20 00:46:02 -06008296 assert(node.getQualifier().isConstant());
John Kessenich55e7d112015-11-15 21:33:39 -07008297
qining4f4bb812016-04-03 23:55:17 -04008298 // Handle front-end constants first (non-specialization constants).
John Kessenich6c292d32016-02-15 20:58:50 -07008299 if (! node.getQualifier().specConstant) {
8300 // hand off to the non-spec-constant path
8301 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
8302 int nextConst = 0;
John Kessenich8985fc92020-03-03 10:25:07 -07008303 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ?
8304 node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
8305 nextConst, false);
John Kessenich6c292d32016-02-15 20:58:50 -07008306 }
8307
8308 // We now know we have a specialization constant to build
8309
Ricardo Garcia232ba0d2020-06-03 15:52:55 +02008310 // Extra capabilities may be needed.
8311 if (node.getType().contains8BitInt())
8312 builder.addCapability(spv::CapabilityInt8);
8313 if (node.getType().contains16BitFloat())
8314 builder.addCapability(spv::CapabilityFloat16);
8315 if (node.getType().contains16BitInt())
8316 builder.addCapability(spv::CapabilityInt16);
8317 if (node.getType().contains64BitInt())
8318 builder.addCapability(spv::CapabilityInt64);
8319 if (node.getType().containsDouble())
8320 builder.addCapability(spv::CapabilityFloat64);
8321
John Kessenichd94c0032016-05-30 19:29:40 -06008322 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
qining4f4bb812016-04-03 23:55:17 -04008323 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
8324 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
8325 std::vector<spv::Id> dimConstId;
8326 for (int dim = 0; dim < 3; ++dim) {
8327 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
8328 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
John Kessenich5d610ee2018-03-07 18:05:55 -07008329 if (specConst) {
8330 builder.addDecoration(dimConstId.back(), spv::DecorationSpecId,
8331 glslangIntermediate->getLocalSizeSpecId(dim));
8332 }
qining4f4bb812016-04-03 23:55:17 -04008333 }
8334 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
8335 }
8336
8337 // An AST node labelled as specialization constant should be a symbol node.
8338 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
8339 if (auto* sn = node.getAsSymbolNode()) {
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008340 spv::Id result;
qining4f4bb812016-04-03 23:55:17 -04008341 if (auto* sub_tree = sn->getConstSubtree()) {
qining27e04a02016-04-14 16:40:20 -04008342 // Traverse the constant constructor sub tree like generating normal run-time instructions.
8343 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
8344 // will set the builder into spec constant op instruction generating mode.
8345 sub_tree->traverse(this);
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008346 result = accessChainLoad(sub_tree->getType());
8347 } else if (auto* const_union_array = &sn->getConstArray()) {
qining4f4bb812016-04-03 23:55:17 -04008348 int nextConst = 0;
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008349 result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
Dan Sinclair70661b92018-11-12 13:56:52 -05008350 } else {
8351 logger->missingFunctionality("Invalid initializer for spec onstant.");
Dan Sinclair70661b92018-11-12 13:56:52 -05008352 return spv::NoResult;
John Kessenich6c292d32016-02-15 20:58:50 -07008353 }
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07008354 builder.addName(result, sn->getName().c_str());
8355 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07008356 }
qining4f4bb812016-04-03 23:55:17 -04008357
8358 // Neither a front-end constant node, nor a specialization constant node with constant union array or
8359 // constant sub tree as initializer.
Lei Zhang17535f72016-05-04 15:55:59 -04008360 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
qining4f4bb812016-04-03 23:55:17 -04008361 return spv::NoResult;
John Kessenich55e7d112015-11-15 21:33:39 -07008362}
8363
John Kessenich140f3df2015-06-26 16:58:36 -06008364// Use 'consts' as the flattened glslang source of scalar constants to recursively
8365// build the aggregate SPIR-V constant.
8366//
8367// If there are not enough elements present in 'consts', 0 will be substituted;
8368// an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
8369//
John Kessenich8985fc92020-03-03 10:25:07 -07008370spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType,
8371 const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
John Kessenich140f3df2015-06-26 16:58:36 -06008372{
8373 // vector of constants for SPIR-V
8374 std::vector<spv::Id> spvConsts;
8375
8376 // Type is used for struct and array constants
8377 spv::Id typeId = convertGlslangToSpvType(glslangType);
8378
8379 if (glslangType.isArray()) {
John Kessenich65c78a02015-08-10 17:08:55 -06008380 glslang::TType elementType(glslangType, 0);
8381 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
qining08408382016-03-21 09:51:37 -04008382 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06008383 } else if (glslangType.isMatrix()) {
John Kessenich65c78a02015-08-10 17:08:55 -06008384 glslang::TType vectorType(glslangType, 0);
John Kessenich140f3df2015-06-26 16:58:36 -06008385 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
qining08408382016-03-21 09:51:37 -04008386 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
Jeff Bolz4605e2e2019-02-19 13:10:32 -06008387 } else if (glslangType.isCoopMat()) {
8388 glslang::TType componentType(glslangType.getBasicType());
8389 spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06008390 } else if (glslangType.isStruct()) {
John Kessenich140f3df2015-06-26 16:58:36 -06008391 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
8392 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
qining08408382016-03-21 09:51:37 -04008393 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
John Kessenich8d72f1a2016-05-20 12:06:03 -06008394 } else if (glslangType.getVectorSize() > 1) {
John Kessenich140f3df2015-06-26 16:58:36 -06008395 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
8396 bool zero = nextConst >= consts.size();
8397 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06008398 case glslang::EbtInt:
8399 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
8400 break;
8401 case glslang::EbtUint:
8402 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
8403 break;
8404 case glslang::EbtFloat:
8405 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
8406 break;
8407 case glslang::EbtBool:
8408 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
8409 break;
8410#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07008411 case glslang::EbtInt8:
8412 spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const()));
8413 break;
8414 case glslang::EbtUint8:
8415 spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const()));
8416 break;
8417 case glslang::EbtInt16:
8418 spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const()));
8419 break;
8420 case glslang::EbtUint16:
8421 spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const()));
8422 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08008423 case glslang::EbtInt64:
8424 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
8425 break;
8426 case glslang::EbtUint64:
8427 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
8428 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008429 case glslang::EbtDouble:
8430 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
8431 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08008432 case glslang::EbtFloat16:
8433 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
8434 break;
John Kessenich39697cd2019-08-08 10:35:51 -06008435#endif
John Kessenich140f3df2015-06-26 16:58:36 -06008436 default:
John Kessenich55e7d112015-11-15 21:33:39 -07008437 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06008438 break;
8439 }
8440 ++nextConst;
8441 }
8442 } else {
8443 // we have a non-aggregate (scalar) constant
8444 bool zero = nextConst >= consts.size();
8445 spv::Id scalar = 0;
8446 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06008447 case glslang::EbtInt:
8448 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
8449 break;
8450 case glslang::EbtUint:
8451 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
8452 break;
8453 case glslang::EbtFloat:
8454 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
8455 break;
8456 case glslang::EbtBool:
8457 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
8458 break;
8459#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07008460 case glslang::EbtInt8:
8461 scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant);
8462 break;
8463 case glslang::EbtUint8:
8464 scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant);
8465 break;
8466 case glslang::EbtInt16:
8467 scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant);
8468 break;
8469 case glslang::EbtUint16:
8470 scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant);
8471 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08008472 case glslang::EbtInt64:
8473 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
8474 break;
8475 case glslang::EbtUint64:
8476 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
8477 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008478 case glslang::EbtDouble:
John Kessenich55e7d112015-11-15 21:33:39 -07008479 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06008480 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08008481 case glslang::EbtFloat16:
8482 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
8483 break;
Jeff Bolz3fd12322019-03-05 23:27:09 -06008484 case glslang::EbtReference:
8485 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
8486 scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar);
8487 break;
John Kessenich39697cd2019-08-08 10:35:51 -06008488#endif
Jeff Bolz04d73732019-05-31 13:06:01 -05008489 case glslang::EbtString:
8490 scalar = builder.getStringId(consts[nextConst].getSConst()->c_str());
8491 break;
John Kessenich140f3df2015-06-26 16:58:36 -06008492 default:
John Kessenich55e7d112015-11-15 21:33:39 -07008493 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06008494 break;
8495 }
8496 ++nextConst;
8497 return scalar;
8498 }
8499
8500 return builder.makeCompositeConstant(typeId, spvConsts);
8501}
8502
John Kessenich7c1aa102015-10-15 13:29:11 -06008503// Return true if the node is a constant or symbol whose reading has no
8504// non-trivial observable cost or effect.
8505bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
8506{
8507 // don't know what this is
8508 if (node == nullptr)
8509 return false;
8510
8511 // a constant is safe
8512 if (node->getAsConstantUnion() != nullptr)
8513 return true;
8514
8515 // not a symbol means non-trivial
8516 if (node->getAsSymbolNode() == nullptr)
8517 return false;
8518
8519 // a symbol, depends on what's being read
8520 switch (node->getType().getQualifier().storage) {
8521 case glslang::EvqTemporary:
8522 case glslang::EvqGlobal:
8523 case glslang::EvqIn:
8524 case glslang::EvqInOut:
8525 case glslang::EvqConst:
8526 case glslang::EvqConstReadOnly:
8527 case glslang::EvqUniform:
8528 return true;
8529 default:
8530 return false;
8531 }
qining25262b32016-05-06 17:25:16 -04008532}
John Kessenich7c1aa102015-10-15 13:29:11 -06008533
8534// A node is trivial if it is a single operation with no side effects.
John Kessenich84cc15f2017-05-24 16:44:47 -06008535// HLSL (and/or vectors) are always trivial, as it does not short circuit.
John Kessenich0d2b4712017-05-19 20:19:00 -06008536// Otherwise, error on the side of saying non-trivial.
John Kessenich7c1aa102015-10-15 13:29:11 -06008537// Return true if trivial.
8538bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
8539{
8540 if (node == nullptr)
8541 return false;
8542
John Kessenich84cc15f2017-05-24 16:44:47 -06008543 // count non scalars as trivial, as well as anything coming from HLSL
8544 if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich0d2b4712017-05-19 20:19:00 -06008545 return true;
8546
John Kessenich7c1aa102015-10-15 13:29:11 -06008547 // symbols and constants are trivial
8548 if (isTrivialLeaf(node))
8549 return true;
8550
8551 // otherwise, it needs to be a simple operation or one or two leaf nodes
8552
8553 // not a simple operation
8554 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
8555 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
8556 if (binaryNode == nullptr && unaryNode == nullptr)
8557 return false;
8558
8559 // not on leaf nodes
8560 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
8561 return false;
8562
8563 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
8564 return false;
8565 }
8566
8567 switch (node->getAsOperator()->getOp()) {
8568 case glslang::EOpLogicalNot:
8569 case glslang::EOpConvIntToBool:
8570 case glslang::EOpConvUintToBool:
8571 case glslang::EOpConvFloatToBool:
8572 case glslang::EOpConvDoubleToBool:
8573 case glslang::EOpEqual:
8574 case glslang::EOpNotEqual:
8575 case glslang::EOpLessThan:
8576 case glslang::EOpGreaterThan:
8577 case glslang::EOpLessThanEqual:
8578 case glslang::EOpGreaterThanEqual:
8579 case glslang::EOpIndexDirect:
8580 case glslang::EOpIndexDirectStruct:
8581 case glslang::EOpLogicalXor:
8582 case glslang::EOpAny:
8583 case glslang::EOpAll:
8584 return true;
8585 default:
8586 return false;
8587 }
8588}
8589
8590// Emit short-circuiting code, where 'right' is never evaluated unless
8591// the left side is true (for &&) or false (for ||).
John Kessenich8985fc92020-03-03 10:25:07 -07008592spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left,
8593 glslang::TIntermTyped& right)
John Kessenich7c1aa102015-10-15 13:29:11 -06008594{
8595 spv::Id boolTypeId = builder.makeBoolType();
8596
8597 // emit left operand
8598 builder.clearAccessChain();
8599 left.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008600 spv::Id leftId = accessChainLoad(left.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008601
8602 // Operands to accumulate OpPhi operands
8603 std::vector<spv::Id> phiOperands;
8604 // accumulate left operand's phi information
8605 phiOperands.push_back(leftId);
8606 phiOperands.push_back(builder.getBuildPoint()->getId());
8607
8608 // Make the two kinds of operation symmetric with a "!"
8609 // || => emit "if (! left) result = right"
8610 // && => emit "if ( left) result = right"
8611 //
8612 // TODO: this runtime "not" for || could be avoided by adding functionality
8613 // to 'builder' to have an "else" without an "then"
8614 if (op == glslang::EOpLogicalOr)
8615 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
8616
8617 // make an "if" based on the left value
Rex Xu57e65922017-07-04 23:23:40 +08008618 spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder);
John Kessenich7c1aa102015-10-15 13:29:11 -06008619
8620 // emit right operand as the "then" part of the "if"
8621 builder.clearAccessChain();
8622 right.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008623 spv::Id rightId = accessChainLoad(right.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008624
8625 // accumulate left operand's phi information
8626 phiOperands.push_back(rightId);
8627 phiOperands.push_back(builder.getBuildPoint()->getId());
8628
8629 // finish the "if"
8630 ifBuilder.makeEndIf();
8631
8632 // phi together the two results
8633 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
8634}
8635
John Kessenicha28f7a72019-08-06 07:00:58 -06008636#ifndef GLSLANG_WEB
Rex Xu9d93a232016-05-05 12:30:44 +08008637// Return type Id of the imported set of extended instructions corresponds to the name.
8638// Import this set if it has not been imported yet.
8639spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
8640{
8641 if (extBuiltinMap.find(name) != extBuiltinMap.end())
8642 return extBuiltinMap[name];
8643 else {
Rex Xu51596642016-09-21 18:56:12 +08008644 builder.addExtension(name);
Rex Xu9d93a232016-05-05 12:30:44 +08008645 spv::Id extBuiltins = builder.import(name);
8646 extBuiltinMap[name] = extBuiltins;
8647 return extBuiltins;
8648 }
8649}
Frank Henigman541f7bb2018-01-16 00:18:26 -05008650#endif
Rex Xu9d93a232016-05-05 12:30:44 +08008651
John Kessenich140f3df2015-06-26 16:58:36 -06008652}; // end anonymous namespace
8653
8654namespace glslang {
8655
John Kessenich68d78fd2015-07-12 19:28:10 -06008656void GetSpirvVersion(std::string& version)
8657{
John Kessenich9e55f632015-07-15 10:03:39 -06008658 const int bufSize = 100;
John Kessenichf98ee232015-07-12 19:39:51 -06008659 char buf[bufSize];
John Kessenich55e7d112015-11-15 21:33:39 -07008660 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
John Kessenich68d78fd2015-07-12 19:28:10 -06008661 version = buf;
8662}
8663
John Kessenicha372a3e2017-11-02 22:32:14 -06008664// For low-order part of the generator's magic number. Bump up
8665// when there is a change in the style (e.g., if SSA form changes,
8666// or a different instruction sequence to do something gets used).
8667int GetSpirvGeneratorVersion()
8668{
John Kessenich3f0d4bc2017-12-16 23:46:37 -07008669 // return 1; // start
8670 // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V
John Kessenich71b5da62018-02-06 08:06:36 -07008671 // return 3; // change/correct barrier-instruction operands, to match memory model group decisions
John Kessenich0216f242018-03-03 11:47:07 -07008672 // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component
John Kessenichac370792018-03-07 11:24:50 -07008673 // return 5; // make OpArrayLength result type be an int with signedness of 0
John Kessenichd6c97552018-06-04 15:33:31 -06008674 // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code,
8675 // versions 4 and 6 each generate OpArrayLength as it has long been done
John Kessenich31c33702019-11-02 21:26:40 -06008676 // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent
John Kessenich3641ff72020-06-10 07:38:31 -06008677 // return 8; // switch to new dead block eliminator; use OpUnreachable
Graeme Leese060882f2020-06-22 11:03:46 +01008678 // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list
8679 return 10; // Generate OpFUnordNotEqual for != comparisons
John Kessenicha372a3e2017-11-02 22:32:14 -06008680}
8681
John Kessenich140f3df2015-06-26 16:58:36 -06008682// Write SPIR-V out to a binary file
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008683void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
John Kessenich140f3df2015-06-26 16:58:36 -06008684{
8685 std::ofstream out;
John Kessenich68d78fd2015-07-12 19:28:10 -06008686 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008687 if (out.fail())
8688 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenich140f3df2015-06-26 16:58:36 -06008689 for (int i = 0; i < (int)spirv.size(); ++i) {
8690 unsigned int word = spirv[i];
8691 out.write((const char*)&word, 4);
8692 }
8693 out.close();
8694}
8695
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008696// Write SPIR-V out to a text file with 32-bit hexadecimal words
Flavioaea3c892017-02-06 11:46:35 -08008697void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008698{
Shahbaz Youssefi1ef2e252020-07-03 15:42:53 -04008699#if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008700 std::ofstream out;
8701 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008702 if (out.fail())
8703 printf("ERROR: Failed to open file: %s\n", baseName);
Ben Claytonfbe9a232020-06-17 11:17:19 +01008704 out << "\t// " <<
8705 GetSpirvGeneratorVersion() <<
8706 GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH <<
8707 GLSLANG_VERSION_FLAVOR << std::endl;
Flavio15017db2017-02-15 14:29:33 -08008708 if (varName != nullptr) {
8709 out << "\t #pragma once" << std::endl;
8710 out << "const uint32_t " << varName << "[] = {" << std::endl;
8711 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008712 const int WORDS_PER_LINE = 8;
8713 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
8714 out << "\t";
8715 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
8716 const unsigned int word = spirv[i + j];
8717 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
8718 if (i + j + 1 < (int)spirv.size()) {
8719 out << ",";
8720 }
8721 }
8722 out << std::endl;
8723 }
Flavio15017db2017-02-15 14:29:33 -08008724 if (varName != nullptr) {
8725 out << "};";
johnkslangf881f082020-08-04 02:13:50 -06008726 out << std::endl;
Flavio15017db2017-02-15 14:29:33 -08008727 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008728 out.close();
John Kessenich155d3512019-08-08 23:29:20 -06008729#endif
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008730}
8731
John Kessenich140f3df2015-06-26 16:58:36 -06008732//
8733// Set up the glslang traversal
8734//
John Kessenich4e11b612018-08-30 16:56:59 -06008735void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
John Kessenich140f3df2015-06-26 16:58:36 -06008736{
Lei Zhang17535f72016-05-04 15:55:59 -04008737 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06008738 GlslangToSpv(intermediate, spirv, &logger, options);
Lei Zhang09caf122016-05-02 18:11:54 -04008739}
8740
John Kessenich4e11b612018-08-30 16:56:59 -06008741void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
John Kessenich121853f2017-05-31 17:11:16 -06008742 spv::SpvBuildLogger* logger, SpvOptions* options)
Lei Zhang09caf122016-05-02 18:11:54 -04008743{
John Kessenich140f3df2015-06-26 16:58:36 -06008744 TIntermNode* root = intermediate.getTreeRoot();
8745
8746 if (root == 0)
8747 return;
8748
John Kessenich4e11b612018-08-30 16:56:59 -06008749 SpvOptions defaultOptions;
John Kessenich121853f2017-05-31 17:11:16 -06008750 if (options == nullptr)
8751 options = &defaultOptions;
8752
John Kessenich4e11b612018-08-30 16:56:59 -06008753 GetThreadPoolAllocator().push();
John Kessenich140f3df2015-06-26 16:58:36 -06008754
John Kessenich2b5ea9f2018-01-31 18:35:56 -07008755 TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
John Kessenich140f3df2015-06-26 16:58:36 -06008756 root->traverse(&it);
John Kessenichfca82622016-11-26 13:23:20 -07008757 it.finishSpv();
John Kessenich140f3df2015-06-26 16:58:36 -06008758 it.dumpSpv(spirv);
8759
GregFfb03a552018-03-29 11:49:14 -06008760#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -06008761 // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
8762 // eg. forward and remove memory writes of opaque types.
Jeff Bolzfd556e32019-06-07 14:42:08 -05008763 bool prelegalization = intermediate.getSource() == EShSourceHlsl;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04008764 if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) {
8765 SpirvToolsTransform(intermediate, spirv, logger, options);
Jeff Bolzfd556e32019-06-07 14:42:08 -05008766 prelegalization = false;
8767 }
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04008768 else if (options->stripDebugInfo) {
8769 // Strip debug info even if optimization is disabled.
8770 SpirvToolsStripDebugInfo(intermediate, spirv, logger);
8771 }
John Kessenich717c80a2018-08-23 15:17:10 -06008772
John Kessenich4e11b612018-08-30 16:56:59 -06008773 if (options->validate)
Jeff Bolzfd556e32019-06-07 14:42:08 -05008774 SpirvToolsValidate(intermediate, spirv, logger, prelegalization);
John Kessenich4e11b612018-08-30 16:56:59 -06008775
John Kessenich717c80a2018-08-23 15:17:10 -06008776 if (options->disassemble)
John Kessenich4e11b612018-08-30 16:56:59 -06008777 SpirvToolsDisassemble(std::cout, spirv);
John Kessenich717c80a2018-08-23 15:17:10 -06008778
GregFcd1f1692017-09-21 18:40:22 -06008779#endif
8780
John Kessenich4e11b612018-08-30 16:56:59 -06008781 GetThreadPoolAllocator().pop();
John Kessenich140f3df2015-06-26 16:58:36 -06008782}
8783
8784}; // end namespace glslang