blob: c7cab841ffb8adc45d4e1b3c0d5d94cfc0b6e1ee [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 Kessenichb23d2322018-12-14 10:47:35 -07003// Copyright (C) 2015-2018 Google, Inc.
John Kessenich66011cb2018-03-06 16:12:04 -07004// Copyright (C) 2017 ARM Limited.
John Kessenich140f3df2015-06-26 16:58:36 -06005//
John Kessenich927608b2017-01-06 12:34:14 -07006// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06007//
John Kessenich927608b2017-01-06 12:34:14 -07008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
John Kessenich140f3df2015-06-26 16:58:36 -060011//
12// Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following
17// disclaimer in the documentation and/or other materials provided
18// with the distribution.
19//
20// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21// contributors may be used to endorse or promote products derived
22// from this software without specific prior written permission.
23//
John Kessenich927608b2017-01-06 12:34:14 -070024// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35// POSSIBILITY OF SUCH DAMAGE.
John Kessenich140f3df2015-06-26 16:58:36 -060036
37//
John Kessenich140f3df2015-06-26 16:58:36 -060038// Visit the nodes in the glslang intermediate tree representation to
39// translate them to SPIR-V.
40//
41
John Kessenich5e4b1242015-08-06 22:53:06 -060042#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060043#include "GlslangToSpv.h"
44#include "SpvBuilder.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060045namespace spv {
Rex Xu51596642016-09-21 18:56:12 +080046 #include "GLSL.std.450.h"
47 #include "GLSL.ext.KHR.h"
Piers Daniell1c5443c2017-12-13 13:07:22 -070048 #include "GLSL.ext.EXT.h"
Rex Xu9d93a232016-05-05 12:30:44 +080049#ifdef AMD_EXTENSIONS
Rex Xu51596642016-09-21 18:56:12 +080050 #include "GLSL.ext.AMD.h"
Rex Xu9d93a232016-05-05 12:30:44 +080051#endif
chaoc0ad6a4e2016-12-19 16:29:34 -080052#ifdef NV_EXTENSIONS
53 #include "GLSL.ext.NV.h"
54#endif
John Kessenich5e4b1242015-08-06 22:53:06 -060055}
John Kessenich140f3df2015-06-26 16:58:36 -060056
57// Glslang includes
baldurk42169c52015-07-08 15:11:59 +020058#include "../glslang/MachineIndependent/localintermediate.h"
59#include "../glslang/MachineIndependent/SymbolTable.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060060#include "../glslang/Include/Common.h"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050061#include "../glslang/Include/revision.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 {
94 spv::Decoration precision;
95 spv::Decoration noContraction;
John Kessenich5611c6d2018-04-05 11:25:02 -060096 spv::Decoration nonUniform;
John Kessenichead86222018-03-28 18:01:20 -060097};
98
99} // namespace
qining4c912612016-04-01 10:35:16 -0400100
John Kessenich140f3df2015-06-26 16:58:36 -0600101//
102// The main holder of information for translating glslang to SPIR-V.
103//
104// Derives from the AST walking base class.
105//
106class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
107public:
John Kessenich2b5ea9f2018-01-31 18:35:56 -0700108 TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger,
109 glslang::SpvOptions& options);
John Kessenichfca82622016-11-26 13:23:20 -0700110 virtual ~TGlslangToSpvTraverser() { }
John Kessenich140f3df2015-06-26 16:58:36 -0600111
112 bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*);
113 bool visitBinary(glslang::TVisit, glslang::TIntermBinary*);
114 void visitConstantUnion(glslang::TIntermConstantUnion*);
115 bool visitSelection(glslang::TVisit, glslang::TIntermSelection*);
116 bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*);
117 void visitSymbol(glslang::TIntermSymbol* symbol);
118 bool visitUnary(glslang::TVisit, glslang::TIntermUnary*);
119 bool visitLoop(glslang::TVisit, glslang::TIntermLoop*);
120 bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*);
121
John Kessenichfca82622016-11-26 13:23:20 -0700122 void finishSpv();
John Kessenich7ba63412015-12-20 17:37:07 -0700123 void dumpSpv(std::vector<unsigned int>& out);
John Kessenich140f3df2015-06-26 16:58:36 -0600124
125protected:
John Kessenich5d610ee2018-03-07 18:05:55 -0700126 TGlslangToSpvTraverser(TGlslangToSpvTraverser&);
127 TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&);
128
Rex Xu17ff3432016-10-14 17:41:45 +0800129 spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier);
Rex Xubbceed72016-05-21 09:40:44 +0800130 spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier);
John Kessenich5611c6d2018-04-05 11:25:02 -0600131 spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier);
Jeff Bolz36831c92018-09-05 10:11:41 -0500132 spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type);
133 spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
134 spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
135 spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
David Netoa901ffe2016-06-08 14:11:40 +0100136 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
John Kessenich5d0fa972016-02-15 11:57:00 -0700137 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
John Kesseniche18fd202018-01-30 11:01:39 -0700138 spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const;
139 spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const;
John Kessenicha2858d92018-01-31 08:11:18 -0700140 spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, unsigned int& dependencyLength) const;
John Kessenicha5c5fb62017-05-05 05:09:58 -0600141 spv::StorageClass TranslateStorageClass(const glslang::TType&);
John Kessenich5611c6d2018-04-05 11:25:02 -0600142 void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType);
John Kessenich140f3df2015-06-26 16:58:36 -0600143 spv::Id createSpvVariable(const glslang::TIntermSymbol*);
144 spv::Id getSampledType(const glslang::TSampler&);
John Kessenich8c8505c2016-07-26 12:50:38 -0600145 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
146 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
147 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600148 spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false);
John Kessenichead86222018-03-28 18:01:20 -0600149 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&,
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600150 bool lastBufferBlockMember, bool forwardReferenceOnly = false);
John Kessenich0e737842017-03-24 18:38:16 -0600151 bool filterMember(const glslang::TType& member);
John Kessenich6090df02016-06-30 21:18:02 -0600152 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
153 glslang::TLayoutPacking, const glslang::TQualifier&);
154 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
155 const glslang::TQualifier&, spv::Id);
John Kessenich6c292d32016-02-15 20:58:50 -0700156 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
John Kessenich32cfd492016-02-02 12:37:46 -0700157 spv::Id accessChainLoad(const glslang::TType& type);
Rex Xu27253232016-02-23 17:51:09 +0800158 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
John Kessenich4bf71552016-09-02 11:20:21 -0600159 void multiTypeStore(const glslang::TType&, spv::Id rValue);
John Kessenichf85e8062015-12-19 13:57:10 -0700160 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
John Kessenich3ac051e2015-12-20 11:29:16 -0700161 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
162 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
John Kessenich5d610ee2018-03-07 18:05:55 -0700163 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset,
164 int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
David Netoa901ffe2016-06-08 14:11:40 +0100165 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
John Kessenich140f3df2015-06-26 16:58:36 -0600166
John Kessenich6fccb3c2016-09-19 16:01:41 -0600167 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
John Kessenichd3ed90b2018-05-04 11:43:03 -0600168 bool writableParam(glslang::TStorageQualifier) const;
John Kessenichd41993d2017-09-10 15:21:05 -0600169 bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam);
John Kessenich140f3df2015-06-26 16:58:36 -0600170 void makeFunctions(const glslang::TIntermSequence&);
171 void makeGlobalInitializers(const glslang::TIntermSequence&);
172 void visitFunctions(const glslang::TIntermSequence&);
173 void handleFunctionEntry(const glslang::TIntermAggregate* node);
Rex Xu04db3f52015-09-16 11:44:02 +0800174 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments);
John Kessenichfc51d282015-08-19 13:34:18 -0600175 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
176 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600177 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
178
John Kessenichead86222018-03-28 18:01:20 -0600179 spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right,
180 glslang::TBasicType typeProxy, bool reduceComparison = true);
181 spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right);
182 spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand,
183 glslang::TBasicType typeProxy);
184 spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand,
185 glslang::TBasicType typeProxy);
186 spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand,
187 glslang::TBasicType typeProxy);
John Kessenichad7645f2018-06-04 19:11:25 -0600188 spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize);
John Kessenich140f3df2015-06-26 16:58:36 -0600189 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
Rex Xu04db3f52015-09-16 11:44:02 +0800190 spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu51596642016-09-21 18:56:12 +0800191 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu430ef402016-10-14 17:22:23 +0800192 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands);
John Kessenich66011cb2018-03-06 16:12:04 -0700193 spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -0600194 spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +0800195 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600196 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
Chao Chen3c366992018-09-19 11:41:59 -0700197#ifdef NV_EXTENSIONS
198 void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier);
199#endif
qining08408382016-03-21 09:51:37 -0400200 spv::Id createSpvConstant(const glslang::TIntermTyped&);
201 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant);
John Kessenich7c1aa102015-10-15 13:29:11 -0600202 bool isTrivialLeaf(const glslang::TIntermTyped* node);
203 bool isTrivial(const glslang::TIntermTyped* node);
204 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
Frank Henigman541f7bb2018-01-16 00:18:26 -0500205#ifdef AMD_EXTENSIONS
Rex Xu9d93a232016-05-05 12:30:44 +0800206 spv::Id getExtBuiltins(const char* name);
Frank Henigman541f7bb2018-01-16 00:18:26 -0500207#endif
John Kessenich66011cb2018-03-06 16:12:04 -0700208 void addPre13Extension(const char* ext)
209 {
210 if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3)
211 builder.addExtension(ext);
212 }
John Kessenich140f3df2015-06-26 16:58:36 -0600213
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600214 unsigned int getBufferReferenceAlignment(const glslang::TType &type) const {
215 if (type.getBasicType() == glslang::EbtReference) {
216 return type.getReferentType()->getQualifier().hasBufferReferenceAlign() ?
217 (1u << type.getReferentType()->getQualifier().layoutBufferReferenceAlign) : 16u;
218 } else {
219 return 0;
220 }
221 }
222
John Kessenich121853f2017-05-31 17:11:16 -0600223 glslang::SpvOptions& options;
John Kessenich140f3df2015-06-26 16:58:36 -0600224 spv::Function* shaderEntry;
John Kesseniched33e052016-10-06 12:59:51 -0600225 spv::Function* currentFunction;
John Kessenich55e7d112015-11-15 21:33:39 -0700226 spv::Instruction* entryPoint;
John Kessenich140f3df2015-06-26 16:58:36 -0600227 int sequenceDepth;
228
Lei Zhang17535f72016-05-04 15:55:59 -0400229 spv::SpvBuildLogger* logger;
Lei Zhang09caf122016-05-02 18:11:54 -0400230
John Kessenich140f3df2015-06-26 16:58:36 -0600231 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
232 spv::Builder builder;
John Kessenich517fe7a2016-11-26 13:31:47 -0700233 bool inEntryPoint;
234 bool entryPointTerminated;
John Kessenich7ba63412015-12-20 17:37:07 -0700235 bool linkageOnly; // true when visiting the set of objects in the AST present only for establishing interface, whether or not they were statically used
John Kessenich59420fd2015-12-21 11:45:34 -0700236 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
John Kessenich140f3df2015-06-26 16:58:36 -0600237 const glslang::TIntermediate* glslangIntermediate;
238 spv::Id stdBuiltins;
Rex Xu9d93a232016-05-05 12:30:44 +0800239 std::unordered_map<const char*, spv::Id> extBuiltinMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600240
John Kessenich2f273362015-07-18 22:34:27 -0600241 std::unordered_map<int, spv::Id> symbolValues;
John Kessenich4bf71552016-09-02 11:20:21 -0600242 std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer
John Kessenich2f273362015-07-18 22:34:27 -0600243 std::unordered_map<std::string, spv::Function*> functionMap;
John Kessenich3ac051e2015-12-20 11:29:16 -0700244 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
John Kessenich5d610ee2018-03-07 18:05:55 -0700245 // for mapping glslang block indices to spv indices (e.g., due to hidden members):
246 std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper;
John Kessenich140f3df2015-06-26 16:58:36 -0600247 std::stack<bool> breakForLoop; // false means break for switch
John Kessenich5d610ee2018-03-07 18:05:55 -0700248 std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator;
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600249 // Map pointee types for EbtReference to their forward pointers
250 std::map<const glslang::TType *, spv::Id> forwardPointers;
John Kessenich140f3df2015-06-26 16:58:36 -0600251};
252
253//
254// Helper functions for translating glslang representations to SPIR-V enumerants.
255//
256
257// Translate glslang profile to SPIR-V source language.
John Kessenich66e2faf2016-03-12 18:34:36 -0700258spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
John Kessenich140f3df2015-06-26 16:58:36 -0600259{
John Kessenich66e2faf2016-03-12 18:34:36 -0700260 switch (source) {
261 case glslang::EShSourceGlsl:
262 switch (profile) {
263 case ENoProfile:
264 case ECoreProfile:
265 case ECompatibilityProfile:
266 return spv::SourceLanguageGLSL;
267 case EEsProfile:
268 return spv::SourceLanguageESSL;
269 default:
270 return spv::SourceLanguageUnknown;
271 }
272 case glslang::EShSourceHlsl:
John Kessenich6fa17642017-04-07 15:33:08 -0600273 return spv::SourceLanguageHLSL;
John Kessenich140f3df2015-06-26 16:58:36 -0600274 default:
275 return spv::SourceLanguageUnknown;
276 }
277}
278
279// Translate glslang language (stage) to SPIR-V execution model.
280spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
281{
282 switch (stage) {
283 case EShLangVertex: return spv::ExecutionModelVertex;
284 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
285 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
286 case EShLangGeometry: return spv::ExecutionModelGeometry;
287 case EShLangFragment: return spv::ExecutionModelFragment;
288 case EShLangCompute: return spv::ExecutionModelGLCompute;
Chao Chen3c366992018-09-19 11:41:59 -0700289#ifdef NV_EXTENSIONS
Ashwin Leleff1783d2018-10-22 16:41:44 -0700290 case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV;
291 case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV;
292 case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV;
293 case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV;
294 case EShLangMissNV: return spv::ExecutionModelMissNV;
295 case EShLangCallableNV: return spv::ExecutionModelCallableNV;
Chao Chen3c366992018-09-19 11:41:59 -0700296 case EShLangTaskNV: return spv::ExecutionModelTaskNV;
297 case EShLangMeshNV: return spv::ExecutionModelMeshNV;
298#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600299 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700300 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600301 return spv::ExecutionModelFragment;
302 }
303}
304
John Kessenich140f3df2015-06-26 16:58:36 -0600305// Translate glslang sampler type to SPIR-V dimensionality.
306spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
307{
308 switch (sampler.dim) {
John Kessenich55e7d112015-11-15 21:33:39 -0700309 case glslang::Esd1D: return spv::Dim1D;
310 case glslang::Esd2D: return spv::Dim2D;
311 case glslang::Esd3D: return spv::Dim3D;
312 case glslang::EsdCube: return spv::DimCube;
313 case glslang::EsdRect: return spv::DimRect;
314 case glslang::EsdBuffer: return spv::DimBuffer;
John Kessenich6c292d32016-02-15 20:58:50 -0700315 case glslang::EsdSubpass: return spv::DimSubpassData;
John Kessenich140f3df2015-06-26 16:58:36 -0600316 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700317 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600318 return spv::Dim2D;
319 }
320}
321
John Kessenichf6640762016-08-01 19:44:00 -0600322// Translate glslang precision to SPIR-V precision decorations.
323spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
John Kessenich140f3df2015-06-26 16:58:36 -0600324{
John Kessenichf6640762016-08-01 19:44:00 -0600325 switch (glslangPrecision) {
John Kessenich61c47a92015-12-14 18:21:19 -0700326 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
John Kessenich5e4b1242015-08-06 22:53:06 -0600327 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
John Kessenich140f3df2015-06-26 16:58:36 -0600328 default:
329 return spv::NoPrecision;
330 }
331}
332
John Kessenichf6640762016-08-01 19:44:00 -0600333// Translate glslang type to SPIR-V precision decorations.
334spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
335{
336 return TranslatePrecisionDecoration(type.getQualifier().precision);
337}
338
John Kessenich140f3df2015-06-26 16:58:36 -0600339// Translate glslang type to SPIR-V block decorations.
John Kessenich67027182017-04-19 18:34:49 -0600340spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer)
John Kessenich140f3df2015-06-26 16:58:36 -0600341{
342 if (type.getBasicType() == glslang::EbtBlock) {
343 switch (type.getQualifier().storage) {
344 case glslang::EvqUniform: return spv::DecorationBlock;
John Kessenich67027182017-04-19 18:34:49 -0600345 case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock;
John Kessenich140f3df2015-06-26 16:58:36 -0600346 case glslang::EvqVaryingIn: return spv::DecorationBlock;
347 case glslang::EvqVaryingOut: return spv::DecorationBlock;
Chao Chenb50c02e2018-09-19 11:42:24 -0700348#ifdef NV_EXTENSIONS
349 case glslang::EvqPayloadNV: return spv::DecorationBlock;
350 case glslang::EvqPayloadInNV: return spv::DecorationBlock;
351 case glslang::EvqHitAttrNV: return spv::DecorationBlock;
Ashwin Leleff1783d2018-10-22 16:41:44 -0700352 case glslang::EvqCallableDataNV: return spv::DecorationBlock;
353 case glslang::EvqCallableDataInNV: return spv::DecorationBlock;
Chao Chenb50c02e2018-09-19 11:42:24 -0700354#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600355 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700356 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600357 break;
358 }
359 }
360
John Kessenich4016e382016-07-15 11:53:56 -0600361 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600362}
363
Rex Xu1da878f2016-02-21 20:59:01 +0800364// Translate glslang type to SPIR-V memory decorations.
Jeff Bolz36831c92018-09-05 10:11:41 -0500365void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, bool useVulkanMemoryModel)
Rex Xu1da878f2016-02-21 20:59:01 +0800366{
Jeff Bolz36831c92018-09-05 10:11:41 -0500367 if (!useVulkanMemoryModel) {
368 if (qualifier.coherent)
369 memory.push_back(spv::DecorationCoherent);
370 if (qualifier.volatil) {
371 memory.push_back(spv::DecorationVolatile);
372 memory.push_back(spv::DecorationCoherent);
373 }
John Kessenich14b85d32018-06-04 15:36:03 -0600374 }
Rex Xu1da878f2016-02-21 20:59:01 +0800375 if (qualifier.restrict)
376 memory.push_back(spv::DecorationRestrict);
377 if (qualifier.readonly)
378 memory.push_back(spv::DecorationNonWritable);
379 if (qualifier.writeonly)
380 memory.push_back(spv::DecorationNonReadable);
381}
382
John Kessenich140f3df2015-06-26 16:58:36 -0600383// Translate glslang type to SPIR-V layout decorations.
John Kessenich3ac051e2015-12-20 11:29:16 -0700384spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
John Kessenich140f3df2015-06-26 16:58:36 -0600385{
386 if (type.isMatrix()) {
John Kessenich3ac051e2015-12-20 11:29:16 -0700387 switch (matrixLayout) {
John Kessenich140f3df2015-06-26 16:58:36 -0600388 case glslang::ElmRowMajor:
389 return spv::DecorationRowMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700390 case glslang::ElmColumnMajor:
John Kessenich140f3df2015-06-26 16:58:36 -0600391 return spv::DecorationColMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700392 default:
393 // opaque layouts don't need a majorness
John Kessenich4016e382016-07-15 11:53:56 -0600394 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600395 }
396 } else {
397 switch (type.getBasicType()) {
398 default:
John Kessenich4016e382016-07-15 11:53:56 -0600399 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600400 break;
401 case glslang::EbtBlock:
402 switch (type.getQualifier().storage) {
403 case glslang::EvqUniform:
404 case glslang::EvqBuffer:
405 switch (type.getQualifier().layoutPacking) {
406 case glslang::ElpShared: return spv::DecorationGLSLShared;
John Kessenich140f3df2015-06-26 16:58:36 -0600407 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
408 default:
John Kessenich4016e382016-07-15 11:53:56 -0600409 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600410 }
411 case glslang::EvqVaryingIn:
412 case glslang::EvqVaryingOut:
Chao Chen3c366992018-09-19 11:41:59 -0700413 if (type.getQualifier().isTaskMemory()) {
414 switch (type.getQualifier().layoutPacking) {
415 case glslang::ElpShared: return spv::DecorationGLSLShared;
416 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
417 default: break;
418 }
419 } else {
420 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
421 }
John Kessenich4016e382016-07-15 11:53:56 -0600422 return spv::DecorationMax;
Chao Chenb50c02e2018-09-19 11:42:24 -0700423#ifdef NV_EXTENSIONS
424 case glslang::EvqPayloadNV:
425 case glslang::EvqPayloadInNV:
426 case glslang::EvqHitAttrNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700427 case glslang::EvqCallableDataNV:
428 case glslang::EvqCallableDataInNV:
Chao Chenb50c02e2018-09-19 11:42:24 -0700429 return spv::DecorationMax;
430#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600431 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700432 assert(0);
John Kessenich4016e382016-07-15 11:53:56 -0600433 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600434 }
435 }
436 }
437}
438
439// Translate glslang type to SPIR-V interpolation decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600440// Returns spv::DecorationMax when no decoration
John Kessenich55e7d112015-11-15 21:33:39 -0700441// should be applied.
Rex Xu17ff3432016-10-14 17:41:45 +0800442spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600443{
Rex Xubbceed72016-05-21 09:40:44 +0800444 if (qualifier.smooth)
John Kessenich55e7d112015-11-15 21:33:39 -0700445 // Smooth decoration doesn't exist in SPIR-V 1.0
John Kessenich4016e382016-07-15 11:53:56 -0600446 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800447 else if (qualifier.nopersp)
John Kessenich55e7d112015-11-15 21:33:39 -0700448 return spv::DecorationNoPerspective;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700449 else if (qualifier.flat)
John Kessenich140f3df2015-06-26 16:58:36 -0600450 return spv::DecorationFlat;
Rex Xu9d93a232016-05-05 12:30:44 +0800451#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +0800452 else if (qualifier.explicitInterp) {
453 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
Rex Xu9d93a232016-05-05 12:30:44 +0800454 return spv::DecorationExplicitInterpAMD;
Rex Xu17ff3432016-10-14 17:41:45 +0800455 }
Rex Xu9d93a232016-05-05 12:30:44 +0800456#endif
Rex Xubbceed72016-05-21 09:40:44 +0800457 else
John Kessenich4016e382016-07-15 11:53:56 -0600458 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800459}
460
461// Translate glslang type to SPIR-V auxiliary storage decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600462// Returns spv::DecorationMax when no decoration
Rex Xubbceed72016-05-21 09:40:44 +0800463// should be applied.
464spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
465{
466 if (qualifier.patch)
467 return spv::DecorationPatch;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700468 else if (qualifier.centroid)
John Kessenich140f3df2015-06-26 16:58:36 -0600469 return spv::DecorationCentroid;
John Kessenich5e801132016-02-15 11:09:46 -0700470 else if (qualifier.sample) {
471 builder.addCapability(spv::CapabilitySampleRateShading);
John Kessenich140f3df2015-06-26 16:58:36 -0600472 return spv::DecorationSample;
John Kessenich5e801132016-02-15 11:09:46 -0700473 } else
John Kessenich4016e382016-07-15 11:53:56 -0600474 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600475}
476
John Kessenich92187592016-02-01 13:45:25 -0700477// If glslang type is invariant, return SPIR-V invariant decoration.
John Kesseniche0b6cad2015-12-24 10:30:13 -0700478spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600479{
John Kesseniche0b6cad2015-12-24 10:30:13 -0700480 if (qualifier.invariant)
John Kessenich140f3df2015-06-26 16:58:36 -0600481 return spv::DecorationInvariant;
482 else
John Kessenich4016e382016-07-15 11:53:56 -0600483 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600484}
485
qining9220dbb2016-05-04 17:34:38 -0400486// If glslang type is noContraction, return SPIR-V NoContraction decoration.
487spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
488{
489 if (qualifier.noContraction)
490 return spv::DecorationNoContraction;
491 else
John Kessenich4016e382016-07-15 11:53:56 -0600492 return spv::DecorationMax;
qining9220dbb2016-05-04 17:34:38 -0400493}
494
John Kessenich5611c6d2018-04-05 11:25:02 -0600495// If glslang type is nonUniform, return SPIR-V NonUniform decoration.
496spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
497{
498 if (qualifier.isNonUniform()) {
499 builder.addExtension("SPV_EXT_descriptor_indexing");
500 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
501 return spv::DecorationNonUniformEXT;
502 } else
503 return spv::DecorationMax;
504}
505
Jeff Bolz36831c92018-09-05 10:11:41 -0500506spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
507{
508 if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) {
509 return spv::MemoryAccessMaskNone;
510 }
511 spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone;
512 if (coherentFlags.volatil ||
513 coherentFlags.coherent ||
514 coherentFlags.devicecoherent ||
515 coherentFlags.queuefamilycoherent ||
516 coherentFlags.workgroupcoherent ||
517 coherentFlags.subgroupcoherent) {
518 mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask |
519 spv::MemoryAccessMakePointerVisibleKHRMask;
520 }
521 if (coherentFlags.nonprivate) {
522 mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask;
523 }
524 if (coherentFlags.volatil) {
525 mask = mask | spv::MemoryAccessVolatileMask;
526 }
527 if (mask != spv::MemoryAccessMaskNone) {
528 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
529 }
530 return mask;
531}
532
533spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
534{
535 if (!glslangIntermediate->usingVulkanMemoryModel()) {
536 return spv::ImageOperandsMaskNone;
537 }
538 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
539 if (coherentFlags.volatil ||
540 coherentFlags.coherent ||
541 coherentFlags.devicecoherent ||
542 coherentFlags.queuefamilycoherent ||
543 coherentFlags.workgroupcoherent ||
544 coherentFlags.subgroupcoherent) {
545 mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask |
546 spv::ImageOperandsMakeTexelVisibleKHRMask;
547 }
548 if (coherentFlags.nonprivate) {
549 mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask;
550 }
551 if (coherentFlags.volatil) {
552 mask = mask | spv::ImageOperandsVolatileTexelKHRMask;
553 }
554 if (mask != spv::ImageOperandsMaskNone) {
555 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
556 }
557 return mask;
558}
559
560spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
561{
562 spv::Builder::AccessChain::CoherentFlags flags;
563 flags.coherent = type.getQualifier().coherent;
564 flags.devicecoherent = type.getQualifier().devicecoherent;
565 flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent;
566 // shared variables are implicitly workgroupcoherent in GLSL.
567 flags.workgroupcoherent = type.getQualifier().workgroupcoherent ||
568 type.getQualifier().storage == glslang::EvqShared;
569 flags.subgroupcoherent = type.getQualifier().subgroupcoherent;
570 // *coherent variables are implicitly nonprivate in GLSL
571 flags.nonprivate = type.getQualifier().nonprivate ||
Jeff Bolzab3c9652018-10-15 22:46:48 -0500572 flags.subgroupcoherent ||
573 flags.workgroupcoherent ||
574 flags.queuefamilycoherent ||
575 flags.devicecoherent ||
576 flags.coherent;
Jeff Bolz36831c92018-09-05 10:11:41 -0500577 flags.volatil = type.getQualifier().volatil;
578 flags.isImage = type.getBasicType() == glslang::EbtSampler;
579 return flags;
580}
581
582spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
583{
584 spv::Scope scope;
585 if (coherentFlags.coherent) {
586 // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model
587 scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
588 } else if (coherentFlags.devicecoherent) {
589 scope = spv::ScopeDevice;
590 } else if (coherentFlags.queuefamilycoherent) {
591 scope = spv::ScopeQueueFamilyKHR;
592 } else if (coherentFlags.workgroupcoherent) {
593 scope = spv::ScopeWorkgroup;
594 } else if (coherentFlags.subgroupcoherent) {
595 scope = spv::ScopeSubgroup;
596 } else {
597 scope = spv::ScopeMax;
598 }
599 if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) {
600 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
601 }
602 return scope;
603}
604
David Netoa901ffe2016-06-08 14:11:40 +0100605// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
606// associated capabilities when required. For some built-in variables, a capability
607// is generated only when using the variable in an executable instruction, but not when
608// just declaring a struct member variable with it. This is true for PointSize,
609// ClipDistance, and CullDistance.
610spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration)
John Kessenich140f3df2015-06-26 16:58:36 -0600611{
612 switch (builtIn) {
John Kessenich92187592016-02-01 13:45:25 -0700613 case glslang::EbvPointSize:
John Kessenich78a45572016-07-08 14:05:15 -0600614 // Defer adding the capability until the built-in is actually used.
615 if (! memberDeclaration) {
616 switch (glslangIntermediate->getStage()) {
617 case EShLangGeometry:
618 builder.addCapability(spv::CapabilityGeometryPointSize);
619 break;
620 case EShLangTessControl:
621 case EShLangTessEvaluation:
622 builder.addCapability(spv::CapabilityTessellationPointSize);
623 break;
624 default:
625 break;
626 }
John Kessenich92187592016-02-01 13:45:25 -0700627 }
628 return spv::BuiltInPointSize;
629
John Kessenichebb50532016-05-16 19:22:05 -0600630 // These *Distance capabilities logically belong here, but if the member is declared and
631 // then never used, consumers of SPIR-V prefer the capability not be declared.
632 // They are now generated when used, rather than here when declared.
633 // Potentially, the specification should be more clear what the minimum
634 // use needed is to trigger the capability.
635 //
John Kessenich92187592016-02-01 13:45:25 -0700636 case glslang::EbvClipDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100637 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800638 builder.addCapability(spv::CapabilityClipDistance);
John Kessenich92187592016-02-01 13:45:25 -0700639 return spv::BuiltInClipDistance;
640
641 case glslang::EbvCullDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100642 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800643 builder.addCapability(spv::CapabilityCullDistance);
John Kessenich92187592016-02-01 13:45:25 -0700644 return spv::BuiltInCullDistance;
645
646 case glslang::EbvViewportIndex:
John Kessenichba6a3c22017-09-13 13:22:50 -0600647 builder.addCapability(spv::CapabilityMultiViewport);
648 if (glslangIntermediate->getStage() == EShLangVertex ||
649 glslangIntermediate->getStage() == EShLangTessControl ||
650 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800651
John Kessenichba6a3c22017-09-13 13:22:50 -0600652 builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer);
653 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800654 }
John Kessenich92187592016-02-01 13:45:25 -0700655 return spv::BuiltInViewportIndex;
656
John Kessenich5e801132016-02-15 11:09:46 -0700657 case glslang::EbvSampleId:
658 builder.addCapability(spv::CapabilitySampleRateShading);
659 return spv::BuiltInSampleId;
660
661 case glslang::EbvSamplePosition:
662 builder.addCapability(spv::CapabilitySampleRateShading);
663 return spv::BuiltInSamplePosition;
664
665 case glslang::EbvSampleMask:
John Kessenich5e801132016-02-15 11:09:46 -0700666 return spv::BuiltInSampleMask;
667
John Kessenich78a45572016-07-08 14:05:15 -0600668 case glslang::EbvLayer:
Chao Chen3c366992018-09-19 11:41:59 -0700669#ifdef NV_EXTENSIONS
670 if (glslangIntermediate->getStage() == EShLangMeshNV) {
671 return spv::BuiltInLayer;
672 }
673#endif
John Kessenichba6a3c22017-09-13 13:22:50 -0600674 builder.addCapability(spv::CapabilityGeometry);
675 if (glslangIntermediate->getStage() == EShLangVertex ||
676 glslangIntermediate->getStage() == EShLangTessControl ||
677 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800678
John Kessenichba6a3c22017-09-13 13:22:50 -0600679 builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer);
680 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800681 }
John Kessenich78a45572016-07-08 14:05:15 -0600682 return spv::BuiltInLayer;
683
John Kessenich140f3df2015-06-26 16:58:36 -0600684 case glslang::EbvPosition: return spv::BuiltInPosition;
John Kessenich140f3df2015-06-26 16:58:36 -0600685 case glslang::EbvVertexId: return spv::BuiltInVertexId;
686 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
John Kessenich6c292d32016-02-15 20:58:50 -0700687 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
688 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
Rex Xuf3b27472016-07-22 18:15:31 +0800689
John Kessenichda581a22015-10-14 14:10:30 -0600690 case glslang::EbvBaseVertex:
John Kessenich66011cb2018-03-06 16:12:04 -0700691 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
Rex Xuf3b27472016-07-22 18:15:31 +0800692 builder.addCapability(spv::CapabilityDrawParameters);
693 return spv::BuiltInBaseVertex;
694
John Kessenichda581a22015-10-14 14:10:30 -0600695 case glslang::EbvBaseInstance:
John Kessenich66011cb2018-03-06 16:12:04 -0700696 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
Rex Xuf3b27472016-07-22 18:15:31 +0800697 builder.addCapability(spv::CapabilityDrawParameters);
698 return spv::BuiltInBaseInstance;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200699
John Kessenichda581a22015-10-14 14:10:30 -0600700 case glslang::EbvDrawId:
John Kessenich66011cb2018-03-06 16:12:04 -0700701 addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters);
Rex Xuf3b27472016-07-22 18:15:31 +0800702 builder.addCapability(spv::CapabilityDrawParameters);
703 return spv::BuiltInDrawIndex;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200704
705 case glslang::EbvPrimitiveId:
706 if (glslangIntermediate->getStage() == EShLangFragment)
707 builder.addCapability(spv::CapabilityGeometry);
708 return spv::BuiltInPrimitiveId;
709
Rex Xu37cdcee2017-06-29 17:46:34 +0800710 case glslang::EbvFragStencilRef:
Rex Xue8fdd792017-08-23 23:24:42 +0800711 builder.addExtension(spv::E_SPV_EXT_shader_stencil_export);
712 builder.addCapability(spv::CapabilityStencilExportEXT);
713 return spv::BuiltInFragStencilRefEXT;
Rex Xu37cdcee2017-06-29 17:46:34 +0800714
John Kessenich140f3df2015-06-26 16:58:36 -0600715 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
John Kessenich140f3df2015-06-26 16:58:36 -0600716 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
717 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
718 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
719 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
720 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
721 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
722 case glslang::EbvFace: return spv::BuiltInFrontFacing;
John Kessenich140f3df2015-06-26 16:58:36 -0600723 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
724 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
725 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
726 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
727 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
728 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
729 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
730 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
Rex Xu51596642016-09-21 18:56:12 +0800731
Rex Xu574ab042016-04-14 16:53:07 +0800732 case glslang::EbvSubGroupSize:
Rex Xu36876e62016-09-23 22:13:43 +0800733 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800734 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
735 return spv::BuiltInSubgroupSize;
736
Rex Xu574ab042016-04-14 16:53:07 +0800737 case glslang::EbvSubGroupInvocation:
Rex Xu36876e62016-09-23 22:13:43 +0800738 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800739 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
740 return spv::BuiltInSubgroupLocalInvocationId;
741
Rex Xu574ab042016-04-14 16:53:07 +0800742 case glslang::EbvSubGroupEqMask:
Rex Xu51596642016-09-21 18:56:12 +0800743 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
744 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
745 return spv::BuiltInSubgroupEqMaskKHR;
746
Rex Xu574ab042016-04-14 16:53:07 +0800747 case glslang::EbvSubGroupGeMask:
Rex Xu51596642016-09-21 18:56:12 +0800748 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
749 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
750 return spv::BuiltInSubgroupGeMaskKHR;
751
Rex Xu574ab042016-04-14 16:53:07 +0800752 case glslang::EbvSubGroupGtMask:
Rex Xu51596642016-09-21 18:56:12 +0800753 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
754 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
755 return spv::BuiltInSubgroupGtMaskKHR;
756
Rex Xu574ab042016-04-14 16:53:07 +0800757 case glslang::EbvSubGroupLeMask:
Rex Xu51596642016-09-21 18:56:12 +0800758 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
759 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
760 return spv::BuiltInSubgroupLeMaskKHR;
761
Rex Xu574ab042016-04-14 16:53:07 +0800762 case glslang::EbvSubGroupLtMask:
Rex Xu51596642016-09-21 18:56:12 +0800763 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
764 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
765 return spv::BuiltInSubgroupLtMaskKHR;
766
John Kessenich66011cb2018-03-06 16:12:04 -0700767 case glslang::EbvNumSubgroups:
768 builder.addCapability(spv::CapabilityGroupNonUniform);
769 return spv::BuiltInNumSubgroups;
770
771 case glslang::EbvSubgroupID:
772 builder.addCapability(spv::CapabilityGroupNonUniform);
773 return spv::BuiltInSubgroupId;
774
775 case glslang::EbvSubgroupSize2:
776 builder.addCapability(spv::CapabilityGroupNonUniform);
777 return spv::BuiltInSubgroupSize;
778
779 case glslang::EbvSubgroupInvocation2:
780 builder.addCapability(spv::CapabilityGroupNonUniform);
781 return spv::BuiltInSubgroupLocalInvocationId;
782
783 case glslang::EbvSubgroupEqMask2:
784 builder.addCapability(spv::CapabilityGroupNonUniform);
785 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
786 return spv::BuiltInSubgroupEqMask;
787
788 case glslang::EbvSubgroupGeMask2:
789 builder.addCapability(spv::CapabilityGroupNonUniform);
790 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
791 return spv::BuiltInSubgroupGeMask;
792
793 case glslang::EbvSubgroupGtMask2:
794 builder.addCapability(spv::CapabilityGroupNonUniform);
795 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
796 return spv::BuiltInSubgroupGtMask;
797
798 case glslang::EbvSubgroupLeMask2:
799 builder.addCapability(spv::CapabilityGroupNonUniform);
800 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
801 return spv::BuiltInSubgroupLeMask;
802
803 case glslang::EbvSubgroupLtMask2:
804 builder.addCapability(spv::CapabilityGroupNonUniform);
805 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
806 return spv::BuiltInSubgroupLtMask;
Rex Xu9d93a232016-05-05 12:30:44 +0800807#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +0800808 case glslang::EbvBaryCoordNoPersp:
809 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
810 return spv::BuiltInBaryCoordNoPerspAMD;
811
812 case glslang::EbvBaryCoordNoPerspCentroid:
813 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
814 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
815
816 case glslang::EbvBaryCoordNoPerspSample:
817 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
818 return spv::BuiltInBaryCoordNoPerspSampleAMD;
819
820 case glslang::EbvBaryCoordSmooth:
821 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
822 return spv::BuiltInBaryCoordSmoothAMD;
823
824 case glslang::EbvBaryCoordSmoothCentroid:
825 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
826 return spv::BuiltInBaryCoordSmoothCentroidAMD;
827
828 case glslang::EbvBaryCoordSmoothSample:
829 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
830 return spv::BuiltInBaryCoordSmoothSampleAMD;
831
832 case glslang::EbvBaryCoordPullModel:
833 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
834 return spv::BuiltInBaryCoordPullModelAMD;
Rex Xu9d93a232016-05-05 12:30:44 +0800835#endif
chaoc771d89f2017-01-13 01:10:53 -0800836
John Kessenich6c8aaac2017-02-27 01:20:51 -0700837 case glslang::EbvDeviceIndex:
John Kessenich66011cb2018-03-06 16:12:04 -0700838 addPre13Extension(spv::E_SPV_KHR_device_group);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700839 builder.addCapability(spv::CapabilityDeviceGroup);
John Kessenich42e33c92017-02-27 01:50:28 -0700840 return spv::BuiltInDeviceIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700841
842 case glslang::EbvViewIndex:
John Kessenich66011cb2018-03-06 16:12:04 -0700843 addPre13Extension(spv::E_SPV_KHR_multiview);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700844 builder.addCapability(spv::CapabilityMultiView);
John Kessenich42e33c92017-02-27 01:50:28 -0700845 return spv::BuiltInViewIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700846
Daniel Koch5154db52018-11-26 10:01:58 -0500847 case glslang::EbvFragSizeEXT:
848 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
849 builder.addCapability(spv::CapabilityFragmentDensityEXT);
850 return spv::BuiltInFragSizeEXT;
851
852 case glslang::EbvFragInvocationCountEXT:
853 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
854 builder.addCapability(spv::CapabilityFragmentDensityEXT);
855 return spv::BuiltInFragInvocationCountEXT;
856
chaoc771d89f2017-01-13 01:10:53 -0800857#ifdef NV_EXTENSIONS
858 case glslang::EbvViewportMaskNV:
Rex Xu5e317ff2017-03-16 23:02:39 +0800859 if (!memberDeclaration) {
860 builder.addExtension(spv::E_SPV_NV_viewport_array2);
861 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
862 }
chaoc771d89f2017-01-13 01:10:53 -0800863 return spv::BuiltInViewportMaskNV;
864 case glslang::EbvSecondaryPositionNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800865 if (!memberDeclaration) {
866 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
867 builder.addCapability(spv::CapabilityShaderStereoViewNV);
868 }
chaoc771d89f2017-01-13 01:10:53 -0800869 return spv::BuiltInSecondaryPositionNV;
870 case glslang::EbvSecondaryViewportMaskNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800871 if (!memberDeclaration) {
872 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
873 builder.addCapability(spv::CapabilityShaderStereoViewNV);
874 }
chaoc771d89f2017-01-13 01:10:53 -0800875 return spv::BuiltInSecondaryViewportMaskNV;
chaocdf3956c2017-02-14 14:52:34 -0800876 case glslang::EbvPositionPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800877 if (!memberDeclaration) {
878 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
879 builder.addCapability(spv::CapabilityPerViewAttributesNV);
880 }
chaocdf3956c2017-02-14 14:52:34 -0800881 return spv::BuiltInPositionPerViewNV;
882 case glslang::EbvViewportMaskPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800883 if (!memberDeclaration) {
884 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
885 builder.addCapability(spv::CapabilityPerViewAttributesNV);
886 }
chaocdf3956c2017-02-14 14:52:34 -0800887 return spv::BuiltInViewportMaskPerViewNV;
Piers Daniell1c5443c2017-12-13 13:07:22 -0700888 case glslang::EbvFragFullyCoveredNV:
889 builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered);
890 builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT);
891 return spv::BuiltInFullyCoveredEXT;
Chao Chen5b2203d2018-09-19 11:43:21 -0700892 case glslang::EbvFragmentSizeNV:
893 builder.addExtension(spv::E_SPV_NV_shading_rate);
894 builder.addCapability(spv::CapabilityShadingRateNV);
895 return spv::BuiltInFragmentSizeNV;
896 case glslang::EbvInvocationsPerPixelNV:
897 builder.addExtension(spv::E_SPV_NV_shading_rate);
898 builder.addCapability(spv::CapabilityShadingRateNV);
899 return spv::BuiltInInvocationsPerPixelNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700900
901 // raytracing
902 case glslang::EbvLaunchIdNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700903 return spv::BuiltInLaunchIdNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700904 case glslang::EbvLaunchSizeNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700905 return spv::BuiltInLaunchSizeNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700906 case glslang::EbvWorldRayOriginNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700907 return spv::BuiltInWorldRayOriginNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700908 case glslang::EbvWorldRayDirectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700909 return spv::BuiltInWorldRayDirectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700910 case glslang::EbvObjectRayOriginNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700911 return spv::BuiltInObjectRayOriginNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700912 case glslang::EbvObjectRayDirectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700913 return spv::BuiltInObjectRayDirectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700914 case glslang::EbvRayTminNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700915 return spv::BuiltInRayTminNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700916 case glslang::EbvRayTmaxNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700917 return spv::BuiltInRayTmaxNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700918 case glslang::EbvInstanceCustomIndexNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700919 return spv::BuiltInInstanceCustomIndexNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700920 case glslang::EbvHitTNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700921 return spv::BuiltInHitTNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700922 case glslang::EbvHitKindNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700923 return spv::BuiltInHitKindNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700924 case glslang::EbvObjectToWorldNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700925 return spv::BuiltInObjectToWorldNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700926 case glslang::EbvWorldToObjectNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700927 return spv::BuiltInWorldToObjectNV;
928 case glslang::EbvIncomingRayFlagsNV:
929 return spv::BuiltInIncomingRayFlagsNV;
Chao Chen9eada4b2018-09-19 11:39:56 -0700930 case glslang::EbvBaryCoordNV:
931 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
932 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
933 return spv::BuiltInBaryCoordNV;
934 case glslang::EbvBaryCoordNoPerspNV:
935 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
936 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
937 return spv::BuiltInBaryCoordNoPerspNV;
Chao Chen3c366992018-09-19 11:41:59 -0700938 case glslang::EbvTaskCountNV:
939 return spv::BuiltInTaskCountNV;
940 case glslang::EbvPrimitiveCountNV:
941 return spv::BuiltInPrimitiveCountNV;
942 case glslang::EbvPrimitiveIndicesNV:
943 return spv::BuiltInPrimitiveIndicesNV;
944 case glslang::EbvClipDistancePerViewNV:
945 return spv::BuiltInClipDistancePerViewNV;
946 case glslang::EbvCullDistancePerViewNV:
947 return spv::BuiltInCullDistancePerViewNV;
948 case glslang::EbvLayerPerViewNV:
949 return spv::BuiltInLayerPerViewNV;
950 case glslang::EbvMeshViewCountNV:
951 return spv::BuiltInMeshViewCountNV;
952 case glslang::EbvMeshViewIndicesNV:
953 return spv::BuiltInMeshViewIndicesNV;
chaoc771d89f2017-01-13 01:10:53 -0800954#endif
Rex Xu3e783f92017-02-22 16:44:48 +0800955 default:
956 return spv::BuiltInMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600957 }
958}
959
Rex Xufc618912015-09-09 16:42:49 +0800960// Translate glslang image layout format to SPIR-V image format.
John Kessenich5d0fa972016-02-15 11:57:00 -0700961spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
Rex Xufc618912015-09-09 16:42:49 +0800962{
963 assert(type.getBasicType() == glslang::EbtSampler);
964
John Kessenich5d0fa972016-02-15 11:57:00 -0700965 // Check for capabilities
966 switch (type.getQualifier().layoutFormat) {
967 case glslang::ElfRg32f:
968 case glslang::ElfRg16f:
969 case glslang::ElfR11fG11fB10f:
970 case glslang::ElfR16f:
971 case glslang::ElfRgba16:
972 case glslang::ElfRgb10A2:
973 case glslang::ElfRg16:
974 case glslang::ElfRg8:
975 case glslang::ElfR16:
976 case glslang::ElfR8:
977 case glslang::ElfRgba16Snorm:
978 case glslang::ElfRg16Snorm:
979 case glslang::ElfRg8Snorm:
980 case glslang::ElfR16Snorm:
981 case glslang::ElfR8Snorm:
982
983 case glslang::ElfRg32i:
984 case glslang::ElfRg16i:
985 case glslang::ElfRg8i:
986 case glslang::ElfR16i:
987 case glslang::ElfR8i:
988
989 case glslang::ElfRgb10a2ui:
990 case glslang::ElfRg32ui:
991 case glslang::ElfRg16ui:
992 case glslang::ElfRg8ui:
993 case glslang::ElfR16ui:
994 case glslang::ElfR8ui:
995 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
996 break;
997
998 default:
999 break;
1000 }
1001
1002 // do the translation
Rex Xufc618912015-09-09 16:42:49 +08001003 switch (type.getQualifier().layoutFormat) {
1004 case glslang::ElfNone: return spv::ImageFormatUnknown;
1005 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
1006 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
1007 case glslang::ElfR32f: return spv::ImageFormatR32f;
1008 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
1009 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
1010 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
1011 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
1012 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
1013 case glslang::ElfR16f: return spv::ImageFormatR16f;
1014 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
1015 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
1016 case glslang::ElfRg16: return spv::ImageFormatRg16;
1017 case glslang::ElfRg8: return spv::ImageFormatRg8;
1018 case glslang::ElfR16: return spv::ImageFormatR16;
1019 case glslang::ElfR8: return spv::ImageFormatR8;
1020 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
1021 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
1022 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
1023 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
1024 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
1025 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
1026 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
1027 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
1028 case glslang::ElfR32i: return spv::ImageFormatR32i;
1029 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
1030 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
1031 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
1032 case glslang::ElfR16i: return spv::ImageFormatR16i;
1033 case glslang::ElfR8i: return spv::ImageFormatR8i;
1034 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
1035 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
1036 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
1037 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
1038 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
1039 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
1040 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
1041 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
1042 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
1043 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
John Kessenich4016e382016-07-15 11:53:56 -06001044 default: return spv::ImageFormatMax;
Rex Xufc618912015-09-09 16:42:49 +08001045 }
1046}
1047
John Kesseniche18fd202018-01-30 11:01:39 -07001048spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const
Rex Xu57e65922017-07-04 23:23:40 +08001049{
John Kesseniche18fd202018-01-30 11:01:39 -07001050 if (selectionNode.getFlatten())
1051 return spv::SelectionControlFlattenMask;
1052 if (selectionNode.getDontFlatten())
1053 return spv::SelectionControlDontFlattenMask;
1054 return spv::SelectionControlMaskNone;
Rex Xu57e65922017-07-04 23:23:40 +08001055}
1056
John Kesseniche18fd202018-01-30 11:01:39 -07001057spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const
steve-lunargf1709e72017-05-02 20:14:50 -06001058{
John Kesseniche18fd202018-01-30 11:01:39 -07001059 if (switchNode.getFlatten())
1060 return spv::SelectionControlFlattenMask;
1061 if (switchNode.getDontFlatten())
1062 return spv::SelectionControlDontFlattenMask;
1063 return spv::SelectionControlMaskNone;
1064}
1065
John Kessenicha2858d92018-01-31 08:11:18 -07001066// return a non-0 dependency if the dependency argument must be set
1067spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
1068 unsigned int& dependencyLength) const
John Kesseniche18fd202018-01-30 11:01:39 -07001069{
1070 spv::LoopControlMask control = spv::LoopControlMaskNone;
1071
1072 if (loopNode.getDontUnroll())
1073 control = control | spv::LoopControlDontUnrollMask;
1074 if (loopNode.getUnroll())
1075 control = control | spv::LoopControlUnrollMask;
LoopDawg4425f242018-02-18 11:40:01 -07001076 if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite)
John Kessenicha2858d92018-01-31 08:11:18 -07001077 control = control | spv::LoopControlDependencyInfiniteMask;
1078 else if (loopNode.getLoopDependency() > 0) {
1079 control = control | spv::LoopControlDependencyLengthMask;
1080 dependencyLength = loopNode.getLoopDependency();
1081 }
John Kesseniche18fd202018-01-30 11:01:39 -07001082
1083 return control;
steve-lunargf1709e72017-05-02 20:14:50 -06001084}
1085
John Kessenicha5c5fb62017-05-05 05:09:58 -06001086// Translate glslang type to SPIR-V storage class.
1087spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
1088{
1089 if (type.getQualifier().isPipeInput())
1090 return spv::StorageClassInput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001091 if (type.getQualifier().isPipeOutput())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001092 return spv::StorageClassOutput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001093
1094 if (glslangIntermediate->getSource() != glslang::EShSourceHlsl ||
1095 type.getQualifier().storage == glslang::EvqUniform) {
1096 if (type.getBasicType() == glslang::EbtAtomicUint)
1097 return spv::StorageClassAtomicCounter;
1098 if (type.containsOpaque())
1099 return spv::StorageClassUniformConstant;
1100 }
1101
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001102#ifdef NV_EXTENSIONS
1103 if (type.getQualifier().isUniformOrBuffer() &&
1104 type.getQualifier().layoutShaderRecordNV) {
1105 return spv::StorageClassShaderRecordBufferNV;
1106 }
1107#endif
1108
John Kessenichbed4e4f2017-09-08 02:38:07 -06001109 if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) {
John Kessenich66011cb2018-03-06 16:12:04 -07001110 addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class);
John Kessenicha5c5fb62017-05-05 05:09:58 -06001111 return spv::StorageClassStorageBuffer;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001112 }
1113
1114 if (type.getQualifier().isUniformOrBuffer()) {
John Kessenicha5c5fb62017-05-05 05:09:58 -06001115 if (type.getQualifier().layoutPushConstant)
1116 return spv::StorageClassPushConstant;
1117 if (type.getBasicType() == glslang::EbtBlock)
1118 return spv::StorageClassUniform;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001119 return spv::StorageClassUniformConstant;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001120 }
John Kessenichbed4e4f2017-09-08 02:38:07 -06001121
1122 switch (type.getQualifier().storage) {
1123 case glslang::EvqShared: return spv::StorageClassWorkgroup;
1124 case glslang::EvqGlobal: return spv::StorageClassPrivate;
1125 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
1126 case glslang::EvqTemporary: return spv::StorageClassFunction;
Chao Chenb50c02e2018-09-19 11:42:24 -07001127#ifdef NV_EXTENSIONS
Ashwin Leleff1783d2018-10-22 16:41:44 -07001128 case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV;
1129 case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV;
1130 case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV;
1131 case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV;
1132 case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV;
Chao Chenb50c02e2018-09-19 11:42:24 -07001133#endif
John Kessenichbed4e4f2017-09-08 02:38:07 -06001134 default:
1135 assert(0);
1136 break;
1137 }
1138
1139 return spv::StorageClassFunction;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001140}
1141
John Kessenich5611c6d2018-04-05 11:25:02 -06001142// Add capabilities pertaining to how an array is indexed.
1143void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
1144 const glslang::TType& indexType)
1145{
1146 if (indexType.getQualifier().isNonUniform()) {
1147 // deal with an asserted non-uniform index
Jeff Bolzc140b962018-07-12 16:51:18 -05001148 // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration
John Kessenich5611c6d2018-04-05 11:25:02 -06001149 if (baseType.getBasicType() == glslang::EbtSampler) {
1150 if (baseType.getQualifier().hasAttachment())
1151 builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT);
1152 else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer)
1153 builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT);
1154 else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer)
1155 builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT);
1156 else if (baseType.isImage())
1157 builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT);
1158 else if (baseType.isTexture())
1159 builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT);
1160 } else if (baseType.getBasicType() == glslang::EbtBlock) {
1161 if (baseType.getQualifier().storage == glslang::EvqBuffer)
1162 builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT);
1163 else if (baseType.getQualifier().storage == glslang::EvqUniform)
1164 builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT);
1165 }
1166 } else {
1167 // assume a dynamically uniform index
1168 if (baseType.getBasicType() == glslang::EbtSampler) {
Jeff Bolzc140b962018-07-12 16:51:18 -05001169 if (baseType.getQualifier().hasAttachment()) {
1170 builder.addExtension("SPV_EXT_descriptor_indexing");
John Kessenich5611c6d2018-04-05 11:25:02 -06001171 builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001172 } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) {
1173 builder.addExtension("SPV_EXT_descriptor_indexing");
John Kessenich5611c6d2018-04-05 11:25:02 -06001174 builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001175 } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) {
1176 builder.addExtension("SPV_EXT_descriptor_indexing");
John Kessenich5611c6d2018-04-05 11:25:02 -06001177 builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001178 }
John Kessenich5611c6d2018-04-05 11:25:02 -06001179 }
1180 }
1181}
1182
qining25262b32016-05-06 17:25:16 -04001183// Return whether or not the given type is something that should be tied to a
John Kessenich6c292d32016-02-15 20:58:50 -07001184// descriptor set.
1185bool IsDescriptorResource(const glslang::TType& type)
1186{
John Kessenichf7497e22016-03-08 21:36:22 -07001187 // uniform and buffer blocks are included, unless it is a push_constant
John Kessenich6c292d32016-02-15 20:58:50 -07001188 if (type.getBasicType() == glslang::EbtBlock)
Chao Chenb50c02e2018-09-19 11:42:24 -07001189 return type.getQualifier().isUniformOrBuffer() &&
1190#ifdef NV_EXTENSIONS
1191 ! type.getQualifier().layoutShaderRecordNV &&
1192#endif
1193 ! type.getQualifier().layoutPushConstant;
John Kessenich6c292d32016-02-15 20:58:50 -07001194
1195 // non block...
1196 // basically samplerXXX/subpass/sampler/texture are all included
1197 // if they are the global-scope-class, not the function parameter
1198 // (or local, if they ever exist) class.
1199 if (type.getBasicType() == glslang::EbtSampler)
1200 return type.getQualifier().isUniformOrBuffer();
1201
1202 // None of the above.
1203 return false;
1204}
1205
John Kesseniche0b6cad2015-12-24 10:30:13 -07001206void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
1207{
1208 if (child.layoutMatrix == glslang::ElmNone)
1209 child.layoutMatrix = parent.layoutMatrix;
1210
1211 if (parent.invariant)
1212 child.invariant = true;
1213 if (parent.nopersp)
1214 child.nopersp = true;
Rex Xu9d93a232016-05-05 12:30:44 +08001215#ifdef AMD_EXTENSIONS
1216 if (parent.explicitInterp)
1217 child.explicitInterp = true;
1218#endif
John Kesseniche0b6cad2015-12-24 10:30:13 -07001219 if (parent.flat)
1220 child.flat = true;
1221 if (parent.centroid)
1222 child.centroid = true;
1223 if (parent.patch)
1224 child.patch = true;
1225 if (parent.sample)
1226 child.sample = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001227 if (parent.coherent)
1228 child.coherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001229 if (parent.devicecoherent)
1230 child.devicecoherent = true;
1231 if (parent.queuefamilycoherent)
1232 child.queuefamilycoherent = true;
1233 if (parent.workgroupcoherent)
1234 child.workgroupcoherent = true;
1235 if (parent.subgroupcoherent)
1236 child.subgroupcoherent = true;
1237 if (parent.nonprivate)
1238 child.nonprivate = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001239 if (parent.volatil)
1240 child.volatil = true;
1241 if (parent.restrict)
1242 child.restrict = true;
1243 if (parent.readonly)
1244 child.readonly = true;
1245 if (parent.writeonly)
1246 child.writeonly = true;
Chao Chen3c366992018-09-19 11:41:59 -07001247#ifdef NV_EXTENSIONS
1248 if (parent.perPrimitiveNV)
1249 child.perPrimitiveNV = true;
1250 if (parent.perViewNV)
1251 child.perViewNV = true;
1252 if (parent.perTaskNV)
1253 child.perTaskNV = true;
1254#endif
John Kesseniche0b6cad2015-12-24 10:30:13 -07001255}
1256
John Kessenichf2b7f332016-09-01 17:05:23 -06001257bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001258{
John Kessenich7b9fa252016-01-21 18:56:57 -07001259 // This should list qualifiers that simultaneous satisfy:
John Kessenichf2b7f332016-09-01 17:05:23 -06001260 // - struct members might inherit from a struct declaration
1261 // (note that non-block structs don't explicitly inherit,
1262 // only implicitly, meaning no decoration involved)
1263 // - affect decorations on the struct members
1264 // (note smooth does not, and expecting something like volatile
1265 // to effect the whole object)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001266 // - are not part of the offset/st430/etc or row/column-major layout
John Kessenichf2b7f332016-09-01 17:05:23 -06001267 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
John Kesseniche0b6cad2015-12-24 10:30:13 -07001268}
1269
John Kessenich140f3df2015-06-26 16:58:36 -06001270//
1271// Implement the TGlslangToSpvTraverser class.
1272//
1273
John Kessenich2b5ea9f2018-01-31 18:35:56 -07001274TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate,
John Kessenich121853f2017-05-31 17:11:16 -06001275 spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options)
1276 : TIntermTraverser(true, false, true),
1277 options(options),
1278 shaderEntry(nullptr), currentFunction(nullptr),
John Kesseniched33e052016-10-06 12:59:51 -06001279 sequenceDepth(0), logger(buildLogger),
John Kessenich2b5ea9f2018-01-31 18:35:56 -07001280 builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger),
John Kessenich517fe7a2016-11-26 13:31:47 -07001281 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
John Kessenich140f3df2015-06-26 16:58:36 -06001282 glslangIntermediate(glslangIntermediate)
1283{
1284 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
1285
1286 builder.clearAccessChain();
John Kessenich2a271162017-07-20 20:00:36 -06001287 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()),
1288 glslangIntermediate->getVersion());
1289
John Kessenich121853f2017-05-31 17:11:16 -06001290 if (options.generateDebugInfo) {
John Kesseniche485c7a2017-05-31 18:50:53 -06001291 builder.setEmitOpLines();
John Kessenich2a271162017-07-20 20:00:36 -06001292 builder.setSourceFile(glslangIntermediate->getSourceFile());
1293
1294 // Set the source shader's text. If for SPV version 1.0, include
1295 // a preamble in comments stating the OpModuleProcessed instructions.
1296 // Otherwise, emit those as actual instructions.
1297 std::string text;
1298 const std::vector<std::string>& processes = glslangIntermediate->getProcesses();
1299 for (int p = 0; p < (int)processes.size(); ++p) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001300 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) {
John Kessenich2a271162017-07-20 20:00:36 -06001301 text.append("// OpModuleProcessed ");
1302 text.append(processes[p]);
1303 text.append("\n");
1304 } else
1305 builder.addModuleProcessed(processes[p]);
1306 }
John Kessenich8717a5d2018-10-26 10:12:32 -06001307 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0)
John Kessenich2a271162017-07-20 20:00:36 -06001308 text.append("#line 1\n");
1309 text.append(glslangIntermediate->getSourceText());
1310 builder.setSourceText(text);
Greg Fischerd445bb22018-12-06 11:13:15 -07001311 // Pass name and text for all included files
1312 const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
1313 for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
1314 builder.addInclude(iItr->first, iItr->second);
John Kessenich121853f2017-05-31 17:11:16 -06001315 }
John Kessenich140f3df2015-06-26 16:58:36 -06001316 stdBuiltins = builder.import("GLSL.std.450");
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001317
1318 spv::AddressingModel addressingModel = spv::AddressingModelLogical;
1319 spv::MemoryModel memoryModel = spv::MemoryModelGLSL450;
1320
1321 if (glslangIntermediate->usingPhysicalStorageBuffer()) {
1322 addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT;
1323 builder.addExtension(spv::E_SPV_EXT_physical_storage_buffer);
1324 builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT);
1325 };
Jeff Bolz36831c92018-09-05 10:11:41 -05001326 if (glslangIntermediate->usingVulkanMemoryModel()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001327 memoryModel = spv::MemoryModelVulkanKHR;
1328 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
Jeff Bolz36831c92018-09-05 10:11:41 -05001329 builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model);
Jeff Bolz36831c92018-09-05 10:11:41 -05001330 }
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001331 builder.setMemoryModel(addressingModel, memoryModel);
1332
John Kessenicheee9d532016-09-19 18:09:30 -06001333 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
1334 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001335
1336 // Add the source extensions
John Kessenich2f273362015-07-18 22:34:27 -06001337 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
1338 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
John Kessenich140f3df2015-06-26 16:58:36 -06001339 builder.addSourceExtension(it->c_str());
1340
1341 // Add the top-level modes for this shader.
1342
John Kessenich92187592016-02-01 13:45:25 -07001343 if (glslangIntermediate->getXfbMode()) {
1344 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06001345 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
John Kessenich92187592016-02-01 13:45:25 -07001346 }
John Kessenich140f3df2015-06-26 16:58:36 -06001347
1348 unsigned int mode;
1349 switch (glslangIntermediate->getStage()) {
1350 case EShLangVertex:
John Kessenich5e4b1242015-08-06 22:53:06 -06001351 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -06001352 break;
1353
steve-lunarge7412492017-03-23 11:56:07 -06001354 case EShLangTessEvaluation:
John Kessenich140f3df2015-06-26 16:58:36 -06001355 case EShLangTessControl:
John Kessenich5e4b1242015-08-06 22:53:06 -06001356 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -06001357
steve-lunarge7412492017-03-23 11:56:07 -06001358 glslang::TLayoutGeometry primitive;
1359
1360 if (glslangIntermediate->getStage() == EShLangTessControl) {
1361 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1362 primitive = glslangIntermediate->getOutputPrimitive();
1363 } else {
1364 primitive = glslangIntermediate->getInputPrimitive();
1365 }
1366
1367 switch (primitive) {
John Kessenich55e7d112015-11-15 21:33:39 -07001368 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1369 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
1370 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
John Kessenich4016e382016-07-15 11:53:56 -06001371 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001372 }
John Kessenich4016e382016-07-15 11:53:56 -06001373 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001374 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1375
John Kesseniche6903322015-10-13 16:29:02 -06001376 switch (glslangIntermediate->getVertexSpacing()) {
1377 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
1378 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
1379 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
John Kessenich4016e382016-07-15 11:53:56 -06001380 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001381 }
John Kessenich4016e382016-07-15 11:53:56 -06001382 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001383 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1384
1385 switch (glslangIntermediate->getVertexOrder()) {
1386 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
1387 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
John Kessenich4016e382016-07-15 11:53:56 -06001388 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001389 }
John Kessenich4016e382016-07-15 11:53:56 -06001390 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001391 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1392
1393 if (glslangIntermediate->getPointMode())
1394 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
John Kessenich140f3df2015-06-26 16:58:36 -06001395 break;
1396
1397 case EShLangGeometry:
John Kessenich5e4b1242015-08-06 22:53:06 -06001398 builder.addCapability(spv::CapabilityGeometry);
John Kessenich140f3df2015-06-26 16:58:36 -06001399 switch (glslangIntermediate->getInputPrimitive()) {
1400 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
1401 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
1402 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
John Kessenich55e7d112015-11-15 21:33:39 -07001403 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001404 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
John Kessenich4016e382016-07-15 11:53:56 -06001405 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001406 }
John Kessenich4016e382016-07-15 11:53:56 -06001407 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001408 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kesseniche6903322015-10-13 16:29:02 -06001409
John Kessenich140f3df2015-06-26 16:58:36 -06001410 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
1411
1412 switch (glslangIntermediate->getOutputPrimitive()) {
1413 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1414 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
1415 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
John Kessenich4016e382016-07-15 11:53:56 -06001416 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001417 }
John Kessenich4016e382016-07-15 11:53:56 -06001418 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001419 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1420 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1421 break;
1422
1423 case EShLangFragment:
John Kessenich5e4b1242015-08-06 22:53:06 -06001424 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -06001425 if (glslangIntermediate->getPixelCenterInteger())
1426 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
John Kesseniche6903322015-10-13 16:29:02 -06001427
John Kessenich140f3df2015-06-26 16:58:36 -06001428 if (glslangIntermediate->getOriginUpperLeft())
1429 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
John Kessenich5e4b1242015-08-06 22:53:06 -06001430 else
1431 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
John Kesseniche6903322015-10-13 16:29:02 -06001432
1433 if (glslangIntermediate->getEarlyFragmentTests())
1434 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
1435
chaocc1204522017-06-30 17:14:30 -07001436 if (glslangIntermediate->getPostDepthCoverage()) {
1437 builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage);
1438 builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage);
1439 builder.addExtension(spv::E_SPV_KHR_post_depth_coverage);
1440 }
1441
John Kesseniche6903322015-10-13 16:29:02 -06001442 switch(glslangIntermediate->getDepth()) {
John Kesseniche6903322015-10-13 16:29:02 -06001443 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
1444 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
John Kessenich4016e382016-07-15 11:53:56 -06001445 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001446 }
John Kessenich4016e382016-07-15 11:53:56 -06001447 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001448 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1449
1450 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
1451 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
John Kessenich140f3df2015-06-26 16:58:36 -06001452 break;
1453
1454 case EShLangCompute:
John Kessenich5e4b1242015-08-06 22:53:06 -06001455 builder.addCapability(spv::CapabilityShader);
John Kessenichb56a26a2015-09-16 16:04:05 -06001456 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1457 glslangIntermediate->getLocalSize(1),
1458 glslangIntermediate->getLocalSize(2));
Chao Chenbeae2252018-09-19 11:40:45 -07001459#ifdef NV_EXTENSIONS
1460 if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) {
1461 builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV);
1462 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV);
1463 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1464 } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) {
1465 builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV);
1466 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV);
1467 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1468 }
1469#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001470 break;
1471
Chao Chen3c366992018-09-19 11:41:59 -07001472#ifdef NV_EXTENSIONS
Chao Chenb50c02e2018-09-19 11:42:24 -07001473 case EShLangRayGenNV:
1474 case EShLangIntersectNV:
1475 case EShLangAnyHitNV:
1476 case EShLangClosestHitNV:
1477 case EShLangMissNV:
1478 case EShLangCallableNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07001479 builder.addCapability(spv::CapabilityRayTracingNV);
1480 builder.addExtension("SPV_NV_ray_tracing");
Chao Chenb50c02e2018-09-19 11:42:24 -07001481 break;
Chao Chen3c366992018-09-19 11:41:59 -07001482 case EShLangTaskNV:
1483 case EShLangMeshNV:
1484 builder.addCapability(spv::CapabilityMeshShadingNV);
1485 builder.addExtension(spv::E_SPV_NV_mesh_shader);
1486 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1487 glslangIntermediate->getLocalSize(1),
1488 glslangIntermediate->getLocalSize(2));
1489 if (glslangIntermediate->getStage() == EShLangMeshNV) {
1490 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1491 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives());
1492
1493 switch (glslangIntermediate->getOutputPrimitive()) {
1494 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1495 case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break;
1496 case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break;
1497 default: mode = spv::ExecutionModeMax; break;
1498 }
1499 if (mode != spv::ExecutionModeMax)
1500 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1501 }
1502 break;
1503#endif
1504
John Kessenich140f3df2015-06-26 16:58:36 -06001505 default:
1506 break;
1507 }
John Kessenich140f3df2015-06-26 16:58:36 -06001508}
1509
John Kessenichfca82622016-11-26 13:23:20 -07001510// Finish creating SPV, after the traversal is complete.
1511void TGlslangToSpvTraverser::finishSpv()
John Kessenich7ba63412015-12-20 17:37:07 -07001512{
John Kessenichf04c51b2018-08-03 15:56:12 -06001513 // Finish the entry point function
John Kessenich517fe7a2016-11-26 13:31:47 -07001514 if (! entryPointTerminated) {
John Kessenichfca82622016-11-26 13:23:20 -07001515 builder.setBuildPoint(shaderEntry->getLastBlock());
1516 builder.leaveFunction();
1517 }
1518
John Kessenich7ba63412015-12-20 17:37:07 -07001519 // finish off the entry-point SPV instruction by adding the Input/Output <id>
rdb32084e82016-02-23 22:17:38 +01001520 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
1521 entryPoint->addIdOperand(*it);
John Kessenich7ba63412015-12-20 17:37:07 -07001522
John Kessenichf04c51b2018-08-03 15:56:12 -06001523 // Add capabilities, extensions, remove unneeded decorations, etc.,
1524 // based on the resulting SPIR-V.
1525 builder.postProcess();
John Kessenich7ba63412015-12-20 17:37:07 -07001526}
1527
John Kessenichfca82622016-11-26 13:23:20 -07001528// Write the SPV into 'out'.
1529void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
John Kessenich140f3df2015-06-26 16:58:36 -06001530{
John Kessenichfca82622016-11-26 13:23:20 -07001531 builder.dump(out);
John Kessenich140f3df2015-06-26 16:58:36 -06001532}
1533
1534//
1535// Implement the traversal functions.
1536//
1537// Return true from interior nodes to have the external traversal
1538// continue on to children. Return false if children were
1539// already processed.
1540//
1541
1542//
qining25262b32016-05-06 17:25:16 -04001543// Symbols can turn into
John Kessenich140f3df2015-06-26 16:58:36 -06001544// - uniform/input reads
1545// - output writes
1546// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
1547// - something simple that degenerates into the last bullet
1548//
1549void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
1550{
qining75d1d802016-04-06 14:42:01 -04001551 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1552 if (symbol->getType().getQualifier().isSpecConstant())
1553 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1554
John Kessenich140f3df2015-06-26 16:58:36 -06001555 // getSymbolId() will set up all the IO decorations on the first call.
1556 // Formal function parameters were mapped during makeFunctions().
1557 spv::Id id = getSymbolId(symbol);
John Kessenich7ba63412015-12-20 17:37:07 -07001558
1559 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
1560 if (builder.isPointer(id)) {
1561 spv::StorageClass sc = builder.getStorageClass(id);
John Kessenich5f77d862017-09-19 11:09:59 -06001562 if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) {
1563 if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0)
1564 iOSet.insert(id);
1565 }
John Kessenich7ba63412015-12-20 17:37:07 -07001566 }
1567
1568 // Only process non-linkage-only nodes for generating actual static uses
John Kessenich6c292d32016-02-15 20:58:50 -07001569 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001570 // Prepare to generate code for the access
1571
1572 // L-value chains will be computed left to right. We're on the symbol now,
1573 // which is the left-most part of the access chain, so now is "clear" time,
1574 // followed by setting the base.
1575 builder.clearAccessChain();
1576
1577 // For now, we consider all user variables as being in memory, so they are pointers,
John Kessenich6c292d32016-02-15 20:58:50 -07001578 // except for
John Kessenich4bf71552016-09-02 11:20:21 -06001579 // A) R-Value arguments to a function, which are an intermediate object.
John Kessenich6c292d32016-02-15 20:58:50 -07001580 // See comments in handleUserFunctionCall().
John Kessenich4bf71552016-09-02 11:20:21 -06001581 // B) Specialization constants (normal constants don't even come in as a variable),
John Kessenich6c292d32016-02-15 20:58:50 -07001582 // These are also pure R-values.
1583 glslang::TQualifier qualifier = symbol->getQualifier();
John Kessenich4bf71552016-09-02 11:20:21 -06001584 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end())
John Kessenich140f3df2015-06-26 16:58:36 -06001585 builder.setAccessChainRValue(id);
1586 else
1587 builder.setAccessChainLValue(id);
1588 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001589
1590 // Process linkage-only nodes for any special additional interface work.
1591 if (linkageOnly) {
1592 if (glslangIntermediate->getHlslFunctionality1()) {
1593 // Map implicit counter buffers to their originating buffers, which should have been
1594 // seen by now, given earlier pruning of unused counters, and preservation of order
1595 // of declaration.
1596 if (symbol->getType().getQualifier().isUniformOrBuffer()) {
1597 if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) {
1598 // Save possible originating buffers for counter buffers, keyed by
1599 // making the potential counter-buffer name.
1600 std::string keyName = symbol->getName().c_str();
1601 keyName = glslangIntermediate->addCounterBufferName(keyName);
1602 counterOriginator[keyName] = symbol;
1603 } else {
1604 // Handle a counter buffer, by finding the saved originating buffer.
1605 std::string keyName = symbol->getName().c_str();
1606 auto it = counterOriginator.find(keyName);
1607 if (it != counterOriginator.end()) {
1608 id = getSymbolId(it->second);
1609 if (id != spv::NoResult) {
1610 spv::Id counterId = getSymbolId(symbol);
John Kessenichf52b6382018-04-05 19:35:38 -06001611 if (counterId != spv::NoResult) {
1612 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
John Kessenich5d610ee2018-03-07 18:05:55 -07001613 builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId);
John Kessenichf52b6382018-04-05 19:35:38 -06001614 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001615 }
1616 }
1617 }
1618 }
1619 }
1620 }
John Kessenich140f3df2015-06-26 16:58:36 -06001621}
1622
1623bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1624{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07001625 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06001626
qining40887662016-04-03 22:20:42 -04001627 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1628 if (node->getType().getQualifier().isSpecConstant())
1629 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1630
John Kessenich140f3df2015-06-26 16:58:36 -06001631 // First, handle special cases
1632 switch (node->getOp()) {
1633 case glslang::EOpAssign:
1634 case glslang::EOpAddAssign:
1635 case glslang::EOpSubAssign:
1636 case glslang::EOpMulAssign:
1637 case glslang::EOpVectorTimesMatrixAssign:
1638 case glslang::EOpVectorTimesScalarAssign:
1639 case glslang::EOpMatrixTimesScalarAssign:
1640 case glslang::EOpMatrixTimesMatrixAssign:
1641 case glslang::EOpDivAssign:
1642 case glslang::EOpModAssign:
1643 case glslang::EOpAndAssign:
1644 case glslang::EOpInclusiveOrAssign:
1645 case glslang::EOpExclusiveOrAssign:
1646 case glslang::EOpLeftShiftAssign:
1647 case glslang::EOpRightShiftAssign:
1648 // A bin-op assign "a += b" means the same thing as "a = a + b"
1649 // where a is evaluated before b. For a simple assignment, GLSL
1650 // says to evaluate the left before the right. So, always, left
1651 // node then right node.
1652 {
1653 // get the left l-value, save it away
1654 builder.clearAccessChain();
1655 node->getLeft()->traverse(this);
1656 spv::Builder::AccessChain lValue = builder.getAccessChain();
1657
1658 // evaluate the right
1659 builder.clearAccessChain();
1660 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001661 spv::Id rValue = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001662
1663 if (node->getOp() != glslang::EOpAssign) {
1664 // the left is also an r-value
1665 builder.setAccessChain(lValue);
John Kessenich32cfd492016-02-02 12:37:46 -07001666 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001667
1668 // do the operation
John Kessenichead86222018-03-28 18:01:20 -06001669 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001670 TranslateNoContractionDecoration(node->getType().getQualifier()),
1671 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06001672 rValue = createBinaryOperation(node->getOp(), decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06001673 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1674 node->getType().getBasicType());
1675
1676 // these all need their counterparts in createBinaryOperation()
John Kessenich55e7d112015-11-15 21:33:39 -07001677 assert(rValue != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001678 }
1679
1680 // store the result
1681 builder.setAccessChain(lValue);
Jeff Bolz36831c92018-09-05 10:11:41 -05001682 multiTypeStore(node->getLeft()->getType(), rValue);
John Kessenich140f3df2015-06-26 16:58:36 -06001683
1684 // assignments are expressions having an rValue after they are evaluated...
1685 builder.clearAccessChain();
1686 builder.setAccessChainRValue(rValue);
1687 }
1688 return false;
1689 case glslang::EOpIndexDirect:
1690 case glslang::EOpIndexDirectStruct:
1691 {
1692 // Get the left part of the access chain.
1693 node->getLeft()->traverse(this);
1694
1695 // Add the next element in the chain
1696
David Netoa901ffe2016-06-08 14:11:40 +01001697 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
John Kessenich140f3df2015-06-26 16:58:36 -06001698 if (! node->getLeft()->getType().isArray() &&
1699 node->getLeft()->getType().isVector() &&
1700 node->getOp() == glslang::EOpIndexDirect) {
1701 // This is essentially a hard-coded vector swizzle of size 1,
1702 // so short circuit the access-chain stuff with a swizzle.
1703 std::vector<unsigned> swizzle;
David Netoa901ffe2016-06-08 14:11:40 +01001704 swizzle.push_back(glslangIndex);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001705 int dummySize;
1706 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1707 TranslateCoherent(node->getLeft()->getType()),
1708 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001709 } else {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001710
1711 // Load through a block reference is performed with a dot operator that
1712 // is mapped to EOpIndexDirectStruct. When we get to the actual reference,
1713 // do a load and reset the access chain.
1714 if (node->getLeft()->getBasicType() == glslang::EbtReference &&
1715 !node->getLeft()->getType().isArray() &&
1716 node->getOp() == glslang::EOpIndexDirectStruct)
1717 {
1718 spv::Id left = accessChainLoad(node->getLeft()->getType());
1719 builder.clearAccessChain();
1720 builder.setAccessChainLValue(left);
1721 }
1722
David Netoa901ffe2016-06-08 14:11:40 +01001723 int spvIndex = glslangIndex;
1724 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1725 node->getOp() == glslang::EOpIndexDirectStruct)
1726 {
1727 // This may be, e.g., an anonymous block-member selection, which generally need
1728 // index remapping due to hidden members in anonymous blocks.
1729 std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()];
1730 assert(remapper.size() > 0);
1731 spvIndex = remapper[glslangIndex];
1732 }
John Kessenichebb50532016-05-16 19:22:05 -06001733
David Netoa901ffe2016-06-08 14:11:40 +01001734 // normal case for indexing array or structure or block
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001735 builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType()));
David Netoa901ffe2016-06-08 14:11:40 +01001736
1737 // Add capabilities here for accessing PointSize and clip/cull distance.
1738 // We have deferred generation of associated capabilities until now.
John Kessenichebb50532016-05-16 19:22:05 -06001739 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
David Netoa901ffe2016-06-08 14:11:40 +01001740 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
John Kessenich140f3df2015-06-26 16:58:36 -06001741 }
1742 }
1743 return false;
1744 case glslang::EOpIndexIndirect:
1745 {
1746 // Structure or array or vector indirection.
1747 // Will use native SPIR-V access-chain for struct and array indirection;
1748 // matrices are arrays of vectors, so will also work for a matrix.
1749 // Will use the access chain's 'component' for variable index into a vector.
1750
1751 // This adapter is building access chains left to right.
1752 // Set up the access chain to the left.
1753 node->getLeft()->traverse(this);
1754
1755 // save it so that computing the right side doesn't trash it
1756 spv::Builder::AccessChain partial = builder.getAccessChain();
1757
1758 // compute the next index in the chain
1759 builder.clearAccessChain();
1760 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001761 spv::Id index = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001762
John Kessenich5611c6d2018-04-05 11:25:02 -06001763 addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType());
1764
John Kessenich140f3df2015-06-26 16:58:36 -06001765 // restore the saved access chain
1766 builder.setAccessChain(partial);
1767
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001768 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) {
1769 int dummySize;
1770 builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()),
1771 TranslateCoherent(node->getLeft()->getType()),
1772 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
1773 } else
1774 builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06001775 }
1776 return false;
1777 case glslang::EOpVectorSwizzle:
1778 {
1779 node->getLeft()->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06001780 std::vector<unsigned> swizzle;
John Kessenich8c8505c2016-07-26 12:50:38 -06001781 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001782 int dummySize;
1783 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1784 TranslateCoherent(node->getLeft()->getType()),
1785 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001786 }
1787 return false;
John Kessenichfdf63472017-01-13 12:27:52 -07001788 case glslang::EOpMatrixSwizzle:
1789 logger->missingFunctionality("matrix swizzle");
1790 return true;
John Kessenich7c1aa102015-10-15 13:29:11 -06001791 case glslang::EOpLogicalOr:
1792 case glslang::EOpLogicalAnd:
1793 {
1794
1795 // These may require short circuiting, but can sometimes be done as straight
1796 // binary operations. The right operand must be short circuited if it has
1797 // side effects, and should probably be if it is complex.
1798 if (isTrivial(node->getRight()->getAsTyped()))
1799 break; // handle below as a normal binary operation
1800 // otherwise, we need to do dynamic short circuiting on the right operand
1801 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped());
1802 builder.clearAccessChain();
1803 builder.setAccessChainRValue(result);
1804 }
1805 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06001806 default:
1807 break;
1808 }
1809
1810 // Assume generic binary op...
1811
John Kessenich32cfd492016-02-02 12:37:46 -07001812 // get right operand
John Kessenich140f3df2015-06-26 16:58:36 -06001813 builder.clearAccessChain();
1814 node->getLeft()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001815 spv::Id left = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001816
John Kessenich32cfd492016-02-02 12:37:46 -07001817 // get left operand
John Kessenich140f3df2015-06-26 16:58:36 -06001818 builder.clearAccessChain();
1819 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001820 spv::Id right = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001821
John Kessenich32cfd492016-02-02 12:37:46 -07001822 // get result
John Kessenichead86222018-03-28 18:01:20 -06001823 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001824 TranslateNoContractionDecoration(node->getType().getQualifier()),
1825 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06001826 spv::Id result = createBinaryOperation(node->getOp(), decorations,
John Kessenich32cfd492016-02-02 12:37:46 -07001827 convertGlslangToSpvType(node->getType()), left, right,
1828 node->getLeft()->getType().getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001829
John Kessenich50e57562015-12-21 21:21:11 -07001830 builder.clearAccessChain();
John Kessenich140f3df2015-06-26 16:58:36 -06001831 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04001832 logger->missingFunctionality("unknown glslang binary operation");
John Kessenich50e57562015-12-21 21:21:11 -07001833 return true; // pick up a child as the place-holder result
John Kessenich140f3df2015-06-26 16:58:36 -06001834 } else {
John Kessenich140f3df2015-06-26 16:58:36 -06001835 builder.setAccessChainRValue(result);
John Kessenich140f3df2015-06-26 16:58:36 -06001836 return false;
1837 }
John Kessenich140f3df2015-06-26 16:58:36 -06001838}
1839
1840bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
1841{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07001842 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06001843
qining40887662016-04-03 22:20:42 -04001844 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1845 if (node->getType().getQualifier().isSpecConstant())
1846 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1847
John Kessenichfc51d282015-08-19 13:34:18 -06001848 spv::Id result = spv::NoResult;
1849
1850 // try texturing first
1851 result = createImageTextureFunctionCall(node);
1852 if (result != spv::NoResult) {
1853 builder.clearAccessChain();
1854 builder.setAccessChainRValue(result);
1855
1856 return false; // done with this node
1857 }
1858
1859 // Non-texturing.
John Kessenichc9a80832015-09-12 12:17:44 -06001860
1861 if (node->getOp() == glslang::EOpArrayLength) {
1862 // Quite special; won't want to evaluate the operand.
1863
John Kessenich5611c6d2018-04-05 11:25:02 -06001864 // Currently, the front-end does not allow .length() on an array until it is sized,
1865 // except for the last block membeor of an SSBO.
1866 // TODO: If this changes, link-time sized arrays might show up here, and need their
1867 // size extracted.
1868
John Kessenichc9a80832015-09-12 12:17:44 -06001869 // Normal .length() would have been constant folded by the front-end.
1870 // So, this has to be block.lastMember.length().
John Kessenichee21fc92015-09-21 21:50:29 -06001871 // SPV wants "block" and member number as the operands, go get them.
John Kessenichead86222018-03-28 18:01:20 -06001872
John Kessenichc9a80832015-09-12 12:17:44 -06001873 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
1874 block->traverse(this);
John Kessenichee21fc92015-09-21 21:50:29 -06001875 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst();
1876 spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member);
John Kessenichc9a80832015-09-12 12:17:44 -06001877
John Kessenich8c869672018-11-28 07:01:37 -07001878 // GLSL semantics say the result of .length() is an int, while SPIR-V says
1879 // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's
1880 // AST expectation of a signed result.
1881 if (glslangIntermediate->getSource() == glslang::EShSourceGlsl)
1882 length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length);
1883
John Kessenichc9a80832015-09-12 12:17:44 -06001884 builder.clearAccessChain();
1885 builder.setAccessChainRValue(length);
1886
1887 return false;
1888 }
1889
John Kessenichfc51d282015-08-19 13:34:18 -06001890 // Start by evaluating the operand
1891
John Kessenich8c8505c2016-07-26 12:50:38 -06001892 // Does it need a swizzle inversion? If so, evaluation is inverted;
1893 // operate first on the swizzle base, then apply the swizzle.
1894 spv::Id invertedType = spv::NoType;
1895 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
1896 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
1897 invertedType = getInvertedSwizzleType(*node->getOperand());
1898
John Kessenich140f3df2015-06-26 16:58:36 -06001899 builder.clearAccessChain();
John Kessenich8c8505c2016-07-26 12:50:38 -06001900 if (invertedType != spv::NoType)
1901 node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this);
1902 else
1903 node->getOperand()->traverse(this);
Rex Xu30f92582015-09-14 10:38:56 +08001904
Rex Xufc618912015-09-09 16:42:49 +08001905 spv::Id operand = spv::NoResult;
1906
1907 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
1908 node->getOp() == glslang::EOpAtomicCounterDecrement ||
Rex Xu7a26c172015-12-08 17:12:09 +08001909 node->getOp() == glslang::EOpAtomicCounter ||
1910 node->getOp() == glslang::EOpInterpolateAtCentroid)
Rex Xufc618912015-09-09 16:42:49 +08001911 operand = builder.accessChainGetLValue(); // Special case l-value operands
1912 else
John Kessenich32cfd492016-02-02 12:37:46 -07001913 operand = accessChainLoad(node->getOperand()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001914
John Kessenichead86222018-03-28 18:01:20 -06001915 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001916 TranslateNoContractionDecoration(node->getType().getQualifier()),
1917 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenich140f3df2015-06-26 16:58:36 -06001918
1919 // it could be a conversion
John Kessenichfc51d282015-08-19 13:34:18 -06001920 if (! result)
John Kessenichead86222018-03-28 18:01:20 -06001921 result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001922
1923 // if not, then possibly an operation
1924 if (! result)
John Kessenichead86222018-03-28 18:01:20 -06001925 result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001926
1927 if (result) {
John Kessenich5611c6d2018-04-05 11:25:02 -06001928 if (invertedType) {
John Kessenichead86222018-03-28 18:01:20 -06001929 result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result);
John Kessenich5611c6d2018-04-05 11:25:02 -06001930 builder.addDecoration(result, decorations.nonUniform);
1931 }
John Kessenich8c8505c2016-07-26 12:50:38 -06001932
John Kessenich140f3df2015-06-26 16:58:36 -06001933 builder.clearAccessChain();
1934 builder.setAccessChainRValue(result);
1935
1936 return false; // done with this node
1937 }
1938
1939 // it must be a special case, check...
1940 switch (node->getOp()) {
1941 case glslang::EOpPostIncrement:
1942 case glslang::EOpPostDecrement:
1943 case glslang::EOpPreIncrement:
1944 case glslang::EOpPreDecrement:
1945 {
1946 // we need the integer value "1" or the floating point "1.0" to add/subtract
Rex Xu8ff43de2016-04-22 16:51:45 +08001947 spv::Id one = 0;
1948 if (node->getBasicType() == glslang::EbtFloat)
1949 one = builder.makeFloatConstant(1.0F);
Rex Xuce31aea2016-07-29 16:13:04 +08001950 else if (node->getBasicType() == glslang::EbtDouble)
1951 one = builder.makeDoubleConstant(1.0);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08001952 else if (node->getBasicType() == glslang::EbtFloat16)
1953 one = builder.makeFloat16Constant(1.0F);
John Kessenich66011cb2018-03-06 16:12:04 -07001954 else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8)
1955 one = builder.makeInt8Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08001956 else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16)
1957 one = builder.makeInt16Constant(1);
John Kessenich66011cb2018-03-06 16:12:04 -07001958 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
1959 one = builder.makeInt64Constant(1);
Rex Xu8ff43de2016-04-22 16:51:45 +08001960 else
1961 one = builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06001962 glslang::TOperator op;
1963 if (node->getOp() == glslang::EOpPreIncrement ||
1964 node->getOp() == glslang::EOpPostIncrement)
1965 op = glslang::EOpAdd;
1966 else
1967 op = glslang::EOpSub;
1968
John Kessenichead86222018-03-28 18:01:20 -06001969 spv::Id result = createBinaryOperation(op, decorations,
Rex Xu8ff43de2016-04-22 16:51:45 +08001970 convertGlslangToSpvType(node->getType()), operand, one,
1971 node->getType().getBasicType());
John Kessenich55e7d112015-11-15 21:33:39 -07001972 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001973
1974 // The result of operation is always stored, but conditionally the
1975 // consumed result. The consumed result is always an r-value.
1976 builder.accessChainStore(result);
1977 builder.clearAccessChain();
1978 if (node->getOp() == glslang::EOpPreIncrement ||
1979 node->getOp() == glslang::EOpPreDecrement)
1980 builder.setAccessChainRValue(result);
1981 else
1982 builder.setAccessChainRValue(operand);
1983 }
1984
1985 return false;
1986
1987 case glslang::EOpEmitStreamVertex:
1988 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
1989 return false;
1990 case glslang::EOpEndStreamPrimitive:
1991 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
1992 return false;
1993
1994 default:
Lei Zhang17535f72016-05-04 15:55:59 -04001995 logger->missingFunctionality("unknown glslang unary");
John Kessenich50e57562015-12-21 21:21:11 -07001996 return true; // pick up operand as placeholder result
John Kessenich140f3df2015-06-26 16:58:36 -06001997 }
John Kessenich140f3df2015-06-26 16:58:36 -06001998}
1999
2000bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
2001{
qining27e04a02016-04-14 16:40:20 -04002002 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2003 if (node->getType().getQualifier().isSpecConstant())
2004 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2005
John Kessenichfc51d282015-08-19 13:34:18 -06002006 spv::Id result = spv::NoResult;
John Kessenich8c8505c2016-07-26 12:50:38 -06002007 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
2008 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
John Kessenichfc51d282015-08-19 13:34:18 -06002009
2010 // try texturing
2011 result = createImageTextureFunctionCall(node);
2012 if (result != spv::NoResult) {
2013 builder.clearAccessChain();
2014 builder.setAccessChainRValue(result);
2015
2016 return false;
Jeff Bolz36831c92018-09-05 10:11:41 -05002017 } else if (node->getOp() == glslang::EOpImageStore ||
Rex Xu129799a2017-07-05 17:23:28 +08002018#ifdef AMD_EXTENSIONS
Jeff Bolz36831c92018-09-05 10:11:41 -05002019 node->getOp() == glslang::EOpImageStoreLod ||
Rex Xu129799a2017-07-05 17:23:28 +08002020#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05002021 node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufc618912015-09-09 16:42:49 +08002022 // "imageStore" is a special case, which has no result
2023 return false;
2024 }
John Kessenichfc51d282015-08-19 13:34:18 -06002025
John Kessenich140f3df2015-06-26 16:58:36 -06002026 glslang::TOperator binOp = glslang::EOpNull;
2027 bool reduceComparison = true;
2028 bool isMatrix = false;
2029 bool noReturnValue = false;
John Kessenich426394d2015-07-23 10:22:48 -06002030 bool atomic = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002031
2032 assert(node->getOp());
2033
John Kessenichf6640762016-08-01 19:44:00 -06002034 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenich140f3df2015-06-26 16:58:36 -06002035
2036 switch (node->getOp()) {
2037 case glslang::EOpSequence:
2038 {
2039 if (preVisit)
2040 ++sequenceDepth;
2041 else
2042 --sequenceDepth;
2043
2044 if (sequenceDepth == 1) {
2045 // If this is the parent node of all the functions, we want to see them
2046 // early, so all call points have actual SPIR-V functions to reference.
2047 // In all cases, still let the traverser visit the children for us.
2048 makeFunctions(node->getAsAggregate()->getSequence());
2049
John Kessenich6fccb3c2016-09-19 16:01:41 -06002050 // Also, we want all globals initializers to go into the beginning of the entry point, before
John Kessenich140f3df2015-06-26 16:58:36 -06002051 // anything else gets there, so visit out of order, doing them all now.
2052 makeGlobalInitializers(node->getAsAggregate()->getSequence());
2053
John Kessenich6a60c2f2016-12-08 21:01:59 -07002054 // 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 -06002055 // so do them manually.
2056 visitFunctions(node->getAsAggregate()->getSequence());
2057
2058 return false;
2059 }
2060
2061 return true;
2062 }
2063 case glslang::EOpLinkerObjects:
2064 {
2065 if (visit == glslang::EvPreVisit)
2066 linkageOnly = true;
2067 else
2068 linkageOnly = false;
2069
2070 return true;
2071 }
2072 case glslang::EOpComma:
2073 {
2074 // processing from left to right naturally leaves the right-most
2075 // lying around in the access chain
2076 glslang::TIntermSequence& glslangOperands = node->getSequence();
2077 for (int i = 0; i < (int)glslangOperands.size(); ++i)
2078 glslangOperands[i]->traverse(this);
2079
2080 return false;
2081 }
2082 case glslang::EOpFunction:
2083 if (visit == glslang::EvPreVisit) {
John Kessenich6fccb3c2016-09-19 16:01:41 -06002084 if (isShaderEntryPoint(node)) {
John Kessenich517fe7a2016-11-26 13:31:47 -07002085 inEntryPoint = true;
John Kessenich140f3df2015-06-26 16:58:36 -06002086 builder.setBuildPoint(shaderEntry->getLastBlock());
John Kesseniched33e052016-10-06 12:59:51 -06002087 currentFunction = shaderEntry;
John Kessenich140f3df2015-06-26 16:58:36 -06002088 } else {
2089 handleFunctionEntry(node);
2090 }
2091 } else {
John Kessenich517fe7a2016-11-26 13:31:47 -07002092 if (inEntryPoint)
2093 entryPointTerminated = true;
John Kesseniche770b3e2015-09-14 20:58:02 -06002094 builder.leaveFunction();
John Kessenich517fe7a2016-11-26 13:31:47 -07002095 inEntryPoint = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002096 }
2097
2098 return true;
2099 case glslang::EOpParameters:
2100 // Parameters will have been consumed by EOpFunction processing, but not
2101 // the body, so we still visited the function node's children, making this
2102 // child redundant.
2103 return false;
2104 case glslang::EOpFunctionCall:
2105 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002106 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002107 if (node->isUserDefined())
2108 result = handleUserFunctionCall(node);
John Kessenich927608b2017-01-06 12:34:14 -07002109 // assert(result); // this can happen for bad shaders because the call graph completeness checking is not yet done
John Kessenich6c292d32016-02-15 20:58:50 -07002110 if (result) {
2111 builder.clearAccessChain();
2112 builder.setAccessChainRValue(result);
2113 } else
Lei Zhang17535f72016-05-04 15:55:59 -04002114 logger->missingFunctionality("missing user function; linker needs to catch that");
John Kessenich140f3df2015-06-26 16:58:36 -06002115
2116 return false;
2117 }
2118 case glslang::EOpConstructMat2x2:
2119 case glslang::EOpConstructMat2x3:
2120 case glslang::EOpConstructMat2x4:
2121 case glslang::EOpConstructMat3x2:
2122 case glslang::EOpConstructMat3x3:
2123 case glslang::EOpConstructMat3x4:
2124 case glslang::EOpConstructMat4x2:
2125 case glslang::EOpConstructMat4x3:
2126 case glslang::EOpConstructMat4x4:
2127 case glslang::EOpConstructDMat2x2:
2128 case glslang::EOpConstructDMat2x3:
2129 case glslang::EOpConstructDMat2x4:
2130 case glslang::EOpConstructDMat3x2:
2131 case glslang::EOpConstructDMat3x3:
2132 case glslang::EOpConstructDMat3x4:
2133 case glslang::EOpConstructDMat4x2:
2134 case glslang::EOpConstructDMat4x3:
2135 case glslang::EOpConstructDMat4x4:
LoopDawg174ccb82017-05-20 21:40:27 -06002136 case glslang::EOpConstructIMat2x2:
2137 case glslang::EOpConstructIMat2x3:
2138 case glslang::EOpConstructIMat2x4:
2139 case glslang::EOpConstructIMat3x2:
2140 case glslang::EOpConstructIMat3x3:
2141 case glslang::EOpConstructIMat3x4:
2142 case glslang::EOpConstructIMat4x2:
2143 case glslang::EOpConstructIMat4x3:
2144 case glslang::EOpConstructIMat4x4:
2145 case glslang::EOpConstructUMat2x2:
2146 case glslang::EOpConstructUMat2x3:
2147 case glslang::EOpConstructUMat2x4:
2148 case glslang::EOpConstructUMat3x2:
2149 case glslang::EOpConstructUMat3x3:
2150 case glslang::EOpConstructUMat3x4:
2151 case glslang::EOpConstructUMat4x2:
2152 case glslang::EOpConstructUMat4x3:
2153 case glslang::EOpConstructUMat4x4:
2154 case glslang::EOpConstructBMat2x2:
2155 case glslang::EOpConstructBMat2x3:
2156 case glslang::EOpConstructBMat2x4:
2157 case glslang::EOpConstructBMat3x2:
2158 case glslang::EOpConstructBMat3x3:
2159 case glslang::EOpConstructBMat3x4:
2160 case glslang::EOpConstructBMat4x2:
2161 case glslang::EOpConstructBMat4x3:
2162 case glslang::EOpConstructBMat4x4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002163 case glslang::EOpConstructF16Mat2x2:
2164 case glslang::EOpConstructF16Mat2x3:
2165 case glslang::EOpConstructF16Mat2x4:
2166 case glslang::EOpConstructF16Mat3x2:
2167 case glslang::EOpConstructF16Mat3x3:
2168 case glslang::EOpConstructF16Mat3x4:
2169 case glslang::EOpConstructF16Mat4x2:
2170 case glslang::EOpConstructF16Mat4x3:
2171 case glslang::EOpConstructF16Mat4x4:
John Kessenich140f3df2015-06-26 16:58:36 -06002172 isMatrix = true;
2173 // fall through
2174 case glslang::EOpConstructFloat:
2175 case glslang::EOpConstructVec2:
2176 case glslang::EOpConstructVec3:
2177 case glslang::EOpConstructVec4:
2178 case glslang::EOpConstructDouble:
2179 case glslang::EOpConstructDVec2:
2180 case glslang::EOpConstructDVec3:
2181 case glslang::EOpConstructDVec4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002182 case glslang::EOpConstructFloat16:
2183 case glslang::EOpConstructF16Vec2:
2184 case glslang::EOpConstructF16Vec3:
2185 case glslang::EOpConstructF16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002186 case glslang::EOpConstructBool:
2187 case glslang::EOpConstructBVec2:
2188 case glslang::EOpConstructBVec3:
2189 case glslang::EOpConstructBVec4:
John Kessenich66011cb2018-03-06 16:12:04 -07002190 case glslang::EOpConstructInt8:
2191 case glslang::EOpConstructI8Vec2:
2192 case glslang::EOpConstructI8Vec3:
2193 case glslang::EOpConstructI8Vec4:
2194 case glslang::EOpConstructUint8:
2195 case glslang::EOpConstructU8Vec2:
2196 case glslang::EOpConstructU8Vec3:
2197 case glslang::EOpConstructU8Vec4:
2198 case glslang::EOpConstructInt16:
2199 case glslang::EOpConstructI16Vec2:
2200 case glslang::EOpConstructI16Vec3:
2201 case glslang::EOpConstructI16Vec4:
2202 case glslang::EOpConstructUint16:
2203 case glslang::EOpConstructU16Vec2:
2204 case glslang::EOpConstructU16Vec3:
2205 case glslang::EOpConstructU16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002206 case glslang::EOpConstructInt:
2207 case glslang::EOpConstructIVec2:
2208 case glslang::EOpConstructIVec3:
2209 case glslang::EOpConstructIVec4:
2210 case glslang::EOpConstructUint:
2211 case glslang::EOpConstructUVec2:
2212 case glslang::EOpConstructUVec3:
2213 case glslang::EOpConstructUVec4:
Rex Xu8ff43de2016-04-22 16:51:45 +08002214 case glslang::EOpConstructInt64:
2215 case glslang::EOpConstructI64Vec2:
2216 case glslang::EOpConstructI64Vec3:
2217 case glslang::EOpConstructI64Vec4:
2218 case glslang::EOpConstructUint64:
2219 case glslang::EOpConstructU64Vec2:
2220 case glslang::EOpConstructU64Vec3:
2221 case glslang::EOpConstructU64Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002222 case glslang::EOpConstructStruct:
John Kessenich6c292d32016-02-15 20:58:50 -07002223 case glslang::EOpConstructTextureSampler:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002224 case glslang::EOpConstructReference:
John Kessenich140f3df2015-06-26 16:58:36 -06002225 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002226 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002227 std::vector<spv::Id> arguments;
Rex Xufc618912015-09-09 16:42:49 +08002228 translateArguments(*node, arguments);
John Kessenich140f3df2015-06-26 16:58:36 -06002229 spv::Id constructed;
John Kessenich6c292d32016-02-15 20:58:50 -07002230 if (node->getOp() == glslang::EOpConstructTextureSampler)
John Kessenich8c8505c2016-07-26 12:50:38 -06002231 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
John Kessenich6c292d32016-02-15 20:58:50 -07002232 else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002233 std::vector<spv::Id> constituents;
2234 for (int c = 0; c < (int)arguments.size(); ++c)
2235 constituents.push_back(arguments[c]);
John Kessenich8c8505c2016-07-26 12:50:38 -06002236 constructed = builder.createCompositeConstruct(resultType(), constituents);
John Kessenich55e7d112015-11-15 21:33:39 -07002237 } else if (isMatrix)
John Kessenich8c8505c2016-07-26 12:50:38 -06002238 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
John Kessenich55e7d112015-11-15 21:33:39 -07002239 else
John Kessenich8c8505c2016-07-26 12:50:38 -06002240 constructed = builder.createConstructor(precision, arguments, resultType());
John Kessenich140f3df2015-06-26 16:58:36 -06002241
2242 builder.clearAccessChain();
2243 builder.setAccessChainRValue(constructed);
2244
2245 return false;
2246 }
2247
2248 // These six are component-wise compares with component-wise results.
2249 // Forward on to createBinaryOperation(), requesting a vector result.
2250 case glslang::EOpLessThan:
2251 case glslang::EOpGreaterThan:
2252 case glslang::EOpLessThanEqual:
2253 case glslang::EOpGreaterThanEqual:
2254 case glslang::EOpVectorEqual:
2255 case glslang::EOpVectorNotEqual:
2256 {
2257 // Map the operation to a binary
2258 binOp = node->getOp();
2259 reduceComparison = false;
2260 switch (node->getOp()) {
2261 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
2262 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
2263 default: binOp = node->getOp(); break;
2264 }
2265
2266 break;
2267 }
2268 case glslang::EOpMul:
John Kessenich8c8505c2016-07-26 12:50:38 -06002269 // component-wise matrix multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002270 binOp = glslang::EOpMul;
2271 break;
2272 case glslang::EOpOuterProduct:
2273 // two vectors multiplied to make a matrix
2274 binOp = glslang::EOpOuterProduct;
2275 break;
2276 case glslang::EOpDot:
2277 {
qining25262b32016-05-06 17:25:16 -04002278 // for scalar dot product, use multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002279 glslang::TIntermSequence& glslangOperands = node->getSequence();
John Kessenich8d72f1a2016-05-20 12:06:03 -06002280 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
John Kessenich140f3df2015-06-26 16:58:36 -06002281 binOp = glslang::EOpMul;
2282 break;
2283 }
2284 case glslang::EOpMod:
2285 // when an aggregate, this is the floating-point mod built-in function,
2286 // which can be emitted by the one in createBinaryOperation()
2287 binOp = glslang::EOpMod;
2288 break;
John Kessenich140f3df2015-06-26 16:58:36 -06002289 case glslang::EOpEmitVertex:
2290 case glslang::EOpEndPrimitive:
2291 case glslang::EOpBarrier:
2292 case glslang::EOpMemoryBarrier:
2293 case glslang::EOpMemoryBarrierAtomicCounter:
2294 case glslang::EOpMemoryBarrierBuffer:
2295 case glslang::EOpMemoryBarrierImage:
2296 case glslang::EOpMemoryBarrierShared:
2297 case glslang::EOpGroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07002298 case glslang::EOpDeviceMemoryBarrier:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002299 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07002300 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002301 case glslang::EOpWorkgroupMemoryBarrier:
2302 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich66011cb2018-03-06 16:12:04 -07002303 case glslang::EOpSubgroupBarrier:
2304 case glslang::EOpSubgroupMemoryBarrier:
2305 case glslang::EOpSubgroupMemoryBarrierBuffer:
2306 case glslang::EOpSubgroupMemoryBarrierImage:
2307 case glslang::EOpSubgroupMemoryBarrierShared:
John Kessenich140f3df2015-06-26 16:58:36 -06002308 noReturnValue = true;
2309 // These all have 0 operands and will naturally finish up in the code below for 0 operands
2310 break;
2311
Jeff Bolz36831c92018-09-05 10:11:41 -05002312 case glslang::EOpAtomicStore:
2313 noReturnValue = true;
2314 // fallthrough
2315 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06002316 case glslang::EOpAtomicAdd:
2317 case glslang::EOpAtomicMin:
2318 case glslang::EOpAtomicMax:
2319 case glslang::EOpAtomicAnd:
2320 case glslang::EOpAtomicOr:
2321 case glslang::EOpAtomicXor:
2322 case glslang::EOpAtomicExchange:
2323 case glslang::EOpAtomicCompSwap:
2324 atomic = true;
2325 break;
2326
John Kessenich0d0c6d32017-07-23 16:08:26 -06002327 case glslang::EOpAtomicCounterAdd:
2328 case glslang::EOpAtomicCounterSubtract:
2329 case glslang::EOpAtomicCounterMin:
2330 case glslang::EOpAtomicCounterMax:
2331 case glslang::EOpAtomicCounterAnd:
2332 case glslang::EOpAtomicCounterOr:
2333 case glslang::EOpAtomicCounterXor:
2334 case glslang::EOpAtomicCounterExchange:
2335 case glslang::EOpAtomicCounterCompSwap:
2336 builder.addExtension("SPV_KHR_shader_atomic_counter_ops");
2337 builder.addCapability(spv::CapabilityAtomicStorageOps);
2338 atomic = true;
2339 break;
2340
Chao Chen3c366992018-09-19 11:41:59 -07002341#ifdef NV_EXTENSIONS
Chao Chenb50c02e2018-09-19 11:42:24 -07002342 case glslang::EOpIgnoreIntersectionNV:
2343 case glslang::EOpTerminateRayNV:
2344 case glslang::EOpTraceNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07002345 case glslang::EOpExecuteCallableNV:
Chao Chen3c366992018-09-19 11:41:59 -07002346 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
2347 noReturnValue = true;
2348 break;
2349#endif
2350
John Kessenich140f3df2015-06-26 16:58:36 -06002351 default:
2352 break;
2353 }
2354
2355 //
2356 // See if it maps to a regular operation.
2357 //
John Kessenich140f3df2015-06-26 16:58:36 -06002358 if (binOp != glslang::EOpNull) {
2359 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
2360 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
2361 assert(left && right);
2362
2363 builder.clearAccessChain();
2364 left->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002365 spv::Id leftId = accessChainLoad(left->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002366
2367 builder.clearAccessChain();
2368 right->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002369 spv::Id rightId = accessChainLoad(right->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002370
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002371 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichead86222018-03-28 18:01:20 -06002372 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002373 TranslateNoContractionDecoration(node->getType().getQualifier()),
2374 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002375 result = createBinaryOperation(binOp, decorations,
John Kessenich8c8505c2016-07-26 12:50:38 -06002376 resultType(), leftId, rightId,
John Kessenich140f3df2015-06-26 16:58:36 -06002377 left->getType().getBasicType(), reduceComparison);
2378
2379 // code above should only make binOp that exists in createBinaryOperation
John Kessenich55e7d112015-11-15 21:33:39 -07002380 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002381 builder.clearAccessChain();
2382 builder.setAccessChainRValue(result);
2383
2384 return false;
2385 }
2386
John Kessenich426394d2015-07-23 10:22:48 -06002387 //
2388 // Create the list of operands.
2389 //
John Kessenich140f3df2015-06-26 16:58:36 -06002390 glslang::TIntermSequence& glslangOperands = node->getSequence();
2391 std::vector<spv::Id> operands;
2392 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
John Kessenich140f3df2015-06-26 16:58:36 -06002393 // special case l-value operands; there are just a few
2394 bool lvalue = false;
2395 switch (node->getOp()) {
John Kessenich55e7d112015-11-15 21:33:39 -07002396 case glslang::EOpFrexp:
John Kessenich140f3df2015-06-26 16:58:36 -06002397 case glslang::EOpModf:
2398 if (arg == 1)
2399 lvalue = true;
2400 break;
Rex Xu7a26c172015-12-08 17:12:09 +08002401 case glslang::EOpInterpolateAtSample:
2402 case glslang::EOpInterpolateAtOffset:
Rex Xu9d93a232016-05-05 12:30:44 +08002403#ifdef AMD_EXTENSIONS
2404 case glslang::EOpInterpolateAtVertex:
2405#endif
John Kessenich8c8505c2016-07-26 12:50:38 -06002406 if (arg == 0) {
Rex Xu7a26c172015-12-08 17:12:09 +08002407 lvalue = true;
John Kessenich8c8505c2016-07-26 12:50:38 -06002408
2409 // Does it need a swizzle inversion? If so, evaluation is inverted;
2410 // operate first on the swizzle base, then apply the swizzle.
John Kessenichecba76f2017-01-06 00:34:48 -07002411 if (glslangOperands[0]->getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002412 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2413 invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
2414 }
Rex Xu7a26c172015-12-08 17:12:09 +08002415 break;
Rex Xud4782c12015-09-06 16:30:11 +08002416 case glslang::EOpAtomicAdd:
2417 case glslang::EOpAtomicMin:
2418 case glslang::EOpAtomicMax:
2419 case glslang::EOpAtomicAnd:
2420 case glslang::EOpAtomicOr:
2421 case glslang::EOpAtomicXor:
2422 case glslang::EOpAtomicExchange:
2423 case glslang::EOpAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05002424 case glslang::EOpAtomicLoad:
2425 case glslang::EOpAtomicStore:
John Kessenich0d0c6d32017-07-23 16:08:26 -06002426 case glslang::EOpAtomicCounterAdd:
2427 case glslang::EOpAtomicCounterSubtract:
2428 case glslang::EOpAtomicCounterMin:
2429 case glslang::EOpAtomicCounterMax:
2430 case glslang::EOpAtomicCounterAnd:
2431 case glslang::EOpAtomicCounterOr:
2432 case glslang::EOpAtomicCounterXor:
2433 case glslang::EOpAtomicCounterExchange:
2434 case glslang::EOpAtomicCounterCompSwap:
Rex Xud4782c12015-09-06 16:30:11 +08002435 if (arg == 0)
2436 lvalue = true;
2437 break;
John Kessenich55e7d112015-11-15 21:33:39 -07002438 case glslang::EOpAddCarry:
2439 case glslang::EOpSubBorrow:
2440 if (arg == 2)
2441 lvalue = true;
2442 break;
2443 case glslang::EOpUMulExtended:
2444 case glslang::EOpIMulExtended:
2445 if (arg >= 2)
2446 lvalue = true;
2447 break;
John Kessenich140f3df2015-06-26 16:58:36 -06002448 default:
2449 break;
2450 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002451 builder.clearAccessChain();
2452 if (invertedType != spv::NoType && arg == 0)
2453 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
2454 else
2455 glslangOperands[arg]->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06002456 if (lvalue)
2457 operands.push_back(builder.accessChainGetLValue());
John Kesseniche485c7a2017-05-31 18:50:53 -06002458 else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002459 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich32cfd492016-02-02 12:37:46 -07002460 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
John Kesseniche485c7a2017-05-31 18:50:53 -06002461 }
John Kessenich140f3df2015-06-26 16:58:36 -06002462 }
John Kessenich426394d2015-07-23 10:22:48 -06002463
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002464 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich426394d2015-07-23 10:22:48 -06002465 if (atomic) {
2466 // Handle all atomics
John Kessenich8c8505c2016-07-26 12:50:38 -06002467 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06002468 } else {
2469 // Pass through to generic operations.
2470 switch (glslangOperands.size()) {
2471 case 0:
John Kessenich8c8505c2016-07-26 12:50:38 -06002472 result = createNoArgOperation(node->getOp(), precision, resultType());
John Kessenich426394d2015-07-23 10:22:48 -06002473 break;
2474 case 1:
John Kessenichead86222018-03-28 18:01:20 -06002475 {
2476 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002477 TranslateNoContractionDecoration(node->getType().getQualifier()),
2478 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002479 result = createUnaryOperation(
2480 node->getOp(), decorations,
2481 resultType(), operands.front(),
2482 glslangOperands[0]->getAsTyped()->getBasicType());
2483 }
John Kessenich426394d2015-07-23 10:22:48 -06002484 break;
2485 default:
John Kessenich8c8505c2016-07-26 12:50:38 -06002486 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06002487 break;
2488 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002489 if (invertedType)
2490 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
John Kessenich140f3df2015-06-26 16:58:36 -06002491 }
2492
2493 if (noReturnValue)
2494 return false;
2495
2496 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04002497 logger->missingFunctionality("unknown glslang aggregate");
John Kessenich50e57562015-12-21 21:21:11 -07002498 return true; // pick up a child as a placeholder operand
John Kessenich140f3df2015-06-26 16:58:36 -06002499 } else {
2500 builder.clearAccessChain();
2501 builder.setAccessChainRValue(result);
2502 return false;
2503 }
2504}
2505
John Kessenich433e9ff2017-01-26 20:31:11 -07002506// This path handles both if-then-else and ?:
2507// The if-then-else has a node type of void, while
2508// ?: has either a void or a non-void node type
2509//
2510// Leaving the result, when not void:
2511// GLSL only has r-values as the result of a :?, but
2512// if we have an l-value, that can be more efficient if it will
2513// become the base of a complex r-value expression, because the
2514// next layer copies r-values into memory to use the access-chain mechanism
John Kessenich140f3df2015-06-26 16:58:36 -06002515bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
2516{
John Kessenich4bee5312018-02-20 21:29:05 -07002517 // See if it simple and safe, or required, to execute both sides.
2518 // Crucially, side effects must be either semantically required or avoided,
2519 // and there are performance trade-offs.
2520 // Return true if required or a good idea (and safe) to execute both sides,
2521 // false otherwise.
2522 const auto bothSidesPolicy = [&]() -> bool {
2523 // do we have both sides?
John Kessenich433e9ff2017-01-26 20:31:11 -07002524 if (node->getTrueBlock() == nullptr ||
2525 node->getFalseBlock() == nullptr)
2526 return false;
2527
John Kessenich4bee5312018-02-20 21:29:05 -07002528 // required? (unless we write additional code to look for side effects
2529 // and make performance trade-offs if none are present)
2530 if (!node->getShortCircuit())
2531 return true;
2532
2533 // if not required to execute both, decide based on performance/practicality...
2534
2535 // see if OpSelect can handle it
2536 if ((!node->getType().isScalar() && !node->getType().isVector()) ||
2537 node->getBasicType() == glslang::EbtVoid)
2538 return false;
2539
John Kessenich433e9ff2017-01-26 20:31:11 -07002540 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
2541 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
2542
2543 // return true if a single operand to ? : is okay for OpSelect
2544 const auto operandOkay = [](glslang::TIntermTyped* node) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07002545 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
John Kessenich433e9ff2017-01-26 20:31:11 -07002546 };
2547
2548 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
2549 operandOkay(node->getFalseBlock()->getAsTyped());
2550 };
2551
John Kessenich4bee5312018-02-20 21:29:05 -07002552 spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue
2553 // emit the condition before doing anything with selection
2554 node->getCondition()->traverse(this);
2555 spv::Id condition = accessChainLoad(node->getCondition()->getType());
2556
2557 // Find a way of executing both sides and selecting the right result.
2558 const auto executeBothSides = [&]() -> void {
2559 // execute both sides
John Kessenich433e9ff2017-01-26 20:31:11 -07002560 node->getTrueBlock()->traverse(this);
2561 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2562 node->getFalseBlock()->traverse(this);
2563 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2564
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002565 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002566
John Kessenich4bee5312018-02-20 21:29:05 -07002567 // done if void
2568 if (node->getBasicType() == glslang::EbtVoid)
2569 return;
John Kesseniche434ad92017-03-30 10:09:28 -06002570
John Kessenich4bee5312018-02-20 21:29:05 -07002571 // emit code to select between trueValue and falseValue
2572
2573 // see if OpSelect can handle it
2574 if (node->getType().isScalar() || node->getType().isVector()) {
2575 // Emit OpSelect for this selection.
2576
2577 // smear condition to vector, if necessary (AST is always scalar)
2578 if (builder.isVector(trueValue))
2579 condition = builder.smearScalar(spv::NoPrecision, condition,
2580 builder.makeVectorType(builder.makeBoolType(),
2581 builder.getNumComponents(trueValue)));
2582
2583 // OpSelect
2584 result = builder.createTriOp(spv::OpSelect,
2585 convertGlslangToSpvType(node->getType()), condition,
2586 trueValue, falseValue);
2587
2588 builder.clearAccessChain();
2589 builder.setAccessChainRValue(result);
2590 } else {
2591 // We need control flow to select the result.
2592 // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path.
2593 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
2594
2595 // Selection control:
2596 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2597
2598 // make an "if" based on the value created by the condition
2599 spv::Builder::If ifBuilder(condition, control, builder);
2600
2601 // emit the "then" statement
2602 builder.createStore(trueValue, result);
2603 ifBuilder.makeBeginElse();
2604 // emit the "else" statement
2605 builder.createStore(falseValue, result);
2606
2607 // finish off the control flow
2608 ifBuilder.makeEndIf();
2609
2610 builder.clearAccessChain();
2611 builder.setAccessChainLValue(result);
2612 }
John Kessenich433e9ff2017-01-26 20:31:11 -07002613 };
2614
John Kessenich4bee5312018-02-20 21:29:05 -07002615 // Execute the one side needed, as per the condition
2616 const auto executeOneSide = [&]() {
2617 // Always emit control flow.
2618 if (node->getBasicType() != glslang::EbtVoid)
2619 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
John Kessenich433e9ff2017-01-26 20:31:11 -07002620
John Kessenich4bee5312018-02-20 21:29:05 -07002621 // Selection control:
2622 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2623
2624 // make an "if" based on the value created by the condition
2625 spv::Builder::If ifBuilder(condition, control, builder);
2626
2627 // emit the "then" statement
2628 if (node->getTrueBlock() != nullptr) {
2629 node->getTrueBlock()->traverse(this);
2630 if (result != spv::NoResult)
2631 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
2632 }
2633
2634 if (node->getFalseBlock() != nullptr) {
2635 ifBuilder.makeBeginElse();
2636 // emit the "else" statement
2637 node->getFalseBlock()->traverse(this);
2638 if (result != spv::NoResult)
2639 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
2640 }
2641
2642 // finish off the control flow
2643 ifBuilder.makeEndIf();
2644
2645 if (result != spv::NoResult) {
2646 builder.clearAccessChain();
2647 builder.setAccessChainLValue(result);
2648 }
2649 };
2650
2651 // Try for OpSelect (or a requirement to execute both sides)
2652 if (bothSidesPolicy()) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07002653 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2654 if (node->getType().getQualifier().isSpecConstant())
2655 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
John Kessenich4bee5312018-02-20 21:29:05 -07002656 executeBothSides();
2657 } else
2658 executeOneSide();
John Kessenich140f3df2015-06-26 16:58:36 -06002659
2660 return false;
2661}
2662
2663bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
2664{
2665 // emit and get the condition before doing anything with switch
2666 node->getCondition()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002667 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002668
Rex Xu57e65922017-07-04 23:23:40 +08002669 // Selection control:
John Kesseniche18fd202018-01-30 11:01:39 -07002670 const spv::SelectionControlMask control = TranslateSwitchControl(*node);
Rex Xu57e65922017-07-04 23:23:40 +08002671
John Kessenich140f3df2015-06-26 16:58:36 -06002672 // browse the children to sort out code segments
2673 int defaultSegment = -1;
2674 std::vector<TIntermNode*> codeSegments;
2675 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
2676 std::vector<int> caseValues;
2677 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
2678 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
2679 TIntermNode* child = *c;
2680 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
baldurkd76692d2015-07-12 11:32:58 +02002681 defaultSegment = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06002682 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
baldurkd76692d2015-07-12 11:32:58 +02002683 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06002684 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst());
2685 } else
2686 codeSegments.push_back(child);
2687 }
2688
qining25262b32016-05-06 17:25:16 -04002689 // handle the case where the last code segment is missing, due to no code
John Kessenich140f3df2015-06-26 16:58:36 -06002690 // statements between the last case and the end of the switch statement
2691 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
2692 (int)codeSegments.size() == defaultSegment)
2693 codeSegments.push_back(nullptr);
2694
2695 // make the switch statement
2696 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
Rex Xu57e65922017-07-04 23:23:40 +08002697 builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks);
John Kessenich140f3df2015-06-26 16:58:36 -06002698
2699 // emit all the code in the segments
2700 breakForLoop.push(false);
2701 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
2702 builder.nextSwitchSegment(segmentBlocks, s);
2703 if (codeSegments[s])
2704 codeSegments[s]->traverse(this);
2705 else
2706 builder.addSwitchBreak();
2707 }
2708 breakForLoop.pop();
2709
2710 builder.endSwitch(segmentBlocks);
2711
2712 return false;
2713}
2714
2715void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
2716{
2717 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04002718 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
John Kessenich140f3df2015-06-26 16:58:36 -06002719
2720 builder.clearAccessChain();
2721 builder.setAccessChainRValue(constant);
2722}
2723
2724bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
2725{
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002726 auto blocks = builder.makeNewLoop();
Dejan Mircevski832c65c2016-01-11 15:57:11 -05002727 builder.createBranch(&blocks.head);
steve-lunargf1709e72017-05-02 20:14:50 -06002728
2729 // Loop control:
John Kessenicha2858d92018-01-31 08:11:18 -07002730 unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite;
2731 const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength);
steve-lunargf1709e72017-05-02 20:14:50 -06002732
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05002733 // Spec requires back edges to target header blocks, and every header block
2734 // must dominate its merge block. Make a header block first to ensure these
2735 // conditions are met. By definition, it will contain OpLoopMerge, followed
2736 // by a block-ending branch. But we don't want to put any other body/test
2737 // instructions in it, since the body/test may have arbitrary instructions,
2738 // including merges of its own.
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002739 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05002740 builder.setBuildPoint(&blocks.head);
John Kessenicha2858d92018-01-31 08:11:18 -07002741 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002742 if (node->testFirst() && node->getTest()) {
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05002743 spv::Block& test = builder.makeNewBlock();
2744 builder.createBranch(&test);
2745
2746 builder.setBuildPoint(&test);
John Kessenich140f3df2015-06-26 16:58:36 -06002747 node->getTest()->traverse(this);
John Kesseniche485c7a2017-05-31 18:50:53 -06002748 spv::Id condition = accessChainLoad(node->getTest()->getType());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002749 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
2750
2751 builder.setBuildPoint(&blocks.body);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05002752 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002753 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05002754 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002755 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05002756 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002757
2758 builder.setBuildPoint(&blocks.continue_target);
2759 if (node->getTerminal())
2760 node->getTerminal()->traverse(this);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05002761 builder.createBranch(&blocks.head);
David Netoc22f37c2015-07-15 16:21:26 -04002762 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002763 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002764 builder.createBranch(&blocks.body);
2765
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05002766 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002767 builder.setBuildPoint(&blocks.body);
2768 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05002769 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002770 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05002771 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002772
2773 builder.setBuildPoint(&blocks.continue_target);
2774 if (node->getTerminal())
2775 node->getTerminal()->traverse(this);
2776 if (node->getTest()) {
2777 node->getTest()->traverse(this);
2778 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07002779 accessChainLoad(node->getTest()->getType());
Dejan Mircevski832c65c2016-01-11 15:57:11 -05002780 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002781 } else {
Dejan Mircevskied55bcd2016-01-19 21:13:38 -05002782 // TODO: unless there was a break/return/discard instruction
2783 // somewhere in the body, this is an infinite loop, so we should
2784 // issue a warning.
Dejan Mircevski832c65c2016-01-11 15:57:11 -05002785 builder.createBranch(&blocks.head);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002786 }
John Kessenich140f3df2015-06-26 16:58:36 -06002787 }
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05002788 builder.setBuildPoint(&blocks.merge);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05002789 builder.closeLoop();
John Kessenich140f3df2015-06-26 16:58:36 -06002790 return false;
2791}
2792
2793bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
2794{
2795 if (node->getExpression())
2796 node->getExpression()->traverse(this);
2797
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002798 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002799
John Kessenich140f3df2015-06-26 16:58:36 -06002800 switch (node->getFlowOp()) {
2801 case glslang::EOpKill:
2802 builder.makeDiscard();
2803 break;
2804 case glslang::EOpBreak:
2805 if (breakForLoop.top())
2806 builder.createLoopExit();
2807 else
2808 builder.addSwitchBreak();
2809 break;
2810 case glslang::EOpContinue:
John Kessenich140f3df2015-06-26 16:58:36 -06002811 builder.createLoopContinue();
2812 break;
2813 case glslang::EOpReturn:
John Kesseniched33e052016-10-06 12:59:51 -06002814 if (node->getExpression()) {
2815 const glslang::TType& glslangReturnType = node->getExpression()->getType();
2816 spv::Id returnId = accessChainLoad(glslangReturnType);
2817 if (builder.getTypeId(returnId) != currentFunction->getReturnType()) {
2818 builder.clearAccessChain();
2819 spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType());
2820 builder.setAccessChainLValue(copyId);
2821 multiTypeStore(glslangReturnType, returnId);
2822 returnId = builder.createLoad(copyId);
2823 }
2824 builder.makeReturn(false, returnId);
2825 } else
John Kesseniche770b3e2015-09-14 20:58:02 -06002826 builder.makeReturn(false);
John Kessenich140f3df2015-06-26 16:58:36 -06002827
2828 builder.clearAccessChain();
2829 break;
2830
2831 default:
John Kessenich55e7d112015-11-15 21:33:39 -07002832 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06002833 break;
2834 }
2835
2836 return false;
2837}
2838
2839spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node)
2840{
qining25262b32016-05-06 17:25:16 -04002841 // First, steer off constants, which are not SPIR-V variables, but
John Kessenich140f3df2015-06-26 16:58:36 -06002842 // can still have a mapping to a SPIR-V Id.
John Kessenich55e7d112015-11-15 21:33:39 -07002843 // This includes specialization constants.
John Kessenich7cc0e282016-03-20 00:46:02 -06002844 if (node->getQualifier().isConstant()) {
Dan Sinclair12fcaa22018-11-13 09:17:44 -05002845 spv::Id result = createSpvConstant(*node);
2846 if (result != spv::NoResult)
2847 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06002848 }
2849
2850 // Now, handle actual variables
John Kessenicha5c5fb62017-05-05 05:09:58 -06002851 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002852 spv::Id spvType = convertGlslangToSpvType(node->getType());
2853
Rex Xucabbb782017-03-24 13:41:14 +08002854 const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) ||
2855 node->getType().containsBasicType(glslang::EbtInt16) ||
2856 node->getType().containsBasicType(glslang::EbtUint16);
Rex Xuf89ad982017-04-07 23:22:33 +08002857 if (contains16BitType) {
John Kessenich18310872018-05-14 22:08:53 -06002858 switch (storageClass) {
2859 case spv::StorageClassInput:
2860 case spv::StorageClassOutput:
John Kessenich66011cb2018-03-06 16:12:04 -07002861 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
Rex Xuf89ad982017-04-07 23:22:33 +08002862 builder.addCapability(spv::CapabilityStorageInputOutput16);
John Kessenich18310872018-05-14 22:08:53 -06002863 break;
2864 case spv::StorageClassPushConstant:
John Kessenich66011cb2018-03-06 16:12:04 -07002865 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
Rex Xuf89ad982017-04-07 23:22:33 +08002866 builder.addCapability(spv::CapabilityStoragePushConstant16);
John Kessenich18310872018-05-14 22:08:53 -06002867 break;
2868 case spv::StorageClassUniform:
John Kessenich66011cb2018-03-06 16:12:04 -07002869 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
Rex Xuf89ad982017-04-07 23:22:33 +08002870 if (node->getType().getQualifier().storage == glslang::EvqBuffer)
2871 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
John Kessenich18310872018-05-14 22:08:53 -06002872 else
2873 builder.addCapability(spv::CapabilityStorageUniform16);
2874 break;
2875 case spv::StorageClassStorageBuffer:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002876 case spv::StorageClassPhysicalStorageBufferEXT:
John Kessenich18310872018-05-14 22:08:53 -06002877 addPre13Extension(spv::E_SPV_KHR_16bit_storage);
2878 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
2879 break;
2880 default:
2881 break;
Rex Xuf89ad982017-04-07 23:22:33 +08002882 }
2883 }
Rex Xuf89ad982017-04-07 23:22:33 +08002884
John Kessenich312dcfb2018-07-03 13:19:51 -06002885 const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) ||
2886 node->getType().containsBasicType(glslang::EbtUint8);
2887 if (contains8BitType) {
2888 if (storageClass == spv::StorageClassPushConstant) {
2889 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2890 builder.addCapability(spv::CapabilityStoragePushConstant8);
2891 } else if (storageClass == spv::StorageClassUniform) {
2892 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2893 builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess);
Neil Henningb6b01f02018-10-23 15:02:29 +01002894 } else if (storageClass == spv::StorageClassStorageBuffer) {
2895 builder.addExtension(spv::E_SPV_KHR_8bit_storage);
2896 builder.addCapability(spv::CapabilityStorageBuffer8BitAccess);
John Kessenich312dcfb2018-07-03 13:19:51 -06002897 }
2898 }
2899
John Kessenich140f3df2015-06-26 16:58:36 -06002900 const char* name = node->getName().c_str();
2901 if (glslang::IsAnonymous(name))
2902 name = "";
2903
2904 return builder.createVariable(storageClass, spvType, name);
2905}
2906
2907// Return type Id of the sampled type.
2908spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
2909{
2910 switch (sampler.type) {
2911 case glslang::EbtFloat: return builder.makeFloatType(32);
Rex Xu1e5d7b02016-11-29 17:36:31 +08002912#ifdef AMD_EXTENSIONS
2913 case glslang::EbtFloat16:
2914 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch);
2915 builder.addCapability(spv::CapabilityFloat16ImageAMD);
2916 return builder.makeFloatType(16);
2917#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002918 case glslang::EbtInt: return builder.makeIntType(32);
2919 case glslang::EbtUint: return builder.makeUintType(32);
2920 default:
John Kessenich55e7d112015-11-15 21:33:39 -07002921 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06002922 return builder.makeFloatType(32);
2923 }
2924}
2925
John Kessenich8c8505c2016-07-26 12:50:38 -06002926// If node is a swizzle operation, return the type that should be used if
2927// the swizzle base is first consumed by another operation, before the swizzle
2928// is applied.
2929spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
2930{
John Kessenichecba76f2017-01-06 00:34:48 -07002931 if (node.getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002932 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2933 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
2934 else
2935 return spv::NoType;
2936}
2937
2938// When inverting a swizzle with a parent op, this function
2939// will apply the swizzle operation to a completed parent operation.
2940spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult)
2941{
2942 std::vector<unsigned> swizzle;
2943 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
2944 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
2945}
2946
John Kessenich8c8505c2016-07-26 12:50:38 -06002947// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
2948void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
2949{
2950 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
2951 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
2952 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
2953}
2954
John Kessenich3ac051e2015-12-20 11:29:16 -07002955// Convert from a glslang type to an SPV type, by calling into a
2956// recursive version of this function. This establishes the inherited
2957// layout state rooted from the top-level type.
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002958spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
John Kessenich140f3df2015-06-26 16:58:36 -06002959{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002960 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
John Kessenich31ed4832015-09-09 17:51:38 -06002961}
2962
2963// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
John Kessenich7b9fa252016-01-21 18:56:57 -07002964// explicitLayout can be kept the same throughout the hierarchical recursive walk.
John Kessenich6090df02016-06-30 21:18:02 -06002965// Mutually recursive with convertGlslangStructToSpvType().
John Kessenichead86222018-03-28 18:01:20 -06002966spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002967 glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
2968 bool lastBufferBlockMember, bool forwardReferenceOnly)
John Kessenich31ed4832015-09-09 17:51:38 -06002969{
John Kesseniche0b6cad2015-12-24 10:30:13 -07002970 spv::Id spvType = spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06002971
2972 switch (type.getBasicType()) {
2973 case glslang::EbtVoid:
2974 spvType = builder.makeVoidType();
John Kessenich55e7d112015-11-15 21:33:39 -07002975 assert (! type.isArray());
John Kessenich140f3df2015-06-26 16:58:36 -06002976 break;
2977 case glslang::EbtFloat:
2978 spvType = builder.makeFloatType(32);
2979 break;
2980 case glslang::EbtDouble:
2981 spvType = builder.makeFloatType(64);
2982 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002983 case glslang::EbtFloat16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002984 spvType = builder.makeFloatType(16);
2985 break;
John Kessenich140f3df2015-06-26 16:58:36 -06002986 case glslang::EbtBool:
John Kessenich103bef92016-02-08 21:38:15 -07002987 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
2988 // a 32-bit int where non-0 means true.
2989 if (explicitLayout != glslang::ElpNone)
2990 spvType = builder.makeUintType(32);
2991 else
2992 spvType = builder.makeBoolType();
John Kessenich140f3df2015-06-26 16:58:36 -06002993 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06002994 case glslang::EbtInt8:
John Kessenich66011cb2018-03-06 16:12:04 -07002995 spvType = builder.makeIntType(8);
2996 break;
2997 case glslang::EbtUint8:
John Kessenich66011cb2018-03-06 16:12:04 -07002998 spvType = builder.makeUintType(8);
2999 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003000 case glslang::EbtInt16:
John Kessenich66011cb2018-03-06 16:12:04 -07003001 spvType = builder.makeIntType(16);
3002 break;
3003 case glslang::EbtUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07003004 spvType = builder.makeUintType(16);
3005 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003006 case glslang::EbtInt:
3007 spvType = builder.makeIntType(32);
3008 break;
3009 case glslang::EbtUint:
3010 spvType = builder.makeUintType(32);
3011 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08003012 case glslang::EbtInt64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003013 spvType = builder.makeIntType(64);
3014 break;
3015 case glslang::EbtUint64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003016 spvType = builder.makeUintType(64);
3017 break;
John Kessenich426394d2015-07-23 10:22:48 -06003018 case glslang::EbtAtomicUint:
John Kessenich2d0cc782016-07-07 13:20:00 -06003019 builder.addCapability(spv::CapabilityAtomicStorage);
John Kessenich426394d2015-07-23 10:22:48 -06003020 spvType = builder.makeUintType(32);
3021 break;
Chao Chenb50c02e2018-09-19 11:42:24 -07003022#ifdef NV_EXTENSIONS
3023 case glslang::EbtAccStructNV:
3024 spvType = builder.makeAccelerationStructureNVType();
3025 break;
3026#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003027 case glslang::EbtSampler:
3028 {
3029 const glslang::TSampler& sampler = type.getSampler();
John Kessenich6c292d32016-02-15 20:58:50 -07003030 if (sampler.sampler) {
3031 // pure sampler
3032 spvType = builder.makeSamplerType();
3033 } else {
3034 // an image is present, make its type
3035 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms,
3036 sampler.image ? 2 : 1, TranslateImageFormat(type));
3037 if (sampler.combined) {
3038 // already has both image and sampler, make the combined type
3039 spvType = builder.makeSampledImageType(spvType);
3040 }
John Kessenich55e7d112015-11-15 21:33:39 -07003041 }
John Kesseniche0b6cad2015-12-24 10:30:13 -07003042 }
John Kessenich140f3df2015-06-26 16:58:36 -06003043 break;
3044 case glslang::EbtStruct:
3045 case glslang::EbtBlock:
3046 {
3047 // If we've seen this struct type, return it
John Kessenich6090df02016-06-30 21:18:02 -06003048 const glslang::TTypeList* glslangMembers = type.getStruct();
John Kesseniche0b6cad2015-12-24 10:30:13 -07003049
3050 // Try to share structs for different layouts, but not yet for other
3051 // kinds of qualification (primarily not yet including interpolant qualification).
John Kessenichf2b7f332016-09-01 17:05:23 -06003052 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003053 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
John Kesseniche0b6cad2015-12-24 10:30:13 -07003054 if (spvType != spv::NoResult)
John Kessenich140f3df2015-06-26 16:58:36 -06003055 break;
3056
3057 // else, we haven't seen it...
John Kessenich140f3df2015-06-26 16:58:36 -06003058 if (type.getBasicType() == glslang::EbtBlock)
John Kessenich6090df02016-06-30 21:18:02 -06003059 memberRemapper[glslangMembers].resize(glslangMembers->size());
3060 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
John Kessenich140f3df2015-06-26 16:58:36 -06003061 }
3062 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003063 case glslang::EbtReference:
3064 {
3065 // Make the forward pointer, then recurse to convert the structure type, then
3066 // patch up the forward pointer with a real pointer type.
3067 if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) {
3068 spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT);
3069 forwardPointers[type.getReferentType()] = forwardId;
3070 }
3071 spvType = forwardPointers[type.getReferentType()];
3072 if (!forwardReferenceOnly) {
3073 spv::Id referentType = convertGlslangToSpvType(*type.getReferentType());
3074 builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT,
3075 forwardPointers[type.getReferentType()],
3076 referentType);
3077 }
3078 }
3079 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003080 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003081 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003082 break;
3083 }
3084
3085 if (type.isMatrix())
3086 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
3087 else {
3088 // If this variable has a vector element count greater than 1, create a SPIR-V vector
3089 if (type.getVectorSize() > 1)
3090 spvType = builder.makeVectorType(spvType, type.getVectorSize());
3091 }
3092
3093 if (type.isArray()) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003094 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
3095
John Kessenichc9a80832015-09-12 12:17:44 -06003096 // Do all but the outer dimension
John Kessenichc9e0a422015-12-29 21:27:24 -07003097 if (type.getArraySizes()->getNumDims() > 1) {
John Kessenichf8842e52016-01-04 19:22:56 -07003098 // We need to decorate array strides for types needing explicit layout, except blocks.
3099 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003100 // Use a dummy glslang type for querying internal strides of
3101 // arrays of arrays, but using just a one-dimensional array.
3102 glslang::TType simpleArrayType(type, 0); // deference type of the array
John Kessenich859b0342018-03-26 00:38:53 -06003103 while (simpleArrayType.getArraySizes()->getNumDims() > 1)
3104 simpleArrayType.getArraySizes()->dereference();
John Kessenichc9e0a422015-12-29 21:27:24 -07003105
3106 // Will compute the higher-order strides here, rather than making a whole
3107 // pile of types and doing repetitive recursion on their contents.
3108 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
3109 }
John Kessenichf8842e52016-01-04 19:22:56 -07003110
3111 // make the arrays
John Kessenichc9e0a422015-12-29 21:27:24 -07003112 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
John Kessenich6c292d32016-02-15 20:58:50 -07003113 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
John Kessenichc9e0a422015-12-29 21:27:24 -07003114 if (stride > 0)
3115 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich6c292d32016-02-15 20:58:50 -07003116 stride *= type.getArraySizes()->getDimSize(dim);
John Kessenichc9e0a422015-12-29 21:27:24 -07003117 }
3118 } else {
3119 // single-dimensional array, and don't yet have stride
3120
John Kessenichf8842e52016-01-04 19:22:56 -07003121 // We need to decorate array strides for types needing explicit layout, except blocks.
John Kessenichc9e0a422015-12-29 21:27:24 -07003122 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
3123 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
John Kessenichc9a80832015-09-12 12:17:44 -06003124 }
John Kessenich31ed4832015-09-09 17:51:38 -06003125
John Kessenichead86222018-03-28 18:01:20 -06003126 // Do the outer dimension, which might not be known for a runtime-sized array.
3127 // (Unsized arrays that survive through linking will be runtime-sized arrays)
3128 if (type.isSizedArray())
John Kessenich6c292d32016-02-15 20:58:50 -07003129 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
John Kessenich5611c6d2018-04-05 11:25:02 -06003130 else {
3131 if (!lastBufferBlockMember) {
3132 builder.addExtension("SPV_EXT_descriptor_indexing");
3133 builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT);
3134 }
John Kessenichead86222018-03-28 18:01:20 -06003135 spvType = builder.makeRuntimeArray(spvType);
John Kessenich5611c6d2018-04-05 11:25:02 -06003136 }
John Kessenichc9e0a422015-12-29 21:27:24 -07003137 if (stride > 0)
3138 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich140f3df2015-06-26 16:58:36 -06003139 }
3140
3141 return spvType;
3142}
3143
John Kessenich0e737842017-03-24 18:38:16 -06003144// TODO: this functionality should exist at a higher level, in creating the AST
3145//
3146// Identify interface members that don't have their required extension turned on.
3147//
3148bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
3149{
Chao Chen3c366992018-09-19 11:41:59 -07003150#ifdef NV_EXTENSIONS
John Kessenich0e737842017-03-24 18:38:16 -06003151 auto& extensions = glslangIntermediate->getRequestedExtensions();
3152
Rex Xubcf291a2017-03-29 23:01:36 +08003153 if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
3154 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3155 return true;
John Kessenich0e737842017-03-24 18:38:16 -06003156 if (member.getFieldName() == "gl_SecondaryPositionNV" &&
3157 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3158 return true;
Chao Chen3c366992018-09-19 11:41:59 -07003159
3160 if (glslangIntermediate->getStage() != EShLangMeshNV) {
3161 if (member.getFieldName() == "gl_ViewportMask" &&
3162 extensions.find("GL_NV_viewport_array2") == extensions.end())
3163 return true;
3164 if (member.getFieldName() == "gl_PositionPerViewNV" &&
3165 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3166 return true;
3167 if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
3168 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3169 return true;
3170 }
3171#endif
John Kessenich0e737842017-03-24 18:38:16 -06003172
3173 return false;
3174};
3175
John Kessenich6090df02016-06-30 21:18:02 -06003176// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
3177// explicitLayout can be kept the same throughout the hierarchical recursive walk.
3178// Mutually recursive with convertGlslangToSpvType().
3179spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
3180 const glslang::TTypeList* glslangMembers,
3181 glslang::TLayoutPacking explicitLayout,
3182 const glslang::TQualifier& qualifier)
3183{
3184 // Create a vector of struct types for SPIR-V to consume
3185 std::vector<spv::Id> spvMembers;
3186 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003187 std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers;
John Kessenich6090df02016-06-30 21:18:02 -06003188 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3189 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3190 if (glslangMember.hiddenMember()) {
3191 ++memberDelta;
3192 if (type.getBasicType() == glslang::EbtBlock)
3193 memberRemapper[glslangMembers][i] = -1;
3194 } else {
John Kessenich0e737842017-03-24 18:38:16 -06003195 if (type.getBasicType() == glslang::EbtBlock) {
John Kessenich6090df02016-06-30 21:18:02 -06003196 memberRemapper[glslangMembers][i] = i - memberDelta;
John Kessenich0e737842017-03-24 18:38:16 -06003197 if (filterMember(glslangMember))
3198 continue;
3199 }
John Kessenich6090df02016-06-30 21:18:02 -06003200 // modify just this child's view of the qualifier
3201 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3202 InheritQualifiers(memberQualifier, qualifier);
3203
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003204 // manually inherit location
John Kessenich6090df02016-06-30 21:18:02 -06003205 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003206 memberQualifier.layoutLocation = qualifier.layoutLocation;
John Kessenich6090df02016-06-30 21:18:02 -06003207
3208 // recurse
John Kessenichead86222018-03-28 18:01:20 -06003209 bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer &&
3210 i == (int)glslangMembers->size() - 1;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003211
3212 // Make forward pointers for any pointer members, and create a list of members to
3213 // convert to spirv types after creating the struct.
3214 if (glslangMember.getBasicType() == glslang::EbtReference) {
3215 if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) {
3216 deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier));
3217 }
3218 spvMembers.push_back(
3219 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true));
3220 } else {
3221 spvMembers.push_back(
3222 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false));
3223 }
John Kessenich6090df02016-06-30 21:18:02 -06003224 }
3225 }
3226
3227 // Make the SPIR-V type
3228 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
John Kessenichf2b7f332016-09-01 17:05:23 -06003229 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003230 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
3231
3232 // Decorate it
3233 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
3234
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003235 for (int i = 0; i < deferredForwardPointers.size(); ++i) {
3236 auto it = deferredForwardPointers[i];
3237 convertGlslangToSpvType(*it.first, explicitLayout, it.second, false);
3238 }
3239
John Kessenich6090df02016-06-30 21:18:02 -06003240 return spvType;
3241}
3242
3243void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
3244 const glslang::TTypeList* glslangMembers,
3245 glslang::TLayoutPacking explicitLayout,
3246 const glslang::TQualifier& qualifier,
3247 spv::Id spvType)
3248{
3249 // Name and decorate the non-hidden members
3250 int offset = -1;
3251 int locationOffset = 0; // for use within the members of this struct
3252 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3253 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3254 int member = i;
John Kessenich0e737842017-03-24 18:38:16 -06003255 if (type.getBasicType() == glslang::EbtBlock) {
John Kessenich6090df02016-06-30 21:18:02 -06003256 member = memberRemapper[glslangMembers][i];
John Kessenich0e737842017-03-24 18:38:16 -06003257 if (filterMember(glslangMember))
3258 continue;
3259 }
John Kessenich6090df02016-06-30 21:18:02 -06003260
3261 // modify just this child's view of the qualifier
3262 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3263 InheritQualifiers(memberQualifier, qualifier);
3264
3265 // using -1 above to indicate a hidden member
John Kessenich5d610ee2018-03-07 18:05:55 -07003266 if (member < 0)
3267 continue;
3268
3269 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
3270 builder.addMemberDecoration(spvType, member,
3271 TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
3272 builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
3273 // Add interpolation and auxiliary storage decorations only to
3274 // top-level members of Input and Output storage classes
3275 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
3276 type.getQualifier().storage == glslang::EvqVaryingOut) {
3277 if (type.getBasicType() == glslang::EbtBlock ||
3278 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
3279 builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
3280 builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
Chao Chen3c366992018-09-19 11:41:59 -07003281#ifdef NV_EXTENSIONS
3282 addMeshNVDecoration(spvType, member, memberQualifier);
3283#endif
John Kessenich6090df02016-06-30 21:18:02 -06003284 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003285 }
3286 builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
John Kessenich6090df02016-06-30 21:18:02 -06003287
John Kessenich5d610ee2018-03-07 18:05:55 -07003288 if (type.getBasicType() == glslang::EbtBlock &&
3289 qualifier.storage == glslang::EvqBuffer) {
3290 // Add memory decorations only to top-level members of shader storage block
3291 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05003292 TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich5d610ee2018-03-07 18:05:55 -07003293 for (unsigned int i = 0; i < memory.size(); ++i)
3294 builder.addMemberDecoration(spvType, member, memory[i]);
3295 }
John Kessenich6090df02016-06-30 21:18:02 -06003296
John Kessenich5d610ee2018-03-07 18:05:55 -07003297 // Location assignment was already completed correctly by the front end,
3298 // just track whether a member needs to be decorated.
3299 // Ignore member locations if the container is an array, as that's
3300 // ill-specified and decisions have been made to not allow this.
3301 if (! type.isArray() && memberQualifier.hasLocation())
3302 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation);
John Kessenich6090df02016-06-30 21:18:02 -06003303
John Kessenich5d610ee2018-03-07 18:05:55 -07003304 if (qualifier.hasLocation()) // track for upcoming inheritance
3305 locationOffset += glslangIntermediate->computeTypeLocationSize(
3306 glslangMember, glslangIntermediate->getStage());
John Kessenich2f47bc92016-06-30 21:47:35 -06003307
John Kessenich5d610ee2018-03-07 18:05:55 -07003308 // component, XFB, others
3309 if (glslangMember.getQualifier().hasComponent())
3310 builder.addMemberDecoration(spvType, member, spv::DecorationComponent,
3311 glslangMember.getQualifier().layoutComponent);
3312 if (glslangMember.getQualifier().hasXfbOffset())
3313 builder.addMemberDecoration(spvType, member, spv::DecorationOffset,
3314 glslangMember.getQualifier().layoutXfbOffset);
3315 else if (explicitLayout != glslang::ElpNone) {
3316 // figure out what to do with offset, which is accumulating
3317 int nextOffset;
3318 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
3319 if (offset >= 0)
3320 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
3321 offset = nextOffset;
3322 }
John Kessenich6090df02016-06-30 21:18:02 -06003323
John Kessenich5d610ee2018-03-07 18:05:55 -07003324 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
3325 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride,
3326 getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
John Kessenich6090df02016-06-30 21:18:02 -06003327
John Kessenich5d610ee2018-03-07 18:05:55 -07003328 // built-in variable decorations
3329 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
3330 if (builtIn != spv::BuiltInMax)
3331 builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
chaoc771d89f2017-01-13 01:10:53 -08003332
John Kessenich5611c6d2018-04-05 11:25:02 -06003333 // nonuniform
3334 builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier()));
3335
John Kessenichead86222018-03-28 18:01:20 -06003336 if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) {
3337 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
3338 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
3339 memberQualifier.semanticName);
3340 }
3341
chaoc771d89f2017-01-13 01:10:53 -08003342#ifdef NV_EXTENSIONS
John Kessenich5d610ee2018-03-07 18:05:55 -07003343 if (builtIn == spv::BuiltInLayer) {
3344 // SPV_NV_viewport_array2 extension
3345 if (glslangMember.getQualifier().layoutViewportRelative){
3346 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
3347 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
3348 builder.addExtension(spv::E_SPV_NV_viewport_array2);
chaoc771d89f2017-01-13 01:10:53 -08003349 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003350 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
3351 builder.addMemberDecoration(spvType, member,
3352 (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
3353 glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
3354 builder.addCapability(spv::CapabilityShaderStereoViewNV);
3355 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
chaocdf3956c2017-02-14 14:52:34 -08003356 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003357 }
3358 if (glslangMember.getQualifier().layoutPassthrough) {
3359 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
3360 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
3361 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
3362 }
chaoc771d89f2017-01-13 01:10:53 -08003363#endif
John Kessenich6090df02016-06-30 21:18:02 -06003364 }
3365
3366 // Decorate the structure
John Kessenich5d610ee2018-03-07 18:05:55 -07003367 builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
3368 builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer()));
John Kessenich6090df02016-06-30 21:18:02 -06003369}
3370
John Kessenich6c292d32016-02-15 20:58:50 -07003371// Turn the expression forming the array size into an id.
3372// This is not quite trivial, because of specialization constants.
3373// Sometimes, a raw constant is turned into an Id, and sometimes
3374// a specialization constant expression is.
3375spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
3376{
3377 // First, see if this is sized with a node, meaning a specialization constant:
3378 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
3379 if (specNode != nullptr) {
3380 builder.clearAccessChain();
3381 specNode->traverse(this);
3382 return accessChainLoad(specNode->getAsTyped()->getType());
3383 }
qining25262b32016-05-06 17:25:16 -04003384
John Kessenich6c292d32016-02-15 20:58:50 -07003385 // Otherwise, need a compile-time (front end) size, get it:
3386 int size = arraySizes.getDimSize(dim);
3387 assert(size > 0);
3388 return builder.makeUintConstant(size);
3389}
3390
John Kessenich103bef92016-02-08 21:38:15 -07003391// Wrap the builder's accessChainLoad to:
3392// - localize handling of RelaxedPrecision
3393// - use the SPIR-V inferred type instead of another conversion of the glslang type
3394// (avoids unnecessary work and possible type punning for structures)
3395// - do conversion of concrete to abstract type
John Kessenich32cfd492016-02-02 12:37:46 -07003396spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
3397{
John Kessenich103bef92016-02-08 21:38:15 -07003398 spv::Id nominalTypeId = builder.accessChainGetInferredType();
Jeff Bolz36831c92018-09-05 10:11:41 -05003399
3400 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3401 coherentFlags |= TranslateCoherent(type);
3402
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003403 unsigned int alignment = builder.getAccessChain().alignment;
3404 alignment |= getBufferReferenceAlignment(type);
3405
John Kessenich5611c6d2018-04-05 11:25:02 -06003406 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type),
Jeff Bolz36831c92018-09-05 10:11:41 -05003407 TranslateNonUniformDecoration(type.getQualifier()),
3408 nominalTypeId,
3409 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003410 TranslateMemoryScope(coherentFlags),
3411 alignment);
John Kessenich103bef92016-02-08 21:38:15 -07003412
3413 // Need to convert to abstract types when necessary
Rex Xu27253232016-02-23 17:51:09 +08003414 if (type.getBasicType() == glslang::EbtBool) {
3415 if (builder.isScalarType(nominalTypeId)) {
3416 // Conversion for bool
3417 spv::Id boolType = builder.makeBoolType();
3418 if (nominalTypeId != boolType)
3419 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
3420 } else if (builder.isVectorType(nominalTypeId)) {
3421 // Conversion for bvec
3422 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3423 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
3424 if (nominalTypeId != bvecType)
3425 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize));
3426 }
3427 }
John Kessenich103bef92016-02-08 21:38:15 -07003428
3429 return loadedId;
John Kessenich32cfd492016-02-02 12:37:46 -07003430}
3431
Rex Xu27253232016-02-23 17:51:09 +08003432// Wrap the builder's accessChainStore to:
3433// - do conversion of concrete to abstract type
John Kessenich4bf71552016-09-02 11:20:21 -06003434//
3435// Implicitly uses the existing builder.accessChain as the storage target.
Rex Xu27253232016-02-23 17:51:09 +08003436void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
3437{
3438 // Need to convert to abstract types when necessary
3439 if (type.getBasicType() == glslang::EbtBool) {
3440 spv::Id nominalTypeId = builder.accessChainGetInferredType();
3441
3442 if (builder.isScalarType(nominalTypeId)) {
3443 // Conversion for bool
3444 spv::Id boolType = builder.makeBoolType();
John Kessenichb6cabc42017-05-19 23:29:50 -06003445 if (nominalTypeId != boolType) {
3446 // keep these outside arguments, for determinant order-of-evaluation
3447 spv::Id one = builder.makeUintConstant(1);
3448 spv::Id zero = builder.makeUintConstant(0);
3449 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
3450 } else if (builder.getTypeId(rvalue) != boolType)
John Kessenich80f92a12017-05-19 23:00:13 -06003451 rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0));
Rex Xu27253232016-02-23 17:51:09 +08003452 } else if (builder.isVectorType(nominalTypeId)) {
3453 // Conversion for bvec
3454 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3455 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
John Kessenichb6cabc42017-05-19 23:29:50 -06003456 if (nominalTypeId != bvecType) {
3457 // keep these outside arguments, for determinant order-of-evaluation
John Kessenich7b8c3862017-05-19 23:44:51 -06003458 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
3459 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
3460 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
John Kessenichb6cabc42017-05-19 23:29:50 -06003461 } else if (builder.getTypeId(rvalue) != bvecType)
John Kessenich80f92a12017-05-19 23:00:13 -06003462 rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue,
3463 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08003464 }
3465 }
3466
Jeff Bolz36831c92018-09-05 10:11:41 -05003467 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3468 coherentFlags |= TranslateCoherent(type);
3469
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003470 unsigned int alignment = builder.getAccessChain().alignment;
3471 alignment |= getBufferReferenceAlignment(type);
3472
Jeff Bolz36831c92018-09-05 10:11:41 -05003473 builder.accessChainStore(rvalue,
3474 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003475 TranslateMemoryScope(coherentFlags), alignment);
Rex Xu27253232016-02-23 17:51:09 +08003476}
3477
John Kessenich4bf71552016-09-02 11:20:21 -06003478// For storing when types match at the glslang level, but not might match at the
3479// SPIR-V level.
3480//
3481// This especially happens when a single glslang type expands to multiple
John Kesseniched33e052016-10-06 12:59:51 -06003482// SPIR-V types, like a struct that is used in a member-undecorated way as well
John Kessenich4bf71552016-09-02 11:20:21 -06003483// as in a member-decorated way.
3484//
3485// NOTE: This function can handle any store request; if it's not special it
3486// simplifies to a simple OpStore.
3487//
3488// Implicitly uses the existing builder.accessChain as the storage target.
3489void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
3490{
John Kessenichb3e24e42016-09-11 12:33:43 -06003491 // we only do the complex path here if it's an aggregate
3492 if (! type.isStruct() && ! type.isArray()) {
John Kessenich4bf71552016-09-02 11:20:21 -06003493 accessChainStore(type, rValue);
3494 return;
3495 }
3496
John Kessenichb3e24e42016-09-11 12:33:43 -06003497 // and, it has to be a case of type aliasing
John Kessenich4bf71552016-09-02 11:20:21 -06003498 spv::Id rType = builder.getTypeId(rValue);
3499 spv::Id lValue = builder.accessChainGetLValue();
3500 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
3501 if (lType == rType) {
3502 accessChainStore(type, rValue);
3503 return;
3504 }
3505
John Kessenichb3e24e42016-09-11 12:33:43 -06003506 // Recursively (as needed) copy an aggregate type to a different aggregate type,
John Kessenich4bf71552016-09-02 11:20:21 -06003507 // where the two types were the same type in GLSL. This requires member
3508 // by member copy, recursively.
3509
John Kessenichb3e24e42016-09-11 12:33:43 -06003510 // If an array, copy element by element.
3511 if (type.isArray()) {
3512 glslang::TType glslangElementType(type, 0);
3513 spv::Id elementRType = builder.getContainedTypeId(rType);
3514 for (int index = 0; index < type.getOuterArraySize(); ++index) {
3515 // get the source member
3516 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
John Kessenich4bf71552016-09-02 11:20:21 -06003517
John Kessenichb3e24e42016-09-11 12:33:43 -06003518 // set up the target storage
3519 builder.clearAccessChain();
3520 builder.setAccessChainLValue(lValue);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003521 builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), getBufferReferenceAlignment(type));
John Kessenich4bf71552016-09-02 11:20:21 -06003522
John Kessenichb3e24e42016-09-11 12:33:43 -06003523 // store the member
3524 multiTypeStore(glslangElementType, elementRValue);
3525 }
3526 } else {
3527 assert(type.isStruct());
John Kessenich4bf71552016-09-02 11:20:21 -06003528
John Kessenichb3e24e42016-09-11 12:33:43 -06003529 // loop over structure members
3530 const glslang::TTypeList& members = *type.getStruct();
3531 for (int m = 0; m < (int)members.size(); ++m) {
3532 const glslang::TType& glslangMemberType = *members[m].type;
3533
3534 // get the source member
3535 spv::Id memberRType = builder.getContainedTypeId(rType, m);
3536 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
3537
3538 // set up the target storage
3539 builder.clearAccessChain();
3540 builder.setAccessChainLValue(lValue);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003541 builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), getBufferReferenceAlignment(type));
John Kessenichb3e24e42016-09-11 12:33:43 -06003542
3543 // store the member
3544 multiTypeStore(glslangMemberType, memberRValue);
3545 }
John Kessenich4bf71552016-09-02 11:20:21 -06003546 }
3547}
3548
John Kessenichf85e8062015-12-19 13:57:10 -07003549// Decide whether or not this type should be
3550// decorated with offsets and strides, and if so
3551// whether std140 or std430 rules should be applied.
3552glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
John Kessenich31ed4832015-09-09 17:51:38 -06003553{
John Kessenichf85e8062015-12-19 13:57:10 -07003554 // has to be a block
3555 if (type.getBasicType() != glslang::EbtBlock)
3556 return glslang::ElpNone;
3557
Chao Chen3c366992018-09-19 11:41:59 -07003558 // has to be a uniform or buffer block or task in/out blocks
John Kessenichf85e8062015-12-19 13:57:10 -07003559 if (type.getQualifier().storage != glslang::EvqUniform &&
Chao Chen3c366992018-09-19 11:41:59 -07003560 type.getQualifier().storage != glslang::EvqBuffer &&
3561 !type.getQualifier().isTaskMemory())
John Kessenichf85e8062015-12-19 13:57:10 -07003562 return glslang::ElpNone;
3563
3564 // return the layout to use
3565 switch (type.getQualifier().layoutPacking) {
3566 case glslang::ElpStd140:
3567 case glslang::ElpStd430:
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003568 case glslang::ElpScalar:
John Kessenichf85e8062015-12-19 13:57:10 -07003569 return type.getQualifier().layoutPacking;
3570 default:
3571 return glslang::ElpNone;
3572 }
John Kessenich31ed4832015-09-09 17:51:38 -06003573}
3574
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003575// Given an array type, returns the integer stride required for that array
John Kessenich3ac051e2015-12-20 11:29:16 -07003576int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003577{
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003578 int size;
John Kessenich49987892015-12-29 17:11:44 -07003579 int stride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003580 glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kesseniche721f492015-12-06 19:17:49 -07003581
3582 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003583}
3584
John Kessenich49987892015-12-29 17:11:44 -07003585// 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 -07003586// when used as a member of an interface block
John Kessenich3ac051e2015-12-20 11:29:16 -07003587int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003588{
John Kessenich49987892015-12-29 17:11:44 -07003589 glslang::TType elementType;
3590 elementType.shallowCopy(matrixType);
3591 elementType.clearArraySizes();
3592
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003593 int size;
John Kessenich49987892015-12-29 17:11:44 -07003594 int stride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003595 glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kessenich49987892015-12-29 17:11:44 -07003596
3597 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003598}
3599
John Kessenich5e4b1242015-08-06 22:53:06 -06003600// Given a member type of a struct, realign the current offset for it, and compute
3601// the next (not yet aligned) offset for the next member, which will get aligned
3602// on the next call.
3603// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
3604// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
3605// -1 means a non-forced member offset (no decoration needed).
John Kessenich735d7e52017-07-13 11:39:16 -06003606void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset,
John Kessenich3ac051e2015-12-20 11:29:16 -07003607 glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
John Kessenich5e4b1242015-08-06 22:53:06 -06003608{
3609 // this will get a positive value when deemed necessary
3610 nextOffset = -1;
3611
John Kessenich5e4b1242015-08-06 22:53:06 -06003612 // override anything in currentOffset with user-set offset
3613 if (memberType.getQualifier().hasOffset())
3614 currentOffset = memberType.getQualifier().layoutOffset;
3615
3616 // It could be that current linker usage in glslang updated all the layoutOffset,
3617 // in which case the following code does not matter. But, that's not quite right
3618 // once cross-compilation unit GLSL validation is done, as the original user
3619 // settings are needed in layoutOffset, and then the following will come into play.
3620
John Kessenichf85e8062015-12-19 13:57:10 -07003621 if (explicitLayout == glslang::ElpNone) {
John Kessenich5e4b1242015-08-06 22:53:06 -06003622 if (! memberType.getQualifier().hasOffset())
3623 currentOffset = -1;
3624
3625 return;
3626 }
3627
John Kessenichf85e8062015-12-19 13:57:10 -07003628 // Getting this far means we need explicit offsets
John Kessenich5e4b1242015-08-06 22:53:06 -06003629 if (currentOffset < 0)
3630 currentOffset = 0;
qining25262b32016-05-06 17:25:16 -04003631
John Kessenich5e4b1242015-08-06 22:53:06 -06003632 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
3633 // but possibly not yet correctly aligned.
3634
3635 int memberSize;
John Kessenich49987892015-12-29 17:11:44 -07003636 int dummyStride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003637 int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kessenich4f1403e2017-04-05 17:38:20 -06003638
3639 // Adjust alignment for HLSL rules
John Kessenich735d7e52017-07-13 11:39:16 -06003640 // TODO: make this consistent in early phases of code:
3641 // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
3642 // Until reflection is brought in sync with these adjustments, don't apply to $Global,
3643 // which is the most likely to rely on reflection, and least likely to rely implicit layouts
John Kesseniche7df8e02018-08-22 17:12:46 -06003644 if (glslangIntermediate->usingHlslOffsets() &&
John Kessenich735d7e52017-07-13 11:39:16 -06003645 ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
John Kessenich4f1403e2017-04-05 17:38:20 -06003646 int dummySize;
3647 int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
3648 if (componentAlignment <= 4)
3649 memberAlignment = componentAlignment;
3650 }
3651
3652 // Bump up to member alignment
John Kessenich5e4b1242015-08-06 22:53:06 -06003653 glslang::RoundToPow2(currentOffset, memberAlignment);
John Kessenich4f1403e2017-04-05 17:38:20 -06003654
3655 // Bump up to vec4 if there is a bad straddle
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003656 if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset))
John Kessenich4f1403e2017-04-05 17:38:20 -06003657 glslang::RoundToPow2(currentOffset, 16);
3658
John Kessenich5e4b1242015-08-06 22:53:06 -06003659 nextOffset = currentOffset + memberSize;
3660}
3661
David Netoa901ffe2016-06-08 14:11:40 +01003662void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
John Kessenichebb50532016-05-16 19:22:05 -06003663{
David Netoa901ffe2016-06-08 14:11:40 +01003664 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
3665 switch (glslangBuiltIn)
3666 {
3667 case glslang::EbvClipDistance:
3668 case glslang::EbvCullDistance:
3669 case glslang::EbvPointSize:
chaoc771d89f2017-01-13 01:10:53 -08003670#ifdef NV_EXTENSIONS
chaoc771d89f2017-01-13 01:10:53 -08003671 case glslang::EbvViewportMaskNV:
3672 case glslang::EbvSecondaryPositionNV:
3673 case glslang::EbvSecondaryViewportMaskNV:
chaocdf3956c2017-02-14 14:52:34 -08003674 case glslang::EbvPositionPerViewNV:
3675 case glslang::EbvViewportMaskPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07003676 case glslang::EbvTaskCountNV:
3677 case glslang::EbvPrimitiveCountNV:
3678 case glslang::EbvPrimitiveIndicesNV:
3679 case glslang::EbvClipDistancePerViewNV:
3680 case glslang::EbvCullDistancePerViewNV:
3681 case glslang::EbvLayerPerViewNV:
3682 case glslang::EbvMeshViewCountNV:
3683 case glslang::EbvMeshViewIndicesNV:
chaoc771d89f2017-01-13 01:10:53 -08003684#endif
David Netoa901ffe2016-06-08 14:11:40 +01003685 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
3686 // Alternately, we could just call this for any glslang built-in, since the
3687 // capability already guards against duplicates.
3688 TranslateBuiltInDecoration(glslangBuiltIn, false);
3689 break;
3690 default:
3691 // Capabilities were already generated when the struct was declared.
3692 break;
3693 }
John Kessenichebb50532016-05-16 19:22:05 -06003694}
3695
John Kessenich6fccb3c2016-09-19 16:01:41 -06003696bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
John Kessenich140f3df2015-06-26 16:58:36 -06003697{
John Kessenicheee9d532016-09-19 18:09:30 -06003698 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
John Kessenich140f3df2015-06-26 16:58:36 -06003699}
3700
John Kessenichd41993d2017-09-10 15:21:05 -06003701// Does parameter need a place to keep writes, separate from the original?
John Kessenich6a14f782017-12-04 02:48:10 -07003702// Assumes called after originalParam(), which filters out block/buffer/opaque-based
3703// qualifiers such that we should have only in/out/inout/constreadonly here.
John Kessenichd3ed90b2018-05-04 11:43:03 -06003704bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
John Kessenichd41993d2017-09-10 15:21:05 -06003705{
John Kessenich6a14f782017-12-04 02:48:10 -07003706 assert(qualifier == glslang::EvqIn ||
3707 qualifier == glslang::EvqOut ||
3708 qualifier == glslang::EvqInOut ||
3709 qualifier == glslang::EvqConstReadOnly);
John Kessenichd41993d2017-09-10 15:21:05 -06003710 return qualifier != glslang::EvqConstReadOnly;
3711}
3712
3713// Is parameter pass-by-original?
3714bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
3715 bool implicitThisParam)
3716{
3717 if (implicitThisParam) // implicit this
3718 return true;
3719 if (glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich6a14f782017-12-04 02:48:10 -07003720 return paramType.getBasicType() == glslang::EbtBlock;
John Kessenichd41993d2017-09-10 15:21:05 -06003721 return paramType.containsOpaque() || // sampler, etc.
3722 (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO
3723}
3724
John Kessenich140f3df2015-06-26 16:58:36 -06003725// Make all the functions, skeletally, without actually visiting their bodies.
3726void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
3727{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003728 const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, bool useVulkanMemoryModel) {
John Kessenichfad62972017-07-18 02:35:46 -06003729 spv::Decoration paramPrecision = TranslatePrecisionDecoration(type);
3730 if (paramPrecision != spv::NoPrecision)
3731 decorations.push_back(paramPrecision);
Jeff Bolz36831c92018-09-05 10:11:41 -05003732 TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003733 if (type.getBasicType() == glslang::EbtReference) {
3734 // Original and non-writable params pass the pointer directly and
3735 // use restrict/aliased, others are stored to a pointer in Function
3736 // memory and use RestrictPointer/AliasedPointer.
3737 if (originalParam(type.getQualifier().storage, type, false) ||
3738 !writableParam(type.getQualifier().storage)) {
3739 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased);
3740 } else {
3741 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
3742 }
3743 }
John Kessenichfad62972017-07-18 02:35:46 -06003744 };
3745
John Kessenich140f3df2015-06-26 16:58:36 -06003746 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
3747 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
John Kessenich6fccb3c2016-09-19 16:01:41 -06003748 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
John Kessenich140f3df2015-06-26 16:58:36 -06003749 continue;
3750
3751 // We're on a user function. Set up the basic interface for the function now,
John Kessenich4bf71552016-09-02 11:20:21 -06003752 // so that it's available to call. Translating the body will happen later.
John Kessenich140f3df2015-06-26 16:58:36 -06003753 //
qining25262b32016-05-06 17:25:16 -04003754 // Typically (except for a "const in" parameter), an address will be passed to the
John Kessenich140f3df2015-06-26 16:58:36 -06003755 // function. What it is an address of varies:
3756 //
John Kessenich4bf71552016-09-02 11:20:21 -06003757 // - "in" parameters not marked as "const" can be written to without modifying the calling
3758 // argument so that write needs to be to a copy, hence the address of a copy works.
John Kessenich140f3df2015-06-26 16:58:36 -06003759 //
3760 // - "const in" parameters can just be the r-value, as no writes need occur.
3761 //
John Kessenich4bf71552016-09-02 11:20:21 -06003762 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
3763 // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy.
John Kessenich140f3df2015-06-26 16:58:36 -06003764
3765 std::vector<spv::Id> paramTypes;
John Kessenichfad62972017-07-18 02:35:46 -06003766 std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter
John Kessenich140f3df2015-06-26 16:58:36 -06003767 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
3768
John Kessenichfad62972017-07-18 02:35:46 -06003769 bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() ==
3770 glslangIntermediate->implicitThisName;
John Kessenich37789792017-03-21 23:56:40 -06003771
John Kessenichfad62972017-07-18 02:35:46 -06003772 paramDecorations.resize(parameters.size());
John Kessenich140f3df2015-06-26 16:58:36 -06003773 for (int p = 0; p < (int)parameters.size(); ++p) {
3774 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
3775 spv::Id typeId = convertGlslangToSpvType(paramType);
John Kessenichd41993d2017-09-10 15:21:05 -06003776 if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0))
John Kessenicha5c5fb62017-05-05 05:09:58 -06003777 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
John Kessenichd41993d2017-09-10 15:21:05 -06003778 else if (writableParam(paramType.getQualifier().storage))
John Kessenich140f3df2015-06-26 16:58:36 -06003779 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
3780 else
John Kessenich4bf71552016-09-02 11:20:21 -06003781 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
Jeff Bolz36831c92018-09-05 10:11:41 -05003782 getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich140f3df2015-06-26 16:58:36 -06003783 paramTypes.push_back(typeId);
3784 }
3785
3786 spv::Block* functionBlock;
John Kessenich32cfd492016-02-02 12:37:46 -07003787 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
3788 convertGlslangToSpvType(glslFunction->getType()),
John Kessenichfad62972017-07-18 02:35:46 -06003789 glslFunction->getName().c_str(), paramTypes,
3790 paramDecorations, &functionBlock);
John Kessenich37789792017-03-21 23:56:40 -06003791 if (implicitThis)
3792 function->setImplicitThis();
John Kessenich140f3df2015-06-26 16:58:36 -06003793
3794 // Track function to emit/call later
3795 functionMap[glslFunction->getName().c_str()] = function;
3796
3797 // Set the parameter id's
3798 for (int p = 0; p < (int)parameters.size(); ++p) {
3799 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
3800 // give a name too
3801 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
3802 }
3803 }
3804}
3805
3806// Process all the initializers, while skipping the functions and link objects
3807void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
3808{
3809 builder.setBuildPoint(shaderEntry->getLastBlock());
3810 for (int i = 0; i < (int)initializers.size(); ++i) {
3811 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
3812 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) {
3813
3814 // We're on a top-level node that's not a function. Treat as an initializer, whose
John Kessenich6fccb3c2016-09-19 16:01:41 -06003815 // code goes into the beginning of the entry point.
John Kessenich140f3df2015-06-26 16:58:36 -06003816 initializer->traverse(this);
3817 }
3818 }
3819}
3820
3821// Process all the functions, while skipping initializers.
3822void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
3823{
3824 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
3825 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
John Kessenich6a60c2f2016-12-08 21:01:59 -07003826 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
John Kessenich140f3df2015-06-26 16:58:36 -06003827 node->traverse(this);
3828 }
3829}
3830
3831void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
3832{
qining25262b32016-05-06 17:25:16 -04003833 // SPIR-V functions should already be in the functionMap from the prepass
John Kessenich140f3df2015-06-26 16:58:36 -06003834 // that called makeFunctions().
John Kesseniched33e052016-10-06 12:59:51 -06003835 currentFunction = functionMap[node->getName().c_str()];
3836 spv::Block* functionBlock = currentFunction->getEntryBlock();
John Kessenich140f3df2015-06-26 16:58:36 -06003837 builder.setBuildPoint(functionBlock);
3838}
3839
Rex Xu04db3f52015-09-16 11:44:02 +08003840void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06003841{
Rex Xufc618912015-09-09 16:42:49 +08003842 const glslang::TIntermSequence& glslangArguments = node.getSequence();
Rex Xu48edadf2015-12-31 16:11:41 +08003843
3844 glslang::TSampler sampler = {};
3845 bool cubeCompare = false;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003846#ifdef AMD_EXTENSIONS
3847 bool f16ShadowCompare = false;
3848#endif
Rex Xu5eafa472016-02-19 22:24:03 +08003849 if (node.isTexture() || node.isImage()) {
Rex Xu48edadf2015-12-31 16:11:41 +08003850 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
3851 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003852#ifdef AMD_EXTENSIONS
3853 f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16;
3854#endif
Rex Xu48edadf2015-12-31 16:11:41 +08003855 }
3856
John Kessenich140f3df2015-06-26 16:58:36 -06003857 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
3858 builder.clearAccessChain();
3859 glslangArguments[i]->traverse(this);
Rex Xufc618912015-09-09 16:42:49 +08003860
3861 // Special case l-value operands
3862 bool lvalue = false;
3863 switch (node.getOp()) {
3864 case glslang::EOpImageAtomicAdd:
3865 case glslang::EOpImageAtomicMin:
3866 case glslang::EOpImageAtomicMax:
3867 case glslang::EOpImageAtomicAnd:
3868 case glslang::EOpImageAtomicOr:
3869 case glslang::EOpImageAtomicXor:
3870 case glslang::EOpImageAtomicExchange:
3871 case glslang::EOpImageAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05003872 case glslang::EOpImageAtomicLoad:
3873 case glslang::EOpImageAtomicStore:
Rex Xufc618912015-09-09 16:42:49 +08003874 if (i == 0)
3875 lvalue = true;
3876 break;
Rex Xu5eafa472016-02-19 22:24:03 +08003877 case glslang::EOpSparseImageLoad:
3878 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
3879 lvalue = true;
3880 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003881#ifdef AMD_EXTENSIONS
3882 case glslang::EOpSparseTexture:
3883 if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2))
3884 lvalue = true;
3885 break;
3886 case glslang::EOpSparseTextureClamp:
3887 if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3))
3888 lvalue = true;
3889 break;
3890 case glslang::EOpSparseTextureLod:
3891 case glslang::EOpSparseTextureOffset:
3892 if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3))
3893 lvalue = true;
3894 break;
3895#else
Rex Xu48edadf2015-12-31 16:11:41 +08003896 case glslang::EOpSparseTexture:
3897 if ((cubeCompare && i == 3) || (! cubeCompare && i == 2))
3898 lvalue = true;
3899 break;
3900 case glslang::EOpSparseTextureClamp:
3901 if ((cubeCompare && i == 4) || (! cubeCompare && i == 3))
3902 lvalue = true;
3903 break;
3904 case glslang::EOpSparseTextureLod:
3905 case glslang::EOpSparseTextureOffset:
3906 if (i == 3)
3907 lvalue = true;
3908 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003909#endif
Rex Xu48edadf2015-12-31 16:11:41 +08003910 case glslang::EOpSparseTextureFetch:
3911 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
3912 lvalue = true;
3913 break;
3914 case glslang::EOpSparseTextureFetchOffset:
3915 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
3916 lvalue = true;
3917 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003918#ifdef AMD_EXTENSIONS
3919 case glslang::EOpSparseTextureLodOffset:
3920 case glslang::EOpSparseTextureGrad:
3921 case glslang::EOpSparseTextureOffsetClamp:
3922 if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4))
3923 lvalue = true;
3924 break;
3925 case glslang::EOpSparseTextureGradOffset:
3926 case glslang::EOpSparseTextureGradClamp:
3927 if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5))
3928 lvalue = true;
3929 break;
3930 case glslang::EOpSparseTextureGradOffsetClamp:
3931 if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6))
3932 lvalue = true;
3933 break;
3934#else
Rex Xu48edadf2015-12-31 16:11:41 +08003935 case glslang::EOpSparseTextureLodOffset:
3936 case glslang::EOpSparseTextureGrad:
3937 case glslang::EOpSparseTextureOffsetClamp:
3938 if (i == 4)
3939 lvalue = true;
3940 break;
3941 case glslang::EOpSparseTextureGradOffset:
3942 case glslang::EOpSparseTextureGradClamp:
3943 if (i == 5)
3944 lvalue = true;
3945 break;
3946 case glslang::EOpSparseTextureGradOffsetClamp:
3947 if (i == 6)
3948 lvalue = true;
3949 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08003950#endif
Rex Xu225e0fc2016-11-17 17:47:59 +08003951 case glslang::EOpSparseTextureGather:
Rex Xu48edadf2015-12-31 16:11:41 +08003952 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
3953 lvalue = true;
3954 break;
3955 case glslang::EOpSparseTextureGatherOffset:
3956 case glslang::EOpSparseTextureGatherOffsets:
3957 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
3958 lvalue = true;
3959 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08003960#ifdef AMD_EXTENSIONS
3961 case glslang::EOpSparseTextureGatherLod:
3962 if (i == 3)
3963 lvalue = true;
3964 break;
3965 case glslang::EOpSparseTextureGatherLodOffset:
3966 case glslang::EOpSparseTextureGatherLodOffsets:
3967 if (i == 4)
3968 lvalue = true;
3969 break;
Rex Xu129799a2017-07-05 17:23:28 +08003970 case glslang::EOpSparseImageLoadLod:
3971 if (i == 3)
3972 lvalue = true;
3973 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08003974#endif
Chao Chen3a137962018-09-19 11:41:27 -07003975#ifdef NV_EXTENSIONS
3976 case glslang::EOpImageSampleFootprintNV:
3977 if (i == 4)
3978 lvalue = true;
3979 break;
3980 case glslang::EOpImageSampleFootprintClampNV:
3981 case glslang::EOpImageSampleFootprintLodNV:
3982 if (i == 5)
3983 lvalue = true;
3984 break;
3985 case glslang::EOpImageSampleFootprintGradNV:
3986 if (i == 6)
3987 lvalue = true;
3988 break;
3989 case glslang::EOpImageSampleFootprintGradClampNV:
3990 if (i == 7)
3991 lvalue = true;
3992 break;
3993#endif
Rex Xufc618912015-09-09 16:42:49 +08003994 default:
3995 break;
3996 }
3997
Rex Xu6b86d492015-09-16 17:48:22 +08003998 if (lvalue)
Rex Xufc618912015-09-09 16:42:49 +08003999 arguments.push_back(builder.accessChainGetLValue());
Rex Xu6b86d492015-09-16 17:48:22 +08004000 else
John Kessenich32cfd492016-02-02 12:37:46 -07004001 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06004002 }
4003}
4004
John Kessenichfc51d282015-08-19 13:34:18 -06004005void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06004006{
John Kessenichfc51d282015-08-19 13:34:18 -06004007 builder.clearAccessChain();
4008 node.getOperand()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07004009 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
John Kessenichfc51d282015-08-19 13:34:18 -06004010}
John Kessenich140f3df2015-06-26 16:58:36 -06004011
John Kessenichfc51d282015-08-19 13:34:18 -06004012spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
4013{
John Kesseniche485c7a2017-05-31 18:50:53 -06004014 if (! node->isImage() && ! node->isTexture())
John Kessenichfc51d282015-08-19 13:34:18 -06004015 return spv::NoResult;
John Kesseniche485c7a2017-05-31 18:50:53 -06004016
greg-lunarg5d43c4a2018-12-07 17:36:33 -07004017 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06004018
John Kessenichfc51d282015-08-19 13:34:18 -06004019 // Process a GLSL texturing op (will be SPV image)
Jeff Bolz36831c92018-09-05 10:11:41 -05004020
4021 const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType()
4022 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType();
4023 const glslang::TSampler sampler = imageType.getSampler();
Rex Xu1e5d7b02016-11-29 17:36:31 +08004024#ifdef AMD_EXTENSIONS
4025 bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate())
4026 ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16
4027 : false;
4028#endif
4029
John Kessenichfc51d282015-08-19 13:34:18 -06004030 std::vector<spv::Id> arguments;
4031 if (node->getAsAggregate())
Rex Xufc618912015-09-09 16:42:49 +08004032 translateArguments(*node->getAsAggregate(), arguments);
John Kessenichfc51d282015-08-19 13:34:18 -06004033 else
4034 translateArguments(*node->getAsUnaryNode(), arguments);
John Kessenichf6640762016-08-01 19:44:00 -06004035 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenichfc51d282015-08-19 13:34:18 -06004036
4037 spv::Builder::TextureParameters params = { };
4038 params.sampler = arguments[0];
4039
Rex Xu04db3f52015-09-16 11:44:02 +08004040 glslang::TCrackedTextureOp cracked;
4041 node->crackTexture(sampler, cracked);
4042
amhagan05506bb2017-06-13 16:53:02 -04004043 const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint;
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004044
John Kessenichfc51d282015-08-19 13:34:18 -06004045 // Check for queries
4046 if (cracked.query) {
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004047 // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first
4048 if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler))
John Kessenich33661452015-12-08 19:32:47 -07004049 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004050
John Kessenichfc51d282015-08-19 13:34:18 -06004051 switch (node->getOp()) {
4052 case glslang::EOpImageQuerySize:
4053 case glslang::EOpTextureQuerySize:
John Kessenich140f3df2015-06-26 16:58:36 -06004054 if (arguments.size() > 1) {
4055 params.lod = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004056 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult);
John Kessenich140f3df2015-06-26 16:58:36 -06004057 } else
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004058 return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004059 case glslang::EOpImageQuerySamples:
4060 case glslang::EOpTextureQuerySamples:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004061 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004062 case glslang::EOpTextureQueryLod:
4063 params.coords = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004064 return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004065 case glslang::EOpTextureQueryLevels:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004066 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult);
Rex Xu48edadf2015-12-31 16:11:41 +08004067 case glslang::EOpSparseTexelsResident:
4068 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
John Kessenichfc51d282015-08-19 13:34:18 -06004069 default:
4070 assert(0);
4071 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004072 }
John Kessenich140f3df2015-06-26 16:58:36 -06004073 }
4074
LoopDawg4425f242018-02-18 11:40:01 -07004075 int components = node->getType().getVectorSize();
4076
4077 if (node->getOp() == glslang::EOpTextureFetch) {
4078 // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed.
4079 // This will only happen through the HLSL path for operator[], so we do not have to handle e.g.
4080 // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic
4081 // here around e.g. which ones return scalars or other types.
4082 components = 4;
4083 }
4084
4085 glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components);
4086
4087 auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); };
4088
Rex Xufc618912015-09-09 16:42:49 +08004089 // Check for image functions other than queries
4090 if (node->isImage()) {
John Kessenich149afc32018-08-14 13:31:43 -06004091 std::vector<spv::IdImmediate> operands;
John Kessenich56bab042015-09-16 10:54:31 -06004092 auto opIt = arguments.begin();
John Kessenich149afc32018-08-14 13:31:43 -06004093 spv::IdImmediate image = { true, *(opIt++) };
4094 operands.push_back(image);
John Kessenich6c292d32016-02-15 20:58:50 -07004095
4096 // Handle subpass operations
4097 // TODO: GLSL should change to have the "MS" only on the type rather than the
4098 // built-in function.
4099 if (cracked.subpass) {
4100 // add on the (0,0) coordinate
4101 spv::Id zero = builder.makeIntConstant(0);
4102 std::vector<spv::Id> comps;
4103 comps.push_back(zero);
4104 comps.push_back(zero);
John Kessenich149afc32018-08-14 13:31:43 -06004105 spv::IdImmediate coord = { true,
4106 builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) };
4107 operands.push_back(coord);
John Kessenich6c292d32016-02-15 20:58:50 -07004108 if (sampler.ms) {
John Kessenich149afc32018-08-14 13:31:43 -06004109 spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask };
4110 operands.push_back(imageOperands);
4111 spv::IdImmediate imageOperand = { true, *(opIt++) };
4112 operands.push_back(imageOperand);
John Kessenich6c292d32016-02-15 20:58:50 -07004113 }
John Kessenichfe4e5722017-10-19 02:07:30 -06004114 spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands);
4115 builder.setPrecision(result, precision);
4116 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07004117 }
4118
John Kessenich149afc32018-08-14 13:31:43 -06004119 spv::IdImmediate coord = { true, *(opIt++) };
4120 operands.push_back(coord);
Rex Xu129799a2017-07-05 17:23:28 +08004121#ifdef AMD_EXTENSIONS
4122 if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) {
4123#else
John Kessenich56bab042015-09-16 10:54:31 -06004124 if (node->getOp() == glslang::EOpImageLoad) {
Rex Xu129799a2017-07-05 17:23:28 +08004125#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05004126 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich55e7d112015-11-15 21:33:39 -07004127 if (sampler.ms) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004128 mask = mask | spv::ImageOperandsSampleMask;
4129 }
Rex Xu129799a2017-07-05 17:23:28 +08004130#ifdef AMD_EXTENSIONS
Jeff Bolz36831c92018-09-05 10:11:41 -05004131 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004132 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4133 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
Jeff Bolz36831c92018-09-05 10:11:41 -05004134 mask = mask | spv::ImageOperandsLodMask;
John Kessenich55e7d112015-11-15 21:33:39 -07004135 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004136#endif
4137 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4138 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
4139 if (mask) {
4140 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4141 operands.push_back(imageOperands);
4142 }
4143 if (mask & spv::ImageOperandsSampleMask) {
4144 spv::IdImmediate imageOperand = { true, *opIt++ };
4145 operands.push_back(imageOperand);
4146 }
4147#ifdef AMD_EXTENSIONS
4148 if (mask & spv::ImageOperandsLodMask) {
4149 spv::IdImmediate imageOperand = { true, *opIt++ };
4150 operands.push_back(imageOperand);
4151 }
4152#endif
4153 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
4154 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4155 operands.push_back(imageOperand);
4156 }
4157
John Kessenich149afc32018-08-14 13:31:43 -06004158 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004159 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
John Kessenichfe4e5722017-10-19 02:07:30 -06004160
John Kessenich149afc32018-08-14 13:31:43 -06004161 std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands));
LoopDawg4425f242018-02-18 11:40:01 -07004162 builder.setPrecision(result[0], precision);
4163
4164 // If needed, add a conversion constructor to the proper size.
4165 if (components != node->getType().getVectorSize())
4166 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4167
4168 return result[0];
Rex Xu129799a2017-07-05 17:23:28 +08004169#ifdef AMD_EXTENSIONS
4170 } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) {
4171#else
John Kessenich56bab042015-09-16 10:54:31 -06004172 } else if (node->getOp() == glslang::EOpImageStore) {
Rex Xu129799a2017-07-05 17:23:28 +08004173#endif
Rex Xu129799a2017-07-05 17:23:28 +08004174
Jeff Bolz36831c92018-09-05 10:11:41 -05004175 // Push the texel value before the operands
4176#ifdef AMD_EXTENSIONS
4177 if (sampler.ms || cracked.lod) {
4178#else
4179 if (sampler.ms) {
4180#endif
John Kessenich149afc32018-08-14 13:31:43 -06004181 spv::IdImmediate texel = { true, *(opIt + 1) };
4182 operands.push_back(texel);
John Kessenich149afc32018-08-14 13:31:43 -06004183 } else {
4184 spv::IdImmediate texel = { true, *opIt };
4185 operands.push_back(texel);
4186 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004187
4188 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
4189 if (sampler.ms) {
4190 mask = mask | spv::ImageOperandsSampleMask;
4191 }
4192#ifdef AMD_EXTENSIONS
4193 if (cracked.lod) {
4194 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4195 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4196 mask = mask | spv::ImageOperandsLodMask;
4197 }
4198#endif
4199 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4200 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask);
4201 if (mask) {
4202 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4203 operands.push_back(imageOperands);
4204 }
4205 if (mask & spv::ImageOperandsSampleMask) {
4206 spv::IdImmediate imageOperand = { true, *opIt++ };
4207 operands.push_back(imageOperand);
4208 }
4209#ifdef AMD_EXTENSIONS
4210 if (mask & spv::ImageOperandsLodMask) {
4211 spv::IdImmediate imageOperand = { true, *opIt++ };
4212 operands.push_back(imageOperand);
4213 }
4214#endif
4215 if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) {
4216 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4217 operands.push_back(imageOperand);
4218 }
4219
John Kessenich56bab042015-09-16 10:54:31 -06004220 builder.createNoResultOp(spv::OpImageWrite, operands);
John Kessenich149afc32018-08-14 13:31:43 -06004221 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004222 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
John Kessenich56bab042015-09-16 10:54:31 -06004223 return spv::NoResult;
Rex Xu129799a2017-07-05 17:23:28 +08004224#ifdef AMD_EXTENSIONS
4225 } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) {
4226#else
Rex Xu5eafa472016-02-19 22:24:03 +08004227 } else if (node->getOp() == glslang::EOpSparseImageLoad) {
Rex Xu129799a2017-07-05 17:23:28 +08004228#endif
Rex Xu5eafa472016-02-19 22:24:03 +08004229 builder.addCapability(spv::CapabilitySparseResidency);
John Kessenich149afc32018-08-14 13:31:43 -06004230 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
Rex Xu5eafa472016-02-19 22:24:03 +08004231 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
4232
Jeff Bolz36831c92018-09-05 10:11:41 -05004233 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
Rex Xu5eafa472016-02-19 22:24:03 +08004234 if (sampler.ms) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004235 mask = mask | spv::ImageOperandsSampleMask;
4236 }
Rex Xu129799a2017-07-05 17:23:28 +08004237#ifdef AMD_EXTENSIONS
Jeff Bolz36831c92018-09-05 10:11:41 -05004238 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004239 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4240 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4241
Jeff Bolz36831c92018-09-05 10:11:41 -05004242 mask = mask | spv::ImageOperandsLodMask;
4243 }
4244#endif
4245 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4246 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
4247 if (mask) {
4248 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
John Kessenich149afc32018-08-14 13:31:43 -06004249 operands.push_back(imageOperands);
Jeff Bolz36831c92018-09-05 10:11:41 -05004250 }
4251 if (mask & spv::ImageOperandsSampleMask) {
John Kessenich149afc32018-08-14 13:31:43 -06004252 spv::IdImmediate imageOperand = { true, *opIt++ };
4253 operands.push_back(imageOperand);
Jeff Bolz36831c92018-09-05 10:11:41 -05004254 }
4255#ifdef AMD_EXTENSIONS
4256 if (mask & spv::ImageOperandsLodMask) {
4257 spv::IdImmediate imageOperand = { true, *opIt++ };
4258 operands.push_back(imageOperand);
4259 }
Rex Xu129799a2017-07-05 17:23:28 +08004260#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05004261 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
4262 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4263 operands.push_back(imageOperand);
Rex Xu5eafa472016-02-19 22:24:03 +08004264 }
4265
4266 // Create the return type that was a special structure
4267 spv::Id texelOut = *opIt;
John Kessenich8c8505c2016-07-26 12:50:38 -06004268 spv::Id typeId0 = resultType();
Rex Xu5eafa472016-02-19 22:24:03 +08004269 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
4270 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
4271
4272 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
4273
4274 // Decode the return type
4275 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
4276 return builder.createCompositeExtract(resultId, typeId0, 0);
John Kessenichcd261442016-01-22 09:54:12 -07004277 } else {
Rex Xu6b86d492015-09-16 17:48:22 +08004278 // Process image atomic operations
4279
4280 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
4281 // as the first source operand, is required by SPIR-V atomic operations.
John Kessenich149afc32018-08-14 13:31:43 -06004282 // For non-MS, the sample value should be 0
4283 spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) };
4284 operands.push_back(sample);
John Kessenich140f3df2015-06-26 16:58:36 -06004285
Jeff Bolz36831c92018-09-05 10:11:41 -05004286 spv::Id resultTypeId;
4287 // imageAtomicStore has a void return type so base the pointer type on
4288 // the type of the value operand.
4289 if (node->getOp() == glslang::EOpImageAtomicStore) {
4290 resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word));
4291 } else {
4292 resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
4293 }
John Kessenich56bab042015-09-16 10:54:31 -06004294 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
Rex Xufc618912015-09-09 16:42:49 +08004295
4296 std::vector<spv::Id> operands;
4297 operands.push_back(pointer);
4298 for (; opIt != arguments.end(); ++opIt)
4299 operands.push_back(*opIt);
4300
John Kessenich8c8505c2016-07-26 12:50:38 -06004301 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
Rex Xufc618912015-09-09 16:42:49 +08004302 }
4303 }
4304
amhagan05506bb2017-06-13 16:53:02 -04004305#ifdef AMD_EXTENSIONS
4306 // Check for fragment mask functions other than queries
4307 if (cracked.fragMask) {
4308 assert(sampler.ms);
4309
4310 auto opIt = arguments.begin();
4311 std::vector<spv::Id> operands;
4312
4313 // Extract the image if necessary
4314 if (builder.isSampledImage(params.sampler))
4315 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4316
4317 operands.push_back(params.sampler);
4318 ++opIt;
4319
4320 if (sampler.isSubpass()) {
4321 // add on the (0,0) coordinate
4322 spv::Id zero = builder.makeIntConstant(0);
4323 std::vector<spv::Id> comps;
4324 comps.push_back(zero);
4325 comps.push_back(zero);
4326 operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps));
4327 }
4328
4329 for (; opIt != arguments.end(); ++opIt)
4330 operands.push_back(*opIt);
4331
4332 spv::Op fragMaskOp = spv::OpNop;
4333 if (node->getOp() == glslang::EOpFragmentMaskFetch)
4334 fragMaskOp = spv::OpFragmentMaskFetchAMD;
4335 else if (node->getOp() == glslang::EOpFragmentFetch)
4336 fragMaskOp = spv::OpFragmentFetchAMD;
4337
4338 builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask);
4339 builder.addCapability(spv::CapabilityFragmentMaskAMD);
4340 return builder.createOp(fragMaskOp, resultType(), operands);
4341 }
4342#endif
4343
Rex Xufc618912015-09-09 16:42:49 +08004344 // Check for texture functions other than queries
Rex Xu48edadf2015-12-31 16:11:41 +08004345 bool sparse = node->isSparseTexture();
Chao Chen3a137962018-09-19 11:41:27 -07004346#ifdef NV_EXTENSIONS
4347 bool imageFootprint = node->isImageFootprint();
4348#endif
4349
Rex Xu71519fe2015-11-11 15:35:47 +08004350 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
4351
John Kessenichfc51d282015-08-19 13:34:18 -06004352 // check for bias argument
4353 bool bias = false;
Rex Xu225e0fc2016-11-17 17:47:59 +08004354#ifdef AMD_EXTENSIONS
4355 if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
4356#else
Rex Xu71519fe2015-11-11 15:35:47 +08004357 if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
Rex Xu225e0fc2016-11-17 17:47:59 +08004358#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004359 int nonBiasArgCount = 2;
Rex Xu225e0fc2016-11-17 17:47:59 +08004360#ifdef AMD_EXTENSIONS
4361 if (cracked.gather)
4362 ++nonBiasArgCount; // comp argument should be present when bias argument is present
Rex Xu1e5d7b02016-11-29 17:36:31 +08004363
4364 if (f16ShadowCompare)
4365 ++nonBiasArgCount;
Rex Xu225e0fc2016-11-17 17:47:59 +08004366#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004367 if (cracked.offset)
4368 ++nonBiasArgCount;
Rex Xu225e0fc2016-11-17 17:47:59 +08004369#ifdef AMD_EXTENSIONS
4370 else if (cracked.offsets)
4371 ++nonBiasArgCount;
4372#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004373 if (cracked.grad)
4374 nonBiasArgCount += 2;
Rex Xu48edadf2015-12-31 16:11:41 +08004375 if (cracked.lodClamp)
4376 ++nonBiasArgCount;
4377 if (sparse)
4378 ++nonBiasArgCount;
Chao Chen3a137962018-09-19 11:41:27 -07004379#ifdef NV_EXTENSIONS
4380 if (imageFootprint)
4381 //Following three extra arguments
4382 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4383 nonBiasArgCount += 3;
4384#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004385 if ((int)arguments.size() > nonBiasArgCount)
4386 bias = true;
4387 }
4388
John Kessenicha5c33d62016-06-02 23:45:21 -06004389 // See if the sampler param should really be just the SPV image part
4390 if (cracked.fetch) {
4391 // a fetch needs to have the image extracted first
4392 if (builder.isSampledImage(params.sampler))
4393 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4394 }
4395
Rex Xu225e0fc2016-11-17 17:47:59 +08004396#ifdef AMD_EXTENSIONS
4397 if (cracked.gather) {
4398 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
4399 if (bias || cracked.lod ||
4400 sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) {
4401 builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod);
Rex Xu301a2bc2017-06-14 23:09:39 +08004402 builder.addCapability(spv::CapabilityImageGatherBiasLodAMD);
Rex Xu225e0fc2016-11-17 17:47:59 +08004403 }
4404 }
4405#endif
4406
John Kessenichfc51d282015-08-19 13:34:18 -06004407 // set the rest of the arguments
John Kessenich55e7d112015-11-15 21:33:39 -07004408
John Kessenichfc51d282015-08-19 13:34:18 -06004409 params.coords = arguments[1];
4410 int extraArgs = 0;
John Kessenich019f08f2016-02-15 15:40:42 -07004411 bool noImplicitLod = false;
John Kessenich55e7d112015-11-15 21:33:39 -07004412
4413 // sort out where Dref is coming from
Rex Xu1e5d7b02016-11-29 17:36:31 +08004414#ifdef AMD_EXTENSIONS
4415 if (cubeCompare || f16ShadowCompare) {
4416#else
Rex Xu48edadf2015-12-31 16:11:41 +08004417 if (cubeCompare) {
Rex Xu1e5d7b02016-11-29 17:36:31 +08004418#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004419 params.Dref = arguments[2];
Rex Xu48edadf2015-12-31 16:11:41 +08004420 ++extraArgs;
4421 } else if (sampler.shadow && cracked.gather) {
John Kessenich55e7d112015-11-15 21:33:39 -07004422 params.Dref = arguments[2];
4423 ++extraArgs;
4424 } else if (sampler.shadow) {
John Kessenichfc51d282015-08-19 13:34:18 -06004425 std::vector<spv::Id> indexes;
John Kessenich76d4dfc2016-06-16 12:43:23 -06004426 int dRefComp;
John Kessenichfc51d282015-08-19 13:34:18 -06004427 if (cracked.proj)
John Kessenich76d4dfc2016-06-16 12:43:23 -06004428 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
John Kessenichfc51d282015-08-19 13:34:18 -06004429 else
John Kessenich76d4dfc2016-06-16 12:43:23 -06004430 dRefComp = builder.getNumComponents(params.coords) - 1;
4431 indexes.push_back(dRefComp);
John Kessenichfc51d282015-08-19 13:34:18 -06004432 params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
4433 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004434
4435 // lod
John Kessenichfc51d282015-08-19 13:34:18 -06004436 if (cracked.lod) {
LoopDawgef94b1a2017-07-24 18:45:37 -06004437 params.lod = arguments[2 + extraArgs];
John Kessenichfc51d282015-08-19 13:34:18 -06004438 ++extraArgs;
Chao Chenbeae2252018-09-19 11:40:45 -07004439 } else if (glslangIntermediate->getStage() != EShLangFragment
4440#ifdef NV_EXTENSIONS
4441 // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs
4442 && !(glslangIntermediate->getStage() == EShLangCompute &&
4443 (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone))
4444#endif
4445 ) {
John Kessenich019f08f2016-02-15 15:40:42 -07004446 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
4447 noImplicitLod = true;
4448 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004449
4450 // multisample
John Kessenich019f08f2016-02-15 15:40:42 -07004451 if (sampler.ms) {
LoopDawgef94b1a2017-07-24 18:45:37 -06004452 params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified
Rex Xu04db3f52015-09-16 11:44:02 +08004453 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06004454 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004455
4456 // gradient
John Kessenichfc51d282015-08-19 13:34:18 -06004457 if (cracked.grad) {
4458 params.gradX = arguments[2 + extraArgs];
4459 params.gradY = arguments[3 + extraArgs];
4460 extraArgs += 2;
4461 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004462
4463 // offset and offsets
John Kessenich55e7d112015-11-15 21:33:39 -07004464 if (cracked.offset) {
John Kessenichfc51d282015-08-19 13:34:18 -06004465 params.offset = arguments[2 + extraArgs];
4466 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07004467 } else if (cracked.offsets) {
4468 params.offsets = arguments[2 + extraArgs];
4469 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06004470 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004471
4472 // lod clamp
Rex Xu48edadf2015-12-31 16:11:41 +08004473 if (cracked.lodClamp) {
4474 params.lodClamp = arguments[2 + extraArgs];
4475 ++extraArgs;
4476 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004477 // sparse
Rex Xu48edadf2015-12-31 16:11:41 +08004478 if (sparse) {
4479 params.texelOut = arguments[2 + extraArgs];
4480 ++extraArgs;
4481 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004482
John Kessenich76d4dfc2016-06-16 12:43:23 -06004483 // gather component
John Kessenich55e7d112015-11-15 21:33:39 -07004484 if (cracked.gather && ! sampler.shadow) {
4485 // default component is 0, if missing, otherwise an argument
4486 if (2 + extraArgs < (int)arguments.size()) {
John Kessenich76d4dfc2016-06-16 12:43:23 -06004487 params.component = arguments[2 + extraArgs];
John Kessenich55e7d112015-11-15 21:33:39 -07004488 ++extraArgs;
Rex Xu225e0fc2016-11-17 17:47:59 +08004489 } else
John Kessenich76d4dfc2016-06-16 12:43:23 -06004490 params.component = builder.makeIntConstant(0);
Rex Xu225e0fc2016-11-17 17:47:59 +08004491 }
Chao Chen3a137962018-09-19 11:41:27 -07004492#ifdef NV_EXTENSIONS
4493 spv::Id resultStruct = spv::NoResult;
4494 if (imageFootprint) {
4495 //Following three extra arguments
4496 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4497 params.granularity = arguments[2 + extraArgs];
4498 params.coarse = arguments[3 + extraArgs];
4499 resultStruct = arguments[4 + extraArgs];
4500 extraArgs += 3;
4501 }
4502#endif
Rex Xu225e0fc2016-11-17 17:47:59 +08004503 // bias
4504 if (bias) {
4505 params.bias = arguments[2 + extraArgs];
4506 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07004507 }
John Kessenichfc51d282015-08-19 13:34:18 -06004508
Chao Chen3a137962018-09-19 11:41:27 -07004509#ifdef NV_EXTENSIONS
4510 if (imageFootprint) {
4511 builder.addExtension(spv::E_SPV_NV_shader_image_footprint);
4512 builder.addCapability(spv::CapabilityImageFootprintNV);
4513
4514
4515 //resultStructType(OpenGL type) contains 5 elements:
4516 //struct gl_TextureFootprint2DNV {
4517 // uvec2 anchor;
4518 // uvec2 offset;
4519 // uvec2 mask;
4520 // uint lod;
4521 // uint granularity;
4522 //};
4523 //or
4524 //struct gl_TextureFootprint3DNV {
4525 // uvec3 anchor;
4526 // uvec3 offset;
4527 // uvec2 mask;
4528 // uint lod;
4529 // uint granularity;
4530 //};
4531 spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct));
4532 assert(builder.isStructType(resultStructType));
4533
4534 //resType (SPIR-V type) contains 6 elements:
4535 //Member 0 must be a Boolean type scalar(LOD),
4536 //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor),
4537 //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset),
4538 //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask),
4539 //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod),
4540 //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity).
4541 std::vector<spv::Id> members;
4542 members.push_back(resultType());
4543 for (int i = 0; i < 5; i++) {
4544 members.push_back(builder.getContainedTypeId(resultStructType, i));
4545 }
4546 spv::Id resType = builder.makeStructType(members, "ResType");
4547
4548 //call ImageFootprintNV
4549 spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params);
4550
4551 //copy resType (SPIR-V type) to resultStructType(OpenGL type)
4552 for (int i = 0; i < 5; i++) {
4553 builder.clearAccessChain();
4554 builder.setAccessChainLValue(resultStruct);
4555
4556 //Accessing to a struct we created, no coherent flag is set
4557 spv::Builder::AccessChain::CoherentFlags flags;
4558 flags.clear();
4559
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004560 builder.accessChainPush(builder.makeIntConstant(i), flags, 0);
Chao Chen3a137962018-09-19 11:41:27 -07004561 builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1));
4562 }
4563 return builder.createCompositeExtract(res, resultType(), 0);
4564 }
4565#endif
4566
John Kessenich65336482016-06-16 14:06:26 -06004567 // projective component (might not to move)
4568 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
4569 // are divided by the last component of P."
4570 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
4571 // unused components will appear after all used components."
4572 if (cracked.proj) {
4573 int projSourceComp = builder.getNumComponents(params.coords) - 1;
4574 int projTargetComp;
4575 switch (sampler.dim) {
4576 case glslang::Esd1D: projTargetComp = 1; break;
4577 case glslang::Esd2D: projTargetComp = 2; break;
4578 case glslang::EsdRect: projTargetComp = 2; break;
4579 default: projTargetComp = projSourceComp; break;
4580 }
4581 // copy the projective coordinate if we have to
4582 if (projTargetComp != projSourceComp) {
John Kessenichecba76f2017-01-06 00:34:48 -07004583 spv::Id projComp = builder.createCompositeExtract(params.coords,
John Kessenich65336482016-06-16 14:06:26 -06004584 builder.getScalarTypeId(builder.getTypeId(params.coords)),
4585 projSourceComp);
4586 params.coords = builder.createCompositeInsert(projComp, params.coords,
4587 builder.getTypeId(params.coords), projTargetComp);
4588 }
4589 }
4590
Jeff Bolz36831c92018-09-05 10:11:41 -05004591 // nonprivate
4592 if (imageType.getQualifier().nonprivate) {
4593 params.nonprivate = true;
4594 }
4595
4596 // volatile
4597 if (imageType.getQualifier().volatil) {
4598 params.volatil = true;
4599 }
4600
St0fFa1184dd2018-04-09 21:08:14 +02004601 std::vector<spv::Id> result( 1,
LoopDawg4425f242018-02-18 11:40:01 -07004602 builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params)
St0fFa1184dd2018-04-09 21:08:14 +02004603 );
LoopDawg4425f242018-02-18 11:40:01 -07004604
4605 if (components != node->getType().getVectorSize())
4606 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4607
4608 return result[0];
John Kessenich140f3df2015-06-26 16:58:36 -06004609}
4610
4611spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
4612{
4613 // Grab the function's pointer from the previously created function
4614 spv::Function* function = functionMap[node->getName().c_str()];
4615 if (! function)
4616 return 0;
4617
4618 const glslang::TIntermSequence& glslangArgs = node->getSequence();
4619 const glslang::TQualifierList& qualifiers = node->getQualifierList();
4620
4621 // See comments in makeFunctions() for details about the semantics for parameter passing.
4622 //
4623 // These imply we need a four step process:
4624 // 1. Evaluate the arguments
4625 // 2. Allocate and make copies of in, out, and inout arguments
4626 // 3. Make the call
4627 // 4. Copy back the results
4628
John Kessenichd3ed90b2018-05-04 11:43:03 -06004629 // 1. Evaluate the arguments and their types
John Kessenich140f3df2015-06-26 16:58:36 -06004630 std::vector<spv::Builder::AccessChain> lValues;
4631 std::vector<spv::Id> rValues;
John Kessenich32cfd492016-02-02 12:37:46 -07004632 std::vector<const glslang::TType*> argTypes;
John Kessenich140f3df2015-06-26 16:58:36 -06004633 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06004634 argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06004635 // build l-value
4636 builder.clearAccessChain();
4637 glslangArgs[a]->traverse(this);
John Kessenichd41993d2017-09-10 15:21:05 -06004638 // keep outputs and pass-by-originals as l-values, evaluate others as r-values
John Kessenichd3ed90b2018-05-04 11:43:03 -06004639 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) ||
John Kessenich6a14f782017-12-04 02:48:10 -07004640 writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06004641 // save l-value
4642 lValues.push_back(builder.getAccessChain());
4643 } else {
4644 // process r-value
John Kessenich32cfd492016-02-02 12:37:46 -07004645 rValues.push_back(accessChainLoad(*argTypes.back()));
John Kessenich140f3df2015-06-26 16:58:36 -06004646 }
4647 }
4648
4649 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
4650 // copy the original into that space.
4651 //
4652 // Also, build up the list of actual arguments to pass in for the call
4653 int lValueCount = 0;
4654 int rValueCount = 0;
4655 std::vector<spv::Id> spvArgs;
4656 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
4657 spv::Id arg;
John Kessenichd3ed90b2018-05-04 11:43:03 -06004658 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07004659 builder.setAccessChain(lValues[lValueCount]);
4660 arg = builder.accessChainGetLValue();
4661 ++lValueCount;
John Kessenichd41993d2017-09-10 15:21:05 -06004662 } else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06004663 // need space to hold the copy
John Kessenichd3ed90b2018-05-04 11:43:03 -06004664 arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param");
John Kessenich140f3df2015-06-26 16:58:36 -06004665 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
4666 // need to copy the input into output space
4667 builder.setAccessChain(lValues[lValueCount]);
John Kessenich32cfd492016-02-02 12:37:46 -07004668 spv::Id copy = accessChainLoad(*argTypes[a]);
John Kessenich4bf71552016-09-02 11:20:21 -06004669 builder.clearAccessChain();
4670 builder.setAccessChainLValue(arg);
John Kessenichd3ed90b2018-05-04 11:43:03 -06004671 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06004672 }
4673 ++lValueCount;
4674 } else {
John Kessenichd3ed90b2018-05-04 11:43:03 -06004675 // process r-value, which involves a copy for a type mismatch
4676 if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) {
4677 spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg");
4678 builder.clearAccessChain();
4679 builder.setAccessChainLValue(argCopy);
4680 multiTypeStore(*argTypes[a], rValues[rValueCount]);
4681 arg = builder.createLoad(argCopy);
4682 } else
4683 arg = rValues[rValueCount];
John Kessenich140f3df2015-06-26 16:58:36 -06004684 ++rValueCount;
4685 }
4686 spvArgs.push_back(arg);
4687 }
4688
4689 // 3. Make the call.
4690 spv::Id result = builder.createFunctionCall(function, spvArgs);
John Kessenich32cfd492016-02-02 12:37:46 -07004691 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06004692
4693 // 4. Copy back out an "out" arguments.
4694 lValueCount = 0;
4695 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06004696 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0))
John Kessenichd41993d2017-09-10 15:21:05 -06004697 ++lValueCount;
4698 else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06004699 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
4700 spv::Id copy = builder.createLoad(spvArgs[a]);
4701 builder.setAccessChain(lValues[lValueCount]);
John Kessenichd3ed90b2018-05-04 11:43:03 -06004702 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06004703 }
4704 ++lValueCount;
4705 }
4706 }
4707
4708 return result;
4709}
4710
4711// Translate AST operation to SPV operation, already having SPV-based operands/types.
John Kessenichead86222018-03-28 18:01:20 -06004712spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06004713 spv::Id typeId, spv::Id left, spv::Id right,
4714 glslang::TBasicType typeProxy, bool reduceComparison)
4715{
John Kessenich66011cb2018-03-06 16:12:04 -07004716 bool isUnsigned = isTypeUnsignedInt(typeProxy);
4717 bool isFloat = isTypeFloat(typeProxy);
Rex Xuc7d36562016-04-27 08:15:37 +08004718 bool isBool = typeProxy == glslang::EbtBool;
John Kessenich140f3df2015-06-26 16:58:36 -06004719
4720 spv::Op binOp = spv::OpNop;
John Kessenichec43d0a2015-07-04 17:17:31 -06004721 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
John Kessenich140f3df2015-06-26 16:58:36 -06004722 bool comparison = false;
4723
4724 switch (op) {
4725 case glslang::EOpAdd:
4726 case glslang::EOpAddAssign:
4727 if (isFloat)
4728 binOp = spv::OpFAdd;
4729 else
4730 binOp = spv::OpIAdd;
4731 break;
4732 case glslang::EOpSub:
4733 case glslang::EOpSubAssign:
4734 if (isFloat)
4735 binOp = spv::OpFSub;
4736 else
4737 binOp = spv::OpISub;
4738 break;
4739 case glslang::EOpMul:
4740 case glslang::EOpMulAssign:
4741 if (isFloat)
4742 binOp = spv::OpFMul;
4743 else
4744 binOp = spv::OpIMul;
4745 break;
4746 case glslang::EOpVectorTimesScalar:
4747 case glslang::EOpVectorTimesScalarAssign:
John Kessenich8d72f1a2016-05-20 12:06:03 -06004748 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
John Kessenichec43d0a2015-07-04 17:17:31 -06004749 if (builder.isVector(right))
4750 std::swap(left, right);
4751 assert(builder.isScalar(right));
4752 needMatchingVectors = false;
4753 binOp = spv::OpVectorTimesScalar;
t.jung697fdf02018-11-14 13:04:39 +01004754 } else if (isFloat)
4755 binOp = spv::OpFMul;
4756 else
John Kessenichec43d0a2015-07-04 17:17:31 -06004757 binOp = spv::OpIMul;
John Kessenich140f3df2015-06-26 16:58:36 -06004758 break;
4759 case glslang::EOpVectorTimesMatrix:
4760 case glslang::EOpVectorTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06004761 binOp = spv::OpVectorTimesMatrix;
4762 break;
4763 case glslang::EOpMatrixTimesVector:
John Kessenich140f3df2015-06-26 16:58:36 -06004764 binOp = spv::OpMatrixTimesVector;
4765 break;
4766 case glslang::EOpMatrixTimesScalar:
4767 case glslang::EOpMatrixTimesScalarAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06004768 binOp = spv::OpMatrixTimesScalar;
4769 break;
4770 case glslang::EOpMatrixTimesMatrix:
4771 case glslang::EOpMatrixTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06004772 binOp = spv::OpMatrixTimesMatrix;
4773 break;
4774 case glslang::EOpOuterProduct:
4775 binOp = spv::OpOuterProduct;
John Kessenichec43d0a2015-07-04 17:17:31 -06004776 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06004777 break;
4778
4779 case glslang::EOpDiv:
4780 case glslang::EOpDivAssign:
4781 if (isFloat)
4782 binOp = spv::OpFDiv;
4783 else if (isUnsigned)
4784 binOp = spv::OpUDiv;
4785 else
4786 binOp = spv::OpSDiv;
4787 break;
4788 case glslang::EOpMod:
4789 case glslang::EOpModAssign:
4790 if (isFloat)
4791 binOp = spv::OpFMod;
4792 else if (isUnsigned)
4793 binOp = spv::OpUMod;
4794 else
4795 binOp = spv::OpSMod;
4796 break;
4797 case glslang::EOpRightShift:
4798 case glslang::EOpRightShiftAssign:
4799 if (isUnsigned)
4800 binOp = spv::OpShiftRightLogical;
4801 else
4802 binOp = spv::OpShiftRightArithmetic;
4803 break;
4804 case glslang::EOpLeftShift:
4805 case glslang::EOpLeftShiftAssign:
4806 binOp = spv::OpShiftLeftLogical;
4807 break;
4808 case glslang::EOpAnd:
4809 case glslang::EOpAndAssign:
4810 binOp = spv::OpBitwiseAnd;
4811 break;
4812 case glslang::EOpLogicalAnd:
John Kessenichec43d0a2015-07-04 17:17:31 -06004813 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06004814 binOp = spv::OpLogicalAnd;
4815 break;
4816 case glslang::EOpInclusiveOr:
4817 case glslang::EOpInclusiveOrAssign:
4818 binOp = spv::OpBitwiseOr;
4819 break;
4820 case glslang::EOpLogicalOr:
John Kessenichec43d0a2015-07-04 17:17:31 -06004821 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06004822 binOp = spv::OpLogicalOr;
4823 break;
4824 case glslang::EOpExclusiveOr:
4825 case glslang::EOpExclusiveOrAssign:
4826 binOp = spv::OpBitwiseXor;
4827 break;
4828 case glslang::EOpLogicalXor:
John Kessenichec43d0a2015-07-04 17:17:31 -06004829 needMatchingVectors = false;
John Kessenich5e4b1242015-08-06 22:53:06 -06004830 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06004831 break;
4832
4833 case glslang::EOpLessThan:
4834 case glslang::EOpGreaterThan:
4835 case glslang::EOpLessThanEqual:
4836 case glslang::EOpGreaterThanEqual:
4837 case glslang::EOpEqual:
4838 case glslang::EOpNotEqual:
4839 case glslang::EOpVectorEqual:
4840 case glslang::EOpVectorNotEqual:
4841 comparison = true;
4842 break;
4843 default:
4844 break;
4845 }
4846
John Kessenich7c1aa102015-10-15 13:29:11 -06004847 // handle mapped binary operations (should be non-comparison)
John Kessenich140f3df2015-06-26 16:58:36 -06004848 if (binOp != spv::OpNop) {
John Kessenich7c1aa102015-10-15 13:29:11 -06004849 assert(comparison == false);
John Kessenich04bb8a02015-12-12 12:28:14 -07004850 if (builder.isMatrix(left) || builder.isMatrix(right))
John Kessenichead86222018-03-28 18:01:20 -06004851 return createBinaryMatrixOperation(binOp, decorations, typeId, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06004852
4853 // No matrix involved; make both operands be the same number of components, if needed
John Kessenichec43d0a2015-07-04 17:17:31 -06004854 if (needMatchingVectors)
John Kessenichead86222018-03-28 18:01:20 -06004855 builder.promoteScalar(decorations.precision, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06004856
qining25262b32016-05-06 17:25:16 -04004857 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichead86222018-03-28 18:01:20 -06004858 builder.addDecoration(result, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06004859 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06004860 return builder.setPrecision(result, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06004861 }
4862
4863 if (! comparison)
4864 return 0;
4865
John Kessenich7c1aa102015-10-15 13:29:11 -06004866 // Handle comparison instructions
John Kessenich140f3df2015-06-26 16:58:36 -06004867
John Kessenich4583b612016-08-07 19:14:22 -06004868 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
John Kessenichead86222018-03-28 18:01:20 -06004869 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) {
4870 spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual);
John Kessenich5611c6d2018-04-05 11:25:02 -06004871 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06004872 return result;
4873 }
John Kessenich140f3df2015-06-26 16:58:36 -06004874
4875 switch (op) {
4876 case glslang::EOpLessThan:
4877 if (isFloat)
4878 binOp = spv::OpFOrdLessThan;
4879 else if (isUnsigned)
4880 binOp = spv::OpULessThan;
4881 else
4882 binOp = spv::OpSLessThan;
4883 break;
4884 case glslang::EOpGreaterThan:
4885 if (isFloat)
4886 binOp = spv::OpFOrdGreaterThan;
4887 else if (isUnsigned)
4888 binOp = spv::OpUGreaterThan;
4889 else
4890 binOp = spv::OpSGreaterThan;
4891 break;
4892 case glslang::EOpLessThanEqual:
4893 if (isFloat)
4894 binOp = spv::OpFOrdLessThanEqual;
4895 else if (isUnsigned)
4896 binOp = spv::OpULessThanEqual;
4897 else
4898 binOp = spv::OpSLessThanEqual;
4899 break;
4900 case glslang::EOpGreaterThanEqual:
4901 if (isFloat)
4902 binOp = spv::OpFOrdGreaterThanEqual;
4903 else if (isUnsigned)
4904 binOp = spv::OpUGreaterThanEqual;
4905 else
4906 binOp = spv::OpSGreaterThanEqual;
4907 break;
4908 case glslang::EOpEqual:
4909 case glslang::EOpVectorEqual:
4910 if (isFloat)
4911 binOp = spv::OpFOrdEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08004912 else if (isBool)
4913 binOp = spv::OpLogicalEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06004914 else
4915 binOp = spv::OpIEqual;
4916 break;
4917 case glslang::EOpNotEqual:
4918 case glslang::EOpVectorNotEqual:
4919 if (isFloat)
4920 binOp = spv::OpFOrdNotEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08004921 else if (isBool)
4922 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06004923 else
4924 binOp = spv::OpINotEqual;
4925 break;
4926 default:
4927 break;
4928 }
4929
qining25262b32016-05-06 17:25:16 -04004930 if (binOp != spv::OpNop) {
4931 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichead86222018-03-28 18:01:20 -06004932 builder.addDecoration(result, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06004933 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06004934 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04004935 }
John Kessenich140f3df2015-06-26 16:58:36 -06004936
4937 return 0;
4938}
4939
John Kessenich04bb8a02015-12-12 12:28:14 -07004940//
4941// Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
4942// These can be any of:
4943//
4944// matrix * scalar
4945// scalar * matrix
4946// matrix * matrix linear algebraic
4947// matrix * vector
4948// vector * matrix
4949// matrix * matrix componentwise
4950// matrix op matrix op in {+, -, /}
4951// matrix op scalar op in {+, -, /}
4952// scalar op matrix op in {+, -, /}
4953//
John Kessenichead86222018-03-28 18:01:20 -06004954spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
4955 spv::Id left, spv::Id right)
John Kessenich04bb8a02015-12-12 12:28:14 -07004956{
4957 bool firstClass = true;
4958
4959 // First, handle first-class matrix operations (* and matrix/scalar)
4960 switch (op) {
4961 case spv::OpFDiv:
4962 if (builder.isMatrix(left) && builder.isScalar(right)) {
4963 // turn matrix / scalar into a multiply...
Neil Robertseddb1312018-03-13 10:57:59 +01004964 spv::Id resultType = builder.getTypeId(right);
4965 right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right);
John Kessenich04bb8a02015-12-12 12:28:14 -07004966 op = spv::OpMatrixTimesScalar;
4967 } else
4968 firstClass = false;
4969 break;
4970 case spv::OpMatrixTimesScalar:
4971 if (builder.isMatrix(right))
4972 std::swap(left, right);
4973 assert(builder.isScalar(right));
4974 break;
4975 case spv::OpVectorTimesMatrix:
4976 assert(builder.isVector(left));
4977 assert(builder.isMatrix(right));
4978 break;
4979 case spv::OpMatrixTimesVector:
4980 assert(builder.isMatrix(left));
4981 assert(builder.isVector(right));
4982 break;
4983 case spv::OpMatrixTimesMatrix:
4984 assert(builder.isMatrix(left));
4985 assert(builder.isMatrix(right));
4986 break;
4987 default:
4988 firstClass = false;
4989 break;
4990 }
4991
qining25262b32016-05-06 17:25:16 -04004992 if (firstClass) {
4993 spv::Id result = builder.createBinOp(op, typeId, left, right);
John Kessenichead86222018-03-28 18:01:20 -06004994 builder.addDecoration(result, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06004995 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06004996 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04004997 }
John Kessenich04bb8a02015-12-12 12:28:14 -07004998
LoopDawg592860c2016-06-09 08:57:35 -06004999 // Handle component-wise +, -, *, %, and / for all combinations of type.
John Kessenich04bb8a02015-12-12 12:28:14 -07005000 // The result type of all of them is the same type as the (a) matrix operand.
5001 // The algorithm is to:
5002 // - break the matrix(es) into vectors
5003 // - smear any scalar to a vector
5004 // - do vector operations
5005 // - make a matrix out the vector results
5006 switch (op) {
5007 case spv::OpFAdd:
5008 case spv::OpFSub:
5009 case spv::OpFDiv:
LoopDawg592860c2016-06-09 08:57:35 -06005010 case spv::OpFMod:
John Kessenich04bb8a02015-12-12 12:28:14 -07005011 case spv::OpFMul:
5012 {
5013 // one time set up...
5014 bool leftMat = builder.isMatrix(left);
5015 bool rightMat = builder.isMatrix(right);
5016 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
5017 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
5018 spv::Id scalarType = builder.getScalarTypeId(typeId);
5019 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
5020 std::vector<spv::Id> results;
5021 spv::Id smearVec = spv::NoResult;
5022 if (builder.isScalar(left))
John Kessenichead86222018-03-28 18:01:20 -06005023 smearVec = builder.smearScalar(decorations.precision, left, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005024 else if (builder.isScalar(right))
John Kessenichead86222018-03-28 18:01:20 -06005025 smearVec = builder.smearScalar(decorations.precision, right, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005026
5027 // do each vector op
5028 for (unsigned int c = 0; c < numCols; ++c) {
5029 std::vector<unsigned int> indexes;
5030 indexes.push_back(c);
5031 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
5032 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
qining25262b32016-05-06 17:25:16 -04005033 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
John Kessenichead86222018-03-28 18:01:20 -06005034 builder.addDecoration(result, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06005035 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005036 results.push_back(builder.setPrecision(result, decorations.precision));
John Kessenich04bb8a02015-12-12 12:28:14 -07005037 }
5038
5039 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005040 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenich5611c6d2018-04-05 11:25:02 -06005041 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005042 return result;
John Kessenich04bb8a02015-12-12 12:28:14 -07005043 }
5044 default:
5045 assert(0);
5046 return spv::NoResult;
5047 }
5048}
5049
John Kessenichead86222018-03-28 18:01:20 -06005050spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
5051 spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06005052{
5053 spv::Op unaryOp = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08005054 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06005055 int libCall = -1;
John Kessenich66011cb2018-03-06 16:12:04 -07005056 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5057 bool isFloat = isTypeFloat(typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06005058
5059 switch (op) {
5060 case glslang::EOpNegative:
John Kessenich7a53f762016-01-20 11:19:27 -07005061 if (isFloat) {
John Kessenich140f3df2015-06-26 16:58:36 -06005062 unaryOp = spv::OpFNegate;
John Kessenich7a53f762016-01-20 11:19:27 -07005063 if (builder.isMatrixType(typeId))
John Kessenichead86222018-03-28 18:01:20 -06005064 return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy);
John Kessenich7a53f762016-01-20 11:19:27 -07005065 } else
John Kessenich140f3df2015-06-26 16:58:36 -06005066 unaryOp = spv::OpSNegate;
5067 break;
5068
5069 case glslang::EOpLogicalNot:
5070 case glslang::EOpVectorLogicalNot:
John Kessenich5e4b1242015-08-06 22:53:06 -06005071 unaryOp = spv::OpLogicalNot;
5072 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005073 case glslang::EOpBitwiseNot:
5074 unaryOp = spv::OpNot;
5075 break;
John Kessenich5e4b1242015-08-06 22:53:06 -06005076
John Kessenich140f3df2015-06-26 16:58:36 -06005077 case glslang::EOpDeterminant:
John Kessenich5e4b1242015-08-06 22:53:06 -06005078 libCall = spv::GLSLstd450Determinant;
John Kessenich140f3df2015-06-26 16:58:36 -06005079 break;
5080 case glslang::EOpMatrixInverse:
John Kessenich5e4b1242015-08-06 22:53:06 -06005081 libCall = spv::GLSLstd450MatrixInverse;
John Kessenich140f3df2015-06-26 16:58:36 -06005082 break;
5083 case glslang::EOpTranspose:
5084 unaryOp = spv::OpTranspose;
5085 break;
5086
5087 case glslang::EOpRadians:
John Kessenich5e4b1242015-08-06 22:53:06 -06005088 libCall = spv::GLSLstd450Radians;
John Kessenich140f3df2015-06-26 16:58:36 -06005089 break;
5090 case glslang::EOpDegrees:
John Kessenich5e4b1242015-08-06 22:53:06 -06005091 libCall = spv::GLSLstd450Degrees;
John Kessenich140f3df2015-06-26 16:58:36 -06005092 break;
5093 case glslang::EOpSin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005094 libCall = spv::GLSLstd450Sin;
John Kessenich140f3df2015-06-26 16:58:36 -06005095 break;
5096 case glslang::EOpCos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005097 libCall = spv::GLSLstd450Cos;
John Kessenich140f3df2015-06-26 16:58:36 -06005098 break;
5099 case glslang::EOpTan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005100 libCall = spv::GLSLstd450Tan;
John Kessenich140f3df2015-06-26 16:58:36 -06005101 break;
5102 case glslang::EOpAcos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005103 libCall = spv::GLSLstd450Acos;
John Kessenich140f3df2015-06-26 16:58:36 -06005104 break;
5105 case glslang::EOpAsin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005106 libCall = spv::GLSLstd450Asin;
John Kessenich140f3df2015-06-26 16:58:36 -06005107 break;
5108 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005109 libCall = spv::GLSLstd450Atan;
John Kessenich140f3df2015-06-26 16:58:36 -06005110 break;
5111
5112 case glslang::EOpAcosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005113 libCall = spv::GLSLstd450Acosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005114 break;
5115 case glslang::EOpAsinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005116 libCall = spv::GLSLstd450Asinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005117 break;
5118 case glslang::EOpAtanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005119 libCall = spv::GLSLstd450Atanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005120 break;
5121 case glslang::EOpTanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005122 libCall = spv::GLSLstd450Tanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005123 break;
5124 case glslang::EOpCosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005125 libCall = spv::GLSLstd450Cosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005126 break;
5127 case glslang::EOpSinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005128 libCall = spv::GLSLstd450Sinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005129 break;
5130
5131 case glslang::EOpLength:
John Kessenich5e4b1242015-08-06 22:53:06 -06005132 libCall = spv::GLSLstd450Length;
John Kessenich140f3df2015-06-26 16:58:36 -06005133 break;
5134 case glslang::EOpNormalize:
John Kessenich5e4b1242015-08-06 22:53:06 -06005135 libCall = spv::GLSLstd450Normalize;
John Kessenich140f3df2015-06-26 16:58:36 -06005136 break;
5137
5138 case glslang::EOpExp:
John Kessenich5e4b1242015-08-06 22:53:06 -06005139 libCall = spv::GLSLstd450Exp;
John Kessenich140f3df2015-06-26 16:58:36 -06005140 break;
5141 case glslang::EOpLog:
John Kessenich5e4b1242015-08-06 22:53:06 -06005142 libCall = spv::GLSLstd450Log;
John Kessenich140f3df2015-06-26 16:58:36 -06005143 break;
5144 case glslang::EOpExp2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005145 libCall = spv::GLSLstd450Exp2;
John Kessenich140f3df2015-06-26 16:58:36 -06005146 break;
5147 case glslang::EOpLog2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005148 libCall = spv::GLSLstd450Log2;
John Kessenich140f3df2015-06-26 16:58:36 -06005149 break;
5150 case glslang::EOpSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005151 libCall = spv::GLSLstd450Sqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005152 break;
5153 case glslang::EOpInverseSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005154 libCall = spv::GLSLstd450InverseSqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005155 break;
5156
5157 case glslang::EOpFloor:
John Kessenich5e4b1242015-08-06 22:53:06 -06005158 libCall = spv::GLSLstd450Floor;
John Kessenich140f3df2015-06-26 16:58:36 -06005159 break;
5160 case glslang::EOpTrunc:
John Kessenich5e4b1242015-08-06 22:53:06 -06005161 libCall = spv::GLSLstd450Trunc;
John Kessenich140f3df2015-06-26 16:58:36 -06005162 break;
5163 case glslang::EOpRound:
John Kessenich5e4b1242015-08-06 22:53:06 -06005164 libCall = spv::GLSLstd450Round;
John Kessenich140f3df2015-06-26 16:58:36 -06005165 break;
5166 case glslang::EOpRoundEven:
John Kessenich5e4b1242015-08-06 22:53:06 -06005167 libCall = spv::GLSLstd450RoundEven;
John Kessenich140f3df2015-06-26 16:58:36 -06005168 break;
5169 case glslang::EOpCeil:
John Kessenich5e4b1242015-08-06 22:53:06 -06005170 libCall = spv::GLSLstd450Ceil;
John Kessenich140f3df2015-06-26 16:58:36 -06005171 break;
5172 case glslang::EOpFract:
John Kessenich5e4b1242015-08-06 22:53:06 -06005173 libCall = spv::GLSLstd450Fract;
John Kessenich140f3df2015-06-26 16:58:36 -06005174 break;
5175
5176 case glslang::EOpIsNan:
5177 unaryOp = spv::OpIsNan;
5178 break;
5179 case glslang::EOpIsInf:
5180 unaryOp = spv::OpIsInf;
5181 break;
LoopDawg592860c2016-06-09 08:57:35 -06005182 case glslang::EOpIsFinite:
5183 unaryOp = spv::OpIsFinite;
5184 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005185
Rex Xucbc426e2015-12-15 16:03:10 +08005186 case glslang::EOpFloatBitsToInt:
5187 case glslang::EOpFloatBitsToUint:
5188 case glslang::EOpIntBitsToFloat:
5189 case glslang::EOpUintBitsToFloat:
Rex Xu8ff43de2016-04-22 16:51:45 +08005190 case glslang::EOpDoubleBitsToInt64:
5191 case glslang::EOpDoubleBitsToUint64:
5192 case glslang::EOpInt64BitsToDouble:
5193 case glslang::EOpUint64BitsToDouble:
Rex Xucabbb782017-03-24 13:41:14 +08005194 case glslang::EOpFloat16BitsToInt16:
5195 case glslang::EOpFloat16BitsToUint16:
5196 case glslang::EOpInt16BitsToFloat16:
5197 case glslang::EOpUint16BitsToFloat16:
Rex Xucbc426e2015-12-15 16:03:10 +08005198 unaryOp = spv::OpBitcast;
5199 break;
5200
John Kessenich140f3df2015-06-26 16:58:36 -06005201 case glslang::EOpPackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005202 libCall = spv::GLSLstd450PackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005203 break;
5204 case glslang::EOpUnpackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005205 libCall = spv::GLSLstd450UnpackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005206 break;
5207 case glslang::EOpPackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005208 libCall = spv::GLSLstd450PackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005209 break;
5210 case glslang::EOpUnpackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005211 libCall = spv::GLSLstd450UnpackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005212 break;
5213 case glslang::EOpPackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005214 libCall = spv::GLSLstd450PackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005215 break;
5216 case glslang::EOpUnpackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005217 libCall = spv::GLSLstd450UnpackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005218 break;
John Kessenichfc51d282015-08-19 13:34:18 -06005219 case glslang::EOpPackSnorm4x8:
5220 libCall = spv::GLSLstd450PackSnorm4x8;
5221 break;
5222 case glslang::EOpUnpackSnorm4x8:
5223 libCall = spv::GLSLstd450UnpackSnorm4x8;
5224 break;
5225 case glslang::EOpPackUnorm4x8:
5226 libCall = spv::GLSLstd450PackUnorm4x8;
5227 break;
5228 case glslang::EOpUnpackUnorm4x8:
5229 libCall = spv::GLSLstd450UnpackUnorm4x8;
5230 break;
5231 case glslang::EOpPackDouble2x32:
5232 libCall = spv::GLSLstd450PackDouble2x32;
5233 break;
5234 case glslang::EOpUnpackDouble2x32:
5235 libCall = spv::GLSLstd450UnpackDouble2x32;
5236 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005237
Rex Xu8ff43de2016-04-22 16:51:45 +08005238 case glslang::EOpPackInt2x32:
5239 case glslang::EOpUnpackInt2x32:
5240 case glslang::EOpPackUint2x32:
5241 case glslang::EOpUnpackUint2x32:
John Kessenich66011cb2018-03-06 16:12:04 -07005242 case glslang::EOpPack16:
5243 case glslang::EOpPack32:
5244 case glslang::EOpPack64:
5245 case glslang::EOpUnpack32:
5246 case glslang::EOpUnpack16:
5247 case glslang::EOpUnpack8:
Rex Xucabbb782017-03-24 13:41:14 +08005248 case glslang::EOpPackInt2x16:
5249 case glslang::EOpUnpackInt2x16:
5250 case glslang::EOpPackUint2x16:
5251 case glslang::EOpUnpackUint2x16:
5252 case glslang::EOpPackInt4x16:
5253 case glslang::EOpUnpackInt4x16:
5254 case glslang::EOpPackUint4x16:
5255 case glslang::EOpUnpackUint4x16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005256 case glslang::EOpPackFloat2x16:
5257 case glslang::EOpUnpackFloat2x16:
5258 unaryOp = spv::OpBitcast;
5259 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005260
John Kessenich140f3df2015-06-26 16:58:36 -06005261 case glslang::EOpDPdx:
5262 unaryOp = spv::OpDPdx;
5263 break;
5264 case glslang::EOpDPdy:
5265 unaryOp = spv::OpDPdy;
5266 break;
5267 case glslang::EOpFwidth:
5268 unaryOp = spv::OpFwidth;
5269 break;
5270 case glslang::EOpDPdxFine:
5271 unaryOp = spv::OpDPdxFine;
5272 break;
5273 case glslang::EOpDPdyFine:
5274 unaryOp = spv::OpDPdyFine;
5275 break;
5276 case glslang::EOpFwidthFine:
5277 unaryOp = spv::OpFwidthFine;
5278 break;
5279 case glslang::EOpDPdxCoarse:
5280 unaryOp = spv::OpDPdxCoarse;
5281 break;
5282 case glslang::EOpDPdyCoarse:
5283 unaryOp = spv::OpDPdyCoarse;
5284 break;
5285 case glslang::EOpFwidthCoarse:
5286 unaryOp = spv::OpFwidthCoarse;
5287 break;
Rex Xu7a26c172015-12-08 17:12:09 +08005288 case glslang::EOpInterpolateAtCentroid:
Rex Xub4a2a6c2018-05-17 13:51:28 +08005289#ifdef AMD_EXTENSIONS
5290 if (typeProxy == glslang::EbtFloat16)
5291 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
5292#endif
Rex Xu7a26c172015-12-08 17:12:09 +08005293 libCall = spv::GLSLstd450InterpolateAtCentroid;
5294 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005295 case glslang::EOpAny:
5296 unaryOp = spv::OpAny;
5297 break;
5298 case glslang::EOpAll:
5299 unaryOp = spv::OpAll;
5300 break;
5301
5302 case glslang::EOpAbs:
John Kessenich5e4b1242015-08-06 22:53:06 -06005303 if (isFloat)
5304 libCall = spv::GLSLstd450FAbs;
5305 else
5306 libCall = spv::GLSLstd450SAbs;
John Kessenich140f3df2015-06-26 16:58:36 -06005307 break;
5308 case glslang::EOpSign:
John Kessenich5e4b1242015-08-06 22:53:06 -06005309 if (isFloat)
5310 libCall = spv::GLSLstd450FSign;
5311 else
5312 libCall = spv::GLSLstd450SSign;
John Kessenich140f3df2015-06-26 16:58:36 -06005313 break;
5314
John Kessenichfc51d282015-08-19 13:34:18 -06005315 case glslang::EOpAtomicCounterIncrement:
5316 case glslang::EOpAtomicCounterDecrement:
5317 case glslang::EOpAtomicCounter:
5318 {
5319 // Handle all of the atomics in one place, in createAtomicOperation()
5320 std::vector<spv::Id> operands;
5321 operands.push_back(operand);
John Kessenichead86222018-03-28 18:01:20 -06005322 return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy);
John Kessenichfc51d282015-08-19 13:34:18 -06005323 }
5324
John Kessenichfc51d282015-08-19 13:34:18 -06005325 case glslang::EOpBitFieldReverse:
5326 unaryOp = spv::OpBitReverse;
5327 break;
5328 case glslang::EOpBitCount:
5329 unaryOp = spv::OpBitCount;
5330 break;
5331 case glslang::EOpFindLSB:
John Kessenich55e7d112015-11-15 21:33:39 -07005332 libCall = spv::GLSLstd450FindILsb;
John Kessenichfc51d282015-08-19 13:34:18 -06005333 break;
5334 case glslang::EOpFindMSB:
John Kessenich55e7d112015-11-15 21:33:39 -07005335 if (isUnsigned)
5336 libCall = spv::GLSLstd450FindUMsb;
5337 else
5338 libCall = spv::GLSLstd450FindSMsb;
John Kessenichfc51d282015-08-19 13:34:18 -06005339 break;
5340
Rex Xu574ab042016-04-14 16:53:07 +08005341 case glslang::EOpBallot:
5342 case glslang::EOpReadFirstInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08005343 case glslang::EOpAnyInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08005344 case glslang::EOpAllInvocations:
Rex Xu338b1852016-05-05 20:38:33 +08005345 case glslang::EOpAllInvocationsEqual:
Rex Xu9d93a232016-05-05 12:30:44 +08005346#ifdef AMD_EXTENSIONS
5347 case glslang::EOpMinInvocations:
5348 case glslang::EOpMaxInvocations:
5349 case glslang::EOpAddInvocations:
5350 case glslang::EOpMinInvocationsNonUniform:
5351 case glslang::EOpMaxInvocationsNonUniform:
5352 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08005353 case glslang::EOpMinInvocationsInclusiveScan:
5354 case glslang::EOpMaxInvocationsInclusiveScan:
5355 case glslang::EOpAddInvocationsInclusiveScan:
5356 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
5357 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
5358 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
5359 case glslang::EOpMinInvocationsExclusiveScan:
5360 case glslang::EOpMaxInvocationsExclusiveScan:
5361 case glslang::EOpAddInvocationsExclusiveScan:
5362 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
5363 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
5364 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
Rex Xu9d93a232016-05-05 12:30:44 +08005365#endif
Rex Xu51596642016-09-21 18:56:12 +08005366 {
5367 std::vector<spv::Id> operands;
5368 operands.push_back(operand);
5369 return createInvocationsOperation(op, typeId, operands, typeProxy);
5370 }
John Kessenich66011cb2018-03-06 16:12:04 -07005371 case glslang::EOpSubgroupAll:
5372 case glslang::EOpSubgroupAny:
5373 case glslang::EOpSubgroupAllEqual:
5374 case glslang::EOpSubgroupBroadcastFirst:
5375 case glslang::EOpSubgroupBallot:
5376 case glslang::EOpSubgroupInverseBallot:
5377 case glslang::EOpSubgroupBallotBitCount:
5378 case glslang::EOpSubgroupBallotInclusiveBitCount:
5379 case glslang::EOpSubgroupBallotExclusiveBitCount:
5380 case glslang::EOpSubgroupBallotFindLSB:
5381 case glslang::EOpSubgroupBallotFindMSB:
5382 case glslang::EOpSubgroupAdd:
5383 case glslang::EOpSubgroupMul:
5384 case glslang::EOpSubgroupMin:
5385 case glslang::EOpSubgroupMax:
5386 case glslang::EOpSubgroupAnd:
5387 case glslang::EOpSubgroupOr:
5388 case glslang::EOpSubgroupXor:
5389 case glslang::EOpSubgroupInclusiveAdd:
5390 case glslang::EOpSubgroupInclusiveMul:
5391 case glslang::EOpSubgroupInclusiveMin:
5392 case glslang::EOpSubgroupInclusiveMax:
5393 case glslang::EOpSubgroupInclusiveAnd:
5394 case glslang::EOpSubgroupInclusiveOr:
5395 case glslang::EOpSubgroupInclusiveXor:
5396 case glslang::EOpSubgroupExclusiveAdd:
5397 case glslang::EOpSubgroupExclusiveMul:
5398 case glslang::EOpSubgroupExclusiveMin:
5399 case glslang::EOpSubgroupExclusiveMax:
5400 case glslang::EOpSubgroupExclusiveAnd:
5401 case glslang::EOpSubgroupExclusiveOr:
5402 case glslang::EOpSubgroupExclusiveXor:
5403 case glslang::EOpSubgroupQuadSwapHorizontal:
5404 case glslang::EOpSubgroupQuadSwapVertical:
5405 case glslang::EOpSubgroupQuadSwapDiagonal: {
5406 std::vector<spv::Id> operands;
5407 operands.push_back(operand);
5408 return createSubgroupOperation(op, typeId, operands, typeProxy);
5409 }
Rex Xu9d93a232016-05-05 12:30:44 +08005410#ifdef AMD_EXTENSIONS
5411 case glslang::EOpMbcnt:
5412 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
5413 libCall = spv::MbcntAMD;
5414 break;
5415
5416 case glslang::EOpCubeFaceIndex:
5417 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5418 libCall = spv::CubeFaceIndexAMD;
5419 break;
5420
5421 case glslang::EOpCubeFaceCoord:
5422 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5423 libCall = spv::CubeFaceCoordAMD;
5424 break;
5425#endif
Jeff Bolz2abe9a42018-03-29 22:52:17 -05005426#ifdef NV_EXTENSIONS
5427 case glslang::EOpSubgroupPartition:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05005428 unaryOp = spv::OpGroupNonUniformPartitionNV;
5429 break;
5430#endif
Jeff Bolz9f2aec42019-01-06 17:58:04 -06005431 case glslang::EOpConstructReference:
5432 unaryOp = spv::OpBitcast;
5433 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005434 default:
5435 return 0;
5436 }
5437
5438 spv::Id id;
5439 if (libCall >= 0) {
5440 std::vector<spv::Id> args;
5441 args.push_back(operand);
Rex Xu9d93a232016-05-05 12:30:44 +08005442 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
Rex Xu338b1852016-05-05 20:38:33 +08005443 } else {
John Kessenich91cef522016-05-05 16:45:40 -06005444 id = builder.createUnaryOp(unaryOp, typeId, operand);
Rex Xu338b1852016-05-05 20:38:33 +08005445 }
John Kessenich140f3df2015-06-26 16:58:36 -06005446
John Kessenichead86222018-03-28 18:01:20 -06005447 builder.addDecoration(id, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06005448 builder.addDecoration(id, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005449 return builder.setPrecision(id, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06005450}
5451
John Kessenich7a53f762016-01-20 11:19:27 -07005452// Create a unary operation on a matrix
John Kessenichead86222018-03-28 18:01:20 -06005453spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5454 spv::Id operand, glslang::TBasicType /* typeProxy */)
John Kessenich7a53f762016-01-20 11:19:27 -07005455{
5456 // Handle unary operations vector by vector.
5457 // The result type is the same type as the original type.
5458 // The algorithm is to:
5459 // - break the matrix into vectors
5460 // - apply the operation to each vector
5461 // - make a matrix out the vector results
5462
5463 // get the types sorted out
5464 int numCols = builder.getNumColumns(operand);
5465 int numRows = builder.getNumRows(operand);
Rex Xuc1992e52016-05-17 18:57:18 +08005466 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
5467 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
John Kessenich7a53f762016-01-20 11:19:27 -07005468 std::vector<spv::Id> results;
5469
5470 // do each vector op
5471 for (int c = 0; c < numCols; ++c) {
5472 std::vector<unsigned int> indexes;
5473 indexes.push_back(c);
Rex Xuc1992e52016-05-17 18:57:18 +08005474 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
5475 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
John Kessenichead86222018-03-28 18:01:20 -06005476 builder.addDecoration(destVec, decorations.noContraction);
John Kessenich5611c6d2018-04-05 11:25:02 -06005477 builder.addDecoration(destVec, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005478 results.push_back(builder.setPrecision(destVec, decorations.precision));
John Kessenich7a53f762016-01-20 11:19:27 -07005479 }
5480
5481 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005482 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenich5611c6d2018-04-05 11:25:02 -06005483 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005484 return result;
John Kessenich7a53f762016-01-20 11:19:27 -07005485}
5486
John Kessenichad7645f2018-06-04 19:11:25 -06005487// For converting integers where both the bitwidth and the signedness could
5488// change, but only do the width change here. The caller is still responsible
5489// for the signedness conversion.
5490spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize)
John Kessenich66011cb2018-03-06 16:12:04 -07005491{
John Kessenichad7645f2018-06-04 19:11:25 -06005492 // Get the result type width, based on the type to convert to.
5493 int width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07005494 switch(op) {
John Kessenichad7645f2018-06-04 19:11:25 -06005495 case glslang::EOpConvInt16ToUint8:
5496 case glslang::EOpConvIntToUint8:
5497 case glslang::EOpConvInt64ToUint8:
5498 case glslang::EOpConvUint16ToInt8:
5499 case glslang::EOpConvUintToInt8:
5500 case glslang::EOpConvUint64ToInt8:
5501 width = 8;
5502 break;
John Kessenich66011cb2018-03-06 16:12:04 -07005503 case glslang::EOpConvInt8ToUint16:
John Kessenichad7645f2018-06-04 19:11:25 -06005504 case glslang::EOpConvIntToUint16:
5505 case glslang::EOpConvInt64ToUint16:
5506 case glslang::EOpConvUint8ToInt16:
5507 case glslang::EOpConvUintToInt16:
5508 case glslang::EOpConvUint64ToInt16:
5509 width = 16;
John Kessenich66011cb2018-03-06 16:12:04 -07005510 break;
5511 case glslang::EOpConvInt8ToUint:
John Kessenichad7645f2018-06-04 19:11:25 -06005512 case glslang::EOpConvInt16ToUint:
5513 case glslang::EOpConvInt64ToUint:
5514 case glslang::EOpConvUint8ToInt:
5515 case glslang::EOpConvUint16ToInt:
5516 case glslang::EOpConvUint64ToInt:
5517 width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07005518 break;
5519 case glslang::EOpConvInt8ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005520 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005521 case glslang::EOpConvIntToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005522 case glslang::EOpConvUint8ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005523 case glslang::EOpConvUint16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005524 case glslang::EOpConvUintToInt64:
John Kessenichad7645f2018-06-04 19:11:25 -06005525 width = 64;
John Kessenich66011cb2018-03-06 16:12:04 -07005526 break;
5527
5528 default:
5529 assert(false && "Default missing");
5530 break;
5531 }
5532
John Kessenichad7645f2018-06-04 19:11:25 -06005533 // Get the conversion operation and result type,
5534 // based on the target width, but the source type.
5535 spv::Id type = spv::NoType;
5536 spv::Op convOp = spv::OpNop;
5537 switch(op) {
5538 case glslang::EOpConvInt8ToUint16:
5539 case glslang::EOpConvInt8ToUint:
5540 case glslang::EOpConvInt8ToUint64:
5541 case glslang::EOpConvInt16ToUint8:
5542 case glslang::EOpConvInt16ToUint:
5543 case glslang::EOpConvInt16ToUint64:
5544 case glslang::EOpConvIntToUint8:
5545 case glslang::EOpConvIntToUint16:
5546 case glslang::EOpConvIntToUint64:
5547 case glslang::EOpConvInt64ToUint8:
5548 case glslang::EOpConvInt64ToUint16:
5549 case glslang::EOpConvInt64ToUint:
5550 convOp = spv::OpSConvert;
5551 type = builder.makeIntType(width);
5552 break;
5553 default:
5554 convOp = spv::OpUConvert;
5555 type = builder.makeUintType(width);
5556 break;
5557 }
5558
John Kessenich66011cb2018-03-06 16:12:04 -07005559 if (vectorSize > 0)
5560 type = builder.makeVectorType(type, vectorSize);
5561
John Kessenichad7645f2018-06-04 19:11:25 -06005562 return builder.createUnaryOp(convOp, type, operand);
John Kessenich66011cb2018-03-06 16:12:04 -07005563}
5564
John Kessenichead86222018-03-28 18:01:20 -06005565spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
5566 spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06005567{
5568 spv::Op convOp = spv::OpNop;
5569 spv::Id zero = 0;
5570 spv::Id one = 0;
5571
5572 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
5573
5574 switch (op) {
John Kessenich66011cb2018-03-06 16:12:04 -07005575 case glslang::EOpConvInt8ToBool:
5576 case glslang::EOpConvUint8ToBool:
5577 zero = builder.makeUint8Constant(0);
5578 zero = makeSmearedConstant(zero, vectorSize);
5579 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
Rex Xucabbb782017-03-24 13:41:14 +08005580 case glslang::EOpConvInt16ToBool:
5581 case glslang::EOpConvUint16ToBool:
John Kessenich66011cb2018-03-06 16:12:04 -07005582 zero = builder.makeUint16Constant(0);
5583 zero = makeSmearedConstant(zero, vectorSize);
5584 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5585 case glslang::EOpConvIntToBool:
5586 case glslang::EOpConvUintToBool:
5587 zero = builder.makeUintConstant(0);
5588 zero = makeSmearedConstant(zero, vectorSize);
5589 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5590 case glslang::EOpConvInt64ToBool:
5591 case glslang::EOpConvUint64ToBool:
5592 zero = builder.makeUint64Constant(0);
John Kessenich140f3df2015-06-26 16:58:36 -06005593 zero = makeSmearedConstant(zero, vectorSize);
5594 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
5595
5596 case glslang::EOpConvFloatToBool:
5597 zero = builder.makeFloatConstant(0.0F);
5598 zero = makeSmearedConstant(zero, vectorSize);
5599 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
5600
5601 case glslang::EOpConvDoubleToBool:
5602 zero = builder.makeDoubleConstant(0.0);
5603 zero = makeSmearedConstant(zero, vectorSize);
5604 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
5605
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005606 case glslang::EOpConvFloat16ToBool:
5607 zero = builder.makeFloat16Constant(0.0F);
5608 zero = makeSmearedConstant(zero, vectorSize);
5609 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005610
John Kessenich140f3df2015-06-26 16:58:36 -06005611 case glslang::EOpConvBoolToFloat:
5612 convOp = spv::OpSelect;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005613 zero = builder.makeFloatConstant(0.0F);
5614 one = builder.makeFloatConstant(1.0F);
John Kessenich140f3df2015-06-26 16:58:36 -06005615 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005616
John Kessenich140f3df2015-06-26 16:58:36 -06005617 case glslang::EOpConvBoolToDouble:
5618 convOp = spv::OpSelect;
5619 zero = builder.makeDoubleConstant(0.0);
5620 one = builder.makeDoubleConstant(1.0);
5621 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005622
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005623 case glslang::EOpConvBoolToFloat16:
5624 convOp = spv::OpSelect;
5625 zero = builder.makeFloat16Constant(0.0F);
5626 one = builder.makeFloat16Constant(1.0F);
5627 break;
John Kessenich66011cb2018-03-06 16:12:04 -07005628
5629 case glslang::EOpConvBoolToInt8:
5630 zero = builder.makeInt8Constant(0);
5631 one = builder.makeInt8Constant(1);
5632 convOp = spv::OpSelect;
5633 break;
5634
5635 case glslang::EOpConvBoolToUint8:
5636 zero = builder.makeUint8Constant(0);
5637 one = builder.makeUint8Constant(1);
5638 convOp = spv::OpSelect;
5639 break;
5640
5641 case glslang::EOpConvBoolToInt16:
5642 zero = builder.makeInt16Constant(0);
5643 one = builder.makeInt16Constant(1);
5644 convOp = spv::OpSelect;
5645 break;
5646
5647 case glslang::EOpConvBoolToUint16:
5648 zero = builder.makeUint16Constant(0);
5649 one = builder.makeUint16Constant(1);
5650 convOp = spv::OpSelect;
5651 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005652
John Kessenich140f3df2015-06-26 16:58:36 -06005653 case glslang::EOpConvBoolToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08005654 case glslang::EOpConvBoolToInt64:
Rex Xucabbb782017-03-24 13:41:14 +08005655 if (op == glslang::EOpConvBoolToInt64)
5656 zero = builder.makeInt64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08005657 else
5658 zero = builder.makeIntConstant(0);
5659
5660 if (op == glslang::EOpConvBoolToInt64)
5661 one = builder.makeInt64Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08005662 else
5663 one = builder.makeIntConstant(1);
5664
John Kessenich140f3df2015-06-26 16:58:36 -06005665 convOp = spv::OpSelect;
5666 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005667
John Kessenich140f3df2015-06-26 16:58:36 -06005668 case glslang::EOpConvBoolToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08005669 case glslang::EOpConvBoolToUint64:
Rex Xucabbb782017-03-24 13:41:14 +08005670 if (op == glslang::EOpConvBoolToUint64)
5671 zero = builder.makeUint64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08005672 else
5673 zero = builder.makeUintConstant(0);
5674
5675 if (op == glslang::EOpConvBoolToUint64)
5676 one = builder.makeUint64Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08005677 else
5678 one = builder.makeUintConstant(1);
5679
John Kessenich140f3df2015-06-26 16:58:36 -06005680 convOp = spv::OpSelect;
5681 break;
5682
John Kessenich66011cb2018-03-06 16:12:04 -07005683 case glslang::EOpConvInt8ToFloat16:
5684 case glslang::EOpConvInt8ToFloat:
5685 case glslang::EOpConvInt8ToDouble:
5686 case glslang::EOpConvInt16ToFloat16:
5687 case glslang::EOpConvInt16ToFloat:
5688 case glslang::EOpConvInt16ToDouble:
5689 case glslang::EOpConvIntToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06005690 case glslang::EOpConvIntToFloat:
5691 case glslang::EOpConvIntToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08005692 case glslang::EOpConvInt64ToFloat:
5693 case glslang::EOpConvInt64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005694 case glslang::EOpConvInt64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06005695 convOp = spv::OpConvertSToF;
5696 break;
5697
John Kessenich66011cb2018-03-06 16:12:04 -07005698 case glslang::EOpConvUint8ToFloat16:
5699 case glslang::EOpConvUint8ToFloat:
5700 case glslang::EOpConvUint8ToDouble:
5701 case glslang::EOpConvUint16ToFloat16:
5702 case glslang::EOpConvUint16ToFloat:
5703 case glslang::EOpConvUint16ToDouble:
5704 case glslang::EOpConvUintToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06005705 case glslang::EOpConvUintToFloat:
5706 case glslang::EOpConvUintToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08005707 case glslang::EOpConvUint64ToFloat:
5708 case glslang::EOpConvUint64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005709 case glslang::EOpConvUint64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06005710 convOp = spv::OpConvertUToF;
5711 break;
5712
5713 case glslang::EOpConvDoubleToFloat:
5714 case glslang::EOpConvFloatToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005715 case glslang::EOpConvDoubleToFloat16:
5716 case glslang::EOpConvFloat16ToDouble:
5717 case glslang::EOpConvFloatToFloat16:
5718 case glslang::EOpConvFloat16ToFloat:
John Kessenich140f3df2015-06-26 16:58:36 -06005719 convOp = spv::OpFConvert;
Rex Xu73e3ce72016-04-27 18:48:17 +08005720 if (builder.isMatrixType(destType))
John Kessenichead86222018-03-28 18:01:20 -06005721 return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06005722 break;
5723
John Kessenich66011cb2018-03-06 16:12:04 -07005724 case glslang::EOpConvFloat16ToInt8:
5725 case glslang::EOpConvFloatToInt8:
5726 case glslang::EOpConvDoubleToInt8:
5727 case glslang::EOpConvFloat16ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08005728 case glslang::EOpConvFloatToInt16:
5729 case glslang::EOpConvDoubleToInt16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005730 case glslang::EOpConvFloat16ToInt:
John Kessenich66011cb2018-03-06 16:12:04 -07005731 case glslang::EOpConvFloatToInt:
5732 case glslang::EOpConvDoubleToInt:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005733 case glslang::EOpConvFloat16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005734 case glslang::EOpConvFloatToInt64:
5735 case glslang::EOpConvDoubleToInt64:
John Kessenich140f3df2015-06-26 16:58:36 -06005736 convOp = spv::OpConvertFToS;
5737 break;
5738
John Kessenich66011cb2018-03-06 16:12:04 -07005739 case glslang::EOpConvUint8ToInt8:
5740 case glslang::EOpConvInt8ToUint8:
5741 case glslang::EOpConvUint16ToInt16:
5742 case glslang::EOpConvInt16ToUint16:
John Kessenich140f3df2015-06-26 16:58:36 -06005743 case glslang::EOpConvUintToInt:
5744 case glslang::EOpConvIntToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08005745 case glslang::EOpConvUint64ToInt64:
5746 case glslang::EOpConvInt64ToUint64:
qininge24aa5e2016-04-07 15:40:27 -04005747 if (builder.isInSpecConstCodeGenMode()) {
5748 // Build zero scalar or vector for OpIAdd.
John Kessenich66011cb2018-03-06 16:12:04 -07005749 if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) {
5750 zero = builder.makeUint8Constant(0);
5751 } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) {
Rex Xucabbb782017-03-24 13:41:14 +08005752 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07005753 } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) {
5754 zero = builder.makeUint64Constant(0);
5755 } else {
Rex Xucabbb782017-03-24 13:41:14 +08005756 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07005757 }
qining189b2032016-04-12 23:16:20 -04005758 zero = makeSmearedConstant(zero, vectorSize);
qininge24aa5e2016-04-07 15:40:27 -04005759 // Use OpIAdd, instead of OpBitcast to do the conversion when
5760 // generating for OpSpecConstantOp instruction.
5761 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
5762 }
5763 // For normal run-time conversion instruction, use OpBitcast.
John Kessenich140f3df2015-06-26 16:58:36 -06005764 convOp = spv::OpBitcast;
5765 break;
5766
John Kessenich66011cb2018-03-06 16:12:04 -07005767 case glslang::EOpConvFloat16ToUint8:
5768 case glslang::EOpConvFloatToUint8:
5769 case glslang::EOpConvDoubleToUint8:
5770 case glslang::EOpConvFloat16ToUint16:
5771 case glslang::EOpConvFloatToUint16:
5772 case glslang::EOpConvDoubleToUint16:
5773 case glslang::EOpConvFloat16ToUint:
John Kessenich140f3df2015-06-26 16:58:36 -06005774 case glslang::EOpConvFloatToUint:
5775 case glslang::EOpConvDoubleToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08005776 case glslang::EOpConvFloatToUint64:
5777 case glslang::EOpConvDoubleToUint64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005778 case glslang::EOpConvFloat16ToUint64:
John Kessenich140f3df2015-06-26 16:58:36 -06005779 convOp = spv::OpConvertFToU;
5780 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08005781
John Kessenich66011cb2018-03-06 16:12:04 -07005782 case glslang::EOpConvInt8ToInt16:
5783 case glslang::EOpConvInt8ToInt:
5784 case glslang::EOpConvInt8ToInt64:
5785 case glslang::EOpConvInt16ToInt8:
Rex Xucabbb782017-03-24 13:41:14 +08005786 case glslang::EOpConvInt16ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08005787 case glslang::EOpConvInt16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005788 case glslang::EOpConvIntToInt8:
5789 case glslang::EOpConvIntToInt16:
5790 case glslang::EOpConvIntToInt64:
5791 case glslang::EOpConvInt64ToInt8:
5792 case glslang::EOpConvInt64ToInt16:
5793 case glslang::EOpConvInt64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08005794 convOp = spv::OpSConvert;
5795 break;
5796
John Kessenich66011cb2018-03-06 16:12:04 -07005797 case glslang::EOpConvUint8ToUint16:
5798 case glslang::EOpConvUint8ToUint:
5799 case glslang::EOpConvUint8ToUint64:
5800 case glslang::EOpConvUint16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08005801 case glslang::EOpConvUint16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08005802 case glslang::EOpConvUint16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005803 case glslang::EOpConvUintToUint8:
5804 case glslang::EOpConvUintToUint16:
5805 case glslang::EOpConvUintToUint64:
5806 case glslang::EOpConvUint64ToUint8:
5807 case glslang::EOpConvUint64ToUint16:
5808 case glslang::EOpConvUint64ToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08005809 convOp = spv::OpUConvert;
5810 break;
5811
John Kessenich66011cb2018-03-06 16:12:04 -07005812 case glslang::EOpConvInt8ToUint16:
5813 case glslang::EOpConvInt8ToUint:
5814 case glslang::EOpConvInt8ToUint64:
5815 case glslang::EOpConvInt16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08005816 case glslang::EOpConvInt16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08005817 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005818 case glslang::EOpConvIntToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08005819 case glslang::EOpConvIntToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07005820 case glslang::EOpConvIntToUint64:
5821 case glslang::EOpConvInt64ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08005822 case glslang::EOpConvInt64ToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07005823 case glslang::EOpConvInt64ToUint:
5824 case glslang::EOpConvUint8ToInt16:
5825 case glslang::EOpConvUint8ToInt:
5826 case glslang::EOpConvUint8ToInt64:
5827 case glslang::EOpConvUint16ToInt8:
5828 case glslang::EOpConvUint16ToInt:
5829 case glslang::EOpConvUint16ToInt64:
5830 case glslang::EOpConvUintToInt8:
5831 case glslang::EOpConvUintToInt16:
5832 case glslang::EOpConvUintToInt64:
5833 case glslang::EOpConvUint64ToInt8:
5834 case glslang::EOpConvUint64ToInt16:
5835 case glslang::EOpConvUint64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08005836 // OpSConvert/OpUConvert + OpBitCast
John Kessenichad7645f2018-06-04 19:11:25 -06005837 operand = createIntWidthConversion(op, operand, vectorSize);
Rex Xu8ff43de2016-04-22 16:51:45 +08005838
5839 if (builder.isInSpecConstCodeGenMode()) {
5840 // Build zero scalar or vector for OpIAdd.
John Kessenich66011cb2018-03-06 16:12:04 -07005841 switch(op) {
5842 case glslang::EOpConvInt16ToUint8:
5843 case glslang::EOpConvIntToUint8:
5844 case glslang::EOpConvInt64ToUint8:
5845 case glslang::EOpConvUint16ToInt8:
5846 case glslang::EOpConvUintToInt8:
5847 case glslang::EOpConvUint64ToInt8:
5848 zero = builder.makeUint8Constant(0);
5849 break;
5850 case glslang::EOpConvInt8ToUint16:
5851 case glslang::EOpConvIntToUint16:
5852 case glslang::EOpConvInt64ToUint16:
5853 case glslang::EOpConvUint8ToInt16:
5854 case glslang::EOpConvUintToInt16:
5855 case glslang::EOpConvUint64ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08005856 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07005857 break;
5858 case glslang::EOpConvInt8ToUint:
5859 case glslang::EOpConvInt16ToUint:
5860 case glslang::EOpConvInt64ToUint:
5861 case glslang::EOpConvUint8ToInt:
5862 case glslang::EOpConvUint16ToInt:
5863 case glslang::EOpConvUint64ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08005864 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07005865 break;
5866 case glslang::EOpConvInt8ToUint64:
5867 case glslang::EOpConvInt16ToUint64:
5868 case glslang::EOpConvIntToUint64:
5869 case glslang::EOpConvUint8ToInt64:
5870 case glslang::EOpConvUint16ToInt64:
5871 case glslang::EOpConvUintToInt64:
Rex Xucabbb782017-03-24 13:41:14 +08005872 zero = builder.makeUint64Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07005873 break;
5874 default:
5875 assert(false && "Default missing");
5876 break;
5877 }
Rex Xu8ff43de2016-04-22 16:51:45 +08005878 zero = makeSmearedConstant(zero, vectorSize);
5879 // Use OpIAdd, instead of OpBitcast to do the conversion when
5880 // generating for OpSpecConstantOp instruction.
5881 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
5882 }
5883 // For normal run-time conversion instruction, use OpBitcast.
5884 convOp = spv::OpBitcast;
5885 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06005886 case glslang::EOpConvUint64ToPtr:
5887 convOp = spv::OpConvertUToPtr;
5888 break;
5889 case glslang::EOpConvPtrToUint64:
5890 convOp = spv::OpConvertPtrToU;
5891 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005892 default:
5893 break;
5894 }
5895
5896 spv::Id result = 0;
5897 if (convOp == spv::OpNop)
5898 return result;
5899
5900 if (convOp == spv::OpSelect) {
5901 zero = makeSmearedConstant(zero, vectorSize);
5902 one = makeSmearedConstant(one, vectorSize);
5903 result = builder.createTriOp(convOp, destType, operand, one, zero);
5904 } else
5905 result = builder.createUnaryOp(convOp, destType, operand);
5906
John Kessenichead86222018-03-28 18:01:20 -06005907 result = builder.setPrecision(result, decorations.precision);
John Kessenich5611c6d2018-04-05 11:25:02 -06005908 builder.addDecoration(result, decorations.nonUniform);
John Kessenichead86222018-03-28 18:01:20 -06005909 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06005910}
5911
5912spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
5913{
5914 if (vectorSize == 0)
5915 return constant;
5916
5917 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
5918 std::vector<spv::Id> components;
5919 for (int c = 0; c < vectorSize; ++c)
5920 components.push_back(constant);
5921 return builder.makeCompositeConstant(vectorTypeId, components);
5922}
5923
John Kessenich426394d2015-07-23 10:22:48 -06005924// For glslang ops that map to SPV atomic opCodes
John Kessenich6c292d32016-02-15 20:58:50 -07005925spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich426394d2015-07-23 10:22:48 -06005926{
5927 spv::Op opCode = spv::OpNop;
5928
5929 switch (op) {
5930 case glslang::EOpAtomicAdd:
Rex Xufc618912015-09-09 16:42:49 +08005931 case glslang::EOpImageAtomicAdd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005932 case glslang::EOpAtomicCounterAdd:
John Kessenich426394d2015-07-23 10:22:48 -06005933 opCode = spv::OpAtomicIAdd;
5934 break;
John Kessenich0d0c6d32017-07-23 16:08:26 -06005935 case glslang::EOpAtomicCounterSubtract:
5936 opCode = spv::OpAtomicISub;
5937 break;
John Kessenich426394d2015-07-23 10:22:48 -06005938 case glslang::EOpAtomicMin:
Rex Xufc618912015-09-09 16:42:49 +08005939 case glslang::EOpImageAtomicMin:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005940 case glslang::EOpAtomicCounterMin:
Rex Xue8fe8b02017-09-26 15:42:56 +08005941 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin;
John Kessenich426394d2015-07-23 10:22:48 -06005942 break;
5943 case glslang::EOpAtomicMax:
Rex Xufc618912015-09-09 16:42:49 +08005944 case glslang::EOpImageAtomicMax:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005945 case glslang::EOpAtomicCounterMax:
Rex Xue8fe8b02017-09-26 15:42:56 +08005946 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax;
John Kessenich426394d2015-07-23 10:22:48 -06005947 break;
5948 case glslang::EOpAtomicAnd:
Rex Xufc618912015-09-09 16:42:49 +08005949 case glslang::EOpImageAtomicAnd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005950 case glslang::EOpAtomicCounterAnd:
John Kessenich426394d2015-07-23 10:22:48 -06005951 opCode = spv::OpAtomicAnd;
5952 break;
5953 case glslang::EOpAtomicOr:
Rex Xufc618912015-09-09 16:42:49 +08005954 case glslang::EOpImageAtomicOr:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005955 case glslang::EOpAtomicCounterOr:
John Kessenich426394d2015-07-23 10:22:48 -06005956 opCode = spv::OpAtomicOr;
5957 break;
5958 case glslang::EOpAtomicXor:
Rex Xufc618912015-09-09 16:42:49 +08005959 case glslang::EOpImageAtomicXor:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005960 case glslang::EOpAtomicCounterXor:
John Kessenich426394d2015-07-23 10:22:48 -06005961 opCode = spv::OpAtomicXor;
5962 break;
5963 case glslang::EOpAtomicExchange:
Rex Xufc618912015-09-09 16:42:49 +08005964 case glslang::EOpImageAtomicExchange:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005965 case glslang::EOpAtomicCounterExchange:
John Kessenich426394d2015-07-23 10:22:48 -06005966 opCode = spv::OpAtomicExchange;
5967 break;
5968 case glslang::EOpAtomicCompSwap:
Rex Xufc618912015-09-09 16:42:49 +08005969 case glslang::EOpImageAtomicCompSwap:
John Kessenich0d0c6d32017-07-23 16:08:26 -06005970 case glslang::EOpAtomicCounterCompSwap:
John Kessenich426394d2015-07-23 10:22:48 -06005971 opCode = spv::OpAtomicCompareExchange;
5972 break;
5973 case glslang::EOpAtomicCounterIncrement:
5974 opCode = spv::OpAtomicIIncrement;
5975 break;
5976 case glslang::EOpAtomicCounterDecrement:
5977 opCode = spv::OpAtomicIDecrement;
5978 break;
5979 case glslang::EOpAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05005980 case glslang::EOpImageAtomicLoad:
5981 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06005982 opCode = spv::OpAtomicLoad;
5983 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05005984 case glslang::EOpAtomicStore:
5985 case glslang::EOpImageAtomicStore:
5986 opCode = spv::OpAtomicStore;
5987 break;
John Kessenich426394d2015-07-23 10:22:48 -06005988 default:
John Kessenich55e7d112015-11-15 21:33:39 -07005989 assert(0);
John Kessenich426394d2015-07-23 10:22:48 -06005990 break;
5991 }
5992
Rex Xue8fe8b02017-09-26 15:42:56 +08005993 if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64)
5994 builder.addCapability(spv::CapabilityInt64Atomics);
5995
John Kessenich426394d2015-07-23 10:22:48 -06005996 // Sort out the operands
5997 // - mapping from glslang -> SPV
Jeff Bolz36831c92018-09-05 10:11:41 -05005998 // - there are extra SPV operands that are optional in glslang
John Kessenich3e60a6f2015-09-14 22:45:16 -06005999 // - compare-exchange swaps the value and comparator
6000 // - compare-exchange has an extra memory semantics
John Kessenich48d6e792017-10-06 21:21:48 -06006001 // - EOpAtomicCounterDecrement needs a post decrement
Jeff Bolz36831c92018-09-05 10:11:41 -05006002 spv::Id pointerId = 0, compareId = 0, valueId = 0;
6003 // scope defaults to Device in the old model, QueueFamilyKHR in the new model
6004 spv::Id scopeId;
6005 if (glslangIntermediate->usingVulkanMemoryModel()) {
6006 scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR);
6007 } else {
6008 scopeId = builder.makeUintConstant(spv::ScopeDevice);
6009 }
6010 // semantics default to relaxed
6011 spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone);
6012 spv::Id semanticsId2 = semanticsId;
6013
6014 pointerId = operands[0];
6015 if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) {
6016 // no additional operands
6017 } else if (opCode == spv::OpAtomicCompareExchange) {
6018 compareId = operands[1];
6019 valueId = operands[2];
6020 if (operands.size() > 3) {
6021 scopeId = operands[3];
6022 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5]));
6023 semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7]));
6024 }
6025 } else if (opCode == spv::OpAtomicLoad) {
6026 if (operands.size() > 1) {
6027 scopeId = operands[1];
6028 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]));
6029 }
6030 } else {
6031 // atomic store or RMW
6032 valueId = operands[1];
6033 if (operands.size() > 2) {
6034 scopeId = operands[2];
6035 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4]));
6036 }
Rex Xu04db3f52015-09-16 11:44:02 +08006037 }
John Kessenich426394d2015-07-23 10:22:48 -06006038
Jeff Bolz36831c92018-09-05 10:11:41 -05006039 // Check for capabilities
6040 unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2);
6041 if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
6042 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6043 }
John Kessenich426394d2015-07-23 10:22:48 -06006044
Jeff Bolz36831c92018-09-05 10:11:41 -05006045 if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) {
6046 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6047 }
John Kessenich48d6e792017-10-06 21:21:48 -06006048
Jeff Bolz36831c92018-09-05 10:11:41 -05006049 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
6050 spvAtomicOperands.push_back(pointerId);
6051 spvAtomicOperands.push_back(scopeId);
6052 spvAtomicOperands.push_back(semanticsId);
6053 if (opCode == spv::OpAtomicCompareExchange) {
6054 spvAtomicOperands.push_back(semanticsId2);
6055 spvAtomicOperands.push_back(valueId);
6056 spvAtomicOperands.push_back(compareId);
6057 } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) {
6058 spvAtomicOperands.push_back(valueId);
6059 }
John Kessenich48d6e792017-10-06 21:21:48 -06006060
Jeff Bolz36831c92018-09-05 10:11:41 -05006061 if (opCode == spv::OpAtomicStore) {
6062 builder.createNoResultOp(opCode, spvAtomicOperands);
6063 return 0;
6064 } else {
6065 spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands);
6066
6067 // GLSL and HLSL atomic-counter decrement return post-decrement value,
6068 // while SPIR-V returns pre-decrement value. Translate between these semantics.
6069 if (op == glslang::EOpAtomicCounterDecrement)
6070 resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1));
6071
6072 return resultId;
6073 }
John Kessenich426394d2015-07-23 10:22:48 -06006074}
6075
John Kessenich91cef522016-05-05 16:45:40 -06006076// Create group invocation operations.
Rex Xu51596642016-09-21 18:56:12 +08006077spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich91cef522016-05-05 16:45:40 -06006078{
Corentin Walleze7061422018-08-08 15:20:15 +02006079#ifdef AMD_EXTENSIONS
John Kessenich66011cb2018-03-06 16:12:04 -07006080 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6081 bool isFloat = isTypeFloat(typeProxy);
Corentin Walleze7061422018-08-08 15:20:15 +02006082#endif
Rex Xu9d93a232016-05-05 12:30:44 +08006083
Rex Xu51596642016-09-21 18:56:12 +08006084 spv::Op opCode = spv::OpNop;
John Kessenich149afc32018-08-14 13:31:43 -06006085 std::vector<spv::IdImmediate> spvGroupOperands;
Rex Xu430ef402016-10-14 17:22:23 +08006086 spv::GroupOperation groupOperation = spv::GroupOperationMax;
6087
chaocf200da82016-12-20 12:44:35 -08006088 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
6089 op == glslang::EOpReadInvocation) {
Rex Xu51596642016-09-21 18:56:12 +08006090 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
6091 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006092 } else if (op == glslang::EOpAnyInvocation ||
6093 op == glslang::EOpAllInvocations ||
6094 op == glslang::EOpAllInvocationsEqual) {
6095 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
6096 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
Rex Xu51596642016-09-21 18:56:12 +08006097 } else {
6098 builder.addCapability(spv::CapabilityGroups);
David Netobb5c02f2016-10-19 10:16:29 -04006099#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +08006100 if (op == glslang::EOpMinInvocationsNonUniform ||
6101 op == glslang::EOpMaxInvocationsNonUniform ||
Rex Xu430ef402016-10-14 17:22:23 +08006102 op == glslang::EOpAddInvocationsNonUniform ||
6103 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6104 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6105 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
6106 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
6107 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
6108 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
Rex Xu17ff3432016-10-14 17:41:45 +08006109 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
David Netobb5c02f2016-10-19 10:16:29 -04006110#endif
Rex Xu51596642016-09-21 18:56:12 +08006111
Rex Xu9d93a232016-05-05 12:30:44 +08006112#ifdef AMD_EXTENSIONS
Rex Xu430ef402016-10-14 17:22:23 +08006113 switch (op) {
6114 case glslang::EOpMinInvocations:
6115 case glslang::EOpMaxInvocations:
6116 case glslang::EOpAddInvocations:
6117 case glslang::EOpMinInvocationsNonUniform:
6118 case glslang::EOpMaxInvocationsNonUniform:
6119 case glslang::EOpAddInvocationsNonUniform:
6120 groupOperation = spv::GroupOperationReduce;
Rex Xu430ef402016-10-14 17:22:23 +08006121 break;
6122 case glslang::EOpMinInvocationsInclusiveScan:
6123 case glslang::EOpMaxInvocationsInclusiveScan:
6124 case glslang::EOpAddInvocationsInclusiveScan:
6125 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6126 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6127 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6128 groupOperation = spv::GroupOperationInclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006129 break;
6130 case glslang::EOpMinInvocationsExclusiveScan:
6131 case glslang::EOpMaxInvocationsExclusiveScan:
6132 case glslang::EOpAddInvocationsExclusiveScan:
6133 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6134 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6135 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6136 groupOperation = spv::GroupOperationExclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006137 break;
Mike Weiblen4e9e4002017-01-20 13:34:10 -07006138 default:
6139 break;
Rex Xu430ef402016-10-14 17:22:23 +08006140 }
John Kessenich149afc32018-08-14 13:31:43 -06006141 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6142 spvGroupOperands.push_back(scope);
6143 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06006144 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006145 spvGroupOperands.push_back(groupOp);
6146 }
Rex Xu9d93a232016-05-05 12:30:44 +08006147#endif
Rex Xu51596642016-09-21 18:56:12 +08006148 }
6149
John Kessenich149afc32018-08-14 13:31:43 -06006150 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) {
6151 spv::IdImmediate op = { true, *opIt };
6152 spvGroupOperands.push_back(op);
6153 }
John Kessenich91cef522016-05-05 16:45:40 -06006154
6155 switch (op) {
6156 case glslang::EOpAnyInvocation:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006157 opCode = spv::OpSubgroupAnyKHR;
Rex Xu51596642016-09-21 18:56:12 +08006158 break;
John Kessenich91cef522016-05-05 16:45:40 -06006159 case glslang::EOpAllInvocations:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006160 opCode = spv::OpSubgroupAllKHR;
Rex Xu51596642016-09-21 18:56:12 +08006161 break;
John Kessenich91cef522016-05-05 16:45:40 -06006162 case glslang::EOpAllInvocationsEqual:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006163 opCode = spv::OpSubgroupAllEqualKHR;
6164 break;
Rex Xu51596642016-09-21 18:56:12 +08006165 case glslang::EOpReadInvocation:
chaocf200da82016-12-20 12:44:35 -08006166 opCode = spv::OpSubgroupReadInvocationKHR;
Rex Xub7072052016-09-26 15:53:40 +08006167 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006168 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006169 break;
6170 case glslang::EOpReadFirstInvocation:
6171 opCode = spv::OpSubgroupFirstInvocationKHR;
6172 break;
6173 case glslang::EOpBallot:
6174 {
6175 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
6176 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
6177 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
6178 //
6179 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
6180 //
6181 spv::Id uintType = builder.makeUintType(32);
6182 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
6183 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
6184
6185 std::vector<spv::Id> components;
6186 components.push_back(builder.createCompositeExtract(result, uintType, 0));
6187 components.push_back(builder.createCompositeExtract(result, uintType, 1));
6188
6189 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
6190 return builder.createUnaryOp(spv::OpBitcast, typeId,
6191 builder.createCompositeConstruct(uvec2Type, components));
6192 }
6193
Rex Xu9d93a232016-05-05 12:30:44 +08006194#ifdef AMD_EXTENSIONS
6195 case glslang::EOpMinInvocations:
6196 case glslang::EOpMaxInvocations:
6197 case glslang::EOpAddInvocations:
Rex Xu430ef402016-10-14 17:22:23 +08006198 case glslang::EOpMinInvocationsInclusiveScan:
6199 case glslang::EOpMaxInvocationsInclusiveScan:
6200 case glslang::EOpAddInvocationsInclusiveScan:
6201 case glslang::EOpMinInvocationsExclusiveScan:
6202 case glslang::EOpMaxInvocationsExclusiveScan:
6203 case glslang::EOpAddInvocationsExclusiveScan:
6204 if (op == glslang::EOpMinInvocations ||
6205 op == glslang::EOpMinInvocationsInclusiveScan ||
6206 op == glslang::EOpMinInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006207 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006208 opCode = spv::OpGroupFMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006209 else {
6210 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006211 opCode = spv::OpGroupUMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006212 else
Rex Xu51596642016-09-21 18:56:12 +08006213 opCode = spv::OpGroupSMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006214 }
Rex Xu430ef402016-10-14 17:22:23 +08006215 } else if (op == glslang::EOpMaxInvocations ||
6216 op == glslang::EOpMaxInvocationsInclusiveScan ||
6217 op == glslang::EOpMaxInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006218 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006219 opCode = spv::OpGroupFMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006220 else {
6221 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006222 opCode = spv::OpGroupUMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006223 else
Rex Xu51596642016-09-21 18:56:12 +08006224 opCode = spv::OpGroupSMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006225 }
6226 } else {
6227 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006228 opCode = spv::OpGroupFAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006229 else
Rex Xu51596642016-09-21 18:56:12 +08006230 opCode = spv::OpGroupIAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006231 }
6232
Rex Xu2bbbe062016-08-23 15:41:05 +08006233 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006234 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006235
6236 break;
Rex Xu9d93a232016-05-05 12:30:44 +08006237 case glslang::EOpMinInvocationsNonUniform:
6238 case glslang::EOpMaxInvocationsNonUniform:
6239 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08006240 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6241 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6242 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6243 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6244 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6245 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6246 if (op == glslang::EOpMinInvocationsNonUniform ||
6247 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6248 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08006249 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006250 opCode = spv::OpGroupFMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006251 else {
6252 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006253 opCode = spv::OpGroupUMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006254 else
Rex Xu51596642016-09-21 18:56:12 +08006255 opCode = spv::OpGroupSMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006256 }
6257 }
Rex Xu430ef402016-10-14 17:22:23 +08006258 else if (op == glslang::EOpMaxInvocationsNonUniform ||
6259 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6260 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08006261 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006262 opCode = spv::OpGroupFMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006263 else {
6264 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006265 opCode = spv::OpGroupUMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006266 else
Rex Xu51596642016-09-21 18:56:12 +08006267 opCode = spv::OpGroupSMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006268 }
6269 }
6270 else {
6271 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006272 opCode = spv::OpGroupFAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006273 else
Rex Xu51596642016-09-21 18:56:12 +08006274 opCode = spv::OpGroupIAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006275 }
6276
Rex Xu2bbbe062016-08-23 15:41:05 +08006277 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006278 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006279
6280 break;
Rex Xu9d93a232016-05-05 12:30:44 +08006281#endif
John Kessenich91cef522016-05-05 16:45:40 -06006282 default:
6283 logger->missingFunctionality("invocation operation");
6284 return spv::NoResult;
6285 }
Rex Xu51596642016-09-21 18:56:12 +08006286
6287 assert(opCode != spv::OpNop);
6288 return builder.createOp(opCode, typeId, spvGroupOperands);
John Kessenich91cef522016-05-05 16:45:40 -06006289}
6290
Rex Xu2bbbe062016-08-23 15:41:05 +08006291// Create group invocation operations on a vector
John Kessenich149afc32018-08-14 13:31:43 -06006292spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
6293 spv::Id typeId, std::vector<spv::Id>& operands)
Rex Xu2bbbe062016-08-23 15:41:05 +08006294{
Rex Xub7072052016-09-26 15:53:40 +08006295#ifdef AMD_EXTENSIONS
Rex Xu2bbbe062016-08-23 15:41:05 +08006296 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
6297 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
Rex Xub7072052016-09-26 15:53:40 +08006298 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
chaocf200da82016-12-20 12:44:35 -08006299 op == spv::OpSubgroupReadInvocationKHR ||
Rex Xu2bbbe062016-08-23 15:41:05 +08006300 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD ||
6301 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD ||
6302 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
Rex Xub7072052016-09-26 15:53:40 +08006303#else
6304 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
6305 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
chaocf200da82016-12-20 12:44:35 -08006306 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
6307 op == spv::OpSubgroupReadInvocationKHR);
Rex Xub7072052016-09-26 15:53:40 +08006308#endif
Rex Xu2bbbe062016-08-23 15:41:05 +08006309
6310 // Handle group invocation operations scalar by scalar.
6311 // The result type is the same type as the original type.
6312 // The algorithm is to:
6313 // - break the vector into scalars
6314 // - apply the operation to each scalar
6315 // - make a vector out the scalar results
6316
6317 // get the types sorted out
Rex Xub7072052016-09-26 15:53:40 +08006318 int numComponents = builder.getNumComponents(operands[0]);
6319 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
Rex Xu2bbbe062016-08-23 15:41:05 +08006320 std::vector<spv::Id> results;
6321
6322 // do each scalar op
6323 for (int comp = 0; comp < numComponents; ++comp) {
6324 std::vector<unsigned int> indexes;
6325 indexes.push_back(comp);
John Kessenich149afc32018-08-14 13:31:43 -06006326 spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) };
6327 std::vector<spv::IdImmediate> spvGroupOperands;
chaocf200da82016-12-20 12:44:35 -08006328 if (op == spv::OpSubgroupReadInvocationKHR) {
6329 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06006330 spv::IdImmediate operand = { true, operands[1] };
6331 spvGroupOperands.push_back(operand);
chaocf200da82016-12-20 12:44:35 -08006332 } else if (op == spv::OpGroupBroadcast) {
John Kessenich149afc32018-08-14 13:31:43 -06006333 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6334 spvGroupOperands.push_back(scope);
Rex Xub7072052016-09-26 15:53:40 +08006335 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06006336 spv::IdImmediate operand = { true, operands[1] };
6337 spvGroupOperands.push_back(operand);
Rex Xub7072052016-09-26 15:53:40 +08006338 } else {
John Kessenich149afc32018-08-14 13:31:43 -06006339 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6340 spvGroupOperands.push_back(scope);
John Kessenichd122a722018-09-18 03:43:30 -06006341 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006342 spvGroupOperands.push_back(groupOp);
Rex Xub7072052016-09-26 15:53:40 +08006343 spvGroupOperands.push_back(scalar);
6344 }
Rex Xu2bbbe062016-08-23 15:41:05 +08006345
Rex Xub7072052016-09-26 15:53:40 +08006346 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
Rex Xu2bbbe062016-08-23 15:41:05 +08006347 }
6348
6349 // put the pieces together
6350 return builder.createCompositeConstruct(typeId, results);
6351}
Rex Xu2bbbe062016-08-23 15:41:05 +08006352
John Kessenich66011cb2018-03-06 16:12:04 -07006353// Create subgroup invocation operations.
John Kessenich149afc32018-08-14 13:31:43 -06006354spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
6355 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich66011cb2018-03-06 16:12:04 -07006356{
6357 // Add the required capabilities.
6358 switch (op) {
6359 case glslang::EOpSubgroupElect:
6360 builder.addCapability(spv::CapabilityGroupNonUniform);
6361 break;
6362 case glslang::EOpSubgroupAll:
6363 case glslang::EOpSubgroupAny:
6364 case glslang::EOpSubgroupAllEqual:
6365 builder.addCapability(spv::CapabilityGroupNonUniform);
6366 builder.addCapability(spv::CapabilityGroupNonUniformVote);
6367 break;
6368 case glslang::EOpSubgroupBroadcast:
6369 case glslang::EOpSubgroupBroadcastFirst:
6370 case glslang::EOpSubgroupBallot:
6371 case glslang::EOpSubgroupInverseBallot:
6372 case glslang::EOpSubgroupBallotBitExtract:
6373 case glslang::EOpSubgroupBallotBitCount:
6374 case glslang::EOpSubgroupBallotInclusiveBitCount:
6375 case glslang::EOpSubgroupBallotExclusiveBitCount:
6376 case glslang::EOpSubgroupBallotFindLSB:
6377 case glslang::EOpSubgroupBallotFindMSB:
6378 builder.addCapability(spv::CapabilityGroupNonUniform);
6379 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
6380 break;
6381 case glslang::EOpSubgroupShuffle:
6382 case glslang::EOpSubgroupShuffleXor:
6383 builder.addCapability(spv::CapabilityGroupNonUniform);
6384 builder.addCapability(spv::CapabilityGroupNonUniformShuffle);
6385 break;
6386 case glslang::EOpSubgroupShuffleUp:
6387 case glslang::EOpSubgroupShuffleDown:
6388 builder.addCapability(spv::CapabilityGroupNonUniform);
6389 builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative);
6390 break;
6391 case glslang::EOpSubgroupAdd:
6392 case glslang::EOpSubgroupMul:
6393 case glslang::EOpSubgroupMin:
6394 case glslang::EOpSubgroupMax:
6395 case glslang::EOpSubgroupAnd:
6396 case glslang::EOpSubgroupOr:
6397 case glslang::EOpSubgroupXor:
6398 case glslang::EOpSubgroupInclusiveAdd:
6399 case glslang::EOpSubgroupInclusiveMul:
6400 case glslang::EOpSubgroupInclusiveMin:
6401 case glslang::EOpSubgroupInclusiveMax:
6402 case glslang::EOpSubgroupInclusiveAnd:
6403 case glslang::EOpSubgroupInclusiveOr:
6404 case glslang::EOpSubgroupInclusiveXor:
6405 case glslang::EOpSubgroupExclusiveAdd:
6406 case glslang::EOpSubgroupExclusiveMul:
6407 case glslang::EOpSubgroupExclusiveMin:
6408 case glslang::EOpSubgroupExclusiveMax:
6409 case glslang::EOpSubgroupExclusiveAnd:
6410 case glslang::EOpSubgroupExclusiveOr:
6411 case glslang::EOpSubgroupExclusiveXor:
6412 builder.addCapability(spv::CapabilityGroupNonUniform);
6413 builder.addCapability(spv::CapabilityGroupNonUniformArithmetic);
6414 break;
6415 case glslang::EOpSubgroupClusteredAdd:
6416 case glslang::EOpSubgroupClusteredMul:
6417 case glslang::EOpSubgroupClusteredMin:
6418 case glslang::EOpSubgroupClusteredMax:
6419 case glslang::EOpSubgroupClusteredAnd:
6420 case glslang::EOpSubgroupClusteredOr:
6421 case glslang::EOpSubgroupClusteredXor:
6422 builder.addCapability(spv::CapabilityGroupNonUniform);
6423 builder.addCapability(spv::CapabilityGroupNonUniformClustered);
6424 break;
6425 case glslang::EOpSubgroupQuadBroadcast:
6426 case glslang::EOpSubgroupQuadSwapHorizontal:
6427 case glslang::EOpSubgroupQuadSwapVertical:
6428 case glslang::EOpSubgroupQuadSwapDiagonal:
6429 builder.addCapability(spv::CapabilityGroupNonUniform);
6430 builder.addCapability(spv::CapabilityGroupNonUniformQuad);
6431 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006432#ifdef NV_EXTENSIONS
6433 case glslang::EOpSubgroupPartitionedAdd:
6434 case glslang::EOpSubgroupPartitionedMul:
6435 case glslang::EOpSubgroupPartitionedMin:
6436 case glslang::EOpSubgroupPartitionedMax:
6437 case glslang::EOpSubgroupPartitionedAnd:
6438 case glslang::EOpSubgroupPartitionedOr:
6439 case glslang::EOpSubgroupPartitionedXor:
6440 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6441 case glslang::EOpSubgroupPartitionedInclusiveMul:
6442 case glslang::EOpSubgroupPartitionedInclusiveMin:
6443 case glslang::EOpSubgroupPartitionedInclusiveMax:
6444 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6445 case glslang::EOpSubgroupPartitionedInclusiveOr:
6446 case glslang::EOpSubgroupPartitionedInclusiveXor:
6447 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6448 case glslang::EOpSubgroupPartitionedExclusiveMul:
6449 case glslang::EOpSubgroupPartitionedExclusiveMin:
6450 case glslang::EOpSubgroupPartitionedExclusiveMax:
6451 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6452 case glslang::EOpSubgroupPartitionedExclusiveOr:
6453 case glslang::EOpSubgroupPartitionedExclusiveXor:
6454 builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned);
6455 builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV);
6456 break;
6457#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006458 default: assert(0 && "Unhandled subgroup operation!");
6459 }
6460
6461 const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
6462 const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble;
6463 const bool isBool = typeProxy == glslang::EbtBool;
6464
6465 spv::Op opCode = spv::OpNop;
6466
6467 // Figure out which opcode to use.
6468 switch (op) {
6469 case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break;
6470 case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break;
6471 case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break;
6472 case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break;
6473 case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break;
6474 case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break;
6475 case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break;
6476 case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break;
6477 case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break;
6478 case glslang::EOpSubgroupBallotBitCount:
6479 case glslang::EOpSubgroupBallotInclusiveBitCount:
6480 case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break;
6481 case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break;
6482 case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break;
6483 case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break;
6484 case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break;
6485 case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break;
6486 case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break;
6487 case glslang::EOpSubgroupAdd:
6488 case glslang::EOpSubgroupInclusiveAdd:
6489 case glslang::EOpSubgroupExclusiveAdd:
6490 case glslang::EOpSubgroupClusteredAdd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006491#ifdef NV_EXTENSIONS
6492 case glslang::EOpSubgroupPartitionedAdd:
6493 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6494 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6495#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006496 if (isFloat) {
6497 opCode = spv::OpGroupNonUniformFAdd;
6498 } else {
6499 opCode = spv::OpGroupNonUniformIAdd;
6500 }
6501 break;
6502 case glslang::EOpSubgroupMul:
6503 case glslang::EOpSubgroupInclusiveMul:
6504 case glslang::EOpSubgroupExclusiveMul:
6505 case glslang::EOpSubgroupClusteredMul:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006506#ifdef NV_EXTENSIONS
6507 case glslang::EOpSubgroupPartitionedMul:
6508 case glslang::EOpSubgroupPartitionedInclusiveMul:
6509 case glslang::EOpSubgroupPartitionedExclusiveMul:
6510#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006511 if (isFloat) {
6512 opCode = spv::OpGroupNonUniformFMul;
6513 } else {
6514 opCode = spv::OpGroupNonUniformIMul;
6515 }
6516 break;
6517 case glslang::EOpSubgroupMin:
6518 case glslang::EOpSubgroupInclusiveMin:
6519 case glslang::EOpSubgroupExclusiveMin:
6520 case glslang::EOpSubgroupClusteredMin:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006521#ifdef NV_EXTENSIONS
6522 case glslang::EOpSubgroupPartitionedMin:
6523 case glslang::EOpSubgroupPartitionedInclusiveMin:
6524 case glslang::EOpSubgroupPartitionedExclusiveMin:
6525#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006526 if (isFloat) {
6527 opCode = spv::OpGroupNonUniformFMin;
6528 } else if (isUnsigned) {
6529 opCode = spv::OpGroupNonUniformUMin;
6530 } else {
6531 opCode = spv::OpGroupNonUniformSMin;
6532 }
6533 break;
6534 case glslang::EOpSubgroupMax:
6535 case glslang::EOpSubgroupInclusiveMax:
6536 case glslang::EOpSubgroupExclusiveMax:
6537 case glslang::EOpSubgroupClusteredMax:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006538#ifdef NV_EXTENSIONS
6539 case glslang::EOpSubgroupPartitionedMax:
6540 case glslang::EOpSubgroupPartitionedInclusiveMax:
6541 case glslang::EOpSubgroupPartitionedExclusiveMax:
6542#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006543 if (isFloat) {
6544 opCode = spv::OpGroupNonUniformFMax;
6545 } else if (isUnsigned) {
6546 opCode = spv::OpGroupNonUniformUMax;
6547 } else {
6548 opCode = spv::OpGroupNonUniformSMax;
6549 }
6550 break;
6551 case glslang::EOpSubgroupAnd:
6552 case glslang::EOpSubgroupInclusiveAnd:
6553 case glslang::EOpSubgroupExclusiveAnd:
6554 case glslang::EOpSubgroupClusteredAnd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006555#ifdef NV_EXTENSIONS
6556 case glslang::EOpSubgroupPartitionedAnd:
6557 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6558 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6559#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006560 if (isBool) {
6561 opCode = spv::OpGroupNonUniformLogicalAnd;
6562 } else {
6563 opCode = spv::OpGroupNonUniformBitwiseAnd;
6564 }
6565 break;
6566 case glslang::EOpSubgroupOr:
6567 case glslang::EOpSubgroupInclusiveOr:
6568 case glslang::EOpSubgroupExclusiveOr:
6569 case glslang::EOpSubgroupClusteredOr:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006570#ifdef NV_EXTENSIONS
6571 case glslang::EOpSubgroupPartitionedOr:
6572 case glslang::EOpSubgroupPartitionedInclusiveOr:
6573 case glslang::EOpSubgroupPartitionedExclusiveOr:
6574#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006575 if (isBool) {
6576 opCode = spv::OpGroupNonUniformLogicalOr;
6577 } else {
6578 opCode = spv::OpGroupNonUniformBitwiseOr;
6579 }
6580 break;
6581 case glslang::EOpSubgroupXor:
6582 case glslang::EOpSubgroupInclusiveXor:
6583 case glslang::EOpSubgroupExclusiveXor:
6584 case glslang::EOpSubgroupClusteredXor:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006585#ifdef NV_EXTENSIONS
6586 case glslang::EOpSubgroupPartitionedXor:
6587 case glslang::EOpSubgroupPartitionedInclusiveXor:
6588 case glslang::EOpSubgroupPartitionedExclusiveXor:
6589#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006590 if (isBool) {
6591 opCode = spv::OpGroupNonUniformLogicalXor;
6592 } else {
6593 opCode = spv::OpGroupNonUniformBitwiseXor;
6594 }
6595 break;
6596 case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break;
6597 case glslang::EOpSubgroupQuadSwapHorizontal:
6598 case glslang::EOpSubgroupQuadSwapVertical:
6599 case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break;
6600 default: assert(0 && "Unhandled subgroup operation!");
6601 }
6602
John Kessenich149afc32018-08-14 13:31:43 -06006603 // get the right Group Operation
6604 spv::GroupOperation groupOperation = spv::GroupOperationMax;
John Kessenich66011cb2018-03-06 16:12:04 -07006605 switch (op) {
John Kessenich149afc32018-08-14 13:31:43 -06006606 default:
6607 break;
John Kessenich66011cb2018-03-06 16:12:04 -07006608 case glslang::EOpSubgroupBallotBitCount:
6609 case glslang::EOpSubgroupAdd:
6610 case glslang::EOpSubgroupMul:
6611 case glslang::EOpSubgroupMin:
6612 case glslang::EOpSubgroupMax:
6613 case glslang::EOpSubgroupAnd:
6614 case glslang::EOpSubgroupOr:
6615 case glslang::EOpSubgroupXor:
John Kessenich149afc32018-08-14 13:31:43 -06006616 groupOperation = spv::GroupOperationReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07006617 break;
6618 case glslang::EOpSubgroupBallotInclusiveBitCount:
6619 case glslang::EOpSubgroupInclusiveAdd:
6620 case glslang::EOpSubgroupInclusiveMul:
6621 case glslang::EOpSubgroupInclusiveMin:
6622 case glslang::EOpSubgroupInclusiveMax:
6623 case glslang::EOpSubgroupInclusiveAnd:
6624 case glslang::EOpSubgroupInclusiveOr:
6625 case glslang::EOpSubgroupInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06006626 groupOperation = spv::GroupOperationInclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07006627 break;
6628 case glslang::EOpSubgroupBallotExclusiveBitCount:
6629 case glslang::EOpSubgroupExclusiveAdd:
6630 case glslang::EOpSubgroupExclusiveMul:
6631 case glslang::EOpSubgroupExclusiveMin:
6632 case glslang::EOpSubgroupExclusiveMax:
6633 case glslang::EOpSubgroupExclusiveAnd:
6634 case glslang::EOpSubgroupExclusiveOr:
6635 case glslang::EOpSubgroupExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06006636 groupOperation = spv::GroupOperationExclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07006637 break;
6638 case glslang::EOpSubgroupClusteredAdd:
6639 case glslang::EOpSubgroupClusteredMul:
6640 case glslang::EOpSubgroupClusteredMin:
6641 case glslang::EOpSubgroupClusteredMax:
6642 case glslang::EOpSubgroupClusteredAnd:
6643 case glslang::EOpSubgroupClusteredOr:
6644 case glslang::EOpSubgroupClusteredXor:
John Kessenich149afc32018-08-14 13:31:43 -06006645 groupOperation = spv::GroupOperationClusteredReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07006646 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006647#ifdef NV_EXTENSIONS
6648 case glslang::EOpSubgroupPartitionedAdd:
6649 case glslang::EOpSubgroupPartitionedMul:
6650 case glslang::EOpSubgroupPartitionedMin:
6651 case glslang::EOpSubgroupPartitionedMax:
6652 case glslang::EOpSubgroupPartitionedAnd:
6653 case glslang::EOpSubgroupPartitionedOr:
6654 case glslang::EOpSubgroupPartitionedXor:
John Kessenich149afc32018-08-14 13:31:43 -06006655 groupOperation = spv::GroupOperationPartitionedReduceNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006656 break;
6657 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6658 case glslang::EOpSubgroupPartitionedInclusiveMul:
6659 case glslang::EOpSubgroupPartitionedInclusiveMin:
6660 case glslang::EOpSubgroupPartitionedInclusiveMax:
6661 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6662 case glslang::EOpSubgroupPartitionedInclusiveOr:
6663 case glslang::EOpSubgroupPartitionedInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06006664 groupOperation = spv::GroupOperationPartitionedInclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006665 break;
6666 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6667 case glslang::EOpSubgroupPartitionedExclusiveMul:
6668 case glslang::EOpSubgroupPartitionedExclusiveMin:
6669 case glslang::EOpSubgroupPartitionedExclusiveMax:
6670 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6671 case glslang::EOpSubgroupPartitionedExclusiveOr:
6672 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06006673 groupOperation = spv::GroupOperationPartitionedExclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006674 break;
6675#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006676 }
6677
John Kessenich149afc32018-08-14 13:31:43 -06006678 // build the instruction
6679 std::vector<spv::IdImmediate> spvGroupOperands;
6680
6681 // Every operation begins with the Execution Scope operand.
6682 spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6683 spvGroupOperands.push_back(executionScope);
6684
6685 // Next, for all operations that use a Group Operation, push that as an operand.
6686 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06006687 spv::IdImmediate groupOperand = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006688 spvGroupOperands.push_back(groupOperand);
6689 }
6690
John Kessenich66011cb2018-03-06 16:12:04 -07006691 // Push back the operands next.
John Kessenich149afc32018-08-14 13:31:43 -06006692 for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) {
6693 spv::IdImmediate operand = { true, *opIt };
6694 spvGroupOperands.push_back(operand);
John Kessenich66011cb2018-03-06 16:12:04 -07006695 }
6696
6697 // Some opcodes have additional operands.
John Kessenich149afc32018-08-14 13:31:43 -06006698 spv::Id directionId = spv::NoResult;
John Kessenich66011cb2018-03-06 16:12:04 -07006699 switch (op) {
6700 default: break;
John Kessenich149afc32018-08-14 13:31:43 -06006701 case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break;
6702 case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break;
6703 case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break;
6704 }
6705 if (directionId != spv::NoResult) {
6706 spv::IdImmediate direction = { true, directionId };
6707 spvGroupOperands.push_back(direction);
John Kessenich66011cb2018-03-06 16:12:04 -07006708 }
6709
6710 return builder.createOp(opCode, typeId, spvGroupOperands);
6711}
6712
John Kessenich5e4b1242015-08-06 22:53:06 -06006713spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06006714{
John Kessenich66011cb2018-03-06 16:12:04 -07006715 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6716 bool isFloat = isTypeFloat(typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -06006717
John Kessenich140f3df2015-06-26 16:58:36 -06006718 spv::Op opCode = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08006719 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06006720 int libCall = -1;
Mark Adams364c21c2016-01-06 13:41:02 -05006721 size_t consumedOperands = operands.size();
John Kessenich55e7d112015-11-15 21:33:39 -07006722 spv::Id typeId0 = 0;
6723 if (consumedOperands > 0)
6724 typeId0 = builder.getTypeId(operands[0]);
Rex Xu470026f2017-03-29 17:12:40 +08006725 spv::Id typeId1 = 0;
6726 if (consumedOperands > 1)
6727 typeId1 = builder.getTypeId(operands[1]);
John Kessenich55e7d112015-11-15 21:33:39 -07006728 spv::Id frexpIntType = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06006729
6730 switch (op) {
6731 case glslang::EOpMin:
John Kessenich5e4b1242015-08-06 22:53:06 -06006732 if (isFloat)
6733 libCall = spv::GLSLstd450FMin;
6734 else if (isUnsigned)
6735 libCall = spv::GLSLstd450UMin;
6736 else
6737 libCall = spv::GLSLstd450SMin;
John Kesseniche7c83cf2015-12-13 13:34:37 -07006738 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06006739 break;
6740 case glslang::EOpModf:
John Kessenich5e4b1242015-08-06 22:53:06 -06006741 libCall = spv::GLSLstd450Modf;
John Kessenich140f3df2015-06-26 16:58:36 -06006742 break;
6743 case glslang::EOpMax:
John Kessenich5e4b1242015-08-06 22:53:06 -06006744 if (isFloat)
6745 libCall = spv::GLSLstd450FMax;
6746 else if (isUnsigned)
6747 libCall = spv::GLSLstd450UMax;
6748 else
6749 libCall = spv::GLSLstd450SMax;
John Kesseniche7c83cf2015-12-13 13:34:37 -07006750 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06006751 break;
6752 case glslang::EOpPow:
John Kessenich5e4b1242015-08-06 22:53:06 -06006753 libCall = spv::GLSLstd450Pow;
John Kessenich140f3df2015-06-26 16:58:36 -06006754 break;
6755 case glslang::EOpDot:
6756 opCode = spv::OpDot;
6757 break;
6758 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06006759 libCall = spv::GLSLstd450Atan2;
John Kessenich140f3df2015-06-26 16:58:36 -06006760 break;
6761
6762 case glslang::EOpClamp:
John Kessenich5e4b1242015-08-06 22:53:06 -06006763 if (isFloat)
6764 libCall = spv::GLSLstd450FClamp;
6765 else if (isUnsigned)
6766 libCall = spv::GLSLstd450UClamp;
6767 else
6768 libCall = spv::GLSLstd450SClamp;
John Kesseniche7c83cf2015-12-13 13:34:37 -07006769 builder.promoteScalar(precision, operands.front(), operands[1]);
6770 builder.promoteScalar(precision, operands.front(), operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06006771 break;
6772 case glslang::EOpMix:
Rex Xud715adc2016-03-15 12:08:31 +08006773 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
6774 assert(isFloat);
John Kessenich55e7d112015-11-15 21:33:39 -07006775 libCall = spv::GLSLstd450FMix;
Rex Xud715adc2016-03-15 12:08:31 +08006776 } else {
John Kessenich6c292d32016-02-15 20:58:50 -07006777 opCode = spv::OpSelect;
Rex Xud715adc2016-03-15 12:08:31 +08006778 std::swap(operands.front(), operands.back());
John Kessenich6c292d32016-02-15 20:58:50 -07006779 }
John Kesseniche7c83cf2015-12-13 13:34:37 -07006780 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06006781 break;
6782 case glslang::EOpStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06006783 libCall = spv::GLSLstd450Step;
John Kesseniche7c83cf2015-12-13 13:34:37 -07006784 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06006785 break;
6786 case glslang::EOpSmoothStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06006787 libCall = spv::GLSLstd450SmoothStep;
John Kesseniche7c83cf2015-12-13 13:34:37 -07006788 builder.promoteScalar(precision, operands[0], operands[2]);
6789 builder.promoteScalar(precision, operands[1], operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06006790 break;
6791
6792 case glslang::EOpDistance:
John Kessenich5e4b1242015-08-06 22:53:06 -06006793 libCall = spv::GLSLstd450Distance;
John Kessenich140f3df2015-06-26 16:58:36 -06006794 break;
6795 case glslang::EOpCross:
John Kessenich5e4b1242015-08-06 22:53:06 -06006796 libCall = spv::GLSLstd450Cross;
John Kessenich140f3df2015-06-26 16:58:36 -06006797 break;
6798 case glslang::EOpFaceForward:
John Kessenich5e4b1242015-08-06 22:53:06 -06006799 libCall = spv::GLSLstd450FaceForward;
John Kessenich140f3df2015-06-26 16:58:36 -06006800 break;
6801 case glslang::EOpReflect:
John Kessenich5e4b1242015-08-06 22:53:06 -06006802 libCall = spv::GLSLstd450Reflect;
John Kessenich140f3df2015-06-26 16:58:36 -06006803 break;
6804 case glslang::EOpRefract:
John Kessenich5e4b1242015-08-06 22:53:06 -06006805 libCall = spv::GLSLstd450Refract;
John Kessenich140f3df2015-06-26 16:58:36 -06006806 break;
Rex Xu7a26c172015-12-08 17:12:09 +08006807 case glslang::EOpInterpolateAtSample:
Rex Xub4a2a6c2018-05-17 13:51:28 +08006808#ifdef AMD_EXTENSIONS
6809 if (typeProxy == glslang::EbtFloat16)
6810 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6811#endif
Rex Xu7a26c172015-12-08 17:12:09 +08006812 libCall = spv::GLSLstd450InterpolateAtSample;
6813 break;
6814 case glslang::EOpInterpolateAtOffset:
Rex Xub4a2a6c2018-05-17 13:51:28 +08006815#ifdef AMD_EXTENSIONS
6816 if (typeProxy == glslang::EbtFloat16)
6817 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
6818#endif
Rex Xu7a26c172015-12-08 17:12:09 +08006819 libCall = spv::GLSLstd450InterpolateAtOffset;
6820 break;
John Kessenich55e7d112015-11-15 21:33:39 -07006821 case glslang::EOpAddCarry:
6822 opCode = spv::OpIAddCarry;
6823 typeId = builder.makeStructResultType(typeId0, typeId0);
6824 consumedOperands = 2;
6825 break;
6826 case glslang::EOpSubBorrow:
6827 opCode = spv::OpISubBorrow;
6828 typeId = builder.makeStructResultType(typeId0, typeId0);
6829 consumedOperands = 2;
6830 break;
6831 case glslang::EOpUMulExtended:
6832 opCode = spv::OpUMulExtended;
6833 typeId = builder.makeStructResultType(typeId0, typeId0);
6834 consumedOperands = 2;
6835 break;
6836 case glslang::EOpIMulExtended:
6837 opCode = spv::OpSMulExtended;
6838 typeId = builder.makeStructResultType(typeId0, typeId0);
6839 consumedOperands = 2;
6840 break;
6841 case glslang::EOpBitfieldExtract:
6842 if (isUnsigned)
6843 opCode = spv::OpBitFieldUExtract;
6844 else
6845 opCode = spv::OpBitFieldSExtract;
6846 break;
6847 case glslang::EOpBitfieldInsert:
6848 opCode = spv::OpBitFieldInsert;
6849 break;
6850
6851 case glslang::EOpFma:
6852 libCall = spv::GLSLstd450Fma;
6853 break;
6854 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08006855 {
6856 libCall = spv::GLSLstd450FrexpStruct;
6857 assert(builder.isPointerType(typeId1));
6858 typeId1 = builder.getContainedTypeId(typeId1);
Rex Xu470026f2017-03-29 17:12:40 +08006859 int width = builder.getScalarTypeWidth(typeId1);
Rex Xu7c88aff2018-04-11 16:56:50 +08006860#ifdef AMD_EXTENSIONS
6861 if (width == 16)
6862 // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16
6863 builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16);
6864#endif
Rex Xu470026f2017-03-29 17:12:40 +08006865 if (builder.getNumComponents(operands[0]) == 1)
6866 frexpIntType = builder.makeIntegerType(width, true);
6867 else
6868 frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0]));
6869 typeId = builder.makeStructResultType(typeId0, frexpIntType);
6870 consumedOperands = 1;
6871 }
John Kessenich55e7d112015-11-15 21:33:39 -07006872 break;
6873 case glslang::EOpLdexp:
6874 libCall = spv::GLSLstd450Ldexp;
6875 break;
6876
Rex Xu574ab042016-04-14 16:53:07 +08006877 case glslang::EOpReadInvocation:
Rex Xu51596642016-09-21 18:56:12 +08006878 return createInvocationsOperation(op, typeId, operands, typeProxy);
Rex Xu574ab042016-04-14 16:53:07 +08006879
John Kessenich66011cb2018-03-06 16:12:04 -07006880 case glslang::EOpSubgroupBroadcast:
6881 case glslang::EOpSubgroupBallotBitExtract:
6882 case glslang::EOpSubgroupShuffle:
6883 case glslang::EOpSubgroupShuffleXor:
6884 case glslang::EOpSubgroupShuffleUp:
6885 case glslang::EOpSubgroupShuffleDown:
6886 case glslang::EOpSubgroupClusteredAdd:
6887 case glslang::EOpSubgroupClusteredMul:
6888 case glslang::EOpSubgroupClusteredMin:
6889 case glslang::EOpSubgroupClusteredMax:
6890 case glslang::EOpSubgroupClusteredAnd:
6891 case glslang::EOpSubgroupClusteredOr:
6892 case glslang::EOpSubgroupClusteredXor:
6893 case glslang::EOpSubgroupQuadBroadcast:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006894#ifdef NV_EXTENSIONS
6895 case glslang::EOpSubgroupPartitionedAdd:
6896 case glslang::EOpSubgroupPartitionedMul:
6897 case glslang::EOpSubgroupPartitionedMin:
6898 case glslang::EOpSubgroupPartitionedMax:
6899 case glslang::EOpSubgroupPartitionedAnd:
6900 case glslang::EOpSubgroupPartitionedOr:
6901 case glslang::EOpSubgroupPartitionedXor:
6902 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6903 case glslang::EOpSubgroupPartitionedInclusiveMul:
6904 case glslang::EOpSubgroupPartitionedInclusiveMin:
6905 case glslang::EOpSubgroupPartitionedInclusiveMax:
6906 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6907 case glslang::EOpSubgroupPartitionedInclusiveOr:
6908 case glslang::EOpSubgroupPartitionedInclusiveXor:
6909 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6910 case glslang::EOpSubgroupPartitionedExclusiveMul:
6911 case glslang::EOpSubgroupPartitionedExclusiveMin:
6912 case glslang::EOpSubgroupPartitionedExclusiveMax:
6913 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6914 case glslang::EOpSubgroupPartitionedExclusiveOr:
6915 case glslang::EOpSubgroupPartitionedExclusiveXor:
6916#endif
John Kessenich66011cb2018-03-06 16:12:04 -07006917 return createSubgroupOperation(op, typeId, operands, typeProxy);
6918
Rex Xu9d93a232016-05-05 12:30:44 +08006919#ifdef AMD_EXTENSIONS
6920 case glslang::EOpSwizzleInvocations:
6921 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6922 libCall = spv::SwizzleInvocationsAMD;
6923 break;
6924 case glslang::EOpSwizzleInvocationsMasked:
6925 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6926 libCall = spv::SwizzleInvocationsMaskedAMD;
6927 break;
6928 case glslang::EOpWriteInvocation:
6929 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
6930 libCall = spv::WriteInvocationAMD;
6931 break;
6932
6933 case glslang::EOpMin3:
6934 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6935 if (isFloat)
6936 libCall = spv::FMin3AMD;
6937 else {
6938 if (isUnsigned)
6939 libCall = spv::UMin3AMD;
6940 else
6941 libCall = spv::SMin3AMD;
6942 }
6943 break;
6944 case glslang::EOpMax3:
6945 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6946 if (isFloat)
6947 libCall = spv::FMax3AMD;
6948 else {
6949 if (isUnsigned)
6950 libCall = spv::UMax3AMD;
6951 else
6952 libCall = spv::SMax3AMD;
6953 }
6954 break;
6955 case glslang::EOpMid3:
6956 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
6957 if (isFloat)
6958 libCall = spv::FMid3AMD;
6959 else {
6960 if (isUnsigned)
6961 libCall = spv::UMid3AMD;
6962 else
6963 libCall = spv::SMid3AMD;
6964 }
6965 break;
6966
6967 case glslang::EOpInterpolateAtVertex:
Rex Xub4a2a6c2018-05-17 13:51:28 +08006968 if (typeProxy == glslang::EbtFloat16)
6969 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu9d93a232016-05-05 12:30:44 +08006970 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
6971 libCall = spv::InterpolateAtVertexAMD;
6972 break;
6973#endif
Jeff Bolz36831c92018-09-05 10:11:41 -05006974 case glslang::EOpBarrier:
6975 {
6976 // This is for the extended controlBarrier function, with four operands.
6977 // The unextended barrier() goes through createNoArgOperation.
6978 assert(operands.size() == 4);
6979 unsigned int executionScope = builder.getConstantScalar(operands[0]);
6980 unsigned int memoryScope = builder.getConstantScalar(operands[1]);
6981 unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]);
6982 builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
6983 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
6984 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6985 }
6986 if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) {
6987 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6988 }
6989 return 0;
6990 }
6991 break;
6992 case glslang::EOpMemoryBarrier:
6993 {
6994 // This is for the extended memoryBarrier function, with three operands.
6995 // The unextended memoryBarrier() goes through createNoArgOperation.
6996 assert(operands.size() == 3);
6997 unsigned int memoryScope = builder.getConstantScalar(operands[0]);
6998 unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]);
6999 builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
7000 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) {
7001 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7002 }
7003 if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) {
7004 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7005 }
7006 return 0;
7007 }
7008 break;
Chao Chen3c366992018-09-19 11:41:59 -07007009
7010#ifdef NV_EXTENSIONS
Chao Chenb50c02e2018-09-19 11:42:24 -07007011 case glslang::EOpReportIntersectionNV:
7012 {
7013 typeId = builder.makeBoolType();
Ashwin Leleff1783d2018-10-22 16:41:44 -07007014 opCode = spv::OpReportIntersectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -07007015 }
7016 break;
7017 case glslang::EOpTraceNV:
7018 {
Ashwin Leleff1783d2018-10-22 16:41:44 -07007019 builder.createNoResultOp(spv::OpTraceNV, operands);
7020 return 0;
7021 }
7022 break;
7023 case glslang::EOpExecuteCallableNV:
7024 {
7025 builder.createNoResultOp(spv::OpExecuteCallableNV, operands);
Chao Chenb50c02e2018-09-19 11:42:24 -07007026 return 0;
7027 }
7028 break;
Chao Chen3c366992018-09-19 11:41:59 -07007029 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
7030 builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands);
7031 return 0;
7032#endif
John Kessenich140f3df2015-06-26 16:58:36 -06007033 default:
7034 return 0;
7035 }
7036
7037 spv::Id id = 0;
John Kessenich2359bd02015-12-06 19:29:11 -07007038 if (libCall >= 0) {
David Neto8d63a3d2015-12-07 16:17:06 -05007039 // Use an extended instruction from the standard library.
7040 // Construct the call arguments, without modifying the original operands vector.
7041 // We might need the remaining arguments, e.g. in the EOpFrexp case.
7042 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
Rex Xu9d93a232016-05-05 12:30:44 +08007043 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
t.jungb16bea82018-11-15 10:21:36 +01007044 } else if (opCode == spv::OpDot && !isFloat) {
7045 // int dot(int, int)
7046 // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached
7047 const int componentCount = builder.getNumComponents(operands[0]);
7048 spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]);
7049 builder.setPrecision(mulOp, precision);
7050 id = builder.createCompositeExtract(mulOp, typeId, 0);
7051 for (int i = 1; i < componentCount; ++i) {
7052 builder.setPrecision(id, precision);
7053 id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i));
7054 }
John Kessenich2359bd02015-12-06 19:29:11 -07007055 } else {
John Kessenich55e7d112015-11-15 21:33:39 -07007056 switch (consumedOperands) {
John Kessenich140f3df2015-06-26 16:58:36 -06007057 case 0:
7058 // should all be handled by visitAggregate and createNoArgOperation
7059 assert(0);
7060 return 0;
7061 case 1:
7062 // should all be handled by createUnaryOperation
7063 assert(0);
7064 return 0;
7065 case 2:
7066 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
7067 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007068 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007069 // anything 3 or over doesn't have l-value operands, so all should be consumed
7070 assert(consumedOperands == operands.size());
7071 id = builder.createOp(opCode, typeId, operands);
John Kessenich140f3df2015-06-26 16:58:36 -06007072 break;
7073 }
7074 }
7075
John Kessenich55e7d112015-11-15 21:33:39 -07007076 // Decode the return types that were structures
7077 switch (op) {
7078 case glslang::EOpAddCarry:
7079 case glslang::EOpSubBorrow:
7080 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7081 id = builder.createCompositeExtract(id, typeId0, 0);
7082 break;
7083 case glslang::EOpUMulExtended:
7084 case glslang::EOpIMulExtended:
7085 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
7086 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7087 break;
7088 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007089 {
7090 assert(operands.size() == 2);
7091 if (builder.isFloatType(builder.getScalarTypeId(typeId1))) {
7092 // "exp" is floating-point type (from HLSL intrinsic)
7093 spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1);
7094 member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1);
7095 builder.createStore(member1, operands[1]);
7096 } else
7097 // "exp" is integer type (from GLSL built-in function)
7098 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
7099 id = builder.createCompositeExtract(id, typeId0, 0);
7100 }
John Kessenich55e7d112015-11-15 21:33:39 -07007101 break;
7102 default:
7103 break;
7104 }
7105
John Kessenich32cfd492016-02-02 12:37:46 -07007106 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06007107}
7108
Rex Xu9d93a232016-05-05 12:30:44 +08007109// Intrinsics with no arguments (or no return value, and no precision).
7110spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
John Kessenich140f3df2015-06-26 16:58:36 -06007111{
Jeff Bolz36831c92018-09-05 10:11:41 -05007112 // GLSL memory barriers use queuefamily scope in new model, device scope in old model
7113 spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
John Kessenich140f3df2015-06-26 16:58:36 -06007114
7115 switch (op) {
7116 case glslang::EOpEmitVertex:
7117 builder.createNoResultOp(spv::OpEmitVertex);
7118 return 0;
7119 case glslang::EOpEndPrimitive:
7120 builder.createNoResultOp(spv::OpEndPrimitive);
7121 return 0;
7122 case glslang::EOpBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007123 if (glslangIntermediate->getStage() == EShLangTessControl) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007124 if (glslangIntermediate->usingVulkanMemoryModel()) {
7125 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7126 spv::MemorySemanticsOutputMemoryKHRMask |
7127 spv::MemorySemanticsAcquireReleaseMask);
7128 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7129 } else {
7130 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone);
7131 }
John Kessenich82979362017-12-11 04:02:24 -07007132 } else {
7133 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7134 spv::MemorySemanticsWorkgroupMemoryMask |
7135 spv::MemorySemanticsAcquireReleaseMask);
7136 }
John Kessenich140f3df2015-06-26 16:58:36 -06007137 return 0;
7138 case glslang::EOpMemoryBarrier:
Jeff Bolz36831c92018-09-05 10:11:41 -05007139 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory |
7140 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007141 return 0;
7142 case glslang::EOpMemoryBarrierAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05007143 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask |
7144 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007145 return 0;
7146 case glslang::EOpMemoryBarrierBuffer:
Jeff Bolz36831c92018-09-05 10:11:41 -05007147 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask |
7148 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007149 return 0;
7150 case glslang::EOpMemoryBarrierImage:
Jeff Bolz36831c92018-09-05 10:11:41 -05007151 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask |
7152 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007153 return 0;
7154 case glslang::EOpMemoryBarrierShared:
Jeff Bolz36831c92018-09-05 10:11:41 -05007155 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask |
7156 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007157 return 0;
7158 case glslang::EOpGroupMemoryBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007159 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory |
7160 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007161 return 0;
LoopDawg6e72fdd2016-06-15 09:50:24 -06007162 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07007163 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice,
John Kessenich82979362017-12-11 04:02:24 -07007164 spv::MemorySemanticsAllMemory |
John Kessenich838d7af2017-12-12 22:50:53 -07007165 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007166 return 0;
John Kessenich838d7af2017-12-12 22:50:53 -07007167 case glslang::EOpDeviceMemoryBarrier:
7168 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7169 spv::MemorySemanticsImageMemoryMask |
7170 spv::MemorySemanticsAcquireReleaseMask);
7171 return 0;
7172 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
7173 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7174 spv::MemorySemanticsImageMemoryMask |
7175 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007176 return 0;
7177 case glslang::EOpWorkgroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07007178 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7179 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007180 return 0;
7181 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07007182 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7183 spv::MemorySemanticsWorkgroupMemoryMask |
7184 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007185 return 0;
John Kessenich66011cb2018-03-06 16:12:04 -07007186 case glslang::EOpSubgroupBarrier:
7187 builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7188 spv::MemorySemanticsAcquireReleaseMask);
7189 return spv::NoResult;
7190 case glslang::EOpSubgroupMemoryBarrier:
7191 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7192 spv::MemorySemanticsAcquireReleaseMask);
7193 return spv::NoResult;
7194 case glslang::EOpSubgroupMemoryBarrierBuffer:
7195 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask |
7196 spv::MemorySemanticsAcquireReleaseMask);
7197 return spv::NoResult;
7198 case glslang::EOpSubgroupMemoryBarrierImage:
7199 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask |
7200 spv::MemorySemanticsAcquireReleaseMask);
7201 return spv::NoResult;
7202 case glslang::EOpSubgroupMemoryBarrierShared:
7203 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7204 spv::MemorySemanticsAcquireReleaseMask);
7205 return spv::NoResult;
7206 case glslang::EOpSubgroupElect: {
7207 std::vector<spv::Id> operands;
7208 return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid);
7209 }
Rex Xu9d93a232016-05-05 12:30:44 +08007210#ifdef AMD_EXTENSIONS
7211 case glslang::EOpTime:
7212 {
7213 std::vector<spv::Id> args; // Dummy arguments
7214 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
7215 return builder.setPrecision(id, precision);
7216 }
7217#endif
Chao Chenb50c02e2018-09-19 11:42:24 -07007218#ifdef NV_EXTENSIONS
7219 case glslang::EOpIgnoreIntersectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07007220 builder.createNoResultOp(spv::OpIgnoreIntersectionNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07007221 return 0;
7222 case glslang::EOpTerminateRayNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07007223 builder.createNoResultOp(spv::OpTerminateRayNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07007224 return 0;
7225#endif
John Kessenich140f3df2015-06-26 16:58:36 -06007226 default:
Lei Zhang17535f72016-05-04 15:55:59 -04007227 logger->missingFunctionality("unknown operation with no arguments");
John Kessenich140f3df2015-06-26 16:58:36 -06007228 return 0;
7229 }
7230}
7231
7232spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
7233{
John Kessenich2f273362015-07-18 22:34:27 -06007234 auto iter = symbolValues.find(symbol->getId());
John Kessenich140f3df2015-06-26 16:58:36 -06007235 spv::Id id;
7236 if (symbolValues.end() != iter) {
7237 id = iter->second;
7238 return id;
7239 }
7240
7241 // it was not found, create it
7242 id = createSpvVariable(symbol);
7243 symbolValues[symbol->getId()] = id;
7244
Rex Xuc884b4a2016-06-29 15:03:44 +08007245 if (symbol->getBasicType() != glslang::EbtBlock) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007246 builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
7247 builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
7248 builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
Chao Chen3c366992018-09-19 11:41:59 -07007249#ifdef NV_EXTENSIONS
7250 addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier());
7251#endif
John Kessenich6c292d32016-02-15 20:58:50 -07007252 if (symbol->getType().getQualifier().hasSpecConstantId())
John Kessenich5d610ee2018-03-07 18:05:55 -07007253 builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
John Kessenich140f3df2015-06-26 16:58:36 -06007254 if (symbol->getQualifier().hasIndex())
7255 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
7256 if (symbol->getQualifier().hasComponent())
7257 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
John Kessenich91e4aa52016-07-07 17:46:42 -06007258 // atomic counters use this:
7259 if (symbol->getQualifier().hasOffset())
7260 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06007261 }
7262
scygan2c864272016-05-18 18:09:17 +02007263 if (symbol->getQualifier().hasLocation())
7264 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
John Kessenich5d610ee2018-03-07 18:05:55 -07007265 builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
John Kessenichf2d8a5c2016-03-03 22:29:11 -07007266 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
John Kessenich92187592016-02-01 13:45:25 -07007267 builder.addCapability(spv::CapabilityGeometryStreams);
John Kessenich140f3df2015-06-26 16:58:36 -06007268 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
John Kessenich92187592016-02-01 13:45:25 -07007269 }
John Kessenich140f3df2015-06-26 16:58:36 -06007270 if (symbol->getQualifier().hasSet())
7271 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
John Kessenich6c292d32016-02-15 20:58:50 -07007272 else if (IsDescriptorResource(symbol->getType())) {
7273 // default to 0
7274 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
7275 }
John Kessenich140f3df2015-06-26 16:58:36 -06007276 if (symbol->getQualifier().hasBinding())
7277 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
Jeff Bolz0a93cfb2018-12-11 20:53:59 -06007278 else if (IsDescriptorResource(symbol->getType())) {
7279 // default to 0
7280 builder.addDecoration(id, spv::DecorationBinding, 0);
7281 }
John Kessenich6c292d32016-02-15 20:58:50 -07007282 if (symbol->getQualifier().hasAttachment())
7283 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
John Kessenich140f3df2015-06-26 16:58:36 -06007284 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07007285 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenichedaf5562017-12-15 06:21:46 -07007286 if (symbol->getQualifier().hasXfbBuffer()) {
John Kessenich140f3df2015-06-26 16:58:36 -06007287 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
John Kessenichedaf5562017-12-15 06:21:46 -07007288 unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer);
7289 if (stride != glslang::TQualifier::layoutXfbStrideEnd)
7290 builder.addDecoration(id, spv::DecorationXfbStride, stride);
7291 }
7292 if (symbol->getQualifier().hasXfbOffset())
7293 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06007294 }
7295
Rex Xu1da878f2016-02-21 20:59:01 +08007296 if (symbol->getType().isImage()) {
7297 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05007298 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel());
Rex Xu1da878f2016-02-21 20:59:01 +08007299 for (unsigned int i = 0; i < memory.size(); ++i)
John Kessenich5d610ee2018-03-07 18:05:55 -07007300 builder.addDecoration(id, memory[i]);
Rex Xu1da878f2016-02-21 20:59:01 +08007301 }
7302
John Kessenich140f3df2015-06-26 16:58:36 -06007303 // built-in variable decorations
John Kessenichebb50532016-05-16 19:22:05 -06007304 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
John Kessenich4016e382016-07-15 11:53:56 -06007305 if (builtIn != spv::BuiltInMax)
John Kessenich5d610ee2018-03-07 18:05:55 -07007306 builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
John Kessenich140f3df2015-06-26 16:58:36 -06007307
John Kessenich5611c6d2018-04-05 11:25:02 -06007308 // nonuniform
7309 builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier()));
7310
John Kessenichecba76f2017-01-06 00:34:48 -07007311#ifdef NV_EXTENSIONS
chaoc0ad6a4e2016-12-19 16:29:34 -08007312 if (builtIn == spv::BuiltInSampleMask) {
7313 spv::Decoration decoration;
7314 // GL_NV_sample_mask_override_coverage extension
7315 if (glslangIntermediate->getLayoutOverrideCoverage())
chaoc771d89f2017-01-13 01:10:53 -08007316 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
chaoc0ad6a4e2016-12-19 16:29:34 -08007317 else
7318 decoration = (spv::Decoration)spv::DecorationMax;
John Kessenich5d610ee2018-03-07 18:05:55 -07007319 builder.addDecoration(id, decoration);
chaoc0ad6a4e2016-12-19 16:29:34 -08007320 if (decoration != spv::DecorationMax) {
7321 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
7322 }
7323 }
chaoc771d89f2017-01-13 01:10:53 -08007324 else if (builtIn == spv::BuiltInLayer) {
7325 // SPV_NV_viewport_array2 extension
John Kessenichb41bff62017-08-11 13:07:17 -06007326 if (symbol->getQualifier().layoutViewportRelative) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007327 builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
chaoc771d89f2017-01-13 01:10:53 -08007328 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
7329 builder.addExtension(spv::E_SPV_NV_viewport_array2);
7330 }
John Kessenichb41bff62017-08-11 13:07:17 -06007331 if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007332 builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
7333 symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
chaoc771d89f2017-01-13 01:10:53 -08007334 builder.addCapability(spv::CapabilityShaderStereoViewNV);
7335 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
7336 }
7337 }
7338
chaoc6e5acae2016-12-20 13:28:52 -08007339 if (symbol->getQualifier().layoutPassthrough) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007340 builder.addDecoration(id, spv::DecorationPassthroughNV);
chaoc771d89f2017-01-13 01:10:53 -08007341 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
chaoc6e5acae2016-12-20 13:28:52 -08007342 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
7343 }
Chao Chen9eada4b2018-09-19 11:39:56 -07007344 if (symbol->getQualifier().pervertexNV) {
7345 builder.addDecoration(id, spv::DecorationPerVertexNV);
7346 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
7347 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
7348 }
chaoc0ad6a4e2016-12-19 16:29:34 -08007349#endif
7350
John Kessenich5d610ee2018-03-07 18:05:55 -07007351 if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) {
7352 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
7353 builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
7354 symbol->getType().getQualifier().semanticName);
7355 }
7356
Jeff Bolz9f2aec42019-01-06 17:58:04 -06007357 if (symbol->getBasicType() == glslang::EbtReference) {
7358 builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
7359 }
7360
John Kessenich140f3df2015-06-26 16:58:36 -06007361 return id;
7362}
7363
Chao Chen3c366992018-09-19 11:41:59 -07007364#ifdef NV_EXTENSIONS
7365// add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object
7366void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
7367{
7368 if (member >= 0) {
Sahil Parmar38772c02018-10-25 23:50:59 -07007369 if (qualifier.perPrimitiveNV) {
7370 // Need to add capability/extension for fragment shader.
7371 // Mesh shader already adds this by default.
7372 if (glslangIntermediate->getStage() == EShLangFragment) {
7373 builder.addCapability(spv::CapabilityMeshShadingNV);
7374 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7375 }
Chao Chen3c366992018-09-19 11:41:59 -07007376 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07007377 }
Chao Chen3c366992018-09-19 11:41:59 -07007378 if (qualifier.perViewNV)
7379 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV);
7380 if (qualifier.perTaskNV)
7381 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV);
7382 } else {
Sahil Parmar38772c02018-10-25 23:50:59 -07007383 if (qualifier.perPrimitiveNV) {
7384 // Need to add capability/extension for fragment shader.
7385 // Mesh shader already adds this by default.
7386 if (glslangIntermediate->getStage() == EShLangFragment) {
7387 builder.addCapability(spv::CapabilityMeshShadingNV);
7388 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7389 }
Chao Chen3c366992018-09-19 11:41:59 -07007390 builder.addDecoration(id, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07007391 }
Chao Chen3c366992018-09-19 11:41:59 -07007392 if (qualifier.perViewNV)
7393 builder.addDecoration(id, spv::DecorationPerViewNV);
7394 if (qualifier.perTaskNV)
7395 builder.addDecoration(id, spv::DecorationPerTaskNV);
7396 }
7397}
7398#endif
7399
John Kessenich55e7d112015-11-15 21:33:39 -07007400// Make a full tree of instructions to build a SPIR-V specialization constant,
John Kessenich6c292d32016-02-15 20:58:50 -07007401// or regular constant if possible.
John Kessenich55e7d112015-11-15 21:33:39 -07007402//
7403// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
7404//
7405// Recursively walk the nodes. The nodes form a tree whose leaves are
7406// regular constants, which themselves are trees that createSpvConstant()
7407// recursively walks. So, this function walks the "top" of the tree:
7408// - emit specialization constant-building instructions for specConstant
7409// - when running into a non-spec-constant, switch to createSpvConstant()
qining08408382016-03-21 09:51:37 -04007410spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
John Kessenich55e7d112015-11-15 21:33:39 -07007411{
John Kessenich7cc0e282016-03-20 00:46:02 -06007412 assert(node.getQualifier().isConstant());
John Kessenich55e7d112015-11-15 21:33:39 -07007413
qining4f4bb812016-04-03 23:55:17 -04007414 // Handle front-end constants first (non-specialization constants).
John Kessenich6c292d32016-02-15 20:58:50 -07007415 if (! node.getQualifier().specConstant) {
7416 // hand off to the non-spec-constant path
7417 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
7418 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04007419 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
John Kessenich6c292d32016-02-15 20:58:50 -07007420 nextConst, false);
7421 }
7422
7423 // We now know we have a specialization constant to build
7424
John Kessenichd94c0032016-05-30 19:29:40 -06007425 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
qining4f4bb812016-04-03 23:55:17 -04007426 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
7427 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
7428 std::vector<spv::Id> dimConstId;
7429 for (int dim = 0; dim < 3; ++dim) {
7430 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
7431 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
John Kessenich5d610ee2018-03-07 18:05:55 -07007432 if (specConst) {
7433 builder.addDecoration(dimConstId.back(), spv::DecorationSpecId,
7434 glslangIntermediate->getLocalSizeSpecId(dim));
7435 }
qining4f4bb812016-04-03 23:55:17 -04007436 }
7437 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
7438 }
7439
7440 // An AST node labelled as specialization constant should be a symbol node.
7441 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
7442 if (auto* sn = node.getAsSymbolNode()) {
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007443 spv::Id result;
qining4f4bb812016-04-03 23:55:17 -04007444 if (auto* sub_tree = sn->getConstSubtree()) {
qining27e04a02016-04-14 16:40:20 -04007445 // Traverse the constant constructor sub tree like generating normal run-time instructions.
7446 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
7447 // will set the builder into spec constant op instruction generating mode.
7448 sub_tree->traverse(this);
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007449 result = accessChainLoad(sub_tree->getType());
7450 } else if (auto* const_union_array = &sn->getConstArray()) {
qining4f4bb812016-04-03 23:55:17 -04007451 int nextConst = 0;
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007452 result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
Dan Sinclair70661b92018-11-12 13:56:52 -05007453 } else {
7454 logger->missingFunctionality("Invalid initializer for spec onstant.");
Dan Sinclair70661b92018-11-12 13:56:52 -05007455 return spv::NoResult;
John Kessenich6c292d32016-02-15 20:58:50 -07007456 }
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007457 builder.addName(result, sn->getName().c_str());
7458 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07007459 }
qining4f4bb812016-04-03 23:55:17 -04007460
7461 // Neither a front-end constant node, nor a specialization constant node with constant union array or
7462 // constant sub tree as initializer.
Lei Zhang17535f72016-05-04 15:55:59 -04007463 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
qining4f4bb812016-04-03 23:55:17 -04007464 return spv::NoResult;
John Kessenich55e7d112015-11-15 21:33:39 -07007465}
7466
John Kessenich140f3df2015-06-26 16:58:36 -06007467// Use 'consts' as the flattened glslang source of scalar constants to recursively
7468// build the aggregate SPIR-V constant.
7469//
7470// If there are not enough elements present in 'consts', 0 will be substituted;
7471// an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
7472//
qining08408382016-03-21 09:51:37 -04007473spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
John Kessenich140f3df2015-06-26 16:58:36 -06007474{
7475 // vector of constants for SPIR-V
7476 std::vector<spv::Id> spvConsts;
7477
7478 // Type is used for struct and array constants
7479 spv::Id typeId = convertGlslangToSpvType(glslangType);
7480
7481 if (glslangType.isArray()) {
John Kessenich65c78a02015-08-10 17:08:55 -06007482 glslang::TType elementType(glslangType, 0);
7483 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
qining08408382016-03-21 09:51:37 -04007484 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06007485 } else if (glslangType.isMatrix()) {
John Kessenich65c78a02015-08-10 17:08:55 -06007486 glslang::TType vectorType(glslangType, 0);
John Kessenich140f3df2015-06-26 16:58:36 -06007487 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
qining08408382016-03-21 09:51:37 -04007488 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06007489 } else if (glslangType.isStruct()) {
John Kessenich140f3df2015-06-26 16:58:36 -06007490 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
7491 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
qining08408382016-03-21 09:51:37 -04007492 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
John Kessenich8d72f1a2016-05-20 12:06:03 -06007493 } else if (glslangType.getVectorSize() > 1) {
John Kessenich140f3df2015-06-26 16:58:36 -06007494 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
7495 bool zero = nextConst >= consts.size();
7496 switch (glslangType.getBasicType()) {
John Kessenich66011cb2018-03-06 16:12:04 -07007497 case glslang::EbtInt8:
7498 spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const()));
7499 break;
7500 case glslang::EbtUint8:
7501 spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const()));
7502 break;
7503 case glslang::EbtInt16:
7504 spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const()));
7505 break;
7506 case glslang::EbtUint16:
7507 spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const()));
7508 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007509 case glslang::EbtInt:
7510 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
7511 break;
7512 case glslang::EbtUint:
7513 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
7514 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08007515 case glslang::EbtInt64:
7516 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
7517 break;
7518 case glslang::EbtUint64:
7519 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
7520 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007521 case glslang::EbtFloat:
7522 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7523 break;
7524 case glslang::EbtDouble:
7525 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
7526 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08007527 case glslang::EbtFloat16:
7528 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7529 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007530 case glslang::EbtBool:
7531 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
7532 break;
7533 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007534 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06007535 break;
7536 }
7537 ++nextConst;
7538 }
7539 } else {
7540 // we have a non-aggregate (scalar) constant
7541 bool zero = nextConst >= consts.size();
7542 spv::Id scalar = 0;
7543 switch (glslangType.getBasicType()) {
John Kessenich66011cb2018-03-06 16:12:04 -07007544 case glslang::EbtInt8:
7545 scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant);
7546 break;
7547 case glslang::EbtUint8:
7548 scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant);
7549 break;
7550 case glslang::EbtInt16:
7551 scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant);
7552 break;
7553 case glslang::EbtUint16:
7554 scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant);
7555 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007556 case glslang::EbtInt:
John Kessenich55e7d112015-11-15 21:33:39 -07007557 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007558 break;
7559 case glslang::EbtUint:
John Kessenich55e7d112015-11-15 21:33:39 -07007560 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007561 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08007562 case glslang::EbtInt64:
7563 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
7564 break;
7565 case glslang::EbtUint64:
7566 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
7567 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007568 case glslang::EbtFloat:
John Kessenich55e7d112015-11-15 21:33:39 -07007569 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007570 break;
7571 case glslang::EbtDouble:
John Kessenich55e7d112015-11-15 21:33:39 -07007572 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007573 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08007574 case glslang::EbtFloat16:
7575 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
7576 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007577 case glslang::EbtBool:
John Kessenich55e7d112015-11-15 21:33:39 -07007578 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007579 break;
7580 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007581 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06007582 break;
7583 }
7584 ++nextConst;
7585 return scalar;
7586 }
7587
7588 return builder.makeCompositeConstant(typeId, spvConsts);
7589}
7590
John Kessenich7c1aa102015-10-15 13:29:11 -06007591// Return true if the node is a constant or symbol whose reading has no
7592// non-trivial observable cost or effect.
7593bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
7594{
7595 // don't know what this is
7596 if (node == nullptr)
7597 return false;
7598
7599 // a constant is safe
7600 if (node->getAsConstantUnion() != nullptr)
7601 return true;
7602
7603 // not a symbol means non-trivial
7604 if (node->getAsSymbolNode() == nullptr)
7605 return false;
7606
7607 // a symbol, depends on what's being read
7608 switch (node->getType().getQualifier().storage) {
7609 case glslang::EvqTemporary:
7610 case glslang::EvqGlobal:
7611 case glslang::EvqIn:
7612 case glslang::EvqInOut:
7613 case glslang::EvqConst:
7614 case glslang::EvqConstReadOnly:
7615 case glslang::EvqUniform:
7616 return true;
7617 default:
7618 return false;
7619 }
qining25262b32016-05-06 17:25:16 -04007620}
John Kessenich7c1aa102015-10-15 13:29:11 -06007621
7622// A node is trivial if it is a single operation with no side effects.
John Kessenich84cc15f2017-05-24 16:44:47 -06007623// HLSL (and/or vectors) are always trivial, as it does not short circuit.
John Kessenich0d2b4712017-05-19 20:19:00 -06007624// Otherwise, error on the side of saying non-trivial.
John Kessenich7c1aa102015-10-15 13:29:11 -06007625// Return true if trivial.
7626bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
7627{
7628 if (node == nullptr)
7629 return false;
7630
John Kessenich84cc15f2017-05-24 16:44:47 -06007631 // count non scalars as trivial, as well as anything coming from HLSL
7632 if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich0d2b4712017-05-19 20:19:00 -06007633 return true;
7634
John Kessenich7c1aa102015-10-15 13:29:11 -06007635 // symbols and constants are trivial
7636 if (isTrivialLeaf(node))
7637 return true;
7638
7639 // otherwise, it needs to be a simple operation or one or two leaf nodes
7640
7641 // not a simple operation
7642 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
7643 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
7644 if (binaryNode == nullptr && unaryNode == nullptr)
7645 return false;
7646
7647 // not on leaf nodes
7648 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
7649 return false;
7650
7651 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
7652 return false;
7653 }
7654
7655 switch (node->getAsOperator()->getOp()) {
7656 case glslang::EOpLogicalNot:
7657 case glslang::EOpConvIntToBool:
7658 case glslang::EOpConvUintToBool:
7659 case glslang::EOpConvFloatToBool:
7660 case glslang::EOpConvDoubleToBool:
7661 case glslang::EOpEqual:
7662 case glslang::EOpNotEqual:
7663 case glslang::EOpLessThan:
7664 case glslang::EOpGreaterThan:
7665 case glslang::EOpLessThanEqual:
7666 case glslang::EOpGreaterThanEqual:
7667 case glslang::EOpIndexDirect:
7668 case glslang::EOpIndexDirectStruct:
7669 case glslang::EOpLogicalXor:
7670 case glslang::EOpAny:
7671 case glslang::EOpAll:
7672 return true;
7673 default:
7674 return false;
7675 }
7676}
7677
7678// Emit short-circuiting code, where 'right' is never evaluated unless
7679// the left side is true (for &&) or false (for ||).
7680spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right)
7681{
7682 spv::Id boolTypeId = builder.makeBoolType();
7683
7684 // emit left operand
7685 builder.clearAccessChain();
7686 left.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08007687 spv::Id leftId = accessChainLoad(left.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06007688
7689 // Operands to accumulate OpPhi operands
7690 std::vector<spv::Id> phiOperands;
7691 // accumulate left operand's phi information
7692 phiOperands.push_back(leftId);
7693 phiOperands.push_back(builder.getBuildPoint()->getId());
7694
7695 // Make the two kinds of operation symmetric with a "!"
7696 // || => emit "if (! left) result = right"
7697 // && => emit "if ( left) result = right"
7698 //
7699 // TODO: this runtime "not" for || could be avoided by adding functionality
7700 // to 'builder' to have an "else" without an "then"
7701 if (op == glslang::EOpLogicalOr)
7702 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
7703
7704 // make an "if" based on the left value
Rex Xu57e65922017-07-04 23:23:40 +08007705 spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder);
John Kessenich7c1aa102015-10-15 13:29:11 -06007706
7707 // emit right operand as the "then" part of the "if"
7708 builder.clearAccessChain();
7709 right.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08007710 spv::Id rightId = accessChainLoad(right.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06007711
7712 // accumulate left operand's phi information
7713 phiOperands.push_back(rightId);
7714 phiOperands.push_back(builder.getBuildPoint()->getId());
7715
7716 // finish the "if"
7717 ifBuilder.makeEndIf();
7718
7719 // phi together the two results
7720 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
7721}
7722
Frank Henigman541f7bb2018-01-16 00:18:26 -05007723#ifdef AMD_EXTENSIONS
Rex Xu9d93a232016-05-05 12:30:44 +08007724// Return type Id of the imported set of extended instructions corresponds to the name.
7725// Import this set if it has not been imported yet.
7726spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
7727{
7728 if (extBuiltinMap.find(name) != extBuiltinMap.end())
7729 return extBuiltinMap[name];
7730 else {
Rex Xu51596642016-09-21 18:56:12 +08007731 builder.addExtension(name);
Rex Xu9d93a232016-05-05 12:30:44 +08007732 spv::Id extBuiltins = builder.import(name);
7733 extBuiltinMap[name] = extBuiltins;
7734 return extBuiltins;
7735 }
7736}
Frank Henigman541f7bb2018-01-16 00:18:26 -05007737#endif
Rex Xu9d93a232016-05-05 12:30:44 +08007738
John Kessenich140f3df2015-06-26 16:58:36 -06007739}; // end anonymous namespace
7740
7741namespace glslang {
7742
John Kessenich68d78fd2015-07-12 19:28:10 -06007743void GetSpirvVersion(std::string& version)
7744{
John Kessenich9e55f632015-07-15 10:03:39 -06007745 const int bufSize = 100;
John Kessenichf98ee232015-07-12 19:39:51 -06007746 char buf[bufSize];
John Kessenich55e7d112015-11-15 21:33:39 -07007747 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
John Kessenich68d78fd2015-07-12 19:28:10 -06007748 version = buf;
7749}
7750
John Kessenicha372a3e2017-11-02 22:32:14 -06007751// For low-order part of the generator's magic number. Bump up
7752// when there is a change in the style (e.g., if SSA form changes,
7753// or a different instruction sequence to do something gets used).
7754int GetSpirvGeneratorVersion()
7755{
John Kessenich3f0d4bc2017-12-16 23:46:37 -07007756 // return 1; // start
7757 // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V
John Kessenich71b5da62018-02-06 08:06:36 -07007758 // return 3; // change/correct barrier-instruction operands, to match memory model group decisions
John Kessenich0216f242018-03-03 11:47:07 -07007759 // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component
John Kessenichac370792018-03-07 11:24:50 -07007760 // return 5; // make OpArrayLength result type be an int with signedness of 0
John Kessenichd6c97552018-06-04 15:33:31 -06007761 // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code,
7762 // versions 4 and 6 each generate OpArrayLength as it has long been done
7763 return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent
John Kessenicha372a3e2017-11-02 22:32:14 -06007764}
7765
John Kessenich140f3df2015-06-26 16:58:36 -06007766// Write SPIR-V out to a binary file
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05007767void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
John Kessenich140f3df2015-06-26 16:58:36 -06007768{
7769 std::ofstream out;
John Kessenich68d78fd2015-07-12 19:28:10 -06007770 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07007771 if (out.fail())
7772 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenich140f3df2015-06-26 16:58:36 -06007773 for (int i = 0; i < (int)spirv.size(); ++i) {
7774 unsigned int word = spirv[i];
7775 out.write((const char*)&word, 4);
7776 }
7777 out.close();
7778}
7779
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05007780// Write SPIR-V out to a text file with 32-bit hexadecimal words
Flavioaea3c892017-02-06 11:46:35 -08007781void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05007782{
7783 std::ofstream out;
7784 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07007785 if (out.fail())
7786 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenichc6c80a62018-03-05 22:23:17 -07007787 out << "\t// " <<
John Kessenich4e11b612018-08-30 16:56:59 -06007788 GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL <<
John Kessenichc6c80a62018-03-05 22:23:17 -07007789 std::endl;
Flavio15017db2017-02-15 14:29:33 -08007790 if (varName != nullptr) {
7791 out << "\t #pragma once" << std::endl;
7792 out << "const uint32_t " << varName << "[] = {" << std::endl;
7793 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05007794 const int WORDS_PER_LINE = 8;
7795 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
7796 out << "\t";
7797 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
7798 const unsigned int word = spirv[i + j];
7799 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
7800 if (i + j + 1 < (int)spirv.size()) {
7801 out << ",";
7802 }
7803 }
7804 out << std::endl;
7805 }
Flavio15017db2017-02-15 14:29:33 -08007806 if (varName != nullptr) {
7807 out << "};";
7808 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05007809 out.close();
7810}
7811
John Kessenich140f3df2015-06-26 16:58:36 -06007812//
7813// Set up the glslang traversal
7814//
John Kessenich4e11b612018-08-30 16:56:59 -06007815void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
John Kessenich140f3df2015-06-26 16:58:36 -06007816{
Lei Zhang17535f72016-05-04 15:55:59 -04007817 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06007818 GlslangToSpv(intermediate, spirv, &logger, options);
Lei Zhang09caf122016-05-02 18:11:54 -04007819}
7820
John Kessenich4e11b612018-08-30 16:56:59 -06007821void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
John Kessenich121853f2017-05-31 17:11:16 -06007822 spv::SpvBuildLogger* logger, SpvOptions* options)
Lei Zhang09caf122016-05-02 18:11:54 -04007823{
John Kessenich140f3df2015-06-26 16:58:36 -06007824 TIntermNode* root = intermediate.getTreeRoot();
7825
7826 if (root == 0)
7827 return;
7828
John Kessenich4e11b612018-08-30 16:56:59 -06007829 SpvOptions defaultOptions;
John Kessenich121853f2017-05-31 17:11:16 -06007830 if (options == nullptr)
7831 options = &defaultOptions;
7832
John Kessenich4e11b612018-08-30 16:56:59 -06007833 GetThreadPoolAllocator().push();
John Kessenich140f3df2015-06-26 16:58:36 -06007834
John Kessenich2b5ea9f2018-01-31 18:35:56 -07007835 TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
John Kessenich140f3df2015-06-26 16:58:36 -06007836 root->traverse(&it);
John Kessenichfca82622016-11-26 13:23:20 -07007837 it.finishSpv();
John Kessenich140f3df2015-06-26 16:58:36 -06007838 it.dumpSpv(spirv);
7839
GregFfb03a552018-03-29 11:49:14 -06007840#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -06007841 // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
7842 // eg. forward and remove memory writes of opaque types.
John Kessenich717c80a2018-08-23 15:17:10 -06007843 if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer)
John Kesseniche7df8e02018-08-22 17:12:46 -06007844 SpirvToolsLegalize(intermediate, spirv, logger, options);
John Kessenich717c80a2018-08-23 15:17:10 -06007845
John Kessenich4e11b612018-08-30 16:56:59 -06007846 if (options->validate)
7847 SpirvToolsValidate(intermediate, spirv, logger);
7848
John Kessenich717c80a2018-08-23 15:17:10 -06007849 if (options->disassemble)
John Kessenich4e11b612018-08-30 16:56:59 -06007850 SpirvToolsDisassemble(std::cout, spirv);
John Kessenich717c80a2018-08-23 15:17:10 -06007851
GregFcd1f1692017-09-21 18:40:22 -06007852#endif
7853
John Kessenich4e11b612018-08-30 16:56:59 -06007854 GetThreadPoolAllocator().pop();
John Kessenich140f3df2015-06-26 16:58:36 -06007855}
7856
7857}; // end namespace glslang