30 case SF::B8G8R8A8_SRGB:
31 return IF::BGRA8_SRGB;
32 case SF::B8G8R8A8_UNORM:
34 case SF::R8G8B8A8_SRGB:
35 return IF::RGBA8_SRGB;
36 case SF::R8G8B8A8_UNORM:
38 case SF::R16G16B16A16_SFLOAT:
39 case SF::A2B10G10R10_UNORM:
41 case SF::R32G32B32A32_SFLOAT:
44 return IF::BGRA8_SRGB;
51 : m_window(
std::move(window))
54 error<std::invalid_argument>(
57 std::source_location::current(),
58 "WindowContainer requires a valid window");
64 "WindowContainer created for window '{}' ({}x{})",
80 const auto& fmt =
m_window->get_create_info().container_format;
81 const uint32_t w =
m_window->get_create_info().width;
82 const uint32_t
h =
m_window->get_create_info().height;
83 const uint32_t c = fmt.color_channels;
88 {
static_cast<uint64_t
>(
h),
static_cast<uint64_t
>(w),
static_cast<uint64_t
>(c) },
91 const size_t sz =
static_cast<size_t>(w) *
h * c;
128 if (!src || src->empty())
132 const std::span<const uint8_t> src_span { src->data(), src->size() };
134 std::vector<DataVariant> result;
137 for (
const auto& r : group.regions) {
141 result.emplace_back(extract_nd_region<uint8_t>(src_span, r, dims));
142 }
catch (
const std::exception& e) {
144 "WindowContainer::get_region_data extraction failed — {}", e.what());
158 "WindowContainer::to_image — no readback data available for '{}'",
166 "WindowContainer::to_image — processed_data[0] is not uint8_t or is empty for '{}'",
172 const auto img_fmt = surface_format_to_image_format(fmt);
177 w,
h, img_fmt,
pixels->data());
181 "WindowContainer::to_image — TextureLoom::create_2d failed for '{}'",
189 const std::shared_ptr<Buffers::VKBuffer>& staging)
const
195 "WindowContainer::to_image(staging) — no readback data for '{}'",
203 "WindowContainer::to_image(staging) — processed_data[0] is not uint8_t or is empty for '{}'",
209 const auto img_fmt = surface_format_to_image_format(fmt);
215 auto img = loom.create_2d(w,
h, img_fmt,
nullptr);
218 "WindowContainer::to_image(staging) — VKImage allocation failed for '{}'",
223 loom.upload_data(img,
pixels->data(),
pixels->size(), staging);
233 "WindowContainer::region_to_image — no readback data for '{}'",
239 if (!src || src->empty()) {
241 "WindowContainer::region_to_image — processed_data[0] is not uint8_t or is empty for '{}'",
246 std::vector<uint8_t> cropped;
248 cropped = extract_region_data<uint8_t>(
249 std::span<const uint8_t> { src->data(), src->size() },
252 }
catch (
const std::exception& e) {
254 "WindowContainer::region_to_image — crop failed for '{}': {}",
255 m_window->get_create_info().title, e.what());
261 "WindowContainer::region_to_image — region must have at least 2 coordinates (SPATIAL_Y, SPATIAL_X)");
265 const auto rh =
static_cast<uint32_t
>(
267 const auto rw =
static_cast<uint32_t
>(
271 const auto img_fmt = surface_format_to_image_format(fmt);
274 rw, rh, img_fmt, cropped.data());
278 "WindowContainer::region_to_image — TextureLoom::create_2d failed ({}x{}) for '{}'",
279 rw, rh,
m_window->get_create_info().title);
288 "WindowContainer::set_region_data — write path not yet implemented");
314 const uint64_t idx = (coordinates[0] * w + coordinates[1]) * c + coordinates[2];
316 if (idx >=
pixels->size())
319 return static_cast<double>((*pixels)[idx]) / 255.0;
352 return (v && !v->empty()) ? v->data() :
nullptr;
360 return std::visit([](
const auto& v) {
return !v.empty(); },
m_processed_data[0]);
366 "WindowContainer::load_region — no-op. Register regions via add_region_group()");
372 "WindowContainer::unload_region — no-op. Remove regions via remove_region_group()");
422 if (old == new_state)
431 std::function<
void(
const std::shared_ptr<SignalSourceContainer>&,
ProcessingState)> callback)
455 auto readback = std::make_shared<WindowAccessProcessor>();
456 readback->on_attach(shared_from_this());
544 "WindowContainer::channel_data — not meaningful for interleaved image data; returning full surface");
577 const uint64_t row_elems = w * c;
578 const uint64_t offset = frame_index * row_elems;
580 if (offset + row_elems >
pixels->size())
584 cache.resize(row_elems);
585 for (uint64_t i = 0; i < row_elems; ++i)
586 cache[i] =
static_cast<double>((*
pixels)[offset + i]) / 255.0;
588 return { cache.data(), cache.size() };
597 std::ranges::fill(output, 0.0);
603 std::ranges::fill(output, 0.0);
609 const uint64_t row_elems = w * c;
610 const uint64_t frames_to_copy = std::min(num_frames,
h - start_frame);
611 const uint64_t elems_to_copy = std::min(frames_to_copy * row_elems,
612 static_cast<uint64_t
>(output.size()));
614 const uint64_t src_offset = start_frame * row_elems;
615 for (uint64_t i = 0; i < elems_to_copy; ++i)
616 output[i] =
static_cast<double>((*
pixels)[src_offset + i]) / 255.0;
618 if (elems_to_copy < output.size())
619 std::fill(output.begin() + elems_to_copy, output.end(), 0.0);
#define MF_INFO(comp, ctx,...)
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
const std::vector< float > * pixels
Type-erased accessor for NDData with semantic view construction.
uint64_t coordinates_to_linear_index(const std::vector< uint64_t > &coordinates) const override
Convert coordinates to linear index based on current memory layout.
ProcessingState get_processing_state() const override
Get the current processing state of the container.
std::atomic< uint32_t > m_registered_readers
std::function< void(const std::shared_ptr< SignalSourceContainer > &, ProcessingState)> m_state_callback
std::atomic< ProcessingState > m_processing_state
void set_processing_chain(const std::shared_ptr< DataProcessingChain > &chain) override
Set the processing chain for this container.
std::vector< double > m_frame_cache
void create_default_processor() override
Create and configure a default processor for this container.
DataAccess channel_data(size_t channel_index) override
Get channel data with semantic interpretation.
bool is_region_loaded(const Region ®ion) const override
Always returns true.
std::span< const double > get_frame(uint64_t frame_index) const override
Get a single frame of data efficiently.
Portal::Graphics::ImageFormat get_image_format() const
Portal ImageFormat corresponding to the live swapchain surface format.
void set_memory_layout(MemoryLayout layout) override
Set the memory layout for this container.
WindowContainer(std::shared_ptr< Core::Window > window)
Construct from an existing managed window.
void unregister_dimension_reader(uint32_t dimension_index) override
Unregister a reader for a specific dimension.
std::shared_ptr< Core::VKImage > to_image() const
Upload the full surface readback to a new VKImage.
void clear() override
Clear all data in the container.
void unregister_state_change_callback() override
Unregister the state change callback, if any.
std::shared_ptr< Core::Window > m_window
uint64_t get_total_elements() const override
Get the total number of elements in the container.
const void * get_raw_data() const override
Get a raw pointer to the underlying data storage.
std::shared_ptr< Core::VKImage > region_to_image(const Region ®ion) const
Crop a region from the last readback and upload it as a VKImage.
std::vector< DataDimension > get_dimensions() const override
Get the dimensions describing the structure of the data.
std::atomic< uint32_t > m_consumed_readers
const std::vector< DataVariant > & get_data() override
Get a reference to the raw data stored in the container.
void load_region(const Region ®ion) override
No-op.
void lock() override
Acquire a lock for thread-safe access.
std::shared_mutex m_data_mutex
std::vector< DataVariant > m_processed_data
std::atomic< bool > m_ready_for_processing
uint64_t get_num_frames() const override
Get the number of frames in the primary (temporal) dimension.
const RegionGroup & get_region_group(const std::string &name) const override
Get a region group by name.
std::unordered_map< std::string, RegionGroup > m_region_groups
void mark_dimension_consumed(uint32_t dimension_index, uint32_t reader_id) override
Mark a dimension as consumed for the current processing cycle.
uint32_t register_dimension_reader(uint32_t dimension_index) override
Register a reader for a specific dimension.
std::unordered_map< std::string, RegionGroup > get_all_region_groups() const override
Get all region groups in the container.
ContainerDataStructure m_structure
void unlock() override
Release a previously acquired lock.
std::vector< DataVariant > m_data
void update_processing_state(ProcessingState new_state) override
Update the processing state of the container.
void mark_ready_for_processing(bool ready) override
Mark the container as ready or not ready for processing.
std::vector< DataVariant > get_region_group_data(const RegionGroup &group) const override
Get data for multiple regions efficiently.
std::shared_ptr< DataProcessingChain > m_processing_chain
std::shared_ptr< DataProcessor > m_default_processor
uint64_t get_frame_size() const override
Get the number of elements that constitute one "frame".
bool has_data() const override
Check if the container currently holds any data.
void remove_region_group(const std::string &name) override
Remove a region group by name.
void set_default_processor(const std::shared_ptr< DataProcessor > &processor) override
Set the default data processor for this container.
MemoryLayout get_memory_layout() const override
Get the memory layout used by this container.
bool has_active_readers() const override
Check if any dimensions currently have active readers.
std::vector< DataVariant > get_segments_data(const std::vector< RegionSegment > &segments) const override
Get data for multiple region segments efficiently.
std::vector< DataVariant > get_region_data(const Region ®ion) const override
Extract data for all regions across all region groups that spatially intersect region.
void set_region_data(const Region ®ion, const std::vector< DataVariant > &data) override
Set data for a specific region.
std::shared_ptr< DataProcessor > get_default_processor() const override
Get the current default data processor.
void unload_region(const Region ®ion) override
No-op.
std::vector< DataAccess > all_channel_data() override
Get all channel data as accessors.
bool all_dimensions_consumed() const override
Check if all active dimensions have been consumed in this cycle.
double get_value_at(const std::vector< uint64_t > &coordinates) const override
Get a single value at the specified coordinates.
void get_frames(std::span< double > output, uint64_t start_frame, uint64_t num_frames) const override
Get multiple frames efficiently.
std::vector< DataVariant > & get_processed_data() override
Get a mutable reference to the processed data buffer.
void register_state_change_callback(std::function< void(const std::shared_ptr< SignalSourceContainer > &, ProcessingState)> callback) override
Register a callback to be invoked on processing state changes.
std::shared_ptr< DataProcessingChain > get_processing_chain() override
Get the current processing chain for this container.
void process_default() override
Process the container's data using the default processor.
bool is_ready_for_processing() const override
Check if the container is ready for processing.
void set_value_at(const std::vector< uint64_t > &coordinates, double value) override
Set a single value at the specified coordinates.
std::vector< uint64_t > linear_index_to_coordinates(uint64_t linear_index) const override
Convert linear index to coordinates based on current memory layout.
bool try_lock() override
Attempt to acquire a lock without blocking.
std::atomic< uint32_t > m_next_reader_id
void add_region_group(const RegionGroup &group) override
Add a named group of regions to the container.
static TextureLoom & instance()
std::shared_ptr< Core::VKImage > create_2d(uint32_t width, uint32_t height, ImageFormat format=ImageFormat::RGBA8, const void *data=nullptr, uint32_t mip_levels=1)
Create a 2D texture.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
ProcessingState
Represents the current processing lifecycle state of a container.
@ IDLE
Container is inactive with no data or not ready for processing.
uint64_t coordinates_to_linear(const std::vector< uint64_t > &coords, const std::vector< DataDimension > &dimensions)
Convert N-dimensional coordinates to a linear index for interleaved data.
@ IMAGE_COLOR
2D RGB/RGBA image
std::vector< uint64_t > linear_to_coordinates(uint64_t index, const std::vector< DataDimension > &dimensions)
Convert a linear index to N-dimensional coordinates for interleaved data.
MemoryLayout
Memory layout for multi-dimensional data.
@ ROW_MAJOR
C/C++ style (last dimension varies fastest)
bool regions_intersect(const Region &r1, const Region &r2) noexcept
Test whether two N-dimensional regions overlap on every shared axis.
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...
ImageFormat
User-friendly image format enum.
SurfaceFormat
Default pixel format for window surfaces (Vulkan-compatible)
std::vector< DataDimension > dimensions
static uint64_t get_height(const std::vector< DataDimension > &dimensions)
Extract height from image/video dimensions.
static uint64_t get_channel_count(const std::vector< DataDimension > &dimensions)
Extract channel count from dimensions.
static size_t get_frame_size(const std::vector< DataDimension > &dimensions)
Extract the size of non time dimensions (channel, spatial, frequency)
static ContainerDataStructure image_interleaved()
Create structure for interleaved image data.
static uint64_t get_total_elements(const std::vector< DataDimension > &dimensions)
Get total elements across all dimensions.
MemoryLayout memory_layout
static uint64_t get_width(const std::vector< DataDimension > &dimensions)
Extract width from image/video dimensions.
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.
std::string name
Descriptive name of the group.
Organizes related signal regions into a categorized collection.
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive)
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive)
Represents a point or span in N-dimensional space.