WM: improve timer precision for more precise playback #111603

Manually merged
Campbell Barton merged 3 commits from ideasman42/blender:pr-high-precision-timers into main 2023-08-30 05:05:41 +02:00

When a timer needs to execute during the idle time (currently 5ms),
use microsecond precision to ensure the timer isn't delayed by the idle
time.

This improves the precision of animation playback on systems that support it
(WIN32, see note below).

In my tests FPS playback varied:

  • 59.94 FPS would jitter between 58.13 & 63.11 FPS.
  • 23.98 FPS would jitter between 23.68 & 24.31 FPS.

Using higher precision timers mostly resolves this issue although there
is still some jitter although it's now limited to ~0.01 FPS.

These values were measured with the FPS samples set to show real times
between frames (without them being smoothed out).

This is a continuation of a fix for #111579 which avoided the worst of the jittering issues at higher frame rates.

It appears WIN32 doesn't support sleeping shorter than 1 millisecond via std::this_thread::sleep_for, making this change have no benefit on WIN32, from tests it also doesn't have any down sides, so avoid platform specific logic. The WIN32 limitation is noted in code-comments. If a method of higher precision method is available this can be investigated.


To reproduce this error, set the FPS samples preference to 1, use the default scene and play animation either at 59.94 or 23.98 FPS.
Using a fractional FPS ensures a fractional FPS is displayed in the view-port.

NOTES:

  • The FPS error is roughly between 2.6 - 8.6% with the higher errors occurring at higher FPS (59.94) in this case, where they're less likely to be noticeable. So we could choose to leave the current logic acceptable as-is, although it would be good to have concrete down-side as reason for rejecting improved timer precision.
  • This PR introduces a high resolution timer into the main loop, although for now it should rarely be used except when timers are about to run (within 5ms).
  • WIN32's native API's for high resolution timing seem quite limited (see this Q&A + linked topics) and I'm not sure how std::this_thread::sleep_for is implemented internally on Windows. It may be that the C++ implementation has down-sides similar to the kinds of workarounds I found online (when looking into implementing a microsecond version of PIL_sleep_ms on WIN32).
When a timer needs to execute during the idle time (currently 5ms), use microsecond precision to ensure the timer isn't delayed by the idle time. This improves the precision of animation playback on systems that support it (WIN32, see note below). In my tests FPS playback varied: - 59.94 FPS would jitter between 58.13 & 63.11 FPS. - 23.98 FPS would jitter between 23.68 & 24.31 FPS. Using higher precision timers mostly resolves this issue although there is still some jitter although it's now limited to ~0.01 FPS. These values were measured with the FPS samples set to show real times between frames (without them being smoothed out). This is a continuation of a fix for #111579 which avoided the worst of the jittering issues at higher frame rates. It appears WIN32 doesn't support sleeping shorter than 1 millisecond via `std::this_thread::sleep_for`, making this change have no benefit on WIN32, from tests it also doesn't have any down sides, so avoid platform specific logic. The WIN32 limitation is noted in code-comments. If a method of higher precision method is available this can be investigated. ---- To reproduce this error, set the FPS samples preference to 1, use the default scene and play animation either at 59.94 or 23.98 FPS. Using a fractional FPS ensures a fractional FPS is displayed in the view-port. NOTES: - The FPS error is roughly between 2.6 - 8.6% with the higher errors occurring at higher FPS (59.94) in this case, where they're less likely to be noticeable. So we could choose to leave the current logic acceptable as-is, although it would be good to have concrete down-side as reason for rejecting improved timer precision. - This PR introduces a high resolution timer into the main loop, although for now it should rarely be used except when timers are about to run (within 5ms). - WIN32's native API's for high resolution timing seem quite limited (see this [Q&A](https://stackoverflow.com/questions/9116618) + linked topics) and I'm not sure how `std::this_thread::sleep_for` is implemented internally on Windows. It may be that the C++ implementation has down-sides similar to the kinds of workarounds I found online (when looking into implementing a microsecond version of `PIL_sleep_ms` on WIN32).
Campbell Barton requested review from Brecht Van Lommel 2023-08-28 10:14:28 +02:00
Campbell Barton requested review from Bastien Montagne 2023-08-28 10:14:38 +02:00
Campbell Barton requested review from Jacques Lucke 2023-08-28 10:14:53 +02:00
Author
Owner

@blender-bot build

@blender-bot build
Bastien Montagne reviewed 2023-08-28 10:49:00 +02:00
Bastien Montagne left a comment
Owner

From code PoV, LGTM.

Have you tried it on a Windows machine? According to this thread e.g., it seems that Windows can have fairly longer times than requested with this call, wondering if it does stabilize the FPS on this OS?

From code PoV, LGTM. Have you tried it on a Windows machine? According to [this thread](https://stackoverflow.com/questions/54493041/unpredictable-behaviour-of-stdsleep-for-on-windows-10) e.g., it seems that Windows can have fairly longer times than requested with this call, wondering if it does stabilize the FPS on this OS?
Author
Owner

@blender-bot package windows

@blender-bot package windows
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111603) when ready.
Author
Owner

@mont29 no, I haven't tested on windows (set the build-bot to create a package).

It seems that this may not work well on windows - or may have higher CPU use.

We could either:

  • Disable high-resolution sleep on WIN32 if there are drawbacks we can't avoid.
  • Disable high-resolution sleep by default, but allow for certain timers (flag timers for e.g) and only use microsecond sleep for these (which could be used by animation playback). Although as this only impacts the moment before a timer runs, it might not be worth the increased complexity from having some timers behave differently from others.
@mont29 no, I haven't tested on windows (set the build-bot to create a package). It seems that this may not work well on windows - or may have higher CPU use. We could either: - Disable high-resolution sleep on WIN32 if there are drawbacks we can't avoid. - Disable high-resolution sleep by default, but allow for certain timers (flag timers for e.g) and only use microsecond sleep for these (which could be used by animation playback). Although as this only impacts the moment before a timer runs, it might not be worth the increased complexity from having some timers behave differently from others.
Author
Owner

Testing on windows std::this_thread::sleep_for doesn't have any noticable advantage over PIL_sleep_ms comparing before/after side-by-side the frame-jitter - the problem was exactly the same.

For reference this was a low end laptop with a built in intel GPU, tested with small windows with a blank view-port:

  • Windows 10 22H2
  • Intel i5-7300, 2.60 GHZ.
Testing on windows `std::this_thread::sleep_for` doesn't have any noticable advantage over `PIL_sleep_ms` comparing before/after side-by-side the frame-jitter - the problem was exactly the same. For reference this was a low end laptop with a built in intel GPU, tested with small windows with a blank view-port: - Windows 10 22H2 - Intel i5-7300, 2.60 GHZ.
Author
Owner

Made a test commit to check on spinning until sub 1ms timers are reached, I'm not especially keen on this but if the overall CPU usage isn't much higher it might be OK.

Update: this doesn't make any noticeable improvement either (nor does in result in noticeably higher CPU usage).


@blender-bot package windows

Made a test commit to check on spinning until sub 1ms timers are reached, I'm not especially keen on this but if the overall CPU usage isn't much higher it might be OK. Update: this doesn't make any noticeable improvement either (nor does in result in noticeably higher CPU usage). ---- @blender-bot package windows
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR111603) when ready.
Campbell Barton force-pushed pr-high-precision-timers from f0f9364f74 to 74d0bb2e81 2023-08-29 02:41:50 +02:00 Compare
Campbell Barton force-pushed pr-high-precision-timers from 74d0bb2e81 to 6d2ea5435a 2023-08-29 02:50:40 +02:00 Compare
Campbell Barton force-pushed pr-high-precision-timers from 6d2ea5435a to 04dd7606db 2023-08-29 03:02:06 +02:00 Compare
Author
Owner

Updated the PR to remove WIN32 ifdef's & the PR message to note that this change doesn't show noticeable improvements on WIN32.

While I'm less keen on this change given it doesn't solve timer precision on WIN32, it's also not great to limit macOS & Linux because of a limitation on WIN32.

We could also define this problem as a technical limitation that doesn't actually impact users, if the FPS playback is good enough from what the user can perceive, the fact the FPS display jitters, could be overlooked.

If others feel positively about this change I think it's OK to include but we could leave it as a known limitation too.

Updated the PR to remove WIN32 ifdef's & the PR message to note that this change doesn't show noticeable improvements on WIN32. While I'm less keen on this change given it doesn't solve timer precision on WIN32, it's also not great to limit macOS & Linux because of a limitation on WIN32. We could also define this problem as a technical limitation that doesn't actually impact users, if the FPS playback is good enough from what the user can perceive, the fact the FPS display jitters, could be overlooked. If others feel positively about this change I think it's OK to include but we could leave it as a known limitation too.
Bastien Montagne approved these changes 2023-08-29 10:39:35 +02:00
Bastien Montagne left a comment
Owner

I think it's fine to improve Linux/OSX, even if Windows cannot benefit from this, the changes are not that big, and in the future maybe the windows std implementation will be better?

Could be worth keeping a task somewhere though about improving our timer precision on Windows.

I think it's fine to improve Linux/OSX, even if Windows cannot benefit from this, the changes are not that big, and in the future maybe the windows `std` implementation will be better? Could be worth keeping a task somewhere though about improving our timer precision on Windows.
Campbell Barton manually merged commit 745f78f9f0 into main 2023-08-30 05:05:41 +02:00
Campbell Barton deleted branch pr-high-precision-timers 2023-08-30 05:05:51 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#111603
No description provided.