blob: caae20114d31f3e264339bfafa2b6ee78bfccafd [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 */
7
8#ifndef SKSL_SPIRVCODEGENERATOR
9#define SKSL_SPIRVCODEGENERATOR
10
11#include <sstream>
12#include <stack>
13#include <tuple>
14#include <unordered_map>
15
16#include "SkSLCodeGenerator.h"
17#include "ir/SkSLBinaryExpression.h"
18#include "ir/SkSLBoolLiteral.h"
19#include "ir/SkSLConstructor.h"
20#include "ir/SkSLFloatLiteral.h"
21#include "ir/SkSLIfStatement.h"
22#include "ir/SkSLIndexExpression.h"
23#include "ir/SkSLInterfaceBlock.h"
24#include "ir/SkSLIntLiteral.h"
25#include "ir/SkSLFieldAccess.h"
26#include "ir/SkSLForStatement.h"
27#include "ir/SkSLFunctionCall.h"
28#include "ir/SkSLFunctionDeclaration.h"
29#include "ir/SkSLFunctionDefinition.h"
30#include "ir/SkSLPrefixExpression.h"
31#include "ir/SkSLPostfixExpression.h"
32#include "ir/SkSLProgramElement.h"
33#include "ir/SkSLReturnStatement.h"
34#include "ir/SkSLStatement.h"
35#include "ir/SkSLSwizzle.h"
36#include "ir/SkSLTernaryExpression.h"
37#include "ir/SkSLVarDeclaration.h"
38#include "ir/SkSLVarDeclarationStatement.h"
39#include "ir/SkSLVariableReference.h"
40#include "spirv.h"
41
42namespace SkSL {
43
44#define kLast_Capability SpvCapabilityMultiViewport
45
46/**
47 * Converts a Program into a SPIR-V binary.
48 */
49class SPIRVCodeGenerator : public CodeGenerator {
50public:
51 class LValue {
52 public:
53 virtual ~LValue() {}
54
55 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
56 // by a pointer (e.g. vector swizzles), returns 0.
57 virtual SpvId getPointer() = 0;
58
59 virtual SpvId load(std::ostream& out) = 0;
60
61 virtual void store(SpvId value, std::ostream& out) = 0;
62 };
63
ethannicholasd598f792016-07-25 10:08:54 -070064 SPIRVCodeGenerator(const Context* context)
65 : fContext(*context)
66 , fCapabilities(1 << SpvCapabilityShader)
ethannicholasb3058bd2016-07-01 08:22:01 -070067 , fIdCount(1)
68 , fBoolTrue(0)
69 , fBoolFalse(0)
70 , fCurrentBlock(0) {
71 this->setupIntrinsics();
72 }
73
ethannicholasf789b382016-08-03 12:43:36 -070074 void generateCode(const Program& program, std::ostream& out) override;
ethannicholasb3058bd2016-07-01 08:22:01 -070075
76private:
77 enum IntrinsicKind {
78 kGLSL_STD_450_IntrinsicKind,
79 kSPIRV_IntrinsicKind,
80 kSpecial_IntrinsicKind
81 };
82
83 enum SpecialIntrinsic {
84 kAtan_SpecialIntrinsic,
85 kTexture_SpecialIntrinsic,
86 kTexture2D_SpecialIntrinsic,
87 kTextureProj_SpecialIntrinsic
88 };
89
90 void setupIntrinsics();
91
92 SpvId nextId();
93
94 SpvId getType(const Type& type);
95
ethannicholasd598f792016-07-25 10:08:54 -070096 SpvId getFunctionType(const FunctionDeclaration& function);
ethannicholasb3058bd2016-07-01 08:22:01 -070097
ethannicholasd598f792016-07-25 10:08:54 -070098 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -070099
ethannicholasf789b382016-08-03 12:43:36 -0700100 std::vector<SpvId> getAccessChain(const Expression& expr, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700101
102 void writeLayout(const Layout& layout, SpvId target);
103
104 void writeLayout(const Layout& layout, SpvId target, int member);
105
106 void writeStruct(const Type& type, SpvId resultId);
107
ethannicholasf789b382016-08-03 12:43:36 -0700108 void writeProgramElement(const ProgramElement& pe, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700109
ethannicholasf789b382016-08-03 12:43:36 -0700110 SpvId writeInterfaceBlock(const InterfaceBlock& intf);
ethannicholasb3058bd2016-07-01 08:22:01 -0700111
ethannicholasd598f792016-07-25 10:08:54 -0700112 SpvId writeFunctionStart(const FunctionDeclaration& f, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700113
ethannicholasd598f792016-07-25 10:08:54 -0700114 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700115
ethannicholasd598f792016-07-25 10:08:54 -0700116 SpvId writeFunction(const FunctionDefinition& f, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700117
ethannicholasf789b382016-08-03 12:43:36 -0700118 void writeGlobalVars(const VarDeclaration& v, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700119
ethannicholasf789b382016-08-03 12:43:36 -0700120 void writeVarDeclaration(const VarDeclaration& decl, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700121
ethannicholasf789b382016-08-03 12:43:36 -0700122 SpvId writeVariableReference(const VariableReference& ref, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700123
ethannicholasf789b382016-08-03 12:43:36 -0700124 std::unique_ptr<LValue> getLValue(const Expression& value, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700125
ethannicholasf789b382016-08-03 12:43:36 -0700126 SpvId writeExpression(const Expression& expr, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700127
ethannicholasf789b382016-08-03 12:43:36 -0700128 SpvId writeIntrinsicCall(const FunctionCall& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700129
ethannicholasf789b382016-08-03 12:43:36 -0700130 SpvId writeFunctionCall(const FunctionCall& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700131
ethannicholasf789b382016-08-03 12:43:36 -0700132 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700133
ethannicholasf789b382016-08-03 12:43:36 -0700134 SpvId writeConstantVector(const Constructor& c);
ethannicholasb3058bd2016-07-01 08:22:01 -0700135
ethannicholasf789b382016-08-03 12:43:36 -0700136 SpvId writeFloatConstructor(const Constructor& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700137
ethannicholasf789b382016-08-03 12:43:36 -0700138 SpvId writeIntConstructor(const Constructor& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700139
ethannicholasf789b382016-08-03 12:43:36 -0700140 SpvId writeMatrixConstructor(const Constructor& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700141
ethannicholasf789b382016-08-03 12:43:36 -0700142 SpvId writeVectorConstructor(const Constructor& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700143
ethannicholasf789b382016-08-03 12:43:36 -0700144 SpvId writeConstructor(const Constructor& c, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700145
ethannicholasf789b382016-08-03 12:43:36 -0700146 SpvId writeFieldAccess(const FieldAccess& f, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700147
ethannicholasf789b382016-08-03 12:43:36 -0700148 SpvId writeSwizzle(const Swizzle& swizzle, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700149
150 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
151 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
152 SpvOp_ ifBool, std::ostream& out);
153
ethannicholasf789b382016-08-03 12:43:36 -0700154 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
155 SpvOp_ ifUInt, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700156
ethannicholasf789b382016-08-03 12:43:36 -0700157 SpvId writeBinaryExpression(const BinaryExpression& b, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158
ethannicholasf789b382016-08-03 12:43:36 -0700159 SpvId writeTernaryExpression(const TernaryExpression& t, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160
ethannicholasf789b382016-08-03 12:43:36 -0700161 SpvId writeIndexExpression(const IndexExpression& expr, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700162
ethannicholasf789b382016-08-03 12:43:36 -0700163 SpvId writeLogicalAnd(const BinaryExpression& b, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700164
ethannicholasf789b382016-08-03 12:43:36 -0700165 SpvId writeLogicalOr(const BinaryExpression& o, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166
ethannicholasf789b382016-08-03 12:43:36 -0700167 SpvId writePrefixExpression(const PrefixExpression& p, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700168
ethannicholasf789b382016-08-03 12:43:36 -0700169 SpvId writePostfixExpression(const PostfixExpression& p, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700170
ethannicholasf789b382016-08-03 12:43:36 -0700171 SpvId writeBoolLiteral(const BoolLiteral& b);
ethannicholasb3058bd2016-07-01 08:22:01 -0700172
ethannicholasf789b382016-08-03 12:43:36 -0700173 SpvId writeIntLiteral(const IntLiteral& i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700174
ethannicholasf789b382016-08-03 12:43:36 -0700175 SpvId writeFloatLiteral(const FloatLiteral& f);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176
ethannicholasf789b382016-08-03 12:43:36 -0700177 void writeStatement(const Statement& s, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700178
ethannicholasf789b382016-08-03 12:43:36 -0700179 void writeBlock(const Block& b, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180
ethannicholasf789b382016-08-03 12:43:36 -0700181 void writeIfStatement(const IfStatement& stmt, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182
ethannicholasf789b382016-08-03 12:43:36 -0700183 void writeForStatement(const ForStatement& f, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184
ethannicholasf789b382016-08-03 12:43:36 -0700185 void writeReturnStatement(const ReturnStatement& r, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186
187 void writeCapabilities(std::ostream& out);
188
ethannicholasf789b382016-08-03 12:43:36 -0700189 void writeInstructions(const Program& program, std::ostream& out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190
191 void writeOpCode(SpvOp_ opCode, int length, std::ostream& out);
192
193 void writeWord(int32_t word, std::ostream& out);
194
195 void writeString(const char* string, std::ostream& out);
196
197 void writeLabel(SpvId id, std::ostream& out);
198
199 void writeInstruction(SpvOp_ opCode, std::ostream& out);
200
201 void writeInstruction(SpvOp_ opCode, const char* string, std::ostream& out);
202
203 void writeInstruction(SpvOp_ opCode, int32_t word1, std::ostream& out);
204
205 void writeInstruction(SpvOp_ opCode, int32_t word1, const char* string, std::ostream& out);
206
207 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, const char* string,
208 std::ostream& out);
209
210 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, std::ostream& out);
211
212 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
213 std::ostream& out);
214
215 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
216 std::ostream& out);
217
218 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
219 int32_t word5, std::ostream& out);
220
221 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
222 int32_t word5, int32_t word6, std::ostream& out);
223
224 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
225 int32_t word5, int32_t word6, int32_t word7, std::ostream& out);
226
227 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
228 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
229 std::ostream& out);
230
ethannicholasd598f792016-07-25 10:08:54 -0700231 const Context& fContext;
232
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 uint64_t fCapabilities;
234 SpvId fIdCount;
235 SpvId fGLSLExtendedInstructions;
236 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
237 std::unordered_map<std::string, Intrinsic> fIntrinsicMap;
ethannicholasd598f792016-07-25 10:08:54 -0700238 std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
239 std::unordered_map<const Variable*, SpvId> fVariableMap;
240 std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
ethannicholasb3058bd2016-07-01 08:22:01 -0700241 std::unordered_map<std::string, SpvId> fTypeMap;
242 std::stringstream fCapabilitiesBuffer;
243 std::stringstream fGlobalInitializersBuffer;
244 std::stringstream fConstantBuffer;
245 std::stringstream fExternalFunctionsBuffer;
246 std::stringstream fVariableBuffer;
247 std::stringstream fNameBuffer;
248 std::stringstream fDecorationBuffer;
249
250 SpvId fBoolTrue;
251 SpvId fBoolFalse;
252 std::unordered_map<int64_t, SpvId> fIntConstants;
253 std::unordered_map<uint64_t, SpvId> fUIntConstants;
254 std::unordered_map<float, SpvId> fFloatConstants;
255 std::unordered_map<double, SpvId> fDoubleConstants;
256 // label of the current block, or 0 if we are not in a block
257 SpvId fCurrentBlock;
258 std::stack<SpvId> fBreakTarget;
259 std::stack<SpvId> fContinueTarget;
260
261 friend class PointerLValue;
262 friend class SwizzleLValue;
263};
264
265}
266
267#endif