mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 14:28:40 +00:00
264 lines
14 KiB
C++
264 lines
14 KiB
C++
// Portable CPU-only unit tests for runtime/src/title_texture_refresh.h (pure C++20 stdlib, no GPU, no game files).
|
|
// Covers merge_title_writes (empty, degenerate, sorted, unsorted, adjacent, nested), title_write_overlaps (O(1)
|
|
// envelope / single-range fast path and multi-range binary search), title_texture_snapshot, and poll_title_textures
|
|
// (resume cursor, mapped/stale skip, budget exhaustion, and explicit UINT64_MAX wrap).
|
|
#include "../src/title_texture_refresh.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <limits>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
std::fprintf(stderr, "line %d: CHECK failed: %s\n", __LINE__, #cond); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
namespace {
|
|
|
|
struct FakeTexture {
|
|
std::uint64_t id = 0;
|
|
bool mapped = false;
|
|
bool stale = false;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
using namespace leonida;
|
|
|
|
// 1. merge_title_writes: empty, single valid/degenerate, sorted, unsorted, adjacent, nested.
|
|
{
|
|
TitleWriteRanges empty;
|
|
merge_title_writes(empty);
|
|
CHECK(empty.empty());
|
|
|
|
TitleWriteRanges single_bad{{0x2000, 0x2000}};
|
|
merge_title_writes(single_bad);
|
|
CHECK(single_bad.empty());
|
|
|
|
TitleWriteRanges single_inv{{0x3000, 0x1000}};
|
|
merge_title_writes(single_inv);
|
|
CHECK(single_inv.empty());
|
|
|
|
TitleWriteRanges single_ok{{0x1000, 0x2000}};
|
|
merge_title_writes(single_ok);
|
|
CHECK(single_ok.size() == 1 && single_ok[0] == std::make_pair(std::uintptr_t{0x1000}, std::uintptr_t{0x2000}));
|
|
|
|
// Already-sorted input with adjacent & nested ranges.
|
|
TitleWriteRanges sorted{{0x1000, 0x2000}, {0x2000, 0x3000}, {0x2500, 0x2800}, {0x5000, 0x5000}, {0x6000, 0x7000}};
|
|
merge_title_writes(sorted);
|
|
CHECK(sorted.size() == 2);
|
|
CHECK(sorted[0] == std::make_pair(std::uintptr_t{0x1000}, std::uintptr_t{0x3000}));
|
|
CHECK(sorted[1] == std::make_pair(std::uintptr_t{0x6000}, std::uintptr_t{0x7000}));
|
|
|
|
// Unsorted overlapping input.
|
|
TitleWriteRanges unsorted{{0x8000, 0x9000}, {0x1000, 0x4000}, {0x2000, 0x3000}, {0x3500, 0x5000}, {0x9000, 0x8000}};
|
|
merge_title_writes(unsorted);
|
|
CHECK(unsorted.size() == 2);
|
|
CHECK(unsorted[0] == std::make_pair(std::uintptr_t{0x1000}, std::uintptr_t{0x5000}));
|
|
CHECK(unsorted[1] == std::make_pair(std::uintptr_t{0x8000}, std::uintptr_t{0x9000}));
|
|
|
|
// Repeated full-space map changes ({0, UINTPTR_MAX}) plus sub-ranges collapse to a single {0, UINTPTR_MAX}.
|
|
TitleWriteRanges full_space{{0x2000, 0x4000}, {0, UINTPTR_MAX}, {0x8000, 0x9000}, {0, UINTPTR_MAX}};
|
|
merge_title_writes(full_space);
|
|
CHECK(full_space.size() == 1);
|
|
CHECK(full_space[0] == std::make_pair(std::uintptr_t{0}, UINTPTR_MAX));
|
|
}
|
|
|
|
// 2. title_write_overlaps: empty, degenerate query, single-range fast path, envelope reject, multi-range boundaries.
|
|
{
|
|
const TitleWriteRanges empty;
|
|
CHECK(!title_write_overlaps(empty, 0x1000, 0x2000));
|
|
|
|
const TitleWriteRanges one{{0x4000, 0x6000}};
|
|
CHECK(!title_write_overlaps(one, 0x4000, 0x4000)); // empty query
|
|
CHECK(!title_write_overlaps(one, 0x5000, 0x4000)); // inverted query
|
|
CHECK(!title_write_overlaps(one, 0x1000, 0x4000)); // half-open touching left
|
|
CHECK(!title_write_overlaps(one, 0x6000, 0x7000)); // half-open touching right
|
|
CHECK(title_write_overlaps(one, 0x3fff, 0x4001)); // overlaps left edge
|
|
CHECK(title_write_overlaps(one, 0x5fff, 0x6001)); // overlaps right edge
|
|
CHECK(title_write_overlaps(one, 0x4500, 0x5500)); // inside
|
|
CHECK(title_write_overlaps(one, 0x1000, 0x9000)); // encloses
|
|
|
|
const TitleWriteRanges multi{{0x1000, 0x2000}, {0x4000, 0x5000}, {0x8000, 0x9000}};
|
|
CHECK(!title_write_overlaps(multi, 0x0, 0x1000));
|
|
CHECK(!title_write_overlaps(multi, 0x9000, 0x10000));
|
|
CHECK(!title_write_overlaps(multi, 0x2000, 0x4000));
|
|
CHECK(!title_write_overlaps(multi, 0x5000, 0x8000));
|
|
CHECK(title_write_overlaps(multi, 0x2000, 0x4001));
|
|
CHECK(title_write_overlaps(multi, 0x4fff, 0x8000));
|
|
CHECK(title_write_overlaps(multi, 0x2500, 0x8500));
|
|
}
|
|
|
|
// 3. title_texture_snapshot & poll_title_textures: cursor wrap, mapped/stale skips, budget exhaustion, UINT64_MAX.
|
|
{
|
|
std::map<std::uint64_t, std::shared_ptr<FakeTexture>> reg;
|
|
reg[0x1000] = std::make_shared<FakeTexture>(FakeTexture{1, false, false});
|
|
reg[0x2000] = std::make_shared<FakeTexture>(FakeTexture{2, true, false}); // mapped -> skipped
|
|
reg[0x3000] = std::make_shared<FakeTexture>(FakeTexture{3, false, true}); // stale -> skipped
|
|
reg[0x4000] = std::make_shared<FakeTexture>(FakeTexture{4, false, false});
|
|
reg[UINT64_MAX] = std::make_shared<FakeTexture>(FakeTexture{5, false, false});
|
|
|
|
const auto snap = title_texture_snapshot(reg);
|
|
CHECK(snap.size() == 5);
|
|
|
|
std::uint64_t cursor = 0;
|
|
std::vector<std::uint64_t> visited;
|
|
// Budget stops after visiting 1st entry (advances at least one entry even when exhausted immediately).
|
|
poll_title_textures(snap, cursor, [&](FakeTexture& t) { visited.push_back(t.id); }, [] { return true; });
|
|
CHECK(visited.size() == 1 && visited[0] == 1 && cursor == 0x1001);
|
|
|
|
// Next pass starts at 0x2000 (skips mapped, skips stale, visits 4 and 5, wraps cursor at UINT64_MAX -> 0).
|
|
visited.clear();
|
|
poll_title_textures(snap, cursor, [&](FakeTexture& t) { visited.push_back(t.id); }, [] { return false; });
|
|
// Visits 0x4000, UINT64_MAX, then wraps inside the loop to 0x1000 (leaving cursor at 0x1001).
|
|
CHECK(visited.size() == 3 && visited[0] == 4 && visited[1] == 5 && visited[2] == 1 && cursor == 0x1001);
|
|
|
|
// Stopping right on UINT64_MAX sets cursor explicitly to 0 so the next pass starts at 0x1000.
|
|
cursor = UINT64_MAX;
|
|
visited.clear();
|
|
poll_title_textures(snap, cursor, [&](FakeTexture& t) { visited.push_back(t.id); }, [] { return true; });
|
|
CHECK(visited.size() == 1 && visited[0] == 5 && cursor == 0u);
|
|
|
|
visited.clear();
|
|
poll_title_textures(snap, cursor, [&](FakeTexture& t) { visited.push_back(t.id); }, [] { return true; });
|
|
CHECK(visited.size() == 1 && visited[0] == 1 && cursor == 0x1001);
|
|
}
|
|
|
|
// 4. effective_min_mip: combines BASE_LEVEL (`first_mip`) and MIN_LOD (`min_lod`), using floor() and handling
|
|
// negative, NaN, +/-Inf, and huge floats without UB.
|
|
{
|
|
CHECK(effective_min_mip(0, 0.0f) == 0u);
|
|
CHECK(effective_min_mip(0, -1.5f) == 0u);
|
|
CHECK(effective_min_mip(2, 0.0f) == 2u);
|
|
CHECK(effective_min_mip(2, -4.0f) == 2u);
|
|
CHECK(effective_min_mip(0, 0.75f) == 0u); // trilinear blends mip 0 and mip 1
|
|
CHECK(effective_min_mip(0, 1.0f) == 1u);
|
|
CHECK(effective_min_mip(0, 1.75f) == 1u); // trilinear blends mip 1 and mip 2
|
|
CHECK(effective_min_mip(3, 1.75f) == 3u);
|
|
CHECK(effective_min_mip(1, 4.25f) == 4u);
|
|
const float qnan = std::numeric_limits<float>::quiet_NaN();
|
|
const float inf = std::numeric_limits<float>::infinity();
|
|
const float flt_max = (std::numeric_limits<float>::max)();
|
|
CHECK(effective_min_mip(0, qnan) == 0u);
|
|
CHECK(effective_min_mip(2, qnan) == 2u);
|
|
CHECK(effective_min_mip(0, -inf) == 0u);
|
|
CHECK(effective_min_mip(2, -inf) == 2u);
|
|
CHECK(effective_min_mip(0, inf) == 64u);
|
|
CHECK(effective_min_mip(0, flt_max) == 64u);
|
|
CHECK(effective_min_mip(100, inf) == 100u);
|
|
}
|
|
|
|
// 5. TitleStreamBudget: bounds unmapped first-time streamed bursts and partial-mip upgrades without ever deferring
|
|
// mapped textures (video planes), already-filled dynamic updates, or the first streamed texture of a pass.
|
|
{
|
|
constexpr std::uint64_t kMiB = 1ull << 20;
|
|
TitleStreamBudget budget(16 * kMiB);
|
|
|
|
// First streamed texture always allowed even if larger than the whole budget (forward progress).
|
|
CHECK(budget.allow(/*mapped=*/false, /*already_filled=*/false, /*partial_upgrade=*/false, 20 * kMiB));
|
|
CHECK(budget.scheduled_bytes() == 20 * kMiB);
|
|
// Second unmapped first-time texture is deferred once budget is exceeded.
|
|
CHECK(!budget.allow(false, false, false, 1 * kMiB));
|
|
// Mapped textures and already-filled dynamic updates are NEVER deferred.
|
|
CHECK(budget.allow(/*mapped=*/true, false, false, 64 * kMiB));
|
|
CHECK(budget.allow(/*mapped=*/false, /*already_filled=*/true, /*partial_upgrade=*/false, 64 * kMiB));
|
|
// Overflow-safe check with UINT64_MAX span.
|
|
CHECK(!budget.allow(false, false, false, UINT64_MAX));
|
|
|
|
// Partial-mip upgrades use half the budget (8 MiB) so mip-by-mip arrivals coalesce across frames.
|
|
TitleStreamBudget partial(16 * kMiB);
|
|
CHECK(partial.allow(false, true, true, 5 * kMiB));
|
|
CHECK(partial.allow(false, true, true, 3 * kMiB)); // total 8 MiB == limit
|
|
CHECK(!partial.allow(false, true, true, 1)); // 8 MiB + 1 > 8 MiB -> deferred
|
|
// First-time streamed textures still get the remaining 8 MiB of the 16 MiB budget.
|
|
CHECK(partial.allow(false, false, false, 8 * kMiB));
|
|
CHECK(!partial.allow(false, false, false, 1));
|
|
|
|
// Budget == 1 byte with partial_upgrade == true still clamps limit to 1 (never 0).
|
|
TitleStreamBudget tiny(1);
|
|
CHECK(tiny.allow(false, true, true, 1));
|
|
CHECK(!tiny.allow(false, true, true, 1));
|
|
|
|
// Budget == 0 disables budgeting completely.
|
|
TitleStreamBudget unlimited(0);
|
|
CHECK(unlimited.allow(false, false, false, 128 * kMiB));
|
|
CHECK(unlimited.allow(false, false, false, 128 * kMiB));
|
|
CHECK(unlimited.allow(false, true, true, 128 * kMiB));
|
|
}
|
|
|
|
// 6. coalesce_page_runs: deduplicates, sorts, and merges contiguous 64 KiB page runs without overflowing UINTPTR_MAX.
|
|
{
|
|
constexpr std::uintptr_t kPage = 0x10000;
|
|
std::vector<std::uintptr_t> empty;
|
|
std::vector<std::pair<std::uintptr_t, std::uintptr_t>> runs;
|
|
coalesce_page_runs(empty, kPage, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.empty());
|
|
|
|
// page_size == 0 emits nothing.
|
|
std::vector<std::uintptr_t> zero_sz = {0x10000};
|
|
coalesce_page_runs(zero_sz, 0, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.empty());
|
|
|
|
// Single page emits one [lo, lo + page_size) run.
|
|
std::vector<std::uintptr_t> single = {0x30000};
|
|
coalesce_page_runs(single, kPage, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.size() == 1 && runs[0] == std::make_pair(std::uintptr_t{0x30000}, std::uintptr_t{0x40000}));
|
|
|
|
runs.clear();
|
|
std::vector<std::uintptr_t> pages = {0x40000, 0x10000, 0x20000, 0x20000, 0x80000, 0x30000};
|
|
coalesce_page_runs(pages, kPage, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.size() == 2);
|
|
CHECK(runs[0] == std::make_pair(std::uintptr_t{0x10000}, std::uintptr_t{0x50000}));
|
|
CHECK(runs[1] == std::make_pair(std::uintptr_t{0x80000}, std::uintptr_t{0x90000}));
|
|
|
|
// Near-UINTPTR_MAX clamping without wraparound; UINTPTR_MAX itself never emits an empty [kMax, kMax) run.
|
|
runs.clear();
|
|
const std::uintptr_t top = UINTPTR_MAX - 0x4000;
|
|
std::vector<std::uintptr_t> hi_pages = {top};
|
|
coalesce_page_runs(hi_pages, kPage, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.size() == 1 && runs[0] == std::make_pair(top, UINTPTR_MAX));
|
|
|
|
runs.clear();
|
|
std::vector<std::uintptr_t> max_page = {UINTPTR_MAX};
|
|
coalesce_page_runs(max_page, kPage, [&](std::uintptr_t lo, std::uintptr_t hi) { runs.emplace_back(lo, hi); });
|
|
CHECK(runs.empty());
|
|
}
|
|
|
|
// 7. PageCommitBits: lock-free 2-bit-per-page commit/notify tracker across 64-bit word boundaries.
|
|
{
|
|
PageCommitBits<> bits;
|
|
CHECK(bits.size() == 0 && !bits.is_committed(0) && !bits.mark_committed(0));
|
|
|
|
bits.init(105, false);
|
|
CHECK(bits.size() == 105);
|
|
for (const std::size_t idx : {std::size_t{0}, std::size_t{31}, std::size_t{32}, std::size_t{63},
|
|
std::size_t{64}, std::size_t{104}}) {
|
|
CHECK(!bits.is_committed(idx) && !bits.is_notified(idx));
|
|
CHECK(bits.mark_committed(idx)); // 0 -> 1
|
|
CHECK(!bits.mark_committed(idx)); // already committed
|
|
CHECK(bits.is_committed(idx) && !bits.is_notified(idx));
|
|
CHECK(bits.mark_committed_and_notified(idx)); // notified 0 -> 1 once
|
|
CHECK(!bits.mark_committed_and_notified(idx)); // repeat does not re-notify
|
|
CHECK(bits.is_committed(idx) && bits.is_notified(idx));
|
|
}
|
|
// Adjacent untouched indices remain clear; out-of-bounds stays safe.
|
|
CHECK(!bits.is_committed(1) && !bits.is_notified(1));
|
|
CHECK(!bits.is_committed(105) && !bits.mark_committed_and_notified(105));
|
|
|
|
// init(..., true) marks all pages committed & notified up front (file-backed / Wine sections).
|
|
bits.init(70, true);
|
|
CHECK(bits.is_committed(0) && bits.is_notified(0));
|
|
CHECK(bits.is_committed(69) && bits.is_notified(69));
|
|
CHECK(!bits.mark_committed(69) && !bits.mark_committed_and_notified(69));
|
|
}
|
|
|
|
std::printf("title_texture_refresh_test: all checks passed\n");
|
|
return 0;
|
|
}
|