Skip to content

uav_clear: float UAV clears round toward zero and saturate (no INF), as D3D does - #1061

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
quackottu:uav-clear-float-round-to-zero
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
quackottu:uav-clear-float-round-to-zero

Conversation

@quackottu

Copy link
Copy Markdown
Contributor

detail::small_float (runtime/src/uav_clear.h) converts a ClearUnorderedAccessViewFloat value for R16/R16G16/R16G16B16A16_FLOAT and R11G11B10_FLOAT views, on both the buffer and the image path. It rounded to nearest even and made any too-large value infinity.

D3D rounds toward zero instead. D3D11.3 functional spec 3.2.2, "Floating Point Conversion": conversion to another float format rounds to zero, and a finite value too large for the target becomes the largest finite value, not infinity. INF stays INF and NaN stays NaN. I measured ClearUnorderedAccessViewFloat on a 1×1 texture with readback; WARP and the RTX 5070's D3D12 driver give the same bytes for every case:

view, value host (WARP = NVIDIA) main
R16, 70000 / 65520 / 1e10 0x7bff (65504) 0x7c00 (+INF)
R16, -1e10 0xfbff 0xfc00
R16, 1e-7 0x0001 0x0002
R16, 3e-8 0x0000 0x0001
R11G11B10, (1e6, 70000, 65504) 0xf7fdffbf (all max finite) 0xf83e07c0 (INF)
R11G11B10, B = 1/3 0x1aa 0x1ab

A clear to a large value wrote infinity, which shader math then turns into NaN (INF·0). The fix truncates, saturates finite overflow to the largest finite, keeps infinity, and corrects the two comments that stated the old rule. With it, every R16 and R11G11B10 case above matches the host. The one difference left is the R11G11B10 NaN payload (WARP 0x3ff, ours 0x3f0), which the header already says may be canonicalised.

Tests: d3d12_uav_clear_test's conversion checks asserted the old rule for 65520 and 1e-7; they now hold the host values. New cases: 70000, -1e10, +INF, and R11G11B10 overflow and 1/3.

Tested (Windows 11, RTX 5070, driver 621.24, MSVC 14.44, LEONIDA_VK_VALIDATION=1): d3d12_uav_clear_test passes with 0 validation errors. With main's uav_clear.h it fails at line 78 (65520). No game files.

Touches uav_clear.h only; #990 is in vulkan_replay.h (buffer offset), a different function.

AI-assisted.

🤖 Generated with Claude Code

…ero and saturate, as D3D does

small_float rounded to nearest even and turned any too-large value into
infinity. D3D11.3 3.2.2 says float-to-float conversion rounds toward zero
and a finite value too large becomes the largest finite (only infinity
stays infinity); ClearUnorderedAccessViewFloat on WARP and on NVIDIA
gives exactly that: R16 70000 or 65520 -> 0x7bff, 1e-7 -> 0x0001,
R11G11B10 (1e6, 1e6, 1e6) -> 0xf7fdffbf, B = 1/3 -> 0x1aa. The layer wrote
infinity (which a shader then turns into NaN) and rounded up.

d3d12_uav_clear_test's conversion checks asserted the old rule for 65520
and 1e-7; they now hold the host values, with overflow, infinity and
R11G11B10 cases added.
@kapitche

Copy link
Copy Markdown
Contributor

Tested 7d764b9 on Windows 11 with two GPUs on one machine (MSVC 14.51): pass

@dcastell1

Copy link
Copy Markdown
Contributor

Tested 7d764b9 on an RTX 5090 (Blackwell), Windows 11 Pro 10.0.26300.9974, NVIDIA 621.24, MSVC 19.44, clean RelWithDebInfo Ninja build:

  • d3d12_uav_clear_test: exit 0.
  • ClearUnorderedAccessViewUint/Float matched for raw, structured and typed buffers and 2D, array and volume textures, whole views and rectangles.
  • The test’s deliberate invalid-clear/subresource paths were rejected as expected; the finite-to-f16/R11G11B10 cases now round toward zero and saturate per the PR.

No game files were used.

@ItsNotRexed
ItsNotRexed merged commit 7ff3d3a into ItsNotRexed:main Nov 22, 2026
9 of 10 checks passed
@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested 7d764b9 on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Home 25H2 (26200.9974), VS 2022 Build Tools, Vulkan SDK 1.4.363.1: pass

  • Build: 0 warnings. ctest: 48/48 passed.
  • d3d12_uav_clear_test: exit 0, all matched, also with LEONIDA_VK_VALIDATION=1.

Small note: the comment above the half-float checks in d3d12_uav_clear_test.cpp still says "round to nearest even; too large is infinity", which the new checks no longer match.

AI-assisted. No game files.

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.

5 participants