blob: 16f5beb68f4a964214443f3bacbb0d200eb3c4e0 [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
15#include "SkSLCodeGenerator.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080016#include "SkSLMemoryLayout.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070017#include "ir/SkSLBinaryExpression.h"
18#include "ir/SkSLBoolLiteral.h"
19#include "ir/SkSLConstructor.h"
Ethan Nicholasfd146aa2017-01-13 16:40:35 -050020#include "ir/SkSLDoStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070021#include "ir/SkSLFloatLiteral.h"
22#include "ir/SkSLIfStatement.h"
23#include "ir/SkSLIndexExpression.h"
24#include "ir/SkSLInterfaceBlock.h"
25#include "ir/SkSLIntLiteral.h"
26#include "ir/SkSLFieldAccess.h"
27#include "ir/SkSLForStatement.h"
28#include "ir/SkSLFunctionCall.h"
29#include "ir/SkSLFunctionDeclaration.h"
30#include "ir/SkSLFunctionDefinition.h"
31#include "ir/SkSLPrefixExpression.h"
32#include "ir/SkSLPostfixExpression.h"
33#include "ir/SkSLProgramElement.h"
34#include "ir/SkSLReturnStatement.h"
35#include "ir/SkSLStatement.h"
Ethan Nicholase92b1b12017-11-13 16:13:21 -050036#include "ir/SkSLSwitchStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070037#include "ir/SkSLSwizzle.h"
38#include "ir/SkSLTernaryExpression.h"
ethannicholas22f939e2016-10-13 13:25:34 -070039#include "ir/SkSLVarDeclarations.h"
40#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070041#include "ir/SkSLVariableReference.h"
Ethan Nicholasfd146aa2017-01-13 16:40:35 -050042#include "ir/SkSLWhileStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070043#include "spirv.h"
44
45namespace SkSL {
46
47#define kLast_Capability SpvCapabilityMultiViewport
48
49/**
50 * Converts a Program into a SPIR-V binary.
51 */
52class SPIRVCodeGenerator : public CodeGenerator {
53public:
54 class LValue {
55 public:
56 virtual ~LValue() {}
Greg Daniel64773e62016-11-22 09:44:03 -050057
ethannicholasb3058bd2016-07-01 08:22:01 -070058 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
59 // by a pointer (e.g. vector swizzles), returns 0.
60 virtual SpvId getPointer() = 0;
61
Ethan Nicholas0df1b042017-03-31 13:56:23 -040062 virtual SpvId load(OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -070063
Ethan Nicholas0df1b042017-03-31 13:56:23 -040064 virtual void store(SpvId value, OutputStream& out) = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -070065 };
66
Ethan Nicholas941e7e22016-12-12 15:33:30 -050067 SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040068 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050069 : INHERITED(program, errors, out)
70 , fContext(*context)
ethannicholas8ac838d2016-11-22 08:39:36 -080071 , fDefaultLayout(MemoryLayout::k140_Standard)
ethannicholasd598f792016-07-25 10:08:54 -070072 , fCapabilities(1 << SpvCapabilityShader)
ethannicholasb3058bd2016-07-01 08:22:01 -070073 , fIdCount(1)
74 , fBoolTrue(0)
75 , fBoolFalse(0)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050076 , fSetupFragPosition(false)
Ethan Nicholas8feeff92017-03-30 14:11:58 -040077 , fCurrentBlock(0)
78 , fSynthetics(nullptr, errors) {
ethannicholasb3058bd2016-07-01 08:22:01 -070079 this->setupIntrinsics();
80 }
81
Ethan Nicholas941e7e22016-12-12 15:33:30 -050082 bool generateCode() override;
ethannicholasb3058bd2016-07-01 08:22:01 -070083
84private:
85 enum IntrinsicKind {
86 kGLSL_STD_450_IntrinsicKind,
87 kSPIRV_IntrinsicKind,
88 kSpecial_IntrinsicKind
89 };
90
91 enum SpecialIntrinsic {
92 kAtan_SpecialIntrinsic,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -050093 kClamp_SpecialIntrinsic,
94 kMax_SpecialIntrinsic,
95 kMin_SpecialIntrinsic,
96 kMix_SpecialIntrinsic,
Ethan Nicholas70a44b22017-11-30 09:09:16 -050097 kMod_SpecialIntrinsic,
Greg Daniel64773e62016-11-22 09:44:03 -050098 kSubpassLoad_SpecialIntrinsic,
Ethan Nicholas0187ae62017-05-03 11:03:44 -040099 kTexture_SpecialIntrinsic,
ethannicholasb3058bd2016-07-01 08:22:01 -0700100 };
101
102 void setupIntrinsics();
103
104 SpvId nextId();
105
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400106 Type getActualType(const Type& type);
107
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 SpvId getType(const Type& type);
109
ethannicholas8ac838d2016-11-22 08:39:36 -0800110 SpvId getType(const Type& type, const MemoryLayout& layout);
111
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400112 SpvId getImageType(const Type& type);
113
ethannicholasd598f792016-07-25 10:08:54 -0700114 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -0700115
ethannicholasd598f792016-07-25 10:08:54 -0700116 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700117
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500118 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
ethannicholas8ac838d2016-11-22 08:39:36 -0800119 SpvStorageClass_ storageClass);
120
Ethan Nicholasa51d7132017-06-09 10:47:31 -0400121 void writePrecisionModifier(const Modifiers& modifiers, SpvId id);
122
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400123 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700124
125 void writeLayout(const Layout& layout, SpvId target);
126
127 void writeLayout(const Layout& layout, SpvId target, int member);
128
ethannicholas8ac838d2016-11-22 08:39:36 -0800129 void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
ethannicholasb3058bd2016-07-01 08:22:01 -0700130
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400131 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132
ethannicholasf789b382016-08-03 12:43:36 -0700133 SpvId writeInterfaceBlock(const InterfaceBlock& intf);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400135 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400137 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700138
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400139 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700140
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400141 void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700142
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400143 void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700144
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400145 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700146
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400147 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700148
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400149 SpvId writeExpression(const Expression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400151 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
152
153 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
154
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500155
156 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
157 SpvId signedInst, SpvId unsignedInst,
158 const std::vector<SpvId>& args, OutputStream& out);
159
160 /**
161 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
162 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
163 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
164 * vec2, vec3).
165 */
166 std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
167 OutputStream& out);
168
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400169 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700170
ethannicholasf789b382016-08-03 12:43:36 -0700171 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700172
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400173 SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700174
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400175 SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500176
Ethan Nicholas925f52d2017-07-19 10:42:50 -0400177 SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
178
Ethan Nicholas84645e32017-02-09 13:57:14 -0500179 /**
180 * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
181 * entries equal to zero.
182 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400183 void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500184
185 /**
186 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
187 * source matrix are filled with zero; entries which do not exist in the destination matrix are
188 * ignored.
189 */
190 void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400191 OutputStream& out);
Ethan Nicholas84645e32017-02-09 13:57:14 -0500192
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400193 SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400195 SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700196
Ethan Nicholasbd553222017-07-18 15:54:59 -0400197 SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
198
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400199 SpvId writeConstructor(const Constructor& c, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400201 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700202
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400203 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204
Ethan Nicholasef653b82017-02-21 13:50:00 -0500205 /**
206 * Folds the potentially-vector result of a logical operation down to a single bool. If
207 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
208 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
209 * returns the original id value.
210 */
Ethan Nicholas48e24052018-03-14 13:51:39 -0400211 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
Ethan Nicholasef653b82017-02-21 13:50:00 -0500212
Ethan Nicholas68990be2017-07-13 09:36:52 -0400213 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -0400214 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
215 SpvOp_ mergeOperator, OutputStream& out);
216
217 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
218 SpvOp_ floatOperator, SpvOp_ intOperator,
219 OutputStream& out);
Ethan Nicholas68990be2017-07-13 09:36:52 -0400220
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500221 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
222 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400223 SpvOp_ ifBool, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700224
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500225 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226 SpvOp_ ifUInt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400228 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400230 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700231
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400232 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700233
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400234 SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700235
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236 SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700237
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400238 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700239
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400240 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700241
ethannicholasf789b382016-08-03 12:43:36 -0700242 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700243
ethannicholasf789b382016-08-03 12:43:36 -0700244 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245
ethannicholasf789b382016-08-03 12:43:36 -0700246 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700247
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400248 void writeStatement(const Statement& s, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700249
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400250 void writeBlock(const Block& b, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700251
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400254 void writeForStatement(const ForStatement& f, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700255
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400256 void writeWhileStatement(const WhileStatement& w, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500257
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400258 void writeDoStatement(const DoStatement& d, OutputStream& out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -0500259
Ethan Nicholase92b1b12017-11-13 16:13:21 -0500260 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
261
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400262 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700263
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400264 void writeCapabilities(OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700265
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400266 void writeInstructions(const Program& program, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700267
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400268 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700269
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400270 void writeWord(int32_t word, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700272 void writeString(const char* string, size_t length, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700273
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400274 void writeLabel(SpvId id, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400276 void writeInstruction(SpvOp_ opCode, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700277
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700278 void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400280 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700281
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700282 void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700283
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700284 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400285 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700286
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500289 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400290 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700291
292 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700294
295 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400296 int32_t word5, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700297
298 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 int32_t word5, int32_t word6, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700300
301 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400302 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700303
304 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500305 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400306 OutputStream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700307
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400308 void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
309
ethannicholasd598f792016-07-25 10:08:54 -0700310 const Context& fContext;
ethannicholas8ac838d2016-11-22 08:39:36 -0800311 const MemoryLayout fDefaultLayout;
ethannicholasd598f792016-07-25 10:08:54 -0700312
ethannicholasb3058bd2016-07-01 08:22:01 -0700313 uint64_t fCapabilities;
314 SpvId fIdCount;
315 SpvId fGLSLExtendedInstructions;
316 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400317 std::unordered_map<String, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700318 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
319 std::unordered_map<const Variable*, SpvId> fVariableMap;
320 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400321 std::unordered_map<String, SpvId> fImageTypeMap;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400322 std::unordered_map<String, SpvId> fTypeMap;
323 StringStream fCapabilitiesBuffer;
324 StringStream fGlobalInitializersBuffer;
325 StringStream fConstantBuffer;
326 StringStream fExtraGlobalsBuffer;
327 StringStream fExternalFunctionsBuffer;
328 StringStream fVariableBuffer;
329 StringStream fNameBuffer;
330 StringStream fDecorationBuffer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700331
332 SpvId fBoolTrue;
333 SpvId fBoolFalse;
334 std::unordered_map<int64_t, SpvId> fIntConstants;
335 std::unordered_map<uint64_t, SpvId> fUIntConstants;
336 std::unordered_map<float, SpvId> fFloatConstants;
337 std::unordered_map<double, SpvId> fDoubleConstants;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500338 bool fSetupFragPosition;
ethannicholasb3058bd2016-07-01 08:22:01 -0700339 // label of the current block, or 0 if we are not in a block
340 SpvId fCurrentBlock;
341 std::stack<SpvId> fBreakTarget;
342 std::stack<SpvId> fContinueTarget;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500343 SpvId fRTHeightStructId = (SpvId) -1;
344 SpvId fRTHeightFieldIndex = (SpvId) -1;
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400345 // holds variables synthesized during output, for lifetime purposes
346 SymbolTable fSynthetics;
Ethan Nicholas5226b772018-05-03 16:20:41 -0400347 int fSkInCount = 1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700348
349 friend class PointerLValue;
350 friend class SwizzleLValue;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500351
352 typedef CodeGenerator INHERITED;
ethannicholasb3058bd2016-07-01 08:22:01 -0700353};
354
355}
356
357#endif