From d2968968c60095ef0eaaeaa2d8f0aef8919b267d Mon Sep 17 00:00:00 2001 From: GGandCo <114749110+GGandCo@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:19:08 +0100 Subject: [PATCH 1/2] d3d12: keep compressed image views within Vulkan BC classes --- runtime/CMakeLists.txt | 2 + runtime/shims/d3d12_vulkan_device.cpp | 30 ++++++++++++- runtime/src/gfx10_descriptors.h | 15 ++++++- runtime/test/d3d12_existing_heap_test.cpp | 55 ++++++++++++++++++++++- runtime/test/gfx10_descriptors_test.cpp | 19 ++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 5a32f794ca..b7e1554e07 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2659,6 +2659,8 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_existing_heap_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_existing_heap COMMAND d3d12_existing_heap_test) add_test(NAME d3d12_host_prefix COMMAND d3d12_existing_heap_test --host-prefix-only) + add_test(NAME d3d12_bc_stream_views COMMAND d3d12_existing_heap_test --bc-views-only) + set_tests_properties(d3d12_bc_stream_views PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(d3d12_host_prefix PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(d3d12_existing_heap PROPERTIES SKIP_RETURN_CODE 77) # The same with write watching off: the guarded cases only, the concurrent writers among them. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 36f3556090..20fe765603 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1819,7 +1819,10 @@ struct TextureCreateInfo { TextureCreateInfo(const D3D12_RESOURCE_DESC& desc, const Format& format, bool attachment_usable) { const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D; const bool one_d = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D; - info.flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0; + // Console descriptors can select the numeric twin of a BC image (e.g. sRGB). + // Cross-codec views are rejected separately, even when block sizes match. + info.flags = (format.typeless || leonida::gfx10::bc_format_class(format.format)) + ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0; if (volume && (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT; info.imageType = volume ? VK_IMAGE_TYPE_3D : one_d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D; @@ -5000,6 +5003,10 @@ HRESULT castable_view_formats(const Format& format, DXGI_FORMAT resource_format, return E_INVALIDARG; } const leonida::BlockFormat block = leonida::block_format(cast.format, cast.aspects); + if (!leonida::gfx10::compatible_bc_formats(format.format, cast.format)) { + log_once("castable format belongs to another BC compatibility class:", castable[i]); + return E_INVALIDARG; + } if (own.bytes && block.bytes && (block.bytes != own.bytes || block.block != own.block)) { log_once("castable format is a different texel size than the resource:", castable[i]); return E_INVALIDARG; @@ -12509,7 +12516,26 @@ class Device final if (f != DXGI_FORMAT_UNKNOWN && std::find(formats.begin(), formats.end(), f) == formats.end()) formats.push_back(f); }; - add(DXGI_FORMAT(d.format)); + const DXGI_FORMAT requested = srv.Format == DXGI_FORMAT_UNKNOWN ? DXGI_FORMAT(d.format) : srv.Format; + const VkFormat requested_host = vulkan_format(requested, false).format; + const VkFormat original_host = vulkan_format(DXGI_FORMAT(d.format), false).format; + const bool bc_reinterpret = !leonida::gfx10::compatible_bc_formats(original_host, requested_host); + // A streamed allocation can be reused for a different BC codec. Untiling still follows + // its original block footprint, but Vulkan needs a new image in the requested codec. + // Never put the retired codec into this image's mutable-format list. + if (bc_reinterpret) { + const xg::Format a = xg::format_info(d.format), b = xg::format_info(requested); + if (!a.bytes || a.bytes != b.bytes || a.side != b.side || + !leonida::gfx10::bc_format_class(original_host) || + !leonida::gfx10::bc_format_class(requested_host)) + return refuse_title_view("incompatible compressed view footprint", d, address), nullptr; + } + if (leonida::gfx10::bc_format_class(requested_host)) { + formats.erase(std::remove_if(formats.begin(), formats.end(), [&](DXGI_FORMAT f) { + return !leonida::gfx10::compatible_bc_formats(requested_host, vulkan_format(f, false).format); + }), formats.end()); + } + add(bc_reinterpret ? requested : DXGI_FORMAT(d.format)); if (srv.Format != DXGI_FORMAT_UNKNOWN && srv.Format != DXGI_FORMAT(d.format)) { const xg::Format a = xg::format_info(d.format), b = xg::format_info(srv.Format); if (a.bytes != b.bytes || a.side != b.side) diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h index 219e0a8c5c..8a227ab07e 100644 --- a/runtime/src/gfx10_descriptors.h +++ b/runtime/src/gfx10_descriptors.h @@ -411,6 +411,18 @@ inline constexpr bool is_depth_stencil_format(VkFormat format) { // when no channel-order match exists (R8G8B8A8_UINT viewed as R32_UINT, say). Depth/stencil images are only // compatible with their own format in Vulkan (VUID-VkImageViewCreateInfo-image-01762 / 09594), and colour // images can never be viewed as depth/stencil. VK_FORMAT_UNDEFINED: no compatible format exists. +// Vulkan BC compatibility classes contain only the numeric variants of one codec. +// Equal block footprints alone are insufficient (e.g. BC6H and BC7 are both 16 bytes). +inline constexpr unsigned bc_format_class(VkFormat format) { + return format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC7_SRGB_BLOCK + ? unsigned(format - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2 + 1 : 0; +} + +inline constexpr bool compatible_bc_formats(VkFormat image, VkFormat view) { + const auto a = bc_format_class(image), b = bc_format_class(view); + return (!a && !b) || (a && a == b); +} + inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_t gfx10_format, bool allow_channel_cast = false) { const FormatRow* image = format_row(image_format); @@ -426,7 +438,8 @@ inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_ if (image_format == VK_FORMAT_B8G8R8A8_UNORM && gfx10_format == 136) return image_format; const FormatRow* fallback = nullptr; for (const FormatRow& row : format_table) { - if (row.gfx10 != gfx10_format || is_depth_stencil_format(row.vulkan)) continue; + if (row.gfx10 != gfx10_format || is_depth_stencil_format(row.vulkan) || + !compatible_bc_formats(image_format, row.vulkan)) continue; if (row.channels == image->channels) return row.vulkan; if (!fallback) fallback = &row; } diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 3d65fc7409..6d40b5bac0 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1988,11 +1988,60 @@ void print_cases(const std::vector& cases, const char* who) { } } // namespace +// Synthetic slot-70 reuse across same-footprint BC codecs must create legal native views. +int check_bc_stream_views(ID3D12Device* device, void* pages) { + const char* who = "BC stream view compatibility"; + EXPECT(pages); + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = desc.Height = 8; + desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; + desc.Format = DXGI_FORMAT_BC7_UNORM_SRGB; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + for (const DXGI_FORMAT format : {DXGI_FORMAT_BC7_UNORM_SRGB, DXGI_FORMAT_BC6H_UF16, + DXGI_FORMAT_BC6H_SF16, DXGI_FORMAT_BC7_UNORM}) { + D3D12_SHADER_RESOURCE_VIEW_DESC srv{}; + srv.Format = format; + srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + srv.Texture2D.MipLevels = 1; + std::array resource{}; + std::array view{}; + std::memcpy(resource.data(), &desc, sizeof(desc)); + std::memcpy(view.data(), &srv, sizeof(srv)); + leonida::gfx10::ImageWords slot{}; + EXPECT(rt::d3d12::write_placed_texture_view(device, false, resource.data(), view.data(), + reinterpret_cast(pages), {reinterpret_cast(slot.data())})); + std::uint32_t key[8]; + std::memcpy(key, slot.data(), sizeof(key)); + EXPECT(rt::d3d12::bindless_hash_lookup(device, key)); + } + // Replace the stream-owned image, then let the normal submission path retire its views. + Ref placement; + desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&placement.p)), S_OK); + GpuReader gpu; + EXPECT(!gpu.init(device, 65536)); + Sleep(3100); // the production descriptor retirement grace period is three seconds + ID3D12CommandList* lists[]{gpu.list.p}; + gpu.queue->ExecuteCommandLists(1, lists); + EXPECT_HR(gpu.queue->Signal(gpu.fence.p, 1), S_OK); + EXPECT_HR(gpu.fence->SetEventOnCompletion(1, gpu.done), S_OK); + EXPECT(WaitForSingleObject(gpu.done, 30000) == WAIT_OBJECT_0); + return 0; +} + int main(int argc, char** argv) { const bool prefix_only = argc == 2 && std::strcmp(argv[1], "--host-prefix-only") == 0; - if (argc != 1 && !prefix_only) return 2; + const bool bc_only = argc == 2 && std::strcmp(argv[1], "--bc-views-only") == 0; + if (argc != 1 && !prefix_only && !bc_only) return 2; + if (bc_only) _putenv_s("LEONIDA_TITLE_IMAGE_DEFER", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0"); + const auto free_pages = [](void* p) { if (p) VirtualFree(p, 0, MEM_RELEASE); }; + // Declared before the device: backing outlives its stream trackers and native images. + std::unique_ptr bc_pages( + bc_only ? VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) : nullptr, free_pages); vktest::Device vk; const auto status = vk.initialize(); if (status == vktest::DeviceStatus::unavailable) return 77; @@ -2013,6 +2062,10 @@ int main(int argc, char** argv) { info.bindless_partially_bound = vk.partially_bound; info.robust_image_access = vk.robust_image_access; // a Texture3D UAV is bound only with it if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; + if (bc_only) { + const int result = check_bc_stream_views(ours.p, bc_pages.get()); + return result || vk.validation_errors() ? 1 : 0; + } if (prefix_only) { if (!vk.host_pointer_alignment) return 77; const int result = check_host_prefix_buffers(ours.p); diff --git a/runtime/test/gfx10_descriptors_test.cpp b/runtime/test/gfx10_descriptors_test.cpp index efd0ce4eee..56852f80ff 100644 --- a/runtime/test/gfx10_descriptors_test.cpp +++ b/runtime/test/gfx10_descriptors_test.cpp @@ -228,6 +228,25 @@ int main() { CHECK(vulkan_view_format(VK_FORMAT_R8G8B8A8_UINT, 20, true) == VK_FORMAT_R32_UINT); CHECK(vulkan_view_format(VK_FORMAT_UNDEFINED, 56) == VK_FORMAT_UNDEFINED); + // Equal compressed block sizes and channel orders do not make BC codecs compatible. + for (const bool cast : {false, true}) { + for (const auto& source : format_table) { + if (source.vulkan < VK_FORMAT_BC1_RGB_UNORM_BLOCK || source.vulkan > VK_FORMAT_BC7_SRGB_BLOCK) + continue; + for (const auto& target : format_table) { + const auto resolved = vulkan_view_format(source.vulkan, target.gfx10, cast); + if (resolved == VK_FORMAT_UNDEFINED) continue; + CHECK(resolved >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && resolved <= VK_FORMAT_BC7_SRGB_BLOCK); + CHECK((resolved - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2 == + (source.vulkan - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2); + } + } + CHECK(vulkan_view_format(VK_FORMAT_BC7_UNORM_BLOCK, + format_from_vulkan(VK_FORMAT_BC7_SRGB_BLOCK), cast) == VK_FORMAT_BC7_SRGB_BLOCK); + CHECK(vulkan_view_format(VK_FORMAT_BC6H_UFLOAT_BLOCK, + format_from_vulkan(VK_FORMAT_BC6H_SFLOAT_BLOCK), cast) == VK_FORMAT_BC6H_SFLOAT_BLOCK); + } + // Depth/stencil images are only compatible with their own format, even with allow_channel_cast = true, // and colour images never resolve to a depth/stencil view format. for (const VkFormat ds : {VK_FORMAT_D16_UNORM, VK_FORMAT_X8_D24_UNORM_PACK32, VK_FORMAT_D32_SFLOAT, From c93cb1abbc9a04f5dcd1612d47eba36042ebb391 Mon Sep 17 00:00:00 2001 From: GGandCo <114749110+GGandCo@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:21:26 +0100 Subject: [PATCH 2/2] docs: describe compressed-view constraints and skip unsupported bindless devices --- docs/d3d12_vulkan.md | 8 ++++++++ runtime/test/d3d12_existing_heap_test.cpp | 1 + 2 files changed, 9 insertions(+) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 6df6657bdf..e10d73687a 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -574,6 +574,14 @@ What is implemented (all of it testable without the game): | a castable-format list | accepted on a texture when the formats map to Vulkan formats: the image is mutable and carries a `VkImageFormatListCreateInfo`; a buffer or an unmapped format is `E_INVALIDARG`. A cast whose texel size differs from the resource's is also `E_INVALIDARG` — the host refuses other sizes, and a size-mismatched format list is a Vulkan validation error (`VUID-VkImageCreateInfo-pNext-06722`). The list is kept on the resource, so an SRV whose format is neither the resource format nor in the list leaves a null descriptor. The resource's own format is always accepted and is added to the Vulkan format list when the D3D12 list omits it. A listed same-size cross-family cast, such as `R8G8B8A8_UINT` to `R32_UINT`, is accepted. An RTV in a listed RGBA8 family format on a typed `R8G8B8A8_UNORM` texture clears through the cast, the same reinterpretation a typeless RTV uses; without a list a typed resource still refuses a different-format RTV. `D3D12_OPTIONS12` reports `RelaxedFormatCastingSupported = TRUE` (this layer requires Vulkan 1.3, where the format list is core). `CreatePlacedResource2`'s list reaches its placed image through the same rules (D3D-90) | | `CreateCommandQueue1`'s creator ID | ignored | + Compressed BC views must stay in the same Vulkan compatibility class: equal block byte counts do not make + BC6H and BC7 interchangeable views of one image. Castable lists reject cross-codec entries. Numeric twins + within a codec (such as BC7 UNORM/sRGB) use mutable images. A CPU-filled slot-70 texture requested in another + same-footprint BC codec is recreated in that requested codec, retaining the original console layout for + untile/copy addressing. The replacement does not carry retired codecs in its Vulkan format list. + `gfx10_descriptors` checks format resolution; `d3d12_bc_stream_views` checks native view registration and + retirement with synthetic BC7/BC6H descriptors (run with `LEONIDA_VK_VALIDATION=1` to check Vulkan usage). + `CreatePlacedResource1/2` wrap the placed path above; `2` carries the castable-format list. `CreateHeap1` remains a stub. - `CreateSampler2` as a wrapper over `CreateSampler` (D3D-28, part 2). Without flags it writes the same S#. diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 6d40b5bac0..bc65c5465b 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -2046,6 +2046,7 @@ int main(int argc, char** argv) { const auto status = vk.initialize(); if (status == vktest::DeviceStatus::unavailable) return 77; if (status != vktest::DeviceStatus::ready) return 1; + if (bc_only && !vk.bindless) return 77; int failures = 0; { Ref ours;