blob: 3d934bd3eb680038bba780fa7a27ebb1f9b09a3f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/ir/SkSLType.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080012
13namespace SkSL {
14
15class MemoryLayout {
16public:
17 enum Standard {
18 k140_Standard,
Timothy Liang609fbe32018-08-10 16:40:49 -040019 k430_Standard,
20 kMetal_Standard
ethannicholas8ac838d2016-11-22 08:39:36 -080021 };
22
Ethan Nicholas0df1b042017-03-31 13:56:23 -040023 MemoryLayout(Standard std)
ethannicholas8ac838d2016-11-22 08:39:36 -080024 : fStd(std) {}
25
26 static size_t vector_alignment(size_t componentSize, int columns) {
27 return componentSize * (columns + columns % 2);
28 }
29
Ethan Nicholas0df1b042017-03-31 13:56:23 -040030 /**
ethannicholas8ac838d2016-11-22 08:39:36 -080031 * 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 Liang609fbe32018-08-10 16:40:49 -040039 case kMetal_Standard: return raw;
ethannicholas8ac838d2016-11-22 08:39:36 -080040 }
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 Nicholase6592142020-09-08 10:22:09 -040049 switch (type.typeKind()) {
50 case Type::TypeKind::kScalar:
ethannicholas8ac838d2016-11-22 08:39:36 -080051 return this->size(type);
Ethan Nicholase6592142020-09-08 10:22:09 -040052 case Type::TypeKind::kVector:
ethannicholas8ac838d2016-11-22 08:39:36 -080053 return vector_alignment(this->size(type.componentType()), type.columns());
Ethan Nicholase6592142020-09-08 10:22:09 -040054 case Type::TypeKind::kMatrix:
Ethan Nicholas0df1b042017-03-31 13:56:23 -040055 return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()),
ethannicholas8ac838d2016-11-22 08:39:36 -080056 type.rows()));
Ethan Nicholase6592142020-09-08 10:22:09 -040057 case Type::TypeKind::kArray:
ethannicholas8ac838d2016-11-22 08:39:36 -080058 return this->roundUpIfNeeded(this->alignment(type.componentType()));
Ethan Nicholase6592142020-09-08 10:22:09 -040059 case Type::TypeKind::kStruct: {
ethannicholas8ac838d2016-11-22 08:39:36 -080060 size_t result = 0;
61 for (const auto& f : type.fields()) {
62 size_t alignment = this->alignment(*f.fType);
63 if (alignment > result) {
64 result = alignment;
65 }
66 }
67 return this->roundUpIfNeeded(result);
68 }
69 default:
Ethan Nicholas0df1b042017-03-31 13:56:23 -040070 ABORT("cannot determine size of type %s", type.name().c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -080071 }
72 }
73
74 /**
75 * For matrices and arrays, returns the number of bytes from the start of one entry (row, in
76 * the case of matrices) to the start of the next.
77 */
78 size_t stride(const Type& type) const {
Ethan Nicholase6592142020-09-08 10:22:09 -040079 switch (type.typeKind()) {
80 case Type::TypeKind::kMatrix: {
Ethan Nicholas4a341972018-07-17 15:38:03 -040081 size_t base = vector_alignment(this->size(type.componentType()), type.rows());
82 return this->roundUpIfNeeded(base);
83 }
Ethan Nicholase6592142020-09-08 10:22:09 -040084 case Type::TypeKind::kArray: {
John Stiles0ebc69c2020-04-17 21:43:10 -070085 int stride = this->size(type.componentType());
86 if (stride > 0) {
87 int align = this->alignment(type.componentType());
88 stride += align - 1;
89 stride -= stride % align;
90 stride = this->roundUpIfNeeded(stride);
91 }
92 return stride;
Ethan Nicholas4a341972018-07-17 15:38:03 -040093 }
ethannicholas8ac838d2016-11-22 08:39:36 -080094 default:
95 ABORT("type does not have a stride");
96 }
97 }
98
99 /**
100 * Returns the size of a type in bytes.
101 */
102 size_t size(const Type& type) const {
Ethan Nicholase6592142020-09-08 10:22:09 -0400103 switch (type.typeKind()) {
104 case Type::TypeKind::kScalar:
ethannicholas8ac838d2016-11-22 08:39:36 -0800105 if (type.name() == "bool") {
106 return 1;
107 }
108 // FIXME need to take precision into account, once we figure out how we want to
109 // handle it...
110 return 4;
Ethan Nicholase6592142020-09-08 10:22:09 -0400111 case Type::TypeKind::kVector:
Timothy Liang609fbe32018-08-10 16:40:49 -0400112 if (fStd == kMetal_Standard && type.columns() == 3) {
113 return 4 * this->size(type.componentType());
114 }
ethannicholas8ac838d2016-11-22 08:39:36 -0800115 return type.columns() * this->size(type.componentType());
Ethan Nicholase6592142020-09-08 10:22:09 -0400116 case Type::TypeKind::kMatrix: // fall through
117 case Type::TypeKind::kArray:
ethannicholas8ac838d2016-11-22 08:39:36 -0800118 return type.columns() * this->stride(type);
Ethan Nicholase6592142020-09-08 10:22:09 -0400119 case Type::TypeKind::kStruct: {
ethannicholas8ac838d2016-11-22 08:39:36 -0800120 size_t total = 0;
121 for (const auto& f : type.fields()) {
122 size_t alignment = this->alignment(*f.fType);
123 if (total % alignment != 0) {
124 total += alignment - total % alignment;
125 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400126 SkASSERT(total % alignment == 0);
ethannicholas8ac838d2016-11-22 08:39:36 -0800127 total += this->size(*f.fType);
128 }
129 size_t alignment = this->alignment(type);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400130 SkASSERT(!type.fields().size() ||
ethannicholas8ac838d2016-11-22 08:39:36 -0800131 (0 == alignment % this->alignment(*type.fields()[0].fType)));
132 return (total + alignment - 1) & ~(alignment - 1);
133 }
134 default:
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400135 ABORT("cannot determine size of type %s", type.name().c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -0800136 }
137 }
138
139 const Standard fStd;
140};
141
John Stilesa6841be2020-08-06 14:11:56 -0400142} // namespace SkSL
ethannicholas8ac838d2016-11-22 08:39:36 -0800143
144#endif