The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Windows timer resolution change from SDL 1.2
eric.w


Joined: 12 Feb 2014
Posts: 38
Hi,I noticed SDL2 leaves the Windows timer resolution alone, so SDL_Delay(1) will sleep for ~15ms (on recent windows versions, afaik), whereas SDL 1.2 set the resolution to 1ms at startup (in SDL_StartTicks).


This is different from both OS X 10.10 and Ubuntu 14.04, where I'm seeing ~1ms resolution for SDL_Delay(1) with SDL2 from hg.


Just wondering if it's planned to leave SDL2/Windows with the default 15ms resolution?


I'm aware that timeBeginPeriod(1) is frowned upon due to the increased power usage (e.g. in this blog post 
https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/ ), but even that article points out that timeBeginPeriod(1) + Sleep(1) are useful for throttling games to 30 FPS. In my case, I help maintain some legacy code that uses SDL_Delay(1) to throttle a mainloop to 72fps (Quakespasm).


I think it might be nice to bring the default resolution up to 1ms on SDL2/Windows, matching the other platforms, and maybe have a hint to request not to do that for low-power games.
Thoughts? I could put together a patch for that if there's interest.


Cheers,
Eric


PS. I came across the 1ms->15ms change when investigating this buffer underrun issue with directsound ( https://bugzilla.libsdl.org/show_bug.cgi?id=2944 ).
Windows timer resolution change from SDL 1.2
Alex Szpakowski
Guest

I was going to point out that SDL 2 does use timeBeginPeriod to set the timer resolution to 1ms (and there’s a SDL hint to change that as well - SDL_HINT_TIMER_RESOLUTION) but I double-checked the source code and apparently timeBeginPeriod is only ever used if Windows’ QueryPerformanceFrequency function doesn’t return successfully... https://hg.libsdl.org/SDL/file/tip/src/timer/windows/SDL_systimer.c#l83

That codepath also means that SDL_HINT_TIMER_RESOLUTION doesn’t actually do anything at all unless QueryPerformanceCounter fails, which isn’t what I expected (since the timer resolution also affects SDL_Delay, not just SDL_GetTicks.)
Quote:
On Apr 14, 2015, at 8:29 PM, Eric Wasylishen wrote:
Hi,I noticed SDL2 leaves the Windows timer resolution alone, so SDL_Delay(1) will sleep for ~15ms (on recent windows versions, afaik), whereas SDL 1.2 set the resolution to 1ms at startup (in SDL_StartTicks).


Windows timer resolution change from SDL 1.2
Sik


Joined: 26 Nov 2011
Posts: 905
2015-04-14 20:40 GMT-03:00, Alex Szpakowski:
Quote:
I was going to point out that SDL 2 does use timeBeginPeriod to set the
timer resolution to 1ms (and there’s a SDL hint to change that as well -
SDL_HINT_TIMER_RESOLUTION) but I double-checked the source code and
apparently timeBeginPeriod is only ever used if Windows’
QueryPerformanceFrequency function doesn’t return successfully...
https://hg.libsdl.org/SDL/file/tip/src/timer/windows/SDL_systimer.c#l83
<https://hg.libsdl.org/SDL/file/tip/src/timer/windows/SDL_systimer.c#l83>

That codepath also means that SDL_HINT_TIMER_RESOLUTION doesn’t actually do
anything at all unless QueryPerformanceCounter fails, which isn’t what I
expected (since the timer resolution also affects SDL_Delay, not just
SDL_GetTicks.)

That'd mean that either the scheduler is way more accurate than I
thought or something else is calling it, because when I messed with it
SDL_Delay(1) would not cause my game to miss frames no matter what
(and we know how SDL_Delay can easily wreck timing, since the value
passed is the *minimum* wait).
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Windows timer resolution change from SDL 1.2
Alex Szpakowski
Guest

Ryan Gordon has pushed some commits to SDL today which fix this, I think.
Quote:
On Apr 14, 2015, at 8:40 PM, Alex Szpakowski wrote:
I was going to point out that SDL 2 does use timeBeginPeriod to set the timer resolution to 1ms (and there’s a SDL hint to change that as well - SDL_HINT_TIMER_RESOLUTION) but I double-checked the source code and apparently timeBeginPeriod is only ever used if Windows’ QueryPerformanceFrequency function doesn’t return successfully... https://hg.libsdl.org/SDL/file/tip/src/timer/windows/SDL_systimer.c#l83

That codepath also means that SDL_HINT_TIMER_RESOLUTION doesn’t actually do anything at all unless QueryPerformanceCounter fails, which isn’t what I expected (since the timer resolution also affects SDL_Delay, not just SDL_GetTicks.)
Quote:
On Apr 14, 2015, at 8:29 PM, Eric Wasylishen wrote:
Hi,I noticed SDL2 leaves the Windows timer resolution alone, so SDL_Delay(1) will sleep for ~15ms (on recent windows versions, afaik), whereas SDL 1.2 set the resolution to 1ms at startup (in SDL_StartTicks).





Windows timer resolution change from SDL 1.2
eric.w


Joined: 12 Feb 2014
Posts: 38
On Mon, Apr 20, 2015 at 1:17 PM, Alex Szpakowski wrote:
Quote:
Ryan Gordon has pushed some commits to SDL today which fix this, I think.





Thanks, the resolution on Windows now defaults to 1ms, and SDL_HINT_TIMER_RESOLUTION can be used to raise that, so that addresses my concerns :-)