Skip to content

Commit

Permalink
MVKQueue: Make sure waitIdle waits for all commands to be encoded bef…
Browse files Browse the repository at this point in the history
…ore enqueuing its own waitUntilCompleted.
  • Loading branch information
js6i committed Jul 31, 2024
1 parent 5b05413 commit 6f253fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "MVKSync.h"
#include "MVKSmallVector.h"
#include <mutex>
#include <condition_variable>

#import <Metal/Metal.h>

Expand Down Expand Up @@ -147,6 +148,9 @@ class MVKQueue : public MVKDispatchableVulkanAPIObject, public MVKDeviceTracking
MVKQueueFamily* _queueFamily;
std::string _name;
dispatch_queue_t _execQueue;
std::mutex _execQueueMutex;
std::condition_variable _execQueueConditionVariable;
uint32_t _execQueueJobCount = 0;
id<MTLCommandQueue> _mtlQueue = nil;
NSString* _mtlCmdBuffLabelBeginCommandBuffer = nil;
NSString* _mtlCmdBuffLabelQueueSubmit = nil;
Expand Down
17 changes: 16 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@
// The submissions will ensure a misconfiguration will be safe to execute.
VkResult rslt = qSubmit->getConfigurationResult();
if (_execQueue) {
dispatch_async(_execQueue, ^{ execute(qSubmit); } );
std::unique_lock lock(_execQueueMutex);
_execQueueJobCount++;

dispatch_async(_execQueue, ^{
execute(qSubmit);

std::unique_lock execLock(_execQueueMutex);
_execQueueJobCount--;
_execQueueConditionVariable.notify_all();
} );
} else {
rslt = execute(qSubmit);
}
Expand Down Expand Up @@ -145,6 +154,12 @@
VkResult rslt = _device->getConfigurationResult();
if (rslt != VK_SUCCESS) { return rslt; }

{
std::unique_lock lock(_execQueueMutex);
while (_execQueueJobCount)
_execQueueConditionVariable.wait(lock);
}

@autoreleasepool {
auto* mtlCmdBuff = getMTLCommandBuffer(cmdUse);
[mtlCmdBuff commit];
Expand Down

0 comments on commit 6f253fc

Please sign in to comment.