From a7b9ea19d9517eeeed7f226abf9f6711e18b9ca7 Mon Sep 17 00:00:00 2001 From: NWX10 <179748639+NWX10@users.noreply.github.com> Date: Sun, 22 Nov 2026 04:08:16 +0300 Subject: [PATCH 1/5] clears: tie rounding like D3D, and the rest of the format classes (D3D-45) Rebased onto main after #525, which added SNORM/SINT render targets for four formats. This keeps #525's formats and adds what it leaves out: - Normalised clears round to nearest even like D3D, so 0.5 on an 8-bit channel is 128 (NVIDIA's clears write 127). The layer hands Vulkan the exact fraction; NaN is 0 and out-of-range values clamp. Covers UNORM, SNORM (scale 127 or 32767, -1.0 is -127), the alpha of the sRGB formats, R10G10B10A2_UNORM and D16_UNORM depth. - More formats: R8/R8G8/R16/R16G16/R16G16B16A16 SNORM, R8/R16 SINT, R8/R16 UINT, R16_UNORM and R16G16_UNORM, with their copy sizes; SINT and UINT of every width clamp to the component range and truncate toward zero. - d3d12_clear_formats_test: 178 cases against WARP over UNORM, SNORM, sRGB, FLOAT, UINT, SINT and the four depth formats. typeless_rtv_test now expects the rounded UNORM value. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 6 + runtime/shims/d3d12_vulkan_device.cpp | 11 + runtime/src/vulkan_replay.h | 117 +++++-- runtime/test/clear_color_test.cpp | 77 ++++- runtime/test/d3d12_clear_formats_test.cpp | 395 ++++++++++++++++++++++ runtime/test/typeless_rtv_test.cpp | 4 +- 6 files changed, 569 insertions(+), 41 deletions(-) create mode 100644 runtime/test/d3d12_clear_formats_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 4cf9bd1839..1e14e50f90 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -553,6 +553,12 @@ if(TARGET Vulkan::Headers) target_compile_definitions(d3d12_typeless_rtv_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(d3d12_typeless_rtv_test PRIVATE /W4 /permissive- /utf-8) + # Clears on every format class against WARP: run explicitly (requires Vulkan hardware and Windows WARP). + add_executable(d3d12_clear_formats_test test/d3d12_clear_formats_test.cpp) + target_link_libraries(d3d12_clear_formats_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) + target_compile_definitions(d3d12_clear_formats_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_clear_formats_test PRIVATE /W4 /permissive- /utf-8) + # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan). if(TARGET gta6rt) target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 6267263bd2..1923581b97 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -87,6 +87,17 @@ Format vulkan_format(DXGI_FORMAT format, bool depth) { case DXGI_FORMAT_R8G8_SINT: return {VK_FORMAT_R8G8_SINT, color}; case DXGI_FORMAT_R16_FLOAT: return {VK_FORMAT_R16_SFLOAT, color}; case DXGI_FORMAT_R8_UNORM: return {VK_FORMAT_R8_UNORM, color}; + case DXGI_FORMAT_R16G16_UNORM: return {VK_FORMAT_R16G16_UNORM, color}; + case DXGI_FORMAT_R16_UNORM: return {VK_FORMAT_R16_UNORM, color}; + case DXGI_FORMAT_R16G16B16A16_SNORM: return {VK_FORMAT_R16G16B16A16_SNORM, color}; + case DXGI_FORMAT_R16G16_SNORM: return {VK_FORMAT_R16G16_SNORM, color}; + case DXGI_FORMAT_R16_SNORM: return {VK_FORMAT_R16_SNORM, color}; + case DXGI_FORMAT_R8G8_SNORM: return {VK_FORMAT_R8G8_SNORM, color}; + case DXGI_FORMAT_R8_SNORM: return {VK_FORMAT_R8_SNORM, color}; + case DXGI_FORMAT_R16_SINT: return {VK_FORMAT_R16_SINT, color}; + case DXGI_FORMAT_R16_UINT: return {VK_FORMAT_R16_UINT, color}; + case DXGI_FORMAT_R8_SINT: return {VK_FORMAT_R8_SINT, color}; + case DXGI_FORMAT_R8_UINT: return {VK_FORMAT_R8_UINT, color}; case DXGI_FORMAT_R32_FLOAT: return {VK_FORMAT_R32_SFLOAT, color}; case DXGI_FORMAT_R32_UINT: return {VK_FORMAT_R32_UINT, color}; case DXGI_FORMAT_R32_SINT: return {VK_FORMAT_R32_SINT, color}; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 3a5db673e2..b17d37b8a7 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -27,6 +27,8 @@ #include #include +#include +#include #include #include #include @@ -362,10 +364,11 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) return {1, 1}; switch (format) { case VK_FORMAT_R32G32B32A32_SFLOAT: - case VK_FORMAT_R32G32B32A32_UINT: - case VK_FORMAT_R32G32B32A32_SINT: return {16, 1}; + case VK_FORMAT_R32G32B32A32_SINT: + case VK_FORMAT_R32G32B32A32_UINT: return {16, 1}; case VK_FORMAT_R16G16B16A16_SFLOAT: case VK_FORMAT_R16G16B16A16_UNORM: + case VK_FORMAT_R16G16B16A16_SNORM: case VK_FORMAT_R16G16B16A16_UINT: case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R32G32_SFLOAT: @@ -385,6 +388,8 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R32_SFLOAT: + case VK_FORMAT_R16G16_UNORM: + case VK_FORMAT_R16G16_SNORM: case VK_FORMAT_R32_UINT: case VK_FORMAT_R32_SINT: case VK_FORMAT_D32_SFLOAT: @@ -394,9 +399,16 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8_SINT: case VK_FORMAT_R16_SFLOAT: + case VK_FORMAT_R8G8_SNORM: case VK_FORMAT_R16_UNORM: + case VK_FORMAT_R16_SNORM: + case VK_FORMAT_R16_UINT: + case VK_FORMAT_R16_SINT: case VK_FORMAT_D16_UNORM: return {2, 1}; - case VK_FORMAT_R8_UNORM: return {1, 1}; + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8_SNORM: + case VK_FORMAT_R8_UINT: + case VK_FORMAT_R8_SINT: return {1, 1}; case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: case VK_FORMAT_BC4_UNORM_BLOCK: @@ -612,51 +624,81 @@ struct Prepared { VkImageResolve resolve{}; // resolves }; -// ClearRenderTargetView takes FLOATs even for integer RTVs. For the UINT formats currently exposed by -// the device, 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. +// D3D converts a float clear value to a normalised format by rounding to nearest even, so an exact tie goes to the +// even neighbour (0.5 on 8 bits is 128). Vulkan leaves the rounding of ties to the GPU, and NVIDIA writes 127, one +// step below WARP and AMD. The value is rounded here and handed over as the exact fraction, which no GPU rounds +// differently. NaN is 0 and values outside 0..1 clamp, as in D3D. +inline float quantize_unorm(float value, double maximum) { + if (!(value > 0.0f)) return 0.0f; + if (value >= 1.0f) return 1.0f; + return static_cast(std::nearbyint(double(value) * maximum) / maximum); +} + +// The same for a signed normalised format: -1..1 on a scale of 127 (8 bits) or 32767 (16 bits), where D3D writes -1.0 +// as -127 and ties go to the even neighbour. +inline float quantize_snorm(float value, double maximum) { + if (!(value > -1.0f)) return value != value ? 0.0f : -1.0f; + if (value >= 1.0f) return 1.0f; + return static_cast(std::nearbyint(double(value) * maximum) / maximum); +} + +// ClearRenderTargetView takes FLOATs even for integer RTVs. For the integer formats exposed by the device, Windows +// D3D12 (WARP and AMD) clamps to the component range and truncates fractions toward zero. Vulkan instead interprets +// the matching union member as an integer already; copying float bits changes the value. +// The normalised formats are rounded like D3D (quantize_unorm, quantize_snorm). inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearColor& input) { - // Integer formats convert the float numerically: NaN maps to zero, negatives clamp to the low end, positive - // overflow to the high end, and fractions truncate toward zero. Unsigned uses uint32, signed uses int32. - std::uint32_t unsigned_maximum = 0; - std::int64_t signed_minimum = 0, signed_maximum = 0; + double minimum = 0, maximum = 0; // the component range of an integer format; maximum 0: not an integer format bool is_signed = 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) { - case VK_FORMAT_R8G8B8A8_UINT: unsigned_maximum = 255; break; + case VK_FORMAT_R8_UINT: + case VK_FORMAT_R8G8B8A8_UINT: maximum = 255; break; + case VK_FORMAT_R16_UINT: maximum = 65535; break; case VK_FORMAT_R32_UINT: - case VK_FORMAT_R32G32B32A32_UINT: unsigned_maximum = std::numeric_limits::max(); break; - case VK_FORMAT_R8G8B8A8_SINT: signed_minimum = -128; signed_maximum = 127; is_signed = true; break; + case VK_FORMAT_R32G32B32A32_UINT: maximum = std::numeric_limits::max(); break; + case VK_FORMAT_R8_SINT: + case VK_FORMAT_R8G8B8A8_SINT: is_signed = true; minimum = -128; maximum = 127; break; + case VK_FORMAT_R16_SINT: is_signed = true; minimum = -32768; maximum = 32767; break; case VK_FORMAT_R32_SINT: case VK_FORMAT_R32G32B32A32_SINT: - signed_minimum = std::numeric_limits::min(); - signed_maximum = std::numeric_limits::max(); is_signed = true; + minimum = std::numeric_limits::min(); + maximum = std::numeric_limits::max(); break; + case VK_FORMAT_R8_SNORM: + case VK_FORMAT_R8G8_SNORM: + case VK_FORMAT_R8G8B8A8_SNORM: snorm = 127; break; + case VK_FORMAT_R16_SNORM: + case VK_FORMAT_R16G16_SNORM: + case VK_FORMAT_R16G16B16A16_SNORM: snorm = 32767; break; + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8G8_UNORM: + case VK_FORMAT_R8G8B8A8_UNORM: + case VK_FORMAT_B8G8R8A8_UNORM: unorm = {255, 255, 255, 255}; break; + case VK_FORMAT_R8G8B8A8_SRGB: + case VK_FORMAT_B8G8R8A8_SRGB: unorm = {0, 0, 0, 255}; break; // the colour channels go through the sRGB curve + case VK_FORMAT_A2B10G10R10_UNORM_PACK32: unorm = {1023, 1023, 1023, 3}; break; + case VK_FORMAT_R16_UNORM: + case VK_FORMAT_R16G16_UNORM: + case VK_FORMAT_R16G16B16A16_UNORM: unorm = {65535, 65535, 65535, 65535}; break; default: break; } VkClearColorValue value{}; for (std::size_t i = 0; i < input.size(); ++i) { - if (is_signed) { - const double v = double(input[i]); - std::int32_t out; - if (v != v) { - out = 0; // NaN - } else if (v <= double(signed_minimum)) { - out = static_cast(signed_minimum); - } else if (v >= double(signed_maximum)) { - out = static_cast(signed_maximum); - } else { - out = static_cast(v); // truncates toward zero - } - value.int32[i] = out; - } else if (!unsigned_maximum) { - value.float32[i] = input[i]; - } else if (!(input[i] > 0.0f)) { - value.uint32[i] = 0; // NaN, negative values and either zero; no out-of-range float-to-int cast. - } else if (double(input[i]) >= double(unsigned_maximum)) { - value.uint32[i] = unsigned_maximum; // Includes +infinity; UINT32_MAX itself is not representable as float. + const double x = input[i]; + if (maximum == 0) { + if (snorm != 0) value.float32[i] = quantize_snorm(input[i], snorm); + else value.float32[i] = unorm[i] != 0.0 ? quantize_unorm(input[i], unorm[i]) : input[i]; + } else if (!is_signed) { + if (!(x > 0.0)) value.uint32[i] = 0; // NaN, negative values and either zero; no out-of-range float-to-int cast. + else if (x >= maximum) value.uint32[i] = static_cast(maximum); // includes +infinity + else value.uint32[i] = static_cast(input[i]); } else { - value.uint32[i] = static_cast(input[i]); + if (x != x) value.int32[i] = 0; + else if (x <= minimum) value.int32[i] = static_cast(minimum); // includes -infinity + else if (x >= maximum) value.int32[i] = static_cast(maximum); + else value.int32[i] = static_cast(input[i]); // toward zero } } return value; @@ -1301,7 +1343,10 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false); auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); - const VkClearDepthStencilValue value{depth_clear->depth, depth_clear->stencil}; + const float depth = p.binding.format == VK_FORMAT_D16_UNORM + ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm + : depth_clear->depth; + const VkClearDepthStencilValue value{depth, depth_clear->stencil}; if (p.rects.empty()) { detail::round_trip(vk, command_buffer, p.binding.image, pieces, image_layout(p.binding, transfer_dst.layout), state, transfer_dst.access, transfer_dst.stages, [&] { diff --git a/runtime/test/clear_color_test.cpp b/runtime/test/clear_color_test.cpp index 8da8c9eef4..43b764a462 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 UINT clears must carry integer values, not float bits. -// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU test. +// Value-level replay regression: full and rectangular UINT 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). +// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU tests. #include "../src/vulkan_replay.h" #include #include @@ -68,7 +69,7 @@ int main() { ++checks; } } - for (const VkFormat format : {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_SFLOAT}) { + for (const VkFormat format : {VK_FORMAT_R32_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT}) { for (const bool rect : {false, true}) { if (!replay_color(format, c.color, rect) || std::memcmp(captured.float32, c.color.data(), sizeof(captured)) != 0) return 1; @@ -76,6 +77,74 @@ int main() { } } } - std::printf("clear_color_test: %u full/rectangle UINT and float replay checks passed\n", checks); + // Normalised formats: D3D rounds to nearest even, the clamp and NaN rules are 0..1 and 0, and an exact tie goes to + // the even neighbour. NVIDIA rounds 0.5 on 8 bits down to 127 by itself, so the value is rounded before Vulkan sees it. + struct Norm { + VkFormat format; + leonida::ClearColor color; + std::array steps; // what D3D writes, in units of the channel's largest value + std::array maximum; + }; + const Norm norms[] = { + {VK_FORMAT_R8G8B8A8_UNORM, {0.5f, 0.499f, 0.501f, 1.0f}, {128, 127, 128, 255}, {255, 255, 255, 255}}, + {VK_FORMAT_B8G8R8A8_UNORM, {-1, 2, nan, 0.5f}, {0, 255, 0, 128}, {255, 255, 255, 255}}, + {VK_FORMAT_R8G8_UNORM, {0.5f, 1.5f, 0, 0}, {128, 255, 0, 0}, {255, 255, 255, 255}}, + {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}}, + {VK_FORMAT_R8G8_SNORM, {nan, inf, -inf, -0.5f}, {0, 127, -127, -64}, {127, 127, 127, 127}}, + {VK_FORMAT_R16G16B16A16_SNORM, {-2, 2, 0.001f, -0.001f}, {-32767, 32767, 33, -33}, + {32767, 32767, 32767, 32767}}, + }; + for (const auto& n : norms) { + for (const bool rect : {false, true}) { + if (!replay_color(n.format, n.color, rect)) return 1; + for (unsigned i = 0; i < 4; ++i) { + const float expected = n.maximum[i] != 0.0 ? static_cast(double(n.steps[i]) / n.maximum[i]) : n.color[i]; + if (captured.float32[i] != expected) { + std::fprintf(stderr, "normalised clear: format %d rect %d channel %u: got %.9g, expected %.9g\n", + int(n.format), int(rect), i, double(captured.float32[i]), double(expected)); + return 1; + } + } + ++checks; + } + } + // The other integer formats clamp to the component range and truncate toward zero (what WARP writes). + struct Integer { + VkFormat format; + leonida::ClearColor color; + std::array expected; + }; + const Integer integers[] = { + {VK_FORMAT_R8_SINT, {-1.5f, -2.5f, 2.5f, 3.5f}, {-1, -2, 2, 3}}, + {VK_FORMAT_R8G8B8A8_SINT, {-129, 128, -32769, 32768}, {-128, 127, -128, 127}}, + {VK_FORMAT_R8G8B8A8_SINT, {2147483648.0f, -2147483904.0f, nan, inf}, {127, -128, 0, 127}}, + {VK_FORMAT_R16_SINT, {-129, 128, -32769, 32768}, {-129, 128, -32768, 32767}}, + {VK_FORMAT_R32_SINT, {-129, 128, -32769, 32768}, {-129, 128, -32769, 32768}}, + {VK_FORMAT_R32_SINT, {2147483648.0f, -2147483904.0f, nan, inf}, {2147483647, -2147483647 - 1, 0, 2147483647}}, + {VK_FORMAT_R32G32B32A32_SINT, {-inf, 0.5f, -0.5f, 100000.0f}, {-2147483647 - 1, 0, 0, 100000}}, + {VK_FORMAT_R8_UINT, {256, 1.5f, -1, nan}, {255, 1, 0, 0}}, + {VK_FORMAT_R16_UINT, {65536, 65535.5f, -0.0f, 2.5f}, {65535, 65535, 0, 2}}, + }; + for (const auto& c : integers) { + for (const bool rect : {false, true}) { + if (!replay_color(c.format, c.color, rect)) return 1; + for (unsigned i = 0; i < 4; ++i) { + if (captured.int32[i] != c.expected[i]) { + std::fprintf(stderr, "integer clear: format %d rect %d channel %u: got %d, expected %d\n", + int(c.format), int(rect), i, captured.int32[i], c.expected[i]); + return 1; + } + } + ++checks; + } + } + std::printf("clear_color_test: %u full/rectangle UINT, SINT, normalised 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 new file mode 100644 index 0000000000..4746fbb564 --- /dev/null +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -0,0 +1,395 @@ +// Clears compared with native Windows D3D12/WARP across the format classes (D3D-45): ClearRenderTargetView on +// UNORM, SNORM, SRGB, FLOAT, UINT and SINT formats and ClearDepthStencilView on every depth format, each with and +// without rectangles. Every case clears, copies the result into a readback buffer through D3D12 and compares the +// bytes with what WARP wrote. Extends d3d12_uint_clear_test.cpp (UINT only). +// No game data or shaders. Run explicitly (needs Vulkan hardware and WARP); LEONIDA_VK_VALIDATION=1 checks Vulkan +// usage. A format the layer cannot create yet is listed as "not exposed" and does not fail the test; any other +// difference does. +#include "vk_test_device.h" +#include "../shims/d3d12_com.h" +#include +#include +#include +#include +#include +#include +#include +#define CHECK(condition) do { \ + if (!(condition)) { std::fprintf(stderr, " line %d: %s\n", __LINE__, #condition); return false; } \ +} while (0) + +namespace { +template +struct Ref { + T* p = nullptr; + ~Ref() { if (p) p->Release(); } + T* operator->() const { return p; } +}; +struct Event { + HANDLE handle = CreateEventW(nullptr, FALSE, FALSE, nullptr); + ~Event() { if (handle) CloseHandle(handle); } +}; + +constexpr UINT kWidth = 5, kHeight = 3; // odd sizes expose a wrong treatment of footprint padding +using Color = std::array; +constexpr float kNaN = std::numeric_limits::quiet_NaN(); +constexpr float kInf = std::numeric_limits::infinity(); + +// Records the copy of one subresource into a readback buffer, to be read after the list has run. +struct Plane { + Ref buffer; + D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{}; + UINT64 total = 0; + unsigned bytes = 0; // per pixel in this plane +}; + +struct Run { + Ref queue; + Ref allocator; + Ref list; + Ref fence; + + bool create(ID3D12Device* device) { + const D3D12_COMMAND_QUEUE_DESC queue_desc{}; + CHECK(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); + CHECK(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); + CHECK(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + CHECK(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + return true; + } + bool execute() { + CHECK(SUCCEEDED(list->Close())); + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + CHECK(SUCCEEDED(queue->Signal(fence.p, 1))); + Event event; + CHECK(event.handle); + CHECK(SUCCEEDED(fence->SetEventOnCompletion(1, event.handle))); + CHECK(WaitForSingleObject(event.handle, 5000) == WAIT_OBJECT_0); + return true; + } +}; + +D3D12_RESOURCE_DESC texture_desc(DXGI_FORMAT format, D3D12_RESOURCE_FLAGS flags) { + D3D12_RESOURCE_DESC texture{}; + texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + texture.Width = kWidth; + texture.Height = kHeight; + texture.DepthOrArraySize = texture.MipLevels = 1; + texture.Format = format; + texture.SampleDesc.Count = 1; + texture.Flags = flags; + return texture; +} + +void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* image, D3D12_RESOURCE_STATES before, + D3D12_RESOURCE_STATES after) { + D3D12_RESOURCE_BARRIER barrier{}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Transition.pResource = image; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.StateBefore = before; + barrier.Transition.StateAfter = after; + list->ResourceBarrier(1, &barrier); +} + +bool add_copy(ID3D12Device* device, ID3D12GraphicsCommandList* list, ID3D12Resource* image, + const D3D12_RESOURCE_DESC& texture, UINT subresource, unsigned bytes, Plane& plane) { + plane.bytes = bytes; + device->GetCopyableFootprints(&texture, subresource, 1, 0, &plane.placed, nullptr, nullptr, &plane.total); + D3D12_RESOURCE_DESC buffer{}; + buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer.Width = plane.total; + buffer.Height = buffer.SampleDesc.Count = 1; + buffer.DepthOrArraySize = buffer.MipLevels = 1; + buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_READBACK; + CHECK(SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &buffer, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&plane.buffer.p)))); + D3D12_TEXTURE_COPY_LOCATION src{}, dst{}; + src.pResource = image; + src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + src.SubresourceIndex = subresource; + dst.pResource = plane.buffer.p; + dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + dst.PlacedFootprint = plane.placed; + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + return true; +} + +bool read_plane(Plane& plane, std::vector& pixels) { + void* mapped = nullptr; + const D3D12_RANGE range{0, SIZE_T(plane.total)}; + CHECK(SUCCEEDED(plane.buffer->Map(0, &range, &mapped)) && mapped); + pixels.resize(kWidth * kHeight * plane.bytes); + for (UINT y = 0; y < kHeight; ++y) + std::memcpy(pixels.data() + y * kWidth * plane.bytes, + static_cast(mapped) + y * plane.placed.Footprint.RowPitch, kWidth * plane.bytes); + const D3D12_RANGE none{}; + plane.buffer->Unmap(0, &none); + return true; +} + +// The two results of a case: false when the device could not run it at all. +enum class Result { ok, not_created, failed }; + +Result clear_color(ID3D12Device* device, DXGI_FORMAT format, unsigned bytes, const Color& full, const Color& part, + std::vector& pixels) { + Run run; + if (!run.create(device)) return Result::failed; + Ref image; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + const D3D12_RESOURCE_DESC texture = texture_desc(format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); + if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &texture, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&image.p)))) + return Result::not_created; + Ref rtv_heap; + const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + if (FAILED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&rtv_heap.p)))) return Result::failed; + const auto handle = rtv_heap->GetCPUDescriptorHandleForHeapStart(); + device->CreateRenderTargetView(image.p, nullptr, handle); + run.list->ClearRenderTargetView(handle, full.data(), 0, nullptr); + const D3D12_RECT rect{1, 1, 4, 3}; + run.list->ClearRenderTargetView(handle, part.data(), 1, &rect); + transition(run.list.p, image.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + Plane plane; + if (!add_copy(device, run.list.p, image.p, texture, 0, bytes, plane) || !run.execute() || + !read_plane(plane, pixels)) + return Result::failed; + return Result::ok; +} + +struct DepthValues { + float depth_full; + UINT8 stencil_full; + float depth_part; + UINT8 stencil_part; +}; + +// Depth and stencil planes after: a full clear of both, a depth-only clear of one rectangle and a stencil-only +// clear of another. planes[1] is empty for a depth-only format. +Result clear_depth(ID3D12Device* device, DXGI_FORMAT format, bool stencil, unsigned depth_bytes, + const DepthValues& v, std::vector planes[2]) { + Run run; + if (!run.create(device)) return Result::failed; + Ref image; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + const D3D12_RESOURCE_DESC texture = texture_desc(format, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); + if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &texture, + D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, IID_PPV_ARGS(&image.p)))) + return Result::not_created; + Ref dsv_heap; + const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + if (FAILED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&dsv_heap.p)))) return Result::failed; + const auto handle = dsv_heap->GetCPUDescriptorHandleForHeapStart(); + device->CreateDepthStencilView(image.p, nullptr, handle); + const D3D12_CLEAR_FLAGS both = stencil ? D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL : D3D12_CLEAR_FLAG_DEPTH; + run.list->ClearDepthStencilView(handle, both, v.depth_full, v.stencil_full, 0, nullptr); + const D3D12_RECT depth_rect{1, 1, 4, 3}; + run.list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, v.depth_part, 0, 1, &depth_rect); + if (stencil) { + const D3D12_RECT stencil_rect{0, 0, 3, 2}; + run.list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_STENCIL, 0.0f, v.stencil_part, 1, &stencil_rect); + } + transition(run.list.p, image.p, D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE); + Plane depth, stencil_plane; + if (!add_copy(device, run.list.p, image.p, texture, 0, depth_bytes, depth) || + (stencil && !add_copy(device, run.list.p, image.p, texture, 1, 1, stencil_plane)) || !run.execute() || + !read_plane(depth, planes[0]) || (stencil && !read_plane(stencil_plane, planes[1]))) + return Result::failed; + return Result::ok; +} + +struct ColorCase { + const char* group; + const char* name; + DXGI_FORMAT format; + unsigned bytes; + const std::vector* colors; +}; + +// Out-of-range values clamp in the normalised classes; NaN is 0 there. NaN is left out of the float classes, whose +// payload bits are not defined. +const std::vector kUnorm = {{0, 0.25f, 0.5f, 1}, {1, 0.75f, 0.125f, 0.5f}, {-1, 2, 0.5f, 1.5f}, + {0.0039f, 0.5019f, 0.9961f, 0.3333f}, {kNaN, kInf, -kInf, 0.5f}}; +const std::vector kSnorm = {{-1, -0.5f, 0.5f, 1}, {-2, 2, 0.001f, -0.001f}, {0, 0.25f, -0.25f, 0.7f}, + {kNaN, kInf, -kInf, -0.5f}}; +const std::vector kFloat = {{0.1f, 1.5f, -3.25f, 65504.0f}, {1e-8f, 70000.0f, -70000.0f, 0.5f}, + {0, 1, 2, 3}, {kInf, -kInf, 1e-5f, 123456.0f}}; +const std::vector kUint = {{1, 2, 3, 4}, {1.5f, 2.5f, 3.5f, 4.5f}, {-1, -0.0f, 254.5f, 255.5f}, + {256, 65536, 16777216, 4294967296.0f}, {kNaN, kInf, -kInf, 0.5f}, + {0.499f, 0.501f, 1.499f, 1.501f}, {4294967040.0f, 3.4e38f, 255, 0}}; +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}}; + +const ColorCase kColorCases[] = { + {"UNORM", "R8_UNORM", DXGI_FORMAT_R8_UNORM, 1, &kUnorm}, + {"UNORM", "R8G8_UNORM", DXGI_FORMAT_R8G8_UNORM, 2, &kUnorm}, + {"UNORM", "R8G8B8A8_UNORM", DXGI_FORMAT_R8G8B8A8_UNORM, 4, &kUnorm}, + {"UNORM", "B8G8R8A8_UNORM", DXGI_FORMAT_B8G8R8A8_UNORM, 4, &kUnorm}, + {"UNORM", "R10G10B10A2_UNORM", DXGI_FORMAT_R10G10B10A2_UNORM, 4, &kUnorm}, + {"UNORM", "R16_UNORM", DXGI_FORMAT_R16_UNORM, 2, &kUnorm}, + {"UNORM", "R16G16_UNORM", DXGI_FORMAT_R16G16_UNORM, 4, &kUnorm}, + {"UNORM", "R16G16B16A16_UNORM", DXGI_FORMAT_R16G16B16A16_UNORM, 8, &kUnorm}, + {"SNORM", "R8_SNORM", DXGI_FORMAT_R8_SNORM, 1, &kSnorm}, + {"SNORM", "R8G8_SNORM", DXGI_FORMAT_R8G8_SNORM, 2, &kSnorm}, + {"SNORM", "R8G8B8A8_SNORM", DXGI_FORMAT_R8G8B8A8_SNORM, 4, &kSnorm}, + {"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}, + {"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}, + {"FLOAT", "R32_FLOAT", DXGI_FORMAT_R32_FLOAT, 4, &kFloat}, + {"FLOAT", "R32G32_FLOAT", DXGI_FORMAT_R32G32_FLOAT, 8, &kFloat}, + {"FLOAT", "R32G32B32A32_FLOAT", DXGI_FORMAT_R32G32B32A32_FLOAT, 16, &kFloat}, + {"FLOAT", "R11G11B10_FLOAT", DXGI_FORMAT_R11G11B10_FLOAT, 4, &kFloat}, + {"UINT", "R8_UINT", DXGI_FORMAT_R8_UINT, 1, &kUint}, + {"UINT", "R16_UINT", DXGI_FORMAT_R16_UINT, 2, &kUint}, + {"UINT", "R8G8B8A8_UINT", DXGI_FORMAT_R8G8B8A8_UINT, 4, &kUint}, + {"UINT", "R32_UINT", DXGI_FORMAT_R32_UINT, 4, &kUint}, + {"UINT", "R32G32B32A32_UINT", DXGI_FORMAT_R32G32B32A32_UINT, 16, &kUint}, + {"SINT", "R8_SINT", DXGI_FORMAT_R8_SINT, 1, &kSint}, + {"SINT", "R16_SINT", DXGI_FORMAT_R16_SINT, 2, &kSint}, + {"SINT", "R8G8B8A8_SINT", DXGI_FORMAT_R8G8B8A8_SINT, 4, &kSint}, + {"SINT", "R32_SINT", DXGI_FORMAT_R32_SINT, 4, &kSint}, + {"SINT", "R32G32B32A32_SINT", DXGI_FORMAT_R32G32B32A32_SINT, 16, &kSint}, +}; + +struct DepthCase { + const char* name; + DXGI_FORMAT format; + bool stencil; + unsigned depth_bytes; +}; + +const DepthCase kDepthCases[] = { + {"D16_UNORM", DXGI_FORMAT_D16_UNORM, false, 2}, + {"D32_FLOAT", DXGI_FORMAT_D32_FLOAT, false, 4}, + {"D24_UNORM_S8_UINT", DXGI_FORMAT_D24_UNORM_S8_UINT, true, 4}, + {"D32_FLOAT_S8X24_UINT", DXGI_FORMAT_D32_FLOAT_S8X24_UINT, true, 4}, +}; + +const DepthValues kDepthValues[] = {{0.25f, 0x5A, 0.75f, 0xA5}, {1.0f, 0xFF, 0.0f, 0x00}, + {0.3333f, 0x01, 0.9999f, 0x80}, {0.5f, 0x7F, 0.0001f, 0x10}}; + +struct Tally { + unsigned matched = 0, differ = 0, not_exposed = 0, failed = 0, skipped = 0; +}; + +// Names the first byte that differs. +void report(const char* group, const char* name, unsigned index, const char* what, + const std::vector& actual, const std::vector& expected) { + for (std::size_t b = 0; b < actual.size() && b < expected.size(); ++b) { + if (actual[b] != expected[b]) { + std::printf("DIFF %-6s %-22s case %u %s byte %zu: layer %02x, WARP %02x\n", group, name, index, what, b, + unsigned(actual[b]), unsigned(expected[b])); + return; + } + } + std::printf("DIFF %-6s %-22s case %u %s: sizes %zu and %zu\n", group, name, index, what, actual.size(), + expected.size()); +} +} // namespace + +int main() { + vktest::Device vk; + if (!vk.create()) return 1; + const unsigned errors_before = vk.validation_errors(); + Tally tally; + { + Ref ours, host; + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = vk.instance_proc; + info.instance = vk.instance; + info.physical = vk.physical; + info.device = vk.device; + info.queue = vk.queue; + info.queue_family = vk.family; + if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; + Ref factory; + Ref warp; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) || + FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) || + FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) { + std::fprintf(stderr, "WARP reference unavailable: differential test cannot run\n"); + return 1; + } + for (const ColorCase& c : kColorCases) { + const auto& colors = *c.colors; + bool reported_exposure = false; + for (unsigned i = 0; i < colors.size(); ++i) { + // Different colours outside and inside the rectangle exercise both Vulkan clear paths. + const Color& full = colors[i]; + const Color& part = colors[(i + 1) % colors.size()]; + std::vector actual, expected; + const Result want = clear_color(host.p, c.format, c.bytes, full, part, expected); + if (want != Result::ok) { + ++tally.skipped; // WARP itself does not offer the format as a render target here + continue; + } + const Result got = clear_color(ours.p, c.format, c.bytes, full, part, actual); + if (got == Result::not_created) { + ++tally.not_exposed; + if (!reported_exposure) std::printf("not exposed: %-6s %s\n", c.group, c.name); + reported_exposure = true; + continue; + } + if (got != Result::ok) { + ++tally.failed; + std::printf("FAIL %-6s %-22s case %u: the layer could not run it\n", c.group, c.name, i); + } else if (actual != expected) { + ++tally.differ; + report(c.group, c.name, i, "colour", actual, expected); + } else { + ++tally.matched; + } + } + } + for (const DepthCase& c : kDepthCases) { + bool reported_exposure = false; + for (unsigned i = 0; i < std::size(kDepthValues); ++i) { + std::vector actual[2], expected[2]; + const Result want = clear_depth(host.p, c.format, c.stencil, c.depth_bytes, kDepthValues[i], expected); + if (want != Result::ok) { + ++tally.skipped; + continue; + } + const Result got = clear_depth(ours.p, c.format, c.stencil, c.depth_bytes, kDepthValues[i], actual); + if (got == Result::not_created) { + ++tally.not_exposed; + if (!reported_exposure) std::printf("not exposed: DEPTH %s\n", c.name); + reported_exposure = true; + continue; + } + if (got != Result::ok) { + ++tally.failed; + std::printf("FAIL DEPTH %-22s case %u: the layer could not run it\n", c.name, i); + continue; + } + bool same = true; + for (int plane = 0; plane < (c.stencil ? 2 : 1); ++plane) { + if (actual[plane] != expected[plane]) { + same = false; + report("DEPTH", c.name, i, plane ? "stencil" : "depth", actual[plane], expected[plane]); + } + } + if (same) ++tally.matched; + else ++tally.differ; + } + } + } // Destroy the D3D12 objects before checking the Vulkan validation counter. + std::printf("clears: %u matched WARP, %u differ, %u failed, %u in formats the layer does not expose, " + "%u skipped (WARP does not offer the format)\n", + tally.matched, tally.differ, tally.failed, tally.not_exposed, tally.skipped); + if (tally.differ || tally.failed) return 1; + if (vk.validation_errors() != errors_before) return 1; + return 0; +} diff --git a/runtime/test/typeless_rtv_test.cpp b/runtime/test/typeless_rtv_test.cpp index b7536e2cf5..6a2529beb5 100644 --- a/runtime/test/typeless_rtv_test.cpp +++ b/runtime/test/typeless_rtv_test.cpp @@ -91,7 +91,9 @@ int main() { if (format == ColorViewFormat::rgba8_uint) { EXPECT(captured.uint32[0] == 1 && captured.uint32[1] == 2 && captured.uint32[2] == 255 && captured.uint32[3] == 0); } else { - EXPECT(std::memcmp(captured.float32, color.data(), sizeof(captured)) == 0); + // UNORM and sRGB views round and clamp like D3D (render_target_clear_color): 1.5 and 256 are 1.0, -1 is 0. + const VkClearColorValue converted = leonida::detail::render_target_clear_color(expected, color); + EXPECT(std::memcmp(captured.float32, converted.float32, sizeof(captured)) == 0); } ++checks; } From 6ff753db81f999e412775700a5b855e783080dc0 Mon Sep 17 00:00:00 2001 From: marquezini Date: Sun, 22 Nov 2026 06:04:27 -0300 Subject: [PATCH 2/5] clears: round ties to even whatever the FP mode, and run d3d12_clear in ctest quantize_unorm/quantize_snorm used std::nearbyint, which follows the thread's rounding mode (matheuskshn's review): under FE_DOWNWARD 0.5 on 8 bits gave 127 and 0.5 on SNORM8 gave 63. round_half_even uses floor() plus an explicit tie check, which does not depend on the mode. clear_color_test converts the ties under FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO and checks them once the default mode is back; with nearbyint it fails as above. d3d12_clear was left out of ctest until this lands (3fc0b21); it is registered again, with SKIP_RETURN_CODE 77 as before. --- runtime/CMakeLists.txt | 3 ++- runtime/src/vulkan_replay.h | 13 +++++++++++-- runtime/test/clear_color_test.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2d9f3cbed1..221e65d2a2 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -828,7 +828,8 @@ if(TARGET Vulkan::Headers) target_link_libraries(d3d12_clear_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) target_compile_definitions(d3d12_clear_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(d3d12_clear_test PRIVATE /W4 /permissive- /utf-8) - # Not in ctest yet: on NVIDIA it fails on the 0.5 tie rounding #521 fixes; register it once that lands. + add_test(NAME d3d12_clear COMMAND d3d12_clear_test) + set_tests_properties(d3d12_clear PROPERTIES SKIP_RETURN_CODE 77) # Synthetic typed-view readbacks against Windows WARP; run explicitly on a Vulkan GPU. add_executable(d3d12_typeless_rtv_test test/d3d12_typeless_rtv_test.cpp) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index a5cf801e42..8d02a8ca17 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -762,10 +762,19 @@ struct Prepared { // even neighbour (0.5 on 8 bits is 128). Vulkan leaves the rounding of ties to the GPU, and NVIDIA writes 127, one // step below WARP and AMD. The value is rounded here and handed over as the exact fraction, which no GPU rounds // differently. NaN is 0 and values outside 0..1 clamp, as in D3D. +// +// Ties go to even whatever the thread's floating-point rounding mode: std::nearbyint follows it, so a game that +// changes the mode would round differently. floor() does not depend on it, and x + 0.5 is exact here (x is at most +// a float times 65535, well inside a double's 53 bits). +inline double round_half_even(double x) { + const double r = std::floor(x + 0.5); + return r - x == 0.5 && std::fmod(r, 2.0) != 0.0 ? r - 1.0 : r; +} + inline float quantize_unorm(float value, double maximum) { if (!(value > 0.0f)) return 0.0f; if (value >= 1.0f) return 1.0f; - return static_cast(std::nearbyint(double(value) * maximum) / maximum); + return static_cast(round_half_even(double(value) * maximum) / maximum); } // The same for a signed normalised format: -1..1 on a scale of 127 (8 bits) or 32767 (16 bits), where D3D writes -1.0 @@ -773,7 +782,7 @@ inline float quantize_unorm(float value, double maximum) { inline float quantize_snorm(float value, double maximum) { if (!(value > -1.0f)) return value != value ? 0.0f : -1.0f; if (value >= 1.0f) return 1.0f; - return static_cast(std::nearbyint(double(value) * maximum) / maximum); + return static_cast(round_half_even(double(value) * maximum) / maximum); } // ClearRenderTargetView takes FLOATs even for integer RTVs. Windows D3D12 (WARP and AMD) clamps to the diff --git a/runtime/test/clear_color_test.cpp b/runtime/test/clear_color_test.cpp index 43884bb7dd..fd350f6f4a 100644 --- a/runtime/test/clear_color_test.cpp +++ b/runtime/test/clear_color_test.cpp @@ -3,6 +3,7 @@ // No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU tests. #include "../src/vulkan_replay.h" #include +#include #include #include #include @@ -195,6 +196,30 @@ int main() { ++checks; } } + // Ties go to even whatever the thread's floating-point rounding mode (std::nearbyint would follow it: 127.5 is + // 127 when rounding down). Converted under each mode, checked after the default mode is back. + { + const int mode = std::fegetround(); + VkClearColorValue unorm[3]{}, snorm[3]{}; + const int modes[3] = {FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO}; + for (int m = 0; m < 3; ++m) { + std::fesetround(modes[m]); + unorm[m] = leonida::detail::render_target_clear_color(VK_FORMAT_R8G8B8A8_UNORM, {0.5f, 0.5f, 0.5f, 0.5f}); + snorm[m] = leonida::detail::render_target_clear_color(VK_FORMAT_R8G8B8A8_SNORM, {-0.5f, 0.5f, -0.5f, 0.5f}); + } + std::fesetround(mode); + for (int m = 0; m < 3; ++m) { + const long u = std::lround(double(unorm[m].float32[0]) * 255.0); + const long s0 = std::lround(double(snorm[m].float32[0]) * 127.0); + const long s1 = std::lround(double(snorm[m].float32[1]) * 127.0); + if (u != 128 || s0 != -64 || s1 != 64) { + std::fprintf(stderr, "tie rounding under FP mode %d: UNORM %ld (128), SNORM %ld %ld (-64 64)\n", + modes[m], u, s0, s1); + return 1; + } + ++checks; + } + } if (failed_formats) { std::fprintf(stderr, "clear_color_test: %u integer format(s) failed\n", failed_formats); return 1; From 2bee23fe7940a186ea9d487394470893a9a33928 Mon Sep 17 00:00:00 2001 From: marquezini Date: Sun, 22 Nov 2026 14:05:02 -0300 Subject: [PATCH 3/5] d3d12_clear_formats_test: register it with ctest, exit 77 without a Vulkan GPU main's test_ctest_registration (#1258) requires every built test to have an add_test. Like the other D3D12 GPU tests, it now returns 77 when no Vulkan device is available and is registered with SKIP_RETURN_CODE 77. --- runtime/CMakeLists.txt | 4 +++- runtime/test/d3d12_clear_formats_test.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 57beceaffc..72195e59a7 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1621,11 +1621,13 @@ if(TARGET Vulkan::Headers) message(STATUS "llvm-mc (set LLVM_MC) or glslangValidator not found: d3d12_subgroup_test is not built") endif() - # Clears on every format class against WARP: run explicitly (requires Vulkan hardware and Windows WARP). + # Clears on every format class against WARP (needs a Vulkan GPU; exit 77 without one). add_executable(d3d12_clear_formats_test test/d3d12_clear_formats_test.cpp) target_link_libraries(d3d12_clear_formats_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) target_compile_definitions(d3d12_clear_formats_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(d3d12_clear_formats_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_clear_formats COMMAND d3d12_clear_formats_test) + set_tests_properties(d3d12_clear_formats PROPERTIES SKIP_RETURN_CODE 77) # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan). if(TARGET gta6rt) diff --git a/runtime/test/d3d12_clear_formats_test.cpp b/runtime/test/d3d12_clear_formats_test.cpp index 4746fbb564..2c67d5563f 100644 --- a/runtime/test/d3d12_clear_formats_test.cpp +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -301,7 +301,9 @@ void report(const char* group, const char* name, unsigned index, const char* wha int main() { vktest::Device vk; - if (!vk.create()) return 1; + const auto device_status = vk.initialize(); + if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips + if (device_status != vktest::DeviceStatus::ready) return 1; const unsigned errors_before = vk.validation_errors(); Tally tally; { From 4e596d1a6dd844366d76c6e459a9f04c1ecafb1a Mon Sep 17 00:00:00 2001 From: marquezini Date: Sun, 22 Nov 2026 14:12:39 -0300 Subject: [PATCH 4/5] d3d12_vulkan_device_test: accept the layer's nearest-even tie on its own adapter The placed plain clear (D3D-90/D3D-221) is compared byte for byte with the host D3D12 runtime when the host is the layer's own adapter. Its 0.5 * 255 = 127.5 is a tie: NVIDIA's D3D12 driver writes 0x7f, WARP and AMD 0x80, and the D3D11.3 spec (3.2.3.6) permits either within 0.6 ULP. With this branch the layer rounds normalised clear values itself, to nearest even, so on an NVIDIA adapter it writes 0x80 and the exact same-adapter comparison failed (RTX 5070, 621.24). On the same adapter a differing plain byte is now accepted only when it is exactly the nearest-even code and within 0.6 ULP; every other difference still fails, and the SRGB-cast comparison is unchanged. --- runtime/test/d3d12_vulkan_device_test.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 91d3244ce8..4e43049b81 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -1621,6 +1621,13 @@ bool within_06_ulp(std::uint8_t a, std::uint8_t b, double ideal) { return std::abs(double(a) - ideal) <= 0.6 && std::abs(double(b) - ideal) <= 0.6; } +// The code nearest to `ideal`, a tie going to the even one: what the layer writes for a normalised clear (#521's +// quantize_unorm), whichever way the GPU's own driver rounds the tie. +double nearest_even_code(double ideal) { + const double r = std::floor(ideal + 0.5); + return r - ideal == 0.5 && std::fmod(r, 2.0) != 0.0 ? r - 1.0 : r; +} + bool create_host_device_for_physical(VkInstance instance, VkPhysicalDevice physical, PFN_vkGetInstanceProcAddr instance_proc, Ref& host, std::string& adapter_name) { @@ -6418,6 +6425,9 @@ int main() { // WARP and NVIDIA's Vulkan driver give 188, Intel's Vulkan driver 187 (UHD 770 101.7092), both within // 0.6 ULP. On the layer's own adapter (same LUID) the comparison stays exact; against another adapter // or WARP a byte may differ when both sides are within 0.6 ULP of the ideal value. + // The plain clear's normalised values are rounded by the layer itself (nearest even, WARP's and AMD's + // code), so on its own NVIDIA adapter the tie byte may be the step above the driver's: accepted only + // when it is exactly that nearest-even code and within 0.6 ULP. const FLOAT clear_value[4] = {0.25f, 0.5f, 0.75f, 1.0f}; // placed_cast_scenario's clear colour const auto within_tolerance = [&](std::uint8_t ours_byte, std::uint8_t host_byte, double ideal) { return !same_adapter && within_06_ulp(ours_byte, host_byte, ideal); @@ -6430,7 +6440,9 @@ int main() { const double plain_ideal = c * 255.0; const double srgb_ideal = (i % 4 == 3 ? c : ideal_srgb(c)) * 255.0; // alpha stays linear if (ours[b] != host[b]) { - if (within_tolerance(ours[b], host[b], plain_ideal)) + const bool layer_tie = double(ours[b]) == nearest_even_code(plain_ideal) && + within_06_ulp(ours[b], host[b], plain_ideal); + if (within_tolerance(ours[b], host[b], plain_ideal) || layer_tie) ++plain_within; else if (differences++ < 8) std::fprintf(stderr, "placed plain clear row %u byte %u: ours %02x, host %02x\n", row, i, From c0a92ebcfc6b04ac43e683f8c9eda4f1bfee6bd5 Mon Sep 17 00:00:00 2001 From: NWX10 <179748639+NWX10@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:46:14 +0300 Subject: [PATCH 5/5] d3d12_vulkan_device: keep main's R8 case order (merge noise) Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 35f723e538..2b6d1c8060 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -202,10 +202,10 @@ Format vulkan_format(DXGI_FORMAT format, bool depth) { case DXGI_FORMAT_R8G8_SINT: return {VK_FORMAT_R8G8_SINT, color}; case DXGI_FORMAT_R16_FLOAT: return {VK_FORMAT_R16_SFLOAT, color}; case DXGI_FORMAT_R8_UNORM: return {VK_FORMAT_R8_UNORM, color}; + case DXGI_FORMAT_R8_TYPELESS: return {VK_FORMAT_R8_UNORM, color, true}; case DXGI_FORMAT_R8_SNORM: return {VK_FORMAT_R8_SNORM, color}; - case DXGI_FORMAT_R8_SINT: return {VK_FORMAT_R8_SINT, color}; case DXGI_FORMAT_R8_UINT: return {VK_FORMAT_R8_UINT, color}; - case DXGI_FORMAT_R8_TYPELESS: return {VK_FORMAT_R8_UNORM, color, true}; + case DXGI_FORMAT_R8_SINT: return {VK_FORMAT_R8_SINT, color}; case DXGI_FORMAT_R32_FLOAT: return {VK_FORMAT_R32_SFLOAT, color}; case DXGI_FORMAT_R32_UINT: return {VK_FORMAT_R32_UINT, color}; case DXGI_FORMAT_R32_SINT: return {VK_FORMAT_R32_SINT, color};