blob: 8f3d862bbd829f1ea88bd8f64b66be634f038a3a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/sksl/SkSLCodeGenerator.h"
16#include "src/sksl/SkSLMemoryLayout.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050017#include "src/sksl/SkSLStringStream.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/sksl/ir/SkSLBinaryExpression.h"
19#include "src/sksl/ir/SkSLBoolLiteral.h"
20#include "src/sksl/ir/SkSLConstructor.h"
21#include "src/sksl/ir/SkSLDoStatement.h"
22#include "src/sksl/ir/SkSLFieldAccess.h"
23#include "src/sksl/ir/SkSLFloatLiteral.h"
24#include "src/sksl/ir/SkSLForStatement.h"
25#include "src/sksl/ir/SkSLFunctionCall.h"
26#include "src/sksl/ir/SkSLFunctionDeclaration.h"
27#include "src/sksl/ir/SkSLFunctionDefinition.h"
28#include "src/sksl/ir/SkSLIfStatement.h"
29#include "src/sksl/ir/SkSLIndexExpression.h"
30#include "src/sksl/ir/SkSLIntLiteral.h"
31#include "src/sksl/ir/SkSLInterfaceBlock.h"
32#include "src/sksl/ir/SkSLPostfixExpression.h"
33#include "src/sksl/ir/SkSLPrefixExpression.h"
34#include "src/sksl/ir/SkSLProgramElement.h"
35#include "src/sksl/ir/SkSLReturnStatement.h"
36#include "src/sksl/ir/SkSLStatement.h"
37#include "src/sksl/ir/SkSLSwitchStatement.h"
38#include "src/sksl/ir/SkSLSwizzle.h"
39#include "src/sksl/ir/SkSLTernaryExpression.h"
40#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLVariableReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070043
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040044union ConstantValue {
45 ConstantValue(int64_t i)
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040046 : fInt(i) {
47 SkASSERT(sizeof(*this) == sizeof(int64_t));
48 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040049
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040050 ConstantValue(SKSL_FLOAT f) {
51 memset(this, 0, sizeof(*this));
52 fFloat = f;
53 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040054
55 bool operator==(const ConstantValue& other) const {
56 return fInt == other.fInt;
57 }
58
59 int64_t fInt;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040060 SKSL_FLOAT fFloat;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040061};
62
63enum class ConstantType {
64 kInt,
65 kUInt,
66 kShort,
67 kUShort,
68 kFloat,
69 kDouble,
70 kHalf,
71};
72
73namespace std {
74
75template <>
76struct hash<std::pair<ConstantValue, ConstantType>> {
77 size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
78 return key.first.fInt ^ (int) key.second;
79 }
80};
81
John Stilesa6841be2020-08-06 14:11:56 -040082} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040083
ethannicholasb3058bd2016-07-01 08:22:01 -070084namespace SkSL {
85
86#define kLast_Capability SpvCapabilityMultiViewport
87
88/**
89 * Converts a Program into a SPIR-V binary.
90 */
91class SPIRVCodeGenerator : public CodeGenerator {
92public:
93 class LValue {
94 public:
95 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050096
ethannicholasb3058bd2016-07-01 08:22:01 -070097 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
98 // by a pointer (e.g. vector swizzles), returns 0.
99 virtual SpvId getPointer() = 0;
100
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400101 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700102
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400103 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700104 };
105
Brian Osman8b43dad2020-10-09 13:31:42 -0400106 SPIRVCodeGenerator(const Context* context,
107 const Program* program,
108 ErrorReporter* errors,
109 OutputStream* out)
110 : INHERITED(program, errors, out)
111 , fContext(*context)
112 , fDefaultLayout(MemoryLayout::k140_Standard)
113 , fCapabilities(0)
114 , fIdCount(1)
115 , fBoolTrue(0)
116 , fBoolFalse(0)
117 , fSetupFragPosition(false)
118 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400119 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 this->setupIntrinsics();
121 }
122
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500123 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700124
125private:
126 enum IntrinsicKind {
127 kGLSL_STD_450_IntrinsicKind,
128 kSPIRV_IntrinsicKind,
129 kSpecial_IntrinsicKind
130 };
131
132 enum SpecialIntrinsic {
133 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500134 kClamp_SpecialIntrinsic,
Brian Osmand1b593f2020-12-28 13:00:46 -0500135 kMatrixCompMult_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500136 kMax_SpecialIntrinsic,
137 kMin_SpecialIntrinsic,
138 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500139 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700140 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400141 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400142 kSampledImage_SpecialIntrinsic,
Brian Osman6ba3be12020-11-13 16:32:52 -0500143 kSmoothStep_SpecialIntrinsic,
144 kStep_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500145 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400146 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 };
148
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400149 enum class Precision {
150 kLow,
151 kHigh,
152 };
153
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 void setupIntrinsics();
155
156 SpvId nextId();
157
Ethan Nicholase2c49992020-10-05 11:49:11 -0400158 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400159
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 SpvId getType(const Type& type);
161
ethannicholas8ac838d2016-11-22 08:39:36 -0800162 SpvId getType(const Type& type, const MemoryLayout& layout);
163
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400164 SpvId getImageType(const Type& type);
165
ethannicholasd598f792016-07-25 10:08:54 -0700166 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700167
ethannicholasd598f792016-07-25 10:08:54 -0700168 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700169
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500170 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800171 SpvStorageClass_ storageClass);
172
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400173 void writePrecisionModifier(Precision precision, SpvId id);
174
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500175 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400176
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400177 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700178
179 void writeLayout(const Layout& layout, SpvId target);
180
181 void writeLayout(const Layout& layout, SpvId target, int member);
182
ethannicholas8ac838d2016-11-22 08:39:36 -0800183 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400185 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186
Stephen White88574972020-06-23 19:09:29 -0400187 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400189 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400191 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400193 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Brian Osmanc0213602020-10-06 14:43:32 -0400195 void writeGlobalVar(Program::Kind kind, const VarDeclaration& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Brian Osmanc0213602020-10-06 14:43:32 -0400197 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400199 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400201 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400203 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400205 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
206
207 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
208
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500209
210 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
211 SpvId signedInst, SpvId unsignedInst,
212 const std::vector<SpvId>& args, OutputStream& out);
213
214 /**
215 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
216 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
217 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
218 * vec2, vec3).
219 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400220 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500221
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400222 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
ethannicholasf789b382016-08-03 12:43:36 -0700224 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500229
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400230 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
231
Ethan Nicholas84645e32017-02-09 13:57:14 -0500232 /**
233 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
234 * entries equal to zero.
235 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500237
238 /**
239 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
240 * source matrix are filled with zero; entries which do not exist in the destination matrix are
241 * ignored.
242 */
243 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400244 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500245
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400246 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400247 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
248 OutputStream& out);
249
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400250 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253
Ethan Nicholasbd553222017-07-18 15:54:59 -0400254 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
255
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400256 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700257
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400258 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700259
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400260 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700261
Ethan Nicholasef653b82017-02-21 13:50:00 -0500262 /**
263 * Folds the potentially-vector result of a logical operation down to a single bool. If
264 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
265 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
266 * returns the original id value.
267 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400268 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500269
Ethan Nicholas68990be2017-07-13 09:36:52 -0400270 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400271 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
272 SpvOp_ mergeOperator, OutputStream& out);
273
274 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
275 SpvOp_ floatOperator, SpvOp_ intOperator,
276 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400277
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500278 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
279 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400280 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700281
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500282 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400283 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700284
Ethan Nicholas49465b42019-04-17 12:22:21 -0400285 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
286 const Type& rightType, SpvId rhs, const Type& resultType,
287 OutputStream& out);
288
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400289 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700290
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700294
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400295 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700296
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400297 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700298
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400301 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700302
ethannicholasf789b382016-08-03 12:43:36 -0700303 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700304
ethannicholasf789b382016-08-03 12:43:36 -0700305 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700306
ethannicholasf789b382016-08-03 12:43:36 -0700307 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700308
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700310
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400311 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700312
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400313 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700314
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700316
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400317 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500318
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500319 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
320
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400321 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700322
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400323 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700324
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400325 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700326
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400327 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700328
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400329 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700330
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700331 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700332
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400335 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700337 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700338
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400339 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700340
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700341 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700342
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700343 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400344 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700345
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400346 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500348 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400349 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700350
351 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400352 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700353
354 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400355 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700356
357 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700359
360 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400361 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700362
363 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500364 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400365 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700366
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400367 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
368
ethannicholasd598f792016-07-25 10:08:54 -0700369 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800370 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700371
ethannicholasb3058bd2016-07-01 08:22:01 -0700372 uint64_t fCapabilities;
373 SpvId fIdCount;
374 SpvId fGLSLExtendedInstructions;
375 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400376 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700377 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
378 std::unordered_map<const Variable*, SpvId> fVariableMap;
379 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400380 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400381 std::unordered_map<String, SpvId> fTypeMap;
382 StringStream fCapabilitiesBuffer;
383 StringStream fGlobalInitializersBuffer;
384 StringStream fConstantBuffer;
385 StringStream fExtraGlobalsBuffer;
386 StringStream fExternalFunctionsBuffer;
387 StringStream fVariableBuffer;
388 StringStream fNameBuffer;
389 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700390
391 SpvId fBoolTrue;
392 SpvId fBoolFalse;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400393 std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500394 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700395 // label of the current block, or 0 if we are not in a block
396 SpvId fCurrentBlock;
397 std::stack<SpvId> fBreakTarget;
398 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000399 SpvId fRTHeightStructId = (SpvId) -1;
400 SpvId fRTHeightFieldIndex = (SpvId) -1;
Jim Van Verthf3ec9832020-10-21 16:09:57 -0400401 SpvStorageClass_ fRTHeightStorageClass;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400402 // holds variables synthesized during output, for lifetime purposes
403 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400404 int fSkInCount = 1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700405
406 friend class PointerLValue;
407 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500408
John Stiles7571f9e2020-09-02 22:42:33 -0400409 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700410};
411
John Stilesa6841be2020-08-06 14:11:56 -0400412} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700413
414#endif