You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?).
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.
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:
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).
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).
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.
(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 😅
(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 😅
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.
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.
I've been thinking, one way to sidestep this entire issue would perhaps be by allowing the user to manage Buffers themselves? Something like:
structBuffer<D>{ ... }// No longer a lifetime hereimpl<D:HasDisplayHandle>Buffer<D>{fnnew(context:&Context<D>,width:u32,height:u32, pixel_format: ...) -> Self{ ...}}implDeref<Target = [u8]>forBuffer{ ... }implSurface<...> {/// Convenience method for creating a buffer with properties matching the surface.fnnew_buffer(&self) -> Buffer<D>{Buffer::new(...)}fnpresent(&mutself,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.
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).
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.
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?
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.
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).
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?
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?
Activity
i509VCB commentedon Jan 1, 2023
Wayland should be able to implement this with frame callbacks.
ids1024 commentedon Jan 1, 2023
This would probably be better implemented in winit as an event sent to the event loop (rust-windowing/winit#2412). We could make
set_bufferblock on the frame callback, which Mesa'seglSwapBuffersdoes wheneglSwapIntervalisn't zero. But that blocks indefinitely on an occluded window, and should at most be an option.qwertzui11 commentedon Oct 12, 2025
On
waylandI had success withwindow.pre_present_notify();. As documented inrequest_redraw().kryptan commentedon Nov 11, 2025
On Windows I found that you can call
DwmFlushinabout_to_waitto limit framerate:Not sure if it's the best way but it works.
[-]Vertical sync[/-][+]Vertical sync (VSync)[/+][-]Vertical sync (VSync)[/-][+]Vertical sync (VSync / frame-limiting)[/+]madsmtm commentedon Dec 17, 2025
Assume for the moment that Winit consistently implemented some sort of rate-limiting
window.request_redraw()inWindowEvent::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:
If we assume a double-buffered implementation, then the first
buffer_mutshould get the back buffer, the firstpresentshould commit it to the display server and swap the buffers, but what about the second and thirdbuffer_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
RedrawRequestedis 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:
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::setContentson 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 withCVDisplayLink/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).dhardy commentedon Dec 17, 2025
wgpu has
PresentModefor a reason: there isn't a one-size-fits-all solution. Using that terminology,Fifowould blocksurface.buffer_mut()until the next back buffer is availableImmediatewould swap buffers as soon aspresentis calledMailbox(IIUC) uses triple buffering but replaces the queued frame (if any) whenpresentis calledFrame-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'sPresentMode::FifoI find that drag-resizing a window with the mouse can lag; usingPresentMode::ImmediateorPresentMode::Mailboxfixes this but results in continual drawing above the target frame rate; the correct fix would be to estimate when to issueRedrawRequestedin order to hit the monitor's frame rate (which is a winit issue not softbuffer).madsmtm commentedon Dec 17, 2025
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,ImmediateandAdaptive(IIUC).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.
dhardy commentedon Dec 17, 2025
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.)
MarijnS95 commentedon Dec 17, 2025
(Without reading back the entire thread, just seeing the last WGPU mention): they have a bunch of PRs open that aim to change/extend
PresentModeagain 😅madsmtm commentedon Dec 17, 2025
Uhh, where?
MarijnS95 commentedon Dec 17, 2025
Sorry, not a PR but an issue hinting at yet more changes being needed: gfx-rs/wgpu#7417
madsmtm commentedon Dec 17, 2025
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.
dhardy commentedon Dec 17, 2025
Yes; I didn't think that
wgpuissue was relevant to this discussion. What is relevant towgpualso is frame-rate limiting (presumably guessing when to scheduleWindowEvent::RedrawRequested) but that's a winit issue; I think rust-windowing/winit#2412.madsmtm commentedon Jan 13, 2026
I've been thinking, one way to sidestep this entire issue would perhaps be by allowing the user to manage
Buffers themselves? Something like:This would avoid us having to make a choice here, and would remove the need for
Buffer::ageand probably alsoSurface::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 Bufferwhich changes its behaviour based onSurface::set_present_mode(PresentMode). Ideally, I'd want to implement that in a cross-platform manner to make maintaining it easier.dhardy commentedon Jan 13, 2026
Isn't this issue just about the behaviour of
Surface::buffer_mutandBuffer::present? I don't think passing off anything to the user is useful here. Better to keep the control insoftbufferbut add aPresentModeparameter toSurface(either at construction time or via a setter method).MarijnS95 commentedon Jan 13, 2026
@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
softbufferusers. 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.madsmtm commentedon Jan 13, 2026
Probably true.
I was thinking that it might be possible to use
WindowEvent::RedrawRequestedto avoidsoftbufferhaving 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 Waylandwl_surface.frame'swl_callback.doneandwl_buffer.release).Indeed.
Not sure I understand what that means, how would it look API-wise in
softbuffer?MarijnS95 commentedon Jan 15, 2026
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
winitin a platform-agnostic fashion.madsmtm commentedon Jan 30, 2026
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 withCATransaction::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::presentprobably shouldn't actually commit on macOS - but that's hard to reconcile with the other platforms? Would it make sense to make aPresentMode::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?madsmtm commentedon Jan 30, 2026
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?