diff --git a/runtime/app/window.cpp b/runtime/app/window.cpp index 70d5d17..5f04aeb 100644 --- a/runtime/app/window.cpp +++ b/runtime/app/window.cpp @@ -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(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(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(frames), (frames - statsFrames) / span, presents / span, static_cast(fresh), intervals ? 1000.0 * intervalSum / intervals : 0.0, - 1000.0 * intervalMax, static_cast(over)); + 1000.0 * intervalMax, static_cast(over), + FramePacer::displayPeriodUs().load(std::memory_order_relaxed) / 1000.0); statsSince = now; statsFrames = frames; presents = fresh = over = intervals = 0; diff --git a/runtime/include/bozo/app/frame_pacer.h b/runtime/include/bozo/app/frame_pacer.h index 5ecf31e..f3d9af1 100644 --- a/runtime/include/bozo/app/frame_pacer.h +++ b/runtime/include/bozo/app/frame_pacer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -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 &displayPresents() noexcept + { + static std::atomic presents{0}; + return presents; + } + static std::atomic &displayPeriodUs() noexcept + { + static std::atomic 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() { @@ -87,7 +106,26 @@ class FramePacer recordWork(std::chrono::duration(now - m_lastReturn).count(), cap, now); if (m_frames.load(std::memory_order_relaxed) == 0u) m_previous = now - std::chrono::microseconds(1000000 / std::max(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(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(std::chrono::duration(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(std::chrono::duration(1.0 / cap)); due = m_previous + period; @@ -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 = [] @@ -162,6 +210,7 @@ class FramePacer std::atomic 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;