OLD | NEW |
---|---|
(Empty) | |
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 SKIASL_TYPE | |
9 #define SKIASL_TYPE | |
10 | |
11 #include "SkSLModifiers.h" | |
12 #include "SkSLSymbol.h" | |
13 #include "../SkSLPosition.h" | |
14 #include "../SkSLUtil.h" | |
15 #include "../spirv.h" | |
16 #include <vector> | |
17 #include <memory> | |
18 | |
19 namespace SkSL { | |
20 | |
21 /** | |
22 * Represents a type, such as int or vec4. | |
23 */ | |
24 class Type : public Symbol { | |
25 public: | |
26 struct Field { | |
27 Field(Modifiers modifiers, std::string name, std::shared_ptr<Type> type) | |
28 : fModifiers(modifiers) | |
29 , fName(std::move(name)) | |
30 , fType(std::move(type)) {} | |
31 | |
32 const std::string description() { | |
33 return fType->description() + " " + fName + ";"; | |
34 } | |
35 | |
36 const Modifiers fModifiers; | |
37 const std::string fName; | |
38 const std::shared_ptr<Type> fType; | |
39 }; | |
40 | |
41 enum Kind { | |
42 kScalar_Kind, | |
43 kVector_Kind, | |
44 kMatrix_Kind, | |
45 kArray_Kind, | |
46 kStruct_Kind, | |
47 kGeneric_Kind, | |
48 kSampler_Kind, | |
49 kOther_Kind | |
50 }; | |
51 | |
52 // Create an "other" (special) type with the given name. These types cannot be directly | |
53 // referenced from user code. | |
54 Type(std::string name) | |
55 : INHERITED(Position(), kType_Kind, std::move(name)) | |
56 , fTypeKind(kOther_Kind) {} | |
57 | |
58 // Create a generic type which maps to the listed types. | |
59 Type(std::string name, std::vector<std::shared_ptr<Type>> types) | |
60 : INHERITED(Position(), kType_Kind, std::move(name)) | |
61 , fTypeKind(kGeneric_Kind) | |
62 , fCoercibleTypes(std::move(types)) { | |
63 ASSERT(fCoercibleTypes.size() == 4); | |
64 } | |
65 | |
66 // Create a struct type with the given fields. | |
67 Type(std::string name, std::vector<Field> fields) | |
68 : INHERITED(Position(), kType_Kind, std::move(name)) | |
69 , fTypeKind(kStruct_Kind) | |
70 , fFields(std::move(fields)) {} | |
71 | |
72 // Create a scalar type. | |
73 Type(std::string name, bool isNumber) | |
74 : INHERITED(Position(), kType_Kind, std::move(name)) | |
75 , fTypeKind(kScalar_Kind) | |
76 , fIsNumber(isNumber) | |
77 , fColumns(1) | |
78 , fRows(1) {} | |
79 | |
80 // Create a scalar type which can be coerced to the listed types. | |
81 Type(std::string name, bool isNumber, std::vector<std::shared_ptr<Type>> coe rcibleTypes) | |
82 : INHERITED(Position(), kType_Kind, std::move(name)) | |
83 , fTypeKind(kScalar_Kind) | |
84 , fIsNumber(isNumber) | |
85 , fCoercibleTypes(std::move(coercibleTypes)) | |
86 , fColumns(1) | |
87 , fRows(1) {} | |
88 | |
89 // Create a vector type. | |
90 Type(std::string name, std::shared_ptr<Type> componentType, int columns) | |
91 : Type(name, kVector_Kind, componentType, columns) {} | |
92 | |
93 // Create a vector or array type. | |
94 Type(std::string name, Kind kind, std::shared_ptr<Type> componentType, int c olumns) | |
95 : INHERITED(Position(), kType_Kind, std::move(name)) | |
96 , fTypeKind(kind) | |
97 , fComponentType(std::move(componentType)) | |
98 , fColumns(columns) | |
99 , fRows(1) | |
100 , fDimensions(SpvDim1D) {} | |
101 | |
102 // Create a matrix type. | |
103 Type(std::string name, std::shared_ptr<Type> componentType, int columns, int rows) | |
104 : INHERITED(Position(), kType_Kind, std::move(name)) | |
105 , fTypeKind(kMatrix_Kind) | |
106 , fComponentType(std::move(componentType)) | |
107 , fColumns(columns) | |
108 , fRows(rows) | |
109 , fDimensions(SpvDim1D) {} | |
110 | |
111 // Create a sampler type. | |
112 Type(std::string name, SpvDim_ dimensions, bool isDepth, bool isArrayed, boo l isMultisampled, | |
113 bool isSampled) | |
114 : INHERITED(Position(), kType_Kind, std::move(name)) | |
115 , fTypeKind(kSampler_Kind) | |
116 , fDimensions(dimensions) | |
117 , fIsDepth(isDepth) | |
118 , fIsArrayed(isArrayed) | |
119 , fIsMultisampled(isMultisampled) | |
120 , fIsSampled(isSampled) {} | |
121 | |
122 std::string name() const { | |
123 return fName; | |
124 } | |
125 | |
126 std::string description() const override { | |
127 return fName; | |
128 } | |
129 | |
130 bool operator==(const Type& other) const { | |
131 return fName == other.fName; | |
132 } | |
133 | |
134 bool operator!=(const Type& other) const { | |
135 return fName != other.fName; | |
136 } | |
137 | |
138 /** | |
139 * Returns the category (scalar, vector, matrix, etc.) of this type. | |
140 */ | |
141 Kind kind() const { | |
142 return fTypeKind; | |
143 } | |
144 | |
145 /** | |
146 * Returns true if this is a numeric scalar type. | |
147 */ | |
148 bool isNumber() const { | |
149 return fIsNumber; | |
150 } | |
151 | |
152 /** | |
153 * Returns true if an instance of this type can be freely coerced (implicitl y converted) to | |
154 * another type. | |
155 */ | |
156 bool canCoerceTo(std::shared_ptr<Type> other) const { | |
157 int cost; | |
158 return determineCoercionCost(other, &cost); | |
159 } | |
160 | |
161 /** | |
162 * Determines the "cost" of coercing (implicitly converting) this type to an other type. The cost | |
163 * is a number with no particular meaning other than that lower costs are pr eferable to higher | |
164 * costs. Returns true if a conversion is possible, false otherwise. The val ue of the out | |
165 * parameter is undefined if false is returned. | |
166 */ | |
167 bool determineCoercionCost(std::shared_ptr<Type> other, int* outCost) const; | |
168 | |
169 /** | |
170 * For matrices and vectors, returns the type of individual cells (e.g. mat2 has a component | |
171 * type of kFloat_Type). For all other types, causes an assertion failure. | |
172 */ | |
173 std::shared_ptr<Type> componentType() const { | |
174 ASSERT(fComponentType); | |
175 return fComponentType; | |
176 } | |
177 | |
178 /** | |
179 * For matrices and vectors, returns the number of columns (e.g. both mat3 a nd vec3 return 3). | |
180 * For scalars, returns 1. For arrays, returns either the size of the array (if known) or -1. | |
181 * For all other types, causes an assertion failure. | |
182 */ | |
183 int columns() const { | |
184 ASSERT(fTypeKind == kScalar_Kind || fTypeKind == kVector_Kind || | |
185 fTypeKind == kMatrix_Kind || fTypeKind == kArray_Kind); | |
186 return fColumns; | |
187 } | |
188 | |
189 /** | |
190 * For matrices, returns the number of rows (e.g. mat2x4 returns 4). For vec tors and scalars, | |
191 * returns 1. For all other types, causes an assertion failure. | |
192 */ | |
193 int rows() const { | |
194 ASSERT(fRows > 0); | |
195 return fRows; | |
196 } | |
197 | |
198 std::vector<Field> fields() const { | |
199 ASSERT(fTypeKind == kStruct_Kind); | |
200 return fFields; | |
201 } | |
202 | |
203 /** | |
204 * For generic types, returns the types that this generic type can substitut e for. For other | |
205 * types, returns a list of other types that this type can be coerced into. | |
206 */ | |
207 std::vector<std::shared_ptr<Type>> coercibleTypes() const { | |
208 ASSERT(fCoercibleTypes.size() > 0); | |
209 return fCoercibleTypes; | |
210 } | |
211 | |
212 int dimensions() const { | |
213 ASSERT(fTypeKind == kSampler_Kind); | |
214 return fDimensions; | |
215 } | |
216 | |
217 bool isDepth() const { | |
218 ASSERT(fTypeKind == kSampler_Kind); | |
219 return fIsDepth; | |
220 } | |
221 | |
222 bool isArrayed() const { | |
223 ASSERT(fTypeKind == kSampler_Kind); | |
224 return fIsArrayed; | |
225 } | |
226 | |
227 bool isMultisampled() const { | |
228 ASSERT(fTypeKind == kSampler_Kind); | |
229 return fIsMultisampled; | |
230 } | |
231 | |
232 bool isSampled() const { | |
233 ASSERT(fTypeKind == kSampler_Kind); | |
234 return fIsSampled; | |
235 } | |
236 | |
237 /** | |
238 * Returns the type's required alignment (when putting this type into a stru ct, the offset must | |
239 * be a multiple of the alignment). | |
240 */ | |
241 size_t alignment() const { | |
242 // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout | |
243 switch (fTypeKind) { | |
244 case kScalar_Kind: | |
245 return this->size(); | |
246 case kVector_Kind: | |
247 return (fColumns + fColumns % 2) * fComponentType->size(); | |
egdaniel
2016/06/30 19:24:15
nit: this logic of (x + x%2) * size is repeated in
| |
248 case kMatrix_Kind: | |
249 return ((fRows + fRows % 2) * fComponentType->size() + 15) & ~15 ; | |
250 case kArray_Kind: | |
251 // round up to next multiple of 16 | |
252 return (fComponentType->alignment() + 15) & ~15; | |
253 case kStruct_Kind: { | |
254 size_t result = 16; | |
255 for (size_t i = 0; i < fFields.size(); i++) { | |
256 size_t alignment = fFields[i].fType->alignment(); | |
257 if (alignment > result) { | |
258 result = alignment; | |
259 } | |
260 } | |
261 } | |
262 default: | |
263 ABORT(("cannot determine size of type " + fName).c_str()); | |
264 } | |
265 } | |
266 | |
267 /** | |
268 * For matrices and arrays, returns the number of bytes from the start of on e entry (row, in | |
269 * the case of matrices) to the start of the next. | |
270 */ | |
271 size_t stride() const { | |
272 switch (fTypeKind) { | |
273 case kMatrix_Kind: // fall through | |
274 case kArray_Kind: | |
275 return this->alignment(); | |
276 default: | |
277 ABORT("type does not have a stride"); | |
278 } | |
279 } | |
280 | |
281 /** | |
282 * Returns the size of this type in bytes. | |
283 */ | |
284 size_t size() const { | |
285 switch (fTypeKind) { | |
286 case kScalar_Kind: | |
287 // FIXME need to take precision into account, once we figure out how we want to | |
288 // handle it... | |
289 return 4; | |
290 case kVector_Kind: | |
291 return fColumns * fComponentType->size(); | |
292 case kMatrix_Kind: | |
293 return (fRows + fRows % 2) * fColumns * fComponentType->size(); | |
294 case kArray_Kind: | |
295 return fColumns * this->stride(); | |
296 case kStruct_Kind: { | |
297 size_t total = 0; | |
298 for (size_t i = 0; i < fFields.size(); i++) { | |
299 size_t alignment = fFields[i].fType->alignment(); | |
300 if (total % alignment != 0) { | |
301 total += alignment - total % alignment; | |
302 } | |
303 ASSERT(false); | |
304 ASSERT(total % alignment == 0); | |
305 total += fFields[i].fType->size(); | |
306 } | |
307 return total; | |
308 } | |
309 default: | |
310 ABORT(("cannot determine size of type " + fName).c_str()); | |
311 } | |
312 } | |
313 | |
314 /** | |
315 * Returns the corresponding vector or matrix type with the specified number of columns and | |
316 * rows. | |
317 */ | |
318 std::shared_ptr<Type> toCompound(int columns, int rows); | |
319 | |
320 private: | |
321 typedef Symbol INHERITED; | |
322 | |
323 const Kind fTypeKind; | |
324 const bool fIsNumber = false; | |
325 const std::shared_ptr<Type> fComponentType = nullptr; | |
326 const std::vector<std::shared_ptr<Type>> fCoercibleTypes = { }; | |
327 const int fColumns = -1; | |
328 const int fRows = -1; | |
329 const std::vector<Field> fFields = { }; | |
330 const SpvDim_ fDimensions = SpvDim1D; | |
331 const bool fIsDepth = false; | |
332 const bool fIsArrayed = false; | |
333 const bool fIsMultisampled = false; | |
334 const bool fIsSampled = false; | |
335 }; | |
336 | |
337 extern const std::shared_ptr<Type> kVoid_Type; | |
338 | |
339 extern const std::shared_ptr<Type> kFloat_Type; | |
340 extern const std::shared_ptr<Type> kVec2_Type; | |
341 extern const std::shared_ptr<Type> kVec3_Type; | |
342 extern const std::shared_ptr<Type> kVec4_Type; | |
343 extern const std::shared_ptr<Type> kDouble_Type; | |
344 extern const std::shared_ptr<Type> kDVec2_Type; | |
345 extern const std::shared_ptr<Type> kDVec3_Type; | |
346 extern const std::shared_ptr<Type> kDVec4_Type; | |
347 extern const std::shared_ptr<Type> kInt_Type; | |
348 extern const std::shared_ptr<Type> kIVec2_Type; | |
349 extern const std::shared_ptr<Type> kIVec3_Type; | |
350 extern const std::shared_ptr<Type> kIVec4_Type; | |
351 extern const std::shared_ptr<Type> kUInt_Type; | |
352 extern const std::shared_ptr<Type> kUVec2_Type; | |
353 extern const std::shared_ptr<Type> kUVec3_Type; | |
354 extern const std::shared_ptr<Type> kUVec4_Type; | |
355 extern const std::shared_ptr<Type> kBool_Type; | |
356 extern const std::shared_ptr<Type> kBVec2_Type; | |
357 extern const std::shared_ptr<Type> kBVec3_Type; | |
358 extern const std::shared_ptr<Type> kBVec4_Type; | |
359 | |
360 extern const std::shared_ptr<Type> kMat2x2_Type; | |
361 extern const std::shared_ptr<Type> kMat2x3_Type; | |
362 extern const std::shared_ptr<Type> kMat2x4_Type; | |
363 extern const std::shared_ptr<Type> kMat3x2_Type; | |
364 extern const std::shared_ptr<Type> kMat3x3_Type; | |
365 extern const std::shared_ptr<Type> kMat3x4_Type; | |
366 extern const std::shared_ptr<Type> kMat4x2_Type; | |
367 extern const std::shared_ptr<Type> kMat4x3_Type; | |
368 extern const std::shared_ptr<Type> kMat4x4_Type; | |
369 | |
370 extern const std::shared_ptr<Type> kDMat2x2_Type; | |
371 extern const std::shared_ptr<Type> kDMat2x3_Type; | |
372 extern const std::shared_ptr<Type> kDMat2x4_Type; | |
373 extern const std::shared_ptr<Type> kDMat3x2_Type; | |
374 extern const std::shared_ptr<Type> kDMat3x3_Type; | |
375 extern const std::shared_ptr<Type> kDMat3x4_Type; | |
376 extern const std::shared_ptr<Type> kDMat4x2_Type; | |
377 extern const std::shared_ptr<Type> kDMat4x3_Type; | |
378 extern const std::shared_ptr<Type> kDMat4x4_Type; | |
379 | |
380 extern const std::shared_ptr<Type> kSampler1D_Type; | |
381 extern const std::shared_ptr<Type> kSampler2D_Type; | |
382 extern const std::shared_ptr<Type> kSampler3D_Type; | |
383 extern const std::shared_ptr<Type> kSamplerCube_Type; | |
384 extern const std::shared_ptr<Type> kSampler2DRect_Type; | |
385 extern const std::shared_ptr<Type> kSampler1DArray_Type; | |
386 extern const std::shared_ptr<Type> kSampler2DArray_Type; | |
387 extern const std::shared_ptr<Type> kSamplerCubeArray_Type; | |
388 extern const std::shared_ptr<Type> kSamplerBuffer_Type; | |
389 extern const std::shared_ptr<Type> kSampler2DMS_Type; | |
390 extern const std::shared_ptr<Type> kSampler2DMSArray_Type; | |
391 | |
392 extern const std::shared_ptr<Type> kGSampler1D_Type; | |
393 extern const std::shared_ptr<Type> kGSampler2D_Type; | |
394 extern const std::shared_ptr<Type> kGSampler3D_Type; | |
395 extern const std::shared_ptr<Type> kGSamplerCube_Type; | |
396 extern const std::shared_ptr<Type> kGSampler2DRect_Type; | |
397 extern const std::shared_ptr<Type> kGSampler1DArray_Type; | |
398 extern const std::shared_ptr<Type> kGSampler2DArray_Type; | |
399 extern const std::shared_ptr<Type> kGSamplerCubeArray_Type; | |
400 extern const std::shared_ptr<Type> kGSamplerBuffer_Type; | |
401 extern const std::shared_ptr<Type> kGSampler2DMS_Type; | |
402 extern const std::shared_ptr<Type> kGSampler2DMSArray_Type; | |
403 | |
404 extern const std::shared_ptr<Type> kSampler1DShadow_Type; | |
405 extern const std::shared_ptr<Type> kSampler2DShadow_Type; | |
406 extern const std::shared_ptr<Type> kSamplerCubeShadow_Type; | |
407 extern const std::shared_ptr<Type> kSampler2DRectShadow_Type; | |
408 extern const std::shared_ptr<Type> kSampler1DArrayShadow_Type; | |
409 extern const std::shared_ptr<Type> kSampler2DArrayShadow_Type; | |
410 extern const std::shared_ptr<Type> kSamplerCubeArrayShadow_Type; | |
411 extern const std::shared_ptr<Type> kGSampler2DArrayShadow_Type; | |
412 extern const std::shared_ptr<Type> kGSamplerCubeArrayShadow_Type; | |
413 | |
414 extern const std::shared_ptr<Type> kGenType_Type; | |
415 extern const std::shared_ptr<Type> kGenDType_Type; | |
416 extern const std::shared_ptr<Type> kGenIType_Type; | |
417 extern const std::shared_ptr<Type> kGenUType_Type; | |
418 extern const std::shared_ptr<Type> kGenBType_Type; | |
419 extern const std::shared_ptr<Type> kMat_Type; | |
420 extern const std::shared_ptr<Type> kVec_Type; | |
421 extern const std::shared_ptr<Type> kGVec_Type; | |
422 extern const std::shared_ptr<Type> kGVec2_Type; | |
423 extern const std::shared_ptr<Type> kGVec3_Type; | |
424 extern const std::shared_ptr<Type> kGVec4_Type; | |
425 extern const std::shared_ptr<Type> kDVec_Type; | |
426 extern const std::shared_ptr<Type> kIVec_Type; | |
427 extern const std::shared_ptr<Type> kUVec_Type; | |
428 extern const std::shared_ptr<Type> kBVec_Type; | |
429 | |
430 extern const std::shared_ptr<Type> kInvalid_Type; | |
431 | |
432 } // namespace | |
433 | |
434 #endif | |
OLD | NEW |