Repository navigation
Particles: preserve emitted particles when Birth Rate changes (#1373) - #1382
Conversation
…torytold#1373) EffectHost::param_at(effect_index, id, layer_time) evaluates a single parameter (by its flattened id, such as birthRate or group/param) of the effect at that index in the layer's stack. A simulation can then sample one parameter at every step without flattening the whole effect each time. The effect index is passed in, rather than read from the renderer's running-effect index as params_at does: GPU-batched effects never update that index, so a batched effect would read another effect's parameters. The renderer's implementation walks the effect group by match id and evaluates just that property (keyframes and expressions) at the comp time of the layer time.
…ame inputs (storytold#1373) SimCache::run_where takes a check on each checkpoint (its step and state). A simulation whose steps depend on inputs outside its key, such as a parameter's values over time, resumes only from a checkpoint reached with the same inputs. A fresh checkpoint now replaces one at the same step, so a changed history doesn't leave stale checkpoints behind. run() is run_where with no check.
psim::births_at builds the GPU's birth schedule (the birth step of every particle id) from the rate of each step, with the CPU step's carry arithmetic. births() for a constant rate now uses it. It stops as soon as the cap would be passed, before adding a step's births, so a huge rate can't build a huge list first.
…a burst lives on (storytold#1373) The simulation runs from layer time 0, but it used the Birth Rate of the rendered frame for every step. A rate keyed from 3 down to 0 simulated no births at all once it reached 0, so the burst's particles vanished that frame although their Longevity had not run out. A ramped rate was simulated as if its current value had always applied. Each step now samples Birth Rate at its own layer time through EffectHost::param_at (with the effect's own index). Without such a host the current value applies throughout, as before. Particles already alive keep moving and ageing whatever the rate. The carry and particle ids continue across rate changes. Birth Rate is left out of the simulation cache key, so an animated rate no longer restarts the simulation every frame. In its place: - The CPU state keeps a hash of the rates it was stepped with (hist). A checkpoint is resumed only when that matches the current history, so moving an earlier key is never served from a stale checkpoint. - The GPU gets the birth schedule of the sampled rates (births_at). Its checkpoints record which births they hold, and are resumed only when the request's births before that step are the same. A constant Birth Rate gives exactly the particles it did. CC Particle Systems II and CC Mr. Mercury keep their constant-rate simulation.
…rytold#1373) A burst lives on after the rate drops to 0: as a constant rate during it, only its particles afterwards, still moving, gone after their Longevity. Another effect's history isn't used. A ramp bears what each moment asked for. Seeking backward and forward matches a simulation from 0. Moving an earlier key isn't served from stale checkpoints with the same current rate. A constant rate is unchanged. The birth schedule matches the CPU's ids.
…rytold#1373) The issue's scene: a Birth Rate keyed 3 until 0.5 s then 0 matches the constant rate during the burst, still draws the burst at 0.9 s with no new births, and is empty once Longevity has run out. Moving the earlier key changes the 0.9 s frame through the layer cache. Layers offset by 0.5 s or stretched 200 % sample their own layer times.
…ytold#1373) With a Birth Rate keyed into a burst, the GPU's particles match the CPU simulation through the burst and after it. A second history with the same system key, whose burst starts later (so the same ids are born at other steps), isn't resumed from the first history's GPU checkpoints.
…d checkpoint checks (storytold#1373)
…storytold#1373) FxHost::param_at mapped the requested layer time to comp time, and EvalCtx::value mapped it back. Under a fractional stretch (33.3 %, 133.33 %) that round trip truncates a tick early, so a Hold key at 0.5 s read its previous value at the step starting there: a Birth Rate cut to 0 at 0.5 s bore one more step of particles (six). EvalCtx::value_at_layer_time reads the keyframes at the given layer time exactly, and still runs an expression at the comp time. EvalCtx::value now uses it, and param_at reads through it. EffectHost::param_is_static says when a parameter has no keyframes and no expression. CC Particle World then skips sampling its Birth Rate at every step, since it's the same throughout.
…storytold#1373) The simulation checks its checkpoints against the Birth Rate history, but a cached layer is returned before the effect runs. The layer key sees the layer's keyframes and its current values, not what an expression returned at earlier times: a Birth Rate expression reading another layer could change the history while the current rate, 0, stayed the same. effects::reads_param_history names CC Particle World. The renderer already declines to cache layers whose time effects (ec.time.*) meet expressions. It now treats Particle World the same way, in the layer key, the input key (only for the effects that input runs) and the dependency walk.
…an expression's history (storytold#1373) A Hold cut at 0.5 s reads 3 just before and 0 at 0.5 s under 33.3 %, 133.33 % and 200 % stretch, and the frame matches the unstretched one. A Birth Rate expression reading a Controller layer's Opacity before 0.5 s: changing the Controller changes the 0.9 s frame, and the cached render matches the uncached one.
…d checks the GPU ran (storytold#1373) The two histories now bear the same 180 particles by 0.9 s at different steps (3/s from 0 to 0.5 s, and from 0.25 to 0.75 s). So a checkpoint check by birth count alone fails the test, not only no check. A test-only dispatch counter asserts each GPU state really came from the GPU rather than the CPU fallback.
…or Particle World (storytold#1373)
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Parameter sampling and cache eligibility crates/effects/src/lib.rs, crates/render/src/eval.rs, crates/render/src/fx_host.rs, crates/render/src/cache.rs, crates/render/src/cache/dependencies.rs |
Effect hosts can sample a parameter at a specified layer time and report whether it is static. Render evaluation uses the supplied layer time for keyframes. Render cache checks now account for expressions on layers with effects that read other times or parameter history. |
Per-step Birth Rate simulation crates/effects/src/psim.rs, crates/effects/src/sim/engine.rs, crates/effects/src/sim/particle_fx.rs, crates/effects/src/sim.rs, crates/effects/src/util.rs, crates/gpu/src/particles.rs |
CC Particle World builds a per-step Birth Rate schedule and uses it to generate births. Existing particles continue to move and age. CPU and GPU checkpoints validate recorded birth history; Particle Systems II retains its fixed-rate path. |
Checkpoint validation and rendering tests crates/effects/src/sim/tests.rs, crates/render/src/tests.rs, crates/gpu/src/tests/particles.rs, docs/architecture.md |
Tests cover keyed rates, seeking, cache behavior, layer-time mapping, expression-driven inputs, and CPU/GPU particle-state comparisons. The architecture documentation describes rate sampling and checkpoint rules. |
Priority:
Estimated code review effort: 4 (Complex) | ~45 minutes
Change: Bug fix · Severity of issue fixed: Medium
Sequence Diagram(s)
sequenceDiagram
participant pw_state
participant rate_history
participant EffectHost
participant accelerated
pw_state->>rate_history: Build per-step Birth Rate schedule
rate_history->>EffectHost: Check static status and sample keyed values
rate_history-->>pw_state: Return per-step rates
pw_state->>accelerated: Simulate with per-step rates
Possibly related issues
- [Bug Report] [EffectCraft] CC Particle World: keyframed Birth Rate removes all particles once the rate reaches 0 #2004: The issue describes keyed Birth Rate reaching zero while existing particles should remain alive. This PR samples rates per step and advances existing particles using that history.
Suggested reviewers: aiongg, echelon
Merge Risk: 
Minimal · up to 902b5
-
No actionable issue was identified in the reviewed change; it is mergeable after normal checks.
Security architecture risk: 
Moderate · up to cedc7
-
The change preserves effect ownership and improves checkpoint correctness. However, very large project-defined layer times can now force large temporary allocations before cached results can be reused, creating a render-process resource-exhaustion risk.
Retained concerns
- Medium · security · inferred: Historical sampling introduces uncapped temporary memory growth before cache reuse or backend limits. A project with a large positive layer time, including one produced by its start time or stretch, causes a rate vector proportional to elapsed steps; CPU fallback also constructs a prefix vector. This occurs even for static rates and warm simulation checkpoints. Rendering such content can exhaust process memory. Existing cold-replay costs predate the PR, but these unconditional history allocations are new.
Security review details
-
Security Blast Radius
- inferred — The identified resource risk affects the process rendering the supplied project and potentially other work sharing its memory. Exploitation requires control over project timing and triggering a render. Remote reachability, tenant sharing, and deployment-level containment are not established.
Security Findings and Attack Paths
- inferred — Project timing reaches EffectCtx.time and determines the materialized history length. A large layer time therefore causes allocation before CPU checkpoint lookup or GPU request rejection. Static-rate detection and particle-count limits do not bound that allocation.
Trust Boundaries and Controls
- observed — Production callers pass the selected stack effect index and the fixed birthRate identifier. Property lookup remains inside that effect group; expression evaluation uses the existing host. The new sampling methods do not invoke guarded layer-rendering entrypoints.
Resilience and Maintainability Implications
- observed — Backend rejection retains CPU fallback, and GPU readback identity includes the complete birth schedule. Deferred readbacks represent in-flight work as a missed pass for retry. These recovery paths do not avoid the history allocation performed beforehand.
Hardening Proposals
- proposed — Establish a per-render history memory and evaluation budget before allocation, with controlled failure or cancellation. Consider bounded streaming or incremental history validation and a fixed-rate fast path, while preserving detection of earlier timeline edits.
Pre-merge checks: 
5
-
Full details: Linked Issues check
-
Explanation
Issue
#1373requires that CC Particle World Birth Rate controls only new births. Existing particles must keep moving and ageing until their Longevity expires. The change samples Birth Rate at each step's own layer time.Phys::step_at_rate(crates/effects/src/sim/engine.rs) uses the sampled rate only for that step's births. Existing particles advance as before.births_atin crates/effects/src/psim.rs keeps the carry arithmetic, so particle ids continue across rate changes.EffectHost::param_atandvalue_at_layer_timeread keyframes at the exact layer time, so a keyed rate reaches zero at the stated time. The reported tests cover the issue's scene: the burst matches a constant rate during the burst, still draws at 0.9 s with no new births, and is empty only after Longevity ends (sim::tests and render tests). A constant rate gives unchanged output (tested bit for bit). Ramped rates, seeking, stretched and offset layers, and expression-driven rates are covered. GPU particles match CPU particles through and after the burst (tests_particles). CPU and GPU checkpoints resume only when their birth or rate history matches, so stale state is not reused. The evidence covers the required behaviour. The assessment relies on the supplied summary and PR description; I did not read the changed source directly in this pass.
Full details: Out of Scope Changes check
-
Explanation
The changes support
#1373. The supporting changes are: exact layer-time evaluation (crates/render/src/eval.rs), the rate-sampling host API (crates/effects/src/lib.rs, crates/render/src/fx_host.rs), the rate-history checkpoints and birth-schedule checks for CPU and GPU (engine.rs, util.rsrun_where, gpu/src/particles.rs), layer-cache and dependency rules for expression-driven Particle World layers (render/src/cache.rs, cache/dependencies.rs), and the tests and the architecture note that describe them. The sharedSimCache::runkeeps its prior selection behaviour through a predicate that accepts every checkpoint. Theacceleratedsignature change passesNonefor CC Particle Systems II, so its constant-rate path is unchanged. The GPUDISPATCHEScounter exists only undercfg(test). No unrelated product, dependency, project format, or default changes appear in the summary. Files of this scope do not show unrelated work. The assessment relies on the supplied summary; the author reports a separate follow-up for CC Particle Systems II and CC Mr. Mercury, which this PR leaves unchanged.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
…story # Conflicts: # crates/effects/src/sim.rs # crates/render/src/lib.rs
|
Merged, thank you @tylerjenningsw! It landed on |
Closes #1373.
CC Particle World simulates from layer time 0 at 60 steps a second, but it used the Birth Rate of the rendered frame for every step. With Birth Rate keyed from 3 down to 0, every frame after the drop was simulated with no births at all, so the burst vanished although its particles were well within their Longevity. A ramped rate was simulated as if its current value had always applied. The issue's 0.25 s frame matched a constant rate of 1.5 pixel for pixel.
The fix
Each step samples Birth Rate at its own layer time. The rate only decides that step's births. Particles already alive keep moving and ageing, and the carry and particle ids continue across rate changes. Without a host that can evaluate the effect at other times, the current value applies throughout, as before.
EffectHost::param_at(effect_index, id, layer_time)evaluates one parameter of one effect. The renderer walks the effect group by match id and evaluates just that property, keyframes and expressions included.EvalCtx::value_at_layer_time). Mapping it to comp time and back truncates a tick early under a fractional stretch, so a Hold cut at 0.5 s read its old value for one step (six extra births at 33.3 % or 133.33 %).EffectHost::param_is_static) isn't sampled at all. The effect passes its ownEffectEnv::effect_index. The existingparams_atreads the renderer's running-effect index, which GPU-batched effects never update, so a batched effect could read another effect's values.PState::hist).SimCache::run_whereresumes only from a checkpoint whose hash matches the current history's prefix. A fresh checkpoint replaces a stale one at the same step.psim::births_at, the CPU's carry arithmetic). Each GPU checkpoint records which births it holds, and is resumed only when the request's births before that step are the same.ec.time.*effects already follow. This applies in the layer key, the input key and the dependency walk (effects::reads_param_history).No project format, dependency or default change.
Checks (Windows 10, Rust 1.95, release; GPU: RTX 3060, Vulkan)
cargo fmt --check, andcargo clippy -p effectcraft-effects -p effectcraft-render -p effectcraft-gpu --no-deps --all-targets --release -- -D warnings: pass. The fix commit also passes clippy on its own.sim::tests, 21/21; 6 new):tests_particles, 5/5; 1 new): with a keyed rate, GPU particles match the CPU (ids, and positions within 1e-4) through and after the burst. The two histories bear the same 180 particles by 0.9 s at different steps, so a checkpoint checked by birth count alone fails. A test-only dispatch counter asserts the GPU really ran rather than the CPU fallback.particle_golden,hostdeterminism and the effectssim/psim/utiltests pass.cargo xtask ciwasn't run locally (it takes over 2.5 hours on this machine). I haven't tried it in the GUI.Not covered here
Summary by CodeRabbit