Files
mandy/runtime/test/xbox_bvh_builder_rules_test.cpp
Tyrannys e7280be166 xbox_bvh_builder: scan every postbuild description for 0x10; rehydrate scratch regions cannot wrap
From kevincommits' Finding 19 (item 3) in #1608.

- builder_build_batched2x kept 16 postbuild descriptions and then left the
  loop, so an offline-statistics (0x10) description at index 17 or later was
  never seen: the element was built instead of skipped, as the DLL would
  refuse it. The rest past 16 are still dropped (logged once) but scanned.
- builder_rehydrate_batched2x aligned each element's scratch size with
  (size + 255) / 256 * 256 and summed them without a bound, so a size near
  2^64 wrapped to a small region or a small total, making the shared scratch
  buffer too small and the regions overlap. Such an element now gets no
  region (and is skipped as one without scratch, logged once).

Both rules move into shims/xbox_bvh_builder_rules.h (filter_postbuild,
add_scratch_region) with a CPU test, xbox_bvh_builder_rules: 0x10 at indices
0, 5, 16, 17 and 19 of 20, and regions near 2^64. The old loop and
arithmetic fail 5 of its checks. xbox_bvh_builder (GPU) and its BVH check
still pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 19:39:04 -06:00

70 lines
3.6 KiB
C++

// The XboxBvhBuilder shim's postbuild filter and rehydrate scratch regions (shims/xbox_bvh_builder_rules.h), on the
// CPU, following kevincommits' finding (#1608, Finding 19, item 3): an offline-statistics (0x10) postbuild
// description after 16 kept ones still skips the element, and a scratch size near 2^64 neither wraps its alignment
// nor the batch's total. No device, no game data.
#include "../shims/xbox_bvh_builder_rules.h"
#include <cstdio>
#include <initializer_list>
namespace {
int failures = 0;
void check(bool ok, const char* what) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
using Post = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC;
constexpr auto kCompacted = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE;
const auto kStatistics = static_cast<D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE>(
leonida::xdxr::kPostbuildInfoOfflineStatistics);
} // namespace
int main() {
using namespace rt::xbox_bvh;
Post kept[kMaxKeptPostbuild];
// 20 descriptions: 0x10 at `at`, the rest COMPACTED_SIZE.
for (const UINT at : {0u, 5u, 16u, 17u, 19u}) {
Post post[20];
for (UINT i = 0; i < 20; ++i) post[i] = {0x1000 + 8ull * i, kCompacted};
post[at].InfoType = kStatistics;
const auto f = filter_postbuild(post, 20, kept);
char what[96];
std::snprintf(what, sizeof(what), "0x10 at index %u of 20 skips the element", at);
check(f.skipped_statistics, what);
check(f.kept == kMaxKeptPostbuild && f.dropped, "the other 19: 16 kept, the rest dropped");
}
{
Post post[20];
for (UINT i = 0; i < 20; ++i) post[i] = {0x1000 + 8ull * i, kCompacted};
const auto f = filter_postbuild(post, 20, kept);
check(!f.skipped_statistics && f.kept == 16 && f.dropped && kept[15].DestBuffer == 0x1000 + 8 * 15,
"20 without 0x10: the first 16 kept, in order");
const auto g = filter_postbuild(post, 3, kept);
check(!g.skipped_statistics && g.kept == 3 && !g.dropped, "3 without 0x10: all kept");
const auto none = filter_postbuild(nullptr, 5, kept);
check(none.kept == 0 && !none.skipped_statistics, "no array: nothing kept");
}
// Scratch regions: aligned, in order; a size or total that would wrap is refused and changes nothing.
constexpr UINT64 align = 256;
UINT64 total = 0, region = 0;
check(add_scratch_region(total, 1, align, region) && region == 0 && total == 256, "size 1: one aligned region");
check(add_scratch_region(total, 0, align, region) && region == 256 && total == 512, "size 0 counts as 1");
check(add_scratch_region(total, 300, align, region) && region == 512 && total == 1024, "size 300: 512 bytes");
UINT64 before = total, region_before = region;
check(!add_scratch_region(total, UINT64_MAX - 100, align, region) && total == before && region == region_before,
"a size within 255 of 2^64 is refused (its alignment would wrap to 0)");
check(!add_scratch_region(total, UINT64_MAX - 1023, align, region) && total == before,
"a size whose aligned region would push the total past 2^64 is refused");
UINT64 big = UINT64_MAX - 511, big_region = 0;
check(add_scratch_region(big, 256, align, big_region) && big == UINT64_MAX - 255 && big_region == UINT64_MAX - 511,
"a region that ends exactly within range fits");
check(!add_scratch_region(big, 512, align, big_region) && big == UINT64_MAX - 255,
"the next one does not fit, and the total stays");
std::printf("%d failure(s)\n", failures);
return failures ? 1 : 0;
}