mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
Share stamped viewport access and retain pass storage between frames
This commit is contained in:
@@ -274,13 +274,37 @@ impl<T: Stored> PerViewport<Stamped<T>> {
|
||||
/// Runs `f` on the record of `viewport` for its pass `pass_nr`; `reset` runs first when the pass changed.
|
||||
#[doc(hidden)]
|
||||
pub fn with_pass<R>(ctx: &Context, viewport: ViewportId, pass_nr: u64, reset: impl FnOnce(&mut T, PassStep), f: impl FnOnce(&mut T) -> R) -> R {
|
||||
Self::with(ctx, viewport, |record| {
|
||||
let mut reset = Some(reset);
|
||||
let mut f = Some(f);
|
||||
let mut out = None;
|
||||
Self::visit_pass(
|
||||
ctx,
|
||||
viewport,
|
||||
pass_nr,
|
||||
&mut |value, step| {
|
||||
if let Some(reset) = reset.take() {
|
||||
reset(value, step);
|
||||
}
|
||||
},
|
||||
&mut |value| {
|
||||
if let Some(f) = f.take() {
|
||||
out = Some(f(value));
|
||||
}
|
||||
},
|
||||
);
|
||||
#[expect(clippy::expect_used, reason = "visit_pass visits the current record exactly once")]
|
||||
out.expect("the stamped viewport record was visited")
|
||||
}
|
||||
|
||||
// Callers share stamp validation and reset ordering, including the parked-record lookup.
|
||||
fn visit_pass(ctx: &Context, viewport: ViewportId, pass_nr: u64, reset: &mut dyn FnMut(&mut T, PassStep), f: &mut dyn FnMut(&mut T)) {
|
||||
Self::visit(ctx, viewport, &mut |record| {
|
||||
if record.pass != pass_nr {
|
||||
reset(&mut record.value, if record.pass + 1 == pass_nr { PassStep::Next } else { PassStep::Skipped });
|
||||
record.pass = pass_nr;
|
||||
}
|
||||
f(&mut record.value)
|
||||
})
|
||||
f(&mut record.value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -225,12 +225,8 @@ fn visit(ctx: &Context, f: &mut dyn FnMut(&PassState)) {
|
||||
/// Moves the context's state into this thread unless it holds it already: the begin hook. States of other contexts
|
||||
/// with no pass running here (a pass a panic left open) go back to their contexts first.
|
||||
pub(crate) fn hold(ctx: &Context) {
|
||||
let stale: Vec<Held> = HELD.with(|held| {
|
||||
let mut held = held.borrow_mut();
|
||||
let (stale, kept) = std::mem::take(&mut held.entries).into_iter().partition(|held| held.ctx != *ctx && frame::running(&held.ctx).is_none());
|
||||
held.entries = kept;
|
||||
stale
|
||||
});
|
||||
let stale: Vec<Held> =
|
||||
HELD.with(|held| held.borrow_mut().entries.extract_if(.., |entry| entry.ctx != *ctx && frame::running(&entry.ctx).is_none()).collect());
|
||||
for held in stale {
|
||||
park(held);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user