Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the VK_KHR_timeline_semaphore extension. #1124

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
shaderSGTypesFeatures->shaderSubgroupExtendedTypes = _metalFeatures.subgroupSize != 0;
break;
}
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES: {
auto* timelineSem4Features = (VkPhysicalDeviceTimelineSemaphoreFeatures*)next;
timelineSem4Features->timelineSemaphore = true;
break;
}
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR: {
auto* uboLayoutFeatures = (VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR*)next;
uboLayoutFeatures->uniformBufferStandardLayout = true;
Expand Down Expand Up @@ -305,6 +310,11 @@
}
break;
#endif
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_PROPERTIES: {
auto* timelineSem4Props = (VkPhysicalDeviceTimelineSemaphoreProperties*)next;
timelineSem4Props->maxTimelineSemaphoreValueDifference = std::numeric_limits<uint64_t>::max();
break;
}
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT: {
auto* inlineUniformBlockProps = (VkPhysicalDeviceInlineUniformBlockPropertiesEXT*)next;
inlineUniformBlockProps->maxInlineUniformBlockSize = _metalFeatures.dynamicMTLBufferSize;
Expand Down Expand Up @@ -2668,12 +2678,30 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope

MVKSemaphore* MVKDevice::createSemaphore(const VkSemaphoreCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator) {
if (_useMTLFenceForSemaphores) {
return new MVKSemaphoreMTLFence(this, pCreateInfo);
} else if (_useMTLEventForSemaphores) {
return new MVKSemaphoreMTLEvent(this, pCreateInfo);
const VkSemaphoreTypeCreateInfo* pTypeCreateInfo = nullptr;
for (auto* next = (const VkBaseInStructure*)pCreateInfo->pNext; next; next = next->pNext) {
switch (next->sType) {
case VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO:
pTypeCreateInfo = (VkSemaphoreTypeCreateInfo*)next;
break;
default:
break;
}
}
if (pTypeCreateInfo && pTypeCreateInfo->semaphoreType == VK_SEMAPHORE_TYPE_TIMELINE) {
if (_pMetalFeatures->events) {
return new MVKTimelineSemaphoreMTLEvent(this, pCreateInfo, pTypeCreateInfo);
} else {
return new MVKTimelineSemaphoreEmulated(this, pCreateInfo, pTypeCreateInfo);
}
} else {
return new MVKSemaphoreEmulated(this, pCreateInfo);
if (_useMTLFenceForSemaphores) {
return new MVKSemaphoreMTLFence(this, pCreateInfo);
} else if (_useMTLEventForSemaphores) {
return new MVKSemaphoreMTLEvent(this, pCreateInfo);
} else {
return new MVKSemaphoreEmulated(this, pCreateInfo);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, by default, _useMTLFenceForSemaphores is enabled and _useMTLEventForSemaphores is disabled, which means regular VkSemaphores will prefer using MTLFence over MTLEvent.

Given that if a timeline semaphore is requested, we must use MTLSharedEvent, should we re-evaluate and possibly flip this? Is there any drive or appetite to prefer using MTLEvent over MTLFence for regular VkSemaphores?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the reason for preferring MTLFence for binary semaphores is that we observed problems when MTLEvents were used with presentation, where waits would time out and subsequently cause the system to revoke access to the device. The presentation code has changed significantly since then, but I'm still investigating whether or not it's safe to start using MTLEvent again.

(I keep forgetting that most people don't have memories as long as mine. How ironic.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now where was that issue again...?

Oh yeah, there were two, #602 and #625.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep forgetting that most people don't have memories as long as mine. How ironic. (🤦🏻‍♂️...for the irony joke).

Yeah...raw memory is definitely not my strong suit. I'd not survive long in medical school. 😉

Sorry...I didn't expect you to troll back into the previous change triggers...but was more looking for your thoughts on whether either our code has evolved...or Apple's MTLEvent code has...based on the sync testing you were doing.

The presentation code has changed significantly since then, but I'm still investigating whether or not it's safe to start using MTLEvent again.

Okay. Thanks for the review. If you do continue to investigate and it reveals that MTLEvents make sense, we can flip back.

}
}

Expand Down
4 changes: 2 additions & 2 deletions MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1263,13 +1263,13 @@

if ( !_availabilitySignalers.empty() ) {
MVKSemaphore* mvkSem = _availabilitySignalers.front().first;
if (mvkSem) { mvkSem->encodeSignal(mtlCmdBuff); }
if (mvkSem) { mvkSem->encodeSignal(mtlCmdBuff, 0); }
}
}

// Signal either or both of the semaphore and fence in the specified tracker pair.
void MVKPresentableSwapchainImage::signal(MVKSwapchainSignaler& signaler, id<MTLCommandBuffer> mtlCmdBuff) {
if (signaler.first) { signaler.first->encodeSignal(mtlCmdBuff); }
if (signaler.first) { signaler.first->encodeSignal(mtlCmdBuff, 0); }
if (signaler.second) { signaler.second->signal(); }
}

Expand Down
3 changes: 3 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@
ADD_DVC_EXT2_ENTRY_POINT(vkGetDeviceGroupSurfacePresentModesKHR, KHR_SWAPCHAIN, KHR_DEVICE_GROUP);
ADD_DVC_EXT2_ENTRY_POINT(vkGetPhysicalDevicePresentRectanglesKHR, KHR_SWAPCHAIN, KHR_DEVICE_GROUP);
ADD_DVC_EXT2_ENTRY_POINT(vkAcquireNextImage2KHR, KHR_SWAPCHAIN, KHR_DEVICE_GROUP);
ADD_DVC_EXT_ENTRY_POINT(vkGetSemaphoreCounterValueKHR, KHR_TIMELINE_SEMAPHORE);
ADD_DVC_EXT_ENTRY_POINT(vkSignalSemaphoreKHR, KHR_TIMELINE_SEMAPHORE);
ADD_DVC_EXT_ENTRY_POINT(vkWaitSemaphoresKHR, KHR_TIMELINE_SEMAPHORE);
ADD_DVC_EXT_ENTRY_POINT(vkDebugMarkerSetObjectTagEXT, EXT_DEBUG_MARKER);
ADD_DVC_EXT_ENTRY_POINT(vkDebugMarkerSetObjectNameEXT, EXT_DEBUG_MARKER);
ADD_DVC_EXT_ENTRY_POINT(vkCmdDebugMarkerBeginEXT, EXT_DEBUG_MARKER);
Expand Down
4 changes: 2 additions & 2 deletions MoltenVK/MoltenVK/GPUObjects/MVKQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class MVKQueueSubmission : public MVKConfigurableObject {
friend class MVKQueue;

MVKQueue* _queue;
MVKSmallVector<MVKSemaphore*> _waitSemaphores;
MVKSmallVector<std::pair<MVKSemaphore*, uint64_t>> _waitSemaphores;
bool _trackPerformance;
};

Expand All @@ -203,7 +203,7 @@ class MVKQueueCommandBufferSubmission : public MVKQueueSubmission {
void finish();
virtual void submitCommandBuffers() {}

MVKSmallVector<MVKSemaphore*> _signalSemaphores;
MVKSmallVector<std::pair<MVKSemaphore*, uint64_t>> _signalSemaphores;
MVKFence* _fence;
id<MTLCommandBuffer> _activeMTLCommandBuffer;
};
Expand Down
35 changes: 27 additions & 8 deletions MoltenVK/MoltenVK/GPUObjects/MVKQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@

_waitSemaphores.reserve(waitSemaphoreCount);
for (uint32_t i = 0; i < waitSemaphoreCount; i++) {
_waitSemaphores.push_back((MVKSemaphore*)pWaitSemaphores[i]);
_waitSemaphores.push_back(make_pair((MVKSemaphore*)pWaitSemaphores[i], (uint64_t)0));
}
}

Expand All @@ -241,13 +241,13 @@
_queue->_submissionCaptureScope->beginScope();

// If using encoded semaphore waiting, do so now.
for (auto* ws : _waitSemaphores) { ws->encodeWait(getActiveMTLCommandBuffer()); }
for (auto& ws : _waitSemaphores) { ws.first->encodeWait(getActiveMTLCommandBuffer(), ws.second); }

// Submit each command buffer.
submitCommandBuffers();

// If using encoded semaphore signaling, do so now.
for (auto* ss : _signalSemaphores) { ss->encodeSignal(getActiveMTLCommandBuffer()); }
for (auto& ss : _signalSemaphores) { ss.first->encodeSignal(getActiveMTLCommandBuffer(), ss.second); }

// Commit the last MTLCommandBuffer.
// Nothing after this because callback might destroy this instance before this function ends.
Expand Down Expand Up @@ -278,7 +278,7 @@
void MVKQueueCommandBufferSubmission::commitActiveMTLCommandBuffer(bool signalCompletion) {

// If using inline semaphore waiting, do so now.
for (auto& ws : _waitSemaphores) { ws->encodeWait(nil); }
for (auto& ws : _waitSemaphores) { ws.first->encodeWait(nil, ws.second); }

MVKDevice* mkvDev = _queue->_device;
uint64_t startTime = mkvDev->getPerformanceTimestamp();
Expand Down Expand Up @@ -308,7 +308,7 @@
_queue->_submissionCaptureScope->endScope();

// If using inline semaphore signaling, do so now.
for (auto& ss : _signalSemaphores) { ss->encodeSignal(nil); }
for (auto& ss : _signalSemaphores) { ss.first->encodeSignal(nil, ss.second); }

// If a fence exists, signal it.
if (_fence) { _fence->signal(); }
Expand All @@ -325,10 +325,29 @@

// pSubmit can be null if just tracking the fence alone
if (pSubmit) {
VkTimelineSemaphoreSubmitInfo* pTimelineSubmit = nullptr;
for (const auto* next = (const VkBaseInStructure*)pSubmit->pNext; next; next = next->pNext) {
switch (next->sType) {
case VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO:
pTimelineSubmit = (VkTimelineSemaphoreSubmitInfo*)next;
break;
default:
break;
}
}
if (pTimelineSubmit) {
// Presentation doesn't support timeline semaphores, so handle wait values here.
uint32_t wsCnt = pTimelineSubmit->waitSemaphoreValueCount;
for (uint32_t i = 0; i < wsCnt; i++) {
_waitSemaphores[i].second = pTimelineSubmit->pWaitSemaphoreValues[i];
}
}
uint32_t ssCnt = pSubmit->signalSemaphoreCount;
_signalSemaphores.reserve(ssCnt);
for (uint32_t i = 0; i < ssCnt; i++) {
_signalSemaphores.push_back((MVKSemaphore*)pSubmit->pSignalSemaphores[i]);
auto ss = make_pair((MVKSemaphore*)pSubmit->pSignalSemaphores[i], (uint64_t)0);
if (pTimelineSubmit) { ss.second = pTimelineSubmit->pSignalSemaphoreValues[i]; }
_signalSemaphores.push_back(ss);
}
}

Expand All @@ -348,12 +367,12 @@
// If the semaphores are not encodable, wait on them inline after presenting.
// The semaphores know what to do.
id<MTLCommandBuffer> mtlCmdBuff = getMTLCommandBuffer();
for (auto& ws : _waitSemaphores) { ws->encodeWait(mtlCmdBuff); }
for (auto& ws : _waitSemaphores) { ws.first->encodeWait(mtlCmdBuff, 0); }
for (int i = 0; i < _presentInfo.size(); i++ ) {
MVKPresentableSwapchainImage *img = _presentInfo[i].presentableImage;
img->presentCAMetalDrawable(mtlCmdBuff, _presentInfo[i]);
}
for (auto& ws : _waitSemaphores) { ws->encodeWait(nil); }
for (auto& ws : _waitSemaphores) { ws.first->encodeWait(nil, 0); }
[mtlCmdBuff commit];

// Let Xcode know the current frame is done, then start a new frame
Expand Down
Loading