blob: 3d77265509d1ffbf77e80760fbf3cf856f11ac75 [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"
42#include "src/sksl/ir/SkSLWhileStatement.h"
43#include "src/sksl/spirv.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070044
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040045union ConstantValue {
46 ConstantValue(int64_t i)
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040047 : fInt(i) {
48 SkASSERT(sizeof(*this) == sizeof(int64_t));
49 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040050
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040051 ConstantValue(SKSL_FLOAT f) {
52 memset(this, 0, sizeof(*this));
53 fFloat = f;
54 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040055
56 bool operator==(const ConstantValue& other) const {
57 return fInt == other.fInt;
58 }
59
60 int64_t fInt;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -040061 SKSL_FLOAT fFloat;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040062};
63
64enum class ConstantType {
65 kInt,
66 kUInt,
67 kShort,
68 kUShort,
69 kFloat,
70 kDouble,
71 kHalf,
72};
73
74namespace std {
75
76template <>
77struct hash<std::pair<ConstantValue, ConstantType>> {
78 size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
79 return key.first.fInt ^ (int) key.second;
80 }
81};
82
John Stilesa6841be2020-08-06 14:11:56 -040083} // namespace std
Ethan Nicholascc5d3e02019-04-19 09:50:56 -040084
ethannicholasb3058bd2016-07-01 08:22:01 -070085namespace SkSL {
86
87#define kLast_Capability SpvCapabilityMultiViewport
88
89/**
90 * Converts a Program into a SPIR-V binary.
91 */
92class SPIRVCodeGenerator : public CodeGenerator {
93public:
94 class LValue {
95 public:
96 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050097
ethannicholasb3058bd2016-07-01 08:22:01 -070098 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
99 // by a pointer (e.g. vector swizzles), returns 0.
100 virtual SpvId getPointer() = 0;
101
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400102 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700103
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400104 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 };
106
Brian Osman8b43dad2020-10-09 13:31:42 -0400107 SPIRVCodeGenerator(const Context* context,
108 const Program* program,
109 ErrorReporter* errors,
110 OutputStream* out)
111 : INHERITED(program, errors, out)
112 , fContext(*context)
113 , fDefaultLayout(MemoryLayout::k140_Standard)
114 , fCapabilities(0)
115 , fIdCount(1)
116 , fBoolTrue(0)
117 , fBoolFalse(0)
118 , fSetupFragPosition(false)
119 , fCurrentBlock(0)
John Stiles7c3515b2020-10-16 18:38:39 -0400120 , fSynthetics(errors, /*builtin=*/true) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700121 this->setupIntrinsics();
122 }
123
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500124 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700125
126private:
127 enum IntrinsicKind {
128 kGLSL_STD_450_IntrinsicKind,
129 kSPIRV_IntrinsicKind,
130 kSpecial_IntrinsicKind
131 };
132
133 enum SpecialIntrinsic {
134 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500135 kClamp_SpecialIntrinsic,
136 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,
Greg Daniel64773e62016-11-22 09:44:03 -0500143 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400144 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700145 };
146
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400147 enum class Precision {
148 kLow,
149 kHigh,
150 };
151
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 void setupIntrinsics();
153
154 SpvId nextId();
155
Ethan Nicholase2c49992020-10-05 11:49:11 -0400156 const Type& getActualType(const Type& type);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400157
ethannicholasb3058bd2016-07-01 08:22:01 -0700158 SpvId getType(const Type& type);
159
ethannicholas8ac838d2016-11-22 08:39:36 -0800160 SpvId getType(const Type& type, const MemoryLayout& layout);
161
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400162 SpvId getImageType(const Type& type);
163
ethannicholasd598f792016-07-25 10:08:54 -0700164 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700165
ethannicholasd598f792016-07-25 10:08:54 -0700166 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700167
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500168 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800169 SpvStorageClass_ storageClass);
170
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400171 void writePrecisionModifier(Precision precision, SpvId id);
172
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500173 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400174
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400175 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176
177 void writeLayout(const Layout& layout, SpvId target);
178
179 void writeLayout(const Layout& layout, SpvId target, int member);
180
ethannicholas8ac838d2016-11-22 08:39:36 -0800181 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400183 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184
Stephen White88574972020-06-23 19:09:29 -0400185 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight = true);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400187 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400189 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400191 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
Brian Osmanc0213602020-10-06 14:43:32 -0400193 void writeGlobalVar(Program::Kind kind, const VarDeclaration& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Brian Osmanc0213602020-10-06 14:43:32 -0400195 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400197 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400199 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400201 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400203 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
204
205 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
206
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500207
208 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
209 SpvId signedInst, SpvId unsignedInst,
210 const std::vector<SpvId>& args, OutputStream& out);
211
212 /**
213 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
214 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
215 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
216 * vec2, vec3).
217 */
John Stiles8e3b6be2020-10-13 11:14:08 -0400218 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500219
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400220 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700221
ethannicholasf789b382016-08-03 12:43:36 -0700222 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700223
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400224 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500227
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400228 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
229
Ethan Nicholas84645e32017-02-09 13:57:14 -0500230 /**
231 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
232 * entries equal to zero.
233 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400234 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500235
236 /**
237 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
238 * source matrix are filled with zero; entries which do not exist in the destination matrix are
239 * ignored.
240 */
241 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400242 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500243
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400244 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400245 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
246 OutputStream& out);
247
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400248 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700249
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400250 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholasbd553222017-07-18 15:54:59 -0400252 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
253
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400254 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700255
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400256 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700257
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400258 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700259
Ethan Nicholasef653b82017-02-21 13:50:00 -0500260 /**
261 * Folds the potentially-vector result of a logical operation down to a single bool. If
262 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
263 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
264 * returns the original id value.
265 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400266 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500267
Ethan Nicholas68990be2017-07-13 09:36:52 -0400268 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400269 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
270 SpvOp_ mergeOperator, OutputStream& out);
271
272 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
273 SpvOp_ floatOperator, SpvOp_ intOperator,
274 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400275
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500276 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
277 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400278 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500280 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400281 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700282
Ethan Nicholas49465b42019-04-17 12:22:21 -0400283 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
284 const Type& rightType, SpvId rhs, const Type& resultType,
285 OutputStream& out);
286
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400289 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700290
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700294
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400295 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700296
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400297 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700298
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
ethannicholasf789b382016-08-03 12:43:36 -0700301 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700302
ethannicholasf789b382016-08-03 12:43:36 -0700303 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700304
ethannicholasf789b382016-08-03 12:43:36 -0700305 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700306
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400307 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700308
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700310
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400311 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700312
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400313 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700314
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 void writeWhileStatement(const WhileStatement& w, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500316
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