blob: 260ddd8fd63d31ecaed66269b1290e59c8aeb962 [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 Stilesfd7252f2021-04-04 22:24:40 -040027#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040028#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesb14a8192021-04-05 11:40:46 -040029#include "src/sksl/ir/SkSLConstructorVectorCast.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/sksl/ir/SkSLDoStatement.h"
31#include "src/sksl/ir/SkSLFieldAccess.h"
32#include "src/sksl/ir/SkSLFloatLiteral.h"
33#include "src/sksl/ir/SkSLForStatement.h"
34#include "src/sksl/ir/SkSLFunctionCall.h"
35#include "src/sksl/ir/SkSLFunctionDeclaration.h"
36#include "src/sksl/ir/SkSLFunctionDefinition.h"
37#include "src/sksl/ir/SkSLIfStatement.h"
38#include "src/sksl/ir/SkSLIndexExpression.h"
39#include "src/sksl/ir/SkSLIntLiteral.h"
40#include "src/sksl/ir/SkSLInterfaceBlock.h"
41#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/ir/SkSLSwitchStatement.h"
45#include "src/sksl/ir/SkSLSwizzle.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070050
John Stilescd806892021-01-06 13:33:31 -050051namespace SkSL {
52
John Stilesbdc3d3c2021-01-06 18:41:40 -050053struct SPIRVNumberConstant {
54 bool operator==(const SPIRVNumberConstant& that) const {
55 return fValueBits == that.fValueBits &&
56 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040057 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050058 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
59 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040060};
61
John Stilescd806892021-01-06 13:33:31 -050062struct SPIRVVectorConstant {
63 bool operator==(const SPIRVVectorConstant& that) const {
64 return fTypeId == that.fTypeId &&
65 fValueId[0] == that.fValueId[0] &&
66 fValueId[1] == that.fValueId[1] &&
67 fValueId[2] == that.fValueId[2] &&
68 fValueId[3] == that.fValueId[3];
69 }
70 SpvId fTypeId;
71 SpvId fValueId[4];
72};
73
74} // namespace SkSL
75
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040076namespace std {
77
78template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050079struct hash<SkSL::SPIRVNumberConstant> {
80 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
81 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040082 }
83};
84
John Stilescd806892021-01-06 13:33:31 -050085template <>
86struct hash<SkSL::SPIRVVectorConstant> {
87 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
88 return SkOpts::hash(&key, sizeof(key));
89 }
90};
91
John Stilesa6841be2020-08-06 14:11:56 -040092} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040093
ethannicholasb3058bd2016-07-01 08:22:01 -070094namespace SkSL {
95
ethannicholasb3058bd2016-07-01 08:22:01 -070096/**
97 * Converts a Program into a SPIR-V binary.
98 */
99class SPIRVCodeGenerator : public CodeGenerator {
100public:
101 class LValue {
102 public:
103 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500104
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500106 // by a pointer (e.g. vector swizzles), returns -1.
107 virtual SpvId getPointer() { return -1; }
108
Ethan Nicholase0707b72021-03-17 11:16:41 -0400109 // Returns true if a valid pointer returned by getPointer represents a memory object
110 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
111 // getPointer() returns -1.
112 virtual bool isMemoryObjectPointer() const { return true; }
113
John Stiles3f14d282021-02-05 09:31:04 -0500114 // Applies a swizzle to the components of the LValue, if possible. This is used to create
115 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
116 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
117 return false;
118 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700119
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400120 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700121
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400122 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700123 };
124
Brian Osman8b43dad2020-10-09 13:31:42 -0400125 SPIRVCodeGenerator(const Context* context,
126 const Program* program,
127 ErrorReporter* errors,
128 OutputStream* out)
129 : INHERITED(program, errors, out)
130 , fContext(*context)
131 , fDefaultLayout(MemoryLayout::k140_Standard)
132 , fCapabilities(0)
133 , fIdCount(1)
134 , fBoolTrue(0)
135 , fBoolFalse(0)
136 , fSetupFragPosition(false)
137 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400138 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700139 this->setupIntrinsics();
140 }
141
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500142 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700143
144private:
145 enum IntrinsicKind {
146 kGLSL_STD_450_IntrinsicKind,
147 kSPIRV_IntrinsicKind,
148 kSpecial_IntrinsicKind
149 };
150
151 enum SpecialIntrinsic {
152 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500153 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500154 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500155 kMax_SpecialIntrinsic,
156 kMin_SpecialIntrinsic,
157 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500158 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700159 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400160 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400161 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500162 kSmoothStep_SpecialIntrinsic,
163 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500164 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400165 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 };
167
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400168 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400169 kDefault,
170 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400171 };
172
ethannicholasb3058bd2016-07-01 08:22:01 -0700173 void setupIntrinsics();
174
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400175 /**
176 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
177 * appropriate, or null to never add one.
178 */
179 SpvId nextId(const Type* type);
180
181 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182
Ethan Nicholase2c49992020-10-05 11:49:11 -0400183 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400184
ethannicholasb3058bd2016-07-01 08:22:01 -0700185 SpvId getType(const Type& type);
186
ethannicholas8ac838d2016-11-22 08:39:36 -0800187 SpvId getType(const Type& type, const MemoryLayout& layout);
188
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400189 SpvId getImageType(const Type& type);
190
ethannicholasd598f792016-07-25 10:08:54 -0700191 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
ethannicholasd598f792016-07-25 10:08:54 -0700193 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500195 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800196 SpvStorageClass_ storageClass);
197
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400198 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700199
200 void writeLayout(const Layout& layout, SpvId target);
201
202 void writeLayout(const Layout& layout, SpvId target, int member);
203
ethannicholas8ac838d2016-11-22 08:39:36 -0800204 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700205
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400206 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207
Stephen White88574972020-06-23 19:09:29 -0400208 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700209
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400210 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700211
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400212 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700213
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400214 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
John Stilesdbd4e6f2021-02-16 13:29:15 -0500216 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
Brian Osmanc0213602020-10-06 14:43:32 -0400218 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400220 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700221
John Stilese40d1662021-01-29 10:08:50 -0500222 int findUniformFieldIndex(const Variable& var) const;
223
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400224 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
229
230 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
231
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500232
233 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
234 SpvId signedInst, SpvId unsignedInst,
235 const std::vector<SpvId>& args, OutputStream& out);
236
237 /**
238 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
239 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
240 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
241 * vec2, vec3).
242 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400243 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500244
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400245 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246
John Stiles2938eea2021-04-01 18:58:25 -0400247 SpvId writeConstantVector(const AnyConstructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700248
John Stilesfd7252f2021-04-04 22:24:40 -0400249 SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700250
John Stilesd9d52712021-01-13 17:15:02 -0500251 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
252 OutputStream& out);
253
John Stilesfd7252f2021-04-04 22:24:40 -0400254 SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500255
John Stilesd9d52712021-01-13 17:15:02 -0500256 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
257 OutputStream& out);
258
John Stilesfd7252f2021-04-04 22:24:40 -0400259 SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400260
John Stilesd9d52712021-01-13 17:15:02 -0500261 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
262 OutputStream& out);
263
John Stilesfd7252f2021-04-04 22:24:40 -0400264 SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out);
John Stilesa60fb172021-01-14 11:06:20 -0500265
John Stiles48c28842021-01-14 11:05:03 -0500266 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
267 OutputStream& out);
268
John Stilesb14a8192021-04-05 11:40:46 -0400269 SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType,
270 OutputStream& out);
271
Ethan Nicholas84645e32017-02-09 13:57:14 -0500272 /**
273 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
274 * entries equal to zero.
275 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400276 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500277
278 /**
279 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
280 * source matrix are filled with zero; entries which do not exist in the destination matrix are
281 * ignored.
282 */
283 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400284 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500285
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400286 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400287 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
288 OutputStream& out);
289
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400290 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700291
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400292 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700293
John Stiles7384b372021-04-01 13:48:15 -0400294 SpvId writeArrayConstructor(const ConstructorArray& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400295
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400296 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700297
John Stilese1182782021-03-30 22:09:37 -0400298 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
299
John Stilesfd7252f2021-04-04 22:24:40 -0400300 SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out);
301
John Stiles2938eea2021-04-01 18:58:25 -0400302 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
303
John Stilesb14a8192021-04-05 11:40:46 -0400304 SpvId writeConstructorVectorCast(const ConstructorVectorCast& c, OutputStream& out);
305
306 SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out);
307
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400308 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700309
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400310 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700311
Ethan Nicholasef653b82017-02-21 13:50:00 -0500312 /**
313 * Folds the potentially-vector result of a logical operation down to a single bool. If
314 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
315 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
316 * returns the original id value.
317 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400318 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500319
Ethan Nicholas68990be2017-07-13 09:36:52 -0400320 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400321 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
322 SpvOp_ mergeOperator, OutputStream& out);
323
324 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
325 SpvOp_ floatOperator, SpvOp_ intOperator,
326 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400327
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500328 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
329 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400330 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700331
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500332 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
John Stilesd94bfdd2021-03-25 11:44:08 -0400335 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
336
John Stiles45990502021-02-16 10:55:27 -0500337 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400338 const Type& rightType, SpvId rhs, const Type& resultType,
339 OutputStream& out);
340
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400341 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700342
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400343 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700344
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400345 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700346
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400347 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700348
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400349 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700350
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400351 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700352
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400353 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700354
ethannicholasf789b382016-08-03 12:43:36 -0700355 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700356
ethannicholasf789b382016-08-03 12:43:36 -0700357 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700358
ethannicholasf789b382016-08-03 12:43:36 -0700359 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700360
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400361 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700362
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400363 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700364
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400365 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700366
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400367 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700368
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400369 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500370
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500371 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
372
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400373 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700374
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400375 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700376
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400377 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700378
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400379 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700380
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400381 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700382
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700383 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700384
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400385 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700386
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400387 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700388
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700389 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700390
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400391 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700392
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700393 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700394
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700395 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400396 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700397
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400398 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700399
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500400 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400401 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402
403 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400404 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700405
406 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400407 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700408
409 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400410 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700411
412 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400413 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700414
415 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500416 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400417 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700418
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400419 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
420
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500421 MemoryLayout memoryLayoutForVariable(const Variable&) const;
422
John Stiles4d6310a2021-01-26 19:58:22 -0500423 struct EntrypointAdapter {
424 std::unique_ptr<FunctionDefinition> entrypointDef;
425 std::unique_ptr<FunctionDeclaration> entrypointDecl;
426 Layout fLayout;
427 Modifiers fModifiers;
428 };
429
430 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
431
John Stilese40d1662021-01-29 10:08:50 -0500432 struct UniformBuffer {
433 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
434 std::unique_ptr<Variable> fInnerVariable;
435 std::unique_ptr<Type> fStruct;
436 };
437
438 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
439
ethannicholasd598f792016-07-25 10:08:54 -0700440 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800441 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700442
ethannicholasb3058bd2016-07-01 08:22:01 -0700443 uint64_t fCapabilities;
444 SpvId fIdCount;
445 SpvId fGLSLExtendedInstructions;
446 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400447 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700448 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
449 std::unordered_map<const Variable*, SpvId> fVariableMap;
450 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400451 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400452 std::unordered_map<String, SpvId> fTypeMap;
453 StringStream fCapabilitiesBuffer;
454 StringStream fGlobalInitializersBuffer;
455 StringStream fConstantBuffer;
456 StringStream fExtraGlobalsBuffer;
457 StringStream fExternalFunctionsBuffer;
458 StringStream fVariableBuffer;
459 StringStream fNameBuffer;
460 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700461
462 SpvId fBoolTrue;
463 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500464 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500465 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500466 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700467 // label of the current block, or 0 if we are not in a block
468 SpvId fCurrentBlock;
469 std::stack<SpvId> fBreakTarget;
470 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000471 SpvId fRTHeightStructId = (SpvId) -1;
472 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400473 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400474 // holds variables synthesized during output, for lifetime purposes
475 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400476 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500477 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
478 // interface block.
479 UniformBuffer fUniformBuffer;
480 std::vector<const VarDeclaration*> fTopLevelUniforms;
481 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
482 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700483
484 friend class PointerLValue;
485 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500486
John Stiles7571f9e2020-09-02 22:42:33 -0400487 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700488};
489
John Stilesa6841be2020-08-06 14:11:56 -0400490} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700491
492#endif