blob: 210769c8d770cb171342ff80084b5d251fdc5e4f [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
11#include "ir/SkSLType.h"
12
13namespace SkSL {
14
15class MemoryLayout {
16public:
17 enum Standard {
18 k140_Standard,
19 k430_Standard
20 };
21
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 MemoryLayout(Standard std)
ethannicholas8ac838d2016-11-22 08:39:36 -080023 : fStd(std) {}
24
25 static size_t vector_alignment(size_t componentSize, int columns) {
26 return componentSize * (columns + columns % 2);
27 }
28
Ethan Nicholas0df1b042017-03-31 13:56:23 -040029 /**
ethannicholas8ac838d2016-11-22 08:39:36 -080030 * Rounds up to the nearest multiple of 16 if in std140, otherwise returns the parameter
31 * unchanged (std140 requires various things to be rounded up to the nearest multiple of 16,
32 * std430 does not).
33 */
34 size_t roundUpIfNeeded(size_t raw) const {
35 switch (fStd) {
36 case k140_Standard: return (raw + 15) & ~15;
37 case k430_Standard: return raw;
38 }
39 ABORT("unreachable");
40 }
41
42 /**
43 * Returns a type's required alignment when used as a standalone variable.
44 */
45 size_t alignment(const Type& type) const {
46 // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout
47 switch (type.kind()) {
48 case Type::kScalar_Kind:
49 return this->size(type);
50 case Type::kVector_Kind:
51 return vector_alignment(this->size(type.componentType()), type.columns());
52 case Type::kMatrix_Kind:
Ethan Nicholas0df1b042017-03-31 13:56:23 -040053 return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()),
ethannicholas8ac838d2016-11-22 08:39:36 -080054 type.rows()));
55 case Type::kArray_Kind:
56 return this->roundUpIfNeeded(this->alignment(type.componentType()));
57 case Type::kStruct_Kind: {
58 size_t result = 0;
59 for (const auto& f : type.fields()) {
60 size_t alignment = this->alignment(*f.fType);
61 if (alignment > result) {
62 result = alignment;
63 }
64 }
65 return this->roundUpIfNeeded(result);
66 }
67 default:
Ethan Nicholas0df1b042017-03-31 13:56:23 -040068 ABORT("cannot determine size of type %s", type.name().c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -080069 }
70 }
71
72 /**
73 * For matrices and arrays, returns the number of bytes from the start of one entry (row, in
74 * the case of matrices) to the start of the next.
75 */
76 size_t stride(const Type& type) const {
77 switch (type.kind()) {
Ethan Nicholas4a341972018-07-17 15:38:03 -040078 case Type::kMatrix_Kind: {
79 size_t base = vector_alignment(this->size(type.componentType()), type.rows());
80 return this->roundUpIfNeeded(base);
81 }
82 case Type::kArray_Kind: {
83 int align = this->alignment(type.componentType());
84 int stride = this->size(type.componentType()) + align - 1;
85 stride -= stride % align;
86 return this->roundUpIfNeeded(stride);
87 }
ethannicholas8ac838d2016-11-22 08:39:36 -080088 default:
89 ABORT("type does not have a stride");
90 }
91 }
92
93 /**
94 * Returns the size of a type in bytes.
95 */
96 size_t size(const Type& type) const {
97 switch (type.kind()) {
98 case Type::kScalar_Kind:
99 if (type.name() == "bool") {
100 return 1;
101 }
102 // FIXME need to take precision into account, once we figure out how we want to
103 // handle it...
104 return 4;
105 case Type::kVector_Kind:
106 return type.columns() * this->size(type.componentType());
107 case Type::kMatrix_Kind: // fall through
108 case Type::kArray_Kind:
109 return type.columns() * this->stride(type);
110 case Type::kStruct_Kind: {
111 size_t total = 0;
112 for (const auto& f : type.fields()) {
113 size_t alignment = this->alignment(*f.fType);
114 if (total % alignment != 0) {
115 total += alignment - total % alignment;
116 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400117 SkASSERT(total % alignment == 0);
ethannicholas8ac838d2016-11-22 08:39:36 -0800118 total += this->size(*f.fType);
119 }
120 size_t alignment = this->alignment(type);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400121 SkASSERT(!type.fields().size() ||
ethannicholas8ac838d2016-11-22 08:39:36 -0800122 (0 == alignment % this->alignment(*type.fields()[0].fType)));
123 return (total + alignment - 1) & ~(alignment - 1);
124 }
125 default:
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400126 ABORT("cannot determine size of type %s", type.name().c_str());
ethannicholas8ac838d2016-11-22 08:39:36 -0800127 }
128 }
129
130 const Standard fStd;
131};
132
133} // namespace
134
135#endif