From 9f0f659db0d3c341d89eb31d551a8257aec5bc6f Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 26 Mar 2019 11:10:17 +0100 Subject: [PATCH] - remove old vid_maxfps implementations as they were garbage anyway and the new one works on all the platforms --- src/d_net.cpp | 13 +-- src/posix/cocoa/i_video.mm | 10 --- src/posix/hardware.h | 33 ------- src/posix/sdl/hardware.cpp | 79 ----------------- src/posix/sdl/sdlglvideo.cpp | 8 -- src/rendering/gl/system/gl_framebuffer.cpp | 1 + .../vulkan/system/vk_framebuffer.cpp | 39 --------- src/rendering/vulkan/system/vk_framebuffer.h | 3 - src/v_framebuffer.cpp | 40 +++++++++ src/v_video.cpp | 5 -- src/v_video.h | 2 + src/win32/gl_sysfb.cpp | 2 - src/win32/hardware.cpp | 86 ------------------- src/win32/hardware.h | 4 - 14 files changed, 44 insertions(+), 281 deletions(-) diff --git a/src/d_net.cpp b/src/d_net.cpp index ec0a90deb4..5b9121a51d 100644 --- a/src/d_net.cpp +++ b/src/d_net.cpp @@ -145,18 +145,7 @@ static int oldentertics; extern bool advancedemo; -CUSTOM_CVAR (Bool, cl_capfps, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -{ - // Do not use the separate FPS limit timer if we are limiting FPS with this. - if (self) - { - I_SetFPSLimit(0); - } - else - { - I_SetFPSLimit(-1); - } -} +CVAR (Bool, cl_capfps, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR(Bool, net_ticbalance, false, CVAR_SERVERINFO | CVAR_NOSAVE) CUSTOM_CVAR(Int, net_extratic, 0, CVAR_SERVERINFO | CVAR_NOSAVE) diff --git a/src/posix/cocoa/i_video.mm b/src/posix/cocoa/i_video.mm index 89471824ee..076c3c8df6 100644 --- a/src/posix/cocoa/i_video.mm +++ b/src/posix/cocoa/i_video.mm @@ -727,16 +727,6 @@ void I_InitGraphics() // --------------------------------------------------------------------------- - -EXTERN_CVAR(Int, vid_maxfps); -EXTERN_CVAR(Bool, cl_capfps); - -// So Apple doesn't support POSIX timers and I can't find a good substitute short of -// having Objective-C Cocoa events or something like that. -void I_SetFPSLimit(int limit) -{ -} - CUSTOM_CVAR(Bool, vid_hidpi, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) { SystemBaseFrameBuffer::UseHiDPI(self); diff --git a/src/posix/hardware.h b/src/posix/hardware.h index 5853d34326..a7918935fb 100644 --- a/src/posix/hardware.h +++ b/src/posix/hardware.h @@ -37,37 +37,4 @@ #include "i_video.h" #include "v_video.h" -// Semaphores -#ifdef __APPLE__ -#include -#include -#include -typedef semaphore_t Semaphore; -#define SEMAPHORE_WAIT(sem) \ - while(semaphore_wait(sem) != KERN_SUCCESS){} -#define SEMAPHORE_SIGNAL(sem) \ - semaphore_signal(sem); -#define SEMAPHORE_INIT(sem, shared, value) \ - semaphore_create(mach_task_self(), &sem, shared, value); -#else -#include -typedef sem_t Semaphore; -#define SEMAPHORE_WAIT(sem) \ - do { \ - while(sem_wait(&sem) != 0); \ - int semValue; \ - sem_getvalue(&sem, &semValue); \ - if(semValue < 1) \ - break; \ - } while(true); -#define SEMAPHORE_SIGNAL(sem) \ - sem_post(&sem); -#define SEMAPHORE_INIT(sem, shared, value) \ - sem_init(&sem, shared, value); -#endif - -extern Semaphore FPSLimitSemaphore; -void I_SetFPSLimit(int limit); - - #endif // __HARDWARE_H__ diff --git a/src/posix/sdl/hardware.cpp b/src/posix/sdl/hardware.cpp index 4ea04f4e01..e631fa3132 100644 --- a/src/posix/sdl/hardware.cpp +++ b/src/posix/sdl/hardware.cpp @@ -85,82 +85,3 @@ void I_InitGraphics () atterm (I_ShutdownGraphics); } - -/** Remaining code is common to Win32 and Linux **/ - -// VIDEO WRAPPERS --------------------------------------------------------- - -//========================================================================== -// -// SetFPSLimit -// -// Initializes an event timer to fire at a rate of /sec. The video -// update will wait for this timer to trigger before updating. -// -// Pass 0 as the limit for unlimited. -// Pass a negative value for the limit to use the value of vid_maxfps. -// -//========================================================================== - -EXTERN_CVAR(Int, vid_maxfps); -EXTERN_CVAR(Bool, cl_capfps); - -#if !defined(__APPLE__) && !defined(__OpenBSD__) -Semaphore FPSLimitSemaphore; - -static void FPSLimitNotify(sigval val) -{ - SEMAPHORE_SIGNAL(FPSLimitSemaphore) -} - -void I_SetFPSLimit(int limit) -{ - static sigevent FPSLimitEvent; - static timer_t FPSLimitTimer; - static bool FPSLimitTimerEnabled = false; - static bool EventSetup = false; - if(!EventSetup) - { - EventSetup = true; - FPSLimitEvent.sigev_notify = SIGEV_THREAD; - FPSLimitEvent.sigev_signo = 0; - FPSLimitEvent.sigev_value.sival_int = 0; - FPSLimitEvent.sigev_notify_function = FPSLimitNotify; - FPSLimitEvent.sigev_notify_attributes = NULL; - - SEMAPHORE_INIT(FPSLimitSemaphore, 0, 0) - } - - if (limit < 0) - { - limit = vid_maxfps; - } - // Kill any leftover timer. - if (FPSLimitTimerEnabled) - { - timer_delete(FPSLimitTimer); - FPSLimitTimerEnabled = false; - } - if (limit == 0) - { // no limit - DPrintf(DMSG_NOTIFY, "FPS timer disabled\n"); - } - else - { - FPSLimitTimerEnabled = true; - if(timer_create(CLOCK_REALTIME, &FPSLimitEvent, &FPSLimitTimer) == -1) - Printf(DMSG_WARNING, "Failed to create FPS limiter event\n"); - itimerspec period = { {0, 0}, {0, 0} }; - period.it_value.tv_nsec = period.it_interval.tv_nsec = 1000000000 / limit; - if(timer_settime(FPSLimitTimer, 0, &period, NULL) == -1) - Printf(DMSG_WARNING, "Failed to set FPS limiter timer\n"); - DPrintf(DMSG_NOTIFY, "FPS timer set to %u ms\n", (unsigned int) period.it_interval.tv_nsec / 1000000); - } -} -#else -// So Apple doesn't support POSIX timers and I can't find a good substitute short of -// having Objective-C Cocoa events or something like that. -void I_SetFPSLimit(int limit) -{ -} -#endif diff --git a/src/posix/sdl/sdlglvideo.cpp b/src/posix/sdl/sdlglvideo.cpp index 0bfb9976ea..9389dfce82 100644 --- a/src/posix/sdl/sdlglvideo.cpp +++ b/src/posix/sdl/sdlglvideo.cpp @@ -71,7 +71,6 @@ extern IVideo *Video; EXTERN_CVAR (Int, vid_adapter) EXTERN_CVAR (Int, vid_displaybits) -EXTERN_CVAR (Int, vid_maxfps) EXTERN_CVAR (Int, vid_defwidth) EXTERN_CVAR (Int, vid_defheight) EXTERN_CVAR (Int, vid_backend) @@ -488,13 +487,6 @@ void SystemGLFrameBuffer::SetVSync( bool vsync ) void SystemGLFrameBuffer::SwapBuffers() { -#if !defined(__APPLE__) && !defined(__OpenBSD__) - if (vid_maxfps && !cl_capfps) - { - SEMAPHORE_WAIT(FPSLimitSemaphore) - } -#endif - SDL_GL_SwapWindow(Priv::window); } diff --git a/src/rendering/gl/system/gl_framebuffer.cpp b/src/rendering/gl/system/gl_framebuffer.cpp index 482338e63d..d5a00b72a6 100644 --- a/src/rendering/gl/system/gl_framebuffer.cpp +++ b/src/rendering/gl/system/gl_framebuffer.cpp @@ -250,6 +250,7 @@ void OpenGLFrameBuffer::Swap() Finish.Reset(); Finish.Clock(); if (swapbefore) glFinish(); + FPSLimit(); SwapBuffers(); if (!swapbefore) glFinish(); Finish.Unclock(); diff --git a/src/rendering/vulkan/system/vk_framebuffer.cpp b/src/rendering/vulkan/system/vk_framebuffer.cpp index 8dbb567142..30ba16f457 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.cpp +++ b/src/rendering/vulkan/system/vk_framebuffer.cpp @@ -59,14 +59,9 @@ #include "vulkan/system/vk_swapchain.h" #include "doomerrors.h" -#include -#include - void Draw2D(F2DDrawer *drawer, FRenderState &state); void DoWriteSavePic(FileWriter *file, ESSType ssformat, uint8_t *scr, int width, int height, sector_t *viewsector, bool upsidedown); -EXTERN_CVAR(Bool, vid_vsync) -EXTERN_CVAR(Int, vid_maxfps) EXTERN_CVAR(Bool, r_drawvoxels) EXTERN_CVAR(Int, gl_tonemap) EXTERN_CVAR(Int, screenblocks) @@ -223,40 +218,6 @@ void VulkanFrameBuffer::Update() Super::Update(); } -void VulkanFrameBuffer::FPSLimit() -{ - using namespace std::chrono; - using namespace std::this_thread; - - if (vid_maxfps <= 0 || vid_vsync) - return; - - uint64_t targetWakeTime = fpsLimitTime + 1'000'000 / vid_maxfps; - - while (true) - { - fpsLimitTime = duration_cast(steady_clock::now().time_since_epoch()).count(); - int64_t timeToWait = targetWakeTime - fpsLimitTime; - - if (timeToWait > 1'000'000 || timeToWait <= 0) - { - break; - } - - if (timeToWait <= 2'000'000) - { - // We are too close to the deadline. OS sleep is not precise enough to wake us before it elapses. - // Yield execution and check time again. - sleep_for(nanoseconds(0)); - } - else - { - // Sleep, but try to wake before deadline. - sleep_for(microseconds(timeToWait - 1'000'000)); - } - } -} - void VulkanFrameBuffer::DeleteFrameObjects() { FrameDeleteList.Images.clear(); diff --git a/src/rendering/vulkan/system/vk_framebuffer.h b/src/rendering/vulkan/system/vk_framebuffer.h index 0e0d26fcf8..6926063052 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.h +++ b/src/rendering/vulkan/system/vk_framebuffer.h @@ -107,7 +107,6 @@ private: void CopyScreenToBuffer(int w, int h, void *data); void UpdateShadowMap(); void DeleteFrameObjects(); - void FPSLimit(); std::unique_ptr mShaderManager; std::unique_ptr mSamplerManager; @@ -132,8 +131,6 @@ private: int lastSwapWidth = 0; int lastSwapHeight = 0; - - uint64_t fpsLimitTime = 0; }; inline VulkanFrameBuffer *GetVulkanFrameBuffer() { return static_cast(screen); } diff --git a/src/v_framebuffer.cpp b/src/v_framebuffer.cpp index ec53fc7e86..93545b2e9d 100644 --- a/src/v_framebuffer.cpp +++ b/src/v_framebuffer.cpp @@ -52,6 +52,9 @@ #include "hwrenderer/utility/hw_clock.h" #include "hwrenderer/data/flatvertices.h" +#include +#include + CVAR(Bool, gl_scale_viewport, true, CVAR_ARCHIVE); CVAR(Bool, vid_fps, false, 0) @@ -60,6 +63,9 @@ CVAR(Int, vid_showpalette, 0, 0) EXTERN_CVAR(Bool, ticker) EXTERN_CVAR(Float, vid_brightness) EXTERN_CVAR(Float, vid_contrast) +EXTERN_CVAR(Bool, vid_vsync) +EXTERN_CVAR(Int, vid_maxfps) +EXTERN_CVAR(Bool, cl_capfps) EXTERN_CVAR(Int, screenblocks) //========================================================================== @@ -415,3 +421,37 @@ void DFrameBuffer::ScaleCoordsFromWindow(int16_t &x, int16_t &y) x = int16_t((x - letterboxX) * Width / letterboxWidth); y = int16_t((y - letterboxY) * Height / letterboxHeight); } + +void DFrameBuffer::FPSLimit() +{ + using namespace std::chrono; + using namespace std::this_thread; + + if (vid_maxfps <= 0 || vid_vsync || cl_capfps) + return; + + uint64_t targetWakeTime = fpsLimitTime + 1'000'000 / vid_maxfps; + + while (true) + { + fpsLimitTime = duration_cast(steady_clock::now().time_since_epoch()).count(); + int64_t timeToWait = targetWakeTime - fpsLimitTime; + + if (timeToWait > 1'000'000 || timeToWait <= 0) + { + break; + } + + if (timeToWait <= 2'000) + { + // We are too close to the deadline. OS sleep is not precise enough to wake us before it elapses. + // Yield execution and check time again. + sleep_for(nanoseconds(0)); + } + else + { + // Sleep, but try to wake before deadline. + sleep_for(microseconds(timeToWait - 2'000)); + } + } +} diff --git a/src/v_video.cpp b/src/v_video.cpp index 6e043383be..91a6fb2b4e 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -70,7 +70,6 @@ #include "g_levellocals.h" #include "am_map.h" -EXTERN_CVAR(Bool, cl_capfps) EXTERN_CVAR(Int, menu_resolution_custom_width) EXTERN_CVAR(Int, menu_resolution_custom_height) @@ -90,10 +89,6 @@ CUSTOM_CVAR(Int, vid_maxfps, 200, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { vid_maxfps = 1000; } - else if (cl_capfps == 0) - { - I_SetFPSLimit(vid_maxfps); - } } CUSTOM_CVAR(Int, vid_rendermode, 4, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) diff --git a/src/v_video.h b/src/v_video.h index 38ed46366b..365f27d85c 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -540,6 +540,7 @@ public: int ScreenToWindowX(int x); int ScreenToWindowY(int y); + void FPSLimit(); // Retrieves a buffer containing image data for a screenshot. // Hint: Pitch can be negative for upside-down images, in which case buffer @@ -556,6 +557,7 @@ protected: void DrawRateStuff (); private: + uint64_t fpsLimitTime = 0; uint64_t LastMS = 0, LastSec = 0, FrameCount = 0, LastCount = 0, LastTic = 0; diff --git a/src/win32/gl_sysfb.cpp b/src/win32/gl_sysfb.cpp index 3ac1d7cf73..c8a6e3ea79 100644 --- a/src/win32/gl_sysfb.cpp +++ b/src/win32/gl_sysfb.cpp @@ -123,8 +123,6 @@ void SystemGLFrameBuffer::SetVSync (bool vsync) void SystemGLFrameBuffer::SwapBuffers() { - // Limiting the frame rate is as simple as waiting for the timer to signal this event. - I_FPSLimit(); ::SwapBuffers(static_cast(Video)->m_hDC); } diff --git a/src/win32/hardware.cpp b/src/win32/hardware.cpp index 014ae6e34f..f194910330 100644 --- a/src/win32/hardware.cpp +++ b/src/win32/hardware.cpp @@ -51,7 +51,6 @@ #include "i_system.h" #include "swrenderer/r_swrenderer.h" -EXTERN_CVAR(Int, vid_maxfps) EXTERN_CVAR(Int, vid_backend) extern HWND Window; @@ -154,88 +153,3 @@ void I_InitGraphics () atterm (I_ShutdownGraphics); } - - -static UINT FPSLimitTimer; -HANDLE FPSLimitEvent; - -//========================================================================== -// -// SetFPSLimit -// -// Initializes an event timer to fire at a rate of /sec. The video -// update will wait for this timer to trigger before updating. -// -// Pass 0 as the limit for unlimited. -// Pass a negative value for the limit to use the value of vid_maxfps. -// -//========================================================================== - -static void StopFPSLimit() -{ - I_SetFPSLimit(0); -} - -void I_SetFPSLimit(int limit) -{ - if (limit < 0) - { - limit = vid_maxfps; - } - // Kill any leftover timer. - if (FPSLimitTimer != 0) - { - timeKillEvent(FPSLimitTimer); - FPSLimitTimer = 0; - } - if (limit == 0) - { // no limit - if (FPSLimitEvent != NULL) - { - CloseHandle(FPSLimitEvent); - FPSLimitEvent = NULL; - } - DPrintf(DMSG_NOTIFY, "FPS timer disabled\n"); - } - else - { - if (FPSLimitEvent == NULL) - { - FPSLimitEvent = CreateEvent(NULL, FALSE, TRUE, NULL); - if (FPSLimitEvent == NULL) - { // Could not create event, so cannot use timer. - Printf(DMSG_WARNING, "Failed to create FPS limitter event\n"); - return; - } - } - atterm(StopFPSLimit); - // Set timer event as close as we can to limit/sec, in milliseconds. - UINT period = 1000 / limit; - FPSLimitTimer = timeSetEvent(period, 0, (LPTIMECALLBACK)FPSLimitEvent, 0, TIME_PERIODIC | TIME_CALLBACK_EVENT_SET); - if (FPSLimitTimer == 0) - { - CloseHandle(FPSLimitEvent); - FPSLimitEvent = NULL; - Printf("Failed to create FPS limiter timer\n"); - return; - } - DPrintf(DMSG_NOTIFY, "FPS timer set to %u ms\n", period); - } -} - -//========================================================================== -// -// StopFPSLimit -// -// Used for cleanup during application shutdown. -// -//========================================================================== - -void I_FPSLimit() -{ - if (FPSLimitEvent != NULL) - { - WaitForSingleObject(FPSLimitEvent, 1000); - } -} - diff --git a/src/win32/hardware.h b/src/win32/hardware.h index a4ab3bc00f..a7918935fb 100644 --- a/src/win32/hardware.h +++ b/src/win32/hardware.h @@ -37,8 +37,4 @@ #include "i_video.h" #include "v_video.h" -void I_SetFPSLimit(int limit); -void I_FPSLimit(); - - #endif // __HARDWARE_H__