From 2e4beefdc25e723b2eca9fde154fb0572b38f6ce Mon Sep 17 00:00:00 2001 From: mrp1no <137657338+mrp1no@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:42:36 +0100 Subject: [PATCH 1/3] d3d12: A8_UNORM render targets through VK_FORMAT_A8_UNORM_KHR (VK_KHR_maintenance5) The retail title's first native pipelines name an A8_UNORM render target (@KkelebekK's RT-5 measurements). The layer stored every A8_UNORM texture as R8_UNORM with a swizzled SRV and refused render targets. Where the GPU has VK_KHR_maintenance5 and can sample VK_FORMAT_A8_UNORM_KHR and blend it as a colour attachment, gta6rt enables the extension and its feature (VulkanDeviceInfo::a8_render_target), and an A8_UNORM texture created with ALLOW_RENDER_TARGET is stored in that format: - its RTVs and the PSO's RTVFormats use it, so draws, blends and clears write the alpha channel (the clear quantizes alpha like the other UNORM formats); - its SRVs view the image in the same format. Vulkan puts A8 in a compatibility class of its own, so it cannot share an image with an R8 view: gfx10's format table gains an 8_UNORM row for it whose one channel X is alpha (channels_a), and the T#'s (0, 0, 0, X) becomes (ZERO, ZERO, ZERO, A) on that view; - CheckFeatureSupport adds RENDER_TARGET and BLENDABLE for A8_UNORM; - the copy paths know its one-byte texel. Other A8_UNORM textures stay R8_UNORM, and devices without the format refuse A8 render targets as before. Depth-stencil and UAV stay refused. Tests (both on an RTX 3060 and an Intel UHD 750, with Vulkan validation): - d3d12_clear_formats: A8_UNORM joins the WARP comparison (was "not exposed"); - d3d12_descriptor_test: format support and creation follow the device; an A8 render target's default and remapped SRVs sample like the R8-backed ones; - d3d12_draw_test: a full-screen draw and two alpha blends into an A8 target; - gfx10_descriptors_test: the A8 row and its view mapping. vk_test_device.h enables maintenance5 like gta6rt, so the whole suite runs with it: ctest gives the same failures as main on both GPUs. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 67950a8d5159912fa9403549a924d92cda9d7db4) --- docs/d3d12_vulkan.md | 8 ++- runtime/shims/d3d12_com.h | 3 + runtime/shims/d3d12_vulkan_device.cpp | 69 +++++++++++++++--- runtime/src/gfx10_descriptors.h | 4 ++ runtime/src/vulkan_replay.h | 9 ++- runtime/test/d3d12_clear_formats_test.cpp | 2 + runtime/test/d3d12_descriptor_test.cpp | 60 ++++++++++++++-- runtime/test/d3d12_draw_test.cpp | 88 +++++++++++++++++++++++ runtime/test/gfx10_descriptors_test.cpp | 21 +++++- runtime/test/vk_test_device.h | 28 ++++++++ 10 files changed, 274 insertions(+), 18 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 5f711aefa0..1eb3c0718b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -453,7 +453,13 @@ What is implemented (all of it testable without the game): are logged and refused until the trace shows what the game asks (question 11); - Single-sampled `A8_UNORM` 2D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose (0, 0, 0, A) before applying the caller's component mapping. Format support includes `MIP` when the Vulkan - image supports multiple levels. Render-target, depth-stencil and UAV creation are unsupported for this format. + image supports multiple levels. Where the GPU has `VK_FORMAT_A8_UNORM_KHR` (`VK_KHR_maintenance5`) and can + sample it and blend it as a colour attachment, a texture created with `ALLOW_RENDER_TARGET` is stored in that + format instead: its RTVs, PSO `RTVFormats` and clears use the alpha channel, its SRVs view it in the same format + (the T# names its one channel X, gfx10's `channels_a`), and format support adds `RENDER_TARGET` and + `BLENDABLE`. Vulkan puts A8 in a compatibility class of its own, so the two kinds of A8 texture cannot share + an image. Without that format, render targets are refused as before; depth-stencil and UAV creation are + unsupported for this format. - reserved resources and `GetResourceTiling` (section 4.2); - query heaps for occlusion, timestamps and pipeline statistics, and `GetTimestampFrequency` on the queue (section 4.6); diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index d0f3b528aa..44b1e90003 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -156,6 +156,9 @@ struct VulkanDeviceInfo { bool fragment_shader_barycentric = false; bool fragment_shader_interlock = false; bool maintenance8 = false; + // VK_KHR_maintenance5, enabled where its VK_FORMAT_A8_UNORM_KHR can be sampled and is a colour attachment the GPU + // can blend: A8_UNORM render targets are then stored in that format (other A8_UNORM textures stay R8_UNORM). + bool a8_render_target = false; // The instance enabled VK_EXT_debug_utils: SetMarker/BeginEvent/EndEvent become debug labels. Otherwise they // do nothing. bool debug_utils = false; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0273f9339b..61845770cb 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -241,6 +241,11 @@ struct Format { // for R9G9B9E5_SHAREDEXP, R16_UINT for B5G5R5A1_UNORM): the format its SRVs, RTVs and T#s use. Its storage view is // `format`, in the uint class; shaders write the packed bits. UNDEFINED for every other texture. VkFormat uint_backed = VK_FORMAT_UNDEFINED; + // An A8_UNORM render target stored as VK_FORMAT_A8_UNORM_KHR (VulkanDeviceInfo::a8_render_target): its RTVs render + // to the alpha channel and its SRVs view it in that format, whose one channel the T# names as X (gfx10's + // channels_a). Other A8_UNORM textures are R8_UNORM images. Vulkan puts A8 in a compatibility class of its own, + // so one image cannot be viewed both ways. + bool a8_as_alpha = false; // A 96-bit R32G32B32 format, held in the R32G32B32A32 image `format` names (ResourceBinding::rgb96). bool rgb96 = false; }; @@ -953,11 +958,21 @@ struct DeviceState { // The format a texture description is created with, D24S8 and R9G9B9E5 UAV fallbacks included. Format texture_format(const D3D12_RESOURCE_DESC& desc) const { if (!xr_bias_compatible(desc)) return {}; - if (desc.Format == DXGI_FORMAT_A8_UNORM && - (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.SampleDesc.Count != 1 || - (desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | - D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)))) - return {}; + if (desc.Format == DXGI_FORMAT_A8_UNORM) { + if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.SampleDesc.Count != 1 || + (desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))) + return {}; + if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) { + if (!info.a8_render_target) { + log_once("A8_UNORM render targets need VK_FORMAT_A8_UNORM_KHR (VK_KHR_maintenance5); format", + desc.Format); + return {}; + } + Format alpha{VK_FORMAT_A8_UNORM_KHR, VK_IMAGE_ASPECT_COLOR_BIT}; + alpha.a8_as_alpha = true; + return alpha; + } + } if (desc.Format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP) { if ((desc.SampleDesc.Count != 1 && (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) || (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))) || @@ -1838,6 +1853,7 @@ class Resource final } bool has_castable_formats() const { return !castable_formats_.empty(); } VkFormat uint_backed() const { return uint_backed_; } // Format::uint_backed + bool a8_as_alpha() const { return a8_as_alpha_; } VkImageAspectFlags aspects() const { return aspects_; } bool rgb96() const { return rgb96_; } // a 96-bit texture in an RGBA32 image std::uint64_t image_address() const { return image_address_; } // what a T# names the texture by @@ -2268,6 +2284,7 @@ class Resource final binding.sampled_in_general = device_.bindless != nullptr; format_ = format.format; uint_backed_ = format.uint_backed; + a8_as_alpha_ = format.a8_as_alpha; aspects_ = format.aspects; image_address_ = device_.next_image_address.fetch_add(256); binding_ = binding; @@ -2615,6 +2632,7 @@ class Resource final std::vector view_formats_; // creation-time castable list; empty means no restriction std::vector castable_formats_; // the same list as the application gave it VkFormat uint_backed_ = VK_FORMAT_UNDEFINED; + bool a8_as_alpha_ = false; std::uint64_t image_address_ = 0; // what a T# names the texture by bool rgb96_ = false; VkImageAspectFlags aspects_ = 0; @@ -3853,7 +3871,9 @@ class PipelineState final graphics_.color_formats.assign(desc.NumRenderTargets, VK_FORMAT_UNDEFINED); for (UINT i = 0; i < desc.NumRenderTargets; ++i) { if (desc.RTVFormats[i] == DXGI_FORMAT_UNKNOWN) continue; - graphics_.color_formats[i] = vulkan_format(desc.RTVFormats[i], false).format; + graphics_.color_formats[i] = desc.RTVFormats[i] == DXGI_FORMAT_A8_UNORM && device_.info.a8_render_target + ? VK_FORMAT_A8_UNORM_KHR + : vulkan_format(desc.RTVFormats[i], false).format; if (graphics_.color_formats[i] == VK_FORMAT_UNDEFINED) { log_once("render-target format not supported yet:", desc.RTVFormats[i]); return E_INVALIDARG; @@ -4762,7 +4782,8 @@ bool image_descriptor(const Resource& resource, const TextureView& view, leonida log_once("incompatible R9G9B9E5 view format:", int(view.format)); return false; } - named = texture_storage_format(view.format, false).format; + // An A8 render target is viewed in its own A8 format (Format::a8_as_alpha), other A8 textures through R8. + named = resource.a8_as_alpha() ? resource_format : texture_storage_format(view.format, false).format; if (g::is_depth_stencil_format(named)) { log_once("depth-stencil view format on a colour texture:", int(view.format)); return false; @@ -6250,7 +6271,13 @@ class Device final const bool typed_bgra8_listed = r && !typed_rgba8 && rgba8_any_order(rd.Format) && r->has_castable_formats(); if (rd.Format == DXGI_FORMAT_A8_UNORM || (desc && desc->Format == DXGI_FORMAT_A8_UNORM)) { - view = {}; + // Only an A8 render target stored as VK_FORMAT_A8_UNORM_KHR takes an RTV, in its own format: the + // attachment view is the image's. + const bool plain_2d = !desc || desc->ViewDimension == D3D12_RTV_DIMENSION_TEXTURE2D || + desc->ViewDimension == D3D12_RTV_DIMENSION_TEXTURE2DARRAY; + if (!r || !r->a8_as_alpha() || rd.Format != DXGI_FORMAT_A8_UNORM || !plain_2d || + (desc && desc->Format != DXGI_FORMAT_UNKNOWN && desc->Format != DXGI_FORMAT_A8_UNORM)) + view = {}; } else if (r && r->uint_backed() == VK_FORMAT_A1R5G5B5_UNORM_PACK16) { // An R16_UINT-backed B5G5R5A1 UAV texture (Format::uint_backed) renders through an A1R5G5B5 view, in // its own format only. @@ -7943,6 +7970,9 @@ class Device final VK_IMAGE_TILING_OPTIMAL, usage, 0, &limits) == VK_SUCCESS && limits.maxMipLevels > 1) fs->Support1 |= D3D12_FORMAT_SUPPORT1_MIP; + // Render targets are stored as VK_FORMAT_A8_UNORM_KHR, which the device checked can blend. + if (state_.info.a8_render_target) + fs->Support1 |= D3D12_FORMAT_SUPPORT1_RENDER_TARGET | D3D12_FORMAT_SUPPORT1_BLENDABLE; } return d3d12_trace::result(method, S_OK); } @@ -9388,6 +9418,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin LEONIDA_INSTANCE(vkGetPhysicalDeviceProperties); LEONIDA_INSTANCE(vkGetPhysicalDeviceQueueFamilyProperties); LEONIDA_INSTANCE(vkGetPhysicalDeviceFeatures2); + LEONIDA_INSTANCE(vkGetPhysicalDeviceFormatProperties); LEONIDA_INSTANCE(vkGetPhysicalDeviceProperties2); LEONIDA_INSTANCE(vkEnumerateDeviceExtensionProperties); LEONIDA_INSTANCE(vkCreateDevice); @@ -9760,6 +9791,25 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin } #endif +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + // VK_KHR_maintenance5 brings VK_FORMAT_A8_UNORM_KHR (VulkanDeviceInfo::a8_render_target). It is enabled only where + // that format can be sampled and is a colour attachment the GPU can blend; nothing else here relies on it. + VkPhysicalDeviceMaintenance5FeaturesKHR maintenance5{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR}; + if (has_device_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) && vkGetPhysicalDeviceFormatProperties) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &maintenance5}; + vkGetPhysicalDeviceFeatures2(info.physical, &query); + VkFormatProperties a8{}; + vkGetPhysicalDeviceFormatProperties(info.physical, VK_FORMAT_A8_UNORM_KHR, &a8); + const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | + VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; + info.a8_render_target = maintenance5.maintenance5 == VK_TRUE && (a8.optimalTilingFeatures & needed) == needed; + if (info.a8_render_target) { + maintenance5.pNext = *v13.next(); + *v13.next() = &maintenance5; + } + } +#endif + #ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME // VK_KHR_shader_fma: OpFmaKHR is a single-rounding multiply-add in SPIR-V (GLSL.std.450 Fma may round twice). // Translated shaders built with `rdna2spirv.py --fma khr` need it (index capability FMAKHR, feature @@ -9909,6 +9959,9 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin if (info.fragment_shader_interlock) device_extensions.push_back(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME); #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME if (info.maintenance8) device_extensions.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME); +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + if (info.a8_render_target) device_extensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); +#endif #endif #ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME if (info.shader_fma) device_extensions.push_back(VK_KHR_SHADER_FMA_EXTENSION_NAME); diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h index b225cddef0..e6fc4df6d4 100644 --- a/runtime/src/gfx10_descriptors.h +++ b/runtime/src/gfx10_descriptors.h @@ -218,6 +218,7 @@ struct FormatRow { }; inline constexpr Swizzle channels_r{select_x, select_zero, select_zero, select_one}; +inline constexpr Swizzle channels_a{select_zero, select_zero, select_zero, select_x}; inline constexpr Swizzle channels_rg{select_x, select_y, select_zero, select_one}; inline constexpr Swizzle channels_rgb{select_x, select_y, select_z, select_one}; inline constexpr Swizzle channels_rgba = identity_swizzle; @@ -227,6 +228,9 @@ inline constexpr Swizzle channels_abgr{select_w, select_z, select_y, select_x}; inline constexpr FormatRow format_table[] = { {1, VK_FORMAT_R8_UNORM, channels_r}, // GFX10_FORMAT_8_UNORM +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + {1, VK_FORMAT_A8_UNORM_KHR, channels_a}, // GFX10_FORMAT_8_UNORM (an A8 render target) +#endif {2, VK_FORMAT_R8_SNORM, channels_r}, // GFX10_FORMAT_8_SNORM {3, VK_FORMAT_R8_USCALED, channels_r}, // GFX10_FORMAT_8_USCALED {4, VK_FORMAT_R8_SSCALED, channels_r}, // GFX10_FORMAT_8_SSCALED diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 889bb71f72..248d341c47 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -466,7 +466,11 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { case VK_FORMAT_R8_UNORM: case VK_FORMAT_R8_SNORM: case VK_FORMAT_R8_UINT: - case VK_FORMAT_R8_SINT: return {1, 1}; + case VK_FORMAT_R8_SINT: +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + case VK_FORMAT_A8_UNORM_KHR: // an A8_UNORM render target (VulkanDeviceInfo::a8_render_target) +#endif + return {1, 1}; case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: case VK_FORMAT_BC4_UNORM_BLOCK: @@ -1156,6 +1160,9 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC case VK_FORMAT_R8G8_UNORM: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_B8G8R8A8_UNORM: is_integer = false; unorm = {255, 255, 255, 255}; break; +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + case VK_FORMAT_A8_UNORM_KHR: is_integer = false; unorm = {0, 0, 0, 255}; break; // alpha only +#endif case VK_FORMAT_R8G8B8A8_SRGB: case VK_FORMAT_B8G8R8A8_SRGB: // the colour channels go through the sRGB curve on the GPU is_integer = false; diff --git a/runtime/test/d3d12_clear_formats_test.cpp b/runtime/test/d3d12_clear_formats_test.cpp index 33088abeec..f8b594a487 100644 --- a/runtime/test/d3d12_clear_formats_test.cpp +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -234,6 +234,7 @@ const std::vector kSint = {{-1, -2, -128, 127}, {-1.5f, -2.5f, 2.5f, 3.5f const ColorCase kColorCases[] = { {"UNORM", "R8_UNORM", DXGI_FORMAT_R8_UNORM, 1, &kUnorm}, + {"UNORM", "A8_UNORM", DXGI_FORMAT_A8_UNORM, 1, &kUnorm}, // the alpha component only {"UNORM", "R8G8_UNORM", DXGI_FORMAT_R8G8_UNORM, 2, &kUnorm}, {"UNORM", "R8G8B8A8_UNORM", DXGI_FORMAT_R8G8B8A8_UNORM, 4, &kUnorm}, {"UNORM", "B8G8R8A8_UNORM", DXGI_FORMAT_B8G8R8A8_UNORM, 4, &kUnorm}, @@ -336,6 +337,7 @@ int main() { info.device = vk.device; info.queue = vk.queue; info.queue_family = vk.family; + info.a8_render_target = vk.a8_render_target; if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; Ref factory; Ref warp; diff --git a/runtime/test/d3d12_descriptor_test.cpp b/runtime/test/d3d12_descriptor_test.cpp index 9d4cf0fbae..3735880951 100644 --- a/runtime/test/d3d12_descriptor_test.cpp +++ b/runtime/test/d3d12_descriptor_test.cpp @@ -333,6 +333,7 @@ int main() { info.device = d.device; info.queue = d.queue; info.queue_family = d.family; + info.a8_render_target = d.a8_render_target; info.texture_compression_bc = d.bc; info.bindless = d.bindless; info.bindless_partially_bound = d.partially_bound; @@ -491,8 +492,10 @@ int main() { EXPECT((support.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE) != 0); EXPECT((support.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_GATHER) != 0); EXPECT((support.Support1 & D3D12_FORMAT_SUPPORT1_MIP) != 0); - EXPECT(!(support.Support1 & (D3D12_FORMAT_SUPPORT1_RENDER_TARGET | D3D12_FORMAT_SUPPORT1_BLENDABLE | - D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)) && !support.Support2); + // Render targets where the device has VK_FORMAT_A8_UNORM_KHR (they are then blendable too); never UAVs. + const bool a8_render_target = (support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET) != 0; + EXPECT(a8_render_target == ((support.Support1 & D3D12_FORMAT_SUPPORT1_BLENDABLE) != 0)); + EXPECT(!(support.Support1 & D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) && !support.Support2); for (UINT count : {1u, 4u}) { D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS quality_levels{DXGI_FORMAT_A8_UNORM, count}; EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &quality_levels, @@ -504,10 +507,12 @@ int main() { auto desc = texture_desc; desc.Format = DXGI_FORMAT_A8_UNORM; desc.Flags = flag; - EXPECT(FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc, - D3D12_RESOURCE_STATE_COMMON, nullptr, - __uuidof(ID3D12Resource), nullptr))); - EXPECT(device->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes == UINT64_MAX); + const bool allowed = flag == D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET && a8_render_target; + // With no output, a valid description is S_FALSE. + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + __uuidof(ID3D12Resource), nullptr)) == allowed); + EXPECT((device->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes == UINT64_MAX) == !allowed); } Ref byte_textures[2]; D3D12_PLACED_SUBRESOURCE_FOOTPRINT byte_placed[2]{}, a8_mip_placed{}; @@ -526,6 +531,17 @@ int main() { } EXPECT(byte_placed[0].Offset == byte_placed[1].Offset && byte_placed[0].Footprint.RowPitch == byte_placed[1].Footprint.RowPitch); + // An A8 render target (VK_FORMAT_A8_UNORM_KHR where the device has it) gets the same bytes, and its SRVs must + // read them as the R8-backed A8 texture's do. Without render-target support, slots 5 and 6 view that one. + Ref a8_target; + if (a8_render_target) { + auto desc = texture_desc; + desc.Format = DXGI_FORMAT_A8_UNORM; + desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&a8_target.p)))); + } for (UINT y = 0; y < size; ++y) { auto* row = static_cast(mapped) + byte_placed[0].Offset + y * byte_placed[0].Footprint.RowPitch; for (UINT x = 0; x < size; ++x) row[x] = std::uint8_t(1 + 3 * (y * size + x)); @@ -1194,7 +1210,7 @@ int main() { } } - constexpr UINT a8_mip_slot = 4, byte_view_count = a8_mip_slot + 1; + constexpr UINT a8_mip_slot = 4, a8_target_slot = 5, byte_view_count = a8_target_slot + 2; D3D12_DESCRIPTOR_HEAP_DESC byte_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, byte_view_count, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; Ref byte_heap; @@ -1224,6 +1240,18 @@ int main() { byte_view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; byte_view.Texture2D.MostDetailedMip = 1; device->CreateShaderResourceView(byte_textures[0].p, &byte_view, byte_slot(a8_mip_slot)); + { + // The A8 render target: a default view (null description) and the remapped one of slot 1. + ID3D12Resource* const a8_source = a8_target.p ? a8_target.p : byte_textures[0].p; + device->CreateShaderResourceView(a8_source, nullptr, byte_slot(a8_target_slot)); + D3D12_SHADER_RESOURCE_VIEW_DESC remapped = byte_view; + remapped.Texture2D.MostDetailedMip = 0; + remapped.Shader4ComponentMapping = D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING( + D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_3, + D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_0, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1, + D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0); + device->CreateShaderResourceView(a8_source, &remapped, byte_slot(a8_target_slot + 1)); + } // D3D-75: plane 1 of a depth-stencil texture is its stencil. The T# is 8_UINT at the texture's own address, // with the eight bits read as G and R, B, A as 0, 0, 1 (D3D12's X24_TYPELESS_G8_UINT / X32_..._G8X24_UINT). @@ -1487,6 +1515,18 @@ int main() { D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); list->ResourceBarrier(1, &ready); } + if (a8_target.p) { + D3D12_TEXTURE_COPY_LOCATION to{}, from{}; + to.pResource = a8_target.p; + to.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + from.pResource = upload.p; + from.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + from.PlacedFootprint = byte_placed[0]; + list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr); + const auto ready = transition(a8_target.p, D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + list->ResourceBarrier(1, &ready); + } EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); // Upload, then the texture into a shader-resource state. Executing the list also brings set 0 up to date. @@ -1976,12 +2016,17 @@ int main() { {value, 0, 0, 255}, {255, value, 255, 0}}; for (UINT k = 0; k < 4; ++k) EXPECT(std::memcmp(byte_results.data() + k * bytes + 4 * i, expected[k], 4) == 0); + // The A8 render target's default and remapped views read as the A8 texture's (slots 0 and 1). + EXPECT(std::memcmp(byte_results.data() + a8_target_slot * bytes + 4 * i, expected[0], 4) == 0); + EXPECT(std::memcmp(byte_results.data() + (a8_target_slot + 1) * bytes + 4 * i, expected[1], 4) == 0); const UINT mip_texel = (i / size / 2) * mip_size + (i % size) / 2; const std::uint8_t mip_expected[] = {0, 0, 0, std::uint8_t(208 + mip_texel)}; EXPECT(std::memcmp(byte_results.data() + a8_mip_slot * bytes + 4 * i, mip_expected, 4) == 0); } std::printf("A8 mip 1: %u samples matched across %u texels\n", texels, mip_size * mip_size); std::printf("A8 and R8 default and remapped views: %u texels matched each\n", texels); + std::printf("A8 render target (%s) default and remapped views: %u texels matched each\n", + a8_target.p ? "VK_FORMAT_A8_UNORM_KHR" : "not supported: the R8-backed texture", texels); // A gather across the center of each 2x2 block uses the A8 view's R-to-A swizzle. // The remapped view of the same image forces alpha to zero. @@ -2887,6 +2932,7 @@ int main() { typeless_heap.reset(); uint_texture.reset(); for (auto& resource : byte_textures) resource.reset(); + a8_target.reset(); byte_heap.reset(); ms_texture.reset(); ms_array.reset(); diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index 36e2f6e37a..d08d39a3de 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -10,6 +10,7 @@ // checks the viewport flip (D3D clip space is y-up) and the front-face winding; // - full-screen blends that read destination alpha into B8G8R8X8 and B8G8R8A8 targets cleared with alpha 0.25: // B8G8R8X8 has no alpha, so destination alpha reads 1 there, as on WARP and AMD's D3D12 driver; +// - an A8_UNORM render target where the device has VK_FORMAT_A8_UNORM_KHR: a draw and two alpha blends; // - draws through RTVs in a format the texture's castable list names, in the other channel order (RGBA8 and BGRA8) // or across B8G8R8X8 and B8G8R8A8: the bytes land in the view's order and encoding. // The shaders are GLSL written to the translator's contract (test/glsl/vs_draw.vert, ps_draw.frag). No game data. @@ -119,6 +120,7 @@ int main() { info.device = d.device; info.queue = d.queue; info.queue_family = d.family; + info.a8_render_target = d.a8_render_target; info.buffer_device_address = true; info.subgroup = d.subgroup; info.shader_dir = library.string(); @@ -856,6 +858,92 @@ int main() { unsigned(std::size(x8_cases))); } + // An A8_UNORM render target (VK_FORMAT_A8_UNORM_KHR, VulkanDeviceInfo::a8_render_target): the full-screen draw + // writes alpha 1, and the alpha blend reads the cleared alpha 0.25 (byte 64) as the destination. + { + D3D12_FEATURE_DATA_FORMAT_SUPPORT a8_support{DXGI_FORMAT_A8_UNORM}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &a8_support, + sizeof(a8_support)))); + if (!(a8_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET)) { + std::printf("A8_UNORM render target: not supported on this device (no VK_FORMAT_A8_UNORM_KHR)\n"); + } else { + struct A8Case { + const char* name; + BOOL blend; + D3D12_BLEND src_alpha, dst_alpha; + int want; + }; + constexpr A8Case a8_cases[] = { + {"no blend", FALSE, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, 255}, + {"alpha ZERO, ONE", TRUE, D3D12_BLEND_ZERO, D3D12_BLEND_ONE, 64}, + {"alpha INV_DEST_ALPHA, ZERO", TRUE, D3D12_BLEND_INV_DEST_ALPHA, D3D12_BLEND_ZERO, 191}, + }; + const D3D12_DESCRIPTOR_HEAP_DESC a8_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref a8_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&a8_heap_desc, IID_PPV_ARGS(&a8_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE a8_rtv{ + SIZE_T(reinterpret_cast(slot(a8_heap.p, 9))(a8_heap.p))}; + D3D12_RESOURCE_DESC a8_desc = rt_desc; + a8_desc.Width = a8_desc.Height = 4; + a8_desc.Format = DXGI_FORMAT_A8_UNORM; + Ref a8_target; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &a8_desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, + IID_PPV_ARGS(&a8_target.p)))); + device->CreateRenderTargetView(a8_target.p, nullptr, a8_rtv); + const FLOAT a8_clear[4] = {0.9f, 0.8f, 0.7f, 0.25f}; // only alpha lands + const D3D12_VIEWPORT a8_viewport{0, 0, 4, 4, 0, 1}; + const D3D12_RECT a8_scissor{0, 0, 4, 4}; + UINT64 a8_fence = 50; // after the B8G8R8X8 block's 41..50, before the step-rate block's 61 + for (const A8Case& c : a8_cases) { + D3D12_GRAPHICS_PIPELINE_STATE_DESC a8_pso_desc = fullscreen_desc; + a8_pso_desc.RTVFormats[0] = DXGI_FORMAT_A8_UNORM; + a8_pso_desc.BlendState.RenderTarget[0] = { + c.blend, FALSE, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, c.src_alpha, + c.dst_alpha, D3D12_BLEND_OP_ADD, D3D12_LOGIC_OP_NOOP, D3D12_COLOR_WRITE_ENABLE_ALL}; + Ref a8_pso; + EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&a8_pso_desc, IID_PPV_ARGS(&a8_pso.p)))); + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, a8_pso.p))); + list->ClearRenderTargetView(a8_rtv, a8_clear, 0, nullptr); + list->SetGraphicsRootSignature(root.p); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->RSSetViewports(1, &a8_viewport); + list->RSSetScissorRects(1, &a8_scissor); + list->OMSetRenderTargets(1, &a8_rtv, FALSE, nullptr); + list->SetGraphicsRootConstantBufferView(1, upload_va + 2048); + list->DrawInstanced(3, 1, 0, 0); + const auto a8_to_copy = transition(a8_target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &a8_to_copy); + D3D12_TEXTURE_COPY_LOCATION a8_dst{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}}; + a8_dst.PlacedFootprint = {0, {DXGI_FORMAT_A8_UNORM, 4, 4, 1, 256}}; + D3D12_TEXTURE_COPY_LOCATION a8_src{a8_target.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}}; + list->CopyTextureRegion(&a8_dst, 0, 0, 0, &a8_src, nullptr); + const auto a8_to_target = transition(a8_target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_RENDER_TARGET); + list->ResourceBarrier(1, &a8_to_target); + EXPECT(list->Close() == S_OK); + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); + ++a8_fence; + EXPECT(SUCCEEDED(queue->Signal(fence.p, a8_fence)) && + SUCCEEDED(fence->SetEventOnCompletion(a8_fence, nullptr))); + const auto* got = static_cast(mapped); + for (UINT y = 0; y < 4; ++y) + for (UINT x = 0; x < 4; ++x) { + const int byte = got[y * 256 + x]; + if (std::abs(byte - c.want) <= 1) continue; + std::fprintf(stderr, "A8 target, %s: texel (%u,%u) got %d, want %d\n", c.name, x, y, + byte, c.want); + return 1; + } + } + std::printf("A8_UNORM render target: clear, draw and %u alpha blends matched\n", + unsigned(std::size(a8_cases))); + } + } + // Instance step rate > 1 (VK_KHR/EXT_vertex_attribute_divisor): two per-instance colours in slot 1: // entry 0 = red, entry 1 = magenta (both preserved by the {1,0,1} tint). With step rate 2: // - Draw 1 (top-left, 2 instances from 0): instances 0 and 1 both read entry 0/2 = 0 (red). diff --git a/runtime/test/gfx10_descriptors_test.cpp b/runtime/test/gfx10_descriptors_test.cpp index 523180ee70..e727053813 100644 --- a/runtime/test/gfx10_descriptors_test.cpp +++ b/runtime/test/gfx10_descriptors_test.cpp @@ -167,7 +167,15 @@ int main() { for (const FormatRow& row : format_table) { CHECK(row.gfx10 >= previous && row.gfx10 > 0 && row.gfx10 < 512); CHECK(seen.insert(row.vulkan).second); - CHECK(valid_swizzle(row.channels) && row.channels[0] != select_zero && row.channels[0] != select_one); + // Every format reads its first channel as red, but A8 (an A8_UNORM render target), whose only channel is alpha. +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + const bool alpha_only = row.vulkan == VK_FORMAT_A8_UNORM_KHR; +#else + const bool alpha_only = false; +#endif + CHECK(valid_swizzle(row.channels) && + (alpha_only ? row.channels == channels_a + : row.channels[0] != select_zero && row.channels[0] != select_one)); CHECK(format_from_vulkan(row.vulkan) == row.gfx10); CHECK(format_swizzle(row.vulkan) == row.channels); if (row.gfx10 != previous) { @@ -176,6 +184,17 @@ int main() { } previous = row.gfx10; } +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + // An A8 render target shares 8_UNORM with R8: an A8 image keeps its own view format, and the T#'s X is its alpha. + CHECK(format_from_vulkan(VK_FORMAT_A8_UNORM_KHR) == 1 && vulkan_format(1) == VK_FORMAT_R8_UNORM); + CHECK(vulkan_view_format(VK_FORMAT_A8_UNORM_KHR, 1) == VK_FORMAT_A8_UNORM_KHR); + { + const VkComponentMapping a8 = vulkan_swizzle(Swizzle{select_zero, select_zero, select_zero, select_x}, + VK_FORMAT_A8_UNORM_KHR); + CHECK(a8.r == VK_COMPONENT_SWIZZLE_ZERO && a8.g == VK_COMPONENT_SWIZZLE_ZERO && + a8.b == VK_COMPONENT_SWIZZLE_ZERO && a8.a == VK_COMPONENT_SWIZZLE_A); + } +#endif CHECK(vulkan_format(56) == VK_FORMAT_R8G8B8A8_UNORM && vulkan_format(130) == VK_FORMAT_R8G8B8A8_SRGB); CHECK(vulkan_format(22) == VK_FORMAT_R32_SFLOAT && format_from_vulkan(VK_FORMAT_D32_SFLOAT) == 22); CHECK(vulkan_format(7) == VK_FORMAT_R16_UNORM && format_from_vulkan(VK_FORMAT_D16_UNORM) == 7); diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 5cccd1836e..114b40f616 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -106,6 +106,9 @@ class Device { bool storage_buffer_8bit = false; // storageBuffer8BitAccess enabled bool conditional_rendering = false; // VK_EXT_conditional_rendering enabled, where available bool host_image_copy = false; // VK_EXT_host_image_copy and hostImageCopy enabled + // VK_KHR_maintenance5 enabled, where VK_FORMAT_A8_UNORM_KHR can be sampled and blended as a colour attachment, as + // gta6rt enables it (VulkanDeviceInfo::a8_render_target). + bool a8_render_target = false; // create(true) only: VK_KHR (else EXT) swapchain_maintenance1 and its feature enabled, with the surface half on the // instance, so a vsync swapchain can switch to IMMEDIATE or FIFO_RELAXED per present. bool swapchain_maintenance1 = false; @@ -499,6 +502,28 @@ class Device { host_copy.pNext = *v13_.next(); // after the conditional rendering features, if any *v13_.next() = &host_copy; } +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + VkPhysicalDeviceMaintenance5FeaturesKHR maintenance5{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR}; + if (std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_KHR_MAINTENANCE_5_EXTENSION_NAME) == 0; + })) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &maintenance5}; + instance_fn("vkGetPhysicalDeviceFeatures2")(physical, &query); + VkFormatProperties a8{}; + instance_fn("vkGetPhysicalDeviceFormatProperties")( + physical, VK_FORMAT_A8_UNORM_KHR, &a8); + const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | + VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | + VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; + a8_render_target = + maintenance5.maintenance5 == VK_TRUE && (a8.optimalTilingFeatures & needed) == needed; + } + if (a8_render_target) { + maintenance5.pNext = *v13_.next(); + *v13_.next() = &maintenance5; + } +#endif #ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME // VK_KHR_shader_fma where the device has it: translated shaders built with --fma khr use OpFmaKHR. @@ -575,6 +600,9 @@ class Device { if (vertex_attribute_divisor) device_extensions.push_back(divisor_extension); if (conditional_rendering) device_extensions.push_back(conditional_extension); if (host_image_copy) device_extensions.push_back(host_image_copy_name); +#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME + if (a8_render_target) device_extensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME); +#endif if (host_pointer_alignment) device_extensions.push_back(VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME); const char* swapchain = "VK_KHR_swapchain"; if (presentation) device_extensions.push_back(swapchain); From 49b750a950409529412d67cb79823e2a4373bc41 Mon Sep 17 00:00:00 2001 From: mrp1no <137657338+mrp1no@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:32:40 +0100 Subject: [PATCH 2/3] d3d12: an A8 render target's castable list takes its own format and refuses the others castable_view_formats() mapped every castable format through vulkan_format(), which has no A8_UNORM case, so CreateCommittedResource3/CreatePlacedResource2 with castable={A8_UNORM} failed with E_INVALIDARG ("castable format not supported yet: 65"). The resource's own format is always viewable (create_texture adds the image's format), so naming A8_UNORM on an A8 texture is now a no-op. An A8 render target's VK_FORMAT_A8_UNORM_KHR image is in a compatibility class of its own, so an R8_UNORM castable entry (same texel size) would have reached its VkImageFormatListCreateInfo. Any format other than A8_UNORM is now refused with E_INVALIDARG before Vulkan. The A8 gate (runtime and vk_test_device.h) also requires TRANSFER_SRC/DST, since every texture is created with transfer usage. d3d12_draw_test runs the A8 clear/draw/blend cases on a second target made through CreateCommittedResource3 with castable={A8_UNORM}, and checks castable={R8_UNORM} returns E_INVALIDARG. Without the shim change it fails with the reported message. Reported by tupacfan04 on #7403. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 18b0bfbaf10626c473f4476878275402ab658174) --- docs/d3d12_vulkan.md | 6 ++-- runtime/shims/d3d12_vulkan_device.cpp | 22 ++++++++++---- runtime/test/d3d12_draw_test.cpp | 43 +++++++++++++++++++++------ runtime/test/vk_test_device.h | 3 +- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 1eb3c0718b..2438a5092b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -454,11 +454,13 @@ What is implemented (all of it testable without the game): - Single-sampled `A8_UNORM` 2D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose (0, 0, 0, A) before applying the caller's component mapping. Format support includes `MIP` when the Vulkan image supports multiple levels. Where the GPU has `VK_FORMAT_A8_UNORM_KHR` (`VK_KHR_maintenance5`) and can - sample it and blend it as a colour attachment, a texture created with `ALLOW_RENDER_TARGET` is stored in that + sample, copy and blend it as a colour attachment, a texture created with `ALLOW_RENDER_TARGET` is stored in that format instead: its RTVs, PSO `RTVFormats` and clears use the alpha channel, its SRVs view it in the same format (the T# names its one channel X, gfx10's `channels_a`), and format support adds `RENDER_TARGET` and `BLENDABLE`. Vulkan puts A8 in a compatibility class of its own, so the two kinds of A8 texture cannot share - an image. Without that format, render targets are refused as before; depth-stencil and UAV creation are + an image. A castable-format list naming `A8_UNORM` itself is a no-op; any other format in it is refused with + `E_INVALIDARG` for such a render target, since no other format can view its image. Without that format, + render targets are refused as before; depth-stencil and UAV creation are unsupported for this format. - reserved resources and `GetResourceTiling` (section 4.2); - query heaps for occlusion, timestamps and pipeline statistics, and `GetTimestampFrequency` on the queue diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 61845770cb..20a1001f30 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1428,12 +1428,20 @@ Format texture_format(const DeviceState& device, const D3D12_RESOURCE_DESC& desc // path, D3D-90's placed one). Each format must map and be the same texel size as the resource's: the host PC // runtime refuses other sizes, and a size-mismatched VkImageFormatListCreateInfo is a Vulkan validation error // (VUID-VkImageCreateInfo-pNext-06722). A nonzero count with a null list is no list. E_INVALIDARG on a bad format. -HRESULT castable_view_formats(const Format& format, UINT32 count, const DXGI_FORMAT* castable, - std::vector& out) { +// An A8_UNORM texture's own format is always viewable (create_texture adds the image's format), so naming it is a +// no-op. An A8 render target's VK_FORMAT_A8_UNORM_KHR image (Format::a8_as_alpha) is in a class of its own: no +// other format can be a view format of it, so any other castable format is refused here, before Vulkan. +HRESULT castable_view_formats(const Format& format, DXGI_FORMAT resource_format, UINT32 count, + const DXGI_FORMAT* castable, std::vector& out) { if (!count || !castable) return S_OK; const bool depth = (format.aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != 0; const leonida::BlockFormat own = leonida::block_format(format.format, format.aspects); for (UINT32 i = 0; i < count; ++i) { + if (resource_format == DXGI_FORMAT_A8_UNORM && castable[i] == DXGI_FORMAT_A8_UNORM) continue; + if (format.a8_as_alpha) { + log_once("castable format of an A8_UNORM render target not supported (only A8_UNORM is):", castable[i]); + return E_INVALIDARG; + } const Format cast = vulkan_format(castable[i], depth); if (cast.format == VK_FORMAT_UNDEFINED) { log_once("castable format not supported yet:", castable[i]); @@ -7419,7 +7427,7 @@ class Device final hr = E_INVALIDARG; } else { std::vector cast_formats; - hr = castable_view_formats(format, castable_count, castable, cast_formats); + hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats); if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats); if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable); } @@ -8584,7 +8592,7 @@ class Device final hr = E_INVALIDARG; } else { std::vector cast_formats; - hr = castable_view_formats(format, castable_count, castable, cast_formats); + hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats); if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats); if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable); } @@ -9793,7 +9801,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin #ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME // VK_KHR_maintenance5 brings VK_FORMAT_A8_UNORM_KHR (VulkanDeviceInfo::a8_render_target). It is enabled only where - // that format can be sampled and is a colour attachment the GPU can blend; nothing else here relies on it. + // that format can be sampled, copied (every texture is a transfer source and destination) and is a colour + // attachment the GPU can blend; nothing else here relies on it. VkPhysicalDeviceMaintenance5FeaturesKHR maintenance5{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR}; if (has_device_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) && vkGetPhysicalDeviceFormatProperties) { VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &maintenance5}; @@ -9801,7 +9810,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin VkFormatProperties a8{}; vkGetPhysicalDeviceFormatProperties(info.physical, VK_FORMAT_A8_UNORM_KHR, &a8); const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | - VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; + VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT | + VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT; info.a8_render_target = maintenance5.maintenance5 == VK_TRUE && (a8.optimalTilingFeatures & needed) == needed; if (info.a8_render_target) { maintenance5.pNext = *v13.next(); diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index d08d39a3de..3faebf2af9 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -10,7 +10,8 @@ // checks the viewport flip (D3D clip space is y-up) and the front-face winding; // - full-screen blends that read destination alpha into B8G8R8X8 and B8G8R8A8 targets cleared with alpha 0.25: // B8G8R8X8 has no alpha, so destination alpha reads 1 there, as on WARP and AMD's D3D12 driver; -// - an A8_UNORM render target where the device has VK_FORMAT_A8_UNORM_KHR: a draw and two alpha blends; +// - an A8_UNORM render target where the device has VK_FORMAT_A8_UNORM_KHR: a draw and two alpha blends, also on +// one created with A8_UNORM as its castable list (an R8_UNORM list is refused); // - draws through RTVs in a format the texture's castable list names, in the other channel order (RGBA8 and BGRA8) // or across B8G8R8X8 and B8G8R8A8: the bytes land in the view's order and encoding. // The shaders are GLSL written to the translator's contract (test/glsl/vs_draw.vert, ps_draw.frag). No game data. @@ -891,12 +892,35 @@ int main() { EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &a8_desc, D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&a8_target.p)))); - device->CreateRenderTargetView(a8_target.p, nullptr, a8_rtv); + // The same target through CreateCommittedResource3 with its own format as the castable list. An R8_UNORM + // list is refused: no other format can be a view format of a VK_FORMAT_A8_UNORM_KHR image. + Ref a8_inner10; + EXPECT(SUCCEEDED(inner->QueryInterface(IID_PPV_ARGS(&a8_inner10.p)))); + D3D12_RESOURCE_DESC1 a8_desc1{}; + a8_desc1.Dimension = a8_desc.Dimension; + a8_desc1.Width = a8_desc.Width; + a8_desc1.Height = a8_desc.Height; + a8_desc1.DepthOrArraySize = a8_desc.DepthOrArraySize; + a8_desc1.MipLevels = a8_desc.MipLevels; + a8_desc1.Format = a8_desc.Format; + a8_desc1.SampleDesc = a8_desc.SampleDesc; + a8_desc1.Layout = a8_desc.Layout; + a8_desc1.Flags = a8_desc.Flags; + const DXGI_FORMAT a8_own = DXGI_FORMAT_A8_UNORM, a8_r8 = DXGI_FORMAT_R8_UNORM; + Ref a8_cast_target, a8_r8_target; + EXPECT(SUCCEEDED(a8_inner10->CreateCommittedResource3(&default_heap, D3D12_HEAP_FLAG_NONE, &a8_desc1, + D3D12_BARRIER_LAYOUT_RENDER_TARGET, nullptr, nullptr, 1, &a8_own, IID_PPV_ARGS(&a8_cast_target.p)))); + EXPECT(a8_inner10->CreateCommittedResource3(&default_heap, D3D12_HEAP_FLAG_NONE, &a8_desc1, + D3D12_BARRIER_LAYOUT_RENDER_TARGET, nullptr, nullptr, 1, &a8_r8, IID_PPV_ARGS(&a8_r8_target.p)) == + E_INVALIDARG); + EXPECT(!a8_r8_target.p); const FLOAT a8_clear[4] = {0.9f, 0.8f, 0.7f, 0.25f}; // only alpha lands const D3D12_VIEWPORT a8_viewport{0, 0, 4, 4, 0, 1}; const D3D12_RECT a8_scissor{0, 0, 4, 4}; - UINT64 a8_fence = 50; // after the B8G8R8X8 block's 41..50, before the step-rate block's 61 + UINT64 a8_fence = 50; // after the B8G8R8X8 block's 41..50; 51..56, before the step-rate block's 61 + for (ID3D12Resource* const a8_resource : {a8_target.p, a8_cast_target.p}) for (const A8Case& c : a8_cases) { + device->CreateRenderTargetView(a8_resource, nullptr, a8_rtv); D3D12_GRAPHICS_PIPELINE_STATE_DESC a8_pso_desc = fullscreen_desc; a8_pso_desc.RTVFormats[0] = DXGI_FORMAT_A8_UNORM; a8_pso_desc.BlendState.RenderTarget[0] = { @@ -913,14 +937,14 @@ int main() { list->OMSetRenderTargets(1, &a8_rtv, FALSE, nullptr); list->SetGraphicsRootConstantBufferView(1, upload_va + 2048); list->DrawInstanced(3, 1, 0, 0); - const auto a8_to_copy = transition(a8_target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + const auto a8_to_copy = transition(a8_resource, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); list->ResourceBarrier(1, &a8_to_copy); D3D12_TEXTURE_COPY_LOCATION a8_dst{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}}; a8_dst.PlacedFootprint = {0, {DXGI_FORMAT_A8_UNORM, 4, 4, 1, 256}}; - D3D12_TEXTURE_COPY_LOCATION a8_src{a8_target.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}}; + D3D12_TEXTURE_COPY_LOCATION a8_src{a8_resource, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}}; list->CopyTextureRegion(&a8_dst, 0, 0, 0, &a8_src, nullptr); - const auto a8_to_target = transition(a8_target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, + const auto a8_to_target = transition(a8_resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); list->ResourceBarrier(1, &a8_to_target); EXPECT(list->Close() == S_OK); @@ -934,13 +958,14 @@ int main() { for (UINT x = 0; x < 4; ++x) { const int byte = got[y * 256 + x]; if (std::abs(byte - c.want) <= 1) continue; - std::fprintf(stderr, "A8 target, %s: texel (%u,%u) got %d, want %d\n", c.name, x, y, + std::fprintf(stderr, "A8 target%s, %s: texel (%u,%u) got %d, want %d\n", + a8_resource == a8_cast_target.p ? " (castable list)" : "", c.name, x, y, byte, c.want); return 1; } } - std::printf("A8_UNORM render target: clear, draw and %u alpha blends matched\n", - unsigned(std::size(a8_cases))); + std::printf("A8_UNORM render target: clear, draw and %u alpha blends matched, also with an A8_UNORM " + "castable list (R8_UNORM refused)\n", unsigned(std::size(a8_cases))); } } diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 114b40f616..dbe48896e3 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -515,7 +515,8 @@ class Device { physical, VK_FORMAT_A8_UNORM_KHR, &a8); const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | - VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT; + VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT | + VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT; a8_render_target = maintenance5.maintenance5 == VK_TRUE && (a8.optimalTilingFeatures & needed) == needed; } From 348dff3d3555dcaee7d7ee524cf84bc18cffddea Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Mon, 23 Nov 2026 18:30:31 +0100 Subject: [PATCH 3/3] d3d12: A8_UNORM textures in 1D and 3D, as the host makes them texture_format refused every A8_UNORM texture that was not 2D, so GetResourceAllocationInfo answered UINT64_MAX for a 1D one (a title's boot sizes a 128-texel 1D A8_UNORM texture and asserts on that size). The host accepts A8_UNORM in 1D, 2D and 3D (the gap is listed by #4533's host sweep). They are R8_UNORM images like the 2D ones; the SRV's alpha-only swizzle does not depend on the dimension. Format support now names TEXTURE1D and TEXTURE3D too. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 952be9b046189b2c365a5be7dcece2d8c09471e4) Adapted to main by Mumogg: resolved on top of the A8 render-target commits as the original integration merge (a5c96e0dd) did: a 2D render target stays VK_FORMAT_A8_UNORM_KHR, and every other A8 texture, in any dimension, is an R8_UNORM image. --- docs/d3d12_vulkan.md | 2 +- runtime/CMakeLists.txt | 7 + runtime/shims/d3d12_vulkan_device.cpp | 8 +- runtime/test/d3d12_a8_dimensions_test.cpp | 233 ++++++++++++++++++++++ 4 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 runtime/test/d3d12_a8_dimensions_test.cpp diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 2438a5092b..cca21d0a09 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -451,7 +451,7 @@ What is implemented (all of it testable without the game): enables BC texture compression, and `D3D12_OPTIONS12` reports `RelaxedFormatCastingSupported = TRUE`, so a committed or placed texture accepts a castable-format list. Other queries (including `OPTIONS6` and `OPTIONS10`) are logged and refused until the trace shows what the game asks (question 11); - - Single-sampled `A8_UNORM` 2D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose + - Single-sampled `A8_UNORM` 1D, 2D and 3D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose (0, 0, 0, A) before applying the caller's component mapping. Format support includes `MIP` when the Vulkan image supports multiple levels. Where the GPU has `VK_FORMAT_A8_UNORM_KHR` (`VK_KHR_maintenance5`) and can sample, copy and blend it as a colour attachment, a texture created with `ALLOW_RENDER_TARGET` is stored in that diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 64e346f40a..7c44d65341 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3077,6 +3077,13 @@ if(TARGET Vulkan::Headers) add_test(NAME d3d12_clear_formats COMMAND d3d12_clear_formats_test) set_tests_properties(d3d12_clear_formats PROPERTIES SKIP_RETURN_CODE 77) + add_executable(d3d12_a8_dimensions_test test/d3d12_a8_dimensions_test.cpp) + target_link_libraries(d3d12_a8_dimensions_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) + target_compile_definitions(d3d12_a8_dimensions_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_a8_dimensions_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_a8_dimensions COMMAND d3d12_a8_dimensions_test) + set_tests_properties(d3d12_a8_dimensions PROPERTIES SKIP_RETURN_CODE 77) + # SetDescriptorHeaps validation, Reset/ClearState/bundle parity, lifetime pinning, direct-index flags. add_executable(d3d12_set_descriptor_heaps_test test/d3d12_set_descriptor_heaps_test.cpp) target_link_libraries(d3d12_set_descriptor_heaps_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 20a1001f30..48beb4cfce 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -958,11 +958,14 @@ struct DeviceState { // The format a texture description is created with, D24S8 and R9G9B9E5 UAV fallbacks included. Format texture_format(const D3D12_RESOURCE_DESC& desc) const { if (!xr_bias_compatible(desc)) return {}; + // A8_UNORM: a 2D render target is VK_FORMAT_A8_UNORM_KHR; any other A8 texture, in 1D, 2D or 3D, is an + // R8_UNORM image, as the host makes all three (a title's boot sizes a 1D one). if (desc.Format == DXGI_FORMAT_A8_UNORM) { - if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.SampleDesc.Count != 1 || + if (desc.SampleDesc.Count != 1 || (desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))) return {}; if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) { + if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return {}; if (!info.a8_render_target) { log_once("A8_UNORM render targets need VK_FORMAT_A8_UNORM_KHR (VK_KHR_maintenance5); format", desc.Format); @@ -7966,7 +7969,8 @@ class Device final if (fs->Format == DXGI_FORMAT_A8_UNORM) { if (optimal & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { - fs->Support1 = D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE | + fs->Support1 = D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TEXTURE2D | + D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE | D3D12_FORMAT_SUPPORT1_SHADER_LOAD; if (optimal & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) fs->Support1 |= D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE | D3D12_FORMAT_SUPPORT1_SHADER_GATHER; diff --git a/runtime/test/d3d12_a8_dimensions_test.cpp b/runtime/test/d3d12_a8_dimensions_test.cpp new file mode 100644 index 0000000000..c421bed89c --- /dev/null +++ b/runtime/test/d3d12_a8_dimensions_test.cpp @@ -0,0 +1,233 @@ +// DXGI_FORMAT_A8_UNORM textures in 1D and 3D. The host makes them (a title's boot sizes a 128-texel 1D one through +// GetResourceAllocationInfo); the layer refused every A8_UNORM texture that was not 2D. They are R8_UNORM images, like +// the 2D ones, and their SRVs read (0, 0, 0, A). +// +// Exercises, for a 1D (128) and a 3D (8x8x4) texture: +// 1. CheckFeatureSupport advertises TEXTURE1D and TEXTURE3D for A8_UNORM +// 2. GetResourceAllocationInfo gives a size and CreateCommittedResource makes the texture +// 3. Bytes uploaded with CopyTextureRegion come back unchanged +// 4. The default SRV is a T# of the right image type, in R8_UNORM's GFX10 format, with the swizzle (0, 0, 0, X) +// 5. Negative controls: unordered access and depth-stencil are still refused in both dimensions +// +// Synthetic data only; no game files. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation. +#include "vk_test_device.h" +#include "../shims/d3d12_com.h" +#include "../src/gfx10_descriptors.h" + +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +namespace { + +namespace g = leonida::gfx10; + +template +struct Ref { + T* p = nullptr; + ~Ref() { reset(); } + void reset() { + if (p) p->Release(); + p = nullptr; + } + T* operator->() const { return p; } +}; + +D3D12_RESOURCE_DESC a8_desc(D3D12_RESOURCE_DIMENSION dimension, UINT64 width, UINT height, UINT16 depth, + D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) { + D3D12_RESOURCE_DESC d{}; + d.Dimension = dimension; + d.Width = width; + d.Height = height; + d.DepthOrArraySize = depth; + d.MipLevels = 1; + d.Format = DXGI_FORMAT_A8_UNORM; + d.SampleDesc = {1, 0}; + d.Flags = flags; + return d; +} + +D3D12_RESOURCE_DESC buf_desc(UINT64 bytes) { + D3D12_RESOURCE_DESC d{}; + d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + d.Width = bytes; + d.Height = 1; + d.DepthOrArraySize = 1; + d.MipLevels = 1; + d.SampleDesc.Count = 1; + d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + return d; +} + +D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after) { + D3D12_RESOURCE_BARRIER b{}; + b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + b.Transition = {resource, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; + return b; +} + +std::uint8_t texel(UINT x, UINT y, UINT z) { return std::uint8_t(1 + x + 7 * y + 61 * z); } + +} // namespace + +int main() { + vktest::Device d; + const auto status = d.initialize(); + if (status == vktest::DeviceStatus::unavailable) return 77; + if (status != vktest::DeviceStatus::ready) return 1; + + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = d.instance_proc; + info.instance = d.instance; + info.physical = d.physical; + info.device = d.device; + info.queue = d.queue; + info.queue_family = d.family; + info.bindless = d.bindless; + info.bindless_partially_bound = d.partially_bound; + info.image_view_min_lod = d.image_view_min_lod; + info.host_image_copy = d.host_image_copy; + Ref device; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p)))); + + // 1. Format support names both dimensions, as the host's does. + D3D12_FEATURE_DATA_FORMAT_SUPPORT support{DXGI_FORMAT_A8_UNORM}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support)))); + EXPECT((support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE1D) && (support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D) && + (support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE3D)); + + Ref queue; + const D3D12_COMMAND_QUEUE_DESC qd{D3D12_COMMAND_LIST_TYPE_DIRECT}; + EXPECT(SUCCEEDED(device->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p)))); + Ref alloc; + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p)))); + Ref fence; + EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + Ref cpu_heap; + const D3D12_DESCRIPTOR_HEAP_DESC cpu_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE}; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&cpu_desc, IID_PPV_ARGS(&cpu_heap.p)))); + const UINT step = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + EXPECT(step >= sizeof(g::ImageWords)); + + const D3D12_HEAP_PROPERTIES def_heap{D3D12_HEAP_TYPE_DEFAULT}; + const D3D12_HEAP_PROPERTIES up_heap{D3D12_HEAP_TYPE_UPLOAD}; + const D3D12_HEAP_PROPERTIES rb_heap{D3D12_HEAP_TYPE_READBACK}; + const std::uint32_t r8_code = g::format_from_vulkan(VK_FORMAT_R8_UNORM); + EXPECT(r8_code != 0); + + struct Case { + const char* name; + D3D12_RESOURCE_DESC desc; + g::ImageType type; + }; + const Case cases[] = { + {"1D", a8_desc(D3D12_RESOURCE_DIMENSION_TEXTURE1D, 128, 1, 1), g::ImageType::image_1d}, + {"3D", a8_desc(D3D12_RESOURCE_DIMENSION_TEXTURE3D, 8, 8, 4), g::ImageType::image_3d}, + }; + UINT64 fence_value = 0; + for (UINT c = 0; c < 2; ++c) { + const Case& k = cases[c]; + const UINT width = UINT(k.desc.Width), height = k.desc.Height; + const UINT depth = k.desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? k.desc.DepthOrArraySize : 1u; + + // 5. Negative controls first: the flags A8_UNORM never takes stay refused in this dimension. + for (auto flag : {D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL}) { + const auto refused = a8_desc(k.desc.Dimension, k.desc.Width, k.desc.Height, k.desc.DepthOrArraySize, flag); + EXPECT(device->GetResourceAllocationInfo(0, 1, &refused).SizeInBytes == UINT64_MAX); + } + + // 2. A size and a texture. + const auto alloc_info = device->GetResourceAllocationInfo(0, 1, &k.desc); + EXPECT(alloc_info.SizeInBytes != UINT64_MAX && alloc_info.SizeInBytes >= UINT64(width) * height * depth); + Ref tex; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&def_heap, D3D12_HEAP_FLAG_NONE, &k.desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&tex.p)))); + + // 3. Upload, copy back, compare. + D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{}; + UINT64 total = 0; + device->GetCopyableFootprints(&k.desc, 0, 1, 0, &fp, nullptr, nullptr, &total); + EXPECT(fp.Footprint.Format == DXGI_FORMAT_A8_UNORM && fp.Footprint.Width == width && + fp.Footprint.Height == height && fp.Footprint.Depth == depth && total != UINT64_MAX); + const auto staging_desc = buf_desc(total); + Ref upload, readback; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&up_heap, D3D12_HEAP_FLAG_NONE, &staging_desc, + D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, + IID_PPV_ARGS(&upload.p)))); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&rb_heap, D3D12_HEAP_FLAG_NONE, &staging_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&readback.p)))); + const UINT pitch = fp.Footprint.RowPitch; + void* mapped = nullptr; + EXPECT(SUCCEEDED(upload->Map(0, nullptr, &mapped))); + std::memset(mapped, 0, size_t(total)); + for (UINT z = 0; z < depth; ++z) + for (UINT y = 0; y < height; ++y) + for (UINT x = 0; x < width; ++x) + static_cast(mapped)[size_t(z) * pitch * height + size_t(y) * pitch + x] = + texel(x, y, z); + upload->Unmap(0, nullptr); + + Ref list; + EXPECT(SUCCEEDED(alloc->Reset())); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, nullptr, + IID_PPV_ARGS(&list.p)))); + D3D12_TEXTURE_COPY_LOCATION placed_up{upload.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + placed_up.PlacedFootprint = fp; + D3D12_TEXTURE_COPY_LOCATION sub{tex.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + sub.SubresourceIndex = 0; + list->CopyTextureRegion(&sub, 0, 0, 0, &placed_up, nullptr); + const auto to_src = transition(tex.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_src); + D3D12_TEXTURE_COPY_LOCATION placed_rb{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + placed_rb.PlacedFootprint = fp; + list->CopyTextureRegion(&placed_rb, 0, 0, 0, &sub, nullptr); + EXPECT(SUCCEEDED(list->Close())); + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); + EXPECT(SUCCEEDED(queue->Signal(fence.p, ++fence_value))); + HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(event != nullptr); + EXPECT(SUCCEEDED(fence->SetEventOnCompletion(fence_value, event))); + const DWORD waited = WaitForSingleObject(event, 10000); + CloseHandle(event); + EXPECT(waited == WAIT_OBJECT_0); + + void* back = nullptr; + const D3D12_RANGE read{0, size_t(total)}; + EXPECT(SUCCEEDED(readback->Map(0, &read, &back))); + UINT mismatches = 0; + for (UINT z = 0; z < depth; ++z) + for (UINT y = 0; y < height; ++y) + for (UINT x = 0; x < width; ++x) + mismatches += static_cast(back)[size_t(z) * pitch * height + + size_t(y) * pitch + x] != texel(x, y, z); + readback->Unmap(0, nullptr); + if (mismatches) std::fprintf(stderr, "%s: %u of %u texels differ\n", k.name, mismatches, width * height * depth); + EXPECT(mismatches == 0); + + // 4. The default SRV: the image type, R8_UNORM's format and the alpha-only swizzle. + D3D12_CPU_DESCRIPTOR_HANDLE handle = cpu_heap->GetCPUDescriptorHandleForHeapStart(); + handle.ptr += SIZE_T(c) * step; + std::memset(reinterpret_cast(handle.ptr), 0x77, step); + device->CreateShaderResourceView(tex.p, nullptr, handle); + g::ImageWords words{}; + std::memcpy(words.data(), reinterpret_cast(handle.ptr), sizeof(words)); + g::Image image{}; + EXPECT(g::is_image(words) && g::decode(words, image)); + EXPECT(image.type == k.type && image.width == width && image.format == r8_code); + EXPECT(k.type != g::ImageType::image_3d || (image.height == height && image.depth == depth)); + const g::Swizzle alpha_only{g::select_zero, g::select_zero, g::select_zero, g::select_x}; + EXPECT(image.swizzle == alpha_only); + std::printf("A8_UNORM %s: sized %llu B, %u texels round-trip, SRV reads (0, 0, 0, A)\n", k.name, + static_cast(alloc_info.SizeInBytes), width * height * depth); + } + std::printf("d3d12_a8_dimensions: PASS\n"); + return 0; +}