ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 8 | #ifndef SKIASL_MEMORYLAYOUT |
| 9 | #define SKIASL_MEMORYLAYOUT |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/ir/SkSLType.h" |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 12 | |
| 13 | namespace SkSL { |
| 14 | |
| 15 | class MemoryLayout { |
| 16 | public: |
| 17 | enum Standard { |
| 18 | k140_Standard, |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 19 | k430_Standard, |
| 20 | kMetal_Standard |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 21 | }; |
| 22 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 23 | MemoryLayout(Standard std) |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 24 | : fStd(std) {} |
| 25 | |
| 26 | static size_t vector_alignment(size_t componentSize, int columns) { |
| 27 | return componentSize * (columns + columns % 2); |
| 28 | } |
| 29 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 30 | /** |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 31 | * Rounds up to the nearest multiple of 16 if in std140, otherwise returns the parameter |
| 32 | * unchanged (std140 requires various things to be rounded up to the nearest multiple of 16, |
| 33 | * std430 does not). |
| 34 | */ |
| 35 | size_t roundUpIfNeeded(size_t raw) const { |
| 36 | switch (fStd) { |
| 37 | case k140_Standard: return (raw + 15) & ~15; |
| 38 | case k430_Standard: return raw; |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 39 | case kMetal_Standard: return raw; |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 40 | } |
| 41 | ABORT("unreachable"); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns a type's required alignment when used as a standalone variable. |
| 46 | */ |
| 47 | size_t alignment(const Type& type) const { |
| 48 | // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 49 | switch (type.typeKind()) { |
| 50 | case Type::TypeKind::kScalar: |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame^] | 51 | case Type::TypeKind::kEnum: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 52 | return this->size(type); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 53 | case Type::TypeKind::kVector: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 54 | return vector_alignment(this->size(type.componentType()), type.columns()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 55 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 56 | return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()), |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 57 | type.rows())); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 58 | case Type::TypeKind::kArray: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 59 | return this->roundUpIfNeeded(this->alignment(type.componentType())); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 60 | case Type::TypeKind::kStruct: { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 61 | size_t result = 0; |
| 62 | for (const auto& f : type.fields()) { |
| 63 | size_t alignment = this->alignment(*f.fType); |
| 64 | if (alignment > result) { |
| 65 | result = alignment; |
| 66 | } |
| 67 | } |
| 68 | return this->roundUpIfNeeded(result); |
| 69 | } |
| 70 | default: |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 71 | ABORT("cannot determine size of type %s", String(type.name()).c_str()); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * For matrices and arrays, returns the number of bytes from the start of one entry (row, in |
| 77 | * the case of matrices) to the start of the next. |
| 78 | */ |
| 79 | size_t stride(const Type& type) const { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 80 | switch (type.typeKind()) { |
| 81 | case Type::TypeKind::kMatrix: { |
Ethan Nicholas | 4a34197 | 2018-07-17 15:38:03 -0400 | [diff] [blame] | 82 | size_t base = vector_alignment(this->size(type.componentType()), type.rows()); |
| 83 | return this->roundUpIfNeeded(base); |
| 84 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 85 | case Type::TypeKind::kArray: { |
John Stiles | 0ebc69c | 2020-04-17 21:43:10 -0700 | [diff] [blame] | 86 | int stride = this->size(type.componentType()); |
| 87 | if (stride > 0) { |
| 88 | int align = this->alignment(type.componentType()); |
| 89 | stride += align - 1; |
| 90 | stride -= stride % align; |
| 91 | stride = this->roundUpIfNeeded(stride); |
| 92 | } |
| 93 | return stride; |
Ethan Nicholas | 4a34197 | 2018-07-17 15:38:03 -0400 | [diff] [blame] | 94 | } |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 95 | default: |
| 96 | ABORT("type does not have a stride"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the size of a type in bytes. |
| 102 | */ |
| 103 | size_t size(const Type& type) const { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 104 | switch (type.typeKind()) { |
| 105 | case Type::TypeKind::kScalar: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 106 | if (type.name() == "bool") { |
| 107 | return 1; |
| 108 | } |
| 109 | // FIXME need to take precision into account, once we figure out how we want to |
| 110 | // handle it... |
| 111 | return 4; |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame^] | 112 | case Type::TypeKind::kEnum: |
| 113 | return 4; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 114 | case Type::TypeKind::kVector: |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 115 | if (fStd == kMetal_Standard && type.columns() == 3) { |
| 116 | return 4 * this->size(type.componentType()); |
| 117 | } |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 118 | return type.columns() * this->size(type.componentType()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 119 | case Type::TypeKind::kMatrix: // fall through |
| 120 | case Type::TypeKind::kArray: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 121 | return type.columns() * this->stride(type); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 122 | case Type::TypeKind::kStruct: { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 123 | size_t total = 0; |
| 124 | for (const auto& f : type.fields()) { |
| 125 | size_t alignment = this->alignment(*f.fType); |
| 126 | if (total % alignment != 0) { |
| 127 | total += alignment - total % alignment; |
| 128 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 129 | SkASSERT(total % alignment == 0); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 130 | total += this->size(*f.fType); |
| 131 | } |
| 132 | size_t alignment = this->alignment(type); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 133 | SkASSERT(!type.fields().size() || |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 134 | (0 == alignment % this->alignment(*type.fields()[0].fType))); |
| 135 | return (total + alignment - 1) & ~(alignment - 1); |
| 136 | } |
| 137 | default: |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 138 | ABORT("cannot determine size of type %s", String(type.name()).c_str()); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame^] | 142 | /** |
| 143 | * Not all types are compatible with memory layout. |
| 144 | */ |
| 145 | size_t layoutIsSupported(const Type& type) const { |
| 146 | switch (type.typeKind()) { |
| 147 | case Type::TypeKind::kScalar: |
| 148 | case Type::TypeKind::kEnum: |
| 149 | case Type::TypeKind::kVector: |
| 150 | case Type::TypeKind::kMatrix: |
| 151 | case Type::TypeKind::kArray: |
| 152 | case Type::TypeKind::kStruct: |
| 153 | return true; |
| 154 | |
| 155 | default: |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 160 | const Standard fStd; |
| 161 | }; |
| 162 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 163 | } // namespace SkSL |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 164 | |
| 165 | #endif |