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