blob: ae5f24d3035b1648e15e1b1a154fb1340cd4474d [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2014-2016 LunarG, Inc.
John Kessenichb23d2322018-12-14 10:47:35 -07003// Copyright (C) 2015-2018 Google, Inc.
John Kessenich66011cb2018-03-06 16:12:04 -07004// Copyright (C) 2017 ARM Limited.
John Kessenich140f3df2015-06-26 16:58:36 -06005//
John Kessenich927608b2017-01-06 12:34:14 -07006// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06007//
John Kessenich927608b2017-01-06 12:34:14 -07008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
John Kessenich140f3df2015-06-26 16:58:36 -060011//
12// Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following
17// disclaimer in the documentation and/or other materials provided
18// with the distribution.
19//
20// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21// contributors may be used to endorse or promote products derived
22// from this software without specific prior written permission.
23//
John Kessenich927608b2017-01-06 12:34:14 -070024// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35// POSSIBILITY OF SUCH DAMAGE.
John Kessenich140f3df2015-06-26 16:58:36 -060036
37//
John Kessenich140f3df2015-06-26 16:58:36 -060038// Visit the nodes in the glslang intermediate tree representation to
39// translate them to SPIR-V.
40//
41
John Kessenich5e4b1242015-08-06 22:53:06 -060042#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060043#include "GlslangToSpv.h"
44#include "SpvBuilder.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060045namespace spv {
Rex Xu51596642016-09-21 18:56:12 +080046 #include "GLSL.std.450.h"
47 #include "GLSL.ext.KHR.h"
Piers Daniell1c5443c2017-12-13 13:07:22 -070048 #include "GLSL.ext.EXT.h"
Rex Xu51596642016-09-21 18:56:12 +080049 #include "GLSL.ext.AMD.h"
chaoc0ad6a4e2016-12-19 16:29:34 -080050 #include "GLSL.ext.NV.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060051}
John Kessenich140f3df2015-06-26 16:58:36 -060052
53// Glslang includes
baldurk42169c52015-07-08 15:11:59 +020054#include "../glslang/MachineIndependent/localintermediate.h"
55#include "../glslang/MachineIndependent/SymbolTable.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060056#include "../glslang/Include/Common.h"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050057#include "../glslang/Include/revision.h"
John Kessenich140f3df2015-06-26 16:58:36 -060058
John Kessenich140f3df2015-06-26 16:58:36 -060059#include <fstream>
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050060#include <iomanip>
Lei Zhang17535f72016-05-04 15:55:59 -040061#include <list>
62#include <map>
63#include <stack>
64#include <string>
65#include <vector>
John Kessenich140f3df2015-06-26 16:58:36 -060066
67namespace {
68
qining4c912612016-04-01 10:35:16 -040069namespace {
70class SpecConstantOpModeGuard {
71public:
72 SpecConstantOpModeGuard(spv::Builder* builder)
73 : builder_(builder) {
74 previous_flag_ = builder->isInSpecConstCodeGenMode();
qining4c912612016-04-01 10:35:16 -040075 }
76 ~SpecConstantOpModeGuard() {
77 previous_flag_ ? builder_->setToSpecConstCodeGenMode()
78 : builder_->setToNormalCodeGenMode();
79 }
qining40887662016-04-03 22:20:42 -040080 void turnOnSpecConstantOpMode() {
81 builder_->setToSpecConstCodeGenMode();
82 }
qining4c912612016-04-01 10:35:16 -040083
84private:
85 spv::Builder* builder_;
86 bool previous_flag_;
87};
John Kessenichead86222018-03-28 18:01:20 -060088
89struct OpDecorations {
John Kessenichb9197c82019-08-11 07:41:45 -060090 public:
91 OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) :
92 precision(precision)
93#ifndef GLSLANG_WEB
94 ,
95 noContraction(noContraction),
96 nonUniform(nonUniform)
97#endif
98 { }
99
John Kessenichead86222018-03-28 18:01:20 -0600100 spv::Decoration precision;
John Kessenichb9197c82019-08-11 07:41:45 -0600101
102#ifdef GLSLANG_WEB
Ryan Harrison7c9accb2019-10-11 11:00:57 -0400103 void addNoContraction(spv::Builder&, spv::Id) const { }
104 void addNonUniform(spv::Builder&, spv::Id) const { }
John Kessenichb9197c82019-08-11 07:41:45 -0600105#else
Ryan Harrison7c9accb2019-10-11 11:00:57 -0400106 void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); }
107 void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); }
John Kessenichb9197c82019-08-11 07:41:45 -0600108 protected:
109 spv::Decoration noContraction;
110 spv::Decoration nonUniform;
111#endif
112
John Kessenichead86222018-03-28 18:01:20 -0600113};
114
115} // namespace
qining4c912612016-04-01 10:35:16 -0400116
John Kessenich140f3df2015-06-26 16:58:36 -0600117//
118// The main holder of information for translating glslang to SPIR-V.
119//
120// Derives from the AST walking base class.
121//
122class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
123public:
John Kessenich2b5ea9f2018-01-31 18:35:56 -0700124 TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger,
125 glslang::SpvOptions& options);
John Kessenichfca82622016-11-26 13:23:20 -0700126 virtual ~TGlslangToSpvTraverser() { }
John Kessenich140f3df2015-06-26 16:58:36 -0600127
128 bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*);
129 bool visitBinary(glslang::TVisit, glslang::TIntermBinary*);
130 void visitConstantUnion(glslang::TIntermConstantUnion*);
131 bool visitSelection(glslang::TVisit, glslang::TIntermSelection*);
132 bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*);
133 void visitSymbol(glslang::TIntermSymbol* symbol);
134 bool visitUnary(glslang::TVisit, glslang::TIntermUnary*);
135 bool visitLoop(glslang::TVisit, glslang::TIntermLoop*);
136 bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*);
137
John Kessenichfca82622016-11-26 13:23:20 -0700138 void finishSpv();
John Kessenich7ba63412015-12-20 17:37:07 -0700139 void dumpSpv(std::vector<unsigned int>& out);
John Kessenich140f3df2015-06-26 16:58:36 -0600140
141protected:
John Kessenich5d610ee2018-03-07 18:05:55 -0700142 TGlslangToSpvTraverser(TGlslangToSpvTraverser&);
143 TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&);
144
Rex Xu17ff3432016-10-14 17:41:45 +0800145 spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier);
Rex Xubbceed72016-05-21 09:40:44 +0800146 spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier);
John Kessenich5611c6d2018-04-05 11:25:02 -0600147 spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier);
Jeff Bolz36831c92018-09-05 10:11:41 -0500148 spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type);
149 spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
150 spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
151 spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags);
David Netoa901ffe2016-06-08 14:11:40 +0100152 spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration);
John Kessenich5d0fa972016-02-15 11:57:00 -0700153 spv::ImageFormat TranslateImageFormat(const glslang::TType& type);
John Kesseniche18fd202018-01-30 11:01:39 -0700154 spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const;
155 spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const;
John Kessenich1f4d0462019-01-12 17:31:41 +0700156 spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const;
John Kessenicha5c5fb62017-05-05 05:09:58 -0600157 spv::StorageClass TranslateStorageClass(const glslang::TType&);
John Kessenich5611c6d2018-04-05 11:25:02 -0600158 void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType);
John Kessenich9c14f772019-06-17 08:38:35 -0600159 spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType);
John Kessenich140f3df2015-06-26 16:58:36 -0600160 spv::Id getSampledType(const glslang::TSampler&);
John Kessenich8c8505c2016-07-26 12:50:38 -0600161 spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&);
162 spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult);
163 void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600164 spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false);
John Kessenichead86222018-03-28 18:01:20 -0600165 spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&,
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600166 bool lastBufferBlockMember, bool forwardReferenceOnly = false);
John Kessenich0e737842017-03-24 18:38:16 -0600167 bool filterMember(const glslang::TType& member);
John Kessenich6090df02016-06-30 21:18:02 -0600168 spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct,
169 glslang::TLayoutPacking, const glslang::TQualifier&);
170 void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking,
171 const glslang::TQualifier&, spv::Id);
John Kessenich6c292d32016-02-15 20:58:50 -0700172 spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim);
John Kessenich32cfd492016-02-02 12:37:46 -0700173 spv::Id accessChainLoad(const glslang::TType& type);
Rex Xu27253232016-02-23 17:51:09 +0800174 void accessChainStore(const glslang::TType& type, spv::Id rvalue);
John Kessenich4bf71552016-09-02 11:20:21 -0600175 void multiTypeStore(const glslang::TType&, spv::Id rValue);
John Kessenichf85e8062015-12-19 13:57:10 -0700176 glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const;
John Kessenich3ac051e2015-12-20 11:29:16 -0700177 int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
178 int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix);
John Kessenich5d610ee2018-03-07 18:05:55 -0700179 void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset,
180 int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix);
David Netoa901ffe2016-06-08 14:11:40 +0100181 void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember);
John Kessenich140f3df2015-06-26 16:58:36 -0600182
John Kessenich6fccb3c2016-09-19 16:01:41 -0600183 bool isShaderEntryPoint(const glslang::TIntermAggregate* node);
John Kessenichd3ed90b2018-05-04 11:43:03 -0600184 bool writableParam(glslang::TStorageQualifier) const;
John Kessenichd41993d2017-09-10 15:21:05 -0600185 bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam);
John Kessenich140f3df2015-06-26 16:58:36 -0600186 void makeFunctions(const glslang::TIntermSequence&);
187 void makeGlobalInitializers(const glslang::TIntermSequence&);
188 void visitFunctions(const glslang::TIntermSequence&);
189 void handleFunctionEntry(const glslang::TIntermAggregate* node);
Jeff Bolz38a52fc2019-06-14 09:56:28 -0500190 void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -0600191 void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments);
192 spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node);
John Kessenich140f3df2015-06-26 16:58:36 -0600193 spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*);
194
John Kessenichead86222018-03-28 18:01:20 -0600195 spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right,
196 glslang::TBasicType typeProxy, bool reduceComparison = true);
197 spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right);
198 spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand,
Jeff Bolz38a52fc2019-06-14 09:56:28 -0500199 glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -0600200 spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand,
201 glslang::TBasicType typeProxy);
202 spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand,
203 glslang::TBasicType typeProxy);
John Kessenichad7645f2018-06-04 19:11:25 -0600204 spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize);
John Kessenich140f3df2015-06-26 16:58:36 -0600205 spv::Id makeSmearedConstant(spv::Id constant, int vectorSize);
Jeff Bolz38a52fc2019-06-14 09:56:28 -0500206 spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags);
Rex Xu51596642016-09-21 18:56:12 +0800207 spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
Rex Xu430ef402016-10-14 17:22:23 +0800208 spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands);
John Kessenich66011cb2018-03-06 16:12:04 -0700209 spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -0600210 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 +0800211 spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600212 spv::Id getSymbolId(const glslang::TIntermSymbol* node);
Chao Chen3c366992018-09-19 11:41:59 -0700213 void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier);
qining08408382016-03-21 09:51:37 -0400214 spv::Id createSpvConstant(const glslang::TIntermTyped&);
215 spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant);
John Kessenich7c1aa102015-10-15 13:29:11 -0600216 bool isTrivialLeaf(const glslang::TIntermTyped* node);
217 bool isTrivial(const glslang::TIntermTyped* node);
218 spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right);
Rex Xu9d93a232016-05-05 12:30:44 +0800219 spv::Id getExtBuiltins(const char* name);
John Kessenich9c14f772019-06-17 08:38:35 -0600220 std::pair<spv::Id, spv::Id> getForcedType(spv::BuiltIn, const glslang::TType&);
221 spv::Id translateForcedType(spv::Id object);
Jeff Bolz53134492019-06-25 13:31:10 -0500222 spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents);
John Kessenich140f3df2015-06-26 16:58:36 -0600223
John Kessenich121853f2017-05-31 17:11:16 -0600224 glslang::SpvOptions& options;
John Kessenich140f3df2015-06-26 16:58:36 -0600225 spv::Function* shaderEntry;
John Kesseniched33e052016-10-06 12:59:51 -0600226 spv::Function* currentFunction;
John Kessenich55e7d112015-11-15 21:33:39 -0700227 spv::Instruction* entryPoint;
John Kessenich140f3df2015-06-26 16:58:36 -0600228 int sequenceDepth;
229
Lei Zhang17535f72016-05-04 15:55:59 -0400230 spv::SpvBuildLogger* logger;
Lei Zhang09caf122016-05-02 18:11:54 -0400231
John Kessenich140f3df2015-06-26 16:58:36 -0600232 // There is a 1:1 mapping between a spv builder and a module; this is thread safe
233 spv::Builder builder;
John Kessenich517fe7a2016-11-26 13:31:47 -0700234 bool inEntryPoint;
235 bool entryPointTerminated;
John Kessenich7ba63412015-12-20 17:37:07 -0700236 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 -0700237 std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface
John Kessenich140f3df2015-06-26 16:58:36 -0600238 const glslang::TIntermediate* glslangIntermediate;
John Kessenich605afc72019-06-17 23:33:09 -0600239 bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp
John Kessenich140f3df2015-06-26 16:58:36 -0600240 spv::Id stdBuiltins;
Rex Xu9d93a232016-05-05 12:30:44 +0800241 std::unordered_map<const char*, spv::Id> extBuiltinMap;
John Kessenich140f3df2015-06-26 16:58:36 -0600242
John Kessenich2f273362015-07-18 22:34:27 -0600243 std::unordered_map<int, spv::Id> symbolValues;
John Kessenich4bf71552016-09-02 11:20:21 -0600244 std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer
John Kessenich2f273362015-07-18 22:34:27 -0600245 std::unordered_map<std::string, spv::Function*> functionMap;
John Kessenich3ac051e2015-12-20 11:29:16 -0700246 std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount];
John Kessenich5d610ee2018-03-07 18:05:55 -0700247 // for mapping glslang block indices to spv indices (e.g., due to hidden members):
248 std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper;
John Kessenich140f3df2015-06-26 16:58:36 -0600249 std::stack<bool> breakForLoop; // false means break for switch
John Kessenich5d610ee2018-03-07 18:05:55 -0700250 std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator;
Jeff Bolz9f2aec42019-01-06 17:58:04 -0600251 // Map pointee types for EbtReference to their forward pointers
252 std::map<const glslang::TType *, spv::Id> forwardPointers;
John Kessenich9c14f772019-06-17 08:38:35 -0600253 // Type forcing, for when SPIR-V wants a different type than the AST,
254 // requiring local translation to and from SPIR-V type on every access.
255 // Maps <builtin-variable-id -> AST-required-type-id>
256 std::unordered_map<spv::Id, spv::Id> forceType;
John Kessenich140f3df2015-06-26 16:58:36 -0600257};
258
259//
260// Helper functions for translating glslang representations to SPIR-V enumerants.
261//
262
263// Translate glslang profile to SPIR-V source language.
John Kessenich66e2faf2016-03-12 18:34:36 -0700264spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
John Kessenich140f3df2015-06-26 16:58:36 -0600265{
John Kessenich155d3512019-08-08 23:29:20 -0600266#ifdef GLSLANG_WEB
267 return spv::SourceLanguageESSL;
268#endif
269
John Kessenich66e2faf2016-03-12 18:34:36 -0700270 switch (source) {
271 case glslang::EShSourceGlsl:
272 switch (profile) {
273 case ENoProfile:
274 case ECoreProfile:
275 case ECompatibilityProfile:
276 return spv::SourceLanguageGLSL;
277 case EEsProfile:
278 return spv::SourceLanguageESSL;
279 default:
280 return spv::SourceLanguageUnknown;
281 }
282 case glslang::EShSourceHlsl:
John Kessenich6fa17642017-04-07 15:33:08 -0600283 return spv::SourceLanguageHLSL;
John Kessenich140f3df2015-06-26 16:58:36 -0600284 default:
285 return spv::SourceLanguageUnknown;
286 }
287}
288
289// Translate glslang language (stage) to SPIR-V execution model.
290spv::ExecutionModel TranslateExecutionModel(EShLanguage stage)
291{
292 switch (stage) {
293 case EShLangVertex: return spv::ExecutionModelVertex;
John Kessenicha28f7a72019-08-06 07:00:58 -0600294 case EShLangFragment: return spv::ExecutionModelFragment;
295#ifndef GLSLANG_WEB
296 case EShLangCompute: return spv::ExecutionModelGLCompute;
John Kessenich140f3df2015-06-26 16:58:36 -0600297 case EShLangTessControl: return spv::ExecutionModelTessellationControl;
298 case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation;
299 case EShLangGeometry: return spv::ExecutionModelGeometry;
Ashwin Leleff1783d2018-10-22 16:41:44 -0700300 case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV;
301 case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV;
302 case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV;
303 case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV;
304 case EShLangMissNV: return spv::ExecutionModelMissNV;
305 case EShLangCallableNV: return spv::ExecutionModelCallableNV;
Chao Chen3c366992018-09-19 11:41:59 -0700306 case EShLangTaskNV: return spv::ExecutionModelTaskNV;
307 case EShLangMeshNV: return spv::ExecutionModelMeshNV;
308#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600309 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700310 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600311 return spv::ExecutionModelFragment;
312 }
313}
314
John Kessenich140f3df2015-06-26 16:58:36 -0600315// Translate glslang sampler type to SPIR-V dimensionality.
316spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
317{
318 switch (sampler.dim) {
John Kessenich55e7d112015-11-15 21:33:39 -0700319 case glslang::Esd1D: return spv::Dim1D;
320 case glslang::Esd2D: return spv::Dim2D;
321 case glslang::Esd3D: return spv::Dim3D;
322 case glslang::EsdCube: return spv::DimCube;
323 case glslang::EsdRect: return spv::DimRect;
324 case glslang::EsdBuffer: return spv::DimBuffer;
John Kessenich6c292d32016-02-15 20:58:50 -0700325 case glslang::EsdSubpass: return spv::DimSubpassData;
John Kessenich140f3df2015-06-26 16:58:36 -0600326 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700327 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600328 return spv::Dim2D;
329 }
330}
331
John Kessenichf6640762016-08-01 19:44:00 -0600332// Translate glslang precision to SPIR-V precision decorations.
333spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
John Kessenich140f3df2015-06-26 16:58:36 -0600334{
John Kessenichf6640762016-08-01 19:44:00 -0600335 switch (glslangPrecision) {
John Kessenich61c47a92015-12-14 18:21:19 -0700336 case glslang::EpqLow: return spv::DecorationRelaxedPrecision;
John Kessenich5e4b1242015-08-06 22:53:06 -0600337 case glslang::EpqMedium: return spv::DecorationRelaxedPrecision;
John Kessenich140f3df2015-06-26 16:58:36 -0600338 default:
339 return spv::NoPrecision;
340 }
341}
342
John Kessenichf6640762016-08-01 19:44:00 -0600343// Translate glslang type to SPIR-V precision decorations.
344spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
345{
346 return TranslatePrecisionDecoration(type.getQualifier().precision);
347}
348
John Kessenich140f3df2015-06-26 16:58:36 -0600349// Translate glslang type to SPIR-V block decorations.
John Kessenich67027182017-04-19 18:34:49 -0600350spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer)
John Kessenich140f3df2015-06-26 16:58:36 -0600351{
352 if (type.getBasicType() == glslang::EbtBlock) {
353 switch (type.getQualifier().storage) {
354 case glslang::EvqUniform: return spv::DecorationBlock;
John Kessenich67027182017-04-19 18:34:49 -0600355 case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock;
John Kessenich140f3df2015-06-26 16:58:36 -0600356 case glslang::EvqVaryingIn: return spv::DecorationBlock;
357 case glslang::EvqVaryingOut: return spv::DecorationBlock;
John Kessenicha28f7a72019-08-06 07:00:58 -0600358#ifndef GLSLANG_WEB
Chao Chenb50c02e2018-09-19 11:42:24 -0700359 case glslang::EvqPayloadNV: return spv::DecorationBlock;
360 case glslang::EvqPayloadInNV: return spv::DecorationBlock;
361 case glslang::EvqHitAttrNV: return spv::DecorationBlock;
Ashwin Leleff1783d2018-10-22 16:41:44 -0700362 case glslang::EvqCallableDataNV: return spv::DecorationBlock;
363 case glslang::EvqCallableDataInNV: return spv::DecorationBlock;
Chao Chenb50c02e2018-09-19 11:42:24 -0700364#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600365 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700366 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -0600367 break;
368 }
369 }
370
John Kessenich4016e382016-07-15 11:53:56 -0600371 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600372}
373
Rex Xu1da878f2016-02-21 20:59:01 +0800374// Translate glslang type to SPIR-V memory decorations.
Jeff Bolz36831c92018-09-05 10:11:41 -0500375void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, bool useVulkanMemoryModel)
Rex Xu1da878f2016-02-21 20:59:01 +0800376{
John Kessenichb9197c82019-08-11 07:41:45 -0600377#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -0500378 if (!useVulkanMemoryModel) {
379 if (qualifier.coherent)
380 memory.push_back(spv::DecorationCoherent);
381 if (qualifier.volatil) {
382 memory.push_back(spv::DecorationVolatile);
383 memory.push_back(spv::DecorationCoherent);
384 }
John Kessenich14b85d32018-06-04 15:36:03 -0600385 }
Rex Xu1da878f2016-02-21 20:59:01 +0800386 if (qualifier.restrict)
387 memory.push_back(spv::DecorationRestrict);
John Kessenichdeec1932019-08-13 08:00:30 -0600388 if (qualifier.isReadOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800389 memory.push_back(spv::DecorationNonWritable);
John Kessenichdeec1932019-08-13 08:00:30 -0600390 if (qualifier.isWriteOnly())
Rex Xu1da878f2016-02-21 20:59:01 +0800391 memory.push_back(spv::DecorationNonReadable);
John Kessenichb9197c82019-08-11 07:41:45 -0600392#endif
Rex Xu1da878f2016-02-21 20:59:01 +0800393}
394
John Kessenich140f3df2015-06-26 16:58:36 -0600395// Translate glslang type to SPIR-V layout decorations.
John Kessenich3ac051e2015-12-20 11:29:16 -0700396spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
John Kessenich140f3df2015-06-26 16:58:36 -0600397{
398 if (type.isMatrix()) {
John Kessenich3ac051e2015-12-20 11:29:16 -0700399 switch (matrixLayout) {
John Kessenich140f3df2015-06-26 16:58:36 -0600400 case glslang::ElmRowMajor:
401 return spv::DecorationRowMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700402 case glslang::ElmColumnMajor:
John Kessenich140f3df2015-06-26 16:58:36 -0600403 return spv::DecorationColMajor;
John Kessenich3ac051e2015-12-20 11:29:16 -0700404 default:
405 // opaque layouts don't need a majorness
John Kessenich4016e382016-07-15 11:53:56 -0600406 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600407 }
408 } else {
409 switch (type.getBasicType()) {
410 default:
John Kessenich4016e382016-07-15 11:53:56 -0600411 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600412 break;
413 case glslang::EbtBlock:
414 switch (type.getQualifier().storage) {
415 case glslang::EvqUniform:
416 case glslang::EvqBuffer:
417 switch (type.getQualifier().layoutPacking) {
418 case glslang::ElpShared: return spv::DecorationGLSLShared;
John Kessenich140f3df2015-06-26 16:58:36 -0600419 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
420 default:
John Kessenich4016e382016-07-15 11:53:56 -0600421 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600422 }
423 case glslang::EvqVaryingIn:
424 case glslang::EvqVaryingOut:
Chao Chen3c366992018-09-19 11:41:59 -0700425 if (type.getQualifier().isTaskMemory()) {
426 switch (type.getQualifier().layoutPacking) {
427 case glslang::ElpShared: return spv::DecorationGLSLShared;
428 case glslang::ElpPacked: return spv::DecorationGLSLPacked;
429 default: break;
430 }
431 } else {
432 assert(type.getQualifier().layoutPacking == glslang::ElpNone);
433 }
John Kessenich4016e382016-07-15 11:53:56 -0600434 return spv::DecorationMax;
John Kessenicha28f7a72019-08-06 07:00:58 -0600435#ifndef GLSLANG_WEB
Chao Chenb50c02e2018-09-19 11:42:24 -0700436 case glslang::EvqPayloadNV:
437 case glslang::EvqPayloadInNV:
438 case glslang::EvqHitAttrNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700439 case glslang::EvqCallableDataNV:
440 case glslang::EvqCallableDataInNV:
Chao Chenb50c02e2018-09-19 11:42:24 -0700441 return spv::DecorationMax;
442#endif
John Kessenich140f3df2015-06-26 16:58:36 -0600443 default:
John Kessenich55e7d112015-11-15 21:33:39 -0700444 assert(0);
John Kessenich4016e382016-07-15 11:53:56 -0600445 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600446 }
447 }
448 }
449}
450
451// Translate glslang type to SPIR-V interpolation decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600452// Returns spv::DecorationMax when no decoration
John Kessenich55e7d112015-11-15 21:33:39 -0700453// should be applied.
Rex Xu17ff3432016-10-14 17:41:45 +0800454spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600455{
Rex Xubbceed72016-05-21 09:40:44 +0800456 if (qualifier.smooth)
John Kessenich55e7d112015-11-15 21:33:39 -0700457 // Smooth decoration doesn't exist in SPIR-V 1.0
John Kessenich4016e382016-07-15 11:53:56 -0600458 return spv::DecorationMax;
John Kessenich7015bd62019-08-01 03:28:08 -0600459 else if (qualifier.isNonPerspective())
John Kessenich55e7d112015-11-15 21:33:39 -0700460 return spv::DecorationNoPerspective;
John Kesseniche0b6cad2015-12-24 10:30:13 -0700461 else if (qualifier.flat)
John Kessenich140f3df2015-06-26 16:58:36 -0600462 return spv::DecorationFlat;
John Kessenicha28f7a72019-08-06 07:00:58 -0600463 else if (qualifier.isExplicitInterpolation()) {
Rex Xu17ff3432016-10-14 17:41:45 +0800464 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
Rex Xu9d93a232016-05-05 12:30:44 +0800465 return spv::DecorationExplicitInterpAMD;
Rex Xu17ff3432016-10-14 17:41:45 +0800466 }
Rex Xubbceed72016-05-21 09:40:44 +0800467 else
John Kessenich4016e382016-07-15 11:53:56 -0600468 return spv::DecorationMax;
Rex Xubbceed72016-05-21 09:40:44 +0800469}
470
471// Translate glslang type to SPIR-V auxiliary storage decorations.
John Kessenich4016e382016-07-15 11:53:56 -0600472// Returns spv::DecorationMax when no decoration
Rex Xubbceed72016-05-21 09:40:44 +0800473// should be applied.
474spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
475{
John Kessenichb9197c82019-08-11 07:41:45 -0600476 if (qualifier.centroid)
John Kessenich140f3df2015-06-26 16:58:36 -0600477 return spv::DecorationCentroid;
John Kessenichb9197c82019-08-11 07:41:45 -0600478#ifndef GLSLANG_WEB
479 else if (qualifier.patch)
480 return spv::DecorationPatch;
John Kessenich5e801132016-02-15 11:09:46 -0700481 else if (qualifier.sample) {
482 builder.addCapability(spv::CapabilitySampleRateShading);
John Kessenich140f3df2015-06-26 16:58:36 -0600483 return spv::DecorationSample;
John Kessenichb9197c82019-08-11 07:41:45 -0600484 }
485#endif
486
487 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600488}
489
John Kessenich92187592016-02-01 13:45:25 -0700490// If glslang type is invariant, return SPIR-V invariant decoration.
John Kesseniche0b6cad2015-12-24 10:30:13 -0700491spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
John Kessenich140f3df2015-06-26 16:58:36 -0600492{
John Kesseniche0b6cad2015-12-24 10:30:13 -0700493 if (qualifier.invariant)
John Kessenich140f3df2015-06-26 16:58:36 -0600494 return spv::DecorationInvariant;
495 else
John Kessenich4016e382016-07-15 11:53:56 -0600496 return spv::DecorationMax;
John Kessenich140f3df2015-06-26 16:58:36 -0600497}
498
qining9220dbb2016-05-04 17:34:38 -0400499// If glslang type is noContraction, return SPIR-V NoContraction decoration.
500spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
501{
John Kessenichb9197c82019-08-11 07:41:45 -0600502#ifndef GLSLANG_WEB
John Kessenicha28f7a72019-08-06 07:00:58 -0600503 if (qualifier.isNoContraction())
qining9220dbb2016-05-04 17:34:38 -0400504 return spv::DecorationNoContraction;
505 else
John Kessenichb9197c82019-08-11 07:41:45 -0600506#endif
John Kessenich4016e382016-07-15 11:53:56 -0600507 return spv::DecorationMax;
qining9220dbb2016-05-04 17:34:38 -0400508}
509
John Kessenich5611c6d2018-04-05 11:25:02 -0600510// If glslang type is nonUniform, return SPIR-V NonUniform decoration.
511spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
512{
John Kessenichb9197c82019-08-11 07:41:45 -0600513#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -0600514 if (qualifier.isNonUniform()) {
John Kessenich8317e6c2019-08-18 23:58:08 -0600515 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -0600516 builder.addCapability(spv::CapabilityShaderNonUniformEXT);
517 return spv::DecorationNonUniformEXT;
518 } else
John Kessenichb9197c82019-08-11 07:41:45 -0600519#endif
John Kessenich5611c6d2018-04-05 11:25:02 -0600520 return spv::DecorationMax;
521}
522
John Kessenichb9197c82019-08-11 07:41:45 -0600523spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(
524 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500525{
Jeff Bolz36831c92018-09-05 10:11:41 -0500526 spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600527
528#ifndef GLSLANG_WEB
529 if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage)
530 return mask;
531
Jeff Bolz36831c92018-09-05 10:11:41 -0500532 if (coherentFlags.volatil ||
533 coherentFlags.coherent ||
534 coherentFlags.devicecoherent ||
535 coherentFlags.queuefamilycoherent ||
536 coherentFlags.workgroupcoherent ||
537 coherentFlags.subgroupcoherent) {
538 mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask |
539 spv::MemoryAccessMakePointerVisibleKHRMask;
540 }
541 if (coherentFlags.nonprivate) {
542 mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask;
543 }
544 if (coherentFlags.volatil) {
545 mask = mask | spv::MemoryAccessVolatileMask;
546 }
547 if (mask != spv::MemoryAccessMaskNone) {
548 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
549 }
John Kessenichb9197c82019-08-11 07:41:45 -0600550#endif
551
Jeff Bolz36831c92018-09-05 10:11:41 -0500552 return mask;
553}
554
John Kessenichb9197c82019-08-11 07:41:45 -0600555spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(
556 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500557{
Jeff Bolz36831c92018-09-05 10:11:41 -0500558 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenichb9197c82019-08-11 07:41:45 -0600559
560#ifndef GLSLANG_WEB
561 if (!glslangIntermediate->usingVulkanMemoryModel())
562 return mask;
563
Jeff Bolz36831c92018-09-05 10:11:41 -0500564 if (coherentFlags.volatil ||
565 coherentFlags.coherent ||
566 coherentFlags.devicecoherent ||
567 coherentFlags.queuefamilycoherent ||
568 coherentFlags.workgroupcoherent ||
569 coherentFlags.subgroupcoherent) {
570 mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask |
571 spv::ImageOperandsMakeTexelVisibleKHRMask;
572 }
573 if (coherentFlags.nonprivate) {
574 mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask;
575 }
576 if (coherentFlags.volatil) {
577 mask = mask | spv::ImageOperandsVolatileTexelKHRMask;
578 }
579 if (mask != spv::ImageOperandsMaskNone) {
580 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
581 }
John Kessenichb9197c82019-08-11 07:41:45 -0600582#endif
583
Jeff Bolz36831c92018-09-05 10:11:41 -0500584 return mask;
585}
586
587spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
588{
John Kessenichb9197c82019-08-11 07:41:45 -0600589 spv::Builder::AccessChain::CoherentFlags flags = {};
590#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -0500591 flags.coherent = type.getQualifier().coherent;
592 flags.devicecoherent = type.getQualifier().devicecoherent;
593 flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent;
594 // shared variables are implicitly workgroupcoherent in GLSL.
595 flags.workgroupcoherent = type.getQualifier().workgroupcoherent ||
596 type.getQualifier().storage == glslang::EvqShared;
597 flags.subgroupcoherent = type.getQualifier().subgroupcoherent;
Jeff Bolz38cbad12019-03-05 14:40:07 -0600598 flags.volatil = type.getQualifier().volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500599 // *coherent variables are implicitly nonprivate in GLSL
600 flags.nonprivate = type.getQualifier().nonprivate ||
Jeff Bolzab3c9652018-10-15 22:46:48 -0500601 flags.subgroupcoherent ||
602 flags.workgroupcoherent ||
603 flags.queuefamilycoherent ||
604 flags.devicecoherent ||
Jeff Bolz38cbad12019-03-05 14:40:07 -0600605 flags.coherent ||
606 flags.volatil;
Jeff Bolz36831c92018-09-05 10:11:41 -0500607 flags.isImage = type.getBasicType() == glslang::EbtSampler;
John Kessenichb9197c82019-08-11 07:41:45 -0600608#endif
Jeff Bolz36831c92018-09-05 10:11:41 -0500609 return flags;
610}
611
John Kessenichb9197c82019-08-11 07:41:45 -0600612spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(
613 const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
Jeff Bolz36831c92018-09-05 10:11:41 -0500614{
John Kessenichb9197c82019-08-11 07:41:45 -0600615 spv::Scope scope = spv::ScopeMax;
616
617#ifndef GLSLANG_WEB
Jeff Bolz38cbad12019-03-05 14:40:07 -0600618 if (coherentFlags.volatil || coherentFlags.coherent) {
Jeff Bolz36831c92018-09-05 10:11:41 -0500619 // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model
620 scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
621 } else if (coherentFlags.devicecoherent) {
622 scope = spv::ScopeDevice;
623 } else if (coherentFlags.queuefamilycoherent) {
624 scope = spv::ScopeQueueFamilyKHR;
625 } else if (coherentFlags.workgroupcoherent) {
626 scope = spv::ScopeWorkgroup;
627 } else if (coherentFlags.subgroupcoherent) {
628 scope = spv::ScopeSubgroup;
Jeff Bolz36831c92018-09-05 10:11:41 -0500629 }
630 if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) {
631 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
632 }
John Kessenichb9197c82019-08-11 07:41:45 -0600633#endif
634
Jeff Bolz36831c92018-09-05 10:11:41 -0500635 return scope;
636}
637
David Netoa901ffe2016-06-08 14:11:40 +0100638// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate
639// associated capabilities when required. For some built-in variables, a capability
640// is generated only when using the variable in an executable instruction, but not when
641// just declaring a struct member variable with it. This is true for PointSize,
642// ClipDistance, and CullDistance.
643spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration)
John Kessenich140f3df2015-06-26 16:58:36 -0600644{
645 switch (builtIn) {
John Kessenich92187592016-02-01 13:45:25 -0700646 case glslang::EbvPointSize:
John Kessenich155d3512019-08-08 23:29:20 -0600647#ifndef GLSLANG_WEB
John Kessenich78a45572016-07-08 14:05:15 -0600648 // Defer adding the capability until the built-in is actually used.
649 if (! memberDeclaration) {
650 switch (glslangIntermediate->getStage()) {
651 case EShLangGeometry:
652 builder.addCapability(spv::CapabilityGeometryPointSize);
653 break;
654 case EShLangTessControl:
655 case EShLangTessEvaluation:
656 builder.addCapability(spv::CapabilityTessellationPointSize);
657 break;
658 default:
659 break;
660 }
John Kessenich92187592016-02-01 13:45:25 -0700661 }
John Kessenich155d3512019-08-08 23:29:20 -0600662#endif
John Kessenich92187592016-02-01 13:45:25 -0700663 return spv::BuiltInPointSize;
664
John Kessenicha28f7a72019-08-06 07:00:58 -0600665 case glslang::EbvPosition: return spv::BuiltInPosition;
666 case glslang::EbvVertexId: return spv::BuiltInVertexId;
667 case glslang::EbvInstanceId: return spv::BuiltInInstanceId;
668 case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex;
669 case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex;
670
671 case glslang::EbvFragCoord: return spv::BuiltInFragCoord;
672 case glslang::EbvPointCoord: return spv::BuiltInPointCoord;
673 case glslang::EbvFace: return spv::BuiltInFrontFacing;
674 case glslang::EbvFragDepth: return spv::BuiltInFragDepth;
675
676#ifndef GLSLANG_WEB
John Kessenichebb50532016-05-16 19:22:05 -0600677 // These *Distance capabilities logically belong here, but if the member is declared and
678 // then never used, consumers of SPIR-V prefer the capability not be declared.
679 // They are now generated when used, rather than here when declared.
680 // Potentially, the specification should be more clear what the minimum
681 // use needed is to trigger the capability.
682 //
John Kessenich92187592016-02-01 13:45:25 -0700683 case glslang::EbvClipDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100684 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800685 builder.addCapability(spv::CapabilityClipDistance);
John Kessenich92187592016-02-01 13:45:25 -0700686 return spv::BuiltInClipDistance;
687
688 case glslang::EbvCullDistance:
David Netoa901ffe2016-06-08 14:11:40 +0100689 if (!memberDeclaration)
Rex Xu3e783f92017-02-22 16:44:48 +0800690 builder.addCapability(spv::CapabilityCullDistance);
John Kessenich92187592016-02-01 13:45:25 -0700691 return spv::BuiltInCullDistance;
692
693 case glslang::EbvViewportIndex:
John Kessenichba6a3c22017-09-13 13:22:50 -0600694 builder.addCapability(spv::CapabilityMultiViewport);
695 if (glslangIntermediate->getStage() == EShLangVertex ||
696 glslangIntermediate->getStage() == EShLangTessControl ||
697 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800698
John Kessenich8317e6c2019-08-18 23:58:08 -0600699 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
John Kessenichba6a3c22017-09-13 13:22:50 -0600700 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800701 }
John Kessenich92187592016-02-01 13:45:25 -0700702 return spv::BuiltInViewportIndex;
703
John Kessenich5e801132016-02-15 11:09:46 -0700704 case glslang::EbvSampleId:
705 builder.addCapability(spv::CapabilitySampleRateShading);
706 return spv::BuiltInSampleId;
707
708 case glslang::EbvSamplePosition:
709 builder.addCapability(spv::CapabilitySampleRateShading);
710 return spv::BuiltInSamplePosition;
711
712 case glslang::EbvSampleMask:
John Kessenich5e801132016-02-15 11:09:46 -0700713 return spv::BuiltInSampleMask;
714
John Kessenich78a45572016-07-08 14:05:15 -0600715 case glslang::EbvLayer:
Chao Chen3c366992018-09-19 11:41:59 -0700716 if (glslangIntermediate->getStage() == EShLangMeshNV) {
717 return spv::BuiltInLayer;
718 }
John Kessenichba6a3c22017-09-13 13:22:50 -0600719 builder.addCapability(spv::CapabilityGeometry);
720 if (glslangIntermediate->getStage() == EShLangVertex ||
721 glslangIntermediate->getStage() == EShLangTessControl ||
722 glslangIntermediate->getStage() == EShLangTessEvaluation) {
Rex Xu5e317ff2017-03-16 23:02:39 +0800723
John Kessenich8317e6c2019-08-18 23:58:08 -0600724 builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5);
John Kessenichba6a3c22017-09-13 13:22:50 -0600725 builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT);
Rex Xu5e317ff2017-03-16 23:02:39 +0800726 }
John Kessenich78a45572016-07-08 14:05:15 -0600727 return spv::BuiltInLayer;
728
John Kessenichda581a22015-10-14 14:10:30 -0600729 case glslang::EbvBaseVertex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600730 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800731 builder.addCapability(spv::CapabilityDrawParameters);
732 return spv::BuiltInBaseVertex;
733
John Kessenichda581a22015-10-14 14:10:30 -0600734 case glslang::EbvBaseInstance:
John Kessenich8317e6c2019-08-18 23:58:08 -0600735 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800736 builder.addCapability(spv::CapabilityDrawParameters);
737 return spv::BuiltInBaseInstance;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200738
John Kessenichda581a22015-10-14 14:10:30 -0600739 case glslang::EbvDrawId:
John Kessenich8317e6c2019-08-18 23:58:08 -0600740 builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3);
Rex Xuf3b27472016-07-22 18:15:31 +0800741 builder.addCapability(spv::CapabilityDrawParameters);
742 return spv::BuiltInDrawIndex;
Maciej Jesionowski04b3e872016-09-26 16:49:09 +0200743
744 case glslang::EbvPrimitiveId:
745 if (glslangIntermediate->getStage() == EShLangFragment)
746 builder.addCapability(spv::CapabilityGeometry);
747 return spv::BuiltInPrimitiveId;
748
Rex Xu37cdcee2017-06-29 17:46:34 +0800749 case glslang::EbvFragStencilRef:
Rex Xue8fdd792017-08-23 23:24:42 +0800750 builder.addExtension(spv::E_SPV_EXT_shader_stencil_export);
751 builder.addCapability(spv::CapabilityStencilExportEXT);
752 return spv::BuiltInFragStencilRefEXT;
Rex Xu37cdcee2017-06-29 17:46:34 +0800753
John Kessenich140f3df2015-06-26 16:58:36 -0600754 case glslang::EbvInvocationId: return spv::BuiltInInvocationId;
John Kessenich140f3df2015-06-26 16:58:36 -0600755 case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner;
756 case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter;
757 case glslang::EbvTessCoord: return spv::BuiltInTessCoord;
758 case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices;
John Kessenich140f3df2015-06-26 16:58:36 -0600759 case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation;
760 case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups;
761 case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize;
762 case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId;
763 case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId;
764 case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex;
765 case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId;
Rex Xu51596642016-09-21 18:56:12 +0800766
Rex Xu574ab042016-04-14 16:53:07 +0800767 case glslang::EbvSubGroupSize:
Rex Xu36876e62016-09-23 22:13:43 +0800768 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800769 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
770 return spv::BuiltInSubgroupSize;
771
Rex Xu574ab042016-04-14 16:53:07 +0800772 case glslang::EbvSubGroupInvocation:
Rex Xu36876e62016-09-23 22:13:43 +0800773 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +0800774 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
775 return spv::BuiltInSubgroupLocalInvocationId;
776
Rex Xu574ab042016-04-14 16:53:07 +0800777 case glslang::EbvSubGroupEqMask:
Rex Xu51596642016-09-21 18:56:12 +0800778 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
779 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600780 return spv::BuiltInSubgroupEqMask;
Rex Xu51596642016-09-21 18:56:12 +0800781
Rex Xu574ab042016-04-14 16:53:07 +0800782 case glslang::EbvSubGroupGeMask:
Rex Xu51596642016-09-21 18:56:12 +0800783 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
784 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600785 return spv::BuiltInSubgroupGeMask;
Rex Xu51596642016-09-21 18:56:12 +0800786
Rex Xu574ab042016-04-14 16:53:07 +0800787 case glslang::EbvSubGroupGtMask:
Rex Xu51596642016-09-21 18:56:12 +0800788 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
789 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600790 return spv::BuiltInSubgroupGtMask;
Rex Xu51596642016-09-21 18:56:12 +0800791
Rex Xu574ab042016-04-14 16:53:07 +0800792 case glslang::EbvSubGroupLeMask:
Rex Xu51596642016-09-21 18:56:12 +0800793 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
794 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600795 return spv::BuiltInSubgroupLeMask;
Rex Xu51596642016-09-21 18:56:12 +0800796
Rex Xu574ab042016-04-14 16:53:07 +0800797 case glslang::EbvSubGroupLtMask:
Rex Xu51596642016-09-21 18:56:12 +0800798 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
799 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
John Kessenich9c14f772019-06-17 08:38:35 -0600800 return spv::BuiltInSubgroupLtMask;
Rex Xu51596642016-09-21 18:56:12 +0800801
John Kessenich66011cb2018-03-06 16:12:04 -0700802 case glslang::EbvNumSubgroups:
803 builder.addCapability(spv::CapabilityGroupNonUniform);
804 return spv::BuiltInNumSubgroups;
805
806 case glslang::EbvSubgroupID:
807 builder.addCapability(spv::CapabilityGroupNonUniform);
808 return spv::BuiltInSubgroupId;
809
810 case glslang::EbvSubgroupSize2:
811 builder.addCapability(spv::CapabilityGroupNonUniform);
812 return spv::BuiltInSubgroupSize;
813
814 case glslang::EbvSubgroupInvocation2:
815 builder.addCapability(spv::CapabilityGroupNonUniform);
816 return spv::BuiltInSubgroupLocalInvocationId;
817
818 case glslang::EbvSubgroupEqMask2:
819 builder.addCapability(spv::CapabilityGroupNonUniform);
820 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
821 return spv::BuiltInSubgroupEqMask;
822
823 case glslang::EbvSubgroupGeMask2:
824 builder.addCapability(spv::CapabilityGroupNonUniform);
825 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
826 return spv::BuiltInSubgroupGeMask;
827
828 case glslang::EbvSubgroupGtMask2:
829 builder.addCapability(spv::CapabilityGroupNonUniform);
830 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
831 return spv::BuiltInSubgroupGtMask;
832
833 case glslang::EbvSubgroupLeMask2:
834 builder.addCapability(spv::CapabilityGroupNonUniform);
835 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
836 return spv::BuiltInSubgroupLeMask;
837
838 case glslang::EbvSubgroupLtMask2:
839 builder.addCapability(spv::CapabilityGroupNonUniform);
840 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
841 return spv::BuiltInSubgroupLtMask;
John Kessenich9c14f772019-06-17 08:38:35 -0600842
Rex Xu17ff3432016-10-14 17:41:45 +0800843 case glslang::EbvBaryCoordNoPersp:
844 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
845 return spv::BuiltInBaryCoordNoPerspAMD;
846
847 case glslang::EbvBaryCoordNoPerspCentroid:
848 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
849 return spv::BuiltInBaryCoordNoPerspCentroidAMD;
850
851 case glslang::EbvBaryCoordNoPerspSample:
852 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
853 return spv::BuiltInBaryCoordNoPerspSampleAMD;
854
855 case glslang::EbvBaryCoordSmooth:
856 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
857 return spv::BuiltInBaryCoordSmoothAMD;
858
859 case glslang::EbvBaryCoordSmoothCentroid:
860 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
861 return spv::BuiltInBaryCoordSmoothCentroidAMD;
862
863 case glslang::EbvBaryCoordSmoothSample:
864 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
865 return spv::BuiltInBaryCoordSmoothSampleAMD;
866
867 case glslang::EbvBaryCoordPullModel:
868 builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
869 return spv::BuiltInBaryCoordPullModelAMD;
chaoc771d89f2017-01-13 01:10:53 -0800870
John Kessenich6c8aaac2017-02-27 01:20:51 -0700871 case glslang::EbvDeviceIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600872 builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700873 builder.addCapability(spv::CapabilityDeviceGroup);
John Kessenich42e33c92017-02-27 01:50:28 -0700874 return spv::BuiltInDeviceIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700875
876 case glslang::EbvViewIndex:
John Kessenich8317e6c2019-08-18 23:58:08 -0600877 builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3);
John Kessenich6c8aaac2017-02-27 01:20:51 -0700878 builder.addCapability(spv::CapabilityMultiView);
John Kessenich42e33c92017-02-27 01:50:28 -0700879 return spv::BuiltInViewIndex;
John Kessenich6c8aaac2017-02-27 01:20:51 -0700880
Daniel Koch5154db52018-11-26 10:01:58 -0500881 case glslang::EbvFragSizeEXT:
882 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
883 builder.addCapability(spv::CapabilityFragmentDensityEXT);
884 return spv::BuiltInFragSizeEXT;
885
886 case glslang::EbvFragInvocationCountEXT:
887 builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density);
888 builder.addCapability(spv::CapabilityFragmentDensityEXT);
889 return spv::BuiltInFragInvocationCountEXT;
890
chaoc771d89f2017-01-13 01:10:53 -0800891 case glslang::EbvViewportMaskNV:
Rex Xu5e317ff2017-03-16 23:02:39 +0800892 if (!memberDeclaration) {
893 builder.addExtension(spv::E_SPV_NV_viewport_array2);
894 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
895 }
chaoc771d89f2017-01-13 01:10:53 -0800896 return spv::BuiltInViewportMaskNV;
897 case glslang::EbvSecondaryPositionNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800898 if (!memberDeclaration) {
899 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
900 builder.addCapability(spv::CapabilityShaderStereoViewNV);
901 }
chaoc771d89f2017-01-13 01:10:53 -0800902 return spv::BuiltInSecondaryPositionNV;
903 case glslang::EbvSecondaryViewportMaskNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800904 if (!memberDeclaration) {
905 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
906 builder.addCapability(spv::CapabilityShaderStereoViewNV);
907 }
chaoc771d89f2017-01-13 01:10:53 -0800908 return spv::BuiltInSecondaryViewportMaskNV;
chaocdf3956c2017-02-14 14:52:34 -0800909 case glslang::EbvPositionPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800910 if (!memberDeclaration) {
911 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
912 builder.addCapability(spv::CapabilityPerViewAttributesNV);
913 }
chaocdf3956c2017-02-14 14:52:34 -0800914 return spv::BuiltInPositionPerViewNV;
915 case glslang::EbvViewportMaskPerViewNV:
Rex Xu3e783f92017-02-22 16:44:48 +0800916 if (!memberDeclaration) {
917 builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes);
918 builder.addCapability(spv::CapabilityPerViewAttributesNV);
919 }
chaocdf3956c2017-02-14 14:52:34 -0800920 return spv::BuiltInViewportMaskPerViewNV;
Piers Daniell1c5443c2017-12-13 13:07:22 -0700921 case glslang::EbvFragFullyCoveredNV:
922 builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered);
923 builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT);
924 return spv::BuiltInFullyCoveredEXT;
Chao Chen5b2203d2018-09-19 11:43:21 -0700925 case glslang::EbvFragmentSizeNV:
926 builder.addExtension(spv::E_SPV_NV_shading_rate);
927 builder.addCapability(spv::CapabilityShadingRateNV);
928 return spv::BuiltInFragmentSizeNV;
929 case glslang::EbvInvocationsPerPixelNV:
930 builder.addExtension(spv::E_SPV_NV_shading_rate);
931 builder.addCapability(spv::CapabilityShadingRateNV);
932 return spv::BuiltInInvocationsPerPixelNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700933
Daniel Koch593a4e02019-05-27 16:46:31 -0400934 // ray tracing
Chao Chenb50c02e2018-09-19 11:42:24 -0700935 case glslang::EbvLaunchIdNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700936 return spv::BuiltInLaunchIdNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700937 case glslang::EbvLaunchSizeNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700938 return spv::BuiltInLaunchSizeNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700939 case glslang::EbvWorldRayOriginNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700940 return spv::BuiltInWorldRayOriginNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700941 case glslang::EbvWorldRayDirectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700942 return spv::BuiltInWorldRayDirectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700943 case glslang::EbvObjectRayOriginNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700944 return spv::BuiltInObjectRayOriginNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700945 case glslang::EbvObjectRayDirectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700946 return spv::BuiltInObjectRayDirectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700947 case glslang::EbvRayTminNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700948 return spv::BuiltInRayTminNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700949 case glslang::EbvRayTmaxNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700950 return spv::BuiltInRayTmaxNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700951 case glslang::EbvInstanceCustomIndexNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700952 return spv::BuiltInInstanceCustomIndexNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700953 case glslang::EbvHitTNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700954 return spv::BuiltInHitTNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700955 case glslang::EbvHitKindNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700956 return spv::BuiltInHitKindNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700957 case glslang::EbvObjectToWorldNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700958 return spv::BuiltInObjectToWorldNV;
Chao Chenb50c02e2018-09-19 11:42:24 -0700959 case glslang::EbvWorldToObjectNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -0700960 return spv::BuiltInWorldToObjectNV;
961 case glslang::EbvIncomingRayFlagsNV:
962 return spv::BuiltInIncomingRayFlagsNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400963
964 // barycentrics
Chao Chen9eada4b2018-09-19 11:39:56 -0700965 case glslang::EbvBaryCoordNV:
966 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
967 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
968 return spv::BuiltInBaryCoordNV;
969 case glslang::EbvBaryCoordNoPerspNV:
970 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
971 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
972 return spv::BuiltInBaryCoordNoPerspNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400973
974 // mesh shaders
975 case glslang::EbvTaskCountNV:
Chao Chen3c366992018-09-19 11:41:59 -0700976 return spv::BuiltInTaskCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400977 case glslang::EbvPrimitiveCountNV:
Chao Chen3c366992018-09-19 11:41:59 -0700978 return spv::BuiltInPrimitiveCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400979 case glslang::EbvPrimitiveIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -0700980 return spv::BuiltInPrimitiveIndicesNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400981 case glslang::EbvClipDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -0700982 return spv::BuiltInClipDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400983 case glslang::EbvCullDistancePerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -0700984 return spv::BuiltInCullDistancePerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400985 case glslang::EbvLayerPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -0700986 return spv::BuiltInLayerPerViewNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400987 case glslang::EbvMeshViewCountNV:
Chao Chen3c366992018-09-19 11:41:59 -0700988 return spv::BuiltInMeshViewCountNV;
Daniel Koch593a4e02019-05-27 16:46:31 -0400989 case glslang::EbvMeshViewIndicesNV:
Chao Chen3c366992018-09-19 11:41:59 -0700990 return spv::BuiltInMeshViewIndicesNV;
Daniel Koch2cb2f192019-06-04 08:43:32 -0400991
992 // sm builtins
993 case glslang::EbvWarpsPerSM:
994 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
995 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
996 return spv::BuiltInWarpsPerSMNV;
997 case glslang::EbvSMCount:
998 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
999 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1000 return spv::BuiltInSMCountNV;
1001 case glslang::EbvWarpID:
1002 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1003 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1004 return spv::BuiltInWarpIDNV;
1005 case glslang::EbvSMID:
1006 builder.addExtension(spv::E_SPV_NV_shader_sm_builtins);
1007 builder.addCapability(spv::CapabilityShaderSMBuiltinsNV);
1008 return spv::BuiltInSMIDNV;
John Kessenicha28f7a72019-08-06 07:00:58 -06001009#endif
1010
Rex Xu3e783f92017-02-22 16:44:48 +08001011 default:
1012 return spv::BuiltInMax;
John Kessenich140f3df2015-06-26 16:58:36 -06001013 }
1014}
1015
Rex Xufc618912015-09-09 16:42:49 +08001016// Translate glslang image layout format to SPIR-V image format.
John Kessenich5d0fa972016-02-15 11:57:00 -07001017spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
Rex Xufc618912015-09-09 16:42:49 +08001018{
1019 assert(type.getBasicType() == glslang::EbtSampler);
1020
John Kessenichb9197c82019-08-11 07:41:45 -06001021#ifdef GLSLANG_WEB
1022 return spv::ImageFormatUnknown;
1023#endif
1024
John Kessenich5d0fa972016-02-15 11:57:00 -07001025 // Check for capabilities
John Kessenich7015bd62019-08-01 03:28:08 -06001026 switch (type.getQualifier().getFormat()) {
John Kessenich5d0fa972016-02-15 11:57:00 -07001027 case glslang::ElfRg32f:
1028 case glslang::ElfRg16f:
1029 case glslang::ElfR11fG11fB10f:
1030 case glslang::ElfR16f:
1031 case glslang::ElfRgba16:
1032 case glslang::ElfRgb10A2:
1033 case glslang::ElfRg16:
1034 case glslang::ElfRg8:
1035 case glslang::ElfR16:
1036 case glslang::ElfR8:
1037 case glslang::ElfRgba16Snorm:
1038 case glslang::ElfRg16Snorm:
1039 case glslang::ElfRg8Snorm:
1040 case glslang::ElfR16Snorm:
1041 case glslang::ElfR8Snorm:
1042
1043 case glslang::ElfRg32i:
1044 case glslang::ElfRg16i:
1045 case glslang::ElfRg8i:
1046 case glslang::ElfR16i:
1047 case glslang::ElfR8i:
1048
1049 case glslang::ElfRgb10a2ui:
1050 case glslang::ElfRg32ui:
1051 case glslang::ElfRg16ui:
1052 case glslang::ElfRg8ui:
1053 case glslang::ElfR16ui:
1054 case glslang::ElfR8ui:
1055 builder.addCapability(spv::CapabilityStorageImageExtendedFormats);
1056 break;
1057
1058 default:
1059 break;
1060 }
1061
1062 // do the translation
John Kessenich7015bd62019-08-01 03:28:08 -06001063 switch (type.getQualifier().getFormat()) {
Rex Xufc618912015-09-09 16:42:49 +08001064 case glslang::ElfNone: return spv::ImageFormatUnknown;
1065 case glslang::ElfRgba32f: return spv::ImageFormatRgba32f;
1066 case glslang::ElfRgba16f: return spv::ImageFormatRgba16f;
1067 case glslang::ElfR32f: return spv::ImageFormatR32f;
1068 case glslang::ElfRgba8: return spv::ImageFormatRgba8;
1069 case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm;
1070 case glslang::ElfRg32f: return spv::ImageFormatRg32f;
1071 case glslang::ElfRg16f: return spv::ImageFormatRg16f;
1072 case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f;
1073 case glslang::ElfR16f: return spv::ImageFormatR16f;
1074 case glslang::ElfRgba16: return spv::ImageFormatRgba16;
1075 case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2;
1076 case glslang::ElfRg16: return spv::ImageFormatRg16;
1077 case glslang::ElfRg8: return spv::ImageFormatRg8;
1078 case glslang::ElfR16: return spv::ImageFormatR16;
1079 case glslang::ElfR8: return spv::ImageFormatR8;
1080 case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm;
1081 case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm;
1082 case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm;
1083 case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm;
1084 case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm;
1085 case glslang::ElfRgba32i: return spv::ImageFormatRgba32i;
1086 case glslang::ElfRgba16i: return spv::ImageFormatRgba16i;
1087 case glslang::ElfRgba8i: return spv::ImageFormatRgba8i;
1088 case glslang::ElfR32i: return spv::ImageFormatR32i;
1089 case glslang::ElfRg32i: return spv::ImageFormatRg32i;
1090 case glslang::ElfRg16i: return spv::ImageFormatRg16i;
1091 case glslang::ElfRg8i: return spv::ImageFormatRg8i;
1092 case glslang::ElfR16i: return spv::ImageFormatR16i;
1093 case glslang::ElfR8i: return spv::ImageFormatR8i;
1094 case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui;
1095 case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui;
1096 case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui;
1097 case glslang::ElfR32ui: return spv::ImageFormatR32ui;
1098 case glslang::ElfRg32ui: return spv::ImageFormatRg32ui;
1099 case glslang::ElfRg16ui: return spv::ImageFormatRg16ui;
1100 case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui;
1101 case glslang::ElfRg8ui: return spv::ImageFormatRg8ui;
1102 case glslang::ElfR16ui: return spv::ImageFormatR16ui;
1103 case glslang::ElfR8ui: return spv::ImageFormatR8ui;
John Kessenich4016e382016-07-15 11:53:56 -06001104 default: return spv::ImageFormatMax;
Rex Xufc618912015-09-09 16:42:49 +08001105 }
1106}
1107
John Kesseniche18fd202018-01-30 11:01:39 -07001108spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const
Rex Xu57e65922017-07-04 23:23:40 +08001109{
John Kesseniche18fd202018-01-30 11:01:39 -07001110 if (selectionNode.getFlatten())
1111 return spv::SelectionControlFlattenMask;
1112 if (selectionNode.getDontFlatten())
1113 return spv::SelectionControlDontFlattenMask;
1114 return spv::SelectionControlMaskNone;
Rex Xu57e65922017-07-04 23:23:40 +08001115}
1116
John Kesseniche18fd202018-01-30 11:01:39 -07001117spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const
steve-lunargf1709e72017-05-02 20:14:50 -06001118{
John Kesseniche18fd202018-01-30 11:01:39 -07001119 if (switchNode.getFlatten())
1120 return spv::SelectionControlFlattenMask;
1121 if (switchNode.getDontFlatten())
1122 return spv::SelectionControlDontFlattenMask;
1123 return spv::SelectionControlMaskNone;
1124}
1125
John Kessenicha2858d92018-01-31 08:11:18 -07001126// return a non-0 dependency if the dependency argument must be set
1127spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
John Kessenich1f4d0462019-01-12 17:31:41 +07001128 std::vector<unsigned int>& operands) const
John Kesseniche18fd202018-01-30 11:01:39 -07001129{
1130 spv::LoopControlMask control = spv::LoopControlMaskNone;
1131
1132 if (loopNode.getDontUnroll())
1133 control = control | spv::LoopControlDontUnrollMask;
1134 if (loopNode.getUnroll())
1135 control = control | spv::LoopControlUnrollMask;
LoopDawg4425f242018-02-18 11:40:01 -07001136 if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite)
John Kessenicha2858d92018-01-31 08:11:18 -07001137 control = control | spv::LoopControlDependencyInfiniteMask;
1138 else if (loopNode.getLoopDependency() > 0) {
1139 control = control | spv::LoopControlDependencyLengthMask;
John Kessenich1f4d0462019-01-12 17:31:41 +07001140 operands.push_back((unsigned int)loopNode.getLoopDependency());
1141 }
1142 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
1143 if (loopNode.getMinIterations() > 0) {
1144 control = control | spv::LoopControlMinIterationsMask;
1145 operands.push_back(loopNode.getMinIterations());
1146 }
1147 if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) {
1148 control = control | spv::LoopControlMaxIterationsMask;
1149 operands.push_back(loopNode.getMaxIterations());
1150 }
1151 if (loopNode.getIterationMultiple() > 1) {
1152 control = control | spv::LoopControlIterationMultipleMask;
1153 operands.push_back(loopNode.getIterationMultiple());
1154 }
1155 if (loopNode.getPeelCount() > 0) {
1156 control = control | spv::LoopControlPeelCountMask;
1157 operands.push_back(loopNode.getPeelCount());
1158 }
1159 if (loopNode.getPartialCount() > 0) {
1160 control = control | spv::LoopControlPartialCountMask;
1161 operands.push_back(loopNode.getPartialCount());
1162 }
John Kessenicha2858d92018-01-31 08:11:18 -07001163 }
John Kesseniche18fd202018-01-30 11:01:39 -07001164
1165 return control;
steve-lunargf1709e72017-05-02 20:14:50 -06001166}
1167
John Kessenicha5c5fb62017-05-05 05:09:58 -06001168// Translate glslang type to SPIR-V storage class.
1169spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
1170{
1171 if (type.getQualifier().isPipeInput())
1172 return spv::StorageClassInput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001173 if (type.getQualifier().isPipeOutput())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001174 return spv::StorageClassOutput;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001175
1176 if (glslangIntermediate->getSource() != glslang::EShSourceHlsl ||
John Kessenicha28f7a72019-08-06 07:00:58 -06001177 type.getQualifier().storage == glslang::EvqUniform) {
John Kessenichdeec1932019-08-13 08:00:30 -06001178 if (type.isAtomic())
John Kessenichbed4e4f2017-09-08 02:38:07 -06001179 return spv::StorageClassAtomicCounter;
1180 if (type.containsOpaque())
1181 return spv::StorageClassUniformConstant;
1182 }
1183
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001184 if (type.getQualifier().isUniformOrBuffer() &&
John Kessenichdeec1932019-08-13 08:00:30 -06001185 type.getQualifier().isShaderRecordNV()) {
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001186 return spv::StorageClassShaderRecordBufferNV;
1187 }
Jeff Bolz61a0cd12018-12-14 20:59:53 -06001188
John Kessenichbed4e4f2017-09-08 02:38:07 -06001189 if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001190 builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3);
John Kessenicha5c5fb62017-05-05 05:09:58 -06001191 return spv::StorageClassStorageBuffer;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001192 }
1193
1194 if (type.getQualifier().isUniformOrBuffer()) {
John Kessenich7015bd62019-08-01 03:28:08 -06001195 if (type.getQualifier().isPushConstant())
John Kessenicha5c5fb62017-05-05 05:09:58 -06001196 return spv::StorageClassPushConstant;
1197 if (type.getBasicType() == glslang::EbtBlock)
1198 return spv::StorageClassUniform;
John Kessenichbed4e4f2017-09-08 02:38:07 -06001199 return spv::StorageClassUniformConstant;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001200 }
John Kessenichbed4e4f2017-09-08 02:38:07 -06001201
1202 switch (type.getQualifier().storage) {
John Kessenichbed4e4f2017-09-08 02:38:07 -06001203 case glslang::EvqGlobal: return spv::StorageClassPrivate;
1204 case glslang::EvqConstReadOnly: return spv::StorageClassFunction;
1205 case glslang::EvqTemporary: return spv::StorageClassFunction;
John Kessenicha28f7a72019-08-06 07:00:58 -06001206#ifndef GLSLANG_WEB
John Kessenichdeec1932019-08-13 08:00:30 -06001207 case glslang::EvqShared: return spv::StorageClassWorkgroup;
Ashwin Leleff1783d2018-10-22 16:41:44 -07001208 case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV;
1209 case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV;
1210 case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV;
1211 case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV;
1212 case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV;
Chao Chenb50c02e2018-09-19 11:42:24 -07001213#endif
John Kessenichbed4e4f2017-09-08 02:38:07 -06001214 default:
1215 assert(0);
1216 break;
1217 }
1218
1219 return spv::StorageClassFunction;
John Kessenicha5c5fb62017-05-05 05:09:58 -06001220}
1221
John Kessenich5611c6d2018-04-05 11:25:02 -06001222// Add capabilities pertaining to how an array is indexed.
1223void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
1224 const glslang::TType& indexType)
1225{
John Kessenichb9197c82019-08-11 07:41:45 -06001226#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06001227 if (indexType.getQualifier().isNonUniform()) {
1228 // deal with an asserted non-uniform index
Jeff Bolzc140b962018-07-12 16:51:18 -05001229 // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration
John Kessenich5611c6d2018-04-05 11:25:02 -06001230 if (baseType.getBasicType() == glslang::EbtSampler) {
1231 if (baseType.getQualifier().hasAttachment())
1232 builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001233 else if (baseType.isImage() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001234 builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001235 else if (baseType.isTexture() && baseType.getSampler().isBuffer())
John Kessenich5611c6d2018-04-05 11:25:02 -06001236 builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT);
1237 else if (baseType.isImage())
1238 builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT);
1239 else if (baseType.isTexture())
1240 builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT);
1241 } else if (baseType.getBasicType() == glslang::EbtBlock) {
1242 if (baseType.getQualifier().storage == glslang::EvqBuffer)
1243 builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT);
1244 else if (baseType.getQualifier().storage == glslang::EvqUniform)
1245 builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT);
1246 }
1247 } else {
1248 // assume a dynamically uniform index
1249 if (baseType.getBasicType() == glslang::EbtSampler) {
Jeff Bolzc140b962018-07-12 16:51:18 -05001250 if (baseType.getQualifier().hasAttachment()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001251 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001252 builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001253 } else if (baseType.isImage() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001254 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001255 builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06001256 } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) {
John Kessenich8317e6c2019-08-18 23:58:08 -06001257 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06001258 builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT);
Jeff Bolzc140b962018-07-12 16:51:18 -05001259 }
John Kessenich5611c6d2018-04-05 11:25:02 -06001260 }
1261 }
John Kessenichb9197c82019-08-11 07:41:45 -06001262#endif
John Kessenich5611c6d2018-04-05 11:25:02 -06001263}
1264
qining25262b32016-05-06 17:25:16 -04001265// Return whether or not the given type is something that should be tied to a
John Kessenich6c292d32016-02-15 20:58:50 -07001266// descriptor set.
1267bool IsDescriptorResource(const glslang::TType& type)
1268{
John Kessenichf7497e22016-03-08 21:36:22 -07001269 // uniform and buffer blocks are included, unless it is a push_constant
John Kessenich6c292d32016-02-15 20:58:50 -07001270 if (type.getBasicType() == glslang::EbtBlock)
Chao Chenb50c02e2018-09-19 11:42:24 -07001271 return type.getQualifier().isUniformOrBuffer() &&
John Kessenich7015bd62019-08-01 03:28:08 -06001272 ! type.getQualifier().isShaderRecordNV() &&
1273 ! type.getQualifier().isPushConstant();
John Kessenich6c292d32016-02-15 20:58:50 -07001274
1275 // non block...
1276 // basically samplerXXX/subpass/sampler/texture are all included
1277 // if they are the global-scope-class, not the function parameter
1278 // (or local, if they ever exist) class.
1279 if (type.getBasicType() == glslang::EbtSampler)
1280 return type.getQualifier().isUniformOrBuffer();
1281
1282 // None of the above.
1283 return false;
1284}
1285
John Kesseniche0b6cad2015-12-24 10:30:13 -07001286void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
1287{
1288 if (child.layoutMatrix == glslang::ElmNone)
1289 child.layoutMatrix = parent.layoutMatrix;
1290
1291 if (parent.invariant)
1292 child.invariant = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001293 if (parent.flat)
1294 child.flat = true;
1295 if (parent.centroid)
1296 child.centroid = true;
John Kessenich7015bd62019-08-01 03:28:08 -06001297#ifndef GLSLANG_WEB
John Kesseniche0b6cad2015-12-24 10:30:13 -07001298 if (parent.nopersp)
1299 child.nopersp = true;
Rex Xu9d93a232016-05-05 12:30:44 +08001300 if (parent.explicitInterp)
1301 child.explicitInterp = true;
John Kessenicha28f7a72019-08-06 07:00:58 -06001302 if (parent.perPrimitiveNV)
1303 child.perPrimitiveNV = true;
1304 if (parent.perViewNV)
1305 child.perViewNV = true;
1306 if (parent.perTaskNV)
1307 child.perTaskNV = true;
John Kesseniche0b6cad2015-12-24 10:30:13 -07001308 if (parent.patch)
1309 child.patch = true;
1310 if (parent.sample)
1311 child.sample = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001312 if (parent.coherent)
1313 child.coherent = true;
Jeff Bolz36831c92018-09-05 10:11:41 -05001314 if (parent.devicecoherent)
1315 child.devicecoherent = true;
1316 if (parent.queuefamilycoherent)
1317 child.queuefamilycoherent = true;
1318 if (parent.workgroupcoherent)
1319 child.workgroupcoherent = true;
1320 if (parent.subgroupcoherent)
1321 child.subgroupcoherent = true;
1322 if (parent.nonprivate)
1323 child.nonprivate = true;
Rex Xu1da878f2016-02-21 20:59:01 +08001324 if (parent.volatil)
1325 child.volatil = true;
1326 if (parent.restrict)
1327 child.restrict = true;
1328 if (parent.readonly)
1329 child.readonly = true;
1330 if (parent.writeonly)
1331 child.writeonly = true;
Chao Chen3c366992018-09-19 11:41:59 -07001332#endif
John Kesseniche0b6cad2015-12-24 10:30:13 -07001333}
1334
John Kessenichf2b7f332016-09-01 17:05:23 -06001335bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001336{
John Kessenich7b9fa252016-01-21 18:56:57 -07001337 // This should list qualifiers that simultaneous satisfy:
John Kessenichf2b7f332016-09-01 17:05:23 -06001338 // - struct members might inherit from a struct declaration
1339 // (note that non-block structs don't explicitly inherit,
1340 // only implicitly, meaning no decoration involved)
1341 // - affect decorations on the struct members
1342 // (note smooth does not, and expecting something like volatile
1343 // to effect the whole object)
John Kesseniche0b6cad2015-12-24 10:30:13 -07001344 // - are not part of the offset/st430/etc or row/column-major layout
John Kessenichf2b7f332016-09-01 17:05:23 -06001345 return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock);
John Kesseniche0b6cad2015-12-24 10:30:13 -07001346}
1347
John Kessenich140f3df2015-06-26 16:58:36 -06001348//
1349// Implement the TGlslangToSpvTraverser class.
1350//
1351
John Kessenich2b5ea9f2018-01-31 18:35:56 -07001352TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate,
John Kessenich121853f2017-05-31 17:11:16 -06001353 spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options)
1354 : TIntermTraverser(true, false, true),
1355 options(options),
1356 shaderEntry(nullptr), currentFunction(nullptr),
John Kesseniched33e052016-10-06 12:59:51 -06001357 sequenceDepth(0), logger(buildLogger),
John Kessenich2b5ea9f2018-01-31 18:35:56 -07001358 builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger),
John Kessenich517fe7a2016-11-26 13:31:47 -07001359 inEntryPoint(false), entryPointTerminated(false), linkageOnly(false),
John Kessenich605afc72019-06-17 23:33:09 -06001360 glslangIntermediate(glslangIntermediate),
1361 nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp())
John Kessenich140f3df2015-06-26 16:58:36 -06001362{
1363 spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage());
1364
1365 builder.clearAccessChain();
John Kessenich2a271162017-07-20 20:00:36 -06001366 builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()),
1367 glslangIntermediate->getVersion());
1368
John Kessenich121853f2017-05-31 17:11:16 -06001369 if (options.generateDebugInfo) {
John Kesseniche485c7a2017-05-31 18:50:53 -06001370 builder.setEmitOpLines();
John Kessenich2a271162017-07-20 20:00:36 -06001371 builder.setSourceFile(glslangIntermediate->getSourceFile());
1372
1373 // Set the source shader's text. If for SPV version 1.0, include
1374 // a preamble in comments stating the OpModuleProcessed instructions.
1375 // Otherwise, emit those as actual instructions.
1376 std::string text;
1377 const std::vector<std::string>& processes = glslangIntermediate->getProcesses();
1378 for (int p = 0; p < (int)processes.size(); ++p) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001379 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) {
John Kessenich2a271162017-07-20 20:00:36 -06001380 text.append("// OpModuleProcessed ");
1381 text.append(processes[p]);
1382 text.append("\n");
1383 } else
1384 builder.addModuleProcessed(processes[p]);
1385 }
John Kessenich8717a5d2018-10-26 10:12:32 -06001386 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0)
John Kessenich2a271162017-07-20 20:00:36 -06001387 text.append("#line 1\n");
1388 text.append(glslangIntermediate->getSourceText());
1389 builder.setSourceText(text);
Greg Fischerd445bb22018-12-06 11:13:15 -07001390 // Pass name and text for all included files
1391 const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
1392 for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
1393 builder.addInclude(iItr->first, iItr->second);
John Kessenich121853f2017-05-31 17:11:16 -06001394 }
John Kessenich140f3df2015-06-26 16:58:36 -06001395 stdBuiltins = builder.import("GLSL.std.450");
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001396
1397 spv::AddressingModel addressingModel = spv::AddressingModelLogical;
1398 spv::MemoryModel memoryModel = spv::MemoryModelGLSL450;
1399
1400 if (glslangIntermediate->usingPhysicalStorageBuffer()) {
1401 addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT;
John Kessenich1ff0c182019-10-10 12:01:13 -06001402 builder.addIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, spv::Spv_1_5);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001403 builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT);
1404 };
Jeff Bolz36831c92018-09-05 10:11:41 -05001405 if (glslangIntermediate->usingVulkanMemoryModel()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001406 memoryModel = spv::MemoryModelVulkanKHR;
1407 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
John Kessenich8317e6c2019-08-18 23:58:08 -06001408 builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
Jeff Bolz36831c92018-09-05 10:11:41 -05001409 }
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001410 builder.setMemoryModel(addressingModel, memoryModel);
1411
Jeff Bolz4605e2e2019-02-19 13:10:32 -06001412 if (glslangIntermediate->usingVariablePointers()) {
1413 builder.addCapability(spv::CapabilityVariablePointers);
1414 }
1415
John Kessenicheee9d532016-09-19 18:09:30 -06001416 shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str());
1417 entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str());
John Kessenich140f3df2015-06-26 16:58:36 -06001418
1419 // Add the source extensions
John Kessenich2f273362015-07-18 22:34:27 -06001420 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
1421 for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it)
John Kessenich140f3df2015-06-26 16:58:36 -06001422 builder.addSourceExtension(it->c_str());
1423
1424 // Add the top-level modes for this shader.
1425
John Kessenich92187592016-02-01 13:45:25 -07001426 if (glslangIntermediate->getXfbMode()) {
1427 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenich140f3df2015-06-26 16:58:36 -06001428 builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb);
John Kessenich92187592016-02-01 13:45:25 -07001429 }
John Kessenich140f3df2015-06-26 16:58:36 -06001430
1431 unsigned int mode;
1432 switch (glslangIntermediate->getStage()) {
1433 case EShLangVertex:
John Kessenich5e4b1242015-08-06 22:53:06 -06001434 builder.addCapability(spv::CapabilityShader);
John Kessenich140f3df2015-06-26 16:58:36 -06001435 break;
1436
John Kessenicha28f7a72019-08-06 07:00:58 -06001437 case EShLangFragment:
1438 builder.addCapability(spv::CapabilityShader);
1439 if (glslangIntermediate->getPixelCenterInteger())
1440 builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger);
1441
1442 if (glslangIntermediate->getOriginUpperLeft())
1443 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft);
1444 else
1445 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft);
1446
1447 if (glslangIntermediate->getEarlyFragmentTests())
1448 builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests);
1449
1450 if (glslangIntermediate->getPostDepthCoverage()) {
1451 builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage);
1452 builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage);
1453 builder.addExtension(spv::E_SPV_KHR_post_depth_coverage);
1454 }
1455
John Kessenichb9197c82019-08-11 07:41:45 -06001456 if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing())
1457 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing);
1458
1459#ifndef GLSLANG_WEB
John Kessenicha28f7a72019-08-06 07:00:58 -06001460 switch(glslangIntermediate->getDepth()) {
1461 case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break;
1462 case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break;
1463 default: mode = spv::ExecutionModeMax; break;
1464 }
1465 if (mode != spv::ExecutionModeMax)
1466 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kessenicha28f7a72019-08-06 07:00:58 -06001467 switch (glslangIntermediate->getInterlockOrdering()) {
1468 case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; break;
1469 case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; break;
1470 case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; break;
1471 case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; break;
1472 case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; break;
1473 case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; break;
1474 default: mode = spv::ExecutionModeMax; break;
1475 }
1476 if (mode != spv::ExecutionModeMax) {
1477 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1478 if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT ||
1479 mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) {
1480 builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT);
1481 } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT ||
1482 mode == spv::ExecutionModePixelInterlockUnorderedEXT) {
1483 builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT);
1484 } else {
1485 builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT);
1486 }
1487 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
1488 }
John Kessenichb9197c82019-08-11 07:41:45 -06001489#endif
John Kessenicha28f7a72019-08-06 07:00:58 -06001490 break;
1491
1492#ifndef GLSLANG_WEB
1493 case EShLangCompute:
1494 builder.addCapability(spv::CapabilityShader);
1495 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1496 glslangIntermediate->getLocalSize(1),
1497 glslangIntermediate->getLocalSize(2));
1498 if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) {
1499 builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV);
1500 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV);
1501 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1502 } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) {
1503 builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV);
1504 builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV);
1505 builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives);
1506 }
1507 break;
steve-lunarge7412492017-03-23 11:56:07 -06001508 case EShLangTessEvaluation:
John Kessenich140f3df2015-06-26 16:58:36 -06001509 case EShLangTessControl:
John Kessenich5e4b1242015-08-06 22:53:06 -06001510 builder.addCapability(spv::CapabilityTessellation);
John Kessenich140f3df2015-06-26 16:58:36 -06001511
steve-lunarge7412492017-03-23 11:56:07 -06001512 glslang::TLayoutGeometry primitive;
1513
1514 if (glslangIntermediate->getStage() == EShLangTessControl) {
1515 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1516 primitive = glslangIntermediate->getOutputPrimitive();
1517 } else {
1518 primitive = glslangIntermediate->getInputPrimitive();
1519 }
1520
1521 switch (primitive) {
John Kessenich55e7d112015-11-15 21:33:39 -07001522 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
1523 case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break;
1524 case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break;
John Kessenich4016e382016-07-15 11:53:56 -06001525 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001526 }
John Kessenich4016e382016-07-15 11:53:56 -06001527 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001528 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1529
John Kesseniche6903322015-10-13 16:29:02 -06001530 switch (glslangIntermediate->getVertexSpacing()) {
1531 case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break;
1532 case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break;
1533 case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break;
John Kessenich4016e382016-07-15 11:53:56 -06001534 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001535 }
John Kessenich4016e382016-07-15 11:53:56 -06001536 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001537 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1538
1539 switch (glslangIntermediate->getVertexOrder()) {
1540 case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break;
1541 case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break;
John Kessenich4016e382016-07-15 11:53:56 -06001542 default: mode = spv::ExecutionModeMax; break;
John Kesseniche6903322015-10-13 16:29:02 -06001543 }
John Kessenich4016e382016-07-15 11:53:56 -06001544 if (mode != spv::ExecutionModeMax)
John Kesseniche6903322015-10-13 16:29:02 -06001545 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1546
1547 if (glslangIntermediate->getPointMode())
1548 builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode);
John Kessenich140f3df2015-06-26 16:58:36 -06001549 break;
1550
1551 case EShLangGeometry:
John Kessenich5e4b1242015-08-06 22:53:06 -06001552 builder.addCapability(spv::CapabilityGeometry);
John Kessenich140f3df2015-06-26 16:58:36 -06001553 switch (glslangIntermediate->getInputPrimitive()) {
1554 case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break;
1555 case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break;
1556 case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break;
John Kessenich55e7d112015-11-15 21:33:39 -07001557 case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001558 case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break;
John Kessenich4016e382016-07-15 11:53:56 -06001559 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001560 }
John Kessenich4016e382016-07-15 11:53:56 -06001561 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001562 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
John Kesseniche6903322015-10-13 16:29:02 -06001563
John Kessenich140f3df2015-06-26 16:58:36 -06001564 builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations());
1565
1566 switch (glslangIntermediate->getOutputPrimitive()) {
1567 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1568 case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break;
1569 case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break;
John Kessenich4016e382016-07-15 11:53:56 -06001570 default: mode = spv::ExecutionModeMax; break;
John Kessenich140f3df2015-06-26 16:58:36 -06001571 }
John Kessenich4016e382016-07-15 11:53:56 -06001572 if (mode != spv::ExecutionModeMax)
John Kessenich140f3df2015-06-26 16:58:36 -06001573 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1574 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1575 break;
1576
Chao Chenb50c02e2018-09-19 11:42:24 -07001577 case EShLangRayGenNV:
1578 case EShLangIntersectNV:
1579 case EShLangAnyHitNV:
1580 case EShLangClosestHitNV:
1581 case EShLangMissNV:
1582 case EShLangCallableNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07001583 builder.addCapability(spv::CapabilityRayTracingNV);
1584 builder.addExtension("SPV_NV_ray_tracing");
Chao Chenb50c02e2018-09-19 11:42:24 -07001585 break;
Chao Chen3c366992018-09-19 11:41:59 -07001586 case EShLangTaskNV:
1587 case EShLangMeshNV:
1588 builder.addCapability(spv::CapabilityMeshShadingNV);
1589 builder.addExtension(spv::E_SPV_NV_mesh_shader);
1590 builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0),
1591 glslangIntermediate->getLocalSize(1),
1592 glslangIntermediate->getLocalSize(2));
1593 if (glslangIntermediate->getStage() == EShLangMeshNV) {
1594 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices());
1595 builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives());
1596
1597 switch (glslangIntermediate->getOutputPrimitive()) {
1598 case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break;
1599 case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break;
1600 case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break;
1601 default: mode = spv::ExecutionModeMax; break;
1602 }
1603 if (mode != spv::ExecutionModeMax)
1604 builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode);
1605 }
1606 break;
1607#endif
1608
John Kessenich140f3df2015-06-26 16:58:36 -06001609 default:
1610 break;
1611 }
John Kessenich140f3df2015-06-26 16:58:36 -06001612}
1613
John Kessenichfca82622016-11-26 13:23:20 -07001614// Finish creating SPV, after the traversal is complete.
1615void TGlslangToSpvTraverser::finishSpv()
John Kessenich7ba63412015-12-20 17:37:07 -07001616{
John Kessenichf04c51b2018-08-03 15:56:12 -06001617 // Finish the entry point function
John Kessenich517fe7a2016-11-26 13:31:47 -07001618 if (! entryPointTerminated) {
John Kessenichfca82622016-11-26 13:23:20 -07001619 builder.setBuildPoint(shaderEntry->getLastBlock());
1620 builder.leaveFunction();
1621 }
1622
John Kessenich7ba63412015-12-20 17:37:07 -07001623 // finish off the entry-point SPV instruction by adding the Input/Output <id>
rdb32084e82016-02-23 22:17:38 +01001624 for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it)
1625 entryPoint->addIdOperand(*it);
John Kessenich7ba63412015-12-20 17:37:07 -07001626
John Kessenich23d27752019-07-28 02:12:10 -06001627#ifndef GLSLANG_WEB
John Kessenichf04c51b2018-08-03 15:56:12 -06001628 // Add capabilities, extensions, remove unneeded decorations, etc.,
1629 // based on the resulting SPIR-V.
1630 builder.postProcess();
John Kessenich23d27752019-07-28 02:12:10 -06001631#endif
John Kessenich7ba63412015-12-20 17:37:07 -07001632}
1633
John Kessenichfca82622016-11-26 13:23:20 -07001634// Write the SPV into 'out'.
1635void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
John Kessenich140f3df2015-06-26 16:58:36 -06001636{
John Kessenichfca82622016-11-26 13:23:20 -07001637 builder.dump(out);
John Kessenich140f3df2015-06-26 16:58:36 -06001638}
1639
1640//
1641// Implement the traversal functions.
1642//
1643// Return true from interior nodes to have the external traversal
1644// continue on to children. Return false if children were
1645// already processed.
1646//
1647
1648//
qining25262b32016-05-06 17:25:16 -04001649// Symbols can turn into
John Kessenich140f3df2015-06-26 16:58:36 -06001650// - uniform/input reads
1651// - output writes
1652// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain
1653// - something simple that degenerates into the last bullet
1654//
1655void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
1656{
qining75d1d802016-04-06 14:42:01 -04001657 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1658 if (symbol->getType().getQualifier().isSpecConstant())
1659 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1660
John Kessenich140f3df2015-06-26 16:58:36 -06001661 // getSymbolId() will set up all the IO decorations on the first call.
1662 // Formal function parameters were mapped during makeFunctions().
1663 spv::Id id = getSymbolId(symbol);
John Kessenich7ba63412015-12-20 17:37:07 -07001664
John Kessenich7ba63412015-12-20 17:37:07 -07001665 if (builder.isPointer(id)) {
John Kessenich9c14f772019-06-17 08:38:35 -06001666 // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction
John Kessenich7c7731e2019-01-04 16:47:06 +07001667 // Consider adding to the OpEntryPoint interface list.
1668 // Only looking at structures if they have at least one member.
1669 if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) {
1670 spv::StorageClass sc = builder.getStorageClass(id);
1671 // Before SPIR-V 1.4, we only want to include Input and Output.
1672 // Starting with SPIR-V 1.4, we want all globals.
1673 if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) ||
1674 (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) {
John Kessenich5f77d862017-09-19 11:09:59 -06001675 iOSet.insert(id);
John Kessenich7c7731e2019-01-04 16:47:06 +07001676 }
John Kessenich5f77d862017-09-19 11:09:59 -06001677 }
John Kessenich9c14f772019-06-17 08:38:35 -06001678
1679 // If the SPIR-V type is required to be different than the AST type,
1680 // translate now from the SPIR-V type to the AST type, for the consuming
1681 // operation.
1682 // Note this turns it from an l-value to an r-value.
1683 // Currently, all symbols needing this are inputs; avoid the map lookup when non-input.
1684 if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn)
1685 id = translateForcedType(id);
John Kessenich7ba63412015-12-20 17:37:07 -07001686 }
1687
1688 // Only process non-linkage-only nodes for generating actual static uses
John Kessenich6c292d32016-02-15 20:58:50 -07001689 if (! linkageOnly || symbol->getQualifier().isSpecConstant()) {
John Kessenich140f3df2015-06-26 16:58:36 -06001690 // Prepare to generate code for the access
1691
1692 // L-value chains will be computed left to right. We're on the symbol now,
1693 // which is the left-most part of the access chain, so now is "clear" time,
1694 // followed by setting the base.
1695 builder.clearAccessChain();
1696
1697 // For now, we consider all user variables as being in memory, so they are pointers,
John Kessenich6c292d32016-02-15 20:58:50 -07001698 // except for
John Kessenich4bf71552016-09-02 11:20:21 -06001699 // A) R-Value arguments to a function, which are an intermediate object.
John Kessenich6c292d32016-02-15 20:58:50 -07001700 // See comments in handleUserFunctionCall().
John Kessenich4bf71552016-09-02 11:20:21 -06001701 // B) Specialization constants (normal constants don't even come in as a variable),
John Kessenich6c292d32016-02-15 20:58:50 -07001702 // These are also pure R-values.
John Kessenich9c14f772019-06-17 08:38:35 -06001703 // C) R-Values from type translation, see above call to translateForcedType()
John Kessenich6c292d32016-02-15 20:58:50 -07001704 glslang::TQualifier qualifier = symbol->getQualifier();
John Kessenich9c14f772019-06-17 08:38:35 -06001705 if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() ||
1706 !builder.isPointerType(builder.getTypeId(id)))
John Kessenich140f3df2015-06-26 16:58:36 -06001707 builder.setAccessChainRValue(id);
1708 else
1709 builder.setAccessChainLValue(id);
1710 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001711
John Kessenichb9197c82019-08-11 07:41:45 -06001712#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001713 // Process linkage-only nodes for any special additional interface work.
1714 if (linkageOnly) {
1715 if (glslangIntermediate->getHlslFunctionality1()) {
1716 // Map implicit counter buffers to their originating buffers, which should have been
1717 // seen by now, given earlier pruning of unused counters, and preservation of order
1718 // of declaration.
1719 if (symbol->getType().getQualifier().isUniformOrBuffer()) {
1720 if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) {
1721 // Save possible originating buffers for counter buffers, keyed by
1722 // making the potential counter-buffer name.
1723 std::string keyName = symbol->getName().c_str();
1724 keyName = glslangIntermediate->addCounterBufferName(keyName);
1725 counterOriginator[keyName] = symbol;
1726 } else {
1727 // Handle a counter buffer, by finding the saved originating buffer.
1728 std::string keyName = symbol->getName().c_str();
1729 auto it = counterOriginator.find(keyName);
1730 if (it != counterOriginator.end()) {
1731 id = getSymbolId(it->second);
1732 if (id != spv::NoResult) {
1733 spv::Id counterId = getSymbolId(symbol);
John Kessenichf52b6382018-04-05 19:35:38 -06001734 if (counterId != spv::NoResult) {
1735 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
John Kessenich5d610ee2018-03-07 18:05:55 -07001736 builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId);
John Kessenichf52b6382018-04-05 19:35:38 -06001737 }
John Kessenich5d610ee2018-03-07 18:05:55 -07001738 }
1739 }
1740 }
1741 }
1742 }
1743 }
John Kessenich155d3512019-08-08 23:29:20 -06001744#endif
John Kessenich140f3df2015-06-26 16:58:36 -06001745}
1746
1747bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
1748{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07001749 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06001750
qining40887662016-04-03 22:20:42 -04001751 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
1752 if (node->getType().getQualifier().isSpecConstant())
1753 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
1754
John Kessenich140f3df2015-06-26 16:58:36 -06001755 // First, handle special cases
1756 switch (node->getOp()) {
1757 case glslang::EOpAssign:
1758 case glslang::EOpAddAssign:
1759 case glslang::EOpSubAssign:
1760 case glslang::EOpMulAssign:
1761 case glslang::EOpVectorTimesMatrixAssign:
1762 case glslang::EOpVectorTimesScalarAssign:
1763 case glslang::EOpMatrixTimesScalarAssign:
1764 case glslang::EOpMatrixTimesMatrixAssign:
1765 case glslang::EOpDivAssign:
1766 case glslang::EOpModAssign:
1767 case glslang::EOpAndAssign:
1768 case glslang::EOpInclusiveOrAssign:
1769 case glslang::EOpExclusiveOrAssign:
1770 case glslang::EOpLeftShiftAssign:
1771 case glslang::EOpRightShiftAssign:
1772 // A bin-op assign "a += b" means the same thing as "a = a + b"
1773 // where a is evaluated before b. For a simple assignment, GLSL
1774 // says to evaluate the left before the right. So, always, left
1775 // node then right node.
1776 {
1777 // get the left l-value, save it away
1778 builder.clearAccessChain();
1779 node->getLeft()->traverse(this);
1780 spv::Builder::AccessChain lValue = builder.getAccessChain();
1781
1782 // evaluate the right
1783 builder.clearAccessChain();
1784 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001785 spv::Id rValue = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001786
1787 if (node->getOp() != glslang::EOpAssign) {
1788 // the left is also an r-value
1789 builder.setAccessChain(lValue);
John Kessenich32cfd492016-02-02 12:37:46 -07001790 spv::Id leftRValue = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001791
1792 // do the operation
John Kessenichead86222018-03-28 18:01:20 -06001793 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001794 TranslateNoContractionDecoration(node->getType().getQualifier()),
1795 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06001796 rValue = createBinaryOperation(node->getOp(), decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06001797 convertGlslangToSpvType(node->getType()), leftRValue, rValue,
1798 node->getType().getBasicType());
1799
1800 // these all need their counterparts in createBinaryOperation()
John Kessenich55e7d112015-11-15 21:33:39 -07001801 assert(rValue != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06001802 }
1803
1804 // store the result
1805 builder.setAccessChain(lValue);
Jeff Bolz36831c92018-09-05 10:11:41 -05001806 multiTypeStore(node->getLeft()->getType(), rValue);
John Kessenich140f3df2015-06-26 16:58:36 -06001807
1808 // assignments are expressions having an rValue after they are evaluated...
1809 builder.clearAccessChain();
1810 builder.setAccessChainRValue(rValue);
1811 }
1812 return false;
1813 case glslang::EOpIndexDirect:
1814 case glslang::EOpIndexDirectStruct:
1815 {
John Kessenich61a5ce12019-02-07 08:04:12 -07001816 // Structure, array, matrix, or vector indirection with statically known index.
John Kessenich140f3df2015-06-26 16:58:36 -06001817 // Get the left part of the access chain.
1818 node->getLeft()->traverse(this);
1819
1820 // Add the next element in the chain
1821
David Netoa901ffe2016-06-08 14:11:40 +01001822 const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();
John Kessenich140f3df2015-06-26 16:58:36 -06001823 if (! node->getLeft()->getType().isArray() &&
1824 node->getLeft()->getType().isVector() &&
1825 node->getOp() == glslang::EOpIndexDirect) {
1826 // This is essentially a hard-coded vector swizzle of size 1,
1827 // so short circuit the access-chain stuff with a swizzle.
1828 std::vector<unsigned> swizzle;
David Netoa901ffe2016-06-08 14:11:40 +01001829 swizzle.push_back(glslangIndex);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001830 int dummySize;
1831 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1832 TranslateCoherent(node->getLeft()->getType()),
1833 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001834 } else {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001835
1836 // Load through a block reference is performed with a dot operator that
1837 // is mapped to EOpIndexDirectStruct. When we get to the actual reference,
1838 // do a load and reset the access chain.
John Kessenich7015bd62019-08-01 03:28:08 -06001839 if (node->getLeft()->isReference() &&
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001840 !node->getLeft()->getType().isArray() &&
1841 node->getOp() == glslang::EOpIndexDirectStruct)
1842 {
1843 spv::Id left = accessChainLoad(node->getLeft()->getType());
1844 builder.clearAccessChain();
1845 builder.setAccessChainLValue(left);
1846 }
1847
David Netoa901ffe2016-06-08 14:11:40 +01001848 int spvIndex = glslangIndex;
1849 if (node->getLeft()->getBasicType() == glslang::EbtBlock &&
1850 node->getOp() == glslang::EOpIndexDirectStruct)
1851 {
1852 // This may be, e.g., an anonymous block-member selection, which generally need
1853 // index remapping due to hidden members in anonymous blocks.
1854 std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()];
1855 assert(remapper.size() > 0);
1856 spvIndex = remapper[glslangIndex];
1857 }
John Kessenichebb50532016-05-16 19:22:05 -06001858
David Netoa901ffe2016-06-08 14:11:40 +01001859 // normal case for indexing array or structure or block
Jeff Bolz7895e472019-03-06 13:34:10 -06001860 builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment());
David Netoa901ffe2016-06-08 14:11:40 +01001861
1862 // Add capabilities here for accessing PointSize and clip/cull distance.
1863 // We have deferred generation of associated capabilities until now.
John Kessenichebb50532016-05-16 19:22:05 -06001864 if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray())
David Netoa901ffe2016-06-08 14:11:40 +01001865 declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex);
John Kessenich140f3df2015-06-26 16:58:36 -06001866 }
1867 }
1868 return false;
1869 case glslang::EOpIndexIndirect:
1870 {
John Kessenich61a5ce12019-02-07 08:04:12 -07001871 // Array, matrix, or vector indirection with variable index.
1872 // Will use native SPIR-V access-chain for and array indirection;
John Kessenich140f3df2015-06-26 16:58:36 -06001873 // matrices are arrays of vectors, so will also work for a matrix.
1874 // Will use the access chain's 'component' for variable index into a vector.
1875
1876 // This adapter is building access chains left to right.
1877 // Set up the access chain to the left.
1878 node->getLeft()->traverse(this);
1879
1880 // save it so that computing the right side doesn't trash it
1881 spv::Builder::AccessChain partial = builder.getAccessChain();
1882
1883 // compute the next index in the chain
1884 builder.clearAccessChain();
1885 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001886 spv::Id index = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001887
John Kessenich5611c6d2018-04-05 11:25:02 -06001888 addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType());
1889
John Kessenich140f3df2015-06-26 16:58:36 -06001890 // restore the saved access chain
1891 builder.setAccessChain(partial);
1892
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001893 if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) {
1894 int dummySize;
1895 builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()),
1896 TranslateCoherent(node->getLeft()->getType()),
1897 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
1898 } else
Jeff Bolz7895e472019-03-06 13:34:10 -06001899 builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment());
John Kessenich140f3df2015-06-26 16:58:36 -06001900 }
1901 return false;
1902 case glslang::EOpVectorSwizzle:
1903 {
1904 node->getLeft()->traverse(this);
John Kessenich140f3df2015-06-26 16:58:36 -06001905 std::vector<unsigned> swizzle;
John Kessenich8c8505c2016-07-26 12:50:38 -06001906 convertSwizzle(*node->getRight()->getAsAggregate(), swizzle);
Jeff Bolz9f2aec42019-01-06 17:58:04 -06001907 int dummySize;
1908 builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()),
1909 TranslateCoherent(node->getLeft()->getType()),
1910 glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize));
John Kessenich140f3df2015-06-26 16:58:36 -06001911 }
1912 return false;
John Kessenichfdf63472017-01-13 12:27:52 -07001913 case glslang::EOpMatrixSwizzle:
1914 logger->missingFunctionality("matrix swizzle");
1915 return true;
John Kessenich7c1aa102015-10-15 13:29:11 -06001916 case glslang::EOpLogicalOr:
1917 case glslang::EOpLogicalAnd:
1918 {
1919
1920 // These may require short circuiting, but can sometimes be done as straight
1921 // binary operations. The right operand must be short circuited if it has
1922 // side effects, and should probably be if it is complex.
1923 if (isTrivial(node->getRight()->getAsTyped()))
1924 break; // handle below as a normal binary operation
1925 // otherwise, we need to do dynamic short circuiting on the right operand
1926 spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped());
1927 builder.clearAccessChain();
1928 builder.setAccessChainRValue(result);
1929 }
1930 return false;
John Kessenich140f3df2015-06-26 16:58:36 -06001931 default:
1932 break;
1933 }
1934
1935 // Assume generic binary op...
1936
John Kessenich32cfd492016-02-02 12:37:46 -07001937 // get right operand
John Kessenich140f3df2015-06-26 16:58:36 -06001938 builder.clearAccessChain();
1939 node->getLeft()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001940 spv::Id left = accessChainLoad(node->getLeft()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001941
John Kessenich32cfd492016-02-02 12:37:46 -07001942 // get left operand
John Kessenich140f3df2015-06-26 16:58:36 -06001943 builder.clearAccessChain();
1944 node->getRight()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07001945 spv::Id right = accessChainLoad(node->getRight()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06001946
John Kessenich32cfd492016-02-02 12:37:46 -07001947 // get result
John Kessenichead86222018-03-28 18:01:20 -06001948 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06001949 TranslateNoContractionDecoration(node->getType().getQualifier()),
1950 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06001951 spv::Id result = createBinaryOperation(node->getOp(), decorations,
John Kessenich32cfd492016-02-02 12:37:46 -07001952 convertGlslangToSpvType(node->getType()), left, right,
1953 node->getLeft()->getType().getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06001954
John Kessenich50e57562015-12-21 21:21:11 -07001955 builder.clearAccessChain();
John Kessenich140f3df2015-06-26 16:58:36 -06001956 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04001957 logger->missingFunctionality("unknown glslang binary operation");
John Kessenich50e57562015-12-21 21:21:11 -07001958 return true; // pick up a child as the place-holder result
John Kessenich140f3df2015-06-26 16:58:36 -06001959 } else {
John Kessenich140f3df2015-06-26 16:58:36 -06001960 builder.setAccessChainRValue(result);
John Kessenich140f3df2015-06-26 16:58:36 -06001961 return false;
1962 }
John Kessenich140f3df2015-06-26 16:58:36 -06001963}
1964
John Kessenich9c14f772019-06-17 08:38:35 -06001965// Figure out what, if any, type changes are needed when accessing a specific built-in.
1966// Returns <the type SPIR-V requires for declarion, the type to translate to on use>.
1967// Also see comment for 'forceType', regarding tracking SPIR-V-required types.
1968std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(spv::BuiltIn builtIn,
1969 const glslang::TType& glslangType)
1970{
1971 switch(builtIn)
1972 {
1973 case spv::BuiltInSubgroupEqMask:
1974 case spv::BuiltInSubgroupGeMask:
1975 case spv::BuiltInSubgroupGtMask:
1976 case spv::BuiltInSubgroupLeMask:
1977 case spv::BuiltInSubgroupLtMask: {
1978 // these require changing a 64-bit scaler -> a vector of 32-bit components
1979 if (glslangType.isVector())
1980 break;
1981 std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4),
1982 builder.makeUintType(64));
1983 return ret;
1984 }
1985 default:
1986 break;
1987 }
1988
1989 std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType);
1990 return ret;
1991}
1992
1993// For an object previously identified (see getForcedType() and forceType)
1994// as needing type translations, do the translation needed for a load, turning
1995// an L-value into in R-value.
1996spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object)
1997{
1998 const auto forceIt = forceType.find(object);
1999 if (forceIt == forceType.end())
2000 return object;
2001
2002 spv::Id desiredTypeId = forceIt->second;
2003 spv::Id objectTypeId = builder.getTypeId(object);
2004 assert(builder.isPointerType(objectTypeId));
2005 objectTypeId = builder.getContainedTypeId(objectTypeId);
2006 if (builder.isVectorType(objectTypeId) &&
2007 builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) {
2008 if (builder.getScalarTypeWidth(desiredTypeId) == 64) {
2009 // handle 32-bit v.xy* -> 64-bit
2010 builder.clearAccessChain();
2011 builder.setAccessChainLValue(object);
2012 object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId);
2013 std::vector<spv::Id> components;
2014 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0));
2015 components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1));
2016
2017 spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2);
2018 return builder.createUnaryOp(spv::OpBitcast, desiredTypeId,
2019 builder.createCompositeConstruct(vecType, components));
2020 } else {
2021 logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar");
2022 }
2023 } else {
2024 logger->missingFunctionality("forcing non 32-bit vector type");
2025 }
2026
2027 return object;
2028}
2029
John Kessenich140f3df2015-06-26 16:58:36 -06002030bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
2031{
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002032 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002033
qining40887662016-04-03 22:20:42 -04002034 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2035 if (node->getType().getQualifier().isSpecConstant())
2036 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2037
John Kessenichfc51d282015-08-19 13:34:18 -06002038 spv::Id result = spv::NoResult;
2039
2040 // try texturing first
2041 result = createImageTextureFunctionCall(node);
2042 if (result != spv::NoResult) {
2043 builder.clearAccessChain();
2044 builder.setAccessChainRValue(result);
2045
2046 return false; // done with this node
2047 }
2048
2049 // Non-texturing.
John Kessenichc9a80832015-09-12 12:17:44 -06002050
2051 if (node->getOp() == glslang::EOpArrayLength) {
2052 // Quite special; won't want to evaluate the operand.
2053
John Kessenich5611c6d2018-04-05 11:25:02 -06002054 // Currently, the front-end does not allow .length() on an array until it is sized,
2055 // except for the last block membeor of an SSBO.
2056 // TODO: If this changes, link-time sized arrays might show up here, and need their
2057 // size extracted.
2058
John Kessenichc9a80832015-09-12 12:17:44 -06002059 // Normal .length() would have been constant folded by the front-end.
2060 // So, this has to be block.lastMember.length().
John Kessenichee21fc92015-09-21 21:50:29 -06002061 // SPV wants "block" and member number as the operands, go get them.
John Kessenichead86222018-03-28 18:01:20 -06002062
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002063 spv::Id length;
2064 if (node->getOperand()->getType().isCoopMat()) {
2065 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2066
2067 spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType());
2068 assert(builder.isCooperativeMatrixType(typeId));
2069
2070 length = builder.createCooperativeMatrixLength(typeId);
2071 } else {
2072 glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
2073 block->traverse(this);
2074 unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst();
2075 length = builder.createArrayLength(builder.accessChainGetLValue(), member);
2076 }
John Kessenichc9a80832015-09-12 12:17:44 -06002077
John Kessenich8c869672018-11-28 07:01:37 -07002078 // GLSL semantics say the result of .length() is an int, while SPIR-V says
2079 // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's
2080 // AST expectation of a signed result.
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002081 if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) {
2082 if (builder.isInSpecConstCodeGenMode()) {
2083 length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0));
2084 } else {
2085 length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length);
2086 }
2087 }
John Kessenich8c869672018-11-28 07:01:37 -07002088
John Kessenichc9a80832015-09-12 12:17:44 -06002089 builder.clearAccessChain();
2090 builder.setAccessChainRValue(length);
2091
2092 return false;
2093 }
2094
John Kessenichfc51d282015-08-19 13:34:18 -06002095 // Start by evaluating the operand
2096
John Kessenich8c8505c2016-07-26 12:50:38 -06002097 // Does it need a swizzle inversion? If so, evaluation is inverted;
2098 // operate first on the swizzle base, then apply the swizzle.
2099 spv::Id invertedType = spv::NoType;
2100 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
2101 if (node->getOp() == glslang::EOpInterpolateAtCentroid)
2102 invertedType = getInvertedSwizzleType(*node->getOperand());
2103
John Kessenich140f3df2015-06-26 16:58:36 -06002104 builder.clearAccessChain();
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002105 TIntermNode *operandNode;
John Kessenich8c8505c2016-07-26 12:50:38 -06002106 if (invertedType != spv::NoType)
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002107 operandNode = node->getOperand()->getAsBinaryNode()->getLeft();
John Kessenich8c8505c2016-07-26 12:50:38 -06002108 else
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002109 operandNode = node->getOperand();
2110
2111 operandNode->traverse(this);
Rex Xu30f92582015-09-14 10:38:56 +08002112
Rex Xufc618912015-09-09 16:42:49 +08002113 spv::Id operand = spv::NoResult;
2114
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002115 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2116
John Kessenichfb4f2332019-08-09 03:49:15 -06002117#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08002118 if (node->getOp() == glslang::EOpAtomicCounterIncrement ||
2119 node->getOp() == glslang::EOpAtomicCounterDecrement ||
Rex Xu7a26c172015-12-08 17:12:09 +08002120 node->getOp() == glslang::EOpAtomicCounter ||
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002121 node->getOp() == glslang::EOpInterpolateAtCentroid) {
Rex Xufc618912015-09-09 16:42:49 +08002122 operand = builder.accessChainGetLValue(); // Special case l-value operands
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002123 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
2124 lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType());
2125 } else
John Kessenichfb4f2332019-08-09 03:49:15 -06002126#endif
2127 {
John Kessenich32cfd492016-02-02 12:37:46 -07002128 operand = accessChainLoad(node->getOperand()->getType());
John Kessenichfb4f2332019-08-09 03:49:15 -06002129 }
John Kessenich140f3df2015-06-26 16:58:36 -06002130
John Kessenichead86222018-03-28 18:01:20 -06002131 OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()),
John Kessenich5611c6d2018-04-05 11:25:02 -06002132 TranslateNoContractionDecoration(node->getType().getQualifier()),
2133 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenich140f3df2015-06-26 16:58:36 -06002134
2135 // it could be a conversion
John Kessenichfc51d282015-08-19 13:34:18 -06002136 if (! result)
John Kessenichead86222018-03-28 18:01:20 -06002137 result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType());
John Kessenich140f3df2015-06-26 16:58:36 -06002138
2139 // if not, then possibly an operation
2140 if (! result)
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002141 result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType(), lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002142
2143 if (result) {
John Kessenich5611c6d2018-04-05 11:25:02 -06002144 if (invertedType) {
John Kessenichead86222018-03-28 18:01:20 -06002145 result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result);
John Kessenichb9197c82019-08-11 07:41:45 -06002146 decorations.addNonUniform(builder, result);
John Kessenich5611c6d2018-04-05 11:25:02 -06002147 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002148
John Kessenich140f3df2015-06-26 16:58:36 -06002149 builder.clearAccessChain();
2150 builder.setAccessChainRValue(result);
2151
2152 return false; // done with this node
2153 }
2154
2155 // it must be a special case, check...
2156 switch (node->getOp()) {
2157 case glslang::EOpPostIncrement:
2158 case glslang::EOpPostDecrement:
2159 case glslang::EOpPreIncrement:
2160 case glslang::EOpPreDecrement:
2161 {
2162 // we need the integer value "1" or the floating point "1.0" to add/subtract
Rex Xu8ff43de2016-04-22 16:51:45 +08002163 spv::Id one = 0;
2164 if (node->getBasicType() == glslang::EbtFloat)
2165 one = builder.makeFloatConstant(1.0F);
John Kessenich39697cd2019-08-08 10:35:51 -06002166#ifndef GLSLANG_WEB
Rex Xuce31aea2016-07-29 16:13:04 +08002167 else if (node->getBasicType() == glslang::EbtDouble)
2168 one = builder.makeDoubleConstant(1.0);
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002169 else if (node->getBasicType() == glslang::EbtFloat16)
2170 one = builder.makeFloat16Constant(1.0F);
John Kessenich66011cb2018-03-06 16:12:04 -07002171 else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8)
2172 one = builder.makeInt8Constant(1);
Rex Xucabbb782017-03-24 13:41:14 +08002173 else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16)
2174 one = builder.makeInt16Constant(1);
John Kessenich66011cb2018-03-06 16:12:04 -07002175 else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64)
2176 one = builder.makeInt64Constant(1);
John Kessenich39697cd2019-08-08 10:35:51 -06002177#endif
Rex Xu8ff43de2016-04-22 16:51:45 +08002178 else
2179 one = builder.makeIntConstant(1);
John Kessenich140f3df2015-06-26 16:58:36 -06002180 glslang::TOperator op;
2181 if (node->getOp() == glslang::EOpPreIncrement ||
2182 node->getOp() == glslang::EOpPostIncrement)
2183 op = glslang::EOpAdd;
2184 else
2185 op = glslang::EOpSub;
2186
John Kessenichead86222018-03-28 18:01:20 -06002187 spv::Id result = createBinaryOperation(op, decorations,
Rex Xu8ff43de2016-04-22 16:51:45 +08002188 convertGlslangToSpvType(node->getType()), operand, one,
2189 node->getType().getBasicType());
John Kessenich55e7d112015-11-15 21:33:39 -07002190 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002191
2192 // The result of operation is always stored, but conditionally the
2193 // consumed result. The consumed result is always an r-value.
2194 builder.accessChainStore(result);
2195 builder.clearAccessChain();
2196 if (node->getOp() == glslang::EOpPreIncrement ||
2197 node->getOp() == glslang::EOpPreDecrement)
2198 builder.setAccessChainRValue(result);
2199 else
2200 builder.setAccessChainRValue(operand);
2201 }
2202
2203 return false;
2204
John Kessenich155d3512019-08-08 23:29:20 -06002205#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06002206 case glslang::EOpEmitStreamVertex:
2207 builder.createNoResultOp(spv::OpEmitStreamVertex, operand);
2208 return false;
2209 case glslang::EOpEndStreamPrimitive:
2210 builder.createNoResultOp(spv::OpEndStreamPrimitive, operand);
2211 return false;
John Kessenich155d3512019-08-08 23:29:20 -06002212#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002213
2214 default:
Lei Zhang17535f72016-05-04 15:55:59 -04002215 logger->missingFunctionality("unknown glslang unary");
John Kessenich50e57562015-12-21 21:21:11 -07002216 return true; // pick up operand as placeholder result
John Kessenich140f3df2015-06-26 16:58:36 -06002217 }
John Kessenich140f3df2015-06-26 16:58:36 -06002218}
2219
Jeff Bolz53134492019-06-25 13:31:10 -05002220// Construct a composite object, recursively copying members if their types don't match
2221spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents)
2222{
2223 for (int c = 0; c < (int)constituents.size(); ++c) {
2224 spv::Id& constituent = constituents[c];
2225 spv::Id lType = builder.getContainedTypeId(resultTypeId, c);
2226 spv::Id rType = builder.getTypeId(constituent);
2227 if (lType != rType) {
2228 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
2229 constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent);
2230 } else if (builder.isStructType(rType)) {
2231 std::vector<spv::Id> rTypeConstituents;
2232 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2233 for (int i = 0; i < numrTypeConstituents; ++i) {
2234 rTypeConstituents.push_back(builder.createCompositeExtract(constituent, builder.getContainedTypeId(rType, i), i));
2235 }
2236 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2237 } else {
2238 assert(builder.isArrayType(rType));
2239 std::vector<spv::Id> rTypeConstituents;
2240 int numrTypeConstituents = builder.getNumTypeConstituents(rType);
2241
2242 spv::Id elementRType = builder.getContainedTypeId(rType);
2243 for (int i = 0; i < numrTypeConstituents; ++i) {
2244 rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i));
2245 }
2246 constituents[c] = createCompositeConstruct(lType, rTypeConstituents);
2247 }
2248 }
2249 }
2250 return builder.createCompositeConstruct(resultTypeId, constituents);
2251}
2252
John Kessenich140f3df2015-06-26 16:58:36 -06002253bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
2254{
qining27e04a02016-04-14 16:40:20 -04002255 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
2256 if (node->getType().getQualifier().isSpecConstant())
2257 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
2258
John Kessenichfc51d282015-08-19 13:34:18 -06002259 spv::Id result = spv::NoResult;
John Kessenich8c8505c2016-07-26 12:50:38 -06002260 spv::Id invertedType = spv::NoType; // to use to override the natural type of the node
2261 auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); };
John Kessenichfc51d282015-08-19 13:34:18 -06002262
2263 // try texturing
2264 result = createImageTextureFunctionCall(node);
2265 if (result != spv::NoResult) {
2266 builder.clearAccessChain();
2267 builder.setAccessChainRValue(result);
2268
2269 return false;
John Kessenicha28f7a72019-08-06 07:00:58 -06002270 }
2271#ifndef GLSLANG_WEB
2272 else if (node->getOp() == glslang::EOpImageStore ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002273 node->getOp() == glslang::EOpImageStoreLod ||
Jeff Bolz36831c92018-09-05 10:11:41 -05002274 node->getOp() == glslang::EOpImageAtomicStore) {
Rex Xufc618912015-09-09 16:42:49 +08002275 // "imageStore" is a special case, which has no result
2276 return false;
2277 }
John Kessenicha28f7a72019-08-06 07:00:58 -06002278#endif
John Kessenichfc51d282015-08-19 13:34:18 -06002279
John Kessenich140f3df2015-06-26 16:58:36 -06002280 glslang::TOperator binOp = glslang::EOpNull;
2281 bool reduceComparison = true;
2282 bool isMatrix = false;
2283 bool noReturnValue = false;
John Kessenich426394d2015-07-23 10:22:48 -06002284 bool atomic = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002285
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002286 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
2287
John Kessenich140f3df2015-06-26 16:58:36 -06002288 assert(node->getOp());
2289
John Kessenichf6640762016-08-01 19:44:00 -06002290 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenich140f3df2015-06-26 16:58:36 -06002291
2292 switch (node->getOp()) {
2293 case glslang::EOpSequence:
2294 {
2295 if (preVisit)
2296 ++sequenceDepth;
2297 else
2298 --sequenceDepth;
2299
2300 if (sequenceDepth == 1) {
2301 // If this is the parent node of all the functions, we want to see them
2302 // early, so all call points have actual SPIR-V functions to reference.
2303 // In all cases, still let the traverser visit the children for us.
2304 makeFunctions(node->getAsAggregate()->getSequence());
2305
John Kessenich6fccb3c2016-09-19 16:01:41 -06002306 // Also, we want all globals initializers to go into the beginning of the entry point, before
John Kessenich140f3df2015-06-26 16:58:36 -06002307 // anything else gets there, so visit out of order, doing them all now.
2308 makeGlobalInitializers(node->getAsAggregate()->getSequence());
2309
John Kessenich6a60c2f2016-12-08 21:01:59 -07002310 // 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 -06002311 // so do them manually.
2312 visitFunctions(node->getAsAggregate()->getSequence());
2313
2314 return false;
2315 }
2316
2317 return true;
2318 }
2319 case glslang::EOpLinkerObjects:
2320 {
2321 if (visit == glslang::EvPreVisit)
2322 linkageOnly = true;
2323 else
2324 linkageOnly = false;
2325
2326 return true;
2327 }
2328 case glslang::EOpComma:
2329 {
2330 // processing from left to right naturally leaves the right-most
2331 // lying around in the access chain
2332 glslang::TIntermSequence& glslangOperands = node->getSequence();
2333 for (int i = 0; i < (int)glslangOperands.size(); ++i)
2334 glslangOperands[i]->traverse(this);
2335
2336 return false;
2337 }
2338 case glslang::EOpFunction:
2339 if (visit == glslang::EvPreVisit) {
John Kessenich6fccb3c2016-09-19 16:01:41 -06002340 if (isShaderEntryPoint(node)) {
John Kessenich517fe7a2016-11-26 13:31:47 -07002341 inEntryPoint = true;
John Kessenich140f3df2015-06-26 16:58:36 -06002342 builder.setBuildPoint(shaderEntry->getLastBlock());
John Kesseniched33e052016-10-06 12:59:51 -06002343 currentFunction = shaderEntry;
John Kessenich140f3df2015-06-26 16:58:36 -06002344 } else {
2345 handleFunctionEntry(node);
2346 }
2347 } else {
John Kessenich517fe7a2016-11-26 13:31:47 -07002348 if (inEntryPoint)
2349 entryPointTerminated = true;
John Kesseniche770b3e2015-09-14 20:58:02 -06002350 builder.leaveFunction();
John Kessenich517fe7a2016-11-26 13:31:47 -07002351 inEntryPoint = false;
John Kessenich140f3df2015-06-26 16:58:36 -06002352 }
2353
2354 return true;
2355 case glslang::EOpParameters:
2356 // Parameters will have been consumed by EOpFunction processing, but not
2357 // the body, so we still visited the function node's children, making this
2358 // child redundant.
2359 return false;
2360 case glslang::EOpFunctionCall:
2361 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002362 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002363 if (node->isUserDefined())
2364 result = handleUserFunctionCall(node);
John Kessenich927608b2017-01-06 12:34:14 -07002365 // 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 -07002366 if (result) {
2367 builder.clearAccessChain();
2368 builder.setAccessChainRValue(result);
2369 } else
Lei Zhang17535f72016-05-04 15:55:59 -04002370 logger->missingFunctionality("missing user function; linker needs to catch that");
John Kessenich140f3df2015-06-26 16:58:36 -06002371
2372 return false;
2373 }
2374 case glslang::EOpConstructMat2x2:
2375 case glslang::EOpConstructMat2x3:
2376 case glslang::EOpConstructMat2x4:
2377 case glslang::EOpConstructMat3x2:
2378 case glslang::EOpConstructMat3x3:
2379 case glslang::EOpConstructMat3x4:
2380 case glslang::EOpConstructMat4x2:
2381 case glslang::EOpConstructMat4x3:
2382 case glslang::EOpConstructMat4x4:
2383 case glslang::EOpConstructDMat2x2:
2384 case glslang::EOpConstructDMat2x3:
2385 case glslang::EOpConstructDMat2x4:
2386 case glslang::EOpConstructDMat3x2:
2387 case glslang::EOpConstructDMat3x3:
2388 case glslang::EOpConstructDMat3x4:
2389 case glslang::EOpConstructDMat4x2:
2390 case glslang::EOpConstructDMat4x3:
2391 case glslang::EOpConstructDMat4x4:
LoopDawg174ccb82017-05-20 21:40:27 -06002392 case glslang::EOpConstructIMat2x2:
2393 case glslang::EOpConstructIMat2x3:
2394 case glslang::EOpConstructIMat2x4:
2395 case glslang::EOpConstructIMat3x2:
2396 case glslang::EOpConstructIMat3x3:
2397 case glslang::EOpConstructIMat3x4:
2398 case glslang::EOpConstructIMat4x2:
2399 case glslang::EOpConstructIMat4x3:
2400 case glslang::EOpConstructIMat4x4:
2401 case glslang::EOpConstructUMat2x2:
2402 case glslang::EOpConstructUMat2x3:
2403 case glslang::EOpConstructUMat2x4:
2404 case glslang::EOpConstructUMat3x2:
2405 case glslang::EOpConstructUMat3x3:
2406 case glslang::EOpConstructUMat3x4:
2407 case glslang::EOpConstructUMat4x2:
2408 case glslang::EOpConstructUMat4x3:
2409 case glslang::EOpConstructUMat4x4:
2410 case glslang::EOpConstructBMat2x2:
2411 case glslang::EOpConstructBMat2x3:
2412 case glslang::EOpConstructBMat2x4:
2413 case glslang::EOpConstructBMat3x2:
2414 case glslang::EOpConstructBMat3x3:
2415 case glslang::EOpConstructBMat3x4:
2416 case glslang::EOpConstructBMat4x2:
2417 case glslang::EOpConstructBMat4x3:
2418 case glslang::EOpConstructBMat4x4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002419 case glslang::EOpConstructF16Mat2x2:
2420 case glslang::EOpConstructF16Mat2x3:
2421 case glslang::EOpConstructF16Mat2x4:
2422 case glslang::EOpConstructF16Mat3x2:
2423 case glslang::EOpConstructF16Mat3x3:
2424 case glslang::EOpConstructF16Mat3x4:
2425 case glslang::EOpConstructF16Mat4x2:
2426 case glslang::EOpConstructF16Mat4x3:
2427 case glslang::EOpConstructF16Mat4x4:
John Kessenich140f3df2015-06-26 16:58:36 -06002428 isMatrix = true;
2429 // fall through
2430 case glslang::EOpConstructFloat:
2431 case glslang::EOpConstructVec2:
2432 case glslang::EOpConstructVec3:
2433 case glslang::EOpConstructVec4:
2434 case glslang::EOpConstructDouble:
2435 case glslang::EOpConstructDVec2:
2436 case glslang::EOpConstructDVec3:
2437 case glslang::EOpConstructDVec4:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08002438 case glslang::EOpConstructFloat16:
2439 case glslang::EOpConstructF16Vec2:
2440 case glslang::EOpConstructF16Vec3:
2441 case glslang::EOpConstructF16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002442 case glslang::EOpConstructBool:
2443 case glslang::EOpConstructBVec2:
2444 case glslang::EOpConstructBVec3:
2445 case glslang::EOpConstructBVec4:
John Kessenich66011cb2018-03-06 16:12:04 -07002446 case glslang::EOpConstructInt8:
2447 case glslang::EOpConstructI8Vec2:
2448 case glslang::EOpConstructI8Vec3:
2449 case glslang::EOpConstructI8Vec4:
2450 case glslang::EOpConstructUint8:
2451 case glslang::EOpConstructU8Vec2:
2452 case glslang::EOpConstructU8Vec3:
2453 case glslang::EOpConstructU8Vec4:
2454 case glslang::EOpConstructInt16:
2455 case glslang::EOpConstructI16Vec2:
2456 case glslang::EOpConstructI16Vec3:
2457 case glslang::EOpConstructI16Vec4:
2458 case glslang::EOpConstructUint16:
2459 case glslang::EOpConstructU16Vec2:
2460 case glslang::EOpConstructU16Vec3:
2461 case glslang::EOpConstructU16Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002462 case glslang::EOpConstructInt:
2463 case glslang::EOpConstructIVec2:
2464 case glslang::EOpConstructIVec3:
2465 case glslang::EOpConstructIVec4:
2466 case glslang::EOpConstructUint:
2467 case glslang::EOpConstructUVec2:
2468 case glslang::EOpConstructUVec3:
2469 case glslang::EOpConstructUVec4:
Rex Xu8ff43de2016-04-22 16:51:45 +08002470 case glslang::EOpConstructInt64:
2471 case glslang::EOpConstructI64Vec2:
2472 case glslang::EOpConstructI64Vec3:
2473 case glslang::EOpConstructI64Vec4:
2474 case glslang::EOpConstructUint64:
2475 case glslang::EOpConstructU64Vec2:
2476 case glslang::EOpConstructU64Vec3:
2477 case glslang::EOpConstructU64Vec4:
John Kessenich140f3df2015-06-26 16:58:36 -06002478 case glslang::EOpConstructStruct:
John Kessenich6c292d32016-02-15 20:58:50 -07002479 case glslang::EOpConstructTextureSampler:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06002480 case glslang::EOpConstructReference:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002481 case glslang::EOpConstructCooperativeMatrix:
John Kessenich140f3df2015-06-26 16:58:36 -06002482 {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002483 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich140f3df2015-06-26 16:58:36 -06002484 std::vector<spv::Id> arguments;
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002485 translateArguments(*node, arguments, lvalueCoherentFlags);
John Kessenich140f3df2015-06-26 16:58:36 -06002486 spv::Id constructed;
John Kessenich6c292d32016-02-15 20:58:50 -07002487 if (node->getOp() == glslang::EOpConstructTextureSampler)
John Kessenich8c8505c2016-07-26 12:50:38 -06002488 constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002489 else if (node->getOp() == glslang::EOpConstructStruct ||
2490 node->getOp() == glslang::EOpConstructCooperativeMatrix ||
2491 node->getType().isArray()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002492 std::vector<spv::Id> constituents;
2493 for (int c = 0; c < (int)arguments.size(); ++c)
2494 constituents.push_back(arguments[c]);
Jeff Bolz53134492019-06-25 13:31:10 -05002495 constructed = createCompositeConstruct(resultType(), constituents);
John Kessenich55e7d112015-11-15 21:33:39 -07002496 } else if (isMatrix)
John Kessenich8c8505c2016-07-26 12:50:38 -06002497 constructed = builder.createMatrixConstructor(precision, arguments, resultType());
John Kessenich55e7d112015-11-15 21:33:39 -07002498 else
John Kessenich8c8505c2016-07-26 12:50:38 -06002499 constructed = builder.createConstructor(precision, arguments, resultType());
John Kessenich140f3df2015-06-26 16:58:36 -06002500
2501 builder.clearAccessChain();
2502 builder.setAccessChainRValue(constructed);
2503
2504 return false;
2505 }
2506
2507 // These six are component-wise compares with component-wise results.
2508 // Forward on to createBinaryOperation(), requesting a vector result.
2509 case glslang::EOpLessThan:
2510 case glslang::EOpGreaterThan:
2511 case glslang::EOpLessThanEqual:
2512 case glslang::EOpGreaterThanEqual:
2513 case glslang::EOpVectorEqual:
2514 case glslang::EOpVectorNotEqual:
2515 {
2516 // Map the operation to a binary
2517 binOp = node->getOp();
2518 reduceComparison = false;
2519 switch (node->getOp()) {
2520 case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break;
2521 case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break;
2522 default: binOp = node->getOp(); break;
2523 }
2524
2525 break;
2526 }
2527 case glslang::EOpMul:
John Kessenich8c8505c2016-07-26 12:50:38 -06002528 // component-wise matrix multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002529 binOp = glslang::EOpMul;
2530 break;
2531 case glslang::EOpOuterProduct:
2532 // two vectors multiplied to make a matrix
2533 binOp = glslang::EOpOuterProduct;
2534 break;
2535 case glslang::EOpDot:
2536 {
qining25262b32016-05-06 17:25:16 -04002537 // for scalar dot product, use multiply
John Kessenich140f3df2015-06-26 16:58:36 -06002538 glslang::TIntermSequence& glslangOperands = node->getSequence();
John Kessenich8d72f1a2016-05-20 12:06:03 -06002539 if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1)
John Kessenich140f3df2015-06-26 16:58:36 -06002540 binOp = glslang::EOpMul;
2541 break;
2542 }
2543 case glslang::EOpMod:
2544 // when an aggregate, this is the floating-point mod built-in function,
2545 // which can be emitted by the one in createBinaryOperation()
2546 binOp = glslang::EOpMod;
2547 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002548
2549#ifndef GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06002550 case glslang::EOpEmitVertex:
2551 case glslang::EOpEndPrimitive:
2552 case glslang::EOpBarrier:
2553 case glslang::EOpMemoryBarrier:
2554 case glslang::EOpMemoryBarrierAtomicCounter:
2555 case glslang::EOpMemoryBarrierBuffer:
2556 case glslang::EOpMemoryBarrierImage:
2557 case glslang::EOpMemoryBarrierShared:
2558 case glslang::EOpGroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07002559 case glslang::EOpDeviceMemoryBarrier:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002560 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07002561 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
LoopDawg6e72fdd2016-06-15 09:50:24 -06002562 case glslang::EOpWorkgroupMemoryBarrier:
2563 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich66011cb2018-03-06 16:12:04 -07002564 case glslang::EOpSubgroupBarrier:
2565 case glslang::EOpSubgroupMemoryBarrier:
2566 case glslang::EOpSubgroupMemoryBarrierBuffer:
2567 case glslang::EOpSubgroupMemoryBarrierImage:
2568 case glslang::EOpSubgroupMemoryBarrierShared:
John Kessenich140f3df2015-06-26 16:58:36 -06002569 noReturnValue = true;
2570 // These all have 0 operands and will naturally finish up in the code below for 0 operands
2571 break;
2572
Jeff Bolz36831c92018-09-05 10:11:41 -05002573 case glslang::EOpAtomicStore:
2574 noReturnValue = true;
2575 // fallthrough
2576 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06002577 case glslang::EOpAtomicAdd:
2578 case glslang::EOpAtomicMin:
2579 case glslang::EOpAtomicMax:
2580 case glslang::EOpAtomicAnd:
2581 case glslang::EOpAtomicOr:
2582 case glslang::EOpAtomicXor:
2583 case glslang::EOpAtomicExchange:
2584 case glslang::EOpAtomicCompSwap:
2585 atomic = true;
2586 break;
2587
John Kessenich0d0c6d32017-07-23 16:08:26 -06002588 case glslang::EOpAtomicCounterAdd:
2589 case glslang::EOpAtomicCounterSubtract:
2590 case glslang::EOpAtomicCounterMin:
2591 case glslang::EOpAtomicCounterMax:
2592 case glslang::EOpAtomicCounterAnd:
2593 case glslang::EOpAtomicCounterOr:
2594 case glslang::EOpAtomicCounterXor:
2595 case glslang::EOpAtomicCounterExchange:
2596 case glslang::EOpAtomicCounterCompSwap:
2597 builder.addExtension("SPV_KHR_shader_atomic_counter_ops");
2598 builder.addCapability(spv::CapabilityAtomicStorageOps);
2599 atomic = true;
2600 break;
2601
Chao Chenb50c02e2018-09-19 11:42:24 -07002602 case glslang::EOpIgnoreIntersectionNV:
2603 case glslang::EOpTerminateRayNV:
2604 case glslang::EOpTraceNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07002605 case glslang::EOpExecuteCallableNV:
Chao Chen3c366992018-09-19 11:41:59 -07002606 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
2607 noReturnValue = true;
2608 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002609 case glslang::EOpCooperativeMatrixLoad:
2610 case glslang::EOpCooperativeMatrixStore:
2611 noReturnValue = true;
2612 break;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05002613 case glslang::EOpBeginInvocationInterlock:
2614 case glslang::EOpEndInvocationInterlock:
2615 builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock);
2616 noReturnValue = true;
2617 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002618#endif
Chao Chen3c366992018-09-19 11:41:59 -07002619
John Kessenich140f3df2015-06-26 16:58:36 -06002620 default:
2621 break;
2622 }
2623
2624 //
2625 // See if it maps to a regular operation.
2626 //
John Kessenich140f3df2015-06-26 16:58:36 -06002627 if (binOp != glslang::EOpNull) {
2628 glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped();
2629 glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped();
2630 assert(left && right);
2631
2632 builder.clearAccessChain();
2633 left->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002634 spv::Id leftId = accessChainLoad(left->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002635
2636 builder.clearAccessChain();
2637 right->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07002638 spv::Id rightId = accessChainLoad(right->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06002639
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002640 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichead86222018-03-28 18:01:20 -06002641 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002642 TranslateNoContractionDecoration(node->getType().getQualifier()),
2643 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002644 result = createBinaryOperation(binOp, decorations,
John Kessenich8c8505c2016-07-26 12:50:38 -06002645 resultType(), leftId, rightId,
John Kessenich140f3df2015-06-26 16:58:36 -06002646 left->getType().getBasicType(), reduceComparison);
2647
2648 // code above should only make binOp that exists in createBinaryOperation
John Kessenich55e7d112015-11-15 21:33:39 -07002649 assert(result != spv::NoResult);
John Kessenich140f3df2015-06-26 16:58:36 -06002650 builder.clearAccessChain();
2651 builder.setAccessChainRValue(result);
2652
2653 return false;
2654 }
2655
John Kessenich426394d2015-07-23 10:22:48 -06002656 //
2657 // Create the list of operands.
2658 //
John Kessenich140f3df2015-06-26 16:58:36 -06002659 glslang::TIntermSequence& glslangOperands = node->getSequence();
2660 std::vector<spv::Id> operands;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002661 std::vector<spv::IdImmediate> memoryAccessOperands;
John Kessenich140f3df2015-06-26 16:58:36 -06002662 for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) {
John Kessenich140f3df2015-06-26 16:58:36 -06002663 // special case l-value operands; there are just a few
2664 bool lvalue = false;
2665 switch (node->getOp()) {
John Kessenich140f3df2015-06-26 16:58:36 -06002666 case glslang::EOpModf:
2667 if (arg == 1)
2668 lvalue = true;
2669 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002670#ifndef GLSLANG_WEB
2671 case glslang::EOpFrexp:
2672 if (arg == 1)
2673 lvalue = true;
2674 break;
Rex Xu7a26c172015-12-08 17:12:09 +08002675 case glslang::EOpInterpolateAtSample:
2676 case glslang::EOpInterpolateAtOffset:
Rex Xu9d93a232016-05-05 12:30:44 +08002677 case glslang::EOpInterpolateAtVertex:
John Kessenich8c8505c2016-07-26 12:50:38 -06002678 if (arg == 0) {
Rex Xu7a26c172015-12-08 17:12:09 +08002679 lvalue = true;
John Kessenich8c8505c2016-07-26 12:50:38 -06002680
2681 // Does it need a swizzle inversion? If so, evaluation is inverted;
2682 // operate first on the swizzle base, then apply the swizzle.
John Kessenichecba76f2017-01-06 00:34:48 -07002683 if (glslangOperands[0]->getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06002684 glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
2685 invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType());
2686 }
Rex Xu7a26c172015-12-08 17:12:09 +08002687 break;
Rex Xud4782c12015-09-06 16:30:11 +08002688 case glslang::EOpAtomicAdd:
2689 case glslang::EOpAtomicMin:
2690 case glslang::EOpAtomicMax:
2691 case glslang::EOpAtomicAnd:
2692 case glslang::EOpAtomicOr:
2693 case glslang::EOpAtomicXor:
2694 case glslang::EOpAtomicExchange:
2695 case glslang::EOpAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05002696 case glslang::EOpAtomicLoad:
2697 case glslang::EOpAtomicStore:
John Kessenich0d0c6d32017-07-23 16:08:26 -06002698 case glslang::EOpAtomicCounterAdd:
2699 case glslang::EOpAtomicCounterSubtract:
2700 case glslang::EOpAtomicCounterMin:
2701 case glslang::EOpAtomicCounterMax:
2702 case glslang::EOpAtomicCounterAnd:
2703 case glslang::EOpAtomicCounterOr:
2704 case glslang::EOpAtomicCounterXor:
2705 case glslang::EOpAtomicCounterExchange:
2706 case glslang::EOpAtomicCounterCompSwap:
Rex Xud4782c12015-09-06 16:30:11 +08002707 if (arg == 0)
2708 lvalue = true;
2709 break;
John Kessenich55e7d112015-11-15 21:33:39 -07002710 case glslang::EOpAddCarry:
2711 case glslang::EOpSubBorrow:
2712 if (arg == 2)
2713 lvalue = true;
2714 break;
2715 case glslang::EOpUMulExtended:
2716 case glslang::EOpIMulExtended:
2717 if (arg >= 2)
2718 lvalue = true;
2719 break;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002720 case glslang::EOpCooperativeMatrixLoad:
2721 if (arg == 0 || arg == 1)
2722 lvalue = true;
2723 break;
2724 case glslang::EOpCooperativeMatrixStore:
2725 if (arg == 1)
2726 lvalue = true;
2727 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06002728#endif
John Kessenich140f3df2015-06-26 16:58:36 -06002729 default:
2730 break;
2731 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002732 builder.clearAccessChain();
2733 if (invertedType != spv::NoType && arg == 0)
2734 glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this);
2735 else
2736 glslangOperands[arg]->traverse(this);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002737
John Kessenichb9197c82019-08-11 07:41:45 -06002738#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002739 if (node->getOp() == glslang::EOpCooperativeMatrixLoad ||
2740 node->getOp() == glslang::EOpCooperativeMatrixStore) {
2741
2742 if (arg == 1) {
2743 // fold "element" parameter into the access chain
2744 spv::Builder::AccessChain save = builder.getAccessChain();
2745 builder.clearAccessChain();
2746 glslangOperands[2]->traverse(this);
2747
2748 spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType());
2749
2750 builder.setAccessChain(save);
2751
2752 // Point to the first element of the array.
2753 builder.accessChainPush(elementId, TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()),
Jeff Bolz7895e472019-03-06 13:34:10 -06002754 glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment());
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002755
2756 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
2757 unsigned int alignment = builder.getAccessChain().alignment;
2758
2759 int memoryAccess = TranslateMemoryAccess(coherentFlags);
2760 if (node->getOp() == glslang::EOpCooperativeMatrixLoad)
2761 memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask;
2762 if (node->getOp() == glslang::EOpCooperativeMatrixStore)
2763 memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask;
2764 if (builder.getStorageClass(builder.getAccessChain().base) == spv::StorageClassPhysicalStorageBufferEXT) {
2765 memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2766 }
2767
2768 memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess));
2769
2770 if (memoryAccess & spv::MemoryAccessAlignedMask) {
2771 memoryAccessOperands.push_back(spv::IdImmediate(false, alignment));
2772 }
2773
2774 if (memoryAccess & (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) {
2775 memoryAccessOperands.push_back(spv::IdImmediate(true, builder.makeUintConstant(TranslateMemoryScope(coherentFlags))));
2776 }
2777 } else if (arg == 2) {
2778 continue;
2779 }
2780 }
John Kessenichb9197c82019-08-11 07:41:45 -06002781#endif
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002782
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002783 if (lvalue) {
John Kessenich140f3df2015-06-26 16:58:36 -06002784 operands.push_back(builder.accessChainGetLValue());
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002785 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
2786 lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType());
2787 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002788 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenich32cfd492016-02-02 12:37:46 -07002789 operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType()));
John Kesseniche485c7a2017-05-31 18:50:53 -06002790 }
John Kessenich140f3df2015-06-26 16:58:36 -06002791 }
John Kessenich426394d2015-07-23 10:22:48 -06002792
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002793 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kessenichb9197c82019-08-11 07:41:45 -06002794#ifndef GLSLANG_WEB
Jeff Bolz4605e2e2019-02-19 13:10:32 -06002795 if (node->getOp() == glslang::EOpCooperativeMatrixLoad) {
2796 std::vector<spv::IdImmediate> idImmOps;
2797
2798 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
2799 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
2800 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
2801 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
2802 // get the pointee type
2803 spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0]));
2804 assert(builder.isCooperativeMatrixType(typeId));
2805 // do the op
2806 spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps);
2807 // store the result to the pointer (out param 'm')
2808 builder.createStore(result, operands[0]);
2809 result = 0;
2810 } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) {
2811 std::vector<spv::IdImmediate> idImmOps;
2812
2813 idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf
2814 idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object
2815 idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride
2816 idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor
2817 idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end());
2818
2819 builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps);
2820 result = 0;
2821 } else if (atomic) {
John Kessenich426394d2015-07-23 10:22:48 -06002822 // Handle all atomics
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002823 result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags);
John Kessenichb9197c82019-08-11 07:41:45 -06002824 } else
2825#endif
2826 {
John Kessenich426394d2015-07-23 10:22:48 -06002827 // Pass through to generic operations.
2828 switch (glslangOperands.size()) {
2829 case 0:
John Kessenich8c8505c2016-07-26 12:50:38 -06002830 result = createNoArgOperation(node->getOp(), precision, resultType());
John Kessenich426394d2015-07-23 10:22:48 -06002831 break;
2832 case 1:
John Kessenichead86222018-03-28 18:01:20 -06002833 {
2834 OpDecorations decorations = { precision,
John Kessenich5611c6d2018-04-05 11:25:02 -06002835 TranslateNoContractionDecoration(node->getType().getQualifier()),
2836 TranslateNonUniformDecoration(node->getType().getQualifier()) };
John Kessenichead86222018-03-28 18:01:20 -06002837 result = createUnaryOperation(
2838 node->getOp(), decorations,
2839 resultType(), operands.front(),
Jeff Bolz38a52fc2019-06-14 09:56:28 -05002840 glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags);
John Kessenichead86222018-03-28 18:01:20 -06002841 }
John Kessenich426394d2015-07-23 10:22:48 -06002842 break;
2843 default:
John Kessenich8c8505c2016-07-26 12:50:38 -06002844 result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType());
John Kessenich426394d2015-07-23 10:22:48 -06002845 break;
2846 }
John Kessenich8c8505c2016-07-26 12:50:38 -06002847 if (invertedType)
2848 result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result);
John Kessenich140f3df2015-06-26 16:58:36 -06002849 }
2850
2851 if (noReturnValue)
2852 return false;
2853
2854 if (! result) {
Lei Zhang17535f72016-05-04 15:55:59 -04002855 logger->missingFunctionality("unknown glslang aggregate");
John Kessenich50e57562015-12-21 21:21:11 -07002856 return true; // pick up a child as a placeholder operand
John Kessenich140f3df2015-06-26 16:58:36 -06002857 } else {
2858 builder.clearAccessChain();
2859 builder.setAccessChainRValue(result);
2860 return false;
2861 }
2862}
2863
John Kessenich433e9ff2017-01-26 20:31:11 -07002864// This path handles both if-then-else and ?:
2865// The if-then-else has a node type of void, while
2866// ?: has either a void or a non-void node type
2867//
2868// Leaving the result, when not void:
2869// GLSL only has r-values as the result of a :?, but
2870// if we have an l-value, that can be more efficient if it will
2871// become the base of a complex r-value expression, because the
2872// next layer copies r-values into memory to use the access-chain mechanism
John Kessenich140f3df2015-06-26 16:58:36 -06002873bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node)
2874{
John Kessenich0c1e71a2019-01-10 18:23:06 +07002875 // see if OpSelect can handle it
2876 const auto isOpSelectable = [&]() {
2877 if (node->getBasicType() == glslang::EbtVoid)
2878 return false;
2879 // OpSelect can do all other types starting with SPV 1.4
2880 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) {
2881 // pre-1.4, only scalars and vectors can be handled
2882 if ((!node->getType().isScalar() && !node->getType().isVector()))
2883 return false;
2884 }
2885 return true;
2886 };
2887
John Kessenich4bee5312018-02-20 21:29:05 -07002888 // See if it simple and safe, or required, to execute both sides.
2889 // Crucially, side effects must be either semantically required or avoided,
2890 // and there are performance trade-offs.
2891 // Return true if required or a good idea (and safe) to execute both sides,
2892 // false otherwise.
2893 const auto bothSidesPolicy = [&]() -> bool {
2894 // do we have both sides?
John Kessenich433e9ff2017-01-26 20:31:11 -07002895 if (node->getTrueBlock() == nullptr ||
2896 node->getFalseBlock() == nullptr)
2897 return false;
2898
John Kessenich4bee5312018-02-20 21:29:05 -07002899 // required? (unless we write additional code to look for side effects
2900 // and make performance trade-offs if none are present)
2901 if (!node->getShortCircuit())
2902 return true;
2903
2904 // if not required to execute both, decide based on performance/practicality...
2905
John Kessenich0c1e71a2019-01-10 18:23:06 +07002906 if (!isOpSelectable())
John Kessenich4bee5312018-02-20 21:29:05 -07002907 return false;
2908
John Kessenich433e9ff2017-01-26 20:31:11 -07002909 assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() &&
2910 node->getType() == node->getFalseBlock()->getAsTyped()->getType());
2911
2912 // return true if a single operand to ? : is okay for OpSelect
2913 const auto operandOkay = [](glslang::TIntermTyped* node) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07002914 return node->getAsSymbolNode() || node->getType().getQualifier().isConstant();
John Kessenich433e9ff2017-01-26 20:31:11 -07002915 };
2916
2917 return operandOkay(node->getTrueBlock() ->getAsTyped()) &&
2918 operandOkay(node->getFalseBlock()->getAsTyped());
2919 };
2920
John Kessenich4bee5312018-02-20 21:29:05 -07002921 spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue
2922 // emit the condition before doing anything with selection
2923 node->getCondition()->traverse(this);
2924 spv::Id condition = accessChainLoad(node->getCondition()->getType());
2925
2926 // Find a way of executing both sides and selecting the right result.
2927 const auto executeBothSides = [&]() -> void {
2928 // execute both sides
John Kessenich433e9ff2017-01-26 20:31:11 -07002929 node->getTrueBlock()->traverse(this);
2930 spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2931 node->getFalseBlock()->traverse(this);
2932 spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType());
2933
greg-lunarg5d43c4a2018-12-07 17:36:33 -07002934 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06002935
John Kessenich4bee5312018-02-20 21:29:05 -07002936 // done if void
2937 if (node->getBasicType() == glslang::EbtVoid)
2938 return;
John Kesseniche434ad92017-03-30 10:09:28 -06002939
John Kessenich4bee5312018-02-20 21:29:05 -07002940 // emit code to select between trueValue and falseValue
2941
2942 // see if OpSelect can handle it
John Kessenich0c1e71a2019-01-10 18:23:06 +07002943 if (isOpSelectable()) {
John Kessenich4bee5312018-02-20 21:29:05 -07002944 // Emit OpSelect for this selection.
2945
2946 // smear condition to vector, if necessary (AST is always scalar)
John Kessenich0c1e71a2019-01-10 18:23:06 +07002947 // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar
2948 if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) {
John Kessenich4bee5312018-02-20 21:29:05 -07002949 condition = builder.smearScalar(spv::NoPrecision, condition,
2950 builder.makeVectorType(builder.makeBoolType(),
2951 builder.getNumComponents(trueValue)));
John Kessenich0c1e71a2019-01-10 18:23:06 +07002952 }
John Kessenich4bee5312018-02-20 21:29:05 -07002953
2954 // OpSelect
2955 result = builder.createTriOp(spv::OpSelect,
2956 convertGlslangToSpvType(node->getType()), condition,
2957 trueValue, falseValue);
2958
2959 builder.clearAccessChain();
2960 builder.setAccessChainRValue(result);
2961 } else {
2962 // We need control flow to select the result.
2963 // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path.
2964 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
2965
2966 // Selection control:
2967 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2968
2969 // make an "if" based on the value created by the condition
2970 spv::Builder::If ifBuilder(condition, control, builder);
2971
2972 // emit the "then" statement
2973 builder.createStore(trueValue, result);
2974 ifBuilder.makeBeginElse();
2975 // emit the "else" statement
2976 builder.createStore(falseValue, result);
2977
2978 // finish off the control flow
2979 ifBuilder.makeEndIf();
2980
2981 builder.clearAccessChain();
2982 builder.setAccessChainLValue(result);
2983 }
John Kessenich433e9ff2017-01-26 20:31:11 -07002984 };
2985
John Kessenich4bee5312018-02-20 21:29:05 -07002986 // Execute the one side needed, as per the condition
2987 const auto executeOneSide = [&]() {
2988 // Always emit control flow.
2989 if (node->getBasicType() != glslang::EbtVoid)
2990 result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType()));
John Kessenich433e9ff2017-01-26 20:31:11 -07002991
John Kessenich4bee5312018-02-20 21:29:05 -07002992 // Selection control:
2993 const spv::SelectionControlMask control = TranslateSelectionControl(*node);
2994
2995 // make an "if" based on the value created by the condition
2996 spv::Builder::If ifBuilder(condition, control, builder);
2997
2998 // emit the "then" statement
2999 if (node->getTrueBlock() != nullptr) {
3000 node->getTrueBlock()->traverse(this);
3001 if (result != spv::NoResult)
3002 builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result);
3003 }
3004
3005 if (node->getFalseBlock() != nullptr) {
3006 ifBuilder.makeBeginElse();
3007 // emit the "else" statement
3008 node->getFalseBlock()->traverse(this);
3009 if (result != spv::NoResult)
3010 builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result);
3011 }
3012
3013 // finish off the control flow
3014 ifBuilder.makeEndIf();
3015
3016 if (result != spv::NoResult) {
3017 builder.clearAccessChain();
3018 builder.setAccessChainLValue(result);
3019 }
3020 };
3021
3022 // Try for OpSelect (or a requirement to execute both sides)
3023 if (bothSidesPolicy()) {
John Kessenich8e6c6ce2017-01-28 19:29:42 -07003024 SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
3025 if (node->getType().getQualifier().isSpecConstant())
3026 spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
John Kessenich4bee5312018-02-20 21:29:05 -07003027 executeBothSides();
3028 } else
3029 executeOneSide();
John Kessenich140f3df2015-06-26 16:58:36 -06003030
3031 return false;
3032}
3033
3034bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node)
3035{
3036 // emit and get the condition before doing anything with switch
3037 node->getCondition()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07003038 spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06003039
Rex Xu57e65922017-07-04 23:23:40 +08003040 // Selection control:
John Kesseniche18fd202018-01-30 11:01:39 -07003041 const spv::SelectionControlMask control = TranslateSwitchControl(*node);
Rex Xu57e65922017-07-04 23:23:40 +08003042
John Kessenich140f3df2015-06-26 16:58:36 -06003043 // browse the children to sort out code segments
3044 int defaultSegment = -1;
3045 std::vector<TIntermNode*> codeSegments;
3046 glslang::TIntermSequence& sequence = node->getBody()->getSequence();
3047 std::vector<int> caseValues;
3048 std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate
3049 for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) {
3050 TIntermNode* child = *c;
3051 if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault)
baldurkd76692d2015-07-12 11:32:58 +02003052 defaultSegment = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06003053 else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) {
baldurkd76692d2015-07-12 11:32:58 +02003054 valueIndexToSegment[caseValues.size()] = (int)codeSegments.size();
John Kessenich140f3df2015-06-26 16:58:36 -06003055 caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst());
3056 } else
3057 codeSegments.push_back(child);
3058 }
3059
qining25262b32016-05-06 17:25:16 -04003060 // handle the case where the last code segment is missing, due to no code
John Kessenich140f3df2015-06-26 16:58:36 -06003061 // statements between the last case and the end of the switch statement
3062 if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) ||
3063 (int)codeSegments.size() == defaultSegment)
3064 codeSegments.push_back(nullptr);
3065
3066 // make the switch statement
3067 std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call
Rex Xu57e65922017-07-04 23:23:40 +08003068 builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks);
John Kessenich140f3df2015-06-26 16:58:36 -06003069
3070 // emit all the code in the segments
3071 breakForLoop.push(false);
3072 for (unsigned int s = 0; s < codeSegments.size(); ++s) {
3073 builder.nextSwitchSegment(segmentBlocks, s);
3074 if (codeSegments[s])
3075 codeSegments[s]->traverse(this);
3076 else
3077 builder.addSwitchBreak();
3078 }
3079 breakForLoop.pop();
3080
3081 builder.endSwitch(segmentBlocks);
3082
3083 return false;
3084}
3085
3086void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
3087{
3088 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04003089 spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false);
John Kessenich140f3df2015-06-26 16:58:36 -06003090
3091 builder.clearAccessChain();
3092 builder.setAccessChainRValue(constant);
3093}
3094
3095bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
3096{
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003097 auto blocks = builder.makeNewLoop();
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003098 builder.createBranch(&blocks.head);
steve-lunargf1709e72017-05-02 20:14:50 -06003099
3100 // Loop control:
John Kessenich1f4d0462019-01-12 17:31:41 +07003101 std::vector<unsigned int> operands;
3102 const spv::LoopControlMask control = TranslateLoopControl(*node, operands);
steve-lunargf1709e72017-05-02 20:14:50 -06003103
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003104 // Spec requires back edges to target header blocks, and every header block
3105 // must dominate its merge block. Make a header block first to ensure these
3106 // conditions are met. By definition, it will contain OpLoopMerge, followed
3107 // by a block-ending branch. But we don't want to put any other body/test
3108 // instructions in it, since the body/test may have arbitrary instructions,
3109 // including merges of its own.
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003110 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003111 builder.setBuildPoint(&blocks.head);
John Kessenich1f4d0462019-01-12 17:31:41 +07003112 builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003113 if (node->testFirst() && node->getTest()) {
Dejan Mircevski213bbbe2016-01-20 11:51:43 -05003114 spv::Block& test = builder.makeNewBlock();
3115 builder.createBranch(&test);
3116
3117 builder.setBuildPoint(&test);
John Kessenich140f3df2015-06-26 16:58:36 -06003118 node->getTest()->traverse(this);
John Kesseniche485c7a2017-05-31 18:50:53 -06003119 spv::Id condition = accessChainLoad(node->getTest()->getType());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003120 builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
3121
3122 builder.setBuildPoint(&blocks.body);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003123 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003124 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003125 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003126 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003127 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003128
3129 builder.setBuildPoint(&blocks.continue_target);
3130 if (node->getTerminal())
3131 node->getTerminal()->traverse(this);
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003132 builder.createBranch(&blocks.head);
David Netoc22f37c2015-07-15 16:21:26 -04003133 } else {
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003134 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003135 builder.createBranch(&blocks.body);
3136
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003137 breakForLoop.push(true);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003138 builder.setBuildPoint(&blocks.body);
3139 if (node->getBody())
Dejan Mircevskie537b8b2016-01-10 19:37:00 -05003140 node->getBody()->traverse(this);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003141 builder.createBranch(&blocks.continue_target);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003142 breakForLoop.pop();
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003143
3144 builder.setBuildPoint(&blocks.continue_target);
3145 if (node->getTerminal())
3146 node->getTerminal()->traverse(this);
3147 if (node->getTest()) {
3148 node->getTest()->traverse(this);
3149 spv::Id condition =
John Kessenich32cfd492016-02-02 12:37:46 -07003150 accessChainLoad(node->getTest()->getType());
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003151 builder.createConditionalBranch(condition, &blocks.head, &blocks.merge);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003152 } else {
Dejan Mircevskied55bcd2016-01-19 21:13:38 -05003153 // TODO: unless there was a break/return/discard instruction
3154 // somewhere in the body, this is an infinite loop, so we should
3155 // issue a warning.
Dejan Mircevski832c65c2016-01-11 15:57:11 -05003156 builder.createBranch(&blocks.head);
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003157 }
John Kessenich140f3df2015-06-26 16:58:36 -06003158 }
Dejan Mircevski9c6734c2016-01-10 12:15:13 -05003159 builder.setBuildPoint(&blocks.merge);
Dejan Mircevskic8fbbab2016-01-11 14:48:36 -05003160 builder.closeLoop();
John Kessenich140f3df2015-06-26 16:58:36 -06003161 return false;
3162}
3163
3164bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node)
3165{
3166 if (node->getExpression())
3167 node->getExpression()->traverse(this);
3168
greg-lunarg5d43c4a2018-12-07 17:36:33 -07003169 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06003170
John Kessenich140f3df2015-06-26 16:58:36 -06003171 switch (node->getFlowOp()) {
3172 case glslang::EOpKill:
3173 builder.makeDiscard();
3174 break;
3175 case glslang::EOpBreak:
3176 if (breakForLoop.top())
3177 builder.createLoopExit();
3178 else
3179 builder.addSwitchBreak();
3180 break;
3181 case glslang::EOpContinue:
John Kessenich140f3df2015-06-26 16:58:36 -06003182 builder.createLoopContinue();
3183 break;
3184 case glslang::EOpReturn:
John Kesseniched33e052016-10-06 12:59:51 -06003185 if (node->getExpression()) {
3186 const glslang::TType& glslangReturnType = node->getExpression()->getType();
3187 spv::Id returnId = accessChainLoad(glslangReturnType);
3188 if (builder.getTypeId(returnId) != currentFunction->getReturnType()) {
3189 builder.clearAccessChain();
3190 spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType());
3191 builder.setAccessChainLValue(copyId);
3192 multiTypeStore(glslangReturnType, returnId);
3193 returnId = builder.createLoad(copyId);
3194 }
3195 builder.makeReturn(false, returnId);
3196 } else
John Kesseniche770b3e2015-09-14 20:58:02 -06003197 builder.makeReturn(false);
John Kessenich140f3df2015-06-26 16:58:36 -06003198
3199 builder.clearAccessChain();
3200 break;
3201
John Kessenichb9197c82019-08-11 07:41:45 -06003202#ifndef GLSLANG_WEB
Jeff Bolzba6170b2019-07-01 09:23:23 -05003203 case glslang::EOpDemote:
3204 builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT);
3205 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
3206 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
3207 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003208#endif
Jeff Bolzba6170b2019-07-01 09:23:23 -05003209
John Kessenich140f3df2015-06-26 16:58:36 -06003210 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003211 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003212 break;
3213 }
3214
3215 return false;
3216}
3217
John Kessenich9c14f772019-06-17 08:38:35 -06003218spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType)
John Kessenich140f3df2015-06-26 16:58:36 -06003219{
qining25262b32016-05-06 17:25:16 -04003220 // First, steer off constants, which are not SPIR-V variables, but
John Kessenich140f3df2015-06-26 16:58:36 -06003221 // can still have a mapping to a SPIR-V Id.
John Kessenich55e7d112015-11-15 21:33:39 -07003222 // This includes specialization constants.
John Kessenich7cc0e282016-03-20 00:46:02 -06003223 if (node->getQualifier().isConstant()) {
Dan Sinclair12fcaa22018-11-13 09:17:44 -05003224 spv::Id result = createSpvConstant(*node);
3225 if (result != spv::NoResult)
3226 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06003227 }
3228
3229 // Now, handle actual variables
John Kessenicha5c5fb62017-05-05 05:09:58 -06003230 spv::StorageClass storageClass = TranslateStorageClass(node->getType());
John Kessenich9c14f772019-06-17 08:38:35 -06003231 spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType())
3232 : forcedType;
John Kessenich140f3df2015-06-26 16:58:36 -06003233
John Kessenichb9197c82019-08-11 07:41:45 -06003234 const bool contains16BitType = node->getType().contains16BitFloat() ||
3235 node->getType().contains16BitInt();
Rex Xuf89ad982017-04-07 23:22:33 +08003236 if (contains16BitType) {
John Kessenich18310872018-05-14 22:08:53 -06003237 switch (storageClass) {
3238 case spv::StorageClassInput:
3239 case spv::StorageClassOutput:
John Kessenich8317e6c2019-08-18 23:58:08 -06003240 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003241 builder.addCapability(spv::CapabilityStorageInputOutput16);
John Kessenich18310872018-05-14 22:08:53 -06003242 break;
John Kessenich18310872018-05-14 22:08:53 -06003243 case spv::StorageClassUniform:
John Kessenich8317e6c2019-08-18 23:58:08 -06003244 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
Rex Xuf89ad982017-04-07 23:22:33 +08003245 if (node->getType().getQualifier().storage == glslang::EvqBuffer)
3246 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
John Kessenich18310872018-05-14 22:08:53 -06003247 else
3248 builder.addCapability(spv::CapabilityStorageUniform16);
3249 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003250#ifndef GLSLANG_WEB
3251 case spv::StorageClassPushConstant:
John Kessenich8317e6c2019-08-18 23:58:08 -06003252 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenichb9197c82019-08-11 07:41:45 -06003253 builder.addCapability(spv::CapabilityStoragePushConstant16);
3254 break;
John Kessenich18310872018-05-14 22:08:53 -06003255 case spv::StorageClassStorageBuffer:
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003256 case spv::StorageClassPhysicalStorageBufferEXT:
John Kessenich8317e6c2019-08-18 23:58:08 -06003257 builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
John Kessenich18310872018-05-14 22:08:53 -06003258 builder.addCapability(spv::CapabilityStorageUniformBufferBlock16);
3259 break;
John Kessenichb9197c82019-08-11 07:41:45 -06003260#endif
John Kessenich18310872018-05-14 22:08:53 -06003261 default:
John Kessenichb9197c82019-08-11 07:41:45 -06003262 if (node->getType().contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003263 builder.addCapability(spv::CapabilityFloat16);
John Kessenichb9197c82019-08-11 07:41:45 -06003264 if (node->getType().contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003265 builder.addCapability(spv::CapabilityInt16);
John Kessenich18310872018-05-14 22:08:53 -06003266 break;
Rex Xuf89ad982017-04-07 23:22:33 +08003267 }
3268 }
Rex Xuf89ad982017-04-07 23:22:33 +08003269
John Kessenichb9197c82019-08-11 07:41:45 -06003270 if (node->getType().contains8BitInt()) {
John Kessenich312dcfb2018-07-03 13:19:51 -06003271 if (storageClass == spv::StorageClassPushConstant) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003272 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003273 builder.addCapability(spv::CapabilityStoragePushConstant8);
3274 } else if (storageClass == spv::StorageClassUniform) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003275 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
John Kessenich312dcfb2018-07-03 13:19:51 -06003276 builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess);
Neil Henningb6b01f02018-10-23 15:02:29 +01003277 } else if (storageClass == spv::StorageClassStorageBuffer) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003278 builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
Neil Henningb6b01f02018-10-23 15:02:29 +01003279 builder.addCapability(spv::CapabilityStorageBuffer8BitAccess);
Jeff Bolz2b2316d2019-02-17 22:49:28 -06003280 } else {
3281 builder.addCapability(spv::CapabilityInt8);
John Kessenich312dcfb2018-07-03 13:19:51 -06003282 }
3283 }
3284
John Kessenich140f3df2015-06-26 16:58:36 -06003285 const char* name = node->getName().c_str();
3286 if (glslang::IsAnonymous(name))
3287 name = "";
3288
3289 return builder.createVariable(storageClass, spvType, name);
3290}
3291
3292// Return type Id of the sampled type.
3293spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
3294{
3295 switch (sampler.type) {
John Kessenicha28f7a72019-08-06 07:00:58 -06003296 case glslang::EbtInt: return builder.makeIntType(32);
3297 case glslang::EbtUint: return builder.makeUintType(32);
John Kessenich140f3df2015-06-26 16:58:36 -06003298 case glslang::EbtFloat: return builder.makeFloatType(32);
John Kessenicha28f7a72019-08-06 07:00:58 -06003299#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08003300 case glslang::EbtFloat16:
3301 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch);
3302 builder.addCapability(spv::CapabilityFloat16ImageAMD);
3303 return builder.makeFloatType(16);
3304#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003305 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003306 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003307 return builder.makeFloatType(32);
3308 }
3309}
3310
John Kessenich8c8505c2016-07-26 12:50:38 -06003311// If node is a swizzle operation, return the type that should be used if
3312// the swizzle base is first consumed by another operation, before the swizzle
3313// is applied.
3314spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
3315{
John Kessenichecba76f2017-01-06 00:34:48 -07003316 if (node.getAsOperator() &&
John Kessenich8c8505c2016-07-26 12:50:38 -06003317 node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle)
3318 return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType());
3319 else
3320 return spv::NoType;
3321}
3322
3323// When inverting a swizzle with a parent op, this function
3324// will apply the swizzle operation to a completed parent operation.
3325spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult)
3326{
3327 std::vector<unsigned> swizzle;
3328 convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle);
3329 return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle);
3330}
3331
John Kessenich8c8505c2016-07-26 12:50:38 -06003332// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V.
3333void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
3334{
3335 const glslang::TIntermSequence& swizzleSequence = node.getSequence();
3336 for (int i = 0; i < (int)swizzleSequence.size(); ++i)
3337 swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst());
3338}
3339
John Kessenich3ac051e2015-12-20 11:29:16 -07003340// Convert from a glslang type to an SPV type, by calling into a
3341// recursive version of this function. This establishes the inherited
3342// layout state rooted from the top-level type.
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003343spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
John Kessenich140f3df2015-06-26 16:58:36 -06003344{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003345 return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly);
John Kessenich31ed4832015-09-09 17:51:38 -06003346}
3347
3348// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id.
John Kessenich7b9fa252016-01-21 18:56:57 -07003349// explicitLayout can be kept the same throughout the hierarchical recursive walk.
John Kessenich6090df02016-06-30 21:18:02 -06003350// Mutually recursive with convertGlslangStructToSpvType().
John Kessenichead86222018-03-28 18:01:20 -06003351spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003352 glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
3353 bool lastBufferBlockMember, bool forwardReferenceOnly)
John Kessenich31ed4832015-09-09 17:51:38 -06003354{
John Kesseniche0b6cad2015-12-24 10:30:13 -07003355 spv::Id spvType = spv::NoResult;
John Kessenich140f3df2015-06-26 16:58:36 -06003356
3357 switch (type.getBasicType()) {
3358 case glslang::EbtVoid:
3359 spvType = builder.makeVoidType();
John Kessenich55e7d112015-11-15 21:33:39 -07003360 assert (! type.isArray());
John Kessenich140f3df2015-06-26 16:58:36 -06003361 break;
John Kessenich140f3df2015-06-26 16:58:36 -06003362 case glslang::EbtBool:
John Kessenich103bef92016-02-08 21:38:15 -07003363 // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is
3364 // a 32-bit int where non-0 means true.
3365 if (explicitLayout != glslang::ElpNone)
3366 spvType = builder.makeUintType(32);
3367 else
3368 spvType = builder.makeBoolType();
John Kessenich140f3df2015-06-26 16:58:36 -06003369 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003370 case glslang::EbtInt:
3371 spvType = builder.makeIntType(32);
3372 break;
3373 case glslang::EbtUint:
3374 spvType = builder.makeUintType(32);
3375 break;
3376 case glslang::EbtFloat:
3377 spvType = builder.makeFloatType(32);
3378 break;
3379#ifndef GLSLANG_WEB
3380 case glslang::EbtDouble:
3381 spvType = builder.makeFloatType(64);
3382 break;
3383 case glslang::EbtFloat16:
3384 spvType = builder.makeFloatType(16);
3385 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003386 case glslang::EbtInt8:
John Kessenich66011cb2018-03-06 16:12:04 -07003387 spvType = builder.makeIntType(8);
3388 break;
3389 case glslang::EbtUint8:
John Kessenich66011cb2018-03-06 16:12:04 -07003390 spvType = builder.makeUintType(8);
3391 break;
John Kessenich31aa3d62018-08-15 13:54:09 -06003392 case glslang::EbtInt16:
John Kessenich66011cb2018-03-06 16:12:04 -07003393 spvType = builder.makeIntType(16);
3394 break;
3395 case glslang::EbtUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07003396 spvType = builder.makeUintType(16);
3397 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08003398 case glslang::EbtInt64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003399 spvType = builder.makeIntType(64);
3400 break;
3401 case glslang::EbtUint64:
Rex Xu8ff43de2016-04-22 16:51:45 +08003402 spvType = builder.makeUintType(64);
3403 break;
John Kessenich426394d2015-07-23 10:22:48 -06003404 case glslang::EbtAtomicUint:
John Kessenich2d0cc782016-07-07 13:20:00 -06003405 builder.addCapability(spv::CapabilityAtomicStorage);
John Kessenich426394d2015-07-23 10:22:48 -06003406 spvType = builder.makeUintType(32);
3407 break;
Chao Chenb50c02e2018-09-19 11:42:24 -07003408 case glslang::EbtAccStructNV:
3409 spvType = builder.makeAccelerationStructureNVType();
3410 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06003411 case glslang::EbtReference:
3412 {
3413 // Make the forward pointer, then recurse to convert the structure type, then
3414 // patch up the forward pointer with a real pointer type.
3415 if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) {
3416 spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT);
3417 forwardPointers[type.getReferentType()] = forwardId;
3418 }
3419 spvType = forwardPointers[type.getReferentType()];
3420 if (!forwardReferenceOnly) {
3421 spv::Id referentType = convertGlslangToSpvType(*type.getReferentType());
3422 builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT,
3423 forwardPointers[type.getReferentType()],
3424 referentType);
3425 }
3426 }
3427 break;
Chao Chenb50c02e2018-09-19 11:42:24 -07003428#endif
John Kessenich140f3df2015-06-26 16:58:36 -06003429 case glslang::EbtSampler:
3430 {
3431 const glslang::TSampler& sampler = type.getSampler();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003432 if (sampler.isPureSampler()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003433 spvType = builder.makeSamplerType();
3434 } else {
3435 // an image is present, make its type
John Kessenich3e4b6ff2019-08-08 01:15:24 -06003436 spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler),
3437 sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(),
3438 sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type));
3439 if (sampler.isCombined()) {
John Kessenich6c292d32016-02-15 20:58:50 -07003440 // already has both image and sampler, make the combined type
3441 spvType = builder.makeSampledImageType(spvType);
3442 }
John Kessenich55e7d112015-11-15 21:33:39 -07003443 }
John Kesseniche0b6cad2015-12-24 10:30:13 -07003444 }
John Kessenich140f3df2015-06-26 16:58:36 -06003445 break;
3446 case glslang::EbtStruct:
3447 case glslang::EbtBlock:
3448 {
3449 // If we've seen this struct type, return it
John Kessenich6090df02016-06-30 21:18:02 -06003450 const glslang::TTypeList* glslangMembers = type.getStruct();
John Kesseniche0b6cad2015-12-24 10:30:13 -07003451
3452 // Try to share structs for different layouts, but not yet for other
3453 // kinds of qualification (primarily not yet including interpolant qualification).
John Kessenichf2b7f332016-09-01 17:05:23 -06003454 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003455 spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers];
John Kesseniche0b6cad2015-12-24 10:30:13 -07003456 if (spvType != spv::NoResult)
John Kessenich140f3df2015-06-26 16:58:36 -06003457 break;
3458
3459 // else, we haven't seen it...
John Kessenich140f3df2015-06-26 16:58:36 -06003460 if (type.getBasicType() == glslang::EbtBlock)
John Kessenich6090df02016-06-30 21:18:02 -06003461 memberRemapper[glslangMembers].resize(glslangMembers->size());
3462 spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier);
John Kessenich140f3df2015-06-26 16:58:36 -06003463 }
3464 break;
3465 default:
John Kessenich55e7d112015-11-15 21:33:39 -07003466 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06003467 break;
3468 }
3469
3470 if (type.isMatrix())
3471 spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows());
3472 else {
3473 // If this variable has a vector element count greater than 1, create a SPIR-V vector
3474 if (type.getVectorSize() > 1)
3475 spvType = builder.makeVectorType(spvType, type.getVectorSize());
3476 }
3477
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003478 if (type.isCoopMat()) {
3479 builder.addCapability(spv::CapabilityCooperativeMatrixNV);
3480 builder.addExtension(spv::E_SPV_NV_cooperative_matrix);
3481 if (type.getBasicType() == glslang::EbtFloat16)
3482 builder.addCapability(spv::CapabilityFloat16);
Jeff Bolz387657e2019-08-22 20:28:00 -05003483 if (type.getBasicType() == glslang::EbtUint8 ||
3484 type.getBasicType() == glslang::EbtInt8) {
3485 builder.addCapability(spv::CapabilityInt8);
3486 }
Jeff Bolz4605e2e2019-02-19 13:10:32 -06003487
3488 spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1);
3489 spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2);
3490 spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3);
3491
3492 spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols);
3493 }
3494
John Kessenich140f3df2015-06-26 16:58:36 -06003495 if (type.isArray()) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003496 int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride
3497
John Kessenichc9a80832015-09-12 12:17:44 -06003498 // Do all but the outer dimension
John Kessenichc9e0a422015-12-29 21:27:24 -07003499 if (type.getArraySizes()->getNumDims() > 1) {
John Kessenichf8842e52016-01-04 19:22:56 -07003500 // We need to decorate array strides for types needing explicit layout, except blocks.
3501 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) {
John Kessenichc9e0a422015-12-29 21:27:24 -07003502 // Use a dummy glslang type for querying internal strides of
3503 // arrays of arrays, but using just a one-dimensional array.
3504 glslang::TType simpleArrayType(type, 0); // deference type of the array
John Kessenich859b0342018-03-26 00:38:53 -06003505 while (simpleArrayType.getArraySizes()->getNumDims() > 1)
3506 simpleArrayType.getArraySizes()->dereference();
John Kessenichc9e0a422015-12-29 21:27:24 -07003507
3508 // Will compute the higher-order strides here, rather than making a whole
3509 // pile of types and doing repetitive recursion on their contents.
3510 stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix);
3511 }
John Kessenichf8842e52016-01-04 19:22:56 -07003512
3513 // make the arrays
John Kessenichc9e0a422015-12-29 21:27:24 -07003514 for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) {
John Kessenich6c292d32016-02-15 20:58:50 -07003515 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride);
John Kessenichc9e0a422015-12-29 21:27:24 -07003516 if (stride > 0)
3517 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich6c292d32016-02-15 20:58:50 -07003518 stride *= type.getArraySizes()->getDimSize(dim);
John Kessenichc9e0a422015-12-29 21:27:24 -07003519 }
3520 } else {
3521 // single-dimensional array, and don't yet have stride
3522
John Kessenichf8842e52016-01-04 19:22:56 -07003523 // We need to decorate array strides for types needing explicit layout, except blocks.
John Kessenichc9e0a422015-12-29 21:27:24 -07003524 if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock)
3525 stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix);
John Kessenichc9a80832015-09-12 12:17:44 -06003526 }
John Kessenich31ed4832015-09-09 17:51:38 -06003527
John Kessenichead86222018-03-28 18:01:20 -06003528 // Do the outer dimension, which might not be known for a runtime-sized array.
3529 // (Unsized arrays that survive through linking will be runtime-sized arrays)
3530 if (type.isSizedArray())
John Kessenich6c292d32016-02-15 20:58:50 -07003531 spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride);
John Kessenich5611c6d2018-04-05 11:25:02 -06003532 else {
John Kessenichb9197c82019-08-11 07:41:45 -06003533#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06003534 if (!lastBufferBlockMember) {
John Kessenich8317e6c2019-08-18 23:58:08 -06003535 builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5);
John Kessenich5611c6d2018-04-05 11:25:02 -06003536 builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT);
3537 }
John Kessenichead86222018-03-28 18:01:20 -06003538 spvType = builder.makeRuntimeArray(spvType);
John Kessenichb9197c82019-08-11 07:41:45 -06003539#endif
John Kessenich5611c6d2018-04-05 11:25:02 -06003540 }
John Kessenichc9e0a422015-12-29 21:27:24 -07003541 if (stride > 0)
3542 builder.addDecoration(spvType, spv::DecorationArrayStride, stride);
John Kessenich140f3df2015-06-26 16:58:36 -06003543 }
3544
3545 return spvType;
3546}
3547
John Kessenich0e737842017-03-24 18:38:16 -06003548// TODO: this functionality should exist at a higher level, in creating the AST
3549//
3550// Identify interface members that don't have their required extension turned on.
3551//
3552bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
3553{
John Kessenicha28f7a72019-08-06 07:00:58 -06003554#ifndef GLSLANG_WEB
John Kessenich0e737842017-03-24 18:38:16 -06003555 auto& extensions = glslangIntermediate->getRequestedExtensions();
3556
Rex Xubcf291a2017-03-29 23:01:36 +08003557 if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
3558 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3559 return true;
John Kessenich0e737842017-03-24 18:38:16 -06003560 if (member.getFieldName() == "gl_SecondaryPositionNV" &&
3561 extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
3562 return true;
Chao Chen3c366992018-09-19 11:41:59 -07003563
3564 if (glslangIntermediate->getStage() != EShLangMeshNV) {
3565 if (member.getFieldName() == "gl_ViewportMask" &&
3566 extensions.find("GL_NV_viewport_array2") == extensions.end())
3567 return true;
3568 if (member.getFieldName() == "gl_PositionPerViewNV" &&
3569 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3570 return true;
3571 if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
3572 extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
3573 return true;
3574 }
3575#endif
John Kessenich0e737842017-03-24 18:38:16 -06003576
3577 return false;
3578};
3579
John Kessenich6090df02016-06-30 21:18:02 -06003580// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id.
3581// explicitLayout can be kept the same throughout the hierarchical recursive walk.
3582// Mutually recursive with convertGlslangToSpvType().
3583spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
3584 const glslang::TTypeList* glslangMembers,
3585 glslang::TLayoutPacking explicitLayout,
3586 const glslang::TQualifier& qualifier)
3587{
3588 // Create a vector of struct types for SPIR-V to consume
3589 std::vector<spv::Id> spvMembers;
3590 int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003591 std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers;
John Kessenich6090df02016-06-30 21:18:02 -06003592 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3593 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3594 if (glslangMember.hiddenMember()) {
3595 ++memberDelta;
3596 if (type.getBasicType() == glslang::EbtBlock)
3597 memberRemapper[glslangMembers][i] = -1;
3598 } else {
John Kessenich0e737842017-03-24 18:38:16 -06003599 if (type.getBasicType() == glslang::EbtBlock) {
Ashwin Lelec1e61d62019-07-22 12:36:38 -07003600 if (filterMember(glslangMember)) {
3601 memberDelta++;
3602 memberRemapper[glslangMembers][i] = -1;
John Kessenich0e737842017-03-24 18:38:16 -06003603 continue;
Ashwin Lelec1e61d62019-07-22 12:36:38 -07003604 }
3605 memberRemapper[glslangMembers][i] = i - memberDelta;
John Kessenich0e737842017-03-24 18:38:16 -06003606 }
John Kessenich6090df02016-06-30 21:18:02 -06003607 // modify just this child's view of the qualifier
3608 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3609 InheritQualifiers(memberQualifier, qualifier);
3610
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003611 // manually inherit location
John Kessenich6090df02016-06-30 21:18:02 -06003612 if (! memberQualifier.hasLocation() && qualifier.hasLocation())
John Kessenich7cdf3fc2017-06-04 13:22:39 -06003613 memberQualifier.layoutLocation = qualifier.layoutLocation;
John Kessenich6090df02016-06-30 21:18:02 -06003614
3615 // recurse
John Kessenichead86222018-03-28 18:01:20 -06003616 bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer &&
3617 i == (int)glslangMembers->size() - 1;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003618
3619 // Make forward pointers for any pointer members, and create a list of members to
3620 // convert to spirv types after creating the struct.
John Kessenich7015bd62019-08-01 03:28:08 -06003621 if (glslangMember.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003622 if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) {
3623 deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier));
3624 }
3625 spvMembers.push_back(
3626 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true));
3627 } else {
3628 spvMembers.push_back(
3629 convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false));
3630 }
John Kessenich6090df02016-06-30 21:18:02 -06003631 }
3632 }
3633
3634 // Make the SPIR-V type
3635 spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str());
John Kessenichf2b7f332016-09-01 17:05:23 -06003636 if (! HasNonLayoutQualifiers(type, qualifier))
John Kessenich6090df02016-06-30 21:18:02 -06003637 structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType;
3638
3639 // Decorate it
3640 decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType);
3641
John Kessenichd72f4882019-01-16 14:55:37 +07003642 for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003643 auto it = deferredForwardPointers[i];
3644 convertGlslangToSpvType(*it.first, explicitLayout, it.second, false);
3645 }
3646
John Kessenich6090df02016-06-30 21:18:02 -06003647 return spvType;
3648}
3649
3650void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
3651 const glslang::TTypeList* glslangMembers,
3652 glslang::TLayoutPacking explicitLayout,
3653 const glslang::TQualifier& qualifier,
3654 spv::Id spvType)
3655{
3656 // Name and decorate the non-hidden members
3657 int offset = -1;
3658 int locationOffset = 0; // for use within the members of this struct
3659 for (int i = 0; i < (int)glslangMembers->size(); i++) {
3660 glslang::TType& glslangMember = *(*glslangMembers)[i].type;
3661 int member = i;
John Kessenich0e737842017-03-24 18:38:16 -06003662 if (type.getBasicType() == glslang::EbtBlock) {
John Kessenich6090df02016-06-30 21:18:02 -06003663 member = memberRemapper[glslangMembers][i];
John Kessenich0e737842017-03-24 18:38:16 -06003664 if (filterMember(glslangMember))
3665 continue;
3666 }
John Kessenich6090df02016-06-30 21:18:02 -06003667
3668 // modify just this child's view of the qualifier
3669 glslang::TQualifier memberQualifier = glslangMember.getQualifier();
3670 InheritQualifiers(memberQualifier, qualifier);
3671
3672 // using -1 above to indicate a hidden member
John Kessenich5d610ee2018-03-07 18:05:55 -07003673 if (member < 0)
3674 continue;
3675
3676 builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str());
3677 builder.addMemberDecoration(spvType, member,
3678 TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix));
3679 builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember));
3680 // Add interpolation and auxiliary storage decorations only to
3681 // top-level members of Input and Output storage classes
3682 if (type.getQualifier().storage == glslang::EvqVaryingIn ||
3683 type.getQualifier().storage == glslang::EvqVaryingOut) {
3684 if (type.getBasicType() == glslang::EbtBlock ||
3685 glslangIntermediate->getSource() == glslang::EShSourceHlsl) {
3686 builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier));
3687 builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier));
John Kessenicha28f7a72019-08-06 07:00:58 -06003688#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07003689 addMeshNVDecoration(spvType, member, memberQualifier);
3690#endif
John Kessenich6090df02016-06-30 21:18:02 -06003691 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003692 }
3693 builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier));
John Kessenich6090df02016-06-30 21:18:02 -06003694
John Kessenichb9197c82019-08-11 07:41:45 -06003695#ifndef GLSLANG_WEB
John Kessenich5d610ee2018-03-07 18:05:55 -07003696 if (type.getBasicType() == glslang::EbtBlock &&
3697 qualifier.storage == glslang::EvqBuffer) {
3698 // Add memory decorations only to top-level members of shader storage block
3699 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05003700 TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich5d610ee2018-03-07 18:05:55 -07003701 for (unsigned int i = 0; i < memory.size(); ++i)
3702 builder.addMemberDecoration(spvType, member, memory[i]);
3703 }
John Kessenichb9197c82019-08-11 07:41:45 -06003704#endif
John Kessenich6090df02016-06-30 21:18:02 -06003705
John Kessenich5d610ee2018-03-07 18:05:55 -07003706 // Location assignment was already completed correctly by the front end,
3707 // just track whether a member needs to be decorated.
3708 // Ignore member locations if the container is an array, as that's
3709 // ill-specified and decisions have been made to not allow this.
3710 if (! type.isArray() && memberQualifier.hasLocation())
3711 builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation);
John Kessenich6090df02016-06-30 21:18:02 -06003712
John Kessenich5d610ee2018-03-07 18:05:55 -07003713 if (qualifier.hasLocation()) // track for upcoming inheritance
3714 locationOffset += glslangIntermediate->computeTypeLocationSize(
3715 glslangMember, glslangIntermediate->getStage());
John Kessenich2f47bc92016-06-30 21:47:35 -06003716
John Kessenich5d610ee2018-03-07 18:05:55 -07003717 // component, XFB, others
3718 if (glslangMember.getQualifier().hasComponent())
3719 builder.addMemberDecoration(spvType, member, spv::DecorationComponent,
3720 glslangMember.getQualifier().layoutComponent);
3721 if (glslangMember.getQualifier().hasXfbOffset())
3722 builder.addMemberDecoration(spvType, member, spv::DecorationOffset,
3723 glslangMember.getQualifier().layoutXfbOffset);
3724 else if (explicitLayout != glslang::ElpNone) {
3725 // figure out what to do with offset, which is accumulating
3726 int nextOffset;
3727 updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix);
3728 if (offset >= 0)
3729 builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset);
3730 offset = nextOffset;
3731 }
John Kessenich6090df02016-06-30 21:18:02 -06003732
John Kessenich5d610ee2018-03-07 18:05:55 -07003733 if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone)
3734 builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride,
3735 getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix));
John Kessenich6090df02016-06-30 21:18:02 -06003736
John Kessenich5d610ee2018-03-07 18:05:55 -07003737 // built-in variable decorations
3738 spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true);
3739 if (builtIn != spv::BuiltInMax)
3740 builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn);
chaoc771d89f2017-01-13 01:10:53 -08003741
John Kessenichb9197c82019-08-11 07:41:45 -06003742#ifndef GLSLANG_WEB
John Kessenich5611c6d2018-04-05 11:25:02 -06003743 // nonuniform
3744 builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier()));
3745
John Kessenichead86222018-03-28 18:01:20 -06003746 if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) {
3747 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
3748 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
3749 memberQualifier.semanticName);
3750 }
3751
John Kessenich5d610ee2018-03-07 18:05:55 -07003752 if (builtIn == spv::BuiltInLayer) {
3753 // SPV_NV_viewport_array2 extension
3754 if (glslangMember.getQualifier().layoutViewportRelative){
3755 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV);
3756 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
3757 builder.addExtension(spv::E_SPV_NV_viewport_array2);
chaoc771d89f2017-01-13 01:10:53 -08003758 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003759 if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){
3760 builder.addMemberDecoration(spvType, member,
3761 (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
3762 glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset);
3763 builder.addCapability(spv::CapabilityShaderStereoViewNV);
3764 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
chaocdf3956c2017-02-14 14:52:34 -08003765 }
John Kessenich5d610ee2018-03-07 18:05:55 -07003766 }
3767 if (glslangMember.getQualifier().layoutPassthrough) {
3768 builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV);
3769 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
3770 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
3771 }
chaoc771d89f2017-01-13 01:10:53 -08003772#endif
John Kessenich6090df02016-06-30 21:18:02 -06003773 }
3774
3775 // Decorate the structure
John Kessenich5d610ee2018-03-07 18:05:55 -07003776 builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix));
3777 builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer()));
John Kessenich6090df02016-06-30 21:18:02 -06003778}
3779
John Kessenich6c292d32016-02-15 20:58:50 -07003780// Turn the expression forming the array size into an id.
3781// This is not quite trivial, because of specialization constants.
3782// Sometimes, a raw constant is turned into an Id, and sometimes
3783// a specialization constant expression is.
3784spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim)
3785{
3786 // First, see if this is sized with a node, meaning a specialization constant:
3787 glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
3788 if (specNode != nullptr) {
3789 builder.clearAccessChain();
3790 specNode->traverse(this);
3791 return accessChainLoad(specNode->getAsTyped()->getType());
3792 }
qining25262b32016-05-06 17:25:16 -04003793
John Kessenich6c292d32016-02-15 20:58:50 -07003794 // Otherwise, need a compile-time (front end) size, get it:
3795 int size = arraySizes.getDimSize(dim);
3796 assert(size > 0);
3797 return builder.makeUintConstant(size);
3798}
3799
John Kessenich103bef92016-02-08 21:38:15 -07003800// Wrap the builder's accessChainLoad to:
3801// - localize handling of RelaxedPrecision
3802// - use the SPIR-V inferred type instead of another conversion of the glslang type
3803// (avoids unnecessary work and possible type punning for structures)
3804// - do conversion of concrete to abstract type
John Kessenich32cfd492016-02-02 12:37:46 -07003805spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
3806{
John Kessenich103bef92016-02-08 21:38:15 -07003807 spv::Id nominalTypeId = builder.accessChainGetInferredType();
Jeff Bolz36831c92018-09-05 10:11:41 -05003808
3809 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3810 coherentFlags |= TranslateCoherent(type);
3811
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003812 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06003813 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003814
John Kessenich5611c6d2018-04-05 11:25:02 -06003815 spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type),
Jeff Bolz36831c92018-09-05 10:11:41 -05003816 TranslateNonUniformDecoration(type.getQualifier()),
3817 nominalTypeId,
3818 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003819 TranslateMemoryScope(coherentFlags),
3820 alignment);
John Kessenich103bef92016-02-08 21:38:15 -07003821
3822 // Need to convert to abstract types when necessary
Rex Xu27253232016-02-23 17:51:09 +08003823 if (type.getBasicType() == glslang::EbtBool) {
3824 if (builder.isScalarType(nominalTypeId)) {
3825 // Conversion for bool
3826 spv::Id boolType = builder.makeBoolType();
3827 if (nominalTypeId != boolType)
3828 loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0));
3829 } else if (builder.isVectorType(nominalTypeId)) {
3830 // Conversion for bvec
3831 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3832 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
3833 if (nominalTypeId != bvecType)
3834 loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize));
3835 }
3836 }
John Kessenich103bef92016-02-08 21:38:15 -07003837
3838 return loadedId;
John Kessenich32cfd492016-02-02 12:37:46 -07003839}
3840
Rex Xu27253232016-02-23 17:51:09 +08003841// Wrap the builder's accessChainStore to:
3842// - do conversion of concrete to abstract type
John Kessenich4bf71552016-09-02 11:20:21 -06003843//
3844// Implicitly uses the existing builder.accessChain as the storage target.
Rex Xu27253232016-02-23 17:51:09 +08003845void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
3846{
3847 // Need to convert to abstract types when necessary
3848 if (type.getBasicType() == glslang::EbtBool) {
3849 spv::Id nominalTypeId = builder.accessChainGetInferredType();
3850
3851 if (builder.isScalarType(nominalTypeId)) {
3852 // Conversion for bool
3853 spv::Id boolType = builder.makeBoolType();
John Kessenichb6cabc42017-05-19 23:29:50 -06003854 if (nominalTypeId != boolType) {
3855 // keep these outside arguments, for determinant order-of-evaluation
3856 spv::Id one = builder.makeUintConstant(1);
3857 spv::Id zero = builder.makeUintConstant(0);
3858 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
3859 } else if (builder.getTypeId(rvalue) != boolType)
John Kessenich80f92a12017-05-19 23:00:13 -06003860 rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0));
Rex Xu27253232016-02-23 17:51:09 +08003861 } else if (builder.isVectorType(nominalTypeId)) {
3862 // Conversion for bvec
3863 int vecSize = builder.getNumTypeComponents(nominalTypeId);
3864 spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize);
John Kessenichb6cabc42017-05-19 23:29:50 -06003865 if (nominalTypeId != bvecType) {
3866 // keep these outside arguments, for determinant order-of-evaluation
John Kessenich7b8c3862017-05-19 23:44:51 -06003867 spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize);
3868 spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize);
3869 rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero);
John Kessenichb6cabc42017-05-19 23:29:50 -06003870 } else if (builder.getTypeId(rvalue) != bvecType)
John Kessenich80f92a12017-05-19 23:00:13 -06003871 rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue,
3872 makeSmearedConstant(builder.makeUintConstant(0), vecSize));
Rex Xu27253232016-02-23 17:51:09 +08003873 }
3874 }
3875
Jeff Bolz36831c92018-09-05 10:11:41 -05003876 spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags;
3877 coherentFlags |= TranslateCoherent(type);
3878
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003879 unsigned int alignment = builder.getAccessChain().alignment;
Jeff Bolz7895e472019-03-06 13:34:10 -06003880 alignment |= type.getBufferReferenceAlignment();
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003881
Jeff Bolz36831c92018-09-05 10:11:41 -05003882 builder.accessChainStore(rvalue,
3883 spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask),
Jeff Bolz9f2aec42019-01-06 17:58:04 -06003884 TranslateMemoryScope(coherentFlags), alignment);
Rex Xu27253232016-02-23 17:51:09 +08003885}
3886
John Kessenich4bf71552016-09-02 11:20:21 -06003887// For storing when types match at the glslang level, but not might match at the
3888// SPIR-V level.
3889//
3890// This especially happens when a single glslang type expands to multiple
John Kesseniched33e052016-10-06 12:59:51 -06003891// SPIR-V types, like a struct that is used in a member-undecorated way as well
John Kessenich4bf71552016-09-02 11:20:21 -06003892// as in a member-decorated way.
3893//
3894// NOTE: This function can handle any store request; if it's not special it
3895// simplifies to a simple OpStore.
3896//
3897// Implicitly uses the existing builder.accessChain as the storage target.
3898void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
3899{
John Kessenichb3e24e42016-09-11 12:33:43 -06003900 // we only do the complex path here if it's an aggregate
3901 if (! type.isStruct() && ! type.isArray()) {
John Kessenich4bf71552016-09-02 11:20:21 -06003902 accessChainStore(type, rValue);
3903 return;
3904 }
3905
John Kessenichb3e24e42016-09-11 12:33:43 -06003906 // and, it has to be a case of type aliasing
John Kessenich4bf71552016-09-02 11:20:21 -06003907 spv::Id rType = builder.getTypeId(rValue);
3908 spv::Id lValue = builder.accessChainGetLValue();
3909 spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue));
3910 if (lType == rType) {
3911 accessChainStore(type, rValue);
3912 return;
3913 }
3914
John Kessenichb3e24e42016-09-11 12:33:43 -06003915 // Recursively (as needed) copy an aggregate type to a different aggregate type,
John Kessenich4bf71552016-09-02 11:20:21 -06003916 // where the two types were the same type in GLSL. This requires member
3917 // by member copy, recursively.
3918
John Kessenichfbb6bdf2019-01-15 21:48:27 +07003919 // SPIR-V 1.4 added an instruction to do help do this.
3920 if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) {
3921 // However, bool in uniform space is changed to int, so
3922 // OpCopyLogical does not work for that.
3923 // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules.
3924 bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0);
3925 bool lBool = builder.containsType(lType, spv::OpTypeBool, 0);
3926 if (lBool == rBool) {
3927 spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue);
3928 accessChainStore(type, logicalCopy);
3929 return;
3930 }
3931 }
3932
John Kessenichb3e24e42016-09-11 12:33:43 -06003933 // If an array, copy element by element.
3934 if (type.isArray()) {
3935 glslang::TType glslangElementType(type, 0);
3936 spv::Id elementRType = builder.getContainedTypeId(rType);
3937 for (int index = 0; index < type.getOuterArraySize(); ++index) {
3938 // get the source member
3939 spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index);
John Kessenich4bf71552016-09-02 11:20:21 -06003940
John Kessenichb3e24e42016-09-11 12:33:43 -06003941 // set up the target storage
3942 builder.clearAccessChain();
3943 builder.setAccessChainLValue(lValue);
Jeff Bolz7895e472019-03-06 13:34:10 -06003944 builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), type.getBufferReferenceAlignment());
John Kessenich4bf71552016-09-02 11:20:21 -06003945
John Kessenichb3e24e42016-09-11 12:33:43 -06003946 // store the member
3947 multiTypeStore(glslangElementType, elementRValue);
3948 }
3949 } else {
3950 assert(type.isStruct());
John Kessenich4bf71552016-09-02 11:20:21 -06003951
John Kessenichb3e24e42016-09-11 12:33:43 -06003952 // loop over structure members
3953 const glslang::TTypeList& members = *type.getStruct();
3954 for (int m = 0; m < (int)members.size(); ++m) {
3955 const glslang::TType& glslangMemberType = *members[m].type;
3956
3957 // get the source member
3958 spv::Id memberRType = builder.getContainedTypeId(rType, m);
3959 spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m);
3960
3961 // set up the target storage
3962 builder.clearAccessChain();
3963 builder.setAccessChainLValue(lValue);
Jeff Bolz7895e472019-03-06 13:34:10 -06003964 builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), type.getBufferReferenceAlignment());
John Kessenichb3e24e42016-09-11 12:33:43 -06003965
3966 // store the member
3967 multiTypeStore(glslangMemberType, memberRValue);
3968 }
John Kessenich4bf71552016-09-02 11:20:21 -06003969 }
3970}
3971
John Kessenichf85e8062015-12-19 13:57:10 -07003972// Decide whether or not this type should be
3973// decorated with offsets and strides, and if so
3974// whether std140 or std430 rules should be applied.
3975glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
John Kessenich31ed4832015-09-09 17:51:38 -06003976{
John Kessenichf85e8062015-12-19 13:57:10 -07003977 // has to be a block
3978 if (type.getBasicType() != glslang::EbtBlock)
3979 return glslang::ElpNone;
3980
Chao Chen3c366992018-09-19 11:41:59 -07003981 // has to be a uniform or buffer block or task in/out blocks
John Kessenichf85e8062015-12-19 13:57:10 -07003982 if (type.getQualifier().storage != glslang::EvqUniform &&
Chao Chen3c366992018-09-19 11:41:59 -07003983 type.getQualifier().storage != glslang::EvqBuffer &&
3984 !type.getQualifier().isTaskMemory())
John Kessenichf85e8062015-12-19 13:57:10 -07003985 return glslang::ElpNone;
3986
3987 // return the layout to use
3988 switch (type.getQualifier().layoutPacking) {
3989 case glslang::ElpStd140:
3990 case glslang::ElpStd430:
Jeff Bolz7da39ed2018-11-14 09:30:53 -06003991 case glslang::ElpScalar:
John Kessenichf85e8062015-12-19 13:57:10 -07003992 return type.getQualifier().layoutPacking;
3993 default:
3994 return glslang::ElpNone;
3995 }
John Kessenich31ed4832015-09-09 17:51:38 -06003996}
3997
Jason Ekstrand54aedf12015-09-05 09:50:58 -07003998// Given an array type, returns the integer stride required for that array
John Kessenich3ac051e2015-12-20 11:29:16 -07003999int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004000{
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004001 int size;
John Kessenich49987892015-12-29 17:11:44 -07004002 int stride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004003 glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kesseniche721f492015-12-06 19:17:49 -07004004
4005 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004006}
4007
John Kessenich49987892015-12-29 17:11:44 -07004008// 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 -07004009// when used as a member of an interface block
John Kessenich3ac051e2015-12-20 11:29:16 -07004010int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004011{
John Kessenich49987892015-12-29 17:11:44 -07004012 glslang::TType elementType;
4013 elementType.shallowCopy(matrixType);
4014 elementType.clearArraySizes();
4015
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004016 int size;
John Kessenich49987892015-12-29 17:11:44 -07004017 int stride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004018 glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kessenich49987892015-12-29 17:11:44 -07004019
4020 return stride;
Jason Ekstrand54aedf12015-09-05 09:50:58 -07004021}
4022
John Kessenich5e4b1242015-08-06 22:53:06 -06004023// Given a member type of a struct, realign the current offset for it, and compute
4024// the next (not yet aligned) offset for the next member, which will get aligned
4025// on the next call.
4026// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting
4027// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call.
4028// -1 means a non-forced member offset (no decoration needed).
John Kessenich735d7e52017-07-13 11:39:16 -06004029void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset,
John Kessenich3ac051e2015-12-20 11:29:16 -07004030 glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
John Kessenich5e4b1242015-08-06 22:53:06 -06004031{
4032 // this will get a positive value when deemed necessary
4033 nextOffset = -1;
4034
John Kessenich5e4b1242015-08-06 22:53:06 -06004035 // override anything in currentOffset with user-set offset
4036 if (memberType.getQualifier().hasOffset())
4037 currentOffset = memberType.getQualifier().layoutOffset;
4038
4039 // It could be that current linker usage in glslang updated all the layoutOffset,
4040 // in which case the following code does not matter. But, that's not quite right
4041 // once cross-compilation unit GLSL validation is done, as the original user
4042 // settings are needed in layoutOffset, and then the following will come into play.
4043
John Kessenichf85e8062015-12-19 13:57:10 -07004044 if (explicitLayout == glslang::ElpNone) {
John Kessenich5e4b1242015-08-06 22:53:06 -06004045 if (! memberType.getQualifier().hasOffset())
4046 currentOffset = -1;
4047
4048 return;
4049 }
4050
John Kessenichf85e8062015-12-19 13:57:10 -07004051 // Getting this far means we need explicit offsets
John Kessenich5e4b1242015-08-06 22:53:06 -06004052 if (currentOffset < 0)
4053 currentOffset = 0;
qining25262b32016-05-06 17:25:16 -04004054
John Kessenich5e4b1242015-08-06 22:53:06 -06004055 // Now, currentOffset is valid (either 0, or from a previous nextOffset),
4056 // but possibly not yet correctly aligned.
4057
4058 int memberSize;
John Kessenich49987892015-12-29 17:11:44 -07004059 int dummyStride;
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004060 int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor);
John Kessenich4f1403e2017-04-05 17:38:20 -06004061
4062 // Adjust alignment for HLSL rules
John Kessenich735d7e52017-07-13 11:39:16 -06004063 // TODO: make this consistent in early phases of code:
4064 // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
4065 // Until reflection is brought in sync with these adjustments, don't apply to $Global,
4066 // which is the most likely to rely on reflection, and least likely to rely implicit layouts
John Kesseniche7df8e02018-08-22 17:12:46 -06004067 if (glslangIntermediate->usingHlslOffsets() &&
John Kessenich735d7e52017-07-13 11:39:16 -06004068 ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
John Kessenich4f1403e2017-04-05 17:38:20 -06004069 int dummySize;
4070 int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
4071 if (componentAlignment <= 4)
4072 memberAlignment = componentAlignment;
4073 }
4074
4075 // Bump up to member alignment
John Kessenich5e4b1242015-08-06 22:53:06 -06004076 glslang::RoundToPow2(currentOffset, memberAlignment);
John Kessenich4f1403e2017-04-05 17:38:20 -06004077
4078 // Bump up to vec4 if there is a bad straddle
Jeff Bolz7da39ed2018-11-14 09:30:53 -06004079 if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset))
John Kessenich4f1403e2017-04-05 17:38:20 -06004080 glslang::RoundToPow2(currentOffset, 16);
4081
John Kessenich5e4b1242015-08-06 22:53:06 -06004082 nextOffset = currentOffset + memberSize;
4083}
4084
David Netoa901ffe2016-06-08 14:11:40 +01004085void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
John Kessenichebb50532016-05-16 19:22:05 -06004086{
David Netoa901ffe2016-06-08 14:11:40 +01004087 const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn;
4088 switch (glslangBuiltIn)
4089 {
John Kessenicha28f7a72019-08-06 07:00:58 -06004090 case glslang::EbvPointSize:
4091#ifndef GLSLANG_WEB
David Netoa901ffe2016-06-08 14:11:40 +01004092 case glslang::EbvClipDistance:
4093 case glslang::EbvCullDistance:
chaoc771d89f2017-01-13 01:10:53 -08004094 case glslang::EbvViewportMaskNV:
4095 case glslang::EbvSecondaryPositionNV:
4096 case glslang::EbvSecondaryViewportMaskNV:
chaocdf3956c2017-02-14 14:52:34 -08004097 case glslang::EbvPositionPerViewNV:
4098 case glslang::EbvViewportMaskPerViewNV:
Chao Chen3c366992018-09-19 11:41:59 -07004099 case glslang::EbvTaskCountNV:
4100 case glslang::EbvPrimitiveCountNV:
4101 case glslang::EbvPrimitiveIndicesNV:
4102 case glslang::EbvClipDistancePerViewNV:
4103 case glslang::EbvCullDistancePerViewNV:
4104 case glslang::EbvLayerPerViewNV:
4105 case glslang::EbvMeshViewCountNV:
4106 case glslang::EbvMeshViewIndicesNV:
chaoc771d89f2017-01-13 01:10:53 -08004107#endif
David Netoa901ffe2016-06-08 14:11:40 +01004108 // Generate the associated capability. Delegate to TranslateBuiltInDecoration.
4109 // Alternately, we could just call this for any glslang built-in, since the
4110 // capability already guards against duplicates.
4111 TranslateBuiltInDecoration(glslangBuiltIn, false);
4112 break;
4113 default:
4114 // Capabilities were already generated when the struct was declared.
4115 break;
4116 }
John Kessenichebb50532016-05-16 19:22:05 -06004117}
4118
John Kessenich6fccb3c2016-09-19 16:01:41 -06004119bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
John Kessenich140f3df2015-06-26 16:58:36 -06004120{
John Kessenicheee9d532016-09-19 18:09:30 -06004121 return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0;
John Kessenich140f3df2015-06-26 16:58:36 -06004122}
4123
John Kessenichd41993d2017-09-10 15:21:05 -06004124// Does parameter need a place to keep writes, separate from the original?
John Kessenich6a14f782017-12-04 02:48:10 -07004125// Assumes called after originalParam(), which filters out block/buffer/opaque-based
4126// qualifiers such that we should have only in/out/inout/constreadonly here.
John Kessenichd3ed90b2018-05-04 11:43:03 -06004127bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
John Kessenichd41993d2017-09-10 15:21:05 -06004128{
John Kessenich6a14f782017-12-04 02:48:10 -07004129 assert(qualifier == glslang::EvqIn ||
4130 qualifier == glslang::EvqOut ||
4131 qualifier == glslang::EvqInOut ||
4132 qualifier == glslang::EvqConstReadOnly);
John Kessenichd41993d2017-09-10 15:21:05 -06004133 return qualifier != glslang::EvqConstReadOnly;
4134}
4135
4136// Is parameter pass-by-original?
4137bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
4138 bool implicitThisParam)
4139{
4140 if (implicitThisParam) // implicit this
4141 return true;
4142 if (glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich6a14f782017-12-04 02:48:10 -07004143 return paramType.getBasicType() == glslang::EbtBlock;
John Kessenichd41993d2017-09-10 15:21:05 -06004144 return paramType.containsOpaque() || // sampler, etc.
4145 (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO
4146}
4147
John Kessenich140f3df2015-06-26 16:58:36 -06004148// Make all the functions, skeletally, without actually visiting their bodies.
4149void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
4150{
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004151 const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, bool useVulkanMemoryModel) {
John Kessenichfad62972017-07-18 02:35:46 -06004152 spv::Decoration paramPrecision = TranslatePrecisionDecoration(type);
4153 if (paramPrecision != spv::NoPrecision)
4154 decorations.push_back(paramPrecision);
Jeff Bolz36831c92018-09-05 10:11:41 -05004155 TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel);
John Kessenich7015bd62019-08-01 03:28:08 -06004156 if (type.isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004157 // Original and non-writable params pass the pointer directly and
4158 // use restrict/aliased, others are stored to a pointer in Function
4159 // memory and use RestrictPointer/AliasedPointer.
4160 if (originalParam(type.getQualifier().storage, type, false) ||
4161 !writableParam(type.getQualifier().storage)) {
4162 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased);
4163 } else {
4164 decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
4165 }
4166 }
John Kessenichfad62972017-07-18 02:35:46 -06004167 };
4168
John Kessenich140f3df2015-06-26 16:58:36 -06004169 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4170 glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate();
John Kessenich6fccb3c2016-09-19 16:01:41 -06004171 if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction))
John Kessenich140f3df2015-06-26 16:58:36 -06004172 continue;
4173
4174 // We're on a user function. Set up the basic interface for the function now,
John Kessenich4bf71552016-09-02 11:20:21 -06004175 // so that it's available to call. Translating the body will happen later.
John Kessenich140f3df2015-06-26 16:58:36 -06004176 //
qining25262b32016-05-06 17:25:16 -04004177 // Typically (except for a "const in" parameter), an address will be passed to the
John Kessenich140f3df2015-06-26 16:58:36 -06004178 // function. What it is an address of varies:
4179 //
John Kessenich4bf71552016-09-02 11:20:21 -06004180 // - "in" parameters not marked as "const" can be written to without modifying the calling
4181 // argument so that write needs to be to a copy, hence the address of a copy works.
John Kessenich140f3df2015-06-26 16:58:36 -06004182 //
4183 // - "const in" parameters can just be the r-value, as no writes need occur.
4184 //
John Kessenich4bf71552016-09-02 11:20:21 -06004185 // - "out" and "inout" arguments can't be done as pointers to the calling argument, because
4186 // 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 -06004187
4188 std::vector<spv::Id> paramTypes;
John Kessenichfad62972017-07-18 02:35:46 -06004189 std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter
John Kessenich140f3df2015-06-26 16:58:36 -06004190 glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
4191
John Kessenich155d3512019-08-08 23:29:20 -06004192#ifdef ENABLE_HLSL
John Kessenichfad62972017-07-18 02:35:46 -06004193 bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() ==
4194 glslangIntermediate->implicitThisName;
John Kessenich155d3512019-08-08 23:29:20 -06004195#else
4196 bool implicitThis = false;
4197#endif
John Kessenich37789792017-03-21 23:56:40 -06004198
John Kessenichfad62972017-07-18 02:35:46 -06004199 paramDecorations.resize(parameters.size());
John Kessenich140f3df2015-06-26 16:58:36 -06004200 for (int p = 0; p < (int)parameters.size(); ++p) {
4201 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
4202 spv::Id typeId = convertGlslangToSpvType(paramType);
John Kessenichd41993d2017-09-10 15:21:05 -06004203 if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0))
John Kessenicha5c5fb62017-05-05 05:09:58 -06004204 typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
John Kessenichd41993d2017-09-10 15:21:05 -06004205 else if (writableParam(paramType.getQualifier().storage))
John Kessenich140f3df2015-06-26 16:58:36 -06004206 typeId = builder.makePointer(spv::StorageClassFunction, typeId);
4207 else
John Kessenich4bf71552016-09-02 11:20:21 -06004208 rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId());
Jeff Bolz36831c92018-09-05 10:11:41 -05004209 getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel());
John Kessenich140f3df2015-06-26 16:58:36 -06004210 paramTypes.push_back(typeId);
4211 }
4212
4213 spv::Block* functionBlock;
John Kessenich32cfd492016-02-02 12:37:46 -07004214 spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
4215 convertGlslangToSpvType(glslFunction->getType()),
John Kessenichfad62972017-07-18 02:35:46 -06004216 glslFunction->getName().c_str(), paramTypes,
4217 paramDecorations, &functionBlock);
John Kessenich37789792017-03-21 23:56:40 -06004218 if (implicitThis)
4219 function->setImplicitThis();
John Kessenich140f3df2015-06-26 16:58:36 -06004220
4221 // Track function to emit/call later
4222 functionMap[glslFunction->getName().c_str()] = function;
4223
4224 // Set the parameter id's
4225 for (int p = 0; p < (int)parameters.size(); ++p) {
4226 symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p);
4227 // give a name too
4228 builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str());
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004229
4230 const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
John Kessenichb9197c82019-08-11 07:41:45 -06004231 if (paramType.contains8BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004232 builder.addCapability(spv::CapabilityInt8);
John Kessenichb9197c82019-08-11 07:41:45 -06004233 if (paramType.contains16BitInt())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004234 builder.addCapability(spv::CapabilityInt16);
John Kessenichb9197c82019-08-11 07:41:45 -06004235 if (paramType.contains16BitFloat())
Jeff Bolz2b2316d2019-02-17 22:49:28 -06004236 builder.addCapability(spv::CapabilityFloat16);
John Kessenich140f3df2015-06-26 16:58:36 -06004237 }
4238 }
4239}
4240
4241// Process all the initializers, while skipping the functions and link objects
4242void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
4243{
4244 builder.setBuildPoint(shaderEntry->getLastBlock());
4245 for (int i = 0; i < (int)initializers.size(); ++i) {
4246 glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate();
4247 if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) {
4248
4249 // We're on a top-level node that's not a function. Treat as an initializer, whose
John Kessenich6fccb3c2016-09-19 16:01:41 -06004250 // code goes into the beginning of the entry point.
John Kessenich140f3df2015-06-26 16:58:36 -06004251 initializer->traverse(this);
4252 }
4253 }
4254}
4255
4256// Process all the functions, while skipping initializers.
4257void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
4258{
4259 for (int f = 0; f < (int)glslFunctions.size(); ++f) {
4260 glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate();
John Kessenich6a60c2f2016-12-08 21:01:59 -07004261 if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects))
John Kessenich140f3df2015-06-26 16:58:36 -06004262 node->traverse(this);
4263 }
4264}
4265
4266void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
4267{
qining25262b32016-05-06 17:25:16 -04004268 // SPIR-V functions should already be in the functionMap from the prepass
John Kessenich140f3df2015-06-26 16:58:36 -06004269 // that called makeFunctions().
John Kesseniched33e052016-10-06 12:59:51 -06004270 currentFunction = functionMap[node->getName().c_str()];
4271 spv::Block* functionBlock = currentFunction->getEntryBlock();
John Kessenich140f3df2015-06-26 16:58:36 -06004272 builder.setBuildPoint(functionBlock);
4273}
4274
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004275void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06004276{
Rex Xufc618912015-09-09 16:42:49 +08004277 const glslang::TIntermSequence& glslangArguments = node.getSequence();
Rex Xu48edadf2015-12-31 16:11:41 +08004278
4279 glslang::TSampler sampler = {};
4280 bool cubeCompare = false;
John Kessenicha28f7a72019-08-06 07:00:58 -06004281#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08004282 bool f16ShadowCompare = false;
4283#endif
Rex Xu5eafa472016-02-19 22:24:03 +08004284 if (node.isTexture() || node.isImage()) {
Rex Xu48edadf2015-12-31 16:11:41 +08004285 sampler = glslangArguments[0]->getAsTyped()->getType().getSampler();
4286 cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow;
John Kessenicha28f7a72019-08-06 07:00:58 -06004287#ifndef GLSLANG_WEB
Rex Xu1e5d7b02016-11-29 17:36:31 +08004288 f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16;
4289#endif
Rex Xu48edadf2015-12-31 16:11:41 +08004290 }
4291
John Kessenich140f3df2015-06-26 16:58:36 -06004292 for (int i = 0; i < (int)glslangArguments.size(); ++i) {
4293 builder.clearAccessChain();
4294 glslangArguments[i]->traverse(this);
Rex Xufc618912015-09-09 16:42:49 +08004295
John Kessenicha28f7a72019-08-06 07:00:58 -06004296#ifndef GLSLANG_WEB
Rex Xufc618912015-09-09 16:42:49 +08004297 // Special case l-value operands
4298 bool lvalue = false;
4299 switch (node.getOp()) {
4300 case glslang::EOpImageAtomicAdd:
4301 case glslang::EOpImageAtomicMin:
4302 case glslang::EOpImageAtomicMax:
4303 case glslang::EOpImageAtomicAnd:
4304 case glslang::EOpImageAtomicOr:
4305 case glslang::EOpImageAtomicXor:
4306 case glslang::EOpImageAtomicExchange:
4307 case glslang::EOpImageAtomicCompSwap:
Jeff Bolz36831c92018-09-05 10:11:41 -05004308 case glslang::EOpImageAtomicLoad:
4309 case glslang::EOpImageAtomicStore:
Rex Xufc618912015-09-09 16:42:49 +08004310 if (i == 0)
4311 lvalue = true;
4312 break;
Rex Xu5eafa472016-02-19 22:24:03 +08004313 case glslang::EOpSparseImageLoad:
4314 if ((sampler.ms && i == 3) || (! sampler.ms && i == 2))
4315 lvalue = true;
4316 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004317 case glslang::EOpSparseTexture:
4318 if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2))
4319 lvalue = true;
4320 break;
4321 case glslang::EOpSparseTextureClamp:
4322 if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3))
4323 lvalue = true;
4324 break;
4325 case glslang::EOpSparseTextureLod:
4326 case glslang::EOpSparseTextureOffset:
4327 if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3))
4328 lvalue = true;
4329 break;
Rex Xu48edadf2015-12-31 16:11:41 +08004330 case glslang::EOpSparseTextureFetch:
4331 if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2))
4332 lvalue = true;
4333 break;
4334 case glslang::EOpSparseTextureFetchOffset:
4335 if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3))
4336 lvalue = true;
4337 break;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004338 case glslang::EOpSparseTextureLodOffset:
4339 case glslang::EOpSparseTextureGrad:
4340 case glslang::EOpSparseTextureOffsetClamp:
4341 if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4))
4342 lvalue = true;
4343 break;
4344 case glslang::EOpSparseTextureGradOffset:
4345 case glslang::EOpSparseTextureGradClamp:
4346 if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5))
4347 lvalue = true;
4348 break;
4349 case glslang::EOpSparseTextureGradOffsetClamp:
4350 if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6))
4351 lvalue = true;
4352 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004353 case glslang::EOpSparseTextureGather:
Rex Xu48edadf2015-12-31 16:11:41 +08004354 if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2))
4355 lvalue = true;
4356 break;
4357 case glslang::EOpSparseTextureGatherOffset:
4358 case glslang::EOpSparseTextureGatherOffsets:
4359 if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3))
4360 lvalue = true;
4361 break;
Rex Xu225e0fc2016-11-17 17:47:59 +08004362 case glslang::EOpSparseTextureGatherLod:
4363 if (i == 3)
4364 lvalue = true;
4365 break;
4366 case glslang::EOpSparseTextureGatherLodOffset:
4367 case glslang::EOpSparseTextureGatherLodOffsets:
4368 if (i == 4)
4369 lvalue = true;
4370 break;
Rex Xu129799a2017-07-05 17:23:28 +08004371 case glslang::EOpSparseImageLoadLod:
4372 if (i == 3)
4373 lvalue = true;
4374 break;
Chao Chen3a137962018-09-19 11:41:27 -07004375 case glslang::EOpImageSampleFootprintNV:
4376 if (i == 4)
4377 lvalue = true;
4378 break;
4379 case glslang::EOpImageSampleFootprintClampNV:
4380 case glslang::EOpImageSampleFootprintLodNV:
4381 if (i == 5)
4382 lvalue = true;
4383 break;
4384 case glslang::EOpImageSampleFootprintGradNV:
4385 if (i == 6)
4386 lvalue = true;
4387 break;
4388 case glslang::EOpImageSampleFootprintGradClampNV:
4389 if (i == 7)
4390 lvalue = true;
4391 break;
Rex Xufc618912015-09-09 16:42:49 +08004392 default:
4393 break;
4394 }
4395
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004396 if (lvalue) {
Rex Xufc618912015-09-09 16:42:49 +08004397 arguments.push_back(builder.accessChainGetLValue());
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004398 lvalueCoherentFlags = builder.getAccessChain().coherentFlags;
4399 lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType());
4400 } else
John Kessenicha28f7a72019-08-06 07:00:58 -06004401#endif
John Kessenich32cfd492016-02-02 12:37:46 -07004402 arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06004403 }
4404}
4405
John Kessenichfc51d282015-08-19 13:34:18 -06004406void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
John Kessenich140f3df2015-06-26 16:58:36 -06004407{
John Kessenichfc51d282015-08-19 13:34:18 -06004408 builder.clearAccessChain();
4409 node.getOperand()->traverse(this);
John Kessenich32cfd492016-02-02 12:37:46 -07004410 arguments.push_back(accessChainLoad(node.getOperand()->getType()));
John Kessenichfc51d282015-08-19 13:34:18 -06004411}
John Kessenich140f3df2015-06-26 16:58:36 -06004412
John Kessenichfc51d282015-08-19 13:34:18 -06004413spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
4414{
John Kesseniche485c7a2017-05-31 18:50:53 -06004415 if (! node->isImage() && ! node->isTexture())
John Kessenichfc51d282015-08-19 13:34:18 -06004416 return spv::NoResult;
John Kesseniche485c7a2017-05-31 18:50:53 -06004417
greg-lunarg5d43c4a2018-12-07 17:36:33 -07004418 builder.setLine(node->getLoc().line, node->getLoc().getFilename());
John Kesseniche485c7a2017-05-31 18:50:53 -06004419
John Kessenichfc51d282015-08-19 13:34:18 -06004420 // Process a GLSL texturing op (will be SPV image)
Jeff Bolz36831c92018-09-05 10:11:41 -05004421
John Kessenichf43c7392019-03-31 10:51:57 -06004422 const glslang::TType &imageType = node->getAsAggregate()
4423 ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType()
4424 : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType();
Jeff Bolz36831c92018-09-05 10:11:41 -05004425 const glslang::TSampler sampler = imageType.getSampler();
John Kessenicha28f7a72019-08-06 07:00:58 -06004426#ifdef GLSLANG_WEB
4427 const bool f16ShadowCompare = false;
4428#else
Rex Xu1e5d7b02016-11-29 17:36:31 +08004429 bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate())
John Kessenichf43c7392019-03-31 10:51:57 -06004430 ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16
4431 : false;
Rex Xu1e5d7b02016-11-29 17:36:31 +08004432#endif
4433
John Kessenichf43c7392019-03-31 10:51:57 -06004434 const auto signExtensionMask = [&]() {
4435 if (builder.getSpvVersion() >= spv::Spv_1_4) {
4436 if (sampler.type == glslang::EbtUint)
4437 return spv::ImageOperandsZeroExtendMask;
4438 else if (sampler.type == glslang::EbtInt)
4439 return spv::ImageOperandsSignExtendMask;
4440 }
4441 return spv::ImageOperandsMaskNone;
4442 };
4443
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004444 spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags;
4445
John Kessenichfc51d282015-08-19 13:34:18 -06004446 std::vector<spv::Id> arguments;
4447 if (node->getAsAggregate())
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004448 translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06004449 else
4450 translateArguments(*node->getAsUnaryNode(), arguments);
John Kessenichf6640762016-08-01 19:44:00 -06004451 spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision());
John Kessenichfc51d282015-08-19 13:34:18 -06004452
4453 spv::Builder::TextureParameters params = { };
4454 params.sampler = arguments[0];
4455
Rex Xu04db3f52015-09-16 11:44:02 +08004456 glslang::TCrackedTextureOp cracked;
4457 node->crackTexture(sampler, cracked);
4458
amhagan05506bb2017-06-13 16:53:02 -04004459 const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint;
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004460
John Kessenichfc51d282015-08-19 13:34:18 -06004461 // Check for queries
4462 if (cracked.query) {
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004463 // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first
4464 if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler))
John Kessenich33661452015-12-08 19:32:47 -07004465 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
Maciej Jesionowski7208a972016-10-12 15:40:37 +02004466
John Kessenichfc51d282015-08-19 13:34:18 -06004467 switch (node->getOp()) {
4468 case glslang::EOpImageQuerySize:
4469 case glslang::EOpTextureQuerySize:
John Kessenich140f3df2015-06-26 16:58:36 -06004470 if (arguments.size() > 1) {
4471 params.lod = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004472 return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult);
John Kessenich140f3df2015-06-26 16:58:36 -06004473 } else
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004474 return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult);
John Kessenicha28f7a72019-08-06 07:00:58 -06004475#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06004476 case glslang::EOpImageQuerySamples:
4477 case glslang::EOpTextureQuerySamples:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004478 return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004479 case glslang::EOpTextureQueryLod:
4480 params.coords = arguments[1];
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004481 return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult);
John Kessenichfc51d282015-08-19 13:34:18 -06004482 case glslang::EOpTextureQueryLevels:
steve-lunarg0b5c2ae2017-03-10 12:45:50 -07004483 return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult);
Rex Xu48edadf2015-12-31 16:11:41 +08004484 case glslang::EOpSparseTexelsResident:
4485 return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]);
John Kessenicha28f7a72019-08-06 07:00:58 -06004486#endif
John Kessenichfc51d282015-08-19 13:34:18 -06004487 default:
4488 assert(0);
4489 break;
John Kessenich140f3df2015-06-26 16:58:36 -06004490 }
John Kessenich140f3df2015-06-26 16:58:36 -06004491 }
4492
LoopDawg4425f242018-02-18 11:40:01 -07004493 int components = node->getType().getVectorSize();
4494
4495 if (node->getOp() == glslang::EOpTextureFetch) {
4496 // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed.
4497 // This will only happen through the HLSL path for operator[], so we do not have to handle e.g.
4498 // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic
4499 // here around e.g. which ones return scalars or other types.
4500 components = 4;
4501 }
4502
4503 glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components);
4504
4505 auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); };
4506
Rex Xufc618912015-09-09 16:42:49 +08004507 // Check for image functions other than queries
4508 if (node->isImage()) {
John Kessenich149afc32018-08-14 13:31:43 -06004509 std::vector<spv::IdImmediate> operands;
John Kessenich56bab042015-09-16 10:54:31 -06004510 auto opIt = arguments.begin();
John Kessenich149afc32018-08-14 13:31:43 -06004511 spv::IdImmediate image = { true, *(opIt++) };
4512 operands.push_back(image);
John Kessenich6c292d32016-02-15 20:58:50 -07004513
4514 // Handle subpass operations
4515 // TODO: GLSL should change to have the "MS" only on the type rather than the
4516 // built-in function.
4517 if (cracked.subpass) {
4518 // add on the (0,0) coordinate
4519 spv::Id zero = builder.makeIntConstant(0);
4520 std::vector<spv::Id> comps;
4521 comps.push_back(zero);
4522 comps.push_back(zero);
John Kessenich149afc32018-08-14 13:31:43 -06004523 spv::IdImmediate coord = { true,
4524 builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) };
4525 operands.push_back(coord);
John Kessenichf43c7392019-03-31 10:51:57 -06004526 spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone };
4527 imageOperands.word = imageOperands.word | signExtensionMask();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004528 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06004529 imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask;
4530 }
4531 if (imageOperands.word != spv::ImageOperandsMaskNone) {
John Kessenich149afc32018-08-14 13:31:43 -06004532 operands.push_back(imageOperands);
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004533 if (sampler.isMultiSample()) {
John Kessenichf43c7392019-03-31 10:51:57 -06004534 spv::IdImmediate imageOperand = { true, *(opIt++) };
4535 operands.push_back(imageOperand);
4536 }
John Kessenich6c292d32016-02-15 20:58:50 -07004537 }
John Kessenichfe4e5722017-10-19 02:07:30 -06004538 spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands);
4539 builder.setPrecision(result, precision);
4540 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07004541 }
4542
John Kessenich149afc32018-08-14 13:31:43 -06004543 spv::IdImmediate coord = { true, *(opIt++) };
4544 operands.push_back(coord);
Rex Xu129799a2017-07-05 17:23:28 +08004545 if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004546 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004547 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004548 mask = mask | spv::ImageOperandsSampleMask;
4549 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004550 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004551 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4552 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
Jeff Bolz36831c92018-09-05 10:11:41 -05004553 mask = mask | spv::ImageOperandsLodMask;
John Kessenich55e7d112015-11-15 21:33:39 -07004554 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004555 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4556 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004557 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004558 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004559 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4560 operands.push_back(imageOperands);
4561 }
4562 if (mask & spv::ImageOperandsSampleMask) {
4563 spv::IdImmediate imageOperand = { true, *opIt++ };
4564 operands.push_back(imageOperand);
4565 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004566 if (mask & spv::ImageOperandsLodMask) {
4567 spv::IdImmediate imageOperand = { true, *opIt++ };
4568 operands.push_back(imageOperand);
4569 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004570 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06004571 spv::IdImmediate imageOperand = { true,
4572 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05004573 operands.push_back(imageOperand);
4574 }
4575
John Kessenich149afc32018-08-14 13:31:43 -06004576 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004577 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
John Kessenichfe4e5722017-10-19 02:07:30 -06004578
John Kessenich149afc32018-08-14 13:31:43 -06004579 std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands));
LoopDawg4425f242018-02-18 11:40:01 -07004580 builder.setPrecision(result[0], precision);
4581
4582 // If needed, add a conversion constructor to the proper size.
4583 if (components != node->getType().getVectorSize())
4584 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4585
4586 return result[0];
Rex Xu129799a2017-07-05 17:23:28 +08004587 } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) {
Rex Xu129799a2017-07-05 17:23:28 +08004588
Jeff Bolz36831c92018-09-05 10:11:41 -05004589 // Push the texel value before the operands
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004590 if (sampler.isMultiSample() || cracked.lod) {
John Kessenich149afc32018-08-14 13:31:43 -06004591 spv::IdImmediate texel = { true, *(opIt + 1) };
4592 operands.push_back(texel);
John Kessenich149afc32018-08-14 13:31:43 -06004593 } else {
4594 spv::IdImmediate texel = { true, *opIt };
4595 operands.push_back(texel);
4596 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004597
4598 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004599 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004600 mask = mask | spv::ImageOperandsSampleMask;
4601 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004602 if (cracked.lod) {
4603 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4604 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4605 mask = mask | spv::ImageOperandsLodMask;
4606 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004607 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4608 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004609 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004610 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004611 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
4612 operands.push_back(imageOperands);
4613 }
4614 if (mask & spv::ImageOperandsSampleMask) {
4615 spv::IdImmediate imageOperand = { true, *opIt++ };
4616 operands.push_back(imageOperand);
4617 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004618 if (mask & spv::ImageOperandsLodMask) {
4619 spv::IdImmediate imageOperand = { true, *opIt++ };
4620 operands.push_back(imageOperand);
4621 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004622 if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) {
John Kessenichf43c7392019-03-31 10:51:57 -06004623 spv::IdImmediate imageOperand = { true,
4624 builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
Jeff Bolz36831c92018-09-05 10:11:41 -05004625 operands.push_back(imageOperand);
4626 }
4627
John Kessenich56bab042015-09-16 10:54:31 -06004628 builder.createNoResultOp(spv::OpImageWrite, operands);
John Kessenich149afc32018-08-14 13:31:43 -06004629 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
John Kessenich5d0fa972016-02-15 11:57:00 -07004630 builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
John Kessenich56bab042015-09-16 10:54:31 -06004631 return spv::NoResult;
John Kessenichf43c7392019-03-31 10:51:57 -06004632 } else if (node->getOp() == glslang::EOpSparseImageLoad ||
4633 node->getOp() == glslang::EOpSparseImageLoadLod) {
Rex Xu5eafa472016-02-19 22:24:03 +08004634 builder.addCapability(spv::CapabilitySparseResidency);
John Kessenich149afc32018-08-14 13:31:43 -06004635 if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown)
Rex Xu5eafa472016-02-19 22:24:03 +08004636 builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat);
4637
Jeff Bolz36831c92018-09-05 10:11:41 -05004638 spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone;
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004639 if (sampler.isMultiSample()) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004640 mask = mask | spv::ImageOperandsSampleMask;
4641 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004642 if (cracked.lod) {
Rex Xu129799a2017-07-05 17:23:28 +08004643 builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod);
4644 builder.addCapability(spv::CapabilityImageReadWriteLodAMD);
4645
Jeff Bolz36831c92018-09-05 10:11:41 -05004646 mask = mask | spv::ImageOperandsLodMask;
4647 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004648 mask = mask | TranslateImageOperands(TranslateCoherent(imageType));
4649 mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask);
John Kessenichf43c7392019-03-31 10:51:57 -06004650 mask = mask | signExtensionMask();
John Kessenich6e384fe2019-05-10 06:47:00 -06004651 if (mask != spv::ImageOperandsMaskNone) {
Jeff Bolz36831c92018-09-05 10:11:41 -05004652 spv::IdImmediate imageOperands = { false, (unsigned int)mask };
John Kessenich149afc32018-08-14 13:31:43 -06004653 operands.push_back(imageOperands);
Jeff Bolz36831c92018-09-05 10:11:41 -05004654 }
4655 if (mask & spv::ImageOperandsSampleMask) {
John Kessenich149afc32018-08-14 13:31:43 -06004656 spv::IdImmediate imageOperand = { true, *opIt++ };
4657 operands.push_back(imageOperand);
Jeff Bolz36831c92018-09-05 10:11:41 -05004658 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004659 if (mask & spv::ImageOperandsLodMask) {
4660 spv::IdImmediate imageOperand = { true, *opIt++ };
4661 operands.push_back(imageOperand);
4662 }
Jeff Bolz36831c92018-09-05 10:11:41 -05004663 if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) {
4664 spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) };
4665 operands.push_back(imageOperand);
Rex Xu5eafa472016-02-19 22:24:03 +08004666 }
4667
4668 // Create the return type that was a special structure
4669 spv::Id texelOut = *opIt;
John Kessenich8c8505c2016-07-26 12:50:38 -06004670 spv::Id typeId0 = resultType();
Rex Xu5eafa472016-02-19 22:24:03 +08004671 spv::Id typeId1 = builder.getDerefTypeId(texelOut);
4672 spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1);
4673
4674 spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands);
4675
4676 // Decode the return type
4677 builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut);
4678 return builder.createCompositeExtract(resultId, typeId0, 0);
John Kessenichcd261442016-01-22 09:54:12 -07004679 } else {
Rex Xu6b86d492015-09-16 17:48:22 +08004680 // Process image atomic operations
4681
4682 // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer,
4683 // as the first source operand, is required by SPIR-V atomic operations.
John Kessenich149afc32018-08-14 13:31:43 -06004684 // For non-MS, the sample value should be 0
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004685 spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) };
John Kessenich149afc32018-08-14 13:31:43 -06004686 operands.push_back(sample);
John Kessenich140f3df2015-06-26 16:58:36 -06004687
Jeff Bolz36831c92018-09-05 10:11:41 -05004688 spv::Id resultTypeId;
4689 // imageAtomicStore has a void return type so base the pointer type on
4690 // the type of the value operand.
4691 if (node->getOp() == glslang::EOpImageAtomicStore) {
4692 resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word));
4693 } else {
4694 resultTypeId = builder.makePointer(spv::StorageClassImage, resultType());
4695 }
John Kessenich56bab042015-09-16 10:54:31 -06004696 spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands);
Rex Xufc618912015-09-09 16:42:49 +08004697
4698 std::vector<spv::Id> operands;
4699 operands.push_back(pointer);
4700 for (; opIt != arguments.end(); ++opIt)
4701 operands.push_back(*opIt);
4702
Jeff Bolz38a52fc2019-06-14 09:56:28 -05004703 return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags);
Rex Xufc618912015-09-09 16:42:49 +08004704 }
4705 }
4706
John Kessenicha28f7a72019-08-06 07:00:58 -06004707#ifndef GLSLANG_WEB
amhagan05506bb2017-06-13 16:53:02 -04004708 // Check for fragment mask functions other than queries
4709 if (cracked.fragMask) {
4710 assert(sampler.ms);
4711
4712 auto opIt = arguments.begin();
4713 std::vector<spv::Id> operands;
4714
4715 // Extract the image if necessary
4716 if (builder.isSampledImage(params.sampler))
4717 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4718
4719 operands.push_back(params.sampler);
4720 ++opIt;
4721
4722 if (sampler.isSubpass()) {
4723 // add on the (0,0) coordinate
4724 spv::Id zero = builder.makeIntConstant(0);
4725 std::vector<spv::Id> comps;
4726 comps.push_back(zero);
4727 comps.push_back(zero);
4728 operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps));
4729 }
4730
4731 for (; opIt != arguments.end(); ++opIt)
4732 operands.push_back(*opIt);
4733
4734 spv::Op fragMaskOp = spv::OpNop;
4735 if (node->getOp() == glslang::EOpFragmentMaskFetch)
4736 fragMaskOp = spv::OpFragmentMaskFetchAMD;
4737 else if (node->getOp() == glslang::EOpFragmentFetch)
4738 fragMaskOp = spv::OpFragmentFetchAMD;
4739
4740 builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask);
4741 builder.addCapability(spv::CapabilityFragmentMaskAMD);
4742 return builder.createOp(fragMaskOp, resultType(), operands);
4743 }
4744#endif
4745
Rex Xufc618912015-09-09 16:42:49 +08004746 // Check for texture functions other than queries
Rex Xu48edadf2015-12-31 16:11:41 +08004747 bool sparse = node->isSparseTexture();
Chao Chen3a137962018-09-19 11:41:27 -07004748 bool imageFootprint = node->isImageFootprint();
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004749 bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow();
Rex Xu71519fe2015-11-11 15:35:47 +08004750
John Kessenichfc51d282015-08-19 13:34:18 -06004751 // check for bias argument
4752 bool bias = false;
Rex Xu225e0fc2016-11-17 17:47:59 +08004753 if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06004754 int nonBiasArgCount = 2;
Rex Xu225e0fc2016-11-17 17:47:59 +08004755 if (cracked.gather)
4756 ++nonBiasArgCount; // comp argument should be present when bias argument is present
Rex Xu1e5d7b02016-11-29 17:36:31 +08004757
4758 if (f16ShadowCompare)
4759 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06004760 if (cracked.offset)
4761 ++nonBiasArgCount;
Rex Xu225e0fc2016-11-17 17:47:59 +08004762 else if (cracked.offsets)
4763 ++nonBiasArgCount;
John Kessenichfc51d282015-08-19 13:34:18 -06004764 if (cracked.grad)
4765 nonBiasArgCount += 2;
Rex Xu48edadf2015-12-31 16:11:41 +08004766 if (cracked.lodClamp)
4767 ++nonBiasArgCount;
4768 if (sparse)
4769 ++nonBiasArgCount;
Chao Chen3a137962018-09-19 11:41:27 -07004770 if (imageFootprint)
4771 //Following three extra arguments
4772 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4773 nonBiasArgCount += 3;
John Kessenichfc51d282015-08-19 13:34:18 -06004774 if ((int)arguments.size() > nonBiasArgCount)
4775 bias = true;
4776 }
4777
John Kessenicha5c33d62016-06-02 23:45:21 -06004778 // See if the sampler param should really be just the SPV image part
4779 if (cracked.fetch) {
4780 // a fetch needs to have the image extracted first
4781 if (builder.isSampledImage(params.sampler))
4782 params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler);
4783 }
4784
John Kessenicha28f7a72019-08-06 07:00:58 -06004785#ifndef GLSLANG_WEB
Rex Xu225e0fc2016-11-17 17:47:59 +08004786 if (cracked.gather) {
4787 const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions();
4788 if (bias || cracked.lod ||
4789 sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) {
4790 builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod);
Rex Xu301a2bc2017-06-14 23:09:39 +08004791 builder.addCapability(spv::CapabilityImageGatherBiasLodAMD);
Rex Xu225e0fc2016-11-17 17:47:59 +08004792 }
4793 }
4794#endif
4795
John Kessenichfc51d282015-08-19 13:34:18 -06004796 // set the rest of the arguments
John Kessenich55e7d112015-11-15 21:33:39 -07004797
John Kessenichfc51d282015-08-19 13:34:18 -06004798 params.coords = arguments[1];
4799 int extraArgs = 0;
John Kessenich019f08f2016-02-15 15:40:42 -07004800 bool noImplicitLod = false;
John Kessenich55e7d112015-11-15 21:33:39 -07004801
4802 // sort out where Dref is coming from
Rex Xu1e5d7b02016-11-29 17:36:31 +08004803 if (cubeCompare || f16ShadowCompare) {
John Kessenichfc51d282015-08-19 13:34:18 -06004804 params.Dref = arguments[2];
Rex Xu48edadf2015-12-31 16:11:41 +08004805 ++extraArgs;
4806 } else if (sampler.shadow && cracked.gather) {
John Kessenich55e7d112015-11-15 21:33:39 -07004807 params.Dref = arguments[2];
4808 ++extraArgs;
4809 } else if (sampler.shadow) {
John Kessenichfc51d282015-08-19 13:34:18 -06004810 std::vector<spv::Id> indexes;
John Kessenich76d4dfc2016-06-16 12:43:23 -06004811 int dRefComp;
John Kessenichfc51d282015-08-19 13:34:18 -06004812 if (cracked.proj)
John Kessenich76d4dfc2016-06-16 12:43:23 -06004813 dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref"
John Kessenichfc51d282015-08-19 13:34:18 -06004814 else
John Kessenich76d4dfc2016-06-16 12:43:23 -06004815 dRefComp = builder.getNumComponents(params.coords) - 1;
4816 indexes.push_back(dRefComp);
John Kessenichfc51d282015-08-19 13:34:18 -06004817 params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes);
4818 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004819
4820 // lod
John Kessenichfc51d282015-08-19 13:34:18 -06004821 if (cracked.lod) {
LoopDawgef94b1a2017-07-24 18:45:37 -06004822 params.lod = arguments[2 + extraArgs];
John Kessenichfc51d282015-08-19 13:34:18 -06004823 ++extraArgs;
John Kessenichb9197c82019-08-11 07:41:45 -06004824 } else if (glslangIntermediate->getStage() != EShLangFragment &&
4825 !(glslangIntermediate->getStage() == EShLangCompute &&
4826 glslangIntermediate->hasLayoutDerivativeModeNone())) {
John Kessenich019f08f2016-02-15 15:40:42 -07004827 // we need to invent the default lod for an explicit lod instruction for a non-fragment stage
4828 noImplicitLod = true;
4829 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004830
4831 // multisample
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004832 if (sampler.isMultiSample()) {
LoopDawgef94b1a2017-07-24 18:45:37 -06004833 params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified
Rex Xu04db3f52015-09-16 11:44:02 +08004834 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06004835 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004836
4837 // gradient
John Kessenichfc51d282015-08-19 13:34:18 -06004838 if (cracked.grad) {
4839 params.gradX = arguments[2 + extraArgs];
4840 params.gradY = arguments[3 + extraArgs];
4841 extraArgs += 2;
4842 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004843
4844 // offset and offsets
John Kessenich55e7d112015-11-15 21:33:39 -07004845 if (cracked.offset) {
John Kessenichfc51d282015-08-19 13:34:18 -06004846 params.offset = arguments[2 + extraArgs];
4847 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07004848 } else if (cracked.offsets) {
4849 params.offsets = arguments[2 + extraArgs];
4850 ++extraArgs;
John Kessenichfc51d282015-08-19 13:34:18 -06004851 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004852
John Kessenich3e4b6ff2019-08-08 01:15:24 -06004853#ifndef GLSLANG_WEB
John Kessenich76d4dfc2016-06-16 12:43:23 -06004854 // lod clamp
Rex Xu48edadf2015-12-31 16:11:41 +08004855 if (cracked.lodClamp) {
4856 params.lodClamp = arguments[2 + extraArgs];
4857 ++extraArgs;
4858 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004859 // sparse
Rex Xu48edadf2015-12-31 16:11:41 +08004860 if (sparse) {
4861 params.texelOut = arguments[2 + extraArgs];
4862 ++extraArgs;
4863 }
John Kessenich76d4dfc2016-06-16 12:43:23 -06004864 // gather component
John Kessenich55e7d112015-11-15 21:33:39 -07004865 if (cracked.gather && ! sampler.shadow) {
4866 // default component is 0, if missing, otherwise an argument
4867 if (2 + extraArgs < (int)arguments.size()) {
John Kessenich76d4dfc2016-06-16 12:43:23 -06004868 params.component = arguments[2 + extraArgs];
John Kessenich55e7d112015-11-15 21:33:39 -07004869 ++extraArgs;
Rex Xu225e0fc2016-11-17 17:47:59 +08004870 } else
John Kessenich76d4dfc2016-06-16 12:43:23 -06004871 params.component = builder.makeIntConstant(0);
Rex Xu225e0fc2016-11-17 17:47:59 +08004872 }
Chao Chen3a137962018-09-19 11:41:27 -07004873 spv::Id resultStruct = spv::NoResult;
4874 if (imageFootprint) {
4875 //Following three extra arguments
4876 // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint
4877 params.granularity = arguments[2 + extraArgs];
4878 params.coarse = arguments[3 + extraArgs];
4879 resultStruct = arguments[4 + extraArgs];
4880 extraArgs += 3;
4881 }
4882#endif
Rex Xu225e0fc2016-11-17 17:47:59 +08004883 // bias
4884 if (bias) {
4885 params.bias = arguments[2 + extraArgs];
4886 ++extraArgs;
John Kessenich55e7d112015-11-15 21:33:39 -07004887 }
John Kessenichfc51d282015-08-19 13:34:18 -06004888
John Kessenicha28f7a72019-08-06 07:00:58 -06004889#ifndef GLSLANG_WEB
Chao Chen3a137962018-09-19 11:41:27 -07004890 if (imageFootprint) {
4891 builder.addExtension(spv::E_SPV_NV_shader_image_footprint);
4892 builder.addCapability(spv::CapabilityImageFootprintNV);
4893
4894
4895 //resultStructType(OpenGL type) contains 5 elements:
4896 //struct gl_TextureFootprint2DNV {
4897 // uvec2 anchor;
4898 // uvec2 offset;
4899 // uvec2 mask;
4900 // uint lod;
4901 // uint granularity;
4902 //};
4903 //or
4904 //struct gl_TextureFootprint3DNV {
4905 // uvec3 anchor;
4906 // uvec3 offset;
4907 // uvec2 mask;
4908 // uint lod;
4909 // uint granularity;
4910 //};
4911 spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct));
4912 assert(builder.isStructType(resultStructType));
4913
4914 //resType (SPIR-V type) contains 6 elements:
4915 //Member 0 must be a Boolean type scalar(LOD),
4916 //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor),
4917 //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset),
4918 //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask),
4919 //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod),
4920 //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity).
4921 std::vector<spv::Id> members;
4922 members.push_back(resultType());
4923 for (int i = 0; i < 5; i++) {
4924 members.push_back(builder.getContainedTypeId(resultStructType, i));
4925 }
4926 spv::Id resType = builder.makeStructType(members, "ResType");
4927
4928 //call ImageFootprintNV
John Kessenichf43c7392019-03-31 10:51:57 -06004929 spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj,
4930 cracked.gather, noImplicitLod, params, signExtensionMask());
Chao Chen3a137962018-09-19 11:41:27 -07004931
4932 //copy resType (SPIR-V type) to resultStructType(OpenGL type)
4933 for (int i = 0; i < 5; i++) {
4934 builder.clearAccessChain();
4935 builder.setAccessChainLValue(resultStruct);
4936
4937 //Accessing to a struct we created, no coherent flag is set
4938 spv::Builder::AccessChain::CoherentFlags flags;
4939 flags.clear();
4940
Jeff Bolz9f2aec42019-01-06 17:58:04 -06004941 builder.accessChainPush(builder.makeIntConstant(i), flags, 0);
Chao Chen3a137962018-09-19 11:41:27 -07004942 builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1));
4943 }
4944 return builder.createCompositeExtract(res, resultType(), 0);
4945 }
4946#endif
4947
John Kessenich65336482016-06-16 14:06:26 -06004948 // projective component (might not to move)
4949 // GLSL: "The texture coordinates consumed from P, not including the last component of P,
4950 // are divided by the last component of P."
4951 // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all
4952 // unused components will appear after all used components."
4953 if (cracked.proj) {
4954 int projSourceComp = builder.getNumComponents(params.coords) - 1;
4955 int projTargetComp;
4956 switch (sampler.dim) {
4957 case glslang::Esd1D: projTargetComp = 1; break;
4958 case glslang::Esd2D: projTargetComp = 2; break;
4959 case glslang::EsdRect: projTargetComp = 2; break;
4960 default: projTargetComp = projSourceComp; break;
4961 }
4962 // copy the projective coordinate if we have to
4963 if (projTargetComp != projSourceComp) {
John Kessenichecba76f2017-01-06 00:34:48 -07004964 spv::Id projComp = builder.createCompositeExtract(params.coords,
John Kessenich65336482016-06-16 14:06:26 -06004965 builder.getScalarTypeId(builder.getTypeId(params.coords)),
4966 projSourceComp);
4967 params.coords = builder.createCompositeInsert(projComp, params.coords,
4968 builder.getTypeId(params.coords), projTargetComp);
4969 }
4970 }
4971
Jeff Bolz36831c92018-09-05 10:11:41 -05004972 // nonprivate
4973 if (imageType.getQualifier().nonprivate) {
4974 params.nonprivate = true;
4975 }
4976
4977 // volatile
4978 if (imageType.getQualifier().volatil) {
4979 params.volatil = true;
4980 }
4981
St0fFa1184dd2018-04-09 21:08:14 +02004982 std::vector<spv::Id> result( 1,
John Kessenichf43c7392019-03-31 10:51:57 -06004983 builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather,
4984 noImplicitLod, params, signExtensionMask())
St0fFa1184dd2018-04-09 21:08:14 +02004985 );
LoopDawg4425f242018-02-18 11:40:01 -07004986
4987 if (components != node->getType().getVectorSize())
4988 result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType()));
4989
4990 return result[0];
John Kessenich140f3df2015-06-26 16:58:36 -06004991}
4992
4993spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
4994{
4995 // Grab the function's pointer from the previously created function
4996 spv::Function* function = functionMap[node->getName().c_str()];
4997 if (! function)
4998 return 0;
4999
5000 const glslang::TIntermSequence& glslangArgs = node->getSequence();
5001 const glslang::TQualifierList& qualifiers = node->getQualifierList();
5002
5003 // See comments in makeFunctions() for details about the semantics for parameter passing.
5004 //
5005 // These imply we need a four step process:
5006 // 1. Evaluate the arguments
5007 // 2. Allocate and make copies of in, out, and inout arguments
5008 // 3. Make the call
5009 // 4. Copy back the results
5010
John Kessenichd3ed90b2018-05-04 11:43:03 -06005011 // 1. Evaluate the arguments and their types
John Kessenich140f3df2015-06-26 16:58:36 -06005012 std::vector<spv::Builder::AccessChain> lValues;
5013 std::vector<spv::Id> rValues;
John Kessenich32cfd492016-02-02 12:37:46 -07005014 std::vector<const glslang::TType*> argTypes;
John Kessenich140f3df2015-06-26 16:58:36 -06005015 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005016 argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType());
John Kessenich140f3df2015-06-26 16:58:36 -06005017 // build l-value
5018 builder.clearAccessChain();
5019 glslangArgs[a]->traverse(this);
John Kessenichd41993d2017-09-10 15:21:05 -06005020 // keep outputs and pass-by-originals as l-values, evaluate others as r-values
John Kessenichd3ed90b2018-05-04 11:43:03 -06005021 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) ||
John Kessenich6a14f782017-12-04 02:48:10 -07005022 writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005023 // save l-value
5024 lValues.push_back(builder.getAccessChain());
5025 } else {
5026 // process r-value
John Kessenich32cfd492016-02-02 12:37:46 -07005027 rValues.push_back(accessChainLoad(*argTypes.back()));
John Kessenich140f3df2015-06-26 16:58:36 -06005028 }
5029 }
5030
5031 // 2. Allocate space for anything needing a copy, and if it's "in" or "inout"
5032 // copy the original into that space.
5033 //
5034 // Also, build up the list of actual arguments to pass in for the call
5035 int lValueCount = 0;
5036 int rValueCount = 0;
5037 std::vector<spv::Id> spvArgs;
5038 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
5039 spv::Id arg;
John Kessenichd3ed90b2018-05-04 11:43:03 -06005040 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) {
Jason Ekstrand76d0ac12016-05-25 11:50:21 -07005041 builder.setAccessChain(lValues[lValueCount]);
5042 arg = builder.accessChainGetLValue();
5043 ++lValueCount;
John Kessenichd41993d2017-09-10 15:21:05 -06005044 } else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005045 // need space to hold the copy
John Kessenichd3ed90b2018-05-04 11:43:03 -06005046 arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param");
John Kessenich140f3df2015-06-26 16:58:36 -06005047 if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) {
5048 // need to copy the input into output space
5049 builder.setAccessChain(lValues[lValueCount]);
John Kessenich32cfd492016-02-02 12:37:46 -07005050 spv::Id copy = accessChainLoad(*argTypes[a]);
John Kessenich4bf71552016-09-02 11:20:21 -06005051 builder.clearAccessChain();
5052 builder.setAccessChainLValue(arg);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005053 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005054 }
5055 ++lValueCount;
5056 } else {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005057 // process r-value, which involves a copy for a type mismatch
5058 if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) {
5059 spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg");
5060 builder.clearAccessChain();
5061 builder.setAccessChainLValue(argCopy);
5062 multiTypeStore(*argTypes[a], rValues[rValueCount]);
5063 arg = builder.createLoad(argCopy);
5064 } else
5065 arg = rValues[rValueCount];
John Kessenich140f3df2015-06-26 16:58:36 -06005066 ++rValueCount;
5067 }
5068 spvArgs.push_back(arg);
5069 }
5070
5071 // 3. Make the call.
5072 spv::Id result = builder.createFunctionCall(function, spvArgs);
John Kessenich32cfd492016-02-02 12:37:46 -07005073 builder.setPrecision(result, TranslatePrecisionDecoration(node->getType()));
John Kessenich140f3df2015-06-26 16:58:36 -06005074
5075 // 4. Copy back out an "out" arguments.
5076 lValueCount = 0;
5077 for (int a = 0; a < (int)glslangArgs.size(); ++a) {
John Kessenichd3ed90b2018-05-04 11:43:03 -06005078 if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0))
John Kessenichd41993d2017-09-10 15:21:05 -06005079 ++lValueCount;
5080 else if (writableParam(qualifiers[a])) {
John Kessenich140f3df2015-06-26 16:58:36 -06005081 if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) {
5082 spv::Id copy = builder.createLoad(spvArgs[a]);
5083 builder.setAccessChain(lValues[lValueCount]);
John Kessenichd3ed90b2018-05-04 11:43:03 -06005084 multiTypeStore(*argTypes[a], copy);
John Kessenich140f3df2015-06-26 16:58:36 -06005085 }
5086 ++lValueCount;
5087 }
5088 }
5089
5090 return result;
5091}
5092
5093// Translate AST operation to SPV operation, already having SPV-based operands/types.
John Kessenichead86222018-03-28 18:01:20 -06005094spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
John Kessenich140f3df2015-06-26 16:58:36 -06005095 spv::Id typeId, spv::Id left, spv::Id right,
5096 glslang::TBasicType typeProxy, bool reduceComparison)
5097{
John Kessenich66011cb2018-03-06 16:12:04 -07005098 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5099 bool isFloat = isTypeFloat(typeProxy);
Rex Xuc7d36562016-04-27 08:15:37 +08005100 bool isBool = typeProxy == glslang::EbtBool;
John Kessenich140f3df2015-06-26 16:58:36 -06005101
5102 spv::Op binOp = spv::OpNop;
John Kessenichec43d0a2015-07-04 17:17:31 -06005103 bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector?
John Kessenich140f3df2015-06-26 16:58:36 -06005104 bool comparison = false;
5105
5106 switch (op) {
5107 case glslang::EOpAdd:
5108 case glslang::EOpAddAssign:
5109 if (isFloat)
5110 binOp = spv::OpFAdd;
5111 else
5112 binOp = spv::OpIAdd;
5113 break;
5114 case glslang::EOpSub:
5115 case glslang::EOpSubAssign:
5116 if (isFloat)
5117 binOp = spv::OpFSub;
5118 else
5119 binOp = spv::OpISub;
5120 break;
5121 case glslang::EOpMul:
5122 case glslang::EOpMulAssign:
5123 if (isFloat)
5124 binOp = spv::OpFMul;
5125 else
5126 binOp = spv::OpIMul;
5127 break;
5128 case glslang::EOpVectorTimesScalar:
5129 case glslang::EOpVectorTimesScalarAssign:
John Kessenich8d72f1a2016-05-20 12:06:03 -06005130 if (isFloat && (builder.isVector(left) || builder.isVector(right))) {
John Kessenichec43d0a2015-07-04 17:17:31 -06005131 if (builder.isVector(right))
5132 std::swap(left, right);
5133 assert(builder.isScalar(right));
5134 needMatchingVectors = false;
5135 binOp = spv::OpVectorTimesScalar;
t.jung697fdf02018-11-14 13:04:39 +01005136 } else if (isFloat)
5137 binOp = spv::OpFMul;
5138 else
John Kessenichec43d0a2015-07-04 17:17:31 -06005139 binOp = spv::OpIMul;
John Kessenich140f3df2015-06-26 16:58:36 -06005140 break;
5141 case glslang::EOpVectorTimesMatrix:
5142 case glslang::EOpVectorTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005143 binOp = spv::OpVectorTimesMatrix;
5144 break;
5145 case glslang::EOpMatrixTimesVector:
John Kessenich140f3df2015-06-26 16:58:36 -06005146 binOp = spv::OpMatrixTimesVector;
5147 break;
5148 case glslang::EOpMatrixTimesScalar:
5149 case glslang::EOpMatrixTimesScalarAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005150 binOp = spv::OpMatrixTimesScalar;
5151 break;
5152 case glslang::EOpMatrixTimesMatrix:
5153 case glslang::EOpMatrixTimesMatrixAssign:
John Kessenich140f3df2015-06-26 16:58:36 -06005154 binOp = spv::OpMatrixTimesMatrix;
5155 break;
5156 case glslang::EOpOuterProduct:
5157 binOp = spv::OpOuterProduct;
John Kessenichec43d0a2015-07-04 17:17:31 -06005158 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005159 break;
5160
5161 case glslang::EOpDiv:
5162 case glslang::EOpDivAssign:
5163 if (isFloat)
5164 binOp = spv::OpFDiv;
5165 else if (isUnsigned)
5166 binOp = spv::OpUDiv;
5167 else
5168 binOp = spv::OpSDiv;
5169 break;
5170 case glslang::EOpMod:
5171 case glslang::EOpModAssign:
5172 if (isFloat)
5173 binOp = spv::OpFMod;
5174 else if (isUnsigned)
5175 binOp = spv::OpUMod;
5176 else
5177 binOp = spv::OpSMod;
5178 break;
5179 case glslang::EOpRightShift:
5180 case glslang::EOpRightShiftAssign:
5181 if (isUnsigned)
5182 binOp = spv::OpShiftRightLogical;
5183 else
5184 binOp = spv::OpShiftRightArithmetic;
5185 break;
5186 case glslang::EOpLeftShift:
5187 case glslang::EOpLeftShiftAssign:
5188 binOp = spv::OpShiftLeftLogical;
5189 break;
5190 case glslang::EOpAnd:
5191 case glslang::EOpAndAssign:
5192 binOp = spv::OpBitwiseAnd;
5193 break;
5194 case glslang::EOpLogicalAnd:
John Kessenichec43d0a2015-07-04 17:17:31 -06005195 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005196 binOp = spv::OpLogicalAnd;
5197 break;
5198 case glslang::EOpInclusiveOr:
5199 case glslang::EOpInclusiveOrAssign:
5200 binOp = spv::OpBitwiseOr;
5201 break;
5202 case glslang::EOpLogicalOr:
John Kessenichec43d0a2015-07-04 17:17:31 -06005203 needMatchingVectors = false;
John Kessenich140f3df2015-06-26 16:58:36 -06005204 binOp = spv::OpLogicalOr;
5205 break;
5206 case glslang::EOpExclusiveOr:
5207 case glslang::EOpExclusiveOrAssign:
5208 binOp = spv::OpBitwiseXor;
5209 break;
5210 case glslang::EOpLogicalXor:
John Kessenichec43d0a2015-07-04 17:17:31 -06005211 needMatchingVectors = false;
John Kessenich5e4b1242015-08-06 22:53:06 -06005212 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005213 break;
5214
5215 case glslang::EOpLessThan:
5216 case glslang::EOpGreaterThan:
5217 case glslang::EOpLessThanEqual:
5218 case glslang::EOpGreaterThanEqual:
5219 case glslang::EOpEqual:
5220 case glslang::EOpNotEqual:
5221 case glslang::EOpVectorEqual:
5222 case glslang::EOpVectorNotEqual:
5223 comparison = true;
5224 break;
5225 default:
5226 break;
5227 }
5228
John Kessenich7c1aa102015-10-15 13:29:11 -06005229 // handle mapped binary operations (should be non-comparison)
John Kessenich140f3df2015-06-26 16:58:36 -06005230 if (binOp != spv::OpNop) {
John Kessenich7c1aa102015-10-15 13:29:11 -06005231 assert(comparison == false);
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005232 if (builder.isMatrix(left) || builder.isMatrix(right) ||
5233 builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
John Kessenichead86222018-03-28 18:01:20 -06005234 return createBinaryMatrixOperation(binOp, decorations, typeId, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005235
5236 // No matrix involved; make both operands be the same number of components, if needed
John Kessenichec43d0a2015-07-04 17:17:31 -06005237 if (needMatchingVectors)
John Kessenichead86222018-03-28 18:01:20 -06005238 builder.promoteScalar(decorations.precision, left, right);
John Kessenich140f3df2015-06-26 16:58:36 -06005239
qining25262b32016-05-06 17:25:16 -04005240 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005241 decorations.addNoContraction(builder, result);
5242 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005243 return builder.setPrecision(result, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06005244 }
5245
5246 if (! comparison)
5247 return 0;
5248
John Kessenich7c1aa102015-10-15 13:29:11 -06005249 // Handle comparison instructions
John Kessenich140f3df2015-06-26 16:58:36 -06005250
John Kessenich4583b612016-08-07 19:14:22 -06005251 if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual)
John Kessenichead86222018-03-28 18:01:20 -06005252 && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) {
5253 spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual);
John Kessenichb9197c82019-08-11 07:41:45 -06005254 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005255 return result;
5256 }
John Kessenich140f3df2015-06-26 16:58:36 -06005257
5258 switch (op) {
5259 case glslang::EOpLessThan:
5260 if (isFloat)
5261 binOp = spv::OpFOrdLessThan;
5262 else if (isUnsigned)
5263 binOp = spv::OpULessThan;
5264 else
5265 binOp = spv::OpSLessThan;
5266 break;
5267 case glslang::EOpGreaterThan:
5268 if (isFloat)
5269 binOp = spv::OpFOrdGreaterThan;
5270 else if (isUnsigned)
5271 binOp = spv::OpUGreaterThan;
5272 else
5273 binOp = spv::OpSGreaterThan;
5274 break;
5275 case glslang::EOpLessThanEqual:
5276 if (isFloat)
5277 binOp = spv::OpFOrdLessThanEqual;
5278 else if (isUnsigned)
5279 binOp = spv::OpULessThanEqual;
5280 else
5281 binOp = spv::OpSLessThanEqual;
5282 break;
5283 case glslang::EOpGreaterThanEqual:
5284 if (isFloat)
5285 binOp = spv::OpFOrdGreaterThanEqual;
5286 else if (isUnsigned)
5287 binOp = spv::OpUGreaterThanEqual;
5288 else
5289 binOp = spv::OpSGreaterThanEqual;
5290 break;
5291 case glslang::EOpEqual:
5292 case glslang::EOpVectorEqual:
5293 if (isFloat)
5294 binOp = spv::OpFOrdEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005295 else if (isBool)
5296 binOp = spv::OpLogicalEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005297 else
5298 binOp = spv::OpIEqual;
5299 break;
5300 case glslang::EOpNotEqual:
5301 case glslang::EOpVectorNotEqual:
5302 if (isFloat)
5303 binOp = spv::OpFOrdNotEqual;
Rex Xuc7d36562016-04-27 08:15:37 +08005304 else if (isBool)
5305 binOp = spv::OpLogicalNotEqual;
John Kessenich140f3df2015-06-26 16:58:36 -06005306 else
5307 binOp = spv::OpINotEqual;
5308 break;
5309 default:
5310 break;
5311 }
5312
qining25262b32016-05-06 17:25:16 -04005313 if (binOp != spv::OpNop) {
5314 spv::Id result = builder.createBinOp(binOp, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005315 decorations.addNoContraction(builder, result);
5316 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005317 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005318 }
John Kessenich140f3df2015-06-26 16:58:36 -06005319
5320 return 0;
5321}
5322
John Kessenich04bb8a02015-12-12 12:28:14 -07005323//
5324// Translate AST matrix operation to SPV operation, already having SPV-based operands/types.
5325// These can be any of:
5326//
5327// matrix * scalar
5328// scalar * matrix
5329// matrix * matrix linear algebraic
5330// matrix * vector
5331// vector * matrix
5332// matrix * matrix componentwise
5333// matrix op matrix op in {+, -, /}
5334// matrix op scalar op in {+, -, /}
5335// scalar op matrix op in {+, -, /}
5336//
John Kessenichead86222018-03-28 18:01:20 -06005337spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5338 spv::Id left, spv::Id right)
John Kessenich04bb8a02015-12-12 12:28:14 -07005339{
5340 bool firstClass = true;
5341
5342 // First, handle first-class matrix operations (* and matrix/scalar)
5343 switch (op) {
5344 case spv::OpFDiv:
5345 if (builder.isMatrix(left) && builder.isScalar(right)) {
5346 // turn matrix / scalar into a multiply...
Neil Robertseddb1312018-03-13 10:57:59 +01005347 spv::Id resultType = builder.getTypeId(right);
5348 right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right);
John Kessenich04bb8a02015-12-12 12:28:14 -07005349 op = spv::OpMatrixTimesScalar;
5350 } else
5351 firstClass = false;
5352 break;
5353 case spv::OpMatrixTimesScalar:
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005354 if (builder.isMatrix(right) || builder.isCooperativeMatrix(right))
John Kessenich04bb8a02015-12-12 12:28:14 -07005355 std::swap(left, right);
5356 assert(builder.isScalar(right));
5357 break;
5358 case spv::OpVectorTimesMatrix:
5359 assert(builder.isVector(left));
5360 assert(builder.isMatrix(right));
5361 break;
5362 case spv::OpMatrixTimesVector:
5363 assert(builder.isMatrix(left));
5364 assert(builder.isVector(right));
5365 break;
5366 case spv::OpMatrixTimesMatrix:
5367 assert(builder.isMatrix(left));
5368 assert(builder.isMatrix(right));
5369 break;
5370 default:
5371 firstClass = false;
5372 break;
5373 }
5374
Jeff Bolz4605e2e2019-02-19 13:10:32 -06005375 if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right))
5376 firstClass = true;
5377
qining25262b32016-05-06 17:25:16 -04005378 if (firstClass) {
5379 spv::Id result = builder.createBinOp(op, typeId, left, right);
John Kessenichb9197c82019-08-11 07:41:45 -06005380 decorations.addNoContraction(builder, result);
5381 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005382 return builder.setPrecision(result, decorations.precision);
qining25262b32016-05-06 17:25:16 -04005383 }
John Kessenich04bb8a02015-12-12 12:28:14 -07005384
LoopDawg592860c2016-06-09 08:57:35 -06005385 // Handle component-wise +, -, *, %, and / for all combinations of type.
John Kessenich04bb8a02015-12-12 12:28:14 -07005386 // The result type of all of them is the same type as the (a) matrix operand.
5387 // The algorithm is to:
5388 // - break the matrix(es) into vectors
5389 // - smear any scalar to a vector
5390 // - do vector operations
5391 // - make a matrix out the vector results
5392 switch (op) {
5393 case spv::OpFAdd:
5394 case spv::OpFSub:
5395 case spv::OpFDiv:
LoopDawg592860c2016-06-09 08:57:35 -06005396 case spv::OpFMod:
John Kessenich04bb8a02015-12-12 12:28:14 -07005397 case spv::OpFMul:
5398 {
5399 // one time set up...
5400 bool leftMat = builder.isMatrix(left);
5401 bool rightMat = builder.isMatrix(right);
5402 unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right);
5403 int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right);
5404 spv::Id scalarType = builder.getScalarTypeId(typeId);
5405 spv::Id vecType = builder.makeVectorType(scalarType, numRows);
5406 std::vector<spv::Id> results;
5407 spv::Id smearVec = spv::NoResult;
5408 if (builder.isScalar(left))
John Kessenichead86222018-03-28 18:01:20 -06005409 smearVec = builder.smearScalar(decorations.precision, left, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005410 else if (builder.isScalar(right))
John Kessenichead86222018-03-28 18:01:20 -06005411 smearVec = builder.smearScalar(decorations.precision, right, vecType);
John Kessenich04bb8a02015-12-12 12:28:14 -07005412
5413 // do each vector op
5414 for (unsigned int c = 0; c < numCols; ++c) {
5415 std::vector<unsigned int> indexes;
5416 indexes.push_back(c);
5417 spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec;
5418 spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec;
qining25262b32016-05-06 17:25:16 -04005419 spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec);
John Kessenichb9197c82019-08-11 07:41:45 -06005420 decorations.addNoContraction(builder, result);
5421 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005422 results.push_back(builder.setPrecision(result, decorations.precision));
John Kessenich04bb8a02015-12-12 12:28:14 -07005423 }
5424
5425 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005426 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06005427 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005428 return result;
John Kessenich04bb8a02015-12-12 12:28:14 -07005429 }
5430 default:
5431 assert(0);
5432 return spv::NoResult;
5433 }
5434}
5435
John Kessenichead86222018-03-28 18:01:20 -06005436spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
Jeff Bolz38a52fc2019-06-14 09:56:28 -05005437 spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich140f3df2015-06-26 16:58:36 -06005438{
5439 spv::Op unaryOp = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08005440 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06005441 int libCall = -1;
John Kessenich66011cb2018-03-06 16:12:04 -07005442 bool isUnsigned = isTypeUnsignedInt(typeProxy);
5443 bool isFloat = isTypeFloat(typeProxy);
John Kessenich140f3df2015-06-26 16:58:36 -06005444
5445 switch (op) {
5446 case glslang::EOpNegative:
John Kessenich7a53f762016-01-20 11:19:27 -07005447 if (isFloat) {
John Kessenich140f3df2015-06-26 16:58:36 -06005448 unaryOp = spv::OpFNegate;
John Kessenich7a53f762016-01-20 11:19:27 -07005449 if (builder.isMatrixType(typeId))
John Kessenichead86222018-03-28 18:01:20 -06005450 return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy);
John Kessenich7a53f762016-01-20 11:19:27 -07005451 } else
John Kessenich140f3df2015-06-26 16:58:36 -06005452 unaryOp = spv::OpSNegate;
5453 break;
5454
5455 case glslang::EOpLogicalNot:
5456 case glslang::EOpVectorLogicalNot:
John Kessenich5e4b1242015-08-06 22:53:06 -06005457 unaryOp = spv::OpLogicalNot;
5458 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005459 case glslang::EOpBitwiseNot:
5460 unaryOp = spv::OpNot;
5461 break;
John Kessenich5e4b1242015-08-06 22:53:06 -06005462
John Kessenich140f3df2015-06-26 16:58:36 -06005463 case glslang::EOpDeterminant:
John Kessenich5e4b1242015-08-06 22:53:06 -06005464 libCall = spv::GLSLstd450Determinant;
John Kessenich140f3df2015-06-26 16:58:36 -06005465 break;
5466 case glslang::EOpMatrixInverse:
John Kessenich5e4b1242015-08-06 22:53:06 -06005467 libCall = spv::GLSLstd450MatrixInverse;
John Kessenich140f3df2015-06-26 16:58:36 -06005468 break;
5469 case glslang::EOpTranspose:
5470 unaryOp = spv::OpTranspose;
5471 break;
5472
5473 case glslang::EOpRadians:
John Kessenich5e4b1242015-08-06 22:53:06 -06005474 libCall = spv::GLSLstd450Radians;
John Kessenich140f3df2015-06-26 16:58:36 -06005475 break;
5476 case glslang::EOpDegrees:
John Kessenich5e4b1242015-08-06 22:53:06 -06005477 libCall = spv::GLSLstd450Degrees;
John Kessenich140f3df2015-06-26 16:58:36 -06005478 break;
5479 case glslang::EOpSin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005480 libCall = spv::GLSLstd450Sin;
John Kessenich140f3df2015-06-26 16:58:36 -06005481 break;
5482 case glslang::EOpCos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005483 libCall = spv::GLSLstd450Cos;
John Kessenich140f3df2015-06-26 16:58:36 -06005484 break;
5485 case glslang::EOpTan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005486 libCall = spv::GLSLstd450Tan;
John Kessenich140f3df2015-06-26 16:58:36 -06005487 break;
5488 case glslang::EOpAcos:
John Kessenich5e4b1242015-08-06 22:53:06 -06005489 libCall = spv::GLSLstd450Acos;
John Kessenich140f3df2015-06-26 16:58:36 -06005490 break;
5491 case glslang::EOpAsin:
John Kessenich5e4b1242015-08-06 22:53:06 -06005492 libCall = spv::GLSLstd450Asin;
John Kessenich140f3df2015-06-26 16:58:36 -06005493 break;
5494 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06005495 libCall = spv::GLSLstd450Atan;
John Kessenich140f3df2015-06-26 16:58:36 -06005496 break;
5497
5498 case glslang::EOpAcosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005499 libCall = spv::GLSLstd450Acosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005500 break;
5501 case glslang::EOpAsinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005502 libCall = spv::GLSLstd450Asinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005503 break;
5504 case glslang::EOpAtanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005505 libCall = spv::GLSLstd450Atanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005506 break;
5507 case glslang::EOpTanh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005508 libCall = spv::GLSLstd450Tanh;
John Kessenich140f3df2015-06-26 16:58:36 -06005509 break;
5510 case glslang::EOpCosh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005511 libCall = spv::GLSLstd450Cosh;
John Kessenich140f3df2015-06-26 16:58:36 -06005512 break;
5513 case glslang::EOpSinh:
John Kessenich5e4b1242015-08-06 22:53:06 -06005514 libCall = spv::GLSLstd450Sinh;
John Kessenich140f3df2015-06-26 16:58:36 -06005515 break;
5516
5517 case glslang::EOpLength:
John Kessenich5e4b1242015-08-06 22:53:06 -06005518 libCall = spv::GLSLstd450Length;
John Kessenich140f3df2015-06-26 16:58:36 -06005519 break;
5520 case glslang::EOpNormalize:
John Kessenich5e4b1242015-08-06 22:53:06 -06005521 libCall = spv::GLSLstd450Normalize;
John Kessenich140f3df2015-06-26 16:58:36 -06005522 break;
5523
5524 case glslang::EOpExp:
John Kessenich5e4b1242015-08-06 22:53:06 -06005525 libCall = spv::GLSLstd450Exp;
John Kessenich140f3df2015-06-26 16:58:36 -06005526 break;
5527 case glslang::EOpLog:
John Kessenich5e4b1242015-08-06 22:53:06 -06005528 libCall = spv::GLSLstd450Log;
John Kessenich140f3df2015-06-26 16:58:36 -06005529 break;
5530 case glslang::EOpExp2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005531 libCall = spv::GLSLstd450Exp2;
John Kessenich140f3df2015-06-26 16:58:36 -06005532 break;
5533 case glslang::EOpLog2:
John Kessenich5e4b1242015-08-06 22:53:06 -06005534 libCall = spv::GLSLstd450Log2;
John Kessenich140f3df2015-06-26 16:58:36 -06005535 break;
5536 case glslang::EOpSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005537 libCall = spv::GLSLstd450Sqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005538 break;
5539 case glslang::EOpInverseSqrt:
John Kessenich5e4b1242015-08-06 22:53:06 -06005540 libCall = spv::GLSLstd450InverseSqrt;
John Kessenich140f3df2015-06-26 16:58:36 -06005541 break;
5542
5543 case glslang::EOpFloor:
John Kessenich5e4b1242015-08-06 22:53:06 -06005544 libCall = spv::GLSLstd450Floor;
John Kessenich140f3df2015-06-26 16:58:36 -06005545 break;
5546 case glslang::EOpTrunc:
John Kessenich5e4b1242015-08-06 22:53:06 -06005547 libCall = spv::GLSLstd450Trunc;
John Kessenich140f3df2015-06-26 16:58:36 -06005548 break;
5549 case glslang::EOpRound:
John Kessenich5e4b1242015-08-06 22:53:06 -06005550 libCall = spv::GLSLstd450Round;
John Kessenich140f3df2015-06-26 16:58:36 -06005551 break;
5552 case glslang::EOpRoundEven:
John Kessenich5e4b1242015-08-06 22:53:06 -06005553 libCall = spv::GLSLstd450RoundEven;
John Kessenich140f3df2015-06-26 16:58:36 -06005554 break;
5555 case glslang::EOpCeil:
John Kessenich5e4b1242015-08-06 22:53:06 -06005556 libCall = spv::GLSLstd450Ceil;
John Kessenich140f3df2015-06-26 16:58:36 -06005557 break;
5558 case glslang::EOpFract:
John Kessenich5e4b1242015-08-06 22:53:06 -06005559 libCall = spv::GLSLstd450Fract;
John Kessenich140f3df2015-06-26 16:58:36 -06005560 break;
5561
5562 case glslang::EOpIsNan:
5563 unaryOp = spv::OpIsNan;
5564 break;
5565 case glslang::EOpIsInf:
5566 unaryOp = spv::OpIsInf;
5567 break;
LoopDawg592860c2016-06-09 08:57:35 -06005568 case glslang::EOpIsFinite:
5569 unaryOp = spv::OpIsFinite;
5570 break;
John Kessenich140f3df2015-06-26 16:58:36 -06005571
Rex Xucbc426e2015-12-15 16:03:10 +08005572 case glslang::EOpFloatBitsToInt:
5573 case glslang::EOpFloatBitsToUint:
5574 case glslang::EOpIntBitsToFloat:
5575 case glslang::EOpUintBitsToFloat:
Rex Xu8ff43de2016-04-22 16:51:45 +08005576 case glslang::EOpDoubleBitsToInt64:
5577 case glslang::EOpDoubleBitsToUint64:
5578 case glslang::EOpInt64BitsToDouble:
5579 case glslang::EOpUint64BitsToDouble:
Rex Xucabbb782017-03-24 13:41:14 +08005580 case glslang::EOpFloat16BitsToInt16:
5581 case glslang::EOpFloat16BitsToUint16:
5582 case glslang::EOpInt16BitsToFloat16:
5583 case glslang::EOpUint16BitsToFloat16:
Rex Xucbc426e2015-12-15 16:03:10 +08005584 unaryOp = spv::OpBitcast;
5585 break;
5586
John Kessenich140f3df2015-06-26 16:58:36 -06005587 case glslang::EOpPackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005588 libCall = spv::GLSLstd450PackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005589 break;
5590 case glslang::EOpUnpackSnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005591 libCall = spv::GLSLstd450UnpackSnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005592 break;
5593 case glslang::EOpPackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005594 libCall = spv::GLSLstd450PackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005595 break;
5596 case glslang::EOpUnpackUnorm2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005597 libCall = spv::GLSLstd450UnpackUnorm2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005598 break;
5599 case glslang::EOpPackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005600 libCall = spv::GLSLstd450PackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005601 break;
5602 case glslang::EOpUnpackHalf2x16:
John Kessenich5e4b1242015-08-06 22:53:06 -06005603 libCall = spv::GLSLstd450UnpackHalf2x16;
John Kessenich140f3df2015-06-26 16:58:36 -06005604 break;
John Kessenichb9197c82019-08-11 07:41:45 -06005605#ifndef GLSLANG_WEB
John Kessenichfc51d282015-08-19 13:34:18 -06005606 case glslang::EOpPackSnorm4x8:
5607 libCall = spv::GLSLstd450PackSnorm4x8;
5608 break;
5609 case glslang::EOpUnpackSnorm4x8:
5610 libCall = spv::GLSLstd450UnpackSnorm4x8;
5611 break;
5612 case glslang::EOpPackUnorm4x8:
5613 libCall = spv::GLSLstd450PackUnorm4x8;
5614 break;
5615 case glslang::EOpUnpackUnorm4x8:
5616 libCall = spv::GLSLstd450UnpackUnorm4x8;
5617 break;
5618 case glslang::EOpPackDouble2x32:
5619 libCall = spv::GLSLstd450PackDouble2x32;
5620 break;
5621 case glslang::EOpUnpackDouble2x32:
5622 libCall = spv::GLSLstd450UnpackDouble2x32;
5623 break;
John Kessenichb9197c82019-08-11 07:41:45 -06005624#endif
John Kessenich140f3df2015-06-26 16:58:36 -06005625
Rex Xu8ff43de2016-04-22 16:51:45 +08005626 case glslang::EOpPackInt2x32:
5627 case glslang::EOpUnpackInt2x32:
5628 case glslang::EOpPackUint2x32:
5629 case glslang::EOpUnpackUint2x32:
John Kessenich66011cb2018-03-06 16:12:04 -07005630 case glslang::EOpPack16:
5631 case glslang::EOpPack32:
5632 case glslang::EOpPack64:
5633 case glslang::EOpUnpack32:
5634 case glslang::EOpUnpack16:
5635 case glslang::EOpUnpack8:
Rex Xucabbb782017-03-24 13:41:14 +08005636 case glslang::EOpPackInt2x16:
5637 case glslang::EOpUnpackInt2x16:
5638 case glslang::EOpPackUint2x16:
5639 case glslang::EOpUnpackUint2x16:
5640 case glslang::EOpPackInt4x16:
5641 case glslang::EOpUnpackInt4x16:
5642 case glslang::EOpPackUint4x16:
5643 case glslang::EOpUnpackUint4x16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005644 case glslang::EOpPackFloat2x16:
5645 case glslang::EOpUnpackFloat2x16:
5646 unaryOp = spv::OpBitcast;
5647 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005648
John Kessenich140f3df2015-06-26 16:58:36 -06005649 case glslang::EOpDPdx:
5650 unaryOp = spv::OpDPdx;
5651 break;
5652 case glslang::EOpDPdy:
5653 unaryOp = spv::OpDPdy;
5654 break;
5655 case glslang::EOpFwidth:
5656 unaryOp = spv::OpFwidth;
5657 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06005658
John Kessenich140f3df2015-06-26 16:58:36 -06005659 case glslang::EOpAny:
5660 unaryOp = spv::OpAny;
5661 break;
5662 case glslang::EOpAll:
5663 unaryOp = spv::OpAll;
5664 break;
5665
5666 case glslang::EOpAbs:
John Kessenich5e4b1242015-08-06 22:53:06 -06005667 if (isFloat)
5668 libCall = spv::GLSLstd450FAbs;
5669 else
5670 libCall = spv::GLSLstd450SAbs;
John Kessenich140f3df2015-06-26 16:58:36 -06005671 break;
5672 case glslang::EOpSign:
John Kessenich5e4b1242015-08-06 22:53:06 -06005673 if (isFloat)
5674 libCall = spv::GLSLstd450FSign;
5675 else
5676 libCall = spv::GLSLstd450SSign;
John Kessenich140f3df2015-06-26 16:58:36 -06005677 break;
5678
John Kessenicha28f7a72019-08-06 07:00:58 -06005679#ifndef GLSLANG_WEB
5680 case glslang::EOpDPdxFine:
5681 unaryOp = spv::OpDPdxFine;
5682 break;
5683 case glslang::EOpDPdyFine:
5684 unaryOp = spv::OpDPdyFine;
5685 break;
5686 case glslang::EOpFwidthFine:
5687 unaryOp = spv::OpFwidthFine;
5688 break;
5689 case glslang::EOpDPdxCoarse:
5690 unaryOp = spv::OpDPdxCoarse;
5691 break;
5692 case glslang::EOpDPdyCoarse:
5693 unaryOp = spv::OpDPdyCoarse;
5694 break;
5695 case glslang::EOpFwidthCoarse:
5696 unaryOp = spv::OpFwidthCoarse;
5697 break;
5698 case glslang::EOpInterpolateAtCentroid:
5699 if (typeProxy == glslang::EbtFloat16)
5700 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
5701 libCall = spv::GLSLstd450InterpolateAtCentroid;
5702 break;
John Kessenichfc51d282015-08-19 13:34:18 -06005703 case glslang::EOpAtomicCounterIncrement:
5704 case glslang::EOpAtomicCounterDecrement:
5705 case glslang::EOpAtomicCounter:
5706 {
5707 // Handle all of the atomics in one place, in createAtomicOperation()
5708 std::vector<spv::Id> operands;
5709 operands.push_back(operand);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05005710 return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags);
John Kessenichfc51d282015-08-19 13:34:18 -06005711 }
5712
John Kessenichfc51d282015-08-19 13:34:18 -06005713 case glslang::EOpBitFieldReverse:
5714 unaryOp = spv::OpBitReverse;
5715 break;
5716 case glslang::EOpBitCount:
5717 unaryOp = spv::OpBitCount;
5718 break;
5719 case glslang::EOpFindLSB:
John Kessenich55e7d112015-11-15 21:33:39 -07005720 libCall = spv::GLSLstd450FindILsb;
John Kessenichfc51d282015-08-19 13:34:18 -06005721 break;
5722 case glslang::EOpFindMSB:
John Kessenich55e7d112015-11-15 21:33:39 -07005723 if (isUnsigned)
5724 libCall = spv::GLSLstd450FindUMsb;
5725 else
5726 libCall = spv::GLSLstd450FindSMsb;
John Kessenichfc51d282015-08-19 13:34:18 -06005727 break;
5728
Rex Xu574ab042016-04-14 16:53:07 +08005729 case glslang::EOpBallot:
5730 case glslang::EOpReadFirstInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08005731 case glslang::EOpAnyInvocation:
Rex Xu338b1852016-05-05 20:38:33 +08005732 case glslang::EOpAllInvocations:
Rex Xu338b1852016-05-05 20:38:33 +08005733 case glslang::EOpAllInvocationsEqual:
Rex Xu9d93a232016-05-05 12:30:44 +08005734 case glslang::EOpMinInvocations:
5735 case glslang::EOpMaxInvocations:
5736 case glslang::EOpAddInvocations:
5737 case glslang::EOpMinInvocationsNonUniform:
5738 case glslang::EOpMaxInvocationsNonUniform:
5739 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08005740 case glslang::EOpMinInvocationsInclusiveScan:
5741 case glslang::EOpMaxInvocationsInclusiveScan:
5742 case glslang::EOpAddInvocationsInclusiveScan:
5743 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
5744 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
5745 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
5746 case glslang::EOpMinInvocationsExclusiveScan:
5747 case glslang::EOpMaxInvocationsExclusiveScan:
5748 case glslang::EOpAddInvocationsExclusiveScan:
5749 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
5750 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
5751 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
Rex Xu51596642016-09-21 18:56:12 +08005752 {
5753 std::vector<spv::Id> operands;
5754 operands.push_back(operand);
5755 return createInvocationsOperation(op, typeId, operands, typeProxy);
5756 }
John Kessenich66011cb2018-03-06 16:12:04 -07005757 case glslang::EOpSubgroupAll:
5758 case glslang::EOpSubgroupAny:
5759 case glslang::EOpSubgroupAllEqual:
5760 case glslang::EOpSubgroupBroadcastFirst:
5761 case glslang::EOpSubgroupBallot:
5762 case glslang::EOpSubgroupInverseBallot:
5763 case glslang::EOpSubgroupBallotBitCount:
5764 case glslang::EOpSubgroupBallotInclusiveBitCount:
5765 case glslang::EOpSubgroupBallotExclusiveBitCount:
5766 case glslang::EOpSubgroupBallotFindLSB:
5767 case glslang::EOpSubgroupBallotFindMSB:
5768 case glslang::EOpSubgroupAdd:
5769 case glslang::EOpSubgroupMul:
5770 case glslang::EOpSubgroupMin:
5771 case glslang::EOpSubgroupMax:
5772 case glslang::EOpSubgroupAnd:
5773 case glslang::EOpSubgroupOr:
5774 case glslang::EOpSubgroupXor:
5775 case glslang::EOpSubgroupInclusiveAdd:
5776 case glslang::EOpSubgroupInclusiveMul:
5777 case glslang::EOpSubgroupInclusiveMin:
5778 case glslang::EOpSubgroupInclusiveMax:
5779 case glslang::EOpSubgroupInclusiveAnd:
5780 case glslang::EOpSubgroupInclusiveOr:
5781 case glslang::EOpSubgroupInclusiveXor:
5782 case glslang::EOpSubgroupExclusiveAdd:
5783 case glslang::EOpSubgroupExclusiveMul:
5784 case glslang::EOpSubgroupExclusiveMin:
5785 case glslang::EOpSubgroupExclusiveMax:
5786 case glslang::EOpSubgroupExclusiveAnd:
5787 case glslang::EOpSubgroupExclusiveOr:
5788 case glslang::EOpSubgroupExclusiveXor:
5789 case glslang::EOpSubgroupQuadSwapHorizontal:
5790 case glslang::EOpSubgroupQuadSwapVertical:
5791 case glslang::EOpSubgroupQuadSwapDiagonal: {
5792 std::vector<spv::Id> operands;
5793 operands.push_back(operand);
5794 return createSubgroupOperation(op, typeId, operands, typeProxy);
5795 }
Rex Xu9d93a232016-05-05 12:30:44 +08005796 case glslang::EOpMbcnt:
5797 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
5798 libCall = spv::MbcntAMD;
5799 break;
5800
5801 case glslang::EOpCubeFaceIndex:
5802 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5803 libCall = spv::CubeFaceIndexAMD;
5804 break;
5805
5806 case glslang::EOpCubeFaceCoord:
5807 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader);
5808 libCall = spv::CubeFaceCoordAMD;
5809 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05005810 case glslang::EOpSubgroupPartition:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05005811 unaryOp = spv::OpGroupNonUniformPartitionNV;
5812 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06005813 case glslang::EOpConstructReference:
5814 unaryOp = spv::OpBitcast;
5815 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06005816#endif
Jeff Bolz88220d52019-05-08 10:24:46 -05005817
5818 case glslang::EOpCopyObject:
5819 unaryOp = spv::OpCopyObject;
5820 break;
5821
John Kessenich140f3df2015-06-26 16:58:36 -06005822 default:
5823 return 0;
5824 }
5825
5826 spv::Id id;
5827 if (libCall >= 0) {
5828 std::vector<spv::Id> args;
5829 args.push_back(operand);
Rex Xu9d93a232016-05-05 12:30:44 +08005830 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args);
Rex Xu338b1852016-05-05 20:38:33 +08005831 } else {
John Kessenich91cef522016-05-05 16:45:40 -06005832 id = builder.createUnaryOp(unaryOp, typeId, operand);
Rex Xu338b1852016-05-05 20:38:33 +08005833 }
John Kessenich140f3df2015-06-26 16:58:36 -06005834
John Kessenichb9197c82019-08-11 07:41:45 -06005835 decorations.addNoContraction(builder, id);
5836 decorations.addNonUniform(builder, id);
John Kessenichead86222018-03-28 18:01:20 -06005837 return builder.setPrecision(id, decorations.precision);
John Kessenich140f3df2015-06-26 16:58:36 -06005838}
5839
John Kessenich7a53f762016-01-20 11:19:27 -07005840// Create a unary operation on a matrix
John Kessenichead86222018-03-28 18:01:20 -06005841spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
5842 spv::Id operand, glslang::TBasicType /* typeProxy */)
John Kessenich7a53f762016-01-20 11:19:27 -07005843{
5844 // Handle unary operations vector by vector.
5845 // The result type is the same type as the original type.
5846 // The algorithm is to:
5847 // - break the matrix into vectors
5848 // - apply the operation to each vector
5849 // - make a matrix out the vector results
5850
5851 // get the types sorted out
5852 int numCols = builder.getNumColumns(operand);
5853 int numRows = builder.getNumRows(operand);
Rex Xuc1992e52016-05-17 18:57:18 +08005854 spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows);
5855 spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows);
John Kessenich7a53f762016-01-20 11:19:27 -07005856 std::vector<spv::Id> results;
5857
5858 // do each vector op
5859 for (int c = 0; c < numCols; ++c) {
5860 std::vector<unsigned int> indexes;
5861 indexes.push_back(c);
Rex Xuc1992e52016-05-17 18:57:18 +08005862 spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes);
5863 spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec);
John Kessenichb9197c82019-08-11 07:41:45 -06005864 decorations.addNoContraction(builder, destVec);
5865 decorations.addNonUniform(builder, destVec);
John Kessenichead86222018-03-28 18:01:20 -06005866 results.push_back(builder.setPrecision(destVec, decorations.precision));
John Kessenich7a53f762016-01-20 11:19:27 -07005867 }
5868
5869 // put the pieces together
John Kessenichead86222018-03-28 18:01:20 -06005870 spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06005871 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06005872 return result;
John Kessenich7a53f762016-01-20 11:19:27 -07005873}
5874
John Kessenichad7645f2018-06-04 19:11:25 -06005875// For converting integers where both the bitwidth and the signedness could
5876// change, but only do the width change here. The caller is still responsible
5877// for the signedness conversion.
5878spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize)
John Kessenich66011cb2018-03-06 16:12:04 -07005879{
John Kessenichad7645f2018-06-04 19:11:25 -06005880 // Get the result type width, based on the type to convert to.
5881 int width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07005882 switch(op) {
John Kessenichad7645f2018-06-04 19:11:25 -06005883 case glslang::EOpConvInt16ToUint8:
5884 case glslang::EOpConvIntToUint8:
5885 case glslang::EOpConvInt64ToUint8:
5886 case glslang::EOpConvUint16ToInt8:
5887 case glslang::EOpConvUintToInt8:
5888 case glslang::EOpConvUint64ToInt8:
5889 width = 8;
5890 break;
John Kessenich66011cb2018-03-06 16:12:04 -07005891 case glslang::EOpConvInt8ToUint16:
John Kessenichad7645f2018-06-04 19:11:25 -06005892 case glslang::EOpConvIntToUint16:
5893 case glslang::EOpConvInt64ToUint16:
5894 case glslang::EOpConvUint8ToInt16:
5895 case glslang::EOpConvUintToInt16:
5896 case glslang::EOpConvUint64ToInt16:
5897 width = 16;
John Kessenich66011cb2018-03-06 16:12:04 -07005898 break;
5899 case glslang::EOpConvInt8ToUint:
John Kessenichad7645f2018-06-04 19:11:25 -06005900 case glslang::EOpConvInt16ToUint:
5901 case glslang::EOpConvInt64ToUint:
5902 case glslang::EOpConvUint8ToInt:
5903 case glslang::EOpConvUint16ToInt:
5904 case glslang::EOpConvUint64ToInt:
5905 width = 32;
John Kessenich66011cb2018-03-06 16:12:04 -07005906 break;
5907 case glslang::EOpConvInt8ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005908 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005909 case glslang::EOpConvIntToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07005910 case glslang::EOpConvUint8ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005911 case glslang::EOpConvUint16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07005912 case glslang::EOpConvUintToInt64:
John Kessenichad7645f2018-06-04 19:11:25 -06005913 width = 64;
John Kessenich66011cb2018-03-06 16:12:04 -07005914 break;
5915
5916 default:
5917 assert(false && "Default missing");
5918 break;
5919 }
5920
John Kessenichad7645f2018-06-04 19:11:25 -06005921 // Get the conversion operation and result type,
5922 // based on the target width, but the source type.
5923 spv::Id type = spv::NoType;
5924 spv::Op convOp = spv::OpNop;
5925 switch(op) {
5926 case glslang::EOpConvInt8ToUint16:
5927 case glslang::EOpConvInt8ToUint:
5928 case glslang::EOpConvInt8ToUint64:
5929 case glslang::EOpConvInt16ToUint8:
5930 case glslang::EOpConvInt16ToUint:
5931 case glslang::EOpConvInt16ToUint64:
5932 case glslang::EOpConvIntToUint8:
5933 case glslang::EOpConvIntToUint16:
5934 case glslang::EOpConvIntToUint64:
5935 case glslang::EOpConvInt64ToUint8:
5936 case glslang::EOpConvInt64ToUint16:
5937 case glslang::EOpConvInt64ToUint:
5938 convOp = spv::OpSConvert;
5939 type = builder.makeIntType(width);
5940 break;
5941 default:
5942 convOp = spv::OpUConvert;
5943 type = builder.makeUintType(width);
5944 break;
5945 }
5946
John Kessenich66011cb2018-03-06 16:12:04 -07005947 if (vectorSize > 0)
5948 type = builder.makeVectorType(type, vectorSize);
5949
John Kessenichad7645f2018-06-04 19:11:25 -06005950 return builder.createUnaryOp(convOp, type, operand);
John Kessenich66011cb2018-03-06 16:12:04 -07005951}
5952
John Kessenichead86222018-03-28 18:01:20 -06005953spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
5954 spv::Id operand, glslang::TBasicType typeProxy)
John Kessenich140f3df2015-06-26 16:58:36 -06005955{
5956 spv::Op convOp = spv::OpNop;
5957 spv::Id zero = 0;
5958 spv::Id one = 0;
5959
5960 int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0;
5961
5962 switch (op) {
John Kessenich66011cb2018-03-06 16:12:04 -07005963 case glslang::EOpConvIntToBool:
5964 case glslang::EOpConvUintToBool:
5965 zero = builder.makeUintConstant(0);
5966 zero = makeSmearedConstant(zero, vectorSize);
5967 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06005968 case glslang::EOpConvFloatToBool:
5969 zero = builder.makeFloatConstant(0.0F);
5970 zero = makeSmearedConstant(zero, vectorSize);
5971 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
John Kessenich140f3df2015-06-26 16:58:36 -06005972 case glslang::EOpConvBoolToFloat:
5973 convOp = spv::OpSelect;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005974 zero = builder.makeFloatConstant(0.0F);
5975 one = builder.makeFloatConstant(1.0F);
John Kessenich140f3df2015-06-26 16:58:36 -06005976 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005977
John Kessenich140f3df2015-06-26 16:58:36 -06005978 case glslang::EOpConvBoolToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08005979 case glslang::EOpConvBoolToInt64:
John Kessenichb9197c82019-08-11 07:41:45 -06005980#ifndef GLSLANG_WEB
5981 if (op == glslang::EOpConvBoolToInt64) {
Rex Xucabbb782017-03-24 13:41:14 +08005982 zero = builder.makeInt64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08005983 one = builder.makeInt64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06005984 } else
5985#endif
5986 {
5987 zero = builder.makeIntConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08005988 one = builder.makeIntConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06005989 }
Rex Xucabbb782017-03-24 13:41:14 +08005990
John Kessenich140f3df2015-06-26 16:58:36 -06005991 convOp = spv::OpSelect;
5992 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08005993
John Kessenich140f3df2015-06-26 16:58:36 -06005994 case glslang::EOpConvBoolToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08005995 case glslang::EOpConvBoolToUint64:
John Kessenichb9197c82019-08-11 07:41:45 -06005996#ifndef GLSLANG_WEB
5997 if (op == glslang::EOpConvBoolToUint64) {
Rex Xucabbb782017-03-24 13:41:14 +08005998 zero = builder.makeUint64Constant(0);
Rex Xucabbb782017-03-24 13:41:14 +08005999 one = builder.makeUint64Constant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006000 } else
6001#endif
6002 {
6003 zero = builder.makeUintConstant(0);
Rex Xucabbb782017-03-24 13:41:14 +08006004 one = builder.makeUintConstant(1);
John Kessenichb9197c82019-08-11 07:41:45 -06006005 }
Rex Xucabbb782017-03-24 13:41:14 +08006006
John Kessenich140f3df2015-06-26 16:58:36 -06006007 convOp = spv::OpSelect;
6008 break;
6009
John Kessenich66011cb2018-03-06 16:12:04 -07006010 case glslang::EOpConvInt8ToFloat16:
6011 case glslang::EOpConvInt8ToFloat:
6012 case glslang::EOpConvInt8ToDouble:
6013 case glslang::EOpConvInt16ToFloat16:
6014 case glslang::EOpConvInt16ToFloat:
6015 case glslang::EOpConvInt16ToDouble:
6016 case glslang::EOpConvIntToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006017 case glslang::EOpConvIntToFloat:
6018 case glslang::EOpConvIntToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006019 case glslang::EOpConvInt64ToFloat:
6020 case glslang::EOpConvInt64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006021 case glslang::EOpConvInt64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006022 convOp = spv::OpConvertSToF;
6023 break;
6024
John Kessenich66011cb2018-03-06 16:12:04 -07006025 case glslang::EOpConvUint8ToFloat16:
6026 case glslang::EOpConvUint8ToFloat:
6027 case glslang::EOpConvUint8ToDouble:
6028 case glslang::EOpConvUint16ToFloat16:
6029 case glslang::EOpConvUint16ToFloat:
6030 case glslang::EOpConvUint16ToDouble:
6031 case glslang::EOpConvUintToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006032 case glslang::EOpConvUintToFloat:
6033 case glslang::EOpConvUintToDouble:
Rex Xu8ff43de2016-04-22 16:51:45 +08006034 case glslang::EOpConvUint64ToFloat:
6035 case glslang::EOpConvUint64ToDouble:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006036 case glslang::EOpConvUint64ToFloat16:
John Kessenich140f3df2015-06-26 16:58:36 -06006037 convOp = spv::OpConvertUToF;
6038 break;
6039
John Kessenich66011cb2018-03-06 16:12:04 -07006040 case glslang::EOpConvFloat16ToInt8:
6041 case glslang::EOpConvFloatToInt8:
6042 case glslang::EOpConvDoubleToInt8:
6043 case glslang::EOpConvFloat16ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006044 case glslang::EOpConvFloatToInt16:
6045 case glslang::EOpConvDoubleToInt16:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006046 case glslang::EOpConvFloat16ToInt:
John Kessenich66011cb2018-03-06 16:12:04 -07006047 case glslang::EOpConvFloatToInt:
6048 case glslang::EOpConvDoubleToInt:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006049 case glslang::EOpConvFloat16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006050 case glslang::EOpConvFloatToInt64:
6051 case glslang::EOpConvDoubleToInt64:
John Kessenich140f3df2015-06-26 16:58:36 -06006052 convOp = spv::OpConvertFToS;
6053 break;
6054
John Kessenich66011cb2018-03-06 16:12:04 -07006055 case glslang::EOpConvUint8ToInt8:
6056 case glslang::EOpConvInt8ToUint8:
6057 case glslang::EOpConvUint16ToInt16:
6058 case glslang::EOpConvInt16ToUint16:
John Kessenich140f3df2015-06-26 16:58:36 -06006059 case glslang::EOpConvUintToInt:
6060 case glslang::EOpConvIntToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006061 case glslang::EOpConvUint64ToInt64:
6062 case glslang::EOpConvInt64ToUint64:
qininge24aa5e2016-04-07 15:40:27 -04006063 if (builder.isInSpecConstCodeGenMode()) {
6064 // Build zero scalar or vector for OpIAdd.
John Kessenich39697cd2019-08-08 10:35:51 -06006065#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07006066 if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) {
6067 zero = builder.makeUint8Constant(0);
6068 } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) {
Rex Xucabbb782017-03-24 13:41:14 +08006069 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006070 } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) {
6071 zero = builder.makeUint64Constant(0);
John Kessenich39697cd2019-08-08 10:35:51 -06006072 } else
6073#endif
6074 {
Rex Xucabbb782017-03-24 13:41:14 +08006075 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006076 }
qining189b2032016-04-12 23:16:20 -04006077 zero = makeSmearedConstant(zero, vectorSize);
qininge24aa5e2016-04-07 15:40:27 -04006078 // Use OpIAdd, instead of OpBitcast to do the conversion when
6079 // generating for OpSpecConstantOp instruction.
6080 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6081 }
6082 // For normal run-time conversion instruction, use OpBitcast.
John Kessenich140f3df2015-06-26 16:58:36 -06006083 convOp = spv::OpBitcast;
6084 break;
6085
John Kessenich66011cb2018-03-06 16:12:04 -07006086 case glslang::EOpConvFloat16ToUint8:
6087 case glslang::EOpConvFloatToUint8:
6088 case glslang::EOpConvDoubleToUint8:
6089 case glslang::EOpConvFloat16ToUint16:
6090 case glslang::EOpConvFloatToUint16:
6091 case glslang::EOpConvDoubleToUint16:
6092 case glslang::EOpConvFloat16ToUint:
John Kessenich140f3df2015-06-26 16:58:36 -06006093 case glslang::EOpConvFloatToUint:
6094 case glslang::EOpConvDoubleToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006095 case glslang::EOpConvFloatToUint64:
6096 case glslang::EOpConvDoubleToUint64:
Rex Xuc9e3c3c2016-07-29 16:00:05 +08006097 case glslang::EOpConvFloat16ToUint64:
John Kessenich140f3df2015-06-26 16:58:36 -06006098 convOp = spv::OpConvertFToU;
6099 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08006100
John Kessenich39697cd2019-08-08 10:35:51 -06006101#ifndef GLSLANG_WEB
6102 case glslang::EOpConvInt8ToBool:
6103 case glslang::EOpConvUint8ToBool:
6104 zero = builder.makeUint8Constant(0);
6105 zero = makeSmearedConstant(zero, vectorSize);
6106 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6107 case glslang::EOpConvInt16ToBool:
6108 case glslang::EOpConvUint16ToBool:
6109 zero = builder.makeUint16Constant(0);
6110 zero = makeSmearedConstant(zero, vectorSize);
6111 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6112 case glslang::EOpConvInt64ToBool:
6113 case glslang::EOpConvUint64ToBool:
6114 zero = builder.makeUint64Constant(0);
6115 zero = makeSmearedConstant(zero, vectorSize);
6116 return builder.createBinOp(spv::OpINotEqual, destType, operand, zero);
6117 case glslang::EOpConvDoubleToBool:
6118 zero = builder.makeDoubleConstant(0.0);
6119 zero = makeSmearedConstant(zero, vectorSize);
6120 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
6121 case glslang::EOpConvFloat16ToBool:
6122 zero = builder.makeFloat16Constant(0.0F);
6123 zero = makeSmearedConstant(zero, vectorSize);
6124 return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero);
6125 case glslang::EOpConvBoolToDouble:
6126 convOp = spv::OpSelect;
6127 zero = builder.makeDoubleConstant(0.0);
6128 one = builder.makeDoubleConstant(1.0);
6129 break;
6130 case glslang::EOpConvBoolToFloat16:
6131 convOp = spv::OpSelect;
6132 zero = builder.makeFloat16Constant(0.0F);
6133 one = builder.makeFloat16Constant(1.0F);
6134 break;
6135 case glslang::EOpConvBoolToInt8:
6136 zero = builder.makeInt8Constant(0);
6137 one = builder.makeInt8Constant(1);
6138 convOp = spv::OpSelect;
6139 break;
6140 case glslang::EOpConvBoolToUint8:
6141 zero = builder.makeUint8Constant(0);
6142 one = builder.makeUint8Constant(1);
6143 convOp = spv::OpSelect;
6144 break;
6145 case glslang::EOpConvBoolToInt16:
6146 zero = builder.makeInt16Constant(0);
6147 one = builder.makeInt16Constant(1);
6148 convOp = spv::OpSelect;
6149 break;
6150 case glslang::EOpConvBoolToUint16:
6151 zero = builder.makeUint16Constant(0);
6152 one = builder.makeUint16Constant(1);
6153 convOp = spv::OpSelect;
6154 break;
6155 case glslang::EOpConvDoubleToFloat:
6156 case glslang::EOpConvFloatToDouble:
6157 case glslang::EOpConvDoubleToFloat16:
6158 case glslang::EOpConvFloat16ToDouble:
6159 case glslang::EOpConvFloatToFloat16:
6160 case glslang::EOpConvFloat16ToFloat:
6161 convOp = spv::OpFConvert;
6162 if (builder.isMatrixType(destType))
6163 return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy);
6164 break;
6165
John Kessenich66011cb2018-03-06 16:12:04 -07006166 case glslang::EOpConvInt8ToInt16:
6167 case glslang::EOpConvInt8ToInt:
6168 case glslang::EOpConvInt8ToInt64:
6169 case glslang::EOpConvInt16ToInt8:
Rex Xucabbb782017-03-24 13:41:14 +08006170 case glslang::EOpConvInt16ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006171 case glslang::EOpConvInt16ToInt64:
John Kessenich66011cb2018-03-06 16:12:04 -07006172 case glslang::EOpConvIntToInt8:
6173 case glslang::EOpConvIntToInt16:
6174 case glslang::EOpConvIntToInt64:
6175 case glslang::EOpConvInt64ToInt8:
6176 case glslang::EOpConvInt64ToInt16:
6177 case glslang::EOpConvInt64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006178 convOp = spv::OpSConvert;
6179 break;
6180
John Kessenich66011cb2018-03-06 16:12:04 -07006181 case glslang::EOpConvUint8ToUint16:
6182 case glslang::EOpConvUint8ToUint:
6183 case glslang::EOpConvUint8ToUint64:
6184 case glslang::EOpConvUint16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006185 case glslang::EOpConvUint16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006186 case glslang::EOpConvUint16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006187 case glslang::EOpConvUintToUint8:
6188 case glslang::EOpConvUintToUint16:
6189 case glslang::EOpConvUintToUint64:
6190 case glslang::EOpConvUint64ToUint8:
6191 case glslang::EOpConvUint64ToUint16:
6192 case glslang::EOpConvUint64ToUint:
Rex Xu8ff43de2016-04-22 16:51:45 +08006193 convOp = spv::OpUConvert;
6194 break;
6195
John Kessenich66011cb2018-03-06 16:12:04 -07006196 case glslang::EOpConvInt8ToUint16:
6197 case glslang::EOpConvInt8ToUint:
6198 case glslang::EOpConvInt8ToUint64:
6199 case glslang::EOpConvInt16ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006200 case glslang::EOpConvInt16ToUint:
Rex Xucabbb782017-03-24 13:41:14 +08006201 case glslang::EOpConvInt16ToUint64:
John Kessenich66011cb2018-03-06 16:12:04 -07006202 case glslang::EOpConvIntToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006203 case glslang::EOpConvIntToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006204 case glslang::EOpConvIntToUint64:
6205 case glslang::EOpConvInt64ToUint8:
Rex Xucabbb782017-03-24 13:41:14 +08006206 case glslang::EOpConvInt64ToUint16:
John Kessenich66011cb2018-03-06 16:12:04 -07006207 case glslang::EOpConvInt64ToUint:
6208 case glslang::EOpConvUint8ToInt16:
6209 case glslang::EOpConvUint8ToInt:
6210 case glslang::EOpConvUint8ToInt64:
6211 case glslang::EOpConvUint16ToInt8:
6212 case glslang::EOpConvUint16ToInt:
6213 case glslang::EOpConvUint16ToInt64:
6214 case glslang::EOpConvUintToInt8:
6215 case glslang::EOpConvUintToInt16:
6216 case glslang::EOpConvUintToInt64:
6217 case glslang::EOpConvUint64ToInt8:
6218 case glslang::EOpConvUint64ToInt16:
6219 case glslang::EOpConvUint64ToInt:
Rex Xu8ff43de2016-04-22 16:51:45 +08006220 // OpSConvert/OpUConvert + OpBitCast
John Kessenichad7645f2018-06-04 19:11:25 -06006221 operand = createIntWidthConversion(op, operand, vectorSize);
Rex Xu8ff43de2016-04-22 16:51:45 +08006222
6223 if (builder.isInSpecConstCodeGenMode()) {
6224 // Build zero scalar or vector for OpIAdd.
John Kessenich66011cb2018-03-06 16:12:04 -07006225 switch(op) {
6226 case glslang::EOpConvInt16ToUint8:
6227 case glslang::EOpConvIntToUint8:
6228 case glslang::EOpConvInt64ToUint8:
6229 case glslang::EOpConvUint16ToInt8:
6230 case glslang::EOpConvUintToInt8:
6231 case glslang::EOpConvUint64ToInt8:
6232 zero = builder.makeUint8Constant(0);
6233 break;
6234 case glslang::EOpConvInt8ToUint16:
6235 case glslang::EOpConvIntToUint16:
6236 case glslang::EOpConvInt64ToUint16:
6237 case glslang::EOpConvUint8ToInt16:
6238 case glslang::EOpConvUintToInt16:
6239 case glslang::EOpConvUint64ToInt16:
Rex Xucabbb782017-03-24 13:41:14 +08006240 zero = builder.makeUint16Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006241 break;
6242 case glslang::EOpConvInt8ToUint:
6243 case glslang::EOpConvInt16ToUint:
6244 case glslang::EOpConvInt64ToUint:
6245 case glslang::EOpConvUint8ToInt:
6246 case glslang::EOpConvUint16ToInt:
6247 case glslang::EOpConvUint64ToInt:
Rex Xucabbb782017-03-24 13:41:14 +08006248 zero = builder.makeUintConstant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006249 break;
6250 case glslang::EOpConvInt8ToUint64:
6251 case glslang::EOpConvInt16ToUint64:
6252 case glslang::EOpConvIntToUint64:
6253 case glslang::EOpConvUint8ToInt64:
6254 case glslang::EOpConvUint16ToInt64:
6255 case glslang::EOpConvUintToInt64:
Rex Xucabbb782017-03-24 13:41:14 +08006256 zero = builder.makeUint64Constant(0);
John Kessenich66011cb2018-03-06 16:12:04 -07006257 break;
6258 default:
6259 assert(false && "Default missing");
6260 break;
6261 }
Rex Xu8ff43de2016-04-22 16:51:45 +08006262 zero = makeSmearedConstant(zero, vectorSize);
6263 // Use OpIAdd, instead of OpBitcast to do the conversion when
6264 // generating for OpSpecConstantOp instruction.
6265 return builder.createBinOp(spv::OpIAdd, destType, operand, zero);
6266 }
6267 // For normal run-time conversion instruction, use OpBitcast.
6268 convOp = spv::OpBitcast;
6269 break;
Jeff Bolz9f2aec42019-01-06 17:58:04 -06006270 case glslang::EOpConvUint64ToPtr:
6271 convOp = spv::OpConvertUToPtr;
6272 break;
6273 case glslang::EOpConvPtrToUint64:
6274 convOp = spv::OpConvertPtrToU;
6275 break;
John Kessenich90e402f2019-09-17 23:19:38 -06006276 case glslang::EOpConvPtrToUvec2:
6277 case glslang::EOpConvUvec2ToPtr:
John Kessenichee8e9c12019-10-10 20:54:21 -06006278 if (builder.isVector(operand))
6279 builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer,
6280 spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5);
John Kessenich90e402f2019-09-17 23:19:38 -06006281 convOp = spv::OpBitcast;
6282 break;
John Kessenich39697cd2019-08-08 10:35:51 -06006283#endif
6284
John Kessenich140f3df2015-06-26 16:58:36 -06006285 default:
6286 break;
6287 }
6288
6289 spv::Id result = 0;
6290 if (convOp == spv::OpNop)
6291 return result;
6292
6293 if (convOp == spv::OpSelect) {
6294 zero = makeSmearedConstant(zero, vectorSize);
6295 one = makeSmearedConstant(one, vectorSize);
6296 result = builder.createTriOp(convOp, destType, operand, one, zero);
6297 } else
6298 result = builder.createUnaryOp(convOp, destType, operand);
6299
John Kessenichead86222018-03-28 18:01:20 -06006300 result = builder.setPrecision(result, decorations.precision);
John Kessenichb9197c82019-08-11 07:41:45 -06006301 decorations.addNonUniform(builder, result);
John Kessenichead86222018-03-28 18:01:20 -06006302 return result;
John Kessenich140f3df2015-06-26 16:58:36 -06006303}
6304
6305spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
6306{
6307 if (vectorSize == 0)
6308 return constant;
6309
6310 spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize);
6311 std::vector<spv::Id> components;
6312 for (int c = 0; c < vectorSize; ++c)
6313 components.push_back(constant);
6314 return builder.makeCompositeConstant(vectorTypeId, components);
6315}
6316
John Kessenich426394d2015-07-23 10:22:48 -06006317// For glslang ops that map to SPV atomic opCodes
Jeff Bolz38a52fc2019-06-14 09:56:28 -05006318spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
John Kessenich426394d2015-07-23 10:22:48 -06006319{
6320 spv::Op opCode = spv::OpNop;
6321
6322 switch (op) {
6323 case glslang::EOpAtomicAdd:
Rex Xufc618912015-09-09 16:42:49 +08006324 case glslang::EOpImageAtomicAdd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006325 case glslang::EOpAtomicCounterAdd:
John Kessenich426394d2015-07-23 10:22:48 -06006326 opCode = spv::OpAtomicIAdd;
6327 break;
John Kessenich0d0c6d32017-07-23 16:08:26 -06006328 case glslang::EOpAtomicCounterSubtract:
6329 opCode = spv::OpAtomicISub;
6330 break;
John Kessenich426394d2015-07-23 10:22:48 -06006331 case glslang::EOpAtomicMin:
Rex Xufc618912015-09-09 16:42:49 +08006332 case glslang::EOpImageAtomicMin:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006333 case glslang::EOpAtomicCounterMin:
Rex Xue8fe8b02017-09-26 15:42:56 +08006334 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin;
John Kessenich426394d2015-07-23 10:22:48 -06006335 break;
6336 case glslang::EOpAtomicMax:
Rex Xufc618912015-09-09 16:42:49 +08006337 case glslang::EOpImageAtomicMax:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006338 case glslang::EOpAtomicCounterMax:
Rex Xue8fe8b02017-09-26 15:42:56 +08006339 opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax;
John Kessenich426394d2015-07-23 10:22:48 -06006340 break;
6341 case glslang::EOpAtomicAnd:
Rex Xufc618912015-09-09 16:42:49 +08006342 case glslang::EOpImageAtomicAnd:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006343 case glslang::EOpAtomicCounterAnd:
John Kessenich426394d2015-07-23 10:22:48 -06006344 opCode = spv::OpAtomicAnd;
6345 break;
6346 case glslang::EOpAtomicOr:
Rex Xufc618912015-09-09 16:42:49 +08006347 case glslang::EOpImageAtomicOr:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006348 case glslang::EOpAtomicCounterOr:
John Kessenich426394d2015-07-23 10:22:48 -06006349 opCode = spv::OpAtomicOr;
6350 break;
6351 case glslang::EOpAtomicXor:
Rex Xufc618912015-09-09 16:42:49 +08006352 case glslang::EOpImageAtomicXor:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006353 case glslang::EOpAtomicCounterXor:
John Kessenich426394d2015-07-23 10:22:48 -06006354 opCode = spv::OpAtomicXor;
6355 break;
6356 case glslang::EOpAtomicExchange:
Rex Xufc618912015-09-09 16:42:49 +08006357 case glslang::EOpImageAtomicExchange:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006358 case glslang::EOpAtomicCounterExchange:
John Kessenich426394d2015-07-23 10:22:48 -06006359 opCode = spv::OpAtomicExchange;
6360 break;
6361 case glslang::EOpAtomicCompSwap:
Rex Xufc618912015-09-09 16:42:49 +08006362 case glslang::EOpImageAtomicCompSwap:
John Kessenich0d0c6d32017-07-23 16:08:26 -06006363 case glslang::EOpAtomicCounterCompSwap:
John Kessenich426394d2015-07-23 10:22:48 -06006364 opCode = spv::OpAtomicCompareExchange;
6365 break;
6366 case glslang::EOpAtomicCounterIncrement:
6367 opCode = spv::OpAtomicIIncrement;
6368 break;
6369 case glslang::EOpAtomicCounterDecrement:
6370 opCode = spv::OpAtomicIDecrement;
6371 break;
6372 case glslang::EOpAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05006373 case glslang::EOpImageAtomicLoad:
6374 case glslang::EOpAtomicLoad:
John Kessenich426394d2015-07-23 10:22:48 -06006375 opCode = spv::OpAtomicLoad;
6376 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05006377 case glslang::EOpAtomicStore:
6378 case glslang::EOpImageAtomicStore:
6379 opCode = spv::OpAtomicStore;
6380 break;
John Kessenich426394d2015-07-23 10:22:48 -06006381 default:
John Kessenich55e7d112015-11-15 21:33:39 -07006382 assert(0);
John Kessenich426394d2015-07-23 10:22:48 -06006383 break;
6384 }
6385
Rex Xue8fe8b02017-09-26 15:42:56 +08006386 if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64)
6387 builder.addCapability(spv::CapabilityInt64Atomics);
6388
John Kessenich426394d2015-07-23 10:22:48 -06006389 // Sort out the operands
6390 // - mapping from glslang -> SPV
Jeff Bolz36831c92018-09-05 10:11:41 -05006391 // - there are extra SPV operands that are optional in glslang
John Kessenich3e60a6f2015-09-14 22:45:16 -06006392 // - compare-exchange swaps the value and comparator
6393 // - compare-exchange has an extra memory semantics
John Kessenich48d6e792017-10-06 21:21:48 -06006394 // - EOpAtomicCounterDecrement needs a post decrement
Jeff Bolz36831c92018-09-05 10:11:41 -05006395 spv::Id pointerId = 0, compareId = 0, valueId = 0;
6396 // scope defaults to Device in the old model, QueueFamilyKHR in the new model
6397 spv::Id scopeId;
6398 if (glslangIntermediate->usingVulkanMemoryModel()) {
6399 scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR);
6400 } else {
6401 scopeId = builder.makeUintConstant(spv::ScopeDevice);
6402 }
6403 // semantics default to relaxed
Jeff Bolz016ddee2019-10-17 11:22:57 -05006404 spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && glslangIntermediate->usingVulkanMemoryModel() ?
John Kessenichb9197c82019-08-11 07:41:45 -06006405 spv::MemorySemanticsVolatileMask :
6406 spv::MemorySemanticsMaskNone);
Jeff Bolz36831c92018-09-05 10:11:41 -05006407 spv::Id semanticsId2 = semanticsId;
6408
6409 pointerId = operands[0];
6410 if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) {
6411 // no additional operands
6412 } else if (opCode == spv::OpAtomicCompareExchange) {
6413 compareId = operands[1];
6414 valueId = operands[2];
6415 if (operands.size() > 3) {
6416 scopeId = operands[3];
6417 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5]));
6418 semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7]));
6419 }
6420 } else if (opCode == spv::OpAtomicLoad) {
6421 if (operands.size() > 1) {
6422 scopeId = operands[1];
6423 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]));
6424 }
6425 } else {
6426 // atomic store or RMW
6427 valueId = operands[1];
6428 if (operands.size() > 2) {
6429 scopeId = operands[2];
6430 semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4]));
6431 }
Rex Xu04db3f52015-09-16 11:44:02 +08006432 }
John Kessenich426394d2015-07-23 10:22:48 -06006433
Jeff Bolz36831c92018-09-05 10:11:41 -05006434 // Check for capabilities
6435 unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05006436 if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask |
6437 spv::MemorySemanticsMakeVisibleKHRMask |
6438 spv::MemorySemanticsOutputMemoryKHRMask |
6439 spv::MemorySemanticsVolatileMask)) {
Jeff Bolz36831c92018-09-05 10:11:41 -05006440 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
6441 }
John Kessenich426394d2015-07-23 10:22:48 -06006442
Jeff Bolz36831c92018-09-05 10:11:41 -05006443 if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) {
6444 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
6445 }
John Kessenich48d6e792017-10-06 21:21:48 -06006446
Jeff Bolz36831c92018-09-05 10:11:41 -05006447 std::vector<spv::Id> spvAtomicOperands; // hold the spv operands
6448 spvAtomicOperands.push_back(pointerId);
6449 spvAtomicOperands.push_back(scopeId);
6450 spvAtomicOperands.push_back(semanticsId);
6451 if (opCode == spv::OpAtomicCompareExchange) {
6452 spvAtomicOperands.push_back(semanticsId2);
6453 spvAtomicOperands.push_back(valueId);
6454 spvAtomicOperands.push_back(compareId);
6455 } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) {
6456 spvAtomicOperands.push_back(valueId);
6457 }
John Kessenich48d6e792017-10-06 21:21:48 -06006458
Jeff Bolz36831c92018-09-05 10:11:41 -05006459 if (opCode == spv::OpAtomicStore) {
6460 builder.createNoResultOp(opCode, spvAtomicOperands);
6461 return 0;
6462 } else {
6463 spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands);
6464
6465 // GLSL and HLSL atomic-counter decrement return post-decrement value,
6466 // while SPIR-V returns pre-decrement value. Translate between these semantics.
6467 if (op == glslang::EOpAtomicCounterDecrement)
6468 resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1));
6469
6470 return resultId;
6471 }
John Kessenich426394d2015-07-23 10:22:48 -06006472}
6473
John Kessenich91cef522016-05-05 16:45:40 -06006474// Create group invocation operations.
Rex Xu51596642016-09-21 18:56:12 +08006475spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich91cef522016-05-05 16:45:40 -06006476{
John Kessenich66011cb2018-03-06 16:12:04 -07006477 bool isUnsigned = isTypeUnsignedInt(typeProxy);
6478 bool isFloat = isTypeFloat(typeProxy);
Rex Xu9d93a232016-05-05 12:30:44 +08006479
Rex Xu51596642016-09-21 18:56:12 +08006480 spv::Op opCode = spv::OpNop;
John Kessenich149afc32018-08-14 13:31:43 -06006481 std::vector<spv::IdImmediate> spvGroupOperands;
Rex Xu430ef402016-10-14 17:22:23 +08006482 spv::GroupOperation groupOperation = spv::GroupOperationMax;
6483
chaocf200da82016-12-20 12:44:35 -08006484 if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation ||
6485 op == glslang::EOpReadInvocation) {
Rex Xu51596642016-09-21 18:56:12 +08006486 builder.addExtension(spv::E_SPV_KHR_shader_ballot);
6487 builder.addCapability(spv::CapabilitySubgroupBallotKHR);
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006488 } else if (op == glslang::EOpAnyInvocation ||
6489 op == glslang::EOpAllInvocations ||
6490 op == glslang::EOpAllInvocationsEqual) {
6491 builder.addExtension(spv::E_SPV_KHR_subgroup_vote);
6492 builder.addCapability(spv::CapabilitySubgroupVoteKHR);
Rex Xu51596642016-09-21 18:56:12 +08006493 } else {
6494 builder.addCapability(spv::CapabilityGroups);
Rex Xu17ff3432016-10-14 17:41:45 +08006495 if (op == glslang::EOpMinInvocationsNonUniform ||
6496 op == glslang::EOpMaxInvocationsNonUniform ||
Rex Xu430ef402016-10-14 17:22:23 +08006497 op == glslang::EOpAddInvocationsNonUniform ||
6498 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6499 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6500 op == glslang::EOpAddInvocationsInclusiveScanNonUniform ||
6501 op == glslang::EOpMinInvocationsExclusiveScanNonUniform ||
6502 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform ||
6503 op == glslang::EOpAddInvocationsExclusiveScanNonUniform)
Rex Xu17ff3432016-10-14 17:41:45 +08006504 builder.addExtension(spv::E_SPV_AMD_shader_ballot);
Rex Xu51596642016-09-21 18:56:12 +08006505
Rex Xu430ef402016-10-14 17:22:23 +08006506 switch (op) {
6507 case glslang::EOpMinInvocations:
6508 case glslang::EOpMaxInvocations:
6509 case glslang::EOpAddInvocations:
6510 case glslang::EOpMinInvocationsNonUniform:
6511 case glslang::EOpMaxInvocationsNonUniform:
6512 case glslang::EOpAddInvocationsNonUniform:
6513 groupOperation = spv::GroupOperationReduce;
Rex Xu430ef402016-10-14 17:22:23 +08006514 break;
6515 case glslang::EOpMinInvocationsInclusiveScan:
6516 case glslang::EOpMaxInvocationsInclusiveScan:
6517 case glslang::EOpAddInvocationsInclusiveScan:
6518 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6519 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6520 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6521 groupOperation = spv::GroupOperationInclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006522 break;
6523 case glslang::EOpMinInvocationsExclusiveScan:
6524 case glslang::EOpMaxInvocationsExclusiveScan:
6525 case glslang::EOpAddInvocationsExclusiveScan:
6526 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6527 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6528 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6529 groupOperation = spv::GroupOperationExclusiveScan;
Rex Xu430ef402016-10-14 17:22:23 +08006530 break;
Mike Weiblen4e9e4002017-01-20 13:34:10 -07006531 default:
6532 break;
Rex Xu430ef402016-10-14 17:22:23 +08006533 }
John Kessenich149afc32018-08-14 13:31:43 -06006534 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6535 spvGroupOperands.push_back(scope);
6536 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06006537 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006538 spvGroupOperands.push_back(groupOp);
6539 }
Rex Xu51596642016-09-21 18:56:12 +08006540 }
6541
John Kessenich149afc32018-08-14 13:31:43 -06006542 for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) {
6543 spv::IdImmediate op = { true, *opIt };
6544 spvGroupOperands.push_back(op);
6545 }
John Kessenich91cef522016-05-05 16:45:40 -06006546
6547 switch (op) {
6548 case glslang::EOpAnyInvocation:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006549 opCode = spv::OpSubgroupAnyKHR;
Rex Xu51596642016-09-21 18:56:12 +08006550 break;
John Kessenich91cef522016-05-05 16:45:40 -06006551 case glslang::EOpAllInvocations:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006552 opCode = spv::OpSubgroupAllKHR;
Rex Xu51596642016-09-21 18:56:12 +08006553 break;
John Kessenich91cef522016-05-05 16:45:40 -06006554 case glslang::EOpAllInvocationsEqual:
Ashwin Kolhec720f3e2017-01-18 14:16:49 -08006555 opCode = spv::OpSubgroupAllEqualKHR;
6556 break;
Rex Xu51596642016-09-21 18:56:12 +08006557 case glslang::EOpReadInvocation:
chaocf200da82016-12-20 12:44:35 -08006558 opCode = spv::OpSubgroupReadInvocationKHR;
Rex Xub7072052016-09-26 15:53:40 +08006559 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006560 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006561 break;
6562 case glslang::EOpReadFirstInvocation:
6563 opCode = spv::OpSubgroupFirstInvocationKHR;
6564 break;
6565 case glslang::EOpBallot:
6566 {
6567 // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32
6568 // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in
6569 // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow:
6570 //
6571 // result = Bitcast(SubgroupBallotKHR(Predicate).xy)
6572 //
6573 spv::Id uintType = builder.makeUintType(32);
6574 spv::Id uvec4Type = builder.makeVectorType(uintType, 4);
6575 spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands);
6576
6577 std::vector<spv::Id> components;
6578 components.push_back(builder.createCompositeExtract(result, uintType, 0));
6579 components.push_back(builder.createCompositeExtract(result, uintType, 1));
6580
6581 spv::Id uvec2Type = builder.makeVectorType(uintType, 2);
6582 return builder.createUnaryOp(spv::OpBitcast, typeId,
6583 builder.createCompositeConstruct(uvec2Type, components));
6584 }
6585
Rex Xu9d93a232016-05-05 12:30:44 +08006586 case glslang::EOpMinInvocations:
6587 case glslang::EOpMaxInvocations:
6588 case glslang::EOpAddInvocations:
Rex Xu430ef402016-10-14 17:22:23 +08006589 case glslang::EOpMinInvocationsInclusiveScan:
6590 case glslang::EOpMaxInvocationsInclusiveScan:
6591 case glslang::EOpAddInvocationsInclusiveScan:
6592 case glslang::EOpMinInvocationsExclusiveScan:
6593 case glslang::EOpMaxInvocationsExclusiveScan:
6594 case glslang::EOpAddInvocationsExclusiveScan:
6595 if (op == glslang::EOpMinInvocations ||
6596 op == glslang::EOpMinInvocationsInclusiveScan ||
6597 op == glslang::EOpMinInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006598 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006599 opCode = spv::OpGroupFMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006600 else {
6601 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006602 opCode = spv::OpGroupUMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006603 else
Rex Xu51596642016-09-21 18:56:12 +08006604 opCode = spv::OpGroupSMin;
Rex Xu9d93a232016-05-05 12:30:44 +08006605 }
Rex Xu430ef402016-10-14 17:22:23 +08006606 } else if (op == glslang::EOpMaxInvocations ||
6607 op == glslang::EOpMaxInvocationsInclusiveScan ||
6608 op == glslang::EOpMaxInvocationsExclusiveScan) {
Rex Xu9d93a232016-05-05 12:30:44 +08006609 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006610 opCode = spv::OpGroupFMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006611 else {
6612 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006613 opCode = spv::OpGroupUMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006614 else
Rex Xu51596642016-09-21 18:56:12 +08006615 opCode = spv::OpGroupSMax;
Rex Xu9d93a232016-05-05 12:30:44 +08006616 }
6617 } else {
6618 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006619 opCode = spv::OpGroupFAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006620 else
Rex Xu51596642016-09-21 18:56:12 +08006621 opCode = spv::OpGroupIAdd;
Rex Xu9d93a232016-05-05 12:30:44 +08006622 }
6623
Rex Xu2bbbe062016-08-23 15:41:05 +08006624 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006625 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006626
6627 break;
Rex Xu9d93a232016-05-05 12:30:44 +08006628 case glslang::EOpMinInvocationsNonUniform:
6629 case glslang::EOpMaxInvocationsNonUniform:
6630 case glslang::EOpAddInvocationsNonUniform:
Rex Xu430ef402016-10-14 17:22:23 +08006631 case glslang::EOpMinInvocationsInclusiveScanNonUniform:
6632 case glslang::EOpMaxInvocationsInclusiveScanNonUniform:
6633 case glslang::EOpAddInvocationsInclusiveScanNonUniform:
6634 case glslang::EOpMinInvocationsExclusiveScanNonUniform:
6635 case glslang::EOpMaxInvocationsExclusiveScanNonUniform:
6636 case glslang::EOpAddInvocationsExclusiveScanNonUniform:
6637 if (op == glslang::EOpMinInvocationsNonUniform ||
6638 op == glslang::EOpMinInvocationsInclusiveScanNonUniform ||
6639 op == glslang::EOpMinInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08006640 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006641 opCode = spv::OpGroupFMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006642 else {
6643 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006644 opCode = spv::OpGroupUMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006645 else
Rex Xu51596642016-09-21 18:56:12 +08006646 opCode = spv::OpGroupSMinNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006647 }
6648 }
Rex Xu430ef402016-10-14 17:22:23 +08006649 else if (op == glslang::EOpMaxInvocationsNonUniform ||
6650 op == glslang::EOpMaxInvocationsInclusiveScanNonUniform ||
6651 op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) {
Rex Xu9d93a232016-05-05 12:30:44 +08006652 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006653 opCode = spv::OpGroupFMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006654 else {
6655 if (isUnsigned)
Rex Xu51596642016-09-21 18:56:12 +08006656 opCode = spv::OpGroupUMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006657 else
Rex Xu51596642016-09-21 18:56:12 +08006658 opCode = spv::OpGroupSMaxNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006659 }
6660 }
6661 else {
6662 if (isFloat)
Rex Xu51596642016-09-21 18:56:12 +08006663 opCode = spv::OpGroupFAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006664 else
Rex Xu51596642016-09-21 18:56:12 +08006665 opCode = spv::OpGroupIAddNonUniformAMD;
Rex Xu9d93a232016-05-05 12:30:44 +08006666 }
6667
Rex Xu2bbbe062016-08-23 15:41:05 +08006668 if (builder.isVectorType(typeId))
Rex Xu430ef402016-10-14 17:22:23 +08006669 return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands);
Rex Xu51596642016-09-21 18:56:12 +08006670
6671 break;
John Kessenich91cef522016-05-05 16:45:40 -06006672 default:
6673 logger->missingFunctionality("invocation operation");
6674 return spv::NoResult;
6675 }
Rex Xu51596642016-09-21 18:56:12 +08006676
6677 assert(opCode != spv::OpNop);
6678 return builder.createOp(opCode, typeId, spvGroupOperands);
John Kessenich91cef522016-05-05 16:45:40 -06006679}
6680
Rex Xu2bbbe062016-08-23 15:41:05 +08006681// Create group invocation operations on a vector
John Kessenich149afc32018-08-14 13:31:43 -06006682spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
6683 spv::Id typeId, std::vector<spv::Id>& operands)
Rex Xu2bbbe062016-08-23 15:41:05 +08006684{
6685 assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin ||
6686 op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax ||
Rex Xub7072052016-09-26 15:53:40 +08006687 op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast ||
chaocf200da82016-12-20 12:44:35 -08006688 op == spv::OpSubgroupReadInvocationKHR ||
Rex Xu2bbbe062016-08-23 15:41:05 +08006689 op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD ||
6690 op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD ||
6691 op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD);
6692
6693 // Handle group invocation operations scalar by scalar.
6694 // The result type is the same type as the original type.
6695 // The algorithm is to:
6696 // - break the vector into scalars
6697 // - apply the operation to each scalar
6698 // - make a vector out the scalar results
6699
6700 // get the types sorted out
Rex Xub7072052016-09-26 15:53:40 +08006701 int numComponents = builder.getNumComponents(operands[0]);
6702 spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0]));
Rex Xu2bbbe062016-08-23 15:41:05 +08006703 std::vector<spv::Id> results;
6704
6705 // do each scalar op
6706 for (int comp = 0; comp < numComponents; ++comp) {
6707 std::vector<unsigned int> indexes;
6708 indexes.push_back(comp);
John Kessenich149afc32018-08-14 13:31:43 -06006709 spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) };
6710 std::vector<spv::IdImmediate> spvGroupOperands;
chaocf200da82016-12-20 12:44:35 -08006711 if (op == spv::OpSubgroupReadInvocationKHR) {
6712 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06006713 spv::IdImmediate operand = { true, operands[1] };
6714 spvGroupOperands.push_back(operand);
chaocf200da82016-12-20 12:44:35 -08006715 } else if (op == spv::OpGroupBroadcast) {
John Kessenich149afc32018-08-14 13:31:43 -06006716 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6717 spvGroupOperands.push_back(scope);
Rex Xub7072052016-09-26 15:53:40 +08006718 spvGroupOperands.push_back(scalar);
John Kessenich149afc32018-08-14 13:31:43 -06006719 spv::IdImmediate operand = { true, operands[1] };
6720 spvGroupOperands.push_back(operand);
Rex Xub7072052016-09-26 15:53:40 +08006721 } else {
John Kessenich149afc32018-08-14 13:31:43 -06006722 spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
6723 spvGroupOperands.push_back(scope);
John Kessenichd122a722018-09-18 03:43:30 -06006724 spv::IdImmediate groupOp = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06006725 spvGroupOperands.push_back(groupOp);
Rex Xub7072052016-09-26 15:53:40 +08006726 spvGroupOperands.push_back(scalar);
6727 }
Rex Xu2bbbe062016-08-23 15:41:05 +08006728
Rex Xub7072052016-09-26 15:53:40 +08006729 results.push_back(builder.createOp(op, scalarType, spvGroupOperands));
Rex Xu2bbbe062016-08-23 15:41:05 +08006730 }
6731
6732 // put the pieces together
6733 return builder.createCompositeConstruct(typeId, results);
6734}
Rex Xu2bbbe062016-08-23 15:41:05 +08006735
John Kessenich66011cb2018-03-06 16:12:04 -07006736// Create subgroup invocation operations.
John Kessenich149afc32018-08-14 13:31:43 -06006737spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
6738 std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
John Kessenich66011cb2018-03-06 16:12:04 -07006739{
6740 // Add the required capabilities.
6741 switch (op) {
6742 case glslang::EOpSubgroupElect:
6743 builder.addCapability(spv::CapabilityGroupNonUniform);
6744 break;
6745 case glslang::EOpSubgroupAll:
6746 case glslang::EOpSubgroupAny:
6747 case glslang::EOpSubgroupAllEqual:
6748 builder.addCapability(spv::CapabilityGroupNonUniform);
6749 builder.addCapability(spv::CapabilityGroupNonUniformVote);
6750 break;
6751 case glslang::EOpSubgroupBroadcast:
6752 case glslang::EOpSubgroupBroadcastFirst:
6753 case glslang::EOpSubgroupBallot:
6754 case glslang::EOpSubgroupInverseBallot:
6755 case glslang::EOpSubgroupBallotBitExtract:
6756 case glslang::EOpSubgroupBallotBitCount:
6757 case glslang::EOpSubgroupBallotInclusiveBitCount:
6758 case glslang::EOpSubgroupBallotExclusiveBitCount:
6759 case glslang::EOpSubgroupBallotFindLSB:
6760 case glslang::EOpSubgroupBallotFindMSB:
6761 builder.addCapability(spv::CapabilityGroupNonUniform);
6762 builder.addCapability(spv::CapabilityGroupNonUniformBallot);
6763 break;
6764 case glslang::EOpSubgroupShuffle:
6765 case glslang::EOpSubgroupShuffleXor:
6766 builder.addCapability(spv::CapabilityGroupNonUniform);
6767 builder.addCapability(spv::CapabilityGroupNonUniformShuffle);
6768 break;
6769 case glslang::EOpSubgroupShuffleUp:
6770 case glslang::EOpSubgroupShuffleDown:
6771 builder.addCapability(spv::CapabilityGroupNonUniform);
6772 builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative);
6773 break;
6774 case glslang::EOpSubgroupAdd:
6775 case glslang::EOpSubgroupMul:
6776 case glslang::EOpSubgroupMin:
6777 case glslang::EOpSubgroupMax:
6778 case glslang::EOpSubgroupAnd:
6779 case glslang::EOpSubgroupOr:
6780 case glslang::EOpSubgroupXor:
6781 case glslang::EOpSubgroupInclusiveAdd:
6782 case glslang::EOpSubgroupInclusiveMul:
6783 case glslang::EOpSubgroupInclusiveMin:
6784 case glslang::EOpSubgroupInclusiveMax:
6785 case glslang::EOpSubgroupInclusiveAnd:
6786 case glslang::EOpSubgroupInclusiveOr:
6787 case glslang::EOpSubgroupInclusiveXor:
6788 case glslang::EOpSubgroupExclusiveAdd:
6789 case glslang::EOpSubgroupExclusiveMul:
6790 case glslang::EOpSubgroupExclusiveMin:
6791 case glslang::EOpSubgroupExclusiveMax:
6792 case glslang::EOpSubgroupExclusiveAnd:
6793 case glslang::EOpSubgroupExclusiveOr:
6794 case glslang::EOpSubgroupExclusiveXor:
6795 builder.addCapability(spv::CapabilityGroupNonUniform);
6796 builder.addCapability(spv::CapabilityGroupNonUniformArithmetic);
6797 break;
6798 case glslang::EOpSubgroupClusteredAdd:
6799 case glslang::EOpSubgroupClusteredMul:
6800 case glslang::EOpSubgroupClusteredMin:
6801 case glslang::EOpSubgroupClusteredMax:
6802 case glslang::EOpSubgroupClusteredAnd:
6803 case glslang::EOpSubgroupClusteredOr:
6804 case glslang::EOpSubgroupClusteredXor:
6805 builder.addCapability(spv::CapabilityGroupNonUniform);
6806 builder.addCapability(spv::CapabilityGroupNonUniformClustered);
6807 break;
6808 case glslang::EOpSubgroupQuadBroadcast:
6809 case glslang::EOpSubgroupQuadSwapHorizontal:
6810 case glslang::EOpSubgroupQuadSwapVertical:
6811 case glslang::EOpSubgroupQuadSwapDiagonal:
6812 builder.addCapability(spv::CapabilityGroupNonUniform);
6813 builder.addCapability(spv::CapabilityGroupNonUniformQuad);
6814 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006815 case glslang::EOpSubgroupPartitionedAdd:
6816 case glslang::EOpSubgroupPartitionedMul:
6817 case glslang::EOpSubgroupPartitionedMin:
6818 case glslang::EOpSubgroupPartitionedMax:
6819 case glslang::EOpSubgroupPartitionedAnd:
6820 case glslang::EOpSubgroupPartitionedOr:
6821 case glslang::EOpSubgroupPartitionedXor:
6822 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6823 case glslang::EOpSubgroupPartitionedInclusiveMul:
6824 case glslang::EOpSubgroupPartitionedInclusiveMin:
6825 case glslang::EOpSubgroupPartitionedInclusiveMax:
6826 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6827 case glslang::EOpSubgroupPartitionedInclusiveOr:
6828 case glslang::EOpSubgroupPartitionedInclusiveXor:
6829 case glslang::EOpSubgroupPartitionedExclusiveAdd:
6830 case glslang::EOpSubgroupPartitionedExclusiveMul:
6831 case glslang::EOpSubgroupPartitionedExclusiveMin:
6832 case glslang::EOpSubgroupPartitionedExclusiveMax:
6833 case glslang::EOpSubgroupPartitionedExclusiveAnd:
6834 case glslang::EOpSubgroupPartitionedExclusiveOr:
6835 case glslang::EOpSubgroupPartitionedExclusiveXor:
6836 builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned);
6837 builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV);
6838 break;
John Kessenich66011cb2018-03-06 16:12:04 -07006839 default: assert(0 && "Unhandled subgroup operation!");
6840 }
6841
Jeff Bolzc5b669e2019-09-08 08:49:18 -05006842
6843 const bool isUnsigned = isTypeUnsignedInt(typeProxy);
6844 const bool isFloat = isTypeFloat(typeProxy);
John Kessenich66011cb2018-03-06 16:12:04 -07006845 const bool isBool = typeProxy == glslang::EbtBool;
6846
6847 spv::Op opCode = spv::OpNop;
6848
6849 // Figure out which opcode to use.
6850 switch (op) {
6851 case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break;
6852 case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break;
6853 case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break;
6854 case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break;
6855 case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break;
6856 case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break;
6857 case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break;
6858 case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break;
6859 case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break;
6860 case glslang::EOpSubgroupBallotBitCount:
6861 case glslang::EOpSubgroupBallotInclusiveBitCount:
6862 case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break;
6863 case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break;
6864 case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break;
6865 case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break;
6866 case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break;
6867 case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break;
6868 case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break;
6869 case glslang::EOpSubgroupAdd:
6870 case glslang::EOpSubgroupInclusiveAdd:
6871 case glslang::EOpSubgroupExclusiveAdd:
6872 case glslang::EOpSubgroupClusteredAdd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006873 case glslang::EOpSubgroupPartitionedAdd:
6874 case glslang::EOpSubgroupPartitionedInclusiveAdd:
6875 case glslang::EOpSubgroupPartitionedExclusiveAdd:
John Kessenich66011cb2018-03-06 16:12:04 -07006876 if (isFloat) {
6877 opCode = spv::OpGroupNonUniformFAdd;
6878 } else {
6879 opCode = spv::OpGroupNonUniformIAdd;
6880 }
6881 break;
6882 case glslang::EOpSubgroupMul:
6883 case glslang::EOpSubgroupInclusiveMul:
6884 case glslang::EOpSubgroupExclusiveMul:
6885 case glslang::EOpSubgroupClusteredMul:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006886 case glslang::EOpSubgroupPartitionedMul:
6887 case glslang::EOpSubgroupPartitionedInclusiveMul:
6888 case glslang::EOpSubgroupPartitionedExclusiveMul:
John Kessenich66011cb2018-03-06 16:12:04 -07006889 if (isFloat) {
6890 opCode = spv::OpGroupNonUniformFMul;
6891 } else {
6892 opCode = spv::OpGroupNonUniformIMul;
6893 }
6894 break;
6895 case glslang::EOpSubgroupMin:
6896 case glslang::EOpSubgroupInclusiveMin:
6897 case glslang::EOpSubgroupExclusiveMin:
6898 case glslang::EOpSubgroupClusteredMin:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006899 case glslang::EOpSubgroupPartitionedMin:
6900 case glslang::EOpSubgroupPartitionedInclusiveMin:
6901 case glslang::EOpSubgroupPartitionedExclusiveMin:
John Kessenich66011cb2018-03-06 16:12:04 -07006902 if (isFloat) {
6903 opCode = spv::OpGroupNonUniformFMin;
6904 } else if (isUnsigned) {
6905 opCode = spv::OpGroupNonUniformUMin;
6906 } else {
6907 opCode = spv::OpGroupNonUniformSMin;
6908 }
6909 break;
6910 case glslang::EOpSubgroupMax:
6911 case glslang::EOpSubgroupInclusiveMax:
6912 case glslang::EOpSubgroupExclusiveMax:
6913 case glslang::EOpSubgroupClusteredMax:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006914 case glslang::EOpSubgroupPartitionedMax:
6915 case glslang::EOpSubgroupPartitionedInclusiveMax:
6916 case glslang::EOpSubgroupPartitionedExclusiveMax:
John Kessenich66011cb2018-03-06 16:12:04 -07006917 if (isFloat) {
6918 opCode = spv::OpGroupNonUniformFMax;
6919 } else if (isUnsigned) {
6920 opCode = spv::OpGroupNonUniformUMax;
6921 } else {
6922 opCode = spv::OpGroupNonUniformSMax;
6923 }
6924 break;
6925 case glslang::EOpSubgroupAnd:
6926 case glslang::EOpSubgroupInclusiveAnd:
6927 case glslang::EOpSubgroupExclusiveAnd:
6928 case glslang::EOpSubgroupClusteredAnd:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006929 case glslang::EOpSubgroupPartitionedAnd:
6930 case glslang::EOpSubgroupPartitionedInclusiveAnd:
6931 case glslang::EOpSubgroupPartitionedExclusiveAnd:
John Kessenich66011cb2018-03-06 16:12:04 -07006932 if (isBool) {
6933 opCode = spv::OpGroupNonUniformLogicalAnd;
6934 } else {
6935 opCode = spv::OpGroupNonUniformBitwiseAnd;
6936 }
6937 break;
6938 case glslang::EOpSubgroupOr:
6939 case glslang::EOpSubgroupInclusiveOr:
6940 case glslang::EOpSubgroupExclusiveOr:
6941 case glslang::EOpSubgroupClusteredOr:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006942 case glslang::EOpSubgroupPartitionedOr:
6943 case glslang::EOpSubgroupPartitionedInclusiveOr:
6944 case glslang::EOpSubgroupPartitionedExclusiveOr:
John Kessenich66011cb2018-03-06 16:12:04 -07006945 if (isBool) {
6946 opCode = spv::OpGroupNonUniformLogicalOr;
6947 } else {
6948 opCode = spv::OpGroupNonUniformBitwiseOr;
6949 }
6950 break;
6951 case glslang::EOpSubgroupXor:
6952 case glslang::EOpSubgroupInclusiveXor:
6953 case glslang::EOpSubgroupExclusiveXor:
6954 case glslang::EOpSubgroupClusteredXor:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05006955 case glslang::EOpSubgroupPartitionedXor:
6956 case glslang::EOpSubgroupPartitionedInclusiveXor:
6957 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07006958 if (isBool) {
6959 opCode = spv::OpGroupNonUniformLogicalXor;
6960 } else {
6961 opCode = spv::OpGroupNonUniformBitwiseXor;
6962 }
6963 break;
6964 case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break;
6965 case glslang::EOpSubgroupQuadSwapHorizontal:
6966 case glslang::EOpSubgroupQuadSwapVertical:
6967 case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break;
6968 default: assert(0 && "Unhandled subgroup operation!");
6969 }
6970
John Kessenich149afc32018-08-14 13:31:43 -06006971 // get the right Group Operation
6972 spv::GroupOperation groupOperation = spv::GroupOperationMax;
John Kessenich66011cb2018-03-06 16:12:04 -07006973 switch (op) {
John Kessenich149afc32018-08-14 13:31:43 -06006974 default:
6975 break;
John Kessenich66011cb2018-03-06 16:12:04 -07006976 case glslang::EOpSubgroupBallotBitCount:
6977 case glslang::EOpSubgroupAdd:
6978 case glslang::EOpSubgroupMul:
6979 case glslang::EOpSubgroupMin:
6980 case glslang::EOpSubgroupMax:
6981 case glslang::EOpSubgroupAnd:
6982 case glslang::EOpSubgroupOr:
6983 case glslang::EOpSubgroupXor:
John Kessenich149afc32018-08-14 13:31:43 -06006984 groupOperation = spv::GroupOperationReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07006985 break;
6986 case glslang::EOpSubgroupBallotInclusiveBitCount:
6987 case glslang::EOpSubgroupInclusiveAdd:
6988 case glslang::EOpSubgroupInclusiveMul:
6989 case glslang::EOpSubgroupInclusiveMin:
6990 case glslang::EOpSubgroupInclusiveMax:
6991 case glslang::EOpSubgroupInclusiveAnd:
6992 case glslang::EOpSubgroupInclusiveOr:
6993 case glslang::EOpSubgroupInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06006994 groupOperation = spv::GroupOperationInclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07006995 break;
6996 case glslang::EOpSubgroupBallotExclusiveBitCount:
6997 case glslang::EOpSubgroupExclusiveAdd:
6998 case glslang::EOpSubgroupExclusiveMul:
6999 case glslang::EOpSubgroupExclusiveMin:
7000 case glslang::EOpSubgroupExclusiveMax:
7001 case glslang::EOpSubgroupExclusiveAnd:
7002 case glslang::EOpSubgroupExclusiveOr:
7003 case glslang::EOpSubgroupExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007004 groupOperation = spv::GroupOperationExclusiveScan;
John Kessenich66011cb2018-03-06 16:12:04 -07007005 break;
7006 case glslang::EOpSubgroupClusteredAdd:
7007 case glslang::EOpSubgroupClusteredMul:
7008 case glslang::EOpSubgroupClusteredMin:
7009 case glslang::EOpSubgroupClusteredMax:
7010 case glslang::EOpSubgroupClusteredAnd:
7011 case glslang::EOpSubgroupClusteredOr:
7012 case glslang::EOpSubgroupClusteredXor:
John Kessenich149afc32018-08-14 13:31:43 -06007013 groupOperation = spv::GroupOperationClusteredReduce;
John Kessenich66011cb2018-03-06 16:12:04 -07007014 break;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007015 case glslang::EOpSubgroupPartitionedAdd:
7016 case glslang::EOpSubgroupPartitionedMul:
7017 case glslang::EOpSubgroupPartitionedMin:
7018 case glslang::EOpSubgroupPartitionedMax:
7019 case glslang::EOpSubgroupPartitionedAnd:
7020 case glslang::EOpSubgroupPartitionedOr:
7021 case glslang::EOpSubgroupPartitionedXor:
John Kessenich149afc32018-08-14 13:31:43 -06007022 groupOperation = spv::GroupOperationPartitionedReduceNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007023 break;
7024 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7025 case glslang::EOpSubgroupPartitionedInclusiveMul:
7026 case glslang::EOpSubgroupPartitionedInclusiveMin:
7027 case glslang::EOpSubgroupPartitionedInclusiveMax:
7028 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7029 case glslang::EOpSubgroupPartitionedInclusiveOr:
7030 case glslang::EOpSubgroupPartitionedInclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007031 groupOperation = spv::GroupOperationPartitionedInclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007032 break;
7033 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7034 case glslang::EOpSubgroupPartitionedExclusiveMul:
7035 case glslang::EOpSubgroupPartitionedExclusiveMin:
7036 case glslang::EOpSubgroupPartitionedExclusiveMax:
7037 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7038 case glslang::EOpSubgroupPartitionedExclusiveOr:
7039 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich149afc32018-08-14 13:31:43 -06007040 groupOperation = spv::GroupOperationPartitionedExclusiveScanNV;
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007041 break;
John Kessenich66011cb2018-03-06 16:12:04 -07007042 }
7043
John Kessenich149afc32018-08-14 13:31:43 -06007044 // build the instruction
7045 std::vector<spv::IdImmediate> spvGroupOperands;
7046
7047 // Every operation begins with the Execution Scope operand.
7048 spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) };
7049 spvGroupOperands.push_back(executionScope);
7050
7051 // Next, for all operations that use a Group Operation, push that as an operand.
7052 if (groupOperation != spv::GroupOperationMax) {
John Kessenichd122a722018-09-18 03:43:30 -06007053 spv::IdImmediate groupOperand = { false, (unsigned)groupOperation };
John Kessenich149afc32018-08-14 13:31:43 -06007054 spvGroupOperands.push_back(groupOperand);
7055 }
7056
John Kessenich66011cb2018-03-06 16:12:04 -07007057 // Push back the operands next.
John Kessenich149afc32018-08-14 13:31:43 -06007058 for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) {
7059 spv::IdImmediate operand = { true, *opIt };
7060 spvGroupOperands.push_back(operand);
John Kessenich66011cb2018-03-06 16:12:04 -07007061 }
7062
7063 // Some opcodes have additional operands.
John Kessenich149afc32018-08-14 13:31:43 -06007064 spv::Id directionId = spv::NoResult;
John Kessenich66011cb2018-03-06 16:12:04 -07007065 switch (op) {
7066 default: break;
John Kessenich149afc32018-08-14 13:31:43 -06007067 case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break;
7068 case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break;
7069 case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break;
7070 }
7071 if (directionId != spv::NoResult) {
7072 spv::IdImmediate direction = { true, directionId };
7073 spvGroupOperands.push_back(direction);
John Kessenich66011cb2018-03-06 16:12:04 -07007074 }
7075
7076 return builder.createOp(opCode, typeId, spvGroupOperands);
7077}
7078
John Kessenich5e4b1242015-08-06 22:53:06 -06007079spv::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 -06007080{
John Kessenich66011cb2018-03-06 16:12:04 -07007081 bool isUnsigned = isTypeUnsignedInt(typeProxy);
7082 bool isFloat = isTypeFloat(typeProxy);
John Kessenich5e4b1242015-08-06 22:53:06 -06007083
John Kessenich140f3df2015-06-26 16:58:36 -06007084 spv::Op opCode = spv::OpNop;
Rex Xu9d93a232016-05-05 12:30:44 +08007085 int extBuiltins = -1;
John Kessenich140f3df2015-06-26 16:58:36 -06007086 int libCall = -1;
Mark Adams364c21c2016-01-06 13:41:02 -05007087 size_t consumedOperands = operands.size();
John Kessenich55e7d112015-11-15 21:33:39 -07007088 spv::Id typeId0 = 0;
7089 if (consumedOperands > 0)
7090 typeId0 = builder.getTypeId(operands[0]);
Rex Xu470026f2017-03-29 17:12:40 +08007091 spv::Id typeId1 = 0;
7092 if (consumedOperands > 1)
7093 typeId1 = builder.getTypeId(operands[1]);
John Kessenich55e7d112015-11-15 21:33:39 -07007094 spv::Id frexpIntType = 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007095
7096 switch (op) {
7097 case glslang::EOpMin:
John Kessenich5e4b1242015-08-06 22:53:06 -06007098 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007099 libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin;
John Kessenich5e4b1242015-08-06 22:53:06 -06007100 else if (isUnsigned)
7101 libCall = spv::GLSLstd450UMin;
7102 else
7103 libCall = spv::GLSLstd450SMin;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007104 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007105 break;
7106 case glslang::EOpModf:
John Kessenich5e4b1242015-08-06 22:53:06 -06007107 libCall = spv::GLSLstd450Modf;
John Kessenich140f3df2015-06-26 16:58:36 -06007108 break;
7109 case glslang::EOpMax:
John Kessenich5e4b1242015-08-06 22:53:06 -06007110 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007111 libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax;
John Kessenich5e4b1242015-08-06 22:53:06 -06007112 else if (isUnsigned)
7113 libCall = spv::GLSLstd450UMax;
7114 else
7115 libCall = spv::GLSLstd450SMax;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007116 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007117 break;
7118 case glslang::EOpPow:
John Kessenich5e4b1242015-08-06 22:53:06 -06007119 libCall = spv::GLSLstd450Pow;
John Kessenich140f3df2015-06-26 16:58:36 -06007120 break;
7121 case glslang::EOpDot:
7122 opCode = spv::OpDot;
7123 break;
7124 case glslang::EOpAtan:
John Kessenich5e4b1242015-08-06 22:53:06 -06007125 libCall = spv::GLSLstd450Atan2;
John Kessenich140f3df2015-06-26 16:58:36 -06007126 break;
7127
7128 case glslang::EOpClamp:
John Kessenich5e4b1242015-08-06 22:53:06 -06007129 if (isFloat)
John Kessenich605afc72019-06-17 23:33:09 -06007130 libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp;
John Kessenich5e4b1242015-08-06 22:53:06 -06007131 else if (isUnsigned)
7132 libCall = spv::GLSLstd450UClamp;
7133 else
7134 libCall = spv::GLSLstd450SClamp;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007135 builder.promoteScalar(precision, operands.front(), operands[1]);
7136 builder.promoteScalar(precision, operands.front(), operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007137 break;
7138 case glslang::EOpMix:
Rex Xud715adc2016-03-15 12:08:31 +08007139 if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) {
7140 assert(isFloat);
John Kessenich55e7d112015-11-15 21:33:39 -07007141 libCall = spv::GLSLstd450FMix;
Rex Xud715adc2016-03-15 12:08:31 +08007142 } else {
John Kessenich6c292d32016-02-15 20:58:50 -07007143 opCode = spv::OpSelect;
Rex Xud715adc2016-03-15 12:08:31 +08007144 std::swap(operands.front(), operands.back());
John Kessenich6c292d32016-02-15 20:58:50 -07007145 }
John Kesseniche7c83cf2015-12-13 13:34:37 -07007146 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007147 break;
7148 case glslang::EOpStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007149 libCall = spv::GLSLstd450Step;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007150 builder.promoteScalar(precision, operands.front(), operands.back());
John Kessenich140f3df2015-06-26 16:58:36 -06007151 break;
7152 case glslang::EOpSmoothStep:
John Kessenich5e4b1242015-08-06 22:53:06 -06007153 libCall = spv::GLSLstd450SmoothStep;
John Kesseniche7c83cf2015-12-13 13:34:37 -07007154 builder.promoteScalar(precision, operands[0], operands[2]);
7155 builder.promoteScalar(precision, operands[1], operands[2]);
John Kessenich140f3df2015-06-26 16:58:36 -06007156 break;
7157
7158 case glslang::EOpDistance:
John Kessenich5e4b1242015-08-06 22:53:06 -06007159 libCall = spv::GLSLstd450Distance;
John Kessenich140f3df2015-06-26 16:58:36 -06007160 break;
7161 case glslang::EOpCross:
John Kessenich5e4b1242015-08-06 22:53:06 -06007162 libCall = spv::GLSLstd450Cross;
John Kessenich140f3df2015-06-26 16:58:36 -06007163 break;
7164 case glslang::EOpFaceForward:
John Kessenich5e4b1242015-08-06 22:53:06 -06007165 libCall = spv::GLSLstd450FaceForward;
John Kessenich140f3df2015-06-26 16:58:36 -06007166 break;
7167 case glslang::EOpReflect:
John Kessenich5e4b1242015-08-06 22:53:06 -06007168 libCall = spv::GLSLstd450Reflect;
John Kessenich140f3df2015-06-26 16:58:36 -06007169 break;
7170 case glslang::EOpRefract:
John Kessenich5e4b1242015-08-06 22:53:06 -06007171 libCall = spv::GLSLstd450Refract;
John Kessenich140f3df2015-06-26 16:58:36 -06007172 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06007173#ifndef GLSLANG_WEB
Rex Xu7a26c172015-12-08 17:12:09 +08007174 case glslang::EOpInterpolateAtSample:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007175 if (typeProxy == glslang::EbtFloat16)
7176 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007177 libCall = spv::GLSLstd450InterpolateAtSample;
7178 break;
7179 case glslang::EOpInterpolateAtOffset:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007180 if (typeProxy == glslang::EbtFloat16)
7181 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu7a26c172015-12-08 17:12:09 +08007182 libCall = spv::GLSLstd450InterpolateAtOffset;
7183 break;
John Kessenich55e7d112015-11-15 21:33:39 -07007184 case glslang::EOpAddCarry:
7185 opCode = spv::OpIAddCarry;
7186 typeId = builder.makeStructResultType(typeId0, typeId0);
7187 consumedOperands = 2;
7188 break;
7189 case glslang::EOpSubBorrow:
7190 opCode = spv::OpISubBorrow;
7191 typeId = builder.makeStructResultType(typeId0, typeId0);
7192 consumedOperands = 2;
7193 break;
7194 case glslang::EOpUMulExtended:
7195 opCode = spv::OpUMulExtended;
7196 typeId = builder.makeStructResultType(typeId0, typeId0);
7197 consumedOperands = 2;
7198 break;
7199 case glslang::EOpIMulExtended:
7200 opCode = spv::OpSMulExtended;
7201 typeId = builder.makeStructResultType(typeId0, typeId0);
7202 consumedOperands = 2;
7203 break;
7204 case glslang::EOpBitfieldExtract:
7205 if (isUnsigned)
7206 opCode = spv::OpBitFieldUExtract;
7207 else
7208 opCode = spv::OpBitFieldSExtract;
7209 break;
7210 case glslang::EOpBitfieldInsert:
7211 opCode = spv::OpBitFieldInsert;
7212 break;
7213
7214 case glslang::EOpFma:
7215 libCall = spv::GLSLstd450Fma;
7216 break;
7217 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007218 {
7219 libCall = spv::GLSLstd450FrexpStruct;
7220 assert(builder.isPointerType(typeId1));
7221 typeId1 = builder.getContainedTypeId(typeId1);
Rex Xu470026f2017-03-29 17:12:40 +08007222 int width = builder.getScalarTypeWidth(typeId1);
Rex Xu7c88aff2018-04-11 16:56:50 +08007223 if (width == 16)
7224 // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16
7225 builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16);
Rex Xu470026f2017-03-29 17:12:40 +08007226 if (builder.getNumComponents(operands[0]) == 1)
7227 frexpIntType = builder.makeIntegerType(width, true);
7228 else
7229 frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0]));
7230 typeId = builder.makeStructResultType(typeId0, frexpIntType);
7231 consumedOperands = 1;
7232 }
John Kessenich55e7d112015-11-15 21:33:39 -07007233 break;
7234 case glslang::EOpLdexp:
7235 libCall = spv::GLSLstd450Ldexp;
7236 break;
7237
Rex Xu574ab042016-04-14 16:53:07 +08007238 case glslang::EOpReadInvocation:
Rex Xu51596642016-09-21 18:56:12 +08007239 return createInvocationsOperation(op, typeId, operands, typeProxy);
Rex Xu574ab042016-04-14 16:53:07 +08007240
John Kessenich66011cb2018-03-06 16:12:04 -07007241 case glslang::EOpSubgroupBroadcast:
7242 case glslang::EOpSubgroupBallotBitExtract:
7243 case glslang::EOpSubgroupShuffle:
7244 case glslang::EOpSubgroupShuffleXor:
7245 case glslang::EOpSubgroupShuffleUp:
7246 case glslang::EOpSubgroupShuffleDown:
7247 case glslang::EOpSubgroupClusteredAdd:
7248 case glslang::EOpSubgroupClusteredMul:
7249 case glslang::EOpSubgroupClusteredMin:
7250 case glslang::EOpSubgroupClusteredMax:
7251 case glslang::EOpSubgroupClusteredAnd:
7252 case glslang::EOpSubgroupClusteredOr:
7253 case glslang::EOpSubgroupClusteredXor:
7254 case glslang::EOpSubgroupQuadBroadcast:
Jeff Bolz2abe9a42018-03-29 22:52:17 -05007255 case glslang::EOpSubgroupPartitionedAdd:
7256 case glslang::EOpSubgroupPartitionedMul:
7257 case glslang::EOpSubgroupPartitionedMin:
7258 case glslang::EOpSubgroupPartitionedMax:
7259 case glslang::EOpSubgroupPartitionedAnd:
7260 case glslang::EOpSubgroupPartitionedOr:
7261 case glslang::EOpSubgroupPartitionedXor:
7262 case glslang::EOpSubgroupPartitionedInclusiveAdd:
7263 case glslang::EOpSubgroupPartitionedInclusiveMul:
7264 case glslang::EOpSubgroupPartitionedInclusiveMin:
7265 case glslang::EOpSubgroupPartitionedInclusiveMax:
7266 case glslang::EOpSubgroupPartitionedInclusiveAnd:
7267 case glslang::EOpSubgroupPartitionedInclusiveOr:
7268 case glslang::EOpSubgroupPartitionedInclusiveXor:
7269 case glslang::EOpSubgroupPartitionedExclusiveAdd:
7270 case glslang::EOpSubgroupPartitionedExclusiveMul:
7271 case glslang::EOpSubgroupPartitionedExclusiveMin:
7272 case glslang::EOpSubgroupPartitionedExclusiveMax:
7273 case glslang::EOpSubgroupPartitionedExclusiveAnd:
7274 case glslang::EOpSubgroupPartitionedExclusiveOr:
7275 case glslang::EOpSubgroupPartitionedExclusiveXor:
John Kessenich66011cb2018-03-06 16:12:04 -07007276 return createSubgroupOperation(op, typeId, operands, typeProxy);
7277
Rex Xu9d93a232016-05-05 12:30:44 +08007278 case glslang::EOpSwizzleInvocations:
7279 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7280 libCall = spv::SwizzleInvocationsAMD;
7281 break;
7282 case glslang::EOpSwizzleInvocationsMasked:
7283 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7284 libCall = spv::SwizzleInvocationsMaskedAMD;
7285 break;
7286 case glslang::EOpWriteInvocation:
7287 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot);
7288 libCall = spv::WriteInvocationAMD;
7289 break;
7290
7291 case glslang::EOpMin3:
7292 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7293 if (isFloat)
7294 libCall = spv::FMin3AMD;
7295 else {
7296 if (isUnsigned)
7297 libCall = spv::UMin3AMD;
7298 else
7299 libCall = spv::SMin3AMD;
7300 }
7301 break;
7302 case glslang::EOpMax3:
7303 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7304 if (isFloat)
7305 libCall = spv::FMax3AMD;
7306 else {
7307 if (isUnsigned)
7308 libCall = spv::UMax3AMD;
7309 else
7310 libCall = spv::SMax3AMD;
7311 }
7312 break;
7313 case glslang::EOpMid3:
7314 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax);
7315 if (isFloat)
7316 libCall = spv::FMid3AMD;
7317 else {
7318 if (isUnsigned)
7319 libCall = spv::UMid3AMD;
7320 else
7321 libCall = spv::SMid3AMD;
7322 }
7323 break;
7324
7325 case glslang::EOpInterpolateAtVertex:
Rex Xub4a2a6c2018-05-17 13:51:28 +08007326 if (typeProxy == glslang::EbtFloat16)
7327 builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
Rex Xu9d93a232016-05-05 12:30:44 +08007328 extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter);
7329 libCall = spv::InterpolateAtVertexAMD;
7330 break;
Jeff Bolz36831c92018-09-05 10:11:41 -05007331 case glslang::EOpBarrier:
7332 {
7333 // This is for the extended controlBarrier function, with four operands.
7334 // The unextended barrier() goes through createNoArgOperation.
7335 assert(operands.size() == 4);
7336 unsigned int executionScope = builder.getConstantScalar(operands[0]);
7337 unsigned int memoryScope = builder.getConstantScalar(operands[1]);
7338 unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]);
7339 builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05007340 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7341 spv::MemorySemanticsMakeVisibleKHRMask |
7342 spv::MemorySemanticsOutputMemoryKHRMask |
7343 spv::MemorySemanticsVolatileMask)) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007344 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7345 }
7346 if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) {
7347 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7348 }
7349 return 0;
7350 }
7351 break;
7352 case glslang::EOpMemoryBarrier:
7353 {
7354 // This is for the extended memoryBarrier function, with three operands.
7355 // The unextended memoryBarrier() goes through createNoArgOperation.
7356 assert(operands.size() == 3);
7357 unsigned int memoryScope = builder.getConstantScalar(operands[0]);
7358 unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]);
7359 builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics);
Jeff Bolz38a52fc2019-06-14 09:56:28 -05007360 if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask |
7361 spv::MemorySemanticsMakeVisibleKHRMask |
7362 spv::MemorySemanticsOutputMemoryKHRMask |
7363 spv::MemorySemanticsVolatileMask)) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007364 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7365 }
7366 if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) {
7367 builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR);
7368 }
7369 return 0;
7370 }
7371 break;
Chao Chen3c366992018-09-19 11:41:59 -07007372
Chao Chenb50c02e2018-09-19 11:42:24 -07007373 case glslang::EOpReportIntersectionNV:
7374 {
7375 typeId = builder.makeBoolType();
Ashwin Leleff1783d2018-10-22 16:41:44 -07007376 opCode = spv::OpReportIntersectionNV;
Chao Chenb50c02e2018-09-19 11:42:24 -07007377 }
7378 break;
7379 case glslang::EOpTraceNV:
7380 {
Ashwin Leleff1783d2018-10-22 16:41:44 -07007381 builder.createNoResultOp(spv::OpTraceNV, operands);
7382 return 0;
7383 }
7384 break;
7385 case glslang::EOpExecuteCallableNV:
7386 {
7387 builder.createNoResultOp(spv::OpExecuteCallableNV, operands);
Chao Chenb50c02e2018-09-19 11:42:24 -07007388 return 0;
7389 }
7390 break;
Chao Chen3c366992018-09-19 11:41:59 -07007391 case glslang::EOpWritePackedPrimitiveIndices4x8NV:
7392 builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands);
7393 return 0;
Jeff Bolz4605e2e2019-02-19 13:10:32 -06007394 case glslang::EOpCooperativeMatrixMulAdd:
7395 opCode = spv::OpCooperativeMatrixMulAddNV;
7396 break;
John Kessenicha28f7a72019-08-06 07:00:58 -06007397#endif // GLSLANG_WEB
John Kessenich140f3df2015-06-26 16:58:36 -06007398 default:
7399 return 0;
7400 }
7401
7402 spv::Id id = 0;
John Kessenich2359bd02015-12-06 19:29:11 -07007403 if (libCall >= 0) {
David Neto8d63a3d2015-12-07 16:17:06 -05007404 // Use an extended instruction from the standard library.
7405 // Construct the call arguments, without modifying the original operands vector.
7406 // We might need the remaining arguments, e.g. in the EOpFrexp case.
7407 std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands);
Rex Xu9d93a232016-05-05 12:30:44 +08007408 id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments);
t.jungb16bea82018-11-15 10:21:36 +01007409 } else if (opCode == spv::OpDot && !isFloat) {
7410 // int dot(int, int)
7411 // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached
7412 const int componentCount = builder.getNumComponents(operands[0]);
7413 spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]);
7414 builder.setPrecision(mulOp, precision);
7415 id = builder.createCompositeExtract(mulOp, typeId, 0);
7416 for (int i = 1; i < componentCount; ++i) {
7417 builder.setPrecision(id, precision);
7418 id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i));
7419 }
John Kessenich2359bd02015-12-06 19:29:11 -07007420 } else {
John Kessenich55e7d112015-11-15 21:33:39 -07007421 switch (consumedOperands) {
John Kessenich140f3df2015-06-26 16:58:36 -06007422 case 0:
7423 // should all be handled by visitAggregate and createNoArgOperation
7424 assert(0);
7425 return 0;
7426 case 1:
7427 // should all be handled by createUnaryOperation
7428 assert(0);
7429 return 0;
7430 case 2:
7431 id = builder.createBinOp(opCode, typeId, operands[0], operands[1]);
7432 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007433 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007434 // anything 3 or over doesn't have l-value operands, so all should be consumed
7435 assert(consumedOperands == operands.size());
7436 id = builder.createOp(opCode, typeId, operands);
John Kessenich140f3df2015-06-26 16:58:36 -06007437 break;
7438 }
7439 }
7440
John Kessenichb9197c82019-08-11 07:41:45 -06007441#ifndef GLSLANG_WEB
John Kessenich55e7d112015-11-15 21:33:39 -07007442 // Decode the return types that were structures
7443 switch (op) {
7444 case glslang::EOpAddCarry:
7445 case glslang::EOpSubBorrow:
7446 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7447 id = builder.createCompositeExtract(id, typeId0, 0);
7448 break;
7449 case glslang::EOpUMulExtended:
7450 case glslang::EOpIMulExtended:
7451 builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]);
7452 builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]);
7453 break;
7454 case glslang::EOpFrexp:
Rex Xu470026f2017-03-29 17:12:40 +08007455 {
7456 assert(operands.size() == 2);
7457 if (builder.isFloatType(builder.getScalarTypeId(typeId1))) {
7458 // "exp" is floating-point type (from HLSL intrinsic)
7459 spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1);
7460 member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1);
7461 builder.createStore(member1, operands[1]);
7462 } else
7463 // "exp" is integer type (from GLSL built-in function)
7464 builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]);
7465 id = builder.createCompositeExtract(id, typeId0, 0);
7466 }
John Kessenich55e7d112015-11-15 21:33:39 -07007467 break;
7468 default:
7469 break;
7470 }
John Kessenichb9197c82019-08-11 07:41:45 -06007471#endif
John Kessenich55e7d112015-11-15 21:33:39 -07007472
John Kessenich32cfd492016-02-02 12:37:46 -07007473 return builder.setPrecision(id, precision);
John Kessenich140f3df2015-06-26 16:58:36 -06007474}
7475
Rex Xu9d93a232016-05-05 12:30:44 +08007476// Intrinsics with no arguments (or no return value, and no precision).
7477spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
John Kessenich140f3df2015-06-26 16:58:36 -06007478{
John Kessenich155d3512019-08-08 23:29:20 -06007479#ifndef GLSLANG_WEB
Jeff Bolz36831c92018-09-05 10:11:41 -05007480 // GLSL memory barriers use queuefamily scope in new model, device scope in old model
7481 spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice;
John Kessenich140f3df2015-06-26 16:58:36 -06007482
7483 switch (op) {
7484 case glslang::EOpEmitVertex:
7485 builder.createNoResultOp(spv::OpEmitVertex);
7486 return 0;
7487 case glslang::EOpEndPrimitive:
7488 builder.createNoResultOp(spv::OpEndPrimitive);
7489 return 0;
7490 case glslang::EOpBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007491 if (glslangIntermediate->getStage() == EShLangTessControl) {
Jeff Bolz36831c92018-09-05 10:11:41 -05007492 if (glslangIntermediate->usingVulkanMemoryModel()) {
7493 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7494 spv::MemorySemanticsOutputMemoryKHRMask |
7495 spv::MemorySemanticsAcquireReleaseMask);
7496 builder.addCapability(spv::CapabilityVulkanMemoryModelKHR);
7497 } else {
7498 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone);
7499 }
John Kessenich82979362017-12-11 04:02:24 -07007500 } else {
7501 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7502 spv::MemorySemanticsWorkgroupMemoryMask |
7503 spv::MemorySemanticsAcquireReleaseMask);
7504 }
John Kessenich140f3df2015-06-26 16:58:36 -06007505 return 0;
7506 case glslang::EOpMemoryBarrier:
Jeff Bolz36831c92018-09-05 10:11:41 -05007507 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory |
7508 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007509 return 0;
7510 case glslang::EOpMemoryBarrierAtomicCounter:
Jeff Bolz36831c92018-09-05 10:11:41 -05007511 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask |
7512 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007513 return 0;
7514 case glslang::EOpMemoryBarrierBuffer:
Jeff Bolz36831c92018-09-05 10:11:41 -05007515 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask |
7516 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007517 return 0;
7518 case glslang::EOpMemoryBarrierImage:
Jeff Bolz36831c92018-09-05 10:11:41 -05007519 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask |
7520 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007521 return 0;
7522 case glslang::EOpMemoryBarrierShared:
Jeff Bolz36831c92018-09-05 10:11:41 -05007523 builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask |
7524 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007525 return 0;
7526 case glslang::EOpGroupMemoryBarrier:
John Kessenich82979362017-12-11 04:02:24 -07007527 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory |
7528 spv::MemorySemanticsAcquireReleaseMask);
John Kessenich140f3df2015-06-26 16:58:36 -06007529 return 0;
LoopDawg6e72fdd2016-06-15 09:50:24 -06007530 case glslang::EOpAllMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07007531 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice,
John Kessenich82979362017-12-11 04:02:24 -07007532 spv::MemorySemanticsAllMemory |
John Kessenich838d7af2017-12-12 22:50:53 -07007533 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007534 return 0;
John Kessenich838d7af2017-12-12 22:50:53 -07007535 case glslang::EOpDeviceMemoryBarrier:
7536 builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7537 spv::MemorySemanticsImageMemoryMask |
7538 spv::MemorySemanticsAcquireReleaseMask);
7539 return 0;
7540 case glslang::EOpDeviceMemoryBarrierWithGroupSync:
7541 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask |
7542 spv::MemorySemanticsImageMemoryMask |
7543 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007544 return 0;
7545 case glslang::EOpWorkgroupMemoryBarrier:
John Kessenich838d7af2017-12-12 22:50:53 -07007546 builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7547 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007548 return 0;
7549 case glslang::EOpWorkgroupMemoryBarrierWithGroupSync:
John Kessenich838d7af2017-12-12 22:50:53 -07007550 builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup,
7551 spv::MemorySemanticsWorkgroupMemoryMask |
7552 spv::MemorySemanticsAcquireReleaseMask);
LoopDawg6e72fdd2016-06-15 09:50:24 -06007553 return 0;
John Kessenich66011cb2018-03-06 16:12:04 -07007554 case glslang::EOpSubgroupBarrier:
7555 builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7556 spv::MemorySemanticsAcquireReleaseMask);
7557 return spv::NoResult;
7558 case glslang::EOpSubgroupMemoryBarrier:
7559 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory |
7560 spv::MemorySemanticsAcquireReleaseMask);
7561 return spv::NoResult;
7562 case glslang::EOpSubgroupMemoryBarrierBuffer:
7563 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask |
7564 spv::MemorySemanticsAcquireReleaseMask);
7565 return spv::NoResult;
7566 case glslang::EOpSubgroupMemoryBarrierImage:
7567 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask |
7568 spv::MemorySemanticsAcquireReleaseMask);
7569 return spv::NoResult;
7570 case glslang::EOpSubgroupMemoryBarrierShared:
7571 builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask |
7572 spv::MemorySemanticsAcquireReleaseMask);
7573 return spv::NoResult;
7574 case glslang::EOpSubgroupElect: {
7575 std::vector<spv::Id> operands;
7576 return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid);
7577 }
Rex Xu9d93a232016-05-05 12:30:44 +08007578 case glslang::EOpTime:
7579 {
7580 std::vector<spv::Id> args; // Dummy arguments
7581 spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args);
7582 return builder.setPrecision(id, precision);
7583 }
Chao Chenb50c02e2018-09-19 11:42:24 -07007584 case glslang::EOpIgnoreIntersectionNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07007585 builder.createNoResultOp(spv::OpIgnoreIntersectionNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07007586 return 0;
7587 case glslang::EOpTerminateRayNV:
Ashwin Leleff1783d2018-10-22 16:41:44 -07007588 builder.createNoResultOp(spv::OpTerminateRayNV);
Chao Chenb50c02e2018-09-19 11:42:24 -07007589 return 0;
Jeff Bolzc6f0ce82019-06-03 11:33:50 -05007590
7591 case glslang::EOpBeginInvocationInterlock:
7592 builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT);
7593 return 0;
7594 case glslang::EOpEndInvocationInterlock:
7595 builder.createNoResultOp(spv::OpEndInvocationInterlockEXT);
7596 return 0;
7597
Jeff Bolzba6170b2019-07-01 09:23:23 -05007598 case glslang::EOpIsHelperInvocation:
7599 {
7600 std::vector<spv::Id> args; // Dummy arguments
Rex Xubb7307b2019-07-15 14:57:20 +08007601 builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation);
7602 builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT);
7603 return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args);
Jeff Bolzba6170b2019-07-01 09:23:23 -05007604 }
7605
amhagan91fb0092019-07-10 21:14:38 -04007606 case glslang::EOpReadClockSubgroupKHR: {
7607 std::vector<spv::Id> args;
7608 args.push_back(builder.makeUintConstant(spv::ScopeSubgroup));
7609 builder.addExtension(spv::E_SPV_KHR_shader_clock);
7610 builder.addCapability(spv::CapabilityShaderClockKHR);
7611 return builder.createOp(spv::OpReadClockKHR, typeId, args);
7612 }
7613
7614 case glslang::EOpReadClockDeviceKHR: {
7615 std::vector<spv::Id> args;
7616 args.push_back(builder.makeUintConstant(spv::ScopeDevice));
7617 builder.addExtension(spv::E_SPV_KHR_shader_clock);
7618 builder.addCapability(spv::CapabilityShaderClockKHR);
7619 return builder.createOp(spv::OpReadClockKHR, typeId, args);
7620 }
John Kessenich140f3df2015-06-26 16:58:36 -06007621 default:
John Kessenich155d3512019-08-08 23:29:20 -06007622 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007623 }
John Kessenich155d3512019-08-08 23:29:20 -06007624#endif
7625
7626 logger->missingFunctionality("unknown operation with no arguments");
7627
7628 return 0;
John Kessenich140f3df2015-06-26 16:58:36 -06007629}
7630
7631spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
7632{
John Kessenich2f273362015-07-18 22:34:27 -06007633 auto iter = symbolValues.find(symbol->getId());
John Kessenich140f3df2015-06-26 16:58:36 -06007634 spv::Id id;
7635 if (symbolValues.end() != iter) {
7636 id = iter->second;
7637 return id;
7638 }
7639
7640 // it was not found, create it
John Kessenich9c14f772019-06-17 08:38:35 -06007641 spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
7642 auto forcedType = getForcedType(builtIn, symbol->getType());
7643 id = createSpvVariable(symbol, forcedType.first);
John Kessenich140f3df2015-06-26 16:58:36 -06007644 symbolValues[symbol->getId()] = id;
John Kessenich9c14f772019-06-17 08:38:35 -06007645 if (forcedType.second != spv::NoType)
7646 forceType[id] = forcedType.second;
John Kessenich140f3df2015-06-26 16:58:36 -06007647
Rex Xuc884b4a2016-06-29 15:03:44 +08007648 if (symbol->getBasicType() != glslang::EbtBlock) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007649 builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType()));
7650 builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier()));
7651 builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier()));
John Kessenicha28f7a72019-08-06 07:00:58 -06007652#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07007653 addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier());
John Kessenichb9197c82019-08-11 07:41:45 -06007654 if (symbol->getQualifier().hasComponent())
7655 builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent);
7656 if (symbol->getQualifier().hasIndex())
7657 builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex);
Chao Chen3c366992018-09-19 11:41:59 -07007658#endif
John Kessenich6c292d32016-02-15 20:58:50 -07007659 if (symbol->getType().getQualifier().hasSpecConstantId())
John Kessenich5d610ee2018-03-07 18:05:55 -07007660 builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId);
John Kessenich91e4aa52016-07-07 17:46:42 -06007661 // atomic counters use this:
7662 if (symbol->getQualifier().hasOffset())
7663 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06007664 }
7665
scygan2c864272016-05-18 18:09:17 +02007666 if (symbol->getQualifier().hasLocation())
7667 builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation);
John Kessenich5d610ee2018-03-07 18:05:55 -07007668 builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier()));
John Kessenichf2d8a5c2016-03-03 22:29:11 -07007669 if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) {
John Kessenich92187592016-02-01 13:45:25 -07007670 builder.addCapability(spv::CapabilityGeometryStreams);
John Kessenich140f3df2015-06-26 16:58:36 -06007671 builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream);
John Kessenich92187592016-02-01 13:45:25 -07007672 }
John Kessenich140f3df2015-06-26 16:58:36 -06007673 if (symbol->getQualifier().hasSet())
7674 builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet);
John Kessenich6c292d32016-02-15 20:58:50 -07007675 else if (IsDescriptorResource(symbol->getType())) {
7676 // default to 0
7677 builder.addDecoration(id, spv::DecorationDescriptorSet, 0);
7678 }
John Kessenich140f3df2015-06-26 16:58:36 -06007679 if (symbol->getQualifier().hasBinding())
7680 builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding);
Jeff Bolz0a93cfb2018-12-11 20:53:59 -06007681 else if (IsDescriptorResource(symbol->getType())) {
7682 // default to 0
7683 builder.addDecoration(id, spv::DecorationBinding, 0);
7684 }
John Kessenich6c292d32016-02-15 20:58:50 -07007685 if (symbol->getQualifier().hasAttachment())
7686 builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment);
John Kessenich140f3df2015-06-26 16:58:36 -06007687 if (glslangIntermediate->getXfbMode()) {
John Kessenich92187592016-02-01 13:45:25 -07007688 builder.addCapability(spv::CapabilityTransformFeedback);
John Kessenichedaf5562017-12-15 06:21:46 -07007689 if (symbol->getQualifier().hasXfbBuffer()) {
John Kessenich140f3df2015-06-26 16:58:36 -06007690 builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer);
John Kessenichedaf5562017-12-15 06:21:46 -07007691 unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer);
7692 if (stride != glslang::TQualifier::layoutXfbStrideEnd)
7693 builder.addDecoration(id, spv::DecorationXfbStride, stride);
7694 }
7695 if (symbol->getQualifier().hasXfbOffset())
7696 builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset);
John Kessenich140f3df2015-06-26 16:58:36 -06007697 }
7698
John Kessenichb9197c82019-08-11 07:41:45 -06007699 // add built-in variable decoration
7700 if (builtIn != spv::BuiltInMax) {
7701 builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn);
7702 }
7703
7704#ifndef GLSLANG_WEB
Rex Xu1da878f2016-02-21 20:59:01 +08007705 if (symbol->getType().isImage()) {
7706 std::vector<spv::Decoration> memory;
Jeff Bolz36831c92018-09-05 10:11:41 -05007707 TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel());
Rex Xu1da878f2016-02-21 20:59:01 +08007708 for (unsigned int i = 0; i < memory.size(); ++i)
John Kessenich5d610ee2018-03-07 18:05:55 -07007709 builder.addDecoration(id, memory[i]);
Rex Xu1da878f2016-02-21 20:59:01 +08007710 }
7711
John Kessenich5611c6d2018-04-05 11:25:02 -06007712 // nonuniform
7713 builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier()));
7714
chaoc0ad6a4e2016-12-19 16:29:34 -08007715 if (builtIn == spv::BuiltInSampleMask) {
7716 spv::Decoration decoration;
7717 // GL_NV_sample_mask_override_coverage extension
7718 if (glslangIntermediate->getLayoutOverrideCoverage())
chaoc771d89f2017-01-13 01:10:53 -08007719 decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV;
chaoc0ad6a4e2016-12-19 16:29:34 -08007720 else
7721 decoration = (spv::Decoration)spv::DecorationMax;
John Kessenich5d610ee2018-03-07 18:05:55 -07007722 builder.addDecoration(id, decoration);
chaoc0ad6a4e2016-12-19 16:29:34 -08007723 if (decoration != spv::DecorationMax) {
Jason Macnakdbd4c3c2019-07-12 14:33:02 -07007724 builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV);
chaoc0ad6a4e2016-12-19 16:29:34 -08007725 builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage);
7726 }
7727 }
chaoc771d89f2017-01-13 01:10:53 -08007728 else if (builtIn == spv::BuiltInLayer) {
7729 // SPV_NV_viewport_array2 extension
John Kessenichb41bff62017-08-11 13:07:17 -06007730 if (symbol->getQualifier().layoutViewportRelative) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007731 builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV);
chaoc771d89f2017-01-13 01:10:53 -08007732 builder.addCapability(spv::CapabilityShaderViewportMaskNV);
7733 builder.addExtension(spv::E_SPV_NV_viewport_array2);
7734 }
John Kessenichb41bff62017-08-11 13:07:17 -06007735 if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007736 builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV,
7737 symbol->getQualifier().layoutSecondaryViewportRelativeOffset);
chaoc771d89f2017-01-13 01:10:53 -08007738 builder.addCapability(spv::CapabilityShaderStereoViewNV);
7739 builder.addExtension(spv::E_SPV_NV_stereo_view_rendering);
7740 }
7741 }
7742
chaoc6e5acae2016-12-20 13:28:52 -08007743 if (symbol->getQualifier().layoutPassthrough) {
John Kessenich5d610ee2018-03-07 18:05:55 -07007744 builder.addDecoration(id, spv::DecorationPassthroughNV);
chaoc771d89f2017-01-13 01:10:53 -08007745 builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV);
chaoc6e5acae2016-12-20 13:28:52 -08007746 builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough);
7747 }
Chao Chen9eada4b2018-09-19 11:39:56 -07007748 if (symbol->getQualifier().pervertexNV) {
7749 builder.addDecoration(id, spv::DecorationPerVertexNV);
7750 builder.addCapability(spv::CapabilityFragmentBarycentricNV);
7751 builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric);
7752 }
chaoc0ad6a4e2016-12-19 16:29:34 -08007753
John Kessenich5d610ee2018-03-07 18:05:55 -07007754 if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) {
7755 builder.addExtension("SPV_GOOGLE_hlsl_functionality1");
7756 builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE,
7757 symbol->getType().getQualifier().semanticName);
7758 }
7759
John Kessenich7015bd62019-08-01 03:28:08 -06007760 if (symbol->isReference()) {
Jeff Bolz9f2aec42019-01-06 17:58:04 -06007761 builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT);
7762 }
John Kessenichb9197c82019-08-11 07:41:45 -06007763#endif
Jeff Bolz9f2aec42019-01-06 17:58:04 -06007764
John Kessenich140f3df2015-06-26 16:58:36 -06007765 return id;
7766}
7767
John Kessenicha28f7a72019-08-06 07:00:58 -06007768#ifndef GLSLANG_WEB
Chao Chen3c366992018-09-19 11:41:59 -07007769// add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object
7770void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
7771{
7772 if (member >= 0) {
Sahil Parmar38772c02018-10-25 23:50:59 -07007773 if (qualifier.perPrimitiveNV) {
7774 // Need to add capability/extension for fragment shader.
7775 // Mesh shader already adds this by default.
7776 if (glslangIntermediate->getStage() == EShLangFragment) {
7777 builder.addCapability(spv::CapabilityMeshShadingNV);
7778 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7779 }
Chao Chen3c366992018-09-19 11:41:59 -07007780 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07007781 }
Chao Chen3c366992018-09-19 11:41:59 -07007782 if (qualifier.perViewNV)
7783 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV);
7784 if (qualifier.perTaskNV)
7785 builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV);
7786 } else {
Sahil Parmar38772c02018-10-25 23:50:59 -07007787 if (qualifier.perPrimitiveNV) {
7788 // Need to add capability/extension for fragment shader.
7789 // Mesh shader already adds this by default.
7790 if (glslangIntermediate->getStage() == EShLangFragment) {
7791 builder.addCapability(spv::CapabilityMeshShadingNV);
7792 builder.addExtension(spv::E_SPV_NV_mesh_shader);
7793 }
Chao Chen3c366992018-09-19 11:41:59 -07007794 builder.addDecoration(id, spv::DecorationPerPrimitiveNV);
Sahil Parmar38772c02018-10-25 23:50:59 -07007795 }
Chao Chen3c366992018-09-19 11:41:59 -07007796 if (qualifier.perViewNV)
7797 builder.addDecoration(id, spv::DecorationPerViewNV);
7798 if (qualifier.perTaskNV)
7799 builder.addDecoration(id, spv::DecorationPerTaskNV);
7800 }
7801}
7802#endif
7803
John Kessenich55e7d112015-11-15 21:33:39 -07007804// Make a full tree of instructions to build a SPIR-V specialization constant,
John Kessenich6c292d32016-02-15 20:58:50 -07007805// or regular constant if possible.
John Kessenich55e7d112015-11-15 21:33:39 -07007806//
7807// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though
7808//
7809// Recursively walk the nodes. The nodes form a tree whose leaves are
7810// regular constants, which themselves are trees that createSpvConstant()
7811// recursively walks. So, this function walks the "top" of the tree:
7812// - emit specialization constant-building instructions for specConstant
7813// - when running into a non-spec-constant, switch to createSpvConstant()
qining08408382016-03-21 09:51:37 -04007814spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
John Kessenich55e7d112015-11-15 21:33:39 -07007815{
John Kessenich7cc0e282016-03-20 00:46:02 -06007816 assert(node.getQualifier().isConstant());
John Kessenich55e7d112015-11-15 21:33:39 -07007817
qining4f4bb812016-04-03 23:55:17 -04007818 // Handle front-end constants first (non-specialization constants).
John Kessenich6c292d32016-02-15 20:58:50 -07007819 if (! node.getQualifier().specConstant) {
7820 // hand off to the non-spec-constant path
7821 assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr);
7822 int nextConst = 0;
qining08408382016-03-21 09:51:37 -04007823 return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(),
John Kessenich6c292d32016-02-15 20:58:50 -07007824 nextConst, false);
7825 }
7826
7827 // We now know we have a specialization constant to build
7828
John Kessenich155d3512019-08-08 23:29:20 -06007829#ifndef GLSLANG_WEB
John Kessenichd94c0032016-05-30 19:29:40 -06007830 // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants,
qining4f4bb812016-04-03 23:55:17 -04007831 // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ...
7832 if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) {
7833 std::vector<spv::Id> dimConstId;
7834 for (int dim = 0; dim < 3; ++dim) {
7835 bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet);
7836 dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst));
John Kessenich5d610ee2018-03-07 18:05:55 -07007837 if (specConst) {
7838 builder.addDecoration(dimConstId.back(), spv::DecorationSpecId,
7839 glslangIntermediate->getLocalSizeSpecId(dim));
7840 }
qining4f4bb812016-04-03 23:55:17 -04007841 }
7842 return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true);
7843 }
John Kessenich155d3512019-08-08 23:29:20 -06007844#endif
qining4f4bb812016-04-03 23:55:17 -04007845
7846 // An AST node labelled as specialization constant should be a symbol node.
7847 // Its initializer should either be a sub tree with constant nodes, or a constant union array.
7848 if (auto* sn = node.getAsSymbolNode()) {
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007849 spv::Id result;
qining4f4bb812016-04-03 23:55:17 -04007850 if (auto* sub_tree = sn->getConstSubtree()) {
qining27e04a02016-04-14 16:40:20 -04007851 // Traverse the constant constructor sub tree like generating normal run-time instructions.
7852 // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
7853 // will set the builder into spec constant op instruction generating mode.
7854 sub_tree->traverse(this);
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007855 result = accessChainLoad(sub_tree->getType());
7856 } else if (auto* const_union_array = &sn->getConstArray()) {
qining4f4bb812016-04-03 23:55:17 -04007857 int nextConst = 0;
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007858 result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
Dan Sinclair70661b92018-11-12 13:56:52 -05007859 } else {
7860 logger->missingFunctionality("Invalid initializer for spec onstant.");
Dan Sinclair70661b92018-11-12 13:56:52 -05007861 return spv::NoResult;
John Kessenich6c292d32016-02-15 20:58:50 -07007862 }
Grigory Dzhavadyan4c9876b2018-10-29 22:56:44 -07007863 builder.addName(result, sn->getName().c_str());
7864 return result;
John Kessenich6c292d32016-02-15 20:58:50 -07007865 }
qining4f4bb812016-04-03 23:55:17 -04007866
7867 // Neither a front-end constant node, nor a specialization constant node with constant union array or
7868 // constant sub tree as initializer.
Lei Zhang17535f72016-05-04 15:55:59 -04007869 logger->missingFunctionality("Neither a front-end constant nor a spec constant.");
qining4f4bb812016-04-03 23:55:17 -04007870 return spv::NoResult;
John Kessenich55e7d112015-11-15 21:33:39 -07007871}
7872
John Kessenich140f3df2015-06-26 16:58:36 -06007873// Use 'consts' as the flattened glslang source of scalar constants to recursively
7874// build the aggregate SPIR-V constant.
7875//
7876// If there are not enough elements present in 'consts', 0 will be substituted;
7877// an empty 'consts' can be used to create a fully zeroed SPIR-V constant.
7878//
qining08408382016-03-21 09:51:37 -04007879spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
John Kessenich140f3df2015-06-26 16:58:36 -06007880{
7881 // vector of constants for SPIR-V
7882 std::vector<spv::Id> spvConsts;
7883
7884 // Type is used for struct and array constants
7885 spv::Id typeId = convertGlslangToSpvType(glslangType);
7886
7887 if (glslangType.isArray()) {
John Kessenich65c78a02015-08-10 17:08:55 -06007888 glslang::TType elementType(glslangType, 0);
7889 for (int i = 0; i < glslangType.getOuterArraySize(); ++i)
qining08408382016-03-21 09:51:37 -04007890 spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false));
John Kessenich140f3df2015-06-26 16:58:36 -06007891 } else if (glslangType.isMatrix()) {
John Kessenich65c78a02015-08-10 17:08:55 -06007892 glslang::TType vectorType(glslangType, 0);
John Kessenich140f3df2015-06-26 16:58:36 -06007893 for (int col = 0; col < glslangType.getMatrixCols(); ++col)
qining08408382016-03-21 09:51:37 -04007894 spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false));
Jeff Bolz4605e2e2019-02-19 13:10:32 -06007895 } else if (glslangType.isCoopMat()) {
7896 glslang::TType componentType(glslangType.getBasicType());
7897 spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false));
Jeff Bolz9f2aec42019-01-06 17:58:04 -06007898 } else if (glslangType.isStruct()) {
John Kessenich140f3df2015-06-26 16:58:36 -06007899 glslang::TVector<glslang::TTypeLoc>::const_iterator iter;
7900 for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter)
qining08408382016-03-21 09:51:37 -04007901 spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false));
John Kessenich8d72f1a2016-05-20 12:06:03 -06007902 } else if (glslangType.getVectorSize() > 1) {
John Kessenich140f3df2015-06-26 16:58:36 -06007903 for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) {
7904 bool zero = nextConst >= consts.size();
7905 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06007906 case glslang::EbtInt:
7907 spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst()));
7908 break;
7909 case glslang::EbtUint:
7910 spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst()));
7911 break;
7912 case glslang::EbtFloat:
7913 spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7914 break;
7915 case glslang::EbtBool:
7916 spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst()));
7917 break;
7918#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07007919 case glslang::EbtInt8:
7920 spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const()));
7921 break;
7922 case glslang::EbtUint8:
7923 spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const()));
7924 break;
7925 case glslang::EbtInt16:
7926 spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const()));
7927 break;
7928 case glslang::EbtUint16:
7929 spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const()));
7930 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08007931 case glslang::EbtInt64:
7932 spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const()));
7933 break;
7934 case glslang::EbtUint64:
7935 spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const()));
7936 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007937 case glslang::EbtDouble:
7938 spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst()));
7939 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08007940 case glslang::EbtFloat16:
7941 spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst()));
7942 break;
John Kessenich39697cd2019-08-08 10:35:51 -06007943#endif
John Kessenich140f3df2015-06-26 16:58:36 -06007944 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007945 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06007946 break;
7947 }
7948 ++nextConst;
7949 }
7950 } else {
7951 // we have a non-aggregate (scalar) constant
7952 bool zero = nextConst >= consts.size();
7953 spv::Id scalar = 0;
7954 switch (glslangType.getBasicType()) {
John Kessenich39697cd2019-08-08 10:35:51 -06007955 case glslang::EbtInt:
7956 scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant);
7957 break;
7958 case glslang::EbtUint:
7959 scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant);
7960 break;
7961 case glslang::EbtFloat:
7962 scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
7963 break;
7964 case glslang::EbtBool:
7965 scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant);
7966 break;
7967#ifndef GLSLANG_WEB
John Kessenich66011cb2018-03-06 16:12:04 -07007968 case glslang::EbtInt8:
7969 scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant);
7970 break;
7971 case glslang::EbtUint8:
7972 scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant);
7973 break;
7974 case glslang::EbtInt16:
7975 scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant);
7976 break;
7977 case glslang::EbtUint16:
7978 scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant);
7979 break;
Rex Xu8ff43de2016-04-22 16:51:45 +08007980 case glslang::EbtInt64:
7981 scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant);
7982 break;
7983 case glslang::EbtUint64:
7984 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
7985 break;
John Kessenich140f3df2015-06-26 16:58:36 -06007986 case glslang::EbtDouble:
John Kessenich55e7d112015-11-15 21:33:39 -07007987 scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant);
John Kessenich140f3df2015-06-26 16:58:36 -06007988 break;
Rex Xuc9e3c3c2016-07-29 16:00:05 +08007989 case glslang::EbtFloat16:
7990 scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant);
7991 break;
Jeff Bolz3fd12322019-03-05 23:27:09 -06007992 case glslang::EbtReference:
7993 scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant);
7994 scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar);
7995 break;
John Kessenich39697cd2019-08-08 10:35:51 -06007996#endif
John Kessenich140f3df2015-06-26 16:58:36 -06007997 default:
John Kessenich55e7d112015-11-15 21:33:39 -07007998 assert(0);
John Kessenich140f3df2015-06-26 16:58:36 -06007999 break;
8000 }
8001 ++nextConst;
8002 return scalar;
8003 }
8004
8005 return builder.makeCompositeConstant(typeId, spvConsts);
8006}
8007
John Kessenich7c1aa102015-10-15 13:29:11 -06008008// Return true if the node is a constant or symbol whose reading has no
8009// non-trivial observable cost or effect.
8010bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
8011{
8012 // don't know what this is
8013 if (node == nullptr)
8014 return false;
8015
8016 // a constant is safe
8017 if (node->getAsConstantUnion() != nullptr)
8018 return true;
8019
8020 // not a symbol means non-trivial
8021 if (node->getAsSymbolNode() == nullptr)
8022 return false;
8023
8024 // a symbol, depends on what's being read
8025 switch (node->getType().getQualifier().storage) {
8026 case glslang::EvqTemporary:
8027 case glslang::EvqGlobal:
8028 case glslang::EvqIn:
8029 case glslang::EvqInOut:
8030 case glslang::EvqConst:
8031 case glslang::EvqConstReadOnly:
8032 case glslang::EvqUniform:
8033 return true;
8034 default:
8035 return false;
8036 }
qining25262b32016-05-06 17:25:16 -04008037}
John Kessenich7c1aa102015-10-15 13:29:11 -06008038
8039// A node is trivial if it is a single operation with no side effects.
John Kessenich84cc15f2017-05-24 16:44:47 -06008040// HLSL (and/or vectors) are always trivial, as it does not short circuit.
John Kessenich0d2b4712017-05-19 20:19:00 -06008041// Otherwise, error on the side of saying non-trivial.
John Kessenich7c1aa102015-10-15 13:29:11 -06008042// Return true if trivial.
8043bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
8044{
8045 if (node == nullptr)
8046 return false;
8047
John Kessenich84cc15f2017-05-24 16:44:47 -06008048 // count non scalars as trivial, as well as anything coming from HLSL
8049 if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl)
John Kessenich0d2b4712017-05-19 20:19:00 -06008050 return true;
8051
John Kessenich7c1aa102015-10-15 13:29:11 -06008052 // symbols and constants are trivial
8053 if (isTrivialLeaf(node))
8054 return true;
8055
8056 // otherwise, it needs to be a simple operation or one or two leaf nodes
8057
8058 // not a simple operation
8059 const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode();
8060 const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode();
8061 if (binaryNode == nullptr && unaryNode == nullptr)
8062 return false;
8063
8064 // not on leaf nodes
8065 if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight())))
8066 return false;
8067
8068 if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) {
8069 return false;
8070 }
8071
8072 switch (node->getAsOperator()->getOp()) {
8073 case glslang::EOpLogicalNot:
8074 case glslang::EOpConvIntToBool:
8075 case glslang::EOpConvUintToBool:
8076 case glslang::EOpConvFloatToBool:
8077 case glslang::EOpConvDoubleToBool:
8078 case glslang::EOpEqual:
8079 case glslang::EOpNotEqual:
8080 case glslang::EOpLessThan:
8081 case glslang::EOpGreaterThan:
8082 case glslang::EOpLessThanEqual:
8083 case glslang::EOpGreaterThanEqual:
8084 case glslang::EOpIndexDirect:
8085 case glslang::EOpIndexDirectStruct:
8086 case glslang::EOpLogicalXor:
8087 case glslang::EOpAny:
8088 case glslang::EOpAll:
8089 return true;
8090 default:
8091 return false;
8092 }
8093}
8094
8095// Emit short-circuiting code, where 'right' is never evaluated unless
8096// the left side is true (for &&) or false (for ||).
8097spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right)
8098{
8099 spv::Id boolTypeId = builder.makeBoolType();
8100
8101 // emit left operand
8102 builder.clearAccessChain();
8103 left.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008104 spv::Id leftId = accessChainLoad(left.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008105
8106 // Operands to accumulate OpPhi operands
8107 std::vector<spv::Id> phiOperands;
8108 // accumulate left operand's phi information
8109 phiOperands.push_back(leftId);
8110 phiOperands.push_back(builder.getBuildPoint()->getId());
8111
8112 // Make the two kinds of operation symmetric with a "!"
8113 // || => emit "if (! left) result = right"
8114 // && => emit "if ( left) result = right"
8115 //
8116 // TODO: this runtime "not" for || could be avoided by adding functionality
8117 // to 'builder' to have an "else" without an "then"
8118 if (op == glslang::EOpLogicalOr)
8119 leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId);
8120
8121 // make an "if" based on the left value
Rex Xu57e65922017-07-04 23:23:40 +08008122 spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder);
John Kessenich7c1aa102015-10-15 13:29:11 -06008123
8124 // emit right operand as the "then" part of the "if"
8125 builder.clearAccessChain();
8126 right.traverse(this);
Rex Xub4fd8d12016-03-03 14:38:51 +08008127 spv::Id rightId = accessChainLoad(right.getType());
John Kessenich7c1aa102015-10-15 13:29:11 -06008128
8129 // accumulate left operand's phi information
8130 phiOperands.push_back(rightId);
8131 phiOperands.push_back(builder.getBuildPoint()->getId());
8132
8133 // finish the "if"
8134 ifBuilder.makeEndIf();
8135
8136 // phi together the two results
8137 return builder.createOp(spv::OpPhi, boolTypeId, phiOperands);
8138}
8139
John Kessenicha28f7a72019-08-06 07:00:58 -06008140#ifndef GLSLANG_WEB
Rex Xu9d93a232016-05-05 12:30:44 +08008141// Return type Id of the imported set of extended instructions corresponds to the name.
8142// Import this set if it has not been imported yet.
8143spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
8144{
8145 if (extBuiltinMap.find(name) != extBuiltinMap.end())
8146 return extBuiltinMap[name];
8147 else {
Rex Xu51596642016-09-21 18:56:12 +08008148 builder.addExtension(name);
Rex Xu9d93a232016-05-05 12:30:44 +08008149 spv::Id extBuiltins = builder.import(name);
8150 extBuiltinMap[name] = extBuiltins;
8151 return extBuiltins;
8152 }
8153}
Frank Henigman541f7bb2018-01-16 00:18:26 -05008154#endif
Rex Xu9d93a232016-05-05 12:30:44 +08008155
John Kessenich140f3df2015-06-26 16:58:36 -06008156}; // end anonymous namespace
8157
8158namespace glslang {
8159
John Kessenich68d78fd2015-07-12 19:28:10 -06008160void GetSpirvVersion(std::string& version)
8161{
John Kessenich9e55f632015-07-15 10:03:39 -06008162 const int bufSize = 100;
John Kessenichf98ee232015-07-12 19:39:51 -06008163 char buf[bufSize];
John Kessenich55e7d112015-11-15 21:33:39 -07008164 snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision);
John Kessenich68d78fd2015-07-12 19:28:10 -06008165 version = buf;
8166}
8167
John Kessenicha372a3e2017-11-02 22:32:14 -06008168// For low-order part of the generator's magic number. Bump up
8169// when there is a change in the style (e.g., if SSA form changes,
8170// or a different instruction sequence to do something gets used).
8171int GetSpirvGeneratorVersion()
8172{
John Kessenich3f0d4bc2017-12-16 23:46:37 -07008173 // return 1; // start
8174 // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V
John Kessenich71b5da62018-02-06 08:06:36 -07008175 // return 3; // change/correct barrier-instruction operands, to match memory model group decisions
John Kessenich0216f242018-03-03 11:47:07 -07008176 // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component
John Kessenichac370792018-03-07 11:24:50 -07008177 // return 5; // make OpArrayLength result type be an int with signedness of 0
John Kessenichd6c97552018-06-04 15:33:31 -06008178 // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code,
8179 // versions 4 and 6 each generate OpArrayLength as it has long been done
8180 return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent
John Kessenicha372a3e2017-11-02 22:32:14 -06008181}
8182
John Kessenich140f3df2015-06-26 16:58:36 -06008183// Write SPIR-V out to a binary file
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008184void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
John Kessenich140f3df2015-06-26 16:58:36 -06008185{
8186 std::ofstream out;
John Kessenich68d78fd2015-07-12 19:28:10 -06008187 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008188 if (out.fail())
8189 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenich140f3df2015-06-26 16:58:36 -06008190 for (int i = 0; i < (int)spirv.size(); ++i) {
8191 unsigned int word = spirv[i];
8192 out.write((const char*)&word, 4);
8193 }
8194 out.close();
8195}
8196
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008197// Write SPIR-V out to a text file with 32-bit hexadecimal words
Flavioaea3c892017-02-06 11:46:35 -08008198void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008199{
John Kessenich155d3512019-08-08 23:29:20 -06008200#ifndef GLSLANG_WEB
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008201 std::ofstream out;
8202 out.open(baseName, std::ios::binary | std::ios::out);
John Kessenich8f674e82017-02-18 09:45:40 -07008203 if (out.fail())
8204 printf("ERROR: Failed to open file: %s\n", baseName);
John Kessenichc6c80a62018-03-05 22:23:17 -07008205 out << "\t// " <<
John Kessenich4e11b612018-08-30 16:56:59 -06008206 GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL <<
John Kessenichc6c80a62018-03-05 22:23:17 -07008207 std::endl;
Flavio15017db2017-02-15 14:29:33 -08008208 if (varName != nullptr) {
8209 out << "\t #pragma once" << std::endl;
8210 out << "const uint32_t " << varName << "[] = {" << std::endl;
8211 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008212 const int WORDS_PER_LINE = 8;
8213 for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) {
8214 out << "\t";
8215 for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) {
8216 const unsigned int word = spirv[i + j];
8217 out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word;
8218 if (i + j + 1 < (int)spirv.size()) {
8219 out << ",";
8220 }
8221 }
8222 out << std::endl;
8223 }
Flavio15017db2017-02-15 14:29:33 -08008224 if (varName != nullptr) {
8225 out << "};";
8226 }
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008227 out.close();
John Kessenich155d3512019-08-08 23:29:20 -06008228#endif
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05008229}
8230
John Kessenich140f3df2015-06-26 16:58:36 -06008231//
8232// Set up the glslang traversal
8233//
John Kessenich4e11b612018-08-30 16:56:59 -06008234void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
John Kessenich140f3df2015-06-26 16:58:36 -06008235{
Lei Zhang17535f72016-05-04 15:55:59 -04008236 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06008237 GlslangToSpv(intermediate, spirv, &logger, options);
Lei Zhang09caf122016-05-02 18:11:54 -04008238}
8239
John Kessenich4e11b612018-08-30 16:56:59 -06008240void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
John Kessenich121853f2017-05-31 17:11:16 -06008241 spv::SpvBuildLogger* logger, SpvOptions* options)
Lei Zhang09caf122016-05-02 18:11:54 -04008242{
John Kessenich140f3df2015-06-26 16:58:36 -06008243 TIntermNode* root = intermediate.getTreeRoot();
8244
8245 if (root == 0)
8246 return;
8247
John Kessenich4e11b612018-08-30 16:56:59 -06008248 SpvOptions defaultOptions;
John Kessenich121853f2017-05-31 17:11:16 -06008249 if (options == nullptr)
8250 options = &defaultOptions;
8251
John Kessenich4e11b612018-08-30 16:56:59 -06008252 GetThreadPoolAllocator().push();
John Kessenich140f3df2015-06-26 16:58:36 -06008253
John Kessenich2b5ea9f2018-01-31 18:35:56 -07008254 TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
John Kessenich140f3df2015-06-26 16:58:36 -06008255 root->traverse(&it);
John Kessenichfca82622016-11-26 13:23:20 -07008256 it.finishSpv();
John Kessenich140f3df2015-06-26 16:58:36 -06008257 it.dumpSpv(spirv);
8258
GregFfb03a552018-03-29 11:49:14 -06008259#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -06008260 // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
8261 // eg. forward and remove memory writes of opaque types.
Jeff Bolzfd556e32019-06-07 14:42:08 -05008262 bool prelegalization = intermediate.getSource() == EShSourceHlsl;
8263 if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) {
John Kesseniche7df8e02018-08-22 17:12:46 -06008264 SpirvToolsLegalize(intermediate, spirv, logger, options);
Jeff Bolzfd556e32019-06-07 14:42:08 -05008265 prelegalization = false;
8266 }
John Kessenich717c80a2018-08-23 15:17:10 -06008267
John Kessenich4e11b612018-08-30 16:56:59 -06008268 if (options->validate)
Jeff Bolzfd556e32019-06-07 14:42:08 -05008269 SpirvToolsValidate(intermediate, spirv, logger, prelegalization);
John Kessenich4e11b612018-08-30 16:56:59 -06008270
John Kessenich717c80a2018-08-23 15:17:10 -06008271 if (options->disassemble)
John Kessenich4e11b612018-08-30 16:56:59 -06008272 SpirvToolsDisassemble(std::cout, spirv);
John Kessenich717c80a2018-08-23 15:17:10 -06008273
GregFcd1f1692017-09-21 18:40:22 -06008274#endif
8275
John Kessenich4e11b612018-08-30 16:56:59 -06008276 GetThreadPoolAllocator().pop();
John Kessenich140f3df2015-06-26 16:58:36 -06008277}
8278
8279}; // end namespace glslang