blob: 53e5ea0954e68e048e5a6675335a60cce076da56 [file] [log] [blame]
ethannicholasd598f792016-07-25 10:08:54 -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
ethannicholasd598f792016-07-25 10:08:54 -07008#ifndef SKSL_CONTEXT
9#define SKSL_CONTEXT
10
11#include "ir/SkSLType.h"
ethannicholas22f939e2016-10-13 13:25:34 -070012#include "ir/SkSLExpression.h"
ethannicholasd598f792016-07-25 10:08:54 -070013
14namespace SkSL {
15
16/**
17 * Contains compiler-wide objects, which currently means the core types.
18 */
19class Context {
20public:
21 Context()
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 : fInvalid_Type(new Type(String("<INVALID>")))
23 , fVoid_Type(new Type(String("void")))
24 , fDouble_Type(new Type(String("double"), true))
25 , fDVec2_Type(new Type(String("dvec2"), *fDouble_Type, 2))
26 , fDVec3_Type(new Type(String("dvec3"), *fDouble_Type, 3))
27 , fDVec4_Type(new Type(String("dvec4"), *fDouble_Type, 4))
28 , fFloat_Type(new Type(String("float"), true, { fDouble_Type.get() }))
29 , fVec2_Type(new Type(String("vec2"), *fFloat_Type, 2))
30 , fVec3_Type(new Type(String("vec3"), *fFloat_Type, 3))
31 , fVec4_Type(new Type(String("vec4"), *fFloat_Type, 4))
32 , fUInt_Type(new Type(String("uint"), true, { fFloat_Type.get(), fDouble_Type.get() }))
33 , fUVec2_Type(new Type(String("uvec2"), *fUInt_Type, 2))
34 , fUVec3_Type(new Type(String("uvec3"), *fUInt_Type, 3))
35 , fUVec4_Type(new Type(String("uvec4"), *fUInt_Type, 4))
36 , fInt_Type(new Type(String("int"), true, { fUInt_Type.get(), fFloat_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050037 fDouble_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -040038 , fIVec2_Type(new Type(String("ivec2"), *fInt_Type, 2))
39 , fIVec3_Type(new Type(String("ivec3"), *fInt_Type, 3))
40 , fIVec4_Type(new Type(String("ivec4"), *fInt_Type, 4))
41 , fBool_Type(new Type(String("bool"), false))
42 , fBVec2_Type(new Type(String("bvec2"), *fBool_Type, 2))
43 , fBVec3_Type(new Type(String("bvec3"), *fBool_Type, 3))
44 , fBVec4_Type(new Type(String("bvec4"), *fBool_Type, 4))
45 , fMat2x2_Type(new Type(String("mat2"), *fFloat_Type, 2, 2))
46 , fMat2x3_Type(new Type(String("mat2x3"), *fFloat_Type, 2, 3))
47 , fMat2x4_Type(new Type(String("mat2x4"), *fFloat_Type, 2, 4))
48 , fMat3x2_Type(new Type(String("mat3x2"), *fFloat_Type, 3, 2))
49 , fMat3x3_Type(new Type(String("mat3"), *fFloat_Type, 3, 3))
50 , fMat3x4_Type(new Type(String("mat3x4"), *fFloat_Type, 3, 4))
51 , fMat4x2_Type(new Type(String("mat4x2"), *fFloat_Type, 4, 2))
52 , fMat4x3_Type(new Type(String("mat4x3"), *fFloat_Type, 4, 3))
53 , fMat4x4_Type(new Type(String("mat4"), *fFloat_Type, 4, 4))
54 , fDMat2x2_Type(new Type(String("dmat2"), *fFloat_Type, 2, 2))
55 , fDMat2x3_Type(new Type(String("dmat2x3"), *fFloat_Type, 2, 3))
56 , fDMat2x4_Type(new Type(String("dmat2x4"), *fFloat_Type, 2, 4))
57 , fDMat3x2_Type(new Type(String("dmat3x2"), *fFloat_Type, 3, 2))
58 , fDMat3x3_Type(new Type(String("dmat3"), *fFloat_Type, 3, 3))
59 , fDMat3x4_Type(new Type(String("dmat3x4"), *fFloat_Type, 3, 4))
60 , fDMat4x2_Type(new Type(String("dmat4x2"), *fFloat_Type, 4, 2))
61 , fDMat4x3_Type(new Type(String("dmat4x3"), *fFloat_Type, 4, 3))
62 , fDMat4x4_Type(new Type(String("dmat4"), *fFloat_Type, 4, 4))
63 , fSampler1D_Type(new Type(String("sampler1D"), SpvDim1D, false, false, false, true))
64 , fSampler2D_Type(new Type(String("sampler2D"), SpvDim2D, false, false, false, true))
65 , fSampler3D_Type(new Type(String("sampler3D"), SpvDim3D, false, false, false, true))
66 , fSamplerExternalOES_Type(new Type(String("samplerExternalOES"), SpvDim2D, false, false,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050067 false, true))
Ethan Nicholas0df1b042017-03-31 13:56:23 -040068 , fSamplerCube_Type(new Type(String("samplerCube"), SpvDimCube, false, false, false, true))
69 , fSampler2DRect_Type(new Type(String("sampler2DRect"), SpvDimRect, false, false, false,
Ethan Nicholas2b3dab62016-11-28 12:03:26 -050070 true))
Ethan Nicholas0df1b042017-03-31 13:56:23 -040071 , fSampler1DArray_Type(new Type(String("sampler1DArray")))
72 , fSampler2DArray_Type(new Type(String("sampler2DArray")))
73 , fSamplerCubeArray_Type(new Type(String("samplerCubeArray")))
Ethan Nicholas0187ae62017-05-03 11:03:44 -040074 , fSamplerBuffer_Type(new Type(String("samplerBuffer"), SpvDimBuffer, false, false, false,
75 true))
Ethan Nicholas0df1b042017-03-31 13:56:23 -040076 , fSampler2DMS_Type(new Type(String("sampler2DMS")))
77 , fSampler2DMSArray_Type(new Type(String("sampler2DMSArray")))
78 , fSampler1DShadow_Type(new Type(String("sampler1DShadow")))
79 , fSampler2DShadow_Type(new Type(String("sampler2DShadow")))
80 , fSamplerCubeShadow_Type(new Type(String("samplerCubeShadow")))
81 , fSampler2DRectShadow_Type(new Type(String("sampler2DRectShadow")))
82 , fSampler1DArrayShadow_Type(new Type(String("sampler1DArrayShadow")))
83 , fSampler2DArrayShadow_Type(new Type(String("sampler2DArrayShadow")))
84 , fSamplerCubeArrayShadow_Type(new Type(String("samplerCubeArrayShadow")))
Brian Salomon2a51de82016-11-16 12:06:01 -050085
Brian Salomonbf7b6202016-11-11 16:08:03 -050086 // Related to below FIXME, gsampler*s don't currently expand to cover integer case.
Ethan Nicholas0df1b042017-03-31 13:56:23 -040087 , fISampler2D_Type(new Type(String("isampler2D"), SpvDim2D, false, false, false, true))
Brian Salomon2a51de82016-11-16 12:06:01 -050088
89 // FIXME express these as "gimage2D" that expand to image2D, iimage2D, and uimage2D.
Ethan Nicholas0df1b042017-03-31 13:56:23 -040090 , fImage2D_Type(new Type(String("image2D"), SpvDim2D, false, false, false, true))
91 , fIImage2D_Type(new Type(String("iimage2D"), SpvDim2D, false, false, false, true))
Brian Salomon2a51de82016-11-16 12:06:01 -050092
Greg Daniel64773e62016-11-22 09:44:03 -050093 // FIXME express these as "gsubpassInput" that expand to subpassInput, isubpassInput,
94 // and usubpassInput.
Ethan Nicholas0df1b042017-03-31 13:56:23 -040095 , fSubpassInput_Type(new Type(String("subpassInput"), SpvDimSubpassData, false, false,
Greg Daniel64773e62016-11-22 09:44:03 -050096 false, false))
Ethan Nicholas0df1b042017-03-31 13:56:23 -040097 , fSubpassInputMS_Type(new Type(String("subpassInputMS"), SpvDimSubpassData, false, false,
Greg Daniel64773e62016-11-22 09:44:03 -050098 true, false))
99
ethannicholasd598f792016-07-25 10:08:54 -0700100 // FIXME figure out what we're supposed to do with the gsampler et al. types)
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400101 , fGSampler1D_Type(new Type(String("$gsampler1D"), static_type(*fSampler1D_Type)))
102 , fGSampler2D_Type(new Type(String("$gsampler2D"), static_type(*fSampler2D_Type)))
103 , fGSampler3D_Type(new Type(String("$gsampler3D"), static_type(*fSampler3D_Type)))
104 , fGSamplerCube_Type(new Type(String("$gsamplerCube"), static_type(*fSamplerCube_Type)))
105 , fGSampler2DRect_Type(new Type(String("$gsampler2DRect"), static_type(*fSampler2DRect_Type)))
106 , fGSampler1DArray_Type(new Type(String("$gsampler1DArray"),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500107 static_type(*fSampler1DArray_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400108 , fGSampler2DArray_Type(new Type(String("$gsampler2DArray"),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500109 static_type(*fSampler2DArray_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400110 , fGSamplerCubeArray_Type(new Type(String("$gsamplerCubeArray"),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500111 static_type(*fSamplerCubeArray_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400112 , fGSamplerBuffer_Type(new Type(String("$gsamplerBuffer"), static_type(*fSamplerBuffer_Type)))
113 , fGSampler2DMS_Type(new Type(String("$gsampler2DMS"), static_type(*fSampler2DMS_Type)))
114 , fGSampler2DMSArray_Type(new Type(String("$gsampler2DMSArray"),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500115 static_type(*fSampler2DMSArray_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400116 , fGSampler2DArrayShadow_Type(new Type(String("$gsampler2DArrayShadow"),
ethannicholasd598f792016-07-25 10:08:54 -0700117 static_type(*fSampler2DArrayShadow_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400118 , fGSamplerCubeArrayShadow_Type(new Type(String("$gsamplerCubeArrayShadow"),
ethannicholasd598f792016-07-25 10:08:54 -0700119 static_type(*fSamplerCubeArrayShadow_Type)))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400120 , fGenType_Type(new Type(String("$genType"), { fFloat_Type.get(), fVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500121 fVec3_Type.get(), fVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400122 , fGenDType_Type(new Type(String("$genDType"), { fDouble_Type.get(), fDVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500123 fDVec3_Type.get(), fDVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400124 , fGenIType_Type(new Type(String("$genIType"), { fInt_Type.get(), fIVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500125 fIVec3_Type.get(), fIVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400126 , fGenUType_Type(new Type(String("$genUType"), { fUInt_Type.get(), fUVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500127 fUVec3_Type.get(), fUVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400128 , fGenBType_Type(new Type(String("$genBType"), { fBool_Type.get(), fBVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500129 fBVec3_Type.get(), fBVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400130 , fMat_Type(new Type(String("$mat"), { fMat2x2_Type.get(), fMat2x3_Type.get(),
Ethan Nicholas8c3f4ae2017-02-07 14:08:31 -0500131 fMat2x4_Type.get(), fMat3x2_Type.get(),
132 fMat3x3_Type.get(), fMat3x4_Type.get(),
133 fMat4x2_Type.get(), fMat4x3_Type.get(),
134 fMat4x4_Type.get(), fDMat2x2_Type.get(),
135 fDMat2x3_Type.get(), fDMat2x4_Type.get(),
136 fDMat3x2_Type.get(), fDMat3x3_Type.get(),
137 fDMat3x4_Type.get(), fDMat4x2_Type.get(),
138 fDMat4x3_Type.get(), fDMat4x4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400139 , fVec_Type(new Type(String("$vec"), { fInvalid_Type.get(), fVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500140 fVec3_Type.get(), fVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400141 , fGVec_Type(new Type(String("$gvec")))
142 , fGVec2_Type(new Type(String("$gvec2")))
143 , fGVec3_Type(new Type(String("$gvec3")))
144 , fGVec4_Type(new Type(String("$gvec4"), static_type(*fVec4_Type)))
145 , fDVec_Type(new Type(String("$dvec"), { fInvalid_Type.get(), fDVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500146 fDVec3_Type.get(), fDVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400147 , fIVec_Type(new Type(String("$ivec"), { fInvalid_Type.get(), fIVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500148 fIVec3_Type.get(), fIVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400149 , fUVec_Type(new Type(String("$uvec"), { fInvalid_Type.get(), fUVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500150 fUVec3_Type.get(), fUVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400151 , fBVec_Type(new Type(String("$bvec"), { fInvalid_Type.get(), fBVec2_Type.get(),
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500152 fBVec3_Type.get(), fBVec4_Type.get() }))
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400153 , fSkCaps_Type(new Type(String("$sk_Caps")))
ethannicholas22f939e2016-10-13 13:25:34 -0700154 , fDefined_Expression(new Defined(*fInvalid_Type)) {}
ethannicholasd598f792016-07-25 10:08:54 -0700155
156 static std::vector<const Type*> static_type(const Type& t) {
Greg Daniel64773e62016-11-22 09:44:03 -0500157 return { &t, &t, &t, &t };
ethannicholasd598f792016-07-25 10:08:54 -0700158 }
159
ethannicholas471e8942016-10-28 09:02:46 -0700160 const std::unique_ptr<Type> fInvalid_Type;
ethannicholasd598f792016-07-25 10:08:54 -0700161 const std::unique_ptr<Type> fVoid_Type;
162
163 const std::unique_ptr<Type> fDouble_Type;
164 const std::unique_ptr<Type> fDVec2_Type;
165 const std::unique_ptr<Type> fDVec3_Type;
166 const std::unique_ptr<Type> fDVec4_Type;
167
168 const std::unique_ptr<Type> fFloat_Type;
169 const std::unique_ptr<Type> fVec2_Type;
170 const std::unique_ptr<Type> fVec3_Type;
171 const std::unique_ptr<Type> fVec4_Type;
172
173 const std::unique_ptr<Type> fUInt_Type;
174 const std::unique_ptr<Type> fUVec2_Type;
175 const std::unique_ptr<Type> fUVec3_Type;
176 const std::unique_ptr<Type> fUVec4_Type;
177
178 const std::unique_ptr<Type> fInt_Type;
179 const std::unique_ptr<Type> fIVec2_Type;
180 const std::unique_ptr<Type> fIVec3_Type;
181 const std::unique_ptr<Type> fIVec4_Type;
182
183 const std::unique_ptr<Type> fBool_Type;
184 const std::unique_ptr<Type> fBVec2_Type;
185 const std::unique_ptr<Type> fBVec3_Type;
186 const std::unique_ptr<Type> fBVec4_Type;
187
188 const std::unique_ptr<Type> fMat2x2_Type;
189 const std::unique_ptr<Type> fMat2x3_Type;
190 const std::unique_ptr<Type> fMat2x4_Type;
191 const std::unique_ptr<Type> fMat3x2_Type;
192 const std::unique_ptr<Type> fMat3x3_Type;
193 const std::unique_ptr<Type> fMat3x4_Type;
194 const std::unique_ptr<Type> fMat4x2_Type;
195 const std::unique_ptr<Type> fMat4x3_Type;
196 const std::unique_ptr<Type> fMat4x4_Type;
197
198 const std::unique_ptr<Type> fDMat2x2_Type;
199 const std::unique_ptr<Type> fDMat2x3_Type;
200 const std::unique_ptr<Type> fDMat2x4_Type;
201 const std::unique_ptr<Type> fDMat3x2_Type;
202 const std::unique_ptr<Type> fDMat3x3_Type;
203 const std::unique_ptr<Type> fDMat3x4_Type;
204 const std::unique_ptr<Type> fDMat4x2_Type;
205 const std::unique_ptr<Type> fDMat4x3_Type;
206 const std::unique_ptr<Type> fDMat4x4_Type;
207
208 const std::unique_ptr<Type> fSampler1D_Type;
209 const std::unique_ptr<Type> fSampler2D_Type;
210 const std::unique_ptr<Type> fSampler3D_Type;
ethannicholas5961bc92016-10-12 06:39:56 -0700211 const std::unique_ptr<Type> fSamplerExternalOES_Type;
ethannicholasd598f792016-07-25 10:08:54 -0700212 const std::unique_ptr<Type> fSamplerCube_Type;
213 const std::unique_ptr<Type> fSampler2DRect_Type;
214 const std::unique_ptr<Type> fSampler1DArray_Type;
215 const std::unique_ptr<Type> fSampler2DArray_Type;
216 const std::unique_ptr<Type> fSamplerCubeArray_Type;
217 const std::unique_ptr<Type> fSamplerBuffer_Type;
218 const std::unique_ptr<Type> fSampler2DMS_Type;
219 const std::unique_ptr<Type> fSampler2DMSArray_Type;
220 const std::unique_ptr<Type> fSampler1DShadow_Type;
221 const std::unique_ptr<Type> fSampler2DShadow_Type;
222 const std::unique_ptr<Type> fSamplerCubeShadow_Type;
223 const std::unique_ptr<Type> fSampler2DRectShadow_Type;
224 const std::unique_ptr<Type> fSampler1DArrayShadow_Type;
225 const std::unique_ptr<Type> fSampler2DArrayShadow_Type;
226 const std::unique_ptr<Type> fSamplerCubeArrayShadow_Type;
227
Brian Salomon2a51de82016-11-16 12:06:01 -0500228
Brian Salomonbf7b6202016-11-11 16:08:03 -0500229 const std::unique_ptr<Type> fISampler2D_Type;
230
Brian Salomon2a51de82016-11-16 12:06:01 -0500231 const std::unique_ptr<Type> fImage2D_Type;
232 const std::unique_ptr<Type> fIImage2D_Type;
233
Greg Daniel64773e62016-11-22 09:44:03 -0500234 const std::unique_ptr<Type> fSubpassInput_Type;
235 const std::unique_ptr<Type> fSubpassInputMS_Type;
236
ethannicholasd598f792016-07-25 10:08:54 -0700237 const std::unique_ptr<Type> fGSampler1D_Type;
238 const std::unique_ptr<Type> fGSampler2D_Type;
239 const std::unique_ptr<Type> fGSampler3D_Type;
240 const std::unique_ptr<Type> fGSamplerCube_Type;
241 const std::unique_ptr<Type> fGSampler2DRect_Type;
242 const std::unique_ptr<Type> fGSampler1DArray_Type;
243 const std::unique_ptr<Type> fGSampler2DArray_Type;
244 const std::unique_ptr<Type> fGSamplerCubeArray_Type;
245 const std::unique_ptr<Type> fGSamplerBuffer_Type;
246 const std::unique_ptr<Type> fGSampler2DMS_Type;
247 const std::unique_ptr<Type> fGSampler2DMSArray_Type;
248 const std::unique_ptr<Type> fGSampler2DArrayShadow_Type;
249 const std::unique_ptr<Type> fGSamplerCubeArrayShadow_Type;
250
251 const std::unique_ptr<Type> fGenType_Type;
252 const std::unique_ptr<Type> fGenDType_Type;
253 const std::unique_ptr<Type> fGenIType_Type;
254 const std::unique_ptr<Type> fGenUType_Type;
255 const std::unique_ptr<Type> fGenBType_Type;
256
257 const std::unique_ptr<Type> fMat_Type;
258
259 const std::unique_ptr<Type> fVec_Type;
260
261 const std::unique_ptr<Type> fGVec_Type;
262 const std::unique_ptr<Type> fGVec2_Type;
263 const std::unique_ptr<Type> fGVec3_Type;
264 const std::unique_ptr<Type> fGVec4_Type;
265 const std::unique_ptr<Type> fDVec_Type;
266 const std::unique_ptr<Type> fIVec_Type;
267 const std::unique_ptr<Type> fUVec_Type;
268
269 const std::unique_ptr<Type> fBVec_Type;
270
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500271 const std::unique_ptr<Type> fSkCaps_Type;
272
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400273 // dummy expression used to mark that a variable has a value during dataflow analysis (when it
ethannicholas22f939e2016-10-13 13:25:34 -0700274 // could have several different values, or the analyzer is otherwise unable to assign it a
275 // specific expression)
276 const std::unique_ptr<Expression> fDefined_Expression;
277
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400278private:
ethannicholas22f939e2016-10-13 13:25:34 -0700279 class Defined : public Expression {
280 public:
281 Defined(const Type& type)
282 : INHERITED(Position(), kDefined_Kind, type) {}
283
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400284 virtual String description() const override {
285 return String("<defined>");
ethannicholas22f939e2016-10-13 13:25:34 -0700286 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400287
288 bool hasSideEffects() const override {
289 return false;
290 }
ethannicholas22f939e2016-10-13 13:25:34 -0700291
292 typedef Expression INHERITED;
293 };
ethannicholasd598f792016-07-25 10:08:54 -0700294};
295
296} // namespace
297
298#endif