Skip to content

Vertical sync (VSync / frame-limiting) #29

Open

Description

@LoganDark

Is it possible to perform vertical sync with this library? i.e. deliver a new frame to the screen every monitor refresh, but not any more frequently? It doesn't seem like the library does anything special about it, but I'm not 100% sure how this should work (is it the responsibility of the user to figure out the refresh rate and stuff?).

Activity

  1. i509VCB commented on Jan 1, 2023

    @i509VCB
    Contributor

    Wayland should be able to implement this with frame callbacks.

  2. ids1024 commented on Jan 1, 2023

    @ids1024
    Member

    This would probably be better implemented in winit as an event sent to the event loop (rust-windowing/winit#2412). We could make set_buffer block on the frame callback, which Mesa's eglSwapBuffers does when eglSwapInterval isn't zero. But that blocks indefinitely on an occluded window, and should at most be an option.

  3. qwertzui11 commented on Oct 12, 2025

    @qwertzui11

    On wayland I had success with window.pre_present_notify();. As documented in request_redraw().

  4. kryptan commented on Nov 11, 2025

    @kryptan

    On Windows I found that you can call DwmFlush in about_to_wait to limit framerate:

        fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
            #[cfg(windows)]
            unsafe {
                windows_sys::Win32::Graphics::Dwm::DwmFlush();
            }
        }

    Not sure if it's the best way but it works.

  5. changed the title [-]Vertical sync[/-] [+]Vertical sync (VSync)[/+] on Dec 4, 2025
  6. changed the title [-]Vertical sync (VSync)[/-] [+]Vertical sync (VSync / frame-limiting)[/+] on Dec 4, 2025
  7. madsmtm commented on Dec 17, 2025

    @madsmtm
    Member

    Assume for the moment that Winit consistently implemented some sort of rate-limiting window.request_redraw() in WindowEvent::RedrawRequested.

    What's the expected behaviour for Softbuffer when the user is not rendering at the display refresh rate?

    Asked differently, what's the expected behaviour of the following:

    WindowEvent::RedrawRequested => {
        surface.buffer_mut().unwrap().present().unwrap();
        surface.buffer_mut().unwrap().present().unwrap();
        surface.buffer_mut().unwrap().present().unwrap();
        window.request_redraw();
    }

    If we assume a double-buffered implementation, then the first buffer_mut should get the back buffer, the first present should commit it to the display server and swap the buffers, but what about the second and third buffer_mut?

    Should it block, waiting for the back buffer to stop being used by the display server? It sounds like the only option IIUC, but aren't we re-implementing frame-limiting then? Or perhaps we aren't, if RedrawRequested is properly scheduled to only be triggered once every frame, and the user properly only requests a single buffer each frame, then there should never be a point where the back buffer is in use by the display server, right? (Should we warn, then, if it is?)

    If we don't want blocking, then the only other option is triple-buffering (or creating a new buffer every frame), is it not? With one surface being actively read by the display server, another being commited to the display server (standing ready, but not yet used), and the third being rendered into (with the second and third being swapped between if rendering too fast).

    I guess a similar question is: What should happen if rendering continuously off the main thread? E.g. if you do:

    std::thread::spawn(move || {
        loop {
            surface.buffer_mut().unwrap().present().unwrap();
        }
    });

    What would we like to happen here? I suspect that blocking is desirable for this use-case. But it hints that we should perhaps expose buffer_mut_timeout(dur: Duration) too, to allow doing other stuff on that thread if frame callbacks happen to be far apart?


    I'm asking because using CALayer::setContents on macOS/iOS does not seem to expose any way to know when the layer is finished using the old surface, other than waiting for the next frame with CVDisplayLink/CADisplayLink. And thus, we need to make a conscious decision on how to handle this (do we use double buffering and wait on a condition variable set in the frame callback, or do we use triple buffering and signal the state with atomics).

  8. dhardy commented on Dec 17, 2025

    @dhardy

    wgpu has PresentMode for a reason: there isn't a one-size-fits-all solution. Using that terminology,

    • Fifo would block surface.buffer_mut() until the next back buffer is available
    • Immediate would swap buffers as soon as present is called
    • Mailbox (IIUC) uses triple buffering but replaces the queued frame (if any) when present is called

    Frame-limiting can also be done from another angle: delaying WindowEvent::RedrawRequested. This potentially allows for a more responsive app. For example with Kas GUI, using wgpu's PresentMode::Fifo I find that drag-resizing a window with the mouse can lag; using PresentMode::Immediate or PresentMode::Mailbox fixes this but results in continual drawing above the target frame rate; the correct fix would be to estimate when to issue RedrawRequested in order to hit the monitor's frame rate (which is a winit issue not softbuffer).

  9. madsmtm commented on Dec 17, 2025

    @madsmtm
    Member

    there isn't a one-size-fits-all solution

    Ah, I was fearing that, thanks a lot for the pointer! Do you know of any good literature on these concepts and/or further prior art? I see that the SDL provides Fifo, Immediate and Adaptive (IIUC).

    Immediate would swap buffers as soon as present is called

    I was under the impression that it would violate memory safety, since it'd be a race condition for the CPU to write while the GPU / display driver / whatever reads.

    But then I realized, it's not equivalent to two threads racing, it's something completely different, we're talking about buffers that are synchronized by the OS / hardware, and operating over at least a process boundary. And besides, the buffer should still be internally consistent for the CPU, since the GPU never writes to it, so the biggest issue is the GPU reading partial data, which is a well-known problem: tearing.

  10. dhardy commented on Dec 17, 2025

    @dhardy

    No, sorry. (I have read a couple of interesting articles in the past, but less on platform specific integration, and I am unable to find them now.)

  11. MarijnS95 commented on Dec 17, 2025

    @MarijnS95
    Member

    (Without reading back the entire thread, just seeing the last WGPU mention): they have a bunch of PRs open that aim to change/extend PresentMode again 😅

  12. madsmtm commented on Dec 17, 2025

    @madsmtm
    Member

    (Without reading back the entire thread, just seeing the last WGPU mention): they have a bunch of PRs open that aim to change/extend PresentMode again 😅

    Uhh, where?

  13. MarijnS95 commented on Dec 17, 2025

    @MarijnS95
    Member

    Sorry, not a PR but an issue hinting at yet more changes being needed: gfx-rs/wgpu#7417

  14. madsmtm commented on Dec 17, 2025

    @madsmtm
    Member

    Sorry, not a PR but an issue hinting at yet more changes being needed: gfx-rs/wgpu#7417

    Ah, but that one's only about the fallback modes, which I don't yet know if matters to us, depends on what we'll be able to support on each platform. But definitely something to keep in mind.

  15. dhardy commented on Dec 17, 2025

    @dhardy

    Yes; I didn't think that wgpu issue was relevant to this discussion. What is relevant to wgpu also is frame-rate limiting (presumably guessing when to schedule WindowEvent::RedrawRequested) but that's a winit issue; I think rust-windowing/winit#2412.

  16. madsmtm commented on Jan 13, 2026

    @madsmtm
    Member

    I've been thinking, one way to sidestep this entire issue would perhaps be by allowing the user to manage Buffers themselves? Something like:

    struct Buffer<D> { ... } // No longer a lifetime here
    
    impl<D: HasDisplayHandle> Buffer<D> {
        fn new(context: &Context<D>, width: u32, height: u32, pixel_format: ...) -> Self { ... }
    }
    
    impl Deref<Target = [u8]> for Buffer { ... }
    
    
    impl Surface<...> {
        /// Convenience method for creating a buffer with properties matching the surface.
        fn new_buffer(&self) -> Buffer<D> {
            Buffer::new(...)
        }
    
        fn present(&mut self, buffer: &Buffer) { ... } // Presentation moved to `Surface`
    }

    This would avoid us having to make a choice here, and would remove the need for Buffer::age and probably also Surface::resize.

    I'm pretty sure that such a design would be viable on both Wayland and macOS (unsure about the rest).


    The downside of course is that then, well, the user has to manage buffers themselves 😁.

    Maybe there's a path in between, where we also provide a method like Surface::next_buffer(&mut self) -> &mut Buffer which changes its behaviour based on Surface::set_present_mode(PresentMode). Ideally, I'd want to implement that in a cross-platform manner to make maintaining it easier.

  17. dhardy commented on Jan 13, 2026

    @dhardy

    Isn't this issue just about the behaviour of Surface::buffer_mut and Buffer::present? I don't think passing off anything to the user is useful here. Better to keep the control in softbuffer but add a PresentMode parameter to Surface (either at construction time or via a setter method).

  18. MarijnS95 commented on Jan 13, 2026

    @MarijnS95
    Member

    @madsmtm on Android that would be very useful via https://github.com/MarijnS95/softbuffer/compare/android-surfacecontrol.

    Not sure how much impact it has on Vsync though, besides platform-specific API differences between "unlocking" the "current" buffer versus "setting" a new buffer to a surface to be presented "at some point"? Together with additional APIs (at least on Android) that signal when the buffer was put on screen and when it is ready to be reused again.

    For example, the existing lock()+unlock() sequence might block (throttle) if no free buffers are available, or it might reuse a buffer that is never going to be presented (thus rendering too many useless images, which the reporter wanted to prevent).

    The latter behaviour will always be the case with manually creating and presenting buffers (unless the user "blocks" on when a presented buffer is ready to be reused again, which is typically part of such APIs).


    In the end it all depends on what kind of API or behaviour we want to expose to softbuffer users. I've personally found timeline-based APIs the most convenient to use: the presentation API tells the caller when the next image is going to be presented, and what their submission deadline is. Then the user can not only interpolate their animations correctly, but also work back when they need to start rendering that image (based on previous performance statistics) to further reduce input latency for example. And use the time they don't need (if any) to do something else or sleep.

  19. madsmtm commented on Jan 13, 2026

    @madsmtm
    Member

    Better to keep the control in softbuffer but add a PresentMode parameter to Surface

    Probably true.

    I was thinking that it might be possible to use WindowEvent::RedrawRequested to avoid softbuffer having to handle frame callbacks etc., but I think I'm conflating "draw new frame" and "buffer is no longer in use by the compositor" (on Wayland wl_surface.frame's wl_callback.done and wl_buffer.release).

    In the end it all depends on what kind of API or behaviour we want to expose to softbuffer users.

    Indeed.

    I've personally found timeline-based APIs the most convenient to use

    Not sure I understand what that means, how would it look API-wise in softbuffer?

  20. added this to the Softbuffer v0.5 milestone on Jan 14, 2026
  21. MarijnS95 commented on Jan 15, 2026

    @MarijnS95
    Member

    Not sure I understand what that means, how would it look API-wise in softbuffer?

    It'd need a callback that tells when the next frame(s) are going to be presented with their respective submit deadlines. Basically the present timing API we wanted to add to winit in a platform-agnostic fashion.

  22. madsmtm commented on Jan 30, 2026

    @madsmtm
    Member

    Something interesting is that on macOS, all surface changes (setting CALayer.contents) are sent to the compositor at the end of the current event loop iteration - so you don't actually need a separate "commit" step (and in fact doing the explicit commit with CATransaction::flush() commits the current state of all the application's surfaces to the compositor; I haven't found a way to do it piecewise).

    See #275 (comment) for a few investigation details.

    This has interesting implications, because it means that Buffer::present probably shouldn't actually commit on macOS - but that's hard to reconcile with the other platforms? Would it make sense to make a PresentMode::AllSurfacesInPresent?

    Also unsure how this interacts with Pro Motion (90Hz display rates), IIRC the event loop isn't polled at 90Hz (and thus we wouldn't run the transaction at 90Hz), but maybe we can force it to be using CADisplayLink?

  23. madsmtm commented on Jan 30, 2026

    @madsmtm
    Member

    Waaaaiiittt... That might be wrong, I think transactions actually allow you to choose exactly which parts of the layer tree updates that you want sent to the compositor (effectively a "batched" wl_surface.commit)... And the later (implicit) transaction in the run loop is just gonna do nothing if you've already committed your updates?

  24. removed this from the v0.5 milestone on Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions