MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BufferDownloadProcessor.cpp
Go to the documentation of this file.
2
4
7
8#include "StagingUtils.hpp"
9
10namespace MayaFlux::Buffers {
11
16
22
23void BufferDownloadProcessor::processing_function(std::shared_ptr<Buffer> buffer)
24{
25 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
26 if (!vk_buffer) {
28 "BufferDownloadProcessor 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 target_it = m_target_map.find(buffer);
39 if (target_it == m_target_map.end() || !target_it->second) {
41 "BufferDownloadProcessor has no target configured for this buffer");
42 return;
43 }
44
45 if (vk_buffer->is_host_visible()) {
46 download_host_visible(vk_buffer);
47 } else {
48 download_device_local(vk_buffer);
49 }
50}
51
52void BufferDownloadProcessor::download_host_visible(const std::shared_ptr<VKBuffer>& source)
53{
54 auto target_it = m_target_map.find(source);
55 if (target_it == m_target_map.end() || !target_it->second) {
56 return;
57 }
58 auto target = target_it->second;
59
60 Buffers::download_host_visible(source, std::dynamic_pointer_cast<VKBuffer>(target));
61}
62
63void BufferDownloadProcessor::download_device_local(const std::shared_ptr<VKBuffer>& source)
64{
65 auto target_it = m_target_map.find(source);
66 if (target_it == m_target_map.end() || !target_it->second) {
67 return;
68 }
69 auto target = target_it->second;
70
72
73 auto staging_buffer = m_staging_buffers[source];
74
75 Buffers::download_device_local(source, std::dynamic_pointer_cast<VKBuffer>(target), staging_buffer);
76}
77
78void BufferDownloadProcessor::ensure_staging_buffer(const std::shared_ptr<VKBuffer>& source)
79{
80 auto it = m_staging_buffers.find(source);
81 if (it != m_staging_buffers.end() && it->second->get_size_bytes() >= source->get_size_bytes() && it->second->is_initialized()) {
82 return;
83 }
84
85 auto staging_buffer = std::make_shared<VKBuffer>(
86 source->get_size_bytes(),
89
90 if (!m_buffer_service) {
91 error<std::runtime_error>(
94 std::source_location::current(),
95 "No processing context available for staging buffer initialization");
96 }
97
98 if (!staging_buffer->is_initialized()) {
99 try {
100 m_buffer_service->initialize_buffer(staging_buffer);
101 } catch (const std::exception& e) {
102 error_rethrow(
105 std::source_location::current(),
106 "Failed to initialize staging buffer: {}", e.what());
107 }
108 }
109
110 m_staging_buffers[source] = staging_buffer;
111
113 "Created staging buffer for download: {} bytes", staging_buffer->get_size_bytes());
114}
115
116void BufferDownloadProcessor::on_attach(std::shared_ptr<Buffer> buffer)
117{
118 if (!is_compatible_with(buffer)) {
119 error<std::runtime_error>(
122 std::source_location::current(),
123 "BufferDownloadProcessor can only be attached to VKBuffer");
124 }
125
126 if (!m_buffer_service) {
129 }
130
131 if (!m_buffer_service) {
132 error<std::runtime_error>(
135 std::source_location::current(),
136 "No processing context available for BufferDownloadProcessor");
137 }
138
140 "BufferDownloadProcessor attached");
141}
142
143void BufferDownloadProcessor::on_detach(std::shared_ptr<Buffer> buffer)
144{
145 m_staging_buffers.erase(buffer);
146
148 "BufferDownloadProcessor detached");
149}
150
151bool BufferDownloadProcessor::is_compatible_with(std::shared_ptr<Buffer> buffer) const
152{
153 return std::dynamic_pointer_cast<VKBuffer>(buffer) != nullptr;
154}
155
156void BufferDownloadProcessor::configure_target(const std::shared_ptr<Buffer>& source, std::shared_ptr<Buffer> target)
157{
158 if (!std::dynamic_pointer_cast<VKBuffer>(source)) {
159 error<std::runtime_error>(
162 std::source_location::current(),
163 "Source must be a VKBuffer");
164 }
165
166 m_target_map[source] = std::move(target);
167
169 "Configured download target for source buffer");
170}
171
172void BufferDownloadProcessor::remove_target(const std::shared_ptr<Buffer>& source)
173{
174 m_target_map.erase(source);
175}
176
177std::shared_ptr<Buffer> BufferDownloadProcessor::get_target(const std::shared_ptr<Buffer>& source) const
178{
179 auto it = m_target_map.find(source);
180 return it != m_target_map.end() ? it->second : nullptr;
181}
182
183} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
std::unordered_map< std::shared_ptr< Buffer >, std::shared_ptr< VKBuffer > > m_staging_buffers
std::shared_ptr< Buffer > get_target(const std::shared_ptr< Buffer > &source) const
Get configured target for a source.
void download_device_local(const std::shared_ptr< VKBuffer > &source)
std::unordered_map< std::shared_ptr< Buffer >, std::shared_ptr< Buffer > > m_target_map
void on_detach(std::shared_ptr< Buffer > buffer) override
Called when this processor is detached from a buffer.
void configure_target(const std::shared_ptr< Buffer > &source, std::shared_ptr< Buffer > target)
Configure target buffer for a specific source.
void processing_function(std::shared_ptr< Buffer > buffer) override
The core processing function that must be implemented by derived classes.
void download_host_visible(const std::shared_ptr< VKBuffer > &source)
void ensure_staging_buffer(const std::shared_ptr< VKBuffer > &source)
void remove_target(const std::shared_ptr< Buffer > &source)
Remove target configuration for a source.
void on_attach(std::shared_ptr< Buffer > buffer) override
Called when this processor is attached to a buffer.
bool is_compatible_with(std::shared_ptr< Buffer > buffer) const override
Checks if this processor can handle the specified buffer type.
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.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
void download_host_visible(const std::shared_ptr< VKBuffer > &source, const std::shared_ptr< VKBuffer > &target)
Download data from a host-visible buffer.
void download_device_local(const std::shared_ptr< VKBuffer > &source, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging_buffer)
Download data from a device-local buffer using a staging buffer.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
@ UNKNOWN
Unknown or undefined modality.
std::function< void(const std::shared_ptr< void > &)> initialize_buffer
Initialize a buffer object.
Backend buffer management service interface.