Implement M9.4 revision-tagged microscopic environment rendering

Add SDL-free owned EnvironmentPresentation copied from M6.3 views (no
store pointers or validation tokens), a single-slot EnvironmentLayer that
rejects foreign/regressed updates and reports none/current/stale against
observed authority watermarks, and a bounded camera projection with
explicit missing, not-requested, present-zero, undefined-moisture and
stale encodings (at most 2313 clipped rectangles, reused buffer).

The engineering app renders a local in-memory fixture authority with
configurable refresh cadence and layer selection. ADR-0080 records the
decision; validation evidence and limits are in VALIDATION.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-01 17:45:53 -04:00
co-authored by Claude Opus 5.5
parent bb0412cd0f
commit feca07f791
20 changed files with 1393 additions and 62 deletions
+155 -33
View File
@@ -1,4 +1,5 @@
#include "evo/client/application.hpp"
#include "evo/render/environment.hpp"
#include <algorithm>
#include <array>
#include <charconv>
@@ -6,12 +7,117 @@
#include <cmath>
#include <cstdio>
#include <exception>
#include <memory>
#include <optional>
#include <string_view>
#include <thread>
#include <vector>
namespace {
using evo::world::Field;
constexpr evo::world::WorldIdentity fixture_identity{1, 1};
constexpr std::int64_t fixture_z = -16000;
// Water, nitrogen (never opened: missing), phosphorus (present zero), organic and
// incident energy (never opened: missing). Unrequested fields draw hollow outlines.
constexpr std::uint32_t fixture_mask = (1U << 0) | (1U << 1) | (1U << 2) | (1U << 7) | (1U << 8);
constexpr unsigned frames_per_authority_tick = 60;
// Local in-memory stand-in for authoritative region views until M9.7. It is never
// persisted or published; the client presentation only copies its read-only views.
struct FixtureAuthority {
std::unique_ptr<evo::world::FieldStore> fields;
std::unique_ptr<evo::world::ChunkStore> terrain;
std::vector<evo::world::VoxelAddress> footprint;
std::uint64_t tick{1};
};
std::optional<FixtureAuthority> make_authority() {
FixtureAuthority authority;
std::vector<evo::world::FieldSite> sites;
std::vector<std::uint64_t> water, energy;
for (std::int64_t x = -8; x < 8; ++x)
for (std::int64_t y = -4; y < 4; ++y) {
const auto ux = static_cast<std::uint64_t>(x + 8);
const auto uy = static_cast<std::uint64_t>(y + 4);
authority.footprint.push_back({x, y, fixture_z});
// Habitat boxes vary to show their boundaries inside each 1 m voxel.
const evo::world::HabitatBox box{{50000 * (ux % 4)}, {40000 * uy}, {0},
{800000 - 30000 * uy}, {500000 + 10000 * ux}, {100}};
sites.push_back({authority.footprint.back(), box, {1}});
const auto volume = box.width.value * box.height.value * box.depth.value;
water.push_back(volume >> (2 * ux / 3 + uy));
energy.push_back((ux + 1) * (uy + 1) * (volume >> 4));
}
const std::uint64_t zero = 0;
const evo::world::FieldOpening opening[]{
{Field::water, water}, {Field::phosphorus, {&zero, 1}}, {Field::organic_energy, energy}};
const evo::world::FieldDefaults defaults{{evo::accounting::Femtograms{0},
evo::accounting::Femtograms{1ULL << 50},
evo::accounting::Femtograms{0}}};
constexpr std::size_t budget = 4'000'000;
auto fields =
evo::world::FieldStore::create(fixture_identity, 1, defaults, sites, opening, budget);
auto terrain = evo::world::ChunkStore::create({}, 32, budget);
if (!fields || !terrain)
return std::nullopt;
authority.fields = std::move(*fields);
authority.terrain = std::move(*terrain);
for (const auto voxel : authority.footprint)
if (!authority.terrain->load(evo::world::split(voxel, evo::world::base_chunk_edge)->chunk))
return std::nullopt;
return authority;
}
// One conserved same-channel water move per authority tick (finite fixture stock).
bool advance(FixtureAuthority& authority) {
const auto sites = authority.fields->sites().size();
const auto from = static_cast<std::size_t>(authority.tick * 7 % sites);
const auto amount = authority.fields->read(from, Field::water);
if (!amount)
return false;
++authority.tick;
if (*amount < 8)
return true;
const auto moved = authority.fields->transfer(authority.tick, Field::water, from,
(from + 1) % sites, *amount / 8);
return moved || moved.error() == evo::world::FieldError::capacity;
}
std::optional<evo::client::EnvironmentPresentation> query(const FixtureAuthority& authority) {
const auto view = evo::world::query_environment(
*authority.fields, *authority.terrain, authority.tick, authority.footprint, fixture_mask);
if (!view)
return std::nullopt;
auto presentation = evo::client::present_environment(*view, fixture_z);
if (!presentation)
return std::nullopt;
return std::move(*presentation);
}
std::optional<evo::render::EnvironmentDisplay> parse_layer(std::string_view name) {
using Mode = evo::render::EnvironmentDisplay::Mode;
constexpr std::array<std::string_view, evo::world::field_count> names{"water",
"nitrogen",
"phosphorus",
"organic-carbon",
"organic-nitrogen",
"organic-phosphorus",
"organic-other",
"organic-energy",
"incident-energy",
"heat"};
if (name == "moisture")
return evo::render::EnvironmentDisplay{Mode::moisture};
for (std::size_t i = 0; i < names.size(); ++i)
if (name == names.at(i))
return evo::render::EnvironmentDisplay{Mode::field, static_cast<Field>(i)};
return std::nullopt;
}
bool parse_count(std::string_view value, unsigned limit, unsigned& out) {
const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), out);
return error == std::errc{} && end == value.data() + value.size() && out != 0 && out <= limit;
}
} // namespace
int main(int argc, char** argv) try {
evo::client::Config config;
unsigned frames = 0;
unsigned frames = 0, refresh_every = 1;
evo::render::EnvironmentDisplay display;
for (int i = 1; i < argc; ++i) {
const std::string_view arg{argv[i]};
if (arg == "--hidden")
@@ -19,18 +125,30 @@ int main(int argc, char** argv) try {
else if (arg == "--renderer" && i + 1 < argc)
config.renderer = argv[++i];
else if (arg == "--frames" && i + 1 < argc) {
const std::string_view value{argv[++i]};
const auto [end, error] =
std::from_chars(value.data(), value.data() + value.size(), frames);
if (error != std::errc{} || end != value.data() + value.size() || frames == 0 ||
frames > 100000)
if (!parse_count(argv[++i], 100000, frames))
return 2;
} else if (arg == "--refresh-every" && i + 1 < argc) {
if (!parse_count(argv[++i], 1000, refresh_every))
return 2;
} else if (arg == "--layer" && i + 1 < argc) {
const auto layer = parse_layer(argv[++i]);
if (!layer)
return 2;
display = *layer;
} else {
std::fputs("usage: evo-client [--renderer NAME] [--hidden] [--frames 1..100000]\n",
std::fputs("usage: evo-client [--renderer NAME] [--hidden] [--frames 1..100000]\n"
" [--layer water|nitrogen|...|heat|moisture]\n"
" [--refresh-every 1..1000]\n",
stderr);
return 2;
}
}
auto authority = make_authority();
if (!authority) {
std::fputs("client environment fixture error\n", stderr);
return 1;
}
evo::client::EnvironmentLayer environment;
auto app = evo::client::Application::open(config);
if (!app) {
std::fprintf(stderr, "client initialization error=%d\n", static_cast<int>(app.error()));
@@ -38,11 +156,13 @@ int main(int argc, char** argv) try {
}
// A minimized native window may start with no drawable area. Adopt the
// native viewport before projecting; until then use the requested size.
auto camera =
evo::client::Camera::create({config.width, config.height, config.width, config.height});
auto camera = evo::client::Camera::create(
{config.width, config.height, config.width, config.height}, {}, 0.05);
if (!camera)
return 1;
std::puts("Arrows: pan | wheel: zoom | WASD: intention indicator | Tab: UI capture (orange)");
std::puts("Environment chip: green current | amber stale | red no view");
std::vector<evo::render::Rectangle> boxes;
std::uint64_t count = 0;
auto previous = std::chrono::steady_clock::now();
while (frames == 0 || count < frames) {
@@ -62,7 +182,21 @@ int main(int argc, char** argv) try {
const auto intention = (*app)->sample_intention(count);
if (!intention)
return 1;
std::array<evo::render::Rectangle, 3> boxes{};
// The fixture authority advances at its own cadence. Presentations refresh
// every N authority ticks; in between the advanced watermark shows as stale.
if (count % frames_per_authority_tick == 0) {
if (count != 0 && !advance(*authority))
return 1;
if ((authority->tick - 1) % refresh_every == 0) {
auto next = query(*authority);
if (!next || !environment.accept(std::move(*next)))
return 1;
}
if (!environment.observe(
{fixture_identity, authority->tick, authority->fields->last_command()}))
return 1;
}
boxes.clear();
if (state.logical_width > 0 && state.logical_height > 0 && state.pixel_width > 0 &&
state.pixel_height > 0) {
if (!camera->resize({state.logical_width, state.logical_height, state.pixel_width,
@@ -78,31 +212,19 @@ int main(int argc, char** argv) try {
if (auto result = camera->zoom(std::exp2(navigation.wheel / 4), anchor);
!result && result.error() != evo::client::CameraError::out_of_range)
return 1;
// Local-mm presentation fixture only. Clip before float conversion.
const auto upper = camera->world_to_screen({-80, 48});
const auto lower = camera->world_to_screen({80, -48});
if (!upper || !lower)
// Copied presentation only; clipped to the viewport before float conversion.
if (!evo::render::build_environment(boxes, environment.current(),
environment.freshness(), *camera, display))
return 1;
const double left = std::clamp(upper->x, 0.0, static_cast<double>(state.logical_width));
const double top = std::clamp(upper->y, 0.0, static_cast<double>(state.logical_height));
const double right =
std::clamp(lower->x, 0.0, static_cast<double>(state.logical_width));
const double bottom =
std::clamp(lower->y, 0.0, static_cast<double>(state.logical_height));
boxes[0] = {static_cast<float>(left),
static_cast<float>(top),
static_cast<float>(right - left),
static_cast<float>(bottom - top),
{64, 180, 140, 255}};
}
boxes[1] = {8, 8, 64, 8,
(*app)->ui_captured() ? evo::render::Color{240, 140, 32, 255}
: evo::render::Color{64, 180, 140, 255}};
boxes[2] = {36 + static_cast<float>(intention->move_x) / 50,
48 - static_cast<float>(intention->move_y) / 50,
8,
8,
{240, 240, 240, 255}};
boxes.push_back({8, 8, 64, 8,
(*app)->ui_captured() ? evo::render::Color{240, 140, 32, 255}
: evo::render::Color{64, 180, 140, 255}});
boxes.push_back({36 + static_cast<float>(intention->move_x) / 50,
48 - static_cast<float>(intention->move_y) / 50,
8,
8,
{240, 240, 240, 255}});
const auto frame = evo::render::Frame::create(boxes, {16, 20, 28, 255});
if (!frame)
return 1;