blob: 0789b2d025c03287e3444896f59fdff64d2d1bb6 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/sksl/ir/SkSLDoStatement.h"
28#include "src/sksl/ir/SkSLFieldAccess.h"
29#include "src/sksl/ir/SkSLFloatLiteral.h"
30#include "src/sksl/ir/SkSLForStatement.h"
31#include "src/sksl/ir/SkSLFunctionCall.h"
32#include "src/sksl/ir/SkSLFunctionDeclaration.h"
33#include "src/sksl/ir/SkSLFunctionDefinition.h"
34#include "src/sksl/ir/SkSLIfStatement.h"
35#include "src/sksl/ir/SkSLIndexExpression.h"
36#include "src/sksl/ir/SkSLIntLiteral.h"
37#include "src/sksl/ir/SkSLInterfaceBlock.h"
38#include "src/sksl/ir/SkSLPostfixExpression.h"
39#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLSwitchStatement.h"
42#include "src/sksl/ir/SkSLSwizzle.h"
43#include "src/sksl/ir/SkSLTernaryExpression.h"
44#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070047
John Stilescd806892021-01-06 13:33:31 -050048namespace SkSL {
49
John Stilesbdc3d3c2021-01-06 18:41:40 -050050struct SPIRVNumberConstant {
51 bool operator==(const SPIRVNumberConstant& that) const {
52 return fValueBits == that.fValueBits &&
53 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040054 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050055 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
56 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040057};
58
John Stilescd806892021-01-06 13:33:31 -050059struct SPIRVVectorConstant {
60 bool operator==(const SPIRVVectorConstant& that) const {
61 return fTypeId == that.fTypeId &&
62 fValueId[0] == that.fValueId[0] &&
63 fValueId[1] == that.fValueId[1] &&
64 fValueId[2] == that.fValueId[2] &&
65 fValueId[3] == that.fValueId[3];
66 }
67 SpvId fTypeId;
68 SpvId fValueId[4];
69};
70
71} // namespace SkSL
72
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040073namespace std {
74
75template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050076struct hash<SkSL::SPIRVNumberConstant> {
77 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
78 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040079 }
80};
81
John Stilescd806892021-01-06 13:33:31 -050082template <>
83struct hash<SkSL::SPIRVVectorConstant> {
84 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
85 return SkOpts::hash(&key, sizeof(key));
86 }
87};
88
John Stilesa6841be2020-08-06 14:11:56 -040089} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040090
ethannicholasb3058bd2016-07-01 08:22:01 -070091namespace SkSL {
92
ethannicholasb3058bd2016-07-01 08:22:01 -070093/**
94 * Converts a Program into a SPIR-V binary.
95 */
96class SPIRVCodeGenerator : public CodeGenerator {
97public:
98 class LValue {
99 public:
100 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500101
ethannicholasb3058bd2016-07-01 08:22:01 -0700102 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500103 // by a pointer (e.g. vector swizzles), returns -1.
104 virtual SpvId getPointer() { return -1; }
105
Ethan Nicholase0707b72021-03-17 11:16:41 -0400106 // Returns true if a valid pointer returned by getPointer represents a memory object
107 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
108 // getPointer() returns -1.
109 virtual bool isMemoryObjectPointer() const { return true; }
110
John Stiles3f14d282021-02-05 09:31:04 -0500111 // Applies a swizzle to the components of the LValue, if possible. This is used to create
112 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
113 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
114 return false;
115 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700116
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400117 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700118
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400119 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 };
121
Brian Osman8b43dad2020-10-09 13:31:42 -0400122 SPIRVCodeGenerator(const Context* context,
123 const Program* program,
124 ErrorReporter* errors,
125 OutputStream* out)
126 : INHERITED(program, errors, out)
127 , fContext(*context)
128 , fDefaultLayout(MemoryLayout::k140_Standard)
129 , fCapabilities(0)
130 , fIdCount(1)
131 , fBoolTrue(0)
132 , fBoolFalse(0)
133 , fSetupFragPosition(false)
134 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400135 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700136 this->setupIntrinsics();
137 }
138
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500139 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700140
141private:
142 enum IntrinsicKind {
143 kGLSL_STD_450_IntrinsicKind,
144 kSPIRV_IntrinsicKind,
145 kSpecial_IntrinsicKind
146 };
147
148 enum SpecialIntrinsic {
149 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500150 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500151 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500152 kMax_SpecialIntrinsic,
153 kMin_SpecialIntrinsic,
154 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500155 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700156 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400157 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400158 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500159 kSmoothStep_SpecialIntrinsic,
160 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500161 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400162 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700163 };
164
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400165 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400166 kDefault,
167 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400168 };
169
ethannicholasb3058bd2016-07-01 08:22:01 -0700170 void setupIntrinsics();
171
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400172 /**
173 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
174 * appropriate, or null to never add one.
175 */
176 SpvId nextId(const Type* type);
177
178 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700179
Ethan Nicholase2c49992020-10-05 11:49:11 -0400180 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400181
ethannicholasb3058bd2016-07-01 08:22:01 -0700182 SpvId getType(const Type& type);
183
ethannicholas8ac838d2016-11-22 08:39:36 -0800184 SpvId getType(const Type& type, const MemoryLayout& layout);
185
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400186 SpvId getImageType(const Type& type);
187
ethannicholasd598f792016-07-25 10:08:54 -0700188 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189
ethannicholasd598f792016-07-25 10:08:54 -0700190 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700191
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500192 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800193 SpvStorageClass_ storageClass);
194
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400195 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
197 void writeLayout(const Layout& layout, SpvId target);
198
199 void writeLayout(const Layout& layout, SpvId target, int member);
200
ethannicholas8ac838d2016-11-22 08:39:36 -0800201 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400203 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204
Stephen White88574972020-06-23 19:09:29 -0400205 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700206
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400207 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700208
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400209 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700210
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400211 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700212
John Stilesdbd4e6f2021-02-16 13:29:15 -0500213 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700214
Brian Osmanc0213602020-10-06 14:43:32 -0400215 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700216
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400217 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700218
John Stilese40d1662021-01-29 10:08:50 -0500219 int findUniformFieldIndex(const Variable& var) const;
220
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400221 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700222
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400223 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700224
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400225 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
226
227 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
228
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500229
230 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
231 SpvId signedInst, SpvId unsignedInst,
232 const std::vector<SpvId>& args, OutputStream& out);
233
234 /**
235 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
236 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
237 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
238 * vec2, vec3).
239 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400240 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500241
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400242 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700243
ethannicholasf789b382016-08-03 12:43:36 -0700244 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400246 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700247
John Stilesd9d52712021-01-13 17:15:02 -0500248 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
249 OutputStream& out);
250
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400251 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500252
John Stilesd9d52712021-01-13 17:15:02 -0500253 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
254 OutputStream& out);
255
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400256 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
257
John Stilesd9d52712021-01-13 17:15:02 -0500258 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
259 OutputStream& out);
260
John Stilesa60fb172021-01-14 11:06:20 -0500261 SpvId writeBooleanConstructor(const Constructor& c, OutputStream& out);
262
John Stiles48c28842021-01-14 11:05:03 -0500263 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
264 OutputStream& out);
265
Ethan Nicholas84645e32017-02-09 13:57:14 -0500266 /**
267 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
268 * entries equal to zero.
269 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400270 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500271
272 /**
273 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
274 * source matrix are filled with zero; entries which do not exist in the destination matrix are
275 * ignored.
276 */
277 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400278 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500279
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400280 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400281 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
282 OutputStream& out);
283
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400284 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700285
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400286 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700287
John Stiles7384b372021-04-01 13:48:15 -0400288 SpvId writeArrayConstructor(const ConstructorArray& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400289
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400290 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700291
John Stilese1182782021-03-30 22:09:37 -0400292 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
293
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400294 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700295
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400296 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700297
Ethan Nicholasef653b82017-02-21 13:50:00 -0500298 /**
299 * Folds the potentially-vector result of a logical operation down to a single bool. If
300 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
301 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
302 * returns the original id value.
303 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400304 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500305
Ethan Nicholas68990be2017-07-13 09:36:52 -0400306 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400307 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
308 SpvOp_ mergeOperator, OutputStream& out);
309
310 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
311 SpvOp_ floatOperator, SpvOp_ intOperator,
312 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400313
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500314 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
315 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400316 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700317
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500318 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400319 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700320
John Stilesd94bfdd2021-03-25 11:44:08 -0400321 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
322
John Stiles45990502021-02-16 10:55:27 -0500323 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400324 const Type& rightType, SpvId rhs, const Type& resultType,
325 OutputStream& out);
326
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400327 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700328
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400329 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700330
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400331 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700332
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400335 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400337 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700338
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400339 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700340
ethannicholasf789b382016-08-03 12:43:36 -0700341 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700342
ethannicholasf789b382016-08-03 12:43:36 -0700343 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700344
ethannicholasf789b382016-08-03 12:43:36 -0700345 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700346
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400347 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700348
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400349 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700350
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400351 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700352
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400353 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700354
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400355 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500356
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500357 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
358
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400359 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700360
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400361 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700362
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400363 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700364
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400365 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700366
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400367 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700368
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700369 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700370
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400371 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700372
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400373 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700374
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700375 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700376
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400377 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700378
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700379 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700380
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700381 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400382 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700383
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400384 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700385
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500386 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400387 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700388
389 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
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 int32_t word5, 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, int32_t word6, 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, int32_t word7, 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 Nicholas941e7e22016-12-12 15:33:30 -0500402 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400403 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700404
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400405 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
406
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500407 MemoryLayout memoryLayoutForVariable(const Variable&) const;
408
John Stiles4d6310a2021-01-26 19:58:22 -0500409 struct EntrypointAdapter {
410 std::unique_ptr<FunctionDefinition> entrypointDef;
411 std::unique_ptr<FunctionDeclaration> entrypointDecl;
412 Layout fLayout;
413 Modifiers fModifiers;
414 };
415
416 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
417
John Stilese40d1662021-01-29 10:08:50 -0500418 struct UniformBuffer {
419 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
420 std::unique_ptr<Variable> fInnerVariable;
421 std::unique_ptr<Type> fStruct;
422 };
423
424 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
425
ethannicholasd598f792016-07-25 10:08:54 -0700426 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800427 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700428
ethannicholasb3058bd2016-07-01 08:22:01 -0700429 uint64_t fCapabilities;
430 SpvId fIdCount;
431 SpvId fGLSLExtendedInstructions;
432 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400433 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700434 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
435 std::unordered_map<const Variable*, SpvId> fVariableMap;
436 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400437 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400438 std::unordered_map<String, SpvId> fTypeMap;
439 StringStream fCapabilitiesBuffer;
440 StringStream fGlobalInitializersBuffer;
441 StringStream fConstantBuffer;
442 StringStream fExtraGlobalsBuffer;
443 StringStream fExternalFunctionsBuffer;
444 StringStream fVariableBuffer;
445 StringStream fNameBuffer;
446 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700447
448 SpvId fBoolTrue;
449 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500450 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500451 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500452 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700453 // label of the current block, or 0 if we are not in a block
454 SpvId fCurrentBlock;
455 std::stack<SpvId> fBreakTarget;
456 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000457 SpvId fRTHeightStructId = (SpvId) -1;
458 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400459 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400460 // holds variables synthesized during output, for lifetime purposes
461 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400462 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500463 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
464 // interface block.
465 UniformBuffer fUniformBuffer;
466 std::vector<const VarDeclaration*> fTopLevelUniforms;
467 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
468 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700469
470 friend class PointerLValue;
471 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500472
John Stiles7571f9e2020-09-02 22:42:33 -0400473 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700474};
475
John Stilesa6841be2020-08-06 14:11:56 -0400476} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700477
478#endif