blob: 1fc642a195ce1d6a4ff52fdf296f3bc04fe8e3e9 [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.
3// Copyright (C) 2015-2016 Google, Inc.
John Kessenich140f3df2015-06-26 16:58:36 -06004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenich140f3df2015-06-26 16:58:36 -060010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenich140f3df2015-06-26 16:58:36 -060035
36//
John Kessenich140f3df2015-06-26 16:58:36 -060037// Visit the nodes in the glslang intermediate tree representation to
38// translate them to SPIR-V.
39//
40
John Kessenich5e4b1242015-08-06 22:53:06 -060041#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060042#include "GlslangToSpv.h"
43#include "SpvBuilder.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060044namespace spv {
Rex Xu51596642016-09-21 18:56:12 +080045 #include "GLSL.std.450.h"
46 #include "GLSL.ext.KHR.h"
Rex Xu9d93a232016-05-05 12:30:44 +080047#ifdef AMD_EXTENSIONS
Rex Xu51596642016-09-21 18:56:12 +080048 #include "GLSL.ext.AMD.h"
Rex Xu9d93a232016-05-05 12:30:44 +080049#endif
chaoc0ad6a4e2016-12-19 16:29:34 -080050#ifdef NV_EXTENSIONS
51 #include "GLSL.ext.NV.h"
52#endif
John Kessenich5e4b1242015-08-06 22:53:06 -060053}
John Kessenich140f3df2015-06-26 16:58:36 -060054
55// Glslang includes
baldurk42169c52015-07-08 15:11:59 +020056#include "../glslang/MachineIndependent/localintermediate.h"
57#include "../glslang/MachineIndependent/SymbolTable.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060058#include "../glslang/Include/Common.h"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050059#include "../glslang/Include/revision.h"
John Kessenich140f3df2015-06-26 16:58:36 -060060
John Kessenich140f3df2015-06-26 16:58:36 -060061#include <fstream>
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050062#include <iomanip>
Lei Zhang17535f72016-05-04 15:55:59 -040063#include <list>
64#include <map>
65#include <stack>
66#include <string>
67#include <vector>
John Kessenich140f3df2015-06-26 16:58:36 -060068
69namespace {
70
John Kessenich55e7d112015-11-15 21:33:39 -070071// For low-order part of the generator's magic number. Bump up
72// when there is a change in the style (e.g., if SSA form changes,
73// or a different instruction sequence to do something gets used).
74const int GeneratorVersion = 1;
John Kessenich140f3df2015-06-26 16:58:36 -060075
qining4c912612016-04-01 10:35:16 -040076namespace {
77class SpecConstantOpModeGuard {
78public:
79 SpecConstantOpModeGuard(spv::Builder* builder)
80 : builder_(builder) {
81 previous_flag_ = builder->isInSpecConstCodeGenMode();
qining4c912612016-04-01 10:35:16 -040082 }
83 ~SpecConstantOpModeGuard() {
84 previous_flag_ ? builder_->setToSpecConstCodeGenMode()
85 : builder_->setToNormalCodeGenMode();
86 }
qining40887662016-04-03 22:20:42 -040087 void turnOnSpecConstantOpMode() {
88 builder_->setToSpecConstCodeGenMode();
89 }
qining4c912612016-04-01 10:35:16 -040090
91private:
92 spv::Builder* builder_;
93 bool previous_flag_;
94};
95}
96
John Kessenich140f3df2015-06-26 16:58:36 -060097//
98// The main holder of information for translating glslang to SPIR-V.
99//
100// Derives from the AST walking base class.
101//
102class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
103public:
Lei Zhang17535f72016-05-04 15:55:59 -0400104 TGlslangToSpvTraverser(const glslang::TIntermediate*, spv::SpvBuildLogger* logger);
John Kessenichfca82622016-11-26 13:23:20 -0700105 virtual ~TGlslangToSpvTraverser() { }
John Kessenich140f3df2015-06-26 16:58:36 -0600106
107 bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*);
108 bool visitBinary(glslang::TVisit, glslang::TIntermBinary*);
109 void visitConstantUnion(glslang::TIntermConstantUnion*);
110 bool visitSelection(glslang::TVisit, glslang::TIntermSelection*);
111 bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*);
112 void visitSymbol(glslang::TIntermSymbol* symbol);
113 bool visitUnary(glslang::TVisit, glslang::TIntermUnary*);
114 bool visitLoop(glslang::TVisit, glslang::TIntermLoop*);
115 bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*);
116
John Kessenichfca82622016-11-26 13:23:20 -0700117 void finishSpv();
John Kessenich7ba63412015-12-20 17:37:07 -0700118 void dumpSpv(std::vector<unsigned int>& out);
John Kessenich140f3df2015-06-26 16:58:36 -0600119
120protected:
Rex Xu17ff3432016-10-14 17:41:45 +0800121 spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier);
Rex Xubbceed72016-05-21 09:40:44 +0800122 spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier);
David Netoa901ffe2016-06-08 14:11:40 +0100123 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
John Kessenich5d0fa972016-02-15 11:57:00 -0700124 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
John Kessenich140f3df2015-06-26 16:58:36 -0600125 spv::Id createSpvVariable(const glslang::TIntermSymbol*);
126 spv::Id getSampledType(const glslang::TSampler&);
John Kessenich8c8505c2016-07-26 12:50:38 -0600127 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
128 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
129 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
John Kessenich140f3df2015-06-26 16:58:36 -0600130 spv::Id convertGlslangToSpvType(const glslang::TType& type);
John Kesseniche0b6cad2015-12-24 10:30:13 -0700131 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&);
John Kessenich6090df02016-06-30 21:18:02 -0600132 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
133 glslang::TLayoutPacking, const glslang::TQualifier&);
134 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
135 const glslang::TQualifier&, spv::Id);
John Kessenich6c292d32016-02-15 20:58:50 -0700136 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
John Kessenich32cfd492016-02-02 12:37:46 -0700137 spv::Id accessChainLoad(const glslang::TType& type);
Rex Xu27253232016-02-23 17:51:09 +0800138 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
John Kessenich4bf71552016-09-02 11:20:21 -0600139 void multiTypeStore(const glslang::TType&, spv::Id rValue);
John Kessenichf85e8062015-12-19 13:57:10 -0700140 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
John Kessenich3ac051e2015-12-20 11:29:16 -0700141 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
142 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
143 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
David Netoa901ffe2016-06-08 14:11:40 +0100144 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
John Kessenich140f3df2015-06-26 16:58:36 -0600145
John Kessenich6fccb3c2016-09-19 16:01:41 -0600146 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600147 void makeFunctions(const glslang::TIntermSequence&);
148 void makeGlobalInitializers(const glslang::TIntermSequence&);
149 void visitFunctions(const glslang::TIntermSequence&);
150 void handleFunctionEntry(const glslang::TIntermAggregate* node);
Rex Xu04db3f52015-09-16 11:44:02 +0800151 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments);
John Kessenichfc51d282015-08-19 13:34:18 -0600152 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
153 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600154 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
155
qining25262b32016-05-06 17:25:16 -0400156 spv::Id createBinaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right, glslang::TBasicType typeProxy, bool reduceComparison = true);
157 spv::Id createBinaryMatrixOperation(spv::Op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right);
158 spv::Id createUnaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand,glslang::TBasicType typeProxy);
Rex Xu2bbbe062016-08-23 15:41:05 +0800159 spv::Id createUnaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand,glslang::TBasicType typeProxy);
Rex Xu73e3ce72016-04-27 18:48:17 +0800160 spv::Id createConversion(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id destTypeId, spv::Id operand, glslang::TBasicType typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -0600161 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
Rex Xu04db3f52015-09-16 11:44:02 +0800162 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 +0800163 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu430ef402016-10-14 17:22:23 +0800164 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands);
John Kessenich5e4b1242015-08-06 22:53:06 -0600165 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 +0800166 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600167 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
168 void addDecoration(spv::Id id, spv::Decoration dec);
John Kessenich55e7d112015-11-15 21:33:39 -0700169 void addDecoration(spv::Id id, spv::Decoration dec, unsigned value);
John Kessenich140f3df2015-06-26 16:58:36 -0600170 void addMemberDecoration(spv::Id id, int member, spv::Decoration dec);
John Kessenich92187592016-02-01 13:45:25 -0700171 void addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value);
qining08408382016-03-21 09:51:37 -0400172 spv::Id createSpvConstant(const glslang::TIntermTyped&);
173 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant);
John Kessenich7c1aa102015-10-15 13:29:11 -0600174 bool isTrivialLeaf(const glslang::TIntermTyped* node);
175 bool isTrivial(const glslang::TIntermTyped* node);
176 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
Rex Xu9d93a232016-05-05 12:30:44 +0800177 spv::Id getExtBuiltins(const char* name);
John Kessenich140f3df2015-06-26 16:58:36 -0600178
179 spv::Function* shaderEntry;
John Kesseniched33e052016-10-06 12:59:51 -0600180 spv::Function* currentFunction;
John Kessenich55e7d112015-11-15 21:33:39 -0700181 spv::Instruction* entryPoint;
John Kessenich140f3df2015-06-26 16:58:36 -0600182 int sequenceDepth;
183
Lei Zhang17535f72016-05-04 15:55:59 -0400184 spv::SpvBuildLogger* logger;
Lei Zhang09caf122016-05-02 18:11:54 -0400185
John Kessenich140f3df2015-06-26 16:58:36 -0600186 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
187 spv::Builder builder;
John Kessenich517fe7a2016-11-26 13:31:47 -0700188 bool inEntryPoint;
189 bool entryPointTerminated;
John Kessenich7ba63412015-12-20 17:37:07 -0700190 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 -0700191 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
John Kessenich140f3df2015-06-26 16:58:36 -0600192 const glslang::TIntermediate* glslangIntermediate;
193 spv::Id stdBuiltins;
Rex Xu9d93a232016-05-05 12:30:44 +0800194 std::unordered_map<const char*, spv::Id> extBuiltinMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600195
John Kessenich2f273362015-07-18 22:34:27 -0600196 std::unordered_map<int, spv::Id> symbolValues;
John Kessenich4bf71552016-09-02 11:20:21 -0600197 std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer
John Kessenich2f273362015-07-18 22:34:27 -0600198 std::unordered_map<std::string, spv::Function*> functionMap;
John Kessenich3ac051e2015-12-20 11:29:16 -0700199 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
John Kessenich2f273362015-07-18 22:34:27 -0600200 std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; // for mapping glslang block indices to spv indices (e.g., due to hidden members)
John Kessenich140f3df2015-06-26 16:58:36 -0600201 std::stack<bool> breakForLoop; // false means break for switch
John Kessenich140f3df2015-06-26 16:58:36 -0600202};
203
204//
205// Helper functions for translating glslang representations to SPIR-V enumerants.
206//
207
208// Translate glslang profile to SPIR-V source language.
John Kessenich66e2faf2016-03-12 18:34:36 -0700209spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
John Kessenich140f3df2015-06-26 16:58:36 -0600210{
John Kessenich66e2faf2016-03-12 18:34:36 -0700211 switch (source) {
212 case glslang::EShSourceGlsl:
213 switch (profile) {
214 case ENoProfile:
215 case ECoreProfile:
216 case ECompatibilityProfile:
217 return spv::SourceLanguageGLSL;
218 case EEsProfile:
219 return spv::SourceLanguageESSL;
220 default:
221 return spv::SourceLanguageUnknown;
222 }
223 case glslang::EShSourceHlsl:
John Kessenich927608b2017-01-06 12:34:14 -0700224 // Use SourceLanguageUnknown instead of SourceLanguageHLSL for now, until Vulkan knows what HLSL is
Dan Baker55d5f2d2016-08-15 16:05:45 -0400225 return spv::SourceLanguageUnknown;
John Kessenich140f3df2015-06-26 16:58:36 -0600226 default:
227 return spv::SourceLanguageUnknown;
228 }
229}
230
231// Translate glslang language (stage) to SPIR-V execution model.
232spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
233{
234 switch (stage) {
235 case EShLangVertex: return spv::ExecutionModelVertex;
236 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
237 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
238 case EShLangGeometry: return spv::ExecutionModelGeometry;
239 case EShLangFragment: return spv::ExecutionModelFragment;
240 case EShLangCompute: return spv::ExecutionModelGLCompute;
241 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700242 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600243 return spv::ExecutionModelFragment;
244 }
245}
246
247// Translate glslang type to SPIR-V storage class.
248spv::StorageClass TranslateStorageClass(const glslang::TType& type)
249{
250 if (type.getQualifier().isPipeInput())
251 return spv::StorageClassInput;
252 else if (type.getQualifier().isPipeOutput())
253 return spv::StorageClassOutput;
Jason Ekstrandc24cc292016-06-08 13:52:36 -0700254 else if (type.getBasicType() == glslang::EbtAtomicUint)
255 return spv::StorageClassAtomicCounter;
John Kessenich4a57dce2017-02-24 19:15:46 -0700256 else if (type.containsOpaque())
257 return spv::StorageClassUniformConstant;
John Kessenich140f3df2015-06-26 16:58:36 -0600258 else if (type.getQualifier().isUniformOrBuffer()) {
John Kessenich6c292d32016-02-15 20:58:50 -0700259 if (type.getQualifier().layoutPushConstant)
260 return spv::StorageClassPushConstant;
John Kessenich140f3df2015-06-26 16:58:36 -0600261 if (type.getBasicType() == glslang::EbtBlock)
262 return spv::StorageClassUniform;
263 else
264 return spv::StorageClassUniformConstant;
John Kessenich140f3df2015-06-26 16:58:36 -0600265 } else {
266 switch (type.getQualifier().storage) {
John Kessenich55e7d112015-11-15 21:33:39 -0700267 case glslang::EvqShared: return spv::StorageClassWorkgroup; break;
268 case glslang::EvqGlobal: return spv::StorageClassPrivate;
John Kessenich140f3df2015-06-26 16:58:36 -0600269 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
270 case glslang::EvqTemporary: return spv::StorageClassFunction;
qining25262b32016-05-06 17:25:16 -0400271 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700272 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600273 return spv::StorageClassFunction;
274 }
275 }
276}
277
278// Translate glslang sampler type to SPIR-V dimensionality.
279spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
280{
281 switch (sampler.dim) {
John Kessenich55e7d112015-11-15 21:33:39 -0700282 case glslang::Esd1D: return spv::Dim1D;
283 case glslang::Esd2D: return spv::Dim2D;
284 case glslang::Esd3D: return spv::Dim3D;
285 case glslang::EsdCube: return spv::DimCube;
286 case glslang::EsdRect: return spv::DimRect;
287 case glslang::EsdBuffer: return spv::DimBuffer;
John Kessenich6c292d32016-02-15 20:58:50 -0700288 case glslang::EsdSubpass: return spv::DimSubpassData;
John Kessenich140f3df2015-06-26 16:58:36 -0600289 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700290 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600291 return spv::Dim2D;
292 }
293}
294
John Kessenichf6640762016-08-01 19:44:00 -0600295// Translate glslang precision to SPIR-V precision decorations.
296spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
John Kessenich140f3df2015-06-26 16:58:36 -0600297{
John Kessenichf6640762016-08-01 19:44:00 -0600298 switch (glslangPrecision) {
John Kessenich61c47a92015-12-14 18:21:19 -0700299 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
John Kessenich5e4b1242015-08-06 22:53:06 -0600300 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
John Kessenich140f3df2015-06-26 16:58:36 -0600301 default:
302 return spv::NoPrecision;
303 }
304}
305
John Kessenichf6640762016-08-01 19:44:00 -0600306// Translate glslang type to SPIR-V precision decorations.
307spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
308{
309 return TranslatePrecisionDecoration(type.getQualifier().precision);
310}
311
John Kessenich140f3df2015-06-26 16:58:36 -0600312// Translate glslang type to SPIR-V block decorations.
313spv::Decoration TranslateBlockDecoration(const glslang::TType& type)
314{
315 if (type.getBasicType() == glslang::EbtBlock) {
316 switch (type.getQualifier().storage) {
317 case glslang::EvqUniform: return spv::DecorationBlock;
318 case glslang::EvqBuffer: return spv::DecorationBufferBlock;
319 case glslang::EvqVaryingIn: return spv::DecorationBlock;
320 case glslang::EvqVaryingOut: return spv::DecorationBlock;
321 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700322 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600323 break;
324 }
325 }
326
John Kessenich4016e382016-07-15 11:53:56 -0600327 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600328}
329
Rex Xu1da878f2016-02-21 20:59:01 +0800330// Translate glslang type to SPIR-V memory decorations.
331void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory)
332{
333 if (qualifier.coherent)
334 memory.push_back(spv::DecorationCoherent);
335 if (qualifier.volatil)
336 memory.push_back(spv::DecorationVolatile);
337 if (qualifier.restrict)
338 memory.push_back(spv::DecorationRestrict);
339 if (qualifier.readonly)
340 memory.push_back(spv::DecorationNonWritable);
341 if (qualifier.writeonly)
342 memory.push_back(spv::DecorationNonReadable);
343}
344
John Kessenich140f3df2015-06-26 16:58:36 -0600345// Translate glslang type to SPIR-V layout decorations.
John Kessenich3ac051e2015-12-20 11:29:16 -0700346spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
John Kessenich140f3df2015-06-26 16:58:36 -0600347{
348 if (type.isMatrix()) {
John Kessenich3ac051e2015-12-20 11:29:16 -0700349 switch (matrixLayout) {
John Kessenich140f3df2015-06-26 16:58:36 -0600350 case glslang::ElmRowMajor:
351 return spv::DecorationRowMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700352 case glslang::ElmColumnMajor:
John Kessenich140f3df2015-06-26 16:58:36 -0600353 return spv::DecorationColMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700354 default:
355 // opaque layouts don't need a majorness
John Kessenich4016e382016-07-15 11:53:56 -0600356 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600357 }
358 } else {
359 switch (type.getBasicType()) {
360 default:
John Kessenich4016e382016-07-15 11:53:56 -0600361 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600362 break;
363 case glslang::EbtBlock:
364 switch (type.getQualifier().storage) {
365 case glslang::EvqUniform:
366 case glslang::EvqBuffer:
367 switch (type.getQualifier().layoutPacking) {
368 case glslang::ElpShared: return spv::DecorationGLSLShared;
John Kessenich140f3df2015-06-26 16:58:36 -0600369 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
370 default:
John Kessenich4016e382016-07-15 11:53:56 -0600371 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600372 }
373 case glslang::EvqVaryingIn:
374 case glslang::EvqVaryingOut:
John Kessenich55e7d112015-11-15 21:33:39 -0700375 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
John Kessenich4016e382016-07-15 11:53:56 -0600376 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600377 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700378 assert(0);
John Kessenich4016e382016-07-15 11:53:56 -0600379 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600380 }
381 }
382 }
383}
384
385// Translate glslang type to SPIR-V interpolation decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600386// Returns spv::DecorationMax when no decoration
John Kessenich55e7d112015-11-15 21:33:39 -0700387// should be applied.
Rex Xu17ff3432016-10-14 17:41:45 +0800388spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600389{
Rex Xubbceed72016-05-21 09:40:44 +0800390 if (qualifier.smooth)
John Kessenich55e7d112015-11-15 21:33:39 -0700391 // Smooth decoration doesn't exist in SPIR-V 1.0
John Kessenich4016e382016-07-15 11:53:56 -0600392 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800393 else if (qualifier.nopersp)
John Kessenich55e7d112015-11-15 21:33:39 -0700394 return spv::DecorationNoPerspective;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700395 else if (qualifier.flat)
John Kessenich140f3df2015-06-26 16:58:36 -0600396 return spv::DecorationFlat;
Rex Xu9d93a232016-05-05 12:30:44 +0800397#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +0800398 else if (qualifier.explicitInterp) {
399 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
Rex Xu9d93a232016-05-05 12:30:44 +0800400 return spv::DecorationExplicitInterpAMD;
Rex Xu17ff3432016-10-14 17:41:45 +0800401 }
Rex Xu9d93a232016-05-05 12:30:44 +0800402#endif
Rex Xubbceed72016-05-21 09:40:44 +0800403 else
John Kessenich4016e382016-07-15 11:53:56 -0600404 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800405}
406
407// Translate glslang type to SPIR-V auxiliary storage decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600408// Returns spv::DecorationMax when no decoration
Rex Xubbceed72016-05-21 09:40:44 +0800409// should be applied.
410spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
411{
412 if (qualifier.patch)
413 return spv::DecorationPatch;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700414 else if (qualifier.centroid)
John Kessenich140f3df2015-06-26 16:58:36 -0600415 return spv::DecorationCentroid;
John Kessenich5e801132016-02-15 11:09:46 -0700416 else if (qualifier.sample) {
417 builder.addCapability(spv::CapabilitySampleRateShading);
John Kessenich140f3df2015-06-26 16:58:36 -0600418 return spv::DecorationSample;
John Kessenich5e801132016-02-15 11:09:46 -0700419 } else
John Kessenich4016e382016-07-15 11:53:56 -0600420 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600421}
422
John Kessenich92187592016-02-01 13:45:25 -0700423// If glslang type is invariant, return SPIR-V invariant decoration.
John Kesseniche0b6cad2015-12-24 10:30:13 -0700424spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600425{
John Kesseniche0b6cad2015-12-24 10:30:13 -0700426 if (qualifier.invariant)
John Kessenich140f3df2015-06-26 16:58:36 -0600427 return spv::DecorationInvariant;
428 else
John Kessenich4016e382016-07-15 11:53:56 -0600429 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600430}
431
qining9220dbb2016-05-04 17:34:38 -0400432// If glslang type is noContraction, return SPIR-V NoContraction decoration.
433spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
434{
435 if (qualifier.noContraction)
436 return spv::DecorationNoContraction;
437 else
John Kessenich4016e382016-07-15 11:53:56 -0600438 return spv::DecorationMax;
qining9220dbb2016-05-04 17:34:38 -0400439}
440
David Netoa901ffe2016-06-08 14:11:40 +0100441// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
442// associated capabilities when required. For some built-in variables, a capability
443// is generated only when using the variable in an executable instruction, but not when
444// just declaring a struct member variable with it. This is true for PointSize,
445// ClipDistance, and CullDistance.
446spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration)
John Kessenich140f3df2015-06-26 16:58:36 -0600447{
448 switch (builtIn) {
John Kessenich92187592016-02-01 13:45:25 -0700449 case glslang::EbvPointSize:
John Kessenich78a45572016-07-08 14:05:15 -0600450 // Defer adding the capability until the built-in is actually used.
451 if (! memberDeclaration) {
452 switch (glslangIntermediate->getStage()) {
453 case EShLangGeometry:
454 builder.addCapability(spv::CapabilityGeometryPointSize);
455 break;
456 case EShLangTessControl:
457 case EShLangTessEvaluation:
458 builder.addCapability(spv::CapabilityTessellationPointSize);
459 break;
460 default:
461 break;
462 }
John Kessenich92187592016-02-01 13:45:25 -0700463 }
464 return spv::BuiltInPointSize;
465
John Kessenichebb50532016-05-16 19:22:05 -0600466 // These *Distance capabilities logically belong here, but if the member is declared and
467 // then never used, consumers of SPIR-V prefer the capability not be declared.
468 // They are now generated when used, rather than here when declared.
469 // Potentially, the specification should be more clear what the minimum
470 // use needed is to trigger the capability.
471 //
John Kessenich92187592016-02-01 13:45:25 -0700472 case glslang::EbvClipDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100473 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800474 builder.addCapability(spv::CapabilityClipDistance);
John Kessenich92187592016-02-01 13:45:25 -0700475 return spv::BuiltInClipDistance;
476
477 case glslang::EbvCullDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100478 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800479 builder.addCapability(spv::CapabilityCullDistance);
John Kessenich92187592016-02-01 13:45:25 -0700480 return spv::BuiltInCullDistance;
481
482 case glslang::EbvViewportIndex:
qining3d7b89a2016-03-07 21:32:15 -0500483 builder.addCapability(spv::CapabilityMultiViewport);
chaoc771d89f2017-01-13 01:10:53 -0800484#ifdef NV_EXTENSIONS
485 if (glslangIntermediate->getStage() == EShLangVertex ||
486 glslangIntermediate->getStage() == EShLangTessControl ||
487 glslangIntermediate->getStage() == EShLangTessEvaluation)
488 {
489 builder.addExtension(spv::E_SPV_NV_viewport_array2);
490 builder.addCapability(spv::CapabilityShaderViewportIndexLayerNV);
491 }
492#endif
John Kessenich92187592016-02-01 13:45:25 -0700493 return spv::BuiltInViewportIndex;
494
John Kessenich5e801132016-02-15 11:09:46 -0700495 case glslang::EbvSampleId:
496 builder.addCapability(spv::CapabilitySampleRateShading);
497 return spv::BuiltInSampleId;
498
499 case glslang::EbvSamplePosition:
500 builder.addCapability(spv::CapabilitySampleRateShading);
501 return spv::BuiltInSamplePosition;
502
503 case glslang::EbvSampleMask:
504 builder.addCapability(spv::CapabilitySampleRateShading);
505 return spv::BuiltInSampleMask;
506
John Kessenich78a45572016-07-08 14:05:15 -0600507 case glslang::EbvLayer:
508 builder.addCapability(spv::CapabilityGeometry);
chaoc771d89f2017-01-13 01:10:53 -0800509#ifdef NV_EXTENSIONS
510 if (!memberDeclaration)
511 {
512 if (glslangIntermediate->getStage() == EShLangVertex ||
513 glslangIntermediate->getStage() == EShLangTessControl ||
514 glslangIntermediate->getStage() == EShLangTessEvaluation)
515 {
516 builder.addExtension(spv::E_SPV_NV_viewport_array2);
517 builder.addCapability(spv::CapabilityShaderViewportIndexLayerNV);
518 }
519 }
520#endif
John Kessenich78a45572016-07-08 14:05:15 -0600521 return spv::BuiltInLayer;
522
John Kessenich140f3df2015-06-26 16:58:36 -0600523 case glslang::EbvPosition: return spv::BuiltInPosition;
John Kessenich140f3df2015-06-26 16:58:36 -0600524 case glslang::EbvVertexId: return spv::BuiltInVertexId;
525 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
John Kessenich6c292d32016-02-15 20:58:50 -0700526 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
527 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
Rex Xuf3b27472016-07-22 18:15:31 +0800528
John Kessenichda581a22015-10-14 14:10:30 -0600529 case glslang::EbvBaseVertex:
Rex Xuf3b27472016-07-22 18:15:31 +0800530 builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters);
531 builder.addCapability(spv::CapabilityDrawParameters);
532 return spv::BuiltInBaseVertex;
533
John Kessenichda581a22015-10-14 14:10:30 -0600534 case glslang::EbvBaseInstance:
Rex Xuf3b27472016-07-22 18:15:31 +0800535 builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters);
536 builder.addCapability(spv::CapabilityDrawParameters);
537 return spv::BuiltInBaseInstance;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200538
John Kessenichda581a22015-10-14 14:10:30 -0600539 case glslang::EbvDrawId:
Rex Xuf3b27472016-07-22 18:15:31 +0800540 builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters);
541 builder.addCapability(spv::CapabilityDrawParameters);
542 return spv::BuiltInDrawIndex;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200543
544 case glslang::EbvPrimitiveId:
545 if (glslangIntermediate->getStage() == EShLangFragment)
546 builder.addCapability(spv::CapabilityGeometry);
547 return spv::BuiltInPrimitiveId;
548
John Kessenich140f3df2015-06-26 16:58:36 -0600549 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
John Kessenich140f3df2015-06-26 16:58:36 -0600550 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
551 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
552 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
553 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
554 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
555 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
556 case glslang::EbvFace: return spv::BuiltInFrontFacing;
John Kessenich140f3df2015-06-26 16:58:36 -0600557 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
558 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
559 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
560 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
561 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
562 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
563 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
564 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
Rex Xu51596642016-09-21 18:56:12 +0800565
Rex Xu574ab042016-04-14 16:53:07 +0800566 case glslang::EbvSubGroupSize:
Rex Xu36876e62016-09-23 22:13:43 +0800567 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800568 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
569 return spv::BuiltInSubgroupSize;
570
Rex Xu574ab042016-04-14 16:53:07 +0800571 case glslang::EbvSubGroupInvocation:
Rex Xu36876e62016-09-23 22:13:43 +0800572 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800573 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
574 return spv::BuiltInSubgroupLocalInvocationId;
575
Rex Xu574ab042016-04-14 16:53:07 +0800576 case glslang::EbvSubGroupEqMask:
Rex Xu51596642016-09-21 18:56:12 +0800577 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
578 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
579 return spv::BuiltInSubgroupEqMaskKHR;
580
Rex Xu574ab042016-04-14 16:53:07 +0800581 case glslang::EbvSubGroupGeMask:
Rex Xu51596642016-09-21 18:56:12 +0800582 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
583 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
584 return spv::BuiltInSubgroupGeMaskKHR;
585
Rex Xu574ab042016-04-14 16:53:07 +0800586 case glslang::EbvSubGroupGtMask:
Rex Xu51596642016-09-21 18:56:12 +0800587 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
588 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
589 return spv::BuiltInSubgroupGtMaskKHR;
590
Rex Xu574ab042016-04-14 16:53:07 +0800591 case glslang::EbvSubGroupLeMask:
Rex Xu51596642016-09-21 18:56:12 +0800592 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
593 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
594 return spv::BuiltInSubgroupLeMaskKHR;
595
Rex Xu574ab042016-04-14 16:53:07 +0800596 case glslang::EbvSubGroupLtMask:
Rex Xu51596642016-09-21 18:56:12 +0800597 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
598 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
599 return spv::BuiltInSubgroupLtMaskKHR;
600
Rex Xu9d93a232016-05-05 12:30:44 +0800601#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +0800602 case glslang::EbvBaryCoordNoPersp:
603 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
604 return spv::BuiltInBaryCoordNoPerspAMD;
605
606 case glslang::EbvBaryCoordNoPerspCentroid:
607 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
608 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
609
610 case glslang::EbvBaryCoordNoPerspSample:
611 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
612 return spv::BuiltInBaryCoordNoPerspSampleAMD;
613
614 case glslang::EbvBaryCoordSmooth:
615 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
616 return spv::BuiltInBaryCoordSmoothAMD;
617
618 case glslang::EbvBaryCoordSmoothCentroid:
619 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
620 return spv::BuiltInBaryCoordSmoothCentroidAMD;
621
622 case glslang::EbvBaryCoordSmoothSample:
623 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
624 return spv::BuiltInBaryCoordSmoothSampleAMD;
625
626 case glslang::EbvBaryCoordPullModel:
627 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
628 return spv::BuiltInBaryCoordPullModelAMD;
Rex Xu9d93a232016-05-05 12:30:44 +0800629#endif
chaoc771d89f2017-01-13 01:10:53 -0800630
631#ifdef NV_EXTENSIONS
632 case glslang::EbvViewportMaskNV:
633 builder.addExtension(spv::E_SPV_NV_viewport_array2);
634 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
635 return spv::BuiltInViewportMaskNV;
636 case glslang::EbvSecondaryPositionNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800637 if (!memberDeclaration) {
638 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
639 builder.addCapability(spv::CapabilityShaderStereoViewNV);
640 }
chaoc771d89f2017-01-13 01:10:53 -0800641 return spv::BuiltInSecondaryPositionNV;
642 case glslang::EbvSecondaryViewportMaskNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800643 if (!memberDeclaration) {
644 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
645 builder.addCapability(spv::CapabilityShaderStereoViewNV);
646 }
chaoc771d89f2017-01-13 01:10:53 -0800647 return spv::BuiltInSecondaryViewportMaskNV;
chaocdf3956c2017-02-14 14:52:34 -0800648 case glslang::EbvPositionPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800649 if (!memberDeclaration) {
650 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
651 builder.addCapability(spv::CapabilityPerViewAttributesNV);
652 }
chaocdf3956c2017-02-14 14:52:34 -0800653 return spv::BuiltInPositionPerViewNV;
654 case glslang::EbvViewportMaskPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800655 if (!memberDeclaration) {
656 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
657 builder.addCapability(spv::CapabilityPerViewAttributesNV);
658 }
chaocdf3956c2017-02-14 14:52:34 -0800659 return spv::BuiltInViewportMaskPerViewNV;
chaoc771d89f2017-01-13 01:10:53 -0800660#endif
Rex Xu3e783f92017-02-22 16:44:48 +0800661 default:
662 return spv::BuiltInMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600663 }
664}
665
Rex Xufc618912015-09-09 16:42:49 +0800666// Translate glslang image layout format to SPIR-V image format.
John Kessenich5d0fa972016-02-15 11:57:00 -0700667spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
Rex Xufc618912015-09-09 16:42:49 +0800668{
669 assert(type.getBasicType() == glslang::EbtSampler);
670
John Kessenich5d0fa972016-02-15 11:57:00 -0700671 // Check for capabilities
672 switch (type.getQualifier().layoutFormat) {
673 case glslang::ElfRg32f:
674 case glslang::ElfRg16f:
675 case glslang::ElfR11fG11fB10f:
676 case glslang::ElfR16f:
677 case glslang::ElfRgba16:
678 case glslang::ElfRgb10A2:
679 case glslang::ElfRg16:
680 case glslang::ElfRg8:
681 case glslang::ElfR16:
682 case glslang::ElfR8:
683 case glslang::ElfRgba16Snorm:
684 case glslang::ElfRg16Snorm:
685 case glslang::ElfRg8Snorm:
686 case glslang::ElfR16Snorm:
687 case glslang::ElfR8Snorm:
688
689 case glslang::ElfRg32i:
690 case glslang::ElfRg16i:
691 case glslang::ElfRg8i:
692 case glslang::ElfR16i:
693 case glslang::ElfR8i:
694
695 case glslang::ElfRgb10a2ui:
696 case glslang::ElfRg32ui:
697 case glslang::ElfRg16ui:
698 case glslang::ElfRg8ui:
699 case glslang::ElfR16ui:
700 case glslang::ElfR8ui:
701 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
702 break;
703
704 default:
705 break;
706 }
707
708 // do the translation
Rex Xufc618912015-09-09 16:42:49 +0800709 switch (type.getQualifier().layoutFormat) {
710 case glslang::ElfNone: return spv::ImageFormatUnknown;
711 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
712 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
713 case glslang::ElfR32f: return spv::ImageFormatR32f;
714 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
715 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
716 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
717 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
718 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
719 case glslang::ElfR16f: return spv::ImageFormatR16f;
720 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
721 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
722 case glslang::ElfRg16: return spv::ImageFormatRg16;
723 case glslang::ElfRg8: return spv::ImageFormatRg8;
724 case glslang::ElfR16: return spv::ImageFormatR16;
725 case glslang::ElfR8: return spv::ImageFormatR8;
726 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
727 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
728 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
729 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
730 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
731 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
732 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
733 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
734 case glslang::ElfR32i: return spv::ImageFormatR32i;
735 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
736 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
737 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
738 case glslang::ElfR16i: return spv::ImageFormatR16i;
739 case glslang::ElfR8i: return spv::ImageFormatR8i;
740 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
741 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
742 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
743 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
744 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
745 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
746 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
747 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
748 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
749 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
John Kessenich4016e382016-07-15 11:53:56 -0600750 default: return spv::ImageFormatMax;
Rex Xufc618912015-09-09 16:42:49 +0800751 }
752}
753
qining25262b32016-05-06 17:25:16 -0400754// Return whether or not the given type is something that should be tied to a
John Kessenich6c292d32016-02-15 20:58:50 -0700755// descriptor set.
756bool IsDescriptorResource(const glslang::TType& type)
757{
John Kessenichf7497e22016-03-08 21:36:22 -0700758 // uniform and buffer blocks are included, unless it is a push_constant
John Kessenich6c292d32016-02-15 20:58:50 -0700759 if (type.getBasicType() == glslang::EbtBlock)
John Kessenichf7497e22016-03-08 21:36:22 -0700760 return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant;
John Kessenich6c292d32016-02-15 20:58:50 -0700761
762 // non block...
763 // basically samplerXXX/subpass/sampler/texture are all included
764 // if they are the global-scope-class, not the function parameter
765 // (or local, if they ever exist) class.
766 if (type.getBasicType() == glslang::EbtSampler)
767 return type.getQualifier().isUniformOrBuffer();
768
769 // None of the above.
770 return false;
771}
772
John Kesseniche0b6cad2015-12-24 10:30:13 -0700773void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
774{
775 if (child.layoutMatrix == glslang::ElmNone)
776 child.layoutMatrix = parent.layoutMatrix;
777
778 if (parent.invariant)
779 child.invariant = true;
780 if (parent.nopersp)
781 child.nopersp = true;
Rex Xu9d93a232016-05-05 12:30:44 +0800782#ifdef AMD_EXTENSIONS
783 if (parent.explicitInterp)
784 child.explicitInterp = true;
785#endif
John Kesseniche0b6cad2015-12-24 10:30:13 -0700786 if (parent.flat)
787 child.flat = true;
788 if (parent.centroid)
789 child.centroid = true;
790 if (parent.patch)
791 child.patch = true;
792 if (parent.sample)
793 child.sample = true;
Rex Xu1da878f2016-02-21 20:59:01 +0800794 if (parent.coherent)
795 child.coherent = true;
796 if (parent.volatil)
797 child.volatil = true;
798 if (parent.restrict)
799 child.restrict = true;
800 if (parent.readonly)
801 child.readonly = true;
802 if (parent.writeonly)
803 child.writeonly = true;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700804}
805
John Kessenichf2b7f332016-09-01 17:05:23 -0600806bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
John Kesseniche0b6cad2015-12-24 10:30:13 -0700807{
John Kessenich7b9fa252016-01-21 18:56:57 -0700808 // This should list qualifiers that simultaneous satisfy:
John Kessenichf2b7f332016-09-01 17:05:23 -0600809 // - struct members might inherit from a struct declaration
810 // (note that non-block structs don't explicitly inherit,
811 // only implicitly, meaning no decoration involved)
812 // - affect decorations on the struct members
813 // (note smooth does not, and expecting something like volatile
814 // to effect the whole object)
John Kesseniche0b6cad2015-12-24 10:30:13 -0700815 // - are not part of the offset/st430/etc or row/column-major layout
John Kessenichf2b7f332016-09-01 17:05:23 -0600816 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
John Kesseniche0b6cad2015-12-24 10:30:13 -0700817}
818
John Kessenich140f3df2015-06-26 16:58:36 -0600819//
820// Implement the TGlslangToSpvTraverser class.
821//
822
Lei Zhang17535f72016-05-04 15:55:59 -0400823TGlslangToSpvTraverser::TGlslangToSpvTraverser(const glslang::TIntermediate* glslangIntermediate, spv::SpvBuildLogger* buildLogger)
John Kesseniched33e052016-10-06 12:59:51 -0600824 : TIntermTraverser(true, false, true), shaderEntry(nullptr), currentFunction(nullptr),
825 sequenceDepth(0), logger(buildLogger),
Lei Zhang17535f72016-05-04 15:55:59 -0400826 builder((glslang::GetKhronosToolId() << 16) | GeneratorVersion, logger),
John Kessenich517fe7a2016-11-26 13:31:47 -0700827 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
John Kessenich140f3df2015-06-26 16:58:36 -0600828 glslangIntermediate(glslangIntermediate)
829{
830 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
831
832 builder.clearAccessChain();
John Kessenich66e2faf2016-03-12 18:34:36 -0700833 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), glslangIntermediate->getVersion());
John Kessenich140f3df2015-06-26 16:58:36 -0600834 stdBuiltins = builder.import("GLSL.std.450");
835 builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450);
John Kessenicheee9d532016-09-19 18:09:30 -0600836 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
837 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
John Kessenich140f3df2015-06-26 16:58:36 -0600838
839 // Add the source extensions
John Kessenich2f273362015-07-18 22:34:27 -0600840 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
841 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
John Kessenich140f3df2015-06-26 16:58:36 -0600842 builder.addSourceExtension(it->c_str());
843
844 // Add the top-level modes for this shader.
845
John Kessenich92187592016-02-01 13:45:25 -0700846 if (glslangIntermediate->getXfbMode()) {
847 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -0600848 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
John Kessenich92187592016-02-01 13:45:25 -0700849 }
John Kessenich140f3df2015-06-26 16:58:36 -0600850
851 unsigned int mode;
852 switch (glslangIntermediate->getStage()) {
853 case EShLangVertex:
John Kessenich5e4b1242015-08-06 22:53:06 -0600854 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -0600855 break;
856
857 case EShLangTessControl:
John Kessenich5e4b1242015-08-06 22:53:06 -0600858 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -0600859 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
860 break;
861
862 case EShLangTessEvaluation:
John Kessenich5e4b1242015-08-06 22:53:06 -0600863 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -0600864 switch (glslangIntermediate->getInputPrimitive()) {
John Kessenich55e7d112015-11-15 21:33:39 -0700865 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
866 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
867 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
John Kessenich4016e382016-07-15 11:53:56 -0600868 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -0600869 }
John Kessenich4016e382016-07-15 11:53:56 -0600870 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -0600871 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
872
John Kesseniche6903322015-10-13 16:29:02 -0600873 switch (glslangIntermediate->getVertexSpacing()) {
874 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
875 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
876 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
John Kessenich4016e382016-07-15 11:53:56 -0600877 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -0600878 }
John Kessenich4016e382016-07-15 11:53:56 -0600879 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -0600880 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
881
882 switch (glslangIntermediate->getVertexOrder()) {
883 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
884 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
John Kessenich4016e382016-07-15 11:53:56 -0600885 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -0600886 }
John Kessenich4016e382016-07-15 11:53:56 -0600887 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -0600888 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
889
890 if (glslangIntermediate->getPointMode())
891 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
John Kessenich140f3df2015-06-26 16:58:36 -0600892 break;
893
894 case EShLangGeometry:
John Kessenich5e4b1242015-08-06 22:53:06 -0600895 builder.addCapability(spv::CapabilityGeometry);
John Kessenich140f3df2015-06-26 16:58:36 -0600896 switch (glslangIntermediate->getInputPrimitive()) {
897 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
898 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
899 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
John Kessenich55e7d112015-11-15 21:33:39 -0700900 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
John Kessenich140f3df2015-06-26 16:58:36 -0600901 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
John Kessenich4016e382016-07-15 11:53:56 -0600902 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -0600903 }
John Kessenich4016e382016-07-15 11:53:56 -0600904 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -0600905 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kesseniche6903322015-10-13 16:29:02 -0600906
John Kessenich140f3df2015-06-26 16:58:36 -0600907 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
908
909 switch (glslangIntermediate->getOutputPrimitive()) {
910 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
911 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
912 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
John Kessenich4016e382016-07-15 11:53:56 -0600913 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -0600914 }
John Kessenich4016e382016-07-15 11:53:56 -0600915 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -0600916 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
917 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
918 break;
919
920 case EShLangFragment:
John Kessenich5e4b1242015-08-06 22:53:06 -0600921 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -0600922 if (glslangIntermediate->getPixelCenterInteger())
923 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
John Kesseniche6903322015-10-13 16:29:02 -0600924
John Kessenich140f3df2015-06-26 16:58:36 -0600925 if (glslangIntermediate->getOriginUpperLeft())
926 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
John Kessenich5e4b1242015-08-06 22:53:06 -0600927 else
928 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
John Kesseniche6903322015-10-13 16:29:02 -0600929
930 if (glslangIntermediate->getEarlyFragmentTests())
931 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
932
933 switch(glslangIntermediate->getDepth()) {
John Kesseniche6903322015-10-13 16:29:02 -0600934 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
935 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
John Kessenich4016e382016-07-15 11:53:56 -0600936 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -0600937 }
John Kessenich4016e382016-07-15 11:53:56 -0600938 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -0600939 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
940
941 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
942 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
John Kessenich140f3df2015-06-26 16:58:36 -0600943 break;
944
945 case EShLangCompute:
John Kessenich5e4b1242015-08-06 22:53:06 -0600946 builder.addCapability(spv::CapabilityShader);
John Kessenichb56a26a2015-09-16 16:04:05 -0600947 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
948 glslangIntermediate->getLocalSize(1),
949 glslangIntermediate->getLocalSize(2));
John Kessenich140f3df2015-06-26 16:58:36 -0600950 break;
951
952 default:
953 break;
954 }
John Kessenich140f3df2015-06-26 16:58:36 -0600955}
956
John Kessenichfca82622016-11-26 13:23:20 -0700957// Finish creating SPV, after the traversal is complete.
958void TGlslangToSpvTraverser::finishSpv()
John Kessenich7ba63412015-12-20 17:37:07 -0700959{
John Kessenich517fe7a2016-11-26 13:31:47 -0700960 if (! entryPointTerminated) {
John Kessenichfca82622016-11-26 13:23:20 -0700961 builder.setBuildPoint(shaderEntry->getLastBlock());
962 builder.leaveFunction();
963 }
964
John Kessenich7ba63412015-12-20 17:37:07 -0700965 // finish off the entry-point SPV instruction by adding the Input/Output <id>
rdb32084e82016-02-23 22:17:38 +0100966 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
967 entryPoint->addIdOperand(*it);
John Kessenich7ba63412015-12-20 17:37:07 -0700968
qiningda397332016-03-09 19:54:03 -0500969 builder.eliminateDeadDecorations();
John Kessenich7ba63412015-12-20 17:37:07 -0700970}
971
John Kessenichfca82622016-11-26 13:23:20 -0700972// Write the SPV into 'out'.
973void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
John Kessenich140f3df2015-06-26 16:58:36 -0600974{
John Kessenichfca82622016-11-26 13:23:20 -0700975 builder.dump(out);
John Kessenich140f3df2015-06-26 16:58:36 -0600976}
977
978//
979// Implement the traversal functions.
980//
981// Return true from interior nodes to have the external traversal
982// continue on to children. Return false if children were
983// already processed.
984//
985
986//
qining25262b32016-05-06 17:25:16 -0400987// Symbols can turn into
John Kessenich140f3df2015-06-26 16:58:36 -0600988// - uniform/input reads
989// - output writes
990// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
991// - something simple that degenerates into the last bullet
992//
993void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
994{
qining75d1d802016-04-06 14:42:01 -0400995 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
996 if (symbol->getType().getQualifier().isSpecConstant())
997 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
998
John Kessenich140f3df2015-06-26 16:58:36 -0600999 // getSymbolId() will set up all the IO decorations on the first call.
1000 // Formal function parameters were mapped during makeFunctions().
1001 spv::Id id = getSymbolId(symbol);
John Kessenich7ba63412015-12-20 17:37:07 -07001002
1003 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
1004 if (builder.isPointer(id)) {
1005 spv::StorageClass sc = builder.getStorageClass(id);
1006 if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)
1007 iOSet.insert(id);
1008 }
1009
1010 // Only process non-linkage-only nodes for generating actual static uses
John Kessenich6c292d32016-02-15 20:58:50 -07001011 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001012 // Prepare to generate code for the access
1013
1014 // L-value chains will be computed left to right. We're on the symbol now,
1015 // which is the left-most part of the access chain, so now is "clear" time,
1016 // followed by setting the base.
1017 builder.clearAccessChain();
1018
1019 // For now, we consider all user variables as being in memory, so they are pointers,
John Kessenich6c292d32016-02-15 20:58:50 -07001020 // except for
John Kessenich4bf71552016-09-02 11:20:21 -06001021 // A) R-Value arguments to a function, which are an intermediate object.
John Kessenich6c292d32016-02-15 20:58:50 -07001022 // See comments in handleUserFunctionCall().
John Kessenich4bf71552016-09-02 11:20:21 -06001023 // B) Specialization constants (normal constants don't even come in as a variable),
John Kessenich6c292d32016-02-15 20:58:50 -07001024 // These are also pure R-values.
1025 glslang::TQualifier qualifier = symbol->getQualifier();
John Kessenich4bf71552016-09-02 11:20:21 -06001026 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end())
John Kessenich140f3df2015-06-26 16:58:36 -06001027 builder.setAccessChainRValue(id);
1028 else
1029 builder.setAccessChainLValue(id);
1030 }
1031}
1032
1033bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1034{
qining40887662016-04-03 22:20:42 -04001035 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1036 if (node->getType().getQualifier().isSpecConstant())
1037 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1038
John Kessenich140f3df2015-06-26 16:58:36 -06001039 // First, handle special cases
1040 switch (node->getOp()) {
1041 case glslang::EOpAssign:
1042 case glslang::EOpAddAssign:
1043 case glslang::EOpSubAssign:
1044 case glslang::EOpMulAssign:
1045 case glslang::EOpVectorTimesMatrixAssign:
1046 case glslang::EOpVectorTimesScalarAssign:
1047 case glslang::EOpMatrixTimesScalarAssign:
1048 case glslang::EOpMatrixTimesMatrixAssign:
1049 case glslang::EOpDivAssign:
1050 case glslang::EOpModAssign:
1051 case glslang::EOpAndAssign:
1052 case glslang::EOpInclusiveOrAssign:
1053 case glslang::EOpExclusiveOrAssign:
1054 case glslang::EOpLeftShiftAssign:
1055 case glslang::EOpRightShiftAssign:
1056 // A bin-op assign "a += b" means the same thing as "a = a + b"
1057 // where a is evaluated before b. For a simple assignment, GLSL
1058 // says to evaluate the left before the right. So, always, left
1059 // node then right node.
1060 {
1061 // get the left l-value, save it away
1062 builder.clearAccessChain();
1063 node->getLeft()->traverse(this);
1064 spv::Builder::AccessChain lValue = builder.getAccessChain();
1065
1066 // evaluate the right
1067 builder.clearAccessChain();
1068 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001069 spv::Id rValue = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001070
1071 if (node->getOp() != glslang::EOpAssign) {
1072 // the left is also an r-value
1073 builder.setAccessChain(lValue);
John Kessenich32cfd492016-02-02 12:37:46 -07001074 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001075
1076 // do the operation
John Kessenichf6640762016-08-01 19:44:00 -06001077 rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()),
qining25262b32016-05-06 17:25:16 -04001078 TranslateNoContractionDecoration(node->getType().getQualifier()),
John Kessenich140f3df2015-06-26 16:58:36 -06001079 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1080 node->getType().getBasicType());
1081
1082 // these all need their counterparts in createBinaryOperation()
John Kessenich55e7d112015-11-15 21:33:39 -07001083 assert(rValue != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001084 }
1085
1086 // store the result
1087 builder.setAccessChain(lValue);
John Kessenich4bf71552016-09-02 11:20:21 -06001088 multiTypeStore(node->getType(), rValue);
John Kessenich140f3df2015-06-26 16:58:36 -06001089
1090 // assignments are expressions having an rValue after they are evaluated...
1091 builder.clearAccessChain();
1092 builder.setAccessChainRValue(rValue);
1093 }
1094 return false;
1095 case glslang::EOpIndexDirect:
1096 case glslang::EOpIndexDirectStruct:
1097 {
1098 // Get the left part of the access chain.
1099 node->getLeft()->traverse(this);
1100
1101 // Add the next element in the chain
1102
David Netoa901ffe2016-06-08 14:11:40 +01001103 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
John Kessenich140f3df2015-06-26 16:58:36 -06001104 if (! node->getLeft()->getType().isArray() &&
1105 node->getLeft()->getType().isVector() &&
1106 node->getOp() == glslang::EOpIndexDirect) {
1107 // This is essentially a hard-coded vector swizzle of size 1,
1108 // so short circuit the access-chain stuff with a swizzle.
1109 std::vector<unsigned> swizzle;
David Netoa901ffe2016-06-08 14:11:40 +01001110 swizzle.push_back(glslangIndex);
John Kessenichfa668da2015-09-13 14:46:30 -06001111 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06001112 } else {
David Netoa901ffe2016-06-08 14:11:40 +01001113 int spvIndex = glslangIndex;
1114 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1115 node->getOp() == glslang::EOpIndexDirectStruct)
1116 {
1117 // This may be, e.g., an anonymous block-member selection, which generally need
1118 // index remapping due to hidden members in anonymous blocks.
1119 std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()];
1120 assert(remapper.size() > 0);
1121 spvIndex = remapper[glslangIndex];
1122 }
John Kessenichebb50532016-05-16 19:22:05 -06001123
David Netoa901ffe2016-06-08 14:11:40 +01001124 // normal case for indexing array or structure or block
1125 builder.accessChainPush(builder.makeIntConstant(spvIndex));
1126
1127 // Add capabilities here for accessing PointSize and clip/cull distance.
1128 // We have deferred generation of associated capabilities until now.
John Kessenichebb50532016-05-16 19:22:05 -06001129 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
David Netoa901ffe2016-06-08 14:11:40 +01001130 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
John Kessenich140f3df2015-06-26 16:58:36 -06001131 }
1132 }
1133 return false;
1134 case glslang::EOpIndexIndirect:
1135 {
1136 // Structure or array or vector indirection.
1137 // Will use native SPIR-V access-chain for struct and array indirection;
1138 // matrices are arrays of vectors, so will also work for a matrix.
1139 // Will use the access chain's 'component' for variable index into a vector.
1140
1141 // This adapter is building access chains left to right.
1142 // Set up the access chain to the left.
1143 node->getLeft()->traverse(this);
1144
1145 // save it so that computing the right side doesn't trash it
1146 spv::Builder::AccessChain partial = builder.getAccessChain();
1147
1148 // compute the next index in the chain
1149 builder.clearAccessChain();
1150 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001151 spv::Id index = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001152
1153 // restore the saved access chain
1154 builder.setAccessChain(partial);
1155
1156 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector())
John Kessenichfa668da2015-09-13 14:46:30 -06001157 builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06001158 else
John Kessenichfa668da2015-09-13 14:46:30 -06001159 builder.accessChainPush(index);
John Kessenich140f3df2015-06-26 16:58:36 -06001160 }
1161 return false;
1162 case glslang::EOpVectorSwizzle:
1163 {
1164 node->getLeft()->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06001165 std::vector<unsigned> swizzle;
John Kessenich8c8505c2016-07-26 12:50:38 -06001166 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
John Kessenichfa668da2015-09-13 14:46:30 -06001167 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06001168 }
1169 return false;
John Kessenichfdf63472017-01-13 12:27:52 -07001170 case glslang::EOpMatrixSwizzle:
1171 logger->missingFunctionality("matrix swizzle");
1172 return true;
John Kessenich7c1aa102015-10-15 13:29:11 -06001173 case glslang::EOpLogicalOr:
1174 case glslang::EOpLogicalAnd:
1175 {
1176
1177 // These may require short circuiting, but can sometimes be done as straight
1178 // binary operations. The right operand must be short circuited if it has
1179 // side effects, and should probably be if it is complex.
1180 if (isTrivial(node->getRight()->getAsTyped()))
1181 break; // handle below as a normal binary operation
1182 // otherwise, we need to do dynamic short circuiting on the right operand
1183 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped());
1184 builder.clearAccessChain();
1185 builder.setAccessChainRValue(result);
1186 }
1187 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06001188 default:
1189 break;
1190 }
1191
1192 // Assume generic binary op...
1193
John Kessenich32cfd492016-02-02 12:37:46 -07001194 // get right operand
John Kessenich140f3df2015-06-26 16:58:36 -06001195 builder.clearAccessChain();
1196 node->getLeft()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001197 spv::Id left = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001198
John Kessenich32cfd492016-02-02 12:37:46 -07001199 // get left operand
John Kessenich140f3df2015-06-26 16:58:36 -06001200 builder.clearAccessChain();
1201 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001202 spv::Id right = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001203
John Kessenich32cfd492016-02-02 12:37:46 -07001204 // get result
John Kessenichf6640762016-08-01 19:44:00 -06001205 spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()),
qining25262b32016-05-06 17:25:16 -04001206 TranslateNoContractionDecoration(node->getType().getQualifier()),
John Kessenich32cfd492016-02-02 12:37:46 -07001207 convertGlslangToSpvType(node->getType()), left, right,
1208 node->getLeft()->getType().getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001209
John Kessenich50e57562015-12-21 21:21:11 -07001210 builder.clearAccessChain();
John Kessenich140f3df2015-06-26 16:58:36 -06001211 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04001212 logger->missingFunctionality("unknown glslang binary operation");
John Kessenich50e57562015-12-21 21:21:11 -07001213 return true; // pick up a child as the place-holder result
John Kessenich140f3df2015-06-26 16:58:36 -06001214 } else {
John Kessenich140f3df2015-06-26 16:58:36 -06001215 builder.setAccessChainRValue(result);
John Kessenich140f3df2015-06-26 16:58:36 -06001216 return false;
1217 }
John Kessenich140f3df2015-06-26 16:58:36 -06001218}
1219
1220bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
1221{
qining40887662016-04-03 22:20:42 -04001222 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1223 if (node->getType().getQualifier().isSpecConstant())
1224 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1225
John Kessenichfc51d282015-08-19 13:34:18 -06001226 spv::Id result = spv::NoResult;
1227
1228 // try texturing first
1229 result = createImageTextureFunctionCall(node);
1230 if (result != spv::NoResult) {
1231 builder.clearAccessChain();
1232 builder.setAccessChainRValue(result);
1233
1234 return false; // done with this node
1235 }
1236
1237 // Non-texturing.
John Kessenichc9a80832015-09-12 12:17:44 -06001238
1239 if (node->getOp() == glslang::EOpArrayLength) {
1240 // Quite special; won't want to evaluate the operand.
1241
1242 // Normal .length() would have been constant folded by the front-end.
1243 // So, this has to be block.lastMember.length().
John Kessenichee21fc92015-09-21 21:50:29 -06001244 // SPV wants "block" and member number as the operands, go get them.
John Kessenichc9a80832015-09-12 12:17:44 -06001245 assert(node->getOperand()->getType().isRuntimeSizedArray());
1246 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
1247 block->traverse(this);
John Kessenichee21fc92015-09-21 21:50:29 -06001248 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst();
1249 spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member);
John Kessenichc9a80832015-09-12 12:17:44 -06001250
1251 builder.clearAccessChain();
1252 builder.setAccessChainRValue(length);
1253
1254 return false;
1255 }
1256
John Kessenichfc51d282015-08-19 13:34:18 -06001257 // Start by evaluating the operand
1258
John Kessenich8c8505c2016-07-26 12:50:38 -06001259 // Does it need a swizzle inversion? If so, evaluation is inverted;
1260 // operate first on the swizzle base, then apply the swizzle.
1261 spv::Id invertedType = spv::NoType;
1262 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
1263 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
1264 invertedType = getInvertedSwizzleType(*node->getOperand());
1265
John Kessenich140f3df2015-06-26 16:58:36 -06001266 builder.clearAccessChain();
John Kessenich8c8505c2016-07-26 12:50:38 -06001267 if (invertedType != spv::NoType)
1268 node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this);
1269 else
1270 node->getOperand()->traverse(this);
Rex Xu30f92582015-09-14 10:38:56 +08001271
Rex Xufc618912015-09-09 16:42:49 +08001272 spv::Id operand = spv::NoResult;
1273
1274 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
1275 node->getOp() == glslang::EOpAtomicCounterDecrement ||
Rex Xu7a26c172015-12-08 17:12:09 +08001276 node->getOp() == glslang::EOpAtomicCounter ||
1277 node->getOp() == glslang::EOpInterpolateAtCentroid)
Rex Xufc618912015-09-09 16:42:49 +08001278 operand = builder.accessChainGetLValue(); // Special case l-value operands
1279 else
John Kessenich32cfd492016-02-02 12:37:46 -07001280 operand = accessChainLoad(node->getOperand()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001281
John Kessenichf6640762016-08-01 19:44:00 -06001282 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
qining25262b32016-05-06 17:25:16 -04001283 spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier());
John Kessenich140f3df2015-06-26 16:58:36 -06001284
1285 // it could be a conversion
John Kessenichfc51d282015-08-19 13:34:18 -06001286 if (! result)
John Kessenich8c8505c2016-07-26 12:50:38 -06001287 result = createConversion(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001288
1289 // if not, then possibly an operation
1290 if (! result)
John Kessenich8c8505c2016-07-26 12:50:38 -06001291 result = createUnaryOperation(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001292
1293 if (result) {
John Kessenich8c8505c2016-07-26 12:50:38 -06001294 if (invertedType)
1295 result = createInvertedSwizzle(precision, *node->getOperand(), result);
1296
John Kessenich140f3df2015-06-26 16:58:36 -06001297 builder.clearAccessChain();
1298 builder.setAccessChainRValue(result);
1299
1300 return false; // done with this node
1301 }
1302
1303 // it must be a special case, check...
1304 switch (node->getOp()) {
1305 case glslang::EOpPostIncrement:
1306 case glslang::EOpPostDecrement:
1307 case glslang::EOpPreIncrement:
1308 case glslang::EOpPreDecrement:
1309 {
1310 // we need the integer value "1" or the floating point "1.0" to add/subtract
Rex Xu8ff43de2016-04-22 16:51:45 +08001311 spv::Id one = 0;
1312 if (node->getBasicType() == glslang::EbtFloat)
1313 one = builder.makeFloatConstant(1.0F);
Rex Xuce31aea2016-07-29 16:13:04 +08001314 else if (node->getBasicType() == glslang::EbtDouble)
1315 one = builder.makeDoubleConstant(1.0);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08001316#ifdef AMD_EXTENSIONS
1317 else if (node->getBasicType() == glslang::EbtFloat16)
1318 one = builder.makeFloat16Constant(1.0F);
1319#endif
Rex Xu8ff43de2016-04-22 16:51:45 +08001320 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
1321 one = builder.makeInt64Constant(1);
1322 else
1323 one = builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06001324 glslang::TOperator op;
1325 if (node->getOp() == glslang::EOpPreIncrement ||
1326 node->getOp() == glslang::EOpPostIncrement)
1327 op = glslang::EOpAdd;
1328 else
1329 op = glslang::EOpSub;
1330
John Kessenichf6640762016-08-01 19:44:00 -06001331 spv::Id result = createBinaryOperation(op, precision,
qining25262b32016-05-06 17:25:16 -04001332 TranslateNoContractionDecoration(node->getType().getQualifier()),
Rex Xu8ff43de2016-04-22 16:51:45 +08001333 convertGlslangToSpvType(node->getType()), operand, one,
1334 node->getType().getBasicType());
John Kessenich55e7d112015-11-15 21:33:39 -07001335 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001336
1337 // The result of operation is always stored, but conditionally the
1338 // consumed result. The consumed result is always an r-value.
1339 builder.accessChainStore(result);
1340 builder.clearAccessChain();
1341 if (node->getOp() == glslang::EOpPreIncrement ||
1342 node->getOp() == glslang::EOpPreDecrement)
1343 builder.setAccessChainRValue(result);
1344 else
1345 builder.setAccessChainRValue(operand);
1346 }
1347
1348 return false;
1349
1350 case glslang::EOpEmitStreamVertex:
1351 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
1352 return false;
1353 case glslang::EOpEndStreamPrimitive:
1354 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
1355 return false;
1356
1357 default:
Lei Zhang17535f72016-05-04 15:55:59 -04001358 logger->missingFunctionality("unknown glslang unary");
John Kessenich50e57562015-12-21 21:21:11 -07001359 return true; // pick up operand as placeholder result
John Kessenich140f3df2015-06-26 16:58:36 -06001360 }
John Kessenich140f3df2015-06-26 16:58:36 -06001361}
1362
1363bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
1364{
qining27e04a02016-04-14 16:40:20 -04001365 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1366 if (node->getType().getQualifier().isSpecConstant())
1367 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1368
John Kessenichfc51d282015-08-19 13:34:18 -06001369 spv::Id result = spv::NoResult;
John Kessenich8c8505c2016-07-26 12:50:38 -06001370 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
1371 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
John Kessenichfc51d282015-08-19 13:34:18 -06001372
1373 // try texturing
1374 result = createImageTextureFunctionCall(node);
1375 if (result != spv::NoResult) {
1376 builder.clearAccessChain();
1377 builder.setAccessChainRValue(result);
1378
1379 return false;
John Kessenich56bab042015-09-16 10:54:31 -06001380 } else if (node->getOp() == glslang::EOpImageStore) {
Rex Xufc618912015-09-09 16:42:49 +08001381 // "imageStore" is a special case, which has no result
1382 return false;
1383 }
John Kessenichfc51d282015-08-19 13:34:18 -06001384
John Kessenich140f3df2015-06-26 16:58:36 -06001385 glslang::TOperator binOp = glslang::EOpNull;
1386 bool reduceComparison = true;
1387 bool isMatrix = false;
1388 bool noReturnValue = false;
John Kessenich426394d2015-07-23 10:22:48 -06001389 bool atomic = false;
John Kessenich140f3df2015-06-26 16:58:36 -06001390
1391 assert(node->getOp());
1392
John Kessenichf6640762016-08-01 19:44:00 -06001393 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenich140f3df2015-06-26 16:58:36 -06001394
1395 switch (node->getOp()) {
1396 case glslang::EOpSequence:
1397 {
1398 if (preVisit)
1399 ++sequenceDepth;
1400 else
1401 --sequenceDepth;
1402
1403 if (sequenceDepth == 1) {
1404 // If this is the parent node of all the functions, we want to see them
1405 // early, so all call points have actual SPIR-V functions to reference.
1406 // In all cases, still let the traverser visit the children for us.
1407 makeFunctions(node->getAsAggregate()->getSequence());
1408
John Kessenich6fccb3c2016-09-19 16:01:41 -06001409 // Also, we want all globals initializers to go into the beginning of the entry point, before
John Kessenich140f3df2015-06-26 16:58:36 -06001410 // anything else gets there, so visit out of order, doing them all now.
1411 makeGlobalInitializers(node->getAsAggregate()->getSequence());
1412
John Kessenich6a60c2f2016-12-08 21:01:59 -07001413 // 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 -06001414 // so do them manually.
1415 visitFunctions(node->getAsAggregate()->getSequence());
1416
1417 return false;
1418 }
1419
1420 return true;
1421 }
1422 case glslang::EOpLinkerObjects:
1423 {
1424 if (visit == glslang::EvPreVisit)
1425 linkageOnly = true;
1426 else
1427 linkageOnly = false;
1428
1429 return true;
1430 }
1431 case glslang::EOpComma:
1432 {
1433 // processing from left to right naturally leaves the right-most
1434 // lying around in the access chain
1435 glslang::TIntermSequence& glslangOperands = node->getSequence();
1436 for (int i = 0; i < (int)glslangOperands.size(); ++i)
1437 glslangOperands[i]->traverse(this);
1438
1439 return false;
1440 }
1441 case glslang::EOpFunction:
1442 if (visit == glslang::EvPreVisit) {
John Kessenich6fccb3c2016-09-19 16:01:41 -06001443 if (isShaderEntryPoint(node)) {
John Kessenich517fe7a2016-11-26 13:31:47 -07001444 inEntryPoint = true;
John Kessenich140f3df2015-06-26 16:58:36 -06001445 builder.setBuildPoint(shaderEntry->getLastBlock());
John Kesseniched33e052016-10-06 12:59:51 -06001446 currentFunction = shaderEntry;
John Kessenich140f3df2015-06-26 16:58:36 -06001447 } else {
1448 handleFunctionEntry(node);
1449 }
1450 } else {
John Kessenich517fe7a2016-11-26 13:31:47 -07001451 if (inEntryPoint)
1452 entryPointTerminated = true;
John Kesseniche770b3e2015-09-14 20:58:02 -06001453 builder.leaveFunction();
John Kessenich517fe7a2016-11-26 13:31:47 -07001454 inEntryPoint = false;
John Kessenich140f3df2015-06-26 16:58:36 -06001455 }
1456
1457 return true;
1458 case glslang::EOpParameters:
1459 // Parameters will have been consumed by EOpFunction processing, but not
1460 // the body, so we still visited the function node's children, making this
1461 // child redundant.
1462 return false;
1463 case glslang::EOpFunctionCall:
1464 {
1465 if (node->isUserDefined())
1466 result = handleUserFunctionCall(node);
John Kessenich927608b2017-01-06 12:34:14 -07001467 // 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 -07001468 if (result) {
1469 builder.clearAccessChain();
1470 builder.setAccessChainRValue(result);
1471 } else
Lei Zhang17535f72016-05-04 15:55:59 -04001472 logger->missingFunctionality("missing user function; linker needs to catch that");
John Kessenich140f3df2015-06-26 16:58:36 -06001473
1474 return false;
1475 }
1476 case glslang::EOpConstructMat2x2:
1477 case glslang::EOpConstructMat2x3:
1478 case glslang::EOpConstructMat2x4:
1479 case glslang::EOpConstructMat3x2:
1480 case glslang::EOpConstructMat3x3:
1481 case glslang::EOpConstructMat3x4:
1482 case glslang::EOpConstructMat4x2:
1483 case glslang::EOpConstructMat4x3:
1484 case glslang::EOpConstructMat4x4:
1485 case glslang::EOpConstructDMat2x2:
1486 case glslang::EOpConstructDMat2x3:
1487 case glslang::EOpConstructDMat2x4:
1488 case glslang::EOpConstructDMat3x2:
1489 case glslang::EOpConstructDMat3x3:
1490 case glslang::EOpConstructDMat3x4:
1491 case glslang::EOpConstructDMat4x2:
1492 case glslang::EOpConstructDMat4x3:
1493 case glslang::EOpConstructDMat4x4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08001494#ifdef AMD_EXTENSIONS
1495 case glslang::EOpConstructF16Mat2x2:
1496 case glslang::EOpConstructF16Mat2x3:
1497 case glslang::EOpConstructF16Mat2x4:
1498 case glslang::EOpConstructF16Mat3x2:
1499 case glslang::EOpConstructF16Mat3x3:
1500 case glslang::EOpConstructF16Mat3x4:
1501 case glslang::EOpConstructF16Mat4x2:
1502 case glslang::EOpConstructF16Mat4x3:
1503 case glslang::EOpConstructF16Mat4x4:
1504#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001505 isMatrix = true;
1506 // fall through
1507 case glslang::EOpConstructFloat:
1508 case glslang::EOpConstructVec2:
1509 case glslang::EOpConstructVec3:
1510 case glslang::EOpConstructVec4:
1511 case glslang::EOpConstructDouble:
1512 case glslang::EOpConstructDVec2:
1513 case glslang::EOpConstructDVec3:
1514 case glslang::EOpConstructDVec4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08001515#ifdef AMD_EXTENSIONS
1516 case glslang::EOpConstructFloat16:
1517 case glslang::EOpConstructF16Vec2:
1518 case glslang::EOpConstructF16Vec3:
1519 case glslang::EOpConstructF16Vec4:
1520#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001521 case glslang::EOpConstructBool:
1522 case glslang::EOpConstructBVec2:
1523 case glslang::EOpConstructBVec3:
1524 case glslang::EOpConstructBVec4:
1525 case glslang::EOpConstructInt:
1526 case glslang::EOpConstructIVec2:
1527 case glslang::EOpConstructIVec3:
1528 case glslang::EOpConstructIVec4:
1529 case glslang::EOpConstructUint:
1530 case glslang::EOpConstructUVec2:
1531 case glslang::EOpConstructUVec3:
1532 case glslang::EOpConstructUVec4:
Rex Xu8ff43de2016-04-22 16:51:45 +08001533 case glslang::EOpConstructInt64:
1534 case glslang::EOpConstructI64Vec2:
1535 case glslang::EOpConstructI64Vec3:
1536 case glslang::EOpConstructI64Vec4:
1537 case glslang::EOpConstructUint64:
1538 case glslang::EOpConstructU64Vec2:
1539 case glslang::EOpConstructU64Vec3:
1540 case glslang::EOpConstructU64Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06001541 case glslang::EOpConstructStruct:
John Kessenich6c292d32016-02-15 20:58:50 -07001542 case glslang::EOpConstructTextureSampler:
John Kessenich140f3df2015-06-26 16:58:36 -06001543 {
1544 std::vector<spv::Id> arguments;
Rex Xufc618912015-09-09 16:42:49 +08001545 translateArguments(*node, arguments);
John Kessenich140f3df2015-06-26 16:58:36 -06001546 spv::Id constructed;
John Kessenich6c292d32016-02-15 20:58:50 -07001547 if (node->getOp() == glslang::EOpConstructTextureSampler)
John Kessenich8c8505c2016-07-26 12:50:38 -06001548 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
John Kessenich6c292d32016-02-15 20:58:50 -07001549 else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001550 std::vector<spv::Id> constituents;
1551 for (int c = 0; c < (int)arguments.size(); ++c)
1552 constituents.push_back(arguments[c]);
John Kessenich8c8505c2016-07-26 12:50:38 -06001553 constructed = builder.createCompositeConstruct(resultType(), constituents);
John Kessenich55e7d112015-11-15 21:33:39 -07001554 } else if (isMatrix)
John Kessenich8c8505c2016-07-26 12:50:38 -06001555 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
John Kessenich55e7d112015-11-15 21:33:39 -07001556 else
John Kessenich8c8505c2016-07-26 12:50:38 -06001557 constructed = builder.createConstructor(precision, arguments, resultType());
John Kessenich140f3df2015-06-26 16:58:36 -06001558
1559 builder.clearAccessChain();
1560 builder.setAccessChainRValue(constructed);
1561
1562 return false;
1563 }
1564
1565 // These six are component-wise compares with component-wise results.
1566 // Forward on to createBinaryOperation(), requesting a vector result.
1567 case glslang::EOpLessThan:
1568 case glslang::EOpGreaterThan:
1569 case glslang::EOpLessThanEqual:
1570 case glslang::EOpGreaterThanEqual:
1571 case glslang::EOpVectorEqual:
1572 case glslang::EOpVectorNotEqual:
1573 {
1574 // Map the operation to a binary
1575 binOp = node->getOp();
1576 reduceComparison = false;
1577 switch (node->getOp()) {
1578 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
1579 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
1580 default: binOp = node->getOp(); break;
1581 }
1582
1583 break;
1584 }
1585 case glslang::EOpMul:
John Kessenich8c8505c2016-07-26 12:50:38 -06001586 // component-wise matrix multiply
John Kessenich140f3df2015-06-26 16:58:36 -06001587 binOp = glslang::EOpMul;
1588 break;
1589 case glslang::EOpOuterProduct:
1590 // two vectors multiplied to make a matrix
1591 binOp = glslang::EOpOuterProduct;
1592 break;
1593 case glslang::EOpDot:
1594 {
qining25262b32016-05-06 17:25:16 -04001595 // for scalar dot product, use multiply
John Kessenich140f3df2015-06-26 16:58:36 -06001596 glslang::TIntermSequence& glslangOperands = node->getSequence();
John Kessenich8d72f1a2016-05-20 12:06:03 -06001597 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
John Kessenich140f3df2015-06-26 16:58:36 -06001598 binOp = glslang::EOpMul;
1599 break;
1600 }
1601 case glslang::EOpMod:
1602 // when an aggregate, this is the floating-point mod built-in function,
1603 // which can be emitted by the one in createBinaryOperation()
1604 binOp = glslang::EOpMod;
1605 break;
John Kessenich140f3df2015-06-26 16:58:36 -06001606 case glslang::EOpEmitVertex:
1607 case glslang::EOpEndPrimitive:
1608 case glslang::EOpBarrier:
1609 case glslang::EOpMemoryBarrier:
1610 case glslang::EOpMemoryBarrierAtomicCounter:
1611 case glslang::EOpMemoryBarrierBuffer:
1612 case glslang::EOpMemoryBarrierImage:
1613 case glslang::EOpMemoryBarrierShared:
1614 case glslang::EOpGroupMemoryBarrier:
LoopDawg6e72fdd2016-06-15 09:50:24 -06001615 case glslang::EOpAllMemoryBarrierWithGroupSync:
1616 case glslang::EOpGroupMemoryBarrierWithGroupSync:
1617 case glslang::EOpWorkgroupMemoryBarrier:
1618 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich140f3df2015-06-26 16:58:36 -06001619 noReturnValue = true;
1620 // These all have 0 operands and will naturally finish up in the code below for 0 operands
1621 break;
1622
John Kessenich426394d2015-07-23 10:22:48 -06001623 case glslang::EOpAtomicAdd:
1624 case glslang::EOpAtomicMin:
1625 case glslang::EOpAtomicMax:
1626 case glslang::EOpAtomicAnd:
1627 case glslang::EOpAtomicOr:
1628 case glslang::EOpAtomicXor:
1629 case glslang::EOpAtomicExchange:
1630 case glslang::EOpAtomicCompSwap:
1631 atomic = true;
1632 break;
1633
John Kessenich140f3df2015-06-26 16:58:36 -06001634 default:
1635 break;
1636 }
1637
1638 //
1639 // See if it maps to a regular operation.
1640 //
John Kessenich140f3df2015-06-26 16:58:36 -06001641 if (binOp != glslang::EOpNull) {
1642 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
1643 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
1644 assert(left && right);
1645
1646 builder.clearAccessChain();
1647 left->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001648 spv::Id leftId = accessChainLoad(left->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001649
1650 builder.clearAccessChain();
1651 right->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001652 spv::Id rightId = accessChainLoad(right->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001653
qining25262b32016-05-06 17:25:16 -04001654 result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()),
John Kessenich8c8505c2016-07-26 12:50:38 -06001655 resultType(), leftId, rightId,
John Kessenich140f3df2015-06-26 16:58:36 -06001656 left->getType().getBasicType(), reduceComparison);
1657
1658 // code above should only make binOp that exists in createBinaryOperation
John Kessenich55e7d112015-11-15 21:33:39 -07001659 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001660 builder.clearAccessChain();
1661 builder.setAccessChainRValue(result);
1662
1663 return false;
1664 }
1665
John Kessenich426394d2015-07-23 10:22:48 -06001666 //
1667 // Create the list of operands.
1668 //
John Kessenich140f3df2015-06-26 16:58:36 -06001669 glslang::TIntermSequence& glslangOperands = node->getSequence();
1670 std::vector<spv::Id> operands;
1671 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
John Kessenich140f3df2015-06-26 16:58:36 -06001672 // special case l-value operands; there are just a few
1673 bool lvalue = false;
1674 switch (node->getOp()) {
John Kessenich55e7d112015-11-15 21:33:39 -07001675 case glslang::EOpFrexp:
John Kessenich140f3df2015-06-26 16:58:36 -06001676 case glslang::EOpModf:
1677 if (arg == 1)
1678 lvalue = true;
1679 break;
Rex Xu7a26c172015-12-08 17:12:09 +08001680 case glslang::EOpInterpolateAtSample:
1681 case glslang::EOpInterpolateAtOffset:
Rex Xu9d93a232016-05-05 12:30:44 +08001682#ifdef AMD_EXTENSIONS
1683 case glslang::EOpInterpolateAtVertex:
1684#endif
John Kessenich8c8505c2016-07-26 12:50:38 -06001685 if (arg == 0) {
Rex Xu7a26c172015-12-08 17:12:09 +08001686 lvalue = true;
John Kessenich8c8505c2016-07-26 12:50:38 -06001687
1688 // Does it need a swizzle inversion? If so, evaluation is inverted;
1689 // operate first on the swizzle base, then apply the swizzle.
John Kessenichecba76f2017-01-06 00:34:48 -07001690 if (glslangOperands[0]->getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06001691 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
1692 invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
1693 }
Rex Xu7a26c172015-12-08 17:12:09 +08001694 break;
Rex Xud4782c12015-09-06 16:30:11 +08001695 case glslang::EOpAtomicAdd:
1696 case glslang::EOpAtomicMin:
1697 case glslang::EOpAtomicMax:
1698 case glslang::EOpAtomicAnd:
1699 case glslang::EOpAtomicOr:
1700 case glslang::EOpAtomicXor:
1701 case glslang::EOpAtomicExchange:
1702 case glslang::EOpAtomicCompSwap:
1703 if (arg == 0)
1704 lvalue = true;
1705 break;
John Kessenich55e7d112015-11-15 21:33:39 -07001706 case glslang::EOpAddCarry:
1707 case glslang::EOpSubBorrow:
1708 if (arg == 2)
1709 lvalue = true;
1710 break;
1711 case glslang::EOpUMulExtended:
1712 case glslang::EOpIMulExtended:
1713 if (arg >= 2)
1714 lvalue = true;
1715 break;
John Kessenich140f3df2015-06-26 16:58:36 -06001716 default:
1717 break;
1718 }
John Kessenich8c8505c2016-07-26 12:50:38 -06001719 builder.clearAccessChain();
1720 if (invertedType != spv::NoType && arg == 0)
1721 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
1722 else
1723 glslangOperands[arg]->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06001724 if (lvalue)
1725 operands.push_back(builder.accessChainGetLValue());
1726 else
John Kessenich32cfd492016-02-02 12:37:46 -07001727 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06001728 }
John Kessenich426394d2015-07-23 10:22:48 -06001729
1730 if (atomic) {
1731 // Handle all atomics
John Kessenich8c8505c2016-07-26 12:50:38 -06001732 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06001733 } else {
1734 // Pass through to generic operations.
1735 switch (glslangOperands.size()) {
1736 case 0:
John Kessenich8c8505c2016-07-26 12:50:38 -06001737 result = createNoArgOperation(node->getOp(), precision, resultType());
John Kessenich426394d2015-07-23 10:22:48 -06001738 break;
1739 case 1:
qining25262b32016-05-06 17:25:16 -04001740 result = createUnaryOperation(
1741 node->getOp(), precision,
1742 TranslateNoContractionDecoration(node->getType().getQualifier()),
John Kessenich8c8505c2016-07-26 12:50:38 -06001743 resultType(), operands.front(),
qining25262b32016-05-06 17:25:16 -04001744 glslangOperands[0]->getAsTyped()->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06001745 break;
1746 default:
John Kessenich8c8505c2016-07-26 12:50:38 -06001747 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06001748 break;
1749 }
John Kessenich8c8505c2016-07-26 12:50:38 -06001750 if (invertedType)
1751 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
John Kessenich140f3df2015-06-26 16:58:36 -06001752 }
1753
1754 if (noReturnValue)
1755 return false;
1756
1757 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04001758 logger->missingFunctionality("unknown glslang aggregate");
John Kessenich50e57562015-12-21 21:21:11 -07001759 return true; // pick up a child as a placeholder operand
John Kessenich140f3df2015-06-26 16:58:36 -06001760 } else {
1761 builder.clearAccessChain();
1762 builder.setAccessChainRValue(result);
1763 return false;
1764 }
1765}
1766
John Kessenich433e9ff2017-01-26 20:31:11 -07001767// This path handles both if-then-else and ?:
1768// The if-then-else has a node type of void, while
1769// ?: has either a void or a non-void node type
1770//
1771// Leaving the result, when not void:
1772// GLSL only has r-values as the result of a :?, but
1773// if we have an l-value, that can be more efficient if it will
1774// become the base of a complex r-value expression, because the
1775// next layer copies r-values into memory to use the access-chain mechanism
John Kessenich140f3df2015-06-26 16:58:36 -06001776bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
1777{
John Kessenich433e9ff2017-01-26 20:31:11 -07001778 // See if it simple and safe to generate OpSelect instead of using control flow.
1779 // Crucially, side effects must be avoided, and there are performance trade-offs.
1780 // Return true if good idea (and safe) for OpSelect, false otherwise.
1781 const auto selectPolicy = [&]() -> bool {
1782 if (node->getBasicType() == glslang::EbtVoid)
1783 return false;
1784
1785 if (node->getTrueBlock() == nullptr ||
1786 node->getFalseBlock() == nullptr)
1787 return false;
1788
1789 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
1790 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
1791
1792 // return true if a single operand to ? : is okay for OpSelect
1793 const auto operandOkay = [](glslang::TIntermTyped* node) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07001794 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
John Kessenich433e9ff2017-01-26 20:31:11 -07001795 };
1796
1797 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
1798 operandOkay(node->getFalseBlock()->getAsTyped());
1799 };
1800
1801 // Emit OpSelect for this selection.
1802 const auto handleAsOpSelect = [&]() {
1803 node->getCondition()->traverse(this);
1804 spv::Id condition = accessChainLoad(node->getCondition()->getType());
1805 node->getTrueBlock()->traverse(this);
1806 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
1807 node->getFalseBlock()->traverse(this);
1808 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
1809
1810 spv::Id select = builder.createTriOp(spv::OpSelect, convertGlslangToSpvType(node->getType()), condition, trueValue, falseValue);
1811 builder.clearAccessChain();
1812 builder.setAccessChainRValue(select);
1813 };
1814
1815 // Try for OpSelect
1816
1817 if (selectPolicy()) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07001818 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1819 if (node->getType().getQualifier().isSpecConstant())
1820 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1821
John Kessenich433e9ff2017-01-26 20:31:11 -07001822 handleAsOpSelect();
1823 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06001824 }
1825
John Kessenich433e9ff2017-01-26 20:31:11 -07001826 // Instead, emit control flow...
1827
1828 // Don't handle results as temporaries, because there will be two names
1829 // and better to leave SSA to later passes.
1830 spv::Id result = (node->getBasicType() == glslang::EbtVoid)
1831 ? spv::NoResult
1832 : builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
1833
John Kessenich140f3df2015-06-26 16:58:36 -06001834 // emit the condition before doing anything with selection
1835 node->getCondition()->traverse(this);
1836
1837 // make an "if" based on the value created by the condition
John Kessenich32cfd492016-02-02 12:37:46 -07001838 spv::Builder::If ifBuilder(accessChainLoad(node->getCondition()->getType()), builder);
John Kessenich140f3df2015-06-26 16:58:36 -06001839
John Kessenich433e9ff2017-01-26 20:31:11 -07001840 // emit the "then" statement
1841 if (node->getTrueBlock() != nullptr) {
John Kessenich140f3df2015-06-26 16:58:36 -06001842 node->getTrueBlock()->traverse(this);
John Kessenich433e9ff2017-01-26 20:31:11 -07001843 if (result != spv::NoResult)
1844 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
John Kessenich140f3df2015-06-26 16:58:36 -06001845 }
1846
John Kessenich433e9ff2017-01-26 20:31:11 -07001847 if (node->getFalseBlock() != nullptr) {
John Kessenich140f3df2015-06-26 16:58:36 -06001848 ifBuilder.makeBeginElse();
1849 // emit the "else" statement
1850 node->getFalseBlock()->traverse(this);
John Kessenich433e9ff2017-01-26 20:31:11 -07001851 if (result != spv::NoResult)
John Kessenich32cfd492016-02-02 12:37:46 -07001852 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
John Kessenich140f3df2015-06-26 16:58:36 -06001853 }
1854
John Kessenich433e9ff2017-01-26 20:31:11 -07001855 // finish off the control flow
John Kessenich140f3df2015-06-26 16:58:36 -06001856 ifBuilder.makeEndIf();
1857
John Kessenich433e9ff2017-01-26 20:31:11 -07001858 if (result != spv::NoResult) {
John Kessenich140f3df2015-06-26 16:58:36 -06001859 // GLSL only has r-values as the result of a :?, but
1860 // if we have an l-value, that can be more efficient if it will
1861 // become the base of a complex r-value expression, because the
1862 // next layer copies r-values into memory to use the access-chain mechanism
1863 builder.clearAccessChain();
1864 builder.setAccessChainLValue(result);
1865 }
1866
1867 return false;
1868}
1869
1870bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
1871{
1872 // emit and get the condition before doing anything with switch
1873 node->getCondition()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001874 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001875
1876 // browse the children to sort out code segments
1877 int defaultSegment = -1;
1878 std::vector<TIntermNode*> codeSegments;
1879 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
1880 std::vector<int> caseValues;
1881 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
1882 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
1883 TIntermNode* child = *c;
1884 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
baldurkd76692d2015-07-12 11:32:58 +02001885 defaultSegment = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06001886 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
baldurkd76692d2015-07-12 11:32:58 +02001887 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06001888 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst());
1889 } else
1890 codeSegments.push_back(child);
1891 }
1892
qining25262b32016-05-06 17:25:16 -04001893 // handle the case where the last code segment is missing, due to no code
John Kessenich140f3df2015-06-26 16:58:36 -06001894 // statements between the last case and the end of the switch statement
1895 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
1896 (int)codeSegments.size() == defaultSegment)
1897 codeSegments.push_back(nullptr);
1898
1899 // make the switch statement
1900 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
baldurkd76692d2015-07-12 11:32:58 +02001901 builder.makeSwitch(selector, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks);
John Kessenich140f3df2015-06-26 16:58:36 -06001902
1903 // emit all the code in the segments
1904 breakForLoop.push(false);
1905 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
1906 builder.nextSwitchSegment(segmentBlocks, s);
1907 if (codeSegments[s])
1908 codeSegments[s]->traverse(this);
1909 else
1910 builder.addSwitchBreak();
1911 }
1912 breakForLoop.pop();
1913
1914 builder.endSwitch(segmentBlocks);
1915
1916 return false;
1917}
1918
1919void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
1920{
1921 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04001922 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
John Kessenich140f3df2015-06-26 16:58:36 -06001923
1924 builder.clearAccessChain();
1925 builder.setAccessChainRValue(constant);
1926}
1927
1928bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
1929{
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001930 auto blocks = builder.makeNewLoop();
Dejan Mircevski832c65c2016-01-11 15:57:11 -05001931 builder.createBranch(&blocks.head);
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05001932 // Spec requires back edges to target header blocks, and every header block
1933 // must dominate its merge block. Make a header block first to ensure these
1934 // conditions are met. By definition, it will contain OpLoopMerge, followed
1935 // by a block-ending branch. But we don't want to put any other body/test
1936 // instructions in it, since the body/test may have arbitrary instructions,
1937 // including merges of its own.
1938 builder.setBuildPoint(&blocks.head);
1939 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, spv::LoopControlMaskNone);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001940 if (node->testFirst() && node->getTest()) {
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05001941 spv::Block& test = builder.makeNewBlock();
1942 builder.createBranch(&test);
1943
1944 builder.setBuildPoint(&test);
John Kessenich140f3df2015-06-26 16:58:36 -06001945 node->getTest()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001946 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07001947 accessChainLoad(node->getTest()->getType());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001948 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
1949
1950 builder.setBuildPoint(&blocks.body);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05001951 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001952 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05001953 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001954 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05001955 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001956
1957 builder.setBuildPoint(&blocks.continue_target);
1958 if (node->getTerminal())
1959 node->getTerminal()->traverse(this);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05001960 builder.createBranch(&blocks.head);
David Netoc22f37c2015-07-15 16:21:26 -04001961 } else {
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001962 builder.createBranch(&blocks.body);
1963
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05001964 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001965 builder.setBuildPoint(&blocks.body);
1966 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05001967 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001968 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05001969 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001970
1971 builder.setBuildPoint(&blocks.continue_target);
1972 if (node->getTerminal())
1973 node->getTerminal()->traverse(this);
1974 if (node->getTest()) {
1975 node->getTest()->traverse(this);
1976 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07001977 accessChainLoad(node->getTest()->getType());
Dejan Mircevski832c65c2016-01-11 15:57:11 -05001978 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001979 } else {
Dejan Mircevskied55bcd2016-01-19 21:13:38 -05001980 // TODO: unless there was a break/return/discard instruction
1981 // somewhere in the body, this is an infinite loop, so we should
1982 // issue a warning.
Dejan Mircevski832c65c2016-01-11 15:57:11 -05001983 builder.createBranch(&blocks.head);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001984 }
John Kessenich140f3df2015-06-26 16:58:36 -06001985 }
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05001986 builder.setBuildPoint(&blocks.merge);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05001987 builder.closeLoop();
John Kessenich140f3df2015-06-26 16:58:36 -06001988 return false;
1989}
1990
1991bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
1992{
1993 if (node->getExpression())
1994 node->getExpression()->traverse(this);
1995
1996 switch (node->getFlowOp()) {
1997 case glslang::EOpKill:
1998 builder.makeDiscard();
1999 break;
2000 case glslang::EOpBreak:
2001 if (breakForLoop.top())
2002 builder.createLoopExit();
2003 else
2004 builder.addSwitchBreak();
2005 break;
2006 case glslang::EOpContinue:
John Kessenich140f3df2015-06-26 16:58:36 -06002007 builder.createLoopContinue();
2008 break;
2009 case glslang::EOpReturn:
John Kesseniched33e052016-10-06 12:59:51 -06002010 if (node->getExpression()) {
2011 const glslang::TType& glslangReturnType = node->getExpression()->getType();
2012 spv::Id returnId = accessChainLoad(glslangReturnType);
2013 if (builder.getTypeId(returnId) != currentFunction->getReturnType()) {
2014 builder.clearAccessChain();
2015 spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType());
2016 builder.setAccessChainLValue(copyId);
2017 multiTypeStore(glslangReturnType, returnId);
2018 returnId = builder.createLoad(copyId);
2019 }
2020 builder.makeReturn(false, returnId);
2021 } else
John Kesseniche770b3e2015-09-14 20:58:02 -06002022 builder.makeReturn(false);
John Kessenich140f3df2015-06-26 16:58:36 -06002023
2024 builder.clearAccessChain();
2025 break;
2026
2027 default:
John Kessenich55e7d112015-11-15 21:33:39 -07002028 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06002029 break;
2030 }
2031
2032 return false;
2033}
2034
2035spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node)
2036{
qining25262b32016-05-06 17:25:16 -04002037 // First, steer off constants, which are not SPIR-V variables, but
John Kessenich140f3df2015-06-26 16:58:36 -06002038 // can still have a mapping to a SPIR-V Id.
John Kessenich55e7d112015-11-15 21:33:39 -07002039 // This includes specialization constants.
John Kessenich7cc0e282016-03-20 00:46:02 -06002040 if (node->getQualifier().isConstant()) {
qining08408382016-03-21 09:51:37 -04002041 return createSpvConstant(*node);
John Kessenich140f3df2015-06-26 16:58:36 -06002042 }
2043
2044 // Now, handle actual variables
2045 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
2046 spv::Id spvType = convertGlslangToSpvType(node->getType());
2047
2048 const char* name = node->getName().c_str();
2049 if (glslang::IsAnonymous(name))
2050 name = "";
2051
2052 return builder.createVariable(storageClass, spvType, name);
2053}
2054
2055// Return type Id of the sampled type.
2056spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
2057{
2058 switch (sampler.type) {
2059 case glslang::EbtFloat: return builder.makeFloatType(32);
2060 case glslang::EbtInt: return builder.makeIntType(32);
2061 case glslang::EbtUint: return builder.makeUintType(32);
2062 default:
John Kessenich55e7d112015-11-15 21:33:39 -07002063 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06002064 return builder.makeFloatType(32);
2065 }
2066}
2067
John Kessenich8c8505c2016-07-26 12:50:38 -06002068// If node is a swizzle operation, return the type that should be used if
2069// the swizzle base is first consumed by another operation, before the swizzle
2070// is applied.
2071spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
2072{
John Kessenichecba76f2017-01-06 00:34:48 -07002073 if (node.getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002074 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2075 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
2076 else
2077 return spv::NoType;
2078}
2079
2080// When inverting a swizzle with a parent op, this function
2081// will apply the swizzle operation to a completed parent operation.
2082spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult)
2083{
2084 std::vector<unsigned> swizzle;
2085 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
2086 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
2087}
2088
John Kessenich8c8505c2016-07-26 12:50:38 -06002089// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
2090void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
2091{
2092 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
2093 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
2094 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
2095}
2096
John Kessenich3ac051e2015-12-20 11:29:16 -07002097// Convert from a glslang type to an SPV type, by calling into a
2098// recursive version of this function. This establishes the inherited
2099// layout state rooted from the top-level type.
John Kessenich140f3df2015-06-26 16:58:36 -06002100spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type)
2101{
John Kesseniche0b6cad2015-12-24 10:30:13 -07002102 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier());
John Kessenich31ed4832015-09-09 17:51:38 -06002103}
2104
2105// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
John Kessenich7b9fa252016-01-21 18:56:57 -07002106// explicitLayout can be kept the same throughout the hierarchical recursive walk.
John Kessenich6090df02016-06-30 21:18:02 -06002107// Mutually recursive with convertGlslangStructToSpvType().
John Kesseniche0b6cad2015-12-24 10:30:13 -07002108spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier)
John Kessenich31ed4832015-09-09 17:51:38 -06002109{
John Kesseniche0b6cad2015-12-24 10:30:13 -07002110 spv::Id spvType = spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06002111
2112 switch (type.getBasicType()) {
2113 case glslang::EbtVoid:
2114 spvType = builder.makeVoidType();
John Kessenich55e7d112015-11-15 21:33:39 -07002115 assert (! type.isArray());
John Kessenich140f3df2015-06-26 16:58:36 -06002116 break;
2117 case glslang::EbtFloat:
2118 spvType = builder.makeFloatType(32);
2119 break;
2120 case glslang::EbtDouble:
2121 spvType = builder.makeFloatType(64);
2122 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002123#ifdef AMD_EXTENSIONS
2124 case glslang::EbtFloat16:
2125 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002126 spvType = builder.makeFloatType(16);
2127 break;
2128#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002129 case glslang::EbtBool:
John Kessenich103bef92016-02-08 21:38:15 -07002130 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
2131 // a 32-bit int where non-0 means true.
2132 if (explicitLayout != glslang::ElpNone)
2133 spvType = builder.makeUintType(32);
2134 else
2135 spvType = builder.makeBoolType();
John Kessenich140f3df2015-06-26 16:58:36 -06002136 break;
2137 case glslang::EbtInt:
2138 spvType = builder.makeIntType(32);
2139 break;
2140 case glslang::EbtUint:
2141 spvType = builder.makeUintType(32);
2142 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08002143 case glslang::EbtInt64:
2144 builder.addCapability(spv::CapabilityInt64);
2145 spvType = builder.makeIntType(64);
2146 break;
2147 case glslang::EbtUint64:
2148 builder.addCapability(spv::CapabilityInt64);
2149 spvType = builder.makeUintType(64);
2150 break;
John Kessenich426394d2015-07-23 10:22:48 -06002151 case glslang::EbtAtomicUint:
John Kessenich2d0cc782016-07-07 13:20:00 -06002152 builder.addCapability(spv::CapabilityAtomicStorage);
John Kessenich426394d2015-07-23 10:22:48 -06002153 spvType = builder.makeUintType(32);
2154 break;
John Kessenich140f3df2015-06-26 16:58:36 -06002155 case glslang::EbtSampler:
2156 {
2157 const glslang::TSampler& sampler = type.getSampler();
John Kessenich6c292d32016-02-15 20:58:50 -07002158 if (sampler.sampler) {
2159 // pure sampler
2160 spvType = builder.makeSamplerType();
2161 } else {
2162 // an image is present, make its type
2163 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms,
2164 sampler.image ? 2 : 1, TranslateImageFormat(type));
2165 if (sampler.combined) {
2166 // already has both image and sampler, make the combined type
2167 spvType = builder.makeSampledImageType(spvType);
2168 }
John Kessenich55e7d112015-11-15 21:33:39 -07002169 }
John Kesseniche0b6cad2015-12-24 10:30:13 -07002170 }
John Kessenich140f3df2015-06-26 16:58:36 -06002171 break;
2172 case glslang::EbtStruct:
2173 case glslang::EbtBlock:
2174 {
2175 // If we've seen this struct type, return it
John Kessenich6090df02016-06-30 21:18:02 -06002176 const glslang::TTypeList* glslangMembers = type.getStruct();
John Kesseniche0b6cad2015-12-24 10:30:13 -07002177
2178 // Try to share structs for different layouts, but not yet for other
2179 // kinds of qualification (primarily not yet including interpolant qualification).
John Kessenichf2b7f332016-09-01 17:05:23 -06002180 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06002181 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
John Kesseniche0b6cad2015-12-24 10:30:13 -07002182 if (spvType != spv::NoResult)
John Kessenich140f3df2015-06-26 16:58:36 -06002183 break;
2184
2185 // else, we haven't seen it...
John Kessenich140f3df2015-06-26 16:58:36 -06002186 if (type.getBasicType() == glslang::EbtBlock)
John Kessenich6090df02016-06-30 21:18:02 -06002187 memberRemapper[glslangMembers].resize(glslangMembers->size());
2188 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
John Kessenich140f3df2015-06-26 16:58:36 -06002189 }
2190 break;
2191 default:
John Kessenich55e7d112015-11-15 21:33:39 -07002192 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06002193 break;
2194 }
2195
2196 if (type.isMatrix())
2197 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
2198 else {
2199 // If this variable has a vector element count greater than 1, create a SPIR-V vector
2200 if (type.getVectorSize() > 1)
2201 spvType = builder.makeVectorType(spvType, type.getVectorSize());
2202 }
2203
2204 if (type.isArray()) {
John Kessenichc9e0a422015-12-29 21:27:24 -07002205 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
2206
John Kessenichc9a80832015-09-12 12:17:44 -06002207 // Do all but the outer dimension
John Kessenichc9e0a422015-12-29 21:27:24 -07002208 if (type.getArraySizes()->getNumDims() > 1) {
John Kessenichf8842e52016-01-04 19:22:56 -07002209 // We need to decorate array strides for types needing explicit layout, except blocks.
2210 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
John Kessenichc9e0a422015-12-29 21:27:24 -07002211 // Use a dummy glslang type for querying internal strides of
2212 // arrays of arrays, but using just a one-dimensional array.
2213 glslang::TType simpleArrayType(type, 0); // deference type of the array
2214 while (simpleArrayType.getArraySizes().getNumDims() > 1)
2215 simpleArrayType.getArraySizes().dereference();
2216
2217 // Will compute the higher-order strides here, rather than making a whole
2218 // pile of types and doing repetitive recursion on their contents.
2219 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
2220 }
John Kessenichf8842e52016-01-04 19:22:56 -07002221
2222 // make the arrays
John Kessenichc9e0a422015-12-29 21:27:24 -07002223 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
John Kessenich6c292d32016-02-15 20:58:50 -07002224 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
John Kessenichc9e0a422015-12-29 21:27:24 -07002225 if (stride > 0)
2226 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich6c292d32016-02-15 20:58:50 -07002227 stride *= type.getArraySizes()->getDimSize(dim);
John Kessenichc9e0a422015-12-29 21:27:24 -07002228 }
2229 } else {
2230 // single-dimensional array, and don't yet have stride
2231
John Kessenichf8842e52016-01-04 19:22:56 -07002232 // We need to decorate array strides for types needing explicit layout, except blocks.
John Kessenichc9e0a422015-12-29 21:27:24 -07002233 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
2234 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
John Kessenichc9a80832015-09-12 12:17:44 -06002235 }
John Kessenich31ed4832015-09-09 17:51:38 -06002236
John Kessenichc9a80832015-09-12 12:17:44 -06002237 // Do the outer dimension, which might not be known for a runtime-sized array
2238 if (type.isRuntimeSizedArray()) {
2239 spvType = builder.makeRuntimeArray(spvType);
2240 } else {
2241 assert(type.getOuterArraySize() > 0);
John Kessenich6c292d32016-02-15 20:58:50 -07002242 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
John Kessenichc9a80832015-09-12 12:17:44 -06002243 }
John Kessenichc9e0a422015-12-29 21:27:24 -07002244 if (stride > 0)
2245 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich140f3df2015-06-26 16:58:36 -06002246 }
2247
2248 return spvType;
2249}
2250
John Kessenich6090df02016-06-30 21:18:02 -06002251// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
2252// explicitLayout can be kept the same throughout the hierarchical recursive walk.
2253// Mutually recursive with convertGlslangToSpvType().
2254spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
2255 const glslang::TTypeList* glslangMembers,
2256 glslang::TLayoutPacking explicitLayout,
2257 const glslang::TQualifier& qualifier)
2258{
2259 // Create a vector of struct types for SPIR-V to consume
2260 std::vector<spv::Id> spvMembers;
2261 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks
2262 int locationOffset = 0; // for use across struct members, when they are called recursively
2263 for (int i = 0; i < (int)glslangMembers->size(); i++) {
2264 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
2265 if (glslangMember.hiddenMember()) {
2266 ++memberDelta;
2267 if (type.getBasicType() == glslang::EbtBlock)
2268 memberRemapper[glslangMembers][i] = -1;
2269 } else {
2270 if (type.getBasicType() == glslang::EbtBlock)
2271 memberRemapper[glslangMembers][i] = i - memberDelta;
2272 // modify just this child's view of the qualifier
2273 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
2274 InheritQualifiers(memberQualifier, qualifier);
2275
2276 // manually inherit location; it's more complex
2277 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
2278 memberQualifier.layoutLocation = qualifier.layoutLocation + locationOffset;
2279 if (qualifier.hasLocation())
2280 locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember);
2281
2282 // recurse
2283 spvMembers.push_back(convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier));
2284 }
2285 }
2286
2287 // Make the SPIR-V type
2288 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
John Kessenichf2b7f332016-09-01 17:05:23 -06002289 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06002290 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
2291
2292 // Decorate it
2293 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
2294
2295 return spvType;
2296}
2297
2298void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
2299 const glslang::TTypeList* glslangMembers,
2300 glslang::TLayoutPacking explicitLayout,
2301 const glslang::TQualifier& qualifier,
2302 spv::Id spvType)
2303{
2304 // Name and decorate the non-hidden members
2305 int offset = -1;
2306 int locationOffset = 0; // for use within the members of this struct
2307 for (int i = 0; i < (int)glslangMembers->size(); i++) {
2308 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
2309 int member = i;
2310 if (type.getBasicType() == glslang::EbtBlock)
2311 member = memberRemapper[glslangMembers][i];
2312
2313 // modify just this child's view of the qualifier
2314 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
2315 InheritQualifiers(memberQualifier, qualifier);
2316
2317 // using -1 above to indicate a hidden member
2318 if (member >= 0) {
2319 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
2320 addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
2321 addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
2322 // Add interpolation and auxiliary storage decorations only to top-level members of Input and Output storage classes
John Kessenich65ee2302017-02-06 18:44:52 -07002323 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
2324 type.getQualifier().storage == glslang::EvqVaryingOut) {
2325 if (type.getBasicType() == glslang::EbtBlock ||
2326 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
John Kessenich6090df02016-06-30 21:18:02 -06002327 addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
2328 addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
2329 }
2330 }
2331 addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
2332
2333 if (qualifier.storage == glslang::EvqBuffer) {
2334 std::vector<spv::Decoration> memory;
2335 TranslateMemoryDecoration(memberQualifier, memory);
2336 for (unsigned int i = 0; i < memory.size(); ++i)
2337 addMemberDecoration(spvType, member, memory[i]);
2338 }
2339
John Kessenich2f47bc92016-06-30 21:47:35 -06002340 // Compute location decoration; tricky based on whether inheritance is at play and
2341 // what kind of container we have, etc.
John Kessenich6090df02016-06-30 21:18:02 -06002342 // TODO: This algorithm (and it's cousin above doing almost the same thing) should
2343 // probably move to the linker stage of the front end proper, and just have the
2344 // answer sitting already distributed throughout the individual member locations.
2345 int location = -1; // will only decorate if present or inherited
John Kessenich2f47bc92016-06-30 21:47:35 -06002346 // Ignore member locations if the container is an array, as that's
2347 // ill-specified and decisions have been made to not allow this anyway.
2348 // The object itself must have a location, and that comes out from decorating the object,
2349 // not the type (this code decorates types).
2350 if (! type.isArray()) {
2351 if (memberQualifier.hasLocation()) { // no inheritance, or override of inheritance
2352 // struct members should not have explicit locations
2353 assert(type.getBasicType() != glslang::EbtStruct);
2354 location = memberQualifier.layoutLocation;
2355 } else if (type.getBasicType() != glslang::EbtBlock) {
2356 // If it is a not a Block, (...) Its members are assigned consecutive locations (...)
2357 // The members, and their nested types, must not themselves have Location decorations.
2358 } else if (qualifier.hasLocation()) // inheritance
2359 location = qualifier.layoutLocation + locationOffset;
2360 }
John Kessenich6090df02016-06-30 21:18:02 -06002361 if (location >= 0)
2362 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, location);
2363
John Kessenich2f47bc92016-06-30 21:47:35 -06002364 if (qualifier.hasLocation()) // track for upcoming inheritance
2365 locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember);
2366
John Kessenich6090df02016-06-30 21:18:02 -06002367 // component, XFB, others
2368 if (glslangMember.getQualifier().hasComponent())
2369 builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangMember.getQualifier().layoutComponent);
2370 if (glslangMember.getQualifier().hasXfbOffset())
2371 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangMember.getQualifier().layoutXfbOffset);
2372 else if (explicitLayout != glslang::ElpNone) {
2373 // figure out what to do with offset, which is accumulating
2374 int nextOffset;
2375 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
2376 if (offset >= 0)
2377 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
2378 offset = nextOffset;
2379 }
2380
2381 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
2382 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
2383
2384 // built-in variable decorations
2385 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
John Kessenich4016e382016-07-15 11:53:56 -06002386 if (builtIn != spv::BuiltInMax)
John Kessenich6090df02016-06-30 21:18:02 -06002387 addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
chaoc771d89f2017-01-13 01:10:53 -08002388
2389#ifdef NV_EXTENSIONS
2390 if (builtIn == spv::BuiltInLayer) {
2391 // SPV_NV_viewport_array2 extension
2392 if (glslangMember.getQualifier().layoutViewportRelative){
2393 addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
2394 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
2395 builder.addExtension(spv::E_SPV_NV_viewport_array2);
2396 }
2397 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
2398 addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
2399 builder.addCapability(spv::CapabilityShaderStereoViewNV);
2400 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
2401 }
2402 }
chaocdf3956c2017-02-14 14:52:34 -08002403 if (glslangMember.getQualifier().layoutPassthrough) {
2404 addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
2405 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
2406 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
2407 }
chaoc771d89f2017-01-13 01:10:53 -08002408#endif
John Kessenich6090df02016-06-30 21:18:02 -06002409 }
2410 }
2411
2412 // Decorate the structure
2413 addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
2414 addDecoration(spvType, TranslateBlockDecoration(type));
2415 if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
2416 builder.addCapability(spv::CapabilityGeometryStreams);
2417 builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream);
2418 }
2419 if (glslangIntermediate->getXfbMode()) {
2420 builder.addCapability(spv::CapabilityTransformFeedback);
2421 if (type.getQualifier().hasXfbStride())
2422 builder.addDecoration(spvType, spv::DecorationXfbStride, type.getQualifier().layoutXfbStride);
2423 if (type.getQualifier().hasXfbBuffer())
2424 builder.addDecoration(spvType, spv::DecorationXfbBuffer, type.getQualifier().layoutXfbBuffer);
2425 }
2426}
2427
John Kessenich6c292d32016-02-15 20:58:50 -07002428// Turn the expression forming the array size into an id.
2429// This is not quite trivial, because of specialization constants.
2430// Sometimes, a raw constant is turned into an Id, and sometimes
2431// a specialization constant expression is.
2432spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
2433{
2434 // First, see if this is sized with a node, meaning a specialization constant:
2435 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
2436 if (specNode != nullptr) {
2437 builder.clearAccessChain();
2438 specNode->traverse(this);
2439 return accessChainLoad(specNode->getAsTyped()->getType());
2440 }
qining25262b32016-05-06 17:25:16 -04002441
John Kessenich6c292d32016-02-15 20:58:50 -07002442 // Otherwise, need a compile-time (front end) size, get it:
2443 int size = arraySizes.getDimSize(dim);
2444 assert(size > 0);
2445 return builder.makeUintConstant(size);
2446}
2447
John Kessenich103bef92016-02-08 21:38:15 -07002448// Wrap the builder's accessChainLoad to:
2449// - localize handling of RelaxedPrecision
2450// - use the SPIR-V inferred type instead of another conversion of the glslang type
2451// (avoids unnecessary work and possible type punning for structures)
2452// - do conversion of concrete to abstract type
John Kessenich32cfd492016-02-02 12:37:46 -07002453spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
2454{
John Kessenich103bef92016-02-08 21:38:15 -07002455 spv::Id nominalTypeId = builder.accessChainGetInferredType();
2456 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId);
2457
2458 // Need to convert to abstract types when necessary
Rex Xu27253232016-02-23 17:51:09 +08002459 if (type.getBasicType() == glslang::EbtBool) {
2460 if (builder.isScalarType(nominalTypeId)) {
2461 // Conversion for bool
2462 spv::Id boolType = builder.makeBoolType();
2463 if (nominalTypeId != boolType)
2464 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
2465 } else if (builder.isVectorType(nominalTypeId)) {
2466 // Conversion for bvec
2467 int vecSize = builder.getNumTypeComponents(nominalTypeId);
2468 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
2469 if (nominalTypeId != bvecType)
2470 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize));
2471 }
2472 }
John Kessenich103bef92016-02-08 21:38:15 -07002473
2474 return loadedId;
John Kessenich32cfd492016-02-02 12:37:46 -07002475}
2476
Rex Xu27253232016-02-23 17:51:09 +08002477// Wrap the builder's accessChainStore to:
2478// - do conversion of concrete to abstract type
John Kessenich4bf71552016-09-02 11:20:21 -06002479//
2480// Implicitly uses the existing builder.accessChain as the storage target.
Rex Xu27253232016-02-23 17:51:09 +08002481void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
2482{
2483 // Need to convert to abstract types when necessary
2484 if (type.getBasicType() == glslang::EbtBool) {
2485 spv::Id nominalTypeId = builder.accessChainGetInferredType();
2486
2487 if (builder.isScalarType(nominalTypeId)) {
2488 // Conversion for bool
2489 spv::Id boolType = builder.makeBoolType();
2490 if (nominalTypeId != boolType) {
2491 spv::Id zero = builder.makeUintConstant(0);
2492 spv::Id one = builder.makeUintConstant(1);
2493 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
2494 }
2495 } else if (builder.isVectorType(nominalTypeId)) {
2496 // Conversion for bvec
2497 int vecSize = builder.getNumTypeComponents(nominalTypeId);
2498 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
2499 if (nominalTypeId != bvecType) {
2500 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
2501 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
2502 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
2503 }
2504 }
2505 }
2506
2507 builder.accessChainStore(rvalue);
2508}
2509
John Kessenich4bf71552016-09-02 11:20:21 -06002510// For storing when types match at the glslang level, but not might match at the
2511// SPIR-V level.
2512//
2513// This especially happens when a single glslang type expands to multiple
John Kesseniched33e052016-10-06 12:59:51 -06002514// SPIR-V types, like a struct that is used in a member-undecorated way as well
John Kessenich4bf71552016-09-02 11:20:21 -06002515// as in a member-decorated way.
2516//
2517// NOTE: This function can handle any store request; if it's not special it
2518// simplifies to a simple OpStore.
2519//
2520// Implicitly uses the existing builder.accessChain as the storage target.
2521void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
2522{
John Kessenichb3e24e42016-09-11 12:33:43 -06002523 // we only do the complex path here if it's an aggregate
2524 if (! type.isStruct() && ! type.isArray()) {
John Kessenich4bf71552016-09-02 11:20:21 -06002525 accessChainStore(type, rValue);
2526 return;
2527 }
2528
John Kessenichb3e24e42016-09-11 12:33:43 -06002529 // and, it has to be a case of type aliasing
John Kessenich4bf71552016-09-02 11:20:21 -06002530 spv::Id rType = builder.getTypeId(rValue);
2531 spv::Id lValue = builder.accessChainGetLValue();
2532 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
2533 if (lType == rType) {
2534 accessChainStore(type, rValue);
2535 return;
2536 }
2537
John Kessenichb3e24e42016-09-11 12:33:43 -06002538 // Recursively (as needed) copy an aggregate type to a different aggregate type,
John Kessenich4bf71552016-09-02 11:20:21 -06002539 // where the two types were the same type in GLSL. This requires member
2540 // by member copy, recursively.
2541
John Kessenichb3e24e42016-09-11 12:33:43 -06002542 // If an array, copy element by element.
2543 if (type.isArray()) {
2544 glslang::TType glslangElementType(type, 0);
2545 spv::Id elementRType = builder.getContainedTypeId(rType);
2546 for (int index = 0; index < type.getOuterArraySize(); ++index) {
2547 // get the source member
2548 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
John Kessenich4bf71552016-09-02 11:20:21 -06002549
John Kessenichb3e24e42016-09-11 12:33:43 -06002550 // set up the target storage
2551 builder.clearAccessChain();
2552 builder.setAccessChainLValue(lValue);
2553 builder.accessChainPush(builder.makeIntConstant(index));
John Kessenich4bf71552016-09-02 11:20:21 -06002554
John Kessenichb3e24e42016-09-11 12:33:43 -06002555 // store the member
2556 multiTypeStore(glslangElementType, elementRValue);
2557 }
2558 } else {
2559 assert(type.isStruct());
John Kessenich4bf71552016-09-02 11:20:21 -06002560
John Kessenichb3e24e42016-09-11 12:33:43 -06002561 // loop over structure members
2562 const glslang::TTypeList& members = *type.getStruct();
2563 for (int m = 0; m < (int)members.size(); ++m) {
2564 const glslang::TType& glslangMemberType = *members[m].type;
2565
2566 // get the source member
2567 spv::Id memberRType = builder.getContainedTypeId(rType, m);
2568 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
2569
2570 // set up the target storage
2571 builder.clearAccessChain();
2572 builder.setAccessChainLValue(lValue);
2573 builder.accessChainPush(builder.makeIntConstant(m));
2574
2575 // store the member
2576 multiTypeStore(glslangMemberType, memberRValue);
2577 }
John Kessenich4bf71552016-09-02 11:20:21 -06002578 }
2579}
2580
John Kessenichf85e8062015-12-19 13:57:10 -07002581// Decide whether or not this type should be
2582// decorated with offsets and strides, and if so
2583// whether std140 or std430 rules should be applied.
2584glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
John Kessenich31ed4832015-09-09 17:51:38 -06002585{
John Kessenichf85e8062015-12-19 13:57:10 -07002586 // has to be a block
2587 if (type.getBasicType() != glslang::EbtBlock)
2588 return glslang::ElpNone;
2589
2590 // has to be a uniform or buffer block
2591 if (type.getQualifier().storage != glslang::EvqUniform &&
2592 type.getQualifier().storage != glslang::EvqBuffer)
2593 return glslang::ElpNone;
2594
2595 // return the layout to use
2596 switch (type.getQualifier().layoutPacking) {
2597 case glslang::ElpStd140:
2598 case glslang::ElpStd430:
2599 return type.getQualifier().layoutPacking;
2600 default:
2601 return glslang::ElpNone;
2602 }
John Kessenich31ed4832015-09-09 17:51:38 -06002603}
2604
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002605// Given an array type, returns the integer stride required for that array
John Kessenich3ac051e2015-12-20 11:29:16 -07002606int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002607{
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002608 int size;
John Kessenich49987892015-12-29 17:11:44 -07002609 int stride;
2610 glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
John Kesseniche721f492015-12-06 19:17:49 -07002611
2612 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002613}
2614
John Kessenich49987892015-12-29 17:11:44 -07002615// 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 -07002616// when used as a member of an interface block
John Kessenich3ac051e2015-12-20 11:29:16 -07002617int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002618{
John Kessenich49987892015-12-29 17:11:44 -07002619 glslang::TType elementType;
2620 elementType.shallowCopy(matrixType);
2621 elementType.clearArraySizes();
2622
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002623 int size;
John Kessenich49987892015-12-29 17:11:44 -07002624 int stride;
2625 glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
2626
2627 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07002628}
2629
John Kessenich5e4b1242015-08-06 22:53:06 -06002630// Given a member type of a struct, realign the current offset for it, and compute
2631// the next (not yet aligned) offset for the next member, which will get aligned
2632// on the next call.
2633// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
2634// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
2635// -1 means a non-forced member offset (no decoration needed).
John Kessenich6c292d32016-02-15 20:58:50 -07002636void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& /*structType*/, const glslang::TType& memberType, int& currentOffset, int& nextOffset,
John Kessenich3ac051e2015-12-20 11:29:16 -07002637 glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
John Kessenich5e4b1242015-08-06 22:53:06 -06002638{
2639 // this will get a positive value when deemed necessary
2640 nextOffset = -1;
2641
John Kessenich5e4b1242015-08-06 22:53:06 -06002642 // override anything in currentOffset with user-set offset
2643 if (memberType.getQualifier().hasOffset())
2644 currentOffset = memberType.getQualifier().layoutOffset;
2645
2646 // It could be that current linker usage in glslang updated all the layoutOffset,
2647 // in which case the following code does not matter. But, that's not quite right
2648 // once cross-compilation unit GLSL validation is done, as the original user
2649 // settings are needed in layoutOffset, and then the following will come into play.
2650
John Kessenichf85e8062015-12-19 13:57:10 -07002651 if (explicitLayout == glslang::ElpNone) {
John Kessenich5e4b1242015-08-06 22:53:06 -06002652 if (! memberType.getQualifier().hasOffset())
2653 currentOffset = -1;
2654
2655 return;
2656 }
2657
John Kessenichf85e8062015-12-19 13:57:10 -07002658 // Getting this far means we need explicit offsets
John Kessenich5e4b1242015-08-06 22:53:06 -06002659 if (currentOffset < 0)
2660 currentOffset = 0;
qining25262b32016-05-06 17:25:16 -04002661
John Kessenich5e4b1242015-08-06 22:53:06 -06002662 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
2663 // but possibly not yet correctly aligned.
2664
2665 int memberSize;
John Kessenich49987892015-12-29 17:11:44 -07002666 int dummyStride;
2667 int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, dummyStride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor);
John Kessenich5e4b1242015-08-06 22:53:06 -06002668 glslang::RoundToPow2(currentOffset, memberAlignment);
2669 nextOffset = currentOffset + memberSize;
2670}
2671
David Netoa901ffe2016-06-08 14:11:40 +01002672void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
John Kessenichebb50532016-05-16 19:22:05 -06002673{
David Netoa901ffe2016-06-08 14:11:40 +01002674 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
2675 switch (glslangBuiltIn)
2676 {
2677 case glslang::EbvClipDistance:
2678 case glslang::EbvCullDistance:
2679 case glslang::EbvPointSize:
chaoc771d89f2017-01-13 01:10:53 -08002680#ifdef NV_EXTENSIONS
2681 case glslang::EbvLayer:
2682 case glslang::EbvViewportMaskNV:
2683 case glslang::EbvSecondaryPositionNV:
2684 case glslang::EbvSecondaryViewportMaskNV:
chaocdf3956c2017-02-14 14:52:34 -08002685 case glslang::EbvPositionPerViewNV:
2686 case glslang::EbvViewportMaskPerViewNV:
chaoc771d89f2017-01-13 01:10:53 -08002687#endif
David Netoa901ffe2016-06-08 14:11:40 +01002688 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
2689 // Alternately, we could just call this for any glslang built-in, since the
2690 // capability already guards against duplicates.
2691 TranslateBuiltInDecoration(glslangBuiltIn, false);
2692 break;
2693 default:
2694 // Capabilities were already generated when the struct was declared.
2695 break;
2696 }
John Kessenichebb50532016-05-16 19:22:05 -06002697}
2698
John Kessenich6fccb3c2016-09-19 16:01:41 -06002699bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
John Kessenich140f3df2015-06-26 16:58:36 -06002700{
John Kessenicheee9d532016-09-19 18:09:30 -06002701 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
John Kessenich140f3df2015-06-26 16:58:36 -06002702}
2703
2704// Make all the functions, skeletally, without actually visiting their bodies.
2705void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
2706{
2707 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
2708 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
John Kessenich6fccb3c2016-09-19 16:01:41 -06002709 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
John Kessenich140f3df2015-06-26 16:58:36 -06002710 continue;
2711
2712 // We're on a user function. Set up the basic interface for the function now,
John Kessenich4bf71552016-09-02 11:20:21 -06002713 // so that it's available to call. Translating the body will happen later.
John Kessenich140f3df2015-06-26 16:58:36 -06002714 //
qining25262b32016-05-06 17:25:16 -04002715 // Typically (except for a "const in" parameter), an address will be passed to the
John Kessenich140f3df2015-06-26 16:58:36 -06002716 // function. What it is an address of varies:
2717 //
John Kessenich4bf71552016-09-02 11:20:21 -06002718 // - "in" parameters not marked as "const" can be written to without modifying the calling
2719 // argument so that write needs to be to a copy, hence the address of a copy works.
John Kessenich140f3df2015-06-26 16:58:36 -06002720 //
2721 // - "const in" parameters can just be the r-value, as no writes need occur.
2722 //
John Kessenich4bf71552016-09-02 11:20:21 -06002723 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
2724 // 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 -06002725
2726 std::vector<spv::Id> paramTypes;
John Kessenich32cfd492016-02-02 12:37:46 -07002727 std::vector<spv::Decoration> paramPrecisions;
John Kessenich140f3df2015-06-26 16:58:36 -06002728 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
2729
2730 for (int p = 0; p < (int)parameters.size(); ++p) {
2731 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
2732 spv::Id typeId = convertGlslangToSpvType(paramType);
steve-lunargdd8287a2017-02-23 18:04:12 -07002733 if (paramType.containsOpaque() ||
2734 (paramType.getBasicType() == glslang::EbtBlock && paramType.getQualifier().storage == glslang::EvqBuffer))
Jason Ekstranded15ef12016-06-08 13:54:48 -07002735 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
2736 else if (paramType.getQualifier().storage != glslang::EvqConstReadOnly)
John Kessenich140f3df2015-06-26 16:58:36 -06002737 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
2738 else
John Kessenich4bf71552016-09-02 11:20:21 -06002739 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
John Kessenich32cfd492016-02-02 12:37:46 -07002740 paramPrecisions.push_back(TranslatePrecisionDecoration(paramType));
John Kessenich140f3df2015-06-26 16:58:36 -06002741 paramTypes.push_back(typeId);
2742 }
2743
2744 spv::Block* functionBlock;
John Kessenich32cfd492016-02-02 12:37:46 -07002745 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
2746 convertGlslangToSpvType(glslFunction->getType()),
2747 glslFunction->getName().c_str(), paramTypes, paramPrecisions, &functionBlock);
John Kessenich140f3df2015-06-26 16:58:36 -06002748
2749 // Track function to emit/call later
2750 functionMap[glslFunction->getName().c_str()] = function;
2751
2752 // Set the parameter id's
2753 for (int p = 0; p < (int)parameters.size(); ++p) {
2754 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
2755 // give a name too
2756 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
2757 }
2758 }
2759}
2760
2761// Process all the initializers, while skipping the functions and link objects
2762void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
2763{
2764 builder.setBuildPoint(shaderEntry->getLastBlock());
2765 for (int i = 0; i < (int)initializers.size(); ++i) {
2766 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
2767 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) {
2768
2769 // We're on a top-level node that's not a function. Treat as an initializer, whose
John Kessenich6fccb3c2016-09-19 16:01:41 -06002770 // code goes into the beginning of the entry point.
John Kessenich140f3df2015-06-26 16:58:36 -06002771 initializer->traverse(this);
2772 }
2773 }
2774}
2775
2776// Process all the functions, while skipping initializers.
2777void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
2778{
2779 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
2780 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
John Kessenich6a60c2f2016-12-08 21:01:59 -07002781 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
John Kessenich140f3df2015-06-26 16:58:36 -06002782 node->traverse(this);
2783 }
2784}
2785
2786void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
2787{
qining25262b32016-05-06 17:25:16 -04002788 // SPIR-V functions should already be in the functionMap from the prepass
John Kessenich140f3df2015-06-26 16:58:36 -06002789 // that called makeFunctions().
John Kesseniched33e052016-10-06 12:59:51 -06002790 currentFunction = functionMap[node->getName().c_str()];
2791 spv::Block* functionBlock = currentFunction->getEntryBlock();
John Kessenich140f3df2015-06-26 16:58:36 -06002792 builder.setBuildPoint(functionBlock);
2793}
2794
Rex Xu04db3f52015-09-16 11:44:02 +08002795void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06002796{
Rex Xufc618912015-09-09 16:42:49 +08002797 const glslang::TIntermSequence& glslangArguments = node.getSequence();
Rex Xu48edadf2015-12-31 16:11:41 +08002798
2799 glslang::TSampler sampler = {};
2800 bool cubeCompare = false;
Rex Xu5eafa472016-02-19 22:24:03 +08002801 if (node.isTexture() || node.isImage()) {
Rex Xu48edadf2015-12-31 16:11:41 +08002802 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
2803 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
2804 }
2805
John Kessenich140f3df2015-06-26 16:58:36 -06002806 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
2807 builder.clearAccessChain();
2808 glslangArguments[i]->traverse(this);
Rex Xufc618912015-09-09 16:42:49 +08002809
2810 // Special case l-value operands
2811 bool lvalue = false;
2812 switch (node.getOp()) {
2813 case glslang::EOpImageAtomicAdd:
2814 case glslang::EOpImageAtomicMin:
2815 case glslang::EOpImageAtomicMax:
2816 case glslang::EOpImageAtomicAnd:
2817 case glslang::EOpImageAtomicOr:
2818 case glslang::EOpImageAtomicXor:
2819 case glslang::EOpImageAtomicExchange:
2820 case glslang::EOpImageAtomicCompSwap:
2821 if (i == 0)
2822 lvalue = true;
2823 break;
Rex Xu5eafa472016-02-19 22:24:03 +08002824 case glslang::EOpSparseImageLoad:
2825 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
2826 lvalue = true;
2827 break;
Rex Xu48edadf2015-12-31 16:11:41 +08002828 case glslang::EOpSparseTexture:
2829 if ((cubeCompare && i == 3) || (! cubeCompare && i == 2))
2830 lvalue = true;
2831 break;
2832 case glslang::EOpSparseTextureClamp:
2833 if ((cubeCompare && i == 4) || (! cubeCompare && i == 3))
2834 lvalue = true;
2835 break;
2836 case glslang::EOpSparseTextureLod:
2837 case glslang::EOpSparseTextureOffset:
2838 if (i == 3)
2839 lvalue = true;
2840 break;
2841 case glslang::EOpSparseTextureFetch:
2842 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
2843 lvalue = true;
2844 break;
2845 case glslang::EOpSparseTextureFetchOffset:
2846 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
2847 lvalue = true;
2848 break;
2849 case glslang::EOpSparseTextureLodOffset:
2850 case glslang::EOpSparseTextureGrad:
2851 case glslang::EOpSparseTextureOffsetClamp:
2852 if (i == 4)
2853 lvalue = true;
2854 break;
2855 case glslang::EOpSparseTextureGradOffset:
2856 case glslang::EOpSparseTextureGradClamp:
2857 if (i == 5)
2858 lvalue = true;
2859 break;
2860 case glslang::EOpSparseTextureGradOffsetClamp:
2861 if (i == 6)
2862 lvalue = true;
2863 break;
2864 case glslang::EOpSparseTextureGather:
2865 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
2866 lvalue = true;
2867 break;
2868 case glslang::EOpSparseTextureGatherOffset:
2869 case glslang::EOpSparseTextureGatherOffsets:
2870 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
2871 lvalue = true;
2872 break;
Rex Xufc618912015-09-09 16:42:49 +08002873 default:
2874 break;
2875 }
2876
Rex Xu6b86d492015-09-16 17:48:22 +08002877 if (lvalue)
Rex Xufc618912015-09-09 16:42:49 +08002878 arguments.push_back(builder.accessChainGetLValue());
Rex Xu6b86d492015-09-16 17:48:22 +08002879 else
John Kessenich32cfd492016-02-02 12:37:46 -07002880 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06002881 }
2882}
2883
John Kessenichfc51d282015-08-19 13:34:18 -06002884void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06002885{
John Kessenichfc51d282015-08-19 13:34:18 -06002886 builder.clearAccessChain();
2887 node.getOperand()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002888 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
John Kessenichfc51d282015-08-19 13:34:18 -06002889}
John Kessenich140f3df2015-06-26 16:58:36 -06002890
John Kessenichfc51d282015-08-19 13:34:18 -06002891spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
2892{
Rex Xufc618912015-09-09 16:42:49 +08002893 if (! node->isImage() && ! node->isTexture()) {
John Kessenichfc51d282015-08-19 13:34:18 -06002894 return spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06002895 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002896 auto resultType = [&node,this]{ return convertGlslangToSpvType(node->getType()); };
John Kessenich140f3df2015-06-26 16:58:36 -06002897
John Kessenichfc51d282015-08-19 13:34:18 -06002898 // Process a GLSL texturing op (will be SPV image)
John Kessenichfc51d282015-08-19 13:34:18 -06002899 const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler()
2900 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler();
2901 std::vector<spv::Id> arguments;
2902 if (node->getAsAggregate())
Rex Xufc618912015-09-09 16:42:49 +08002903 translateArguments(*node->getAsAggregate(), arguments);
John Kessenichfc51d282015-08-19 13:34:18 -06002904 else
2905 translateArguments(*node->getAsUnaryNode(), arguments);
John Kessenichf6640762016-08-01 19:44:00 -06002906 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenichfc51d282015-08-19 13:34:18 -06002907
2908 spv::Builder::TextureParameters params = { };
2909 params.sampler = arguments[0];
2910
Rex Xu04db3f52015-09-16 11:44:02 +08002911 glslang::TCrackedTextureOp cracked;
2912 node->crackTexture(sampler, cracked);
2913
John Kessenichfc51d282015-08-19 13:34:18 -06002914 // Check for queries
2915 if (cracked.query) {
Maciej Jesionowski7208a972016-10-12 15:40:37 +02002916 // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first
2917 if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler))
John Kessenich33661452015-12-08 19:32:47 -07002918 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
Maciej Jesionowski7208a972016-10-12 15:40:37 +02002919
John Kessenichfc51d282015-08-19 13:34:18 -06002920 switch (node->getOp()) {
2921 case glslang::EOpImageQuerySize:
2922 case glslang::EOpTextureQuerySize:
John Kessenich140f3df2015-06-26 16:58:36 -06002923 if (arguments.size() > 1) {
2924 params.lod = arguments[1];
John Kessenich5e4b1242015-08-06 22:53:06 -06002925 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params);
John Kessenich140f3df2015-06-26 16:58:36 -06002926 } else
John Kessenich5e4b1242015-08-06 22:53:06 -06002927 return builder.createTextureQueryCall(spv::OpImageQuerySize, params);
John Kessenichfc51d282015-08-19 13:34:18 -06002928 case glslang::EOpImageQuerySamples:
2929 case glslang::EOpTextureQuerySamples:
John Kessenich5e4b1242015-08-06 22:53:06 -06002930 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params);
John Kessenichfc51d282015-08-19 13:34:18 -06002931 case glslang::EOpTextureQueryLod:
2932 params.coords = arguments[1];
2933 return builder.createTextureQueryCall(spv::OpImageQueryLod, params);
2934 case glslang::EOpTextureQueryLevels:
2935 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params);
Rex Xu48edadf2015-12-31 16:11:41 +08002936 case glslang::EOpSparseTexelsResident:
2937 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
John Kessenichfc51d282015-08-19 13:34:18 -06002938 default:
2939 assert(0);
2940 break;
John Kessenich140f3df2015-06-26 16:58:36 -06002941 }
John Kessenich140f3df2015-06-26 16:58:36 -06002942 }
2943
Rex Xufc618912015-09-09 16:42:49 +08002944 // Check for image functions other than queries
2945 if (node->isImage()) {
John Kessenich56bab042015-09-16 10:54:31 -06002946 std::vector<spv::Id> operands;
2947 auto opIt = arguments.begin();
2948 operands.push_back(*(opIt++));
John Kessenich6c292d32016-02-15 20:58:50 -07002949
2950 // Handle subpass operations
2951 // TODO: GLSL should change to have the "MS" only on the type rather than the
2952 // built-in function.
2953 if (cracked.subpass) {
2954 // add on the (0,0) coordinate
2955 spv::Id zero = builder.makeIntConstant(0);
2956 std::vector<spv::Id> comps;
2957 comps.push_back(zero);
2958 comps.push_back(zero);
2959 operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps));
2960 if (sampler.ms) {
2961 operands.push_back(spv::ImageOperandsSampleMask);
2962 operands.push_back(*(opIt++));
2963 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002964 return builder.createOp(spv::OpImageRead, resultType(), operands);
John Kessenich6c292d32016-02-15 20:58:50 -07002965 }
2966
John Kessenich56bab042015-09-16 10:54:31 -06002967 operands.push_back(*(opIt++));
John Kessenich56bab042015-09-16 10:54:31 -06002968 if (node->getOp() == glslang::EOpImageLoad) {
John Kessenich55e7d112015-11-15 21:33:39 -07002969 if (sampler.ms) {
2970 operands.push_back(spv::ImageOperandsSampleMask);
Rex Xu7beb4412015-12-15 17:52:45 +08002971 operands.push_back(*opIt);
John Kessenich55e7d112015-11-15 21:33:39 -07002972 }
John Kessenich5d0fa972016-02-15 11:57:00 -07002973 if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown)
2974 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
John Kessenich8c8505c2016-07-26 12:50:38 -06002975 return builder.createOp(spv::OpImageRead, resultType(), operands);
John Kessenich56bab042015-09-16 10:54:31 -06002976 } else if (node->getOp() == glslang::EOpImageStore) {
Rex Xu7beb4412015-12-15 17:52:45 +08002977 if (sampler.ms) {
2978 operands.push_back(*(opIt + 1));
2979 operands.push_back(spv::ImageOperandsSampleMask);
2980 operands.push_back(*opIt);
2981 } else
2982 operands.push_back(*opIt);
John Kessenich56bab042015-09-16 10:54:31 -06002983 builder.createNoResultOp(spv::OpImageWrite, operands);
John Kessenich5d0fa972016-02-15 11:57:00 -07002984 if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown)
2985 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
John Kessenich56bab042015-09-16 10:54:31 -06002986 return spv::NoResult;
Rex Xu5eafa472016-02-19 22:24:03 +08002987 } else if (node->getOp() == glslang::EOpSparseImageLoad) {
2988 builder.addCapability(spv::CapabilitySparseResidency);
2989 if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown)
2990 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
2991
2992 if (sampler.ms) {
2993 operands.push_back(spv::ImageOperandsSampleMask);
2994 operands.push_back(*opIt++);
2995 }
2996
2997 // Create the return type that was a special structure
2998 spv::Id texelOut = *opIt;
John Kessenich8c8505c2016-07-26 12:50:38 -06002999 spv::Id typeId0 = resultType();
Rex Xu5eafa472016-02-19 22:24:03 +08003000 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
3001 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
3002
3003 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
3004
3005 // Decode the return type
3006 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
3007 return builder.createCompositeExtract(resultId, typeId0, 0);
John Kessenichcd261442016-01-22 09:54:12 -07003008 } else {
Rex Xu6b86d492015-09-16 17:48:22 +08003009 // Process image atomic operations
3010
3011 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
3012 // as the first source operand, is required by SPIR-V atomic operations.
John Kessenichcd261442016-01-22 09:54:12 -07003013 operands.push_back(sampler.ms ? *(opIt++) : builder.makeUintConstant(0)); // For non-MS, the value should be 0
John Kessenich140f3df2015-06-26 16:58:36 -06003014
John Kessenich8c8505c2016-07-26 12:50:38 -06003015 spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
John Kessenich56bab042015-09-16 10:54:31 -06003016 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
Rex Xufc618912015-09-09 16:42:49 +08003017
3018 std::vector<spv::Id> operands;
3019 operands.push_back(pointer);
3020 for (; opIt != arguments.end(); ++opIt)
3021 operands.push_back(*opIt);
3022
John Kessenich8c8505c2016-07-26 12:50:38 -06003023 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
Rex Xufc618912015-09-09 16:42:49 +08003024 }
3025 }
3026
3027 // Check for texture functions other than queries
Rex Xu48edadf2015-12-31 16:11:41 +08003028 bool sparse = node->isSparseTexture();
Rex Xu71519fe2015-11-11 15:35:47 +08003029 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
3030
John Kessenichfc51d282015-08-19 13:34:18 -06003031 // check for bias argument
3032 bool bias = false;
Rex Xu71519fe2015-11-11 15:35:47 +08003033 if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06003034 int nonBiasArgCount = 2;
3035 if (cracked.offset)
3036 ++nonBiasArgCount;
3037 if (cracked.grad)
3038 nonBiasArgCount += 2;
Rex Xu48edadf2015-12-31 16:11:41 +08003039 if (cracked.lodClamp)
3040 ++nonBiasArgCount;
3041 if (sparse)
3042 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06003043
3044 if ((int)arguments.size() > nonBiasArgCount)
3045 bias = true;
3046 }
3047
John Kessenicha5c33d62016-06-02 23:45:21 -06003048 // See if the sampler param should really be just the SPV image part
3049 if (cracked.fetch) {
3050 // a fetch needs to have the image extracted first
3051 if (builder.isSampledImage(params.sampler))
3052 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
3053 }
3054
John Kessenichfc51d282015-08-19 13:34:18 -06003055 // set the rest of the arguments
John Kessenich55e7d112015-11-15 21:33:39 -07003056
John Kessenichfc51d282015-08-19 13:34:18 -06003057 params.coords = arguments[1];
3058 int extraArgs = 0;
John Kessenich019f08f2016-02-15 15:40:42 -07003059 bool noImplicitLod = false;
John Kessenich55e7d112015-11-15 21:33:39 -07003060
3061 // sort out where Dref is coming from
Rex Xu48edadf2015-12-31 16:11:41 +08003062 if (cubeCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06003063 params.Dref = arguments[2];
Rex Xu48edadf2015-12-31 16:11:41 +08003064 ++extraArgs;
3065 } else if (sampler.shadow && cracked.gather) {
John Kessenich55e7d112015-11-15 21:33:39 -07003066 params.Dref = arguments[2];
3067 ++extraArgs;
3068 } else if (sampler.shadow) {
John Kessenichfc51d282015-08-19 13:34:18 -06003069 std::vector<spv::Id> indexes;
John Kessenich76d4dfc2016-06-16 12:43:23 -06003070 int dRefComp;
John Kessenichfc51d282015-08-19 13:34:18 -06003071 if (cracked.proj)
John Kessenich76d4dfc2016-06-16 12:43:23 -06003072 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
John Kessenichfc51d282015-08-19 13:34:18 -06003073 else
John Kessenich76d4dfc2016-06-16 12:43:23 -06003074 dRefComp = builder.getNumComponents(params.coords) - 1;
3075 indexes.push_back(dRefComp);
John Kessenichfc51d282015-08-19 13:34:18 -06003076 params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
3077 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003078
3079 // lod
John Kessenichfc51d282015-08-19 13:34:18 -06003080 if (cracked.lod) {
3081 params.lod = arguments[2];
3082 ++extraArgs;
John Kessenich019f08f2016-02-15 15:40:42 -07003083 } else if (glslangIntermediate->getStage() != EShLangFragment) {
3084 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
3085 noImplicitLod = true;
3086 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003087
3088 // multisample
John Kessenich019f08f2016-02-15 15:40:42 -07003089 if (sampler.ms) {
Rex Xu6b86d492015-09-16 17:48:22 +08003090 params.sample = arguments[2]; // For MS, "sample" should be specified
Rex Xu04db3f52015-09-16 11:44:02 +08003091 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06003092 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003093
3094 // gradient
John Kessenichfc51d282015-08-19 13:34:18 -06003095 if (cracked.grad) {
3096 params.gradX = arguments[2 + extraArgs];
3097 params.gradY = arguments[3 + extraArgs];
3098 extraArgs += 2;
3099 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003100
3101 // offset and offsets
John Kessenich55e7d112015-11-15 21:33:39 -07003102 if (cracked.offset) {
John Kessenichfc51d282015-08-19 13:34:18 -06003103 params.offset = arguments[2 + extraArgs];
3104 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07003105 } else if (cracked.offsets) {
3106 params.offsets = arguments[2 + extraArgs];
3107 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06003108 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003109
3110 // lod clamp
Rex Xu48edadf2015-12-31 16:11:41 +08003111 if (cracked.lodClamp) {
3112 params.lodClamp = arguments[2 + extraArgs];
3113 ++extraArgs;
3114 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003115
3116 // sparse
Rex Xu48edadf2015-12-31 16:11:41 +08003117 if (sparse) {
3118 params.texelOut = arguments[2 + extraArgs];
3119 ++extraArgs;
3120 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003121
3122 // bias
John Kessenichfc51d282015-08-19 13:34:18 -06003123 if (bias) {
3124 params.bias = arguments[2 + extraArgs];
3125 ++extraArgs;
3126 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06003127
3128 // gather component
John Kessenich55e7d112015-11-15 21:33:39 -07003129 if (cracked.gather && ! sampler.shadow) {
3130 // default component is 0, if missing, otherwise an argument
3131 if (2 + extraArgs < (int)arguments.size()) {
John Kessenich76d4dfc2016-06-16 12:43:23 -06003132 params.component = arguments[2 + extraArgs];
John Kessenich55e7d112015-11-15 21:33:39 -07003133 ++extraArgs;
3134 } else {
John Kessenich76d4dfc2016-06-16 12:43:23 -06003135 params.component = builder.makeIntConstant(0);
John Kessenich55e7d112015-11-15 21:33:39 -07003136 }
3137 }
John Kessenichfc51d282015-08-19 13:34:18 -06003138
John Kessenich65336482016-06-16 14:06:26 -06003139 // projective component (might not to move)
3140 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
3141 // are divided by the last component of P."
3142 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
3143 // unused components will appear after all used components."
3144 if (cracked.proj) {
3145 int projSourceComp = builder.getNumComponents(params.coords) - 1;
3146 int projTargetComp;
3147 switch (sampler.dim) {
3148 case glslang::Esd1D: projTargetComp = 1; break;
3149 case glslang::Esd2D: projTargetComp = 2; break;
3150 case glslang::EsdRect: projTargetComp = 2; break;
3151 default: projTargetComp = projSourceComp; break;
3152 }
3153 // copy the projective coordinate if we have to
3154 if (projTargetComp != projSourceComp) {
John Kessenichecba76f2017-01-06 00:34:48 -07003155 spv::Id projComp = builder.createCompositeExtract(params.coords,
John Kessenich65336482016-06-16 14:06:26 -06003156 builder.getScalarTypeId(builder.getTypeId(params.coords)),
3157 projSourceComp);
3158 params.coords = builder.createCompositeInsert(projComp, params.coords,
3159 builder.getTypeId(params.coords), projTargetComp);
3160 }
3161 }
3162
John Kessenich8c8505c2016-07-26 12:50:38 -06003163 return builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params);
John Kessenich140f3df2015-06-26 16:58:36 -06003164}
3165
3166spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
3167{
3168 // Grab the function's pointer from the previously created function
3169 spv::Function* function = functionMap[node->getName().c_str()];
3170 if (! function)
3171 return 0;
3172
3173 const glslang::TIntermSequence& glslangArgs = node->getSequence();
3174 const glslang::TQualifierList& qualifiers = node->getQualifierList();
3175
3176 // See comments in makeFunctions() for details about the semantics for parameter passing.
3177 //
3178 // These imply we need a four step process:
3179 // 1. Evaluate the arguments
3180 // 2. Allocate and make copies of in, out, and inout arguments
3181 // 3. Make the call
3182 // 4. Copy back the results
3183
3184 // 1. Evaluate the arguments
3185 std::vector<spv::Builder::AccessChain> lValues;
3186 std::vector<spv::Id> rValues;
John Kessenich32cfd492016-02-02 12:37:46 -07003187 std::vector<const glslang::TType*> argTypes;
John Kessenich140f3df2015-06-26 16:58:36 -06003188 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07003189 const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType();
John Kessenich140f3df2015-06-26 16:58:36 -06003190 // build l-value
3191 builder.clearAccessChain();
3192 glslangArgs[a]->traverse(this);
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07003193 argTypes.push_back(&paramType);
John Kessenich11765302016-07-31 12:39:46 -06003194 // keep outputs and opaque objects as l-values, evaluate input-only as r-values
John Kessenich4a57dce2017-02-24 19:15:46 -07003195 if (qualifiers[a] != glslang::EvqConstReadOnly || paramType.containsOpaque()) {
John Kessenich140f3df2015-06-26 16:58:36 -06003196 // save l-value
3197 lValues.push_back(builder.getAccessChain());
3198 } else {
3199 // process r-value
John Kessenich32cfd492016-02-02 12:37:46 -07003200 rValues.push_back(accessChainLoad(*argTypes.back()));
John Kessenich140f3df2015-06-26 16:58:36 -06003201 }
3202 }
3203
3204 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
3205 // copy the original into that space.
3206 //
3207 // Also, build up the list of actual arguments to pass in for the call
3208 int lValueCount = 0;
3209 int rValueCount = 0;
3210 std::vector<spv::Id> spvArgs;
3211 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07003212 const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType();
John Kessenich140f3df2015-06-26 16:58:36 -06003213 spv::Id arg;
steve-lunargdd8287a2017-02-23 18:04:12 -07003214 if (paramType.containsOpaque() ||
3215 (paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer)) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07003216 builder.setAccessChain(lValues[lValueCount]);
3217 arg = builder.accessChainGetLValue();
3218 ++lValueCount;
3219 } else if (qualifiers[a] != glslang::EvqConstReadOnly) {
John Kessenich140f3df2015-06-26 16:58:36 -06003220 // need space to hold the copy
John Kessenich140f3df2015-06-26 16:58:36 -06003221 arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param");
3222 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
3223 // need to copy the input into output space
3224 builder.setAccessChain(lValues[lValueCount]);
John Kessenich32cfd492016-02-02 12:37:46 -07003225 spv::Id copy = accessChainLoad(*argTypes[a]);
John Kessenich4bf71552016-09-02 11:20:21 -06003226 builder.clearAccessChain();
3227 builder.setAccessChainLValue(arg);
3228 multiTypeStore(paramType, copy);
John Kessenich140f3df2015-06-26 16:58:36 -06003229 }
3230 ++lValueCount;
3231 } else {
3232 arg = rValues[rValueCount];
3233 ++rValueCount;
3234 }
3235 spvArgs.push_back(arg);
3236 }
3237
3238 // 3. Make the call.
3239 spv::Id result = builder.createFunctionCall(function, spvArgs);
John Kessenich32cfd492016-02-02 12:37:46 -07003240 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06003241
3242 // 4. Copy back out an "out" arguments.
3243 lValueCount = 0;
3244 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenich4bf71552016-09-02 11:20:21 -06003245 const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType();
John Kessenich140f3df2015-06-26 16:58:36 -06003246 if (qualifiers[a] != glslang::EvqConstReadOnly) {
3247 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
3248 spv::Id copy = builder.createLoad(spvArgs[a]);
3249 builder.setAccessChain(lValues[lValueCount]);
John Kessenich4bf71552016-09-02 11:20:21 -06003250 multiTypeStore(paramType, copy);
John Kessenich140f3df2015-06-26 16:58:36 -06003251 }
3252 ++lValueCount;
3253 }
3254 }
3255
3256 return result;
3257}
3258
3259// Translate AST operation to SPV operation, already having SPV-based operands/types.
qining25262b32016-05-06 17:25:16 -04003260spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision,
3261 spv::Decoration noContraction,
John Kessenich140f3df2015-06-26 16:58:36 -06003262 spv::Id typeId, spv::Id left, spv::Id right,
3263 glslang::TBasicType typeProxy, bool reduceComparison)
3264{
Rex Xu8ff43de2016-04-22 16:51:45 +08003265 bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003266#ifdef AMD_EXTENSIONS
3267 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16;
3268#else
John Kessenich140f3df2015-06-26 16:58:36 -06003269 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003270#endif
Rex Xuc7d36562016-04-27 08:15:37 +08003271 bool isBool = typeProxy == glslang::EbtBool;
John Kessenich140f3df2015-06-26 16:58:36 -06003272
3273 spv::Op binOp = spv::OpNop;
John Kessenichec43d0a2015-07-04 17:17:31 -06003274 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
John Kessenich140f3df2015-06-26 16:58:36 -06003275 bool comparison = false;
3276
3277 switch (op) {
3278 case glslang::EOpAdd:
3279 case glslang::EOpAddAssign:
3280 if (isFloat)
3281 binOp = spv::OpFAdd;
3282 else
3283 binOp = spv::OpIAdd;
3284 break;
3285 case glslang::EOpSub:
3286 case glslang::EOpSubAssign:
3287 if (isFloat)
3288 binOp = spv::OpFSub;
3289 else
3290 binOp = spv::OpISub;
3291 break;
3292 case glslang::EOpMul:
3293 case glslang::EOpMulAssign:
3294 if (isFloat)
3295 binOp = spv::OpFMul;
3296 else
3297 binOp = spv::OpIMul;
3298 break;
3299 case glslang::EOpVectorTimesScalar:
3300 case glslang::EOpVectorTimesScalarAssign:
John Kessenich8d72f1a2016-05-20 12:06:03 -06003301 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
John Kessenichec43d0a2015-07-04 17:17:31 -06003302 if (builder.isVector(right))
3303 std::swap(left, right);
3304 assert(builder.isScalar(right));
3305 needMatchingVectors = false;
3306 binOp = spv::OpVectorTimesScalar;
3307 } else
3308 binOp = spv::OpIMul;
John Kessenich140f3df2015-06-26 16:58:36 -06003309 break;
3310 case glslang::EOpVectorTimesMatrix:
3311 case glslang::EOpVectorTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06003312 binOp = spv::OpVectorTimesMatrix;
3313 break;
3314 case glslang::EOpMatrixTimesVector:
John Kessenich140f3df2015-06-26 16:58:36 -06003315 binOp = spv::OpMatrixTimesVector;
3316 break;
3317 case glslang::EOpMatrixTimesScalar:
3318 case glslang::EOpMatrixTimesScalarAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06003319 binOp = spv::OpMatrixTimesScalar;
3320 break;
3321 case glslang::EOpMatrixTimesMatrix:
3322 case glslang::EOpMatrixTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06003323 binOp = spv::OpMatrixTimesMatrix;
3324 break;
3325 case glslang::EOpOuterProduct:
3326 binOp = spv::OpOuterProduct;
John Kessenichec43d0a2015-07-04 17:17:31 -06003327 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06003328 break;
3329
3330 case glslang::EOpDiv:
3331 case glslang::EOpDivAssign:
3332 if (isFloat)
3333 binOp = spv::OpFDiv;
3334 else if (isUnsigned)
3335 binOp = spv::OpUDiv;
3336 else
3337 binOp = spv::OpSDiv;
3338 break;
3339 case glslang::EOpMod:
3340 case glslang::EOpModAssign:
3341 if (isFloat)
3342 binOp = spv::OpFMod;
3343 else if (isUnsigned)
3344 binOp = spv::OpUMod;
3345 else
3346 binOp = spv::OpSMod;
3347 break;
3348 case glslang::EOpRightShift:
3349 case glslang::EOpRightShiftAssign:
3350 if (isUnsigned)
3351 binOp = spv::OpShiftRightLogical;
3352 else
3353 binOp = spv::OpShiftRightArithmetic;
3354 break;
3355 case glslang::EOpLeftShift:
3356 case glslang::EOpLeftShiftAssign:
3357 binOp = spv::OpShiftLeftLogical;
3358 break;
3359 case glslang::EOpAnd:
3360 case glslang::EOpAndAssign:
3361 binOp = spv::OpBitwiseAnd;
3362 break;
3363 case glslang::EOpLogicalAnd:
John Kessenichec43d0a2015-07-04 17:17:31 -06003364 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06003365 binOp = spv::OpLogicalAnd;
3366 break;
3367 case glslang::EOpInclusiveOr:
3368 case glslang::EOpInclusiveOrAssign:
3369 binOp = spv::OpBitwiseOr;
3370 break;
3371 case glslang::EOpLogicalOr:
John Kessenichec43d0a2015-07-04 17:17:31 -06003372 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06003373 binOp = spv::OpLogicalOr;
3374 break;
3375 case glslang::EOpExclusiveOr:
3376 case glslang::EOpExclusiveOrAssign:
3377 binOp = spv::OpBitwiseXor;
3378 break;
3379 case glslang::EOpLogicalXor:
John Kessenichec43d0a2015-07-04 17:17:31 -06003380 needMatchingVectors = false;
John Kessenich5e4b1242015-08-06 22:53:06 -06003381 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06003382 break;
3383
3384 case glslang::EOpLessThan:
3385 case glslang::EOpGreaterThan:
3386 case glslang::EOpLessThanEqual:
3387 case glslang::EOpGreaterThanEqual:
3388 case glslang::EOpEqual:
3389 case glslang::EOpNotEqual:
3390 case glslang::EOpVectorEqual:
3391 case glslang::EOpVectorNotEqual:
3392 comparison = true;
3393 break;
3394 default:
3395 break;
3396 }
3397
John Kessenich7c1aa102015-10-15 13:29:11 -06003398 // handle mapped binary operations (should be non-comparison)
John Kessenich140f3df2015-06-26 16:58:36 -06003399 if (binOp != spv::OpNop) {
John Kessenich7c1aa102015-10-15 13:29:11 -06003400 assert(comparison == false);
John Kessenich04bb8a02015-12-12 12:28:14 -07003401 if (builder.isMatrix(left) || builder.isMatrix(right))
qining25262b32016-05-06 17:25:16 -04003402 return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06003403
3404 // No matrix involved; make both operands be the same number of components, if needed
John Kessenichec43d0a2015-07-04 17:17:31 -06003405 if (needMatchingVectors)
John Kessenich140f3df2015-06-26 16:58:36 -06003406 builder.promoteScalar(precision, left, right);
3407
qining25262b32016-05-06 17:25:16 -04003408 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
3409 addDecoration(result, noContraction);
3410 return builder.setPrecision(result, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06003411 }
3412
3413 if (! comparison)
3414 return 0;
3415
John Kessenich7c1aa102015-10-15 13:29:11 -06003416 // Handle comparison instructions
John Kessenich140f3df2015-06-26 16:58:36 -06003417
John Kessenich4583b612016-08-07 19:14:22 -06003418 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
3419 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left)))
John Kessenich22118352015-12-21 20:54:09 -07003420 return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual);
John Kessenich140f3df2015-06-26 16:58:36 -06003421
3422 switch (op) {
3423 case glslang::EOpLessThan:
3424 if (isFloat)
3425 binOp = spv::OpFOrdLessThan;
3426 else if (isUnsigned)
3427 binOp = spv::OpULessThan;
3428 else
3429 binOp = spv::OpSLessThan;
3430 break;
3431 case glslang::EOpGreaterThan:
3432 if (isFloat)
3433 binOp = spv::OpFOrdGreaterThan;
3434 else if (isUnsigned)
3435 binOp = spv::OpUGreaterThan;
3436 else
3437 binOp = spv::OpSGreaterThan;
3438 break;
3439 case glslang::EOpLessThanEqual:
3440 if (isFloat)
3441 binOp = spv::OpFOrdLessThanEqual;
3442 else if (isUnsigned)
3443 binOp = spv::OpULessThanEqual;
3444 else
3445 binOp = spv::OpSLessThanEqual;
3446 break;
3447 case glslang::EOpGreaterThanEqual:
3448 if (isFloat)
3449 binOp = spv::OpFOrdGreaterThanEqual;
3450 else if (isUnsigned)
3451 binOp = spv::OpUGreaterThanEqual;
3452 else
3453 binOp = spv::OpSGreaterThanEqual;
3454 break;
3455 case glslang::EOpEqual:
3456 case glslang::EOpVectorEqual:
3457 if (isFloat)
3458 binOp = spv::OpFOrdEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08003459 else if (isBool)
3460 binOp = spv::OpLogicalEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06003461 else
3462 binOp = spv::OpIEqual;
3463 break;
3464 case glslang::EOpNotEqual:
3465 case glslang::EOpVectorNotEqual:
3466 if (isFloat)
3467 binOp = spv::OpFOrdNotEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08003468 else if (isBool)
3469 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06003470 else
3471 binOp = spv::OpINotEqual;
3472 break;
3473 default:
3474 break;
3475 }
3476
qining25262b32016-05-06 17:25:16 -04003477 if (binOp != spv::OpNop) {
3478 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
3479 addDecoration(result, noContraction);
3480 return builder.setPrecision(result, precision);
3481 }
John Kessenich140f3df2015-06-26 16:58:36 -06003482
3483 return 0;
3484}
3485
John Kessenich04bb8a02015-12-12 12:28:14 -07003486//
3487// Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
3488// These can be any of:
3489//
3490// matrix * scalar
3491// scalar * matrix
3492// matrix * matrix linear algebraic
3493// matrix * vector
3494// vector * matrix
3495// matrix * matrix componentwise
3496// matrix op matrix op in {+, -, /}
3497// matrix op scalar op in {+, -, /}
3498// scalar op matrix op in {+, -, /}
3499//
qining25262b32016-05-06 17:25:16 -04003500spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right)
John Kessenich04bb8a02015-12-12 12:28:14 -07003501{
3502 bool firstClass = true;
3503
3504 // First, handle first-class matrix operations (* and matrix/scalar)
3505 switch (op) {
3506 case spv::OpFDiv:
3507 if (builder.isMatrix(left) && builder.isScalar(right)) {
3508 // turn matrix / scalar into a multiply...
3509 right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right);
3510 op = spv::OpMatrixTimesScalar;
3511 } else
3512 firstClass = false;
3513 break;
3514 case spv::OpMatrixTimesScalar:
3515 if (builder.isMatrix(right))
3516 std::swap(left, right);
3517 assert(builder.isScalar(right));
3518 break;
3519 case spv::OpVectorTimesMatrix:
3520 assert(builder.isVector(left));
3521 assert(builder.isMatrix(right));
3522 break;
3523 case spv::OpMatrixTimesVector:
3524 assert(builder.isMatrix(left));
3525 assert(builder.isVector(right));
3526 break;
3527 case spv::OpMatrixTimesMatrix:
3528 assert(builder.isMatrix(left));
3529 assert(builder.isMatrix(right));
3530 break;
3531 default:
3532 firstClass = false;
3533 break;
3534 }
3535
qining25262b32016-05-06 17:25:16 -04003536 if (firstClass) {
3537 spv::Id result = builder.createBinOp(op, typeId, left, right);
3538 addDecoration(result, noContraction);
3539 return builder.setPrecision(result, precision);
3540 }
John Kessenich04bb8a02015-12-12 12:28:14 -07003541
LoopDawg592860c2016-06-09 08:57:35 -06003542 // Handle component-wise +, -, *, %, and / for all combinations of type.
John Kessenich04bb8a02015-12-12 12:28:14 -07003543 // The result type of all of them is the same type as the (a) matrix operand.
3544 // The algorithm is to:
3545 // - break the matrix(es) into vectors
3546 // - smear any scalar to a vector
3547 // - do vector operations
3548 // - make a matrix out the vector results
3549 switch (op) {
3550 case spv::OpFAdd:
3551 case spv::OpFSub:
3552 case spv::OpFDiv:
LoopDawg592860c2016-06-09 08:57:35 -06003553 case spv::OpFMod:
John Kessenich04bb8a02015-12-12 12:28:14 -07003554 case spv::OpFMul:
3555 {
3556 // one time set up...
3557 bool leftMat = builder.isMatrix(left);
3558 bool rightMat = builder.isMatrix(right);
3559 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
3560 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
3561 spv::Id scalarType = builder.getScalarTypeId(typeId);
3562 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
3563 std::vector<spv::Id> results;
3564 spv::Id smearVec = spv::NoResult;
3565 if (builder.isScalar(left))
3566 smearVec = builder.smearScalar(precision, left, vecType);
3567 else if (builder.isScalar(right))
3568 smearVec = builder.smearScalar(precision, right, vecType);
3569
3570 // do each vector op
3571 for (unsigned int c = 0; c < numCols; ++c) {
3572 std::vector<unsigned int> indexes;
3573 indexes.push_back(c);
3574 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
3575 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
qining25262b32016-05-06 17:25:16 -04003576 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
3577 addDecoration(result, noContraction);
3578 results.push_back(builder.setPrecision(result, precision));
John Kessenich04bb8a02015-12-12 12:28:14 -07003579 }
3580
3581 // put the pieces together
John Kessenich32cfd492016-02-02 12:37:46 -07003582 return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision);
John Kessenich04bb8a02015-12-12 12:28:14 -07003583 }
3584 default:
3585 assert(0);
3586 return spv::NoResult;
3587 }
3588}
3589
qining25262b32016-05-06 17:25:16 -04003590spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06003591{
3592 spv::Op unaryOp = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08003593 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06003594 int libCall = -1;
Rex Xu8ff43de2016-04-22 16:51:45 +08003595 bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003596#ifdef AMD_EXTENSIONS
3597 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16;
3598#else
Rex Xu04db3f52015-09-16 11:44:02 +08003599 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003600#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003601
3602 switch (op) {
3603 case glslang::EOpNegative:
John Kessenich7a53f762016-01-20 11:19:27 -07003604 if (isFloat) {
John Kessenich140f3df2015-06-26 16:58:36 -06003605 unaryOp = spv::OpFNegate;
John Kessenich7a53f762016-01-20 11:19:27 -07003606 if (builder.isMatrixType(typeId))
qining25262b32016-05-06 17:25:16 -04003607 return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy);
John Kessenich7a53f762016-01-20 11:19:27 -07003608 } else
John Kessenich140f3df2015-06-26 16:58:36 -06003609 unaryOp = spv::OpSNegate;
3610 break;
3611
3612 case glslang::EOpLogicalNot:
3613 case glslang::EOpVectorLogicalNot:
John Kessenich5e4b1242015-08-06 22:53:06 -06003614 unaryOp = spv::OpLogicalNot;
3615 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003616 case glslang::EOpBitwiseNot:
3617 unaryOp = spv::OpNot;
3618 break;
John Kessenich5e4b1242015-08-06 22:53:06 -06003619
John Kessenich140f3df2015-06-26 16:58:36 -06003620 case glslang::EOpDeterminant:
John Kessenich5e4b1242015-08-06 22:53:06 -06003621 libCall = spv::GLSLstd450Determinant;
John Kessenich140f3df2015-06-26 16:58:36 -06003622 break;
3623 case glslang::EOpMatrixInverse:
John Kessenich5e4b1242015-08-06 22:53:06 -06003624 libCall = spv::GLSLstd450MatrixInverse;
John Kessenich140f3df2015-06-26 16:58:36 -06003625 break;
3626 case glslang::EOpTranspose:
3627 unaryOp = spv::OpTranspose;
3628 break;
3629
3630 case glslang::EOpRadians:
John Kessenich5e4b1242015-08-06 22:53:06 -06003631 libCall = spv::GLSLstd450Radians;
John Kessenich140f3df2015-06-26 16:58:36 -06003632 break;
3633 case glslang::EOpDegrees:
John Kessenich5e4b1242015-08-06 22:53:06 -06003634 libCall = spv::GLSLstd450Degrees;
John Kessenich140f3df2015-06-26 16:58:36 -06003635 break;
3636 case glslang::EOpSin:
John Kessenich5e4b1242015-08-06 22:53:06 -06003637 libCall = spv::GLSLstd450Sin;
John Kessenich140f3df2015-06-26 16:58:36 -06003638 break;
3639 case glslang::EOpCos:
John Kessenich5e4b1242015-08-06 22:53:06 -06003640 libCall = spv::GLSLstd450Cos;
John Kessenich140f3df2015-06-26 16:58:36 -06003641 break;
3642 case glslang::EOpTan:
John Kessenich5e4b1242015-08-06 22:53:06 -06003643 libCall = spv::GLSLstd450Tan;
John Kessenich140f3df2015-06-26 16:58:36 -06003644 break;
3645 case glslang::EOpAcos:
John Kessenich5e4b1242015-08-06 22:53:06 -06003646 libCall = spv::GLSLstd450Acos;
John Kessenich140f3df2015-06-26 16:58:36 -06003647 break;
3648 case glslang::EOpAsin:
John Kessenich5e4b1242015-08-06 22:53:06 -06003649 libCall = spv::GLSLstd450Asin;
John Kessenich140f3df2015-06-26 16:58:36 -06003650 break;
3651 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06003652 libCall = spv::GLSLstd450Atan;
John Kessenich140f3df2015-06-26 16:58:36 -06003653 break;
3654
3655 case glslang::EOpAcosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003656 libCall = spv::GLSLstd450Acosh;
John Kessenich140f3df2015-06-26 16:58:36 -06003657 break;
3658 case glslang::EOpAsinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003659 libCall = spv::GLSLstd450Asinh;
John Kessenich140f3df2015-06-26 16:58:36 -06003660 break;
3661 case glslang::EOpAtanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003662 libCall = spv::GLSLstd450Atanh;
John Kessenich140f3df2015-06-26 16:58:36 -06003663 break;
3664 case glslang::EOpTanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003665 libCall = spv::GLSLstd450Tanh;
John Kessenich140f3df2015-06-26 16:58:36 -06003666 break;
3667 case glslang::EOpCosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003668 libCall = spv::GLSLstd450Cosh;
John Kessenich140f3df2015-06-26 16:58:36 -06003669 break;
3670 case glslang::EOpSinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06003671 libCall = spv::GLSLstd450Sinh;
John Kessenich140f3df2015-06-26 16:58:36 -06003672 break;
3673
3674 case glslang::EOpLength:
John Kessenich5e4b1242015-08-06 22:53:06 -06003675 libCall = spv::GLSLstd450Length;
John Kessenich140f3df2015-06-26 16:58:36 -06003676 break;
3677 case glslang::EOpNormalize:
John Kessenich5e4b1242015-08-06 22:53:06 -06003678 libCall = spv::GLSLstd450Normalize;
John Kessenich140f3df2015-06-26 16:58:36 -06003679 break;
3680
3681 case glslang::EOpExp:
John Kessenich5e4b1242015-08-06 22:53:06 -06003682 libCall = spv::GLSLstd450Exp;
John Kessenich140f3df2015-06-26 16:58:36 -06003683 break;
3684 case glslang::EOpLog:
John Kessenich5e4b1242015-08-06 22:53:06 -06003685 libCall = spv::GLSLstd450Log;
John Kessenich140f3df2015-06-26 16:58:36 -06003686 break;
3687 case glslang::EOpExp2:
John Kessenich5e4b1242015-08-06 22:53:06 -06003688 libCall = spv::GLSLstd450Exp2;
John Kessenich140f3df2015-06-26 16:58:36 -06003689 break;
3690 case glslang::EOpLog2:
John Kessenich5e4b1242015-08-06 22:53:06 -06003691 libCall = spv::GLSLstd450Log2;
John Kessenich140f3df2015-06-26 16:58:36 -06003692 break;
3693 case glslang::EOpSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06003694 libCall = spv::GLSLstd450Sqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06003695 break;
3696 case glslang::EOpInverseSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06003697 libCall = spv::GLSLstd450InverseSqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06003698 break;
3699
3700 case glslang::EOpFloor:
John Kessenich5e4b1242015-08-06 22:53:06 -06003701 libCall = spv::GLSLstd450Floor;
John Kessenich140f3df2015-06-26 16:58:36 -06003702 break;
3703 case glslang::EOpTrunc:
John Kessenich5e4b1242015-08-06 22:53:06 -06003704 libCall = spv::GLSLstd450Trunc;
John Kessenich140f3df2015-06-26 16:58:36 -06003705 break;
3706 case glslang::EOpRound:
John Kessenich5e4b1242015-08-06 22:53:06 -06003707 libCall = spv::GLSLstd450Round;
John Kessenich140f3df2015-06-26 16:58:36 -06003708 break;
3709 case glslang::EOpRoundEven:
John Kessenich5e4b1242015-08-06 22:53:06 -06003710 libCall = spv::GLSLstd450RoundEven;
John Kessenich140f3df2015-06-26 16:58:36 -06003711 break;
3712 case glslang::EOpCeil:
John Kessenich5e4b1242015-08-06 22:53:06 -06003713 libCall = spv::GLSLstd450Ceil;
John Kessenich140f3df2015-06-26 16:58:36 -06003714 break;
3715 case glslang::EOpFract:
John Kessenich5e4b1242015-08-06 22:53:06 -06003716 libCall = spv::GLSLstd450Fract;
John Kessenich140f3df2015-06-26 16:58:36 -06003717 break;
3718
3719 case glslang::EOpIsNan:
3720 unaryOp = spv::OpIsNan;
3721 break;
3722 case glslang::EOpIsInf:
3723 unaryOp = spv::OpIsInf;
3724 break;
LoopDawg592860c2016-06-09 08:57:35 -06003725 case glslang::EOpIsFinite:
3726 unaryOp = spv::OpIsFinite;
3727 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003728
Rex Xucbc426e2015-12-15 16:03:10 +08003729 case glslang::EOpFloatBitsToInt:
3730 case glslang::EOpFloatBitsToUint:
3731 case glslang::EOpIntBitsToFloat:
3732 case glslang::EOpUintBitsToFloat:
Rex Xu8ff43de2016-04-22 16:51:45 +08003733 case glslang::EOpDoubleBitsToInt64:
3734 case glslang::EOpDoubleBitsToUint64:
3735 case glslang::EOpInt64BitsToDouble:
3736 case glslang::EOpUint64BitsToDouble:
Rex Xucbc426e2015-12-15 16:03:10 +08003737 unaryOp = spv::OpBitcast;
3738 break;
3739
John Kessenich140f3df2015-06-26 16:58:36 -06003740 case glslang::EOpPackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003741 libCall = spv::GLSLstd450PackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003742 break;
3743 case glslang::EOpUnpackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003744 libCall = spv::GLSLstd450UnpackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003745 break;
3746 case glslang::EOpPackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003747 libCall = spv::GLSLstd450PackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003748 break;
3749 case glslang::EOpUnpackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003750 libCall = spv::GLSLstd450UnpackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003751 break;
3752 case glslang::EOpPackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003753 libCall = spv::GLSLstd450PackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003754 break;
3755 case glslang::EOpUnpackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06003756 libCall = spv::GLSLstd450UnpackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06003757 break;
John Kessenichfc51d282015-08-19 13:34:18 -06003758 case glslang::EOpPackSnorm4x8:
3759 libCall = spv::GLSLstd450PackSnorm4x8;
3760 break;
3761 case glslang::EOpUnpackSnorm4x8:
3762 libCall = spv::GLSLstd450UnpackSnorm4x8;
3763 break;
3764 case glslang::EOpPackUnorm4x8:
3765 libCall = spv::GLSLstd450PackUnorm4x8;
3766 break;
3767 case glslang::EOpUnpackUnorm4x8:
3768 libCall = spv::GLSLstd450UnpackUnorm4x8;
3769 break;
3770 case glslang::EOpPackDouble2x32:
3771 libCall = spv::GLSLstd450PackDouble2x32;
3772 break;
3773 case glslang::EOpUnpackDouble2x32:
3774 libCall = spv::GLSLstd450UnpackDouble2x32;
3775 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003776
Rex Xu8ff43de2016-04-22 16:51:45 +08003777 case glslang::EOpPackInt2x32:
3778 case glslang::EOpUnpackInt2x32:
3779 case glslang::EOpPackUint2x32:
3780 case glslang::EOpUnpackUint2x32:
Rex Xuc9f34922016-09-09 17:50:07 +08003781 unaryOp = spv::OpBitcast;
Rex Xu8ff43de2016-04-22 16:51:45 +08003782 break;
3783
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003784#ifdef AMD_EXTENSIONS
3785 case glslang::EOpPackFloat2x16:
3786 case glslang::EOpUnpackFloat2x16:
3787 unaryOp = spv::OpBitcast;
3788 break;
3789#endif
3790
John Kessenich140f3df2015-06-26 16:58:36 -06003791 case glslang::EOpDPdx:
3792 unaryOp = spv::OpDPdx;
3793 break;
3794 case glslang::EOpDPdy:
3795 unaryOp = spv::OpDPdy;
3796 break;
3797 case glslang::EOpFwidth:
3798 unaryOp = spv::OpFwidth;
3799 break;
3800 case glslang::EOpDPdxFine:
John Kessenich92187592016-02-01 13:45:25 -07003801 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003802 unaryOp = spv::OpDPdxFine;
3803 break;
3804 case glslang::EOpDPdyFine:
John Kessenich92187592016-02-01 13:45:25 -07003805 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003806 unaryOp = spv::OpDPdyFine;
3807 break;
3808 case glslang::EOpFwidthFine:
John Kessenich92187592016-02-01 13:45:25 -07003809 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003810 unaryOp = spv::OpFwidthFine;
3811 break;
3812 case glslang::EOpDPdxCoarse:
John Kessenich92187592016-02-01 13:45:25 -07003813 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003814 unaryOp = spv::OpDPdxCoarse;
3815 break;
3816 case glslang::EOpDPdyCoarse:
John Kessenich92187592016-02-01 13:45:25 -07003817 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003818 unaryOp = spv::OpDPdyCoarse;
3819 break;
3820 case glslang::EOpFwidthCoarse:
John Kessenich92187592016-02-01 13:45:25 -07003821 builder.addCapability(spv::CapabilityDerivativeControl);
John Kessenich140f3df2015-06-26 16:58:36 -06003822 unaryOp = spv::OpFwidthCoarse;
3823 break;
Rex Xu7a26c172015-12-08 17:12:09 +08003824 case glslang::EOpInterpolateAtCentroid:
John Kessenich92187592016-02-01 13:45:25 -07003825 builder.addCapability(spv::CapabilityInterpolationFunction);
Rex Xu7a26c172015-12-08 17:12:09 +08003826 libCall = spv::GLSLstd450InterpolateAtCentroid;
3827 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003828 case glslang::EOpAny:
3829 unaryOp = spv::OpAny;
3830 break;
3831 case glslang::EOpAll:
3832 unaryOp = spv::OpAll;
3833 break;
3834
3835 case glslang::EOpAbs:
John Kessenich5e4b1242015-08-06 22:53:06 -06003836 if (isFloat)
3837 libCall = spv::GLSLstd450FAbs;
3838 else
3839 libCall = spv::GLSLstd450SAbs;
John Kessenich140f3df2015-06-26 16:58:36 -06003840 break;
3841 case glslang::EOpSign:
John Kessenich5e4b1242015-08-06 22:53:06 -06003842 if (isFloat)
3843 libCall = spv::GLSLstd450FSign;
3844 else
3845 libCall = spv::GLSLstd450SSign;
John Kessenich140f3df2015-06-26 16:58:36 -06003846 break;
3847
John Kessenichfc51d282015-08-19 13:34:18 -06003848 case glslang::EOpAtomicCounterIncrement:
3849 case glslang::EOpAtomicCounterDecrement:
3850 case glslang::EOpAtomicCounter:
3851 {
3852 // Handle all of the atomics in one place, in createAtomicOperation()
3853 std::vector<spv::Id> operands;
3854 operands.push_back(operand);
Rex Xu04db3f52015-09-16 11:44:02 +08003855 return createAtomicOperation(op, precision, typeId, operands, typeProxy);
John Kessenichfc51d282015-08-19 13:34:18 -06003856 }
3857
John Kessenichfc51d282015-08-19 13:34:18 -06003858 case glslang::EOpBitFieldReverse:
3859 unaryOp = spv::OpBitReverse;
3860 break;
3861 case glslang::EOpBitCount:
3862 unaryOp = spv::OpBitCount;
3863 break;
3864 case glslang::EOpFindLSB:
John Kessenich55e7d112015-11-15 21:33:39 -07003865 libCall = spv::GLSLstd450FindILsb;
John Kessenichfc51d282015-08-19 13:34:18 -06003866 break;
3867 case glslang::EOpFindMSB:
John Kessenich55e7d112015-11-15 21:33:39 -07003868 if (isUnsigned)
3869 libCall = spv::GLSLstd450FindUMsb;
3870 else
3871 libCall = spv::GLSLstd450FindSMsb;
John Kessenichfc51d282015-08-19 13:34:18 -06003872 break;
3873
Rex Xu574ab042016-04-14 16:53:07 +08003874 case glslang::EOpBallot:
3875 case glslang::EOpReadFirstInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08003876 case glslang::EOpAnyInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08003877 case glslang::EOpAllInvocations:
Rex Xu338b1852016-05-05 20:38:33 +08003878 case glslang::EOpAllInvocationsEqual:
Rex Xu9d93a232016-05-05 12:30:44 +08003879#ifdef AMD_EXTENSIONS
3880 case glslang::EOpMinInvocations:
3881 case glslang::EOpMaxInvocations:
3882 case glslang::EOpAddInvocations:
3883 case glslang::EOpMinInvocationsNonUniform:
3884 case glslang::EOpMaxInvocationsNonUniform:
3885 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08003886 case glslang::EOpMinInvocationsInclusiveScan:
3887 case glslang::EOpMaxInvocationsInclusiveScan:
3888 case glslang::EOpAddInvocationsInclusiveScan:
3889 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
3890 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
3891 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
3892 case glslang::EOpMinInvocationsExclusiveScan:
3893 case glslang::EOpMaxInvocationsExclusiveScan:
3894 case glslang::EOpAddInvocationsExclusiveScan:
3895 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
3896 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
3897 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
Rex Xu9d93a232016-05-05 12:30:44 +08003898#endif
Rex Xu51596642016-09-21 18:56:12 +08003899 {
3900 std::vector<spv::Id> operands;
3901 operands.push_back(operand);
3902 return createInvocationsOperation(op, typeId, operands, typeProxy);
3903 }
Rex Xu9d93a232016-05-05 12:30:44 +08003904
3905#ifdef AMD_EXTENSIONS
3906 case glslang::EOpMbcnt:
3907 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
3908 libCall = spv::MbcntAMD;
3909 break;
3910
3911 case glslang::EOpCubeFaceIndex:
3912 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
3913 libCall = spv::CubeFaceIndexAMD;
3914 break;
3915
3916 case glslang::EOpCubeFaceCoord:
3917 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
3918 libCall = spv::CubeFaceCoordAMD;
3919 break;
3920#endif
Rex Xu338b1852016-05-05 20:38:33 +08003921
John Kessenich140f3df2015-06-26 16:58:36 -06003922 default:
3923 return 0;
3924 }
3925
3926 spv::Id id;
3927 if (libCall >= 0) {
3928 std::vector<spv::Id> args;
3929 args.push_back(operand);
Rex Xu9d93a232016-05-05 12:30:44 +08003930 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
Rex Xu338b1852016-05-05 20:38:33 +08003931 } else {
John Kessenich91cef522016-05-05 16:45:40 -06003932 id = builder.createUnaryOp(unaryOp, typeId, operand);
Rex Xu338b1852016-05-05 20:38:33 +08003933 }
John Kessenich140f3df2015-06-26 16:58:36 -06003934
qining25262b32016-05-06 17:25:16 -04003935 addDecoration(id, noContraction);
John Kessenich32cfd492016-02-02 12:37:46 -07003936 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06003937}
3938
John Kessenich7a53f762016-01-20 11:19:27 -07003939// Create a unary operation on a matrix
qining25262b32016-05-06 17:25:16 -04003940spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand, glslang::TBasicType /* typeProxy */)
John Kessenich7a53f762016-01-20 11:19:27 -07003941{
3942 // Handle unary operations vector by vector.
3943 // The result type is the same type as the original type.
3944 // The algorithm is to:
3945 // - break the matrix into vectors
3946 // - apply the operation to each vector
3947 // - make a matrix out the vector results
3948
3949 // get the types sorted out
3950 int numCols = builder.getNumColumns(operand);
3951 int numRows = builder.getNumRows(operand);
Rex Xuc1992e52016-05-17 18:57:18 +08003952 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
3953 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
John Kessenich7a53f762016-01-20 11:19:27 -07003954 std::vector<spv::Id> results;
3955
3956 // do each vector op
3957 for (int c = 0; c < numCols; ++c) {
3958 std::vector<unsigned int> indexes;
3959 indexes.push_back(c);
Rex Xuc1992e52016-05-17 18:57:18 +08003960 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
3961 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
3962 addDecoration(destVec, noContraction);
3963 results.push_back(builder.setPrecision(destVec, precision));
John Kessenich7a53f762016-01-20 11:19:27 -07003964 }
3965
3966 // put the pieces together
John Kessenich32cfd492016-02-02 12:37:46 -07003967 return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision);
John Kessenich7a53f762016-01-20 11:19:27 -07003968}
3969
Rex Xu73e3ce72016-04-27 18:48:17 +08003970spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id destType, spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06003971{
3972 spv::Op convOp = spv::OpNop;
3973 spv::Id zero = 0;
3974 spv::Id one = 0;
Rex Xu8ff43de2016-04-22 16:51:45 +08003975 spv::Id type = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06003976
3977 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
3978
3979 switch (op) {
3980 case glslang::EOpConvIntToBool:
3981 case glslang::EOpConvUintToBool:
Rex Xu8ff43de2016-04-22 16:51:45 +08003982 case glslang::EOpConvInt64ToBool:
3983 case glslang::EOpConvUint64ToBool:
3984 zero = (op == glslang::EOpConvInt64ToBool ||
3985 op == glslang::EOpConvUint64ToBool) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003986 zero = makeSmearedConstant(zero, vectorSize);
3987 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
3988
3989 case glslang::EOpConvFloatToBool:
3990 zero = builder.makeFloatConstant(0.0F);
3991 zero = makeSmearedConstant(zero, vectorSize);
3992 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
3993
3994 case glslang::EOpConvDoubleToBool:
3995 zero = builder.makeDoubleConstant(0.0);
3996 zero = makeSmearedConstant(zero, vectorSize);
3997 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
3998
Rex Xuc9e3c3c2016-07-29 16:00:05 +08003999#ifdef AMD_EXTENSIONS
4000 case glslang::EOpConvFloat16ToBool:
4001 zero = builder.makeFloat16Constant(0.0F);
4002 zero = makeSmearedConstant(zero, vectorSize);
4003 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
4004#endif
4005
John Kessenich140f3df2015-06-26 16:58:36 -06004006 case glslang::EOpConvBoolToFloat:
4007 convOp = spv::OpSelect;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004008 zero = builder.makeFloatConstant(0.0F);
4009 one = builder.makeFloatConstant(1.0F);
John Kessenich140f3df2015-06-26 16:58:36 -06004010 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004011
John Kessenich140f3df2015-06-26 16:58:36 -06004012 case glslang::EOpConvBoolToDouble:
4013 convOp = spv::OpSelect;
4014 zero = builder.makeDoubleConstant(0.0);
4015 one = builder.makeDoubleConstant(1.0);
4016 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004017
4018#ifdef AMD_EXTENSIONS
4019 case glslang::EOpConvBoolToFloat16:
4020 convOp = spv::OpSelect;
4021 zero = builder.makeFloat16Constant(0.0F);
4022 one = builder.makeFloat16Constant(1.0F);
4023 break;
4024#endif
4025
John Kessenich140f3df2015-06-26 16:58:36 -06004026 case glslang::EOpConvBoolToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08004027 case glslang::EOpConvBoolToInt64:
4028 zero = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(0) : builder.makeIntConstant(0);
4029 one = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(1) : builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06004030 convOp = spv::OpSelect;
4031 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004032
John Kessenich140f3df2015-06-26 16:58:36 -06004033 case glslang::EOpConvBoolToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08004034 case glslang::EOpConvBoolToUint64:
4035 zero = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0);
4036 one = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(1) : builder.makeUintConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06004037 convOp = spv::OpSelect;
4038 break;
4039
4040 case glslang::EOpConvIntToFloat:
4041 case glslang::EOpConvIntToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08004042 case glslang::EOpConvInt64ToFloat:
4043 case glslang::EOpConvInt64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004044#ifdef AMD_EXTENSIONS
4045 case glslang::EOpConvIntToFloat16:
4046 case glslang::EOpConvInt64ToFloat16:
4047#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004048 convOp = spv::OpConvertSToF;
4049 break;
4050
4051 case glslang::EOpConvUintToFloat:
4052 case glslang::EOpConvUintToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08004053 case glslang::EOpConvUint64ToFloat:
4054 case glslang::EOpConvUint64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004055#ifdef AMD_EXTENSIONS
4056 case glslang::EOpConvUintToFloat16:
4057 case glslang::EOpConvUint64ToFloat16:
4058#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004059 convOp = spv::OpConvertUToF;
4060 break;
4061
4062 case glslang::EOpConvDoubleToFloat:
4063 case glslang::EOpConvFloatToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004064#ifdef AMD_EXTENSIONS
4065 case glslang::EOpConvDoubleToFloat16:
4066 case glslang::EOpConvFloat16ToDouble:
4067 case glslang::EOpConvFloatToFloat16:
4068 case glslang::EOpConvFloat16ToFloat:
4069#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004070 convOp = spv::OpFConvert;
Rex Xu73e3ce72016-04-27 18:48:17 +08004071 if (builder.isMatrixType(destType))
4072 return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06004073 break;
4074
4075 case glslang::EOpConvFloatToInt:
4076 case glslang::EOpConvDoubleToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08004077 case glslang::EOpConvFloatToInt64:
4078 case glslang::EOpConvDoubleToInt64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004079#ifdef AMD_EXTENSIONS
4080 case glslang::EOpConvFloat16ToInt:
4081 case glslang::EOpConvFloat16ToInt64:
4082#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004083 convOp = spv::OpConvertFToS;
4084 break;
4085
4086 case glslang::EOpConvUintToInt:
4087 case glslang::EOpConvIntToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08004088 case glslang::EOpConvUint64ToInt64:
4089 case glslang::EOpConvInt64ToUint64:
qininge24aa5e2016-04-07 15:40:27 -04004090 if (builder.isInSpecConstCodeGenMode()) {
4091 // Build zero scalar or vector for OpIAdd.
Rex Xu64bcfdb2016-09-05 16:10:14 +08004092 zero = (op == glslang::EOpConvUint64ToInt64 ||
4093 op == glslang::EOpConvInt64ToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0);
qining189b2032016-04-12 23:16:20 -04004094 zero = makeSmearedConstant(zero, vectorSize);
qininge24aa5e2016-04-07 15:40:27 -04004095 // Use OpIAdd, instead of OpBitcast to do the conversion when
4096 // generating for OpSpecConstantOp instruction.
4097 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
4098 }
4099 // For normal run-time conversion instruction, use OpBitcast.
John Kessenich140f3df2015-06-26 16:58:36 -06004100 convOp = spv::OpBitcast;
4101 break;
4102
4103 case glslang::EOpConvFloatToUint:
4104 case glslang::EOpConvDoubleToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08004105 case glslang::EOpConvFloatToUint64:
4106 case glslang::EOpConvDoubleToUint64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004107#ifdef AMD_EXTENSIONS
4108 case glslang::EOpConvFloat16ToUint:
4109 case glslang::EOpConvFloat16ToUint64:
4110#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004111 convOp = spv::OpConvertFToU;
4112 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08004113
4114 case glslang::EOpConvIntToInt64:
4115 case glslang::EOpConvInt64ToInt:
4116 convOp = spv::OpSConvert;
4117 break;
4118
4119 case glslang::EOpConvUintToUint64:
4120 case glslang::EOpConvUint64ToUint:
4121 convOp = spv::OpUConvert;
4122 break;
4123
4124 case glslang::EOpConvIntToUint64:
4125 case glslang::EOpConvInt64ToUint:
4126 case glslang::EOpConvUint64ToInt:
4127 case glslang::EOpConvUintToInt64:
4128 // OpSConvert/OpUConvert + OpBitCast
4129 switch (op) {
4130 case glslang::EOpConvIntToUint64:
4131 convOp = spv::OpSConvert;
4132 type = builder.makeIntType(64);
4133 break;
4134 case glslang::EOpConvInt64ToUint:
4135 convOp = spv::OpSConvert;
4136 type = builder.makeIntType(32);
4137 break;
4138 case glslang::EOpConvUint64ToInt:
4139 convOp = spv::OpUConvert;
4140 type = builder.makeUintType(32);
4141 break;
4142 case glslang::EOpConvUintToInt64:
4143 convOp = spv::OpUConvert;
4144 type = builder.makeUintType(64);
4145 break;
4146 default:
4147 assert(0);
4148 break;
4149 }
4150
4151 if (vectorSize > 0)
4152 type = builder.makeVectorType(type, vectorSize);
4153
4154 operand = builder.createUnaryOp(convOp, type, operand);
4155
4156 if (builder.isInSpecConstCodeGenMode()) {
4157 // Build zero scalar or vector for OpIAdd.
4158 zero = (op == glslang::EOpConvIntToUint64 ||
4159 op == glslang::EOpConvUintToInt64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0);
4160 zero = makeSmearedConstant(zero, vectorSize);
4161 // Use OpIAdd, instead of OpBitcast to do the conversion when
4162 // generating for OpSpecConstantOp instruction.
4163 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
4164 }
4165 // For normal run-time conversion instruction, use OpBitcast.
4166 convOp = spv::OpBitcast;
4167 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004168 default:
4169 break;
4170 }
4171
4172 spv::Id result = 0;
4173 if (convOp == spv::OpNop)
4174 return result;
4175
4176 if (convOp == spv::OpSelect) {
4177 zero = makeSmearedConstant(zero, vectorSize);
4178 one = makeSmearedConstant(one, vectorSize);
4179 result = builder.createTriOp(convOp, destType, operand, one, zero);
4180 } else
4181 result = builder.createUnaryOp(convOp, destType, operand);
4182
John Kessenich32cfd492016-02-02 12:37:46 -07004183 return builder.setPrecision(result, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06004184}
4185
4186spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
4187{
4188 if (vectorSize == 0)
4189 return constant;
4190
4191 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
4192 std::vector<spv::Id> components;
4193 for (int c = 0; c < vectorSize; ++c)
4194 components.push_back(constant);
4195 return builder.makeCompositeConstant(vectorTypeId, components);
4196}
4197
John Kessenich426394d2015-07-23 10:22:48 -06004198// For glslang ops that map to SPV atomic opCodes
John Kessenich6c292d32016-02-15 20:58:50 -07004199spv::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 -06004200{
4201 spv::Op opCode = spv::OpNop;
4202
4203 switch (op) {
4204 case glslang::EOpAtomicAdd:
Rex Xufc618912015-09-09 16:42:49 +08004205 case glslang::EOpImageAtomicAdd:
John Kessenich426394d2015-07-23 10:22:48 -06004206 opCode = spv::OpAtomicIAdd;
4207 break;
4208 case glslang::EOpAtomicMin:
Rex Xufc618912015-09-09 16:42:49 +08004209 case glslang::EOpImageAtomicMin:
Rex Xu04db3f52015-09-16 11:44:02 +08004210 opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMin : spv::OpAtomicSMin;
John Kessenich426394d2015-07-23 10:22:48 -06004211 break;
4212 case glslang::EOpAtomicMax:
Rex Xufc618912015-09-09 16:42:49 +08004213 case glslang::EOpImageAtomicMax:
Rex Xu04db3f52015-09-16 11:44:02 +08004214 opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMax : spv::OpAtomicSMax;
John Kessenich426394d2015-07-23 10:22:48 -06004215 break;
4216 case glslang::EOpAtomicAnd:
Rex Xufc618912015-09-09 16:42:49 +08004217 case glslang::EOpImageAtomicAnd:
John Kessenich426394d2015-07-23 10:22:48 -06004218 opCode = spv::OpAtomicAnd;
4219 break;
4220 case glslang::EOpAtomicOr:
Rex Xufc618912015-09-09 16:42:49 +08004221 case glslang::EOpImageAtomicOr:
John Kessenich426394d2015-07-23 10:22:48 -06004222 opCode = spv::OpAtomicOr;
4223 break;
4224 case glslang::EOpAtomicXor:
Rex Xufc618912015-09-09 16:42:49 +08004225 case glslang::EOpImageAtomicXor:
John Kessenich426394d2015-07-23 10:22:48 -06004226 opCode = spv::OpAtomicXor;
4227 break;
4228 case glslang::EOpAtomicExchange:
Rex Xufc618912015-09-09 16:42:49 +08004229 case glslang::EOpImageAtomicExchange:
John Kessenich426394d2015-07-23 10:22:48 -06004230 opCode = spv::OpAtomicExchange;
4231 break;
4232 case glslang::EOpAtomicCompSwap:
Rex Xufc618912015-09-09 16:42:49 +08004233 case glslang::EOpImageAtomicCompSwap:
John Kessenich426394d2015-07-23 10:22:48 -06004234 opCode = spv::OpAtomicCompareExchange;
4235 break;
4236 case glslang::EOpAtomicCounterIncrement:
4237 opCode = spv::OpAtomicIIncrement;
4238 break;
4239 case glslang::EOpAtomicCounterDecrement:
4240 opCode = spv::OpAtomicIDecrement;
4241 break;
4242 case glslang::EOpAtomicCounter:
4243 opCode = spv::OpAtomicLoad;
4244 break;
4245 default:
John Kessenich55e7d112015-11-15 21:33:39 -07004246 assert(0);
John Kessenich426394d2015-07-23 10:22:48 -06004247 break;
4248 }
4249
4250 // Sort out the operands
4251 // - mapping from glslang -> SPV
4252 // - there are extra SPV operands with no glslang source
John Kessenich3e60a6f2015-09-14 22:45:16 -06004253 // - compare-exchange swaps the value and comparator
4254 // - compare-exchange has an extra memory semantics
John Kessenich426394d2015-07-23 10:22:48 -06004255 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
4256 auto opIt = operands.begin(); // walk the glslang operands
4257 spvAtomicOperands.push_back(*(opIt++));
Rex Xu04db3f52015-09-16 11:44:02 +08004258 spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope?
4259 spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics?
4260 if (opCode == spv::OpAtomicCompareExchange) {
Rex Xubba5c802015-09-16 13:20:37 +08004261 // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL
4262 // differs from that in SPIR-V. Hence, special processing is required.
Rex Xu04db3f52015-09-16 11:44:02 +08004263 spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone));
John Kessenich3e60a6f2015-09-14 22:45:16 -06004264 spvAtomicOperands.push_back(*(opIt + 1));
4265 spvAtomicOperands.push_back(*opIt);
4266 opIt += 2;
Rex Xu04db3f52015-09-16 11:44:02 +08004267 }
John Kessenich426394d2015-07-23 10:22:48 -06004268
John Kessenich3e60a6f2015-09-14 22:45:16 -06004269 // Add the rest of the operands, skipping any that were dealt with above.
John Kessenich426394d2015-07-23 10:22:48 -06004270 for (; opIt != operands.end(); ++opIt)
4271 spvAtomicOperands.push_back(*opIt);
4272
4273 return builder.createOp(opCode, typeId, spvAtomicOperands);
4274}
4275
John Kessenich91cef522016-05-05 16:45:40 -06004276// Create group invocation operations.
Rex Xu51596642016-09-21 18:56:12 +08004277spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich91cef522016-05-05 16:45:40 -06004278{
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004279#ifdef AMD_EXTENSIONS
Jamie Madill57cb69a2016-11-09 13:49:24 -05004280 bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004281 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004282#endif
Rex Xu9d93a232016-05-05 12:30:44 +08004283
Rex Xu51596642016-09-21 18:56:12 +08004284 spv::Op opCode = spv::OpNop;
Rex Xu51596642016-09-21 18:56:12 +08004285 std::vector<spv::Id> spvGroupOperands;
Rex Xu430ef402016-10-14 17:22:23 +08004286 spv::GroupOperation groupOperation = spv::GroupOperationMax;
4287
chaocf200da82016-12-20 12:44:35 -08004288 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
4289 op == glslang::EOpReadInvocation) {
Rex Xu51596642016-09-21 18:56:12 +08004290 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
4291 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08004292 } else if (op == glslang::EOpAnyInvocation ||
4293 op == glslang::EOpAllInvocations ||
4294 op == glslang::EOpAllInvocationsEqual) {
4295 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
4296 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
Rex Xu51596642016-09-21 18:56:12 +08004297 } else {
4298 builder.addCapability(spv::CapabilityGroups);
David Netobb5c02f2016-10-19 10:16:29 -04004299#ifdef AMD_EXTENSIONS
Rex Xu17ff3432016-10-14 17:41:45 +08004300 if (op == glslang::EOpMinInvocationsNonUniform ||
4301 op == glslang::EOpMaxInvocationsNonUniform ||
Rex Xu430ef402016-10-14 17:22:23 +08004302 op == glslang::EOpAddInvocationsNonUniform ||
4303 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
4304 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
4305 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
4306 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
4307 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
4308 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
Rex Xu17ff3432016-10-14 17:41:45 +08004309 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
David Netobb5c02f2016-10-19 10:16:29 -04004310#endif
Rex Xu51596642016-09-21 18:56:12 +08004311
4312 spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
Rex Xu9d93a232016-05-05 12:30:44 +08004313#ifdef AMD_EXTENSIONS
Rex Xu430ef402016-10-14 17:22:23 +08004314 switch (op) {
4315 case glslang::EOpMinInvocations:
4316 case glslang::EOpMaxInvocations:
4317 case glslang::EOpAddInvocations:
4318 case glslang::EOpMinInvocationsNonUniform:
4319 case glslang::EOpMaxInvocationsNonUniform:
4320 case glslang::EOpAddInvocationsNonUniform:
4321 groupOperation = spv::GroupOperationReduce;
4322 spvGroupOperands.push_back(groupOperation);
4323 break;
4324 case glslang::EOpMinInvocationsInclusiveScan:
4325 case glslang::EOpMaxInvocationsInclusiveScan:
4326 case glslang::EOpAddInvocationsInclusiveScan:
4327 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
4328 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
4329 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
4330 groupOperation = spv::GroupOperationInclusiveScan;
4331 spvGroupOperands.push_back(groupOperation);
4332 break;
4333 case glslang::EOpMinInvocationsExclusiveScan:
4334 case glslang::EOpMaxInvocationsExclusiveScan:
4335 case glslang::EOpAddInvocationsExclusiveScan:
4336 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
4337 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
4338 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
4339 groupOperation = spv::GroupOperationExclusiveScan;
4340 spvGroupOperands.push_back(groupOperation);
4341 break;
Mike Weiblen4e9e4002017-01-20 13:34:10 -07004342 default:
4343 break;
Rex Xu430ef402016-10-14 17:22:23 +08004344 }
Rex Xu9d93a232016-05-05 12:30:44 +08004345#endif
Rex Xu51596642016-09-21 18:56:12 +08004346 }
4347
4348 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt)
4349 spvGroupOperands.push_back(*opIt);
John Kessenich91cef522016-05-05 16:45:40 -06004350
4351 switch (op) {
4352 case glslang::EOpAnyInvocation:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08004353 opCode = spv::OpSubgroupAnyKHR;
Rex Xu51596642016-09-21 18:56:12 +08004354 break;
John Kessenich91cef522016-05-05 16:45:40 -06004355 case glslang::EOpAllInvocations:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08004356 opCode = spv::OpSubgroupAllKHR;
Rex Xu51596642016-09-21 18:56:12 +08004357 break;
John Kessenich91cef522016-05-05 16:45:40 -06004358 case glslang::EOpAllInvocationsEqual:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08004359 opCode = spv::OpSubgroupAllEqualKHR;
4360 break;
Rex Xu51596642016-09-21 18:56:12 +08004361 case glslang::EOpReadInvocation:
chaocf200da82016-12-20 12:44:35 -08004362 opCode = spv::OpSubgroupReadInvocationKHR;
Rex Xub7072052016-09-26 15:53:40 +08004363 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08004364 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08004365 break;
4366 case glslang::EOpReadFirstInvocation:
4367 opCode = spv::OpSubgroupFirstInvocationKHR;
4368 break;
4369 case glslang::EOpBallot:
4370 {
4371 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
4372 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
4373 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
4374 //
4375 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
4376 //
4377 spv::Id uintType = builder.makeUintType(32);
4378 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
4379 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
4380
4381 std::vector<spv::Id> components;
4382 components.push_back(builder.createCompositeExtract(result, uintType, 0));
4383 components.push_back(builder.createCompositeExtract(result, uintType, 1));
4384
4385 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
4386 return builder.createUnaryOp(spv::OpBitcast, typeId,
4387 builder.createCompositeConstruct(uvec2Type, components));
4388 }
4389
Rex Xu9d93a232016-05-05 12:30:44 +08004390#ifdef AMD_EXTENSIONS
4391 case glslang::EOpMinInvocations:
4392 case glslang::EOpMaxInvocations:
4393 case glslang::EOpAddInvocations:
Rex Xu430ef402016-10-14 17:22:23 +08004394 case glslang::EOpMinInvocationsInclusiveScan:
4395 case glslang::EOpMaxInvocationsInclusiveScan:
4396 case glslang::EOpAddInvocationsInclusiveScan:
4397 case glslang::EOpMinInvocationsExclusiveScan:
4398 case glslang::EOpMaxInvocationsExclusiveScan:
4399 case glslang::EOpAddInvocationsExclusiveScan:
4400 if (op == glslang::EOpMinInvocations ||
4401 op == glslang::EOpMinInvocationsInclusiveScan ||
4402 op == glslang::EOpMinInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08004403 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004404 opCode = spv::OpGroupFMin;
Rex Xu9d93a232016-05-05 12:30:44 +08004405 else {
4406 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08004407 opCode = spv::OpGroupUMin;
Rex Xu9d93a232016-05-05 12:30:44 +08004408 else
Rex Xu51596642016-09-21 18:56:12 +08004409 opCode = spv::OpGroupSMin;
Rex Xu9d93a232016-05-05 12:30:44 +08004410 }
Rex Xu430ef402016-10-14 17:22:23 +08004411 } else if (op == glslang::EOpMaxInvocations ||
4412 op == glslang::EOpMaxInvocationsInclusiveScan ||
4413 op == glslang::EOpMaxInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08004414 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004415 opCode = spv::OpGroupFMax;
Rex Xu9d93a232016-05-05 12:30:44 +08004416 else {
4417 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08004418 opCode = spv::OpGroupUMax;
Rex Xu9d93a232016-05-05 12:30:44 +08004419 else
Rex Xu51596642016-09-21 18:56:12 +08004420 opCode = spv::OpGroupSMax;
Rex Xu9d93a232016-05-05 12:30:44 +08004421 }
4422 } else {
4423 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004424 opCode = spv::OpGroupFAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08004425 else
Rex Xu51596642016-09-21 18:56:12 +08004426 opCode = spv::OpGroupIAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08004427 }
4428
Rex Xu2bbbe062016-08-23 15:41:05 +08004429 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08004430 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08004431
4432 break;
Rex Xu9d93a232016-05-05 12:30:44 +08004433 case glslang::EOpMinInvocationsNonUniform:
4434 case glslang::EOpMaxInvocationsNonUniform:
4435 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08004436 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
4437 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
4438 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
4439 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
4440 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
4441 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
4442 if (op == glslang::EOpMinInvocationsNonUniform ||
4443 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
4444 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08004445 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004446 opCode = spv::OpGroupFMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004447 else {
4448 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08004449 opCode = spv::OpGroupUMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004450 else
Rex Xu51596642016-09-21 18:56:12 +08004451 opCode = spv::OpGroupSMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004452 }
4453 }
Rex Xu430ef402016-10-14 17:22:23 +08004454 else if (op == glslang::EOpMaxInvocationsNonUniform ||
4455 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
4456 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08004457 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004458 opCode = spv::OpGroupFMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004459 else {
4460 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08004461 opCode = spv::OpGroupUMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004462 else
Rex Xu51596642016-09-21 18:56:12 +08004463 opCode = spv::OpGroupSMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004464 }
4465 }
4466 else {
4467 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08004468 opCode = spv::OpGroupFAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004469 else
Rex Xu51596642016-09-21 18:56:12 +08004470 opCode = spv::OpGroupIAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08004471 }
4472
Rex Xu2bbbe062016-08-23 15:41:05 +08004473 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08004474 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08004475
4476 break;
Rex Xu9d93a232016-05-05 12:30:44 +08004477#endif
John Kessenich91cef522016-05-05 16:45:40 -06004478 default:
4479 logger->missingFunctionality("invocation operation");
4480 return spv::NoResult;
4481 }
Rex Xu51596642016-09-21 18:56:12 +08004482
4483 assert(opCode != spv::OpNop);
4484 return builder.createOp(opCode, typeId, spvGroupOperands);
John Kessenich91cef522016-05-05 16:45:40 -06004485}
4486
Rex Xu2bbbe062016-08-23 15:41:05 +08004487// Create group invocation operations on a vector
Rex Xu430ef402016-10-14 17:22:23 +08004488spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands)
Rex Xu2bbbe062016-08-23 15:41:05 +08004489{
Rex Xub7072052016-09-26 15:53:40 +08004490#ifdef AMD_EXTENSIONS
Rex Xu2bbbe062016-08-23 15:41:05 +08004491 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
4492 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
Rex Xub7072052016-09-26 15:53:40 +08004493 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
chaocf200da82016-12-20 12:44:35 -08004494 op == spv::OpSubgroupReadInvocationKHR ||
Rex Xu2bbbe062016-08-23 15:41:05 +08004495 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD ||
4496 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD ||
4497 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
Rex Xub7072052016-09-26 15:53:40 +08004498#else
4499 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
4500 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
chaocf200da82016-12-20 12:44:35 -08004501 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
4502 op == spv::OpSubgroupReadInvocationKHR);
Rex Xub7072052016-09-26 15:53:40 +08004503#endif
Rex Xu2bbbe062016-08-23 15:41:05 +08004504
4505 // Handle group invocation operations scalar by scalar.
4506 // The result type is the same type as the original type.
4507 // The algorithm is to:
4508 // - break the vector into scalars
4509 // - apply the operation to each scalar
4510 // - make a vector out the scalar results
4511
4512 // get the types sorted out
Rex Xub7072052016-09-26 15:53:40 +08004513 int numComponents = builder.getNumComponents(operands[0]);
4514 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
Rex Xu2bbbe062016-08-23 15:41:05 +08004515 std::vector<spv::Id> results;
4516
4517 // do each scalar op
4518 for (int comp = 0; comp < numComponents; ++comp) {
4519 std::vector<unsigned int> indexes;
4520 indexes.push_back(comp);
Rex Xub7072052016-09-26 15:53:40 +08004521 spv::Id scalar = builder.createCompositeExtract(operands[0], scalarType, indexes);
Rex Xub7072052016-09-26 15:53:40 +08004522 std::vector<spv::Id> spvGroupOperands;
chaocf200da82016-12-20 12:44:35 -08004523 if (op == spv::OpSubgroupReadInvocationKHR) {
4524 spvGroupOperands.push_back(scalar);
4525 spvGroupOperands.push_back(operands[1]);
4526 } else if (op == spv::OpGroupBroadcast) {
4527 spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
Rex Xub7072052016-09-26 15:53:40 +08004528 spvGroupOperands.push_back(scalar);
4529 spvGroupOperands.push_back(operands[1]);
4530 } else {
chaocf200da82016-12-20 12:44:35 -08004531 spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
Rex Xu430ef402016-10-14 17:22:23 +08004532 spvGroupOperands.push_back(groupOperation);
Rex Xub7072052016-09-26 15:53:40 +08004533 spvGroupOperands.push_back(scalar);
4534 }
Rex Xu2bbbe062016-08-23 15:41:05 +08004535
Rex Xub7072052016-09-26 15:53:40 +08004536 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
Rex Xu2bbbe062016-08-23 15:41:05 +08004537 }
4538
4539 // put the pieces together
4540 return builder.createCompositeConstruct(typeId, results);
4541}
Rex Xu2bbbe062016-08-23 15:41:05 +08004542
John Kessenich5e4b1242015-08-06 22:53:06 -06004543spv::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 -06004544{
Rex Xu8ff43de2016-04-22 16:51:45 +08004545 bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004546#ifdef AMD_EXTENSIONS
4547 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16;
4548#else
John Kessenich5e4b1242015-08-06 22:53:06 -06004549 bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08004550#endif
John Kessenich5e4b1242015-08-06 22:53:06 -06004551
John Kessenich140f3df2015-06-26 16:58:36 -06004552 spv::Op opCode = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08004553 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06004554 int libCall = -1;
Mark Adams364c21c2016-01-06 13:41:02 -05004555 size_t consumedOperands = operands.size();
John Kessenich55e7d112015-11-15 21:33:39 -07004556 spv::Id typeId0 = 0;
4557 if (consumedOperands > 0)
4558 typeId0 = builder.getTypeId(operands[0]);
4559 spv::Id frexpIntType = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06004560
4561 switch (op) {
4562 case glslang::EOpMin:
John Kessenich5e4b1242015-08-06 22:53:06 -06004563 if (isFloat)
4564 libCall = spv::GLSLstd450FMin;
4565 else if (isUnsigned)
4566 libCall = spv::GLSLstd450UMin;
4567 else
4568 libCall = spv::GLSLstd450SMin;
John Kesseniche7c83cf2015-12-13 13:34:37 -07004569 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06004570 break;
4571 case glslang::EOpModf:
John Kessenich5e4b1242015-08-06 22:53:06 -06004572 libCall = spv::GLSLstd450Modf;
John Kessenich140f3df2015-06-26 16:58:36 -06004573 break;
4574 case glslang::EOpMax:
John Kessenich5e4b1242015-08-06 22:53:06 -06004575 if (isFloat)
4576 libCall = spv::GLSLstd450FMax;
4577 else if (isUnsigned)
4578 libCall = spv::GLSLstd450UMax;
4579 else
4580 libCall = spv::GLSLstd450SMax;
John Kesseniche7c83cf2015-12-13 13:34:37 -07004581 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06004582 break;
4583 case glslang::EOpPow:
John Kessenich5e4b1242015-08-06 22:53:06 -06004584 libCall = spv::GLSLstd450Pow;
John Kessenich140f3df2015-06-26 16:58:36 -06004585 break;
4586 case glslang::EOpDot:
4587 opCode = spv::OpDot;
4588 break;
4589 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06004590 libCall = spv::GLSLstd450Atan2;
John Kessenich140f3df2015-06-26 16:58:36 -06004591 break;
4592
4593 case glslang::EOpClamp:
John Kessenich5e4b1242015-08-06 22:53:06 -06004594 if (isFloat)
4595 libCall = spv::GLSLstd450FClamp;
4596 else if (isUnsigned)
4597 libCall = spv::GLSLstd450UClamp;
4598 else
4599 libCall = spv::GLSLstd450SClamp;
John Kesseniche7c83cf2015-12-13 13:34:37 -07004600 builder.promoteScalar(precision, operands.front(), operands[1]);
4601 builder.promoteScalar(precision, operands.front(), operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06004602 break;
4603 case glslang::EOpMix:
Rex Xud715adc2016-03-15 12:08:31 +08004604 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
4605 assert(isFloat);
John Kessenich55e7d112015-11-15 21:33:39 -07004606 libCall = spv::GLSLstd450FMix;
Rex Xud715adc2016-03-15 12:08:31 +08004607 } else {
John Kessenich6c292d32016-02-15 20:58:50 -07004608 opCode = spv::OpSelect;
Rex Xud715adc2016-03-15 12:08:31 +08004609 std::swap(operands.front(), operands.back());
John Kessenich6c292d32016-02-15 20:58:50 -07004610 }
John Kesseniche7c83cf2015-12-13 13:34:37 -07004611 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06004612 break;
4613 case glslang::EOpStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06004614 libCall = spv::GLSLstd450Step;
John Kesseniche7c83cf2015-12-13 13:34:37 -07004615 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06004616 break;
4617 case glslang::EOpSmoothStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06004618 libCall = spv::GLSLstd450SmoothStep;
John Kesseniche7c83cf2015-12-13 13:34:37 -07004619 builder.promoteScalar(precision, operands[0], operands[2]);
4620 builder.promoteScalar(precision, operands[1], operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06004621 break;
4622
4623 case glslang::EOpDistance:
John Kessenich5e4b1242015-08-06 22:53:06 -06004624 libCall = spv::GLSLstd450Distance;
John Kessenich140f3df2015-06-26 16:58:36 -06004625 break;
4626 case glslang::EOpCross:
John Kessenich5e4b1242015-08-06 22:53:06 -06004627 libCall = spv::GLSLstd450Cross;
John Kessenich140f3df2015-06-26 16:58:36 -06004628 break;
4629 case glslang::EOpFaceForward:
John Kessenich5e4b1242015-08-06 22:53:06 -06004630 libCall = spv::GLSLstd450FaceForward;
John Kessenich140f3df2015-06-26 16:58:36 -06004631 break;
4632 case glslang::EOpReflect:
John Kessenich5e4b1242015-08-06 22:53:06 -06004633 libCall = spv::GLSLstd450Reflect;
John Kessenich140f3df2015-06-26 16:58:36 -06004634 break;
4635 case glslang::EOpRefract:
John Kessenich5e4b1242015-08-06 22:53:06 -06004636 libCall = spv::GLSLstd450Refract;
John Kessenich140f3df2015-06-26 16:58:36 -06004637 break;
Rex Xu7a26c172015-12-08 17:12:09 +08004638 case glslang::EOpInterpolateAtSample:
John Kessenich92187592016-02-01 13:45:25 -07004639 builder.addCapability(spv::CapabilityInterpolationFunction);
Rex Xu7a26c172015-12-08 17:12:09 +08004640 libCall = spv::GLSLstd450InterpolateAtSample;
4641 break;
4642 case glslang::EOpInterpolateAtOffset:
John Kessenich92187592016-02-01 13:45:25 -07004643 builder.addCapability(spv::CapabilityInterpolationFunction);
Rex Xu7a26c172015-12-08 17:12:09 +08004644 libCall = spv::GLSLstd450InterpolateAtOffset;
4645 break;
John Kessenich55e7d112015-11-15 21:33:39 -07004646 case glslang::EOpAddCarry:
4647 opCode = spv::OpIAddCarry;
4648 typeId = builder.makeStructResultType(typeId0, typeId0);
4649 consumedOperands = 2;
4650 break;
4651 case glslang::EOpSubBorrow:
4652 opCode = spv::OpISubBorrow;
4653 typeId = builder.makeStructResultType(typeId0, typeId0);
4654 consumedOperands = 2;
4655 break;
4656 case glslang::EOpUMulExtended:
4657 opCode = spv::OpUMulExtended;
4658 typeId = builder.makeStructResultType(typeId0, typeId0);
4659 consumedOperands = 2;
4660 break;
4661 case glslang::EOpIMulExtended:
4662 opCode = spv::OpSMulExtended;
4663 typeId = builder.makeStructResultType(typeId0, typeId0);
4664 consumedOperands = 2;
4665 break;
4666 case glslang::EOpBitfieldExtract:
4667 if (isUnsigned)
4668 opCode = spv::OpBitFieldUExtract;
4669 else
4670 opCode = spv::OpBitFieldSExtract;
4671 break;
4672 case glslang::EOpBitfieldInsert:
4673 opCode = spv::OpBitFieldInsert;
4674 break;
4675
4676 case glslang::EOpFma:
4677 libCall = spv::GLSLstd450Fma;
4678 break;
4679 case glslang::EOpFrexp:
4680 libCall = spv::GLSLstd450FrexpStruct;
4681 if (builder.getNumComponents(operands[0]) == 1)
4682 frexpIntType = builder.makeIntegerType(32, true);
4683 else
4684 frexpIntType = builder.makeVectorType(builder.makeIntegerType(32, true), builder.getNumComponents(operands[0]));
4685 typeId = builder.makeStructResultType(typeId0, frexpIntType);
4686 consumedOperands = 1;
4687 break;
4688 case glslang::EOpLdexp:
4689 libCall = spv::GLSLstd450Ldexp;
4690 break;
4691
Rex Xu574ab042016-04-14 16:53:07 +08004692 case glslang::EOpReadInvocation:
Rex Xu51596642016-09-21 18:56:12 +08004693 return createInvocationsOperation(op, typeId, operands, typeProxy);
Rex Xu574ab042016-04-14 16:53:07 +08004694
Rex Xu9d93a232016-05-05 12:30:44 +08004695#ifdef AMD_EXTENSIONS
4696 case glslang::EOpSwizzleInvocations:
4697 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
4698 libCall = spv::SwizzleInvocationsAMD;
4699 break;
4700 case glslang::EOpSwizzleInvocationsMasked:
4701 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
4702 libCall = spv::SwizzleInvocationsMaskedAMD;
4703 break;
4704 case glslang::EOpWriteInvocation:
4705 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
4706 libCall = spv::WriteInvocationAMD;
4707 break;
4708
4709 case glslang::EOpMin3:
4710 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
4711 if (isFloat)
4712 libCall = spv::FMin3AMD;
4713 else {
4714 if (isUnsigned)
4715 libCall = spv::UMin3AMD;
4716 else
4717 libCall = spv::SMin3AMD;
4718 }
4719 break;
4720 case glslang::EOpMax3:
4721 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
4722 if (isFloat)
4723 libCall = spv::FMax3AMD;
4724 else {
4725 if (isUnsigned)
4726 libCall = spv::UMax3AMD;
4727 else
4728 libCall = spv::SMax3AMD;
4729 }
4730 break;
4731 case glslang::EOpMid3:
4732 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
4733 if (isFloat)
4734 libCall = spv::FMid3AMD;
4735 else {
4736 if (isUnsigned)
4737 libCall = spv::UMid3AMD;
4738 else
4739 libCall = spv::SMid3AMD;
4740 }
4741 break;
4742
4743 case glslang::EOpInterpolateAtVertex:
4744 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
4745 libCall = spv::InterpolateAtVertexAMD;
4746 break;
4747#endif
4748
John Kessenich140f3df2015-06-26 16:58:36 -06004749 default:
4750 return 0;
4751 }
4752
4753 spv::Id id = 0;
John Kessenich2359bd02015-12-06 19:29:11 -07004754 if (libCall >= 0) {
David Neto8d63a3d2015-12-07 16:17:06 -05004755 // Use an extended instruction from the standard library.
4756 // Construct the call arguments, without modifying the original operands vector.
4757 // We might need the remaining arguments, e.g. in the EOpFrexp case.
4758 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
Rex Xu9d93a232016-05-05 12:30:44 +08004759 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
John Kessenich2359bd02015-12-06 19:29:11 -07004760 } else {
John Kessenich55e7d112015-11-15 21:33:39 -07004761 switch (consumedOperands) {
John Kessenich140f3df2015-06-26 16:58:36 -06004762 case 0:
4763 // should all be handled by visitAggregate and createNoArgOperation
4764 assert(0);
4765 return 0;
4766 case 1:
4767 // should all be handled by createUnaryOperation
4768 assert(0);
4769 return 0;
4770 case 2:
4771 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
4772 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004773 default:
John Kessenich55e7d112015-11-15 21:33:39 -07004774 // anything 3 or over doesn't have l-value operands, so all should be consumed
4775 assert(consumedOperands == operands.size());
4776 id = builder.createOp(opCode, typeId, operands);
John Kessenich140f3df2015-06-26 16:58:36 -06004777 break;
4778 }
4779 }
4780
John Kessenich55e7d112015-11-15 21:33:39 -07004781 // Decode the return types that were structures
4782 switch (op) {
4783 case glslang::EOpAddCarry:
4784 case glslang::EOpSubBorrow:
4785 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
4786 id = builder.createCompositeExtract(id, typeId0, 0);
4787 break;
4788 case glslang::EOpUMulExtended:
4789 case glslang::EOpIMulExtended:
4790 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
4791 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
4792 break;
4793 case glslang::EOpFrexp:
David Neto8d63a3d2015-12-07 16:17:06 -05004794 assert(operands.size() == 2);
John Kessenich55e7d112015-11-15 21:33:39 -07004795 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
4796 id = builder.createCompositeExtract(id, typeId0, 0);
4797 break;
4798 default:
4799 break;
4800 }
4801
John Kessenich32cfd492016-02-02 12:37:46 -07004802 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06004803}
4804
Rex Xu9d93a232016-05-05 12:30:44 +08004805// Intrinsics with no arguments (or no return value, and no precision).
4806spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
John Kessenich140f3df2015-06-26 16:58:36 -06004807{
4808 // TODO: get the barrier operands correct
4809
4810 switch (op) {
4811 case glslang::EOpEmitVertex:
4812 builder.createNoResultOp(spv::OpEmitVertex);
4813 return 0;
4814 case glslang::EOpEndPrimitive:
4815 builder.createNoResultOp(spv::OpEndPrimitive);
4816 return 0;
4817 case glslang::EOpBarrier:
chrgau01@arm.comc3f1cdf2016-11-14 10:10:05 +01004818 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsMaskNone);
John Kessenich140f3df2015-06-26 16:58:36 -06004819 return 0;
4820 case glslang::EOpMemoryBarrier:
John Kessenich5e4b1242015-08-06 22:53:06 -06004821 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory);
John Kessenich140f3df2015-06-26 16:58:36 -06004822 return 0;
4823 case glslang::EOpMemoryBarrierAtomicCounter:
John Kessenich5e4b1242015-08-06 22:53:06 -06004824 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -06004825 return 0;
4826 case glslang::EOpMemoryBarrierBuffer:
John Kessenich5e4b1242015-08-06 22:53:06 -06004827 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -06004828 return 0;
4829 case glslang::EOpMemoryBarrierImage:
John Kessenich5e4b1242015-08-06 22:53:06 -06004830 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -06004831 return 0;
4832 case glslang::EOpMemoryBarrierShared:
John Kessenich55e7d112015-11-15 21:33:39 -07004833 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -06004834 return 0;
4835 case glslang::EOpGroupMemoryBarrier:
John Kessenich55e7d112015-11-15 21:33:39 -07004836 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask);
John Kessenich140f3df2015-06-26 16:58:36 -06004837 return 0;
LoopDawg6e72fdd2016-06-15 09:50:24 -06004838 case glslang::EOpAllMemoryBarrierWithGroupSync:
4839 // Control barrier with non-"None" semantic is also a memory barrier.
4840 builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsAllMemory);
4841 return 0;
4842 case glslang::EOpGroupMemoryBarrierWithGroupSync:
4843 // Control barrier with non-"None" semantic is also a memory barrier.
4844 builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask);
4845 return 0;
4846 case glslang::EOpWorkgroupMemoryBarrier:
4847 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask);
4848 return 0;
4849 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
4850 // Control barrier with non-"None" semantic is also a memory barrier.
4851 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask);
4852 return 0;
Rex Xu9d93a232016-05-05 12:30:44 +08004853#ifdef AMD_EXTENSIONS
4854 case glslang::EOpTime:
4855 {
4856 std::vector<spv::Id> args; // Dummy arguments
4857 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
4858 return builder.setPrecision(id, precision);
4859 }
4860#endif
John Kessenich140f3df2015-06-26 16:58:36 -06004861 default:
Lei Zhang17535f72016-05-04 15:55:59 -04004862 logger->missingFunctionality("unknown operation with no arguments");
John Kessenich140f3df2015-06-26 16:58:36 -06004863 return 0;
4864 }
4865}
4866
4867spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
4868{
John Kessenich2f273362015-07-18 22:34:27 -06004869 auto iter = symbolValues.find(symbol->getId());
John Kessenich140f3df2015-06-26 16:58:36 -06004870 spv::Id id;
4871 if (symbolValues.end() != iter) {
4872 id = iter->second;
4873 return id;
4874 }
4875
4876 // it was not found, create it
4877 id = createSpvVariable(symbol);
4878 symbolValues[symbol->getId()] = id;
4879
Rex Xuc884b4a2016-06-29 15:03:44 +08004880 if (symbol->getBasicType() != glslang::EbtBlock) {
John Kessenich140f3df2015-06-26 16:58:36 -06004881 addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
John Kesseniche0b6cad2015-12-24 10:30:13 -07004882 addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
Rex Xubbceed72016-05-21 09:40:44 +08004883 addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
John Kessenich6c292d32016-02-15 20:58:50 -07004884 if (symbol->getType().getQualifier().hasSpecConstantId())
4885 addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
John Kessenich140f3df2015-06-26 16:58:36 -06004886 if (symbol->getQualifier().hasIndex())
4887 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
4888 if (symbol->getQualifier().hasComponent())
4889 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
4890 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07004891 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06004892 if (symbol->getQualifier().hasXfbStride())
John Kessenich5e4b1242015-08-06 22:53:06 -06004893 builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride);
John Kessenich140f3df2015-06-26 16:58:36 -06004894 if (symbol->getQualifier().hasXfbBuffer())
4895 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
4896 if (symbol->getQualifier().hasXfbOffset())
4897 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
4898 }
John Kessenich91e4aa52016-07-07 17:46:42 -06004899 // atomic counters use this:
4900 if (symbol->getQualifier().hasOffset())
4901 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06004902 }
4903
scygan2c864272016-05-18 18:09:17 +02004904 if (symbol->getQualifier().hasLocation())
4905 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
John Kesseniche0b6cad2015-12-24 10:30:13 -07004906 addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
John Kessenichf2d8a5c2016-03-03 22:29:11 -07004907 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
John Kessenich92187592016-02-01 13:45:25 -07004908 builder.addCapability(spv::CapabilityGeometryStreams);
John Kessenich140f3df2015-06-26 16:58:36 -06004909 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
John Kessenich92187592016-02-01 13:45:25 -07004910 }
John Kessenich140f3df2015-06-26 16:58:36 -06004911 if (symbol->getQualifier().hasSet())
4912 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
John Kessenich6c292d32016-02-15 20:58:50 -07004913 else if (IsDescriptorResource(symbol->getType())) {
4914 // default to 0
4915 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
4916 }
John Kessenich140f3df2015-06-26 16:58:36 -06004917 if (symbol->getQualifier().hasBinding())
4918 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
John Kessenich6c292d32016-02-15 20:58:50 -07004919 if (symbol->getQualifier().hasAttachment())
4920 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
John Kessenich140f3df2015-06-26 16:58:36 -06004921 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07004922 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06004923 if (symbol->getQualifier().hasXfbStride())
John Kessenich5e4b1242015-08-06 22:53:06 -06004924 builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride);
John Kessenich140f3df2015-06-26 16:58:36 -06004925 if (symbol->getQualifier().hasXfbBuffer())
4926 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
4927 }
4928
Rex Xu1da878f2016-02-21 20:59:01 +08004929 if (symbol->getType().isImage()) {
4930 std::vector<spv::Decoration> memory;
4931 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory);
4932 for (unsigned int i = 0; i < memory.size(); ++i)
4933 addDecoration(id, memory[i]);
4934 }
4935
John Kessenich140f3df2015-06-26 16:58:36 -06004936 // built-in variable decorations
John Kessenichebb50532016-05-16 19:22:05 -06004937 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
John Kessenich4016e382016-07-15 11:53:56 -06004938 if (builtIn != spv::BuiltInMax)
John Kessenich92187592016-02-01 13:45:25 -07004939 addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
John Kessenich140f3df2015-06-26 16:58:36 -06004940
John Kessenichecba76f2017-01-06 00:34:48 -07004941#ifdef NV_EXTENSIONS
chaoc0ad6a4e2016-12-19 16:29:34 -08004942 if (builtIn == spv::BuiltInSampleMask) {
4943 spv::Decoration decoration;
4944 // GL_NV_sample_mask_override_coverage extension
4945 if (glslangIntermediate->getLayoutOverrideCoverage())
chaoc771d89f2017-01-13 01:10:53 -08004946 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
chaoc0ad6a4e2016-12-19 16:29:34 -08004947 else
4948 decoration = (spv::Decoration)spv::DecorationMax;
4949 addDecoration(id, decoration);
4950 if (decoration != spv::DecorationMax) {
4951 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
4952 }
4953 }
chaoc771d89f2017-01-13 01:10:53 -08004954 else if (builtIn == spv::BuiltInLayer) {
4955 // SPV_NV_viewport_array2 extension
4956 if (symbol->getQualifier().layoutViewportRelative)
4957 {
4958 addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
4959 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
4960 builder.addExtension(spv::E_SPV_NV_viewport_array2);
4961 }
4962 if(symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048)
4963 {
4964 addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
4965 builder.addCapability(spv::CapabilityShaderStereoViewNV);
4966 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
4967 }
4968 }
4969
chaoc6e5acae2016-12-20 13:28:52 -08004970 if (symbol->getQualifier().layoutPassthrough) {
chaoc771d89f2017-01-13 01:10:53 -08004971 addDecoration(id, spv::DecorationPassthroughNV);
4972 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
chaoc6e5acae2016-12-20 13:28:52 -08004973 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
4974 }
chaoc0ad6a4e2016-12-19 16:29:34 -08004975#endif
4976
John Kessenich140f3df2015-06-26 16:58:36 -06004977 return id;
4978}
4979
John Kessenich55e7d112015-11-15 21:33:39 -07004980// If 'dec' is valid, add no-operand decoration to an object
John Kessenich140f3df2015-06-26 16:58:36 -06004981void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec)
4982{
John Kessenich4016e382016-07-15 11:53:56 -06004983 if (dec != spv::DecorationMax)
John Kessenich140f3df2015-06-26 16:58:36 -06004984 builder.addDecoration(id, dec);
4985}
4986
John Kessenich55e7d112015-11-15 21:33:39 -07004987// If 'dec' is valid, add a one-operand decoration to an object
4988void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value)
4989{
John Kessenich4016e382016-07-15 11:53:56 -06004990 if (dec != spv::DecorationMax)
John Kessenich55e7d112015-11-15 21:33:39 -07004991 builder.addDecoration(id, dec, value);
4992}
4993
4994// If 'dec' is valid, add a no-operand decoration to a struct member
John Kessenich140f3df2015-06-26 16:58:36 -06004995void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec)
4996{
John Kessenich4016e382016-07-15 11:53:56 -06004997 if (dec != spv::DecorationMax)
John Kessenich140f3df2015-06-26 16:58:36 -06004998 builder.addMemberDecoration(id, (unsigned)member, dec);
4999}
5000
John Kessenich92187592016-02-01 13:45:25 -07005001// If 'dec' is valid, add a one-operand decoration to a struct member
5002void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value)
5003{
John Kessenich4016e382016-07-15 11:53:56 -06005004 if (dec != spv::DecorationMax)
John Kessenich92187592016-02-01 13:45:25 -07005005 builder.addMemberDecoration(id, (unsigned)member, dec, value);
5006}
5007
John Kessenich55e7d112015-11-15 21:33:39 -07005008// Make a full tree of instructions to build a SPIR-V specialization constant,
John Kessenich6c292d32016-02-15 20:58:50 -07005009// or regular constant if possible.
John Kessenich55e7d112015-11-15 21:33:39 -07005010//
5011// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
5012//
5013// Recursively walk the nodes. The nodes form a tree whose leaves are
5014// regular constants, which themselves are trees that createSpvConstant()
5015// recursively walks. So, this function walks the "top" of the tree:
5016// - emit specialization constant-building instructions for specConstant
5017// - when running into a non-spec-constant, switch to createSpvConstant()
qining08408382016-03-21 09:51:37 -04005018spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
John Kessenich55e7d112015-11-15 21:33:39 -07005019{
John Kessenich7cc0e282016-03-20 00:46:02 -06005020 assert(node.getQualifier().isConstant());
John Kessenich55e7d112015-11-15 21:33:39 -07005021
qining4f4bb812016-04-03 23:55:17 -04005022 // Handle front-end constants first (non-specialization constants).
John Kessenich6c292d32016-02-15 20:58:50 -07005023 if (! node.getQualifier().specConstant) {
5024 // hand off to the non-spec-constant path
5025 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
5026 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04005027 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
John Kessenich6c292d32016-02-15 20:58:50 -07005028 nextConst, false);
5029 }
5030
5031 // We now know we have a specialization constant to build
5032
John Kessenichd94c0032016-05-30 19:29:40 -06005033 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
qining4f4bb812016-04-03 23:55:17 -04005034 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
5035 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
5036 std::vector<spv::Id> dimConstId;
5037 for (int dim = 0; dim < 3; ++dim) {
5038 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
5039 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
5040 if (specConst)
5041 addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim));
5042 }
5043 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
5044 }
5045
5046 // An AST node labelled as specialization constant should be a symbol node.
5047 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
5048 if (auto* sn = node.getAsSymbolNode()) {
5049 if (auto* sub_tree = sn->getConstSubtree()) {
qining27e04a02016-04-14 16:40:20 -04005050 // Traverse the constant constructor sub tree like generating normal run-time instructions.
5051 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
5052 // will set the builder into spec constant op instruction generating mode.
5053 sub_tree->traverse(this);
5054 return accessChainLoad(sub_tree->getType());
qining4f4bb812016-04-03 23:55:17 -04005055 } else if (auto* const_union_array = &sn->getConstArray()){
5056 int nextConst = 0;
Endre Omaad58d452017-01-31 21:08:19 +01005057 spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
5058 builder.addName(id, sn->getName().c_str());
5059 return id;
John Kessenich6c292d32016-02-15 20:58:50 -07005060 }
5061 }
qining4f4bb812016-04-03 23:55:17 -04005062
5063 // Neither a front-end constant node, nor a specialization constant node with constant union array or
5064 // constant sub tree as initializer.
Lei Zhang17535f72016-05-04 15:55:59 -04005065 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
qining4f4bb812016-04-03 23:55:17 -04005066 exit(1);
5067 return spv::NoResult;
John Kessenich55e7d112015-11-15 21:33:39 -07005068}
5069
John Kessenich140f3df2015-06-26 16:58:36 -06005070// Use 'consts' as the flattened glslang source of scalar constants to recursively
5071// build the aggregate SPIR-V constant.
5072//
5073// If there are not enough elements present in 'consts', 0 will be substituted;
5074// an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
5075//
qining08408382016-03-21 09:51:37 -04005076spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
John Kessenich140f3df2015-06-26 16:58:36 -06005077{
5078 // vector of constants for SPIR-V
5079 std::vector<spv::Id> spvConsts;
5080
5081 // Type is used for struct and array constants
5082 spv::Id typeId = convertGlslangToSpvType(glslangType);
5083
5084 if (glslangType.isArray()) {
John Kessenich65c78a02015-08-10 17:08:55 -06005085 glslang::TType elementType(glslangType, 0);
5086 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
qining08408382016-03-21 09:51:37 -04005087 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06005088 } else if (glslangType.isMatrix()) {
John Kessenich65c78a02015-08-10 17:08:55 -06005089 glslang::TType vectorType(glslangType, 0);
John Kessenich140f3df2015-06-26 16:58:36 -06005090 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
qining08408382016-03-21 09:51:37 -04005091 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06005092 } else if (glslangType.getStruct()) {
5093 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
5094 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
qining08408382016-03-21 09:51:37 -04005095 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
John Kessenich8d72f1a2016-05-20 12:06:03 -06005096 } else if (glslangType.getVectorSize() > 1) {
John Kessenich140f3df2015-06-26 16:58:36 -06005097 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
5098 bool zero = nextConst >= consts.size();
5099 switch (glslangType.getBasicType()) {
5100 case glslang::EbtInt:
5101 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
5102 break;
5103 case glslang::EbtUint:
5104 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
5105 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08005106 case glslang::EbtInt64:
5107 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
5108 break;
5109 case glslang::EbtUint64:
5110 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
5111 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005112 case glslang::EbtFloat:
5113 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
5114 break;
5115 case glslang::EbtDouble:
5116 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
5117 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005118#ifdef AMD_EXTENSIONS
5119 case glslang::EbtFloat16:
5120 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
5121 break;
5122#endif
John Kessenich140f3df2015-06-26 16:58:36 -06005123 case glslang::EbtBool:
5124 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
5125 break;
5126 default:
John Kessenich55e7d112015-11-15 21:33:39 -07005127 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06005128 break;
5129 }
5130 ++nextConst;
5131 }
5132 } else {
5133 // we have a non-aggregate (scalar) constant
5134 bool zero = nextConst >= consts.size();
5135 spv::Id scalar = 0;
5136 switch (glslangType.getBasicType()) {
5137 case glslang::EbtInt:
John Kessenich55e7d112015-11-15 21:33:39 -07005138 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06005139 break;
5140 case glslang::EbtUint:
John Kessenich55e7d112015-11-15 21:33:39 -07005141 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06005142 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08005143 case glslang::EbtInt64:
5144 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
5145 break;
5146 case glslang::EbtUint64:
5147 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
5148 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005149 case glslang::EbtFloat:
John Kessenich55e7d112015-11-15 21:33:39 -07005150 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06005151 break;
5152 case glslang::EbtDouble:
John Kessenich55e7d112015-11-15 21:33:39 -07005153 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06005154 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005155#ifdef AMD_EXTENSIONS
5156 case glslang::EbtFloat16:
5157 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
5158 break;
5159#endif
John Kessenich140f3df2015-06-26 16:58:36 -06005160 case glslang::EbtBool:
John Kessenich55e7d112015-11-15 21:33:39 -07005161 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06005162 break;
5163 default:
John Kessenich55e7d112015-11-15 21:33:39 -07005164 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06005165 break;
5166 }
5167 ++nextConst;
5168 return scalar;
5169 }
5170
5171 return builder.makeCompositeConstant(typeId, spvConsts);
5172}
5173
John Kessenich7c1aa102015-10-15 13:29:11 -06005174// Return true if the node is a constant or symbol whose reading has no
5175// non-trivial observable cost or effect.
5176bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
5177{
5178 // don't know what this is
5179 if (node == nullptr)
5180 return false;
5181
5182 // a constant is safe
5183 if (node->getAsConstantUnion() != nullptr)
5184 return true;
5185
5186 // not a symbol means non-trivial
5187 if (node->getAsSymbolNode() == nullptr)
5188 return false;
5189
5190 // a symbol, depends on what's being read
5191 switch (node->getType().getQualifier().storage) {
5192 case glslang::EvqTemporary:
5193 case glslang::EvqGlobal:
5194 case glslang::EvqIn:
5195 case glslang::EvqInOut:
5196 case glslang::EvqConst:
5197 case glslang::EvqConstReadOnly:
5198 case glslang::EvqUniform:
5199 return true;
5200 default:
5201 return false;
5202 }
qining25262b32016-05-06 17:25:16 -04005203}
John Kessenich7c1aa102015-10-15 13:29:11 -06005204
5205// A node is trivial if it is a single operation with no side effects.
5206// Error on the side of saying non-trivial.
5207// Return true if trivial.
5208bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
5209{
5210 if (node == nullptr)
5211 return false;
5212
5213 // symbols and constants are trivial
5214 if (isTrivialLeaf(node))
5215 return true;
5216
5217 // otherwise, it needs to be a simple operation or one or two leaf nodes
5218
5219 // not a simple operation
5220 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
5221 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
5222 if (binaryNode == nullptr && unaryNode == nullptr)
5223 return false;
5224
5225 // not on leaf nodes
5226 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
5227 return false;
5228
5229 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
5230 return false;
5231 }
5232
5233 switch (node->getAsOperator()->getOp()) {
5234 case glslang::EOpLogicalNot:
5235 case glslang::EOpConvIntToBool:
5236 case glslang::EOpConvUintToBool:
5237 case glslang::EOpConvFloatToBool:
5238 case glslang::EOpConvDoubleToBool:
5239 case glslang::EOpEqual:
5240 case glslang::EOpNotEqual:
5241 case glslang::EOpLessThan:
5242 case glslang::EOpGreaterThan:
5243 case glslang::EOpLessThanEqual:
5244 case glslang::EOpGreaterThanEqual:
5245 case glslang::EOpIndexDirect:
5246 case glslang::EOpIndexDirectStruct:
5247 case glslang::EOpLogicalXor:
5248 case glslang::EOpAny:
5249 case glslang::EOpAll:
5250 return true;
5251 default:
5252 return false;
5253 }
5254}
5255
5256// Emit short-circuiting code, where 'right' is never evaluated unless
5257// the left side is true (for &&) or false (for ||).
5258spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right)
5259{
5260 spv::Id boolTypeId = builder.makeBoolType();
5261
5262 // emit left operand
5263 builder.clearAccessChain();
5264 left.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08005265 spv::Id leftId = accessChainLoad(left.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06005266
5267 // Operands to accumulate OpPhi operands
5268 std::vector<spv::Id> phiOperands;
5269 // accumulate left operand's phi information
5270 phiOperands.push_back(leftId);
5271 phiOperands.push_back(builder.getBuildPoint()->getId());
5272
5273 // Make the two kinds of operation symmetric with a "!"
5274 // || => emit "if (! left) result = right"
5275 // && => emit "if ( left) result = right"
5276 //
5277 // TODO: this runtime "not" for || could be avoided by adding functionality
5278 // to 'builder' to have an "else" without an "then"
5279 if (op == glslang::EOpLogicalOr)
5280 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
5281
5282 // make an "if" based on the left value
5283 spv::Builder::If ifBuilder(leftId, builder);
5284
5285 // emit right operand as the "then" part of the "if"
5286 builder.clearAccessChain();
5287 right.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08005288 spv::Id rightId = accessChainLoad(right.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06005289
5290 // accumulate left operand's phi information
5291 phiOperands.push_back(rightId);
5292 phiOperands.push_back(builder.getBuildPoint()->getId());
5293
5294 // finish the "if"
5295 ifBuilder.makeEndIf();
5296
5297 // phi together the two results
5298 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
5299}
5300
Rex Xu9d93a232016-05-05 12:30:44 +08005301// Return type Id of the imported set of extended instructions corresponds to the name.
5302// Import this set if it has not been imported yet.
5303spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
5304{
5305 if (extBuiltinMap.find(name) != extBuiltinMap.end())
5306 return extBuiltinMap[name];
5307 else {
Rex Xu51596642016-09-21 18:56:12 +08005308 builder.addExtension(name);
Rex Xu9d93a232016-05-05 12:30:44 +08005309 spv::Id extBuiltins = builder.import(name);
5310 extBuiltinMap[name] = extBuiltins;
5311 return extBuiltins;
5312 }
5313}
5314
John Kessenich140f3df2015-06-26 16:58:36 -06005315}; // end anonymous namespace
5316
5317namespace glslang {
5318
John Kessenich68d78fd2015-07-12 19:28:10 -06005319void GetSpirvVersion(std::string& version)
5320{
John Kessenich9e55f632015-07-15 10:03:39 -06005321 const int bufSize = 100;
John Kessenichf98ee232015-07-12 19:39:51 -06005322 char buf[bufSize];
John Kessenich55e7d112015-11-15 21:33:39 -07005323 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
John Kessenich68d78fd2015-07-12 19:28:10 -06005324 version = buf;
5325}
5326
John Kessenich140f3df2015-06-26 16:58:36 -06005327// Write SPIR-V out to a binary file
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005328void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
John Kessenich140f3df2015-06-26 16:58:36 -06005329{
5330 std::ofstream out;
John Kessenich68d78fd2015-07-12 19:28:10 -06005331 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07005332 if (out.fail())
5333 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenich140f3df2015-06-26 16:58:36 -06005334 for (int i = 0; i < (int)spirv.size(); ++i) {
5335 unsigned int word = spirv[i];
5336 out.write((const char*)&word, 4);
5337 }
5338 out.close();
5339}
5340
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005341// Write SPIR-V out to a text file with 32-bit hexadecimal words
Flavioaea3c892017-02-06 11:46:35 -08005342void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005343{
5344 std::ofstream out;
5345 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07005346 if (out.fail())
5347 printf("ERROR: Failed to open file: %s\n", baseName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005348 out << "\t// " GLSLANG_REVISION " " GLSLANG_DATE << std::endl;
Flavio15017db2017-02-15 14:29:33 -08005349 if (varName != nullptr) {
5350 out << "\t #pragma once" << std::endl;
5351 out << "const uint32_t " << varName << "[] = {" << std::endl;
5352 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005353 const int WORDS_PER_LINE = 8;
5354 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
5355 out << "\t";
5356 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
5357 const unsigned int word = spirv[i + j];
5358 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
5359 if (i + j + 1 < (int)spirv.size()) {
5360 out << ",";
5361 }
5362 }
5363 out << std::endl;
5364 }
Flavio15017db2017-02-15 14:29:33 -08005365 if (varName != nullptr) {
5366 out << "};";
5367 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05005368 out.close();
5369}
5370
John Kessenich140f3df2015-06-26 16:58:36 -06005371//
5372// Set up the glslang traversal
5373//
5374void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv)
5375{
Lei Zhang17535f72016-05-04 15:55:59 -04005376 spv::SpvBuildLogger logger;
5377 GlslangToSpv(intermediate, spirv, &logger);
Lei Zhang09caf122016-05-02 18:11:54 -04005378}
5379
Lei Zhang17535f72016-05-04 15:55:59 -04005380void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, spv::SpvBuildLogger* logger)
Lei Zhang09caf122016-05-02 18:11:54 -04005381{
John Kessenich140f3df2015-06-26 16:58:36 -06005382 TIntermNode* root = intermediate.getTreeRoot();
5383
5384 if (root == 0)
5385 return;
5386
5387 glslang::GetThreadPoolAllocator().push();
5388
Lei Zhang17535f72016-05-04 15:55:59 -04005389 TGlslangToSpvTraverser it(&intermediate, logger);
John Kessenich140f3df2015-06-26 16:58:36 -06005390 root->traverse(&it);
John Kessenichfca82622016-11-26 13:23:20 -07005391 it.finishSpv();
John Kessenich140f3df2015-06-26 16:58:36 -06005392 it.dumpSpv(spirv);
5393
5394 glslang::GetThreadPoolAllocator().pop();
5395}
5396
5397}; // end namespace glslang