MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BufferUploadProcessor.cpp
Go to the documentation of this file.
2
4
7
8#include "StagingUtils.hpp"
9
10namespace MayaFlux::Buffers {
11
16
22
23void BufferUploadProcessor::processing_function(std::shared_ptr<Buffer> buffer)
24{
25 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
26 if (!vk_buffer) {
28 "BufferUploadProcessor requires VKBuffer");
29 return;
30 }
31
32 if (!vk_buffer->is_initialized()) {
34 "VKBuffer not initialized - register with BufferManager first");
35 return;
36 }
37
38 auto source_it = m_source_map.find(buffer);
39 if (source_it == m_source_map.end() || !source_it->second) {
41 "BufferUploadProcessor has no source configured for this buffer");
42 return;
43 }
44
45 auto source = source_it->second;
46 auto source_data = std::dynamic_pointer_cast<VKBuffer>(source)->get_data();
47 if (source_data.empty()) {
49 "Source buffer has no data to upload");
50 return;
51 }
52
53 if (vk_buffer->is_host_visible()) {
54 upload_host_visible(vk_buffer, source_data[0]);
55 } else {
56 upload_device_local(vk_buffer, source_data[0]);
57 }
58}
59
60void BufferUploadProcessor::upload_device_local(const std::shared_ptr<VKBuffer>& target, const Kakshya::DataVariant& data)
61{
63 auto staging_buffer = m_staging_buffers[target];
64 Buffers::upload_device_local(target, staging_buffer, data);
65}
66
67void BufferUploadProcessor::ensure_staging_buffer(const std::shared_ptr<VKBuffer>& target)
68{
69 auto it = m_staging_buffers.find(target);
70 if (it != m_staging_buffers.end() && it->second->get_size_bytes() >= target->get_size_bytes() && it->second->is_initialized()) {
71 return;
72 }
73
74 auto staging_buffer = std::make_shared<VKBuffer>(
75 target->get_size_bytes(),
78
79 if (!m_buffer_service) {
80 error<std::runtime_error>(
83 std::source_location::current(),
84 "No processing context available for staging buffer initialization");
85 }
86
87 if (!staging_buffer->is_initialized()) {
88 try {
89 m_buffer_service->initialize_buffer(staging_buffer);
90 } catch (const std::exception& e) {
91 error_rethrow(
94 std::source_location::current(),
95 "Failed to initialize staging buffer: {}", e.what());
96 }
97 }
98
99 m_staging_buffers[target] = staging_buffer;
100
102 "Created staging buffer: {} bytes", staging_buffer->get_size_bytes());
103}
104
105void BufferUploadProcessor::on_attach(std::shared_ptr<Buffer> buffer)
106{
107 if (!is_compatible_with(buffer)) {
108 error<std::runtime_error>(
111 std::source_location::current(),
112 "BufferUploadProcessor can only be attached to VKBuffer");
113 }
114
115 if (!m_buffer_service) {
118 }
119
120 if (!m_buffer_service) {
121 error<std::runtime_error>(
124 std::source_location::current(),
125 "BufferUploadProcessor requires a valid buffer service");
126 }
127
129 "BufferUploadProcessor attached to buffer");
130}
131
132void BufferUploadProcessor::on_detach(std::shared_ptr<Buffer> buffer)
133{
134 m_staging_buffers.erase(buffer);
135
137 "BufferUploadProcessor detached from buffer");
138}
139
140bool BufferUploadProcessor::is_compatible_with(std::shared_ptr<Buffer> buffer) const
141{
142 return std::dynamic_pointer_cast<VKBuffer>(buffer) != nullptr;
143}
144
145void BufferUploadProcessor::configure_source(const std::shared_ptr<Buffer>& target, std::shared_ptr<Buffer> source)
146{
147 if (!std::dynamic_pointer_cast<VKBuffer>(target)) {
148 error<std::runtime_error>(
151 std::source_location::current(),
152 "Target must be a VKBuffer");
153 }
154
155 m_source_map[target] = std::move(source);
156
158 "Configured upload source for target buffer");
159}
160
161void BufferUploadProcessor::remove_source(const std::shared_ptr<Buffer>& target)
162{
163 m_source_map.erase(target);
164}
165
166std::shared_ptr<Buffer> BufferUploadProcessor::get_source(const std::shared_ptr<Buffer>& target) const
167{
168 auto it = m_source_map.find(target);
169 return it != m_source_map.end() ? it->second : nullptr;
170}
171
172}
#define MF_INFO(comp, ctx,...)
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
void processing_function(std::shared_ptr< Buffer > buffer) override
The core processing function that must be implemented by derived classes.
void configure_source(const std::shared_ptr< Buffer > &target, std::shared_ptr< Buffer > source)
Configure source buffer for a specific target.
void remove_source(const std::shared_ptr< Buffer > &target)
Remove source configuration for a target.
bool is_compatible_with(std::shared_ptr< Buffer > buffer) const override
Checks if this processor can handle the specified buffer type.
std::unordered_map< std::shared_ptr< Buffer >, std::shared_ptr< Buffer > > m_source_map
std::shared_ptr< Buffer > get_source(const std::shared_ptr< Buffer > &target) const
Get configured source for a target.
void upload_device_local(const std::shared_ptr< VKBuffer > &target, const Kakshya::DataVariant &data)
void on_detach(std::shared_ptr< Buffer > buffer) override
Called when this processor is detached from a buffer.
std::unordered_map< std::shared_ptr< Buffer >, std::shared_ptr< VKBuffer > > m_staging_buffers
void on_attach(std::shared_ptr< Buffer > buffer) override
Called when this processor is attached to a buffer.
void ensure_staging_buffer(const std::shared_ptr< VKBuffer > &target)
Registry::Service::BufferService * m_buffer_service
Definition VKBuffer.hpp:468
@ STAGING
Host-visible staging buffer (CPU-writable)
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
void upload_host_visible(const std::shared_ptr< VKBuffer > &target, const Kakshya::DataVariant &data)
Upload data to a host-visible buffer.
void upload_device_local(const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging_buffer, const Kakshya::DataVariant &data)
Upload data to a device-local buffer using a staging buffer.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:73
@ UNKNOWN
Unknown or undefined modality.
std::function< void(const std::shared_ptr< void > &)> initialize_buffer
Initialize a buffer object.
Backend buffer management service interface.