Skip to content

Quantize RGB565 render-target clears at each channel width - #2268

Open
Bonchaloo wants to merge 1 commit into
ProjectLeonida:mainfrom
Bonchaloo:fix/rgb565-rtv-clear
Open

Bonchaloo wants to merge 1 commit into
ProjectLeonida:mainfrom
Bonchaloo:fix/rgb565-rtv-clear

Conversation

@Bonchaloo

@Bonchaloo Bonchaloo commented Nov 26, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as RT-CLEAR-RGB565-UNORM.

What and why

The RTV replay path already pre-rounds normalized clear values so the conversion is independent of the driver’s tie handling. RGB565 fell through to the raw float path. Apply the existing converter with channel maxima of 31, 63 and 31. Cover full and rectangle clears, midpoint rounding, NaN and clamping.

Tested

On the parent revision the new midpoint case failed (red 0.5, expected 16/31). With the change all 519 clear replay checks passed. The no-game-data check passed for both changed files.

GPUs tested: Apple M5 Max, Apple Metal driver on macOS 27.3. A separate native Metal RGB565 clear and readback produced 0x8410 for both the raw and pre-rounded midpoint. That device already rounds this case the same way. The failing regression was at the renderer’s Vulkan command boundary; the replay test uses synthetic inputs and captures the clear without a GPU.

Full native runtime CTest suite on Apple M5 Max with MoltenVK 1.4.2: 105 passed, 0 failed, 3 skipped (shader_library_corpus, vk_device_markers, vk_sync_validation).

AI-assisted: yes.

@Bonchaloo

Copy link
Copy Markdown
Author

My Discord username is bonchalo.

@real-rusher

Copy link
Copy Markdown
Contributor

Verified on main a73a57e (Ubuntu 24.04 x86-64, GCC 13.3, CMake 3.28, Ninja, no GPU). Merges cleanly. ctest -R '^clear_color$' passes with this PR; the PR's runtime/test/clear_color_test.cpp built against main's vulkan_replay.h fails, so the new case fails on main and passes with the change.

AI-assisted: yes (Claude Code)

@ecox982745

Copy link
Copy Markdown

Confirmed that the patch changes the stored RGB565 bits on an RTX 4090. For an RGB clear of (0.5, 0.5, 0.5), the Vulkan layer wrote 0x7bef on parent a73a57e13 and 0x8410 on head 1e0de21b0. The head matches the proposed nearest-even conversion, (16, 32, 16). Native D3D12 on the same card wrote 0x7bef, and Microsoft WARP wrote 0x8410.

I exercised ClearRenderTargetView through the D3D12 layer on Windows 11, NVIDIA 616.64, with Khronos validation. The same test-only probe ran on both revisions. It covered 199 input vectors, including primaries, clamping, NaN, and the float32 values at and immediately around the 5- and 6-bit rounding thresholds. Each input was used once as a whole-target clear and once as a rectangular clear on a 5×3 image. Every texel inside and outside the rectangle was checked.

Readback path Packed words matching nearest-even after exact scaling Packed words matching nearest-even after float32 scaling
Leonida Vulkan, parent 8/199 8/199
Leonida Vulkan, PR head 199/199 137/199
Native D3D12, RTX 4090 8/199 8/199
Native D3D12, WARP 137/199 199/199

Here “exact scaling” means multiplying each supplied float32 by 31, 63 or 31 in double precision before rounding. “Float32 scaling” rounds that product to float32 first. The WARP result is an observation on this build. The two calculations can pick neighboring codes near a half-step. The 62 head/WARP disagreements were at most one code per channel. These inputs deliberately concentrate on thresholds, so the counts describe this test set.

The full and rectangular readbacks agreed across all paths. Repeating the matrix gave identical results and no Vulkan validation errors. The PR's 519 CPU replay checks also passed. This verifies the layer's stated rounding rule on a Vulkan device where the midpoint changes.

@Bonchaloo

Copy link
Copy Markdown
Author

Ready for maintainer review. The RTX 4090 readback confirms the patch applies Leonida’s existing normalized-clear rounding rule. Native D3D12 on that GPU and WARP differed near rounding thresholds, as documented above. Is the existing Leonida rule the intended behavior for RGB565?

The fork CI run reports action_required with zero jobs. Could a maintainer approve it if CI is wanted for review? The M5 Max suite completed with 105 passed and 3 skipped.

I’ve kept the independent viewport fix (#2271) in draft. Should it enter review now or after this one lands?

AI-assisted: yes.

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

Reviewed and tested merged onto main (493867366) on Windows 11 (MSVC 19.44, Ninja Release).

  • Adding VK_FORMAT_R5G6B5_UNORM_PACK16 with {31, 63, 31, 0} channel widths to render_target_clear_color() accurately quantizes DXGI_FORMAT_B5G6R5_UNORM clears per channel (and complements the other 16-bit packed UNORM formats in #2479 without overlap).
  • Built clear_color_test and ran ctest --output-on-failure -V -R ^clear_color$: 1/1 passed (519 checks, 0.02 s).

AI-assisted (Antigravity, Gemini).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This changes stored RGB565 pixels on physical gfx906 (RADV Mesa 26.0.8), but not at the exact midpoint. Comparing main 1bc055e6c with 1e0de21b0 merged into it, both stored 0x8410 for RGB (0.5, 0.5, 0.5). With inputs one representable float above the tested channel thresholds, main stored (3,45,21) / 0x1db5; the merge stored the expected (4,46,22) / 0x25d6. Just below those thresholds both stored 0x1db5.

The difference covered all 315 pixels of the full clear and all 88 pixels inside the rectangle. Across 30 finite clears, the merge matched all 6,045 fill pixels; main missed 403. All 3,405 outside-rectangle pixels stayed unchanged. The identical replay unit test also went from a midpoint command-value failure to 519 checks passing. No validation errors or synchronization hazards; NaN cases were checked at the command boundary only. This verifies the runtime’s nearest-even rule, not a universal native D3D12 tie rule.

AI-assisted.

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

Reviewed head 1e0de21b0 from the supplied diff against b6bf3aa0b. Static audit only. I ran no builds, tests, or GPU workloads.

  • runtime/shims/d3d12_vulkan_device.cpp:2454 maps DXGI_FORMAT_B5G6R5_UNORM to VK_FORMAT_R5G6B5_UNORM_PACK16. Clear inputs stay in logical RGBA order, and the packed word places red in bits 11–15, green in bits 5–10, and blue in bits 0–4. The new maxima of 31, 63, and 31 match that layout. The fourth float is passed through, and the format has no alpha channel. Both vkCmdClearColorImage and vkCmdClearAttachments receive the converted value from runtime/src/vulkan_replay.h.
  • The shared converter sends NaN, nonpositive values, and negative infinity to zero, and sends values at or above one, including positive infinity, to one. For finite in-range float32 inputs, the double-precision product is rounded to nearest, ties to even. At these scales the only exactly representable half-step input is 0.5, where half-up and nearest-even both yield codes 16, 32, and 16. Vulkan receives floats representing the selected codes away from the half-step boundaries. The supplied native D3D12 measurements differ across drivers near thresholds, consistent with the variation documented in docs/d3d12_vulkan.md:272.
  • runtime/test/clear_color_test.cpp:140 exercises the midpoint, NaN, and clamping through both replay callbacks. The midpoint assertion detects the original float passthrough. The existing packed readback check in runtime/test/d3d12_vulkan_device_test.cpp:6015 covers the red and blue bit positions for whole and rectangular clears. The change uses an existing Vulkan format constant and standard C++ in the same CTest target for Windows/MSVC and Linux.
  • Both patch hunks fit b6bf3aa0b. The packed-format change at e7d11f0da discussed as #2479 adds RGB5551 and RGBA4444 cases and updates the RGBA4444 fallback. Its switch and test additions are at distinct insertion points from the RGB565 changes. The merge labeled #2479 in the supplied main history is d09752898 and changes Wine test tooling only.

AI-assisted.

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