Skip to content

native/macos: Add frame pacing for smooth motion#623

Open
Pewnack wants to merge 4 commits intonot-fl3:masterfrom
Pewnack:macos_framepacer
Open

native/macos: Add frame pacing for smooth motion#623
Pewnack wants to merge 4 commits intonot-fl3:masterfrom
Pewnack:macos_framepacer

Conversation

@Pewnack
Copy link
Copy Markdown
Contributor

@Pewnack Pewnack commented Apr 18, 2026

Fixes #559

On macOS, if you are using OpenGL, motion stutters. Motion is not smooth/fluent. You can see it immediately when you run

cargo run --example quad

The problem is that there isn't any real frame pacing in the current main loop. The main loop pushes out a new frame each iteration with flushBuffer and relies on VSYNC (swap_interval = 1) for pacing. But that's not enough on macOS. flushBuffer gives us the new frame, but it's up to the display server (Quartz Compositor) to decide when to use it.

This change adds a frame pacer using macOS's CVDisplayLink callback. Basically what this change does is this:

  • Adds a flag frame_ready that tells us when to start rendering the next frame.
  • CVDisplayLink callback sets the flag to true
  • The main loop wait for frame_ready to become true and continues with the next iteration.

There might be other ways to do it. But I wanted to keep the main loop intact as much as possible.

With this frame pacer motion is now super smooth 😄

Before version v0.4.6 it was still reasonably smooth. That's because rendering was done differently. In v.0.4.5 each iteration of the main loop called setNeedsDisaply:YES on the view to trigger a redraw. This marks the view as 'dirty' signaling the display server to update it. The redraw performed a flushBuffer with VSYNC.

Pewnack added 4 commits April 14, 2026 18:46
… wait twice. Added timeouts to the frame pacer to prevent waiting forever for a next frame.
…he timeout expired. Waiting for the timeout already gave time to other process/threads/
@Pewnack Pewnack changed the title Macos framepacer native/macos: Add frame pacing for smooth motion Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unstable framerate on macOS

1 participant