blob: 9552957da04c41ebf8d4040221eee4e38bd43ae5 [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/SkSLCodeGenerator.h"
20#include "src/sksl/SkSLMemoryLayout.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050021#include "src/sksl/SkSLStringStream.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 Stilese1182782021-03-30 22:09:37 -040026#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles2938eea2021-04-01 18:58:25 -040027#include "src/sksl/ir/SkSLConstructorSplat.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/sksl/ir/SkSLDoStatement.h"
29#include "src/sksl/ir/SkSLFieldAccess.h"
30#include "src/sksl/ir/SkSLFloatLiteral.h"
31#include "src/sksl/ir/SkSLForStatement.h"
32#include "src/sksl/ir/SkSLFunctionCall.h"
33#include "src/sksl/ir/SkSLFunctionDeclaration.h"
34#include "src/sksl/ir/SkSLFunctionDefinition.h"
35#include "src/sksl/ir/SkSLIfStatement.h"
36#include "src/sksl/ir/SkSLIndexExpression.h"
37#include "src/sksl/ir/SkSLIntLiteral.h"
38#include "src/sksl/ir/SkSLInterfaceBlock.h"
39#include "src/sksl/ir/SkSLPostfixExpression.h"
40#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/sksl/ir/SkSLSwitchStatement.h"
43#include "src/sksl/ir/SkSLSwizzle.h"
44#include "src/sksl/ir/SkSLTernaryExpression.h"
45#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070048
John Stilescd806892021-01-06 13:33:31 -050049namespace SkSL {
50
John Stilesbdc3d3c2021-01-06 18:41:40 -050051struct SPIRVNumberConstant {
52 bool operator==(const SPIRVNumberConstant& that) const {
53 return fValueBits == that.fValueBits &&
54 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040055 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050056 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
57 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040058};
59
John Stilescd806892021-01-06 13:33:31 -050060struct SPIRVVectorConstant {
61 bool operator==(const SPIRVVectorConstant& that) const {
62 return fTypeId == that.fTypeId &&
63 fValueId[0] == that.fValueId[0] &&
64 fValueId[1] == that.fValueId[1] &&
65 fValueId[2] == that.fValueId[2] &&
66 fValueId[3] == that.fValueId[3];
67 }
68 SpvId fTypeId;
69 SpvId fValueId[4];
70};
71
72} // namespace SkSL
73
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040074namespace std {
75
76template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050077struct hash<SkSL::SPIRVNumberConstant> {
78 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
79 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040080 }
81};
82
John Stilescd806892021-01-06 13:33:31 -050083template <>
84struct hash<SkSL::SPIRVVectorConstant> {
85 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
86 return SkOpts::hash(&key, sizeof(key));
87 }
88};
89
John Stilesa6841be2020-08-06 14:11:56 -040090} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040091
ethannicholasb3058bd2016-07-01 08:22:01 -070092namespace SkSL {
93
ethannicholasb3058bd2016-07-01 08:22:01 -070094/**
95 * Converts a Program into a SPIR-V binary.
96 */
97class SPIRVCodeGenerator : public CodeGenerator {
98public:
99 class LValue {
100 public:
101 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500102
ethannicholasb3058bd2016-07-01 08:22:01 -0700103 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500104 // by a pointer (e.g. vector swizzles), returns -1.
105 virtual SpvId getPointer() { return -1; }
106
Ethan Nicholase0707b72021-03-17 11:16:41 -0400107 // Returns true if a valid pointer returned by getPointer represents a memory object
108 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
109 // getPointer() returns -1.
110 virtual bool isMemoryObjectPointer() const { return true; }
111
John Stiles3f14d282021-02-05 09:31:04 -0500112 // Applies a swizzle to the components of the LValue, if possible. This is used to create
113 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
114 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
115 return false;
116 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700117
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400118 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700119
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400120 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700121 };
122
Brian Osman8b43dad2020-10-09 13:31:42 -0400123 SPIRVCodeGenerator(const Context* context,
124 const Program* program,
125 ErrorReporter* errors,
126 OutputStream* out)
127 : INHERITED(program, errors, out)
128 , fContext(*context)
129 , fDefaultLayout(MemoryLayout::k140_Standard)
130 , fCapabilities(0)
131 , fIdCount(1)
132 , fBoolTrue(0)
133 , fBoolFalse(0)
134 , fSetupFragPosition(false)
135 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400136 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700137 this->setupIntrinsics();
138 }
139
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500140 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700141
142private:
143 enum IntrinsicKind {
144 kGLSL_STD_450_IntrinsicKind,
145 kSPIRV_IntrinsicKind,
146 kSpecial_IntrinsicKind
147 };
148
149 enum SpecialIntrinsic {
150 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500151 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500152 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500153 kMax_SpecialIntrinsic,
154 kMin_SpecialIntrinsic,
155 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500156 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700157 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400158 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400159 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500160 kSmoothStep_SpecialIntrinsic,
161 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500162 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400163 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700164 };
165
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400166 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400167 kDefault,
168 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400169 };
170
ethannicholasb3058bd2016-07-01 08:22:01 -0700171 void setupIntrinsics();
172
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400173 /**
174 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
175 * appropriate, or null to never add one.
176 */
177 SpvId nextId(const Type* type);
178
179 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180
Ethan Nicholase2c49992020-10-05 11:49:11 -0400181 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400182
ethannicholasb3058bd2016-07-01 08:22:01 -0700183 SpvId getType(const Type& type);
184
ethannicholas8ac838d2016-11-22 08:39:36 -0800185 SpvId getType(const Type& type, const MemoryLayout& layout);
186
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400187 SpvId getImageType(const Type& type);
188
ethannicholasd598f792016-07-25 10:08:54 -0700189 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
ethannicholasd598f792016-07-25 10:08:54 -0700191 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500193 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800194 SpvStorageClass_ storageClass);
195
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400196 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700197
198 void writeLayout(const Layout& layout, SpvId target);
199
200 void writeLayout(const Layout& layout, SpvId target, int member);
201
ethannicholas8ac838d2016-11-22 08:39:36 -0800202 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700203
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400204 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700205
Stephen White88574972020-06-23 19:09:29 -0400206 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400208 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700209
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400210 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700211
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400212 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700213
John Stilesdbd4e6f2021-02-16 13:29:15 -0500214 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
Brian Osmanc0213602020-10-06 14:43:32 -0400216 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400218 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
John Stilese40d1662021-01-29 10:08:50 -0500220 int findUniformFieldIndex(const Variable& var) const;
221
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400222 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400224 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
227
228 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
229
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500230
231 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
232 SpvId signedInst, SpvId unsignedInst,
233 const std::vector<SpvId>& args, OutputStream& out);
234
235 /**
236 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
237 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
238 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
239 * vec2, vec3).
240 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400241 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500242
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400243 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700244
John Stiles2938eea2021-04-01 18:58:25 -0400245 SpvId writeConstantVector(const AnyConstructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400247 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700248
John Stilesd9d52712021-01-13 17:15:02 -0500249 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
250 OutputStream& out);
251
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500253
John Stilesd9d52712021-01-13 17:15:02 -0500254 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
255 OutputStream& out);
256
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400257 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
258
John Stilesd9d52712021-01-13 17:15:02 -0500259 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
260 OutputStream& out);
261
John Stilesa60fb172021-01-14 11:06:20 -0500262 SpvId writeBooleanConstructor(const Constructor& c, OutputStream& out);
263
John Stiles48c28842021-01-14 11:05:03 -0500264 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
265 OutputStream& out);
266
Ethan Nicholas84645e32017-02-09 13:57:14 -0500267 /**
268 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
269 * entries equal to zero.
270 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400271 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500272
273 /**
274 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
275 * source matrix are filled with zero; entries which do not exist in the destination matrix are
276 * ignored.
277 */
278 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400279 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500280
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400281 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400282 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
283 OutputStream& out);
284
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400285 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700286
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288
John Stiles7384b372021-04-01 13:48:15 -0400289 SpvId writeArrayConstructor(const ConstructorArray& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400290
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
John Stilese1182782021-03-30 22:09:37 -0400293 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
294
John Stiles2938eea2021-04-01 18:58:25 -0400295 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
296
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400297 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700298
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
Ethan Nicholasef653b82017-02-21 13:50:00 -0500301 /**
302 * Folds the potentially-vector result of a logical operation down to a single bool. If
303 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
304 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
305 * returns the original id value.
306 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400307 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500308
Ethan Nicholas68990be2017-07-13 09:36:52 -0400309 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400310 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
311 SpvOp_ mergeOperator, OutputStream& out);
312
313 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
314 SpvOp_ floatOperator, SpvOp_ intOperator,
315 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400316
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500317 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
318 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400319 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700320
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500321 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400322 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700323
John Stilesd94bfdd2021-03-25 11:44:08 -0400324 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
325
John Stiles45990502021-02-16 10:55:27 -0500326 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400327 const Type& rightType, SpvId rhs, const Type& resultType,
328 OutputStream& out);
329
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400330 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700331
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400332 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700333
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400334 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700335
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400336 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700337
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400338 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700339
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400340 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700341
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400342 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700343
ethannicholasf789b382016-08-03 12:43:36 -0700344 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700345
ethannicholasf789b382016-08-03 12:43:36 -0700346 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347
ethannicholasf789b382016-08-03 12:43:36 -0700348 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700349
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400350 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700351
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400352 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700353
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400354 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700355
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400356 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700357
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500359
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500360 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
361
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400362 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700363
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400364 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700365
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400366 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700367
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400368 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700369
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400370 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700371
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700372 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700373
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400374 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700375
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400376 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700377
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700378 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700379
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400380 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700381
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700382 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700383
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700384 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400385 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700386
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400387 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700388
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500389 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400390 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700391
392 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400393 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700394
395 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400396 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700397
398 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400399 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700400
401 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400402 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700403
404 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500405 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400406 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700407
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400408 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
409
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500410 MemoryLayout memoryLayoutForVariable(const Variable&) const;
411
John Stiles4d6310a2021-01-26 19:58:22 -0500412 struct EntrypointAdapter {
413 std::unique_ptr<FunctionDefinition> entrypointDef;
414 std::unique_ptr<FunctionDeclaration> entrypointDecl;
415 Layout fLayout;
416 Modifiers fModifiers;
417 };
418
419 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
420
John Stilese40d1662021-01-29 10:08:50 -0500421 struct UniformBuffer {
422 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
423 std::unique_ptr<Variable> fInnerVariable;
424 std::unique_ptr<Type> fStruct;
425 };
426
427 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
428
ethannicholasd598f792016-07-25 10:08:54 -0700429 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800430 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700431
ethannicholasb3058bd2016-07-01 08:22:01 -0700432 uint64_t fCapabilities;
433 SpvId fIdCount;
434 SpvId fGLSLExtendedInstructions;
435 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400436 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700437 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
438 std::unordered_map<const Variable*, SpvId> fVariableMap;
439 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400440 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400441 std::unordered_map<String, SpvId> fTypeMap;
442 StringStream fCapabilitiesBuffer;
443 StringStream fGlobalInitializersBuffer;
444 StringStream fConstantBuffer;
445 StringStream fExtraGlobalsBuffer;
446 StringStream fExternalFunctionsBuffer;
447 StringStream fVariableBuffer;
448 StringStream fNameBuffer;
449 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700450
451 SpvId fBoolTrue;
452 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500453 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500454 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500455 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700456 // label of the current block, or 0 if we are not in a block
457 SpvId fCurrentBlock;
458 std::stack<SpvId> fBreakTarget;
459 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000460 SpvId fRTHeightStructId = (SpvId) -1;
461 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400462 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400463 // holds variables synthesized during output, for lifetime purposes
464 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400465 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500466 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
467 // interface block.
468 UniformBuffer fUniformBuffer;
469 std::vector<const VarDeclaration*> fTopLevelUniforms;
470 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
471 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700472
473 friend class PointerLValue;
474 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500475
John Stiles7571f9e2020-09-02 22:42:33 -0400476 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700477};
478
John Stilesa6841be2020-08-06 14:11:56 -0400479} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700480
481#endif