blob: a42a4cc85534ff9aa328ab90f13757f80b0797e6 [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 */
7
8#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()
ethannicholas471e8942016-10-28 09:02:46 -070022 : fInvalid_Type(new Type("<INVALID>"))
23 , fVoid_Type(new Type("void"))
ethannicholasd598f792016-07-25 10:08:54 -070024 , fDouble_Type(new Type("double", true))
25 , fDVec2_Type(new Type("dvec2", *fDouble_Type, 2))
26 , fDVec3_Type(new Type("dvec3", *fDouble_Type, 3))
27 , fDVec4_Type(new Type("dvec4", *fDouble_Type, 4))
28 , fFloat_Type(new Type("float", true, { fDouble_Type.get() }))
29 , fVec2_Type(new Type("vec2", *fFloat_Type, 2))
30 , fVec3_Type(new Type("vec3", *fFloat_Type, 3))
31 , fVec4_Type(new Type("vec4", *fFloat_Type, 4))
32 , fUInt_Type(new Type("uint", true, { fFloat_Type.get(), fDouble_Type.get() }))
33 , fUVec2_Type(new Type("uvec2", *fUInt_Type, 2))
34 , fUVec3_Type(new Type("uvec3", *fUInt_Type, 3))
35 , fUVec4_Type(new Type("uvec4", *fUInt_Type, 4))
36 , fInt_Type(new Type("int", true, { fUInt_Type.get(), fFloat_Type.get(), fDouble_Type.get() }))
37 , fIVec2_Type(new Type("ivec2", *fInt_Type, 2))
38 , fIVec3_Type(new Type("ivec3", *fInt_Type, 3))
39 , fIVec4_Type(new Type("ivec4", *fInt_Type, 4))
40 , fBool_Type(new Type("bool", false))
41 , fBVec2_Type(new Type("bvec2", *fBool_Type, 2))
42 , fBVec3_Type(new Type("bvec3", *fBool_Type, 3))
43 , fBVec4_Type(new Type("bvec4", *fBool_Type, 4))
44 , fMat2x2_Type(new Type("mat2", *fFloat_Type, 2, 2))
45 , fMat2x3_Type(new Type("mat2x3", *fFloat_Type, 2, 3))
46 , fMat2x4_Type(new Type("mat2x4", *fFloat_Type, 2, 4))
47 , fMat3x2_Type(new Type("mat3x2", *fFloat_Type, 3, 2))
48 , fMat3x3_Type(new Type("mat3", *fFloat_Type, 3, 3))
49 , fMat3x4_Type(new Type("mat3x4", *fFloat_Type, 3, 4))
50 , fMat4x2_Type(new Type("mat4x2", *fFloat_Type, 4, 2))
51 , fMat4x3_Type(new Type("mat4x3", *fFloat_Type, 4, 3))
52 , fMat4x4_Type(new Type("mat4", *fFloat_Type, 4, 4))
53 , fDMat2x2_Type(new Type("dmat2", *fFloat_Type, 2, 2))
54 , fDMat2x3_Type(new Type("dmat2x3", *fFloat_Type, 2, 3))
55 , fDMat2x4_Type(new Type("dmat2x4", *fFloat_Type, 2, 4))
56 , fDMat3x2_Type(new Type("dmat3x2", *fFloat_Type, 3, 2))
57 , fDMat3x3_Type(new Type("dmat3", *fFloat_Type, 3, 3))
58 , fDMat3x4_Type(new Type("dmat3x4", *fFloat_Type, 3, 4))
59 , fDMat4x2_Type(new Type("dmat4x2", *fFloat_Type, 4, 2))
60 , fDMat4x3_Type(new Type("dmat4x3", *fFloat_Type, 4, 3))
61 , fDMat4x4_Type(new Type("dmat4", *fFloat_Type, 4, 4))
62 , fSampler1D_Type(new Type("sampler1D", SpvDim1D, false, false, false, true))
63 , fSampler2D_Type(new Type("sampler2D", SpvDim2D, false, false, false, true))
64 , fSampler3D_Type(new Type("sampler3D", SpvDim3D, false, false, false, true))
ethannicholas5961bc92016-10-12 06:39:56 -070065 , fSamplerExternalOES_Type(new Type("samplerExternalOES", SpvDim2D, false, false, false, true))
ethannicholasd598f792016-07-25 10:08:54 -070066 , fSamplerCube_Type(new Type("samplerCube"))
67 , fSampler2DRect_Type(new Type("sampler2DRect"))
68 , fSampler1DArray_Type(new Type("sampler1DArray"))
69 , fSampler2DArray_Type(new Type("sampler2DArray"))
70 , fSamplerCubeArray_Type(new Type("samplerCubeArray"))
71 , fSamplerBuffer_Type(new Type("samplerBuffer"))
72 , fSampler2DMS_Type(new Type("sampler2DMS"))
73 , fSampler2DMSArray_Type(new Type("sampler2DMSArray"))
74 , fSampler1DShadow_Type(new Type("sampler1DShadow"))
75 , fSampler2DShadow_Type(new Type("sampler2DShadow"))
76 , fSamplerCubeShadow_Type(new Type("samplerCubeShadow"))
77 , fSampler2DRectShadow_Type(new Type("sampler2DRectShadow"))
78 , fSampler1DArrayShadow_Type(new Type("sampler1DArrayShadow"))
79 , fSampler2DArrayShadow_Type(new Type("sampler2DArrayShadow"))
80 , fSamplerCubeArrayShadow_Type(new Type("samplerCubeArrayShadow"))
Brian Salomon2a51de82016-11-16 12:06:01 -050081
Brian Salomonbf7b6202016-11-11 16:08:03 -050082 // Related to below FIXME, gsampler*s don't currently expand to cover integer case.
83 , fISampler2D_Type(new Type("isampler2D", SpvDim2D, false, false, false, true))
Brian Salomon2a51de82016-11-16 12:06:01 -050084
85 // FIXME express these as "gimage2D" that expand to image2D, iimage2D, and uimage2D.
86 , fImage2D_Type(new Type("image2D", SpvDim2D, false, false, false, true))
87 , fIImage2D_Type(new Type("iimage2D", SpvDim2D, false, false, false, true))
88
ethannicholasd598f792016-07-25 10:08:54 -070089 // FIXME figure out what we're supposed to do with the gsampler et al. types)
90 , fGSampler1D_Type(new Type("$gsampler1D", static_type(*fSampler1D_Type)))
91 , fGSampler2D_Type(new Type("$gsampler2D", static_type(*fSampler2D_Type)))
92 , fGSampler3D_Type(new Type("$gsampler3D", static_type(*fSampler3D_Type)))
93 , fGSamplerCube_Type(new Type("$gsamplerCube", static_type(*fSamplerCube_Type)))
94 , fGSampler2DRect_Type(new Type("$gsampler2DRect", static_type(*fSampler2DRect_Type)))
95 , fGSampler1DArray_Type(new Type("$gsampler1DArray", static_type(*fSampler1DArray_Type)))
96 , fGSampler2DArray_Type(new Type("$gsampler2DArray", static_type(*fSampler2DArray_Type)))
97 , fGSamplerCubeArray_Type(new Type("$gsamplerCubeArray", static_type(*fSamplerCubeArray_Type)))
98 , fGSamplerBuffer_Type(new Type("$gsamplerBuffer", static_type(*fSamplerBuffer_Type)))
99 , fGSampler2DMS_Type(new Type("$gsampler2DMS", static_type(*fSampler2DMS_Type)))
100 , fGSampler2DMSArray_Type(new Type("$gsampler2DMSArray", static_type(*fSampler2DMSArray_Type)))
101 , fGSampler2DArrayShadow_Type(new Type("$gsampler2DArrayShadow",
102 static_type(*fSampler2DArrayShadow_Type)))
103 , fGSamplerCubeArrayShadow_Type(new Type("$gsamplerCubeArrayShadow",
104 static_type(*fSamplerCubeArrayShadow_Type)))
105 , fGenType_Type(new Type("$genType", { fFloat_Type.get(), fVec2_Type.get(), fVec3_Type.get(),
106 fVec4_Type.get() }))
107 , fGenDType_Type(new Type("$genDType", { fDouble_Type.get(), fDVec2_Type.get(),
108 fDVec3_Type.get(), fDVec4_Type.get() }))
109 , fGenIType_Type(new Type("$genIType", { fInt_Type.get(), fIVec2_Type.get(), fIVec3_Type.get(),
110 fIVec4_Type.get() }))
111 , fGenUType_Type(new Type("$genUType", { fUInt_Type.get(), fUVec2_Type.get(), fUVec3_Type.get(),
112 fUVec4_Type.get() }))
113 , fGenBType_Type(new Type("$genBType", { fBool_Type.get(), fBVec2_Type.get(), fBVec3_Type.get(),
114 fBVec4_Type.get() }))
115 , fMat_Type(new Type("$mat"))
ethannicholas471e8942016-10-28 09:02:46 -0700116 , fVec_Type(new Type("$vec", { fInvalid_Type.get(), fVec2_Type.get(), fVec3_Type.get(),
ethannicholasd598f792016-07-25 10:08:54 -0700117 fVec4_Type.get() }))
118 , fGVec_Type(new Type("$gvec"))
119 , fGVec2_Type(new Type("$gvec2"))
120 , fGVec3_Type(new Type("$gvec3"))
121 , fGVec4_Type(new Type("$gvec4", static_type(*fVec4_Type)))
ethannicholas471e8942016-10-28 09:02:46 -0700122 , fDVec_Type(new Type("$dvec", { fInvalid_Type.get(), fDVec2_Type.get(), fDVec3_Type.get(),
123 fDVec4_Type.get() }))
124 , fIVec_Type(new Type("$ivec", { fInvalid_Type.get(), fIVec2_Type.get(), fIVec3_Type.get(),
125 fIVec4_Type.get() }))
126 , fUVec_Type(new Type("$uvec", { fInvalid_Type.get(), fUVec2_Type.get(), fUVec3_Type.get(),
127 fUVec4_Type.get() }))
128 , fBVec_Type(new Type("$bvec", { fInvalid_Type.get(), fBVec2_Type.get(), fBVec3_Type.get(),
ethannicholasd598f792016-07-25 10:08:54 -0700129 fBVec4_Type.get() }))
ethannicholas22f939e2016-10-13 13:25:34 -0700130 , fDefined_Expression(new Defined(*fInvalid_Type)) {}
ethannicholasd598f792016-07-25 10:08:54 -0700131
132 static std::vector<const Type*> static_type(const Type& t) {
133 return { &t, &t, &t, &t };
134 }
135
ethannicholas471e8942016-10-28 09:02:46 -0700136 const std::unique_ptr<Type> fInvalid_Type;
ethannicholasd598f792016-07-25 10:08:54 -0700137 const std::unique_ptr<Type> fVoid_Type;
138
139 const std::unique_ptr<Type> fDouble_Type;
140 const std::unique_ptr<Type> fDVec2_Type;
141 const std::unique_ptr<Type> fDVec3_Type;
142 const std::unique_ptr<Type> fDVec4_Type;
143
144 const std::unique_ptr<Type> fFloat_Type;
145 const std::unique_ptr<Type> fVec2_Type;
146 const std::unique_ptr<Type> fVec3_Type;
147 const std::unique_ptr<Type> fVec4_Type;
148
149 const std::unique_ptr<Type> fUInt_Type;
150 const std::unique_ptr<Type> fUVec2_Type;
151 const std::unique_ptr<Type> fUVec3_Type;
152 const std::unique_ptr<Type> fUVec4_Type;
153
154 const std::unique_ptr<Type> fInt_Type;
155 const std::unique_ptr<Type> fIVec2_Type;
156 const std::unique_ptr<Type> fIVec3_Type;
157 const std::unique_ptr<Type> fIVec4_Type;
158
159 const std::unique_ptr<Type> fBool_Type;
160 const std::unique_ptr<Type> fBVec2_Type;
161 const std::unique_ptr<Type> fBVec3_Type;
162 const std::unique_ptr<Type> fBVec4_Type;
163
164 const std::unique_ptr<Type> fMat2x2_Type;
165 const std::unique_ptr<Type> fMat2x3_Type;
166 const std::unique_ptr<Type> fMat2x4_Type;
167 const std::unique_ptr<Type> fMat3x2_Type;
168 const std::unique_ptr<Type> fMat3x3_Type;
169 const std::unique_ptr<Type> fMat3x4_Type;
170 const std::unique_ptr<Type> fMat4x2_Type;
171 const std::unique_ptr<Type> fMat4x3_Type;
172 const std::unique_ptr<Type> fMat4x4_Type;
173
174 const std::unique_ptr<Type> fDMat2x2_Type;
175 const std::unique_ptr<Type> fDMat2x3_Type;
176 const std::unique_ptr<Type> fDMat2x4_Type;
177 const std::unique_ptr<Type> fDMat3x2_Type;
178 const std::unique_ptr<Type> fDMat3x3_Type;
179 const std::unique_ptr<Type> fDMat3x4_Type;
180 const std::unique_ptr<Type> fDMat4x2_Type;
181 const std::unique_ptr<Type> fDMat4x3_Type;
182 const std::unique_ptr<Type> fDMat4x4_Type;
183
184 const std::unique_ptr<Type> fSampler1D_Type;
185 const std::unique_ptr<Type> fSampler2D_Type;
186 const std::unique_ptr<Type> fSampler3D_Type;
ethannicholas5961bc92016-10-12 06:39:56 -0700187 const std::unique_ptr<Type> fSamplerExternalOES_Type;
ethannicholasd598f792016-07-25 10:08:54 -0700188 const std::unique_ptr<Type> fSamplerCube_Type;
189 const std::unique_ptr<Type> fSampler2DRect_Type;
190 const std::unique_ptr<Type> fSampler1DArray_Type;
191 const std::unique_ptr<Type> fSampler2DArray_Type;
192 const std::unique_ptr<Type> fSamplerCubeArray_Type;
193 const std::unique_ptr<Type> fSamplerBuffer_Type;
194 const std::unique_ptr<Type> fSampler2DMS_Type;
195 const std::unique_ptr<Type> fSampler2DMSArray_Type;
196 const std::unique_ptr<Type> fSampler1DShadow_Type;
197 const std::unique_ptr<Type> fSampler2DShadow_Type;
198 const std::unique_ptr<Type> fSamplerCubeShadow_Type;
199 const std::unique_ptr<Type> fSampler2DRectShadow_Type;
200 const std::unique_ptr<Type> fSampler1DArrayShadow_Type;
201 const std::unique_ptr<Type> fSampler2DArrayShadow_Type;
202 const std::unique_ptr<Type> fSamplerCubeArrayShadow_Type;
203
Brian Salomon2a51de82016-11-16 12:06:01 -0500204
Brian Salomonbf7b6202016-11-11 16:08:03 -0500205 const std::unique_ptr<Type> fISampler2D_Type;
206
Brian Salomon2a51de82016-11-16 12:06:01 -0500207 const std::unique_ptr<Type> fImage2D_Type;
208 const std::unique_ptr<Type> fIImage2D_Type;
209
ethannicholasd598f792016-07-25 10:08:54 -0700210 const std::unique_ptr<Type> fGSampler1D_Type;
211 const std::unique_ptr<Type> fGSampler2D_Type;
212 const std::unique_ptr<Type> fGSampler3D_Type;
213 const std::unique_ptr<Type> fGSamplerCube_Type;
214 const std::unique_ptr<Type> fGSampler2DRect_Type;
215 const std::unique_ptr<Type> fGSampler1DArray_Type;
216 const std::unique_ptr<Type> fGSampler2DArray_Type;
217 const std::unique_ptr<Type> fGSamplerCubeArray_Type;
218 const std::unique_ptr<Type> fGSamplerBuffer_Type;
219 const std::unique_ptr<Type> fGSampler2DMS_Type;
220 const std::unique_ptr<Type> fGSampler2DMSArray_Type;
221 const std::unique_ptr<Type> fGSampler2DArrayShadow_Type;
222 const std::unique_ptr<Type> fGSamplerCubeArrayShadow_Type;
223
224 const std::unique_ptr<Type> fGenType_Type;
225 const std::unique_ptr<Type> fGenDType_Type;
226 const std::unique_ptr<Type> fGenIType_Type;
227 const std::unique_ptr<Type> fGenUType_Type;
228 const std::unique_ptr<Type> fGenBType_Type;
229
230 const std::unique_ptr<Type> fMat_Type;
231
232 const std::unique_ptr<Type> fVec_Type;
233
234 const std::unique_ptr<Type> fGVec_Type;
235 const std::unique_ptr<Type> fGVec2_Type;
236 const std::unique_ptr<Type> fGVec3_Type;
237 const std::unique_ptr<Type> fGVec4_Type;
238 const std::unique_ptr<Type> fDVec_Type;
239 const std::unique_ptr<Type> fIVec_Type;
240 const std::unique_ptr<Type> fUVec_Type;
241
242 const std::unique_ptr<Type> fBVec_Type;
243
ethannicholas22f939e2016-10-13 13:25:34 -0700244 // dummy expression used to mark that a variable has a value during dataflow analysis (when it
245 // could have several different values, or the analyzer is otherwise unable to assign it a
246 // specific expression)
247 const std::unique_ptr<Expression> fDefined_Expression;
248
249private:
250 class Defined : public Expression {
251 public:
252 Defined(const Type& type)
253 : INHERITED(Position(), kDefined_Kind, type) {}
254
255 virtual std::string description() const override {
256 return "<defined>";
257 }
258
259 typedef Expression INHERITED;
260 };
ethannicholasd598f792016-07-25 10:08:54 -0700261};
262
263} // namespace
264
265#endif