22 Registry::Service::DisplayService* get_display_service()
28 "SurfaceUtils: DisplayService not available");
37 Registry::Service::BufferService* get_buffer_service()
43 "SurfaceUtils: BufferService not available");
52 std::vector<DataDimension> make_pixel_dimensions(
53 uint32_t pixel_width, uint32_t pixel_height, uint32_t channel_count)
57 {
static_cast<uint64_t
>(pixel_height),
58 static_cast<uint64_t
>(pixel_width),
59 static_cast<uint64_t
>(channel_count) },
76 void fill_variant_from_raw(
79 const Core::SurfaceFormatTraits& traits,
82 if (traits.is_float && traits.bits_per_channel == 32U) {
83 const size_t n = byte_count /
sizeof(float);
84 std::vector<float> v(n);
85 std::memcpy(v.data(), raw, byte_count);
87 }
else if (traits.bits_per_channel == 16U) {
88 const size_t n = byte_count /
sizeof(uint16_t);
89 std::vector<uint16_t> v(n);
90 std::memcpy(v.data(), raw, byte_count);
92 }
else if (traits.is_packed) {
93 const size_t n = byte_count /
sizeof(uint32_t);
94 std::vector<uint32_t> v(n);
95 std::memcpy(v.data(), raw, byte_count);
98 std::vector<uint8_t> v(byte_count);
99 std::memcpy(v.data(), raw, byte_count);
111 const std::shared_ptr<Core::Window>& window)
113 auto* svc = get_display_service();
117 const int raw = svc->get_swapchain_format(std::static_pointer_cast<void>(window));
122 const std::shared_ptr<Core::Window>& window,
125 uint32_t pixel_width,
126 uint32_t pixel_height,
129 static std::vector<DataDimension> s_empty_dims;
130 static DataVariant s_empty_var = std::vector<uint8_t> {};
133 auto* display = get_display_service();
137 const auto window_handle = std::static_pointer_cast<void>(window);
139 const int raw_fmt = display->get_swapchain_format(window_handle);
140 const auto vk_fmt =
static_cast<vk::Format
>(raw_fmt);
145 uint32_t full_w = 0, full_h = 0;
146 display->get_swapchain_extent(window_handle, full_w, full_h);
147 if (full_w == 0 || full_h == 0)
150 auto frame = display->get_last_frame(window_handle);
151 if (!frame || frame->empty()) {
153 "SurfaceUtils::readback_region: no captured frame for '{}'",
154 window->get_create_info().title);
158 if (x_offset + pixel_width > full_w || y_offset + pixel_height > full_h) {
160 "SurfaceUtils::readback_region: region {}x{} at ({},{}) exceeds surface {}x{} for '{}'",
161 pixel_width, pixel_height, x_offset, y_offset, full_w, full_h,
162 window->get_create_info().title);
166 const size_t full_row =
static_cast<size_t>(full_w) * bpp;
167 const size_t region_row =
static_cast<size_t>(pixel_width) * bpp;
168 const size_t byte_count = region_row * pixel_height;
170 const size_t expected = full_row * full_h;
171 if (frame->size() < expected) {
173 "SurfaceUtils::readback_region: frame {} bytes, expected {} for '{}'",
174 frame->size(), expected, window->get_create_info().title);
178 std::vector<uint8_t> region(byte_count);
179 const uint8_t* base = frame->data()
180 +
static_cast<size_t>(y_offset) * full_row
181 +
static_cast<size_t>(x_offset) * bpp;
183 for (uint32_t row = 0; row < pixel_height; ++row) {
184 std::memcpy(region.data() + row * region_row,
185 base + row * full_row,
189 fill_variant_from_raw(region.data(), byte_count, traits, out_variant);
191 auto dims = make_pixel_dimensions(pixel_width, pixel_height, traits.channel_count);
196 const std::shared_ptr<Core::Window>& window)
198 auto* svc = get_display_service();
202 uint32_t w = 0,
h = 0;
203 svc->get_swapchain_extent(std::static_pointer_cast<void>(window), w,
h);
209 auto* svc = get_display_service();
213 uint32_t w = 0,
h = 0;
214 svc->get_swapchain_extent(std::static_pointer_cast<void>(window), w,
h);
215 return w > 0 &&
h > 0;
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
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.
GraphicsSurfaceInfo::SurfaceFormat from_vk_format(vk::Format fmt)
Reverse-map a vk::Format to the nearest GraphicsSurfaceInfo::SurfaceFormat.
SurfaceFormatTraits get_surface_format_traits(GraphicsSurfaceInfo::SurfaceFormat fmt)
Query DataVariant-dispatch traits for a surface format.
uint32_t vk_format_bytes_per_pixel(vk::Format fmt)
Byte width of a single pixel for a given Vulkan format.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
bool is_readback_available(const std::shared_ptr< Core::Window > &window)
Check whether a completed frame is currently available for readback.
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.
@ UNKNOWN
Unknown or undefined modality.
@ IMAGE_COLOR
2D RGB/RGBA image
@ ROW_MAJOR
C/C++ style (last dimension varies fastest)
std::pair< uint32_t, uint32_t > query_surface_extent(const std::shared_ptr< Core::Window > &window)
Query the current pixel dimensions of the window's swapchain.
DataAccess 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 ...
Core::GraphicsSurfaceInfo::SurfaceFormat query_surface_format(const std::shared_ptr< Core::Window > &window)
Query the actual vk::Format in use by the window's live swapchain, translated back to the MayaFlux su...
SurfaceFormat
Default pixel format for window surfaces (Vulkan-compatible)
@ B8G8R8A8_SRGB
Most common - 8-bit SRGB.
static std::vector< DataDimension > create_dimensions(DataModality modality, const std::vector< uint64_t > &shape, MemoryLayout layout=MemoryLayout::ROW_MAJOR)
Create dimension descriptors for a data modality.