From 0b312b05c6c76a96b9bd66cfe90fc8c92c98f139 Mon Sep 17 00:00:00 2001 From: cemalyakla <105042441+cemalyakla@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:06:26 +0000 Subject: [PATCH] Runtime: report DXGI format plane counts through CheckFeatureSupport --- docs/d3d12_vulkan.md | 16 ++++--- runtime/shims/d3d12_vulkan_device.cpp | 32 +++++++++++++ runtime/test/d3d12_copy_compare.h | 10 ++-- runtime/test/d3d12_vulkan_device_test.cpp | 57 +++++++++++++++++++++++ 4 files changed, 105 insertions(+), 10 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 75843cef7..ee0cf5a08 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -246,13 +246,12 @@ What is implemented (all of it testable without the game): - packed `R9G9B9E5_SHAREDEXP` textures map to `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` for uploads and sampled views; - the descriptor shader samples `B5G5R5A1_UNORM` through a 2D SRV. Opaque and transparent red check bit 15, and mixed-channel texels check the five-bit color fields; - - `CheckFeatureSupport` for feature levels (up to 12_1), architecture, D3D12 options, GPU virtual-address - support, format support, multisample quality levels, shader model and root signature version. + - `CheckFeatureSupport` for feature levels (up to 12_1), architecture, D3D12 options (`OPTIONS`, + `OPTIONS2`–`5`, `7`–`9`, `11`, `13`–`21`), GPU virtual-address support, format support, format plane counts, + multisample quality levels, shader model and root signature version. `D3D12_OPTIONS8` reports unaligned block textures when the Vulkan logical device enables BC texture compression. - Other queries are logged and refused until the trace shows what the game asks (question 11); - - `CheckFeatureSupport` for feature levels (up to 12_1), architecture, D3D12 options (`OPTIONS` and `OPTIONS2`–`5`, `7`, `9`, `11`, `13`–`21`), GPU virtual-address - support, format support and multisample quality levels. Other queries (including `OPTIONS1`/`6`/`8`/`10`/`12`, `FORMAT_INFO`, `SHADER_MODEL`, and `ROOT_SIGNATURE` left to sibling PRs) are logged and refused until the trace - shows what the game asks (question 11); + Other queries, including `OPTIONS1`, `6`, `10` and `12`, are logged and refused until the trace shows what + the game asks (question 11); - reserved resources and `GetResourceTiling` (section 4.2); - `GetCopyableFootprints` for buffers, 2D textures and volumes, including BC1–7 and the two-plane depth-stencil formats. The test compares every output with the PC runtime's; that comparison found that planar @@ -883,6 +882,11 @@ The trace will enumerate them. `SupportedSampleCountsWithNoOutputs = 0x1f`, `LEONIDA_VK_VALIDATION=1` with `0` validation errors). - `D3D12_FEATURE_FORMAT_SUPPORT`: map to `vkGetPhysicalDeviceFormatProperties`; multisample render-target and resolve bits also require the format's image-usage/sample-count query to succeed. +- `D3D12_FEATURE_FORMAT_INFO`: report the DXGI plane count independently of Vulkan texture support. Both + depth/stencil families (including their view formats), NV12, P010, P016 and NV11 have two planes. The + device test compares counts and HRESULTs with native D3D12 on the same adapter. If an older host rejects the + sampler-feedback formats or `A4B4G4R4_UNORM` with `E_INVALIDARG`, the test still requires the layer to return + `S_OK` and one plane for them. - `D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS`: query `vkGetPhysicalDeviceImageFormatProperties` for the matching colour or depth/stencil attachment usage, plus the transfer and sampled usages used by the resource implementation. Vulkan exposes one standard sample pattern, so a supported sample count reports one quality level (quality index 0); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 12361cc71..1f43f177c 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2108,6 +2108,38 @@ public: va->MaxGPUVirtualAddressBitsPerProcess = 48; return d3d12_trace::result(method, S_OK); } + case D3D12_FEATURE_FORMAT_INFO: { + if (size != sizeof(D3D12_FEATURE_DATA_FORMAT_INFO)) return E_INVALIDARG; + auto* info = static_cast(data); + // Plane count describes the DXGI layout even when the backend cannot create the texture. + switch (info->Format) { + case DXGI_FORMAT_R32G8X24_TYPELESS: + case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: + case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS: + case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT: + case DXGI_FORMAT_R24G8_TYPELESS: + case DXGI_FORMAT_D24_UNORM_S8_UINT: + case DXGI_FORMAT_R24_UNORM_X8_TYPELESS: + case DXGI_FORMAT_X24_TYPELESS_G8_UINT: + case DXGI_FORMAT_NV12: + case DXGI_FORMAT_P010: + case DXGI_FORMAT_P016: + case DXGI_FORMAT_NV11: + info->PlaneCount = 2; + break; + case DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE: + case DXGI_FORMAT_SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE: + case DXGI_FORMAT_A4B4G4R4_UNORM: + info->PlaneCount = 1; + break; + default: + // Native D3D12 accepts UNKNOWN but rejects R1_UNORM and the gaps in the enum. + if (UINT(info->Format) > DXGI_FORMAT_B4G4R4A4_UNORM || info->Format == DXGI_FORMAT_R1_UNORM) + return E_INVALIDARG; + info->PlaneCount = 1; + } + return S_OK; + } case D3D12_FEATURE_FORMAT_SUPPORT: { if (size != sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)) return d3d12_trace::result(method, E_INVALIDARG); auto* fs = static_cast(data); diff --git a/runtime/test/d3d12_copy_compare.h b/runtime/test/d3d12_copy_compare.h index 50175f2c2..19f08a8ef 100644 --- a/runtime/test/d3d12_copy_compare.h +++ b/runtime/test/d3d12_copy_compare.h @@ -38,8 +38,8 @@ struct Ptr { T* operator->() const { return p; } }; -// The host runtime's device for this comparison and a name for the report; false if there is none. -inline bool create_host_impl(Ptr& host, std::string& name) { +// An explicit adapter LUID takes precedence over LEONIDA_HOST_ADAPTER for same-adapter comparisons. +inline bool try_create_host(Ptr& host, std::string& name, const LUID* luid = nullptr) { const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); const HMODULE dxgi = LoadLibraryExA("dxgi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); if (!d3d12 || !dxgi) return false; @@ -54,7 +54,9 @@ inline bool create_host_impl(Ptr& host, std::string& name) { char want[128] = {}; GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", want, sizeof(want)); Ptr adapter; - if (want[0]) { + if (luid) { + if (FAILED(factory->EnumAdapterByLuid(*luid, IID_PPV_ARGS(&adapter.p)))) return false; + } else if (want[0]) { std::string needle(want); std::transform(needle.begin(), needle.end(), needle.begin(), [](unsigned char c) { return char(std::tolower(c)); }); if (needle == "warp") { @@ -98,7 +100,7 @@ inline bool create_host_impl(Ptr& host, std::string& name) { // A host device, or false when there is none (the comparisons are then skipped). When LEONIDA_HOST_ADAPTER names an // adapter that cannot be had, the run fails: a typo must not turn the comparisons into a silent pass. inline bool create_host(Ptr& host, std::string& name) { - if (create_host_impl(host, name)) return true; + if (try_create_host(host, name)) return true; char want[128] = {}; if (GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", want, sizeof(want)) && want[0]) { std::fprintf(stderr, "LEONIDA_HOST_ADAPTER=%s: no such adapter, or no D3D12 device on it\n", want); diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index cc2688c1a..4b9c8c996 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -368,6 +368,54 @@ bool host_has_graphics_command_list7(ID3D12Device* host) { return SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list7.p))); } +int compare_format_info(ID3D12Device* ours, const vktest::Device& vk) { + VkPhysicalDeviceIDProperties id{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES}; + VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id}; + vk.instance_fn("vkGetPhysicalDeviceProperties2")(vk.physical, &properties); + LUID luid{}; + static_assert(sizeof(luid) == sizeof(id.deviceLUID)); + std::memcpy(&luid, id.deviceLUID, sizeof(luid)); + copycmp::Ptr host; + std::string adapter_name; + if (!id.deviceLUIDValid || !copycmp::try_create_host(host, adapter_name, &luid)) { + std::printf("FORMAT_INFO: no native D3D12 device on the Vulkan adapter, skipped comparison\n"); + return 0; + } + + unsigned compared = 0, host_unknown = 0; + int mismatches = 0; + const auto compare = [&](DXGI_FORMAT format, UINT size) { + D3D12_FEATURE_DATA_FORMAT_INFO expected{format, 0xff}, actual = expected; + HRESULT expected_hr = host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &expected, size); + // Some Windows runtimes predate these single-plane formats. Still require the layer to recognize them. + if (expected_hr == E_INVALIDARG && size == sizeof(expected) && + (format == DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE || + format == DXGI_FORMAT_SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE || format == DXGI_FORMAT_A4B4G4R4_UNORM)) { + expected_hr = S_OK; + expected.PlaneCount = 1; + ++host_unknown; + std::printf("FORMAT_INFO format %u: host returned E_INVALIDARG, checking for one plane\n", UINT(format)); + } + const HRESULT our_hr = ours->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &actual, size); + ++compared; + if (our_hr != expected_hr || actual.PlaneCount != expected.PlaneCount || actual.Format != format) { + std::fprintf(stderr, "FORMAT_INFO format %u size %u: HRESULT/planes %08lx/%u, expected %08lx/%u\n", + UINT(format), size, static_cast(our_hr), UINT(actual.PlaneCount), + static_cast(expected_hr), UINT(expected.PlaneCount)); + ++mismatches; + } + }; + constexpr UINT size = sizeof(D3D12_FEATURE_DATA_FORMAT_INFO); + // Include UNKNOWN, holes between format families, and the value after the last named format. + for (UINT format = 0; format <= UINT(DXGI_FORMAT_A4B4G4R4_UNORM) + 1; ++format) + compare(static_cast(format), size); + compare(DXGI_FORMAT_FORCE_UINT, size); + compare(DXGI_FORMAT_D24_UNORM_S8_UINT, size - 1); + std::printf("FORMAT_INFO on %s: %u queries, %u newer formats unknown to host, %d mismatches\n", + adapter_name.c_str(), compared, host_unknown, mismatches); + return mismatches; +} + // GetCopyableFootprints must match the PC runtime's exactly, since applications size and fill upload buffers from // it. Unaligned block-compressed cases are skipped when the host runtime cannot answer them, so the rest still // run. Returns the number of mismatches, or -1 when the host has no D3D12 device to compare with. @@ -1622,6 +1670,15 @@ int main() { EXPECT(va_support.MaxGPUVirtualAddressBitsPerResource == 40); EXPECT(va_support.MaxGPUVirtualAddressBitsPerProcess == 48); + EXPECT(compare_format_info(device.p, d) == 0); + // Use a real oversized buffer and check that rejecting it leaves the output untouched. + D3D12_FEATURE_DATA_FORMAT_INFO format_info[2]{{DXGI_FORMAT_D24_UNORM_S8_UINT, 0xff}}; + EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, nullptr, sizeof(format_info[0])) == E_INVALIDARG); + EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, format_info, sizeof(format_info)) == E_INVALIDARG); + EXPECT(format_info[0].PlaneCount == 0xff); + EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, format_info, sizeof(format_info[0])) == S_OK); + EXPECT(format_info[0].PlaneCount == 2); + D3D12_FEATURE_DATA_FORMAT_SUPPORT format_support{DXGI_FORMAT_R8G8B8A8_UNORM}; EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &format_support, sizeof(format_support)))); EXPECT(format_support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);