Skip to content
Merged
11 changes: 10 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,8 @@ if(TARGET Vulkan::Headers)
target_compile_options(d3d12_clear_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_rgb9e5_rtv COMMAND d3d12_clear_test --rgb9e5)
set_tests_properties(d3d12_rgb9e5_rtv PROPERTIES SKIP_RETURN_CODE 77)
# 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)
Expand Down Expand Up @@ -1890,6 +1891,14 @@ 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 (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)
target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com)
Expand Down
63 changes: 58 additions & 5 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <functional>
#include <limits>
Expand Down Expand Up @@ -410,8 +411,8 @@ 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:
Expand Down Expand Up @@ -1009,16 +1010,46 @@ struct PreparedDraw {
}
};

// 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.
//
// 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Avoid the mode-sensitive addition for tiny positive values

x can be much smaller than one double ULP at 0.5 (for example, float denorm_min multiplied by 255). Under FE_UPWARD, x + 0.5 rounds to the next double above 0.5, so floor returns 1 and the clear becomes 1/255 instead of 0. This contradicts the comment that the sum is exact and breaks the intended rounding-mode independence for near-zero inputs. Please handle this range without adding the tiny value to 0.5, and include a near-zero case in the rounding-mode coverage.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching the assumption about x + 0.5. I ran the three clear helpers verbatim from c0a92ebc with native MSVC /fp:strict and all four fesetround modes.

For float denorm_min() scaled by 255, FE_UPWARD moves the sum to nextafter(0.5, 1.0). That is still below 1, so floor returns 0. R8 UNORM with the positive input and R8 SNORM with both signs returned zero in all four modes.

I also checked 598,284 scaled inputs per mode at the half-integer thresholds and neighboring float values across the supported scales 3, 127, 255, 1023, 32767 and 65535. All selected the expected integer. A near-zero regression case would cover the distinction: the addition can round while staying in the same interval for floor.

AI-assisted.

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<float>(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
// 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<float>(round_half_even(double(value) * maximum) / maximum);
}

// 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.
// already; copying float bits changes the value without a validation error. 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, 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;
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) {
case VK_FORMAT_R8_UINT:
case VK_FORMAT_R8G8_UINT:
Expand All @@ -1039,13 +1070,33 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC
case VK_FORMAT_R32_SINT:
case VK_FORMAT_R32G32_SINT:
case VK_FORMAT_R32G32B32A32_SINT: low = s32_min; high = s32_max; is_signed = true; break;
case VK_FORMAT_R8_SNORM:
case VK_FORMAT_R8G8_SNORM:
case VK_FORMAT_R8G8B8A8_SNORM: is_integer = false; snorm = 127; break;
case VK_FORMAT_R16_SNORM:
case VK_FORMAT_R16G16_SNORM:
case VK_FORMAT_R16G16B16A16_SNORM: is_integer = false; snorm = 32767; break;
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_R8G8_UNORM:
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
is_integer = false;
unorm = {0, 0, 0, 255};
break;
case VK_FORMAT_A2B10G10R10_UNORM_PACK32: is_integer = false; unorm = {1023, 1023, 1023, 3}; break;
case VK_FORMAT_R16_UNORM:
case VK_FORMAT_R16G16_UNORM:
case VK_FORMAT_R16G16B16A16_UNORM: is_integer = false; unorm = {65535, 65535, 65535, 65535}; break;
default: is_integer = false; break;
}
if (format != VK_FORMAT_A2B10G10R10_UINT_PACK32) alpha_high = high;
VkClearColorValue value{};
for (std::size_t i = 0; i < input.size(); ++i) {
if (!is_integer) {
value.float32[i] = input[i];
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];
continue;
}
const double maximum = i == 3 ? alpha_high : high;
Expand Down Expand Up @@ -3237,7 +3288,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
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 float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h
: depth_clear->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, pieces, image_layout(p.binding, transfer_dst.layout),
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,7 +1,9 @@
// Value-level replay regression: full and rectangular integer 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 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).
// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU tests.
#include "../src/vulkan_replay.h"
#include <array>
#include <cfenv>
#include <cmath>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -116,7 +118,7 @@ int main() {
if (!format_ok) ++failed_formats;
}
for (const auto& c : cases) {
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, rect) || std::memcmp(captured.float32, c.data(), sizeof(captured)) != 0) {
std::fprintf(stderr, "float clear: format %d rect %d changed the value\n", int(format), int(rect));
Expand All @@ -126,10 +128,102 @@ int main() {
}
}
}
// 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<std::int32_t, 4> steps; // what D3D writes, in units of the channel's largest value
std::array<double, 4> 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<float>(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<std::int32_t, 4> 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;
}
}
// 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;
}
std::printf("clear_color_test: %u full/rectangle integer and float replay checks passed\n", checks);
std::printf("clear_color_test: %u full/rectangle integer, normalised and float replay checks passed\n", checks);
return 0;
}
Loading
Loading