diff --git a/src/macosx/OSXViewDelegate.m b/src/macosx/OSXViewDelegate.m index c06aff7..9a2f933 100644 --- a/src/macosx/OSXViewDelegate.m +++ b/src/macosx/OSXViewDelegate.m @@ -4,6 +4,17 @@ #import +extern double g_time_for_frame; +extern bool g_use_hardware_sync; +//-- +bool g_target_fps_changed = true; + +//------------------------------------- +void +set_target_fps_aux() { + g_target_fps_changed = true; +} + //------------------------------------- #define kShader(inc, src) @inc#src @@ -157,6 +168,12 @@ NSString *g_shader_src = kShader( //------------------------------------- - (void) drawInMTKView:(nonnull MTKView *) view { + if (g_target_fps_changed) { + view.preferredFramesPerSecond = (int) (1.0 / g_time_for_frame); + g_target_fps_changed = false; + g_use_hardware_sync = true; + } + // Wait to ensure only MaxBuffersInFlight number of frames are getting proccessed // by any stage in the Metal pipeline (App, Metal, Drivers, GPU, etc) dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); @@ -205,7 +222,12 @@ NSString *g_shader_src = kShader( [renderEncoder endEncoding]; // Schedule a present once the framebuffer is complete using the current drawable - [commandBuffer presentDrawable:view.currentDrawable]; + if ([commandBuffer respondsToSelector:@selector(presentDrawable:afterMinimumDuration:)]) { + [commandBuffer presentDrawable:view.currentDrawable afterMinimumDuration:g_time_for_frame]; + } + else { + [commandBuffer presentDrawable:view.currentDrawable]; + } } // Finalize rendering here & push the command buffer to the GPU