MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ download_image_data()

void MayaFlux::Core::BackendResourceManager::download_image_data ( std::shared_ptr< VKImage image,
void *  data,
size_t  size,
const std::shared_ptr< Buffers::VKBuffer > &  staging = nullptr,
bool  deferred = false,
vk::ImageLayout  restore_layout = vk::ImageLayout::eShaderReadOnlyOptimal,
vk::PipelineStageFlags  restore_stage = vk::PipelineStageFlagBits::eFragmentShader 
)

Download image data to a host pointer.

When staging is null, allocates and destroys a per-call staging buffer internally. When staging is supplied, it is used as-is and not destroyed after — the caller owns its lifetime.

When deferred is false (default), submits under a fence and blocks the calling thread on vkWaitForFences. This is non-blocking relative to the graphics queue (waitIdle is never called). When deferred is true and staging is supplied, records commands for deferred submission; the caller is responsible for flushing.

Parameters
imageSource image.
dataDestination host pointer, at least size bytes.
sizeByte count to read.
stagingPersistent host-visible buffer, or nullptr for per-call allocation.
deferredRecord for deferred submission (requires staging).
restore_layoutLayout to restore after the copy.
restore_stagePipeline stage that consumes the image after restore.

Definition at line 864 of file BackendResoureManager.cpp.

872{
873 if (!image || !data) {
875 "download_image_data: invalid parameters");
876 return;
877 }
878
879 std::shared_ptr<Buffers::VKBuffer> local_staging;
880 Buffers::VKBuffer* active_staging {};
881 if (!staging) {
882 local_staging = std::make_shared<Buffers::VKBuffer>(
883 size,
884 Buffers::VKBuffer::Usage::STAGING,
886 initialize_buffer(local_staging);
887 active_staging = local_staging.get();
888 } else {
889 active_staging = staging.get();
890 }
891
892 auto record_command = [&](vk::CommandBuffer cmd) {
893 vk::ImageMemoryBarrier barrier {};
894 barrier.oldLayout = image->get_current_layout();
895 barrier.newLayout = vk::ImageLayout::eTransferSrcOptimal;
896 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
897 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
898 barrier.image = image->get_image();
899 barrier.subresourceRange.aspectMask = image->get_aspect_flags();
900 barrier.subresourceRange.baseMipLevel = 0;
901 barrier.subresourceRange.levelCount = image->get_mip_levels();
902 barrier.subresourceRange.baseArrayLayer = 0;
903 barrier.subresourceRange.layerCount = image->get_array_layers();
904 barrier.srcAccessMask = vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eShaderRead;
905 barrier.dstAccessMask = vk::AccessFlagBits::eTransferRead;
906
907 cmd.pipelineBarrier(
908 vk::PipelineStageFlagBits::eFragmentShader | vk::PipelineStageFlagBits::eComputeShader,
909 vk::PipelineStageFlagBits::eTransfer,
910 vk::DependencyFlags {}, {}, {}, barrier);
911
912 vk::BufferImageCopy region {};
913 region.bufferOffset = 0;
914 region.bufferRowLength = 0;
915 region.bufferImageHeight = 0;
916 region.imageSubresource.aspectMask = image->get_aspect_flags();
917 region.imageSubresource.mipLevel = 0;
918 region.imageSubresource.baseArrayLayer = 0;
919 region.imageSubresource.layerCount = image->get_array_layers();
920 region.imageOffset = vk::Offset3D { 0, 0, 0 };
921 region.imageExtent = vk::Extent3D {
922 image->get_width(),
923 image->get_height(),
924 image->get_depth()
925 };
926
927 cmd.copyImageToBuffer(
928 image->get_image(),
929 vk::ImageLayout::eTransferSrcOptimal,
930 active_staging->get_buffer(),
931 1, &region);
932
933 barrier.oldLayout = vk::ImageLayout::eTransferSrcOptimal;
934 barrier.newLayout = restore_layout;
935 barrier.srcAccessMask = vk::AccessFlagBits::eTransferRead;
936 barrier.dstAccessMask = vk::AccessFlagBits::eMemoryRead;
937
938 cmd.pipelineBarrier(
939 vk::PipelineStageFlagBits::eTransfer,
940 restore_stage,
941 vk::DependencyFlags {}, {}, {}, barrier);
942 };
943
944 if (deferred && staging) {
945 record_deferred_commands(record_command);
946 image->set_current_layout(restore_layout);
947 return;
948 }
949
950 auto device = m_context.get_device();
951
953 record_command(cmd);
954 cmd.end();
955
956 vk::FenceCreateInfo fence_info {};
957 vk::Fence fence = device.createFence(fence_info);
958
959 vk::SubmitInfo submit_info {};
960 submit_info.commandBufferCount = 1;
961 submit_info.pCommandBuffers = &cmd;
962
963 if (auto result = m_context.get_graphics_queue().submit(1, &submit_info, fence);
964 result != vk::Result::eSuccess) {
966 "download_image_data: queue submit failed: {}", vk::to_string(result));
967 device.destroyFence(fence);
969 if (local_staging)
970 cleanup_buffer(local_staging);
971 return;
972 }
973
974 image->set_current_layout(restore_layout);
975
976 if (auto result = device.waitForFences(1, &fence, VK_TRUE, UINT64_MAX);
977 result != vk::Result::eSuccess) {
979 "download_image_data: waitForFences failed: {}", vk::to_string(result));
980 }
981
982 vk::MappedMemoryRange range { active_staging->get_buffer_resources().memory, 0, VK_WHOLE_SIZE };
983 if (auto result = device.invalidateMappedMemoryRanges(1, &range);
984 result != vk::Result::eSuccess) {
986 "download_image_data: invalidate failed: {}", vk::to_string(result));
987 }
988
989 void* mapped = active_staging->get_mapped_ptr();
990 if (mapped) {
991 std::memcpy(data, mapped, size);
992 } else {
994 "download_image_data: staging buffer has no mapped pointer");
995 }
996
997 device.destroyFence(fence);
999
1000 if (local_staging)
1001 cleanup_buffer(local_staging);
1002
1004 "download_image_data: {} bytes from image {}x{}",
1005 size, image->get_width(), image->get_height());
1006}
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
vk::Fence fence
vk::CommandBuffer cmd
IO::ImageData image
Definition Decoder.cpp:64
void cleanup_buffer(const std::shared_ptr< Buffers::VKBuffer > &buffer)
Cleanup a buffer and release associated resources.
void initialize_buffer(const std::shared_ptr< Buffers::VKBuffer > &buffer)
Initialize a buffer for use with the graphics backend.
void record_deferred_commands(const std::function< void(vk::CommandBuffer)> &recorder)
Record deferred command recording for buffer operations.
void free_command_buffer(vk::CommandBuffer command_buffer)
Free a command buffer back to the pool.
vk::CommandBuffer begin_single_time_commands()
Begin single-time command (for transfers, etc.)
vk::Device get_device() const
Get logical device.
Definition VKContext.hpp:49
vk::Queue get_graphics_queue() const
Get graphics queue.
Definition VKContext.hpp:54
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
@ IMAGE_COLOR
2D RGB/RGBA image
std::vector< double > range(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Value range (max - min) per window.
Definition Analysis.cpp:452

References MayaFlux::Core::VKCommandManager::begin_single_time_commands(), cleanup_buffer(), cmd, MayaFlux::Journal::Core, fence, MayaFlux::Core::VKCommandManager::free_command_buffer(), MayaFlux::Core::VKContext::get_device(), MayaFlux::Core::VKContext::get_graphics_queue(), MayaFlux::Journal::GraphicsBackend, image, MayaFlux::Kakshya::IMAGE_COLOR, initialize_buffer(), m_command_manager, m_context, MF_DEBUG, MF_ERROR, and record_deferred_commands().

Referenced by MayaFlux::Portal::Graphics::TextureLoom::download_data(), and MayaFlux::Core::BackendWindowHandler::setup_backend_service().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: