From 88db48b8158e1d53291301b59207e5e9f2dcc442 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 11 Feb 2024 06:16:45 +0100 Subject: [PATCH 1/6] use Windows High Precision Timer on Windows builds lul may help with frametime and other stuff --- src/sdl/i_system.c | 87 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index c04434347..86d3c85b9 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3159,12 +3159,24 @@ static Uint64 timer_frequency; precise_t I_GetPreciseTime(void) { +#if defined(_WIN32) + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + return counter.QuadPart; +#else return SDL_GetPerformanceCounter(); +#endif } UINT64 I_GetPrecisePrecision(void) { +#if defined(_WIN32) + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + return frequency.QuadPart; +#else return SDL_GetPerformanceFrequency(); +#endif } static UINT32 frame_rate; @@ -3234,46 +3246,67 @@ void I_Sleep(UINT32 ms) void I_SleepDuration(precise_t duration) { #if defined(__linux__) || defined(__FreeBSD__) - UINT64 precision = I_GetPrecisePrecision(); - struct timespec ts = { - .tv_sec = duration / precision, - .tv_nsec = duration * 1000000000 / precision % 1000000000, - }; - int status; - do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); - while (status == EINTR); + UINT64 precision = I_GetPrecisePrecision(); + struct timespec ts = { + .tv_sec = duration / precision, + .tv_nsec = duration * 1000000000 / precision % 1000000000, + }; + int status; + do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); + while (status == EINTR); +#elif defined(_WIN32) + LARGE_INTEGER frequency; + LARGE_INTEGER start, current; + + QueryPerformanceCounter(&start); + + LONGLONG targetTicks = duration * frequency.QuadPart; + + do { + QueryPerformanceCounter(¤t); + LONGLONG elapsedTicks = current.QuadPart - start.QuadPart; + + LONGLONG remainingTicks = targetTicks - elapsedTicks; + + LONGLONG remainingTime = remainingTicks / frequency.QuadPart; + + if (remainingTime > 0.001) { + Sleep((DWORD)(remainingTime * 1000)); + } + + } while (current.QuadPart - start.QuadPart < targetTicks); #else - UINT64 precision = I_GetPrecisePrecision(); - INT32 sleepvalue = cv_sleep.value; - UINT64 delaygranularity; - precise_t cur; - precise_t dest; + UINT64 precision = I_GetPrecisePrecision(); + INT32 sleepvalue = cv_sleep.value; + UINT64 delaygranularity; + precise_t cur; + precise_t dest; - { - double gran = round(((double)(precision / 1000) * sleepvalue * MIN_SLEEP_DURATION_MS)); - delaygranularity = (UINT64)gran; - } + { + double gran = round(((double)(precision / 1000) * sleepvalue * MIN_SLEEP_DURATION_MS)); + delaygranularity = (UINT64)gran; + } - cur = I_GetPreciseTime(); - dest = cur + duration; + cur = I_GetPreciseTime(); + dest = cur + duration; // the reason this is not dest > cur is because the precise counter may wrap // two's complement arithmetic is our friend here, though! // e.g. cur 0xFFFFFFFFFFFFFFFE = -2, dest 0x0000000000000001 = 1 // 0x0000000000000001 - 0xFFFFFFFFFFFFFFFE = 3 - while ((INT64)(dest - cur) > 0) - { + while ((INT64)(dest - cur) > 0) + { // If our cv_sleep value exceeds the remaining sleep duration, use the // hard sleep function. - if (sleepvalue > 0 && (dest - cur) > delaygranularity) - { - I_Sleep(sleepvalue); - } + if (sleepvalue > 0 && (dest - cur) > delaygranularity) + { + I_Sleep(sleepvalue); + } // Otherwise, this is a spinloop. - cur = I_GetPreciseTime(); - } + cur = I_GetPreciseTime(); + } #endif } From 31964b34af2151a39423e6bad9957675ea920124 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 11 Feb 2024 06:29:28 +0100 Subject: [PATCH 2/6] oops --- src/sdl/i_system.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 86d3c85b9..058b07355 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3258,6 +3258,7 @@ void I_SleepDuration(precise_t duration) LARGE_INTEGER frequency; LARGE_INTEGER start, current; + QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); LONGLONG targetTicks = duration * frequency.QuadPart; From a747e9c7c0ccac483217f57a3cbc20980aea440d Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 11 Feb 2024 06:38:45 +0100 Subject: [PATCH 3/6] add it here too --- src/sdl/i_system.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 058b07355..f2d702d42 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3203,14 +3203,24 @@ static void I_InitFrameTime(const UINT64 now, const UINT32 cap) } double I_GetFrameTime(void) -{ +{ +#if defined(_WIN32) + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + const UINT64 nowValue = now.QuadPart; +#else const UINT64 now = SDL_GetPerformanceCounter(); +#endif const UINT32 cap = R_GetFramerateCap(); if (cap != frame_rate) { // Maybe do this in a OnChange function for cv_fpscap? +#if defined(_WIN32) + I_InitFrameTime(nowValue, cap); +#else I_InitFrameTime(now, cap); +#endif } if (frame_rate == 0) @@ -3220,10 +3230,17 @@ double I_GetFrameTime(void) } else { +#if defined(_WIN32) + elapsed_frames += (nowValue - frame_epoch) / frame_frequency; +#else elapsed_frames += (now - frame_epoch) / frame_frequency; +#endif } - - frame_epoch = now; // moving epoch +#if defined(_WIN32) + frame_epoch = nowValue; // moving epoch +#else + frame_epoch = now; // moving epoch +#endif return elapsed_frames; } @@ -3232,8 +3249,13 @@ double I_GetFrameTime(void) // void I_StartupTimer(void) { - timer_frequency = SDL_GetPerformanceFrequency(); - +#if defined(_WIN32) + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + timer_frequency = frequency.QuadPart; +#else + timer_frequency = SDL_GetPerformanceFrequency(); +#endif I_InitFrameTime(0, R_GetFramerateCap()); elapsed_frames = 0.0; } From 858b56d5aec553d1231eecf361d1069535bfdcb8 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 11 Feb 2024 13:42:12 +0100 Subject: [PATCH 4/6] Dont need this I_GetPrecise Precision already gets the high res windows time anyways --- src/sdl/i_system.c | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index f2d702d42..9b780ec31 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3276,28 +3276,6 @@ void I_SleepDuration(precise_t duration) int status; do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); while (status == EINTR); -#elif defined(_WIN32) - LARGE_INTEGER frequency; - LARGE_INTEGER start, current; - - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&start); - - LONGLONG targetTicks = duration * frequency.QuadPart; - - do { - QueryPerformanceCounter(¤t); - LONGLONG elapsedTicks = current.QuadPart - start.QuadPart; - - LONGLONG remainingTicks = targetTicks - elapsedTicks; - - LONGLONG remainingTime = remainingTicks / frequency.QuadPart; - - if (remainingTime > 0.001) { - Sleep((DWORD)(remainingTime * 1000)); - } - - } while (current.QuadPart - start.QuadPart < targetTicks); #else UINT64 precision = I_GetPrecisePrecision(); INT32 sleepvalue = cv_sleep.value; From 45ff86361b93574fa257fd17f78edcfc107e633f Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 11 Feb 2024 13:45:48 +0100 Subject: [PATCH 5/6] a --- src/sdl/i_system.c | 54 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 9b780ec31..1b5eda982 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3268,46 +3268,46 @@ void I_Sleep(UINT32 ms) void I_SleepDuration(precise_t duration) { #if defined(__linux__) || defined(__FreeBSD__) - UINT64 precision = I_GetPrecisePrecision(); - struct timespec ts = { - .tv_sec = duration / precision, - .tv_nsec = duration * 1000000000 / precision % 1000000000, - }; - int status; - do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); - while (status == EINTR); + UINT64 precision = I_GetPrecisePrecision(); + struct timespec ts = { + .tv_sec = duration / precision, + .tv_nsec = duration * 1000000000 / precision % 1000000000, + }; + int status; + do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); + while (status == EINTR); #else - UINT64 precision = I_GetPrecisePrecision(); - INT32 sleepvalue = cv_sleep.value; - UINT64 delaygranularity; - precise_t cur; - precise_t dest; + UINT64 precision = I_GetPrecisePrecision(); + INT32 sleepvalue = cv_sleep.value; + UINT64 delaygranularity; + precise_t cur; + precise_t dest; - { - double gran = round(((double)(precision / 1000) * sleepvalue * MIN_SLEEP_DURATION_MS)); - delaygranularity = (UINT64)gran; - } + { + double gran = round(((double)(precision / 1000) * sleepvalue * MIN_SLEEP_DURATION_MS)); + delaygranularity = (UINT64)gran; + } - cur = I_GetPreciseTime(); - dest = cur + duration; + cur = I_GetPreciseTime(); + dest = cur + duration; // the reason this is not dest > cur is because the precise counter may wrap // two's complement arithmetic is our friend here, though! // e.g. cur 0xFFFFFFFFFFFFFFFE = -2, dest 0x0000000000000001 = 1 // 0x0000000000000001 - 0xFFFFFFFFFFFFFFFE = 3 - while ((INT64)(dest - cur) > 0) - { + while ((INT64)(dest - cur) > 0) + { // If our cv_sleep value exceeds the remaining sleep duration, use the // hard sleep function. - if (sleepvalue > 0 && (dest - cur) > delaygranularity) - { - I_Sleep(sleepvalue); - } + if (sleepvalue > 0 && (dest - cur) > delaygranularity) + { + I_Sleep(sleepvalue); + } // Otherwise, this is a spinloop. - cur = I_GetPreciseTime(); - } + cur = I_GetPreciseTime(); + } #endif } From 2e9704067ce8e95df9301073825e10981f521cac Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 25 Feb 2024 04:08:25 +0100 Subject: [PATCH 6/6] use SleepEX on windows and set the min sleep duration to 16ms --- src/sdl/i_system.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index cdc0de212..48da364e2 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -43,12 +43,15 @@ typedef HANDLE (WINAPI *p_OpenFileMappingA) (DWORD, BOOL, LPCSTR); typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T); #endif - // A little more than the minimum sleep duration on Windows. // May be incorrect for other platforms, but we don't currently have a way to // query the scheduler granularity. SDL will do what's needed to make this as // low as possible though. +#if defined(_WIN32) +#define MIN_SLEEP_DURATION_MS 1.6 +#else #define MIN_SLEEP_DURATION_MS 2.1 +#endif #include #include @@ -3187,7 +3190,7 @@ precise_t I_GetPreciseTime(void) #if defined(_WIN32) LARGE_INTEGER counter; QueryPerformanceCounter(&counter); - return counter.QuadPart; + return counter.QuadPart; #else return SDL_GetPerformanceCounter(); #endif @@ -3275,11 +3278,11 @@ double I_GetFrameTime(void) void I_StartupTimer(void) { #if defined(_WIN32) - LARGE_INTEGER frequency; - QueryPerformanceFrequency(&frequency); - timer_frequency = frequency.QuadPart; + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + timer_frequency = frequency.QuadPart; #else - timer_frequency = SDL_GetPerformanceFrequency(); + timer_frequency = SDL_GetPerformanceFrequency(); #endif I_InitFrameTime(0, R_GetFramerateCap()); elapsed_frames = 0.0; @@ -3326,7 +3329,12 @@ void I_SleepDuration(precise_t duration) // hard sleep function. if (sleepvalue > 0 && (dest - cur) > delaygranularity) { +#if defined(_WIN32) + DWORD sleepDuration = (DWORD)min((INT64)(dest - cur), sleepvalue); + SleepEx(sleepDuration, TRUE); +#else I_Sleep(sleepvalue); +#endif } // Otherwise, this is a spinloop.