diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3cd9d6d5e8..1a09b8007d 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -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`. diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index bcaea02f8a..749cb76a61 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -1126,17 +1126,55 @@ inline float quantize_snorm(float value, double maximum) { return static_cast(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(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(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 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) { @@ -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; @@ -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; } diff --git a/runtime/test/clear_color_test.cpp b/runtime/test/clear_color_test.cpp index 6f792cd83a..f82a28032f 100644 --- a/runtime/test/clear_color_test.cpp +++ b/runtime/test/clear_color_test.cpp @@ -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 @@ -8,6 +9,7 @@ #include #include #include +#include namespace { VkClearColorValue captured{}; @@ -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); @@ -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}}, @@ -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((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; @@ -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 values; + for (unsigned j = 0; j < 4096; ++j) { + values.push_back(static_cast(j / 4095.0)); + const float step = static_cast((j + 0.5) / 4095.0); + values.insert(values.end(), {std::nextafter(step, 0.0f), step, std::nextafter(step, 2.0f)}); + } + std::vector 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; } diff --git a/runtime/test/d3d12_clear_formats_test.cpp b/runtime/test/d3d12_clear_formats_test.cpp index 33088abeec..38c0eebfef 100644 --- a/runtime/test/d3d12_clear_formats_test.cpp +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -8,7 +8,9 @@ #include "vk_test_device.h" #include "../shims/d3d12_com.h" #include +#include #include +#include #include #include #include @@ -232,6 +234,31 @@ const std::vector kUint = {{1, 2, 3, 4}, {1.5f, 2.5f, 3.5f, 4.5f}, {-1, - const std::vector kSint = {{-1, -2, -128, 127}, {-1.5f, -2.5f, 2.5f, 3.5f}, {-129, 128, -32769, 32768}, {2147483648.0f, -2147483904.0f, kNaN, kInf}, {-kInf, 0.5f, -0.5f, 100000.0f}}; +// The linear value of an sRGB encoding given on the scale of the 8-bit codes (IEC 61966-2-1). +float srgb_linear(double code) { + const double encoded = code / 255.0; + return static_cast(encoded <= 0.04045 ? encoded / 12.92 : std::pow((encoded + 0.055) / 1.055, 2.4)); +} + +// The sRGB formats take the UNORM colours and then, three to a colour, the linear value of each of the 256 codes and +// of each of the 255 places halfway between two codes. A GPU stores a code's own value as that code, but each one +// (and WARP) puts the step between two codes somewhere else near the halfway value, so a clear that leaves the +// encoding to the GPU differs from WARP on many of the second kind and on a few of the UNORM colours (0.5 is 187 on +// an Intel Iris Xe and 188 on WARP). +std::vector srgb_colors() { + std::vector values; + for (int code = 0; code < 256; ++code) values.push_back(srgb_linear(code)); + for (int code = 0; code < 255; ++code) values.push_back(srgb_linear(code + 0.5)); + std::vector colors = kUnorm; + for (std::size_t i = 0; i < values.size(); i += 3) { + const std::size_t last = values.size() - 1; + colors.push_back({values[i], values[std::min(i + 1, last)], values[std::min(i + 2, last)], + i % 2 ? 0.25f : 1.0f}); + } + return colors; +} +const std::vector kSrgb = srgb_colors(); + const ColorCase kColorCases[] = { {"UNORM", "R8_UNORM", DXGI_FORMAT_R8_UNORM, 1, &kUnorm}, {"UNORM", "R8G8_UNORM", DXGI_FORMAT_R8G8_UNORM, 2, &kUnorm}, @@ -247,8 +274,8 @@ const ColorCase kColorCases[] = { {"SNORM", "R16_SNORM", DXGI_FORMAT_R16_SNORM, 2, &kSnorm}, {"SNORM", "R16G16_SNORM", DXGI_FORMAT_R16G16_SNORM, 4, &kSnorm}, {"SNORM", "R16G16B16A16_SNORM", DXGI_FORMAT_R16G16B16A16_SNORM, 8, &kSnorm}, - {"SRGB", "R8G8B8A8_UNORM_SRGB", DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 4, &kUnorm}, - {"SRGB", "B8G8R8A8_UNORM_SRGB", DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, 4, &kUnorm}, + {"SRGB", "R8G8B8A8_UNORM_SRGB", DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 4, &kSrgb}, + {"SRGB", "B8G8R8A8_UNORM_SRGB", DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, 4, &kSrgb}, {"FLOAT", "R16_FLOAT", DXGI_FORMAT_R16_FLOAT, 2, &kFloat}, {"FLOAT", "R16G16_FLOAT", DXGI_FORMAT_R16G16_FLOAT, 4, &kFloat}, {"FLOAT", "R16G16B16A16_FLOAT", DXGI_FORMAT_R16G16B16A16_FLOAT, 8, &kFloat}, diff --git a/runtime/test/d3d12_clear_test.cpp b/runtime/test/d3d12_clear_test.cpp index abc3b0bd79..4fa86045dd 100644 --- a/runtime/test/d3d12_clear_test.cpp +++ b/runtime/test/d3d12_clear_test.cpp @@ -495,8 +495,8 @@ int main(int argc, char** argv) { {65535.5f, 65536, 1023.5f, 1024}, {3.5f, 4, 2147483520.0f, -2147483648.0f} }; // Float-valued formats (UNORM/SNORM/FLOAT); out-of-range values exercise clamping. NaN/inf and SRGB are - // omitted: NaN/inf conversion is driver-defined, and the linear->sRGB encoding rounds differently at the .5 - // boundary on WARP vs Vulkan (a 1-ulp driver difference the shim cannot control). + // omitted: NaN/inf conversion is driver-defined, and d3d12_clear_formats_test compares the SRGB clears (the + // layer encodes them itself, as WARP does). const std::array float_values[] = { {0, 0.25f, 0.5f, 1}, {1, 1, 1, 1}, {0.5f, 0.5f, 0.5f, 0.5f}, {-0.5f, 0, 1.5f, 2} }; @@ -556,9 +556,10 @@ int main(int argc, char** argv) { // RTVs through a castable-format list in another channel order or family, or a typed BGRA8 texture's sRGB // view: the clear is stored in the view's channel order and encoding, as WARP and AMD's D3D12 driver store // it. An X8 view leaves its X byte undefined (WARP and AMD's driver differ), so it isn't compared. The bytes - // are compared within 1: drivers round the 0.5 tie differently (WARP 128, NVIDIA 127), and Intel's encodes - // sRGB 1 off WARP in places (its own D3D12 driver stores the same bytes as the layer there); a wrong channel - // order or a missing sRGB encoding is many steps off. + // are compared within 1, from when the GPU converted the value: drivers round the 0.5 tie differently (WARP + // 128, NVIDIA 127) and Intel's encodes sRGB 1 off WARP in places (in its own D3D12 driver too). The layer now + // does both itself (render_target_clear_color); a wrong channel order or a missing sRGB encoding is many + // steps off. // A host that can't take a castable-format list (Windows 10's d3d12.dll has no ID3D12Device10; the in-box WARP // 10.0.22621.5415 on Windows 11 23H2 reports RelaxedFormatCastingSupported = 0 and CreateCommittedResource3 // fails) clears a texture in the view's own format instead: the bytes are stored in the view's channel order diff --git a/runtime/test/d3d12_state_sweep_test.cpp b/runtime/test/d3d12_state_sweep_test.cpp index 0ce033e99f..2bf196cb49 100644 --- a/runtime/test/d3d12_state_sweep_test.cpp +++ b/runtime/test/d3d12_state_sweep_test.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -336,6 +337,19 @@ std::vector build_cases() { c.name = tag + "no blend"; c.format = f; cases.push_back(std::move(c)); + // An sRGB target starts from the linear values of three codes (124, 170 and 203, near the 0.2, 0.4 and 0.6 of + // the other targets), which every implementation measured stores as those codes. Another value lands on the + // code above or below it depending on who encodes: the layer does as WARP does, a GPU's D3D12 driver as its + // hardware does (0.2 is 124 on WARP and 123 on an Intel Iris Xe), and this sweep is about the blending. + if (f == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB || f == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB) { + for (std::size_t i = cases.size() - 4; i < cases.size(); ++i) { + const unsigned codes[3] = {124, 170, 203}; + for (int channel = 0; channel < 3; ++channel) { + const double encoded = codes[channel] / 255.0; + cases[i].clear_color[channel] = static_cast(std::pow((encoded + 0.055) / 1.055, 2.4)); + } + } + } } // Depth: the draw's depth is 0; the clear is 0 (EQUAL-ish) or 0.5. for (int func = 1; func <= 8; ++func)