1use super::plan::{build_particle_plan, particle_subgraph_hash};
2use super::sim::{RegionSpec, step_simulation};
3use super::*;
4use std::path::Path;
5use std::time::Instant;
6
7fn make_white_sprite(size: u32) -> SpriteImage {
8 SpriteImage {
9 size: [size, size],
10 rgba: vec![255u8; (size as usize) * (size as usize) * 4],
11 }
12}
13
14fn count_nonzero_alpha(buf: &[u8]) -> usize {
15 buf.chunks_exact(4).filter(|px| px[3] > 0).count()
16}
17
18#[test]
19fn test_particle_emitter_and_point_force_deterministic() {
20 let comp_src = r#"
21Composition {
22 GlobalRange = { 0, 100 },
23 RenderRange = { 0, 100 },
24 Tools = {
25 SpriteSrc = Background {
26 Inputs = {
27 Width = Input { Value = 32, },
28 Height = Input { Value = 32, },
29 },
30 },
31 pEmitter1SizeOverLife = LUTBezier {
32 KeyColorSplines = {
33 [0] = {
34 [0] = { 0.5, RH = { 0.333, 0.5 }, Flags = { Linear = true } },
35 [1] = { 0.25, LH = { 0.667, 0.25 }, Flags = { Linear = true } }
36 }
37 },
38 },
39 pEmitter1 = pEmitter {
40 Inputs = {
41 RandomSeed = Input { Value = 42, },
42 Number = Input { Value = 15, },
43 Lifespan = Input { Value = 60, },
44 Region = Input { Value = FuID { "RectRgn" }, },
45 ["RectRgn.Width"] = Input { Value = 0.6, },
46 ["RectRgn.Height"] = Input { Value = 0.4, },
47 Style = Input { Value = FuID { "ParticleStyleBitmap" }, },
48 ["ParticleStyleBitmap.DropToolsHere"] = Input {
49 SourceOp = "SpriteSrc",
50 Source = "Output",
51 },
52 ["ParticleStyle.Red"] = Input { Value = 1, },
53 ["ParticleStyle.Green"] = Input { Value = 0, },
54 ["ParticleStyle.Blue"] = Input { Value = 0, },
55 ["ParticleStyle.ColorVariance"] = Input { Value = 1, },
56 ["ParticleStyle.GreenHigh"] = Input { Value = 1, },
57 ["ParticleStyle.Size"] = Input { Value = 0.12, },
58 ["ParticleStyle.SizeOverLife"] = Input {
59 SourceOp = "pEmitter1SizeOverLife",
60 Source = "Value",
61 },
62 ["ParticleStyle.FadeIn"] = Input { Value = 0.1, },
63 ["ParticleStyle.FadeOut"] = Input { Value = 0.1, },
64 },
65 },
66 pPointForce1 = pPointForce {
67 Inputs = {
68 Strength = Input { Value = -3.0, },
69 Power = Input { Value = 0.5, },
70 ["Translate.X"] = Input { Value = 0.0, },
71 ["Translate.Y"] = Input { Value = 0.0, },
72 Input = Input {
73 SourceOp = "pEmitter1",
74 Source = "Output",
75 },
76 },
77 },
78 pRender1 = pRender {
79 Inputs = {
80 Input = Input {
81 SourceOp = "pPointForce1",
82 Source = "Output",
83 },
84 },
85 },
86 },
87}
88"#;
89
90 let comp = Comp::from_fusion_str(comp_src).expect("parse synthetic particle comp");
91 let render_id = comp.node_by_name["pRender1"];
92 let emitter_id = comp.node_by_name["pEmitter1"];
93 let sprite_id = comp.node_by_name["SpriteSrc"];
94 let force_id = comp.node_by_name["pPointForce1"];
95
96 // Verify bitmap source discovery.
97 let sources = collect_bitmap_sources(&comp, render_id);
98 assert_eq!(sources, vec![(emitter_id, sprite_id)]);
99
100 let expr = ExprEngine::new();
101 let mut sprites = HashMap::new();
102 sprites.insert(emitter_id, make_white_sprite(16));
103
104 let dims = [160, 90];
105
106 // 1. Determinism: two fresh caches produce bit-identical buffers.
107 let mut cache_a = ParticleCache::default();
108 let mut cache_b = ParticleCache::default();
109 let img_a = render_particles_2d(&mut cache_a, &comp, render_id, 25.0, dims, &expr, &sprites);
110 let img_b = render_particles_2d(&mut cache_b, &comp, render_id, 25.0, dims, &expr, &sprites);
111 assert_eq!(img_a, img_b, "particle render must be bit-identical across runs");
112 assert!(
113 count_nonzero_alpha(&img_a) > 50,
114 "expected non-empty rendered particles"
115 );
116
117 // 2. Force affects particle trajectories: bypassing pPointForce1 changes output.
118 let mut comp_no_force = comp.clone();
119 comp_no_force.nodes[force_id.0].bypassed = true;
120 comp_no_force.revision += 1;
121 let mut cache_no_force = ParticleCache::default();
122 let img_no_force = render_particles_2d(
123 &mut cache_no_force,
124 &comp_no_force,
125 render_id,
126 25.0,
127 dims,
128 &expr,
129 &sprites,
130 );
131 assert_ne!(
132 img_a, img_no_force,
133 "pPointForce must visibly change particle trajectories"
134 );
135
136 // 3. Cache consistency & sequential speedup: stepping 25 -> 26 matches fresh 0 -> 26.
137 let t_seq_start = Instant::now();
138 let img_26_cached =
139 render_particles_2d(&mut cache_a, &comp, render_id, 26.0, dims, &expr, &sprites);
140 let seq_elapsed = t_seq_start.elapsed();
141
142 let mut fresh_cache = ParticleCache::default();
143 let t_fresh_start = Instant::now();
144 let img_26_fresh = render_particles_2d(
145 &mut fresh_cache,
146 &comp,
147 render_id,
148 26.0,
149 dims,
150 &expr,
151 &sprites,
152 );
153 let fresh_elapsed = t_fresh_start.elapsed();
154 assert_eq!(
155 img_26_cached, img_26_fresh,
156 "cached sequential step must match fresh simulation"
157 );
158 assert!(
159 seq_elapsed <= fresh_elapsed * 2 + std::time::Duration::from_millis(5),
160 "cached sequential step should be fast (seq={seq_elapsed:?}, fresh={fresh_elapsed:?})"
161 );
162
163 cache_a.clear();
164 assert!(cache_a.entries.is_empty());
165}
166
167#[test]
168fn test_01_intro_particles_if_present() {
169 let path = Path::new("/Volumes/Project/trees/fusion/01_intro.comp");
170 if !path.exists() {
171 return;
172 }
173
174 let comp = Comp::from_fusion_file(path).expect("parse 01_intro.comp");
175 let render_id = comp.node_by_name["pRender1"];
176 let emitter_id = comp.node_by_name["pEmitter1"];
177 let bc4_id = comp.node_by_name["BrightnessContrast4"];
178
179 let sources = collect_bitmap_sources(&comp, render_id);
180 assert_eq!(
181 sources,
182 vec![(emitter_id, bc4_id)],
183 "01_intro.comp pEmitter1 bitmap source should be BrightnessContrast4"
184 );
185
186 let expr = ExprEngine::new();
187 let mut sprites = HashMap::new();
188 sprites.insert(emitter_id, make_white_sprite(32));
189
190 // In 01_intro.comp, TimeStretcher1 evaluates pRender1 at SourceTime = time / 25
191 // (so comp f600 -> t=24, comp f1600 -> t=64).
192 let ts_node = &comp.nodes[comp.node_by_name["TimeStretcher1"].0];
193 let t_f600 = comp.eval_number(ts_node, "SourceTime", 600.0, 24.0, &expr);
194 let t_f1600 = comp.eval_number(ts_node, "SourceTime", 1600.0, 64.0, &expr);
195
196 let dims = [320, 180];
197 let mut cache = ParticleCache::default();
198 let start = Instant::now();
199 let buf_600 = render_particles_2d(&mut cache, &comp, render_id, t_f600, dims, &expr, &sprites);
200 let buf_1600 =
201 render_particles_2d(&mut cache, &comp, render_id, t_f1600, dims, &expr, &sprites);
202 let elapsed = start.elapsed();
203
204 let nz_600 = count_nonzero_alpha(&buf_600);
205 assert!(
206 nz_600 > 0,
207 "expected non-zero alpha pixels at f600 (SourceTime={t_f600})"
208 );
209 assert_eq!(
210 buf_1600.len(),
211 dims[0] as usize * dims[1] as usize * 4,
212 "f1600 SourceTime={t_f1600}"
213 );
214 assert!(
215 elapsed.as_secs_f64() < 2.0,
216 "01_intro particle render should be fast, took {elapsed:?}"
217 );
218}
219
220#[test]
221fn test_pbounce_reverses_velocity_and_applies_friction() {
222 let comp_src = r#"
223Composition {
224 GlobalRange = { 0, 20 },
225 RenderRange = { 0, 20 },
226 Tools = {
227 pEmitter1 = pEmitter {
228 Inputs = {
229 Number = Input { Value = 1, },
230 Lifespan = Input { Value = 50, },
231 Velocity = Input { Value = 1.0, },
232 Angle = Input { Value = -60, },
233 ["SphereRgn.Size"] = Input { Value = 0, },
234 ["SphereRgn.Translate.Y"] = Input { Value = 0.2, },
235 },
236 },
237 pBounce1 = pBounce {
238 Inputs = {
239 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
240 Elasticity = Input { Value = 0.8, },
241 Friction = Input { Value = 0.25, },
242 Spin = Input { Value = 3.0, },
243 Region = Input { Value = FuID { "CubeRgn" }, },
244 ["CubeRgn.Translate.Y"] = Input { Value = -0.25, },
245 ["CubeRgn.Width"] = Input { Value = 2.0, },
246 ["CubeRgn.Height"] = Input { Value = 0.5, },
247 ["CubeRgn.Depth"] = Input { Value = 2.0, },
248 },
249 },
250 pRender1 = pRender {
251 Inputs = {
252 Input = Input { SourceOp = "pBounce1", Source = "Output", },
253 },
254 },
255 },
256}
257"#;
258 let comp = Comp::from_fusion_str(comp_src).expect("parse pBounce comp");
259 let render_id = comp.node_by_name["pRender1"];
260 let plan = build_particle_plan(&comp, render_id);
261 let expr = ExprEngine::new();
262 let mut state = SimState::default();
263
264 // Step until the first emitted particle (born at f=0 at y=0.2, vy≈-0.0866, vx=0.05)
265 // crosses the floor at y=0.0 around frame 3.
266 let empty_fields = HashMap::new();
267 let no_sprites = HashMap::new();
268 for f in 0..=4 {
269 step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr);
270 }
271 let first = state.particles.first().expect("particle emitted");
272 let init_vx = 0.1 * (-60.0_f64).to_radians().cos();
273 let init_vy = 0.1 * (-60.0_f64).to_radians().sin();
274 assert!(
275 first.vel[1] > 0.0,
276 "pBounce should reverse downward Y velocity (got vy={})",
277 first.vel[1]
278 );
279 assert!(
280 (first.vel[1] - (-init_vy * 0.8)).abs() < 1e-4,
281 "expected reflected vy={}, got {}",
282 -init_vy * 0.8,
283 first.vel[1]
284 );
285 assert!(
286 (first.vel[0] - init_vx * 0.75).abs() < 1e-4,
287 "expected friction-damped vx={}, got {}",
288 init_vx * 0.75,
289 first.vel[0]
290 );
291 assert!(first.spin_z.abs() > 0.5, "expected Spin impulse on bounce");
292 assert!(first.pos[1] > 0.0, "bounced particle should be above floor y=0");
293}
294
295/// Single-particle comp: `Number` is a spline that is 1 at frame 0 and 0 afterwards.
296fn single_particle_comp(emitter_inputs: &str, forces: &str, last_node: &str) -> Comp {
297 let src = format!(
298 r#"
299Composition {{
300 GlobalRange = {{ 0, 100 }},
301 RenderRange = {{ 0, 100 }},
302 Tools = {{
303 EN = BezierSpline {{
304 KeyFrames = {{
305 [0] = {{ 1, Flags = {{ Linear = true }} }},
306 [1] = {{ 0, Flags = {{ Linear = true }} }}
307 }}
308 }},
309 pEmitter1 = pEmitter {{
310 Inputs = {{
311 Number = Input {{ SourceOp = "EN", Source = "Value", }},
312 Lifespan = Input {{ Value = 100, }},
313 ["SphereRgn.Size"] = Input {{ Value = 0, }},
314 {emitter_inputs}
315 }},
316 }},
317 {forces}
318 pRender1 = pRender {{
319 Inputs = {{
320 Input = Input {{ SourceOp = "{last_node}", Source = "Output", }},
321 }},
322 }},
323 }},
324}}
325"#
326 );
327 Comp::from_fusion_str(&src).expect("parse single-particle comp")
328}
329
330fn render(comp: &Comp, time: f64, dims: [u32; 2], sprites: &HashMap<NodeId, SpriteImage>) -> Vec<u8> {
331 let mut cache = ParticleCache::default();
332 render_particles_2d(&mut cache, comp, comp.node_by_name["pRender1"], time, dims, &ExprEngine::new(), sprites)
333}
334
335/// `(x, y, alpha)` of every pixel with non-zero alpha.
336fn lit(buf: &[u8], width: usize) -> Vec<(usize, usize, u8)> {
337 buf.chunks_exact(4)
338 .enumerate()
339 .filter(|(_, px)| px[3] > 0)
340 .map(|(i, px)| (i % width, i / width, px[3]))
341 .collect()
342}
343
344fn alpha_mass(buf: &[u8]) -> f64 {
345 buf.chunks_exact(4).map(|px| px[3] as f64 / 255.0).sum()
346}
347
348#[test]
349fn point_style_is_one_pixel_regardless_of_size() {
350 let comp = single_particle_comp(r#"["ParticleStyle.Size"] = Input { Value = 5, },"#, "", "pEmitter1");
351 let buf = render(&comp, 0.0, [200, 100], &HashMap::new());
352 assert_eq!(lit(&buf, 200), vec![(100, 50, 255)]);
353}
354
355#[test]
356fn blob_is_gaussian_with_sigma_per_size_unit() {
357 let comp = single_particle_comp(
358 r#"Style = Input { Value = FuID { "ParticleStyleBlob" }, }, ["ParticleStyle.Size"] = Input { Value = 2, },"#,
359 "",
360 "pEmitter1",
361 );
362 // Fusion 21.1: Size 2 blob has alpha mass ~276 (= 2*pi*(2*3.316)^2).
363 let mass = alpha_mass(&render(&comp, 0.0, [256, 256], &HashMap::new()));
364 assert!((mass - 276.0).abs() < 8.0, "blob mass {mass}");
365}
366
367#[test]
368fn newborn_moves_once_at_tenth_width_velocity() {
369 let comp = single_particle_comp(
370 r#"VelocityControls = Input { Value = 1, }, Velocity = Input { Value = 1, }, Angle = Input { Value = 0, },"#,
371 "",
372 "pEmitter1",
373 );
374 let at = |t: f64| lit(&render(&comp, t, [200, 100], &HashMap::new()), 200)[0].0;
375 assert_eq!(at(0.0), 120);
376 assert_eq!(at(2.0), 160);
377}
378
379#[test]
380fn directional_force_accelerates_down_by_strength_over_one_hundred() {
381 let comp = single_particle_comp(
382 "",
383 r#"pDirectionalForce1 = pDirectionalForce {
384 Inputs = {
385 Strength = Input { Value = 1, },
386 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
387 },
388 },"#,
389 "pDirectionalForce1",
390 );
391 // y(f) = 0.01 * f(f+1)/2 widths below the spawn point.
392 let buf = render(&comp, 4.0, [1000, 1000], &HashMap::new());
393 assert_eq!(lit(&buf, 1000), vec![(500, 600, 255)]);
394}
395
396#[test]
397fn particle_lives_while_age_is_below_lifespan() {
398 let comp = single_particle_comp(r#"Lifespan = Input { Value = 3, },"#, "", "pEmitter1");
399 let count = |t: f64| lit(&render(&comp, t, [64, 64], &HashMap::new()), 64).len();
400 assert_eq!((count(0.0), count(2.0), count(3.0)), (1, 1, 0));
401}
402
403#[test]
404fn fade_out_starts_at_fade_out_fraction_of_life() {
405 let comp = single_particle_comp(
406 r#"Lifespan = Input { Value = 20, }, Style = Input { Value = FuID { "ParticleStyleBlob" }, },
407 ["ParticleStyle.Size"] = Input { Value = 2, }, ["ParticleStyle.FadeOut"] = Input { Value = 0.5, },"#,
408 "",
409 "pEmitter1",
410 );
411 let mass = |t: f64| alpha_mass(&render(&comp, t, [256, 256], &HashMap::new()));
412 assert!((mass(15.0) / mass(10.0) - 0.5).abs() < 0.02);
413}
414
415#[test]
416fn bitmap_width_is_size_times_sprite_pixels() {
417 let comp = single_particle_comp(
418 r#"Style = Input { Value = FuID { "ParticleStyleBitmap" }, }, ["ParticleStyle.Size"] = Input { Value = 2, },"#,
419 "",
420 "pEmitter1",
421 );
422 let mut sprites = HashMap::new();
423 sprites.insert(comp.node_by_name["pEmitter1"], make_white_sprite(4));
424 let pixels = lit(&render(&comp, 0.0, [200, 100], &sprites), 200);
425 let (min_x, max_x) = pixels.iter().fold((usize::MAX, 0), |(lo, hi), &(x, _, _)| (lo.min(x), hi.max(x)));
426 assert!((8..=9).contains(&(max_x - min_x + 1)), "bitmap width {}", max_x - min_x + 1);
427}
428
429#[test]
430fn default_region_is_sphere_of_diameter_one_tenth_width() {
431 let comp = Comp::from_fusion_str("Composition { Tools = { pEmitter1 = pEmitter { Inputs = { }, }, }, }")
432 .expect("parse bare emitter");
433 let node = &comp.nodes[comp.node_by_name["pEmitter1"].0];
434 match RegionSpec::from_node(&comp, node, 0.0, &ExprEngine::new()) {
435 RegionSpec::Sphere { radius, .. } => assert!((radius - 0.05).abs() < 1e-12),
436 _ => panic!("default region must be a sphere"),
437 }
438}
439
440#[test]
441fn depth_scales_screen_position_by_camera_distance() {
442 let comp = single_particle_comp(
443 r#"["SphereRgn.Translate.X"] = Input { Value = 0.2, }, ["SphereRgn.Translate.Z"] = Input { Value = 0.9722222222222222, },"#,
444 "",
445 "pEmitter1",
446 );
447 // z equal to the camera distance halves the projected offset: x = (0.2 * 0.5 + 0.5) * 1000.
448 let buf = render(&comp, 0.0, [1000, 1000], &HashMap::new());
449 assert_eq!(lit(&buf, 1000), vec![(600, 500, 255)]);
450}
451
452#[test]
453fn particle_cache_advances_past_600_incrementally_and_scrubs_with_snapshots() {
454 let comp_src = r#"
455Composition {
456 GlobalRange = { 0, 800 },
457 RenderRange = { 0, 800 },
458 Tools = {
459 pEmitter1 = pEmitter {
460 Inputs = {
461 RandomSeed = Input { Value = 7, },
462 Number = Input { Value = 2, },
463 Lifespan = Input { Value = 25, },
464 Velocity = Input { Value = 0.2, },
465 VelocityVariance = Input { Value = 0.1, },
466 AngleVariance = Input { Value = 180, },
467 },
468 },
469 pRender1 = pRender {
470 Inputs = {
471 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
472 },
473 },
474 },
475}
476"#;
477 let comp = Comp::from_fusion_str(comp_src).expect("parse particle comp");
478 let render_id = comp.node_by_name["pRender1"];
479 let expr = ExprEngine::new();
480 let sprites = HashMap::new();
481 let dims = [64, 64];
482
483 let mut cache = ParticleCache::default();
484 let mut last_img = Vec::new();
485 for f in 580..=625 {
486 last_img = render_particles_2d(&mut cache, &comp, render_id, f as f64, dims, &expr, &sprites);
487 }
488
489 let key = (
490 "pRender1".to_owned(),
491 particle_subgraph_hash(&comp, render_id, 0),
492 );
493 let cached = cache.entries.get(&key).expect("cached entry");
494 assert_eq!(cached.base_start, 0);
495 assert_eq!(cached.current_frame, 625);
496 assert!(cached.snapshots.contains_key(&600), "expected snapshot at frame 600");
497
498 // Scrubbing back to frame 605 restores snapshot 600 and matches direct render at 605.
499 let scrub_605 = render_particles_2d(&mut cache, &comp, render_id, 605.0, dims, &expr, &sprites);
500 let mut fresh_cache = ParticleCache::default();
501 let fresh_605 = render_particles_2d(&mut fresh_cache, &comp, render_id, 605.0, dims, &expr, &sprites);
502 assert_eq!(scrub_605, fresh_605, "scrubbed frame 605 must match fresh simulation");
503
504 let mut fresh_625_cache = ParticleCache::default();
505 let fresh_625 = render_particles_2d(&mut fresh_625_cache, &comp, render_id, 625.0, dims, &expr, &sprites);
506 assert_eq!(last_img, fresh_625, "incremental advance past 600 must match fresh simulation");
507}
508
509#[test]
510fn particle_cache_ignores_unrelated_comp_edits_and_node_id_shifts() {
511 let with_unrelated = r#"
512Composition {
513 GlobalRange = { 0, 100 },
514 RenderRange = { 0, 100 },
515 Tools = {
516 UnrelatedBg = Background {
517 Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, },
518 },
519 pEmitter1 = pEmitter {
520 Inputs = {
521 RandomSeed = Input { Value = 19, },
522 Number = Input { Value = 8, },
523 Lifespan = Input { Value = 40, },
524 Velocity = Input { Value = 0.5, },
525 VelocityVariance = Input { Value = 0.25, },
526 AngleVariance = Input { Value = 180, },
527 },
528 },
529 pRender1 = pRender {
530 Inputs = {
531 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
532 },
533 },
534 },
535}
536"#;
537 let without_unrelated = r#"
538Composition {
539 GlobalRange = { 0, 100 },
540 RenderRange = { 0, 100 },
541 Tools = {
542 pEmitter1 = pEmitter {
543 Inputs = {
544 RandomSeed = Input { Value = 19, },
545 Number = Input { Value = 8, },
546 Lifespan = Input { Value = 40, },
547 Velocity = Input { Value = 0.5, },
548 VelocityVariance = Input { Value = 0.25, },
549 AngleVariance = Input { Value = 180, },
550 },
551 },
552 pRender1 = pRender {
553 Inputs = {
554 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
555 },
556 },
557 },
558}
559"#;
560 let mut comp_a = Comp::from_fusion_str(with_unrelated).expect("parse comp_a");
561 let comp_b = Comp::from_fusion_str(without_unrelated).expect("parse comp_b");
562 let ra = comp_a.node_by_name["pRender1"];
563 let rb = comp_b.node_by_name["pRender1"];
564 assert_ne!(ra, rb, "deleting UnrelatedBg should shift NodeIds");
565
566 let expr = ExprEngine::new();
567 let sprites = HashMap::new();
568 let dims = [64, 64];
569
570 let mut cache_a = ParticleCache::default();
571 let img_a_20 = render_particles_2d(&mut cache_a, &comp_a, ra, 20.0, dims, &expr, &sprites);
572
573 // Bumping comp.revision (simulating unrelated node edit) reuses cached simulation.
574 comp_a.revision += 99;
575 let hash_before = particle_subgraph_hash(&comp_a, ra, 0);
576 let hash_after_del = particle_subgraph_hash(&comp_b, rb, 0);
577 assert_eq!(hash_before, hash_after_del, "subgraph hash must ignore NodeId shifts and unrelated nodes");
578
579 let mut cache_b = ParticleCache::default();
580 let img_b_20 = render_particles_2d(&mut cache_b, &comp_b, rb, 20.0, dims, &expr, &sprites);
581 assert_eq!(img_a_20, img_b_20, "deleting an unrelated node must not re-randomize particles");
582
583 // Stepping comp_b using cache_a (which has old NodeIds!) must still match fresh comp_b at frame 21.
584 let img_a_to_b_21 = render_particles_2d(&mut cache_a, &comp_b, rb, 21.0, dims, &expr, &sprites);
585 let mut fresh_b = ParticleCache::default();
586 let img_b_21 = render_particles_2d(&mut fresh_b, &comp_b, rb, 21.0, dims, &expr, &sprites);
587 assert_eq!(img_a_to_b_21, img_b_21);
588}
589
590#[test]
591fn cyclic_expression_modifier_does_not_stack_overflow() {
592 let comp_src = r#"
593Composition {
594 GlobalRange = { 0, 20 },
595 RenderRange = { 0, 20 },
596 Tools = ordered() {
597 Expr1 = Expression {
598 Inputs = {
599 n1 = Input { SourceOp = "Expr1", Source = "NumberResult", },
600 NumberExpression = Input { Value = "n1 + 4", },
601 },
602 },
603 pEmitter1 = pEmitter {
604 Inputs = {
605 RandomSeed = Input { Value = 7, },
606 Number = Input { SourceOp = "Expr1", Source = "NumberResult", },
607 Lifespan = Input { Value = 15, },
608 Velocity = Input { Value = 0.2, },
609 },
610 },
611 pRender1 = pRender {
612 Inputs = {
613 Input = Input { SourceOp = "pEmitter1", Source = "Output", },
614 },
615 },
616 },
617}
618"#;
619 let comp = Comp::from_fusion_str(comp_src).expect("parse cyclic Expr1 comp");
620 assert!(comp.expression_modifiers.contains_key("Expr1"));
621 let pr = comp.find_node("pRender1").expect("find pRender1");
622 let expr = ExprEngine::new();
623 let sprites = HashMap::new();
624 let mut cache = ParticleCache::default();
625
626 // Both subgraph hashing and particle evaluation must terminate cleanly without overflowing.
627 let h1 = particle_subgraph_hash(&comp, pr, 0);
628 let h2 = particle_subgraph_hash(&comp, pr, 0);
629 assert_eq!(h1, h2);
630
631 let img = render_particles_2d(&mut cache, &comp, pr, 5.0, [64, 64], &expr, &sprites);
632 assert_eq!(img.len(), 64 * 64 * 4);
633}
634
635#[test]
636fn test_p_vortex_swirl_and_spiral_angle() {
637 let make_comp = |angle: f64| {
638 format!(
639 r#"Composition {{
640 GlobalRange = {{ 0, 20 }},
641 RenderRange = {{ 0, 20 }},
642 Tools = ordered() {{
643 EN = BezierSpline {{
644 KeyFrames = {{
645 [0] = {{ 1, Flags = {{ Linear = true }} }},
646 [1] = {{ 0, Flags = {{ Linear = true }} }},
647 }},
648 }},
649 E = pEmitter {{
650 Inputs = {{
651 Number = Input {{ SourceOp = "EN", Source = "Value", }},
652 Lifespan = Input {{ Value = 50, }},
653 Region = Input {{ Value = FuID {{ "Point" }}, }},
654 ["Point.Translate.X"] = Input {{ Value = 0.2, }},
655 ["Point.Translate.Y"] = Input {{ Value = 0.0, }},
656 Style = Input {{ Value = FuID {{ "ParticleStyleBlob" }}, }},
657 ["ParticleStyle.Size"] = Input {{ Value = 0.5, }},
658 }},
659 }},
660 V = pVortex {{
661 Inputs = {{
662 Input = Input {{ SourceOp = "E", Source = "Output", }},
663 Strength = Input {{ Value = 1.0, }},
664 Power = Input {{ Value = 1.0, }},
665 Angle = Input {{ Value = {angle}, }},
666 ["Translate.X"] = Input {{ Value = 0.0, }},
667 ["Translate.Y"] = Input {{ Value = 0.0, }},
668 }},
669 }},
670 R = pRender {{
671 Inputs = {{ Input = Input {{ SourceOp = "V", Source = "Output", }}, }},
672 }},
673 }},
674 }}"#
675 )
676 };
677
678 let expr = ExprEngine::new();
679 let empty_fields = HashMap::new();
680
681 let no_sprites = HashMap::new();
682 // Angle = 0: pure CCW tangential swirl -> particle at (+0.2, 0) gains +Y velocity.
683 let comp_tan = Comp::from_fusion_str(&make_comp(0.0)).unwrap();
684 let plan_tan = build_particle_plan(&comp_tan, comp_tan.node_by_name["R"]);
685 let mut state_tan = SimState::default();
686 for f in 0..=4 {
687 step_simulation(&mut state_tan, &comp_tan, &plan_tan, &empty_fields, &no_sprites, f, 0, &expr);
688 }
689 let p_tan = state_tan.particles[0];
690 assert!(p_tan.vel[1] > 0.01, "tangential vortex must swirl +Y at (+X,0), got {:?}", p_tan.vel);
691
692 // Angle = +60: inward spiral -> radius shrinks relative to initial 0.2.
693 let comp_in = Comp::from_fusion_str(&make_comp(60.0)).unwrap();
694 let plan_in = build_particle_plan(&comp_in, comp_in.node_by_name["R"]);
695 let mut state_in = SimState::default();
696 for f in 0..=5 {
697 step_simulation(&mut state_in, &comp_in, &plan_in, &empty_fields, &no_sprites, f, 0, &expr);
698 }
699 let r_in = state_in.particles[0].pos[0].hypot(state_in.particles[0].pos[1]);
700 assert!(r_in < 0.19, "positive Angle vortex should spiral inward, got r={r_in}");
701
702 // Angle = -60: outward spiral -> radius grows relative to initial 0.2.
703 let comp_out = Comp::from_fusion_str(&make_comp(-60.0)).unwrap();
704 let plan_out = build_particle_plan(&comp_out, comp_out.node_by_name["R"]);
705 let mut state_out = SimState::default();
706 for f in 0..=5 {
707 step_simulation(&mut state_out, &comp_out, &plan_out, &empty_fields, &no_sprites, f, 0, &expr);
708 }
709 let r_out = state_out.particles[0].pos[0].hypot(state_out.particles[0].pos[1]);
710 assert!(r_out > 0.21, "negative Angle vortex should spiral outward, got r={r_out}");
711}
712
713#[test]
714fn test_p_flock_cohesion_separation_alignment_and_perf() {
715 let comp_src = r#"Composition {
716 GlobalRange = { 0, 20 },
717 RenderRange = { 0, 20 },
718 Tools = ordered() {
719 EN = BezierSpline {
720 KeyFrames = {
721 [0] = { 1, Flags = { Linear = true } },
722 [1] = { 0, Flags = { Linear = true } },
723 },
724 },
725 E1 = pEmitter {
726 Inputs = {
727 Number = Input { SourceOp = "EN", Source = "Value", },
728 Lifespan = Input { Value = 50, },
729 Region = Input { Value = FuID { "Point" }, },
730 ["Point.Translate.X"] = Input { Value = -0.08, },
731 Velocity = Input { Value = 0.0, },
732 },
733 },
734 E2 = pEmitter {
735 Inputs = {
736 Number = Input { SourceOp = "EN", Source = "Value", },
737 Lifespan = Input { Value = 50, },
738 Region = Input { Value = FuID { "Point" }, },
739 ["Point.Translate.X"] = Input { Value = 0.08, },
740 Velocity = Input { Value = 0.2, },
741 Angle = Input { Value = 90.0, },
742 },
743 },
744 M = pMerge {
745 Inputs = {
746 Input1 = Input { SourceOp = "E1", Source = "Output", },
747 Input2 = Input { SourceOp = "E2", Source = "Output", },
748 },
749 },
750 FlockCoh = pFlock {
751 Inputs = {
752 Input = Input { SourceOp = "M", Source = "Output", },
753 Cohesion = Input { Value = 1.0, },
754 Separation = Input { Value = 0.0, },
755 Alignment = Input { Value = 1.0, },
756 Radius = Input { Value = 0.3, },
757 Strength = Input { Value = 1.0, },
758 },
759 },
760 FlockSep = pFlock {
761 Inputs = {
762 Input = Input { SourceOp = "M", Source = "Output", },
763 Cohesion = Input { Value = 0.0, },
764 Separation = Input { Value = 1.0, },
765 Alignment = Input { Value = 0.0, },
766 Radius = Input { Value = 0.4, },
767 Strength = Input { Value = 1.0, },
768 },
769 },
770 RCoh = pRender { Inputs = { Input = Input { SourceOp = "FlockCoh", Source = "Output", }, }, },
771 RSep = pRender { Inputs = { Input = Input { SourceOp = "FlockSep", Source = "Output", }, }, },
772 },
773 }"#;
774 let comp = Comp::from_fusion_str(comp_src).unwrap();
775 let expr = ExprEngine::new();
776 let empty_fields = HashMap::new();
777
778 let no_sprites = HashMap::new();
779 // Cohesion + Alignment: particles at x=-0.08 and x=+0.08 pull together and share +Y velocity.
780 let plan_coh = build_particle_plan(&comp, comp.node_by_name["RCoh"]);
781 let mut state_coh = SimState::default();
782 for f in 0..=4 {
783 step_simulation(&mut state_coh, &comp, &plan_coh, &empty_fields, &no_sprites, f, 0, &expr);
784 }
785 let dx_coh = (state_coh.particles[1].pos[0] - state_coh.particles[0].pos[0]).abs();
786 assert!(dx_coh < 0.155, "cohesion should pull particles closer than 0.16, got {dx_coh}");
787 assert!(state_coh.particles[0].vel[1] > 0.002, "alignment should pull stationary particle toward neighbor +Y vel");
788
789 // Separation: particles within repel radius push farther apart.
790 let plan_sep = build_particle_plan(&comp, comp.node_by_name["RSep"]);
791 let mut state_sep = SimState::default();
792 for f in 0..=4 {
793 step_simulation(&mut state_sep, &comp, &plan_sep, &empty_fields, &no_sprites, f, 0, &expr);
794 }
795 let dx_sep = (state_sep.particles[1].pos[0] - state_sep.particles[0].pos[0]).abs();
796 assert!(dx_sep > 0.165, "separation should push particles farther than 0.16, got {dx_sep}");
797
798 // Stress check: 2,000 overlapping particles must step quickly without O(N^2) stall.
799 let heavy_src = r#"Composition {
800 GlobalRange = { 0, 5 },
801 RenderRange = { 0, 5 },
802 Tools = ordered() {
803 E = pEmitter {
804 Inputs = {
805 Number = Input { Value = 1000, },
806 Lifespan = Input { Value = 20, },
807 },
808 },
809 F = pFlock {
810 Inputs = {
811 Input = Input { SourceOp = "E", Source = "Output", },
812 Radius = Input { Value = 0.2, },
813 },
814 },
815 R = pRender { Inputs = { Input = Input { SourceOp = "F", Source = "Output", }, }, },
816 },
817 }"#;
818 let heavy_comp = Comp::from_fusion_str(heavy_src).unwrap();
819 let heavy_plan = build_particle_plan(&heavy_comp, heavy_comp.node_by_name["R"]);
820 let mut heavy_state = SimState::default();
821 let t0 = Instant::now();
822 for f in 0..=2 {
823 step_simulation(&mut heavy_state, &heavy_comp, &heavy_plan, &empty_fields, &no_sprites, f, 0, &expr);
824 }
825 assert!(heavy_state.particles.len() >= 2000);
826 assert!(
827 t0.elapsed() < std::time::Duration::from_secs(2),
828 "pFlock on 2000+ overlapping particles took too long: {:?}",
829 t0.elapsed()
830 );
831}
832
833#[test]
834fn test_p_spawn_children_velocity_transfer_and_cap() {
835 let comp_src = r#"Composition {
836 GlobalRange = { 0, 20 },
837 RenderRange = { 0, 20 },
838 Tools = ordered() {
839 EN = BezierSpline {
840 KeyFrames = {
841 [0] = { 2, Flags = { Linear = true } },
842 [1] = { 0, Flags = { Linear = true } },
843 },
844 },
845 E = pEmitter {
846 Inputs = {
847 Number = Input { SourceOp = "EN", Source = "Value", },
848 Lifespan = Input { Value = 20, },
849 Velocity = Input { Value = 0.5, },
850 Angle = Input { Value = 0.0, },
851 Style = Input { Value = FuID { "ParticleStyleBlob" }, },
852 ["ParticleStyle.Size"] = Input { Value = 0.6, },
853 ["ParticleStyle.Red"] = Input { Value = 1.0, },
854 ["ParticleStyle.Green"] = Input { Value = 0.0, },
855 ["ParticleStyle.Blue"] = Input { Value = 0.0, },
856 },
857 },
858 S = pSpawn {
859 Inputs = {
860 Input = Input { SourceOp = "E", Source = "Output", },
861 Number = Input { Value = 3, },
862 Lifespan = Input { Value = 10, },
863 VelocityTransfer = Input { Value = 0.5, },
864 Velocity = Input { Value = 0.2, },
865 Angle = Input { Value = 90.0, },
866 Set = Input { Value = 2, },
867 Style = Input { Value = FuID { "ParticleStyleNGon" }, },
868 ["ParticleStyle.Red"] = Input { Value = 0.0, },
869 ["ParticleStyle.Green"] = Input { Value = 1.0, },
870 ["ParticleStyle.Blue"] = Input { Value = 0.0, },
871 },
872 },
873 R = pRender {
874 Inputs = { Input = Input { SourceOp = "S", Source = "Output", }, },
875 },
876 },
877 }"#;
878 let comp = Comp::from_fusion_str(comp_src).unwrap();
879 let render_id = comp.node_by_name["R"];
880 let plan = build_particle_plan(&comp, render_id);
881 let expr = ExprEngine::new();
882 let empty_fields = HashMap::new();
883 let mut state = SimState::default();
884
885 // Frame 0: 2 parents emitted. Frame 1: each parent spawns 3 children => 2 + 6 = 8.
886 // Frame 2: only the 2 original parents spawn (NOT the 6 children!) => 2 + 6 + 6 = 14.
887 let no_sprites = HashMap::new();
888 for f in 0..=2 {
889 step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr);
890 }
891 assert_eq!(state.particles.len(), 14, "expected 2 parents + 12 non-recursive children");
892
893 let spawn_hash = stable_name_hash("S");
894 let children: Vec<_> = state.particles.iter().filter(|p| p.emitter_hash == spawn_hash).collect();
895 assert_eq!(children.len(), 12);
896 for ch in &children {
897 assert_eq!(ch.sets, 1u32 << 1, "child should be assigned to Set 2");
898 assert_eq!(ch.style_override, Some(ParticleStyleKind::NGon));
899 assert!((ch.vel[0] - 0.025).abs() < 1e-6, "VelocityTransfer=0.5 of vx=0.05 -> 0.025, got {}", ch.vel[0]);
900 assert!((ch.vel[1] - 0.02).abs() < 1e-6, "Velocity=0.2 at Angle=90 -> vy=0.02, got {}", ch.vel[1]);
901 assert!((ch.color[1] - 1.0).abs() < 1e-5 && ch.color[0] < 1e-5);
902 }
903
904 // Verify rendering includes both parents and children.
905 let mut cache = ParticleCache::default();
906 let img = render_particles_2d(&mut cache, &comp, render_id, 3.0, [128, 128], &expr, &HashMap::new());
907 let green_px = img.chunks_exact(4).filter(|c| c[1] > 40 && c[0] < 10 && c[3] > 20).count();
908 assert!(green_px > 10, "spawned green child particles must render visibly, got {green_px}");
909}
910
911#[test]
912fn test_p_change_style_color_size_style_and_region() {
913 let comp_src = r#"Composition {
914 GlobalRange = { 0, 10 },
915 RenderRange = { 0, 10 },
916 Tools = ordered() {
917 EN = BezierSpline {
918 KeyFrames = {
919 [0] = { 1, Flags = { Linear = true } },
920 [1] = { 0, Flags = { Linear = true } },
921 },
922 },
923 E_in = pEmitter {
924 Inputs = {
925 Number = Input { SourceOp = "EN", Source = "Value", },
926 Lifespan = Input { Value = 20, },
927 Region = Input { Value = FuID { "Point" }, },
928 ["Point.Translate.X"] = Input { Value = -0.2, },
929 Style = Input { Value = FuID { "ParticleStylePoint" }, },
930 ["ParticleStyle.Size"] = Input { Value = 0.2, },
931 ["ParticleStyle.Red"] = Input { Value = 1.0, },
932 ["ParticleStyle.Green"] = Input { Value = 0.0, },
933 ["ParticleStyle.Blue"] = Input { Value = 0.0, },
934 },
935 },
936 E_out = pEmitter {
937 Inputs = {
938 Number = Input { SourceOp = "EN", Source = "Value", },
939 Lifespan = Input { Value = 20, },
940 Region = Input { Value = FuID { "Point" }, },
941 ["Point.Translate.X"] = Input { Value = 0.25, },
942 Style = Input { Value = FuID { "ParticleStylePoint" }, },
943 ["ParticleStyle.Size"] = Input { Value = 0.2, },
944 ["ParticleStyle.Red"] = Input { Value = 1.0, },
945 ["ParticleStyle.Green"] = Input { Value = 0.0, },
946 ["ParticleStyle.Blue"] = Input { Value = 0.0, },
947 },
948 },
949 M = pMerge {
950 Inputs = {
951 Input1 = Input { SourceOp = "E_in", Source = "Output", },
952 Input2 = Input { SourceOp = "E_out", Source = "Output", },
953 },
954 },
955 CS = pChangeStyle {
956 Inputs = {
957 Input = Input { SourceOp = "M", Source = "Output", },
958 Region = Input { Value = FuID { "SphereRgn" }, },
959 ["SphereRgn.Translate.X"] = Input { Value = -0.2, },
960 ["SphereRgn.Size"] = Input { Value = 0.2, },
961 Style = Input { Value = FuID { "ParticleStyleBlob" }, },
962 ["ParticleStyle.Size"] = Input { Value = 1.5, },
963 ["ParticleStyle.Red"] = Input { Value = 0.0, },
964 ["ParticleStyle.Green"] = Input { Value = 0.0, },
965 ["ParticleStyle.Blue"] = Input { Value = 1.0, },
966 Probability = Input { Value = 1.0, },
967 },
968 },
969 R = pRender {
970 Inputs = { Input = Input { SourceOp = "CS", Source = "Output", }, },
971 },
972 },
973 }"#;
974 let comp = Comp::from_fusion_str(comp_src).unwrap();
975 let render_id = comp.node_by_name["R"];
976 let plan = build_particle_plan(&comp, render_id);
977 let expr = ExprEngine::new();
978 let empty_fields = HashMap::new();
979 let no_sprites = HashMap::new();
980 let mut state = SimState::default();
981 for f in 0..=2 {
982 step_simulation(&mut state, &comp, &plan, &empty_fields, &no_sprites, f, 0, &expr);
983 }
984 let p_in = state.particles.iter().find(|p| p.pos[0] < 0.0).unwrap();
985 let p_out = state.particles.iter().find(|p| p.pos[0] > 0.0).unwrap();
986
987 assert_eq!(p_in.style_override, Some(ParticleStyleKind::Blob));
988 assert!((p_in.size - 1.5).abs() < 1e-6);
989 assert!(p_in.color[2] > 0.99 && p_in.color[0] < 0.01);
990
991 assert_eq!(p_out.style_override, None, "particle outside Region must keep original style");
992 assert!((p_out.size - 0.2).abs() < 1e-6);
993 assert!(p_out.color[0] > 0.99 && p_out.color[2] < 0.01);
994
995 // Rendered buffer must have a large blue blob on the left and a tiny red point on the right.
996 let mut cache = ParticleCache::default();
997 let img = render_particles_2d(&mut cache, &comp, render_id, 2.0, [100, 100], &expr, &HashMap::new());
998 let blue_px = img.chunks_exact(4).filter(|c| c[2] > 40 && c[0] < 10 && c[3] > 20).count();
999 let red_px = img.chunks_exact(4).filter(|c| c[0] > 40 && c[2] < 10 && c[3] > 20).count();
1000 assert!(blue_px > 20, "changed particle must render as a large blue blob, got {blue_px}");
1001 assert_eq!(red_px, 1, "unchanged particle must remain a 1px red point, got {red_px}");
1002}
1003
1004#[test]
1005fn test_p_gradient_force_luma_gradient_and_gpu_eval() {
1006 let comp_src = r#"Composition {
1007 GlobalRange = { 0, 10 },
1008 RenderRange = { 0, 10 },
1009 Tools = ordered() {
1010 RampBG = Background {
1011 Inputs = {
1012 Width = Input { Value = 64, },
1013 Height = Input { Value = 64, },
1014 Type = Input { Value = FuID { "Horizontal" }, },
1015 TopLeftRed = Input { Value = 0.0, },
1016 TopLeftGreen = Input { Value = 0.0, },
1017 TopLeftBlue = Input { Value = 0.0, },
1018 TopRightRed = Input { Value = 1.0, },
1019 TopRightGreen = Input { Value = 1.0, },
1020 TopRightBlue = Input { Value = 1.0, },
1021 },
1022 },
1023 EN = BezierSpline {
1024 KeyFrames = {
1025 [0] = { 1, Flags = { Linear = true } },
1026 [1] = { 0, Flags = { Linear = true } },
1027 },
1028 },
1029 E = pEmitter {
1030 Inputs = {
1031 Number = Input { SourceOp = "EN", Source = "Value", },
1032 Lifespan = Input { Value = 30, },
1033 Region = Input { Value = FuID { "Point" }, },
1034 ["Point.Translate.X"] = Input { Value = 0.0, },
1035 ["Point.Translate.Y"] = Input { Value = 0.0, },
1036 Style = Input { Value = FuID { "ParticleStyleBlob" }, },
1037 ["ParticleStyle.Size"] = Input { Value = 1.0, },
1038 },
1039 },
1040 GF = pGradientForce {
1041 Inputs = {
1042 Input = Input { SourceOp = "E", Source = "Output", },
1043 Image = Input { SourceOp = "RampBG", Source = "Output", },
1044 Strength = Input { Value = 1.0, },
1045 },
1046 },
1047 R = pRender {
1048 Inputs = {
1049 Width = Input { Value = 64, },
1050 Height = Input { Value = 64, },
1051 Input = Input { SourceOp = "GF", Source = "Output", },
1052 },
1053 },
1054 },
1055 }"#;
1056 let comp = Comp::from_fusion_str(comp_src).unwrap();
1057 let render_id = comp.node_by_name["R"];
1058 let gf_id = comp.node_by_name["GF"];
1059 let bg_id = comp.node_by_name["RampBG"];
1060
1061 // 1. Verify bitmap/image source discovery for pGradientForce.
1062 let sources = collect_bitmap_sources(&comp, render_id);
1063 assert_eq!(sources, vec![(gf_id, bg_id)]);
1064
1065 // 2. Build a synthetic left-to-right linear ramp sprite and verify rightward acceleration.
1066 let mut ramp_rgba = vec![0u8; 64 * 64 * 4];
1067 for y in 0..64 {
1068 for x in 0..64 {
1069 let v = (x * 255 / 63) as u8;
1070 let off = (y * 64 + x) * 4;
1071 ramp_rgba[off] = v;
1072 ramp_rgba[off + 1] = v;
1073 ramp_rgba[off + 2] = v;
1074 ramp_rgba[off + 3] = 255;
1075 }
1076 }
1077 let mut sprites = HashMap::new();
1078 sprites.insert(gf_id, SpriteImage { size: [64, 64], rgba: ramp_rgba });
1079
1080 let plan = build_particle_plan(&comp, render_id);
1081 let fields = super::forces::build_gradient_fields(&comp, &plan.modifiers, &sprites);
1082 let expr = ExprEngine::new();
1083 let mut state = SimState::default();
1084 for f in 0..=4 {
1085 step_simulation(&mut state, &comp, &plan, &fields, &sprites, f, 0, &expr);
1086 }
1087 let p = state.particles[0];
1088 assert!(p.vel[0] > 0.02, "left-to-right bright ramp must push particle +X, got {:?}", p.vel);
1089 assert!(p.vel[1].abs() < 1e-4, "horizontal ramp must have near-zero Y force, got {}", p.vel[1]);
1090 assert!(p.pos[0] > 0.05, "particle should move significantly right, got x={}", p.pos[0]);
1091
1092 // 3. End-to-end GPU evaluation of RampBG -> pGradientForce -> pRender.
1093 let Ok(mut eng) = crate::Engine::new_headless() else { return };
1094 let (tex, stats) = eng.evaluate(&comp, render_id, 5.0);
1095 assert!(stats.unsupported_nodes.is_empty(), "unsupported: {:?}", stats.unsupported_nodes);
1096 let px = eng.readback_rgba(&tex).unwrap();
1097 let px_ref = px.as_slice();
1098 // Particle started at x=32 and moved right into the right half (x > 36).
1099 let right_mass: u64 = (0..64)
1100 .flat_map(|y| (36..64).map(move |x| px_ref[(y * 64 + x) * 4 + 3] as u64))
1101 .sum();
1102 let left_mass: u64 = (0..64)
1103 .flat_map(|y| (0..28).map(move |x| px_ref[(y * 64 + x) * 4 + 3] as u64))
1104 .sum();
1105 assert!(
1106 right_mass > left_mass * 4 + 100,
1107 "GPU pGradientForce should push particle into right half (left={left_mass}, right={right_mass})"
1108 );
1109}