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 from upstream-pr/1810 into main
abnalstlll-byte commented 2026-11-25 12:22:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-25T12:22:20Z.


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 (#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 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162191969).

AI-assisted (Claude Code)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491235437 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-25T12:22:20Z. --- ### 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 (#1608, [comment 7162097442](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-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 (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162191969). AI-assisted (Claude Code)
ImLele69 commented 2026-11-25 13:22:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T13:22:04Z.


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 98c2f7777, the switch works under Wine: the log says LEONIDA_HOST_IMAGE_COPY=0; CPU texture copies use a staging submit.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7163673931 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7163673931). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T13:22:04Z. --- 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` 98c2f7777, 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 commented 2026-11-25 13:45:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by CLHelper on 2026-11-25T13:45:50Z.


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 5e95fa2f4 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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164254112 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164254112). Originally posted by [CLHelper](https://github.com/CLHelper) on 2026-11-25T13:45:50Z. --- 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` 5e95fa2f4 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 commented 2026-11-25 13:50:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by potatoe0100 on 2026-11-25T13:50:33Z.


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164369335 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164369335). Originally posted by [potatoe0100](https://github.com/potatoe0100) on 2026-11-25T13:50:33Z. --- 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 commented 2026-11-25 13:53:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T13:53:43Z.


@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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164445718 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164445718). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T13:53:43Z. --- @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 commented 2026-11-25 14:56:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-25T14:56:21Z.


See my note on #1851 (https://github.com/ubiquitarianism/leonida/pull/1851#issuecomment-7166050100): 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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166050696 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7166050696). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-25T14:56:21Z. --- See my note on #1851 (https://github.com/ubiquitarianism/leonida/pull/1851#issuecomment-7166050100): 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 commented 2026-11-25 16:11:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-25T16:11:19Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167670527 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7167670527). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-25T16:11:19Z. --- Closing in favor of @QemmHD's #1851, which provides the full policy with automatic NVIDIA detection and comprehensive tests. Great work!

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1810
No description provided.