-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_ubo.cpp
More file actions
276 lines (238 loc) · 9.86 KB
/
static_ubo.cpp
File metadata and controls
276 lines (238 loc) · 9.86 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "static_ubo.h"
#include <cassert>
uint32_t get_base_alignment(Std140AlignmentType::InlineType type, bool in_array) {
switch (type) {
case Std140AlignmentType::InlineType::Float:
case Std140AlignmentType::InlineType::Uint:
case Std140AlignmentType::InlineType::Int:
case Std140AlignmentType::InlineType::Bool:
return in_array ? 16 : 4;
case Std140AlignmentType::InlineType::Vec2:
return in_array ? 16 : 8;
case Std140AlignmentType::InlineType::Vec3:
case Std140AlignmentType::InlineType::Vec4:
case Std140AlignmentType::InlineType::Mat4:
return 16;
default:
assert(false);
return in_array ? 16 : 4;
}
}
uint32_t get_size(Std140AlignmentType::InlineType type, bool in_array) {
switch (type) {
case Std140AlignmentType::InlineType::Float:
case Std140AlignmentType::InlineType::Uint:
case Std140AlignmentType::InlineType::Int:
case Std140AlignmentType::InlineType::Bool:
return in_array ? 16 : 4;
case Std140AlignmentType::InlineType::Vec2:
return in_array ? 16 : 8;
case Std140AlignmentType::InlineType::Vec3:
return in_array ? 16 : 12;
case Std140AlignmentType::InlineType::Vec4:
return 16;
case Std140AlignmentType::InlineType::Mat4:
return 64;
default:
assert(false);
return in_array ? 16 : 4;
}
}
uint32_t get_base_alignment(Std430AlignmentType::InlineType type) {
switch (type) {
case Std430AlignmentType::InlineType::Float:
case Std430AlignmentType::InlineType::Uint:
case Std430AlignmentType::InlineType::Int:
case Std430AlignmentType::InlineType::Bool:
return 4;
case Std430AlignmentType::InlineType::Vec2:
return 8;
case Std430AlignmentType::InlineType::Vec3:
case Std430AlignmentType::InlineType::Vec4:
return 16;
case Std430AlignmentType::InlineType::Mat4:
return 16; // mat4 = array of vec4
default:
assert(false);
return 4;
}
}
uint32_t get_size(Std430AlignmentType::InlineType type, bool in_array) {
switch (type) {
case Std430AlignmentType::InlineType::Float:
case Std430AlignmentType::InlineType::Uint:
case Std430AlignmentType::InlineType::Int:
case Std430AlignmentType::InlineType::Bool:
return 4;
case Std430AlignmentType::InlineType::Vec2:
return 8;
case Std430AlignmentType::InlineType::Vec3:
return in_array ? 16 : 12; // padded to 16 only in arrays
case Std430AlignmentType::InlineType::Vec4:
return 16;
case Std430AlignmentType::InlineType::Mat4:
return 64; // 4 × vec4
default:
assert(false);
return 4;
}
}
uint32_t round_up_to(uint32_t value, uint32_t alignment) {
return (value + alignment - 1) & ~(alignment - 1);
}
Std140AlignmentType& Std140AlignmentType::add(InlineType type, const std::string& name, uint32_t array_count) {
uint32_t base_alignment = get_base_alignment(type, array_count > 1);
Range range;
range.offset = round_up_to(_total_size, base_alignment);
range.stride = get_size(type, array_count > 1);
range.size = range.stride * array_count;
_field_ranges.push_back(range);
_field_name_range_map[name] = _field_ranges.size() - 1;
_field_name_inline_type_map[name] = type;
_total_size = range.offset + range.size; // ignore trailing padding
return *this;
}
Std140AlignmentType& Std140AlignmentType::add(Std140AlignmentType& custom_type, const std::string& name, uint32_t array_count) {
uint32_t base_alignment = 16;
Range range;
range.offset = round_up_to(_total_size, base_alignment);
range.stride = round_up_to(custom_type._total_size, 16);
range.size = range.stride * array_count;
_field_ranges.push_back(range);
_field_name_range_map[name] = _field_ranges.size() - 1;
_field_name_custom_type_map[name] = custom_type;
_total_size = range.offset + range.size; // ignore trailing padding
return *this;
}
Std430AlignmentType& Std430AlignmentType::add(InlineType type, const std::string& name, uint32_t array_count) {
uint32_t base_alignment = get_base_alignment(type);
_max_base_alignment = std::max(_max_base_alignment, base_alignment);
Range range;
range.offset = round_up_to(_total_size, base_alignment);
range.stride = get_size(type, array_count > 1);
range.size = range.stride * array_count;
_field_ranges.push_back(range);
_field_name_range_map[name] = _field_ranges.size() - 1;
_field_name_inline_type_map[name] = type;
_total_size = range.offset + range.size; // ignore trailing padding
return *this;
}
Std430AlignmentType& Std430AlignmentType::add(Std430AlignmentType& custom_type, const std::string& name, uint32_t array_count) {
uint32_t base_alignment = custom_type._max_base_alignment;
_max_base_alignment = std::max(_max_base_alignment, base_alignment);
Range range;
range.offset = round_up_to(_total_size, base_alignment);
range.stride = round_up_to(custom_type._total_size, base_alignment);
range.size = range.stride * array_count;
_field_ranges.push_back(range);
_field_name_range_map[name] = _field_ranges.size() - 1;
_field_name_custom_type_map[name] = custom_type;
_total_size = range.offset + range.size; // ignore trailing padding
return *this;
}
template<typename T>
typename T::Range find_range_recursive(T& type, BufferObjectAccess& access, size_t depth) {
if (depth >= access._path.size()) {
// end of access path reached
return { 0, 0, 0 };
}
StaticUBOAccess::Access& acc = access._path[depth];
assert(type._field_name_range_map.find(acc.field_name) != type._field_name_range_map.end());
T::Range& range = type._field_ranges[type._field_name_range_map[acc.field_name]];
auto& inline_type_iter = type._field_name_inline_type_map.find(acc.field_name);
if (inline_type_iter != type._field_name_inline_type_map.end()) {
// inline type found
T::InlineType type = inline_type_iter->second;
VkDeviceSize offset = range.offset + acc.id * range.stride;
VkDeviceSize size = get_size(type, false);
VkDeviceSize stride = range.stride;
return { offset, stride, size };
}
auto& custom_type_iter = type._field_name_custom_type_map.find(acc.field_name);
if (custom_type_iter != type._field_name_custom_type_map.end()) {
// custom type found
T& custom_type = custom_type_iter->second;
T::Range sub_range = find_range_recursive(custom_type, access, ++depth);
VkDeviceSize offset = range.offset + acc.id * range.stride + sub_range.offset;
VkDeviceSize size = (sub_range.size == 0 ? range.size : sub_range.size);
VkDeviceSize stride = (sub_range.stride == 0 ? range.stride : sub_range.stride);
return { offset, stride, size };
}
// acc.field_name does not exist
assert(false);
return {0, 0, 0};
}
StaticUBO::StaticUBO(const Std140AlignmentType& layout) {
otcv::BufferBuilder bb;
bb.usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.host_access(otcv::BufferBuilder::Access::Coherent)
.size(layout._total_size);
_buf = new otcv::Buffer(bb);
_layout = layout;
}
StaticUBO::~StaticUBO() {
delete _buf;
_buf = nullptr;
}
void StaticUBO::set(StaticUBOAccess& access, const void* value) {
Std140AlignmentType::Range range = find_range_recursive(_layout, access, 0);
assert(_buf->mapped);
std::memcpy((char*)_buf->mapped + range.offset, value, range.size);
}
StaticUBOArray::StaticUBOArray(const Std140AlignmentType& layout, uint32_t n_ubos, uint32_t ubo_alignment) {
_stride = round_up_to(layout._total_size, ubo_alignment);
_n_ubos = n_ubos;
otcv::BufferBuilder bb;
bb.usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.host_access(otcv::BufferBuilder::Access::Coherent)
.size(_stride * _n_ubos);
_buf = new otcv::Buffer(bb);
_layout = layout;
}
StaticUBOArray::~StaticUBOArray() {
delete _buf;
_buf = nullptr;
}
void StaticUBOArray::set(uint32_t ubo_id, StaticUBOAccess& access, const void* value) {
assert(ubo_id < _n_ubos);
Std140AlignmentType::Range range = find_range_recursive(_layout, access, 0);
assert(_buf->mapped);
assert(_stride * ubo_id + range.offset < _buf->builder._info.size);
std::memcpy((char*)_buf->mapped + _stride * ubo_id + range.offset, value, range.size);
}
SSBO::SSBO(const Std430AlignmentType& layout, uint32_t n_ssbo, VkBufferUsageFlags additional_usage) {
_stride = round_up_to(layout._total_size, layout._max_base_alignment);
_n_ubos = n_ssbo;
otcv::BufferBuilder bb;
bb.usage(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | additional_usage)
.host_access(otcv::BufferBuilder::Access::Invisible)
.size(_stride * _n_ubos);
_buf = new otcv::Buffer(bb);
_staging_buf.resize(_buf->builder._info.size);
_layout = layout;
}
SSBO::~SSBO() {
delete _buf;
_buf = nullptr;
}
void SSBO::write(std::vector<WriteContext>& writes) {
for (WriteContext& write : writes) {
assert(write.id < _n_ubos);
for (WriteContext::AccessContext& acc_ctx : write.access_ctxs) {
BufferObjectAccess& acc = acc_ctx.acc;
const void* value = acc_ctx.value;
Std430AlignmentType::Range range = find_range_recursive(_layout, acc, 0);
assert(_stride * write.id + range.offset + range.size <= _staging_buf.size());
std::memcpy(_staging_buf.data() + _stride * write.id + range.offset, value, range.size);
}
}
_buf->populate(_staging_buf.data());
}
Std430AlignmentType::Range SSBO::range_of(uint32_t id, SSBOAccess& acc) {
assert(id < _n_ubos);
Std430AlignmentType::Range range = find_range_recursive(_layout, acc, 0);
range.offset += _stride * id;
range.stride = (range.stride == 0 ? _stride : range.stride);
range.size = (range.size == 0 ? _layout._total_size : range.size);
return range;
}