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