blob: 5c08c82557f0c15319126a760fd92d7ff6ddd29d [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>
John Stilesd7437ee2021-08-02 11:56:16 -040014#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070015
Ethan Nicholasdaed2592021-03-04 14:30:25 -050016#include "include/private/SkSLModifiers.h"
Ethan Nicholas24c17722021-03-09 13:10:59 -050017#include "include/private/SkSLProgramElement.h"
18#include "include/private/SkSLStatement.h"
John Stilescd806892021-01-06 13:33:31 -050019#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/SkSLMemoryLayout.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050021#include "src/sksl/SkSLStringStream.h"
John Stiles3738ef52021-04-13 10:41:57 -040022#include "src/sksl/codegen/SkSLCodeGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/ir/SkSLBinaryExpression.h"
24#include "src/sksl/ir/SkSLBoolLiteral.h"
25#include "src/sksl/ir/SkSLConstructor.h"
John Stiles7384b372021-04-01 13:48:15 -040026#include "src/sksl/ir/SkSLConstructorArray.h"
John Stiles8cad6372021-04-07 12:31:13 -040027#include "src/sksl/ir/SkSLConstructorCompound.h"
28#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
John Stilese1182782021-03-30 22:09:37 -040029#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040030#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040031#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040032#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesd47330f2021-04-08 23:25:52 -040033#include "src/sksl/ir/SkSLConstructorStruct.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLDoStatement.h"
35#include "src/sksl/ir/SkSLFieldAccess.h"
36#include "src/sksl/ir/SkSLFloatLiteral.h"
37#include "src/sksl/ir/SkSLForStatement.h"
38#include "src/sksl/ir/SkSLFunctionCall.h"
39#include "src/sksl/ir/SkSLFunctionDeclaration.h"
40#include "src/sksl/ir/SkSLFunctionDefinition.h"
41#include "src/sksl/ir/SkSLIfStatement.h"
42#include "src/sksl/ir/SkSLIndexExpression.h"
43#include "src/sksl/ir/SkSLIntLiteral.h"
44#include "src/sksl/ir/SkSLInterfaceBlock.h"
45#include "src/sksl/ir/SkSLPostfixExpression.h"
46#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/sksl/ir/SkSLSwitchStatement.h"
49#include "src/sksl/ir/SkSLSwizzle.h"
50#include "src/sksl/ir/SkSLTernaryExpression.h"
51#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070054
John Stilescd806892021-01-06 13:33:31 -050055namespace SkSL {
56
John Stilesbdc3d3c2021-01-06 18:41:40 -050057struct SPIRVNumberConstant {
58 bool operator==(const SPIRVNumberConstant& that) const {
59 return fValueBits == that.fValueBits &&
60 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040061 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050062 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
63 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040064};
65
John Stilescd806892021-01-06 13:33:31 -050066struct SPIRVVectorConstant {
67 bool operator==(const SPIRVVectorConstant& that) const {
68 return fTypeId == that.fTypeId &&
69 fValueId[0] == that.fValueId[0] &&
70 fValueId[1] == that.fValueId[1] &&
71 fValueId[2] == that.fValueId[2] &&
72 fValueId[3] == that.fValueId[3];
73 }
74 SpvId fTypeId;
75 SpvId fValueId[4];
76};
77
78} // namespace SkSL
79
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040080namespace std {
81
82template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050083struct hash<SkSL::SPIRVNumberConstant> {
84 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
85 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040086 }
87};
88
John Stilescd806892021-01-06 13:33:31 -050089template <>
90struct hash<SkSL::SPIRVVectorConstant> {
91 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
92 return SkOpts::hash(&key, sizeof(key));
93 }
94};
95
John Stilesa6841be2020-08-06 14:11:56 -040096} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040097
ethannicholasb3058bd2016-07-01 08:22:01 -070098namespace SkSL {
99
ethannicholasb3058bd2016-07-01 08:22:01 -0700100/**
101 * Converts a Program into a SPIR-V binary.
102 */
103class SPIRVCodeGenerator : public CodeGenerator {
104public:
105 class LValue {
106 public:
107 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500108
ethannicholasb3058bd2016-07-01 08:22:01 -0700109 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500110 // by a pointer (e.g. vector swizzles), returns -1.
111 virtual SpvId getPointer() { return -1; }
112
Ethan Nicholase0707b72021-03-17 11:16:41 -0400113 // Returns true if a valid pointer returned by getPointer represents a memory object
114 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
115 // getPointer() returns -1.
116 virtual bool isMemoryObjectPointer() const { return true; }
117
John Stiles3f14d282021-02-05 09:31:04 -0500118 // Applies a swizzle to the components of the LValue, if possible. This is used to create
119 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
120 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
121 return false;
122 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700123
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400124 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700125
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400126 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 };
128
Brian Osman8b43dad2020-10-09 13:31:42 -0400129 SPIRVCodeGenerator(const Context* context,
130 const Program* program,
Brian Osman8b43dad2020-10-09 13:31:42 -0400131 OutputStream* out)
Ethan Nicholas3abc6c62021-08-13 11:20:09 -0400132 : INHERITED(context, program, out)
Brian Osman8b43dad2020-10-09 13:31:42 -0400133 , fDefaultLayout(MemoryLayout::k140_Standard)
134 , fCapabilities(0)
135 , fIdCount(1)
136 , fBoolTrue(0)
137 , fBoolFalse(0)
138 , fSetupFragPosition(false)
139 , fCurrentBlock(0)
Ethan Nicholasc7774a72021-08-27 15:34:05 -0400140 , fSynthetics(fContext, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 this->setupIntrinsics();
142 }
143
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500144 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700145
146private:
John Stilesaaac4e42021-05-06 14:08:28 -0400147 enum IntrinsicOpcodeKind {
148 kGLSL_STD_450_IntrinsicOpcodeKind,
149 kSPIRV_IntrinsicOpcodeKind,
150 kSpecial_IntrinsicOpcodeKind
ethannicholasb3058bd2016-07-01 08:22:01 -0700151 };
152
153 enum SpecialIntrinsic {
154 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500155 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500156 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500157 kMax_SpecialIntrinsic,
158 kMin_SpecialIntrinsic,
159 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500160 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700161 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400162 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400163 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500164 kSmoothStep_SpecialIntrinsic,
165 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500166 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400167 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700168 };
169
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400170 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400171 kDefault,
172 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400173 };
174
John Stiles07367122021-09-08 13:12:26 -0400175 struct TempVar {
176 SpvId spvId;
177 const Type* type;
178 std::unique_ptr<SPIRVCodeGenerator::LValue> lvalue;
179 };
180
ethannicholasb3058bd2016-07-01 08:22:01 -0700181 void setupIntrinsics();
182
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400183 /**
184 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
185 * appropriate, or null to never add one.
186 */
187 SpvId nextId(const Type* type);
188
189 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
Ethan Nicholase2c49992020-10-05 11:49:11 -0400191 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400192
ethannicholasb3058bd2016-07-01 08:22:01 -0700193 SpvId getType(const Type& type);
194
ethannicholas8ac838d2016-11-22 08:39:36 -0800195 SpvId getType(const Type& type, const MemoryLayout& layout);
196
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400197 SpvId getImageType(const Type& type);
198
ethannicholasd598f792016-07-25 10:08:54 -0700199 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200
ethannicholasd598f792016-07-25 10:08:54 -0700201 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500203 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800204 SpvStorageClass_ storageClass);
205
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400206 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207
208 void writeLayout(const Layout& layout, SpvId target);
209
210 void writeLayout(const Layout& layout, SpvId target, int member);
211
ethannicholas8ac838d2016-11-22 08:39:36 -0800212 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700213
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400214 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
Brian Salomond8d85b92021-07-07 09:41:17 -0400216 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTFlip = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400218 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400220 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700221
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400222 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
John Stilesdbd4e6f2021-02-16 13:29:15 -0500224 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
Brian Osmanc0213602020-10-06 14:43:32 -0400226 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
John Stilese40d1662021-01-29 10:08:50 -0500230 int findUniformFieldIndex(const Variable& var) const;
231
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400232 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700233
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400234 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700235
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
237
John Stiles07367122021-09-08 13:12:26 -0400238 SpvId writeFunctionCallArgument(const Expression& arg,
239 const Modifiers& paramModifiers,
240 std::vector<TempVar>* tempVars,
241 OutputStream& out);
242
243 void copyBackTempVars(const std::vector<TempVar>& tempVars, OutputStream& out);
244
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400245 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
246
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500247
248 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
249 SpvId signedInst, SpvId unsignedInst,
250 const std::vector<SpvId>& args, OutputStream& out);
251
252 /**
Brian Salomond8d85b92021-07-07 09:41:17 -0400253 * Promotes an expression to a vector. If the expression is already a vector with vectorSize
254 * columns, returns it unmodified. If the expression is a scalar, either promotes it to a
255 * vector (if vectorSize > 1) or returns it unmodified (if vectorSize == 1). Asserts if the
256 * expression is already a vector and it does not have vectorSize columns.
257 */
258 SpvId vectorize(const Expression& expr, int vectorSize, OutputStream& out);
259
260 /**
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500261 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
262 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
263 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
264 * vec2, vec3).
265 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400266 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500267
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400268 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700269
John Stiles2938eea2021-04-01 18:58:25 -0400270 SpvId writeConstantVector(const AnyConstructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271
John Stilesa91bf052021-05-17 09:34:03 -0400272 SpvId writeScalarToMatrixSplat(const Type& matrixType, SpvId scalarId, OutputStream& out);
273
John Stilesfd7252f2021-04-04 22:24:40 -0400274 SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275
John Stilesd9d52712021-01-13 17:15:02 -0500276 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
277 OutputStream& out);
278
John Stilesfd7252f2021-04-04 22:24:40 -0400279 SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500280
John Stilesd9d52712021-01-13 17:15:02 -0500281 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
282 OutputStream& out);
283
John Stilesfd7252f2021-04-04 22:24:40 -0400284 SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400285
John Stilesd9d52712021-01-13 17:15:02 -0500286 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
287 OutputStream& out);
288
John Stilesfd7252f2021-04-04 22:24:40 -0400289 SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out);
John Stilesa60fb172021-01-14 11:06:20 -0500290
John Stiles48c28842021-01-14 11:05:03 -0500291 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
292 OutputStream& out);
293
John Stilesb14a8192021-04-05 11:40:46 -0400294 SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType,
295 OutputStream& out);
296
Ethan Nicholas84645e32017-02-09 13:57:14 -0500297 /**
298 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
299 * entries equal to zero.
300 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400301 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500302
303 /**
304 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
305 * source matrix are filled with zero; entries which do not exist in the destination matrix are
306 * ignored.
307 */
John Stiles268a73f2021-04-07 12:30:22 -0400308 SpvId writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500309
John Stilesbeb2fbf2021-07-08 18:54:39 -0400310 void addColumnEntry(const Type& columnType, std::vector<SpvId>* currentColumn,
311 std::vector<SpvId>* columnIds, int rows, SpvId entry, OutputStream& out);
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400312
John Stiles8cad6372021-04-07 12:31:13 -0400313 SpvId writeConstructorCompound(const ConstructorCompound& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700314
John Stiles8cad6372021-04-07 12:31:13 -0400315 SpvId writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out);
John Stiles2bec8ab2021-04-06 18:40:04 -0400316
John Stiles8cad6372021-04-07 12:31:13 -0400317 SpvId writeVectorConstructor(const ConstructorCompound& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318
John Stilesd47330f2021-04-08 23:25:52 -0400319 SpvId writeCompositeConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400320
John Stilese1182782021-03-30 22:09:37 -0400321 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
322
John Stiles5abb9e12021-04-06 13:47:19 -0400323 SpvId writeConstructorMatrixResize(const ConstructorMatrixResize& c, OutputStream& out);
324
John Stilesfd7252f2021-04-04 22:24:40 -0400325 SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out);
326
John Stiles2938eea2021-04-01 18:58:25 -0400327 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
328
John Stiles8cad6372021-04-07 12:31:13 -0400329 SpvId writeConstructorCompoundCast(const ConstructorCompoundCast& c, OutputStream& out);
John Stilesb14a8192021-04-05 11:40:46 -0400330
331 SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out);
332
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400335 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholasef653b82017-02-21 13:50:00 -0500337 /**
338 * Folds the potentially-vector result of a logical operation down to a single bool. If
339 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
340 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
341 * returns the original id value.
342 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400343 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500344
Ethan Nicholas68990be2017-07-13 09:36:52 -0400345 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400346 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
347 SpvOp_ mergeOperator, OutputStream& out);
348
John Stilesbc5c2a02021-04-08 11:44:53 -0400349 SpvId writeStructComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
350 OutputStream& out);
351
John Stiles35092102021-04-08 23:30:51 -0400352 SpvId writeArrayComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
353 OutputStream& out);
354
355 // Used by writeStructComparison and writeArrayComparison to logically combine field-by-field
356 // comparisons into an overall comparison result.
357 // - `a.x == b.x` merged with `a.y == b.y` generates `(a.x == b.x) && (a.y == b.y)`
358 // - `a.x != b.x` merged with `a.y != b.y` generates `(a.x != b.x) || (a.y != b.y)`
359 SpvId mergeComparisons(SpvId comparison, SpvId allComparisons, Operator op, OutputStream& out);
360
Ethan Nicholas0df21132018-07-10 09:37:51 -0400361 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
John Stiles43b593c2021-05-13 22:03:27 -0400362 SpvOp_ op, OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400363
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500364 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
365 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400366 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700367
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500368 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400369 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700370
John Stilesd94bfdd2021-03-25 11:44:08 -0400371 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
372
John Stiles45990502021-02-16 10:55:27 -0500373 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400374 const Type& rightType, SpvId rhs, const Type& resultType,
375 OutputStream& out);
376
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400377 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700378
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400379 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700380
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400381 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700382
John Stilesbc5c2a02021-04-08 11:44:53 -0400383 SpvId writeLogicalAnd(const Expression& left, const Expression& right, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700384
John Stilesbc5c2a02021-04-08 11:44:53 -0400385 SpvId writeLogicalOr(const Expression& left, const Expression& right, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700386
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400387 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700388
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400389 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700390
ethannicholasf789b382016-08-03 12:43:36 -0700391 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700392
ethannicholasf789b382016-08-03 12:43:36 -0700393 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700394
ethannicholasf789b382016-08-03 12:43:36 -0700395 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700396
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400397 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700398
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400399 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700400
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400401 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400403 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700404
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400405 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500406
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500407 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
408
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400409 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700410
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400411 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700412
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400413 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700414
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400415 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700416
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400417 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700418
Ethan Nicholas962dec42021-06-10 13:06:39 -0400419 void writeString(skstd::string_view s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700420
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400421 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700422
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400423 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700424
Ethan Nicholas962dec42021-06-10 13:06:39 -0400425 void writeInstruction(SpvOp_ opCode, skstd::string_view string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700426
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400427 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700428
Ethan Nicholas962dec42021-06-10 13:06:39 -0400429 void writeInstruction(SpvOp_ opCode, int32_t word1, skstd::string_view string,
430 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700431
Ethan Nicholas962dec42021-06-10 13:06:39 -0400432 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, skstd::string_view string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400433 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700434
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400435 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700436
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500437 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400438 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700439
440 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400441 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700442
443 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400444 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700445
446 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400447 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700448
449 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400450 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700451
452 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500453 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400454 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700455
John Stilesd7437ee2021-08-02 11:56:16 -0400456 bool isDead(const Variable& var) const;
457
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500458 MemoryLayout memoryLayoutForVariable(const Variable&) const;
459
John Stiles4d6310a2021-01-26 19:58:22 -0500460 struct EntrypointAdapter {
461 std::unique_ptr<FunctionDefinition> entrypointDef;
462 std::unique_ptr<FunctionDeclaration> entrypointDecl;
463 Layout fLayout;
464 Modifiers fModifiers;
465 };
466
467 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
468
John Stilese40d1662021-01-29 10:08:50 -0500469 struct UniformBuffer {
470 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
471 std::unique_ptr<Variable> fInnerVariable;
472 std::unique_ptr<Type> fStruct;
473 };
474
475 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
476
Brian Salomond8d85b92021-07-07 09:41:17 -0400477 void addRTFlipUniform(int offset);
478
ethannicholas8ac838d2016-11-22 08:39:36 -0800479 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700480
ethannicholasb3058bd2016-07-01 08:22:01 -0700481 uint64_t fCapabilities;
482 SpvId fIdCount;
483 SpvId fGLSLExtendedInstructions;
John Stilesaaac4e42021-05-06 14:08:28 -0400484 typedef std::tuple<IntrinsicOpcodeKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
485 std::unordered_map<IntrinsicKind, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700486 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
487 std::unordered_map<const Variable*, SpvId> fVariableMap;
488 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400489 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400490 std::unordered_map<String, SpvId> fTypeMap;
491 StringStream fCapabilitiesBuffer;
492 StringStream fGlobalInitializersBuffer;
493 StringStream fConstantBuffer;
494 StringStream fExtraGlobalsBuffer;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400495 StringStream fVariableBuffer;
496 StringStream fNameBuffer;
497 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700498
499 SpvId fBoolTrue;
500 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500501 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500502 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500503 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700504 // label of the current block, or 0 if we are not in a block
505 SpvId fCurrentBlock;
506 std::stack<SpvId> fBreakTarget;
507 std::stack<SpvId> fContinueTarget;
Brian Salomond8d85b92021-07-07 09:41:17 -0400508 bool fWroteRTFlip = false;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400509 // holds variables synthesized during output, for lifetime purposes
510 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400511 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500512 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
513 // interface block.
514 UniformBuffer fUniformBuffer;
515 std::vector<const VarDeclaration*> fTopLevelUniforms;
516 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
John Stilesd7437ee2021-08-02 11:56:16 -0400517 std::unordered_set<const Variable*> fSPIRVBonusVariables;
John Stilese40d1662021-01-29 10:08:50 -0500518 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700519
520 friend class PointerLValue;
521 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500522
John Stiles7571f9e2020-09-02 22:42:33 -0400523 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700524};
525
John Stilesa6841be2020-08-06 14:11:56 -0400526} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700527
528#endif