From b7980d3efe51d09f74e589e67f8e38890f926afc Mon Sep 17 00:00:00 2001 From: d7ommy Date: Wed, 25 Nov 2026 15:21:57 +0300 Subject: [PATCH] d3d12: support LEONIDA_HOST_IMAGE_COPY=0 to force staging texture copies (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) --- docs/d3d12_vulkan.md | 3 +++ runtime/shims/d3d12_vulkan_device.cpp | 12 +++++++++++- runtime/test/d3d12_vulkan_device_test.cpp | 14 ++++++++++++++ runtime/test/vk_test_device.h | 7 +++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 53d9747968..3c364f6ad6 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2919,6 +2919,9 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv without host-transfer usage before it is bound, and takes the staging path. `d3d12_vulkan_device_test` and `d3d12_subresource_copy_test` force this branch on any GPU with `VulkanDeviceInfo::force_host_transfer_retry` (their third CPU-copy path; the second compares it with the host runtime). + - Setting `LEONIDA_HOST_IMAGE_COPY=0` (or `off`) disables `VK_EXT_host_image_copy` and routes all CPU texture + copies through the staging submit path. This provides an immediate diagnostic bypass for driver-side host image + transfer write faults (e.g. `WRITE_INVALID` stops during world-start loading on NVIDIA GPUs). - 96-bit textures (`R32G32B32_FLOAT`/`_UINT`/`_SINT`/`_TYPELESS`, D3D-62) are `R32G32B32A32` images (`ResourceBinding::rgb96`), on every GPU. Most GPUs have no optimal-tiling RGB32 image (an RX 9070 XT reports no optimal features for `R32G32B32_SFLOAT`), and one that has would not avoid the repack: D3D12 places diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 49743d0c1d..1c4d425ea2 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -17952,11 +17952,21 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin } const char* host_image_copy_name = VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME; info.host_image_copy = has_host_image_copy && host_copy.hostImageCopy; + char host_copy_env[8]{}; + DWORD host_copy_len = 0; + if (getenv_s(&host_copy_len, host_copy_env, "LEONIDA_HOST_IMAGE_COPY") == 0 && host_copy_len > 1) { + if (host_copy_env[0] == '0' || std::strcmp(host_copy_env, "off") == 0) { + info.host_image_copy = false; + } + } if (info.host_image_copy) { host_copy.pNext = *v13.next(); // after the conditional rendering features, if any *v13.next() = &host_copy; } else { - std::fprintf(stderr, "[rt] d3d12: no VK_EXT_host_image_copy; CPU texture copies use a staging submit\n"); + std::fprintf(stderr, "[rt] d3d12: %s; CPU texture copies use a staging submit\n", + (host_copy_len > 1 && (host_copy_env[0] == '0' || std::strcmp(host_copy_env, "off") == 0)) + ? "LEONIDA_HOST_IMAGE_COPY=0" + : "no VK_EXT_host_image_copy"); } const auto has_device_extension = [&](const char* name) { diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 9df462d3fd..c38ff2c195 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -7086,6 +7086,20 @@ int main() { EXPECT(cpu_copies(device.p, d.bc, path.name) == 0); } + // LEONIDA_HOST_IMAGE_COPY=0 forces staging copies even on a device with VK_EXT_host_image_copy. + { + _putenv_s("LEONIDA_HOST_IMAGE_COPY", "0"); + Ref forced_staging; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&forced_staging.p)))); + const rt::d3d12::VulkanDeviceInfo* made = rt::d3d12::vulkan_device_info(forced_staging.p); + EXPECT(made != nullptr); + if (made) { + EXPECT(!made->host_image_copy); + } + EXPECT(cpu_copies(forced_staging.p, d.bc, "staging copy with LEONIDA_HOST_IMAGE_COPY=0") == 0); + _putenv_s("LEONIDA_HOST_IMAGE_COPY", ""); + } + { Ref removable; EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&removable.p)))); diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 66db0df3c1..836bb25b6c 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -550,6 +550,13 @@ class Device { instance_fn("vkGetPhysicalDeviceFeatures2")(physical, &query); host_image_copy = host_copy.hostImageCopy == VK_TRUE; } + char host_copy_env[8]{}; + DWORD host_copy_len = 0; + if (getenv_s(&host_copy_len, host_copy_env, "LEONIDA_HOST_IMAGE_COPY") == 0 && host_copy_len > 1) { + if (host_copy_env[0] == '0' || std::strcmp(host_copy_env, "off") == 0) { + host_image_copy = false; + } + } const char* host_image_copy_name = VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME; if (host_image_copy) { host_copy.pNext = *v13_.next(); // after the conditional rendering features, if any