Skip to content

Runtime: WriteToSubresource and ReadFromSubresource answer their arguments as the host does - #2736

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
mrp1no:d3d-subresource-host
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
mrp1no:d3d-subresource-host

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

New D3D item, claimed in #2516 (claim). No dependencies. It touches cpu_copies in d3d12_vulkan_device_test.cpp in one spot (four expectations), away from the lines #2462/#2242/#2297 change.

What was wrong

WriteToSubresource / ReadFromSubresource were only checked against hand-written expectations, and two of those didn't match what D3D12 does:

  • Empty or inverted boxes. The host refuses a box only when its right, bottom or back edge lies past the mip's, or when it cuts a compressed block. An empty box (left == right, top == bottom or front == back), or an inverted one that passes those checks, is S_OK and copies nothing. That includes one that starts past the right edge and ends inside. A read leaves the destination untouched. The layer answered E_INVALIDARG.
  • Pitches. The host doesn't check them. Rows or slices that overlap (a row pitch smaller than a row, a depth pitch smaller than a slice, or 0) are copied in order. The layer refused them.

The change

  • region() checks the far edges, then block alignment, and then treats an empty box as no copy. cpu_copy returns S_OK for it and no longer checks the pitches. The order is the host's: an empty box past the edge, or one that cuts a BC block, is still E_INVALIDARG.
  • New d3d12_subresource_copy_test runs the same 117 steps on the Vulkan-backed device and on the host runtime, once per copy path (host image copy where the GPU has it, and the staging submit). The resources: textures in write-back and write-combine custom heaps, a volume, a 2-layer array, BC1 blocks, a buffer, an upload-heap buffer, a default-heap texture and a 4-sample one. The steps: whole and boxed copies, boxes outside, empty, inverted or cutting a block, subresources past the last, pitches too small or 0, and copies before Map. Every HRESULT is recorded, and every read records a hash of its whole 4 KiB destination, so a byte written outside the region counts too. Reads right after the empty and inverted boxes check that nothing was written.
  • Not compared, and printed as such:
    • null data: the host faults; the layer still answers E_INVALIDARG.
    • which bytes win when a read's rows overlap: that's the order the driver copies texels in, tiled on NVIDIA. The HRESULT is still compared.
    • a default-heap texture: the host refuses it. The layer copies it on purpose and logs it, as docs/d3d12_vulkan.md already says.
  • cpu_copies expected the old refusals for an empty box and overlapping slices; it now expects S_OK. docs/d3d12_vulkan.md lists the rules.

Tested

Windows 11 Pro 26300, MSVC 19.52 (VS 18 Build Tools, Ninja, RelWithDebInfo), NVIDIA GeForce RTX 3060, driver 610.88, Vulkan headers/loader 1.4.357:

  • d3d12_subresource_copy_test with LEONIDA_VK_VALIDATION=1: on each path, 109 steps compared, 8 not compared, 0 mismatches, validation clean.
  • Negative control: the same test against main's layer gives 17 mismatches on the first path.
  • d3d12_vulkan_device_test with validation: pass (both cpu_copies paths).
  • Full ctest: 125/125 passed (glslang and the validation layer installed; LEONIDA_GAME_DIR set, so the package-gated tests ran too).

No game files involved. AI-assisted (Claude Code).

🤖 Generated with Claude Code

…ime does

d3d12_subresource_copy_test runs the same 117 steps on the Vulkan-backed device
and on the host runtime: textures in write-back and write-combine custom heaps,
a volume, an array, BC1 blocks, a buffer, an upload-heap buffer, a default-heap
texture and a multisampled one; whole and boxed copies, boxes outside, empty,
inverted or cutting a block, subresources past the last, pitches too small or
0, and copies before Map. It records every HRESULT and a hash of the whole
destination of every read, once per copy path (host image copy and staging).
On main 17 steps differed (RTX 3060, driver 610.88); now none do. The host's
rules:

- a box is refused only when its right, bottom or back edge lies past the
  mip's, or it cuts a compressed block; an empty or inverted box that passes
  those checks is S_OK and copies nothing (the layer refused it);
- the row and depth pitches are not checked: rows or slices that overlap are
  copied in order (the layer refused a pitch smaller than a row or slice).

Not compared, and reported as such: null data (the host faults; the layer
still answers E_INVALIDARG), which bytes win when a read's rows overlap (the
driver's copy order, tiled on NVIDIA), and a default-heap texture, which the
host refuses and the layer copies on purpose, as docs/d3d12_vulkan.md says.

d3d12_vulkan_device_test's cpu_copies expected the old refusals for an empty
box and overlapping slices, and now expects S_OK.

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

Copy link
Copy Markdown

Not executable on this machine (Bazzite 45 / Linux 7.3.4, GCC 16.2.1): this is the Windows D3D12 layer. runtime/shims/d3d12_com.h includes <windows.h> and <d3d12.h>, and the new target sits inside if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) (the option defaults to ${WIN32}). In a fresh build directory of my worktree, cmake --build build-linux --target d3d12_subresource_copy_test answers:

ninja: error: unknown target 'd3d12_subresource_copy_test'

and the shared Linux binaries cannot call WriteToSubresource / ReadFromSubresource at all. So the 117-step host-vs-layer comparison and the 17-mismatch negative control are not re-run here — they need a Windows machine. There are no other comments on this PR, so this is a review-only result, not a duplicate.

Code review of the change (base 49475796):

  • region() checks the far edges (right/bottom/back > width/height/depth) before the block alignment, then turns an empty or inverted box into rows = row_bytes = slices = 0 and returns true. That is the host order the description states: an empty box past the edge, or one cutting a BC block, is still E_INVALIDARG.
  • cpu_copy() keeps null data at E_INVALIDARG, returns S_OK for the empty region before the CPU-visible-heap check, and no longer validates the pitches, so overlapping rows/slices are copied in order as on the host.
  • cpu_copies() in d3d12_vulkan_device_test.cpp is updated consistently (empty box → S_OK, volume depth pitch 32 for 64-byte slices → S_OK); the texture row-pitch case it drops is covered by the new dedicated test.
  • docs/d3d12_vulkan.md lists the same rules and the three deliberately not-compared cases (null data, overlapping-read bytes, default-heap texture).

What the review cannot establish: the actual HRESULTs and the whole-destination hashes of the 117 steps, across BC1, write-back/write-combine heaps, the volume, the array, the multisample texture and the default-heap case; nor the validation-layer result. That is exactly what the description says was run on an RTX 3060, and I cannot confirm it here.

No defect found in the review.

Verdict: LGTM (review only; no execution possible on this machine).

AI-assisted review.

@rafaelpssz

Copy link
Copy Markdown
Contributor

Tested c226964 on Windows 11, RTX 3060 (driver 616.64), Vulkan SDK 1.4.363.1, built with clang-cl + xwin (RelWithDebInfo).

  • d3d12_subresource_copy_test: OK. For both copy paths, host image copy and staging, the result is 109 steps compared with the host (8 more not compared), 0 mismatches. The 8 not compared are the null-data steps (the host faults) and the default-heap copies (the layer copies on purpose), as described.
  • d3d12_vulkan_device_test: exit 0, with all of its sections matching, including the four changed cpu_copies expectations.
  • Red check: I put back runtime/shims/d3d12_vulkan_device.cpp from the parent commit and rebuilt only the new test. The host-image-copy path then reports 17 mismatches of the 109 and the test fails, so the new comparison catches the old empty-box and pitch answers.

Thanks for the reviews on #2214 and #2215. Glad to return the favour.

@bielfreedom

Copy link
Copy Markdown

Tested c226964e (on its base 49475796) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010, Windows 11 Pro build 26200, MSVC build of runtime/, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass.

  • d3d12_subresource_copy_test: both copy paths match the host runtime on this GPU:
    • CPU copies (host image copy): 109 steps compared with the host (8 more not compared), 0 mismatches
    • CPU copies (staging copy): 109 steps compared with the host (8 more not compared), 0 mismatches
    • The 8 uncompared steps are the ones the test marks as such: null data (the host faults) and the default-heap reads and writes the layer copies on purpose.
  • d3d12_vulkan_device passes (the changed cpu_copies expectations).
  • Full ctest: 116/116 pass, 0 failures, 0 validation errors.

AI-assisted (Claude Code). No game files.

@grahamster2

grahamster2 commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested c226964 merged onto main ec8ec4c (clean merge) on Windows 11 26200, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo, on both adapters of one PC: pass.

LEONIDA_VK_DEVICE d3d12_subresource_copy d3d12_vulkan_device
NVIDIA RTX 3080, 610.88 OK. Host image copy and staging copy: 109 steps compared with the host each (8 more not compared), 0 mismatches pass
AMD Radeon iGPU (Ryzen 5 7600X, gfx1036, RDNA2), AMD 24.30.66 OK. Only the staging-copy path runs on this driver: 109 steps compared with the host (8 more not compared), 0 mismatches one failure, line 4351: command_queue(host.p, rt_desc) == S_OK. main has it on this AMD host in an elevated shell too (#2422), and nothing else fails

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 125/125 on the RTX 4080 SUPER. On the Intel UHD 770 only the known d3d12_descriptor (min-LOD) and d3d12_subgroup (fragment probe) fail (#2891).

@XFlawless1

Copy link
Copy Markdown
Contributor

Tested c226964e merged onto main (ec8ec4c9): pass on both GPUs.

Windows 11 Pro 26300, MSVC 19.44 (VS 2022 Build Tools), Ninja RelWithDebInfo via scripts/build_runtime.ps1, Vulkan SDK 1.4.363.0. Full ctest -j1 once per GPU with LEONIDA_VK_DEVICE: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2 0x13C0, driver 32.0.21042.62) and NVIDIA RTX 5080 (621.14). AI-assisted (Claude Code). No game files.

  • Radeon: 123/123. RTX 5080: 123/123. The new d3d12_subresource_copy passes on both (0.65 s and 0.48 s).
  • main alone at ec8ec4c9: 122/122 pass on both GPUs. The only tests not run are those that need game files or the validation layer (shader_library_corpus, bink_*, fsr_dispatch, bvh_builder, vk_sync_validation, d3d12_first_light), plus d3d12_rgb9e5_rtv on the RTX 5080. All of these are skipped on main too.

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.

8 participants