Skip to content

Commit

Permalink
Remove Metal staging buffer cache
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Jul 9, 2023
1 parent 0fd3c40 commit dc225c1
Showing 1 changed file with 2 additions and 64 deletions.
66 changes: 2 additions & 64 deletions src/Veldrid/MTL/MTLCommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ internal unsafe class MTLCommandList : CommandList
private bool[] _vertexBuffersActive;
private bool _disposed;

private readonly List<MTLBuffer> _availableStagingBuffers = new List<MTLBuffer>();
private readonly Dictionary<MTLCommandBuffer, List<MTLBuffer>> _submittedStagingBuffers = new Dictionary<MTLCommandBuffer, List<MTLBuffer>>();
private readonly object _submittedCommandsLock = new object();

public MTLCommandBuffer CommandBuffer => _cb;

public MTLCommandList(ref CommandListDescription description, MTLGraphicsDevice gd)
Expand Down Expand Up @@ -357,7 +353,7 @@ private protected override void UpdateBufferCore(DeviceBuffer buffer, uint buffe
|| (sizeInBytes % 4 != 0 && bufferOffsetInBytes != 0 && sizeInBytes != buffer.SizeInBytes);

MTLBuffer dstMTLBuffer = Util.AssertSubtype<DeviceBuffer, MTLBuffer>(buffer);
MTLBuffer staging = GetFreeStagingBuffer(sizeInBytes);
MTLBuffer staging = (MTLBuffer)_gd.ResourceFactory.CreateBuffer(new BufferDescription(sizeInBytes, BufferUsage.Staging));

_gd.UpdateBuffer(staging, 0, source, sizeInBytes);

Expand All @@ -376,47 +372,7 @@ private protected override void UpdateBufferCore(DeviceBuffer buffer, uint buffe
(UIntPtr)(sizeInBytes + sizeRoundFactor));
}

lock (_submittedCommandsLock)
{
if (!_submittedStagingBuffers.TryGetValue(_cb, out List<MTLBuffer> bufferList))
{
_submittedStagingBuffers[_cb] = bufferList = new List<MTLBuffer>();
}

bufferList.Add(staging);
}
}

private MTLBuffer GetFreeStagingBuffer(uint sizeInBytes)
{
lock (_submittedCommandsLock)
{
foreach (MTLBuffer buffer in _availableStagingBuffers)
{
if (buffer.SizeInBytes >= sizeInBytes)
{
_availableStagingBuffers.Remove(buffer);
return buffer;
}
}
}

DeviceBuffer staging = _gd.ResourceFactory.CreateBuffer(
new BufferDescription(sizeInBytes, BufferUsage.Staging));

return Util.AssertSubtype<DeviceBuffer, MTLBuffer>(staging);
}

public void OnCompleted(MTLCommandBuffer cb)
{
lock (_submittedCommandsLock)
{
if (_submittedStagingBuffers.TryGetValue(cb, out List<MTLBuffer> bufferList))
{
_availableStagingBuffers.AddRange(bufferList);
_submittedStagingBuffers.Remove(cb);
}
}
staging.Dispose();
}

protected override void CopyBufferCore(
Expand Down Expand Up @@ -1278,24 +1234,6 @@ public override void Dispose()
_disposed = true;
EnsureNoRenderPass();

lock (_submittedStagingBuffers)
{
foreach (MTLBuffer buffer in _availableStagingBuffers)
{
buffer.Dispose();
}

foreach (KeyValuePair<MTLCommandBuffer, List<MTLBuffer>> kvp in _submittedStagingBuffers)
{
foreach (MTLBuffer buffer in kvp.Value)
{
buffer.Dispose();
}
}

_submittedStagingBuffers.Clear();
}

if (_cb.NativePtr != IntPtr.Zero)
{
ObjectiveCRuntime.release(_cb.NativePtr);
Expand Down

0 comments on commit dc225c1

Please sign in to comment.