35 bool extract_vertex_columns(
36 const Kakshya::VertexLayout& layout,
37 std::span<const uint8_t> raw,
39 std::vector<glm::vec3>& positions,
40 std::vector<SpatialAttribute>& attributes)
42 if (layout.stride_bytes !=
sizeof(Kakshya::Vertex)) {
46 std::vector<Kakshya::DataVariant> channels;
47 Kakshya::VertexInsertion insertion(channels);
48 if (!insertion.insert_interleaved(
49 raw.first(vertex_count *
sizeof(Kakshya::Vertex)),
54 constexpr std::array<const char*, 5> column_names {
"color",
"scalar",
"uv",
"normal",
"tangent" };
56 positions = std::move(std::get<std::vector<glm::vec3>>(channels[0]));
57 for (
size_t i = 0; i < column_names.size(); ++i) {
59 .values = std::move(channels[i + 1]) });
86 std::optional<std::vector<int32_t>> curve_vertex_counts(
88 const std::vector<uint32_t>& cluster_ids,
92 std::vector<int32_t> counts;
94 while (i < cluster_ids.size()) {
96 while (j < cluster_ids.size() && cluster_ids[j] == cluster_ids[i]) {
99 counts.push_back(
static_cast<int32_t
>(j - i));
105 if (vertex_count % 2 != 0) {
108 return std::vector<int32_t>(vertex_count / 2, 2);
114 const std::shared_ptr<Buffers::RelaxationGridBuffer>& grid,
116 std::vector<glm::vec3>& positions,
117 std::vector<uint64_t>& ids)
121 "relaxation_grid_positions: null grid");
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();
129 positions.resize(cell_count);
130 ids.resize(cell_count);
132 for (uint32_t i = 0; i < cell_count; ++i) {
133 const uint32_t ix = i % width;
134 const uint32_t iy = i / width;
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);
139 positions[i] = glm::vec3((fx * 2.0F - 1.0F) * extent, (fy * 2.0F - 1.0F) * extent, 0.0F);
148 const std::string& stream_name,
153 "write_operator_sample: null operator");
158 if (vertex_count == 0) {
160 "write_operator_sample: operator reports zero vertices");
168 "write_operator_sample: vertex buffer smaller than layout implies");
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");
181 if (cluster_ids.size() != vertex_count) {
183 "write_operator_sample: build_cluster_ids() count mismatch");
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);
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);
214 .values = std::move(values) });
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 });
226 std::vector<uint64_t> ids(vertex_count);
227 std::ranges::iota(ids, uint64_t { 0 });
230 if (!velocities.empty() && velocities.size() != vertex_count) {
232 "write_operator_sample: extract_vertex_velocities() count mismatch");
236 return cache.
write(stream_name,
239 .positions = positions,
241 .velocities = velocities,
242 .attributes = attributes });
262 bool write_network_geometry_buffer_gpu_sample(
264 const std::string& stream_name,
265 const std::shared_ptr<Buffers::NetworkGeometryBuffer>& buffer,
269 if (vertex_count == 0) {
271 "write_network_geometry_buffer_sample: operator reports zero vertices");
276 if (layout.stride_bytes !=
sizeof(Kakshya::Vertex)) {
278 "write_network_geometry_buffer_sample: vertex layout is not a Kakshya::Vertex record");
282 std::vector<uint8_t> raw(vertex_count * layout.stride_bytes);
283 std::shared_ptr<Buffers::VKBuffer> staging;
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");
296 std::optional<std::vector<int32_t>> vertex_counts_per_curve;
300 if (structural_cluster_ids.size() != vertex_count) {
302 "write_network_geometry_buffer_sample: build_cluster_ids() count mismatch");
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);
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);
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 });
334 std::vector<uint64_t> ids(vertex_count);
335 std::ranges::iota(ids, uint64_t { 0 });
337 return cache.write(stream_name,
340 .positions = positions,
342 .attributes = attributes });
349 const std::string& stream_name,
350 const std::shared_ptr<Buffers::NetworkGeometryBuffer>& buffer)
354 "write_network_geometry_buffer_sample: null buffer");
358 auto network = buffer->get_network();
361 "write_network_geometry_buffer_sample: buffer has no network");
365 auto chain = network->get_operator_chain();
371 "write_network_geometry_buffer_sample: network's primary operator is not a GraphicsOperator");
374 return write_network_geometry_buffer_gpu_sample(cache, stream_name, buffer, graphics_op);
377 std::vector<std::pair<std::string, Nodes::Network::GraphicsOperator*>> graphics_ops;
379 graphics_ops.emplace_back(std::string(primary->get_type_name()), primary);
382 for (
const auto& op : chain->operators()) {
384 graphics_ops.emplace_back(std::string(secondary->get_type_name()), secondary);
389 if (graphics_ops.empty()) {
391 "write_network_geometry_buffer_sample: network has no GraphicsOperator");
395 if (graphics_ops.size() == 1) {
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));
#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.
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.