blob: aff2e7fc43cf0f4e12e2df5ab64e775181e5e15b [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
50 ConstantValue(double d)
51 : fDouble(d) {}
52
53 bool operator==(const ConstantValue& other) const {
54 return fInt == other.fInt;
55 }
56
57 int64_t fInt;
58 double fDouble;
59};
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
80}
81
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)
115 , fSynthetics(nullptr, 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,
Greg Daniel64773e62016-11-22 09:44:03 -0500137 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400138 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700139 };
140
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400141 enum class Precision {
142 kLow,
143 kHigh,
144 };
145
ethannicholasb3058bd2016-07-01 08:22:01 -0700146 void setupIntrinsics();
147
148 SpvId nextId();
149
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400150 Type getActualType(const Type& type);
151
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 SpvId getType(const Type& type);
153
ethannicholas8ac838d2016-11-22 08:39:36 -0800154 SpvId getType(const Type& type, const MemoryLayout& layout);
155
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400156 SpvId getImageType(const Type& type);
157
ethannicholasd598f792016-07-25 10:08:54 -0700158 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700159
ethannicholasd598f792016-07-25 10:08:54 -0700160 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700161
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500162 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800163 SpvStorageClass_ storageClass);
164
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400165 void writePrecisionModifier(Precision precision, SpvId id);
166
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500167 void writePrecisionModifier(const Type& type, SpvId id);
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400168
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400169 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700170
171 void writeLayout(const Layout& layout, SpvId target);
172
173 void writeLayout(const Layout& layout, SpvId target, int member);
174
ethannicholas8ac838d2016-11-22 08:39:36 -0800175 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400177 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700178
ethannicholasf789b382016-08-03 12:43:36 -0700179 SpvId writeInterfaceBlock(const InterfaceBlock& intf);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400181 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400183 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400185 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400187 void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400189 void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400191 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400193 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400195 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400197 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
198
199 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
200
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500201
202 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
203 SpvId signedInst, SpvId unsignedInst,
204 const std::vector<SpvId>& args, OutputStream& out);
205
206 /**
207 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
208 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
209 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
210 * vec2, vec3).
211 */
212 std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
213 OutputStream& out);
214
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400215 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700216
ethannicholasf789b382016-08-03 12:43:36 -0700217 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700218
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400219 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700220
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400221 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500222
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400223 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
224
Ethan Nicholas84645e32017-02-09 13:57:14 -0500225 /**
226 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
227 * entries equal to zero.
228 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400229 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500230
231 /**
232 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
233 * source matrix are filled with zero; entries which do not exist in the destination matrix are
234 * ignored.
235 */
236 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400237 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500238
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400239 void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -0400240 std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
241 OutputStream& out);
242
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400243 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700244
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400245 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246
Ethan Nicholasbd553222017-07-18 15:54:59 -0400247 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
248
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400249 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700250
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400251 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700252
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400253 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700254
Ethan Nicholasef653b82017-02-21 13:50:00 -0500255 /**
256 * Folds the potentially-vector result of a logical operation down to a single bool. If
257 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
258 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
259 * returns the original id value.
260 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400261 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500262
Ethan Nicholas68990be2017-07-13 09:36:52 -0400263 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400264 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
265 SpvOp_ mergeOperator, OutputStream& out);
266
267 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
268 SpvOp_ floatOperator, SpvOp_ intOperator,
269 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400270
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500271 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
272 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400273 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700274
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500275 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400276 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700277
Ethan Nicholas49465b42019-04-17 12:22:21 -0400278 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
279 const Type& rightType, SpvId rhs, const Type& resultType,
280 OutputStream& out);
281
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400282 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700283
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400284 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700285
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400286 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700287
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400288 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700289
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400290 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700291
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400292 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700293
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400294 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700295
ethannicholasf789b382016-08-03 12:43:36 -0700296 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700297
ethannicholasf789b382016-08-03 12:43:36 -0700298 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700299
ethannicholasf789b382016-08-03 12:43:36 -0700300 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700301
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400302 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700303
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400304 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700305
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400306 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700307
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400308 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700309
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400310 void writeWhileStatement(const WhileStatement& w, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500311
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400312 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500313
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500314 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
315
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400316 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700317
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400318 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700319
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400320 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700321
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400322 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700323
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400324 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700325
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700326 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700327
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400328 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700329
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400330 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700331
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700332 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700333
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400334 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700335
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700336 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700337
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700338 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400339 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700340
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400341 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700342
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500343 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400344 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700345
346 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400347 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700348
349 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400350 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700351
352 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400353 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700354
355 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400356 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700357
358 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500359 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400360 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700361
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400362 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
363
ethannicholasd598f792016-07-25 10:08:54 -0700364 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800365 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700366
ethannicholasb3058bd2016-07-01 08:22:01 -0700367 uint64_t fCapabilities;
368 SpvId fIdCount;
369 SpvId fGLSLExtendedInstructions;
370 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400371 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700372 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
373 std::unordered_map<const Variable*, SpvId> fVariableMap;
374 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400375 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400376 std::unordered_map<String, SpvId> fTypeMap;
377 StringStream fCapabilitiesBuffer;
378 StringStream fGlobalInitializersBuffer;
379 StringStream fConstantBuffer;
380 StringStream fExtraGlobalsBuffer;
381 StringStream fExternalFunctionsBuffer;
382 StringStream fVariableBuffer;
383 StringStream fNameBuffer;
384 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700385
386 SpvId fBoolTrue;
387 SpvId fBoolFalse;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400388 std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
Ethan Nicholasac285b12019-02-12 16:05:18 -0500389 // The constant float2(0, 1), used in swizzling
390 SpvId fConstantZeroOneVector = 0;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500391 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700392 // label of the current block, or 0 if we are not in a block
393 SpvId fCurrentBlock;
394 std::stack<SpvId> fBreakTarget;
395 std::stack<SpvId> fContinueTarget;
Greg Daniele6ab9982018-08-22 13:56:32 +0000396 SpvId fRTHeightStructId = (SpvId) -1;
397 SpvId fRTHeightFieldIndex = (SpvId) -1;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400398 // holds variables synthesized during output, for lifetime purposes
399 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400400 int fSkInCount = 1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700401
402 friend class PointerLValue;
403 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500404
405 typedef CodeGenerator INHERITED;
ethannicholasb3058bd2016-07-01 08:22:01 -0700406};
407
408}
409
410#endif