blob: dbf73ba5ed0fc9da8bbbc24eeff97137a04f5744 [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"
33#include "src/sksl/ir/SkSLPostfixExpression.h"
34#include "src/sksl/ir/SkSLPrefixExpression.h"
35#include "src/sksl/ir/SkSLProgramElement.h"
36#include "src/sksl/ir/SkSLReturnStatement.h"
37#include "src/sksl/ir/SkSLStatement.h"
38#include "src/sksl/ir/SkSLSwitchStatement.h"
39#include "src/sksl/ir/SkSLSwizzle.h"
40#include "src/sksl/ir/SkSLTernaryExpression.h"
41#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070044
John Stilescd806892021-01-06 13:33:31 -050045namespace SkSL {
46
John Stilesbdc3d3c2021-01-06 18:41:40 -050047struct SPIRVNumberConstant {
48 bool operator==(const SPIRVNumberConstant& that) const {
49 return fValueBits == that.fValueBits &&
50 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040051 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050052 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
53 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040054};
55
John Stilescd806892021-01-06 13:33:31 -050056struct SPIRVVectorConstant {
57 bool operator==(const SPIRVVectorConstant& that) const {
58 return fTypeId == that.fTypeId &&
59 fValueId[0] == that.fValueId[0] &&
60 fValueId[1] == that.fValueId[1] &&
61 fValueId[2] == that.fValueId[2] &&
62 fValueId[3] == that.fValueId[3];
63 }
64 SpvId fTypeId;
65 SpvId fValueId[4];
66};
67
68} // namespace SkSL
69
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040070namespace std {
71
72template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050073struct hash<SkSL::SPIRVNumberConstant> {
74 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
75 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040076 }
77};
78
John Stilescd806892021-01-06 13:33:31 -050079template <>
80struct hash<SkSL::SPIRVVectorConstant> {
81 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
82 return SkOpts::hash(&key, sizeof(key));
83 }
84};
85
John Stilesa6841be2020-08-06 14:11:56 -040086} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040087
ethannicholasb3058bd2016-07-01 08:22:01 -070088namespace SkSL {
89
ethannicholasb3058bd2016-07-01 08:22:01 -070090/**
91 * Converts a Program into a SPIR-V binary.
92 */
93class SPIRVCodeGenerator : public CodeGenerator {
94public:
95 class LValue {
96 public:
97 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050098
ethannicholasb3058bd2016-07-01 08:22:01 -070099 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
100 // by a pointer (e.g. vector swizzles), returns 0.
101 virtual SpvId getPointer() = 0;
102
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400103 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700104
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400105 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700106 };
107
Brian Osman8b43dad2020-10-09 13:31:42 -0400108 SPIRVCodeGenerator(const Context* context,
109 const Program* program,
110 ErrorReporter* errors,
111 OutputStream* out)
112 : INHERITED(program, errors, out)
113 , fContext(*context)
114 , fDefaultLayout(MemoryLayout::k140_Standard)
115 , fCapabilities(0)
116 , fIdCount(1)
117 , fBoolTrue(0)
118 , fBoolFalse(0)
119 , fSetupFragPosition(false)
120 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400121 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700122 this->setupIntrinsics();
123 }
124
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500125 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700126
127private:
128 enum IntrinsicKind {
129 kGLSL_STD_450_IntrinsicKind,
130 kSPIRV_IntrinsicKind,
131 kSpecial_IntrinsicKind
132 };
133
134 enum SpecialIntrinsic {
135 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500136 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500137 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500138 kMax_SpecialIntrinsic,
139 kMin_SpecialIntrinsic,
140 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500141 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700142 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400143 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400144 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500145 kSmoothStep_SpecialIntrinsic,
146 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500147 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400148 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700149 };
150
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400151 enum class Precision {
152 kLow,
153 kHigh,
154 };
155
ethannicholasb3058bd2016-07-01 08:22:01 -0700156 void setupIntrinsics();
157
158 SpvId nextId();
159
Ethan Nicholase2c49992020-10-05 11:49:11 -0400160 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400161
ethannicholasb3058bd2016-07-01 08:22:01 -0700162 SpvId getType(const Type& type);
163
ethannicholas8ac838d2016-11-22 08:39:36 -0800164 SpvId getType(const Type& type, const MemoryLayout& layout);
165
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400166 SpvId getImageType(const Type& type);
167
ethannicholasd598f792016-07-25 10:08:54 -0700168 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700169
ethannicholasd598f792016-07-25 10:08:54 -0700170 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700171
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500172 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800173 SpvStorageClass_ storageClass);
174
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400175 void writePrecisionModifier(Precision precision, SpvId id);
176
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500177 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400178
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400179 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180
181 void writeLayout(const Layout& layout, SpvId target);
182
183 void writeLayout(const Layout& layout, SpvId target, int member);
184
ethannicholas8ac838d2016-11-22 08:39:36 -0800185 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400187 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188
Stephen White88574972020-06-23 19:09:29 -0400189 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400191 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400193 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400195 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Brian Osmanc0213602020-10-06 14:43:32 -0400197 void writeGlobalVar(Program::Kind kind, const VarDeclaration& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198
Brian Osmanc0213602020-10-06 14:43:32 -0400199 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400201 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400203 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400205 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700206
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400207 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
208
209 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
210
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500211
212 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
213 SpvId signedInst, SpvId unsignedInst,
214 const std::vector<SpvId>& args, OutputStream& out);
215
216 /**
217 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
218 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
219 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
220 * vec2, vec3).
221 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400222 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500223
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400224 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
ethannicholasf789b382016-08-03 12:43:36 -0700226 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
John Stilesd9d52712021-01-13 17:15:02 -0500230 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
231 OutputStream& out);
232
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400233 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500234
John Stilesd9d52712021-01-13 17:15:02 -0500235 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
236 OutputStream& out);
237
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400238 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
239
John Stilesd9d52712021-01-13 17:15:02 -0500240 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
241 OutputStream& out);
242
John Stilesa60fb172021-01-14 11:06:20 -0500243 SpvId writeBooleanConstructor(const Constructor& c, OutputStream& out);
244
John Stiles48c28842021-01-14 11:05:03 -0500245 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
246 OutputStream& out);
247
Ethan Nicholas84645e32017-02-09 13:57:14 -0500248 /**
249 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
250 * entries equal to zero.
251 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500253
254 /**
255 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
256 * source matrix are filled with zero; entries which do not exist in the destination matrix are
257 * ignored.
258 */
259 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400260 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500261
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400262 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400263 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
264 OutputStream& out);
265
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400266 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700267
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400268 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700269
Ethan Nicholasbd553222017-07-18 15:54:59 -0400270 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
271
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400272 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700273
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400274 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400276 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700277
Ethan Nicholasef653b82017-02-21 13:50:00 -0500278 /**
279 * Folds the potentially-vector result of a logical operation down to a single bool. If
280 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
281 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
282 * returns the original id value.
283 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400284 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500285
Ethan Nicholas68990be2017-07-13 09:36:52 -0400286 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400287 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
288 SpvOp_ mergeOperator, OutputStream& out);
289
290 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
291 SpvOp_ floatOperator, SpvOp_ intOperator,
292 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400293
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500294 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
295 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400296 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700297
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500298 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
Ethan Nicholas49465b42019-04-17 12:22:21 -0400301 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
302 const Type& rightType, SpvId rhs, const Type& resultType,
303 OutputStream& out);
304
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400305 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700306
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400307 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700308
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700310
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400311 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700312
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400313 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700314
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700316
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400317 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318
ethannicholasf789b382016-08-03 12:43:36 -0700319 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700320
ethannicholasf789b382016-08-03 12:43:36 -0700321 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700322
ethannicholasf789b382016-08-03 12:43:36 -0700323 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700324
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400325 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700326
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400327 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700328
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400329 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700330
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400331 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700332
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500334
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500335 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
336
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400337 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700338
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400339 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700340
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400341 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700342
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400343 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700344
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400345 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700346
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700347 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700348
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400349 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700350
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400351 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700352
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700353 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700354
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400355 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700356
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700357 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700358
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700359 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400360 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700361
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400362 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700363
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500364 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400365 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700366
367 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
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 int32_t word5, 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, int32_t word6, 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, int32_t word7, 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 Nicholas941e7e22016-12-12 15:33:30 -0500380 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400381 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700382
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400383 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
384
ethannicholasd598f792016-07-25 10:08:54 -0700385 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800386 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700387
ethannicholasb3058bd2016-07-01 08:22:01 -0700388 uint64_t fCapabilities;
389 SpvId fIdCount;
390 SpvId fGLSLExtendedInstructions;
391 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400392 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700393 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
394 std::unordered_map<const Variable*, SpvId> fVariableMap;
395 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400396 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400397 std::unordered_map<String, SpvId> fTypeMap;
398 StringStream fCapabilitiesBuffer;
399 StringStream fGlobalInitializersBuffer;
400 StringStream fConstantBuffer;
401 StringStream fExtraGlobalsBuffer;
402 StringStream fExternalFunctionsBuffer;
403 StringStream fVariableBuffer;
404 StringStream fNameBuffer;
405 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700406
407 SpvId fBoolTrue;
408 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500409 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500410 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500411 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700412 // label of the current block, or 0 if we are not in a block
413 SpvId fCurrentBlock;
414 std::stack<SpvId> fBreakTarget;
415 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000416 SpvId fRTHeightStructId = (SpvId) -1;
417 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400418 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400419 // holds variables synthesized during output, for lifetime purposes
420 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400421 int fSkInCount = 1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700422
423 friend class PointerLValue;
424 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500425
John Stiles7571f9e2020-09-02 22:42:33 -0400426 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700427};
428
John Stilesa6841be2020-08-06 14:11:56 -0400429} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700430
431#endif