Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2641,13 +2641,52 @@ The trace will enumerate them.
and 2^32 in alpha match WARP). `d3d12_clear_test` compares full and rectangular D3D12 readbacks with WARP across
UNORM, SNORM, FLOAT, all 15 exposed UINT/SINT render targets (11 value vectors, including 8-/16-/10-/2-bit range
boundaries) and depth/stencil; `clear_color_test` checks both replay paths for 19 integer formats without a GPU,
including unchanged float values. SRGB clears are omitted from the comparison: the
linear→sRGB encoding rounds differently at the `.5` boundary on WARP vs Vulkan (a 1-ulp driver difference).
including unchanged float values. SRGB clears are omitted from that comparison; `d3d12_clear_formats_test`
compares them (next item).
**Open:** on NVIDIA RTX 5080 (driver 616.56) clears that land exactly on a `.5` UNORM/SNORM step round down where
WARP rounds up: `R8G8B8A8_UNORM`/`SNORM` 0.5 store `7f`/`3f` vs WARP `80`/`40`, and a `D16_UNORM` 0.5 depth clear
stores `0x7fff` vs WARP `0x8000` (all on `main` before this change; same 1-ulp class as SRGB, not addressed here).
`D32_FLOAT` and `D32_FLOAT_S8X24_UINT` match WARP.
This does not add typed-view reinterpretation of typeless resources.
- **sRGB RTV clears (D3D-CLEAR-SRGB-ENCODE):** `ClearRenderTargetView` through an `R8G8B8A8_UNORM_SRGB` or
`B8G8R8A8_UNORM_SRGB` view encodes the colour channels in the layer and hands Vulkan the linear value of the
resulting code (`srgb8_from_linear` and `srgb8_linear` in `vulkan_replay.h`); alpha is linear and rounds like
UNORM. Before, the linear float went to `vkCmdClearColorImage` or `vkCmdClearAttachments` as it was and the GPU
encoded it, so the stored byte depended on the GPU: 0.5 was `bb` on an Intel Iris Xe and `bc` on WARP and an
NVIDIA RTX 4060. **Measured** (Windows 11 build 26100, WARP 10.0.26100.5074, Iris Xe with Intel 32.0.101.7092, RTX
4060 Laptop with NVIDIA 616.92; a scratch program cleared 256-slice sRGB targets through the layer and through
WARP, a whole view and a rectangle each, and bisected over float bit patterns):
- No driver is wrong. D3D permits FLOAT → SRGB a tolerance of 0.6 of a step (Direct3D 11.3 functional
specification, 3.2.3.8), and each implementation puts the 255 steps between two codes somewhere else: measured
against the halfway point of the ideal curve, the Iris Xe's sit between 0.18 of a code below and 0.33 above, the
RTX 4060's within 0.10 and WARP's between 0.29 below and 0.34 above. On each GPU both Vulkan commands have the
same 255 steps. Of 60 000 random values (half uniform in 0..1, half log-uniform from 1e-6) the Iris Xe stored
another byte than WARP for 6 649 and the RTX 4060 for 3 648, never more than one code away.
- WARP takes the value to 12 bits first and encodes that: `j = floor(x * 4095 + 0.5)` with the product rounded to
single precision, then the nearest code of the IEC 61966-2-1 curve at `j / 4095`. The 255 floats at which WARP's
byte changes are the 255 this rule gives (with the product in exact arithmetic only 188 are: `0x3f018018` times
4095 is 2071.4999986, which a float rounds to 2071.5), and WARP agrees with it on 84 582 further values: every
12-bit value, five floats around each of the 4 095 places between two of them, the 60 000 random ones, 0, 1,
values outside 0..1, NaN and both infinities. It is not the nearest code of the ideal curve for 2 852 of the
60 000 (0.0004 is 2, the curve gives 1.32).
- WARP and both GPUs store the linear value of a code as that code, for all 256 codes and with both commands. That
value is the one farthest from any implementation's steps, so it is what the layer hands over; a GPU whose
encoding is more than half a code off at one of these values would still differ.
- Each GPU's own D3D12 driver stores what its Vulkan driver stores (the same byte for 84 582 of 84 582 values, on
both GPUs): the encoding is the hardware's. So the layer used to store the bytes of the D3D12 driver of the GPU it
ran on, and now stores WARP's on every GPU, which differ from the Intel driver's for those 6 649 of 60 000 values
and from the NVIDIA driver's for 3 648. That is the choice made before for the ties of UNORM clears and for D24:
the layer converts, WARP is the reference, and a host driver that does otherwise is a host quirk.

`clear_color_test` (no GPU) pins 38 values to the bytes WARP stored, checks that every code encodes to itself and
that the result is the same under each floating-point rounding mode. `d3d12_clear_formats_test` clears both formats
to the UNORM colours, the value of every code and the value halfway between every two codes (176 colours each, as
a whole view and as a rectangle): against WARP, 178 of the 352 cases differed on the Iris Xe and 170 on the RTX
4060 before, none after (the five UNORM colours alone differed in 10 of 10 cases on the Iris Xe and in none on the
RTX 4060). `d3d12_state_sweep_test` compares with the D3D12 driver of the same adapter and cleared its sRGB targets
to 0.2, 0.4 and 0.6, of which the first two are codes 124 and 170 on WARP and 123 and 169 on the Iris Xe; it now
clears them to the values of codes 124, 170 and 203, so its eight sRGB cases compare the blending whichever way
the host encodes. A draw into an sRGB target is still encoded by the GPU.
- **Depth clears.** Microsoft documents a `[0, 1]` clamp. On Windows 11 build 26100, both WARP and native D3D12 on
an RTX 5070 Ti (610.74) preserved finite out-of-range `D32_FLOAT` values and quiet NaN in full and rectangle
clears. Replay preserves those values when the Vulkan device has enabled `VK_EXT_depth_range_unrestricted`.
Expand Down
46 changes: 43 additions & 3 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,17 +1126,55 @@ inline float quantize_snorm(float value, double maximum) {
return static_cast<float>(round_half_even(double(value) * maximum) / maximum);
}

// An sRGB format takes a linear value and stores its encoding. D3D lets an implementation place the 255 steps of
// that encoding within 0.6 of a step of the ideal curve, and each one measured places them elsewhere: of 60 000
// random values, an Intel Iris Xe (driver 32.0.101.7092) stores another byte than WARP for 6 649 and an NVIDIA RTX
// 4060 (616.92) for 3 648, in vkCmdClearColorImage and vkCmdClearAttachments alike (0.5 is 187 on the Intel GPU and
// 188 on the other two). So the value is encoded here, the way WARP does it, and Vulkan gets the linear value in the
// middle of that code's range (srgb8_linear), which both drivers store as that code for all 256. A GPU's own D3D12
// driver encodes as its Vulkan driver does, so where that differs from WARP the layer now differs from that driver:
// WARP is the reference here, as it is for the ties of quantize_unorm and for D24.
//
// WARP (10.0.26100.5074) takes the value to 12 bits first - x * 4095 in single precision, rounded half up - and
// encodes that with the curve of IEC 61966-2-1, rounded to nearest. Bisecting where its result changes gives 255
// floats, and this function changes at the same 255; it also agrees on 84 582 other values (docs/d3d12_vulkan.md).
// NaN and values below 0 are 0, values above 1 are 255.
inline unsigned srgb8_from_linear(float value) {
if (!(value > 0.0f)) return 0;
if (value >= 1.0f) return 255;
// The single-precision product, rounded to nearest even whatever the thread's rounding mode (see
// round_half_even): a double holds the product exactly (24 bits times 12), and a float keeps 24 bits of it.
const double product = double(value) * 4095.0;
if (product < 0.25) return 0; // rounds to the 12-bit value 0, and keeps denormals away from frexp
int exponent = 0;
std::frexp(product, &exponent);
const double ulp = std::ldexp(1.0, exponent - 24);
const double single = round_half_even(product / ulp) * ulp;
const double linear = std::floor(single + 0.5) / 4095.0;
const double encoded = linear < 0.0031308 ? 12.92 * linear : 1.055 * std::pow(linear, 1.0 / 2.4) - 0.055;
return static_cast<unsigned>(std::floor(encoded * 255.0 + 0.5));
}

// The linear value an 8-bit sRGB code stands for, which is the one farthest from the values a GPU may encode as a
// neighbouring code.
inline float srgb8_linear(unsigned code) {
if (code >= 255) return 1.0f;
const double encoded = code / 255.0;
return static_cast<float>(encoded <= 0.04045 ? encoded / 12.92 : std::pow((encoded + 0.055) / 1.055, 2.4));
}

// ClearRenderTargetView takes FLOATs even for integer RTVs. Windows D3D12 (WARP and AMD) clamps to the
// component range and truncates fractions. Vulkan instead interprets the matching union member as an integer
// already; copying float bits changes the value without a validation error. The normalised formats are rounded like
// D3D (quantize_unorm, quantize_snorm).
// D3D (quantize_unorm, quantize_snorm), and the colour channels of the sRGB formats are encoded like WARP
// (srgb8_from_linear).
inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearColor& input) {
// Integer formats convert the float numerically: NaN maps to zero, values below the range clamp to the low
// end, positive overflow (and +infinity) to the high end, and fractions truncate toward zero. Unsigned uses
// uint32, signed int32. A2B10G10R10 clamps RGB to 10 bits and alpha (channel 3) to 2 bits.
constexpr double u32_max = 4294967295.0, s32_min = -2147483648.0, s32_max = 2147483647.0;
double low = 0, high = 0, alpha_high = 0;
bool is_integer = true, is_signed = false;
bool is_integer = true, is_signed = false, srgb = false;
std::array<double, 4> unorm{}; // the largest value of each normalised channel; 0 leaves the channel to the GPU
double snorm = 0; // the largest value of a signed normalised format
switch (format) {
Expand Down Expand Up @@ -1170,8 +1208,9 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC
case VK_FORMAT_R8G8B8A8_UNORM:
case VK_FORMAT_B8G8R8A8_UNORM: is_integer = false; unorm = {255, 255, 255, 255}; break;
case VK_FORMAT_R8G8B8A8_SRGB:
case VK_FORMAT_B8G8R8A8_SRGB: // the colour channels go through the sRGB curve on the GPU
case VK_FORMAT_B8G8R8A8_SRGB: // the colour channels are encoded below; alpha is linear
is_integer = false;
srgb = true;
unorm = {0, 0, 0, 255};
break;
case VK_FORMAT_A2B10G10R10_UNORM_PACK32: is_integer = false; unorm = {1023, 1023, 1023, 3}; break;
Expand All @@ -1185,6 +1224,7 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC
for (std::size_t i = 0; i < input.size(); ++i) {
if (!is_integer) {
if (snorm != 0) value.float32[i] = quantize_snorm(input[i], snorm);
else if (srgb && i < 3) value.float32[i] = srgb8_linear(srgb8_from_linear(input[i]));
else value.float32[i] = unorm[i] != 0.0 ? quantize_unorm(input[i], unorm[i]) : input[i];
continue;
}
Expand Down
102 changes: 98 additions & 4 deletions runtime/test/clear_color_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Value-level replay regression: full and rectangular integer clears must carry integer values, not float bits,
// and normalised clears are rounded to nearest even like D3D (0.5 on 8 bits is 128).
// normalised clears are rounded to nearest even like D3D (0.5 on 8 bits is 128), and the colour channels of an sRGB
// clear are encoded like WARP (0.5 is code 188) and handed over as that code's linear value.
// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU tests.
#include "../src/vulkan_replay.h"
#include <array>
Expand All @@ -8,6 +9,7 @@
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>

namespace {
VkClearColorValue captured{};
Expand Down Expand Up @@ -45,6 +47,17 @@ bool replay_color(VkFormat format, const leonida::ClearColor& color, bool rectan
// Integer RTV format with its component range; A2B10G10R10 has a 2-bit alpha.
struct IntegerFormat { VkFormat format; const char* name; double low, high, alpha_high; bool is_signed; };

float from_bits(std::uint32_t word) {
float value;
std::memcpy(&value, &word, sizeof value);
return value;
}

// The ideal sRGB encoding of a linear value in 0..1 (IEC 61966-2-1), on a scale of 1.
double srgb_encode(double linear) {
return linear < 0.0031308 ? 12.92 * linear : 1.055 * std::pow(linear, 1.0 / 2.4) - 0.055;
}

// Reference conversion measured against WARP: NaN -> 0, clamp to the range, truncate fractions toward zero.
std::uint32_t expected_bits(float input, double low, double high, bool is_signed) {
const double v = double(input);
Expand Down Expand Up @@ -143,8 +156,6 @@ int main() {
{VK_FORMAT_A2B10G10R10_UNORM_PACK32, {0.5f, 0.5f, 0.5f, 0.5f}, {512, 512, 512, 2}, {1023, 1023, 1023, 3}},
{VK_FORMAT_R16G16B16A16_UNORM, {0.5f, 0.25f, 0.75f, 1.0f}, {32768, 16384, 49151, 65535},
{65535, 65535, 65535, 65535}},
{VK_FORMAT_R8G8B8A8_SRGB, {0.2f, 0.7f, 0.5f, 0.5f}, {0, 0, 0, 128}, {0, 0, 0, 255}}, // colour: the GPU's sRGB curve
{VK_FORMAT_B8G8R8A8_SRGB, {0.5f, 0.25f, 0.75f, 0.499f}, {0, 0, 0, 127}, {0, 0, 0, 255}},
{VK_FORMAT_R16_UNORM, {0.5f, 0, 0, 0}, {32768, 0, 0, 0}, {65535, 65535, 65535, 65535}},
// Signed normalised: -1.0 is -127 (-32767), the scale is 127 (32767) and ties go to the even neighbour.
{VK_FORMAT_R8G8B8A8_SNORM, {-1, -0.5f, 0.5f, 1}, {-127, -64, 64, 127}, {127, 127, 127, 127}},
Expand All @@ -166,6 +177,67 @@ int main() {
++checks;
}
}
// sRGB: the colour channels are encoded here the way WARP encodes them and handed over as the linear value of that
// code, so the byte does not depend on where a GPU puts the steps of its own curve; alpha is linear and rounds like
// UNORM. The codes are the bytes WARP 10.0.26100.5074 stored for these clears of an R8G8B8A8_UNORM_SRGB target.
// WARP takes the value to 12 bits first (x * 4095 in single precision, rounded half up), so some are not the
// nearest code of the ideal curve.
struct Srgb {
float linear;
unsigned code;
};
const Srgb srgb[] = {
{0.5f, 188}, {0.25f, 137}, {0.75f, 225}, {0.125f, 99}, {0.0039f, 13}, {0.5019f, 188}, {0.9961f, 255},
{0.2f, 124}, {0.7f, 218}, {0.0001f, 0}, {0.001f, 3}, {0.003f, 10}, {0.0031308f, 10}, {0.004f, 13},
{0.01f, 25}, {0.05f, 63}, {0.1f, 89}, {0.3f, 149}, {0.6f, 203}, {0.9f, 243}, {0.99f, 254}, {0.999f, 255},
{0.9999f, 255}, {0, 0}, {1, 255}, {-1, 0}, {2, 255}, {nan, 0}, {inf, 255}, {-inf, 0},
// The ideal curve gives 1.32, 2.64, 5.27 and 6.59 for these.
{0.0004f, 2}, {0.0008f, 2}, {0.0016f, 6}, {0.002f, 6},
// Neighbouring floats on the two sides of a step: x * 4095 is just below 0.5 and 0.5 after rounding to
// single precision (0.50000003 exactly); 2071.49976 and 2071.5 after rounding (2071.4999986 exactly).
{from_bits(0x39000800), 0}, {from_bits(0x39000801), 1}, {from_bits(0x3f018017), 188},
{from_bits(0x3f018018), 189},
};
for (const VkFormat format : {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_B8G8R8A8_SRGB}) {
for (std::size_t first = 0; first < std::size(srgb); first += 3) {
leonida::ClearColor color{0, 0, 0, first % 2 ? 0.499f : 0.5f};
unsigned codes[3] = {0, 0, 0};
for (std::size_t i = 0; i < 3 && first + i < std::size(srgb); ++i) {
color[i] = srgb[first + i].linear;
codes[i] = srgb[first + i].code;
}
for (const bool rect : {false, true}) {
if (!replay_color(format, color, rect)) return 1;
for (unsigned i = 0; i < 3; ++i) {
// The value is the middle of the code's range: its ideal encoding is the code itself.
const double encoded = srgb_encode(captured.float32[i]) * 255.0;
if (captured.float32[i] != leonida::detail::srgb8_linear(codes[i]) ||
std::fabs(encoded - codes[i]) > 0.001) {
std::fprintf(stderr, "sRGB clear: format %d rect %d value %.9g: got %.9g, which encodes to "
"%.4f, expected code %u\n", int(format), int(rect), double(color[i]),
double(captured.float32[i]), encoded, codes[i]);
return 1;
}
}
const float alpha = static_cast<float>((first % 2 ? 127.0 : 128.0) / 255.0);
if (captured.float32[3] != alpha) {
std::fprintf(stderr, "sRGB clear: format %d rect %d: alpha %.9g, expected %.9g\n", int(format),
int(rect), double(captured.float32[3]), double(alpha));
return 1;
}
++checks;
}
}
}
// Every code is the code of its own linear value, so a clear to a colour read back from an sRGB target stores
// that colour again.
for (unsigned code = 0; code < 256; ++code) {
if (leonida::detail::srgb8_from_linear(leonida::detail::srgb8_linear(code)) != code) {
std::fprintf(stderr, "sRGB code %u does not encode to itself\n", code);
return 1;
}
}
++checks;
// The other integer formats clamp to the component range and truncate toward zero (what WARP writes).
struct Integer {
VkFormat format;
Expand Down Expand Up @@ -225,11 +297,33 @@ int main() {
}
++checks;
}
// The sRGB encoding too: every 12-bit value and the floats around each step between two of them give the
// same code under each mode as under the default one (a float multiply or std::nearbyint would follow it).
std::vector<float> values;
for (unsigned j = 0; j < 4096; ++j) {
values.push_back(static_cast<float>(j / 4095.0));
const float step = static_cast<float>((j + 0.5) / 4095.0);
values.insert(values.end(), {std::nextafter(step, 0.0f), step, std::nextafter(step, 2.0f)});
}
std::vector<unsigned> codes[4];
for (int m = 0; m < 4; ++m) {
if (m < 3) std::fesetround(modes[m]);
for (const float v : values) codes[m].push_back(leonida::detail::srgb8_from_linear(v));
std::fesetround(mode);
}
for (int m = 0; m < 3; ++m) {
if (codes[m] != codes[3]) {
std::fprintf(stderr, "sRGB encoding under FP mode %d differs from the default mode\n", modes[m]);
return 1;
}
++checks;
}
}
if (failed_formats) {
std::fprintf(stderr, "clear_color_test: %u integer format(s) failed\n", failed_formats);
return 1;
}
std::printf("clear_color_test: %u full/rectangle integer, normalised and float replay checks passed\n", checks);
std::printf("clear_color_test: %u full/rectangle integer, normalised, sRGB and float replay checks passed\n",
checks);
return 0;
}
Loading
Loading