[PR #1179] vulkan_replay: sRGB render-target clears are encoded in the layer, as WARP encodes them (D3D-CLEAR-SRGB-ENCODE) #1212

Open
opened 2026-11-26 20:20:28 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1179
Author: @MalexAson
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/srgb-rtv-clear-encode


📝 Commits (1)

  • e2c13db vulkan_replay: sRGB render-target clears are encoded in the layer, as WARP encodes them (D3D-CLEAR-SRGB-ENCODE)

📊 Changes

6 files changed (+231 additions, -16 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+41 -2)
📝 runtime/src/vulkan_replay.h (+43 -3)
📝 runtime/test/clear_color_test.cpp (+98 -4)
📝 runtime/test/d3d12_clear_formats_test.cpp (+29 -2)
📝 runtime/test/d3d12_clear_test.cpp (+6 -5)
📝 runtime/test/d3d12_state_sweep_test.cpp (+14 -0)

📄 Description

Claimed in the coordination issue as: D3D-CLEAR-SRGB-ENCODE

What and why

ClearRenderTargetView on R8G8B8A8_UNORM_SRGB and B8G8R8A8_UNORM_SRGB handed the linear float to vkCmdClearColorImage or vkCmdClearAttachments and the GPU encoded it, so the stored byte depended on the GPU. Against WARP, d3d12_clear_formats differed in 10 clears and d3d12_srgb_uav in 2 on an Intel Iris Xe (0.5 is bb there and bc on WARP).

What I measured (the new "sRGB RTV clears" item in docs/d3d12_vulkan.md has the details):

  • No driver is wrong. D3D permits FLOAT → SRGB 0.6 of a step of tolerance (Direct3D 11.3 functional specification, 3.2.3.8), and each implementation puts the 255 steps between two codes somewhere else. Of 60 000 random values the Iris Xe stores another byte than WARP for 6 649 and an RTX 4060 for 3 648, never more than one code away. The RTX 4060 passes on main only because the five test colours miss those values.
  • WARP takes the value to 12 bits first (x * 4095 in single precision, rounded half up) and then takes the nearest code of the sRGB curve. All 255 floats at which its byte changes are the ones this rule gives, and 84 582 further values agree.
  • WARP and both GPUs store the linear value of a code as that code, for all 256 codes.
  • Each GPU's own D3D12 driver stores exactly what its Vulkan driver stores (84 582 of 84 582 values, on both GPUs).

The change: render_target_clear_color encodes the colour channels itself by WARP's rule (srgb8_from_linear) and hands Vulkan the linear value of that code (srgb8_linear). Alpha is unchanged. This is what quantize_unorm does for UNORM ties.

One thing for a maintainer to decide. A GPU's own D3D12 driver encodes as its Vulkan driver does, so the layer used to equal that driver and differ from WARP; now it equals WARP on every GPU and differs from that driver where the driver differs from WARP. I followed what the layer does for UNORM ties and D24: the layer converts, WARP is the reference, and a host that does otherwise is a host quirk. If you would rather keep the GPU's encoding, the alternative is to leave the layer alone and compare the sRGB colour channels with WARP within one code in the two tests. Say so and I will redo it that way.

d3d12_state_sweep_test compares with the same adapter's D3D12 driver and cleared its sRGB targets to 0.2, 0.4 and 0.6, of which Intel's driver stores the first two as 123 and 169 where WARP has 124 and 170. With the layer change it differed in 4 of 482 cases on the Iris Xe, so its sRGB targets now start from the values of codes 124, 170 and 203, which every implementation measured stores alike. The sweep is about blending.

Not changed: a draw into an sRGB target is still encoded by the GPU, and d3d12_clear_test still compares castable sRGB views within 1.

Tested

On main 6293c1c4f with MSVC 14.50 (Build Tools 2026), RelWithDebInfo, WARP 10.0.26100.5074, each on both GPUs through LEONIDA_VK_DEVICE:

  • d3d12_clear_formats: before, 241 matched WARP and 10 differ on the Iris Xe (251 and 0 on the RTX 4060). With the extended sRGB colours on the unfixed layer, 178 of the 352 sRGB cases differ on the Iris Xe and 170 on the RTX 4060. After: 593 matched and 0 differ on both.
  • d3d12_srgb_uav: before, 2 of 4 clears differ on the Iris Xe; after, 0 failures on both.
  • clear_color (no GPU): 567 checks pass.
  • A scratch sweep (not in this pull request) of 84 838 values through the fixed layer against WARP: 0 differ on both GPUs, for both formats, as a whole view and as a rectangle, on plain render targets and on UAV ones backed by a UNORM image.
  • ctest for the 23 tests that clear or present sRGB targets or use the changed code (d3d12_clear, d3d12_clear_restricted_depth, d3d12_clear_formats, d3d12_srgb_uav, d3d12_uav_clear, d3d12_short_uav_clear, d3d12_typeless_rtv, d3d12_typeless_families, d3d12_typeless_depth, d3d12_state_sweep, d3d12_draw, d3d12_present, d3d12_close_parity, d3d12_vulkan_device, dxgi_swapchain, xbox_present, xs_proxy_mock, vk_presenter_sync, typeless_rtv, clear_color, replay_clear_gate, replay_uav_clear_offset, d3d12_rgb9e5_rtv): on both GPUs 21 pass, d3d12_rgb9e5_rtv skips and d3d12_vulkan_device fails. It fails the same way without this change (three resource-flag cases of compare_invalid_arguments against the host runtime), which has nothing to do with clears.
  • Not run: the whole ctest (only these targets were built), LEONIDA_VK_VALIDATION=1 (no validation layer on this PC), an AMD GPU, Linux.

GPUs tested: Intel Iris Xe (Raptor Lake-P, 8086:a7a0), Intel 32.0.101.7092, Windows 11 build 26100; NVIDIA GeForce RTX 4060 Laptop GPU, NVIDIA 616.92, Windows 11 build 26100

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (not applicable: no translator change)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1179 **Author:** [@MalexAson](https://github.com/MalexAson) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/srgb-rtv-clear-encode` --- ### 📝 Commits (1) - [`e2c13db`](https://github.com/ubiquitarianism/leonida/commit/e2c13db54a0320c7c9d8ed3302dcb40e58469c08) vulkan_replay: sRGB render-target clears are encoded in the layer, as WARP encodes them (D3D-CLEAR-SRGB-ENCODE) ### 📊 Changes **6 files changed** (+231 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+41 -2) 📝 `runtime/src/vulkan_replay.h` (+43 -3) 📝 `runtime/test/clear_color_test.cpp` (+98 -4) 📝 `runtime/test/d3d12_clear_formats_test.cpp` (+29 -2) 📝 `runtime/test/d3d12_clear_test.cpp` (+6 -5) 📝 `runtime/test/d3d12_state_sweep_test.cpp` (+14 -0) </details> ### 📄 Description Claimed in the coordination issue as: D3D-CLEAR-SRGB-ENCODE ### What and why `ClearRenderTargetView` on `R8G8B8A8_UNORM_SRGB` and `B8G8R8A8_UNORM_SRGB` handed the linear float to `vkCmdClearColorImage` or `vkCmdClearAttachments` and the GPU encoded it, so the stored byte depended on the GPU. Against WARP, `d3d12_clear_formats` differed in 10 clears and `d3d12_srgb_uav` in 2 on an Intel Iris Xe (0.5 is `bb` there and `bc` on WARP). What I measured (the new "sRGB RTV clears" item in `docs/d3d12_vulkan.md` has the details): - No driver is wrong. D3D permits FLOAT → SRGB 0.6 of a step of tolerance (Direct3D 11.3 functional specification, 3.2.3.8), and each implementation puts the 255 steps between two codes somewhere else. Of 60 000 random values the Iris Xe stores another byte than WARP for 6 649 and an RTX 4060 for 3 648, never more than one code away. The RTX 4060 passes on `main` only because the five test colours miss those values. - WARP takes the value to 12 bits first (`x * 4095` in single precision, rounded half up) and then takes the nearest code of the sRGB curve. All 255 floats at which its byte changes are the ones this rule gives, and 84 582 further values agree. - WARP and both GPUs store the linear value of a code as that code, for all 256 codes. - Each GPU's own D3D12 driver stores exactly what its Vulkan driver stores (84 582 of 84 582 values, on both GPUs). The change: `render_target_clear_color` encodes the colour channels itself by WARP's rule (`srgb8_from_linear`) and hands Vulkan the linear value of that code (`srgb8_linear`). Alpha is unchanged. This is what `quantize_unorm` does for UNORM ties. **One thing for a maintainer to decide.** A GPU's own D3D12 driver encodes as its Vulkan driver does, so the layer used to equal that driver and differ from WARP; now it equals WARP on every GPU and differs from that driver where the driver differs from WARP. I followed what the layer does for UNORM ties and D24: the layer converts, WARP is the reference, and a host that does otherwise is a host quirk. If you would rather keep the GPU's encoding, the alternative is to leave the layer alone and compare the sRGB colour channels with WARP within one code in the two tests. Say so and I will redo it that way. `d3d12_state_sweep_test` compares with the same adapter's D3D12 driver and cleared its sRGB targets to 0.2, 0.4 and 0.6, of which Intel's driver stores the first two as 123 and 169 where WARP has 124 and 170. With the layer change it differed in 4 of 482 cases on the Iris Xe, so its sRGB targets now start from the values of codes 124, 170 and 203, which every implementation measured stores alike. The sweep is about blending. Not changed: a draw into an sRGB target is still encoded by the GPU, and `d3d12_clear_test` still compares castable sRGB views within 1. ### Tested On `main` 6293c1c4f with MSVC 14.50 (Build Tools 2026), RelWithDebInfo, WARP 10.0.26100.5074, each on both GPUs through `LEONIDA_VK_DEVICE`: - `d3d12_clear_formats`: before, 241 matched WARP and 10 differ on the Iris Xe (251 and 0 on the RTX 4060). With the extended sRGB colours on the unfixed layer, 178 of the 352 sRGB cases differ on the Iris Xe and 170 on the RTX 4060. After: 593 matched and 0 differ on both. - `d3d12_srgb_uav`: before, 2 of 4 clears differ on the Iris Xe; after, 0 failures on both. - `clear_color` (no GPU): 567 checks pass. - A scratch sweep (not in this pull request) of 84 838 values through the fixed layer against WARP: 0 differ on both GPUs, for both formats, as a whole view and as a rectangle, on plain render targets and on UAV ones backed by a UNORM image. - `ctest` for the 23 tests that clear or present sRGB targets or use the changed code (`d3d12_clear`, `d3d12_clear_restricted_depth`, `d3d12_clear_formats`, `d3d12_srgb_uav`, `d3d12_uav_clear`, `d3d12_short_uav_clear`, `d3d12_typeless_rtv`, `d3d12_typeless_families`, `d3d12_typeless_depth`, `d3d12_state_sweep`, `d3d12_draw`, `d3d12_present`, `d3d12_close_parity`, `d3d12_vulkan_device`, `dxgi_swapchain`, `xbox_present`, `xs_proxy_mock`, `vk_presenter_sync`, `typeless_rtv`, `clear_color`, `replay_clear_gate`, `replay_uav_clear_offset`, `d3d12_rgb9e5_rtv`): on both GPUs 21 pass, `d3d12_rgb9e5_rtv` skips and `d3d12_vulkan_device` fails. It fails the same way without this change (three resource-flag cases of `compare_invalid_arguments` against the host runtime), which has nothing to do with clears. - Not run: the whole `ctest` (only these targets were built), `LEONIDA_VK_VALIDATION=1` (no validation layer on this PC), an AMD GPU, Linux. GPUs tested: Intel Iris Xe (Raptor Lake-P, 8086:a7a0), Intel 32.0.101.7092, Windows 11 build 26100; NVIDIA GeForce RTX 4060 Laptop GPU, NVIDIA 616.92, Windows 11 build 26100 AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (not applicable: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1212
No description provided.