Skip to content

Commit

Permalink
Merge pull request #2289 from SRSaunders/present-handler
Browse files Browse the repository at this point in the history
Add estimate of presentMargin in returned data from vkGetPastPresentationTimingGOOGLE()
  • Loading branch information
billhollings committed Jul 30, 2024
2 parents 5cf286a + f4bb1b4 commit 5b05413
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ typedef struct {
uint64_t desiredPresentTime; // VK_GOOGLE_display_timing desired presentation time in nanoseconds
uint32_t presentID; // VK_GOOGLE_display_timing presentID
VkPresentModeKHR presentMode; // VK_EXT_swapchain_maintenance1 present mode specialization
bool hasPresentTime; // Keep track of whether presentation included VK_GOOGLE_display_timing
} MVKImagePresentInfo;

/** Tracks a semaphore and fence for later signaling. */
Expand Down Expand Up @@ -505,6 +504,7 @@ class MVKPresentableSwapchainImage : public MVKSwapchainImage {
MVKSmallVector<MVKSwapchainSignaler, 1> _availabilitySignalers;
MVKSwapchainSignaler _preSignaler = {};
std::mutex _availabilityLock;
uint64_t _beginPresentTime = 0;
uint64_t _presentationStartTime = 0;
};

Expand Down
3 changes: 2 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ static void signalAndUntrack(const MVKSwapchainSignaler& signaler) {
void MVKPresentableSwapchainImage::beginPresentation(const MVKImagePresentInfo& presentInfo) {
retain();
_swapchain->beginPresentation(presentInfo);
_beginPresentTime = mvkGetRuntimeNanoseconds();
_presentationStartTime = getPerformanceTimestamp();
}

Expand All @@ -1652,7 +1653,7 @@ static void signalAndUntrack(const MVKSwapchainSignaler& signaler) {
// VkDevice, have been destroyed by the time of this callback, so do not reference them.
lock_guard<mutex> lock(_detachmentLock);
if (_device) { addPerformanceInterval(getPerformanceStats().queue.presentSwapchains, _presentationStartTime); }
if (_swapchain) { _swapchain->endPresentation(presentInfo, actualPresentTime); }
if (_swapchain) { _swapchain->endPresentation(presentInfo, _beginPresentTime, actualPresentTime); }
}

// Makes an image available for acquisition by the app.
Expand Down
3 changes: 1 addition & 2 deletions MoltenVK/MoltenVK/GPUObjects/MVKQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@
// Populate the array of swapchain images, testing each one for status
uint32_t scCnt = pPresentInfo->swapchainCount;
const VkPresentTimeGOOGLE* pPresentTimes = nullptr;
if (pPresentTimesInfo && pPresentTimesInfo->pTimes) {
if (pPresentTimesInfo) {
pPresentTimes = pPresentTimesInfo->pTimes;
MVKAssert(pPresentTimesInfo->swapchainCount == scCnt, "VkPresentTimesInfoGOOGLE swapchainCount must match VkPresentInfo swapchainCount.");
}
Expand Down Expand Up @@ -800,7 +800,6 @@
presentInfo.presentMode = pPresentModes ? pPresentModes[scIdx] : VK_PRESENT_MODE_MAX_ENUM_KHR;
presentInfo.fence = pFences ? (MVKFence*)pFences[scIdx] : nullptr;
if (pPresentTimes) {
presentInfo.hasPresentTime = true;
presentInfo.presentID = pPresentTimes[scIdx].presentID;
presentInfo.desiredPresentTime = pPresentTimes[scIdx].desiredPresentTime;
}
Expand Down
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MVKSwapchain : public MVKVulkanAPIDeviceObject {
void renderWatermark(id<MTLTexture> mtlTexture, id<MTLCommandBuffer> mtlCmdBuff);
void markFrameInterval();
void beginPresentation(const MVKImagePresentInfo& presentInfo);
void endPresentation(const MVKImagePresentInfo& presentInfo, uint64_t actualPresentTime = 0);
void endPresentation(const MVKImagePresentInfo& presentInfo, uint64_t beginPresentTime, uint64_t actualPresentTime = 0);
void forceUnpresentedImageCompletion();

MVKSurface* _surface = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
_unpresentedImageCount++;
}

void MVKSwapchain::endPresentation(const MVKImagePresentInfo& presentInfo, uint64_t actualPresentTime) {
void MVKSwapchain::endPresentation(const MVKImagePresentInfo& presentInfo, uint64_t beginPresentTime, uint64_t actualPresentTime) {
_unpresentedImageCount--;

std::lock_guard<std::mutex> lock(_presentHistoryLock);
Expand All @@ -266,9 +266,9 @@
_presentTimingHistory[_presentHistoryIndex].presentID = presentInfo.presentID;
_presentTimingHistory[_presentHistoryIndex].desiredPresentTime = presentInfo.desiredPresentTime;
_presentTimingHistory[_presentHistoryIndex].actualPresentTime = actualPresentTime;
// These details are not available in Metal
// These details are not available in Metal, but can estimate earliestPresentTime by using actualPresentTime instead
_presentTimingHistory[_presentHistoryIndex].earliestPresentTime = actualPresentTime;
_presentTimingHistory[_presentHistoryIndex].presentMargin = 0;
_presentTimingHistory[_presentHistoryIndex].presentMargin = actualPresentTime > beginPresentTime ? actualPresentTime - beginPresentTime : 0;
_presentHistoryIndex = (_presentHistoryIndex + 1) % kMaxPresentationHistory;
}

Expand Down

0 comments on commit 5b05413

Please sign in to comment.