Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m27s
Foundation / checks (network) (push) Successful in 12m46s
Foundation / checks (tsan) (push) Successful in 7m59s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 25m13s
Foundation / checks (postgres) (push) Canceled after 12m48s
ADR-0095. The engineering client times presentation with the tick clock: its own marker between ticks, other markers interpolated a few ticks behind (grey when stale). Silence beyond 3 ticks and long-gap holds are stale. A lost session leaves its last view frozen and labelled stale until the reconnect's fresh baseline; nothing is predicted or sent meanwhile. evo-motion-trace --remote measures remote-marker error between two clients through the relay, with a 1.5 s outage (motion-trace-remote-smoke CTest). Prediction errors are recorded only for ticks inputs are scheduled for. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
5.9 KiB
C++
157 lines
5.9 KiB
C++
#include "evo/client/interpolation.hpp"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace evo::client {
|
|
namespace {
|
|
double ms(TickClock::Clock::duration d) {
|
|
return std::chrono::duration<double, std::milli>(d).count();
|
|
}
|
|
// Held-vector motion in micrometres for a fractional number of ticks (presentation only).
|
|
double motion_um(std::int16_t component, double ticks) {
|
|
return static_cast<double>(component) *
|
|
static_cast<double>(network::motion::speed_um_per_tick) / 1000.0 * ticks;
|
|
}
|
|
} // namespace
|
|
|
|
void TickClock::reset() noexcept {
|
|
base_tick_.reset();
|
|
newest_ = 0;
|
|
returned_ = 0;
|
|
}
|
|
|
|
double TickClock::raw(double base, Clock::time_point now) const {
|
|
return base + (ms(now - base_time_) / limits_.tick_ms);
|
|
}
|
|
|
|
void TickClock::observe(std::uint64_t tick, Clock::time_point now) {
|
|
newest_ = std::max(newest_, tick);
|
|
seen_ = now;
|
|
const auto sample = static_cast<double>(tick);
|
|
if (!base_tick_) {
|
|
base_tick_ = sample;
|
|
base_time_ = now;
|
|
return;
|
|
}
|
|
const auto estimate = raw(*base_tick_, now);
|
|
const auto error = sample - estimate;
|
|
if (error > limits_.snap_ticks) {
|
|
base_tick_ = sample; // Far behind (a pause or a reconnect): catch up at once.
|
|
++snaps_;
|
|
} else {
|
|
base_tick_ = estimate + (error * limits_.gain); // Slew; at() keeps it monotonic.
|
|
}
|
|
base_time_ = now;
|
|
}
|
|
|
|
std::optional<double> TickClock::silence(Clock::time_point now) const {
|
|
if (!base_tick_)
|
|
return std::nullopt;
|
|
return ms(now - seen_) / limits_.tick_ms;
|
|
}
|
|
|
|
std::optional<double> TickClock::at(Clock::time_point now) {
|
|
if (!base_tick_)
|
|
return std::nullopt;
|
|
const auto bounded =
|
|
std::min(raw(*base_tick_, now), static_cast<double>(newest_) + limits_.max_ahead);
|
|
returned_ = std::max(returned_, bounded);
|
|
return returned_;
|
|
}
|
|
|
|
void MarkerInterpolator::reset() {
|
|
snapshots_.clear();
|
|
gaps_.clear();
|
|
delay_ = target_ = limits_.min_delay;
|
|
}
|
|
|
|
void MarkerInterpolator::push(std::uint64_t tick, std::span<const RemoteMarker> markers) {
|
|
if (!snapshots_.empty() && tick <= snapshots_.back().tick) {
|
|
++stats_.ignored;
|
|
return;
|
|
}
|
|
++stats_.snapshots;
|
|
if (!snapshots_.empty()) {
|
|
gaps_.push_back(tick - snapshots_.back().tick);
|
|
while (gaps_.size() > limits_.gap_window)
|
|
gaps_.pop_front();
|
|
// Cover the 90th-percentile gap between received snapshots, plus the clock's lead.
|
|
std::vector<std::uint64_t> sorted(gaps_.begin(), gaps_.end());
|
|
std::ranges::sort(sorted);
|
|
const auto p90 = sorted.at((sorted.size() - 1) * 9 / 10);
|
|
target_ = std::clamp(static_cast<double>(p90) + 1, limits_.min_delay, limits_.max_delay);
|
|
}
|
|
Snapshot s{tick, {markers.begin(), markers.end()}};
|
|
std::ranges::sort(s.markers, {}, &RemoteMarker::id);
|
|
snapshots_.push_back(std::move(s));
|
|
while (snapshots_.size() > limits_.max_snapshots)
|
|
snapshots_.pop_front();
|
|
}
|
|
|
|
void MarkerInterpolator::advance(double elapsed_ms) {
|
|
if (elapsed_ms <= 0)
|
|
return;
|
|
const auto seconds = elapsed_ms / 1000;
|
|
if (delay_ < target_)
|
|
delay_ = std::min(target_, delay_ + (limits_.raise_per_second * seconds));
|
|
else
|
|
delay_ = std::max(target_, delay_ - (limits_.lower_per_second * seconds));
|
|
}
|
|
|
|
std::vector<PresentedMarker> MarkerInterpolator::at(double render_tick) {
|
|
std::vector<PresentedMarker> out;
|
|
if (snapshots_.empty())
|
|
return out;
|
|
const auto& newest = snapshots_.back();
|
|
const auto show = [&](const Snapshot& s) {
|
|
for (const auto& m : s.markers)
|
|
out.push_back({m.id, static_cast<double>(m.state.x), static_cast<double>(m.state.y)});
|
|
};
|
|
if (render_tick >= static_cast<double>(newest.tick)) {
|
|
// Beyond the newest snapshot: continue the held vector briefly, then freeze (stale).
|
|
const auto beyond = render_tick - static_cast<double>(newest.tick);
|
|
const auto ticks = std::min(beyond, limits_.max_extrapolation);
|
|
const bool stale = beyond > limits_.max_extrapolation;
|
|
for (const auto& m : newest.markers)
|
|
out.push_back({m.id,
|
|
static_cast<double>(m.state.x) + motion_um(m.state.vector.x, ticks),
|
|
static_cast<double>(m.state.y) + motion_um(m.state.vector.y, ticks),
|
|
ticks > 0, stale});
|
|
stats_.extrapolated_frames += ticks > 0 ? 1 : 0;
|
|
stats_.stale_frames += stale ? 1 : 0;
|
|
return out;
|
|
}
|
|
if (render_tick <= static_cast<double>(snapshots_.front().tick)) {
|
|
show(snapshots_.front()); // Not enough history yet: the oldest as it was.
|
|
return out;
|
|
}
|
|
// a.tick <= render_tick < b.tick.
|
|
const auto b = std::ranges::find_if(
|
|
snapshots_, [&](const Snapshot& s) { return static_cast<double>(s.tick) > render_tick; });
|
|
const auto& a = *std::prev(b);
|
|
if (b->tick - a.tick > limits_.max_gap) {
|
|
++stats_.gap_snaps; // Too far apart to interpolate: hold the older (stale) until b.
|
|
show(a);
|
|
for (auto& m : out)
|
|
m.stale = true;
|
|
return out;
|
|
}
|
|
const auto t =
|
|
(render_tick - static_cast<double>(a.tick)) / static_cast<double>(b->tick - a.tick);
|
|
for (const auto& m : a.markers) {
|
|
const auto next = std::ranges::lower_bound(b->markers, m.id, {}, &RemoteMarker::id);
|
|
if (next == b->markers.end() || next->id != m.id) {
|
|
// Removed by b: shown where it was until b's tick, never moved toward a removal.
|
|
out.push_back({m.id, static_cast<double>(m.state.x), static_cast<double>(m.state.y)});
|
|
continue;
|
|
}
|
|
const auto lerp = [t](std::int64_t from, std::int64_t to) {
|
|
return static_cast<double>(from) +
|
|
((static_cast<double>(to) - static_cast<double>(from)) * t);
|
|
};
|
|
out.push_back({m.id, lerp(m.state.x, next->state.x), lerp(m.state.y, next->state.y)});
|
|
}
|
|
return out; // Markers new in b appear from b's tick.
|
|
}
|
|
} // namespace evo::client
|