MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SpatialTransfer.cpp
Go to the documentation of this file.
1#include "SpatialTransfer.hpp"
2
3#include "SpatialExport.hpp"
4
6
9
12
14
15#include <chrono>
16
17namespace MayaFlux::IO {
18
20 std::string stream_name,
21 std::shared_ptr<Buffers::NetworkGeometryBuffer> buffer)
22{
24 .stream_name = std::move(stream_name),
25 .capture_frame = [buffer](SpatialCache& cache, const std::string& name) {
26 return write_network_geometry_buffer_sample(cache, name, buffer);
27 }
28 };
29}
30
31namespace {
32 std::atomic<uint32_t> g_next_spatial_capture_id { 1 };
33
34 bool run_sources(SpatialCache& cache, const std::vector<SpatialCaptureSource>& sources)
35 {
36 for (const auto& source : sources) {
37 if (!source.capture_frame(cache, source.stream_name)) {
39 "SpatialCapture: source '{}' failed", source.stream_name);
40 return false;
41 }
42 }
43 return true;
44 }
45} // namespace
46
48 std::shared_ptr<SpatialCache> cache;
49 std::vector<SpatialCaptureSource> sources;
50 uint32_t max_frames {};
51 std::atomic<uint32_t> frame { 0 };
52 std::atomic<bool> stop_requested { false };
53 std::atomic<bool> recording { false };
54};
55
57{
58 if (state.max_frames != 0 && state.frame.load(std::memory_order_relaxed) >= state.max_frames) {
59 return false;
60 }
61
62 if (!run_sources(*state.cache, state.sources)) {
64 "SpatialCapture: failed at frame {}", state.frame.load(std::memory_order_relaxed));
65 return false;
66 }
67
68 state.frame.fetch_add(1, std::memory_order_relaxed);
69 return true;
70}
71
73 Vruta::TaskScheduler& scheduler,
74 std::shared_ptr<SpatialCache> cache,
75 std::vector<SpatialCaptureSource> sources)
76 : m_scheduler(scheduler)
77 , m_cache(std::move(cache))
78 , m_sources(std::move(sources))
79{
80}
81
86
88{
89 return m_state && m_state->recording.load(std::memory_order_acquire);
90}
91
93{
94 return m_state ? m_state->frame.load(std::memory_order_relaxed) : 0;
95}
96
97void SpatialCapture::start(uint32_t max_frames, uint64_t frame_interval)
98{
99 if (!m_cache) {
101 "SpatialCapture: cannot start, null cache");
102 return;
103 }
104
105 if (m_sources.empty()) {
107 "SpatialCapture: cannot start, no sources");
108 return;
109 }
110
111 stop();
112
113 m_state = std::make_shared<CaptureState>();
114 m_state->cache = m_cache;
115 m_state->sources = m_sources;
116 m_state->max_frames = max_frames;
117 m_state->recording.store(true, std::memory_order_release);
118
119 m_task_name = "spatial_capture_"
120 + std::to_string(g_next_spatial_capture_id.fetch_add(1, std::memory_order_relaxed));
121
122 auto routine = [](Vruta::TaskScheduler&,
123 std::shared_ptr<CaptureState> state,
124 uint64_t interval) -> Vruta::GraphicsRoutine {
125 auto& p = co_await Kriya::GetGraphicsPromise {};
126 while (!p.should_terminate
127 && !state->stop_requested.load(std::memory_order_acquire)
128 && run_one_frame(*state)) {
129 co_await Kriya::FrameDelay { .frames_to_wait = interval };
130 }
131 state->recording.store(false, std::memory_order_release);
132 };
133
134 m_scheduler.add_task(
135 std::make_shared<Vruta::GraphicsRoutine>(
136 routine(m_scheduler, m_state, frame_interval)),
137 m_task_name, false);
138
140 "SpatialCapture: recording {} stream(s) every {} frame(s), {}",
141 m_sources.size(), frame_interval,
142 max_frames == 0 ? std::string("unbounded")
143 : std::format("{} frames", max_frames));
144}
145
147{
148 if (!m_state || !m_state->recording.load(std::memory_order_acquire)) {
149 return;
150 }
151
152 m_state->stop_requested.store(true, std::memory_order_release);
153 m_scheduler.cancel_task(m_task_name);
154
156 "SpatialCapture: stop requested after {} frames",
157 m_state->frame.load(std::memory_order_relaxed));
158}
159
161 std::shared_ptr<Buffers::RelaxationGridBuffer> grid,
162 std::string stream_name,
163 float extent,
164 ToAttributes to_attributes)
165 : m_grid(std::move(grid))
166 , m_stream_name(std::move(stream_name))
167 , m_to_attributes(std::move(to_attributes))
168{
170}
171
176
177bool RelaxationGridCapture::start(const std::string& filepath)
178{
179 auto cache = std::make_shared<SpatialCache>();
180 if (!cache->open(filepath)) {
182 "RelaxationGridCapture: failed to open '{}': {}", filepath, cache->get_last_error());
183 return false;
184 }
185
186 m_cache = std::move(cache);
188 "RelaxationGridCapture: recording '{}' to '{}'", m_stream_name, filepath);
189 return true;
190}
191
193{
194 if (!m_cache) {
195 return;
196 }
197
198 m_cache->close();
199 m_cache.reset();
200
202 "RelaxationGridCapture: stopped '{}'", m_stream_name);
203}
204
205bool RelaxationGridCapture::write_snapshot(std::span<const uint8_t> bytes)
206{
207 if (!m_cache) {
208 return true;
209 }
210
211 return m_cache->write(m_stream_name,
214 .positions = m_positions,
215 .ids = m_ids,
216 .attributes = m_to_attributes(bytes) });
217}
218
220 const std::string& path_pattern,
221 const std::vector<SpatialCaptureSource>& sources)
222{
223 if (sources.empty()) {
225 "save_spatial_snapshot: no sources");
226 return false;
227 }
228
229 const auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
230 std::chrono::system_clock::now().time_since_epoch())
231 .count();
232 const auto path = resolve_sequence_path(path_pattern, static_cast<uint64_t>(now_ms));
233
234 SpatialCache cache;
235 if (!cache.open(path)) {
237 "save_spatial_snapshot: failed to open '{}': {}", path, cache.get_last_error());
238 return false;
239 }
240
241 const bool ok = run_sources(cache, sources);
242 cache.close();
243 return ok;
244}
245
246} // namespace MayaFlux::IO
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
std::function< std::vector< SpatialAttribute >(std::span< const uint8_t >)> ToAttributes
std::shared_ptr< SpatialCache > m_cache
bool start(const std::string &filepath)
Open the archive.
RelaxationGridCapture(std::shared_ptr< Buffers::RelaxationGridBuffer > grid, std::string stream_name, float extent, ToAttributes to_attributes)
std::shared_ptr< Buffers::RelaxationGridBuffer > m_grid
bool write_snapshot(std::span< const uint8_t > bytes)
Write one snapshot as a sample, if currently capturing.
std::string get_last_error() const
bool open(const std::string &filepath)
Open an Alembic archive for writing, Ogawa backend.
void close()
Finalize and close the archive.
Alembic-backed writer for time-sampled spatial entity state: particle systems, point clouds,...
std::shared_ptr< SpatialCache > m_cache
std::vector< SpatialCaptureSource > m_sources
static bool run_one_frame(CaptureState &state)
One tick: write every source's stream.
void start(uint32_t max_frames=0, uint64_t frame_interval=1)
Spawn the capture routine, resetting the frame counter.
std::shared_ptr< CaptureState > m_state
Vruta::TaskScheduler & m_scheduler
void stop()
Request the capture routine stop.
SpatialCapture(Vruta::TaskScheduler &scheduler, std::shared_ptr< SpatialCache > cache, std::vector< SpatialCaptureSource > sources)
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:513
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:56
bool relaxation_grid_positions(const std::shared_ptr< Buffers::RelaxationGridBuffer > &grid, float extent, std::vector< glm::vec3 > &positions, std::vector< uint64_t > &ids)
Generate the fixed grid positions and cell ids a RelaxationGridBuffer's cells occupy,...
bool save_spatial_snapshot(const std::string &path_pattern, const std::vector< SpatialCaptureSource > &sources)
Open a fresh SpatialCache, write every source once, then close it.
bool write_network_geometry_buffer_sample(SpatialCache &cache, const std::string &stream_name, const std::shared_ptr< Buffers::NetworkGeometryBuffer > &buffer)
Pack a NetworkGeometryBuffer's driving network into one or more SpatialCache streams,...
std::string resolve_sequence_path(std::string_view pattern, uint64_t frame)
Substitute a frame index into a numbered output pattern.
SpatialCaptureSource make_network_geometry_source(std::string stream_name, std::shared_ptr< Buffers::NetworkGeometryBuffer > buffer)
Build a SpatialCaptureSource that packs a NetworkGeometryBuffer's driving network through write_netwo...
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
GetPromiseBase< Vruta::graphics_promise > GetGraphicsPromise
Graphics domain promise accessor.
One named stream a SpatialCapture (or save_spatial_snapshot) writes into a SpatialCache each tick.
std::shared_ptr< SpatialCache > cache
std::vector< SpatialCaptureSource > sources
One sample's worth of data for one named stream.
graphics-domain awaiter for frame-accurate timing delays