blob: 61712eccffb0eebc6e46055cda8c48394db19400 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKIASL_MEMORYLAYOUT
#define SKIASL_MEMORYLAYOUT
#include "ir/SkSLType.h"
namespace SkSL {
class MemoryLayout {
public:
enum Standard {
k140_Standard,
k430_Standard
};
MemoryLayout(Standard std)
: fStd(std) {}
static size_t vector_alignment(size_t componentSize, int columns) {
return componentSize * (columns + columns % 2);
}
/**
* Rounds up to the nearest multiple of 16 if in std140, otherwise returns the parameter
* unchanged (std140 requires various things to be rounded up to the nearest multiple of 16,
* std430 does not).
*/
size_t roundUpIfNeeded(size_t raw) const {
switch (fStd) {
case k140_Standard: return (raw + 15) & ~15;
case k430_Standard: return raw;
}
ABORT("unreachable");
}
/**
* Returns a type's required alignment when used as a standalone variable.
*/
size_t alignment(const Type& type) const {
// See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout
switch (type.kind()) {
case Type::kScalar_Kind:
return this->size(type);
case Type::kVector_Kind:
return vector_alignment(this->size(type.componentType()), type.columns());
case Type::kMatrix_Kind:
return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()),
type.rows()));
case Type::kArray_Kind:
return this->roundUpIfNeeded(this->alignment(type.componentType()));
case Type::kStruct_Kind: {
size_t result = 0;
for (const auto& f : type.fields()) {
size_t alignment = this->alignment(*f.fType);
if (alignment > result) {
result = alignment;
}
}
return this->roundUpIfNeeded(result);
}
default:
ABORT("cannot determine size of type %s", type.name().c_str());
}
}
/**
* For matrices and arrays, returns the number of bytes from the start of one entry (row, in
* the case of matrices) to the start of the next.
*/
size_t stride(const Type& type) const {
switch (type.kind()) {
case Type::kMatrix_Kind: // fall through
case Type::kArray_Kind:
return this->alignment(type);
default:
ABORT("type does not have a stride");
}
}
/**
* Returns the size of a type in bytes.
*/
size_t size(const Type& type) const {
switch (type.kind()) {
case Type::kScalar_Kind:
if (type.name() == "bool") {
return 1;
}
// FIXME need to take precision into account, once we figure out how we want to
// handle it...
return 4;
case Type::kVector_Kind:
return type.columns() * this->size(type.componentType());
case Type::kMatrix_Kind: // fall through
case Type::kArray_Kind:
return type.columns() * this->stride(type);
case Type::kStruct_Kind: {
size_t total = 0;
for (const auto& f : type.fields()) {
size_t alignment = this->alignment(*f.fType);
if (total % alignment != 0) {
total += alignment - total % alignment;
}
ASSERT(total % alignment == 0);
total += this->size(*f.fType);
}
size_t alignment = this->alignment(type);
ASSERT(!type.fields().size() ||
(0 == alignment % this->alignment(*type.fields()[0].fType)));
return (total + alignment - 1) & ~(alignment - 1);
}
default:
ABORT("cannot determine size of type %s", type.name().c_str());
}
}
const Standard fStd;
};
} // namespace
#endif
|