Skip to content

d3d12: emulate R9G9B9E5_SHAREDEXP UAV textures with mutable R32_UINT - #1287

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
LocranKing:feat/r9g9b9e5-uav-emulation
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
LocranKing:feat/r9g9b9e5-uav-emulation

Conversation

@LocranKing

Copy link
Copy Markdown
Contributor

Emulates DXGI_FORMAT_R9G9B9E5_SHAREDEXP textures created with D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS on Vulkan GPUs where VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 lacks native VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT (documented in docs/d3d12_vulkan.md Appendix A.3 and measured in #864).

Claim: #1006 (comment)

AI-assisted. No game files (all committed tests are synthetic; optional local fsr_dispatch_test verification reports outcomes only, no game data).

Root cause & negative control on unmodified main (d3841c0 / e7584c52)

On an NVIDIA RTX 3060 Ti (driver 621.01, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1):

  • VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 reports optimalTilingFeatures = 0x0001d401 (SAMPLED=1, FILTER=1, STORAGE=0, COLOR_ATT=0) and vkGetPhysicalDeviceImageFormatProperties2(2D, STORAGE) returns VK_ERROR_FORMAT_NOT_SUPPORTED (-11).
  • Before this PR on create_vulkan_device_standalone:
    • CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, DXGI_FORMAT_R9G9B9E5_SHAREDEXP) reported TYPED_UNORDERED_ACCESS_VIEW = 0, UAV_TYPED_STORE = 0, UAV_TYPED_LOAD = 0.
    • CreateCommittedResource(1920x1080, R9G9B9E5_SHAREDEXP, ALLOW_UNORDERED_ACCESS) returned E_INVALIDARG (0x80070057).
    • GetResourceAllocationInfo returned SizeInBytes = UINT64_MAX.
    • CreateReservedResource returned E_INVALIDARG (0x80070057).
    • leonida::uav_clear::encode(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, ...) returned false.

What changed

  • runtime/shims/d3d12_vulkan_device.cpp:
    • Probe once at device creation (probe_r9g9b9e5_uav_emulation) whether VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 lacks native STORAGE_IMAGE while vkGetPhysicalDeviceImageFormatProperties2 accepts a 2D VK_FORMAT_R32_UINT image with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT and VkImageFormatListCreateInfo {VK_FORMAT_R32_UINT, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32}.
    • Update check_image_support to use vkGetPhysicalDeviceImageFormatProperties2 forwarding create.pNext (so VkImageFormatListCreateInfo is checked accurately by both CreateCommittedResource, CreateReservedResource, and GetResourceAllocationInfo), sharing TextureCreateInfo across creation and allocation sizing.
    • In texture_format: keep non-UAV R9G9B9E5_SHAREDEXP textures on native E5B9G9R9; refuse ALLOW_RENDER_TARGET, ALLOW_DEPTH_STENCIL, DENY_SHADER_RESOURCE, and MSAA on R9G9B9E5_SHAREDEXP (matching D3D12 / preventing the backing R32_UINT's COLOR_ATTACHMENT_BIT from exposing an invalid RT); back ALLOW_UNORDERED_ACCESS with mutable VK_FORMAT_R32_UINT when emulated. Also guard CreateRenderTargetView to write a null RTV for R9G9B9E5_SHAREDEXP.
    • In CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT): advertise D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW, D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE (and UAV_TYPED_LOAD when R32_UINT has storage atomics) while keeping RENDER_TARGET off.
    • In image_descriptor and DeviceState::bind_image: keep the logical format VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 for GFX10 T# encoding (FORMAT 132, channels_rgb) and reject incompatible view casts in both directions (preserving strict gfx10::vulkan_view_format). In set 0, create entry.sampled as VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 with VkImageViewUsageCreateInfo{VK_IMAGE_USAGE_SAMPLED_BIT} (chaining VkImageViewMinLodCreateInfoEXT when active) and entry.storage as VK_FORMAT_R32_UINT with VkImageViewUsageCreateInfo{VK_IMAGE_USAGE_STORAGE_BIT} bound in views.as_uint and views.r32ui (never views.as_float, avoiding VUID-vkCmdDispatch-OpTypeImage-07028).
  • runtime/src/vulkan_backend.h:
    • Chain VkImageViewUsageCreateInfo{VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT} on color attachment views created in VulkanContext::view() for images with color_attachment (used by rectangle UAV clears via vkCmdClearAttachments).
  • runtime/src/uav_clear.h:
    • Add VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 (Kind::shared_exp): ClearUnorderedAccessViewFloat packs RGB9E5 with round-half-up (clamping negative/NaN to 0 and overflow/+Inf to 65408.0, bumping the exponent when max_m == 512), ClearUnorderedAccessViewUint writes 0, and decode unpacks float RGB for native-storage GPUs (while emulated R32_UINT-backed images decode through R32_UINT directly). Verified bit-for-bit against Windows WARP across 37 float vectors, 11 uint vectors, and partial rectangles.
  • Tests & Docs:
    • Added runtime/test/d3d12_r9g9b9e5_uav_test.cpp + runtime/test/glsl/cs_r9g9b9e5_uav.comp (registered in runtime/CMakeLists.txt, skips with exit 77 if no Vulkan GPU): format support, 1D/2D 1920x1080/2D-array+mips/3D creation, GetResourceAllocationInfo (4 KiB & 64 KiB), CreateReservedResource + GetResourceTiling, flag & cast refusals, WARP differential UAV clears, cross-backing CopyResource/CopyTextureRegion + WriteToSubresource/ReadFromSubresource, and a set-0 compute round-trip (write packed RGB9E5 via R32_UINT bindings 9 & 4 → sample decoded HDR float RGB via E5B9G9R9 binding 0).
    • Extended runtime/test/d3d12_uav_clear_test.cpp with pinned RGB9E5 conversion checks and GPU clear cases.
    • Added optional LEONIDA_FSR_DEVICE=vulkan to runtime/test/fsr_dispatch_test.cpp (mirroring LEONIDA_BVH_DEVICE=vulkan in bvh_builder_test.cpp).
    • Updated docs/d3d12_vulkan.md (implemented summary and Appendix A.3).

Verification

Environment: Windows 11 Build 26200 x64, NVIDIA GeForce RTX 3060 Ti (driver 621.01, Vulkan 1.4.357), Vulkan SDK 1.4.363.0, MSVC 14.44.35207, VK_LOADER_LAYERS_DISABLE=~implicit~, LEONIDA_VK_VALIDATION=1, commit 48496b96 (rebased on e7584c52).

  • Zero /W4 compiler warnings across all modified and new targets.
  • ctest --test-dir runtime/build --output-on-failure: 62/62 passed (1 disabled: gameinput without SDL3), including d3d12_r9g9b9e5_uav, d3d12_uav_clear, d3d12_descriptor, d3d12_vulkan_device, d3d12_copy_xfmt, replay_uav_clear_offset — 0 Vulkan validation errors (including synchronization validation).
  • d3d12_r9g9b9e5_uav_test: 6/6 WARP differential UAV clear sequences bit-exact, 16/16 UAV↔non-UAV copy texels matched, 16/16 set-0 R32_UINT storage → E5B9G9R9 sampled HDR texels matched, 0 validation errors.
  • Optional local outcome-only check (LEONIDA_FSR_DEVICE=vulkan LEONIDA_FSR_NO_STANDIN=1 fsr_dispatch_test.exe on 1.0.2.1): ffxCreateContext(backend 0x1000002, version 4.1.1) -> OK (0) and ffxDestroyContext -> OK (0) with 0 stand-ins, fsr_dispatch_test: OK (0 failures), 0 validation errors.
  • python -m compileall -q make_game.py tools tests: exit 0.

Open / out of scope

@Vonsek

Vonsek commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Two reproducible test issues on 48496b9, using the source-built fixture with Vulkan validation on Windows.

  1. On AMD Radeon Graphics (1002:13c0, Vulkan 1.4.315), the unmodified executable exits 1 at line 399. E5B9G9R9 exposes both native storage and color-attachment support. The shim reports Support1 0x0264c3f0 and Support2 0x80. With only that assertion relaxed, the native clear/sampling path passes all six WARP clear comparisons, all 16 copy texels and all 16 sampled texels, with zero validation errors. Please reconcile the unconditional RENDER_TARGET assertion with the shim's actual contract on native-capable hardware.
  2. On RTX 5080 (616.92, Vulkan 1.4.351), the original emulated path passes. As a negative control I changed only the binding-4 storage write at texel 10 from 0x00000001 to 0. The stock comparison still reported 16/16. Using exact channel equality instead, the same change failed at texel 10 red (got 0, expected 5.96046e-08). Restoring the shader passed all channels on both GPUs. Every RGB9E5 mantissa times its power-of-two scale is exactly representable in float32 for this point sample, so exact comparison also handles a NaN readback correctly.

I also queried native D3D12 directly via a separate one-format executable, enumerating adapters on the same Windows installation. The Radeon reports Support1 0x02e8d3f0, Support2 0x000002c0 (RENDER_TARGET=1, UAV_TYPED_STORE=1); the RTX reports 0x008013f0 / 0x00000200 (both 0); WARP reports 0x02ecd3f1 / 0x000002c0 (both 1). The native D3D12 driver agrees with the shim's positive RENDER_TARGET on the Radeon.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 62/62 on the RTX 4080 SUPER, including the new R9G9B9E5 UAV test. On the Intel UHD 770 the new test passes too. The 4 Intel failures (vk_binary_occlusion, d3d12_vulkan_device #962 which #1285 fixes, d3d12_descriptor min-LOD, d3d12_copy_xfmt) are known and unrelated.

@Pixel-Katana

Copy link
Copy Markdown

Code review of 48496b9. The emulation itself looks right to me; three points, none blocking, plus a pass on Blackwell.

Checked, no issue:

  • to_rgb9e5: max(0, exp_raw + 15) is the EXT_texture_shared_exponent max(-16, floor(log2 max)) + 16. After the 65408 clamp the largest value lands at exponent 31 / mantissa 511, and the max_m == 512 bump can't overflow the 5-bit exponent (that would need max ≥ 65472). NaN, -0 and negatives go to 0 via !(v > 0); the division runs in double, so it is exact.
  • The UAV clear path encodes with the view's logical format (E5B9G9R9 from image_descriptor), not the R32_UINT backing, so float clears reach shared_exp.
  • View chains in bind_image: the min-LOD struct now chains behind VkImageViewUsageCreateInfo{SAMPLED}, and the storage view gets its own {STORAGE} usage and R32_UINT format; non-emulated images keep the previous chain.

Points:

  1. vulkan_backend.h is broader than this format. VulkanContext::view() now adds VkImageViewUsageCreateInfo{COLOR_ATTACHMENT} to every colour view of a color_attachment image, not only R9G9B9E5 ones. That's valid (a subset of the image's usage) and ctest is clean, but it changes every RTV/rectangle-clear view, so it may be worth a line in the description.
  2. A shader-side typed access would find no image. image_store (and the runtime-typed image_load) pick the float storage view for any non-*_UINT/*_SINT T# format, and format 132 is a float format; the emulated image is bound only in as_uint/r32ui (deliberately, for VUID 07028). So a translated store or typed load through a format-132 T# reaches the empty float slot. That matches "shader-side packing remains open", and WARP also advertises typed load/store, but nothing reports it at run time. A log_once where bind_image skips as_float for an emulated image would make the gap visible when a real shader hits it.
  3. Nit: UAV_TYPED_LOAD is gated on R32_UINT's STORAGE_IMAGE_ATOMIC_BIT. Typed loads are about reading the format (R32_UINT storage reads need no format-less feature), so the atomic bit looks like an indirect proxy; STORAGE_IMAGE_BIT alone, or WARP's answer, seems closer.

Ran: RTX 5080, driver 621.24, Windows 11, runtime built from this head. Takes the emulated path (R9G9B9E5 UAV mode: emulated mutable R32_UINT, Support1 0x020003f1 / Support2 0xc0). With LEONIDA_VK_VALIDATION=1: d3d12_r9g9b9e5_uav_test OK (6/6 WARP clears, 16/16 copies, 16/16 set-0 round trip, 0 validation errors), ctest 62/62, d3d12_uav_clear_test exit 0. run_gpu_tests.py 91/118 (27 wave64 skips); emulated 118/118. This doesn't address @Vonsek's comparison point: with the stock tolerance the round trip would pass a one-bit mantissa error here too.

AI-assisted review (Claude Code).

@danosovsky

Copy link
Copy Markdown

Tested 48496b9 on Windows 11 (26300), MSVC BuildTools 2022, RelWithDebInfo via scripts\build_runtime.ps1, with and without LEONIDA_VK_VALIDATION=1: pass on NVIDIA, fail on AMD

GPU, driver d3d12_r9g9b9e5_uav_test
NVIDIA RTX 4070 SUPER, 621.24 OK, exit 0, 0 validation errors; mode "emulated mutable R32_UINT" (Support1=0x020003f1 Support2=0x000000c0); UAV clears 6/6 match WARP, copies 16/16, bindless round-trip 16/16
AMD Radeon(TM) Graphics (Ryzen 7600X iGPU, RDNA2), 25.10.36 / 32.0.21036.18 exit 1: line 399: (fs.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET) == 0

Likely cause: in CheckFeatureSupport(FORMAT_SUPPORT) the RENDER_TARGET bit (and the MSAA/BLENDABLE bits) come straight from the native VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 features, and only the emulated branch adds UAV bits; nothing masks them for R9G9B9E5. On a GPU whose driver reports COLOR_ATTACHMENT for E5B9G9R9, RENDER_TARGET stays on, while texture_format now refuses ALLOW_RENDER_TARGET for it. Clearing the render-target bits for R9G9B9E5_SHAREDEXP regardless of mode should fix it (hypothesis, not re-tested).

AI-assisted run.

@ItsNotRexed
ItsNotRexed merged commit 3ea9ca3 into ItsNotRexed:main Nov 22, 2026
Dom-in added a commit to Dom-in/Leonida that referenced this pull request Nov 22, 2026
…support

D3D12 doesn't render to the shared-exponent format, but AMD's RDNA4 Vulkan driver (26.11.2) gives
E5B9G9R9_UFLOAT_PACK32 COLOR_ATTACHMENT and COLOR_ATTACHMENT_BLEND, and CheckFeatureSupport(FORMAT_SUPPORT)
passed them through as RENDER_TARGET, BLENDABLE and the multisample bits. d3d12_r9g9b9e5_uav (ItsNotRexed#1287) failed
on an RX 9070 at its RENDER_TARGET == 0 check. The format's render-target, blend, depth-stencil and
multisample bits are now cleared whatever the driver advertises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants