Skip to content

Commit

Permalink
Remove mutex around command buffer completion handler
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Jul 9, 2023
1 parent dc225c1 commit d8d382c
Showing 1 changed file with 4 additions and 43 deletions.
47 changes: 4 additions & 43 deletions src/Veldrid/MTL/MTLGraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ private static readonly Dictionary<IntPtr, MTLGraphicsDevice> s_aotRegisteredBlo
private readonly bool[] _supportedSampleCounts;
private BackendInfoMetal _metalInfo;

private readonly object _submittedCommandsLock = new object();
private readonly Dictionary<MTLCommandBuffer, MTLCommandList> _submittedCLs = new Dictionary<MTLCommandBuffer, MTLCommandList>();
private MTLCommandBuffer _latestSubmittedCB;

private readonly object _resetEventsLock = new object();
private readonly List<ManualResetEvent[]> _resetEvents = new List<ManualResetEvent[]>();

private const string UnalignedBufferCopyPipelineMacOSName = "MTL_UnalignedBufferCopy_macOS";
private const string UnalignedBufferCopyPipelineiOSName = "MTL_UnalignedBufferCopy_iOS";
private readonly object _unalignedBufferCopyPipelineLock = new object();
Expand Down Expand Up @@ -201,22 +194,7 @@ public MTLGraphicsDevice(

public override GraphicsDeviceFeatures Features { get; }

private void OnCommandBufferCompleted(IntPtr block, MTLCommandBuffer cb)
{
lock (_submittedCommandsLock)
{
MTLCommandList cl = _submittedCLs[cb];
_submittedCLs.Remove(cb);
cl.OnCompleted(cb);

if (_latestSubmittedCB.NativePtr == cb.NativePtr)
{
_latestSubmittedCB = default(MTLCommandBuffer);
}
}

ObjectiveCRuntime.release(cb.NativePtr);
}
private void OnCommandBufferCompleted(IntPtr block, MTLCommandBuffer cb) => ObjectiveCRuntime.release(cb.NativePtr);

// Xamarin AOT requires native callbacks be static.
[MonoPInvokeCallback(typeof(MTLCommandBufferHandler))]
Expand All @@ -240,12 +218,7 @@ private protected override void SubmitCommandsCore(CommandList commandList, Fenc
mtlCL.CommandBuffer.encodeSignalEvent(mtlFence.SharedEvent.NativePtr, MTLFence.SIGNALED);

mtlCL.CommandBuffer.addCompletedHandler(_completionBlockLiteral);

lock (_submittedCommandsLock)
{
_submittedCLs.Add(mtlCL.CommandBuffer, mtlCL);
_latestSubmittedCB = mtlCL.Commit();
}
mtlCL.Commit();
}

private protected override void WaitForNextFrameReadyCore()
Expand Down Expand Up @@ -438,20 +411,8 @@ private protected override void UpdateTextureCore(

private protected override void WaitForIdleCore()
{
MTLCommandBuffer lastCB = default(MTLCommandBuffer);

lock (_submittedCommandsLock)
{
lastCB = _latestSubmittedCB;
ObjectiveCRuntime.retain(lastCB.NativePtr);
}

if (lastCB.NativePtr != IntPtr.Zero && lastCB.status != MTLCommandBufferStatus.Completed)
{
lastCB.waitUntilCompleted();
}

ObjectiveCRuntime.release(lastCB.NativePtr);
// todo: this may not work as it's not guaranteed that _latestSubmittedCB hasn't already been released yet.
throw new NotSupportedException("WaitForIdle is not supported on Metal currently. Use a fence and spin on the signal (because WaitForFence is also unsupported).");
}

protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource)
Expand Down

0 comments on commit d8d382c

Please sign in to comment.