480 lines
27 KiB
C++
480 lines
27 KiB
C++
#include "settlement/core/voxel_region.hpp"
|
|
#include "settlement/core/worker_pool.hpp"
|
|
|
|
#include <Void/Core/Hash.hpp>
|
|
#include <Void/Core/Scope.hpp>
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <future>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <mutex>
|
|
#include <span>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace settlement;
|
|
using Void::Testing::Test;
|
|
constexpr auto smallest = std::numeric_limits<std::int32_t>::min();
|
|
constexpr auto largest = std::numeric_limits<std::int32_t>::max();
|
|
constexpr auto exhausted = std::numeric_limits<std::uint64_t>::max();
|
|
|
|
void require(bool condition, const char* message = "voxel region invariant failed") {
|
|
if (!condition) throw std::runtime_error(message);
|
|
}
|
|
VoxelRegionDescriptor descriptor(VoxelBounds bounds = {{-64, -64, -32}, {64, 64, 32}}) {
|
|
return {7, 42, voxel_generator_version, bounds};
|
|
}
|
|
PreparedVoxelChunk generated(const VoxelRegion& region, ChunkCoordinate coordinate) {
|
|
auto candidate = region.prepare_generated(coordinate);
|
|
require(candidate.has_value(), "valid chunk generation refused");
|
|
return std::move(*candidate);
|
|
}
|
|
void activate(VoxelRegion& region, std::span<const ChunkCoordinate> coordinates) {
|
|
std::vector<PreparedVoxelChunk> candidates;
|
|
for (auto coordinate : coordinates) candidates.push_back(generated(region, coordinate));
|
|
require(region.activate(candidates).has_value(), "valid chunk activation refused");
|
|
}
|
|
VoxelEdit change(const VoxelRegion& region, VoxelPosition position) {
|
|
const auto sample = region.sample(position);
|
|
require(sample.availability == VoxelAvailability::Loaded);
|
|
return {position, sample.material == MaterialId::Air ? MaterialId::Stone : MaterialId::Air, sample.stamp.revision};
|
|
}
|
|
void equivalent(const VoxelRegion& left, const VoxelRegion& right) {
|
|
require(left.descriptor() == right.descriptor());
|
|
const auto a = left.directory(), b = right.directory();
|
|
require(a->size() == b->size());
|
|
for (std::size_t i = 0; i < a->size(); ++i) {
|
|
require((*a)[i].coordinate == (*b)[i].coordinate &&
|
|
(*a)[i].chunk->revision() == (*b)[i].chunk->revision());
|
|
require(std::equal((*a)[i].chunk->cells().begin(), (*a)[i].chunk->cells().end(), (*b)[i].chunk->cells().begin()),
|
|
"complete material pages differ");
|
|
}
|
|
}
|
|
template<class T> void error(const std::expected<T, VoxelError>& result, VoxelError expected) {
|
|
require(!result && result.error() == expected, "unexpected operation result");
|
|
}
|
|
bool contains(const std::vector<ChunkCoordinate>& coordinates, ChunkCoordinate coordinate) {
|
|
return std::binary_search(coordinates.begin(), coordinates.end(), coordinate);
|
|
}
|
|
|
|
struct [[= Void::Testing::name("VoxelRegionTests")]] VoxelRegionTests : Void::Testing::Suite<VoxelRegionTests> {
|
|
[[= Void::Testing::tag("voxel-region")]] Test signed_coordinates_and_x_fast_layout();
|
|
[[= Void::Testing::tag("voxel-region")]] Test descriptor_validation_and_sparse_extreme_bounds();
|
|
[[= Void::Testing::tag("voxel-region")]] Test generated_pages_have_fixed_materials_and_continuous_surface();
|
|
[[= Void::Testing::tag("voxel-region")]] Test preparation_order_and_worker_order_preserve_complete_pages();
|
|
[[= Void::Testing::tag("voxel-region")]] Test partial_pages_mask_outside_and_validate_import();
|
|
[[= Void::Testing::tag("voxel-region")]] Test availability_and_snapshot_lifetimes();
|
|
[[= Void::Testing::tag("voxel-region")]] Test atomic_multichunk_edit_reuses_unchanged_pages();
|
|
[[= Void::Testing::tag("voxel-region")]] Test refused_batches_and_identical_writes_preserve_publication();
|
|
[[= Void::Testing::tag("voxel-region")]] Test face_neighbors_and_unavailable_boundaries();
|
|
[[= Void::Testing::tag("voxel-region")]] Test activation_checks_source_duplicates_and_prior_publication();
|
|
[[= Void::Testing::tag("voxel-region")]] Test imported_revision_exhaustion_preserves_all_pages();
|
|
[[= Void::Testing::tag("voxel-region")]] Test page_edit_and_staging_budgets_are_atomic();
|
|
[[= Void::Testing::tag("voxel-region")]] Test doorway_crossing_two_chunks_opens_atomically();
|
|
[[= Void::Testing::tag("voxel-region")]] Test full_resident_directory_copies_one_changed_page();
|
|
};
|
|
|
|
Test VoxelRegionTests::signed_coordinates_and_x_fast_layout() {
|
|
constexpr std::array values{smallest, -33, -32, -1, 0, 31, 32, largest};
|
|
constexpr std::array chunks{-67108864, -2, -1, -1, 0, 0, 1, 67108863};
|
|
constexpr std::array locals{0, 31, 0, 31, 0, 31, 0, 31};
|
|
for (std::size_t i = 0; i < values.size(); ++i) {
|
|
for (auto y : values) for (auto z : values) {
|
|
const VoxelPosition position{values[i], y, z};
|
|
const auto address = split_voxel(position);
|
|
require(address.chunk.x == chunks[i] && address.local.x == locals[i]);
|
|
require(join_voxel(address.chunk, address.local) == position);
|
|
}
|
|
}
|
|
require(voxel_index({1, 0, 0}) == 1 && voxel_index({0, 1, 0}) == 32 &&
|
|
voxel_index({0, 0, 1}) == 1024 && voxel_index({31, 31, 31}) == voxel_chunk_cells - 1);
|
|
require(!voxel_index({32, 0, 0}) && !voxel_index({0, 255, 0}));
|
|
require(!join_voxel({67108864, 0, 0}, {}) && !join_voxel({-67108865, 0, 0}, {31, 0, 0}));
|
|
require(!join_voxel({}, {0, 0, 32}));
|
|
}
|
|
|
|
Test VoxelRegionTests::descriptor_validation_and_sparse_extreme_bounds() {
|
|
const auto rejected = [](VoxelRegionDescriptor definition) {
|
|
try { const VoxelRegion region(definition); }
|
|
catch (const std::invalid_argument&) { return true; }
|
|
return false;
|
|
};
|
|
auto definition = descriptor(); definition.id = 0; require(rejected(definition));
|
|
definition = descriptor(); definition.generator_version = 0; require(rejected(definition));
|
|
definition.generator_version = 2; require(rejected(definition));
|
|
definition = descriptor(); definition.bounds.upper.x = definition.bounds.lower.x; require(rejected(definition));
|
|
definition = descriptor(); definition.bounds.lower.z = std::int64_t{smallest} - 1; require(rejected(definition));
|
|
definition = descriptor(); definition.bounds.upper.y = std::int64_t{largest} + 2; require(rejected(definition));
|
|
VoxelRegion sparse(descriptor({{smallest, smallest, smallest},
|
|
{std::int64_t{largest} + 1, std::int64_t{largest} + 1, std::int64_t{largest} + 1}}));
|
|
require(sparse.directory()->empty() && sparse.resident_bytes() == sizeof(VoxelDirectory));
|
|
const std::array coordinates{ChunkCoordinate{-67108864, -67108864, -67108864},
|
|
ChunkCoordinate{67108863, 67108863, 67108863}};
|
|
activate(sparse, coordinates);
|
|
require(sparse.sample({smallest, smallest, smallest}).material == MaterialId::Stone);
|
|
require(sparse.sample({largest, largest, largest}).availability == VoxelAvailability::Loaded &&
|
|
sparse.sample({largest, largest, largest}).material == MaterialId::Air);
|
|
error(sparse.prepare_generated({largest, 0, 0}), VoxelError::InvalidCoordinate);
|
|
}
|
|
|
|
Test VoxelRegionTests::generated_pages_have_fixed_materials_and_continuous_surface() {
|
|
VoxelRegion region(descriptor());
|
|
std::vector<ChunkCoordinate> coordinates;
|
|
for (int x = -1; x <= 0; ++x) for (int y = -1; y <= 0; ++y) for (int z = -1; z <= 0; ++z) {
|
|
coordinates.push_back({x, y, z});
|
|
}
|
|
activate(region, coordinates);
|
|
const auto height = [&](int x, int y) {
|
|
int top = -20;
|
|
for (int z = -16; z < 16; ++z) if (region.sample({x, y, z}).material != MaterialId::Air) top = z;
|
|
require(top >= -8 && top <= 8);
|
|
require(region.sample({x, y, top}).material == MaterialId::Soil &&
|
|
region.sample({x, y, top - 2}).material == MaterialId::Soil &&
|
|
region.sample({x, y, top - 3}).material == MaterialId::Stone &&
|
|
region.sample({x, y, top + 1}).material == MaterialId::Air);
|
|
return top;
|
|
};
|
|
for (int x = -31; x < 31; ++x) for (int y = -31; y < 31; ++y) {
|
|
const auto top = height(x, y);
|
|
require(std::abs(top - height(x + 1, y)) <= 1 && std::abs(top - height(x, y + 1)) <= 1,
|
|
"generator has a discontinuous chunk/lattice surface");
|
|
}
|
|
const auto page = region.directory()->front().chunk;
|
|
const auto hash = Void::Hash::fnv1a64(std::as_bytes(page->cells()));
|
|
std::cout << "generator1 seed42 chunk(-1,-1,-1) fnv1a64=" << hash << '\n';
|
|
// Recorded against the canonical little-endian generator, independently
|
|
// reproduced by the fixture calculation before accepting this revision.
|
|
require(hash == 13302332479434111011ULL, "canonical generator fixture changed");
|
|
}
|
|
|
|
Test VoxelRegionTests::preparation_order_and_worker_order_preserve_complete_pages() {
|
|
const std::array coordinates{ChunkCoordinate{0, 0, 0}, ChunkCoordinate{-1, 0, 0},
|
|
ChunkCoordinate{0, -1, -1}, ChunkCoordinate{1, 1, 0}};
|
|
VoxelRegion forward(descriptor());
|
|
activate(forward, coordinates);
|
|
VoxelRegion reverse(descriptor());
|
|
auto reversed = coordinates; std::reverse(reversed.begin(), reversed.end());
|
|
activate(reverse, reversed);
|
|
equivalent(forward, reverse);
|
|
for (std::size_t workers : {2, 6, 12}) for (bool reverse_completion : {false, true}) {
|
|
VoxelRegion parallel(descriptor());
|
|
WorkerPool pool(workers);
|
|
std::vector<PreparedVoxelChunk> candidates;
|
|
for (std::size_t pair = 0; pair < coordinates.size(); pair += 2) {
|
|
std::mutex mutex;
|
|
std::condition_variable changed;
|
|
int ready = 0;
|
|
std::array<bool, 2> permitted{};
|
|
bool released = false;
|
|
std::array<std::future<std::expected<PreparedVoxelChunk, VoxelError>>, 2> futures;
|
|
// Release and drain every accepted task before its captured gate
|
|
// dies, including a failed submission or assertion.
|
|
auto release = Void::Scope::on_exit([&] {
|
|
{
|
|
std::lock_guard lock(mutex);
|
|
released = true;
|
|
changed.notify_all();
|
|
}
|
|
for (auto& future : futures) if (future.valid()) future.wait();
|
|
});
|
|
for (std::size_t index = 0; index < 2; ++index) {
|
|
futures[index] = pool.submit([&, parallel, coordinate = coordinates[pair + index], index] {
|
|
auto result = parallel.prepare_generated(coordinate);
|
|
std::unique_lock lock(mutex);
|
|
++ready;
|
|
changed.notify_all();
|
|
changed.wait(lock, [&] { return released || permitted[index]; });
|
|
return result;
|
|
});
|
|
}
|
|
{
|
|
std::unique_lock lock(mutex);
|
|
require(changed.wait_for(lock, std::chrono::seconds(5), [&] { return ready == 2; }),
|
|
"two real generator tasks did not reach completion gates");
|
|
}
|
|
const std::array<std::size_t, 2> order = reverse_completion ?
|
|
std::array<std::size_t, 2>{1, 0} : std::array<std::size_t, 2>{0, 1};
|
|
for (auto index : order) {
|
|
{
|
|
std::lock_guard lock(mutex);
|
|
permitted[index] = true;
|
|
changed.notify_all();
|
|
}
|
|
require(futures[index].wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
auto result = futures[index].get();
|
|
require(result.has_value());
|
|
candidates.push_back(std::move(*result));
|
|
}
|
|
}
|
|
require(parallel.activate(candidates).has_value());
|
|
equivalent(forward, parallel);
|
|
}
|
|
auto other_definition = descriptor(); ++other_definition.seed;
|
|
VoxelRegion other(other_definition); activate(other, coordinates);
|
|
require(!std::equal(forward.directory()->front().chunk->cells().begin(),
|
|
forward.directory()->front().chunk->cells().end(), other.directory()->front().chunk->cells().begin()));
|
|
}
|
|
|
|
Test VoxelRegionTests::partial_pages_mask_outside_and_validate_import() {
|
|
const std::array bounds{VoxelBounds{{-1, -1, -1}, {1, 1, 1}},
|
|
VoxelBounds{{largest, largest, largest}, {std::int64_t{largest} + 1,
|
|
std::int64_t{largest} + 1, std::int64_t{largest} + 1}},
|
|
VoxelBounds{{smallest, smallest, smallest}, {std::int64_t{smallest} + 1,
|
|
std::int64_t{smallest} + 1, std::int64_t{smallest} + 1}}};
|
|
for (const auto& bound : bounds) {
|
|
VoxelRegion region(descriptor(bound));
|
|
const VoxelPosition position{static_cast<std::int32_t>(bound.lower.x),
|
|
static_cast<std::int32_t>(bound.lower.y), static_cast<std::int32_t>(bound.lower.z)};
|
|
const auto coordinate = split_voxel(position).chunk;
|
|
auto candidate = generated(region, coordinate);
|
|
const auto cells = candidate.chunk()->cells();
|
|
for (std::size_t i = 0; i < cells.size(); ++i) {
|
|
const auto p = join_voxel(coordinate, {static_cast<std::uint8_t>(i % 32),
|
|
static_cast<std::uint8_t>((i / 32) % 32), static_cast<std::uint8_t>(i / 1024)});
|
|
require(p.has_value());
|
|
if (!bound.contains(*p)) require(cells[i] == MaterialId::Air);
|
|
}
|
|
auto restored = region.prepare_restored(coordinate, 72, cells);
|
|
require(restored.has_value() && restored->chunk()->revision() == 72);
|
|
std::array<MaterialId, voxel_chunk_cells> owned;
|
|
std::copy(cells.begin(), cells.end(), owned.begin());
|
|
auto independent = region.prepare_restored(coordinate, 1, owned);
|
|
require(independent.has_value());
|
|
const auto index = *voxel_index(split_voxel(position).local);
|
|
owned[index] = static_cast<MaterialId>(200);
|
|
error(region.prepare_restored(coordinate, 1, owned), VoxelError::InvalidMaterial);
|
|
require(independent->chunk()->cells()[index] == cells[index], "import retained caller's mutable cells");
|
|
owned[index] = cells[index];
|
|
const auto outside_index = (index + 1234) % voxel_chunk_cells;
|
|
owned[outside_index] = MaterialId::Stone;
|
|
error(region.prepare_restored(coordinate, 1, owned), VoxelError::InvalidCoordinate);
|
|
error(region.prepare_restored(coordinate, 0, cells), VoxelError::InvalidRevision);
|
|
error(region.prepare_restored(coordinate, 1, cells.first(100)), VoxelError::InvalidSource);
|
|
}
|
|
}
|
|
|
|
Test VoxelRegionTests::availability_and_snapshot_lifetimes() {
|
|
VoxelRegion region(descriptor());
|
|
const auto absent = region.sample({0, 0, 20});
|
|
require(absent.availability == VoxelAvailability::Unavailable);
|
|
const auto outside = region.sample({64, 0, 20});
|
|
require(outside.availability == VoxelAvailability::Outside);
|
|
auto held = region;
|
|
const std::array coordinates{ChunkCoordinate{0, 0, 0}};
|
|
activate(region, coordinates);
|
|
require(!region.matches(absent.stamp) && held.matches(absent.stamp));
|
|
const auto loaded = region.sample({0, 0, 20});
|
|
require(loaded.availability == VoxelAvailability::Loaded && loaded.material == MaterialId::Air);
|
|
require(held.sample({0, 0, 20}).availability == VoxelAvailability::Unavailable);
|
|
auto owned_directory = region.directory();
|
|
auto retained = region;
|
|
const std::array changes{change(region, {0, 0, 20})};
|
|
require(region.edit(changes).has_value());
|
|
require(retained.matches(loaded.stamp) && !region.matches(loaded.stamp));
|
|
require(owned_directory->front().chunk->cells()[20 * 1024] == MaterialId::Air);
|
|
auto other_stamp = loaded.stamp; ++other_stamp.region; require(!retained.matches(other_stamp));
|
|
}
|
|
|
|
Test VoxelRegionTests::atomic_multichunk_edit_reuses_unchanged_pages() {
|
|
VoxelRegion region(descriptor());
|
|
const std::array coordinates{ChunkCoordinate{-1, 0, 0}, ChunkCoordinate{0, 0, 0}, ChunkCoordinate{1, 0, 0}};
|
|
activate(region, coordinates);
|
|
const auto held = region;
|
|
const std::array changes{change(region, {0, 2, 20}), change(region, {-1, 2, 20}), change(region, {4, 4, 20})};
|
|
const auto result = region.edit(changes);
|
|
require(result.has_value() && result->changed_chunks.size() == 2 && result->changed_cells.size() == 3);
|
|
require(result->changed_cells[0].position == VoxelPosition{-1, 2, 20} &&
|
|
result->changed_cells[1].position == VoxelPosition{0, 2, 20});
|
|
require(result->changed_chunks[0].coordinate == ChunkCoordinate{-1, 0, 0} &&
|
|
result->changed_chunks[1].coordinate == ChunkCoordinate{0, 0, 0});
|
|
for (const auto& change : changes) {
|
|
require(region.sample(change.position).material == change.material);
|
|
require(held.sample(change.position).material != change.material);
|
|
require(region.sample(change.position).stamp.revision == 2);
|
|
}
|
|
require(region.directory()->back().chunk == held.directory()->back().chunk,
|
|
"untouched geometry was copied");
|
|
VoxelRegion other = held;
|
|
auto reversed = changes; std::reverse(reversed.begin(), reversed.end());
|
|
require(other.edit(reversed).has_value()); equivalent(region, other);
|
|
}
|
|
|
|
Test VoxelRegionTests::refused_batches_and_identical_writes_preserve_publication() {
|
|
VoxelRegion region(descriptor());
|
|
const std::array coordinates{ChunkCoordinate{-1, 0, 0}, ChunkCoordinate{0, 0, 0}};
|
|
activate(region, coordinates);
|
|
const auto held = region;
|
|
auto changes = std::array{change(region, {-1, 1, 20}), change(region, {0, 1, 20})};
|
|
++changes[1].expected_revision;
|
|
error(region.edit(changes), VoxelError::Stale);
|
|
--changes[1].expected_revision; changes[1].material = static_cast<MaterialId>(255);
|
|
error(region.edit(changes), VoxelError::InvalidMaterial);
|
|
changes[1] = changes[0]; error(region.edit(changes), VoxelError::Duplicate);
|
|
changes[1] = {{32, 1, 20}, MaterialId::Stone, 1}; error(region.edit(changes), VoxelError::Unavailable);
|
|
changes[1].position.x = 64; error(region.edit(changes), VoxelError::InvalidCoordinate);
|
|
require(region.directory() == held.directory()); equivalent(region, held);
|
|
require(region.edit({}).has_value() && region.directory() == held.directory());
|
|
const auto current = region.sample({0, 1, 20});
|
|
std::array identical{VoxelEdit{{0, 1, 20}, current.material, current.stamp.revision}};
|
|
const auto result = region.edit(identical);
|
|
require(result && result->changed_chunks.empty() && result->changed_cells.empty() && result->dirty_chunks.empty());
|
|
require(region.directory() == held.directory());
|
|
--identical[0].expected_revision; error(region.edit(identical), VoxelError::Stale);
|
|
require(region.directory() == held.directory());
|
|
}
|
|
|
|
Test VoxelRegionTests::face_neighbors_and_unavailable_boundaries() {
|
|
VoxelRegion region(descriptor({{-64, -64, -64}, {64, 64, 64}}));
|
|
const std::array coordinates{ChunkCoordinate{-1, -1, -1}, ChunkCoordinate{0, -1, -1}};
|
|
activate(region, coordinates);
|
|
const auto neighbor = region.chunk_stamp({0, -1, -1});
|
|
const auto absent = region.chunk_stamp({-1, 0, -1});
|
|
const std::array changes{change(region, {-1, -1, -1})};
|
|
const auto result = region.edit(changes);
|
|
require(result && result->dirty_chunks.size() == 4);
|
|
require(contains(result->dirty_chunks, {-1, -1, -1}) && contains(result->dirty_chunks, {0, -1, -1}) &&
|
|
contains(result->dirty_chunks, {-1, 0, -1}) && contains(result->dirty_chunks, {-1, -1, 0}));
|
|
require(!contains(result->dirty_chunks, {0, 0, -1}) && !contains(result->dirty_chunks, {0, 0, 0}));
|
|
require(region.matches(neighbor) && region.matches(absent));
|
|
const std::array interior{change(region, {-2, -2, -2})};
|
|
const auto result2 = region.edit(interior);
|
|
require(result2 && result2->dirty_chunks == std::vector<ChunkCoordinate>{{-1, -1, -1}});
|
|
VoxelRegion edge(descriptor({{largest, 0, 0}, {std::int64_t{largest} + 1, 1, 1}}));
|
|
const std::array last_chunk{split_voxel({largest, 0, 0}).chunk}; activate(edge, last_chunk);
|
|
const std::array last_change{change(edge, {largest, 0, 0})};
|
|
const auto last_result = edge.edit(last_change);
|
|
require(last_result && last_result->dirty_chunks == std::vector<ChunkCoordinate>{last_chunk[0]});
|
|
}
|
|
|
|
Test VoxelRegionTests::activation_checks_source_duplicates_and_prior_publication() {
|
|
VoxelRegion region(descriptor());
|
|
auto candidate = generated(region, {});
|
|
const auto empty = region.directory();
|
|
const std::array duplicate{candidate, candidate}; error(region.activate(duplicate), VoxelError::Duplicate);
|
|
require(region.directory() == empty);
|
|
for (int field = 0; field < 3; ++field) {
|
|
auto wrong = descriptor();
|
|
if (field == 0) ++wrong.id;
|
|
if (field == 1) ++wrong.seed;
|
|
if (field == 2) --wrong.bounds.lower.x;
|
|
VoxelRegion other(wrong);
|
|
const std::array mixed{candidate, generated(other, {-1, 0, 0})};
|
|
error(region.activate(mixed), VoxelError::InvalidSource);
|
|
require(region.directory() == empty);
|
|
}
|
|
const std::array first{candidate}; require(region.activate(first).has_value());
|
|
const auto held = region.directory();
|
|
const std::array late{generated(region, {-1, 0, 0}), candidate};
|
|
error(region.activate(late), VoxelError::Stale); require(region.directory() == held);
|
|
}
|
|
|
|
Test VoxelRegionTests::imported_revision_exhaustion_preserves_all_pages() {
|
|
VoxelRegion region(descriptor());
|
|
const auto source = generated(region, {});
|
|
auto maximum = region.prepare_restored({}, exhausted, source.chunk()->cells());
|
|
require(maximum.has_value());
|
|
const std::array pages{generated(region, {-1, 0, 0}), *maximum};
|
|
require(region.activate(pages).has_value());
|
|
const auto held = region;
|
|
const std::array changes{change(region, {-1, 1, 20}), change(region, {0, 1, 20})};
|
|
error(region.edit(changes), VoxelError::RevisionExhausted);
|
|
require(region.directory() == held.directory()); equivalent(region, held);
|
|
const auto current = region.sample({0, 1, 20});
|
|
const std::array identical{VoxelEdit{{0, 1, 20}, current.material, exhausted}};
|
|
require(region.edit(identical).has_value() && region.directory() == held.directory());
|
|
}
|
|
|
|
Test VoxelRegionTests::page_edit_and_staging_budgets_are_atomic() {
|
|
VoxelRegion source(descriptor());
|
|
const std::array pages{generated(source, {}), generated(source, {-1, 0, 0})};
|
|
VoxelLimits one; one.max_resident_chunks = 1;
|
|
VoxelRegion bounded(descriptor(), one);
|
|
const auto empty = bounded.directory();
|
|
error(bounded.activate(pages), VoxelError::LimitExceeded); require(bounded.directory() == empty);
|
|
require(bounded.activate(std::span(pages).first(1)).has_value());
|
|
error(bounded.activate(std::span(pages).last(1)), VoxelError::LimitExceeded);
|
|
VoxelLimits bytes; bytes.max_resident_bytes = sizeof(VoxelDirectory) + sizeof(VoxelChunkEntry) + sizeof(VoxelChunk) - 1;
|
|
VoxelRegion small(descriptor(), bytes);
|
|
error(small.activate(std::span(pages).first(1)), VoxelError::LimitExceeded);
|
|
VoxelLimits transactions; transactions.max_transaction_bytes = sizeof(VoxelChunk) + 1024;
|
|
VoxelRegion limited(descriptor(), transactions);
|
|
error(limited.prepare_generated({}), VoxelError::LimitExceeded);
|
|
require(limited.activate(std::span(pages).first(1)).has_value());
|
|
const auto held = limited.directory();
|
|
const std::array edits{change(limited, {1, 1, 20})};
|
|
error(limited.edit(edits), VoxelError::LimitExceeded); require(limited.directory() == held);
|
|
VoxelLimits edit_limit; edit_limit.max_edit_cells = 1;
|
|
VoxelRegion two(descriptor(), edit_limit); require(two.activate(pages).has_value());
|
|
const std::array too_many{change(two, {1, 1, 20}), change(two, {-1, 1, 20})};
|
|
error(two.edit(too_many), VoxelError::LimitExceeded);
|
|
require(two.resident_bytes() == sizeof(VoxelDirectory) + 2 * (sizeof(VoxelChunkEntry) + sizeof(VoxelChunk)));
|
|
VoxelLimits hostile; hostile.max_resident_chunks = std::numeric_limits<std::size_t>::max();
|
|
VoxelRegion sparse(descriptor(), hostile); require(sparse.directory()->empty());
|
|
std::cout << "voxel payload: page=" << sizeof(VoxelChunk) << " directory entry=" << sizeof(VoxelChunkEntry)
|
|
<< " active256=" << sizeof(VoxelDirectory) + 256 * (sizeof(VoxelChunkEntry) + sizeof(VoxelChunk)) << '\n';
|
|
}
|
|
|
|
Test VoxelRegionTests::doorway_crossing_two_chunks_opens_atomically() {
|
|
require(voxel_blocks_passage(MaterialId::DoorClosed) && !voxel_blocks_passage(MaterialId::DoorOpen));
|
|
require(!voxel_provides_support(MaterialId::DoorClosed) && !voxel_provides_support(MaterialId::DoorOpen));
|
|
require(!voxel_blocks_passage(MaterialId::Air) && !voxel_provides_support(MaterialId::Air));
|
|
for (auto material : {MaterialId::Soil, MaterialId::Stone}) {
|
|
require(voxel_blocks_passage(material) && voxel_provides_support(material));
|
|
}
|
|
VoxelRegion region(descriptor({{0, 0, 0}, {32, 32, 64}}));
|
|
const std::array coordinates{ChunkCoordinate{0, 0, 0}, ChunkCoordinate{0, 0, 1}};
|
|
activate(region, coordinates);
|
|
const std::array closed{VoxelEdit{{2, 2, 31}, MaterialId::DoorClosed, 1},
|
|
VoxelEdit{{2, 2, 32}, MaterialId::DoorClosed, 1}};
|
|
require(region.edit(closed).has_value());
|
|
const auto held = region;
|
|
std::array opened{VoxelEdit{{2, 2, 31}, MaterialId::DoorOpen, 2},
|
|
VoxelEdit{{2, 2, 32}, MaterialId::DoorOpen, 1}};
|
|
error(region.edit(opened), VoxelError::Stale);
|
|
require(region.directory() == held.directory());
|
|
opened[1].expected_revision = 2;
|
|
const auto result = region.edit(opened);
|
|
require(result && result->changed_chunks.size() == 2 && result->changed_cells.size() == 2);
|
|
for (auto edit : opened) {
|
|
require(region.sample(edit.position).material == MaterialId::DoorOpen);
|
|
require(held.sample(edit.position).material == MaterialId::DoorClosed);
|
|
}
|
|
auto imported = region.prepare_restored(coordinates[0], 3, region.directory()->front().chunk->cells());
|
|
require(imported.has_value());
|
|
}
|
|
|
|
Test VoxelRegionTests::full_resident_directory_copies_one_changed_page() {
|
|
using Clock = std::chrono::steady_clock;
|
|
VoxelRegion region(descriptor({{-256, -256, 0}, {256, 256, 32}}));
|
|
std::vector<ChunkCoordinate> coordinates;
|
|
for (int x = -8; x < 8; ++x) for (int y = -8; y < 8; ++y) coordinates.push_back({x, y, 0});
|
|
const auto preparation_started = Clock::now();
|
|
activate(region, coordinates);
|
|
const auto preparation_done = Clock::now();
|
|
const auto held = region;
|
|
require(held.directory() == region.directory() && held.directory()->size() == 256);
|
|
const std::array edits{change(region, {1, 1, 20})};
|
|
const auto edit_started = Clock::now();
|
|
const auto result = region.edit(edits);
|
|
const auto edit_done = Clock::now();
|
|
require(result && result->changed_chunks.size() == 1);
|
|
std::size_t shared = 0;
|
|
for (std::size_t i = 0; i < region.directory()->size(); ++i) {
|
|
if ((*held.directory())[i].chunk == (*region.directory())[i].chunk) ++shared;
|
|
}
|
|
require(shared == 255 && region.directory() != held.directory());
|
|
require(held.sample(edits[0].position).material != region.sample(edits[0].position).material);
|
|
std::cout << "full256 pages: prepare+activate_us="
|
|
<< std::chrono::duration_cast<std::chrono::microseconds>(preparation_done - preparation_started).count()
|
|
<< " edit_us=" << std::chrono::duration_cast<std::chrono::microseconds>(edit_done - edit_started).count()
|
|
<< " shared_pages=" << shared << " resident_payload=" << region.resident_bytes() << '\n';
|
|
}
|
|
} // namespace
|