45 if (!is_field_compatible_network(
m_network.get())) {
54 std::shared_ptr<Nodes::Network::GpuFieldOperator> field_op;
55 for (
const auto& op : op_chain->operators()) {
56 field_op = std::dynamic_pointer_cast<Nodes::Network::GpuFieldOperator>(op);
65 auto self = std::dynamic_pointer_cast<NetworkGeometryBuffer>(shared_from_this());
67 if (!self || !chain) {
69 "NetworkGeometryBuffer: no processing chain to wire field stages onto");
73 if (field_op->binding_count() > 0) {
74 chain->add_postprocessor(std::make_shared<VertexFieldProcessor>(field_op), self);
77 const auto& pconfig = field_op->get_field_config();
78 if (!pconfig.absorb_radius.has_value() && !pconfig.density_color) {
82 if (pconfig.transfer_on_claim && !pconfig.absorb_radius.has_value()) {
84 "NetworkGeometryBuffer: transfer_on_claim requires absorb_radius (there is no "
85 "claim to reinterpret without it); transfer disabled");
90 float cell_size = 0.0F;
91 if (pconfig.cell_size.has_value()) {
92 cell_size = *pconfig.cell_size;
97 "NetworkGeometryBuffer: SpatialFieldConfig has no cell_size and the network's "
98 "primary operator is not a PhysicsOperator, so one cannot be derived");
103 if (!hash_config.has_value()) {
105 "NetworkGeometryBuffer: SpatialHashConfig::from_network failed");
109 const uint32_t live_count = hash_config->particle_count;
110 bool reserve_enabled = pconfig.reserve_fraction > 0.0F;
112 if (reserve_enabled && !pconfig.absorb_radius.has_value()) {
114 "NetworkGeometryBuffer: reserve_fraction requires absorb_radius (destroy-on-"
115 "absorption has nothing to destroy without claims); reserve capacity disabled");
116 reserve_enabled =
false;
119 uint32_t total_count = live_count;
120 if (reserve_enabled) {
121 const auto reserve_count =
static_cast<uint32_t
>(
122 std::ceil(
static_cast<float>(live_count) * pconfig.reserve_fraction));
123 total_count = live_count + reserve_count;
125 const size_t needed_bytes =
static_cast<size_t>(total_count) * hash_config->stride_words * 4;
126 self->resize(needed_bytes,
true);
128 hash_config->particle_count = total_count;
131 if (total_count != live_count) {
132 hash_config->declare_fields(self);
134 std::vector<uint32_t> cluster_ids(total_count, 0U);
135 if (
auto* graphics_op
137 auto live_ids = graphics_op->build_cluster_ids();
138 std::copy_n(live_ids.begin(),
139 std::min<size_t>(live_ids.size(), live_count), cluster_ids.begin());
141 std::shared_ptr<VKBuffer> cluster_staging;
143 cluster_ids.size() *
sizeof(uint32_t), cluster_staging);
145 self->ensure_cluster_ids();
146 hash_config->declare_fields(self);
149 std::optional<PopulationConfig> pop_config;
150 if (reserve_enabled) {
153 chain->add_processor(std::make_shared<PopulationInitProcessor>(*pop_config), self);
156 chain->add_processor(std::make_shared<HashClearProcessor>(*hash_config), self);
157 chain->add_processor(std::make_shared<HashCountProcessor>(*hash_config, reserve_enabled), self);
158 chain->add_processor(std::make_shared<HashScanProcessor>(*hash_config), self);
159 chain->add_processor(std::make_shared<HashScatterProcessor>(*hash_config, reserve_enabled), self);
161 if (pconfig.density_color) {
162 chain->add_processor(
163 std::make_shared<HashDensityColorProcessor>(*hash_config, field_op), self);
166 if (!pconfig.absorb_radius.has_value()) {
170 MutationConfig claim_config { .
hash = *hash_config, .absorb_radius = *pconfig.absorb_radius };
173 const bool transfer_on_claim = pconfig.transfer_on_claim;
175 chain->add_processor(std::make_shared<ClaimInitProcessor>(claim_config), self);
176 chain->add_processor(
177 std::make_shared<ClaimProcessor>(claim_config, field_op, reserve_enabled), self);
178 chain->add_processor(std::make_shared<ClaimFlattenProcessor>(claim_config), self);
179 chain->add_processor(
180 std::make_shared<ClaimAccumulateProcessor>(
181 claim_config, physics, reserve_enabled ? live_count : 0U, field_op, transfer_on_claim),
184 if (pconfig.cosmetic_swallow) {
185 chain->add_processor(
186 std::make_shared<ClaimSwallowProcessor>(claim_config, field_op, transfer_on_claim), self);
189 if (transfer_on_claim) {
190 chain->add_processor(std::make_shared<ClaimTransferProcessor>(claim_config), self);
193 if (pop_config.has_value()) {
194 chain->add_processor(
195 std::make_shared<PopulationSpawnProcessor>(*pop_config, field_op), self);