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

◆ readback_region()

MAYAFLUX_API DataAccess MayaFlux::Kakshya::readback_region ( const std::shared_ptr< Core::Window > &  window,
uint32_t  x_offset,
uint32_t  y_offset,
uint32_t  pixel_width,
uint32_t  pixel_height,
DataVariant out_variant 
)

Read a pixel rectangle from the last completed swapchain frame into a DataVariant whose element type matches the live swapchain format.

Format → DataVariant mapping: B8G8R8A8_SRGB / R8G8B8A8_SRGB / B8G8R8A8_UNORM / R8G8B8A8_UNORM → std::vector<uint8_t> (4 bytes/pixel) R16G16B16A16_SFLOAT → std::vector<uint16_t> (8 bytes/pixel, raw half-float bits) A2B10G10R10_UNORM → std::vector<uint32_t> (4 bytes/pixel, packed word) R32G32B32A32_SFLOAT → std::vector<float> (16 bytes/pixel)

"Last completed frame" semantics: the swapchain image whose in-flight fence has already signaled. Safe to call without stalling the render pipeline.

Dimensions on the returned DataAccess (IMAGE_COLOR convention): [0] SPATIAL_Y — pixel_height [1] SPATIAL_X — pixel_width [2] CHANNEL — channel_count derived from format traits

Parameters
windowWindow whose surface is being read.
x_offsetLeft edge of the pixel rectangle (inclusive).
y_offsetTop edge of the pixel rectangle (inclusive).
pixel_widthWidth of the rectangle in pixels.
pixel_heightHeight of the rectangle in pixels.
out_variantReceives the typed pixel data. Left unchanged on failure.
Returns
DataAccess wrapping out_variant with IMAGE_COLOR modality. Returns a default-constructed DataAccess on failure.

Definition at line 122 of file SurfaceUtils.cpp.

129{
130 static std::vector<DataDimension> s_empty_dims;
131 static DataVariant s_empty_var = std::vector<uint8_t> {};
132 static DataAccess s_fail { s_empty_var, s_empty_dims, DataModality::UNKNOWN };
133
134 auto* display = get_display_service();
135 auto* buf_svc = get_buffer_service();
136 if (!display || !buf_svc)
137 return s_fail;
138
139 const auto window_handle = std::static_pointer_cast<void>(window);
140
141 const int raw_fmt = display->get_swapchain_format(window_handle);
142 const auto vk_fmt = static_cast<vk::Format>(raw_fmt);
143 const auto mf_fmt = Core::from_vk_format(vk_fmt);
144 const auto traits = Core::get_surface_format_traits(mf_fmt);
145 const uint32_t bpp = Core::vk_format_bytes_per_pixel(vk_fmt);
146
147 const size_t byte_count = static_cast<size_t>(pixel_width) * pixel_height * bpp;
148
149 auto staging = Buffers::create_staging_buffer(byte_count);
150 if (!staging) {
151 MF_RT_ERROR(Journal::Component::Kakshya, Journal::Context::ContainerProcessing,
152 "SurfaceUtils::readback_region: staging allocation failed ({} bytes) for '{}'",
153 byte_count, window->get_create_info().title);
154 return s_fail;
155 }
156
157 const auto& res = staging->get_buffer_resources();
158
159 buf_svc->invalidate_range(res.memory, 0, byte_count);
160 void* mapped = buf_svc->map_buffer(res.memory, 0, byte_count);
161 if (!mapped) {
162 buf_svc->destroy_buffer(std::static_pointer_cast<void>(staging));
163 MF_RT_ERROR(Journal::Component::Kakshya, Journal::Context::ContainerProcessing,
164 "SurfaceUtils::readback_region: staging map failed for '{}'",
165 window->get_create_info().title);
166 return s_fail;
167 }
168
169 const bool ok = display->readback_swapchain_region(
170 window_handle,
171 mapped,
172 x_offset, y_offset,
173 pixel_width, pixel_height,
174 byte_count);
175
176 if (!ok) {
177 buf_svc->unmap_buffer(res.memory);
178 buf_svc->destroy_buffer(std::static_pointer_cast<void>(staging));
179 MF_RT_ERROR(Journal::Component::Kakshya, Journal::Context::ContainerProcessing,
180 "SurfaceUtils::readback_region: GPU copy failed for '{}'",
181 window->get_create_info().title);
182 return s_fail;
183 }
184
185 fill_variant_from_raw(mapped, byte_count, traits, out_variant);
186 buf_svc->unmap_buffer(res.memory);
187 buf_svc->destroy_buffer(std::static_pointer_cast<void>(staging));
188
189 auto dims = make_pixel_dimensions(pixel_width, pixel_height, traits.channel_count);
190 return { out_variant, dims, DataModality::IMAGE_COLOR };
191}
#define MF_RT_ERROR(comp, ctx,...)
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

References MayaFlux::Journal::ContainerProcessing, MayaFlux::Buffers::create_staging_buffer(), MayaFlux::Core::from_vk_format(), MayaFlux::Core::get_surface_format_traits(), IMAGE_COLOR, MayaFlux::Journal::Kakshya, MF_RT_ERROR, UNKNOWN, and MayaFlux::Core::vk_format_bytes_per_pixel().

Referenced by MayaFlux::Kakshya::WindowAccessProcessor::process().

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