Skip to content

RT-STATE-AUDIT-FORMATS: quantize 16-bit packed UNORM and rgba4 fallback clears - #2479

Open
lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-state-audit-formats-packed-clear
Open

lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-state-audit-formats-packed-clear

Conversation

@lixonik

@lixonik lixonik commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Part of RT-STATE-AUDIT-FORMATS (#2464; based on the format-state audit notes in #1608 by @kevincommits and @Tauwni).

Summary

  • Add VK_FORMAT_A1R5G5B5_UNORM_PACK16, VK_FORMAT_A4R4G4B4_UNORM_PACK16, VK_FORMAT_R4G4B4A4_UNORM_PACK16, and VK_FORMAT_A4B4G4R4_UNORM_PACK16 to detail::render_target_clear_color in runtime/src/vulkan_replay.h so 16-bit packed UNORM clears follow D3D12 round-to-nearest-even, clamp, and NaN -> 0 rules across GPUs (VK_FORMAT_R5G6B5_UNORM_PACK16 is intentionally left untouched to avoid overlapping Quantize RGB565 render-target clears at each channel width #2268).
  • Replace std::nearbyint(std::clamp(channel, 0.0f, 1.0f) * 15.0f) / 15.0f in the rgba4_render_fallback clear path (runtime/src/vulkan_replay.h) with detail::quantize_unorm(channel, 15.0) so 4-bit tie rounding and NaN handling do not depend on the thread's active floating-point rounding mode.

Tests

  • runtime/test/clear_color_test.cpp: added 16-bit packed UNORM clear quantization cases (A1R5G5B5, A4R4G4B4, R4G4B4A4, A4B4G4R4) and rgba4_render_fallback replay coverage under FE_DOWNWARD, FE_UPWARD, and FE_TOWARDZERO (full + rectangular clears, tie/clamp/NaN, including VK_FORMAT_UNDEFINED).
  • runtime/test/d3d12_b4g4r4a4_target_test.cpp: clear {0.5f, -1.0f, 2.0f, NaN} under FE_DOWNWARD and verify RGBA8 readback {0x88, 0x00, 0xFF, 0x00} (skips with 77 on GPUs with native A4R4G4B4 color attachment support).
  • runtime/test/d3d12_clear_formats_test.cpp: added B5G5R5A1_UNORM and A4B4G4R4_UNORM differential WARP cases.

Negative control & verification

  • On unmodified main (ec91d695c):
    • clear_color_test failed on A1R5G5B5 (got 0.5, expected 0.516129017).
    • d3d12_b4g4r4a4_target_test failed on RTX 5070 under FE_DOWNWARD (texel (8, 4) = 77 00 ff 00, want 88 00 ff 00).
    • d3d12_clear_formats_test failed on RTX 5070 vs WARP on 4 B5G5R5A1_UNORM cases (layer 0f, WARP 10 and layer ef, WARP f0).
  • With the fix:
    • ctest -R "^(clear_color|d3d12_b4g4r4a4_target|d3d12_b5g5r5a1_uav|d3d12_clear_formats)$": 4/4 passed.
    • python scripts/check_no_game_data.py: clean.
    • python -m unittest tests/test_ctest_registration.py: 6/6 OK.
    • git diff --check: clean.

GPUs tested: NVIDIA GeForce RTX 5070 (Windows 11, native Vulkan + WARP; VK_LAYER_KHRONOS_validation not installed locally).

AI-assisted (Antigravity, Gemini).

Independent RTX 3080 Vulkan validation: all three focused CTests passed, including the RGBA8 fallback and the nonzero green outside the rect. Test report.

@djrolfes

Copy link
Copy Markdown
Contributor

Independent Linux/Wine + NVIDIA RTX 3070 Ti review/test at e7d11f0da (merged cleanly on origin/main c0201a806, wine-11.22, NVIDIA 620.73.04). AI-assisted.

  • Diff looks clean: render_target_clear_color adds 16-bit packed UNORM quantization (A1R5G5B5, A4R4G4B4, R4G4B4A4, A4B4G4R4), and rgba4_render_fallback switches from std::nearbyint to detail::quantize_unorm(channel, 15.0) so clears are independent of FP rounding mode.
  • clear_color (native Linux build): PASSED (including all FP rounding modes and rgba4_render_fallback).
  • d3d12_b4g4r4a4_target (Wine/Vulkan RTX 3070 Ti): PASSED (RGBA8 image, render-target clears 64x32 matched, SRV T# format 136, 0 validation errors, including the FE_DOWNWARD clear test).
  • d3d12_clear_formats compiled and ran on Wine/RTX 3070 Ti (0 failed, 0 in formats the layer does not expose); host WARP comparison differs under Wine because Wine's D3D12 is not Microsoft WARP.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Tested for #2464 as Aslanek asked: unit tests and a game run on my machine; CI ignored for now as asked.

RUN pr2479-court | 2026-11-27 | MoonEyes | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        main ec91d695c + PR #2479 (head e7d11f0da), merged locally without conflict, nothing pushed | shaders: main's own translation
MACHINE:     RTX 4060 8 GB, desktop, only GPU, display attached | NVIDIA 610.88 | Windows 11 Pro 26300 | Ryzen 5 3600 | 32 GB | Windows GPU timeout at the default (2 s)
RECIPE:      short recipe: docs/running.md Windows NVIDIA recipe, pipeline cache warm, 330 s cap, scripted button presses from 60 s | build RelWithDebInfo, only the PR's targets built
TESTS:
  - clear_color_test: pass on the PR | fails on main: not checked yet
  - d3d12_b4g4r4a4_target_test: pass on the PR | fails on main: not checked yet
  - d3d12_clear_formats_test: pass on the PR | fails on main: not checked yet (the main build finished after this write-up; the tests have not been run on main yet; I will add the result)
GAME:        session=1 at 155 s | world entered at 210 s | last window >= 1 img/s ends at 317 s | 0 frames after world entry + 240 s | end: time limit (cap 330 s, ended at 334 s) | device loss: no (no fault report) | private RAM max 16153 MB
COMPARED TO: w77 (main ec91d695c, same short recipe): session=1 at 145 s, world at 195 s, last window >= 1 img/s at 293 s, private RAM max 16043 MB, ended by the time limit (337 s)
             w79 (main, same short recipe): world at 200 s, last window >= 1 img/s at 286 s, private RAM max 16583 MB, ended by the time limit (350 s)
             one run each (n=1); on main the spread of world entry on this recipe is about 165 to 200 s, so no conclusion on timings
VERDICT:     unit tests pass, check on main pending; game run clean to the time limit, no device loss; no timing conclusion (n=1)

Reading (marked):

  • MEASURED: the display stops after the world is entered (107 s after world entry for the last window >= 1 img/s here; 98 s on w77, 86 s on w79). Present on main too, not caused by this PR (finding Coordinating who works on what (continued from #14) #1608 (comment)).
  • MEASURED: the run reached the time limit without a device loss; this says the PR does not break the game boot on this machine, not that it changes anything in the game.

Limits: one machine, one run for the PR, one run for each main witness.

AI-assisted (Claude Code, Claude Opus 5.5)

@MoonEyes

Copy link
Copy Markdown
Collaborator

Follow-up to my RUN above: the check on main is now done. Each of the three tests, built from this PR's test files on top of main ec91d69's sources, fails on main and passes with the PR:

  • clear_color_test: fails on main | pass on the PR
  • d3d12_b4g4r4a4_target_test: fails on main | pass on the PR
  • d3d12_clear_formats_test: fails on main | pass on the PR

Same machine as above (RTX 4060 8 GB, NVIDIA 610.88, Windows 11). One correction to the RUN above: on main with this exact recipe the world entry was 195 and 200 s (two runs), not "about 165 to 200 s"; the 165-175 s runs had one setting changed. The rest of the RUN is unchanged.

AI-assisted (Claude Code, Claude Opus 5.5)

@Tauwni

Tauwni commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Tested on a Mac under Wine (Apple M3, macOS 26.5.1, Wine 11.0 with its MoltenVK 1.4.1), merged onto main 43a9d562d. Profile S, no game files. (MEASURED, 1 run each)

Test Test file only on main With this PR
d3d12_b4g4r4a4_target fails (line 213: wrong == 0) passes
clear_color fails (format 8 rect 0 channel 0: got 0.5, expected 0.516129017) passes
d3d12_clear_formats fails fails, identically

d3d12_clear_formats also fails on plain main here: it needs WARP as its reference (WARP reference unavailable: differential test cannot run), which Wine lacks. Not this PR.

This matters on Macs in particular: MoltenVK has no A4R4G4B4 colour attachment, so rgba4_render_fallback is the path every Mac takes.

AI-assisted (Claude Code). Report only.

@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head e7d11f0da on Windows 11 (MSVC 2022 19.44) with dual-GPU Vulkan validation (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363.0).

Verification

  • Built clear_color_test, d3d12_b4g4r4a4_target_test, and d3d12_clear_formats_test.
  • clear_color_test: 525 checks passed.
  • d3d12_clear_formats_test (LEONIDA_VK_VALIDATION=1): passed with 0 validation errors on both NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02).
  • d3d12_b4g4r4a4_target_test (LEONIDA_VK_VALIDATION=1): passed on RTX 5080 (RGBA8 fallback exercised, 0 validation errors); skipped (77) on AMD Radeon (native A4R4G4B4 attachment).

Notes (non-blocking)

  • Channel widths for 5:5:5:1 and 4:4:4:4 (logical RGBA order per VkClearColorValue), NaN/Inf clamping, and directed-rounding quantization for the RGBA8 fallback (vulkan_replay.h:1550-1567, 1637-1653, 4862-4894) all look right.
  • Minor follow-up note (vulkan_replay.h:1637-1653): DXGI_FORMAT_B5G6R5_UNORM (VK_FORMAT_R5G6B5_UNORM_PACK16) is also an exposed 16-bit packed UNORM RTV (d3d12_vulkan_device.cpp:2335-2338) and still falls through to driver rounding; fine as a follow-up if covering all 16-bit packed UNORM RTVs.

Approve.

AI-assisted review + local MSVC/dual-GPU verification.

@timbot103 timbot103 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.

Static code audit against e7d11f0da + ec740bcc3. The packed clear conversion and the shared color payload for full and rectangular clears look correct.

Both regions now expect green 0x00, so a fallback that always writes zero to green passes (runtime/test/d3d12_b4g4r4a4_target_test.cpp:205). Could the outside clear use green 10.0f / 15.0f and expect 0xAA there? That preserves a nonzero green readback alongside the new negative clamp, tie, and NaN checks.

AI-assisted.

@lixonik

lixonik commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@timbot103 Good catch. 8f45eda7d uses 10.0f / 15.0f green outside the rect and checks for 0xAA. The inside still checks negative green clamping to zero, the red tie, blue clamping and NaN alpha. d3d12_b4g4r4a4_target, d3d12_clear_formats and clear_color all passed on Windows 11 with MSVC and RTX 5070 (610.88).

AI-assisted.

@Maticcm

Maticcm commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Independent RTX 3080 verification at exact 8f45eda7dbbf091a63ff8732e18a8b191a82e1b8 and parent e7d11f0da41ea971af3627be077c998a9481c915. Native Windows 11 build 26340, NVIDIA driver 630.20, 10 GiB, MSVC 19.50.35717 RelWithDebInfo, Vulkan SDK 1.4.363.1 with Khronos validation.

All three focused CTests passed on both revisions (clear_color, d3d12_clear_formats, d3d12_b4g4r4a4_target) without skips. On the exact head, the 4444 render target used the RGBA8 fallback and all 64×32 pixels matched, including the new nonzero green 0xAA outside the clear rectangle. The format suite reported 261 WARP matches, zero differing or failed clears, and five checks for formats the layer does not expose. The fallback fixture reported zero Vulkan validation errors.

Synthetic fixtures. AI-assisted (Codex).

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Windows 11 + RTX 3090 + WARP Verification (8f45eda7db merged with main c8bf0e7b65) — PASS

  • Environment: Windows 11 (10.0.26200), MSVC 19.44.35207 (/O2 /MD /EHsc /std:c++20 /W4 /permissive- /utf-8), NVIDIA GeForce RTX 3090 (610.88) + Microsoft WARP + VK_LAYER_KHRONOS_validation 1.4.363.1 (LEONIDA_VK_VALIDATION=1).
  • Merge & Hygiene: Clean git merge-tree onto main (c8bf0e7b65) (0 conflicts); python -I scripts/check_no_game_data.py rc=0; 0 new /W4 warnings.
  • Red -> Green:
    • Red 1 (clear_color_test.cpp compiled against main runtime/src/vulkan_replay.h): fails with rc=1 (normalised clear: format 8 rect 0 channel 0: got 0.5, expected 0.516129017).
    • Red 2 (d3d12_b4g4r4a4_target_test.cpp linked against main on NVIDIA GeForce RTX 3090 under FE_DOWNWARD): fails with rc=1 (texel (11, 4) = 77 00 ff 00, want 88 00 ff 00 because std::nearbyint rounded 0.5 * 15 downward to 7 instead of 8).
    • Green (8f45eda7db + main c8bf0e7b65):
      1. clear_color_test.exe: rc=0 (525 full/rectangle integer, normalised and float replay checks passed across FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO, out-of-range, and NaN inputs).
      2. d3d12_b4g4r4a4_target_test.exe on NVIDIA GeForce RTX 3090: rc=0 (OK (RGBA8 image, render-target clears 64x32 matched, SRV T# format 136, 0 validation errors)).
      3. d3d12_clear_formats_test.exe on NVIDIA GeForce RTX 3090 + WARP: rc=0 (clears: 261 matched WARP, 0 differ, 0 failed, 0 VUID- validation errors).

AI-assisted.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — READY

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

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.

8 participants