Skip to content

D3D12.X: read title texture generations without the TEXTURES lock - #3580

Draft
raylitebull wants to merge 2 commits into
mainfrom
perf/texture-generations
Draft

raylitebull wants to merge 2 commits into
mainfrom
perf/texture-generations

Conversation

@raylitebull

@raylitebull raylitebull commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Stacked on #3576; only the top commit (D3D12.X: read title texture generations without the TEXTURES lock) belongs here.

capture_transitions runs for every barrier recorded on a list while textures are kept in step, and capture_gpu_write for every copy and clear into one of them. Each took the TEXTURES mutex to read one registration generation. Submits hold that lock while they choose which textures to upload, so recording threads waited behind them. Now the generations are mirrored in a map that both read under a shared lock.

What is in it

  • title_textures.rs: RES_GENERATIONS, the generation of every active or shadowed entry of TEXTURES. Table writes it under the TEXTURES lock where an entry is placed and where it leaves (release, retirement). capture_transitions and capture_gpu_write read it. Tables made by tests leave it alone (Table::global). In debug builds, update_lengths checks the map against the table.
  • Not ported from amd-dev: the SHADOWED_IDS mirror for uncover_drawn, which main does not have.
  • Test: the_generation_mirror_follows_the_table. The existing transitions_apply_when_their_list_is_submitted covers capture_transitions through the mirror.

Tested

  • No Windows machine or GPU here: a Linux x86_64 cloud machine (Ubuntu 24.04), rustc 1.99.0, debug build.
  • cargo check --workspace --all-targets --target x86_64-pc-windows-gnu: no errors, no new warnings.
  • The lib unit tests of the touched modules, built for x86_64-pc-windows-gnu from this branch (both commits) and run under Wine 9.0 (no GPU) with --test-threads=4, next to the same filters on main b2b1f109:
    • hle::d3d12x::title_textures: 66 passed, 1 failed, 1 ignored (main: 64 / 1 / 1)
    • hle::d3d12x::list_x: 110 passed, 4 failed (main: 110 / 4)
      The failures are the same tests on both, ones that need a D3D12 device or Windows' working-set queries, which Wine lacks.

Regression run
Not run yet: there was no Windows machine or title here. scripts/smoke.ps1 still has to run on a title, built from main and from this head, so PR checks fails until those two blocks are added.

Not tested

  • The Windows (MSVC) build and the whole suite (cargo test --workspace -- --test-threads=4) on Windows, with or without a GPU; CI's no-GPU job is the first Windows run.
  • Any title or performance on main. On amd-dev this was part of 4d6d4a77, measured together with its other lock changes there (draw recording 168.1 -> 47.6 us avg on an RX 9070 XT).

AI use

  • Capy with Claude Opus 5.5 ported raylitebull's amd-dev commit 4d6d4a77 to main, split it into this PR and five others, and wrote the test, the commit message and this description. The amd-dev measurements are raylitebull's own runs.

Open in Capy

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
update_lengths, called under the TEXTURES lock on every placement,
release, retirement and uncover, counted the CPU-backed entries by walking
all of by_res. A retail title keeps tens of thousands of entries there
(55k+ in the amd-dev measurement), so every texture create and release
paid for a full scan while holding the lock that submits and recording
also take.

Table now keeps cpu_backed_count up to date where by_res changes
(insert_active / remove_active), and update_lengths stores it.

Ported from amd-dev 4d6d4a7.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 04737e3, the top commit read; draft, stacked on #3576):

  • The gain isn't measured for this change. The 168 -> 47.6 µs figure covers the whole amd-dev commit 4d6d4a7, on amd-dev, not this commit on main.
  • A new cost inside TEXTURES. Every insert / remove / retire_range now also takes a global write lock and a hash insert while TEXTURES is held, and can wait on readers of the generations. During texture-streaming bursts that could cost more than it saves (reasoned, not measured).
  • Duplicated state. The generation map has to follow by_res / shadowed. Today all their changes go through these three functions, but a future change outside them would silently lose transitions. A debug_assert comparing the two in update_lengths, as suggested on D3D12.X: count CPU-backed title textures as they come and go #3576, would catch it.

Fine: it's an RwLock, not hand-rolled atomics, so there are no ordering issues or torn reads, and only a u64 is copied out. Writes happen inside the TEXTURES critical section (title_textures.rs ~226, ~431). Reuse is safe: generations strictly increase (~422), and publish rechecks them under TEXTURES (~1001, ~1021). I found no other writer of by_res / shadowed in the crate. It's always on, with no switch. #801 and #2437 edit title_textures.rs too.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

capture_transitions (every barrier recorded on a list while textures are
kept in step) and capture_gpu_write (every copy and clear into one) took
the TEXTURES mutex to read one registration generation each. Submits hold
that lock while they choose the textures to upload, so recording threads
waited behind them.

RES_GENERATIONS mirrors the generation of every active or shadowed entry
of TEXTURES. Table writes it under the TEXTURES lock where an entry is
placed and where it leaves (release, retirement), and the two capture
paths read it under a shared lock. Tables made by tests leave it alone.

Ported from amd-dev 4d6d4a7, which measured this together with its other
lock changes on amd-dev (draw recording 168 -> 48 us avg on an RX 9070
XT); this part was not measured on its own or on main.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@raylitebull raylitebull added the gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) label Dec 1, 2026
@raylitebull
raylitebull force-pushed the perf/texture-generations branch from 04737e3 to c36eedf Compare December 1, 2026 14:57
@github-actions github-actions Bot added needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up (head c36eedf; the diff from 04737e3 read). The duplicated state is now checked: in debug builds update_lengths asserts that RES_GENERATIONS holds exactly the entries of by_res and shadowed with the same generations, along with #3576's CPU-backed count. It reads RES_GENERATIONS while TEXTURES is held, the same order the writers use, so it adds no lock-order risk. The full scan runs on every update in debug builds only, so it slows the debug test suite a little but not release. Still open from my review: the cost of the write lock taken inside TEXTURES on every insert / remove, and a measurement on main.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@github-actions github-actions Bot removed the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@capy-ai

capy-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@Foniass thanks, the check is in (c36eedf), as you confirmed. On the write lock inside TEXTURES: it's taken once per placement, release and retirement. Those already hold TEXTURES and do more work there (by_va, src_pieces, coarse), and readers hold the read lock only for a few hash lookups. I agree only a measurement settles it, and the smoke runs will include one.

AI use: written by Capy (Claude Opus 5.5) and posted on raylitebull's behalf, with their go-ahead.

@Foniass

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Test report (not a merge-check: draft, PR description red)
base: b2b1f10
head: c36eedf
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2904 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: not repeated here; my review of this same head is at #3580 (comment), and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run: a lock change whose cost a 60 s run capped at 60 presents per second can't show.
assessment: merges, builds and passes the whole suite on current main, including its new consistency checks in the debug-build tests (stacked on #3576, which this run includes); still open from my review: the write lock inside TEXTURES on every insert / remove, and a measurement on main
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants