diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 53d9747968..04d11a733b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2919,6 +2919,14 @@ 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). + - `LEONIDA_HOST_IMAGE_COPY` chooses whether the device enables the extension at all (`src/host_image_copy_policy.h`, + tested by `host_image_copy_policy_test`): `1` wherever the device has it, `0` never, and `auto`, the default, as + `1` except on NVIDIA (`vendorID` `0x10DE`), where it is off. On an RTX 4070 SUPER (Windows 11) a real boot lost + the device with `WRITE_INVALID` in 7 of 7 runs that used host image copies and in 0 of 2 that did not; why is + still under investigation. Off, the extension is neither enabled nor chained, no texture gets host-transfer + usage, and every copy takes the staging path, as on a device without the extension. Device creation logs one + line, `[rt] d3d12: host image copy: on (available)` or `off (...)` with the reason. Results with + `LEONIDA_HOST_IMAGE_COPY=1` on other NVIDIA GPUs and drivers are welcome in an issue. - 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/CMakeLists.txt b/runtime/CMakeLists.txt index e9cb8d4345..cbeccc831b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -797,6 +797,10 @@ if(BUILD_TESTING) add_executable(vulkan_device_filter_test test/vulkan_device_filter_test.cpp) target_include_directories(vulkan_device_filter_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) add_test(NAME vulkan_device_filter COMMAND vulkan_device_filter_test) + # LEONIDA_HOST_IMAGE_COPY's choice (runtime/src/host_image_copy_policy.h): pure stdlib, no Vulkan instance. + add_executable(host_image_copy_policy_test test/host_image_copy_policy_test.cpp) + target_include_directories(host_image_copy_policy_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) + add_test(NAME host_image_copy_policy COMMAND host_image_copy_policy_test) add_executable(vk_presenter_capture_colour_test test/vk_presenter_capture_colour_test.cpp) add_test(NAME vk_presenter_capture_colour COMMAND vk_presenter_capture_colour_test) if(TARGET gta6rt) diff --git a/runtime/README.md b/runtime/README.md index 1573b8883c..6d9cbd7aaf 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -323,6 +323,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_CONSOLE_FORMATS` | on | Texture descriptions the console accepts and a PC GPU may not (an R9G9B9E5 render target, an sRGB RGBA8 or BGRA8 texture with unordered access) get a host format that can do the job. `0` refuses them, as a PC D3D12 runtime does. | | `LEONIDA_CONSOLE_HEAP_SHADOW` | on | A console descriptor heap has a copy in the descriptor arena that the shaders read. `0` keeps its bytes in the title's memory only (the comparison for a run that misbehaves with the copy). | | `LEONIDA_IMPORT_BELOW_ARENA` | unset | `1` keeps a host-memory import that the driver put below the GPU address arena. By default such an import is not used and the shadow buffer in the arena answers, because a V# cannot name an address below the arena. | +| `LEONIDA_HOST_IMAGE_COPY` | `auto` | Whether `WriteToSubresource`/`ReadFromSubresource` copy on the CPU with `VK_EXT_host_image_copy`. `auto` (unset, or any text other than `0` and `1`) uses it where the device has it, except on NVIDIA (vendor `0x10DE`), where a real boot lost the device; `1` uses it wherever the device has it; `0` never enables the extension, so every CPU texture copy takes the staging submit. Device creation logs one `[rt] d3d12: host image copy:` line with the choice and why (`docs/d3d12_vulkan.md`). | | `LEONIDA_ORDER_TEXTURE_UPLOADS` | on | A title texture in a host-transfer image is filled through the ordered host copy (`write_host_texture`). `0` fills it with `WriteToSubresource`. | | `LEONIDA_PLACED_IMAGE_KEYS` | on | A texture the title placed at its own address (device slot 133) and that the GPU writes keeps its own image address as its T# key. `0` re-keys it to the title address, like a texture the GPU does not write. | | `LEONIDA_PLACED_TEXTURE_UAV` | on | Device slot 71 writes an unordered-access view of a texture in the title's placed memory. `0` writes a null descriptor. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 49743d0c1d..14a4f59afc 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -62,6 +62,7 @@ #include "../src/title_window_plan.h" #include "../src/vulkan_present.h" #include "../src/vulkan_device_filter.h" +#include "../src/host_image_copy_policy.h" #include "../src/vulkan13_features.h" #include "../src/vulkan_index_view.h" #include "d3d12_compute_input_probe.h" @@ -17951,13 +17952,17 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin vkGetPhysicalDeviceFeatures2(info.physical, &query); } const char* host_image_copy_name = VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME; - info.host_image_copy = has_host_image_copy && host_copy.hostImageCopy; + // LEONIDA_HOST_IMAGE_COPY (src/host_image_copy_policy.h). Off, the extension is neither enabled nor chained, so no + // image gets host-transfer usage and every CPU texture copy takes the staging submit, as on a device without it. + const leonida::HostImageCopyChoice host_copy_choice = leonida::host_image_copy_choice( + std::getenv("LEONIDA_HOST_IMAGE_COPY"), has_host_image_copy && host_copy.hostImageCopy, chosen.vendorID); + info.host_image_copy = host_copy_choice.use; 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: host image copy: %s (%s)%s\n", info.host_image_copy ? "on" : "off", + host_copy_choice.reason, info.host_image_copy ? "" : "; CPU texture copies use a staging submit"); const auto has_device_extension = [&](const char* name) { return std::any_of(extensions.begin(), extensions.end(), diff --git a/runtime/src/host_image_copy_policy.h b/runtime/src/host_image_copy_policy.h new file mode 100644 index 0000000000..a67be2a885 --- /dev/null +++ b/runtime/src/host_image_copy_policy.h @@ -0,0 +1,33 @@ +// LEONIDA_HOST_IMAGE_COPY: whether the Vulkan D3D12 layer (shims/d3d12_vulkan_device.cpp) enables +// VK_EXT_host_image_copy for WriteToSubresource/ReadFromSubresource. Pure stdlib, so the choice is tested without a +// GPU (test/host_image_copy_policy_test.cpp). +// +// 0 never: the extension is not enabled and every CPU texture copy takes the staging submit. +// 1 whenever the device has the extension and its hostImageCopy feature. +// auto (unset, empty or any other text) as 1, except on NVIDIA, where it is off: a real boot on an RTX 4070 SUPER +// lost the device (WRITE_INVALID) in 7 of 7 runs with host image copies and in 0 of 2 without them. The +// cause is not known yet (docs/d3d12_vulkan.md). +#pragma once + +#include +#include + +namespace leonida { + +constexpr std::uint32_t kNvidiaVendorId = 0x10DE; // VkPhysicalDeviceProperties::vendorID + +struct HostImageCopyChoice { + bool use; + const char* reason; // static text for the device-creation log line +}; + +// `setting` is the variable's text (null when unset); `available` is the extension with its hostImageCopy feature. +inline HostImageCopyChoice host_image_copy_choice(const char* setting, bool available, std::uint32_t vendor_id) { + if (setting && std::strcmp(setting, "0") == 0) return {false, "LEONIDA_HOST_IMAGE_COPY=0"}; + if (!available) return {false, "not available"}; + if (setting && std::strcmp(setting, "1") == 0) return {true, "LEONIDA_HOST_IMAGE_COPY=1"}; + if (vendor_id == kNvidiaVendorId) return {false, "auto: NVIDIA"}; + return {true, "available"}; +} + +} // namespace leonida diff --git a/runtime/test/host_image_copy_policy_test.cpp b/runtime/test/host_image_copy_policy_test.cpp new file mode 100644 index 0000000000..eaf288ba31 --- /dev/null +++ b/runtime/test/host_image_copy_policy_test.cpp @@ -0,0 +1,62 @@ +// Unit tests for LEONIDA_HOST_IMAGE_COPY (runtime/src/host_image_copy_policy.h). No Vulkan instance and no GPU: the +// choice is a pure function of the variable's text, the extension's availability and the vendor. +#include "host_image_copy_policy.h" + +#include +#include +#include +#include + +namespace { + +int failures = 0; + +void expect(const char* setting, bool available, std::uint32_t vendor, bool use, const char* reason) { + const leonida::HostImageCopyChoice got = leonida::host_image_copy_choice(setting, available, vendor); + if (got.use == use && got.reason && std::strcmp(got.reason, reason) == 0) return; + std::fprintf(stderr, "FAIL setting=%s available=%d vendor=0x%04X: got %s (%s), want %s (%s)\n", + setting ? setting : "(unset)", available ? 1 : 0, static_cast(vendor), + got.use ? "on" : "off", got.reason ? got.reason : "(null)", use ? "on" : "off", reason); + ++failures; +} + +} // namespace + +int main() { + constexpr std::uint32_t nvidia = 0x10DE, amd = 0x1002, intel = 0x8086; + static_assert(leonida::kNvidiaVendorId == nvidia, "NVIDIA's PCI vendor id"); + + // auto (unset, empty, the word itself): on where available, except on NVIDIA. + for (const char* automatic : {static_cast(nullptr), "", "auto"}) { + expect(automatic, true, amd, true, "available"); + expect(automatic, true, intel, true, "available"); + expect(automatic, true, nvidia, false, "auto: NVIDIA"); + expect(automatic, false, amd, false, "not available"); + expect(automatic, false, nvidia, false, "not available"); + } + + // 0: never, whatever the device has. + expect("0", true, amd, false, "LEONIDA_HOST_IMAGE_COPY=0"); + expect("0", true, nvidia, false, "LEONIDA_HOST_IMAGE_COPY=0"); + expect("0", false, amd, false, "LEONIDA_HOST_IMAGE_COPY=0"); + + // 1: whenever available, NVIDIA included; it cannot turn on what the device does not have. + expect("1", true, nvidia, true, "LEONIDA_HOST_IMAGE_COPY=1"); + expect("1", true, amd, true, "LEONIDA_HOST_IMAGE_COPY=1"); + expect("1", false, nvidia, false, "not available"); + expect("1", false, amd, false, "not available"); + + // Anything else is auto: only the exact texts 0 and 1 are switches. + for (const char* garbage : {"yes", "on", "off", "true", "01", "10", " 1", "1 ", "2", "-1", "AUTO", "nvidia"}) { + expect(garbage, true, amd, true, "available"); + expect(garbage, true, nvidia, false, "auto: NVIDIA"); + expect(garbage, false, amd, false, "not available"); + } + + if (failures) { + std::fprintf(stderr, "%d failure(s)\n", failures); + return 1; + } + std::printf("host_image_copy_policy: all cases passed\n"); + return 0; +}