aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLSPIRVCodeGenerator.h
blob: 84c582e53e932aab3b9208acf9bb6131c63592e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
 
#ifndef SKSL_SPIRVCODEGENERATOR
#define SKSL_SPIRVCODEGENERATOR

#include <stack>
#include <tuple>
#include <unordered_map>

#include "SkStream.h"
#include "SkSLCodeGenerator.h"
#include "ir/SkSLBinaryExpression.h"
#include "ir/SkSLBoolLiteral.h"
#include "ir/SkSLConstructor.h"
#include "ir/SkSLFloatLiteral.h"
#include "ir/SkSLIfStatement.h"
#include "ir/SkSLIndexExpression.h"
#include "ir/SkSLInterfaceBlock.h"
#include "ir/SkSLIntLiteral.h"
#include "ir/SkSLFieldAccess.h"
#include "ir/SkSLForStatement.h"
#include "ir/SkSLFunctionCall.h"
#include "ir/SkSLFunctionDeclaration.h"
#include "ir/SkSLFunctionDefinition.h"
#include "ir/SkSLPrefixExpression.h"
#include "ir/SkSLPostfixExpression.h"
#include "ir/SkSLProgramElement.h"
#include "ir/SkSLReturnStatement.h"
#include "ir/SkSLStatement.h"
#include "ir/SkSLSwizzle.h"
#include "ir/SkSLTernaryExpression.h"
#include "ir/SkSLVarDeclarations.h"
#include "ir/SkSLVarDeclarationsStatement.h"
#include "ir/SkSLVariableReference.h"
#include "spirv.h"

namespace SkSL {

#define kLast_Capability SpvCapabilityMultiViewport

/**
 * Converts a Program into a SPIR-V binary.
 */
class SPIRVCodeGenerator : public CodeGenerator {
public:
    class LValue {
    public:
        virtual ~LValue() {}
        
        // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
        // by a pointer (e.g. vector swizzles), returns 0.
        virtual SpvId getPointer() = 0;

        virtual SpvId load(SkWStream& out) = 0;

        virtual void store(SpvId value, SkWStream& out) = 0;
    };

    SPIRVCodeGenerator(const Context* context)
    : fContext(*context)
    , fCapabilities(1 << SpvCapabilityShader)
    , fIdCount(1)
    , fBoolTrue(0)
    , fBoolFalse(0)
    , fCurrentBlock(0) {
        this->setupIntrinsics();
    }

    void generateCode(const Program& program, SkWStream& out) override;

private:
    enum IntrinsicKind {
        kGLSL_STD_450_IntrinsicKind,
        kSPIRV_IntrinsicKind,
        kSpecial_IntrinsicKind
    };

    enum SpecialIntrinsic {
        kAtan_SpecialIntrinsic,
        kTexture_SpecialIntrinsic,
        kTexture2D_SpecialIntrinsic,
        kTextureProj_SpecialIntrinsic
    };

    void setupIntrinsics();

    SpvId nextId();

    SpvId getType(const Type& type);

    SpvId getFunctionType(const FunctionDeclaration& function);

    SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);

    std::vector<SpvId> getAccessChain(const Expression& expr, SkWStream& out);

    void writeLayout(const Layout& layout, SpvId target);

    void writeLayout(const Layout& layout, SpvId target, int member);

    void writeStruct(const Type& type, SpvId resultId);

    void writeProgramElement(const ProgramElement& pe, SkWStream& out);

    SpvId writeInterfaceBlock(const InterfaceBlock& intf);

    SpvId writeFunctionStart(const FunctionDeclaration& f, SkWStream& out);
    
    SpvId writeFunctionDeclaration(const FunctionDeclaration& f, SkWStream& out);

    SpvId writeFunction(const FunctionDefinition& f, SkWStream& out);

    void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, SkWStream& out);

    void writeVarDeclarations(const VarDeclarations& decl, SkWStream& out);

    SpvId writeVariableReference(const VariableReference& ref, SkWStream& out);

    std::unique_ptr<LValue> getLValue(const Expression& value, SkWStream& out);

    SpvId writeExpression(const Expression& expr, SkWStream& out);
    
    SpvId writeIntrinsicCall(const FunctionCall& c, SkWStream& out);

    SpvId writeFunctionCall(const FunctionCall& c, SkWStream& out);

    SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, SkWStream& out);

    SpvId writeConstantVector(const Constructor& c);

    SpvId writeFloatConstructor(const Constructor& c, SkWStream& out);

    SpvId writeIntConstructor(const Constructor& c, SkWStream& out);
    
    SpvId writeMatrixConstructor(const Constructor& c, SkWStream& out);

    SpvId writeVectorConstructor(const Constructor& c, SkWStream& out);

    SpvId writeConstructor(const Constructor& c, SkWStream& out);

    SpvId writeFieldAccess(const FieldAccess& f, SkWStream& out);

    SpvId writeSwizzle(const Swizzle& swizzle, SkWStream& out);

    SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs, 
                               SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt, 
                               SpvOp_ ifBool, SkWStream& out);

    SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt, 
                               SpvOp_ ifUInt, SkWStream& out);

    SpvId writeBinaryExpression(const BinaryExpression& b, SkWStream& out);

    SpvId writeTernaryExpression(const TernaryExpression& t, SkWStream& out);

    SpvId writeIndexExpression(const IndexExpression& expr, SkWStream& out);

    SpvId writeLogicalAnd(const BinaryExpression& b, SkWStream& out);

    SpvId writeLogicalOr(const BinaryExpression& o, SkWStream& out);

    SpvId writePrefixExpression(const PrefixExpression& p, SkWStream& out);

    SpvId writePostfixExpression(const PostfixExpression& p, SkWStream& out);

    SpvId writeBoolLiteral(const BoolLiteral& b);

    SpvId writeIntLiteral(const IntLiteral& i);

    SpvId writeFloatLiteral(const FloatLiteral& f);

    void writeStatement(const Statement& s, SkWStream& out);

    void writeBlock(const Block& b, SkWStream& out);

    void writeIfStatement(const IfStatement& stmt, SkWStream& out);

    void writeForStatement(const ForStatement& f, SkWStream& out);

    void writeReturnStatement(const ReturnStatement& r, SkWStream& out);

    void writeCapabilities(SkWStream& out);

    void writeInstructions(const Program& program, SkWStream& out);

    void writeOpCode(SpvOp_ opCode, int length, SkWStream& out);

    void writeWord(int32_t word, SkWStream& out);

    void writeString(const char* string, SkWStream& out);

    void writeLabel(SpvId id, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, const char* string, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, const char* string, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, const char* string,
                          SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, 
                          SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
                          SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
                          int32_t word5, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
                          int32_t word5, int32_t word6, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
                          int32_t word5, int32_t word6, int32_t word7, SkWStream& out);

    void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
                          int32_t word5, int32_t word6, int32_t word7, int32_t word8, 
                          SkWStream& out);

    const Context& fContext;

    uint64_t fCapabilities;
    SpvId fIdCount;
    SpvId fGLSLExtendedInstructions;
    typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
    std::unordered_map<SkString, Intrinsic> fIntrinsicMap;
    std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
    std::unordered_map<const Variable*, SpvId> fVariableMap;
    std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
    std::unordered_map<SkString, SpvId> fTypeMap;
    SkDynamicMemoryWStream fCapabilitiesBuffer;
    SkDynamicMemoryWStream fGlobalInitializersBuffer;
    SkDynamicMemoryWStream fConstantBuffer;
    SkDynamicMemoryWStream fExternalFunctionsBuffer;
    SkDynamicMemoryWStream fVariableBuffer;
    SkDynamicMemoryWStream fNameBuffer;
    SkDynamicMemoryWStream fDecorationBuffer;

    SpvId fBoolTrue;
    SpvId fBoolFalse;
    std::unordered_map<int64_t, SpvId> fIntConstants;
    std::unordered_map<uint64_t, SpvId> fUIntConstants;
    std::unordered_map<float, SpvId> fFloatConstants;
    std::unordered_map<double, SpvId> fDoubleConstants;
    // label of the current block, or 0 if we are not in a block
    SpvId fCurrentBlock;
    std::stack<SpvId> fBreakTarget;
    std::stack<SpvId> fContinueTarget;

    friend class PointerLValue;
    friend class SwizzleLValue;
};

}

#endif