use metal preferredFramesPerSecond (macOS 10.11+) and commandBuffer presentDrawable:afterMinimumDuration: (macOS 10.15.4+)

(programmed from Windows, maybe do not compile)
This commit is contained in:
Carlos Aragones 2021-04-28 13:21:08 +02:00
parent b4122c3d6a
commit dbd882ed22

View File

@ -4,6 +4,17 @@
#import <MetalKit/MetalKit.h>
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,8 +222,13 @@ NSString *g_shader_src = kShader(
[renderEncoder endEncoding];
// Schedule a present once the framebuffer is complete using the current drawable
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
[commandBuffer commit];