perf(agc): reuse image-table elements whose descriptors repeat #6223

Closed
Thunderhawkk wants to merge 3 commits from upstream-pr/6223 into main
Thunderhawkk commented 2026-10-15 05:04:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Thunderhawkk on 2026-10-15T05:04:34Z.


What

An image-table build resolves every element through the texture cache even when one record of the table changed, or when the same descriptors appear in another table of the same submission. This reuses the resolved texture of an element whose descriptor repeats, inside the safety envelope the whole-table reuse already uses.

  • Every element is pushed with a per-element clean flag and its surface generation, so a table with one non-clean element is still remembered (today one bad element discards the whole table).
  • The whole-table fast path stays, gated on a fullyClean flag set when the table was remembered.
  • A donor lookup by TextureKey over the clean elements of the tables remembered in the current state: an element reuses a donor while its texture is still the cache's entry for the key, its surface is unchanged at the donor's generation and no pending work overlaps it; anything else resolves fresh.
  • The state gate is unchanged (epoch, unwatch serial, allocations, depth surfaces), so donors only come from the same ordering window the whole-table path already trusted.
  • New counters (elementsReused, elementsResolved) and the [textures] report line carries them.

Cross-submission (cross-epoch) reuse is not in this change. Making donors cross epochs safely needs the write watch on every element range; I proposed that direction on #5961 and can measure it if the maintainers want it.

Tested

Windows 11 (26200), RTX 5070 Ti 16 GiB, NVIDIA 617.42, MinGW GCC 15.2.0:

  • agc_driver_image_table_reuse_tests passes. The "two tables that differ in one record" case now also asserts element counters: 47 of 48 elements reused per-element, 143 reused / 49 resolved over the case. The table-level expectations are unchanged (whole-table reuses count as reused, per-element builds as resolved).
  • agc_driver_graphics_tests passes.
  • Marvel's Wolverine PPSA03671 01.001.005 converted with --windows, APS5_PROFILE_DRAW=1, the same 421,247-word compute dispatch (program 0x11d0323c00) on the same machine:
main:          [dispatch] resources took 10483 ms (421247 words, program 0x11d0323c00)
this PR:       [dispatch] resources took  1042 ms (421247 words, program 0x11d0323c00)
  • With local instrumentation, 79.8% of the table elements resolved in one run repeated within one collect epoch (1,278,265 of 1,601,915); those repeats are what this PR reuses. The 844k-1M-word dispatch family, whose descriptors do not repeat, is unaffected.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA03671 #5516
  • GPUs tested: NVIDIA RTX 5070 Ti 16 GiB, driver 617.42, Windows 11 26200 (the execution test ran on this GPU)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4868484337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6223). Originally posted by [Thunderhawkk](https://github.com/Thunderhawkk) on 2026-10-15T05:04:34Z. --- ### What An image-table build resolves every element through the texture cache even when one record of the table changed, or when the same descriptors appear in another table of the same submission. This reuses the resolved texture of an element whose descriptor repeats, inside the safety envelope the whole-table reuse already uses. - Every element is pushed with a per-element `clean` flag and its surface generation, so a table with one non-clean element is still remembered (today one bad element discards the whole table). - The whole-table fast path stays, gated on a `fullyClean` flag set when the table was remembered. - A donor lookup by `TextureKey` over the clean elements of the tables remembered in the current state: an element reuses a donor while its texture is still the cache's entry for the key, its surface is unchanged at the donor's generation and no pending work overlaps it; anything else resolves fresh. - The state gate is unchanged (epoch, unwatch serial, allocations, depth surfaces), so donors only come from the same ordering window the whole-table path already trusted. - New counters (`elementsReused`, `elementsResolved`) and the `[textures]` report line carries them. Cross-submission (cross-epoch) reuse is not in this change. Making donors cross epochs safely needs the write watch on every element range; I proposed that direction on #5961 and can measure it if the maintainers want it. ### Tested Windows 11 (26200), RTX 5070 Ti 16 GiB, NVIDIA 617.42, MinGW GCC 15.2.0: - `agc_driver_image_table_reuse_tests` passes. The "two tables that differ in one record" case now also asserts element counters: 47 of 48 elements reused per-element, 143 reused / 49 resolved over the case. The table-level expectations are unchanged (whole-table reuses count as `reused`, per-element builds as `resolved`). - `agc_driver_graphics_tests` passes. - Marvel's Wolverine PPSA03671 01.001.005 converted with `--windows`, `APS5_PROFILE_DRAW=1`, the same 421,247-word compute dispatch (program `0x11d0323c00`) on the same machine: ``` main: [dispatch] resources took 10483 ms (421247 words, program 0x11d0323c00) this PR: [dispatch] resources took 1042 ms (421247 words, program 0x11d0323c00) ``` - With local instrumentation, 79.8% of the table elements resolved in one run repeated within one collect epoch (1,278,265 of 1,601,915); those repeats are what this PR reuses. The 844k-1M-word dispatch family, whose descriptors do not repeat, is unaffected. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA03671 #5516 - [x] GPUs tested: NVIDIA RTX 5070 Ti 16 GiB, driver 617.42, Windows 11 26200 (the execution test ran on this GPU) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 05:04:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T05:04:51Z.


Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6184850062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6223#issuecomment-6184850062). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T05:04:51Z. --- Thank you for your contribution! Authors with no pull requests merged into `main` can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.
oneandonlydean commented 2026-10-17 23:57:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T23:57:57Z.


Thanks for this: per-element reuse is a real gap (main still only reuses whole tables), and the Wolverine numbers are a strong motivation. The branch is about 1,400 commits behind main and conflicts in ShaderResources.cpp and the debt file, so it needs a rebase. A few points to cover in it.

1. Filter donors by state and device explicitly. At your head the donors are same-state only because FindReusedTable runs just before the donor index is built and erases every remembered table whose TableState differs. The donor loop itself doesn't check it, even though the comment says "the remembered tables of this state". On current main, resolveImageTables skips FindReusedTable for tables the shader stores to (storageTable ? nullptr : FindReusedTable(...), from the image-store-through-tables change). After a straight rebase, a storage-table build would therefore index donors from a list that was never pruned to the current state, and could reuse textures from an older epoch, which your own debt entry says isn't covered. Please check remembered.state == state and remembered.device == context.device in the donor loop.

2. No donors from storage elements. Elements with imageWritten set go through the storage-table path on main. They must never be recorded as clean donors, and a table that has one should not be fullyClean.

3. Negative tests for the donor path. The added assertion counts elements in an existing scenario (143 reused, 49 resolved), but none of the donor path's three guards is tested. Please add cases where an element repeats in a different table after its donor resolved, and:

  • the donor's surface is written by the CPU → the new table samples the new texels;
  • a pending image store overlaps the donor's surface → the new table samples the stored texels;
  • the texture cache is cleared or the entry evicted → the element resolves fresh.

Each should fail with its guard removed (UnchangedSince at the donor generation, AnyPendingOverlaps, the cache-entry identity check).

4. Re-measure on the rebased head. ShaderResources.cpp has changed about 30 times on main since your base (descriptor-cache publication, DCC key presync, texture-cache reclaim, storage budget). Please repeat the Wolverine (PPSA03671) resource-build measurement on the rebased head.

5. The remembered-table LRU. Tables that aren't fully clean are now remembered too, so they take slots in the 4-entry ReusedTables list and can evict fully clean tables, which lowers whole-table reuse. Please report the [textures] image tables N reused, M resolved counts before and after on a scene that already reused whole tables, to show whole-table reuse doesn't drop. If it does, a separate donor list, or remembering only tables with at least one clean element, may help.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243432375 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6223#issuecomment-6243432375). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T23:57:57Z. --- Thanks for this: per-element reuse is a real gap (`main` still only reuses whole tables), and the Wolverine numbers are a strong motivation. The branch is about 1,400 commits behind `main` and conflicts in `ShaderResources.cpp` and the debt file, so it needs a rebase. A few points to cover in it. **1. Filter donors by state and device explicitly.** At your head the donors are same-state only because `FindReusedTable` runs just before the donor index is built and erases every remembered table whose `TableState` differs. The donor loop itself doesn't check it, even though the comment says "the remembered tables of this state". On current `main`, `resolveImageTables` skips `FindReusedTable` for tables the shader stores to (`storageTable ? nullptr : FindReusedTable(...)`, from the image-store-through-tables change). After a straight rebase, a storage-table build would therefore index donors from a list that was never pruned to the current state, and could reuse textures from an older epoch, which your own debt entry says isn't covered. Please check `remembered.state == state` and `remembered.device == context.device` in the donor loop. **2. No donors from storage elements.** Elements with `imageWritten` set go through the storage-table path on `main`. They must never be recorded as clean donors, and a table that has one should not be `fullyClean`. **3. Negative tests for the donor path.** The added assertion counts elements in an existing scenario (143 reused, 49 resolved), but none of the donor path's three guards is tested. Please add cases where an element repeats in a *different* table after its donor resolved, and: - the donor's surface is written by the CPU → the new table samples the new texels; - a pending image store overlaps the donor's surface → the new table samples the stored texels; - the texture cache is cleared or the entry evicted → the element resolves fresh. Each should fail with its guard removed (`UnchangedSince` at the donor generation, `AnyPendingOverlaps`, the cache-entry identity check). **4. Re-measure on the rebased head.** `ShaderResources.cpp` has changed about 30 times on `main` since your base (descriptor-cache publication, DCC key presync, texture-cache reclaim, storage budget). Please repeat the Wolverine (PPSA03671) resource-build measurement on the rebased head. **5. The remembered-table LRU.** Tables that aren't fully clean are now remembered too, so they take slots in the 4-entry `ReusedTables` list and can evict fully clean tables, which lowers whole-table reuse. Please report the `[textures] image tables N reused, M resolved` counts before and after on a scene that already reused whole tables, to show whole-table reuse doesn't drop. If it does, a separate donor list, or remembering only tables with at least one clean element, may help.
CnRJay commented 2026-11-01 19:52:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:52:32Z.

Review: COMMENTED


Points on 6223:

  1. Comments in code. ShaderResources.cpp gains several comment blocks ("Elements a table build bound without a fresh lookup...", "Elements of the remembered tables of this state, by key..."). The rule is no comments except #endif and namespace ends.

  2. Per-build donor scan. Each table build constructs a fresh donors unordered_map by walking ReusedTables() and every entry in every remembered table, whenever state.epoch != 0. That is work proportional to all remembered elements, paid on every build, to save lookups on a hit. What bounds ReusedTables() and how many elements does it hold in the scene you measured? If a title builds many tables per frame this scan could cost more than the lookups it saves.

  3. Speed claim. The PR is perf; the counters (tableElementsReused / tableElementsResolved) show hit rate, not time. Please attach tools/trace_digest.py summaries of main and the change from the same machine and scene.

  4. Negative control. The UnchangedSince guard is the only thing preventing a stale donor from being reused when guest memory under a repeating descriptor changes. Is there a test that rewrites the backing bytes and fails when that guard is removed?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729932262 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6223#pullrequestreview-5729932262). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:52:32Z. **Review:** COMMENTED --- Points on 6223: 1. **Comments in code.** `ShaderResources.cpp` gains several comment blocks ("Elements a table build bound without a fresh lookup...", "Elements of the remembered tables of this state, by key..."). The rule is no comments except `#endif` and namespace ends. 2. **Per-build donor scan.** Each table build constructs a fresh `donors` `unordered_map` by walking `ReusedTables()` and every `entry` in every remembered table, whenever `state.epoch != 0`. That is work proportional to all remembered elements, paid on every build, to save lookups on a hit. What bounds `ReusedTables()` and how many elements does it hold in the scene you measured? If a title builds many tables per frame this scan could cost more than the lookups it saves. 3. **Speed claim.** The PR is `perf`; the counters (`tableElementsReused` / `tableElementsResolved`) show hit rate, not time. Please attach `tools/trace_digest.py` summaries of main and the change from the same machine and scene. 4. **Negative control.** The `UnchangedSince` guard is the only thing preventing a stale donor from being reused when guest memory under a repeating descriptor changes. Is there a test that rewrites the backing bytes and fails when that guard is removed?

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6223
No description provided.