Skip to content

Runtime: WriteToSubresource and ReadFromSubresource on the Vulkan device - #288

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Parakeet-Wheat:d3d-subresource-cpu-copy
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Parakeet-Wheat:d3d-subresource-cpu-copy

Conversation

@Parakeet-Wheat

@Parakeet-Wheat Parakeet-Wheat commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

New item claimed in #156: ID3D12Resource::WriteToSubresource / ReadFromSubresource on the Vulkan device (docs/d3d12_vulkan.md §4.2; WriteToSubresource is in the first-frame API set, §6.1).

Depends on: #391
Depends on: #459
Depends on: #497

(None of these is needed for this PR to work. Each one conflicts with it in git. I checked with git merge-tree against every open PR that touches the same files, counting only conflicts this branch adds on top of what the PR already has with main. The conflicts are in d3d12_vulkan_device.cpp, d3d12_com.h, vk_test_device.h and d3d12_vulkan_device_test.cpp, and they're mechanical: a new VulkanDeviceInfo field, a new optional extension in device creation, new members in Resource and DeviceState. About twenty other open PRs on these files already conflict with main after the last merge round, so I'll re-check them after they rebase. I'll rebase onto whichever lands first.)

What and why

Both methods were generated stubs that returned E_NOTIMPL. D3D12 completes them on the CPU before returning, and leaves it to the application to keep the GPU off the subresource meanwhile. So a blocking copy is the whole contract. The design doc said "a staging copy at the next submit", which works for writes but not for reads, because a read must return the data at once. §4.2 is corrected.

  • Map (d3d12_vulkan_device.cpp): a texture in a CPU-visible custom heap maps with a null pointer, as D3D12 allows before these calls. A non-null pointer, or a default-heap texture, is still E_INVALIDARG.
  • Host path: when the device has VK_EXT_host_image_copy (VulkanDeviceInfo::host_image_copy), textures in CPU-visible custom heaps are created with VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT if the format supports it. At creation they're moved to GENERAL and zero-filled on the host, so a CPU write before any command list isn't overwritten by a first use's zero fill. While a subresource is in GENERAL (D3D12's COMMON), the calls copy with vkCopyMemoryToImageEXT/vkCopyImageToMemoryEXT. The device checks that GENERAL is in the device's host copy layouts instead of assuming it.
  • Staging path: used for everything else (no extension, a subresource a barrier has moved, a texture the format excludes). The call records a CopyTextureRegion from or to a host-visible staging buffer at D3D12's footprint pitch, replays it on the device's queue with the existing VulkanQueue, and waits. The layout tracker, layout transitions and first-use zero fill therefore behave exactly as for the application's own copies, with no new replay code.
  • Multisampled textures (on main since Runtime: ResolveSubresource and multisampled textures on Vulkan #228): neither path accepts them. D3D12 has no CPU copies of multisampled textures, and Vulkan can't do host or buffer copies of them. Host transfer usage is only added to single-sample textures.
  • Texture creation builds its VkImageCreateInfo with texture_create_info from Runtime: GetResourceAllocationInfo on the Vulkan device (D3D-23) #327 (GetResourceAllocationInfo) and adds the host transfer usage on top, so both still describe the same image. Host-copy textures start in GENERAL, which is also the layout Runtime: texture views and samplers go into the bindless set (M2, the device's part) #328's bindless set expects for their views.
  • Checks: both paths reject what D3D12 rejects: an unknown subresource, a box outside the mip or empty, a box that cuts a compressed block except at the mip edge, null data, and pitches whose rows or slices would overlap. The application's pitches can be anything else; data is repacked to and from them.
  • Default heaps: D3D12 doesn't allow these calls on a default-heap texture. The layer still does the copy (staged) and logs it once, rather than fail, because the console allows more than the PC runtime does and a silent failure would be worse.
  • Devices: the standalone device and vk_test_device.h enable the extension and its hostImageCopy feature when the GPU has them. Since Runtime: SetPredication on VK_EXT_conditional_rendering (D3D-18) #258 (SetPredication) is on main, both may enable VK_EXT_conditional_rendering too: the device extensions are one list, and the host copy features are chained in front of the conditional rendering features instead of replacing them.

Tested

Windows 11, NVIDIA GeForce RTX 3070 (driver 610.88, VK_EXT_host_image_copy present), Vulkan SDK 1.4.363.0, MSVC 14.44 (VS 2022), rebased on 91b36cf. No game files.

  • cmake --build runtime/build --config RelWithDebInfo: builds with no warnings.
  • ctest -C RelWithDebInfo: 23/23.
  • d3d12_vulkan_device_test with LEONIDA_VK_VALIDATION=1: passes with no validation errors. The new checks run twice, once on a device with host image copy and once on a second D3D12 device on the same Vulkan device with it turned off, so one GPU covers both paths. Each run covers:
    • a new texture reads back zero-filled;
    • a 5x4 box written from 100-byte rows and read back whole at a 40-byte pitch;
    • mip 1 written whole and read back as a box;
    • three slices of a 3D texture with separate row and slice pitches;
    • BC1, written whole and then one block replaced through a box;
    • a default-heap texture;
    • the rejected calls;
    • GPU copies in both directions: a GPU copy reads what the CPU wrote, and the CPU reads what a GPU upload wrote.
  • The existing checks in that test are unchanged and pass, including main's allocation info comparison and reserved resources (Runtime: reserved resources and GetResourceTiling on Vulkan (D3D-20) #295).
  • vk_command_replay_test part E (predication, from Runtime: SetPredication on VK_EXT_conditional_rendering (D3D-18) #258) and d3d12_com_test pass on the same device, so both optional extensions work together.

(Validation on this PC also reports a loader error from a third-party overlay layer that fails to load. With that layer disabled through VK_LOADER_LAYERS_DISABLE, the run has zero validation errors.)

Checklist

  • 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. (Not applicable: no translator change.)

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@MrCheese5

Copy link
Copy Markdown
Contributor

Tested 407a14e on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass

  • Clean RelWithDebInfo Ninja build of runtime/ (Vulkan SDK 1.4.363.0), then ctest --test-dir runtime/build --output-on-failure: 15/15 passed.
  • GPU tests with LEONIDA_VK_VALIDATION=1 and VK_LAYER_KHRONOS_validation: d3d12_com_test, d3d12_descriptor_test, d3d12_vulkan_device_test, vk_command_replay_test all exit 0, with 0 validation messages.

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

@0xTehuti 0xTehuti 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.

Independent Windows Hardware Test & Review (D3D-22: WriteToSubresource / ReadFromSubresource)

Tested and verified on Windows 11 with an NVIDIA GeForce RTX 2070 SUPER (Driver 595.79, Vulkan 1.4.321) in isolated worktree D:\Workspaces\Leonida-pr288 (407a14e).


Hardware & Toolchain Environment

  • OS: Windows 11 Pro 24H2 (x64)
  • GPU: NVIDIA GeForce RTX 2070 SUPER (8203 MB dedicated VRAM)
  • Driver: NVIDIA 595.79 (Vulkan 1.4.321, WDDM 3.2)
  • Toolchain: MSVC 2022 v14.44 (19.44.35225) + Ninja 1.13 + CMake 3.31 (RelWithDebInfo)
  • Flags: /std:c++20 /W4 /permissive- /utf-8

Compilation Results

  • 100 / 100 targets compiled cleanly.
  • Diagnostics: 0 errors, 0 warnings under /W4 /permissive- /utf-8.

Test Execution Summary

Test Target / Suite Status Execution Summary
d3d12_vulkan_device_test.exe PASSED Both execution paths validated: (1) Host image copy (VK_EXT_host_image_copy) and (2) Staging buffer fallback. Validated zero-fill on creation, box writes/reads across arbitrary row/slice pitches, mips, 3D volume slices, BC1 sub-block replacement, rejected invalid calls, and bi-directional GPU copies.
CTest Suite (15 / 15) PASSED 100% pass rate (loader, pe_loader, device_proxy, root_sig, command_list, descriptor_table, bink_decode, bink_audio, xmem, xgameruntime, root_signature, root_signature_oracle, d3d12_gcl8, d3d12_state_map, gfx10_descriptors).
d3d12_com_test.exe PASSED COM list / queue / fence texels matched; queue wait held GPU.

Execution Log Excerpt:

WriteToSubresource/ReadFromSubresource (host image copy): boxes, pitches, a mip, a volume, BC1, rejected calls and GPU copies both ways matched
WriteToSubresource/ReadFromSubresource (staging copy): boxes, pitches, a mip, a volume, BC1, rejected calls and GPU copies both ways matched
Vulkan device behind the Xbox-ABI proxy: clears, zero-filled first use, uploads, texture, buffer and BC copies, a resolve, and GetCopyableFootprints all matched

Clean, robust, and correctly handles both the host transfer extension and staging fallback paths. LGTM!


AI-assisted verification following repository guidelines (AGENTS.md).

@Parakeet-Wheat
Parakeet-Wheat force-pushed the d3d-subresource-cpu-copy branch 2 times, most recently from 74c99af to 245e43c Compare November 22, 2026 00:07
@MrCheese5

Copy link
Copy Markdown
Contributor

Tested 245e43c on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass

  • Clean RelWithDebInfo Ninja build of runtime/ (Vulkan SDK 1.4.363.0), then ctest --test-dir runtime/build --output-on-failure: 19/19 passed.
  • GPU tests with LEONIDA_VK_VALIDATION=1 and VK_LAYER_KHRONOS_validation: d3d12_com_test, d3d12_descriptor_test, d3d12_uint_clear_test, d3d12_vulkan_device_test, vk_command_replay_test all exit 0, with 0 validation messages.

This re-tests the new head (earlier pass: 407a14e).

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

@lohant27-netizen

Copy link
Copy Markdown

Tested: PR head 245e43c merged onto main 4abd545, ctest 19/19. Built and run on NVIDIA GeForce RTX 5070, driver 621.01, Windows 11, Vulkan SDK 1.4.363, MSVC 14.44 (VS 2022 Build Tools). All D3D12 GPU tests (d3d12_com_test, d3d12_vulkan_device_test, d3d12_descriptor_test, d3d12_uint_clear_test, vk_command_replay_test) exit 0 with and without LEONIDA_VK_VALIDATION=1, so 0 validation errors. No game files. AI-assisted (Claude Code).

Both were generated stubs; WriteToSubresource is in the first-frame API set
(docs/d3d12_vulkan.md section 6.1). Textures in CPU-visible custom heaps map with a
null pointer, as on D3D12, and copy on the host with VK_EXT_host_image_copy while
they are in GENERAL (D3D12's COMMON). Every other case is a CopyTextureRegion
from or to a staging buffer, replayed on the device's queue and waited for, so the
layout tracker and first-use zero fill behave as for the application's copies.
The standalone and test devices enable the extension where the GPU has it.

d3d12_vulkan_device_test checks both paths: boxes and pitches, a mip, a volume,
BC1, rejected calls, a default-heap texture, and GPU copies in both directions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Parakeet-Wheat
Parakeet-Wheat force-pushed the d3d-subresource-cpu-copy branch from 245e43c to adc1549 Compare November 22, 2026 00:30
@Parakeet-Wheat

Copy link
Copy Markdown
Contributor Author

Rebased onto 91b36cf (now adc1549) after #258 and #295 landed. Thanks @0xTehuti, @kotorvr and @lohant27-netizen for the earlier runs.

One real change from the rebase, not just a textual merge: with #258 on main, the standalone device and vk_test_device.h can enable both VK_EXT_conditional_rendering and VK_EXT_host_image_copy. They're now one extension list, and the host copy features are chained in front of the conditional rendering features (keeping the old pNext = nullptr would have dropped conditional rendering whenever host copy was on).

Retested on the RTX 3070 (driver 610.88): ctest 23/23; with LEONIDA_VK_VALIDATION=1, vk_command_replay_test parts A to E, d3d12_com_test and d3d12_vulkan_device_test (both copy paths, reserved resources) pass with 0 VUID lines.

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit b28c8ac into ItsNotRexed:main Nov 22, 2026
5 of 9 checks passed
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.

5 participants