From 2aaf5a2db48b33d7fca27d8d34d9395e066485b7 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 15 May 2022 08:12:13 -0700 Subject: [PATCH] Zero-time check for WaitForVBlank --- BeefySysLib/platform/win/WinBFApp.cpp | 30 ++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/BeefySysLib/platform/win/WinBFApp.cpp b/BeefySysLib/platform/win/WinBFApp.cpp index b1b2c328..c219b78e 100644 --- a/BeefySysLib/platform/win/WinBFApp.cpp +++ b/BeefySysLib/platform/win/WinBFApp.cpp @@ -1297,6 +1297,10 @@ void BFP_CALLTYPE WinBFApp::VSyncThreadProcThunk(void* ptr) void WinBFApp::VSyncThreadProc() { + DWORD lastBlankFinish = GetTickCount(); + + Array waitTimes; + while (!mClosing) { bool didWait = false; @@ -1315,13 +1319,29 @@ void WinBFApp::VSyncThreadProc() if (output != NULL) { - DWORD start = GetTickCount(); + DWORD startTick = GetTickCount(); bool success = output->WaitForVBlank() == 0; - int elapsed = (int)(GetTickCount() - start); - if (elapsed >= 20) + DWORD endTick = GetTickCount(); + + if (success) { - NOP; - } + int elapsed = (int)(endTick - startTick); + waitTimes.Add(elapsed); + if (waitTimes.mSize > 8) + waitTimes.RemoveAt(0); + + if (elapsed <= 1) + { + bool hadNonZero = false; + for (auto waitTime : waitTimes) + { + if (waitTime > 1) + hadNonZero = true; + } + if (!hadNonZero) + success = false; + } + } if (success) {