MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SpatialExport.cpp
Go to the documentation of this file.
1#include "SpatialExport.hpp"
2
11
13
14namespace MayaFlux::IO {
15
16namespace {
17
18 /**
19 * @brief Read every vertex, whole, into SpatialSample-ready columns.
20 *
21 * Decoded by Kakshya::VertexInsertion against the canonical Vertex layout.
22 *
23 * A GraphicsOperator's raw vertex buffer is Kakshya::Vertex records:
24 * PointVertex/LineVertex/MeshVertex all share its exact 68-byte layout,
25 * and Vertex's own doc calls it "pipeline-ready" for exactly this.
26 * position becomes SpatialSample::positions; color, scalar, uv, normal,
27 * and tangent each become their own named attribute (scalar carries
28 * size/thickness/weight depending on the concrete vertex type, per
29 * Vertex's own doc).
30 *
31 * @return False if @p layout is not this 68-byte record (no known
32 * GraphicsOperator today produces anything else); positions and
33 * attributes are left untouched in that case.
34 */
35 bool extract_vertex_columns(
36 const Kakshya::VertexLayout& layout,
37 std::span<const uint8_t> raw,
38 size_t vertex_count,
39 std::vector<glm::vec3>& positions,
40 std::vector<SpatialAttribute>& attributes)
41 {
42 if (layout.stride_bytes != sizeof(Kakshya::Vertex)) {
43 return false;
44 }
45
46 std::vector<Kakshya::DataVariant> channels;
47 Kakshya::VertexInsertion insertion(channels);
48 if (!insertion.insert_interleaved(
49 raw.first(vertex_count * sizeof(Kakshya::Vertex)),
50 Kakshya::VertexLayout::for_points(sizeof(Kakshya::Vertex)))) {
51 return false;
52 }
53
54 constexpr std::array<const char*, 5> column_names { "color", "scalar", "uv", "normal", "tangent" };
55
56 positions = std::move(std::get<std::vector<glm::vec3>>(channels[0]));
57 for (size_t i = 0; i < column_names.size(); ++i) {
58 attributes.push_back(SpatialAttribute { .name = column_names[i], .scope = SpatialScope::Varying,
59 .values = std::move(channels[i + 1]) });
60 }
61
62 return true;
63 }
64
65 /**
66 * @brief Chunk a curve topology's flat vertex array into per-curve
67 * vertex counts.
68 * @param topology LINE_STRIP or LINE_LIST. Any other value is a
69 * caller error (not asserted here; the caller
70 * only reaches this for those two).
71 * @param cluster_ids build_cluster_ids()-shaped: one entry per vertex,
72 * contiguous runs of equal value in
73 * get_vertex_data() order.
74 * @return For LINE_STRIP, the run-length encoding of @p cluster_ids:
75 * one curve per contiguous cluster run, matching how
76 * PathOperator lays out each path as one such run (a single
77 * continuous interpolated strip). For LINE_LIST, fixed pairs
78 * (every count is 2), matching how TopologyOperator lays out
79 * each graph as independent expanded edges rather than one
80 * connected strip through the whole graph; cluster_ids is
81 * unused in this case since edge boundaries do not follow
82 * cluster boundaries. nullopt if @p vertex_count is odd for
83 * LINE_LIST, which no known producer of that topology should
84 * ever report.
85 */
86 std::optional<std::vector<int32_t>> curve_vertex_counts(
88 const std::vector<uint32_t>& cluster_ids,
89 size_t vertex_count)
90 {
92 std::vector<int32_t> counts;
93 size_t i = 0;
94 while (i < cluster_ids.size()) {
95 size_t j = i + 1;
96 while (j < cluster_ids.size() && cluster_ids[j] == cluster_ids[i]) {
97 ++j;
98 }
99 counts.push_back(static_cast<int32_t>(j - i));
100 i = j;
101 }
102 return counts;
103 }
104
105 if (vertex_count % 2 != 0) {
106 return std::nullopt;
107 }
108 return std::vector<int32_t>(vertex_count / 2, 2);
109 }
110
111} // namespace
112
114 const std::shared_ptr<Buffers::RelaxationGridBuffer>& grid,
115 float extent,
116 std::vector<glm::vec3>& positions,
117 std::vector<uint64_t>& ids)
118{
119 if (!grid) {
121 "relaxation_grid_positions: null grid");
122 return false;
123 }
124
125 const uint32_t width = grid->get_grid_width();
126 const uint32_t height = grid->get_grid_height();
127 const uint32_t cell_count = grid->get_cell_count();
128
129 positions.resize(cell_count);
130 ids.resize(cell_count);
131
132 for (uint32_t i = 0; i < cell_count; ++i) {
133 const uint32_t ix = i % width;
134 const uint32_t iy = i / width;
135
136 const float fx = (static_cast<float>(ix) + 0.5F) / static_cast<float>(width);
137 const float fy = (static_cast<float>(iy) + 0.5F) / static_cast<float>(height);
138
139 positions[i] = glm::vec3((fx * 2.0F - 1.0F) * extent, (fy * 2.0F - 1.0F) * extent, 0.0F);
140 ids[i] = i;
141 }
142
143 return true;
144}
145
147 SpatialCache& cache,
148 const std::string& stream_name,
150{
151 if (!op) {
153 "write_operator_sample: null operator");
154 return false;
155 }
156
157 const size_t vertex_count = op->get_vertex_count();
158 if (vertex_count == 0) {
160 "write_operator_sample: operator reports zero vertices");
161 return false;
162 }
163
164 const Kakshya::VertexLayout layout = op->get_vertex_layout();
165 const std::span<const uint8_t> raw = op->get_vertex_data();
166 if (layout.stride_bytes == 0 || raw.size() < static_cast<size_t>(layout.stride_bytes) * vertex_count) {
168 "write_operator_sample: vertex buffer smaller than layout implies");
169 return false;
170 }
171
172 std::vector<glm::vec3> positions;
173 std::vector<SpatialAttribute> attributes;
174 if (!extract_vertex_columns(layout, raw, vertex_count, positions, attributes)) {
176 "write_operator_sample: vertex layout is not a Kakshya::Vertex record");
177 return false;
178 }
179
180 std::vector<uint32_t> cluster_ids = op->build_cluster_ids();
181 if (cluster_ids.size() != vertex_count) {
183 "write_operator_sample: build_cluster_ids() count mismatch");
184 return false;
185 }
186
187 const auto topology = op->declared_topology().value_or(Portal::Graphics::PrimitiveTopology::POINT_LIST);
188 std::optional<std::vector<int32_t>> vertex_counts_per_curve;
191 vertex_counts_per_curve = curve_vertex_counts(topology, cluster_ids, vertex_count);
192 if (!vertex_counts_per_curve) {
194 "write_operator_sample: {} vertices cannot form LINE_LIST pairs", vertex_count);
195 return false;
196 }
197 }
198
199 attributes.push_back(SpatialAttribute {
200 .name = "cluster",
201 .scope = SpatialScope::Varying,
202 .values = Kakshya::DataVariant { std::move(cluster_ids) } });
203
204 for (auto& [name, values] : op->extract_vertex_attributes()) {
205 const size_t count = std::visit([](const auto& v) { return v.size(); }, values);
206 if (count != vertex_count) {
208 "write_operator_sample: extract_vertex_attributes() '{}' count mismatch", name);
209 return false;
210 }
211 attributes.push_back(SpatialAttribute {
212 .name = name,
213 .scope = SpatialScope::Varying,
214 .values = std::move(values) });
215 }
216
217 if (vertex_counts_per_curve) {
218 return cache.write(stream_name,
220 .topology = topology,
221 .positions = positions,
222 .vertex_counts_per_curve = *vertex_counts_per_curve,
223 .attributes = attributes });
224 }
225
226 std::vector<uint64_t> ids(vertex_count);
227 std::ranges::iota(ids, uint64_t { 0 });
228
229 std::vector<glm::vec3> velocities = op->extract_vertex_velocities();
230 if (!velocities.empty() && velocities.size() != vertex_count) {
232 "write_operator_sample: extract_vertex_velocities() count mismatch");
233 return false;
234 }
235
236 return cache.write(stream_name,
239 .positions = positions,
240 .ids = ids,
241 .velocities = velocities,
242 .attributes = attributes });
243}
244
245namespace {
246
247 /**
248 * @brief GPU-authoritative path: download vertex bytes straight from the
249 * buffer itself, since NetworkGeometryBuffer is a VKBuffer.
250 *
251 * Population size, record schema, and declared_topology() come from
252 * the still-CPU-tracked primary GraphicsOperator (these describe the
253 * buffer's contract, not its live GPU bytes, so they stay valid even
254 * once a GpuFieldOperator owns those bytes). A curve topology chunks
255 * on build_cluster_ids(), same as write_operator_sample(), since graph/
256 * path membership is structural and unaffected by what a GpuFieldOperator
257 * does to positions. Attaches a "cluster" attribute from the declared
258 * hash_cluster_id state field when present; velocities and any other
259 * per-rule state are left out, since no name or shape for them is known
260 * generically here.
261 */
262 bool write_network_geometry_buffer_gpu_sample(
263 SpatialCache& cache,
264 const std::string& stream_name,
265 const std::shared_ptr<Buffers::NetworkGeometryBuffer>& buffer,
267 {
268 const size_t vertex_count = graphics_op->get_vertex_count();
269 if (vertex_count == 0) {
271 "write_network_geometry_buffer_sample: operator reports zero vertices");
272 return false;
273 }
274
275 const Kakshya::VertexLayout layout = graphics_op->get_vertex_layout();
276 if (layout.stride_bytes != sizeof(Kakshya::Vertex)) {
278 "write_network_geometry_buffer_sample: vertex layout is not a Kakshya::Vertex record");
279 return false;
280 }
281
282 std::vector<uint8_t> raw(vertex_count * layout.stride_bytes);
283 std::shared_ptr<Buffers::VKBuffer> staging;
285 Buffers::generation_slot(Buffers::gpu_buffer_handle(buffer)), raw.data(), raw.size(), staging);
286
287 std::vector<glm::vec3> positions;
288 std::vector<SpatialAttribute> attributes;
289 if (!extract_vertex_columns(layout, raw, vertex_count, positions, attributes)) {
291 "write_network_geometry_buffer_sample: vertex layout is not a Kakshya::Vertex record");
292 return false;
293 }
294
295 const auto topology = graphics_op->declared_topology().value_or(Portal::Graphics::PrimitiveTopology::POINT_LIST);
296 std::optional<std::vector<int32_t>> vertex_counts_per_curve;
299 std::vector<uint32_t> structural_cluster_ids = graphics_op->build_cluster_ids();
300 if (structural_cluster_ids.size() != vertex_count) {
302 "write_network_geometry_buffer_sample: build_cluster_ids() count mismatch");
303 return false;
304 }
305 vertex_counts_per_curve = curve_vertex_counts(topology, structural_cluster_ids, vertex_count);
306 if (!vertex_counts_per_curve) {
308 "write_network_geometry_buffer_sample: {} vertices cannot form LINE_LIST pairs", vertex_count);
309 return false;
310 }
311 }
312
313 if (buffer->has_state("hash_cluster_id")
314 && buffer->get_state_bytes("hash_cluster_id") == vertex_count * sizeof(uint32_t)) {
315 std::vector<uint32_t> cluster_ids(vertex_count);
317 buffer->read_state_slot("hash_cluster_id"), cluster_ids.data(),
318 cluster_ids.size() * sizeof(uint32_t), staging);
319 attributes.push_back(SpatialAttribute {
320 .name = "cluster",
321 .scope = SpatialScope::Varying,
322 .values = Kakshya::DataVariant { std::move(cluster_ids) } });
323 }
324
325 if (vertex_counts_per_curve) {
326 return cache.write(stream_name,
328 .topology = topology,
329 .positions = positions,
330 .vertex_counts_per_curve = *vertex_counts_per_curve,
331 .attributes = attributes });
332 }
333
334 std::vector<uint64_t> ids(vertex_count);
335 std::ranges::iota(ids, uint64_t { 0 });
336
337 return cache.write(stream_name,
340 .positions = positions,
341 .ids = ids,
342 .attributes = attributes });
343 }
344
345} // namespace
346
348 SpatialCache& cache,
349 const std::string& stream_name,
350 const std::shared_ptr<Buffers::NetworkGeometryBuffer>& buffer)
351{
352 if (!buffer) {
354 "write_network_geometry_buffer_sample: null buffer");
355 return false;
356 }
357
358 auto network = buffer->get_network();
359 if (!network) {
361 "write_network_geometry_buffer_sample: buffer has no network");
362 return false;
363 }
364
365 auto chain = network->get_operator_chain();
366
367 if (chain && chain->find<Nodes::Network::GpuFieldOperator>()) {
368 auto* graphics_op = dynamic_cast<Nodes::Network::GraphicsOperator*>(network->get_operator());
369 if (!graphics_op) {
371 "write_network_geometry_buffer_sample: network's primary operator is not a GraphicsOperator");
372 return false;
373 }
374 return write_network_geometry_buffer_gpu_sample(cache, stream_name, buffer, graphics_op);
375 }
376
377 std::vector<std::pair<std::string, Nodes::Network::GraphicsOperator*>> graphics_ops;
378 if (auto* primary = dynamic_cast<Nodes::Network::GraphicsOperator*>(network->get_operator())) {
379 graphics_ops.emplace_back(std::string(primary->get_type_name()), primary);
380 }
381 if (chain) {
382 for (const auto& op : chain->operators()) {
383 if (auto* secondary = dynamic_cast<Nodes::Network::GraphicsOperator*>(op.get())) {
384 graphics_ops.emplace_back(std::string(secondary->get_type_name()), secondary);
385 }
386 }
387 }
388
389 if (graphics_ops.empty()) {
391 "write_network_geometry_buffer_sample: network has no GraphicsOperator");
392 return false;
393 }
394
395 if (graphics_ops.size() == 1) {
396 return write_operator_sample(cache, stream_name, graphics_ops.front().second);
397 }
398
399 std::unordered_map<std::string, int> seen;
400 for (const auto& [type_name, op] : graphics_ops) {
401 int& occurrence = seen[type_name];
402 std::string set_name = stream_name;
403 set_name.append("_").append(type_name);
404 if (occurrence != 0) {
405 set_name.append(std::to_string(occurrence));
406 }
407 ++occurrence;
408
409 if (!write_operator_sample(cache, set_name, op)) {
410 return false;
411 }
412 }
413
414 return true;
415}
416
417} // namespace MayaFlux::IO
#define MF_ERROR(comp, ctx,...)
bool write(const std::string &stream_name, const SpatialSample &sample)
Append one sample to a named stream.
Alembic-backed writer for time-sampled spatial entity state: particle systems, point clouds,...
Chain operator that declares Tendency field deformation as a compute shader rather than evaluating it...
virtual std::vector< std::pair< std::string, Kakshya::DataVariant > > extract_vertex_attributes() const
Extra named, per-vertex attributes beyond position/color/size, for an external cache/export consumer.
virtual size_t get_vertex_count() const =0
Get number of vertices (may differ from point count for topology/path).
virtual std::vector< uint32_t > build_cluster_ids() const
Per-vertex collection index, global index order.
virtual Kakshya::VertexLayout get_vertex_layout() const =0
Get vertex layout describing vertex structure.
virtual std::optional< Portal::Graphics::PrimitiveTopology > declared_topology() const
Topology this operator's vertex data is authored for, if it holds a node to ask.
virtual std::vector< glm::vec3 > extract_vertex_velocities() const
Per-vertex velocity, for a consumer that wants to write it as a native channel (e....
virtual std::span< const uint8_t > get_vertex_data() const =0
Get vertex data for GPU upload.
Operator that produces GPU-renderable geometry.
Portal::Graphics::GpuBufferHandle gpu_buffer_handle(const std::shared_ptr< VKBuffer > &buffer)
View a buffer's primary GPU storage without reading it back.
VKBufferResources::GenerationSlot generation_slot(const Portal::Graphics::GpuBufferHandle &handle)
Express a buffer handle as the slot the back_buffers transfer helpers take.
void download_back_buffer(const VKBufferResources::GenerationSlot &slot, void *data, size_t size, std::shared_ptr< VKBuffer > &staging)
Download a raw back_buffers slot to host memory.
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 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,...
bool write_operator_sample(SpatialCache &cache, const std::string &stream_name, const Nodes::Network::GraphicsOperator *op)
Pack one GraphicsOperator's current vertex state into a SpatialCache stream, as a Points or Curves sa...
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
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:58
PrimitiveTopology
Vertex assembly primitive topology.
One named, scoped channel of already-typed values.
One sample's worth of data for one named stream.
uint32_t stride_bytes
Total bytes per vertex (stride in Vulkan terms) e.g., 3 floats (position) + 3 floats (normal) = 24 by...
static VertexLayout for_points(uint32_t stride=68)
Factory: layout for PointVertex (position, color, size, uv, normal, tangent).
Complete description of vertex data layout in a buffer.