Keep and merge a run of passes over one shadowed texture once, not once a pass

What: a render pass whose color is a shadowed texture merged back (its truth a buffer's bytes
another writer may write, such as a no-copy buffer's host pages or a Shared buffer's), loaded and
stored, now joins a run of passes (pass_runs.rs) where it may. The run's first pass copies in (or
takes the carried image) and keeps, as before. Each later pass goes on over the image the pass
before it left (ShadowSync::go_on) and keeps nothing. Each pass leaves its merge to its batch's
submission (Deferred::RunMerge, RunWork::Shadow), and the run's next pass takes it over, so the
merge recorded is the last pass's, compared against the first keep. The run holds the keep's
scratch or kept image (RunHeld::Shadow, its MergeLease) until it ends or the command buffer
commits.

Why: consecutive render passes over one merged-back shadowed texture each copied in, kept and
merged the whole texture. 700 one-rectangle passes over a 1024x1024 BGRA8Unorm texture over host
pages took 1,401 shadow copies and 700 merges.

The rule, structural, with no names: a run ends wherever something other than its own passes
could observe or change the bytes, as in the linear runs' list:
- any encoder between its passes that is not a render encoder;
- a render encoder that names the bytes' buffer or its host range, reaches memory it does not
  name, fences, resolves, writes visibility results, records later or renders over a rate map;
- a render encoder with an attachment over those bytes, or whose draws or attachments take a
  shadowed texture over them (the run's own texture bound to a draw included);
- a signal, a wait or a host step (a new batch), and the command buffer's end;
- for a shadow, the encoder no longer carrying the image (the carry dropped).
A shadowed color joins only when it is loaded and stored, has no stand-in and no planes, belongs
to a pass of one render pass instance that is not inline and not inside a predicated laying, is
laid out as one image's texels at level 0 and slice 0, and merges back.

Encoded again: unlike a linear attachment's run, a shadowed texture's run may hold passes over
host pages, which a rebind before the first submission encodes again. Such a pass leaves its run
(RunMerge::leave). The merge it took over records after all (given back), and the pass copies in,
keeps and merges for itself. Where a later pass took its merge over, it keeps the run's scratch
again after its own merge (RunMerge::keep_after), so that later pass's run merges what the passes
after it change. Run segments now record again (Segment::records_again) and splice. An encoder
encoded again takes no pooled scratch or kept image (ShadowMerge::new's `lent`). A run's merge is
resolved at its calls (Deferred::resolved_at_its_calls), so it holds no host pages past commit;
before this, a rebind of such a run was refused with Rebind(EncodedUses).

Two more changes. A run's keep that a later pass writes outside merges and keeps every texel next,
and a run's pass that needs texels the copy-in did not hold copies every texel in. A merge in whole
blocks stays in blocks where the keep holds the pass's area (stores_blocks), under the same
read-to-store window that shadow.rs's Stored attachments already state.

Counted with the existing counters: shadow_copies, shadow_texels_kept and
shadow_merge_dispatches_planned fall from one per pass to one per run. docs/FAST-PATHS.md records
the run and its costs.

Verified on the M3 Max (MoltenVK 1.4.2), --cfg m2v_gpu, under maclock and memcap:
- tests::shadow_runs (new, all pass):
  - runs_over_host_pages_keep_and_merge_once: 1024x1024 BGRA8Unorm over host pages, rows of 15360
    bytes, with blits, foreign render encoders, reads, CPU byte writes, a signal and an aliasing
    view between runs. Mid-sequence and final reads match a private texture byte for byte, and
    the padding is untouched. Joined: 22 copies, 9 merges. Apart: 112 copies, 54 merges.
  - rebound_passes_leave_their_runs: 3 encoders encoded again, 0 refused, bytes equal.
  - scissored_runs_keep_again_once_and_merge_in_blocks: keeps at passes [0, 1], all in blocks.
- Runtime suites (shadow, merge_scratch, linear_runs): 69 of 69 passed.
- Validation runtime_lane (shadow, padded, linear): 91 of 91 passed.
- Negative controls, each a sabotaged build, source restored and touched after; each fails:
  - go_on off: every pass keeps;
  - follows across encoders: keeps [D0, D8, D9, D10, A11, D12];
  - leave off: 961,469 bytes differ;
  - stores_blocks reverted: every scissored pass keeps.
- Release probe, 700 passes in one command buffer, base a543f6428d against this commit: shadow
  copies 3 against 1,401, texels kept 1,048,576 against 734,003,200, merges planned 1 against 700.
  Commit to completion, runs 1-3: 31.6-35.3 ms against 69.3-99.9 ms. With a 1x1 render encoder
  between every two: 61.6-65.0 ms against 107.4-118.6 ms.
On the RTX 5080 Laptop GPU (NVIDIA 615.71.09), the same probe, runs 1-3: 6.6-10.0 ms against
40.6-43.0 ms, and 9.7-12.3 ms against 44.5-47.1 ms. The same 11 shadow, merge and linear device
tests passed there.
clippy --all-targets --all-features -D warnings passed plain and with --cfg m2v_gpu, and cargo fmt
was run.

(cherry picked from commit bf783428131f46c7289518a50b8505e809ecc8e5)
(cherry picked from commit 99949a4cd08cd042e448484e0b1d2f5da1898db5)
This commit is contained in:
Anees Iqbal committed 2026-11-03 15:58:53 -08:00
1 parent eab0350e04
commit 241ee2c0d1
18 files changed
+1641 -246

No files matched your search

@@ -43,6 +43,11 @@
//! its host step needs. Work a host step recorded again since (with the barriers between its
//! commands, `gpu_execute::Rerecord`, or as a route outgrew it, `DeferredPass::outgrown`)
//! keeps that step's recording.
//! - **Runs of passes.** An encoder encoded again that was a pass of a run of passes over a
//! shadowed texture merged once (`pass_runs.rs`) leaves the run once it is put in place: the
//! merge of the run's pass before it, which it took over, records after all, and it keeps the
//! run's scratch after its own merge where a later pass went on over it (`State::replaying_keeps`,
//! recorded by `EncoderCore::finish_replay`). Its run's other passes are not encoded again.
//! - **What cannot be encoded again stays as before.** An encoder whose log is gone (a compute
//! encoder of a command buffer that used command predication whose unit laid a step before it
//! opened, or one of its own that the unit's later encoders read, or a loop, or left work out;
@@ -56,6 +61,7 @@ use super::rebound_use::Unlogged;
use super::{lock, CommandBuffer, CommandBufferShared, State};
use crate::deferred::{Deferred, StepInputs};
use crate::encoder::{RecordedSegment, Replayed, ReplayedPart, Segment};
use crate::pass_runs::RunMerge;
use crate::pipeline::budget::Urgency;
use crate::replay::Replay;
use crate::residency::Residency;
@@ -136,15 +142,27 @@ impl CommandBufferShared {
// A compute encoder of a command buffer that used command predication, which
// encodes again exactly, does so in its unit begun anew
// (`Predication::replaying`).
let residency = {
// The merges its first encoding left to runs of passes (`pass_runs.rs`): encoded again,
// it leaves them, and keeps after itself what a later pass of a run went on over.
let (residency, runs) = {
let mut state = lock(&self.state);
state.replaying_predication = match state.predicated {
true => state.predication.replaying(at),
false => None,
};
state.residency_before(at, replay.kind())
let runs: Vec<Arc<RunMerge>> = (state.recorded.iter())
.filter(|part| part.encoder == at)
.flat_map(|part| &part.segments)
.filter_map(|segment| match &segment.work {
Deferred::RunMerge(merge) => Some(Arc::clone(merge)),
_ => None,
})
.collect();
state.replaying_keeps = runs.iter().filter_map(|run| run.keep_after()).collect();
(state.residency_before(at, replay.kind()), runs)
};
let run = replay.run(&handle, residency);
lock(&self.state).replaying_keeps.clear();
// Its steps' gates listed as the commit lists the first encoding's.
let run = {
let mut state = lock(&self.state);
@@ -190,6 +208,9 @@ impl CommandBufferShared {
Ok(()) => {
counts.encoder_reencoded();
spliced.push(at);
for run in &runs {
run.leave();
}
}
Err(unspliced) => {
counts.reencode_refused();
@@ -314,7 +335,8 @@ impl State {
/// `VkCommandBuffer`s go with the pool unsubmitted. Refused, changing nothing and giving
/// `replayed` back, where its place holds other parts than the encoding again's, or a segment
/// but one whose pipelines are being made (which goes, the encoding again having left one of
/// its own in its place) or work its commit recorded on the GPU alone, or its group was
/// its own in its place), work its commit recorded on the GPU alone, or a run's merge (which
/// goes, the encoding again merging after itself, `pass_runs.rs`), or its group was
/// submitted, or the command buffer completed.
pub(super) fn splice_replay(
&mut self,
@@ -329,8 +351,9 @@ impl State {
let replaces = |part: &usize| {
let recorded = &self.recorded[*part];
recorded.sparse.is_empty()
&& (recorded.segments.iter())
.all(|segment| segment.keeps_pipelines() || segment.gpu_alone)
&& (recorded.segments.iter()).all(|segment| {
segment.keeps_pipelines() || segment.gpu_alone || segment.work.joins_a_run()
})
};
let unjudged = self.unjudged_parts();
let fits = !self.finished()