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

Closed
github-bot wants to merge 1 commit from gh-pr/1810/head into gh-pr/1810/base
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 12:22 UTC · original
abnalstlll-byte:feat/rt-host-image-copy-env → main · +35 −1 · 4 files · 1 commits · closed without merging

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 (#1608).

AI-assisted (Claude Code)

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 12:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810) `abnalstlll-byte:feat/rt-host-image-copy-env` → `main` · +35 −1 · 4 files · 1 commits · closed without merging ### 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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608)) 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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). AI-assisted (Claude Code)
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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:06:31 +00:00
github-bot closed this pull request 2026-11-26 10:06:31 +00:00
Author
Owner

@⁠ImLele69 on GitHub · 2026-11-25 13:22 UTC · original

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

**@⁠ImLele69** on GitHub · 2026-11-25 13:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7163673931) 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).
Author
Owner

@⁠CLHelper on GitHub · 2026-11-25 13:45 UTC · original

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.

**@⁠CLHelper** on GitHub · 2026-11-25 13:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164254112) 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.
Author
Owner

@⁠potatoe0100 on GitHub · 2026-11-25 13:50 UTC · original

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)

**@⁠potatoe0100** on GitHub · 2026-11-25 13:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164369335) 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)
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 13:53 UTC · original

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

**@⁠QemmHD** on GitHub · 2026-11-25 13:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7164445718) @⁠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).
Author
Owner

@⁠OreoFR on GitHub · 2026-11-25 14:56 UTC · original

See my note on #1851 (#1851): 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.

**@⁠OreoFR** on GitHub · 2026-11-25 14:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7166050696) See my note on #1851 (https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/1851): 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.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 16:11 UTC · original

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

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 16:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1810#issuecomment-7167670527) Closing in favor of @⁠QemmHD's #1851, which provides the full policy with automatic NVIDIA detection and comprehensive tests. Great work!

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.