MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MeshNetworkProcessor.cpp
Go to the documentation of this file.
2
4
7
9
10namespace MayaFlux::Buffers {
11
12namespace {
13
14struct MilledMeshNetworkVertex {
15 Kakshya::MeshVertex mesh;
16 float slot_low;
17 float slot_high;
18};
19
20static_assert(sizeof(MilledMeshNetworkVertex) == sizeof(Kakshya::MeshVertex) + 2 * sizeof(float));
21
22Kakshya::VertexLayout milled_mesh_network_layout()
23{
24 auto layout = Kakshya::VertexLayout::for_meshes(sizeof(MilledMeshNetworkVertex));
25 layout.attributes.push_back({
26 .component_modality = Kakshya::DataModality::SCALAR_F32,
27 .offset_in_vertex = static_cast<uint32_t>(sizeof(Kakshya::MeshVertex)),
28 .name = "slot_index_low"
29 });
30 layout.attributes.push_back({
31 .component_modality = Kakshya::DataModality::SCALAR_F32,
32 .offset_in_vertex = static_cast<uint32_t>(sizeof(Kakshya::MeshVertex) + sizeof(float)),
33 .name = "slot_index_high"
34 });
35 return layout;
36}
37
38} // namespace
39
41 std::shared_ptr<Nodes::Network::MeshNetwork> network)
42 : m_network(std::move(network))
43{
46}
47
49{
50 if (m_milling_slot_attribute == enabled)
51 return;
52
54 for (auto& slot : m_network->slots())
55 slot.dirty = true;
56}
57
58// =============================================================================
59// on_attach
60// =============================================================================
61
62void MeshNetworkProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
63{
64 if (!m_network || m_network->slot_count() == 0) {
66 "MeshNetworkProcessor attached to empty network");
67 return;
68 }
69
70 auto vk_buf = std::dynamic_pointer_cast<VKBuffer>(buffer);
71 if (!vk_buf) {
73 "MeshNetworkProcessor: attached buffer is not a VKBuffer");
74 return;
75 }
76
77 m_network->ensure_sorted();
78
79 ensure_initialized(vk_buf);
80
82 upload_combined(vk_buf);
84 if (!m_model_aggregate.empty() && !m_slot_index_aggregate.empty()) {
87 push_ssbo_bindings(vk_buf);
88 }
90
92 "MeshNetworkProcessor: initialised — {} slots, {} bytes vertex, {} indices",
93 m_network->slot_count(),
94 m_vertex_aggregate.size(),
95 m_index_aggregate.size());
96}
97
98void MeshNetworkProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
99{
100 m_gpu_index_buffer.reset();
101 m_vertex_staging.reset();
102 m_index_staging.reset();
103 if (m_model_arena) {
104 m_model_arena->release();
105 }
106 if (m_slot_index_arena) {
107 m_slot_index_arena->release();
108 }
109 m_model_arena.reset();
110 m_slot_index_arena.reset();
111}
112
113// =============================================================================
114// processing_function
115// =============================================================================
116
117void MeshNetworkProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
118{
119 if (!m_network)
120 return;
121
122 m_network->ensure_sorted();
123
124 auto vk_buf = std::dynamic_pointer_cast<VKBuffer>(buffer);
125 if (!vk_buf)
126 return;
127
128 if (!any_slot_dirty())
129 return;
130
131 upload_combined(vk_buf);
132 link_index_resources(vk_buf);
134
135 if (!m_model_arena || !m_model_arena->is_initialized()
136 || !m_slot_index_arena || !m_slot_index_arena->is_initialized())
138
139 upload_ssbos();
140 push_ssbo_bindings(vk_buf);
141
143 "MeshNetworkProcessor: re-uploaded {} bytes vertex, {} indices",
145}
146
147// =============================================================================
148// Private helpers
149// =============================================================================
150
152 const std::shared_ptr<VKBuffer>& vertex_buf)
153{
154 const size_t v_bytes = total_vertex_bytes();
155 const size_t i_bytes = total_index_count() * sizeof(uint32_t);
156
157 if (v_bytes == 0 || i_bytes == 0) {
159 "MeshNetworkProcessor::allocate_gpu_buffers: network has no geometry");
160 return;
161 }
162
163 if (!vertex_buf->is_host_visible())
165
166 m_gpu_index_buffer = std::make_shared<VKBuffer>(
167 i_bytes, VKBuffer::Usage::INDEX, Kakshya::DataModality::UNKNOWN);
170
172 "MeshNetworkProcessor: allocated vertex ({} bytes) + index ({} bytes)",
173 v_bytes, i_bytes);
174}
175
177 const std::shared_ptr<VKBuffer>& vertex_buf)
178{
179 const auto& slots = m_network->slots();
180 const auto& order = m_network->sorted_indices();
181
182 m_vertex_aggregate.clear();
183 m_index_aggregate.clear();
184 m_model_aggregate.clear();
186
187 uint32_t running_vertex_count = 0;
188
189 for (uint32_t idx : order) {
190 const auto& slot = slots[idx];
191 if (!slot.node)
192 continue;
193
194 const auto& verts = slot.node->get_mesh_vertices();
195 const auto& indices = slot.node->get_mesh_indices();
196
198 const auto slot_id = static_cast<uint32_t>(m_model_aggregate.size());
199 const float slot_low = static_cast<float>(slot_id & 0xFFFFU);
200 const float slot_high = static_cast<float>(slot_id >> 16U);
201 for (const auto& vertex : verts) {
202 const MilledMeshNetworkVertex packed { vertex, slot_low, slot_high };
203 const auto* bytes = reinterpret_cast<const uint8_t*>(&packed);
204 m_vertex_aggregate.insert(
205 m_vertex_aggregate.end(), bytes, bytes + sizeof(packed));
206 }
207 } else {
208 const size_t v_bytes = verts.size() * sizeof(Kakshya::MeshVertex);
209 const auto* v_src = reinterpret_cast<const uint8_t*>(verts.data());
210 m_vertex_aggregate.insert(m_vertex_aggregate.end(), v_src, v_src + v_bytes);
211 }
212
213 for (uint32_t i : indices)
214 m_index_aggregate.push_back(i + running_vertex_count);
215
216 m_model_aggregate.push_back(slot.world_transform);
217
218 const auto sorted_slot_pos = static_cast<uint32_t>(m_model_aggregate.size() - 1);
220 m_slot_index_aggregate.end(), verts.size(), sorted_slot_pos);
221
222 running_vertex_count += static_cast<uint32_t>(verts.size());
223 }
224
225 if (m_vertex_aggregate.empty())
226 return;
227
228 const size_t v_required = m_vertex_aggregate.size();
229 if (v_required > vertex_buf->get_size_bytes()) {
230 const auto new_size = static_cast<size_t>(
231 static_cast<float>(v_required) * 1.5F);
232 vertex_buf->resize(new_size, false);
234 m_vertex_staging->resize(new_size, false);
235 }
236 if (m_vertex_staging && v_required > m_vertex_staging->get_size_bytes())
237 m_vertex_staging->resize(v_required, false);
238
240 m_vertex_aggregate.data(),
241 v_required,
242 vertex_buf,
243 vertex_buf->is_host_visible() ? nullptr : m_vertex_staging);
244
245 const size_t i_required = m_index_aggregate.size() * sizeof(uint32_t);
246 if (m_gpu_index_buffer && i_required > m_gpu_index_buffer->get_size_bytes()) {
247 const auto new_size = static_cast<size_t>(
248 static_cast<float>(i_required) * 1.5F);
249 m_gpu_index_buffer->resize(new_size, false);
250 if (m_index_staging)
251 m_index_staging->resize(new_size, false);
252 }
253
254 if (m_gpu_index_buffer && !m_index_aggregate.empty()) {
256 m_index_aggregate.data(),
257 i_required,
260 }
261
263 auto layout = milled_mesh_network_layout();
264 layout.vertex_count = running_vertex_count;
265 vertex_buf->set_vertex_layout(layout);
266 } else for (uint32_t idx : order) {
267 const auto& slot = slots[idx];
268 if (slot.node && slot.node->get_mesh_vertex_count() > 0) {
269 if (auto layout = slot.node->get_vertex_layout()) {
270 layout->vertex_count = running_vertex_count;
271 vertex_buf->set_vertex_layout(*layout);
272 }
273 break;
274 }
275 }
276}
277
279 const std::shared_ptr<VKBuffer>& vertex_buf)
280{
282 return;
283
284 vertex_buf->set_index_resources(
285 m_gpu_index_buffer->get_buffer(),
286 m_gpu_index_buffer->get_buffer_resources().memory,
287 m_gpu_index_buffer->get_size_bytes());
288}
289
291{
292 return std::ranges::any_of(m_network->slots(), [](const auto& slot) {
293 return slot.dirty || (slot.node && slot.node->needs_gpu_update());
294 });
295}
296
298{
299 for (auto& slot : m_network->slots()) {
300 slot.dirty = false;
301 if (slot.node)
302 slot.node->clear_gpu_update_flag();
303 }
304}
305
307{
308 size_t total = 0;
309 for (const auto& slot : m_network->slots()) {
310 if (slot.node) {
311 const auto stride = sizeof(Kakshya::MeshVertex)
312 + (m_milling_slot_attribute ? 2 * sizeof(float) : 0);
313 total += slot.node->get_mesh_vertex_count() * stride;
314 }
315 }
316 return total;
317}
318
320{
321 size_t total = 0;
322 for (const auto& slot : m_network->slots()) {
323 if (slot.node)
324 total += slot.node->get_mesh_indices().size();
325 }
326 return total;
327}
328
330{
331 const size_t model_bytes = m_model_aggregate.size() * sizeof(glm::mat4);
332 const size_t slot_index_bytes = m_slot_index_aggregate.size() * sizeof(uint32_t);
333
334 if (model_bytes == 0 || slot_index_bytes == 0) {
335 MF_WARN(Journal::Component::Buffers, Journal::Context::BufferProcessing, "MeshNetworkProcessor::allocate_ssbo_buffers: aggregates empty at alloc time");
336 return;
337 }
338
340
341 m_model_arena = std::make_shared<Portal::Graphics::SlotArena>();
342 m_model_arena->initialize(model_bytes, usage, 3);
343
344 m_slot_index_arena = std::make_shared<Portal::Graphics::SlotArena>();
345 m_slot_index_arena->initialize(slot_index_bytes, usage, 3);
346}
347
349{
350 if (m_model_aggregate.empty() || !m_model_arena || !m_model_arena->is_initialized()
351 || !m_slot_index_arena || !m_slot_index_arena->is_initialized())
352 return;
353
355 const size_t model_bytes = m_model_aggregate.size() * sizeof(glm::mat4);
356 const size_t slot_index_bytes = m_slot_index_aggregate.size() * sizeof(uint32_t);
357
358 if (model_bytes > m_model_arena->slot_bytes()) {
359 m_model_arena->initialize(model_bytes * 3 / 2, usage, 3);
360 }
361 if (slot_index_bytes > m_slot_index_arena->slot_bytes()) {
362 m_slot_index_arena->initialize(slot_index_bytes * 3 / 2, usage, 3);
363 }
364
365 m_model_slot = m_model_arena->acquire();
366 m_model_arena->write(m_model_slot, m_model_aggregate.data(), model_bytes);
367
369 m_slot_index_arena->write(m_slot_index_slot, m_slot_index_aggregate.data(), slot_index_bytes);
370}
371
372void MeshNetworkProcessor::push_ssbo_bindings(const std::shared_ptr<VKBuffer>& buffer)
373{
374 if (!m_model_arena || !m_model_arena->is_initialized())
375 return;
376
377 auto& bindings = buffer->get_engine_context().ssbo_bindings;
378
379 auto push = [&](uint32_t binding_idx, const std::shared_ptr<Portal::Graphics::SlotArena>& arena, size_t slot) {
381 info.set = 0;
382 info.binding = binding_idx;
383 info.type = vk::DescriptorType::eStorageBuffer;
384 info.buffer_info = arena->info(slot);
385 info.arena = arena;
386 info.slot = slot;
387
388 auto it = std::ranges::find_if(bindings, [&](const auto& b) {
389 return b.set == info.set && b.binding == info.binding;
390 });
391 if (it != bindings.end()) {
392 *it = info;
393 } else {
394 bindings.push_back(info);
395 }
396 };
397
398 push(1, m_model_arena, m_model_slot);
399 if (m_slot_index_arena && m_slot_index_arena->is_initialized()) {
401 }
402}
403
404} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
Core::GlobalNetworkConfig network
Definition Config.cpp:43
std::vector< uint32_t > indices
Kakshya::MeshVertex mesh
float slot_low
float slot_high
std::shared_ptr< NetworkGeometryBuffer > buffer
uint32_t total
size_t b
uint16_t usage
std::string name
Definition VKDevice.cpp:123
std::shared_ptr< Portal::Graphics::SlotArena > m_model_arena
Per-slot world matrices in sorted_indices order. One mat4 per slot.
std::vector< uint32_t > m_slot_index_aggregate
CPU-side scratch for per-vertex slot indices, reused each cycle.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
std::vector< uint8_t > m_vertex_aggregate
Aggregate scratch buffers reused each cycle to avoid allocation.
void link_index_resources(const std::shared_ptr< VKBuffer > &vertex_buf)
void upload_combined(const std::shared_ptr< VKBuffer > &vertex_buf)
MeshNetworkProcessor(std::shared_ptr< Nodes::Network::MeshNetwork > network)
void allocate_gpu_buffers(const std::shared_ptr< VKBuffer > &vertex_buf)
void push_ssbo_bindings(const std::shared_ptr< VKBuffer > &buffer)
std::shared_ptr< Nodes::Network::MeshNetwork > m_network
std::shared_ptr< Portal::Graphics::SlotArena > m_slot_index_arena
Per-vertex slot index. One uint32_t per concatenated vertex.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
std::vector< glm::mat4 > m_model_aggregate
CPU-side scratch for model matrices, reused each cycle.
Registry::Service::BufferService * m_buffer_service
Definition VKBuffer.hpp:722
void ensure_initialized(const std::shared_ptr< VKBuffer > &buffer)
Definition VKBuffer.cpp:531
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging, size_t dst_offset, bool awaited)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
@ UNKNOWN
Unknown or undefined modality.
@ SCALAR_F32
Single-channel float data.
@ HOST_STORAGE
Host-visible storage buffer (eStorageBuffer + eHostVisible|eHostCoherent)
Vertex type for indexed triangle mesh primitives (TRIANGLE_LIST topology)
static VertexLayout for_meshes(uint32_t stride=68)
Factory: layout for MeshVertex (position, color, weight, uv, normal, tangent)
std::function< void(const std::shared_ptr< void > &)> initialize_buffer
Initialize a buffer object.