diff --git a/docs/d3d12_xs_surface.md b/docs/d3d12_xs_surface.md index 4e4b018d4b..aad3958117 100644 --- a/docs/d3d12_xs_surface.md +++ b/docs/d3d12_xs_surface.md @@ -223,6 +223,20 @@ the fact; the spelling may be de-branded. The other 18 function names and all 83 standard GUID names carry no token and can be used as published. +**Feature level at device creation (BL-2).** The 24-word `params` block carries no feature level that Leonida +reads, so the host path of `D3D12XboxCreateDevice` picks the PC `D3D12CreateDevice`'s *minimum* itself. It used to +ask for `D3D_FEATURE_LEVEL_12_0`, which a comment called the least restrictive floor. That is wrong: the minimum +refuses every adapter below it, so a host that tops out at 11_x (vkd3d on some Wine/Proton setups) failed with +`E_INVALIDARG`. `rt::d3d12_xs::create_host_device` now asks for `kHostDeviceFloor` = 11_0, the lowest level D3D12 +creates a device at. The device keeps its highest level, which `D3D12_FEATURE_FEATURE_LEVELS` reports and the run log +prints ("host device created at minimum feature level 11_0; highest level 12_1" with WARP or the RTX 5070 Ti, driver +617.42). `xs_proxy_mock_test` drives it with mock hosts that top out at 11_0, 11_1, 12_0, 12_1 and 12_2. Each one is +created at 11_0, and the Xbox-ABI proxy's slot 13 (`CheckFeatureSupport`) reports that host's own top level. With +12_0 as the floor, the 11_x hosts fail. The Vulkan-backed device (`LEONIDA_D3D12=vulkan`) takes no feature level. It +answers `FEATURE_LEVELS` up to 12_1, because it reports no ray tracing tier and no sampler feedback, both of which +12_2 requires (`docs/d3d12_vulkan.md`). No Wine host was available, so the original 11_x failure is not reproduced +here; the mock is its stand-in. + ### 2.4 How ordinals 72–99 were recovered SCARAB does not publish a per-ordinal map for the 28 standard D3D12 IIDs. The map is derivable diff --git a/runtime/shims/d3d12_xs.cpp b/runtime/shims/d3d12_xs.cpp index 8e54c3fd56..28c4ce695f 100644 --- a/runtime/shims/d3d12_xs.cpp +++ b/runtime/shims/d3d12_xs.cpp @@ -119,8 +119,9 @@ HRESULT WINAPI create_root_signature_deserializer(const void* data, SIZE_T size, // Measured shape: D3D12XboxCreateDevice(adapter, params, riid, out), where params is a 24-word block that is only // logged and not used to create the device. The struct's total size is unknown, so the log reads 24 x 32-bit words -// (96 bytes) and no more. The PC D3D12CreateDevice needs a minimum feature level instead, so 12_0 is used as the -// least restrictive floor; higher feature levels are still exposed by the created device. +// (96 bytes) and no more. The PC D3D12CreateDevice needs a minimum feature level instead: create_host_device asks for +// 11_0, the least restrictive one (BL-2; 12_0 refused an adapter that tops out at 11_x), and the device still has its +// highest level. HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, const IID* riid, void** out) { constexpr auto method = "D3D12XboxCreateDevice"; using rt::d3d12_trace::result; @@ -190,13 +191,11 @@ HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, cons if (!create) return result(method, E_NOTIMPL); ID3D12Device* real = nullptr; - HRESULT hr = create(adapter, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), - reinterpret_cast(&real)); - if (FAILED(hr) || !real) { - if (real) real->Release(); + HRESULT hr = rt::d3d12_xs::create_host_device(create, adapter, &real); + if (FAILED(hr)) { std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: PC D3D12CreateDevice failed (hr=0x%08lx)\n", static_cast(hr)); - return result(method, FAILED(hr) ? hr : E_FAIL); + return result(method, hr); } hr = rt::d3d12_xs::create_device_proxy(real, *riid, out); diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 85aece0bd2..465d15adfd 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -2317,6 +2317,33 @@ HRESULT create_device_proxy(ID3D12Device* real, REFIID riid, void** out) { return S_OK; } +D3D_FEATURE_LEVEL max_feature_level(ID3D12Device* device) { + if (!device) return D3D_FEATURE_LEVEL(0); + static const D3D_FEATURE_LEVEL kLevels[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_2}; + D3D12_FEATURE_DATA_FEATURE_LEVELS levels{UINT(std::size(kLevels)), kLevels, D3D_FEATURE_LEVEL(0)}; + if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &levels, sizeof(levels)))) + return D3D_FEATURE_LEVEL(0); + return levels.MaxSupportedFeatureLevel; +} + +HRESULT create_host_device(HostCreateDeviceFn create, IUnknown* adapter, ID3D12Device** out) { + if (!out) return E_POINTER; + *out = nullptr; + if (!create) return E_NOTIMPL; + ID3D12Device* device = nullptr; + const HRESULT hr = create(adapter, kHostDeviceFloor, __uuidof(ID3D12Device), reinterpret_cast(&device)); + if (FAILED(hr) || !device) { + if (device) device->Release(); + return FAILED(hr) ? hr : E_FAIL; + } + const D3D_FEATURE_LEVEL level = max_feature_level(device); + std::fprintf(stderr, "[rt] d3d12_xs: host device created at minimum feature level 11_0; highest level %u_%u\n", + unsigned(level) >> 12, (unsigned(level) >> 8) & 0xf); + *out = device; + return S_OK; +} + bool is_device_proxy(const void* device) { return device && *reinterpret_cast(device) == reinterpret_cast(get_vtable()); } diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index b7f020bebe..7a6611fec1 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -77,6 +77,18 @@ HRESULT create_device_proxy(ID3D12Device* real, REFIID riid, void** out); // True when `device` points at a proxy created by create_device_proxy. Used by the tests. bool is_device_proxy(const void* device); +// The host path of D3D12XboxCreateDevice (BL-2, docs/d3d12_xs_surface.md): the PC D3D12CreateDevice (`create`) +// asked for kHostDeviceFloor, the lowest feature level D3D12 creates a device at. It is a minimum, so asking for it +// refuses no adapter the title can run on and still gives the device its highest level, which FEATURE_LEVELS then +// reports. A null `create`, or S_OK with no device, is E_NOTIMPL / E_FAIL; `*out` is null on failure. +constexpr D3D_FEATURE_LEVEL kHostDeviceFloor = D3D_FEATURE_LEVEL_11_0; +using HostCreateDeviceFn = HRESULT(WINAPI*)(IUnknown* adapter, D3D_FEATURE_LEVEL minimum, REFIID riid, void** out); +HRESULT create_host_device(HostCreateDeviceFn create, IUnknown* adapter, ID3D12Device** out); + +// The highest of 11_0, 11_1, 12_0, 12_1 and 12_2 `device` reports through D3D12_FEATURE_FEATURE_LEVELS, or 0 when +// the query fails. +D3D_FEATURE_LEVEL max_feature_level(ID3D12Device* device); + // IDXGIDevice1 on the device. A GDKX title asks its device for IDXGIDevice1, then GetAdapter and EnumOutputs(0), // to find the output it gives SetFrameIntervalX (docs/d3d12_vulkan.md section 4.5). The proxy answers IDXGIObject, // IDXGIDevice and IDXGIDevice1 itself, as one COM object with the device (same reference count, QueryInterface diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 888bedf244..318cf60e60 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -18,6 +18,7 @@ #include +#include #include #include #include @@ -98,6 +99,7 @@ struct Mock { std::uint64_t args[kMockSlots][5]; std::uint64_t array[kMockSlots][4]; // device: the first entries of the array argument, as received D3D12_COMMAND_QUEUE_DESC queue_desc; // device: the description of its last CreateCommandQueue (slot 8) + D3D_FEATURE_LEVEL max_level; // device: the highest level FEATURE_LEVELS reports (0: the query is refused) }; Mock g_pool[256]; @@ -235,6 +237,18 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { return a1; // a struct-returning method hands the buffer's address back in RAX } case 50: copy_array(m, slot, a3, a2); break; // (flags, count, objects, fence, value) + case 13: { // CheckFeatureSupport(feature, data, size): FEATURE_LEVELS only, as the host answers it + auto* levels = reinterpret_cast(a2); + if (static_cast(a1) != D3D12_FEATURE_FEATURE_LEVELS || !levels || !m->max_level || + static_cast(a3) != sizeof(*levels) || !levels->pFeatureLevelsRequested) + return static_cast(E_INVALIDARG); + D3D_FEATURE_LEVEL best = D3D_FEATURE_LEVEL(0); + for (UINT i = 0; i < levels->NumFeatureLevels; ++i) + if (levels->pFeatureLevelsRequested[i] <= m->max_level) + best = std::max(best, levels->pFeatureLevelsRequested[i]); + levels->MaxSupportedFeatureLevel = best; + return static_cast(best ? S_OK : DXGI_ERROR_UNSUPPORTED); + } default: break; } return S_OK; @@ -403,6 +417,67 @@ unsigned long release(void* object) { return static_cast(raw(object, 2)); } +// ---- 0. The host device's feature-level floor (BL-2) ---- + +// A host D3D12CreateDevice whose adapter tops out at g_host_max: a minimum above it is refused with E_INVALIDARG, +// as on a host that tops out at 11_x, and g_host_mode makes it fail outright or succeed with no object. +D3D_FEATURE_LEVEL g_host_max = D3D_FEATURE_LEVEL_12_1; +D3D_FEATURE_LEVEL g_host_minimum = D3D_FEATURE_LEVEL(0); // the minimum the last call asked for +enum class HostMode { normal, fail, no_object } g_host_mode = HostMode::normal; + +HRESULT WINAPI fake_create_device(IUnknown*, D3D_FEATURE_LEVEL minimum, REFIID riid, void** out) { + g_host_minimum = minimum; + if (!out) return S_FALSE; + *out = nullptr; + if (g_host_mode == HostMode::fail) return DXGI_ERROR_UNSUPPORTED; + if (g_host_mode == HostMode::no_object) return S_OK; + if (minimum > g_host_max) return E_INVALIDARG; + Mock* m = new_mock(Kind::device); + m->max_level = g_host_max; + return mock_query(m, &riid, out); +} + +int check_host_device_floor() { + g_label = "xs_proxy_mock host feature level"; + EXPECT(xs::kHostDeviceFloor == D3D_FEATURE_LEVEL_11_0); + const D3D_FEATURE_LEVEL tops[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_2}; + for (const D3D_FEATURE_LEVEL top : tops) { + g_host_max = top; + // The floor 12_0 the host path used to ask for refuses the adapters that top out at 11_x. + void* old = nullptr; + const HRESULT old_hr = fake_create_device(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), &old); + EXPECT(FAILED(old_hr) == (top < D3D_FEATURE_LEVEL_12_0)); + if (old) release(old); + + ID3D12Device* real = nullptr; + EXPECT(xs::create_host_device(&fake_create_device, nullptr, &real) == S_OK && real); + EXPECT(g_host_minimum == D3D_FEATURE_LEVEL_11_0); + EXPECT(xs::max_feature_level(real) == top); // the device keeps its highest level + // The title asks the device it gets (the Xbox-ABI proxy) through slot 13, CheckFeatureSupport. + void* device = nullptr; + EXPECT(SUCCEEDED(xs::create_device_proxy(real, __uuidof(ID3D12Device), &device)) && device); + D3D12_FEATURE_DATA_FEATURE_LEVELS levels{UINT(std::size(tops)), tops, D3D_FEATURE_LEVEL(0)}; + EXPECT(hr_of(raw(device, 13, D3D12_FEATURE_FEATURE_LEVELS, word(&levels), sizeof(levels))) == S_OK); + EXPECT(levels.MaxSupportedFeatureLevel == top); + release(device); + } + g_host_max = D3D_FEATURE_LEVEL_12_1; + ID3D12Device* real = reinterpret_cast(&g_host_max); // must be cleared on every failure + EXPECT(xs::create_host_device(nullptr, nullptr, &real) == E_NOTIMPL && !real); + EXPECT(xs::create_host_device(&fake_create_device, nullptr, nullptr) == E_POINTER); + g_host_mode = HostMode::fail; + real = reinterpret_cast(&g_host_max); + EXPECT(xs::create_host_device(&fake_create_device, nullptr, &real) == DXGI_ERROR_UNSUPPORTED && !real); + g_host_mode = HostMode::no_object; + real = reinterpret_cast(&g_host_max); + EXPECT(xs::create_host_device(&fake_create_device, nullptr, &real) == E_FAIL && !real); + g_host_mode = HostMode::normal; + EXPECT(xs::max_feature_level(nullptr) == D3D_FEATURE_LEVEL(0)); + g_label = "xs_proxy_mock"; + return 0; +} + // ---- 1. Device interface levels ---- int check_levels() { @@ -1714,7 +1789,7 @@ int main(int argc, char** argv) { return run_fault_mode(argv[1]); } if (argc != 1) return 2; - if (check_tail(true) || check_levels() || check_slot48() || check_fence1() || check_create_failures() || check_open_shared() || + if (check_tail(true) || check_host_device_floor() || check_levels() || check_slot48() || check_fence1() || check_create_failures() || check_open_shared() || check_alloc_failures() || check_allocators() || check_xbox_slots() || check_console_command_list() || check_custom_heap_properties() || check_resource_allocation_info() || check_slot70() || check_committed_resource3_x() ||