blob: 9bbc805a5b2a2c7e7ff5c340c6e8ea78305f6077 [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"
41#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
42#include "src/sksl/ir/SkSLVariableReference.h"
43#include "src/sksl/ir/SkSLWhileStatement.h"
44#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070045
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040046union ConstantValue {
47 ConstantValue(int64_t i)
48 : fInt(i) {}
49
Brian Osmana0c82f02020-10-01 08:56:53 -040050 ConstantValue(double d)
51 : fDouble(d) {}
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040052
53 bool operator==(const ConstantValue& other) const {
54 return fInt == other.fInt;
55 }
56
57 int64_t fInt;
Brian Osmana0c82f02020-10-01 08:56:53 -040058 double fDouble;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040059};
60
61enum class ConstantType {
62 kInt,
63 kUInt,
64 kShort,
65 kUShort,
66 kFloat,
67 kDouble,
68 kHalf,
69};
70
71namespace std {
72
73template <>
74struct hash<std::pair<ConstantValue, ConstantType>> {
75 size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
76 return key.first.fInt ^ (int) key.second;
77 }
78};
79
John Stilesa6841be2020-08-06 14:11:56 -040080} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040081
ethannicholasb3058bd2016-07-01 08:22:01 -070082namespace SkSL {
83
84#define kLast_Capability SpvCapabilityMultiViewport
85
86/**
87 * Converts a Program into a SPIR-V binary.
88 */
89class SPIRVCodeGenerator : public CodeGenerator {
90public:
91 class LValue {
92 public:
93 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050094
ethannicholasb3058bd2016-07-01 08:22:01 -070095 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
96 // by a pointer (e.g. vector swizzles), returns 0.
97 virtual SpvId getPointer() = 0;
98
Ethan Nicholas0df1b042017-03-31 13:56:23 -040099 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700100
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400101 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700102 };
103
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500104 SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400105 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500106 : INHERITED(program, errors, out)
107 , fContext(*context)
ethannicholas8ac838d2016-11-22 08:39:36 -0800108 , fDefaultLayout(MemoryLayout::k140_Standard)
Ethan Nicholas81d15112018-07-13 12:48:50 -0400109 , fCapabilities(0)
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 , fIdCount(1)
111 , fBoolTrue(0)
112 , fBoolFalse(0)
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500113 , fSetupFragPosition(false)
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400114 , fCurrentBlock(0)
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400115 , fSynthetics(errors) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 this->setupIntrinsics();
117 }
118
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500119 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700120
121private:
122 enum IntrinsicKind {
123 kGLSL_STD_450_IntrinsicKind,
124 kSPIRV_IntrinsicKind,
125 kSpecial_IntrinsicKind
126 };
127
128 enum SpecialIntrinsic {
129 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500130 kClamp_SpecialIntrinsic,
131 kMax_SpecialIntrinsic,
132 kMin_SpecialIntrinsic,
133 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500134 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700135 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400136 kSaturate_SpecialIntrinsic,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400137 kSampledImage_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500138 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400139 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700140 };
141
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400142 enum class Precision {
143 kLow,
144 kHigh,
145 };
146
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 void setupIntrinsics();
148
149 SpvId nextId();
150
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 Type getActualType(const Type& type);
152
ethannicholasb3058bd2016-07-01 08:22:01 -0700153 SpvId getType(const Type& type);
154
ethannicholas8ac838d2016-11-22 08:39:36 -0800155 SpvId getType(const Type& type, const MemoryLayout& layout);
156
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400157 SpvId getImageType(const Type& type);
158
ethannicholasd598f792016-07-25 10:08:54 -0700159 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160
ethannicholasd598f792016-07-25 10:08:54 -0700161 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700162
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500163 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800164 SpvStorageClass_ storageClass);
165
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400166 void writePrecisionModifier(Precision precision, SpvId id);
167
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500168 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400169
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400170 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700171
172 void writeLayout(const Layout& layout, SpvId target);
173
174 void writeLayout(const Layout& layout, SpvId target, int member);
175
ethannicholas8ac838d2016-11-22 08:39:36 -0800176 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700177
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400178 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700179
Stephen White88574972020-06-23 19:09:29 -0400180 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400182 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400184 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700185
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400186 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400188 void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400190 void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700191
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400192 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700193
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400194 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400196 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700197
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400198 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
199
200 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
201
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500202
203 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
204 SpvId signedInst, SpvId unsignedInst,
205 const std::vector<SpvId>& args, OutputStream& out);
206
207 /**
208 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
209 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
210 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
211 * vec2, vec3).
212 */
213 std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
214 OutputStream& out);
215
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400216 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
ethannicholasf789b382016-08-03 12:43:36 -0700218 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400220 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700221
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400222 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500223
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400224 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
225
Ethan Nicholas84645e32017-02-09 13:57:14 -0500226 /**
227 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
228 * entries equal to zero.
229 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400230 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500231
232 /**
233 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
234 * source matrix are filled with zero; entries which do not exist in the destination matrix are
235 * ignored.
236 */
237 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400238 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500239
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400240 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400241 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
242 OutputStream& out);
243
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400244 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400246 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700247
Ethan Nicholasbd553222017-07-18 15:54:59 -0400248 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
249
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400250 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400254 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700255
Ethan Nicholasef653b82017-02-21 13:50:00 -0500256 /**
257 * Folds the potentially-vector result of a logical operation down to a single bool. If
258 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
259 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
260 * returns the original id value.
261 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400262 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500263
Ethan Nicholas68990be2017-07-13 09:36:52 -0400264 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400265 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
266 SpvOp_ mergeOperator, OutputStream& out);
267
268 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
269 SpvOp_ floatOperator, SpvOp_ intOperator,
270 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400271
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500272 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
273 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400274 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500276 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400277 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700278
Ethan Nicholas49465b42019-04-17 12:22:21 -0400279 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
280 const Type& rightType, SpvId rhs, const Type& resultType,
281 OutputStream& out);
282
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400283 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700284
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400285 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700286
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400289 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700290
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700294
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400295 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700296
ethannicholasf789b382016-08-03 12:43:36 -0700297 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700298
ethannicholasf789b382016-08-03 12:43:36 -0700299 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
ethannicholasf789b382016-08-03 12:43:36 -0700301 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700302
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400303 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700304
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400305 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700306
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400307 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700308
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700310
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400311 void writeWhileStatement(const WhileStatement& w, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500312
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400313 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500314
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500315 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
316
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400317 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400319 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700320
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400321 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700322
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400323 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700324
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400325 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700326
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700327 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700328
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400329 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700330
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400331 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700332
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700333 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400335 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700337 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700338
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700339 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400340 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700341
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400342 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700343
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500344 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400345 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700346
347 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400348 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700349
350 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400351 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700352
353 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400354 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700355
356 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400357 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700358
359 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500360 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400361 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700362
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400363 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
364
ethannicholasd598f792016-07-25 10:08:54 -0700365 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800366 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700367
ethannicholasb3058bd2016-07-01 08:22:01 -0700368 uint64_t fCapabilities;
369 SpvId fIdCount;
370 SpvId fGLSLExtendedInstructions;
371 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400372 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700373 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
374 std::unordered_map<const Variable*, SpvId> fVariableMap;
375 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400376 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400377 std::unordered_map<String, SpvId> fTypeMap;
378 StringStream fCapabilitiesBuffer;
379 StringStream fGlobalInitializersBuffer;
380 StringStream fConstantBuffer;
381 StringStream fExtraGlobalsBuffer;
382 StringStream fExternalFunctionsBuffer;
383 StringStream fVariableBuffer;
384 StringStream fNameBuffer;
385 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700386
387 SpvId fBoolTrue;
388 SpvId fBoolFalse;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400389 std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500390 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 // label of the current block, or 0 if we are not in a block
392 SpvId fCurrentBlock;
393 std::stack<SpvId> fBreakTarget;
394 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000395 SpvId fRTHeightStructId = (SpvId) -1;
396 SpvId fRTHeightFieldIndex = (SpvId) -1;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400397 // holds variables synthesized during output, for lifetime purposes
398 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400399 int fSkInCount = 1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700400
401 friend class PointerLValue;
402 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500403
John Stiles7571f9e2020-09-02 22:42:33 -0400404 using INHERITED = CodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700405};
406
John Stilesa6841be2020-08-06 14:11:56 -0400407} // namespace SkSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700408
409#endif