Skip to content

d3d12: render targets laid out linearly go back into title memory (black photos) - #2915

Open
kurrutako wants to merge 2 commits into
ProjectLeonida:mainfrom
kurrutako:fix/d3d12-linear-targets-back
Open

kurrutako wants to merge 2 commits into
ProjectLeonida:mainfrom
kurrutako:fix/d3d12-linear-targets-back

Conversation

@kurrutako

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-LINEAR-TARGETS-BACK (#2570 (comment))

What and why

A title lays a render target out linearly in its memory (layout 1, or 0x100 | the linear swizzle mode) when its CPU reads what is drawn there, for example a photo it encodes to an image file from that memory. On the D3D12 runtime the host texture was the target's only picture: nothing copied it back, so the memory under it stayed zero and such photos were saved black.

This PR:

  • adds resource::linear_target (linear_target_desc for the description): a 2D, single-sample texture with the render-target flag in a linear layout;
  • makes texback::wanted also take such targets, so the existing copy-back path (texback) returns them to title memory when the list closes;
  • marks a bound render target written in draw.rs (list.texture_written), so the copy-back sees draws into it;
  • adds LEONIDA_TARGET_BACK_LINEAR=0 to turn it off (as before).

Targets in the library's own tiled layouts, MSAA targets and textures without the target flag are unchanged.

Tested

Played on Windows 11, RTX 5070 12 GB, NVIDIA 621.01. D3D12 runtime, diag build, Series X profile.

Build Photos taken in game
Before (same base, without this change) Saved black (every file the same small size)
With this change Saved with the picture
  • cargo test -p leonida-gpu --lib: 634 passed (new test resource::tests::only_linear_render_targets_go_back).
  • cargo clippy -p leonida-gpu --all-targets -- -D warnings: nothing in the changed lines (the four lints it reports are in main already: host.rs, pipeline/host.rs, pipeline/known.rs).
  • python -I scripts/check_no_game_data.py on the three files: clean.
  • Not measured: the frame-time cost of the copy-back (it runs only for such targets). Not tried: Series S, other GPUs.

GPUs tested: NVIDIA GeForce RTX 5070 12 GB, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

AI-assisted (Claude Code)

🤖 Generated with Claude Code

A title lays a render target out linearly in its memory when its CPU reads
it, as a photo it encodes to an image file. The host copy was the target's
only picture, so the memory under it stayed zero and such photos were saved
black. Draws into such a target now mark it written, and texback copies it
back when the list closes. LEONIDA_TARGET_BACK_LINEAR=0 turns it off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@merzleckeier

Copy link
Copy Markdown
Contributor

Independent GPU readback of synthetic linear render targets at 8c8ae9b4 + 7fbdf851. I built the same 64×16 RGBA8 fixture on each tree. It registers the render-target state, performs a real D3D12 clear, calls HostList::texture_written, closes and submits the list, and checks CPU backing after synchronous landing (LEONIDA_TEXBACK_ASYNC=0).

With the PR’s default setting, both linear layouts (0x1 and 0x100) returned all 1,024 red RGBA pixels on both Radeon RADV (Mesa 26.3.1) and GeForce RTX 4070 SUPER (620.73.04), using Wine 11.22 / VKD3D-Proton 3.1.0. Main and the PR with LEONIDA_TARGET_BACK_LINEAR=0 both retained zero-filled CPU backing. Layout 0x0 retained zero-filled backing in every run. The existing texture-back test selection passed all eight on main and all nine on the PR, including the new eligibility test.

The fixture covers selection and the production close, copy, and landing path after explicit write registration. MEASURED with synthetic textures. AI-assisted (Codex).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 8c8ae9b4: 634 lib tests pass including the new predicate test, forcing the RENDER_TARGET guard makes it fail, clippy keeps only the four pre-existing warnings, and merge-tree against current main is clean. The hook is right: targets are !fills (resource.rs:1884-1888), so the host copy is the only picture, and close-time registration (draw.rs:3063 → host.rs:8878, :9295) runs after the list's resolves with one back_wanted entry per owner, so there is no double copy.

Two scope issues:

  1. linear_target_desc accepts mip_levels > 1 / depth_or_array_size > 1 (resource.rs:3959), while texture_written marks every subresource (host.rs:9265, texback.rs:164) and non-copy tickets write the whole allocation (texback.rs:496-500). For a mip chain or array, that copies back mips/slices no draw touched, from a host copy that was never uploaded (targets are !fills). Please restrict it to the RTV's subresource, or at least single-mip/single-slice.
  2. A linear target written only by ResolveSubresource is still uncovered. texture_copied is gated on title_written (texture.rs:1290-1294), which is false when the target flag is set, so an MSAA→linear-photo resolve stays black. Is the measured photo path a direct draw? A synthetic resolve fixture would settle it — and would also cover the case where an earlier list's whole-target copy-back lands pre-resolve pixels.

Minor: texback.rs:71 is over rustfmt width.

The in-tree test only checks the predicate plumbing — not wanted(), the toggle, or the copy/land path — so the end-to-end claim still rests on the author's and the independent commenter's runs. No conflict with current main; resource.rs overlaps #2908/#2765/#2658 only outside these hunks.

@merzleckeier

Copy link
Copy Markdown
Contributor

Asynchronous linear target readback with a held writer at 8c8ae9b4 on 60f7dcd9. I cleared a synthetic 64×16 RGBA8 D3D12 render target to red, explicitly registered the write with HostList::texture_written, and submitted through execute_sync_internal. The test held the production texture read-backs worker immediately before the CPU write. On both an RTX 4070 SUPER and Radeon RADV (Wine 11.22 / VKD3D-Proton 3.1.0), submission returned while the worker was held and CPU backing was still zero. After release both linear layouts (0x1 and 0x100) returned all 1,024 red pixels. Synchronous mode returned the same pixels on the submitting thread.

Main and the PR with LEONIDA_TARGET_BACK_LINEAR=0 both retained zero backing for these targets. Nonlinear 0x0 stayed zero in all eight hardware runs. Focused existing tests passed 8/8 on main and 9/9 on the PR per GPU. The later main update 93ffc482 changes the launcher and its test in this merged tree.

This covers explicitly registered single-subresource targets. The multi-subresource and resolve-only cases raised in review were outside this fixture. AI-assisted (Codex).

@merzleckeier

Copy link
Copy Markdown
Contributor

Two linear-target edge cases reproduced on hardware at 8c8ae9b4 on 93ffc482, with synchronous landing. I compared main, the PR with linear readback on, and the same PR with LEONIDA_TARGET_BACK_LINEAR=0 on an RTX 4070 SUPER (620.73.4) and Radeon RADV (26.3.1), using Wine 11.22 / VKD3D-Proton 3.1.0.

  • Resolve-only target. The console ResolveSubresource entry resolved a green 4× MSAA source into a linear single-sample target. An independent host readback found 1,024/1,024 green pixels in each of 24 resolves (both layouts, both GPUs, all three arms, with and without a separate earlier red clear). CPU backing had zero green pixels in all 24. With the PR on and a previously registered red clear, it retained all 1,024 red pixels. Fresh backing and the other controls retained zero-filled pixels.
  • Two mips × two slices. I initialized every CPU subresource yellow and the host subresources blue, cleared only mip 0/slice 0 to red, and explicitly registered the write. With the PR on, the selected 1,024 pixels became red and the untouched subresources’ 256, 1,024, and 256 pixels all changed from CPU yellow to host blue. The main and disabled controls retained all four yellow sentinels. Both layout codes (0x1, 0x100) gave the same result.

MEASURED with synthetic textures. The array fixture explicitly registers the write and the resolve fixture uses the console resolve entry directly. AI-assisted (Codex).

Review of ProjectLeonida#2915: a draw marks every subresource of a target written, and
the host copy of one no draw touched holds nothing the title had, so only
targets of one mip and one slice qualify. A resolve into such a target now
goes back too (texture_copied), as a resolve into a texture the title fills
does. texback.rs line width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kurrutako

Copy link
Copy Markdown
Contributor Author

Thanks @KelboLive, and @merzleckeier for the hardware runs (the two-mip/two-slice one shows exactly the problem in point 1). Pushed 58e8535c:

  1. One subresource only. linear_target_desc now also requires mip_levels == 1 and depth_or_array_size == 1, so a mip chain or an array is never copied back from host subresources no draw touched. The test covers both cases.
  2. Resolve-only targets. resolve (list.rs) now also calls texture_copied(dst, Some(dst_sub)) for a linear target when targets_back() is on, as it already did for textures the title fills. The measured photo path is a direct draw: the draw.rs hook is what made the in-game photos come out. This covers the resolve case too.
  3. texback.rs:71 is wrapped to rustfmt width.

cargo test -p leonida-gpu --lib: 634 passed. clippy reports only the pre-existing warnings. check_no_game_data.py on the changed files is clean, and the branch merges cleanly with current main. Not re-run in game after this push. The resolve change is covered only by the reasoning above, not by a fixture of mine.

AI-assisted (Claude Code)

@merzleckeier

Copy link
Copy Markdown
Contributor

Hardware recheck of the single-subresource and resolve update at 58e8535c on 99a29d10. Compared main, prior head 8c8ae9b4, and the updated head on RTX 4070 SUPER and Radeon RADV (Wine 11.22 / VKD3D-Proton 3.1.0).

  • Resolve-only: the console call resolved a green 4× MSAA source. With both readback switches enabled, the new head returned all 1,024 green pixels to CPU memory in every layout/preload case on both GPUs (8 synchronous resolves). Main and the prior head retained old CPU pixels. Separately disabling either LEONIDA_TARGET_BACK_LINEAR or LEONIDA_COPY_BACK still left the GPU resolve intact and suppressed CPU landing.
  • Subresource coverage: after an explicitly registered clear of mip 0/slice 0, the new head preserved every CPU sentinel for each of the 2-mip, 2-slice, and 2-mip × 2-slice descriptors in both linear layouts. The prior head overwrote CPU pixels in all three shapes.
  • Async: an exact-filter test on each GPU held the real CPU writer before landing. In all 8 cases the GPU buffer had all 1,024 green pixels while CPU backing still held the earlier zeros or red pixels. Each landed all 1,024 green pixels after release.

All 10 synchronous matrix runs passed both diagnostics. Existing focused tests passed 8 on main and 9 on each PR head per GPU. MEASURED with synthetic textures and an instrumented async writer. AI-assisted (Codex).

@EdenBendheim

Copy link
Copy Markdown
Contributor

Independent native Windows 11 + RTX 3090 verification (VERIFY-2915-RTX3090, claimed in #2570): PASS

  • Tested: PR head 58e8535cf merged cleanly onto main 913b88b90 (diagnostics,mimalloc, exe_sha16: 1ebc71ceabfb0417).
  • Hardware / OS: Windows 11, AMD Ryzen 9 5900X, 32 GiB RAM, NVIDIA GeForce RTX 3090 24 GiB (driver 610.88).
  • Clean-room check: python -I scripts/check_no_game_data.py passed on all changed files.
  • Unit red/green (cargo test -p leonida-gpu --locked -- only_linear_render_targets_go_back --test-threads=1):
    • Baseline 913b88b90 with PR test overlaid: compile failure (linear_target_desc missing).
    • Merged PR (default and --features diagnostics): 1 passed, 0 failed in both configurations.
  • Retail 300 s in-game dynamic-stress gate (scripts/game-gate.ps1, Series X 1080–1440p, -NoFrameGen, upgraded 8-leg sprint/wide-turn/jump gate_driver + LEONIDA_GAME_ACTIONS_RUN="150:time 23"):
    • Verdict: PASS (reasons=[], warnings=[], exit=clean, stop=target, cache_saved=true, GATE CONTROL fired with moved=2.54 m, max from_control_m=9.2 m, time 23 confirmed PASS).
    • Metrics (world_s=304.2s, world+60s, 4,808 presents, 6,537 median draws): 20.03 FPS median, 15.54 FPS p5, 64.37 ms p95 frame time, 0.430 s longest present gap, 0 missed pipeline waits, 31,218 MB max private memory, 250 MB min commit left, 12,033 MB max VRAM (0 s over budget).
  • Scope note: This confirms stability and zero regressions over 300 s of live night/sprint gameplay (and descriptor eligibility in unit tests); taking an actual in-game camera photo wasn't scripted in the automated run.

AI-assisted: yes (Gemini 4 Neon orchestrator; Codex GPT-6.2 Astra build, unit tests, and serialized RTX 3090 gate execution).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants