AGC: a buffer whose range ends a few KiB into a stale depth plane stores the whole plane on every reused draw (PPSA31162 runs 3-10x slower) #8840
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T02:13:20Z.
What happens
In Super Meat Boy 3D (PPSA31162), level 1-1 runs at one of a few different speeds on the same build, chosen per run. In the slow runs, one small buffer that most draws bind has a range that ends a few KiB inside a 3840x2160 depth plane. Every reuse of those draws then stores the whole 31.9 MiB plane into guest memory, about 240 times per frame.
Same machine, same
mainlibs, same scripted route, 60 s measured in the level:depth plane storewrite-backs per 10 sTwelve runs across
maineea640a5bf,maina806c69bfeand #8756 split into these groups at random; none of the three builds decides it. The figures come from the[draw] pipelinedand[storage] uploads by pathlines. In a slow run's profile, the queue worker spends about 11 % of its samples inShaderResources::Revalidate→FlushDepthPlanes→StorageTexture::FlushPending/writeBack.Where it comes from
A temporary counter in
FlushDepthPlanes, plus a print inRevalidate'sdirectRegionsloop, shows the slow runs' stores coming from one direct region:ShaderData. Its base is 64 KiB-aligned, and its size changes by tens of bytes between frames and runs: 0x10c5c, 0x10c74, 0x10c8c, 0x10cb4. It looks like per-frame data whose descriptor range runs about 3 KiB past its 64 KiB block.Revalidateof a reused build then callsFlushDepthPlanesover this region. That copies the whole depth image into the mirror storage image and writes all 510 blocks of the plane back, for a 3 KiB overlap. In the slow runs this happens about 900 times per 5 s.I can't tell whether the shader ever reads those last 3 KiB. The descriptor says it may.
Why it isn't simply skipped
#7154 (@XairRrR) added these flushes so a buffer read of a depth plane sees the drawn depth. Uncharted (#4001) copies its depth plane through a raw buffer compute and needs that. So a buffer that overlaps a stale plane has to see current depth there, and the fix belongs in how much gets stored, not in whether it does.
Possible directions
SW_64KB_Z_Xeach block is a fixed 2D tile of the image, so the copy and the write-back can be limited to those tiles.@XairRrR, does (1) fit how you meant the stale-plane ranges to work, or would you rather handle this yourself? I'm happy to prototype it and measure the slow runs.
Environment
Ubuntu 24.04 (Linux 6.8), NVIDIA RTX 4070 Ti (595.99.02), GCC 13.3, Release libs. The title is Super Meat Boy 3D (PPSA31162), scripted from its map-load log lines.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T02:33:34Z.
A check on how much of the slow mode these stores explain. A diagnostic switch makes
Revalidate'sdirectRegionsloop skipFlushDepthPlanes, keeping itsCompletePendingCpuWrites. Same level, same machine, samemaina806c69bfeplus the diagnostic, four runs:depth plane storewrite-backs per 10 sAll four land in the fast group. Without the switch, 7 of the 12 earlier runs were slow, so four fast runs in a row by chance is unlikely (about 3 %). The remaining 340 to 440 stores per 10 s come from the other flush sites, as in the fast runs. So in this title the slow mode is these stores and nothing else.
This switch is only a measurement. Skipping the flush breaks the coherence #7154 added for buffer reads of a plane, and it isn't a proposal.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T03:34:25Z.
I traced the current path through
FlushDepthPlanesandStorageTexture::FlushPending. There is a useful 64 KiB range-writeback mechanism inStorageTexture, but changing only theFlushPending(plane, fullPlaneBytes)call to use the requested range is not sufficient:StoreDepthPlanestill copies the whole attachment into the mirror, andMarkWholeWritten()marks every unit pending. More importantly,DepthSurface::SyncFromMemory()has one plane-widesyncedGeneration; after a partial flush, a guest write to one untouched unit can trigger a whole-plane refresh and replace depth data that is still GPU-stale.A safe partial flush appears to need all of these together: per-64 KiB stale tracking for the plane, a way to copy only selected Z_X tiles into the mirror (including pipe-bank XOR and edge tiles), and per-unit guest/GPU generation precedence so untouched stale tiles survive intervening guest writes. The stale units also need to remain registered across partial flushes, with pending state updated only after successful command recording. Without the tile-address mapping and generation work, a range-limited writeback risks silently returning stale mirror bytes or losing newer depth data. This is a source audit; I did not run a title or claim a fix.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T03:46:47Z.
@MarcZX10 thanks, I agree. The plane-wide
syncedGenerationis what makes a range-limited write-back unsafe on its own. After one, a guest write to an untouched unit would reload the whole plane over depth that is still only on the GPU. So option (1) needs your three parts together, and the per-unit generation precedence is the hard one.Before anyone picks a direction, I'll measure where a slow run's time actually goes: the whole-image copy into the mirror, the 31.9 MiB write-back, or the wait for the GPU before it. If it's mostly the wait, option (2) (ordering the store before the reading draw on the GPU) may be the smaller change. I'll post the split here.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T03:56:07Z.
The split, from timers around the three parts of
FlushDepthPlanes. Same level and machine,maina806c69bfeplus the diagnostics. The slow run:SyncFromMemorycheck and recordingStoreDepthPlane's copy into the mirrorMarkWholeWrittenStorageTexture::FlushPendingover the planeSo nearly all the cost is the write-back of the whole 31.9 MiB plane, about 3.1 ms each time, which is close to 10 GB/s whether it's a copy or a wait. Recording the mirror copy is negligible. That calling thread spends about 75 % of its time in these write-backs. In the fast run of the same pair, 22 stores per 5 s took 5.4 ms in all.
This points at the bytes written back rather than the copy into the mirror. Limiting the write-back to the 64 KiB units a read overlaps would cut it by up to 510 times here (one unit in this title). It still needs the per-unit stale tracking and generation precedence you described, so that the units not written back keep the GPU's depth. I haven't split
FlushPendingitself into its GPU wait and its copy.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T05:28:35Z.
Thanks, that split makes the optimization target much clearer: the 31.9 MiB
FlushPendingwriteback accounts for nearly all of the slow-run time, while the CPU-side mirror copy and bookkeeping are small. The range-limited writeback still needs the per-unit stale state and generation precedence we discussed; otherwise an untouched GPU-stale tile can be replaced after a guest write. I agree the next useful evidence is separating wait time from bytes copied insideFlushPending, before choosing between range-limited stores and GPU ordering.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T06:49:27Z.
The split inside
StorageTexture::FlushPending, for the "depth plane store" calls only. These are timers around its parts, plus two inside the write-back paths: buffer allocation andSubmitAndWait. Same level and machine,maina806c69bfeplus the diagnostics, three runs. Two came up in the slow mode, one in the fast mode.Recorder::Sync()orFinishUpTo()writeBackover the flushed images)Inside the write-back, every store took the GPU-direct path: 1,159 to 1,170 direct write-backs and none through the CPU per 5 s in the first slow run. It is recorded behind the open batch with no CPU wait. Buffer allocation is 0.1 ms per 5 s and
SubmitAndWait0 ms. The bytes still move on the GPU: 26,966 to 36,401 MiB (26.3 to 35.5 GiB) stored per 5 s in the slow runs, about 31.9 MiB per store, against 669 to 701 MiB in the fast run.So the CPU side of the 3.1 ms isn't copying. It's about 3.4 to 4.7 ms per wait in
Recorder::Sync(), which submits the open batch and waits for the GPU because that batch has a pending write over the plane. What the GPU runs during those waits includes the earlier stores' whole-plane retiles.I haven't checked which recorded write each sync waits on. One candidate is in the code: the direct path ends with
recorder->NotePendingWrite(firstStored, ...)over the stored span. That leaves the plane with a pending writer in the open batch, so the next store of the same plane in that batch would take theSync()branch even with no draw in between. If that's what happens here, a range-limited store would cut the GPU bytes, but each store would still wait for the batch. Ordering the store on the GPU, or not counting the write-back's own pending write as a reason to sync, would remove the waits. I can count which writer the syncs see (a draw or the previous write-back) if that helps decide.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T07:22:39Z.
I counted which recorded write each of those syncs waits on. For the depth plane stores, the diagnostics tag every pending-write note over a plane: recorder entry, caller, and whether it came from the store's write-back, the
StoreDepthPlanemirror copy, or the publish that endsFlushPending. WhenFlushPendingtakes theSync()branch, it looks at the last note over the plane. Same level, slow mode. I added the tags over five runs, and each run's counts agreed with the earlier ones. The last run tags the publish separately, and its three 5 s windows give:writeBack)StoreDepthPlanecopy into the mirrorPublishShadow(address, bytes, scope, ...)at the end ofFlushPending, noted throughUnitShadow.cpp'sNotePendingWritesSo the chain is store, publish, then the next store of the same plane. Each depth plane store ends by recording the publish of the plane's unit shadows into the import. The next store of that plane finds the publish as the open batch's pending writer and waits in
Sync()(3.4 to 4.7 ms) for the GPU to finish it, then records its own retile and publish behind it. Nothing else wrote the plane in between, and no CPU write needed the bytes. The next store is itself recorded on the GPU after the publish, so the CPU wait doesn't order anything that the batch wouldn't already order.My earlier guess,
NotePendingWriteat the end of the direct write-back path, was wrong: none of the syncs wait on it.That points at the GPU-ordering option rather than range-limited stores: let a depth plane store that will take the GPU-direct path skip the
Sync()when the open writer over its range is only an earlier store or publish of the same plane. I haven't tried it yet. I can prototype that and check it against the depth and storage tests, unless you see a reason the CPU must observe the published bytes first, such as the keep-blocks decision orcompareUntrackedreading guest memory.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T07:36:24Z.
I tried the GPU-ordering idea and it doesn't settle this, so I take back the last paragraph above.
Prototype (diagnostics only,
APS5_DIAG_SKIP_OWN_PUBLISH_SYNC=1): a depth plane store skipsSync()when the open batch's last write over its plane is the previous depth store's publish. Same level, three runs:Sync()calls per 5 sFinishUpTo()calls per 5 sWithout the syncs, the open batch grows until something submits it. The next store then finds the plane's writer in flight and waits in
FinishUpTo(), about 125 to 155 ms per wait, so the waiting is nearly the same. The GPU still moves about 31.9 MiB per store into the host import, around 8 GiB/s. That looks bound by the transfer to host memory rather than by the CPU-side ordering. Skipping the sync only overlaps a part of it.So the bytes are the cost after all. A store limited to the units a read overlaps would cut the transfer, as you proposed, with the per-unit stale state and generation precedence you described. The prototype stays in my local diagnostics.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T07:44:17Z.
This separates the two costs: the skipped
Sync()does not remove the wait, because it moves toFinishUpTo(), while the same full-plane transfer remains. That makes partial write-back the remaining plausible route to the measured win. The implementation still has to preserve per-unit stale state and CPU-write precedence; I would keep the prototype scoped to the requested Z_X units and compare untouched neighboring units against a control before treating it as safe.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T10:03:35Z.
@MarcZX10 the range-limited stores are up as #9026. They follow the requirements you set out here:
On the control you asked for, the new execution test checks the untouched neighbouring units in memory after each partial store. With write watching, a guest write into one unit of a partly stored plane keeps that unit's guest value. The units that are still stale keep the drawn depth, and a LESS draw then passes only over the guest-written unit.
On level 1-1, four alternating pairs: this change ran at 33 to 46 presents per 10 s in every run. Base ran at 2 to 27, and one of its runs hit the slow mode. The table is in the PR.
AI-assisted: yes (Claude Code)