Threaded video: Display Pacing On VBlank Events

A setting, off by default. With Display Pacing, vsync off and
fullscreen, the hold releases each core frame on a vblank the display
server counted instead of sleeping to a deadline: the vblank nearest
the frame's due time, which accumulates in the content's period on the
clock, so 60 on 120 Hz and 59.94 on 60 Hz keep the content's rate. It
holds only where a frame's start cannot cost latency: the driver says
its present tears (the new present_immediate poke: Direct3D 11 and 12,
Vulkan in immediate mode), no variable refresh rate is in use, and the
content is no faster than the display. Everywhere else the timer
schedule paces, as it does with the setting off.

The display server's wait_vblank now returns a vblank count and the
latest one's time, with a bounded wait for the next. On Windows a
thread of its own sits in D3DKMTWaitForVerticalBlankEvent, which takes
no timeout, and counts; it parks unasked, and when the wait fails or
comes straight back. The hold waits two content periods at most, takes
no vblank that is old, and stands down when vblanks stop or do not
keep the display's grid - for good the second time running.

The harness drives the hold from a vblank clock of its own: the rate
at 60, 120 and 144 Hz, each case it stands aside for, and each way it
stands down. samples/gfx/win32_d3dkmt runs the Windows clock under
Wine on entry points of its own.
This commit is contained in:
libretroadmin committed 2026-11-16 09:54:12 +01:00
1 parent f6fc1c9d6e
commit c92579e47b
22 files changed
+737 -42

No files matched your search

+5 -2
View File
@@ -2438,13 +2438,16 @@ jobs:
samples/gfx/win32_window_proc/run.sh
python3 samples/gfx/win32_window_proc/modal_frame_run.py
- name: D3DKMT binding (mingw + Wine, headless)
- name: D3DKMT binding and vblank clock (mingw + Wine, headless)
shell: bash
run: |
set -o pipefail
# win32_common.c with HAVE_D3DKMT, which no other Linux job
# compiles, on entry points of the test's own: the binding
# follows the window's monitor and closes what it leaves.
# follows the window's monitor and closes what it leaves, and
# the vblank clock counts, bounds its caller's wait when the
# display stops, does not spin on a wait that fails or comes
# straight back, and parks unasked.
samples/gfx/win32_d3dkmt/run.sh
- name: Build and run window_driver_test for Windows (Wine)
+1
View File
@@ -1,4 +1,5 @@
# Future
- VIDEO/THREADED: 'Display Pacing On VBlank Events' (off by default): with Display Pacing, V-Sync off and fullscreen, each core frame is released on the display's own vblank events instead of a timer, where the display server has them (Windows) and the driver's present tears (Direct3D 11/12, Vulkan immediate mode); the timer paces everywhere else and whenever the events stop or keep no fixed rate
- VIDEO/THREADED: With V-Sync off, Display Pacing holds the core to the content's rate before the first frame is presented too; at content start or after a driver reinit a core ran hundreds or thousands of frames at once with audio sync off as well
- WIN32: Scanline Sync reads the scanline of the monitor the window is on, and follows the window to another; it read the first display's wherever the window was
- LIBRETRO-COMMON/FLAC: 24- and 32-bit FLAC decoded to 16 bits is rounded to the nearest step instead of cut down (every sample was half a step low on average); 16-bit FLAC is unchanged
+5
View File
@@ -571,6 +571,11 @@
* times. Off: the fixed-timer pacing threaded video always had. */
#define DEFAULT_VIDEO_THREADED_DISPLAY_PACING false
/* With display pacing, vsync off and a present that tears: release
* each core frame on the display's vblank events instead of a timer.
* Off: the timer. */
#define DEFAULT_VIDEO_THREADED_PACING_VBLANK false
/* Leave a frame the core marks as a repeat unpresented, so the display
* sees the content's own frame rate. Off: every repeat is presented. */
#define DEFAULT_VIDEO_SKIP_DUPE_FRAMES false
+1
View File
@@ -752,6 +752,7 @@ typedef struct settings
bool video_threaded;
bool video_threaded_present_repeat;
bool video_threaded_display_pacing;
bool video_threaded_pacing_vblank;
bool video_d3d9ex_present;
bool video_present_timing_from_display;
bool video_skip_dupe_frames;
+120 -10
View File
@@ -109,6 +109,7 @@ typedef char win32_dwm_timing_info_size_check[
#include <features/features_cpu.h>
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#include <rthreads/retro_eventcount.h>
#endif
#ifdef HAVE_DYLIB
#include <dynamic/dylib.h>
@@ -396,7 +397,7 @@ static void d3dkmt_init(void)
GetProcAddress(gdi32, "D3DKMTOpenAdapterFromHdc");
pD3DKMTGetScanLine = (D3DKMTGETSCANLINE)
GetProcAddress(gdi32, "D3DKMTGetScanLine");
/* Optional: d3dkmt_wait_vblank() reports its absence so the
/* Optional: d3dkmt_vblank_wait() reports its absence so the
* caller can fall back, and an adapter left open is only that. */
pD3DKMTWaitForVerticalBlankEvent = (D3DKMTWAITFORVERTICALBLANKEVENT)
GetProcAddress(gdi32, "D3DKMTWaitForVerticalBlankEvent");
@@ -410,16 +411,125 @@ static void d3dkmt_init(void)
d3dkmt_bind_first();
}
bool d3dkmt_wait_vblank(void)
#ifdef HAVE_THREADS
/* The vblank clock. D3DKMTWaitForVerticalBlankEvent takes no timeout
* and cannot be interrupted, so a thread of its own waits on it and
* counts for d3dkmt_vblank_wait(). It parks once nobody has asked for
* D3DKMT_VBLANK_IDLE vblanks and while the wait fails, and is never
* joined. */
#define D3DKMT_VBLANK_IDLE 256
static retro_eventcount_t d3dkmt_vb_tick;
static retro_eventcount_t d3dkmt_vb_wake;
static retro_atomic_int_t d3dkmt_vb_count = RETRO_ATOMIC_INT_INITIALIZER(0);
/* low 32 bits of the latest one's time */
static retro_atomic_int_t d3dkmt_vb_at = RETRO_ATOMIC_INT_INITIALIZER(0);
static retro_atomic_int_t d3dkmt_vb_asked = RETRO_ATOMIC_INT_INITIALIZER(0);
static retro_atomic_int_t d3dkmt_vb_ticking = RETRO_ATOMIC_INT_INITIALIZER(0);
/* The caller's: 0 not started, 1 running, -1 cannot */
static int d3dkmt_vb_state;
static void d3dkmt_vblank_thread(void *userdata)
{
D3DKMT_WAITFORVERTICALBLANKEVENT vb;
if ( !pD3DKMTWaitForVerticalBlankEvent
|| !d3dkmt_bound(&vb.hAdapter, &vb.VidPnSourceId))
return false;
/* hDevice is documented optional and is not needed to wait on a
* VidPn source. */
vb.hDevice = 0;
return (pD3DKMTWaitForVerticalBlankEvent(&vb) == STATUS_SUCCESS);
unsigned idle = 0;
unsigned quick = 0;
(void)userdata;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
for (;;)
{
int key;
D3DKMT_WAITFORVERTICALBLANKEVENT vb;
if (retro_atomic_exchange_int(&d3dkmt_vb_asked, 0))
idle = 0;
if ( idle++ < D3DKMT_VBLANK_IDLE
&& d3dkmt_bound(&vb.hAdapter, &vb.VidPnSourceId))
{
retro_time_t before = cpu_features_get_time_usec();
/* hDevice is documented optional and is not needed to wait
* on a VidPn source. */
vb.hDevice = 0;
if (pD3DKMTWaitForVerticalBlankEvent(&vb) == STATUS_SUCCESS)
{
retro_time_t now = cpu_features_get_time_usec();
int n = (int)((unsigned)
retro_atomic_load_relaxed_int(&d3dkmt_vb_count) + 1u);
/* Called just ahead of a vblank it comes straight back
* once; one that keeps doing so is not waiting at all */
quick = (now - before < 200) ? quick + 1 : 0;
if (quick < 2)
{
retro_atomic_store_release_int(&d3dkmt_vb_at,
(int)(uint32_t)now);
retro_atomic_store_release_int(&d3dkmt_vb_count,
n ? n : 1);
retro_atomic_store_release_int(&d3dkmt_vb_ticking, 1);
retro_eventcount_notify(&d3dkmt_vb_tick);
continue;
}
}
}
retro_atomic_store_release_int(&d3dkmt_vb_ticking, 0);
retro_eventcount_notify(&d3dkmt_vb_tick);
key = retro_eventcount_prepare_wait(&d3dkmt_vb_wake);
if (retro_atomic_load_acquire_int(&d3dkmt_vb_asked))
retro_eventcount_cancel_wait(&d3dkmt_vb_wake);
else
retro_eventcount_commit_wait(&d3dkmt_vb_wake, key);
idle = 0;
quick = 0;
}
}
#endif
unsigned d3dkmt_vblank_wait(unsigned seen, unsigned timeout_us,
retro_time_t *at)
{
#ifdef HAVE_THREADS
unsigned n;
retro_time_t now, end;
if (d3dkmt_vb_state <= 0)
{
sthread_t *thread;
if (d3dkmt_vb_state < 0 || !pD3DKMTWaitForVerticalBlankEvent)
return 0;
d3dkmt_vb_state = -1;
if ( !retro_eventcount_init(&d3dkmt_vb_tick)
|| !retro_eventcount_init(&d3dkmt_vb_wake)
|| !(thread = sthread_create(d3dkmt_vblank_thread, NULL)))
return 0;
sthread_detach(thread);
d3dkmt_vb_state = 1;
}
if (!retro_atomic_load_relaxed_int(&d3dkmt_vb_asked))
{
retro_atomic_store_release_int(&d3dkmt_vb_asked, 1);
retro_eventcount_notify(&d3dkmt_vb_wake);
}
now = cpu_features_get_time_usec();
end = now + timeout_us;
for (;;)
{
int key = retro_eventcount_prepare_wait(&d3dkmt_vb_tick);
if (!retro_atomic_load_acquire_int(&d3dkmt_vb_ticking))
{
retro_eventcount_cancel_wait(&d3dkmt_vb_tick);
return 0;
}
n = (unsigned)retro_atomic_load_acquire_int(&d3dkmt_vb_count);
if (n != seen || now >= end)
{
retro_eventcount_cancel_wait(&d3dkmt_vb_tick);
break;
}
retro_eventcount_commit_wait_timeout(&d3dkmt_vb_tick, key, end - now);
now = cpu_features_get_time_usec();
}
now = cpu_features_get_time_usec();
*at = now - (retro_time_t)(uint32_t)((uint32_t)now
- (uint32_t)retro_atomic_load_acquire_int(&d3dkmt_vb_at));
return n;
#else
return 0;
#endif
}
int d3dkmt_scanline_get(void)
+8 -7
View File
@@ -324,15 +324,16 @@ static D3DKMTCLOSEADAPTER pD3DKMTCloseAdapter;
extern int d3dkmt_scanline_get(void);
/* Block until the display signals vertical blank. Returns false when
* the entry point is unavailable or the wait fails, in which case the
* caller has no anchor and must fall back to polling.
/* The display's vblank count, and in @at when the latest one was on
* cpu_features_get_time_usec()'s clock, after waiting @timeout_us at
* most for the count to pass @seen. 0, without waiting, while none are
* being counted: the clock is starting, the display is not scanning
* out, or there are no vblank events here. One calling thread.
*
* Measured on a 4K120 panel: 0 intervals outside +-20%% of the median
* across 499 samples, p1..p99 spread 30 us, period accurate to 0.02%%.
* That makes it a usable phase reference; GetScanLine at ~223 us a call
* is not. */
extern bool d3dkmt_wait_vblank(void);
* across 499 samples, p1..p99 spread 30 us, period accurate to 0.02%%. */
unsigned d3dkmt_vblank_wait(unsigned seen, unsigned timeout_us,
retro_time_t *at);
/* Binds the two above to the monitor @hwnd is on. Called by the
* window's thread whenever the window may have changed monitors;
+3 -2
View File
@@ -1649,10 +1649,11 @@ static int win32_display_server_get_scanline(void *data)
return d3dkmt_scanline_get();
}
static bool win32_display_server_wait_vblank(void *data)
static unsigned win32_display_server_wait_vblank(void *data,
unsigned seen, unsigned timeout_us, retro_time_t *at)
{
(void)data;
return d3dkmt_wait_vblank();
return d3dkmt_vblank_wait(seen, timeout_us, at);
}
#endif
+11 -1
View File
@@ -5971,6 +5971,15 @@ static void d3d11_driver_pacing_mark(void *data, uint64_t frame_id,
frame_id, marker);
}
static bool d3d11_present_immediate(void *data)
{
d3d11_video_t *d3d11 = (d3d11_video_t*)data;
/* as d3d11_gfx_frame() chooses DXGI_PRESENT_ALLOW_TEARING */
return d3d11
&& (d3d11->flags & D3D11_ST_FLAG_HAS_ALLOW_TEARING)
&& !(d3d11->flags & D3D11_ST_FLAG_VSYNC);
}
static retro_time_t d3d11_get_last_present_time(void *data)
{
DXGI_FRAME_STATISTICS stats;
@@ -10740,7 +10749,8 @@ static const video_poke_interface_t d3d11_poke_interface = {
d3d11_driver_pacing_wait,
d3d11_driver_pacing_mark,
NULL, /* prepare_import_texture */
d3d11_frame_mem_map
d3d11_frame_mem_map,
d3d11_present_immediate
};
static void d3d11_gfx_get_poke_interface(void* data,
+11 -1
View File
@@ -7782,6 +7782,13 @@ static void d3d12_driver_pacing_mark(void *data, uint64_t frame_id,
frame_id, marker);
}
static bool d3d12_present_immediate(void *data)
{
d3d12_video_t *d3d12 = (d3d12_video_t*)data;
/* as d3d12_gfx_frame() chooses DXGI_PRESENT_ALLOW_TEARING */
return d3d12 && !(d3d12->flags & D3D12_ST_FLAG_VSYNC);
}
static retro_time_t d3d12_get_last_present_time(void *data)
{
DXGI_FRAME_STATISTICS stats;
@@ -13784,7 +13791,10 @@ static const video_poke_interface_t d3d12_poke_interface = {
false, /* texture_lend_per_write */
d3d12_driver_pacing_set,
d3d12_driver_pacing_wait,
d3d12_driver_pacing_mark
d3d12_driver_pacing_mark,
NULL, /* prepare_import_texture */
NULL, /* frame_mem_map */
d3d12_present_immediate
};
static void d3d12_gfx_get_poke_interface(void* data, const video_poke_interface_t** iface)
+18 -2
View File
@@ -9953,6 +9953,20 @@ static retro_time_t vulkan_get_last_present_wait(void *data, bool *shown)
return data ? vulkan_present_wait : 0;
}
static bool vulkan_present_immediate(void *data)
{
vk_t *vk = (vk_t*)data;
if (!vk || !vk->context)
return false;
#if defined(_WIN32) && !defined(__WINRT__)
/* Presented through DXGI, the swapchain's mode is not what shows */
if (VULKAN_CTX_DATA_FROM_CONTEXT(vk->context)->dxgi_presenter)
return false;
#endif
return vk->context->swapchain_present_mode
== VK_PRESENT_MODE_IMMEDIATE_KHR;
}
static retro_time_t vulkan_get_last_present_time(void *data)
{
retro_time_t t;
@@ -17352,10 +17366,12 @@ static const video_poke_interface_t vulkan_poke_interface = {
#if defined(HAVE_VAAPI) || (defined(ANDROID) \
&& (defined(HAVE_RMP4) || defined(HAVE_RVP9) || defined(HAVE_RAV1) \
|| defined(HAVE_RECORD_MEDIACODEC)))
vulkan_prepare_import_texture
vulkan_prepare_import_texture,
#else
NULL
NULL,
#endif
NULL, /* frame_mem_map */
vulkan_present_immediate
};
static void vulkan_get_poke_interface(void *data,
+10 -4
View File
@@ -25,6 +25,7 @@
#include <boolean.h>
#include <retro_inline.h>
#include <libretro.h>
#include "video_defines.h"
#include "modeline/modeline_core.h"
@@ -96,8 +97,11 @@ typedef struct video_display_server
*
* get_scanline returns the current beam position in scanlines,
* blanking included, or a negative value if unavailable.
* wait_vblank blocks until the next vertical blank and returns
* false if it cannot.
* wait_vblank returns the display's vblank count and, in @at, when
* the latest one was (cpu_features_get_time_usec's clock), after
* waiting @timeout_us at most for the count to pass @seen. It
* returns 0, without waiting, while it has no vblank events to
* count. For one calling thread.
*
* Both are optional and a server may implement one without the
* other, but Scanline Sync needs get_scanline: it targets a specific
@@ -111,7 +115,8 @@ typedef struct video_display_server
* from DRM's vblank timestamps. X11's glXWaitForMscOML and Wayland's
* presentation-time are not wired up. */
int (*get_scanline)(void *data);
bool (*wait_vblank)(void *data);
unsigned (*wait_vblank)(void *data, unsigned seen,
unsigned timeout_us, retro_time_t *at);
/* Video modeline application. The engine in gfx/modeline/ generates
* a full video_modeline_t; these ops put it on the wire. A server
@@ -188,7 +193,8 @@ void video_display_server_media_refresh(unsigned num, unsigned den);
bool video_display_server_get_flags(gfx_ctx_flags_t *flags);
int video_display_server_get_scanline(void);
bool video_display_server_wait_vblank(void);
unsigned video_display_server_wait_vblank(unsigned seen,
unsigned timeout_us, retro_time_t *at);
/* The beam's line elapsed_ns after line 0 of a frame frame_ns long
* and total_lines tall, blanking included. elapsed_ns may be
+8 -3
View File
@@ -3315,13 +3315,14 @@ bool video_display_server_has_scanline(void)
return current_display_server && current_display_server->get_scanline;
}
bool video_display_server_wait_vblank(void)
unsigned video_display_server_wait_vblank(unsigned seen,
unsigned timeout_us, retro_time_t *at)
{
video_driver_state_t *video_st = &video_driver_st;
if (current_display_server && current_display_server->wait_vblank)
return current_display_server->wait_vblank(
video_st->current_display_server_data);
return false;
video_st->current_display_server_data, seen, timeout_us, at);
return 0;
}
float video_display_server_get_refresh_rate(void)
@@ -7479,8 +7480,12 @@ void video_driver_build_info(video_frame_info_t *video_info)
#ifdef HAVE_THREADS
video_info->threaded_display_pacing = settings->bools.video_threaded_display_pacing
&& video_st->thread_wrapper_active;
video_info->threaded_pacing_vblank = video_info->threaded_display_pacing
&& settings->bools.video_threaded_pacing_vblank
&& !settings->bools.vrr_runloop_enable;
#else
video_info->threaded_display_pacing = false;
video_info->threaded_pacing_vblank = false;
#endif
video_info->present_timing_from_display = settings->bools.video_present_timing_from_display;
video_info->scan_subframes = settings->bools.video_scan_subframes;
+9
View File
@@ -784,6 +784,10 @@ typedef struct video_frame_info
/* The threaded presenter is holding each push to the display's
* vblank: the setting is on and the wrapper is running. */
bool threaded_display_pacing;
/* ...and may hold on the display's vblank events instead of a
* timer: its setting is on, and no variable refresh rate is in
* use. */
bool threaded_pacing_vblank;
bool present_timing_from_display;
/* This frame's views and where each is drawn. views.num_views is 0
* when the frame is drawn whole. */
@@ -1485,6 +1489,11 @@ typedef struct video_poke_interface
* All mappings in a pool must use the same pitch (zero permits any pitch). */
bool (*frame_mem_map)(void *data, uintptr_t id,
video_frame_memory_t *mapping);
/* Whether a present made now, with vsync off, goes straight to the
* scanout - it tears - rather than waiting for a vblank or a
* compositor. Called after a frame, on its thread. A driver that
* cannot tell leaves it NULL. */
bool (*present_immediate)(void *data);
} video_poke_interface_t;
/* dims is the frame's size, VIDEO_SCALE_PACK'd; msg is for showing a
+106
View File
@@ -33,6 +33,7 @@
#endif
#include "video_driver.h"
#include "video_display_server.h"
#include "video_thread_wrapper.h"
#include "gfx_instrument.h"
@@ -1856,6 +1857,7 @@ static void video_thread_publish_stats(thread_video_t *thr)
int flags =
(thr->present_repeat ? VIDEO_THREAD_STAT_F_PRESENT_REPEAT : 0)
| (thr->phase_from_display ? VIDEO_THREAD_STAT_F_PHASE_DISPLAY : 0)
| (thr->present_immediate ? VIDEO_THREAD_STAT_F_PRESENT_IMMEDIATE : 0)
| (thr->latency_from_display ? VIDEO_THREAD_STAT_F_LAT_DISPLAY : 0)
| (retro_atomic_load_relaxed_int(&thr->display_pacing_pub)
? VIDEO_THREAD_STAT_F_DISPLAY_PACING : 0);
@@ -3339,6 +3341,9 @@ static void video_thread_loop(void *data)
? (retro_time_t)(1000000.0f * (float)presents / hz) : 0;
new_repeat = video_info->retain_output;
new_ask = video_info->present_timing_from_display;
thr->present_immediate = thr->poke
&& thr->poke->present_immediate
&& thr->poke->present_immediate(thr->driver_data);
}
else
thr->present_repeat = false;
@@ -3894,6 +3899,98 @@ static VIDEO_NOINLINE void video_thread_handoff_latch(thread_video_t *thr,
}
}
/* Frames held on the display's vblanks that earn a failure a second
* chance, and how long it stands aside before taking it. */
#define VIDEO_THREAD_VBLANK_TRUST 256
static void video_thread_vblank_stand_down(thread_video_t *thr)
{
thr->vblank_off = thr->vblank_held < VIDEO_THREAD_VBLANK_TRUST
? ~0u : VIDEO_THREAD_VBLANK_TRUST;
thr->vblank_held = 0;
thr->vblank_late = 0;
thr->vblank_due = 0;
}
/* With vsync off and a present that tears, when a frame starts adds no
* latency, so the display's vblanks release it in place of a timer:
* each frame on the vblank nearest its due time, which accumulates in
* the content's period on the clock. The wait is bounded; vblanks that
* stop, or keep no fixed grid (a variable refresh rate), stand the
* hold down - for good the second time running. False: the caller's
* schedule. */
static VIDEO_NOINLINE bool video_thread_hold_vblank(thread_video_t *thr,
retro_time_t now, retro_time_t content, retro_time_t period)
{
retro_time_t bound;
retro_time_t at = 0;
bool waited = false;
unsigned n;
/* Content faster than the display is due between vblanks, which
* only a timer can place */
if (content < period - period / 16)
return false;
if (thr->vblank_off)
{
if (thr->vblank_off != ~0u)
thr->vblank_off--;
return false;
}
n = thr->vblank_wait(0, 0, &at);
if (!n || now - at > period * 4)
{
/* None counted yet, none here, or the last one is old and the
* display is not scanning out: asked again shortly */
thr->vblank_off = 4;
thr->vblank_due = 0;
return false;
}
/* From the latest vblank at the start and after a stall: no backlog */
if (thr->vblank_due <= 0 || thr->vblank_due < now - content)
thr->vblank_due = at;
else
thr->vblank_due += content;
bound = now + content * 2;
/* A frame went out a period late for having queued behind another:
* a content period more, so the queue drains */
if (retro_atomic_fetch_and_int(&thr->drain_pending, 0))
{
thr->vblank_due += content;
bound += content;
thr->handoff.drains++;
}
while (at + period / 2 < thr->vblank_due)
{
unsigned seen = n;
retro_time_t left = bound - cpu_features_get_time_usec();
if (left > 0)
n = thr->vblank_wait(seen, (unsigned)left, &at);
if (left <= 0 || !n || n == seen)
{
/* They stopped: the display is asleep, or gone */
video_thread_vblank_stand_down(thr);
thr->content_due = 0;
return true;
}
waited = true;
}
/* On a fixed grid the vblank waited for is within half a period of
* the due time */
if (waited && at > thr->vblank_due + period)
{
if (++thr->vblank_late >= 8)
video_thread_vblank_stand_down(thr);
}
else if (waited && thr->vblank_late)
thr->vblank_late--;
if (thr->vblank_held < VIDEO_THREAD_VBLANK_TRUST)
thr->vblank_held++;
/* The clock schedule starts over, should it pace again */
thr->content_due = 0;
return true;
}
/* The hold's wait, to @target on the clock */
static void video_thread_hold_until(thread_video_t *thr,
retro_time_t now, retro_time_t target)
@@ -4024,6 +4121,11 @@ static VIDEO_NOINLINE void video_thread_pace_hold(thread_video_t *thr,
/* For the video thread, which judges a queued frame by it */
retro_atomic_store_release_int(&thr->content_period_us,
(int)content);
if ( thr->vblank_pacing
&& thr->nonblock
&& (snap.flags & VIDEO_THREAD_STAT_F_PRESENT_IMMEDIATE)
&& video_thread_hold_vblank(thr, now, content, period))
return;
if (thr->content_due <= 0 || thr->content_due < now - content)
thr->content_due = next;
else
@@ -4320,6 +4422,8 @@ static bool video_thread_frame(void *data, const void *frame_,
thr->fast_forward = video_info->input_driver_nonblock_state;
thr->core_running = video_info->core_running;
thr->menu_content_rate = video_info->menu_content_rate;
thr->vblank_pacing = video_info->threaded_pacing_vblank
&& video_info->fullscreen;
retro_atomic_store_relaxed_int(&thr->display_pacing_pub,
thr->display_pacing ? 1 : 0);
}
@@ -4799,6 +4903,8 @@ static bool video_thread_init(thread_video_t *thr,
retro_atomic_int_init(&thr->repeat_request, 0);
retro_atomic_int_init(&thr->repeat_group, 0);
retro_atomic_int_init(&thr->drain_pending, 0);
thr->vblank_wait = video_display_server_wait_vblank;
thr->vblank_held = VIDEO_THREAD_VBLANK_TRUST;
retro_atomic_int_init(&thr->movie_capable, 0);
video_thread_movie_state_init(&thr->movie_state);
retro_atomic_int_init(&thr->core_time_us, 0);
+17
View File
@@ -368,6 +368,7 @@ enum video_thread_vp_slot
#define VIDEO_THREAD_STAT_F_PHASE_DISPLAY (1 << 1)
#define VIDEO_THREAD_STAT_F_LAT_DISPLAY (1 << 2)
#define VIDEO_THREAD_STAT_F_DISPLAY_PACING (1 << 3)
#define VIDEO_THREAD_STAT_F_PRESENT_IMMEDIATE (1 << 4)
/* Cache-line padding, where two threads write fields that would
* otherwise share a line. A line carrying a write from each is pulled
@@ -484,6 +485,8 @@ typedef struct thread_video
/* Whether the last repeat deadline came from a display timestamp
* the driver reported (true) or from the clock (false). Stats. */
bool phase_from_display;
/* The driver's present_immediate() after the last frame. Stats. */
bool present_immediate;
/* The wrapped driver's answer to get_refresh_rate, polled on the
* video thread after each frame; 0 when it has none. The presenter
* paces on it in preference to the setting. Video thread only -
@@ -545,6 +548,9 @@ typedef struct thread_video
* info at the push: the hold keeps the content's period over the
* stopped core */
bool menu_content_rate;
/* Display Pacing On VBlank Events may hold this frame, from the
* frame info at the push */
bool vblank_pacing;
/* What the main thread writes every frame, kept off the lines the
* video thread writes (see VIDEO_THREAD_LINE): the push's clock and
* display pacing's moving averages and schedule, then the values it
@@ -565,6 +571,17 @@ typedef struct thread_video
retro_time_t content_due;
retro_time_t content_period;
retro_time_t run_start;
/* The hold on the display's vblank events
* (video_thread_hold_vblank): its own due time, the display
* server's vblank clock, the frames it stands aside for (~0u: for
* good), the frames it has held since it last stood down, and the
* vblanks that came late for their frame. Main thread. */
retro_time_t vblank_due;
unsigned (*vblank_wait)(unsigned seen, unsigned timeout_us,
retro_time_t *at);
unsigned vblank_off;
unsigned vblank_held;
unsigned vblank_late;
/* Frames Driver Pacing released, the driver's sleep in the hold's
* place (video_thread_pace_hold). Main thread. */
uint64_t driver_released;
+1
View File
@@ -2183,6 +2183,7 @@ int menu_cbs_init_bind_sublabel(menu_file_list_cbs_t *cbs,
{ MENU_ENUM_LABEL_VIDEO_D3D9EX_PRESENT, MENU_ENUM_SUBLABEL_VIDEO_D3D9EX_PRESENT },
{ MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT, MENU_ENUM_SUBLABEL_VIDEO_THREADED_PRESENT_REPEAT },
{ MENU_ENUM_LABEL_VIDEO_THREADED_DISPLAY_PACING, MENU_ENUM_SUBLABEL_VIDEO_THREADED_DISPLAY_PACING },
{ MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK, MENU_ENUM_SUBLABEL_VIDEO_THREADED_PACING_VBLANK },
{ MENU_ENUM_LABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY, MENU_ENUM_SUBLABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY },
{ MENU_ENUM_LABEL_VIDEO_SKIP_DUPE_FRAMES, MENU_ENUM_SUBLABEL_VIDEO_SKIP_DUPE_FRAMES },
{ MENU_ENUM_LABEL_VIDEO_MAX_FRAME_LATENCY, MENU_ENUM_SUBLABEL_VIDEO_MAX_FRAME_LATENCY },
+9
View File
@@ -12959,6 +12959,9 @@ unsigned menu_displaylist_build_list(
{MENU_ENUM_LABEL_VIDEO_WAITABLE_SWAPCHAINS, PARSE_ONLY_BOOL, false},
{MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT, PARSE_ONLY_BOOL, false},
{MENU_ENUM_LABEL_VIDEO_THREADED_DISPLAY_PACING, PARSE_ONLY_BOOL, false},
#ifdef HAVE_D3DKMT
{MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK, PARSE_ONLY_BOOL, false},
#endif
{MENU_ENUM_LABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY, PARSE_ONLY_BOOL, false},
{MENU_ENUM_LABEL_VIDEO_SKIP_DUPE_FRAMES, PARSE_ONLY_BOOL, true},
{MENU_ENUM_LABEL_VIDEO_MAX_FRAME_LATENCY, PARSE_ONLY_INT, false},
@@ -13022,6 +13025,12 @@ unsigned menu_displaylist_build_list(
build_list[i].checked = *video_driver_get_threaded()
&& !video_driver_is_hw_context();
break;
case MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK:
/* The hold's own setting: only where it holds */
build_list[i].checked = *video_driver_get_threaded()
&& !video_driver_is_hw_context()
&& config_get_ptr()->bools.video_threaded_display_pacing;
break;
case MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT:
/* Only where threaded video is on and can be honoured:
* a hardware-rendered core runs unthreaded whatever
+2
View File
@@ -943,6 +943,7 @@ extern const struct msg_hash_us_lbl_blob_s msg_hash_us_lbl_blob;
#define MENU_ENUM_LABEL_VIDEO_D3D9EX_PRESENT_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_D3D9EX_PRESENT)
#define MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT)
#define MENU_ENUM_LABEL_VIDEO_THREADED_DISPLAY_PACING_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_THREADED_DISPLAY_PACING)
#define MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK)
#define MENU_ENUM_LABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY)
#define MENU_ENUM_LABEL_VIDEO_SKIP_DUPE_FRAMES_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_SKIP_DUPE_FRAMES)
#define MENU_ENUM_LABEL_VIDEO_MAX_FRAME_LATENCY_STR (msg_hash_us_lbl_blob.m_MENU_ENUM_LABEL_VIDEO_MAX_FRAME_LATENCY)
@@ -2735,6 +2736,7 @@ extern const struct msg_hash_us_lbl_blob_s msg_hash_us_lbl_blob;
#define MENU_ENUM_LABEL_VIDEO_D3D9EX_PRESENT_STR "video_d3d9ex_present"
#define MENU_ENUM_LABEL_VIDEO_THREADED_PRESENT_REPEAT_STR "video_threaded_present_repeat"
#define MENU_ENUM_LABEL_VIDEO_THREADED_DISPLAY_PACING_STR "video_threaded_display_pacing"
#define MENU_ENUM_LABEL_VIDEO_THREADED_PACING_VBLANK_STR "video_threaded_pacing_vblank"
#define MENU_ENUM_LABEL_VIDEO_PRESENT_TIMING_FROM_DISPLAY_STR "video_present_timing_from_display"
#define MENU_ENUM_LABEL_VIDEO_SKIP_DUPE_FRAMES_STR "video_skip_dupe_frames"
#define MENU_ENUM_LABEL_VIDEO_MAX_FRAME_LATENCY_STR "video_max_frame_latency"
@@ -5723,6 +5723,244 @@ static void lane_blocking_present(void)
/* and nothing should be dropped: N frames take about N periods. */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* Lane: display pacing on the display's vblank events */
/* With its setting on, vsync off, fullscreen and a driver whose */
/* present tears, the hold releases each frame on a vblank the */
/* display server counted, at the content's rate whatever the */
/* display's is. It stands aside wherever one of those is missing, */
/* and stands down when the vblanks stop or keep no fixed rate. */
/* ------------------------------------------------------------------ */
static video_poke_interface_t vblane_poke;
static const video_poke_interface_t *vblane_inner_poke;
static retro_atomic_int_t vblane_immediate;
/* The display server's clock: a display at vblane_period from
* vblane_origin, stopped at vblane_stop. The hold's thread only. */
static retro_time_t vblane_origin;
static retro_time_t vblane_period;
static retro_time_t vblane_stop;
/* stop as the next wait starts: no vblank comes for it */
static bool vblane_stop_armed;
static unsigned vblane_asks;
static unsigned vblane_waits;
static bool vblane_present_immediate(void *data)
{
(void)data;
return retro_atomic_load_acquire_int(&vblane_immediate) != 0;
}
static unsigned vblane_count(retro_time_t now, retro_time_t *at)
{
unsigned n;
if (vblane_stop && now > vblane_stop)
now = vblane_stop;
n = (unsigned)((now - vblane_origin) / vblane_period);
*at = vblane_origin + (retro_time_t)n * vblane_period;
return n + 1;
}
static unsigned vblane_wait(unsigned seen, unsigned timeout_us,
retro_time_t *at)
{
retro_time_t now = cpu_features_get_time_usec();
retro_time_t end = now + timeout_us;
unsigned n;
if (vblane_stop_armed && timeout_us)
{
vblane_stop = now;
vblane_stop_armed = false;
}
n = vblane_count(now, at);
vblane_asks++;
if (n == seen && now < end)
vblane_waits++;
while (n == seen && now < end)
{
retro_time_t next = *at + vblane_period;
if (next > end || (vblane_stop && next > vblane_stop))
next = end;
if (next - now > 2500)
retro_sleep(1);
now = cpu_features_get_time_usec();
n = vblane_count(now, at);
}
return n;
}
static void display_pacing_measure(float display_hz, unsigned n,
float expect_fps, const char *what);
/* The hold as a driver just up has it */
static void vblane_reset(thread_video_t *thr, float stub_hz)
{
thr->vblank_off = 0;
thr->vblank_held = 256;
thr->vblank_late = 0;
thr->vblank_due = 0;
vblane_origin = cpu_features_get_time_usec();
vblane_period = (retro_time_t)(1000000.0f / stub_hz);
vblane_stop = 0;
vblane_stop_armed = false;
vblane_asks = 0;
vblane_waits = 0;
}
static void lane_vblank_pacing(void)
{
unsigned had = failures;
video_driver_state_t *video_st = video_state_get_ptr();
settings_t *settings = config_get_ptr();
bool saved_pacing = settings->bools.video_threaded_display_pacing;
bool saved_vblank = settings->bools.video_threaded_pacing_vblank;
bool saved_fullscreen = settings->bools.video_fullscreen;
bool saved_vrr = settings->bools.vrr_runloop_enable;
float saved_hz = settings->floats.video_refresh_rate;
unsigned (*saved_wait)(unsigned, unsigned, retro_time_t*);
thread_video_t *thr;
unsigned i;
settings->bools.video_threaded_display_pacing = true;
settings->bools.video_threaded_pacing_vblank = true;
settings->bools.video_fullscreen = true;
settings->bools.vrr_runloop_enable = false;
set_threaded_via_setting(true);
run_frames(10);
expect_wrapper(true, "vblank-pacing lane");
if (menu_is_up())
command_event(CMD_EVENT_MENU_TOGGLE, NULL);
run_frames(3);
video_thread_wait_idle();
thr = (thread_video_t*)video_st->data;
CHECK(thr->nonblock, "vblank-pacing lane: the harness runs with vsync off");
thr->driver->poke_interface(thr->driver_data, &vblane_inner_poke);
vblane_poke = *vblane_inner_poke;
vblane_poke.present_immediate = vblane_present_immediate;
retro_atomic_store_release_int(&vblane_immediate, 1);
set_poke(thr, &vblane_poke);
saved_wait = thr->vblank_wait;
thr->vblank_wait = vblane_wait;
/* On the display's vblanks, at the content's rate: 60 fps on 60 Hz,
* on 120 Hz every other one, and on 144 Hz two and three in turn */
{
static const float hz[] = { 60.0f, 120.0f, 144.0f };
for (i = 0; i < ARRAY_SIZE(hz); i++)
{
vblane_reset(thr, hz[i]);
display_pacing_measure(hz[i], 60, 60.0f, "on vblank events");
CHECK(vblane_waits >= 40,
"vblank pacing at %.0f Hz: %u frames of 66 waited on a vblank",
hz[i], vblane_waits);
CHECK(!thr->vblank_off && thr->vblank_held == 256,
"vblank pacing at %.0f Hz stood down (off %u, late %u)",
hz[i], thr->vblank_off, thr->vblank_late);
}
}
/* Content faster than the display is due between its vblanks: the
* timer's to place */
settings->floats.video_refresh_rate = 30.0f;
run_frames(6);
video_thread_wait_idle();
run_frames(2);
vblane_reset(thr, 30.0f);
display_pacing_measure(30.0f, 30, 60.0f, "display slower than the content");
CHECK(!vblane_asks, "display slower than the content: the vblank clock was asked %u times",
vblane_asks);
settings->floats.video_refresh_rate = saved_hz;
/* Wherever a present does not go straight out, or the setting is
* off, windowed, or a variable refresh rate is in use, the timer
* paces and the vblank clock is not asked */
for (i = 0; i < 4; i++)
{
static const char *const what[] = {
"present waits", "setting off", "windowed", "variable refresh" };
if (i == 0)
retro_atomic_store_release_int(&vblane_immediate, 0);
else if (i == 1)
settings->bools.video_threaded_pacing_vblank = false;
else if (i == 2)
settings->bools.video_fullscreen = false;
else
settings->bools.vrr_runloop_enable = true;
run_frames(3);
video_thread_wait_idle();
vblane_reset(thr, 60.0f);
display_pacing_measure(60.0f, 30, 60.0f, what[i]);
CHECK(!vblane_asks, "vblank pacing, %s: the vblank clock was asked %u times",
what[i], vblane_asks);
retro_atomic_store_release_int(&vblane_immediate, 1);
settings->bools.video_threaded_pacing_vblank = true;
settings->bools.video_fullscreen = true;
settings->bools.vrr_runloop_enable = false;
}
run_frames(3);
video_thread_wait_idle();
/* The vblanks stop - the display went to sleep: the wait is bounded,
* the hold stands down and the timer keeps the rate. Stopped again
* before it has held long, it stands down for good. */
vblane_reset(thr, 60.0f);
run_frames(10);
vblane_stop_armed = true;
display_pacing_measure(60.0f, 60, 60.0f, "vblanks stopped");
CHECK(vblane_stop && thr->vblank_off && thr->vblank_off != ~0u,
"vblanks stopped: the hold did not stand down (off %u)",
thr->vblank_off);
vblane_stop = 0;
thr->vblank_off = 0;
run_frames(10);
CHECK(!thr->vblank_off, "vblanks back: the hold did not take them (off %u)",
thr->vblank_off);
vblane_stop_armed = true;
display_pacing_measure(60.0f, 30, 60.0f, "vblanks stopped again");
CHECK(vblane_stop && thr->vblank_off == ~0u,
"vblanks stopped twice: the hold is to stand down for good (off %u)",
thr->vblank_off);
/* Stopped before the hold asks - the display asleep all along - it
* neither waits nor releases frames on a vblank long past */
vblane_reset(thr, 60.0f);
vblane_stop = vblane_origin;
retro_sleep(100);
display_pacing_measure(60.0f, 30, 60.0f, "vblanks long stopped");
CHECK(!vblane_waits && thr->vblank_off != ~0u,
"vblanks long stopped: %u waits, off %u", vblane_waits,
thr->vblank_off);
/* Vblanks that keep no fixed rate - a variable refresh rate, where
* one comes only with a present: here 40 a second on what is said
* to be a 120 Hz display, each inside the wait's bound and late
* for its frame. The content's rate is kept by the timer once the
* hold has stood down. */
vblane_reset(thr, 40.0f);
settings->floats.video_refresh_rate = 120.0f;
run_frames(40);
CHECK(thr->vblank_off,
"vblanks off the display's grid: the hold did not stand down (late %u)",
thr->vblank_late);
display_pacing_measure(120.0f, 60, 60.0f, "vblanks off the grid");
video_thread_wait_idle();
thr->vblank_wait = saved_wait;
vblane_reset(thr, 60.0f);
set_poke(thr, vblane_inner_poke);
run_frames(3);
if (!menu_is_up())
command_event(CMD_EVENT_MENU_TOGGLE, NULL);
settings->bools.video_threaded_display_pacing = saved_pacing;
settings->bools.video_threaded_pacing_vblank = saved_vblank;
settings->bools.video_fullscreen = saved_fullscreen;
settings->bools.vrr_runloop_enable = saved_vrr;
settings->floats.video_refresh_rate = saved_hz;
if (failures == had)
fprintf(stderr, "[pass] vblank-pacing lane\n");
}
/* ------------------------------------------------------------------ */
/* Lane: display pacing before the first present */
/* With vsync off the present holds nothing, and until the video */
@@ -14458,6 +14696,7 @@ int main(int argc, char *argv[])
{
lane_display_pacing();
lane_first_present_hold();
lane_vblank_pacing();
lane_blocking_present();
lane_pacing_queue_drain();
lane_pacing_fast_display();
@@ -14541,6 +14780,7 @@ int main(int argc, char *argv[])
{
lane_display_pacing();
lane_first_present_hold();
lane_vblank_pacing();
lane_blocking_present();
lane_pacing_queue_drain();
lane_pacing_fast_display();
+1 -1
View File
@@ -1,5 +1,5 @@
#!/bin/sh
# Builds the D3DKMT binding test with mingw-w64 and
# Builds the D3DKMT binding and vblank clock test with mingw-w64 and
# runs it under Wine on a headless X server. It links the window
# procedure sample's stand-ins for what win32_common.c calls.
set -eu
+146 -9
View File
@@ -1,7 +1,8 @@
/* The D3DKMT binding of gfx/common/win32_common.c, on entry points of
* the test's own: Wine has neither D3DKMTGetScanLine nor
* D3DKMTWaitForVerticalBlankEvent, and a CI machine has one display.
* The file is included for its statics. */
/* The D3DKMT binding and vblank clock of gfx/common/win32_common.c,
* on entry points of the test's own: Wine has neither
* D3DKMTGetScanLine nor D3DKMTWaitForVerticalBlankEvent, and a CI
* machine has no display to wait on. The file is included for its
* statics. */
#include "../../../gfx/common/win32_common.c"
#include <stdio.h>
@@ -19,6 +20,8 @@ static unsigned failures;
} \
} while (0)
enum { FK_TICK = 0, FK_FAIL, FK_INSTANT, FK_BLOCK };
static volatile LONG fk_opens;
static volatile LONG fk_open_fails;
static volatile LONG fk_closes;
@@ -26,6 +29,8 @@ static volatile LONG fk_closed;
static volatile LONG fk_waits;
static volatile LONG fk_wait_adapter;
static volatile LONG fk_scan_adapter;
static volatile LONG fk_mode;
static volatile LONG fk_hz = 60;
static NTSTATUS CALLBACK fk_open(D3DKMT_OPENADAPTERFROMHDC *d)
{
@@ -52,16 +57,61 @@ static NTSTATUS CALLBACK fk_scan(D3DKMT_GETSCANLINE *d)
return STATUS_SUCCESS;
}
/* A display at fk_hz, one that fails, one whose wait comes straight
* back, and one that has stopped scanning out */
static NTSTATUS CALLBACK fk_wait(D3DKMT_WAITFORVERTICALBLANKEVENT *d)
{
InterlockedIncrement(&fk_waits);
InterlockedExchange(&fk_wait_adapter, (LONG)d->hAdapter);
switch (fk_mode)
{
case FK_FAIL:
return 1;
case FK_INSTANT:
return STATUS_SUCCESS;
case FK_BLOCK:
while (fk_mode == FK_BLOCK)
Sleep(5);
return 1;
default:
break;
}
{
retro_time_t period = 1000000 / fk_hz;
retro_time_t now = cpu_features_get_time_usec();
retro_time_t next = (now / period + 1) * period;
while (now < next)
{
if (next - now > 2500)
Sleep(1);
now = cpu_features_get_time_usec();
}
}
return STATUS_SUCCESS;
}
/* Asks until the clock answers @want_ticking, a second at most */
static unsigned ask_until(bool want_ticking)
{
unsigned i;
unsigned n = 0;
retro_time_t at = 0;
for (i = 0; i < 200; i++)
{
n = d3dkmt_vblank_wait(0, 0, &at);
if ((n != 0) == want_ticking)
break;
Sleep(5);
}
return n;
}
int main(void)
{
unsigned i;
unsigned i, n, seen;
LONG calls;
retro_time_t at = 0;
retro_time_t t0, took;
D3DKMT_HANDLE adapter;
UINT source;
HWND hwnd;
@@ -72,7 +122,7 @@ int main(void)
if (!pD3DKMTGetScanLine)
CHECK(d3dkmt_scanline_get() == -1, "a scanline with no entry point");
if (!pD3DKMTWaitForVerticalBlankEvent)
CHECK(!d3dkmt_wait_vblank(), "a vblank with no entry point");
CHECK(!d3dkmt_vblank_wait(0, 0, &at), "vblanks with no entry point");
hwnd = CreateWindowA("STATIC", "d3dkmt", WS_OVERLAPPEDWINDOW,
10, 10, 200, 200, NULL, NULL, GetModuleHandle(NULL), NULL);
@@ -89,7 +139,6 @@ int main(void)
/* ---- the binding follows the window ---- */
CHECK(!d3dkmt_bound(&adapter, &source), "bound before a window said where");
CHECK(d3dkmt_scanline_get() == -1, "a scanline with nothing bound");
CHECK(!d3dkmt_wait_vblank() && !fk_waits, "a vblank with nothing bound");
win32_d3dkmt_follow(hwnd, false);
CHECK(fk_opens == 1 && d3dkmt_bound(&adapter, &source)
&& adapter == 0x101 && source == 3,
@@ -119,10 +168,98 @@ int main(void)
"failed open: %ld closes, adapter %x", (long)fk_closes,
(unsigned)adapter);
CHECK(d3dkmt_wait_vblank() && fk_waits == 1 && fk_wait_adapter == 0x102,
"vblank not waited for on the bound source: adapter %lx",
/* ---- the clock counts the display's vblanks ---- */
seen = ask_until(true);
CHECK(seen, "the vblank clock never started");
t0 = cpu_features_get_time_usec();
for (i = 0; i < 30; i++)
{
retro_time_t now;
n = d3dkmt_vblank_wait(seen, 200000, &at);
now = cpu_features_get_time_usec();
CHECK(n && n != seen, "vblank %u: count %u after %u", i, n, seen);
CHECK(at <= now && now - at < 100000,
"vblank %u: timed %ld us before now", i, (long)(now - at));
seen = n;
}
took = cpu_features_get_time_usec() - t0;
CHECK(took > 350000 && took < 800000,
"30 vblanks at 60 Hz in %ld us", (long)took);
CHECK(fk_wait_adapter == 0x102, "waited on adapter %lx",
(long)fk_wait_adapter);
/* ---- and follows a rebind ---- */
win32_d3dkmt_follow(hwnd, true);
for (i = 0; i < 10 && fk_wait_adapter != 0x104; i++)
seen = d3dkmt_vblank_wait(seen, 200000, &at);
CHECK(fk_wait_adapter == 0x104 && fk_closed == 0x102,
"after a rebind: waits on %lx, closed %lx",
(long)fk_wait_adapter, (long)fk_closed);
seen = ask_until(true);
/* ---- a display that stops: the caller's wait is bounded ---- */
InterlockedExchange(&fk_mode, FK_BLOCK);
took = 0;
for (i = 0; i < 3; i++)
{
t0 = cpu_features_get_time_usec();
n = d3dkmt_vblank_wait(seen, 50000, &at);
took = cpu_features_get_time_usec() - t0;
if (n == seen)
break;
seen = n;
}
CHECK(n == seen && took >= 40000 && took < 500000,
"stopped display: count %u after %u, in %ld us", n, seen,
(long)took);
/* ---- a wait that fails: none counted, and no retry unasked ---- */
InterlockedExchange(&fk_mode, FK_FAIL);
CHECK(!ask_until(false), "a failing wait still counts");
Sleep(100);
calls = fk_waits;
Sleep(300);
CHECK(fk_waits == calls, "a failing wait retried unasked: %ld times",
(long)(fk_waits - calls));
InterlockedExchange(&fk_mode, FK_TICK);
CHECK(ask_until(true), "the clock did not come back");
/* ---- a wait that comes straight back is not spun on ---- */
InterlockedExchange(&fk_mode, FK_INSTANT);
calls = fk_waits;
for (i = 0; i < 30; i++)
{
d3dkmt_vblank_wait(0, 0, &at);
Sleep(10);
}
CHECK(fk_waits - calls < 200, "spun on a wait that does not wait: %ld calls",
(long)(fk_waits - calls));
InterlockedExchange(&fk_mode, FK_TICK);
/* ---- unasked, it parks after D3DKMT_VBLANK_IDLE vblanks ---- */
InterlockedExchange(&fk_hz, 1000);
CHECK(ask_until(true), "the clock did not come back at 1000 Hz");
/* counted from before the last ask, a few vblanks ahead of the one
* the clock takes it on */
calls = fk_waits;
d3dkmt_vblank_wait(0, 0, &at);
for (i = 0; i < 100; i++)
{
LONG before = fk_waits;
Sleep(200);
if (fk_waits == before || fk_waits - calls > D3DKMT_VBLANK_IDLE + 64)
break;
}
CHECK(fk_waits - calls >= D3DKMT_VBLANK_IDLE
&& fk_waits - calls <= D3DKMT_VBLANK_IDLE + 64,
"unasked, the clock waited on %ld vblanks before parking",
(long)(fk_waits - calls));
calls = fk_waits;
Sleep(200);
CHECK(fk_waits == calls, "parked, the clock kept waiting: %ld vblanks",
(long)(fk_waits - calls));
CHECK(ask_until(true), "parked, the clock did not come back when asked");
if (hwnd)
DestroyWindow(hwnd);
if (failures)
+5
View File
@@ -72,6 +72,11 @@ S_BOOL(video_threaded_display_pacing, VIDEO_THREADED_DISPLAY_PACING,
DEFAULT_VIDEO_THREADED_DISPLAY_PACING, SD_FLAG_NONE, 0, CMD_EVENT_NONE,
"Threaded Video Display Pacing",
"With Threaded Video, start each core frame as late as the next display refresh allows, from measured core and render times, instead of on a fixed timer. Lowers latency to what Frame Delay reaches on unthreaded video, and a frame that runs long is repeated rather than missed. With Driver Pacing in effect, the driver releases each frame, and the hold only keeps it on the display's refresh. Off: the timer paces, as before.")
S_BOOL(video_threaded_pacing_vblank, VIDEO_THREADED_PACING_VBLANK,
"video_threaded_pacing_vblank",
DEFAULT_VIDEO_THREADED_PACING_VBLANK, SD_FLAG_NONE, 0, CMD_EVENT_NONE,
"Display Pacing On VBlank Events",
"With Threaded Video Display Pacing, V-Sync off and fullscreen, release each core frame on the display's own vertical blank events instead of a timer. Applies where the display server has those events (Windows) and the video driver says its present tears rather than waits (Direct3D 11 and 12, Vulkan in immediate mode); the timer paces everywhere else, and wherever the events stop or do not keep a fixed rate. The tear line then sits at one height instead of drifting. Off: the timer, as before.")
S_BOOL(video_present_timing_from_display, VIDEO_PRESENT_TIMING_FROM_DISPLAY,
"video_present_timing_from_display",
DEFAULT_VIDEO_PRESENT_TIMING_FROM_DISPLAY, SD_FLAG_NONE, 0, CMD_EVENT_NONE,