blob: 68e3d315fbda44ae6314a748f40bc20df11cafec [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_SPIRVCODEGENERATOR
9#define SKSL_SPIRVCODEGENERATOR
10
ethannicholasb3058bd2016-07-01 08:22:01 -070011#include <stack>
12#include <tuple>
13#include <unordered_map>
14
Ethan Nicholasdaed2592021-03-04 14:30:25 -050015#include "include/private/SkSLModifiers.h"
Ethan Nicholas24c17722021-03-09 13:10:59 -050016#include "include/private/SkSLProgramElement.h"
17#include "include/private/SkSLStatement.h"
John Stilescd806892021-01-06 13:33:31 -050018#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLMemoryLayout.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050020#include "src/sksl/SkSLStringStream.h"
John Stiles3738ef52021-04-13 10:41:57 -040021#include "src/sksl/codegen/SkSLCodeGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/sksl/ir/SkSLBinaryExpression.h"
23#include "src/sksl/ir/SkSLBoolLiteral.h"
24#include "src/sksl/ir/SkSLConstructor.h"
John Stiles7384b372021-04-01 13:48:15 -040025#include "src/sksl/ir/SkSLConstructorArray.h"
John Stiles8cad6372021-04-07 12:31:13 -040026#include "src/sksl/ir/SkSLConstructorCompound.h"
27#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
John Stilese1182782021-03-30 22:09:37 -040028#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040029#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040030#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040031#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesd47330f2021-04-08 23:25:52 -040032#include "src/sksl/ir/SkSLConstructorStruct.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLDoStatement.h"
34#include "src/sksl/ir/SkSLFieldAccess.h"
35#include "src/sksl/ir/SkSLFloatLiteral.h"
36#include "src/sksl/ir/SkSLForStatement.h"
37#include "src/sksl/ir/SkSLFunctionCall.h"
38#include "src/sksl/ir/SkSLFunctionDeclaration.h"
39#include "src/sksl/ir/SkSLFunctionDefinition.h"
40#include "src/sksl/ir/SkSLIfStatement.h"
41#include "src/sksl/ir/SkSLIndexExpression.h"
42#include "src/sksl/ir/SkSLIntLiteral.h"
43#include "src/sksl/ir/SkSLInterfaceBlock.h"
44#include "src/sksl/ir/SkSLPostfixExpression.h"
45#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/sksl/ir/SkSLSwitchStatement.h"
48#include "src/sksl/ir/SkSLSwizzle.h"
49#include "src/sksl/ir/SkSLTernaryExpression.h"
50#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070053
John Stilescd806892021-01-06 13:33:31 -050054namespace SkSL {
55
John Stilesbdc3d3c2021-01-06 18:41:40 -050056struct SPIRVNumberConstant {
57 bool operator==(const SPIRVNumberConstant& that) const {
58 return fValueBits == that.fValueBits &&
59 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040060 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050061 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
62 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040063};
64
John Stilescd806892021-01-06 13:33:31 -050065struct SPIRVVectorConstant {
66 bool operator==(const SPIRVVectorConstant& that) const {
67 return fTypeId == that.fTypeId &&
68 fValueId[0] == that.fValueId[0] &&
69 fValueId[1] == that.fValueId[1] &&
70 fValueId[2] == that.fValueId[2] &&
71 fValueId[3] == that.fValueId[3];
72 }
73 SpvId fTypeId;
74 SpvId fValueId[4];
75};
76
77} // namespace SkSL
78
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040079namespace std {
80
81template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050082struct hash<SkSL::SPIRVNumberConstant> {
83 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
84 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040085 }
86};
87
John Stilescd806892021-01-06 13:33:31 -050088template <>
89struct hash<SkSL::SPIRVVectorConstant> {
90 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
91 return SkOpts::hash(&key, sizeof(key));
92 }
93};
94
John Stilesa6841be2020-08-06 14:11:56 -040095} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040096
ethannicholasb3058bd2016-07-01 08:22:01 -070097namespace SkSL {
98
ethannicholasb3058bd2016-07-01 08:22:01 -070099/**
100 * Converts a Program into a SPIR-V binary.
101 */
102class SPIRVCodeGenerator : public CodeGenerator {
103public:
104 class LValue {
105 public:
106 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500107
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500109 // by a pointer (e.g. vector swizzles), returns -1.
110 virtual SpvId getPointer() { return -1; }
111
Ethan Nicholase0707b72021-03-17 11:16:41 -0400112 // Returns true if a valid pointer returned by getPointer represents a memory object
113 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
114 // getPointer() returns -1.
115 virtual bool isMemoryObjectPointer() const { return true; }
116
John Stiles3f14d282021-02-05 09:31:04 -0500117 // Applies a swizzle to the components of the LValue, if possible. This is used to create
118 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
119 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
120 return false;
121 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700122
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400123 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700124
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400125 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700126 };
127
Brian Osman8b43dad2020-10-09 13:31:42 -0400128 SPIRVCodeGenerator(const Context* context,
129 const Program* program,
130 ErrorReporter* errors,
131 OutputStream* out)
132 : INHERITED(program, errors, out)
133 , fContext(*context)
134 , fDefaultLayout(MemoryLayout::k140_Standard)
135 , fCapabilities(0)
136 , fIdCount(1)
137 , fBoolTrue(0)
138 , fBoolFalse(0)
139 , fSetupFragPosition(false)
140 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400141 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700142 this->setupIntrinsics();
143 }
144
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500145 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700146
147private:
John Stilesaaac4e42021-05-06 14:08:28 -0400148 enum IntrinsicOpcodeKind {
149 kGLSL_STD_450_IntrinsicOpcodeKind,
150 kSPIRV_IntrinsicOpcodeKind,
151 kSpecial_IntrinsicOpcodeKind
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 };
153
154 enum SpecialIntrinsic {
155 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500156 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500157 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500158 kMax_SpecialIntrinsic,
159 kMin_SpecialIntrinsic,
160 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500161 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700162 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400163 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400164 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500165 kSmoothStep_SpecialIntrinsic,
166 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500167 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400168 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700169 };
170
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400171 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400172 kDefault,
173 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400174 };
175
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 void setupIntrinsics();
177
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400178 /**
179 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
180 * appropriate, or null to never add one.
181 */
182 SpvId nextId(const Type* type);
183
184 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700185
Ethan Nicholase2c49992020-10-05 11:49:11 -0400186 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400187
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 SpvId getType(const Type& type);
189
ethannicholas8ac838d2016-11-22 08:39:36 -0800190 SpvId getType(const Type& type, const MemoryLayout& layout);
191
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400192 SpvId getImageType(const Type& type);
193
ethannicholasd598f792016-07-25 10:08:54 -0700194 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195
ethannicholasd598f792016-07-25 10:08:54 -0700196 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700197
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500198 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800199 SpvStorageClass_ storageClass);
200
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400201 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
203 void writeLayout(const Layout& layout, SpvId target);
204
205 void writeLayout(const Layout& layout, SpvId target, int member);
206
ethannicholas8ac838d2016-11-22 08:39:36 -0800207 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700208
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400209 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700210
Brian Salomond8d85b92021-07-07 09:41:17 -0400211 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTFlip = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700212
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400213 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700214
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400215 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700216
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400217 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700218
John Stilesdbd4e6f2021-02-16 13:29:15 -0500219 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700220
Brian Osmanc0213602020-10-06 14:43:32 -0400221 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700222
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400223 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700224
John Stilese40d1662021-01-29 10:08:50 -0500225 int findUniformFieldIndex(const Variable& var) const;
226
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400227 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700228
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400229 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700230
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400231 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
232
233 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
234
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500235
236 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
237 SpvId signedInst, SpvId unsignedInst,
238 const std::vector<SpvId>& args, OutputStream& out);
239
240 /**
Brian Salomond8d85b92021-07-07 09:41:17 -0400241 * Promotes an expression to a vector. If the expression is already a vector with vectorSize
242 * columns, returns it unmodified. If the expression is a scalar, either promotes it to a
243 * vector (if vectorSize > 1) or returns it unmodified (if vectorSize == 1). Asserts if the
244 * expression is already a vector and it does not have vectorSize columns.
245 */
246 SpvId vectorize(const Expression& expr, int vectorSize, OutputStream& out);
247
248 /**
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500249 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
250 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
251 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
252 * vec2, vec3).
253 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400254 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500255
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400256 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700257
John Stiles2938eea2021-04-01 18:58:25 -0400258 SpvId writeConstantVector(const AnyConstructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700259
John Stilesa91bf052021-05-17 09:34:03 -0400260 SpvId writeScalarToMatrixSplat(const Type& matrixType, SpvId scalarId, OutputStream& out);
261
John Stilesfd7252f2021-04-04 22:24:40 -0400262 SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700263
John Stilesd9d52712021-01-13 17:15:02 -0500264 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
265 OutputStream& out);
266
John Stilesfd7252f2021-04-04 22:24:40 -0400267 SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500268
John Stilesd9d52712021-01-13 17:15:02 -0500269 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
270 OutputStream& out);
271
John Stilesfd7252f2021-04-04 22:24:40 -0400272 SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400273
John Stilesd9d52712021-01-13 17:15:02 -0500274 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
275 OutputStream& out);
276
John Stilesfd7252f2021-04-04 22:24:40 -0400277 SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out);
John Stilesa60fb172021-01-14 11:06:20 -0500278
John Stiles48c28842021-01-14 11:05:03 -0500279 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
280 OutputStream& out);
281
John Stilesb14a8192021-04-05 11:40:46 -0400282 SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType,
283 OutputStream& out);
284
Ethan Nicholas84645e32017-02-09 13:57:14 -0500285 /**
286 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
287 * entries equal to zero.
288 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400289 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500290
291 /**
292 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
293 * source matrix are filled with zero; entries which do not exist in the destination matrix are
294 * ignored.
295 */
John Stiles268a73f2021-04-07 12:30:22 -0400296 SpvId writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500297
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400298 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400299 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
300 OutputStream& out);
301
John Stiles8cad6372021-04-07 12:31:13 -0400302 SpvId writeConstructorCompound(const ConstructorCompound& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700303
John Stiles8cad6372021-04-07 12:31:13 -0400304 SpvId writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out);
John Stiles2bec8ab2021-04-06 18:40:04 -0400305
John Stiles8cad6372021-04-07 12:31:13 -0400306 SpvId writeVectorConstructor(const ConstructorCompound& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700307
John Stilesd47330f2021-04-08 23:25:52 -0400308 SpvId writeCompositeConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400309
John Stilese1182782021-03-30 22:09:37 -0400310 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
311
John Stiles5abb9e12021-04-06 13:47:19 -0400312 SpvId writeConstructorMatrixResize(const ConstructorMatrixResize& c, OutputStream& out);
313
John Stilesfd7252f2021-04-04 22:24:40 -0400314 SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out);
315
John Stiles2938eea2021-04-01 18:58:25 -0400316 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
317
John Stiles8cad6372021-04-07 12:31:13 -0400318 SpvId writeConstructorCompoundCast(const ConstructorCompoundCast& c, OutputStream& out);
John Stilesb14a8192021-04-05 11:40:46 -0400319
320 SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out);
321
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400322 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700323
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400324 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700325
Ethan Nicholasef653b82017-02-21 13:50:00 -0500326 /**
327 * Folds the potentially-vector result of a logical operation down to a single bool. If
328 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
329 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
330 * returns the original id value.
331 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400332 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500333
Ethan Nicholas68990be2017-07-13 09:36:52 -0400334 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400335 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
336 SpvOp_ mergeOperator, OutputStream& out);
337
John Stilesbc5c2a02021-04-08 11:44:53 -0400338 SpvId writeStructComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
339 OutputStream& out);
340
John Stiles35092102021-04-08 23:30:51 -0400341 SpvId writeArrayComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
342 OutputStream& out);
343
344 // Used by writeStructComparison and writeArrayComparison to logically combine field-by-field
345 // comparisons into an overall comparison result.
346 // - `a.x == b.x` merged with `a.y == b.y` generates `(a.x == b.x) && (a.y == b.y)`
347 // - `a.x != b.x` merged with `a.y != b.y` generates `(a.x != b.x) || (a.y != b.y)`
348 SpvId mergeComparisons(SpvId comparison, SpvId allComparisons, Operator op, OutputStream& out);
349
Ethan Nicholas0df21132018-07-10 09:37:51 -0400350 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
John Stiles43b593c2021-05-13 22:03:27 -0400351 SpvOp_ op, OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400352
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500353 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
354 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400355 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700356
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500357 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700359
John Stilesd94bfdd2021-03-25 11:44:08 -0400360 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
361
John Stiles45990502021-02-16 10:55:27 -0500362 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400363 const Type& rightType, SpvId rhs, const Type& resultType,
364 OutputStream& out);
365
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400366 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700367
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400368 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700369
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400370 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700371
John Stilesbc5c2a02021-04-08 11:44:53 -0400372 SpvId writeLogicalAnd(const Expression& left, const Expression& right, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700373
John Stilesbc5c2a02021-04-08 11:44:53 -0400374 SpvId writeLogicalOr(const Expression& left, const Expression& right, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700375
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400376 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700377
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400378 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700379
ethannicholasf789b382016-08-03 12:43:36 -0700380 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700381
ethannicholasf789b382016-08-03 12:43:36 -0700382 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700383
ethannicholasf789b382016-08-03 12:43:36 -0700384 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700385
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400386 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700387
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400388 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700389
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400390 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700391
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400392 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700393
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400394 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500395
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500396 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
397
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400398 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700399
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400400 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700401
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400402 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700403
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400404 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700405
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400406 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700407
Ethan Nicholas962dec42021-06-10 13:06:39 -0400408 void writeString(skstd::string_view s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700409
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400410 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700411
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400412 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700413
Ethan Nicholas962dec42021-06-10 13:06:39 -0400414 void writeInstruction(SpvOp_ opCode, skstd::string_view string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700415
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400416 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700417
Ethan Nicholas962dec42021-06-10 13:06:39 -0400418 void writeInstruction(SpvOp_ opCode, int32_t word1, skstd::string_view string,
419 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700420
Ethan Nicholas962dec42021-06-10 13:06:39 -0400421 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, skstd::string_view string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400422 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400424 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700425
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500426 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400427 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700428
429 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400430 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700431
432 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400433 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700434
435 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400436 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700437
438 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400439 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700440
441 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500442 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400443 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700444
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400445 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
446
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500447 MemoryLayout memoryLayoutForVariable(const Variable&) const;
448
John Stiles4d6310a2021-01-26 19:58:22 -0500449 struct EntrypointAdapter {
450 std::unique_ptr<FunctionDefinition> entrypointDef;
451 std::unique_ptr<FunctionDeclaration> entrypointDecl;
452 Layout fLayout;
453 Modifiers fModifiers;
454 };
455
456 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
457
John Stilese40d1662021-01-29 10:08:50 -0500458 struct UniformBuffer {
459 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
460 std::unique_ptr<Variable> fInnerVariable;
461 std::unique_ptr<Type> fStruct;
462 };
463
464 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
465
Brian Salomond8d85b92021-07-07 09:41:17 -0400466 void addRTFlipUniform(int offset);
467
ethannicholasd598f792016-07-25 10:08:54 -0700468 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800469 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700470
ethannicholasb3058bd2016-07-01 08:22:01 -0700471 uint64_t fCapabilities;
472 SpvId fIdCount;
473 SpvId fGLSLExtendedInstructions;
John Stilesaaac4e42021-05-06 14:08:28 -0400474 typedef std::tuple<IntrinsicOpcodeKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
475 std::unordered_map<IntrinsicKind, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700476 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
477 std::unordered_map<const Variable*, SpvId> fVariableMap;
478 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400479 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400480 std::unordered_map<String, SpvId> fTypeMap;
481 StringStream fCapabilitiesBuffer;
482 StringStream fGlobalInitializersBuffer;
483 StringStream fConstantBuffer;
484 StringStream fExtraGlobalsBuffer;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400485 StringStream fVariableBuffer;
486 StringStream fNameBuffer;
487 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700488
489 SpvId fBoolTrue;
490 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500491 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500492 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500493 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700494 // label of the current block, or 0 if we are not in a block
495 SpvId fCurrentBlock;
496 std::stack<SpvId> fBreakTarget;
497 std::stack<SpvId> fContinueTarget;
Brian Salomond8d85b92021-07-07 09:41:17 -0400498 bool fWroteRTFlip = false;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400499 // holds variables synthesized during output, for lifetime purposes
500 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400501 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500502 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
503 // interface block.
504 UniformBuffer fUniformBuffer;
505 std::vector<const VarDeclaration*> fTopLevelUniforms;
506 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
507 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700508
509 friend class PointerLValue;
510 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500511
John Stiles7571f9e2020-09-02 22:42:33 -0400512 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700513};
514
John Stilesa6841be2020-08-06 14:11:56 -0400515} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700516
517#endif