Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions runtime/app/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,58 @@ namespace bozo::app
g.BindFramebuffer(render::gl::kFramebuffer, 0);
}
SDL_GL_SwapWindow(window);
// On the monitor's vsync at the game's rate (no frames between, not lockstep, a shown window): the present
// waited out (glFinish after the swap returns at the refresh it lands on), so the driver queues no frames
// ahead of the display (each one a refresh's delay to the player's input: up to three with vsync), and its
// time is the refresh's. Each counts for the frame pacer (FramePacer::displayPresents), with their period
// once steady: the game's frames then end on the refreshes. PS2X_NO_SWAP_FINISH=1: the swap alone.
{
static const bool noFinish = [] { const char *v = std::getenv("PS2X_NO_SWAP_FINISH"); return v && *v == '1'; }();
static Clock::time_point lastSwap{};
static double periodUs = 0.0;
static uint32_t steady = 0;
if (rate == 0 && options.vsync && !options.lockstep && !options.hidden && !noFinish)
{
g.Finish();
const Clock::time_point swapped = Clock::now();
const double us = lastSwap == Clock::time_point{} ? 0.0
: std::chrono::duration<double, std::micro>(swapped - lastSwap).count();
lastSwap = swapped;
// Steady: thirty refreshes in a row within 1.5 ms of the period so far (a skipped refresh, a load's
// wait, a stale picture's present start it again; the pacer goes back to its clock meanwhile).
if (periodUs > 0.0 && std::fabs(us - periodUs) < 1500.0)
{
periodUs += (us - periodUs) * 0.05;
++steady;
}
else
{
periodUs = us > 4000.0 && us < 50000.0 ? us : 0.0;
steady = 0;
}
FramePacer::displayPeriodUs().store(steady >= 30u ? static_cast<uint32_t>(periodUs + 0.5) : 0u,
std::memory_order_relaxed);
FramePacer::displayPresents().fetch_add(1u, std::memory_order_release);
}
else if (lastSwap != Clock::time_point{})
{
lastSwap = {};
periodUs = 0.0;
steady = 0;
FramePacer::displayPeriodUs().store(0u, std::memory_order_relaxed);
}
}

if (options.stats && seconds(statsSince, now) >= 2.0)
{
const double span = seconds(statsSince, now);
std::fprintf(stderr,
"[window] t=%.1f s, game frame %llu: %.2f game frames/s, %.1f presents/s, %llu new pictures; time between "
"pictures %.2f ms avg, %.2f ms max, %llu over 20 ms\n",
"pictures %.2f ms avg, %.2f ms max, %llu over 20 ms; display %.3f ms\n",
elapsed, static_cast<unsigned long long>(frames), (frames - statsFrames) / span, presents / span,
static_cast<unsigned long long>(fresh), intervals ? 1000.0 * intervalSum / intervals : 0.0,
1000.0 * intervalMax, static_cast<unsigned long long>(over));
1000.0 * intervalMax, static_cast<unsigned long long>(over),
FramePacer::displayPeriodUs().load(std::memory_order_relaxed) / 1000.0);
statsSince = now;
statsFrames = frames;
presents = fresh = over = intervals = 0;
Expand Down
51 changes: 50 additions & 1 deletion runtime/include/bozo/app/frame_pacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -64,6 +65,24 @@ class FramePacer
return speed;
}

// The display's refresh, from the window (any thread): each present it made on the monitor's vsync (with the
// driver's queue drained, so each one is a refresh), and their steady period in microseconds (0: none, the presents
// not on vsync or not steady). While that period is within 1 % of the cap's, a paced frame ends on the display's
// next refresh instead of the pacer's own clock: on the pacer's clock the two 60 Hz drifted apart, and every so
// often a picture stayed two refreshes and the next was never shown (60 frames a second that do not feel smooth).
// A 59.94 Hz monitor runs the game 0.1 % slow; the sound's rate follows (bozo::app::AudioDevice). PS2X_NO_VSYNC_PACE=1
// keeps the pacer's clock.
static std::atomic<uint64_t> &displayPresents() noexcept
{
static std::atomic<uint64_t> presents{0};
return presents;
}
static std::atomic<uint32_t> &displayPeriodUs() noexcept
{
static std::atomic<uint32_t> period{0};
return period;
}

// Waits until the cap allows the next frame to start, then returns the time since the previous frame end.
double frameEnd()
{
Expand All @@ -87,7 +106,26 @@ class FramePacer
recordWork(std::chrono::duration<double>(now - m_lastReturn).count(), cap, now);
if (m_frames.load(std::memory_order_relaxed) == 0u)
m_previous = now - std::chrono::microseconds(1000000 / std::max<uint32_t>(cap, 60u));
if (cap != 0u && m_paced.load(std::memory_order_relaxed))
const uint32_t displayUs = displayPeriodUs().load(std::memory_order_relaxed);
const bool onDisplay = cap != 0u && !fast && m_paced.load(std::memory_order_relaxed) && displayUs != 0u &&
std::fabs(static_cast<double>(displayUs) * cap - 1e6) < 1e4 && !vsyncPaceOff();
if (onDisplay)
{
// The display's next refresh after this frame's start: not before nine tenths of a period (a refresh the
// frame already passed, a present that came early), not after two (no refresh: a hidden window).
const auto period = std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(1.0 / cap));
const Clock::time_point earliest = m_previous + period * 9 / 10, latest = m_previous + period * 2;
for (;;)
{
now = Clock::now();
if (now >= latest ||
(now >= earliest && displayPresents().load(std::memory_order_acquire) != m_displaySeen))
break;
sleepUntil(now + std::chrono::microseconds(250));
}
m_displaySeen = displayPresents().load(std::memory_order_acquire);
}
else if (cap != 0u && m_paced.load(std::memory_order_relaxed))
{
const auto period = std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(1.0 / cap));
due = m_previous + period;
Expand Down Expand Up @@ -117,6 +155,16 @@ class FramePacer
}

private:
static bool vsyncPaceOff()
{
static const bool off = []
{
const char *value = std::getenv("PS2X_NO_VSYNC_PACE");
return value && *value == '1';
}();
return off;
}

static bool statsWanted()
{
static const bool wanted = []
Expand Down Expand Up @@ -162,6 +210,7 @@ class FramePacer
std::atomic<double> m_lastStep{1.0 / 60.0};
std::chrono::steady_clock::time_point m_previous{};
std::chrono::steady_clock::time_point m_lastReturn{};
uint64_t m_displaySeen = 0;
// PS2X_FRAME_STATS (the game's thread only)
std::chrono::steady_clock::time_point m_statsSince{};
double m_workSum = 0.0, m_workMax = 0.0;
Expand Down
Loading