Skip to content

Commit

Permalink
[Metal] Fixed multi-recording of command buffers before submission.
Browse files Browse the repository at this point in the history
Don't call dispatch_semaphore_wait() if command buffer was not submitted prior.
This fixes Metal backend for CommandBufferEncode test.
  • Loading branch information
LukasBanana committed Jun 3, 2024
1 parent 3874cde commit e774985
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions sources/Renderer/Metal/Command/MTCommandQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
{
auto& directCommandBufferMT = LLGL_CAST(MTDirectCommandBuffer&, commandBufferMT);
if (!directCommandBufferMT.IsImmediateCmdBuffer())
{
SubmitCommandBuffer(directCommandBufferMT.GetNative());
directCommandBufferMT.MarkSubmitted();
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions sources/Renderer/Metal/Command/MTDirectCommandBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class MTDirectCommandBuffer final : public MTCommandBuffer
// Returns false.
bool IsMultiSubmitCmdBuffer() const override;

// Marks this command buffer as submitted to the command queue, allowing its semaphore to be signaled.
void MarkSubmitted();

// Returns the native MTLCommandBuffer object.
inline id<MTLCommandBuffer> GetNative() const
{
Expand Down Expand Up @@ -69,8 +72,9 @@ class MTDirectCommandBuffer final : public MTCommandBuffer

private:

id<MTLCommandBuffer> cmdBuffer_ = nil;
dispatch_semaphore_t cmdBufferSemaphore_ = nil;
id<MTLCommandBuffer> cmdBuffer_ = nil;
dispatch_semaphore_t cmdBufferSemaphore_ = nil;
bool cmdBufferSubmitted_ = false;

MTCommandQueue& cmdQueue_;
MTCommandContext context_;
Expand Down
12 changes: 11 additions & 1 deletion sources/Renderer/Metal/Command/MTDirectCommandBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
void MTDirectCommandBuffer::Begin()
{
/* Wait until next command buffer becomes available */
dispatch_semaphore_wait(cmdBufferSemaphore_, DISPATCH_TIME_FOREVER);
if (cmdBufferSubmitted_)
dispatch_semaphore_wait(cmdBufferSemaphore_, DISPATCH_TIME_FOREVER);

/* Allocate new command buffer from command queue */
cmdBuffer_ = [cmdQueue_.GetNative() commandBuffer];
Expand All @@ -68,6 +69,7 @@
addCompletedHandler:^(id<MTLCommandBuffer> cmdBuffer)
{
dispatch_semaphore_signal(blockSemaphore);
this->cmdBufferSubmitted_ = false;
}
];

Expand All @@ -83,7 +85,10 @@

/* Commit native buffer right after encoding for immediate command buffers */
if (IsImmediateCmdBuffer())
{
cmdQueue_.SubmitCommandBuffer(GetNative());
MarkSubmitted();
}

ResetRenderStates();
}
Expand Down Expand Up @@ -1055,6 +1060,11 @@ static void TrapIndirectPatchesNotSupported()
return false; // always false for MTDirectCommandBuffer
}

void MTDirectCommandBuffer::MarkSubmitted()
{
cmdBufferSubmitted_ = true;
}


/*
* ======= Private: =======
Expand Down

0 comments on commit e774985

Please sign in to comment.