Only pace presentation if display sync is enabled

This commit is contained in:
Cameron Gutman 2024-02-25 18:45:41 -06:00
parent 76d0eb6b63
commit 654e386263

View File

@ -199,15 +199,17 @@ public:
return; return;
} }
// Pace ourselves by waiting if too many frames are pending presentation if (m_MetalLayer.displaySyncEnabled) {
SDL_LockMutex(m_PresentationMutex); // Pace ourselves by waiting if too many frames are pending presentation
if (m_PendingPresentationCount > 2) { SDL_LockMutex(m_PresentationMutex);
if (SDL_CondWaitTimeout(m_PresentationCond, m_PresentationMutex, 100) == SDL_MUTEX_TIMEDOUT) { if (m_PendingPresentationCount > 2) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, if (SDL_CondWaitTimeout(m_PresentationCond, m_PresentationMutex, 100) == SDL_MUTEX_TIMEDOUT) {
"Presentation wait timed out after 100 ms"); SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Presentation wait timed out after 100 ms");
}
} }
SDL_UnlockMutex(m_PresentationMutex);
} }
SDL_UnlockMutex(m_PresentationMutex);
} }
}} }}
@ -492,16 +494,18 @@ public:
[renderEncoder endEncoding]; [renderEncoder endEncoding];
// Queue a completion callback on the drawable to pace our rendering if (m_MetalLayer.displaySyncEnabled) {
SDL_LockMutex(m_PresentationMutex); // Queue a completion callback on the drawable to pace our rendering
m_PendingPresentationCount++;
SDL_UnlockMutex(m_PresentationMutex);
[m_NextDrawable addPresentedHandler:^(id<MTLDrawable>) {
SDL_LockMutex(m_PresentationMutex); SDL_LockMutex(m_PresentationMutex);
m_PendingPresentationCount--; m_PendingPresentationCount++;
SDL_CondSignal(m_PresentationCond);
SDL_UnlockMutex(m_PresentationMutex); SDL_UnlockMutex(m_PresentationMutex);
}]; [m_NextDrawable addPresentedHandler:^(id<MTLDrawable>) {
SDL_LockMutex(m_PresentationMutex);
m_PendingPresentationCount--;
SDL_CondSignal(m_PresentationCond);
SDL_UnlockMutex(m_PresentationMutex);
}];
}
// Flip to the newly rendered buffer // Flip to the newly rendered buffer
[commandBuffer presentDrawable:m_NextDrawable]; [commandBuffer presentDrawable:m_NextDrawable];