Skip to content

Commit

Permalink
[iOS] Follow up commit to fix use of macOS specific vsync control.
Browse files Browse the repository at this point in the history
Obj-C method 'setDisplaySyncEnabled' is only available in CAMetalLayer on macOS, not on iOS.
Use old method of default preferred frame rate for iOS swap-chain.
  • Loading branch information
LukasBanana committed Jun 12, 2024
1 parent 54b58d3 commit debaa09
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions sources/Renderer/Metal/MTSwapChain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,36 @@ - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
return (&renderPass_);
}

static NSInteger GetPrimaryDisplayRefreshRate()
{
constexpr NSInteger defaultRefreshRate = 60;
if (const Display* display = Display::GetPrimary())
return static_cast<NSInteger>(display->GetDisplayMode().refreshRate);
else
return defaultRefreshRate;
}

bool MTSwapChain::SetVsyncInterval(std::uint32_t vsyncInterval)
{
static const NSInteger defaultRefreshRate = 60;
if (vsyncInterval > 0)
{
/* Apply v-sync interval to display refresh rate */
if (auto display = Display::GetPrimary())
view_.preferredFramesPerSecond = static_cast<NSInteger>(display->GetDisplayMode().refreshRate / vsyncInterval);
else
view_.preferredFramesPerSecond = defaultRefreshRate / static_cast<NSInteger>(vsyncInterval);

#ifdef LLGL_OS_MACOS
/* Enable display sync in CAMetalLayer */
[(CAMetalLayer*)[view_ layer] setDisplaySyncEnabled:YES];
#endif

/* Apply v-sync interval to display refresh rate */
view_.preferredFramesPerSecond = GetPrimaryDisplayRefreshRate() / static_cast<NSInteger>(vsyncInterval);
}
else
{
#ifdef LLGL_OS_MACOS
/* Disable display sync in CAMetalLayer */
[(CAMetalLayer*)[view_ layer] setDisplaySyncEnabled:NO];
#else
/* Set preferred frame rate to default value */
view_.preferredFramesPerSecond = GetPrimaryDisplayRefreshRate();
#endif
}
return true;
}
Expand Down

0 comments on commit debaa09

Please sign in to comment.