Cleanup Pipeline

Housekeeping

More housekeeping
This commit is contained in:
Isaac Marovitz 2024-06-21 00:21:06 +01:00 committed by Isaac Marovitz
parent 30b50a99e4
commit b1928461bb
24 changed files with 77 additions and 92 deletions

View file

@ -5,7 +5,7 @@ using System.Threading;
namespace Ryujinx.Graphics.Metal
{
public interface IAuto
interface IAuto
{
bool HasCommandBufferDependency(CommandBufferScoped cbs);
@ -14,13 +14,13 @@ namespace Ryujinx.Graphics.Metal
void DecrementReferenceCount();
}
public interface IAutoPrivate : IAuto
interface IAutoPrivate : IAuto
{
void AddCommandBufferDependencies(CommandBufferScoped cbs);
}
[SupportedOSPlatform("macos")]
public class Auto<T> : IAutoPrivate, IDisposable where T : IDisposable
class Auto<T> : IAutoPrivate, IDisposable where T : IDisposable
{
private int _referenceCount;
private T _value;

View file

@ -26,7 +26,7 @@ namespace Ryujinx.Graphics.Metal
if (_pool == null)
{
MTLCommandQueue queue = _renderer.BackgroundQueue;
_pool = new CommandBufferPool(queue.Device, queue);
_pool = new CommandBufferPool(queue);
}
return _pool;

View file

@ -8,7 +8,7 @@ using System.Threading;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class BufferHolder : IDisposable
class BufferHolder : IDisposable
{
private CacheByRange<BufferHolder> _cachedConvertedBuffers;

View file

@ -8,7 +8,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
public readonly struct ScopedTemporaryBuffer : IDisposable
readonly struct ScopedTemporaryBuffer : IDisposable
{
private readonly BufferManager _bufferManager;
private readonly bool _isReserved;
@ -39,7 +39,7 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public class BufferManager : IDisposable
class BufferManager : IDisposable
{
private readonly IdList<BufferHolder> _buffers;

View file

@ -1,11 +1,10 @@
using SharpMetal.Metal;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
public interface ICacheKey : IDisposable
interface ICacheKey : IDisposable
{
bool KeyEqual(ICacheKey other);
}
@ -116,7 +115,7 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public readonly struct Dependency
readonly struct Dependency
{
private readonly BufferHolder _buffer;
private readonly int _offset;

View file

@ -7,14 +7,12 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class CommandBufferPool : IDisposable
class CommandBufferPool : IDisposable
{
public const int MaxCommandBuffers = 16;
private readonly int _totalCommandBuffers;
private readonly int _totalCommandBuffersMask;
private readonly MTLDevice _device;
private readonly MTLCommandQueue _queue;
[SupportedOSPlatform("macos")]
@ -45,9 +43,8 @@ namespace Ryujinx.Graphics.Metal
private int _queuedCount;
private int _inUseCount;
public CommandBufferPool(MTLDevice device, MTLCommandQueue queue)
public CommandBufferPool(MTLCommandQueue queue)
{
_device = device;
_queue = queue;
_totalCommandBuffers = MaxCommandBuffers;

View file

@ -5,7 +5,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public readonly struct CommandBufferScoped : IDisposable
readonly struct CommandBufferScoped : IDisposable
{
private readonly CommandBufferPool _pool;
public MTLCommandBuffer CommandBuffer { get; }

View file

@ -7,7 +7,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class ComputePipelineCache : StateCache<MTLComputePipelineState, MTLFunction, MTLFunction>
class ComputePipelineCache : StateCache<MTLComputePipelineState, MTLFunction, MTLFunction>
{
private readonly MTLDevice _device;

View file

@ -4,7 +4,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public struct DepthStencilHash
struct DepthStencilHash
{
public struct StencilHash
{
@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public class DepthStencilCache : StateCache<MTLDepthStencilState, MTLDepthStencilDescriptor, DepthStencilHash>
class DepthStencilCache : StateCache<MTLDepthStencilState, MTLDepthStencilDescriptor, DepthStencilHash>
{
private readonly MTLDevice _device;

View file

@ -5,7 +5,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public readonly struct DisposableBuffer : IDisposable
readonly struct DisposableBuffer : IDisposable
{
public MTLBuffer Value { get; }

View file

@ -5,7 +5,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
public struct DirtyFlags
struct DirtyFlags
{
public bool RenderPipeline = false;
public bool ComputePipeline = false;
@ -21,7 +21,7 @@ namespace Ryujinx.Graphics.Metal
}
}
public record struct BufferRef
record struct BufferRef
{
public Auto<DisposableBuffer> Buffer;
public int Index;

View file

@ -360,7 +360,7 @@ namespace Ryujinx.Graphics.Metal
{
_currentState.IndexType = MTLIndexType.UInt16;
_currentState.IndexBufferOffset = (ulong)buffer.Offset;
_currentState.IndexBuffer = _bufferManager.GetBufferI8ToI16(_pipeline.CurrentCommandBuffer, buffer.Handle, buffer.Offset, buffer.Size);
_currentState.IndexBuffer = _bufferManager.GetBufferI8ToI16(_pipeline.Cbs, buffer.Handle, buffer.Offset, buffer.Size);
}
else
{
@ -1036,12 +1036,12 @@ namespace Ryujinx.Graphics.Metal
if (range.HasValue)
{
offset = range.Value.Offset;
mtlBuffer = autoBuffer.Get(_pipeline.CurrentCommandBuffer, offset, range.Value.Size, range.Value.Write).Value;
mtlBuffer = autoBuffer.Get(_pipeline.Cbs, offset, range.Value.Size, range.Value.Write).Value;
}
else
{
mtlBuffer = autoBuffer.Get(_pipeline.CurrentCommandBuffer).Value;
mtlBuffer = autoBuffer.Get(_pipeline.Cbs).Value;
}
renderCommandEncoder.SetVertexBuffer(mtlBuffer, (ulong)offset, (ulong)index);
@ -1072,12 +1072,12 @@ namespace Ryujinx.Graphics.Metal
if (range.HasValue)
{
offset = range.Value.Offset;
mtlBuffer = autoBuffer.Get(_pipeline.CurrentCommandBuffer, offset, range.Value.Size, range.Value.Write).Value;
mtlBuffer = autoBuffer.Get(_pipeline.Cbs, offset, range.Value.Size, range.Value.Write).Value;
}
else
{
mtlBuffer = autoBuffer.Get(_pipeline.CurrentCommandBuffer).Value;
mtlBuffer = autoBuffer.Get(_pipeline.Cbs).Value;
}
computeCommandEncoder.SetBuffer(mtlBuffer, (ulong)offset, (ulong)index);

View file

@ -6,7 +6,7 @@ using System.Threading;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class FenceHolder : IDisposable
class FenceHolder : IDisposable
{
private MTLCommandBuffer _fence;
private int _referenceCount;

View file

@ -10,7 +10,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class HelperShader : IDisposable
class HelperShader : IDisposable
{
private const int ConvertElementsPerWorkgroup = 32 * 100; // Work group size of 32 times 100 elements.
private const string ShadersSourcePath = "/Ryujinx.Graphics.Metal/Shaders";
@ -268,7 +268,7 @@ namespace Ryujinx.Graphics.Metal
// TODO: Flush
using var buffer = _renderer.BufferManager.ReserveOrCreate(_pipeline.CurrentCommandBuffer, ClearColorBufferSize);
using var buffer = _renderer.BufferManager.ReserveOrCreate(_pipeline.Cbs, ClearColorBufferSize);
buffer.Holder.SetDataUnchecked(buffer.Offset, clearColor);
_pipeline.SetUniformBuffers([new BufferAssignment(0, buffer.Range)]);

View file

@ -21,7 +21,6 @@ namespace Ryujinx.Graphics.Metal
public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
public bool PreferThreading => true;
public IPipeline Pipeline => _pipeline;
public IWindow Window => _window;
@ -54,9 +53,9 @@ namespace Ryujinx.Graphics.Metal
layer.Device = _device;
layer.FramebufferOnly = false;
CommandBufferPool = new CommandBufferPool(_device, _queue);
CommandBufferPool = new CommandBufferPool(_queue);
_window = new Window(this, layer);
_pipeline = new Pipeline(_device, this, _queue);
_pipeline = new Pipeline(_device, this);
BufferManager = new BufferManager(_device, this, _pipeline);
_pipeline.InitEncoderStateManager(BufferManager);
@ -222,7 +221,7 @@ namespace Ryujinx.Graphics.Metal
public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
{
BufferManager.SetData(buffer, offset, data, _pipeline.CurrentCommandBuffer, _pipeline.EndRenderPassDelegate);
BufferManager.SetData(buffer, offset, data, _pipeline.Cbs, _pipeline.EndRenderPassDelegate);
}
public void UpdateCounters()

View file

@ -8,7 +8,7 @@ namespace Ryujinx.Graphics.Metal
/// Holder for multiple host GPU fences.
/// </summary>
[SupportedOSPlatform("macos")]
public class MultiFenceHolder
class MultiFenceHolder
{
private const int BufferUsageTrackingGranularity = 4096;

View file

@ -18,40 +18,30 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public class Pipeline : IPipeline, IDisposable
class Pipeline : IPipeline, IDisposable
{
private readonly MTLDevice _device;
private readonly MTLCommandQueue _commandQueue;
private readonly MetalRenderer _renderer;
private CommandBufferScoped Cbs;
private CommandBufferScoped? PreloadCbs;
public MTLCommandBuffer CommandBuffer;
public readonly Action EndRenderPassDelegate;
public CommandBufferScoped CurrentCommandBuffer => Cbs;
private MTLCommandEncoder? _currentEncoder;
public MTLCommandEncoder? CurrentEncoder => _currentEncoder;
private EncoderType _currentEncoderType = EncoderType.None;
public EncoderType CurrentEncoderType => _currentEncoderType;
private EncoderStateManager _encoderStateManager;
public Pipeline(MTLDevice device, MetalRenderer renderer, MTLCommandQueue commandQueue)
public readonly Action EndRenderPassDelegate;
public MTLCommandBuffer CommandBuffer;
internal CommandBufferScoped Cbs { get; private set; }
internal MTLCommandEncoder? CurrentEncoder { get; private set; }
internal EncoderType CurrentEncoderType { get; private set; } = EncoderType.None;
public Pipeline(MTLDevice device, MetalRenderer renderer)
{
_device = device;
_renderer = renderer;
_commandQueue = commandQueue;
EndRenderPassDelegate = EndCurrentPass;
CommandBuffer = (Cbs = _renderer.CommandBufferPool.Rent()).CommandBuffer;
}
public void InitEncoderStateManager(BufferManager bufferManager)
internal void InitEncoderStateManager(BufferManager bufferManager)
{
_encoderStateManager = new EncoderStateManager(_device, bufferManager, this);
}
@ -79,13 +69,13 @@ namespace Ryujinx.Graphics.Metal
public MTLRenderCommandEncoder GetOrCreateRenderEncoder(bool forDraw = false)
{
MTLRenderCommandEncoder renderCommandEncoder;
if (_currentEncoder == null || _currentEncoderType != EncoderType.Render)
if (CurrentEncoder == null || CurrentEncoderType != EncoderType.Render)
{
renderCommandEncoder = BeginRenderPass();
}
else
{
renderCommandEncoder = new MTLRenderCommandEncoder(_currentEncoder.Value);
renderCommandEncoder = new MTLRenderCommandEncoder(CurrentEncoder.Value);
}
if (forDraw)
@ -98,11 +88,11 @@ namespace Ryujinx.Graphics.Metal
public MTLBlitCommandEncoder GetOrCreateBlitEncoder()
{
if (_currentEncoder != null)
if (CurrentEncoder != null)
{
if (_currentEncoderType == EncoderType.Blit)
if (CurrentEncoderType == EncoderType.Blit)
{
return new MTLBlitCommandEncoder(_currentEncoder.Value);
return new MTLBlitCommandEncoder(CurrentEncoder.Value);
}
}
@ -112,13 +102,13 @@ namespace Ryujinx.Graphics.Metal
public MTLComputeCommandEncoder GetOrCreateComputeEncoder()
{
MTLComputeCommandEncoder computeCommandEncoder;
if (_currentEncoder == null || _currentEncoderType != EncoderType.Compute)
if (CurrentEncoder == null || CurrentEncoderType != EncoderType.Compute)
{
computeCommandEncoder = BeginComputePass();
}
else
{
computeCommandEncoder = new MTLComputeCommandEncoder(_currentEncoder.Value);
computeCommandEncoder = new MTLComputeCommandEncoder(CurrentEncoder.Value);
}
_encoderStateManager.RebindComputeState(computeCommandEncoder);
@ -128,62 +118,62 @@ namespace Ryujinx.Graphics.Metal
public void EndCurrentPass()
{
if (_currentEncoder != null)
if (CurrentEncoder != null)
{
switch (_currentEncoderType)
switch (CurrentEncoderType)
{
case EncoderType.Blit:
new MTLBlitCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
new MTLBlitCommandEncoder(CurrentEncoder.Value).EndEncoding();
CurrentEncoder = null;
break;
case EncoderType.Compute:
new MTLComputeCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
new MTLComputeCommandEncoder(CurrentEncoder.Value).EndEncoding();
CurrentEncoder = null;
break;
case EncoderType.Render:
new MTLRenderCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
new MTLRenderCommandEncoder(CurrentEncoder.Value).EndEncoding();
CurrentEncoder = null;
break;
default:
throw new ArgumentOutOfRangeException();
}
_currentEncoderType = EncoderType.None;
CurrentEncoderType = EncoderType.None;
}
}
public MTLRenderCommandEncoder BeginRenderPass()
private MTLRenderCommandEncoder BeginRenderPass()
{
EndCurrentPass();
var renderCommandEncoder = _encoderStateManager.CreateRenderCommandEncoder();
_currentEncoder = renderCommandEncoder;
_currentEncoderType = EncoderType.Render;
CurrentEncoder = renderCommandEncoder;
CurrentEncoderType = EncoderType.Render;
return renderCommandEncoder;
}
public MTLBlitCommandEncoder BeginBlitPass()
private MTLBlitCommandEncoder BeginBlitPass()
{
EndCurrentPass();
var descriptor = new MTLBlitPassDescriptor();
var blitCommandEncoder = Cbs.CommandBuffer.BlitCommandEncoder(descriptor);
_currentEncoder = blitCommandEncoder;
_currentEncoderType = EncoderType.Blit;
CurrentEncoder = blitCommandEncoder;
CurrentEncoderType = EncoderType.Blit;
return blitCommandEncoder;
}
public MTLComputeCommandEncoder BeginComputePass()
private MTLComputeCommandEncoder BeginComputePass()
{
EndCurrentPass();
var computeCommandEncoder = _encoderStateManager.CreateComputeCommandEncoder();
_currentEncoder = computeCommandEncoder;
_currentEncoderType = EncoderType.Compute;
CurrentEncoder = computeCommandEncoder;
CurrentEncoderType = EncoderType.Compute;
return computeCommandEncoder;
}
@ -232,7 +222,7 @@ namespace Ryujinx.Graphics.Metal
public void Barrier()
{
switch (_currentEncoderType)
switch (CurrentEncoderType)
{
case EncoderType.Render:
{
@ -546,7 +536,7 @@ namespace Ryujinx.Graphics.Metal
_encoderStateManager.UpdateStorageBuffers(buffers);
}
public void SetStorageBuffers(int first, ReadOnlySpan<Auto<DisposableBuffer>> buffers)
internal void SetStorageBuffers(int first, ReadOnlySpan<Auto<DisposableBuffer>> buffers)
{
_encoderStateManager.UpdateStorageBuffers(first, buffers);
}

View file

@ -7,7 +7,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public struct RenderPipelineHash
struct RenderPipelineHash
{
public MTLFunction VertexFunction;
public MTLFunction FragmentFunction;
@ -162,7 +162,7 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public class RenderPipelineCache : StateCache<MTLRenderPipelineState, MTLRenderPipelineDescriptor, RenderPipelineHash>
class RenderPipelineCache : StateCache<MTLRenderPipelineState, MTLRenderPipelineDescriptor, RenderPipelineHash>
{
private readonly MTLDevice _device;

View file

@ -8,7 +8,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
public readonly struct StagingBufferReserved
readonly struct StagingBufferReserved
{
public readonly BufferHolder Buffer;
public readonly int Offset;
@ -23,7 +23,7 @@ namespace Ryujinx.Graphics.Metal
}
[SupportedOSPlatform("macos")]
public class StagingBuffer : IDisposable
class StagingBuffer : IDisposable
{
private const int BufferSize = 32 * 1024 * 1024;

View file

@ -5,7 +5,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public abstract class StateCache<T, TDescriptor, THash> : IDisposable where T : IDisposable
abstract class StateCache<T, TDescriptor, THash> : IDisposable where T : IDisposable
{
private readonly Dictionary<THash, T> _cache = new();

View file

@ -5,7 +5,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class StringHelper
class StringHelper
{
public static NSString NSString(string source)
{

View file

@ -7,7 +7,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class SyncManager
class SyncManager
{
private class SyncHandle
{

View file

@ -8,7 +8,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public class Texture : TextureBase, ITexture
class Texture : TextureBase, ITexture
{
public Texture(MTLDevice device, MetalRenderer renderer, Pipeline pipeline, TextureCreateInfo info) : base(device, renderer, pipeline, info)
{
@ -162,7 +162,7 @@ namespace Ryujinx.Graphics.Metal
public void CopyTo(BufferRange range, int layer, int level, int stride)
{
var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
var cbs = _pipeline.CurrentCommandBuffer;
var cbs = _pipeline.Cbs;
int outSize = Info.GetMipSize(level);

View file

@ -6,7 +6,7 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
public abstract class TextureBase : IDisposable
abstract class TextureBase : IDisposable
{
private bool _disposed;