-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GUI] Support gui.fps_limit and reduce idle power consumption #1611
Changes from 13 commits
6ed785f
24b5af7
46f63d4
559d96e
d2ef53f
cc2a876
879e715
dd63547
57bb8dd
f1fa416
af718a8
b38abf6
55619b0
574a621
015e818
a2e70c6
9d0abd6
726786a
ca6b011
ca623c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -57,18 +57,82 @@ double Time::get_time() { | |||||
} | ||||||
#endif | ||||||
|
||||||
#ifdef _WIN64 | ||||||
#include <Windows.h> | ||||||
#pragma comment(lib, "Winmm.lib") | ||||||
|
||||||
void win_usleep(double us) { | ||||||
using us_t = chrono::duration<double, std::micro>; | ||||||
auto start = chrono::high_resolution_clock::now(); | ||||||
do { | ||||||
// still little possible to release cpu. | ||||||
// Note: | ||||||
// https://docs.microsoft.com/zh-cn/windows/win32/api/synchapi/nf-synchapi-sleep | ||||||
Sleep(0); | ||||||
} while ((us_t(chrono::high_resolution_clock::now() - start).count()) < us); | ||||||
} | ||||||
|
||||||
void win_msleep(DWORD ms) { | ||||||
if (ms == 0) | ||||||
Sleep(0); | ||||||
else { | ||||||
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||||||
timeSetEvent(ms, 1, (LPTIMECALLBACK)hEvent, 0, | ||||||
TIME_ONESHOT | TIME_CALLBACK_EVENT_SET); | ||||||
WaitForSingleObject(hEvent, INFINITE); | ||||||
CloseHandle(hEvent); | ||||||
} | ||||||
} | ||||||
#endif | ||||||
|
||||||
void Time::usleep(double us) { | ||||||
#ifdef _WIN64 | ||||||
Sleep(DWORD(us * 1e-3)); | ||||||
// use win_usleep for accuracy. | ||||||
if (us < 999) | ||||||
win_usleep(us); | ||||||
// use win_msleep to release cpu, precision < 1ms | ||||||
else | ||||||
win_msleep(DWORD(us * 1e-3)); | ||||||
#else | ||||||
::usleep(us); | ||||||
#endif | ||||||
} | ||||||
|
||||||
void Time::msleep(double ms) { | ||||||
#ifdef _WIN64 | ||||||
win_msleep(DWORD(ms)); | ||||||
#else | ||||||
::usleep(ms * 1e3_f64); | ||||||
#endif | ||||||
} | ||||||
|
||||||
void Time::sleep(double s) { | ||||||
Time::usleep(s * 1e6_f64); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is a mush more accurate milliseconds level sleep implementation instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds cool! But is that possible to sleep for microseconds (us) on Windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's sad truth that sleep accuracy can not be guaranteed when delay time is lower than 1ms :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related problem : is-there-a-windows-equivalent-of-nanosleep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sleeping for us seems tricky on win, how about just: #ifndef _WIN64
} while (dt > 1e-4_f64); // until dt <= 100us
#else
} while (dt > 1e-2_f64); // until dt <= 10ms
#endif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1ms is more accurate ( 1/60 = 16.6 ms , 1/120 = 8.3ms , 1/40 = 25 ms )
|
||||||
|
||||||
void Time::wait_until(double t) { | ||||||
// microsecond (us) sleep on Windows... sadly. | ||||||
double dt; | ||||||
if (t < Time::get_time()) { | ||||||
return; | ||||||
} | ||||||
do { // use system-provided sleep for large scale sleeping: | ||||||
dt = t - Time::get_time(); | ||||||
if (dt <= 0) { | ||||||
return; | ||||||
} | ||||||
#ifdef _WIN64 | ||||||
Time::sleep(dt * 0.5); | ||||||
#else | ||||||
Time::sleep(dt * (dt < 4e-2_f64 ? 0.02 : 0.4)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is any difference here ( using 0.02 or 0.4 )?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ensures that the dt of sleep is more accurate when small; without this, I found the FPS to be around 59; with this, I found the FPS to be accurately 60.00. |
||||||
#endif | ||||||
archibate marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} while (dt > 2e-4_f64); // until dt <= 200us | ||||||
|
||||||
// use an EBFE loop for small scale waiting: | ||||||
while (Time::get_time() < t - 1e-6_f64) | ||||||
; // until dt <= 1us | ||||||
} | ||||||
|
||||||
double Time::Timer::get_time() { | ||||||
return Time::get_time(); | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! I can confirm that it's running happily on my machine!
One concern, what is
Winmm.lib
? Will it cause compat issue?We'd better include it in
CMakeList.txt
instead of an inline#pragma comment
, WDYT?I'd like to provide more usage about linking libraries in CMake if you are not familiar to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Winmm.lib
is just a system multimedia API , which contains high resolution timer event functions .Winmm.dll
could be found in system path . And the minimum support client is winXP . This might not cause incompatibility problem.See : MSDN
At the last commit I have moved
#pragma commit
away and usedtarget_link_library
instead .