Skip to content

Runtime: clear typed UAVs of typeless textures in the view's format - #2084

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
luisbaker:d3d-typeless-uav-clear
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
luisbaker:d3d-typeless-uav-clear

Conversation

@luisbaker

Copy link
Copy Markdown
Contributor

Fixes the UAV-clears part of @zneel's finding: clearing a typed UAV view of a typeless texture (such as an R16G16_UNORM UAV of an R16G16_TYPELESS texture backed by VK_FORMAT_R16G16_SFLOAT, or an R32_UINT UAV of R32_TYPELESS) previously decoded the encoded bytes through the backing image format and cleared whole views via vkCmdClearColorImage, corrupting floating-point NaN bit patterns (e.g., ClearUnorderedAccessViewFloat(1.0) on R16G16_UNORM wrote 0x7fff7fff instead of 0xffffffff).

Summary of changes

  • runtime/src/vulkan_replay.h:
    • Added optional ReplayFunctions::color_attachment_format predicate (defaults to view_format != VK_FORMAT_E5B9G9R9_UFLOAT_PACK32).
    • For texture ClearUnorderedAccess when view_format != p.binding.format and the resource + view_format support COLOR_ATTACHMENT_BIT, decodes the clear value in view_format, sets p.clear_format = view_format, and routes whole-view clears through vkCmdBeginRendering + vkCmdClearAttachments (matching ClearRenderTarget's format-reinterpretation path) instead of round-tripping through p.binding.format.
    • Preserves the R32_UINT path for emulated R9G9B9E5_SHAREDEXP and vkCmdClearColorImage for same-format unpredicated whole-view clears.
  • runtime/src/vulkan_backend.h & runtime/shims/d3d12_vulkan_device.cpp: wired color_attachment_format to vkGetPhysicalDeviceFormatProperties (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT).
  • runtime/test/replay_uav_clear_offset_test.cpp & runtime/test/d3d12_uav_clear_test.cpp: added replay unit tests and GPU readback tests covering R16G16_TYPELESS, R16G16B16A16_TYPELESS, R32_TYPELESS, R32G32_TYPELESS, R32G32B32A32_TYPELESS, R8G8B8A8_TYPELESS, and R16_TYPELESS across UNORM/SNORM/UINT/SINT views (including 0xffff and sNaN bit patterns).

Tested

Windows 11, Intel(R) Graphics (driver 32.0.101.6737), MSVC 14.44, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo, no game files. All passed both normally and with LEONIDA_VK_VALIDATION=1 (0 validation errors):

  • replay_uav_clear_offset_test — pass
  • d3d12_uav_clear_test — pass
  • typeless_rtv_test — pass (93 scenarios)
  • vk_command_replay_test — pass
  • d3d12_r9g9b9e5_uav_test — pass (6/6 matched WARP bit-for-bit)

Claim: #1902 (comment)

AI-assisted.

ClearUnorderedAccess previously decoded encoded bytes into p.binding.format and cleared whole views via vkCmdClearColorImage, which corrupted non-float values (such as R16G16_UNORM 1.0 = 0xffff or R32_UINT sNaN bits) on SFLOAT-backed typeless textures. When view_format differs from p.binding.format and supports COLOR_ATTACHMENT, decode in view_format and clear through an attachment view of view_format (preserving the R32_UINT path for emulated R9G9B9E5_SHAREDEXP). Adds replay and GPU readback tests.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-attachable-view fallback still loses the exact bits this change is meant to preserve. For example, an R16G16_UNORM UAV view of an R16G16_SFLOAT-backed typeless image can encode Float(1,1) as 0xffff components; when the view format lacks COLOR_ATTACHMENT support, this branch decodes those bytes as backing-format half floats (NaNs) and sends them through vkCmdClearColorImage. Vulkan does not guarantee that a floating-point NaN clear round-trips to the same NaN payload/bits, so the clear can still be corrupted. R16G16_UNORM is not required to support COLOR_ATTACHMENT on every implementation. The fallback test uses zero values, which cannot expose this case. Please use a bit-exact fallback or return unsupported, and exercise a nonzero pattern.

@towade

towade commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 8af232c5 on Windows 11 26H2 (26300.10075), AMD Radeon RX 9070 XT (driver 32.0.32029.2010), MSVC 19.44 Release, Vulkan SDK 1.4.363.0.

Stock driver capabilities: pass. With LEONIDA_VK_DEVICE=9070 XT and LEONIDA_VK_VALIDATION=1, CTest passed replay_uav_clear_offset, d3d12_uav_clear, typeless_rtv (93 scenarios), and d3d12_r9g9b9e5_uav (6/6 matched WARP). I ran vk_command_replay_test.exe directly; it also passed. No Vulkan validation errors.

Forced non-attachable view: reproduced the existing review finding. The AMD driver advertises COLOR_ATTACHMENT for VK_FORMAT_R16G16_UNORM. In a separate copy of the GPU test I masked only that bit for that format in the device shim's vkGetPhysicalDeviceFormatProperties callback, leaving the real GPU and replay code unchanged. The same whole-view ClearUnorderedAccessViewFloat(1, 1) on an R16G16_TYPELESS texture through an R16G16_UNORM UAV passes without the mask. With the mask it returns 0x7fff7fff per texel instead of 0xffffffff (256 differing bytes across both 8×8 layers). This exercises the fallback on AMD; the driver did not naturally select it.

Synthetic fixtures only. AI-assisted (Codex).

@CharlyRien

Copy link
Copy Markdown

Tested 8af232c (merged on current main 4eb6155) on RTX 3090, driver 616.56, Windows 11 23H2: pass. MSVC 2022 / Ninja, Vulkan SDK 1.4.363. With LEONIDA_VK_VALIDATION=1, the D3D12/Vulkan device tests give no failure or validation error beyond what main already shows on this machine. ctest fails the same 3 tests as main does here (d3d12_draw: #1966; d3d12_vulkan_device and d3d12_typeless_rtv: driver/23H2 limits). run_gpu_tests.py 136/180 passed, 44 skipped (wave64 on a 32-wide GPU), the same as main.

AI-assisted test run.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 8af232c merged onto main 4eb6155 on Windows 11, RTX 4060 Ti (Ada), NVIDIA 621.24, MSVC (VS 2026), scripts/build_runtime.ps1 + ctest -C RelWithDebInfo: pass. 110 tests; every test passes except d3d12_draw, which fails identically on main itself (fence signal 21 is not above 30, fixed by #1995). No new failures against the main baseline.

AI-assisted (Claude Code).

@nonverifiedperson

Copy link
Copy Markdown
Contributor

Tested 8af232c5 on two GPUs: no regressions. Every result matches the main baseline, and the tests this PR touches pass in all four runs.

Windows 10 22H2, MSVC 19.44, Vulkan SDK 1.4.363.0, scripts/build_runtime.ps1 (RelWithDebInfo, Ninja), builds clean. GPU chosen with LEONIDA_VK_DEVICE.

  • AMD Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, RDNA2), AMD driver 32.0.13036.4
  • NVIDIA RTX 5070 Ti, driver 32.0.16.2135

ctest, PR vs baseline (baseline = 4eb6155d, the merge base, built the same way):

GPU LEONIDA_VK_VALIDATION PR main VUIDs (PR / main)
Radeon 0 2 of 116 fail same 2 —
Radeon 1 2 of 116 fail same 2 1 / 1
RTX 5070 Ti 0 1 of 116 fails same 1 —
RTX 5070 Ti 1 1 of 116 fails same 1 1 / 1
  • d3d12_uav_clear, replay_uav_clear_offset, typeless_rtv, d3d12_r9g9b9e5_uav and command_list pass in all four runs.
  • Already failing on main, identical with and without the PR: d3d12_draw on both GPUs (bundle blend check, "got 128,0,128,255, expected red 64") and, on the Radeon only, d3d12_vulkan_device at line 3594: argument_mismatches <= 0. The only VUID is VUID-vkDestroyPipeline-pipeline-00765, also on main.

scripts/run_gpu_tests.py at the PR head: Radeon 167/180 passed, 13 skipped; RTX 5070 Ti 136/180 passed, 44 skipped (wave64 tests without emulation). No failures, same as main.

This adds an RDNA2 iGPU and a Blackwell card to @towade's RX 9070 XT and @CharlyRien's RTX 3090 runs. What it doesn't cover: no typed clear of a typeless texture beyond what those tests do; no format outside the ones they exercise.

No game files. AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 2fe08d7 into ItsNotRexed:main Nov 22, 2026
4 checks passed
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.

7 participants