14 target->get_modality());
16 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
18 if (bytes > target->get_size_bytes()) {
19 error<std::runtime_error>(
22 std::source_location::current(),
23 "Upload data size {} exceeds buffer capacity {}",
24 bytes, target->get_size_bytes());
27 auto& target_resources = target->get_buffer_resources();
28 void* mapped = target_resources.mapped_ptr;
30 error<std::runtime_error>(
33 std::source_location::current(),
34 "Host-visible buffer has no mapped pointer");
37 std::memcpy(mapped, ptr, bytes);
39 target->mark_dirty_range(0, bytes);
44 if (!buffer_service) {
45 error<std::runtime_error>(
48 std::source_location::current(),
49 "upload_host_visible requires a valid buffer service");
52 auto dirty_ranges = target->get_and_clear_dirty_ranges();
53 for (
auto& [offset, size] : dirty_ranges) {
54 buffer_service->flush_range(
55 target_resources.memory,
66 target->get_modality());
68 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
70 if (bytes > target->get_size_bytes()) {
71 error<std::runtime_error>(
74 std::source_location::current(),
75 "Upload data size {} exceeds buffer capacity {}",
76 bytes, target->get_size_bytes());
79 auto& staging_resources = staging_buffer->get_buffer_resources();
81 void* staging_mapped = staging_resources.mapped_ptr;
82 if (!staging_mapped) {
83 error<std::runtime_error>(
86 std::source_location::current(),
87 "Staging buffer has no mapped pointer");
90 std::memcpy(staging_mapped, ptr, bytes);
91 staging_buffer->mark_dirty_range(0, bytes);
96 if (!buffer_service) {
97 error<std::runtime_error>(
100 std::source_location::current(),
101 "upload_host_visible requires a valid buffer service");
104 auto dirty_ranges = staging_buffer->get_and_clear_dirty_ranges();
105 for (
auto& [offset, size] : dirty_ranges) {
106 buffer_service->flush_range(
107 staging_resources.memory,
112 buffer_service->execute_immediate([&](
void* ptr) {
113 vk::BufferCopy copy_region;
114 copy_region.srcOffset = 0;
115 copy_region.dstOffset = 0;
116 copy_region.size = bytes;
118 vk::CommandBuffer cmd(
static_cast<VkCommandBuffer
>(ptr));
121 staging_buffer->get_buffer(),
122 target->get_buffer(),
129 auto& source_resources = source->get_buffer_resources();
130 void* mapped = source_resources.mapped_ptr;
132 error<std::runtime_error>(
135 std::source_location::current(),
136 "Host-visible buffer has no mapped pointer");
139 source->mark_invalid_range(0, source->get_size_bytes());
144 if (!buffer_service) {
145 error<std::runtime_error>(
148 std::source_location::current(),
149 "upload_host_visible requires a valid buffer service");
152 auto invalid_ranges = source->get_and_clear_invalid_ranges();
153 for (
auto& [offset, size] : invalid_ranges) {
154 buffer_service->invalidate_range(
155 source_resources.memory,
160 std::vector<uint8_t> raw_bytes(source->get_size_bytes());
161 std::memcpy(raw_bytes.data(), mapped, source->get_size_bytes());
163 std::dynamic_pointer_cast<VKBuffer>(target)->set_data({ raw_bytes });
166void download_device_local(
const std::shared_ptr<VKBuffer>& source,
const std::shared_ptr<VKBuffer>& target,
const std::shared_ptr<VKBuffer>& staging_buffer)
171 if (!buffer_service) {
172 error<std::runtime_error>(
175 std::source_location::current(),
176 "upload_host_visible requires a valid buffer service");
179 buffer_service->execute_immediate([&](
void* ptr) {
180 vk::BufferCopy copy_region;
181 copy_region.srcOffset = 0;
182 copy_region.dstOffset = 0;
183 copy_region.size = source->get_size_bytes();
185 auto cmd =
static_cast<vk::CommandBuffer*
>(ptr);
188 source->get_buffer(),
189 staging_buffer->get_buffer(),
193 staging_buffer->mark_invalid_range(0, source->get_size_bytes());
195 auto& staging_resources = staging_buffer->get_buffer_resources();
196 auto invalid_ranges = staging_buffer->get_and_clear_invalid_ranges();
197 for (
auto& [offset, size] : invalid_ranges) {
198 buffer_service->invalidate_range(
199 staging_resources.memory,
204 void* staging_mapped = staging_resources.mapped_ptr;
205 if (!staging_mapped) {
206 error<std::runtime_error>(
209 std::source_location::current(),
210 "Staging buffer has no mapped pointer");
213 std::vector<uint8_t> raw_bytes(source->get_size_bytes());
214 std::memcpy(raw_bytes.data(), staging_mapped, source->get_size_bytes());
216 std::dynamic_pointer_cast<VKBuffer>(target)->set_data({ raw_bytes });
221 return buffer && !buffer->is_host_visible();
226 auto buffer = std::make_shared<VKBuffer>(
233 if (!buffer_service) {
234 error<std::runtime_error>(
237 std::source_location::current(),
238 "create_staging_buffer requires a valid buffer service");
241 buffer_service->initialize_buffer(buffer);
249 const std::shared_ptr<VKBuffer>& target,
250 const std::shared_ptr<VKBuffer>& staging)
253 error<std::invalid_argument>(
256 std::source_location::current(),
257 "upload_to_gpu: target buffer is null");
264 std::vector<uint8_t> raw_bytes(size);
265 std::memcpy(raw_bytes.data(), data, size);
268 if (target->is_host_visible()) {
271 std::shared_ptr<VKBuffer> staging_buf = staging;
282 const std::shared_ptr<VKBuffer>& source,
285 const std::shared_ptr<VKBuffer>& staging)
288 error<std::invalid_argument>(
291 std::source_location::current(),
292 "download_from_gpu: source buffer is null");
299 auto temp_target = std::make_shared<VKBuffer>(
302 if (source->is_host_visible()) {
305 std::shared_ptr<VKBuffer> staging_buf = staging;
314 auto temp_data = temp_target->get_data();
316 if (temp_data.empty()) {
317 error<std::runtime_error>(
320 std::source_location::current(),
321 "download_from_gpu: failed to retrieve data from temporary buffer");
324 if (temp_data.size() > 1) {
326 "download_from_gpu: unexpected multiple data variants in temporary buffer. Only the first will be used.");
332 source->get_modality());
334 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
336 std::memcpy(data, ptr, std::min(size, bytes));
340 const std::shared_ptr<AudioBuffer>& audio_buffer,
341 const std::shared_ptr<VKBuffer>& gpu_buffer,
342 const std::shared_ptr<VKBuffer>& staging)
344 if (gpu_buffer->get_format() != vk::Format::eR64Sfloat && gpu_buffer->get_format() != vk::Format::eUndefined) {
346 "GPU buffer format is {} but audio requires R64Sfloat for double precision. "
347 "Create VKBuffer with DataModality::AUDIO_1D or AUDIO_MULTICHANNEL.",
348 vk::to_string(gpu_buffer->get_format()));
349 error<std::runtime_error>(
352 std::source_location::current(),
353 "GPU buffer format mismatch for audio upload");
356 auto& audio_data = audio_buffer->get_data();
358 if (audio_data.empty()) {
360 "AudioBuffer contains no data to upload");
364 const void* data_ptr = audio_data.data();
365 size_t data_bytes = audio_data.size() *
sizeof(double);
370 "Uploaded {} bytes of double-precision audio to GPU", data_bytes);
374 const std::shared_ptr<VKBuffer>& gpu_buffer,
375 const std::shared_ptr<AudioBuffer>& audio_buffer,
376 const std::shared_ptr<VKBuffer>& staging)
380 auto dimensions = std::vector<Kakshya::DataDimension> {
382 gpu_buffer->get_size_bytes() /
sizeof(
double),
386 gpu_buffer, downloaded_data, dimensions,
389 auto double_view = accessor.
view<
double>();
391 audio_buffer->get_data() = std::vector<double>(double_view.begin(), double_view.end());
394 "Downloaded {} samples of double-precision audio from GPU", double_view.size());
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
@ STAGING
Host-visible staging buffer (CPU-writable)
auto gpu_buffer() const
Get raw buffer info for GPU upload.
auto view() const
Get explicit typed view of data.
Type-erased accessor for NDData with semantic view construction.
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_audio_to_gpu(const std::shared_ptr< AudioBuffer > &audio_buffer, const std::shared_ptr< VKBuffer > &gpu_buffer, const std::shared_ptr< VKBuffer > &staging)
Upload AudioBuffer to GPU (always double precision)
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.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
bool is_device_local(const std::shared_ptr< VKBuffer > &buffer)
Check if buffer is device-local (staging needed)
void download_audio_from_gpu(const std::shared_ptr< VKBuffer > &gpu_buffer, const std::shared_ptr< AudioBuffer > &audio_buffer, const std::shared_ptr< VKBuffer > &staging)
Download GPU buffer to AudioBuffer (expects double precision)
void download_from_gpu(const std::shared_ptr< VKBuffer > &source, void *data, size_t size, const std::shared_ptr< VKBuffer > &staging)
Download from GPU buffer to raw data (auto-detects host-visible vs device-local)
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.
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
@ 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.
@ AUDIO_1D
1D audio signal
@ UNKNOWN
Unknown or undefined modality.
static DataDimension time(uint64_t samples, std::string name="time")
Convenience constructor for a temporal (time) dimension.
Backend buffer management service interface.