Download from GPU buffer to raw data (auto-detects host-visible vs device-local)
324{
325 if (!source) {
326 error<std::invalid_argument>(
327 Journal::Component::Buffers,
328 Journal::Context::BufferProcessing,
329 std::source_location::current(),
330 "download_from_gpu: source buffer is null");
331 }
332
333 if (size == 0) {
334 return;
335 }
336
337 auto temp_target = std::make_shared<VKBuffer>(
338 size, VKBuffer::Usage::STAGING, Kakshya::DataModality::UNKNOWN);
339
340 auto buffer_service = Registry::BackendRegistry::instance()
341 .get_service<Registry::Service::BufferService>();
342
343 if (!buffer_service) {
344 error<std::runtime_error>(
345 Journal::Component::Buffers,
346 Journal::Context::BufferProcessing,
347 std::source_location::current(),
348 "download_from_gpu requires a valid buffer service");
349 }
350
351 buffer_service->initialize_buffer(temp_target);
352
353 if (
source->is_host_visible()) {
355 } else {
356 std::shared_ptr<VKBuffer> staging_buf = staging;
357
358 if (!staging_buf) {
360 }
361
363 }
364
365 auto temp_data = temp_target->get_data();
366
367 if (temp_data.empty()) {
368 error<std::runtime_error>(
369 Journal::Component::Buffers,
370 Journal::Context::BufferProcessing,
371 std::source_location::current(),
372 "download_from_gpu: failed to retrieve data from temporary buffer");
373 }
374
375 if (temp_data.size() > 1) {
376 MF_ERROR(Journal::Component::Buffers, Journal::Context::BufferProcessing,
377 "download_from_gpu: unexpected multiple data variants in temporary buffer. Only the first will be used.");
378 }
379
380 Kakshya::DataAccess accessor(
381 const_cast<Kakshya::DataVariant&>(temp_data[0]),
382 {},
384
385 auto [
ptr, bytes, format_hint] = accessor.gpu_buffer();
386
387 std::memcpy(data,
ptr, std::min(size, bytes));
388}
#define MF_ERROR(comp, ctx,...)
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
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.