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
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4251,6 +4251,13 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(clear_color_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME clear_color COMMAND clear_color_test)
# CPU side of the Depth24 emulation (src/depth24.h) under every floating-point rounding mode; no GPU, no Vulkan.
add_executable(depth24_cpu_test test/depth24_cpu_test.cpp)
target_link_libraries(depth24_cpu_test PRIVATE Vulkan::Headers)
if(WIN32)
target_compile_definitions(depth24_cpu_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME depth24_cpu COMMAND depth24_cpu_test)
# FramePacer of the Xbox presentation path on a fake clock (src/frame_pacer.h); no GPU, no Vulkan.
add_executable(frame_pacer_test test/frame_pacer_test.cpp)
find_package(Threads REQUIRED)
Expand Down
36 changes: 31 additions & 5 deletions runtime/src/depth24.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <vulkan/vulkan.h>

#include <bit>
#include <cmath>
#include <cstdint>

Expand All @@ -28,19 +29,44 @@ inline constexpr std::uint32_t depth24_max = 0xffffff;
// The conversion of a float to a 24-bit UNORM as WARP and AMD's D3D12 drivers do it (measured against both, 5215
// clear values, ties and their neighbours included, in d3d12_depth24_compare.h): NaN and values below 0 give 0, 1 and above the
// maximum, anything in between multiplies by 2^24 - 1 in float (a rounded product, so a value a hair over a tie can
// land on it) and then rounds to nearest, ties to even (std::nearbyint's default mode). NVIDIA's D3D12 driver breaks
// land on it) and then rounds to nearest, ties to even. NVIDIA's D3D12 driver breaks
// some of those ties the other way, and its Vulkan driver, which has a native D24, agrees with it; a texture the
// layer stores as D32 follows WARP and AMD, which is what the Xbox build's own hardware is.
//
// Both roundings (the product to a float, the float to an integer) are the GPU's round-to-nearest-even whatever the
// calling thread's floating-point rounding mode: a clear can run on a thread that set FE_DOWNWARD or FE_TOWARDZERO,
// and a float multiply, std::nearbyint and a double-to-float conversion all follow that mode (0x1p-23f gives 1 rather
// than 2 when rounding down). So the product is formed exactly in a double (24 bits times 24 bits fit in 53, so no
// rounding happens in any mode) and rounded by depth24_detail::round_even, which uses floor, fmod and ldexp only: those
// are exact in every mode and need no fenv access.
namespace depth24_detail {
// x >= 0 and below 2^52 with x + 0.5 exact (callers pass at most 24 integer bits and 24 fraction bits).
inline double round_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;
}
} // namespace depth24_detail

inline std::uint32_t depth24_quantize(float value) {
if (!(value > 0.0f)) return 0;
if (value >= 1.0f) return depth24_max;
const float product = value * 16777215.0f; // a float multiply, not a double one
return static_cast<std::uint32_t>(std::nearbyint(product));
const double exact = double(value) * 16777215.0; // exact: 24 + 24 bits
// The float product the GPU multiply gives: 24 significant bits, ties to even.
int exponent = 0;
std::frexp(exact, &exponent); // exact = f * 2^exponent, f in [0.5, 1)
const double product = std::ldexp(depth24_detail::round_even(std::ldexp(exact, 24 - exponent)), exponent - 24);
return static_cast<std::uint32_t>(depth24_detail::round_even(product));
}

// The float stored for a 24-bit UNORM: the nearest float to d / 16777215 (quantising it gives d back).
// The float stored for a 24-bit UNORM: the nearest float to d / 16777215 (quantising it gives d back). The same
// arithmetic as depth24_convert.comp, no division: for d = m * 2^(e-23), m of 24 bits, the quotient is exactly
// float(m + 1) * 2^(e-47) (see the shader). Every step is exact, so the thread's rounding mode does not matter.
inline float depth24_to_float(std::uint32_t d) {
return static_cast<float>(double(d & depth24_max) / double(depth24_max));
d &= depth24_max;
if (d == 0) return 0.0f;
const int e = std::bit_width(d) - 1;
const std::uint32_t m = d << (23 - e);
return std::ldexp(static_cast<float>(m + 1u), e - 47);
}

// A depth value as the layer's D32 must hold it for a D24 texture: what a clear writes, so that reading the texture
Expand Down
129 changes: 129 additions & 0 deletions runtime/test/depth24_cpu_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// The CPU side of the Depth24 emulation (src/depth24.h: depth24_quantize, depth24_to_float, depth24_clear_value) must
// equal depth24_convert.comp (roundEven of a correctly rounded float product; integer arithmetic for the upload) on a
// thread that has changed its floating-point rounding mode: a float multiply, std::nearbyint and a double-to-float
// conversion follow that mode, so 0x1p-23f quantised to 1 under FE_DOWNWARD and FE_TOWARDZERO instead of 2.
// Under each mode the results must equal the round-to-nearest ones (a float multiply and std::nearbyint, run under
// the default mode only) and fixed expectations, ties at the integer boundary included. The test sets the mode itself;
// the header never does. No Vulkan loader, GPU, shaders or game data.
#include "../src/depth24.h"

#include <cfenv>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>

namespace {

float as_float(std::uint32_t bits) {
float f;
std::memcpy(&f, &bits, 4);
return f;
}

// The reference, used under round-to-nearest only: a float multiply, then nearbyint (ties to even).
std::uint32_t reference_quantize(float value) {
if (!(value > 0.0f)) return 0;
if (value >= 1.0f) return leonida::depth24_max;
const volatile float product = value * 16777215.0f;
return static_cast<std::uint32_t>(std::nearbyint(product));
}

std::vector<float> corpus() {
std::vector<float> values = {0.0f, -0.0f, -1.0f, 1.0f, 2.0f, 0.5f, 0x1p-23f, 0x1p-24f, 0x1p-25f, 0x1p-26f,
0x1p-149f, 0x1p-126f, std::nextafter(1.0f, 0.0f), std::nextafter(1.0f, 2.0f),
as_float(0x7fc00000u), as_float(0x7f800000u), as_float(0xff800000u)};
// A stride through the bit patterns of [0, 1] (a prime step), the floats around every 251st code's midpoint
// (d + 0.5) / 16777215, where the product is a tie or a hair off one, and the powers of two.
for (std::uint32_t bits = 0; bits <= 0x3f800000u; bits += 4099u) values.push_back(as_float(bits));
for (std::uint32_t d = 0; d < leonida::depth24_max; d += 251u) {
const float tie = float((double(d) + 0.5) / 16777215.0);
values.push_back(tie);
values.push_back(std::nextafter(tie, 0.0f));
values.push_back(std::nextafter(tie, 1.0f));
}
for (int k = 0; k < 24; ++k) values.push_back(std::ldexp(1.0f, -k));
return values;
}

} // namespace

int main() {
using namespace leonida;
unsigned checks = 0;

// Fixed expectations. 0x1p-23f * 16777215 is 2 - 2^-23, representable, and nearest to 2 (rounding down gave 1).
struct Case { float value; std::uint32_t code; };
const Case cases[] = {
{0x1p-23f, 2}, {0.0f, 0}, {-1.0f, 0}, {1.0f, depth24_max}, {2.0f, depth24_max}, {0x1p-25f, 0}, {0x1p-24f, 1},
{0.5f, 8388608}, // 0.5 * 16777215 = 8388607.5 exactly: the tie goes to the even neighbour
};
const int modes[] = {FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO};
const int saved = std::fegetround();
for (const int mode : modes) {
std::fesetround(mode);
std::vector<std::uint32_t> got;
for (const Case& c : cases) got.push_back(depth24_quantize(c.value));
std::fesetround(saved);
for (std::size_t i = 0; i < got.size(); ++i) {
if (got[i] != cases[i].code) {
std::fprintf(stderr, "FP mode %d: depth24_quantize(%a) = %u, expected %u\n", mode,
double(cases[i].value), got[i], cases[i].code);
return 1;
}
++checks;
}
}

const std::vector<float> values = corpus();
std::vector<std::uint32_t> expected(values.size());
for (std::size_t i = 0; i < values.size(); ++i) expected[i] = reference_quantize(values[i]);
for (const int mode : modes) {
std::fesetround(mode);
std::vector<std::uint32_t> got(values.size());
std::vector<float> clears(values.size());
for (std::size_t i = 0; i < values.size(); ++i) {
got[i] = depth24_quantize(values[i]);
clears[i] = depth24_clear_value(values[i]);
}
// The upload direction: a stride through the codes, and the ends.
std::vector<float> floats;
for (std::uint32_t d = 0; d <= depth24_max; d += 4093u) floats.push_back(depth24_to_float(d));
floats.push_back(depth24_to_float(depth24_max));
std::fesetround(saved);

for (std::size_t i = 0; i < values.size(); ++i) {
if (got[i] != expected[i]) {
std::fprintf(stderr, "FP mode %d: depth24_quantize(%a) = %u, round-to-nearest gives %u\n", mode,
double(values[i]), got[i], expected[i]);
return 1;
}
const float want = float(double(expected[i]) / 16777215.0); // nearest, under the restored mode
if (std::memcmp(&clears[i], &want, 4) != 0) {
std::fprintf(stderr, "FP mode %d: depth24_clear_value(%a) = %a, expected %a\n", mode,
double(values[i]), double(clears[i]), double(want));
return 1;
}
++checks;
}
std::size_t k = 0;
for (std::uint32_t d = 0; d <= depth24_max; d += 4093u, ++k) {
const float want = float(double(d) / 16777215.0);
if (std::memcmp(&floats[k], &want, 4) != 0 || depth24_quantize(floats[k]) != d) {
std::fprintf(stderr, "FP mode %d: depth24_to_float(%u) = %a, expected %a\n", mode, d,
double(floats[k]), double(want));
return 1;
}
++checks;
}
const float top = floats.back();
if (top != 1.0f) {
std::fprintf(stderr, "FP mode %d: depth24_to_float(0xffffff) = %a, expected 1\n", mode, double(top));
return 1;
}
}
std::printf("depth24_cpu_test: %u checks passed under FE_TONEAREST, FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO\n",
checks);
return 0;
}