blob: 97266dbbd5952e3d311bf52bbf1b6e003c98a5e2 [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"
17#include "src/sksl/ir/SkSLBinaryExpression.h"
18#include "src/sksl/ir/SkSLBoolLiteral.h"
19#include "src/sksl/ir/SkSLConstructor.h"
20#include "src/sksl/ir/SkSLDoStatement.h"
21#include "src/sksl/ir/SkSLFieldAccess.h"
22#include "src/sksl/ir/SkSLFloatLiteral.h"
23#include "src/sksl/ir/SkSLForStatement.h"
24#include "src/sksl/ir/SkSLFunctionCall.h"
25#include "src/sksl/ir/SkSLFunctionDeclaration.h"
26#include "src/sksl/ir/SkSLFunctionDefinition.h"
27#include "src/sksl/ir/SkSLIfStatement.h"
28#include "src/sksl/ir/SkSLIndexExpression.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLInterfaceBlock.h"
31#include "src/sksl/ir/SkSLPostfixExpression.h"
32#include "src/sksl/ir/SkSLPrefixExpression.h"
33#include "src/sksl/ir/SkSLProgramElement.h"
34#include "src/sksl/ir/SkSLReturnStatement.h"
35#include "src/sksl/ir/SkSLStatement.h"
36#include "src/sksl/ir/SkSLSwitchStatement.h"
37#include "src/sksl/ir/SkSLSwizzle.h"
38#include "src/sksl/ir/SkSLTernaryExpression.h"
39#include "src/sksl/ir/SkSLVarDeclarations.h"
40#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
41#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)
47 : fInt(i) {}
48
49 ConstantValue(double d)
50 : fDouble(d) {}
51
52 bool operator==(const ConstantValue& other) const {
53 return fInt == other.fInt;
54 }
55
56 int64_t fInt;
57 double fDouble;
58};
59
60enum class ConstantType {
61 kInt,
62 kUInt,
63 kShort,
64 kUShort,
65 kFloat,
66 kDouble,
67 kHalf,
68};
69
70namespace std {
71
72template <>
73struct hash<std::pair<ConstantValue, ConstantType>> {
74 size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
75 return key.first.fInt ^ (int) key.second;
76 }
77};
78
79}
80
ethannicholasb3058bd2016-07-01 08:22:01 -070081namespace SkSL {
82
83#define kLast_Capability SpvCapabilityMultiViewport
84
85/**
86 * Converts a Program into a SPIR-V binary.
87 */
88class SPIRVCodeGenerator : public CodeGenerator {
89public:
90 class LValue {
91 public:
92 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050093
ethannicholasb3058bd2016-07-01 08:22:01 -070094 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
95 // by a pointer (e.g. vector swizzles), returns 0.
96 virtual SpvId getPointer() = 0;
97
Ethan Nicholas0df1b042017-03-31 13:56:23 -040098 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -070099
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400100 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700101 };
102
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500103 SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400104 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500105 : INHERITED(program, errors, out)
106 , fContext(*context)
ethannicholas8ac838d2016-11-22 08:39:36 -0800107 , fDefaultLayout(MemoryLayout::k140_Standard)
Ethan Nicholas81d15112018-07-13 12:48:50 -0400108 , fCapabilities(0)
ethannicholasb3058bd2016-07-01 08:22:01 -0700109 , fIdCount(1)
110 , fBoolTrue(0)
111 , fBoolFalse(0)
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500112 , fSetupFragPosition(false)
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400113 , fCurrentBlock(0)
114 , fSynthetics(nullptr, errors) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700115 this->setupIntrinsics();
116 }
117
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500118 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -0700119
120private:
121 enum IntrinsicKind {
122 kGLSL_STD_450_IntrinsicKind,
123 kSPIRV_IntrinsicKind,
124 kSpecial_IntrinsicKind
125 };
126
127 enum SpecialIntrinsic {
128 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500129 kClamp_SpecialIntrinsic,
130 kMax_SpecialIntrinsic,
131 kMin_SpecialIntrinsic,
132 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500133 kMod_SpecialIntrinsic,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700134 kDFdy_SpecialIntrinsic,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400135 kSaturate_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -0500136 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400137 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700138 };
139
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400140 enum class Precision {
141 kLow,
142 kHigh,
143 };
144
ethannicholasb3058bd2016-07-01 08:22:01 -0700145 void setupIntrinsics();
146
147 SpvId nextId();
148
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 Type getActualType(const Type& type);
150
ethannicholasb3058bd2016-07-01 08:22:01 -0700151 SpvId getType(const Type& type);
152
ethannicholas8ac838d2016-11-22 08:39:36 -0800153 SpvId getType(const Type& type, const MemoryLayout& layout);
154
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400155 SpvId getImageType(const Type& type);
156
ethannicholasd598f792016-07-25 10:08:54 -0700157 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158
ethannicholasd598f792016-07-25 10:08:54 -0700159 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500161 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800162 SpvStorageClass_ storageClass);
163
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400164 void writePrecisionModifier(Precision precision, SpvId id);
165
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500166 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400167
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400168 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700169
170 void writeLayout(const Layout& layout, SpvId target);
171
172 void writeLayout(const Layout& layout, SpvId target, int member);
173
ethannicholas8ac838d2016-11-22 08:39:36 -0800174 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700175
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400176 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700177
ethannicholasf789b382016-08-03 12:43:36 -0700178 SpvId writeInterfaceBlock(const InterfaceBlock& intf);
ethannicholasb3058bd2016-07-01 08:22:01 -0700179
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400180 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400182 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400184 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700185
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400186 void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400188 void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400190 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700191
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400192 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700193
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400194 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400196 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
197
198 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
199
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500200
201 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
202 SpvId signedInst, SpvId unsignedInst,
203 const std::vector<SpvId>& args, OutputStream& out);
204
205 /**
206 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
207 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
208 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
209 * vec2, vec3).
210 */
211 std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
212 OutputStream& out);
213
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400214 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700215
ethannicholasf789b382016-08-03 12:43:36 -0700216 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700217
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400218 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400220 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500221
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400222 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
223
Ethan Nicholas84645e32017-02-09 13:57:14 -0500224 /**
225 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
226 * entries equal to zero.
227 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500229
230 /**
231 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
232 * source matrix are filled with zero; entries which do not exist in the destination matrix are
233 * ignored.
234 */
235 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500237
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400238 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400239 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
240 OutputStream& out);
241
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400242 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700243
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400244 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245
Ethan Nicholasbd553222017-07-18 15:54:59 -0400246 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
247
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400248 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700249
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400250 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253
Ethan Nicholasef653b82017-02-21 13:50:00 -0500254 /**
255 * Folds the potentially-vector result of a logical operation down to a single bool. If
256 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
257 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
258 * returns the original id value.
259 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400260 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500261
Ethan Nicholas68990be2017-07-13 09:36:52 -0400262 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400263 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
264 SpvOp_ mergeOperator, OutputStream& out);
265
266 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
267 SpvOp_ floatOperator, SpvOp_ intOperator,
268 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400269
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500270 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
271 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400272 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700273
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500274 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400275 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700276
Ethan Nicholas49465b42019-04-17 12:22:21 -0400277 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
278 const Type& rightType, SpvId rhs, const Type& resultType,
279 OutputStream& out);
280
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400281 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700282
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400283 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700284
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400285 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700286
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400289 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700290
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700294
ethannicholasf789b382016-08-03 12:43:36 -0700295 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700296
ethannicholasf789b382016-08-03 12:43:36 -0700297 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700298
ethannicholasf789b382016-08-03 12:43:36 -0700299 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400301 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700302
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400303 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700304
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400305 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700306
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400307 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700308
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 void writeWhileStatement(const WhileStatement& w, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500310
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400311 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500312
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500313 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
314
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700316
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400317 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700318
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400319 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700320
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400321 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700322
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400323 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700324
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700325 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700326
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400327 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700328
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400329 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700330
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700331 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700332
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400333 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700334
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700335 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700336
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700337 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400338 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700339
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400340 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700341
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500342 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400343 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700344
345 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400346 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347
348 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400349 int32_t word5, 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 int32_t word5, int32_t word6, 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, int32_t word6, int32_t word7, 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 Nicholas941e7e22016-12-12 15:33:30 -0500358 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400359 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700360
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400361 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
362
ethannicholasd598f792016-07-25 10:08:54 -0700363 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800364 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700365
ethannicholasb3058bd2016-07-01 08:22:01 -0700366 uint64_t fCapabilities;
367 SpvId fIdCount;
368 SpvId fGLSLExtendedInstructions;
369 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400370 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700371 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
372 std::unordered_map<const Variable*, SpvId> fVariableMap;
373 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400374 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400375 std::unordered_map<String, SpvId> fTypeMap;
376 StringStream fCapabilitiesBuffer;
377 StringStream fGlobalInitializersBuffer;
378 StringStream fConstantBuffer;
379 StringStream fExtraGlobalsBuffer;
380 StringStream fExternalFunctionsBuffer;
381 StringStream fVariableBuffer;
382 StringStream fNameBuffer;
383 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700384
385 SpvId fBoolTrue;
386 SpvId fBoolFalse;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400387 std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
Ethan Nicholasac285b12019-02-12 16:05:18 -0500388 // The constant float2(0, 1), used in swizzling
389 SpvId fConstantZeroOneVector = 0;
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
404 typedef CodeGenerator INHERITED;
ethannicholasb3058bd2016-07-01 08:22:01 -0700405};
406
407}
408
409#endif