Skip to content

d3d12: support LEONIDA_HOST_IMAGE_COPY=0 to force staging texture copies (RT-HOST-IMAGE-COPY-ENV) - #1810

Closed
abnalstlll-byte wants to merge 1 commit into
ProjectLeonida:mainfrom
abnalstlll-byte:feat/rt-host-image-copy-env
Closed

abnalstlll-byte wants to merge 1 commit into
ProjectLeonida:mainfrom
abnalstlll-byte:feat/rt-host-image-copy-env

Conversation

@abnalstlll-byte

Copy link
Copy Markdown
Contributor

Description

This PR implements the LEONIDA_HOST_IMAGE_COPY=0 (or off) environment variable switch, enabling community testers and CI to disable VK_EXT_host_image_copy and route all CPU texture transfers through the existing staging submit path.

Context & Finding

  • Hardware measurements reported by @QemmHD on RTX 4070 SUPER (Coordinating who works on what (continued from #14) #1608, comment 7162097442) revealed that VK_EXT_host_image_copy is the direct trigger for the fatal WRITE_INVALID device loss during the transition to the first world frame (crashing at ~6,570 commands).
  • Disabling host image copies allows the title to run past 29,726 commands, 49,019 draws, and 7,431 PresentX frames with zero device loss.
  • Prior to this PR, testers had to manually modify C++ source and recompile to test this path.

Changes

  1. runtime/shims/d3d12_vulkan_device.cpp:
    • Check LEONIDA_HOST_IMAGE_COPY via getenv_s. If set to "0" or "off", set info.host_image_copy = false and log [rt] d3d12: LEONIDA_HOST_IMAGE_COPY=0; CPU texture copies use a staging submit.
    • Naturally omits host_copy from the Vulkan 1.3 pNext chain, omits VK_EXT_host_image_copy from device_extensions, and skips entry point loading.
  2. runtime/test/vk_test_device.h:
    • Also respect LEONIDA_HOST_IMAGE_COPY=0 in TestDevice for consistent test harness behavior.
  3. runtime/test/d3d12_vulkan_device_test.cpp:
    • Added test case verifying that _putenv_s("LEONIDA_HOST_IMAGE_COPY", "0") disables made->host_image_copy on standalone device creation and successfully executes CPU texture copies through the staging path.
  4. docs/d3d12_vulkan.md:
    • Documented LEONIDA_HOST_IMAGE_COPY=0 under subresource copy options.

Verification

  • python -I scripts/check_no_game_data.py: Clean-room confirmed, 0 violations.
  • python -I tests/test_text_write_line_endings.py: 3/3 tests passed.

Claimed in #1608 (#1608 (comment)).

AI-assisted (Claude Code)

…ies (RT-HOST-IMAGE-COPY-ENV)

Support setting LEONIDA_HOST_IMAGE_COPY=0 (or off) to bypass
VK_EXT_host_image_copy when creating the Vulkan device, forcing all
WriteToSubresource and ReadFromSubresource CPU texture transfers through
the existing staging queue submit path.

This provides an immediate diagnostic bypass for driver-side host image
transfer write faults (e.g. WRITE_INVALID device loss during world-start
streaming on NVIDIA GPUs) while underlying driver/subresource layout
investigations continue.

Add test coverage in d3d12_vulkan_device_test.cpp and document in
docs/d3d12_vulkan.md.

AI-assisted (Claude Code)
@ImLele69

Copy link
Copy Markdown
Contributor

This doesn't build with clang-cl 20 against the xwin UCRT headers (runtime/build/windows, Linux cross build):

d3d12_vulkan_device.cpp(17996,9): error: no matching function for call to 'getenv_s'
note: candidate function template not viable: no known conversion from 'DWORD *' (aka 'unsigned long *') to 'size_t *' (aka 'unsigned long long *') for 1st argument

getenv_s takes a size_t* for the length. Changing DWORD host_copy_len = 0; to std::size_t host_copy_len = 0; is enough. With that change, merged together with #1827 on main 98c2f77, the switch works under Wine: the log says LEONIDA_HOST_IMAGE_COPY=0; CPU texture copies use a staging submit.

AI-assisted (Claude Code).

@CLHelper

Copy link
Copy Markdown
Contributor

This does not build with MSVC: runtime/shims/d3d12_vulkan_device.cpp (the new block in the host-image-copy query) fails with error C2660: 'getenv_s': function does not take 3 arguments (VS 2022 Build Tools, Ninja, RelWithDebInfo, on main 5e95fa2 with #1827 and #1849). MSVC's getenv_s is (size_t* len, char* buf, size_t size, const char* name), and len is a size_t*, not a DWORD*.

For a local test I replaced the call with GetEnvironmentVariableA("LEONIDA_HOST_IMAGE_COPY", host_copy_env, sizeof host_copy_env) (as the other LEONIDA_* reads in this file do), keeping host_copy_len > 1 meaning "set" by adding one. With that, LEONIDA_HOST_IMAGE_COPY=0 logs CPU texture copies use a staging submit, and an 8-minute boot on an RTX 4060 Laptop 8 GB (NVIDIA 576.80, Windows 11) with #1827 and #1849 had no device loss and no WRITE_INVALID (25,511 submissions, 12,744 presents; it reached the story load late, after slow manual menus, so this is not yet a world-frame result). AI-assisted.

@potatoe0100

Copy link
Copy Markdown
Contributor

Build failure with MSVC: b7980d3ef merged onto main 98c2f7777 (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 (MSVC 19.44) RelWithDebInfo + Ninja.

runtime\shims\d3d12_vulkan_device.cpp(17957): error C2660: 'getenv_s': function does not take 3 arguments

host_copy_len is declared DWORD, so the three-argument getenv_s(size_t*, char (&)[N], const char*) template doesn't match; the other calls in this file use std::size_t. Declaring it std::size_t host_copy_len = 0; should fix it. I didn't get as far as running the tests.

AI-assisted (Claude)

@QemmHD

QemmHD commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@abnalstlll-byte thanks for picking this up so fast, and sorry for the overlap: I opened #1851 an hour later without spotting this one. They overlap in d3d12_vulkan_device.cpp where the feature is chosen. #1851 also adds an auto default (off on NVIDIA), a unit-tested decision function and a log line, and ecox982745 found the NVIDIA default needs #1827 to land first (title images otherwise stay unfilled). To avoid two competing switches I'm happy either way. #1851 can be rebased on top of yours, keeping your 0/off handling and crediting you. Or, if you prefer, fold auto and the test into yours and I'll close mine. Maintainers' call.

AI-assisted (Claude Code, Claude Opus 5.5).

@OreoFR

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

See my note on #1851 (#1851 (comment)): with #1893 and host image copy on, crash A did not reproduce in 2 runs here. Host copy off may no longer be needed for that crash; a check on another card is needed before deciding.

AI-assisted.

@abnalstlll-byte

Copy link
Copy Markdown
Contributor Author

Closing in favor of @QemmHD's #1851, which provides the full policy with automatic NVIDIA detection and comprehensive tests. Great work!

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.

6 participants