SkSLContext.h revision 113628d76176a1ab3e6719c59efff23cd10ab213
1/*
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_CONTEXT
9#define SKSL_CONTEXT
10
11#include "ir/SkSLType.h"
12#include "ir/SkSLExpression.h"
13
14namespace SkSL {
15
16/**
17 * Contains compiler-wide objects, which currently means the core types.
18 */
19class Context {
20public:
21    Context()
22    : fInvalid_Type(new Type(SkString("<INVALID>")))
23    , fVoid_Type(new Type(SkString("void")))
24    , fDouble_Type(new Type(SkString("double"), true))
25    , fDVec2_Type(new Type(SkString("dvec2"), *fDouble_Type, 2))
26    , fDVec3_Type(new Type(SkString("dvec3"), *fDouble_Type, 3))
27    , fDVec4_Type(new Type(SkString("dvec4"), *fDouble_Type, 4))
28    , fFloat_Type(new Type(SkString("float"), true, { fDouble_Type.get() }))
29    , fVec2_Type(new Type(SkString("vec2"), *fFloat_Type, 2))
30    , fVec3_Type(new Type(SkString("vec3"), *fFloat_Type, 3))
31    , fVec4_Type(new Type(SkString("vec4"), *fFloat_Type, 4))
32    , fUInt_Type(new Type(SkString("uint"), true, { fFloat_Type.get(), fDouble_Type.get() }))
33    , fUVec2_Type(new Type(SkString("uvec2"), *fUInt_Type, 2))
34    , fUVec3_Type(new Type(SkString("uvec3"), *fUInt_Type, 3))
35    , fUVec4_Type(new Type(SkString("uvec4"), *fUInt_Type, 4))
36    , fInt_Type(new Type(SkString("int"), true, { fUInt_Type.get(), fFloat_Type.get(),
37                                                  fDouble_Type.get() }))
38    , fIVec2_Type(new Type(SkString("ivec2"), *fInt_Type, 2))
39    , fIVec3_Type(new Type(SkString("ivec3"), *fInt_Type, 3))
40    , fIVec4_Type(new Type(SkString("ivec4"), *fInt_Type, 4))
41    , fBool_Type(new Type(SkString("bool"), false))
42    , fBVec2_Type(new Type(SkString("bvec2"), *fBool_Type, 2))
43    , fBVec3_Type(new Type(SkString("bvec3"), *fBool_Type, 3))
44    , fBVec4_Type(new Type(SkString("bvec4"), *fBool_Type, 4))
45    , fMat2x2_Type(new Type(SkString("mat2"),   *fFloat_Type, 2, 2))
46    , fMat2x3_Type(new Type(SkString("mat2x3"), *fFloat_Type, 2, 3))
47    , fMat2x4_Type(new Type(SkString("mat2x4"), *fFloat_Type, 2, 4))
48    , fMat3x2_Type(new Type(SkString("mat3x2"), *fFloat_Type, 3, 2))
49    , fMat3x3_Type(new Type(SkString("mat3"),   *fFloat_Type, 3, 3))
50    , fMat3x4_Type(new Type(SkString("mat3x4"), *fFloat_Type, 3, 4))
51    , fMat4x2_Type(new Type(SkString("mat4x2"), *fFloat_Type, 4, 2))
52    , fMat4x3_Type(new Type(SkString("mat4x3"), *fFloat_Type, 4, 3))
53    , fMat4x4_Type(new Type(SkString("mat4"),   *fFloat_Type, 4, 4))
54    , fDMat2x2_Type(new Type(SkString("dmat2"),   *fFloat_Type, 2, 2))
55    , fDMat2x3_Type(new Type(SkString("dmat2x3"), *fFloat_Type, 2, 3))
56    , fDMat2x4_Type(new Type(SkString("dmat2x4"), *fFloat_Type, 2, 4))
57    , fDMat3x2_Type(new Type(SkString("dmat3x2"), *fFloat_Type, 3, 2))
58    , fDMat3x3_Type(new Type(SkString("dmat3"),   *fFloat_Type, 3, 3))
59    , fDMat3x4_Type(new Type(SkString("dmat3x4"), *fFloat_Type, 3, 4))
60    , fDMat4x2_Type(new Type(SkString("dmat4x2"), *fFloat_Type, 4, 2))
61    , fDMat4x3_Type(new Type(SkString("dmat4x3"), *fFloat_Type, 4, 3))
62    , fDMat4x4_Type(new Type(SkString("dmat4"),   *fFloat_Type, 4, 4))
63    , fSampler1D_Type(new Type(SkString("sampler1D"), SpvDim1D, false, false, false, true))
64    , fSampler2D_Type(new Type(SkString("sampler2D"), SpvDim2D, false, false, false, true))
65    , fSampler3D_Type(new Type(SkString("sampler3D"), SpvDim3D, false, false, false, true))
66    , fSamplerExternalOES_Type(new Type(SkString("samplerExternalOES"), SpvDim2D, false, false,
67                                        false, true))
68    , fSamplerCube_Type(new Type(SkString("samplerCube"), SpvDimCube, false, false, false, true))
69    , fSampler2DRect_Type(new Type(SkString("sampler2DRect"), SpvDimRect, false, false, false,
70                                   true))
71    , fSampler1DArray_Type(new Type(SkString("sampler1DArray")))
72    , fSampler2DArray_Type(new Type(SkString("sampler2DArray")))
73    , fSamplerCubeArray_Type(new Type(SkString("samplerCubeArray")))
74    , fSamplerBuffer_Type(new Type(SkString("samplerBuffer")))
75    , fSampler2DMS_Type(new Type(SkString("sampler2DMS")))
76    , fSampler2DMSArray_Type(new Type(SkString("sampler2DMSArray")))
77    , fSampler1DShadow_Type(new Type(SkString("sampler1DShadow")))
78    , fSampler2DShadow_Type(new Type(SkString("sampler2DShadow")))
79    , fSamplerCubeShadow_Type(new Type(SkString("samplerCubeShadow")))
80    , fSampler2DRectShadow_Type(new Type(SkString("sampler2DRectShadow")))
81    , fSampler1DArrayShadow_Type(new Type(SkString("sampler1DArrayShadow")))
82    , fSampler2DArrayShadow_Type(new Type(SkString("sampler2DArrayShadow")))
83    , fSamplerCubeArrayShadow_Type(new Type(SkString("samplerCubeArrayShadow")))
84
85    // Related to below FIXME, gsampler*s don't currently expand to cover integer case.
86    , fISampler2D_Type(new Type(SkString("isampler2D"), SpvDim2D, false, false, false, true))
87
88    // FIXME express these as "gimage2D" that expand to image2D, iimage2D, and uimage2D.
89    , fImage2D_Type(new Type(SkString("image2D"), SpvDim2D, false, false, false, true))
90    , fIImage2D_Type(new Type(SkString("iimage2D"), SpvDim2D, false, false, false, true))
91
92    // FIXME express these as "gsubpassInput" that expand to subpassInput, isubpassInput,
93    // and usubpassInput.
94    , fSubpassInput_Type(new Type(SkString("subpassInput"), SpvDimSubpassData, false, false,
95                                           false, false))
96    , fSubpassInputMS_Type(new Type(SkString("subpassInputMS"), SpvDimSubpassData, false, false,
97                                             true, false))
98
99    // FIXME figure out what we're supposed to do with the gsampler et al. types)
100    , fGSampler1D_Type(new Type(SkString("$gsampler1D"), static_type(*fSampler1D_Type)))
101    , fGSampler2D_Type(new Type(SkString("$gsampler2D"), static_type(*fSampler2D_Type)))
102    , fGSampler3D_Type(new Type(SkString("$gsampler3D"), static_type(*fSampler3D_Type)))
103    , fGSamplerCube_Type(new Type(SkString("$gsamplerCube"), static_type(*fSamplerCube_Type)))
104    , fGSampler2DRect_Type(new Type(SkString("$gsampler2DRect"), static_type(*fSampler2DRect_Type)))
105    , fGSampler1DArray_Type(new Type(SkString("$gsampler1DArray"),
106                                     static_type(*fSampler1DArray_Type)))
107    , fGSampler2DArray_Type(new Type(SkString("$gsampler2DArray"),
108                                     static_type(*fSampler2DArray_Type)))
109    , fGSamplerCubeArray_Type(new Type(SkString("$gsamplerCubeArray"),
110                                       static_type(*fSamplerCubeArray_Type)))
111    , fGSamplerBuffer_Type(new Type(SkString("$gsamplerBuffer"), static_type(*fSamplerBuffer_Type)))
112    , fGSampler2DMS_Type(new Type(SkString("$gsampler2DMS"), static_type(*fSampler2DMS_Type)))
113    , fGSampler2DMSArray_Type(new Type(SkString("$gsampler2DMSArray"),
114                                       static_type(*fSampler2DMSArray_Type)))
115    , fGSampler2DArrayShadow_Type(new Type(SkString("$gsampler2DArrayShadow"),
116                                           static_type(*fSampler2DArrayShadow_Type)))
117    , fGSamplerCubeArrayShadow_Type(new Type(SkString("$gsamplerCubeArrayShadow"),
118                                             static_type(*fSamplerCubeArrayShadow_Type)))
119    , fGenType_Type(new Type(SkString("$genType"), { fFloat_Type.get(), fVec2_Type.get(),
120                                                     fVec3_Type.get(), fVec4_Type.get() }))
121    , fGenDType_Type(new Type(SkString("$genDType"), { fDouble_Type.get(), fDVec2_Type.get(),
122                                                       fDVec3_Type.get(), fDVec4_Type.get() }))
123    , fGenIType_Type(new Type(SkString("$genIType"), { fInt_Type.get(), fIVec2_Type.get(),
124                                                       fIVec3_Type.get(), fIVec4_Type.get() }))
125    , fGenUType_Type(new Type(SkString("$genUType"), { fUInt_Type.get(), fUVec2_Type.get(),
126                                                       fUVec3_Type.get(), fUVec4_Type.get() }))
127    , fGenBType_Type(new Type(SkString("$genBType"), { fBool_Type.get(), fBVec2_Type.get(),
128                                                       fBVec3_Type.get(), fBVec4_Type.get() }))
129    , fMat_Type(new Type(SkString("$mat")))
130    , fVec_Type(new Type(SkString("$vec"), { fInvalid_Type.get(), fVec2_Type.get(),
131                                             fVec3_Type.get(), fVec4_Type.get() }))
132    , fGVec_Type(new Type(SkString("$gvec")))
133    , fGVec2_Type(new Type(SkString("$gvec2")))
134    , fGVec3_Type(new Type(SkString("$gvec3")))
135    , fGVec4_Type(new Type(SkString("$gvec4"), static_type(*fVec4_Type)))
136    , fDVec_Type(new Type(SkString("$dvec"), { fInvalid_Type.get(), fDVec2_Type.get(),
137                                              fDVec3_Type.get(), fDVec4_Type.get() }))
138    , fIVec_Type(new Type(SkString("$ivec"), { fInvalid_Type.get(), fIVec2_Type.get(),
139                                               fIVec3_Type.get(), fIVec4_Type.get() }))
140    , fUVec_Type(new Type(SkString("$uvec"), { fInvalid_Type.get(), fUVec2_Type.get(),
141                                               fUVec3_Type.get(), fUVec4_Type.get() }))
142    , fBVec_Type(new Type(SkString("$bvec"), { fInvalid_Type.get(), fBVec2_Type.get(),
143                                               fBVec3_Type.get(), fBVec4_Type.get() }))
144    , fSkCaps_Type(new Type(SkString("$sk_Caps")))
145    , fDefined_Expression(new Defined(*fInvalid_Type)) {}
146
147    static std::vector<const Type*> static_type(const Type& t) {
148        return { &t, &t, &t, &t };
149    }
150
151    const std::unique_ptr<Type> fInvalid_Type;
152    const std::unique_ptr<Type> fVoid_Type;
153
154    const std::unique_ptr<Type> fDouble_Type;
155    const std::unique_ptr<Type> fDVec2_Type;
156    const std::unique_ptr<Type> fDVec3_Type;
157    const std::unique_ptr<Type> fDVec4_Type;
158
159    const std::unique_ptr<Type> fFloat_Type;
160    const std::unique_ptr<Type> fVec2_Type;
161    const std::unique_ptr<Type> fVec3_Type;
162    const std::unique_ptr<Type> fVec4_Type;
163
164    const std::unique_ptr<Type> fUInt_Type;
165    const std::unique_ptr<Type> fUVec2_Type;
166    const std::unique_ptr<Type> fUVec3_Type;
167    const std::unique_ptr<Type> fUVec4_Type;
168
169    const std::unique_ptr<Type> fInt_Type;
170    const std::unique_ptr<Type> fIVec2_Type;
171    const std::unique_ptr<Type> fIVec3_Type;
172    const std::unique_ptr<Type> fIVec4_Type;
173
174    const std::unique_ptr<Type> fBool_Type;
175    const std::unique_ptr<Type> fBVec2_Type;
176    const std::unique_ptr<Type> fBVec3_Type;
177    const std::unique_ptr<Type> fBVec4_Type;
178
179    const std::unique_ptr<Type> fMat2x2_Type;
180    const std::unique_ptr<Type> fMat2x3_Type;
181    const std::unique_ptr<Type> fMat2x4_Type;
182    const std::unique_ptr<Type> fMat3x2_Type;
183    const std::unique_ptr<Type> fMat3x3_Type;
184    const std::unique_ptr<Type> fMat3x4_Type;
185    const std::unique_ptr<Type> fMat4x2_Type;
186    const std::unique_ptr<Type> fMat4x3_Type;
187    const std::unique_ptr<Type> fMat4x4_Type;
188
189    const std::unique_ptr<Type> fDMat2x2_Type;
190    const std::unique_ptr<Type> fDMat2x3_Type;
191    const std::unique_ptr<Type> fDMat2x4_Type;
192    const std::unique_ptr<Type> fDMat3x2_Type;
193    const std::unique_ptr<Type> fDMat3x3_Type;
194    const std::unique_ptr<Type> fDMat3x4_Type;
195    const std::unique_ptr<Type> fDMat4x2_Type;
196    const std::unique_ptr<Type> fDMat4x3_Type;
197    const std::unique_ptr<Type> fDMat4x4_Type;
198
199    const std::unique_ptr<Type> fSampler1D_Type;
200    const std::unique_ptr<Type> fSampler2D_Type;
201    const std::unique_ptr<Type> fSampler3D_Type;
202    const std::unique_ptr<Type> fSamplerExternalOES_Type;
203    const std::unique_ptr<Type> fSamplerCube_Type;
204    const std::unique_ptr<Type> fSampler2DRect_Type;
205    const std::unique_ptr<Type> fSampler1DArray_Type;
206    const std::unique_ptr<Type> fSampler2DArray_Type;
207    const std::unique_ptr<Type> fSamplerCubeArray_Type;
208    const std::unique_ptr<Type> fSamplerBuffer_Type;
209    const std::unique_ptr<Type> fSampler2DMS_Type;
210    const std::unique_ptr<Type> fSampler2DMSArray_Type;
211    const std::unique_ptr<Type> fSampler1DShadow_Type;
212    const std::unique_ptr<Type> fSampler2DShadow_Type;
213    const std::unique_ptr<Type> fSamplerCubeShadow_Type;
214    const std::unique_ptr<Type> fSampler2DRectShadow_Type;
215    const std::unique_ptr<Type> fSampler1DArrayShadow_Type;
216    const std::unique_ptr<Type> fSampler2DArrayShadow_Type;
217    const std::unique_ptr<Type> fSamplerCubeArrayShadow_Type;
218
219
220    const std::unique_ptr<Type> fISampler2D_Type;
221
222    const std::unique_ptr<Type> fImage2D_Type;
223    const std::unique_ptr<Type> fIImage2D_Type;
224
225    const std::unique_ptr<Type> fSubpassInput_Type;
226    const std::unique_ptr<Type> fSubpassInputMS_Type;
227
228    const std::unique_ptr<Type> fGSampler1D_Type;
229    const std::unique_ptr<Type> fGSampler2D_Type;
230    const std::unique_ptr<Type> fGSampler3D_Type;
231    const std::unique_ptr<Type> fGSamplerCube_Type;
232    const std::unique_ptr<Type> fGSampler2DRect_Type;
233    const std::unique_ptr<Type> fGSampler1DArray_Type;
234    const std::unique_ptr<Type> fGSampler2DArray_Type;
235    const std::unique_ptr<Type> fGSamplerCubeArray_Type;
236    const std::unique_ptr<Type> fGSamplerBuffer_Type;
237    const std::unique_ptr<Type> fGSampler2DMS_Type;
238    const std::unique_ptr<Type> fGSampler2DMSArray_Type;
239    const std::unique_ptr<Type> fGSampler2DArrayShadow_Type;
240    const std::unique_ptr<Type> fGSamplerCubeArrayShadow_Type;
241
242    const std::unique_ptr<Type> fGenType_Type;
243    const std::unique_ptr<Type> fGenDType_Type;
244    const std::unique_ptr<Type> fGenIType_Type;
245    const std::unique_ptr<Type> fGenUType_Type;
246    const std::unique_ptr<Type> fGenBType_Type;
247
248    const std::unique_ptr<Type> fMat_Type;
249
250    const std::unique_ptr<Type> fVec_Type;
251
252    const std::unique_ptr<Type> fGVec_Type;
253    const std::unique_ptr<Type> fGVec2_Type;
254    const std::unique_ptr<Type> fGVec3_Type;
255    const std::unique_ptr<Type> fGVec4_Type;
256    const std::unique_ptr<Type> fDVec_Type;
257    const std::unique_ptr<Type> fIVec_Type;
258    const std::unique_ptr<Type> fUVec_Type;
259
260    const std::unique_ptr<Type> fBVec_Type;
261
262    const std::unique_ptr<Type> fSkCaps_Type;
263
264    // dummy expression used to mark that a variable has a value during dataflow analysis (when it
265    // could have several different values, or the analyzer is otherwise unable to assign it a
266    // specific expression)
267    const std::unique_ptr<Expression> fDefined_Expression;
268
269private:
270    class Defined : public Expression {
271    public:
272        Defined(const Type& type)
273        : INHERITED(Position(), kDefined_Kind, type) {}
274
275        bool hasSideEffects() const override {
276            return false;
277        }
278
279        SkString description() const override {
280            return SkString("<defined>");
281        }
282
283        typedef Expression INHERITED;
284    };
285};
286
287} // namespace
288
289#endif
290