Skip to content
Open
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
4 changes: 2 additions & 2 deletions docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ fresh name or delete the old one. `LEONIDA_D3D12=vulkan` selects the Vulkan-back
traces command lists, resources and pipelines; without it the layer uses the Windows D3D12 device, and the
`[d3d12-trace]` lines are only what the Xbox-ABI proxy forwards (device creation, queue, fence and allocator
calls), which `boot_trace.py` summarizes without complaint. A run on the Vulkan device prints
`[rt] d3d12: Vulkan device: <name>`; check the log for it (a runtime built without the Vulkan headers accepts the
variable and stays on the Windows device). Keep the traces out of the repository: they can quote whatever the game prints.
`[rt] d3d12: Vulkan device: <name>`; check the log for it (a runtime built without the Vulkan headers stays on the Windows
device and says so in one `[rt] d3d12_xs: LEONIDA_D3D12=vulkan is set, but ...` line). Keep the traces out of the repository: they can quote whatever the game prints.

Useful switches:

Expand Down
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,13 @@ if(NOT TARGET Vulkan::Headers)
FetchContent_MakeAvailable(VulkanHeaders)
endif()
endif()
# LEONIDA_D3D12=vulkan asked of a runtime with no Vulkan-backed device (shims/d3d12_backend.h). Outside the block
# below on purpose: a configure without the Vulkan headers is the case it is about.
add_executable(d3d12_backend_test test/d3d12_backend_test.cpp)
if(WIN32 AND MSVC)
target_compile_options(d3d12_backend_test PRIVATE /W4 /permissive- /utf-8)
endif()
add_test(NAME d3d12_backend COMMAND d3d12_backend_test)
if(TARGET Vulkan::Headers)
add_executable(vulkan_device_fault_test test/vulkan_device_fault_test.cpp)
target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers)
Expand Down
23 changes: 23 additions & 0 deletions runtime/shims/d3d12_backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Which device D3D12XboxCreateDevice puts behind the Xbox-ABI proxy, as LEONIDA_D3D12 asks (RT-VULKAN-REQUESTED-NOT-BUILT).
// Header only, so the test needs neither a device nor the Vulkan headers.
#pragma once

#include <cstring>

namespace rt::d3d12_xs::backend {

// LEONIDA_D3D12=vulkan, exactly: the one value that selects the Vulkan-backed device.
inline bool vulkan_requested(const char* value) { return value && std::strcmp(value, "vulkan") == 0; }

// What to tell the user when the variable asks for the Vulkan-backed device and this runtime has none, else null.
// A runtime configured without the Vulkan headers is built without LEONIDA_VULKAN_DEVICE: it used to take the variable
// without a word and run the title on the host's D3D12 device, which stops somewhere else and looks like a boot result.
inline const char* vulkan_not_built_note(const char* value, bool vulkan_built) {
if (vulkan_built || !vulkan_requested(value)) return nullptr;
return "LEONIDA_D3D12=vulkan is set, but this runtime was built without the Vulkan-backed device (its configure "
"found no Vulkan headers), so the title gets the host's D3D12 device instead. Configure again with "
"VULKAN_SDK set or -DLEONIDA_VULKAN_HEADERS_FETCH=ON; -DLEONIDA_REQUIRE_VULKAN=ON makes a configure "
"without the headers fail";
}

} // namespace rt::d3d12_xs::backend
17 changes: 15 additions & 2 deletions runtime/shims/d3d12_xs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifdef LEONIDA_VULKAN_DEVICE
#include "d3d12_com.h"
#endif
#include "d3d12_backend.h"
#include "d3d12_trace.h"
#include "d3d12_xs_device.h"
#include "d3d12_xs_rootsig.h"
Expand Down Expand Up @@ -198,12 +199,24 @@ HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, cons
const bool pc_lists = getenv_s(&lists_length, lists_mode, "LEONIDA_D3D12_LISTS") == 0 &&
std::strcmp(lists_mode, "pc") == 0;

#ifdef LEONIDA_VULKAN_DEVICE
// Opt-in until the Vulkan device covers the first frame: LEONIDA_D3D12=vulkan puts the Vulkan-backed device
// behind the same proxy instead of the host's D3D12 device.
char backend[16] = {};
size_t backend_length = 0;
if (getenv_s(&backend_length, backend, "LEONIDA_D3D12") == 0 && std::strcmp(backend, "vulkan") == 0) {
const bool backend_read = getenv_s(&backend_length, backend, "LEONIDA_D3D12") == 0;
#ifdef LEONIDA_VULKAN_DEVICE
constexpr bool vulkan_built = true;
#else
constexpr bool vulkan_built = false;
#endif
// A runtime configured without the Vulkan headers has no such device: say so once instead of running the title on
// the host's device as if nothing had been asked.
if (const char* note = rt::d3d12_xs::backend::vulkan_not_built_note(backend_read ? backend : nullptr, vulkan_built)) {
static std::atomic<bool> noted{false};
if (!noted.exchange(true, std::memory_order_relaxed)) std::fprintf(stderr, "[rt] d3d12_xs: %s\n", note);
}
#ifdef LEONIDA_VULKAN_DEVICE
if (backend_read && rt::d3d12_xs::backend::vulkan_requested(backend)) {
ID3D12Device* vulkan = nullptr;
HRESULT vhr = rt::d3d12::create_vulkan_device_standalone(
__uuidof(ID3D12Device), reinterpret_cast<void**>(&vulkan), rt::d3d12::QueueTestSettings::ignore);
Expand Down
41 changes: 41 additions & 0 deletions runtime/test/d3d12_backend_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// d3d12_backend.h (RT-VULKAN-REQUESTED-NOT-BUILT): LEONIDA_D3D12=vulkan in a runtime that has no Vulkan-backed device
// gets a note; every other case gets none. CPU only: no device, no Vulkan headers, no game file.
#include "../shims/d3d12_backend.h"

#include <cstdio>
#include <cstring>

namespace backend = rt::d3d12_xs::backend;

static int failures = 0;
static void check(bool condition, const char* what) {
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
if (!condition) ++failures;
}

int main() {
check(backend::vulkan_requested("vulkan"), "requested: vulkan");
check(!backend::vulkan_requested(nullptr), "requested: the variable is not set");
check(!backend::vulkan_requested(""), "requested: an empty value");
check(!backend::vulkan_requested("Vulkan"), "requested: another case is another value, as the device branch reads it");
check(!backend::vulkan_requested("vulkan "), "requested: a trailing space is another value");
check(!backend::vulkan_requested("host"), "requested: another backend name");

const char* note = backend::vulkan_not_built_note("vulkan", false);
check(note != nullptr, "note: vulkan asked of a runtime without the device");
check(note && std::strstr(note, "LEONIDA_D3D12=vulkan") && std::strstr(note, "host's D3D12 device"),
"note: names the variable and the device the title gets");
check(note && std::strstr(note, "LEONIDA_VULKAN_HEADERS_FETCH") && std::strstr(note, "LEONIDA_REQUIRE_VULKAN") &&
std::strstr(note, "VULKAN_SDK"),
"note: names the ways to get the headers and the option that fails the configure");

check(backend::vulkan_not_built_note("vulkan", true) == nullptr, "note: none when the device is built in");
check(backend::vulkan_not_built_note(nullptr, false) == nullptr, "note: none when the variable is not set");
check(backend::vulkan_not_built_note("", false) == nullptr, "note: none for an empty value");
check(backend::vulkan_not_built_note("Vulkan", false) == nullptr,
"note: none for a value the Vulkan build would not take either");
check(backend::vulkan_not_built_note(nullptr, true) == nullptr, "note: none, device built in and nothing asked");

std::printf("%d failures\n", failures);
return failures ? 1 : 0;
}
Loading