blob: 16d35f584e0e4840ff2a4363643c6856c14ba58c [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
John Stilescd806892021-01-06 13:33:31 -050015#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/sksl/SkSLCodeGenerator.h"
17#include "src/sksl/SkSLMemoryLayout.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050018#include "src/sksl/SkSLStringStream.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/ir/SkSLBinaryExpression.h"
20#include "src/sksl/ir/SkSLBoolLiteral.h"
21#include "src/sksl/ir/SkSLConstructor.h"
22#include "src/sksl/ir/SkSLDoStatement.h"
23#include "src/sksl/ir/SkSLFieldAccess.h"
24#include "src/sksl/ir/SkSLFloatLiteral.h"
25#include "src/sksl/ir/SkSLForStatement.h"
26#include "src/sksl/ir/SkSLFunctionCall.h"
27#include "src/sksl/ir/SkSLFunctionDeclaration.h"
28#include "src/sksl/ir/SkSLFunctionDefinition.h"
29#include "src/sksl/ir/SkSLIfStatement.h"
30#include "src/sksl/ir/SkSLIndexExpression.h"
31#include "src/sksl/ir/SkSLIntLiteral.h"
32#include "src/sksl/ir/SkSLInterfaceBlock.h"
John Stiles4d6310a2021-01-26 19:58:22 -050033#include "src/sksl/ir/SkSLModifiers.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLPostfixExpression.h"
35#include "src/sksl/ir/SkSLPrefixExpression.h"
36#include "src/sksl/ir/SkSLProgramElement.h"
37#include "src/sksl/ir/SkSLReturnStatement.h"
38#include "src/sksl/ir/SkSLStatement.h"
39#include "src/sksl/ir/SkSLSwitchStatement.h"
40#include "src/sksl/ir/SkSLSwizzle.h"
41#include "src/sksl/ir/SkSLTernaryExpression.h"
42#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070045
John Stilescd806892021-01-06 13:33:31 -050046namespace SkSL {
47
John Stilesbdc3d3c2021-01-06 18:41:40 -050048struct SPIRVNumberConstant {
49 bool operator==(const SPIRVNumberConstant& that) const {
50 return fValueBits == that.fValueBits &&
51 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040052 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050053 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
54 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040055};
56
John Stilescd806892021-01-06 13:33:31 -050057struct SPIRVVectorConstant {
58 bool operator==(const SPIRVVectorConstant& that) const {
59 return fTypeId == that.fTypeId &&
60 fValueId[0] == that.fValueId[0] &&
61 fValueId[1] == that.fValueId[1] &&
62 fValueId[2] == that.fValueId[2] &&
63 fValueId[3] == that.fValueId[3];
64 }
65 SpvId fTypeId;
66 SpvId fValueId[4];
67};
68
69} // namespace SkSL
70
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040071namespace std {
72
73template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050074struct hash<SkSL::SPIRVNumberConstant> {
75 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
76 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040077 }
78};
79
John Stilescd806892021-01-06 13:33:31 -050080template <>
81struct hash<SkSL::SPIRVVectorConstant> {
82 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
83 return SkOpts::hash(&key, sizeof(key));
84 }
85};
86
John Stilesa6841be2020-08-06 14:11:56 -040087} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040088
ethannicholasb3058bd2016-07-01 08:22:01 -070089namespace SkSL {
90
ethannicholasb3058bd2016-07-01 08:22:01 -070091/**
92 * Converts a Program into a SPIR-V binary.
93 */
94class SPIRVCodeGenerator : public CodeGenerator {
95public:
96 class LValue {
97 public:
98 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050099
ethannicholasb3058bd2016-07-01 08:22:01 -0700100 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
101 // by a pointer (e.g. vector swizzles), returns 0.
102 virtual SpvId getPointer() = 0;
103
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400104 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700105
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400106 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700107 };
108
Brian Osman8b43dad2020-10-09 13:31:42 -0400109 SPIRVCodeGenerator(const Context* context,
110 const Program* program,
111 ErrorReporter* errors,
112 OutputStream* out)
113 : INHERITED(program, errors, out)
114 , fContext(*context)
115 , fDefaultLayout(MemoryLayout::k140_Standard)
116 , fCapabilities(0)
117 , fIdCount(1)
118 , fBoolTrue(0)
119 , fBoolFalse(0)
120 , fSetupFragPosition(false)
121 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400122 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700123 this->setupIntrinsics();
124 }
125
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500126 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700127
128private:
129 enum IntrinsicKind {
130 kGLSL_STD_450_IntrinsicKind,
131 kSPIRV_IntrinsicKind,
132 kSpecial_IntrinsicKind
133 };
134
135 enum SpecialIntrinsic {
136 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500137 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500138 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500139 kMax_SpecialIntrinsic,
140 kMin_SpecialIntrinsic,
141 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500142 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700143 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400144 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400145 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500146 kSmoothStep_SpecialIntrinsic,
147 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500148 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400149 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 };
151
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400152 enum class Precision {
153 kLow,
154 kHigh,
155 };
156
ethannicholasb3058bd2016-07-01 08:22:01 -0700157 void setupIntrinsics();
158
159 SpvId nextId();
160
Ethan Nicholase2c49992020-10-05 11:49:11 -0400161 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162
ethannicholasb3058bd2016-07-01 08:22:01 -0700163 SpvId getType(const Type& type);
164
ethannicholas8ac838d2016-11-22 08:39:36 -0800165 SpvId getType(const Type& type, const MemoryLayout& layout);
166
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400167 SpvId getImageType(const Type& type);
168
ethannicholasd598f792016-07-25 10:08:54 -0700169 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700170
ethannicholasd598f792016-07-25 10:08:54 -0700171 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700172
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500173 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800174 SpvStorageClass_ storageClass);
175
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400176 void writePrecisionModifier(Precision precision, SpvId id);
177
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500178 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400179
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400180 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181
182 void writeLayout(const Layout& layout, SpvId target);
183
184 void writeLayout(const Layout& layout, SpvId target, int member);
185
ethannicholas8ac838d2016-11-22 08:39:36 -0800186 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400188 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189
Stephen White88574972020-06-23 19:09:29 -0400190 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700191
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400192 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700193
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400194 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400196 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700197
John Stilese40d1662021-01-29 10:08:50 -0500198 void writeGlobalVar(Program::Kind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700199
Brian Osmanc0213602020-10-06 14:43:32 -0400200 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700201
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400202 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700203
John Stilese40d1662021-01-29 10:08:50 -0500204 int findUniformFieldIndex(const Variable& var) const;
205
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400206 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400208 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700209
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400210 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
211
212 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
213
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500214
215 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
216 SpvId signedInst, SpvId unsignedInst,
217 const std::vector<SpvId>& args, OutputStream& out);
218
219 /**
220 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
221 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
222 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
223 * vec2, vec3).
224 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400225 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500226
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400227 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700228
ethannicholasf789b382016-08-03 12:43:36 -0700229 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700230
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400231 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700232
John Stilesd9d52712021-01-13 17:15:02 -0500233 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
234 OutputStream& out);
235
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500237
John Stilesd9d52712021-01-13 17:15:02 -0500238 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
239 OutputStream& out);
240
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400241 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
242
John Stilesd9d52712021-01-13 17:15:02 -0500243 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
244 OutputStream& out);
245
John Stilesa60fb172021-01-14 11:06:20 -0500246 SpvId writeBooleanConstructor(const Constructor& c, OutputStream& out);
247
John Stiles48c28842021-01-14 11:05:03 -0500248 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
249 OutputStream& out);
250
Ethan Nicholas84645e32017-02-09 13:57:14 -0500251 /**
252 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
253 * entries equal to zero.
254 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400255 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500256
257 /**
258 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
259 * source matrix are filled with zero; entries which do not exist in the destination matrix are
260 * ignored.
261 */
262 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400263 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500264
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400265 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400266 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
267 OutputStream& out);
268
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400269 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700270
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400271 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700272
Ethan Nicholasbd553222017-07-18 15:54:59 -0400273 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
274
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400275 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700276
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400277 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700278
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400279 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700280
Ethan Nicholasef653b82017-02-21 13:50:00 -0500281 /**
282 * Folds the potentially-vector result of a logical operation down to a single bool. If
283 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
284 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
285 * returns the original id value.
286 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400287 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500288
Ethan Nicholas68990be2017-07-13 09:36:52 -0400289 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400290 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
291 SpvOp_ mergeOperator, OutputStream& out);
292
293 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
294 SpvOp_ floatOperator, SpvOp_ intOperator,
295 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400296
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500297 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
298 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500301 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400302 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700303
Ethan Nicholas49465b42019-04-17 12:22:21 -0400304 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
305 const Type& rightType, SpvId rhs, const Type& resultType,
306 OutputStream& out);
307
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400308 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700309
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400310 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700311
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400312 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700313
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400314 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700315
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400316 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700317
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400318 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700319
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400320 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700321
ethannicholasf789b382016-08-03 12:43:36 -0700322 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700323
ethannicholasf789b382016-08-03 12:43:36 -0700324 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700325
ethannicholasf789b382016-08-03 12:43:36 -0700326 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700327
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400328 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700329
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400330 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700331
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400332 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700333
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400334 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700335
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400336 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500337
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500338 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
339
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400340 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700341
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400342 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700343
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400344 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700345
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400346 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400348 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700349
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700350 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700351
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400352 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700353
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400354 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700355
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700356 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700357
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700359
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700360 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700361
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700362 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400363 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700364
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400365 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700366
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500367 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400368 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700369
370 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400371 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700372
373 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400374 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700375
376 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400377 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700378
379 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400380 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700381
382 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500383 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400384 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700385
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400386 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
387
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500388 MemoryLayout memoryLayoutForVariable(const Variable&) const;
389
John Stiles4d6310a2021-01-26 19:58:22 -0500390 struct EntrypointAdapter {
391 std::unique_ptr<FunctionDefinition> entrypointDef;
392 std::unique_ptr<FunctionDeclaration> entrypointDecl;
393 Layout fLayout;
394 Modifiers fModifiers;
395 };
396
397 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
398
John Stilese40d1662021-01-29 10:08:50 -0500399 struct UniformBuffer {
400 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
401 std::unique_ptr<Variable> fInnerVariable;
402 std::unique_ptr<Type> fStruct;
403 };
404
405 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
406
ethannicholasd598f792016-07-25 10:08:54 -0700407 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800408 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700409
ethannicholasb3058bd2016-07-01 08:22:01 -0700410 uint64_t fCapabilities;
411 SpvId fIdCount;
412 SpvId fGLSLExtendedInstructions;
413 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400414 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700415 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
416 std::unordered_map<const Variable*, SpvId> fVariableMap;
417 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400418 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400419 std::unordered_map<String, SpvId> fTypeMap;
420 StringStream fCapabilitiesBuffer;
421 StringStream fGlobalInitializersBuffer;
422 StringStream fConstantBuffer;
423 StringStream fExtraGlobalsBuffer;
424 StringStream fExternalFunctionsBuffer;
425 StringStream fVariableBuffer;
426 StringStream fNameBuffer;
427 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700428
429 SpvId fBoolTrue;
430 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500431 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500432 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500433 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700434 // label of the current block, or 0 if we are not in a block
435 SpvId fCurrentBlock;
436 std::stack<SpvId> fBreakTarget;
437 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000438 SpvId fRTHeightStructId = (SpvId) -1;
439 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400440 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400441 // holds variables synthesized during output, for lifetime purposes
442 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400443 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500444 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
445 // interface block.
446 UniformBuffer fUniformBuffer;
447 std::vector<const VarDeclaration*> fTopLevelUniforms;
448 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
449 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700450
451 friend class PointerLValue;
452 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500453
John Stiles7571f9e2020-09-02 22:42:33 -0400454 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700455};
456
John Stilesa6841be2020-08-06 14:11:56 -0400457} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700458
459#endif