Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/sksl/SkSLMemoryLayout.h

Issue 2187433003: added support for push_constant layout (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: this is right. If it disagrees with the spec, the spec is wrong. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 */
7
8 #ifndef SKIASL_MEMORYLAYOUT
9 #define SKIASL_MEMORYLAYOUT
10
11 #include "ir/SkSLType.h"
12
13 namespace SkSL {
14
15 class MemoryLayout {
16 public:
17 enum Standard {
18 k140_Standard,
19 k430_Standard
20 };
21
22 MemoryLayout(Standard std)
23 : fStd(std) {}
24
25 static size_t vector_alignment(size_t componentSize, int columns) {
26 return componentSize * (columns + columns % 2);
27 }
28
29 /**
30 * Perform required rounding on array (and matrix) component sizes. std140 r equires array and
31 * matrix strides to be rounded up to the nearest multiple of 16, std430 doe s not.
32 */
33 size_t roundArraySize(size_t raw) const {
egdaniel 2016/11/15 15:31:18 Lets give this a more generic name since we use it
34 switch (fStd) {
35 case k140_Standard: return (raw + 15) & ~15;
36 case k430_Standard: return raw;
37 }
38 ABORT("unreachable");
39 }
40
41 /**
42 * Returns a type's required alignment when used as a standalone variable.
43 */
44 size_t alignment(const Type& type) const {
45 // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout
46 switch (type.kind()) {
47 case Type::kScalar_Kind:
48 return this->size(type);
49 case Type::kVector_Kind:
50 return vector_alignment(this->size(type.componentType()), type.c olumns());
51 case Type::kMatrix_Kind:
52 return this->roundArraySize(vector_alignment(this->size(type.com ponentType()),
53 type.rows()));
54 case Type::kArray_Kind:
55 return this->roundArraySize(this->alignment(type.componentType() ));
56 case Type::kStruct_Kind: {
57 size_t result = 0;
58 for (const auto& f : type.fields()) {
59 size_t alignment = this->alignment(*f.fType);
60 if (alignment > result) {
61 result = alignment;
62 }
63 }
64 return this->roundArraySize(result);
65 }
66 default:
67 ABORT(("cannot determine size of type " + type.name()).c_str());
68 }
69 }
70
71 /**
72 * For matrices and arrays, returns the number of bytes from the start of on e entry (row, in
73 * the case of matrices) to the start of the next.
74 */
75 size_t stride(const Type& type) const {
76 switch (type.kind()) {
77 case Type::kMatrix_Kind: // fall through
78 case Type::kArray_Kind:
79 return this->alignment(type);
80 default:
81 ABORT("type does not have a stride");
82 }
83 }
84
85 /**
86 * Returns the size of a type in bytes.
87 */
88 size_t size(const Type& type) const {
89 switch (type.kind()) {
90 case Type::kScalar_Kind:
91 if (type.name() == "bool") {
92 return 1;
93 }
94 // FIXME need to take precision into account, once we figure out how we want to
95 // handle it...
96 return 4;
97 case Type::kVector_Kind:
98 return type.columns() * this->size(type.componentType());
99 case Type::kMatrix_Kind:
100 return vector_alignment(this->size(type.componentType()), type.r ows()) *
101 type.columns();
102 case Type::kArray_Kind:
103 return type.columns() * this->stride(type);
104 case Type::kStruct_Kind: {
105 size_t total = 0;
106 for (const auto& f : type.fields()) {
107 size_t alignment = this->alignment(*f.fType);
108 if (total % alignment != 0) {
109 total += alignment - total % alignment;
110 }
111 ASSERT(total % alignment == 0);
112 total += this->size(*f.fType);
113 }
114 size_t alignment = this->alignment(type);
egdaniel 2016/11/15 15:31:18 add my totally insane assert :)
115 return (total + alignment - 1) & ~(alignment - 1);
116 }
117 default:
118 ABORT(("cannot determine size of type " + type.name()).c_str());
119 }
120 }
121
122 const Standard fStd;
123 };
124
125 } // namespace
126
127 #endif
OLDNEW
« no previous file with comments | « gn/tests.gni ('k') | src/sksl/SkSLParser.cpp » ('j') | tests/SkSLMemoryLayoutTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698