Skip to content

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

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

kurrutako wants to merge 1 commit 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

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.

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.

3 participants