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 static size_t vector_alignment(size_t componentSize, int columns) { |
| 238 return componentSize * (columns + columns % 2); |
| 239 } |
| 240 |
| 241 /** |
| 242 * Returns the type's required alignment (when putting this type into a stru
ct, the offset must |
| 243 * be a multiple of the alignment). |
| 244 */ |
| 245 size_t alignment() const { |
| 246 // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout |
| 247 switch (fTypeKind) { |
| 248 case kScalar_Kind: |
| 249 return this->size(); |
| 250 case kVector_Kind: |
| 251 return vector_alignment(fComponentType->size(), fColumns); |
| 252 case kMatrix_Kind: |
| 253 return (vector_alignment(fComponentType->size(), fRows) + 15) &
~15; |
| 254 case kArray_Kind: |
| 255 // round up to next multiple of 16 |
| 256 return (fComponentType->alignment() + 15) & ~15; |
| 257 case kStruct_Kind: { |
| 258 size_t result = 16; |
| 259 for (size_t i = 0; i < fFields.size(); i++) { |
| 260 size_t alignment = fFields[i].fType->alignment(); |
| 261 if (alignment > result) { |
| 262 result = alignment; |
| 263 } |
| 264 } |
| 265 } |
| 266 default: |
| 267 ABORT(("cannot determine size of type " + fName).c_str()); |
| 268 } |
| 269 } |
| 270 |
| 271 /** |
| 272 * For matrices and arrays, returns the number of bytes from the start of on
e entry (row, in |
| 273 * the case of matrices) to the start of the next. |
| 274 */ |
| 275 size_t stride() const { |
| 276 switch (fTypeKind) { |
| 277 case kMatrix_Kind: // fall through |
| 278 case kArray_Kind: |
| 279 return this->alignment(); |
| 280 default: |
| 281 ABORT("type does not have a stride"); |
| 282 } |
| 283 } |
| 284 |
| 285 /** |
| 286 * Returns the size of this type in bytes. |
| 287 */ |
| 288 size_t size() const { |
| 289 switch (fTypeKind) { |
| 290 case kScalar_Kind: |
| 291 // FIXME need to take precision into account, once we figure out
how we want to |
| 292 // handle it... |
| 293 return 4; |
| 294 case kVector_Kind: |
| 295 return fColumns * fComponentType->size(); |
| 296 case kMatrix_Kind: |
| 297 return vector_alignment(fComponentType->size(), fRows) * fColumn
s; |
| 298 case kArray_Kind: |
| 299 return fColumns * this->stride(); |
| 300 case kStruct_Kind: { |
| 301 size_t total = 0; |
| 302 for (size_t i = 0; i < fFields.size(); i++) { |
| 303 size_t alignment = fFields[i].fType->alignment(); |
| 304 if (total % alignment != 0) { |
| 305 total += alignment - total % alignment; |
| 306 } |
| 307 ASSERT(false); |
| 308 ASSERT(total % alignment == 0); |
| 309 total += fFields[i].fType->size(); |
| 310 } |
| 311 return total; |
| 312 } |
| 313 default: |
| 314 ABORT(("cannot determine size of type " + fName).c_str()); |
| 315 } |
| 316 } |
| 317 |
| 318 /** |
| 319 * Returns the corresponding vector or matrix type with the specified number
of columns and |
| 320 * rows. |
| 321 */ |
| 322 std::shared_ptr<Type> toCompound(int columns, int rows); |
| 323 |
| 324 private: |
| 325 typedef Symbol INHERITED; |
| 326 |
| 327 const Kind fTypeKind; |
| 328 const bool fIsNumber = false; |
| 329 const std::shared_ptr<Type> fComponentType = nullptr; |
| 330 const std::vector<std::shared_ptr<Type>> fCoercibleTypes = { }; |
| 331 const int fColumns = -1; |
| 332 const int fRows = -1; |
| 333 const std::vector<Field> fFields = { }; |
| 334 const SpvDim_ fDimensions = SpvDim1D; |
| 335 const bool fIsDepth = false; |
| 336 const bool fIsArrayed = false; |
| 337 const bool fIsMultisampled = false; |
| 338 const bool fIsSampled = false; |
| 339 }; |
| 340 |
| 341 extern const std::shared_ptr<Type> kVoid_Type; |
| 342 |
| 343 extern const std::shared_ptr<Type> kFloat_Type; |
| 344 extern const std::shared_ptr<Type> kVec2_Type; |
| 345 extern const std::shared_ptr<Type> kVec3_Type; |
| 346 extern const std::shared_ptr<Type> kVec4_Type; |
| 347 extern const std::shared_ptr<Type> kDouble_Type; |
| 348 extern const std::shared_ptr<Type> kDVec2_Type; |
| 349 extern const std::shared_ptr<Type> kDVec3_Type; |
| 350 extern const std::shared_ptr<Type> kDVec4_Type; |
| 351 extern const std::shared_ptr<Type> kInt_Type; |
| 352 extern const std::shared_ptr<Type> kIVec2_Type; |
| 353 extern const std::shared_ptr<Type> kIVec3_Type; |
| 354 extern const std::shared_ptr<Type> kIVec4_Type; |
| 355 extern const std::shared_ptr<Type> kUInt_Type; |
| 356 extern const std::shared_ptr<Type> kUVec2_Type; |
| 357 extern const std::shared_ptr<Type> kUVec3_Type; |
| 358 extern const std::shared_ptr<Type> kUVec4_Type; |
| 359 extern const std::shared_ptr<Type> kBool_Type; |
| 360 extern const std::shared_ptr<Type> kBVec2_Type; |
| 361 extern const std::shared_ptr<Type> kBVec3_Type; |
| 362 extern const std::shared_ptr<Type> kBVec4_Type; |
| 363 |
| 364 extern const std::shared_ptr<Type> kMat2x2_Type; |
| 365 extern const std::shared_ptr<Type> kMat2x3_Type; |
| 366 extern const std::shared_ptr<Type> kMat2x4_Type; |
| 367 extern const std::shared_ptr<Type> kMat3x2_Type; |
| 368 extern const std::shared_ptr<Type> kMat3x3_Type; |
| 369 extern const std::shared_ptr<Type> kMat3x4_Type; |
| 370 extern const std::shared_ptr<Type> kMat4x2_Type; |
| 371 extern const std::shared_ptr<Type> kMat4x3_Type; |
| 372 extern const std::shared_ptr<Type> kMat4x4_Type; |
| 373 |
| 374 extern const std::shared_ptr<Type> kDMat2x2_Type; |
| 375 extern const std::shared_ptr<Type> kDMat2x3_Type; |
| 376 extern const std::shared_ptr<Type> kDMat2x4_Type; |
| 377 extern const std::shared_ptr<Type> kDMat3x2_Type; |
| 378 extern const std::shared_ptr<Type> kDMat3x3_Type; |
| 379 extern const std::shared_ptr<Type> kDMat3x4_Type; |
| 380 extern const std::shared_ptr<Type> kDMat4x2_Type; |
| 381 extern const std::shared_ptr<Type> kDMat4x3_Type; |
| 382 extern const std::shared_ptr<Type> kDMat4x4_Type; |
| 383 |
| 384 extern const std::shared_ptr<Type> kSampler1D_Type; |
| 385 extern const std::shared_ptr<Type> kSampler2D_Type; |
| 386 extern const std::shared_ptr<Type> kSampler3D_Type; |
| 387 extern const std::shared_ptr<Type> kSamplerCube_Type; |
| 388 extern const std::shared_ptr<Type> kSampler2DRect_Type; |
| 389 extern const std::shared_ptr<Type> kSampler1DArray_Type; |
| 390 extern const std::shared_ptr<Type> kSampler2DArray_Type; |
| 391 extern const std::shared_ptr<Type> kSamplerCubeArray_Type; |
| 392 extern const std::shared_ptr<Type> kSamplerBuffer_Type; |
| 393 extern const std::shared_ptr<Type> kSampler2DMS_Type; |
| 394 extern const std::shared_ptr<Type> kSampler2DMSArray_Type; |
| 395 |
| 396 extern const std::shared_ptr<Type> kGSampler1D_Type; |
| 397 extern const std::shared_ptr<Type> kGSampler2D_Type; |
| 398 extern const std::shared_ptr<Type> kGSampler3D_Type; |
| 399 extern const std::shared_ptr<Type> kGSamplerCube_Type; |
| 400 extern const std::shared_ptr<Type> kGSampler2DRect_Type; |
| 401 extern const std::shared_ptr<Type> kGSampler1DArray_Type; |
| 402 extern const std::shared_ptr<Type> kGSampler2DArray_Type; |
| 403 extern const std::shared_ptr<Type> kGSamplerCubeArray_Type; |
| 404 extern const std::shared_ptr<Type> kGSamplerBuffer_Type; |
| 405 extern const std::shared_ptr<Type> kGSampler2DMS_Type; |
| 406 extern const std::shared_ptr<Type> kGSampler2DMSArray_Type; |
| 407 |
| 408 extern const std::shared_ptr<Type> kSampler1DShadow_Type; |
| 409 extern const std::shared_ptr<Type> kSampler2DShadow_Type; |
| 410 extern const std::shared_ptr<Type> kSamplerCubeShadow_Type; |
| 411 extern const std::shared_ptr<Type> kSampler2DRectShadow_Type; |
| 412 extern const std::shared_ptr<Type> kSampler1DArrayShadow_Type; |
| 413 extern const std::shared_ptr<Type> kSampler2DArrayShadow_Type; |
| 414 extern const std::shared_ptr<Type> kSamplerCubeArrayShadow_Type; |
| 415 extern const std::shared_ptr<Type> kGSampler2DArrayShadow_Type; |
| 416 extern const std::shared_ptr<Type> kGSamplerCubeArrayShadow_Type; |
| 417 |
| 418 extern const std::shared_ptr<Type> kGenType_Type; |
| 419 extern const std::shared_ptr<Type> kGenDType_Type; |
| 420 extern const std::shared_ptr<Type> kGenIType_Type; |
| 421 extern const std::shared_ptr<Type> kGenUType_Type; |
| 422 extern const std::shared_ptr<Type> kGenBType_Type; |
| 423 extern const std::shared_ptr<Type> kMat_Type; |
| 424 extern const std::shared_ptr<Type> kVec_Type; |
| 425 extern const std::shared_ptr<Type> kGVec_Type; |
| 426 extern const std::shared_ptr<Type> kGVec2_Type; |
| 427 extern const std::shared_ptr<Type> kGVec3_Type; |
| 428 extern const std::shared_ptr<Type> kGVec4_Type; |
| 429 extern const std::shared_ptr<Type> kDVec_Type; |
| 430 extern const std::shared_ptr<Type> kIVec_Type; |
| 431 extern const std::shared_ptr<Type> kUVec_Type; |
| 432 extern const std::shared_ptr<Type> kBVec_Type; |
| 433 |
| 434 extern const std::shared_ptr<Type> kInvalid_Type; |
| 435 |
| 436 } // namespace |
| 437 |
| 438 #endif |
OLD | NEW |