8000 macos: honour swap_interval: Some(0) by benface · Pull Request #694 · not-fl3/miniquad · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ pub struct Platform {
/// There is no guarantee the FPS will match the specified `swap_interval`.
/// In other words, `swap_interval` is only a hint to the GPU driver and
/// not a reliable way to limit the game's FPS.
///
/// On macOS, `Some(0)` draws each frame as soon as the last one is done,
/// even while the window is hidden, and any other value keeps to the
/// display's refresh rate.
pub swap_interval: Option<i32>,

/// If `true`, the event loop will block until [`schedule_update`] is called.
Expand Down
14 changes: 11 additions & 3 deletions src/native/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct MacosDisplay {
gl_context: ObjcId,
fullscreen: bool,
occluded: bool,
/// Whether frames keep to the display's refresh rate, as they do
/// unless `swap_interval` is `Some(0)`.
vsync: bool,
// [NSCursor hide]/unhide calls should be balanced
// hide/hide/unhide will keep cursor hidden
// so need to keep internal cursor state to avoid problems from
Expand Down Expand Up @@ -992,7 +995,7 @@ fn get_window_payload(this: &Object) -> &mut MacosDisplay {
}
}

unsafe fn create_metal_view(_: &mut MacosDisplay, sample_count: i32, _: bool) -> ObjcId {
unsafe fn create_metal_view(display: &mut MacosDisplay, sample_count: i32, _: bool) -> ObjcId {
let mtl_device_obj = MTLCreateSystemDefaultDevice();
let view_class = define_metal_view_class();
let view: ObjcId = msg_send![view_class, alloc];
Expand All @@ -1006,6 +1009,10 @@ unsafe fn create_metal_view(_: &mut MacosDisplay, sample_count: i32, _: bool) ->
];
let () = msg_send![view, setSampleCount: sample_count];
let () = msg_send![view, setPaused: true];
if !display.vsync {
let layer: ObjcId = msg_send![view, layer];
let () = msg_send![layer, setDisplaySyncEnabled: NO];
}

view
}
Expand Down Expand Up @@ -1280,7 +1287,7 @@ unsafe fn perform_redraw(
}
unsafe {
// reduce CPU usage hen window is in background
if display.occluded {
if display.vsync && display.occluded {
let now = Instant::now();
let framerate = 60.0;
let period = (1.0 / framerate * 1000.) as u64;
Expand Down Expand Up @@ -1326,6 +1333,7 @@ where
gl_context: std::ptr::null_mut(),
fullscreen: false,
occluded: false,
vsync: conf.platform.swap_interval != Some(0),
cursor_shown: true,
current_cursor: CursorIcon::Default,
cursor_grabbed: false,
Expand Down Expand Up @@ -1461,7 +1469,7 @@ where
// Basically reimplementing msg_send![ns_app, run] here
let distant_future: ObjcId = msg_send![class!(NSDate), distantFuture];
let distant_past: ObjcId = msg_send![class!(NSDate), distantPast];
let frame_pacer = if conf.platform.blocking_event_loop {
let frame_pacer = if conf.platform.blocking_event_loop || !display.vsync {
None
} else {
FramePacer::new()
Expand Down
Loading
0