Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
11 changes: 8 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
33 changes: 33 additions & 0 deletions runtime/src/host_image_copy_policy.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <cstring>

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
62 changes: 62 additions & 0 deletions runtime/test/host_image_copy_policy_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <cstdio>
#include <cstring>
#include <initializer_list>

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<unsigned>(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<const char*>(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;
}
Loading