blob: 562f59db2d71253127f0690fcf73d97d7c5e7aa8 [file] [log] [blame]
ethannicholas8ac838d2016-11-22 08:39:36 -08001/*
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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholas8ac838d2016-11-22 08:39:36 -08008#ifndef SKIASL_MEMORYLAYOUT
9#define SKIASL_MEMORYLAYOUT
10
John Stiles21f5f452020-11-30 09:57:59 -050011#include <algorithm>
12
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/sksl/ir/SkSLType.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080014
15namespace SkSL {
16
17class MemoryLayout {
18public:
19 enum Standard {
20 k140_Standard,
Timothy Liang609fbe32018-08-10 16:40:49 -040021 k430_Standard,
22 kMetal_Standard
ethannicholas8ac838d2016-11-22 08:39:36 -080023 };
24
Ethan Nicholas0df1b042017-03-31 13:56:23 -040025 MemoryLayout(Standard std)
ethannicholas8ac838d2016-11-22 08:39:36 -080026 : fStd(std) {}
27
28 static size_t vector_alignment(size_t componentSize, int columns) {
29 return componentSize * (columns + columns % 2);
30 }
31
Ethan Nicholas0df1b042017-03-31 13:56:23 -040032 /**
ethannicholas8ac838d2016-11-22 08:39:36 -080033 * 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 Liang609fbe32018-08-10 16:40:49 -040041 case kMetal_Standard: return raw;
ethannicholas8ac838d2016-11-22 08:39:36 -080042 }
43 ABORT("unreachable");
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 Nicholase6592142020-09-08 10:22:09 -040051 switch (type.typeKind()) {
52 case Type::TypeKind::kScalar:
John Stiles0023c0c2020-11-16 13:32:18 -050053 case Type::TypeKind::kEnum:
ethannicholas8ac838d2016-11-22 08:39:36 -080054 return this->size(type);
Ethan Nicholase6592142020-09-08 10:22:09 -040055 case Type::TypeKind::kVector:
ethannicholas8ac838d2016-11-22 08:39:36 -080056 return vector_alignment(this->size(type.componentType()), type.columns());
Ethan Nicholase6592142020-09-08 10:22:09 -040057 case Type::TypeKind::kMatrix:
Ethan Nicholas0df1b042017-03-31 13:56:23 -040058 return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()),
ethannicholas8ac838d2016-11-22 08:39:36 -080059 type.rows()));
Ethan Nicholase6592142020-09-08 10:22:09 -040060 case Type::TypeKind::kArray:
ethannicholas8ac838d2016-11-22 08:39:36 -080061 return this->roundUpIfNeeded(this->alignment(type.componentType()));
Ethan Nicholase6592142020-09-08 10:22:09 -040062 case Type::TypeKind::kStruct: {
ethannicholas8ac838d2016-11-22 08:39:36 -080063 size_t result = 0;
64 for (const auto& f : type.fields()) {
65 size_t alignment = this->alignment(*f.fType);
66 if (alignment > result) {
67 result = alignment;
68 }
69 }
70 return this->roundUpIfNeeded(result);
71 }
72 default:
Ethan Nicholase2c49992020-10-05 11:49:11 -040073 ABORT("cannot determine size of type %s", String(type.name()).c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -080074 }
75 }
76
77 /**
78 * For matrices and arrays, returns the number of bytes from the start of one entry (row, in
79 * the case of matrices) to the start of the next.
80 */
81 size_t stride(const Type& type) const {
Ethan Nicholase6592142020-09-08 10:22:09 -040082 switch (type.typeKind()) {
83 case Type::TypeKind::kMatrix: {
Ethan Nicholas4a341972018-07-17 15:38:03 -040084 size_t base = vector_alignment(this->size(type.componentType()), type.rows());
85 return this->roundUpIfNeeded(base);
86 }
Ethan Nicholase6592142020-09-08 10:22:09 -040087 case Type::TypeKind::kArray: {
John Stiles0ebc69c2020-04-17 21:43:10 -070088 int stride = this->size(type.componentType());
89 if (stride > 0) {
90 int align = this->alignment(type.componentType());
91 stride += align - 1;
92 stride -= stride % align;
93 stride = this->roundUpIfNeeded(stride);
94 }
95 return stride;
Ethan Nicholas4a341972018-07-17 15:38:03 -040096 }
ethannicholas8ac838d2016-11-22 08:39:36 -080097 default:
98 ABORT("type does not have a stride");
99 }
100 }
101
102 /**
103 * Returns the size of a type in bytes.
104 */
105 size_t size(const Type& type) const {
Ethan Nicholase6592142020-09-08 10:22:09 -0400106 switch (type.typeKind()) {
107 case Type::TypeKind::kScalar:
John Stiles4dfa9772020-11-24 10:18:05 -0500108 if (type.isBoolean()) {
ethannicholas8ac838d2016-11-22 08:39:36 -0800109 return 1;
110 }
111 // FIXME need to take precision into account, once we figure out how we want to
112 // handle it...
113 return 4;
John Stiles0023c0c2020-11-16 13:32:18 -0500114 case Type::TypeKind::kEnum:
115 return 4;
Ethan Nicholase6592142020-09-08 10:22:09 -0400116 case Type::TypeKind::kVector:
Timothy Liang609fbe32018-08-10 16:40:49 -0400117 if (fStd == kMetal_Standard && type.columns() == 3) {
118 return 4 * this->size(type.componentType());
119 }
ethannicholas8ac838d2016-11-22 08:39:36 -0800120 return type.columns() * this->size(type.componentType());
Ethan Nicholase6592142020-09-08 10:22:09 -0400121 case Type::TypeKind::kMatrix: // fall through
122 case Type::TypeKind::kArray:
ethannicholas8ac838d2016-11-22 08:39:36 -0800123 return type.columns() * this->stride(type);
Ethan Nicholase6592142020-09-08 10:22:09 -0400124 case Type::TypeKind::kStruct: {
ethannicholas8ac838d2016-11-22 08:39:36 -0800125 size_t total = 0;
126 for (const auto& f : type.fields()) {
127 size_t alignment = this->alignment(*f.fType);
128 if (total % alignment != 0) {
129 total += alignment - total % alignment;
130 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400131 SkASSERT(total % alignment == 0);
ethannicholas8ac838d2016-11-22 08:39:36 -0800132 total += this->size(*f.fType);
133 }
134 size_t alignment = this->alignment(type);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400135 SkASSERT(!type.fields().size() ||
ethannicholas8ac838d2016-11-22 08:39:36 -0800136 (0 == alignment % this->alignment(*type.fields()[0].fType)));
137 return (total + alignment - 1) & ~(alignment - 1);
138 }
139 default:
Ethan Nicholase2c49992020-10-05 11:49:11 -0400140 ABORT("cannot determine size of type %s", String(type.name()).c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -0800141 }
142 }
143
John Stiles0023c0c2020-11-16 13:32:18 -0500144 /**
145 * Not all types are compatible with memory layout.
146 */
John Stiles21f5f452020-11-30 09:57:59 -0500147 static size_t LayoutIsSupported(const Type& type) {
John Stiles0023c0c2020-11-16 13:32:18 -0500148 switch (type.typeKind()) {
149 case Type::TypeKind::kScalar:
150 case Type::TypeKind::kEnum:
151 case Type::TypeKind::kVector:
152 case Type::TypeKind::kMatrix:
John Stiles0023c0c2020-11-16 13:32:18 -0500153 return true;
154
John Stiles21f5f452020-11-30 09:57:59 -0500155 case Type::TypeKind::kArray:
156 return LayoutIsSupported(type.componentType());
157
158 case Type::TypeKind::kStruct:
159 return std::all_of(
160 type.fields().begin(), type.fields().end(),
161 [](const Type::Field& f) { return LayoutIsSupported(*f.fType); });
162
John Stiles0023c0c2020-11-16 13:32:18 -0500163 default:
164 return false;
165 }
166 }
167
ethannicholas8ac838d2016-11-22 08:39:36 -0800168 const Standard fStd;
169};
170
John Stilesa6841be2020-08-06 14:11:56 -0400171} // namespace SkSL
ethannicholas8ac838d2016-11-22 08:39:36 -0800172
173#endif