blob: 0b98b44754a2d550e5a9827b687457d4d29614cc [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 Stiles2bec8ab2021-04-06 18:40:04 -040026#include "src/sksl/ir/SkSLConstructorComposite.h"
John Stiles268a73f2021-04-07 12:30:22 -040027#include "src/sksl/ir/SkSLConstructorCompositeCast.h"
John Stilese1182782021-03-30 22:09:37 -040028#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040029#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040030#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040031#include "src/sksl/ir/SkSLConstructorSplat.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/sksl/ir/SkSLDoStatement.h"
33#include "src/sksl/ir/SkSLFieldAccess.h"
34#include "src/sksl/ir/SkSLFloatLiteral.h"
35#include "src/sksl/ir/SkSLForStatement.h"
36#include "src/sksl/ir/SkSLFunctionCall.h"
37#include "src/sksl/ir/SkSLFunctionDeclaration.h"
38#include "src/sksl/ir/SkSLFunctionDefinition.h"
39#include "src/sksl/ir/SkSLIfStatement.h"
40#include "src/sksl/ir/SkSLIndexExpression.h"
41#include "src/sksl/ir/SkSLIntLiteral.h"
42#include "src/sksl/ir/SkSLInterfaceBlock.h"
43#include "src/sksl/ir/SkSLPostfixExpression.h"
44#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLReturnStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/ir/SkSLSwitchStatement.h"
47#include "src/sksl/ir/SkSLSwizzle.h"
48#include "src/sksl/ir/SkSLTernaryExpression.h"
49#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070052
John Stilescd806892021-01-06 13:33:31 -050053namespace SkSL {
54
John Stilesbdc3d3c2021-01-06 18:41:40 -050055struct SPIRVNumberConstant {
56 bool operator==(const SPIRVNumberConstant& that) const {
57 return fValueBits == that.fValueBits &&
58 fKind == that.fKind;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040059 }
John Stilesbdc3d3c2021-01-06 18:41:40 -050060 int64_t fValueBits; // contains either an SKSL_INT or zero-padded bits from an SKSL_FLOAT
61 SkSL::Type::NumberKind fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040062};
63
John Stilescd806892021-01-06 13:33:31 -050064struct SPIRVVectorConstant {
65 bool operator==(const SPIRVVectorConstant& that) const {
66 return fTypeId == that.fTypeId &&
67 fValueId[0] == that.fValueId[0] &&
68 fValueId[1] == that.fValueId[1] &&
69 fValueId[2] == that.fValueId[2] &&
70 fValueId[3] == that.fValueId[3];
71 }
72 SpvId fTypeId;
73 SpvId fValueId[4];
74};
75
76} // namespace SkSL
77
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040078namespace std {
79
80template <>
John Stilesbdc3d3c2021-01-06 18:41:40 -050081struct hash<SkSL::SPIRVNumberConstant> {
82 size_t operator()(const SkSL::SPIRVNumberConstant& key) const {
83 return key.fValueBits ^ (int)key.fKind;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040084 }
85};
86
John Stilescd806892021-01-06 13:33:31 -050087template <>
88struct hash<SkSL::SPIRVVectorConstant> {
89 size_t operator()(const SkSL::SPIRVVectorConstant& key) const {
90 return SkOpts::hash(&key, sizeof(key));
91 }
92};
93
John Stilesa6841be2020-08-06 14:11:56 -040094} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040095
ethannicholasb3058bd2016-07-01 08:22:01 -070096namespace SkSL {
97
ethannicholasb3058bd2016-07-01 08:22:01 -070098/**
99 * Converts a Program into a SPIR-V binary.
100 */
101class SPIRVCodeGenerator : public CodeGenerator {
102public:
103 class LValue {
104 public:
105 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -0500106
ethannicholasb3058bd2016-07-01 08:22:01 -0700107 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
John Stiles3f14d282021-02-05 09:31:04 -0500108 // by a pointer (e.g. vector swizzles), returns -1.
109 virtual SpvId getPointer() { return -1; }
110
Ethan Nicholase0707b72021-03-17 11:16:41 -0400111 // Returns true if a valid pointer returned by getPointer represents a memory object
112 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
113 // getPointer() returns -1.
114 virtual bool isMemoryObjectPointer() const { return true; }
115
John Stiles3f14d282021-02-05 09:31:04 -0500116 // Applies a swizzle to the components of the LValue, if possible. This is used to create
117 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
118 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
119 return false;
120 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700121
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400122 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700123
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400124 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700125 };
126
Brian Osman8b43dad2020-10-09 13:31:42 -0400127 SPIRVCodeGenerator(const Context* context,
128 const Program* program,
129 ErrorReporter* errors,
130 OutputStream* out)
131 : INHERITED(program, errors, out)
132 , fContext(*context)
133 , fDefaultLayout(MemoryLayout::k140_Standard)
134 , fCapabilities(0)
135 , fIdCount(1)
136 , fBoolTrue(0)
137 , fBoolFalse(0)
138 , fSetupFragPosition(false)
139 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400140 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 this->setupIntrinsics();
142 }
143
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500144 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700145
146private:
147 enum IntrinsicKind {
148 kGLSL_STD_450_IntrinsicKind,
149 kSPIRV_IntrinsicKind,
150 kSpecial_IntrinsicKind
151 };
152
153 enum SpecialIntrinsic {
154 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500155 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500156 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500157 kMax_SpecialIntrinsic,
158 kMin_SpecialIntrinsic,
159 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500160 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700161 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400162 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400163 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500164 kSmoothStep_SpecialIntrinsic,
165 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500166 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400167 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700168 };
169
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400170 enum class Precision {
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400171 kDefault,
172 kRelaxed,
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400173 };
174
ethannicholasb3058bd2016-07-01 08:22:01 -0700175 void setupIntrinsics();
176
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400177 /**
178 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
179 * appropriate, or null to never add one.
180 */
181 SpvId nextId(const Type* type);
182
183 SpvId nextId(Precision precision);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184
Ethan Nicholase2c49992020-10-05 11:49:11 -0400185 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400186
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 SpvId getType(const Type& type);
188
ethannicholas8ac838d2016-11-22 08:39:36 -0800189 SpvId getType(const Type& type, const MemoryLayout& layout);
190
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400191 SpvId getImageType(const Type& type);
192
ethannicholasd598f792016-07-25 10:08:54 -0700193 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
ethannicholasd598f792016-07-25 10:08:54 -0700195 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500197 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800198 SpvStorageClass_ storageClass);
199
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400200 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700201
202 void writeLayout(const Layout& layout, SpvId target);
203
204 void writeLayout(const Layout& layout, SpvId target, int member);
205
ethannicholas8ac838d2016-11-22 08:39:36 -0800206 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400208 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700209
Stephen White88574972020-06-23 19:09:29 -0400210 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700211
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400212 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700213
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400214 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400216 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
John Stilesdbd4e6f2021-02-16 13:29:15 -0500218 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
Brian Osmanc0213602020-10-06 14:43:32 -0400220 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700221
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400222 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
John Stilese40d1662021-01-29 10:08:50 -0500224 int findUniformFieldIndex(const Variable& var) const;
225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400230 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
231
232 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
233
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500234
235 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
236 SpvId signedInst, SpvId unsignedInst,
237 const std::vector<SpvId>& args, OutputStream& out);
238
239 /**
240 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
241 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
242 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
243 * vec2, vec3).
244 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400245 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500246
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400247 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700248
John Stiles2938eea2021-04-01 18:58:25 -0400249 SpvId writeConstantVector(const AnyConstructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700250
John Stilesfd7252f2021-04-04 22:24:40 -0400251 SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700252
John Stilesd9d52712021-01-13 17:15:02 -0500253 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
254 OutputStream& out);
255
John Stilesfd7252f2021-04-04 22:24:40 -0400256 SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500257
John Stilesd9d52712021-01-13 17:15:02 -0500258 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
259 OutputStream& out);
260
John Stilesfd7252f2021-04-04 22:24:40 -0400261 SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400262
John Stilesd9d52712021-01-13 17:15:02 -0500263 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
264 OutputStream& out);
265
John Stilesfd7252f2021-04-04 22:24:40 -0400266 SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out);
John Stilesa60fb172021-01-14 11:06:20 -0500267
John Stiles48c28842021-01-14 11:05:03 -0500268 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
269 OutputStream& out);
270
John Stilesb14a8192021-04-05 11:40:46 -0400271 SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType,
272 OutputStream& out);
273
Ethan Nicholas84645e32017-02-09 13:57:14 -0500274 /**
275 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
276 * entries equal to zero.
277 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400278 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500279
280 /**
281 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
282 * source matrix are filled with zero; entries which do not exist in the destination matrix are
283 * ignored.
284 */
John Stiles268a73f2021-04-07 12:30:22 -0400285 SpvId writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500286
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400287 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400288 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
289 OutputStream& out);
290
John Stiles2bec8ab2021-04-06 18:40:04 -0400291 SpvId writeConstructorComposite(const ConstructorComposite& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
John Stiles2bec8ab2021-04-06 18:40:04 -0400293 SpvId writeMatrixConstructor(const ConstructorComposite& c, OutputStream& out);
294
295 SpvId writeVectorConstructor(const ConstructorComposite& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700296
John Stiles7384b372021-04-01 13:48:15 -0400297 SpvId writeArrayConstructor(const ConstructorArray& c, OutputStream& out);
Ethan Nicholasbd553222017-07-18 15:54:59 -0400298
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
John Stilese1182782021-03-30 22:09:37 -0400301 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
302
John Stiles5abb9e12021-04-06 13:47:19 -0400303 SpvId writeConstructorMatrixResize(const ConstructorMatrixResize& c, OutputStream& out);
304
John Stilesfd7252f2021-04-04 22:24:40 -0400305 SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out);
306
John Stiles2938eea2021-04-01 18:58:25 -0400307 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
308
John Stiles268a73f2021-04-07 12:30:22 -0400309 SpvId writeConstructorCompositeCast(const ConstructorCompositeCast& c, OutputStream& out);
John Stilesb14a8192021-04-05 11:40:46 -0400310
311 SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out);
312
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400313 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700314
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700316
Ethan Nicholasef653b82017-02-21 13:50:00 -0500317 /**
318 * Folds the potentially-vector result of a logical operation down to a single bool. If
319 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
320 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
321 * returns the original id value.
322 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400323 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500324
Ethan Nicholas68990be2017-07-13 09:36:52 -0400325 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400326 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
327 SpvOp_ mergeOperator, OutputStream& out);
328
329 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
330 SpvOp_ floatOperator, SpvOp_ intOperator,
331 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400332
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500333 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
334 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400335 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500337 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400338 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700339
John Stilesd94bfdd2021-03-25 11:44:08 -0400340 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
341
John Stiles45990502021-02-16 10:55:27 -0500342 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
Ethan Nicholas49465b42019-04-17 12:22:21 -0400343 const Type& rightType, SpvId rhs, const Type& resultType,
344 OutputStream& out);
345
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400346 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400348 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700349
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400350 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700351
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400352 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700353
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400354 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700355
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400356 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700357
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700359
ethannicholasf789b382016-08-03 12:43:36 -0700360 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700361
ethannicholasf789b382016-08-03 12:43:36 -0700362 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700363
ethannicholasf789b382016-08-03 12:43:36 -0700364 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700365
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400366 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700367
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400368 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700369
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400370 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700371
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400372 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700373
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400374 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500375
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500376 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
377
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400378 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700379
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400380 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700381
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400382 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700383
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400384 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700385
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400386 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700387
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700388 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700389
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400390 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700391
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400392 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700393
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700394 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700395
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400396 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700397
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700398 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700399
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700400 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400401 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400403 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700404
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500405 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400406 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700407
408 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400409 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700410
411 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400412 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700413
414 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400415 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700416
417 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400418 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700419
420 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500421 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400422 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400424 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
425
Brian Osman2a4c0fb2021-01-22 13:41:40 -0500426 MemoryLayout memoryLayoutForVariable(const Variable&) const;
427
John Stiles4d6310a2021-01-26 19:58:22 -0500428 struct EntrypointAdapter {
429 std::unique_ptr<FunctionDefinition> entrypointDef;
430 std::unique_ptr<FunctionDeclaration> entrypointDecl;
431 Layout fLayout;
432 Modifiers fModifiers;
433 };
434
435 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
436
John Stilese40d1662021-01-29 10:08:50 -0500437 struct UniformBuffer {
438 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
439 std::unique_ptr<Variable> fInnerVariable;
440 std::unique_ptr<Type> fStruct;
441 };
442
443 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
444
ethannicholasd598f792016-07-25 10:08:54 -0700445 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800446 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700447
ethannicholasb3058bd2016-07-01 08:22:01 -0700448 uint64_t fCapabilities;
449 SpvId fIdCount;
450 SpvId fGLSLExtendedInstructions;
451 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400452 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700453 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
454 std::unordered_map<const Variable*, SpvId> fVariableMap;
455 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400456 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400457 std::unordered_map<String, SpvId> fTypeMap;
458 StringStream fCapabilitiesBuffer;
459 StringStream fGlobalInitializersBuffer;
460 StringStream fConstantBuffer;
461 StringStream fExtraGlobalsBuffer;
462 StringStream fExternalFunctionsBuffer;
463 StringStream fVariableBuffer;
464 StringStream fNameBuffer;
465 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700466
467 SpvId fBoolTrue;
468 SpvId fBoolFalse;
John Stilesbdc3d3c2021-01-06 18:41:40 -0500469 std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants;
John Stilescd806892021-01-06 13:33:31 -0500470 std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500471 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700472 // label of the current block, or 0 if we are not in a block
473 SpvId fCurrentBlock;
474 std::stack<SpvId> fBreakTarget;
475 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000476 SpvId fRTHeightStructId = (SpvId) -1;
477 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400478 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400479 // holds variables synthesized during output, for lifetime purposes
480 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400481 int fSkInCount = 1;
John Stilese40d1662021-01-29 10:08:50 -0500482 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
483 // interface block.
484 UniformBuffer fUniformBuffer;
485 std::vector<const VarDeclaration*> fTopLevelUniforms;
486 std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index>
487 SpvId fUniformBufferId = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700488
489 friend class PointerLValue;
490 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500491
John Stiles7571f9e2020-09-02 22:42:33 -0400492 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700493};
494
John Stilesa6841be2020-08-06 14:11:56 -0400495} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700496
497#endif