Better index buffer management
This commit is contained in:
parent
18a1741348
commit
655823ecc7
5 changed files with 108 additions and 34 deletions
|
@ -135,6 +135,18 @@ namespace Ryujinx.Graphics.Metal
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Auto<DisposableBuffer> GetBuffer(BufferHandle handle, bool isWrite, out int size)
|
||||||
|
{
|
||||||
|
if (TryGetBuffer(handle, out var holder))
|
||||||
|
{
|
||||||
|
size = holder.Size;
|
||||||
|
return holder.GetBuffer(isWrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
size = 0;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public Auto<DisposableBuffer> GetBuffer(BufferHandle handle, int offset, int size, bool isWrite)
|
public Auto<DisposableBuffer> GetBuffer(BufferHandle handle, int offset, int size, bool isWrite)
|
||||||
{
|
{
|
||||||
if (TryGetBuffer(handle, out var holder))
|
if (TryGetBuffer(handle, out var holder))
|
||||||
|
|
|
@ -93,9 +93,7 @@ namespace Ryujinx.Graphics.Metal
|
||||||
public readonly BufferRef[] StorageBufferRefs = new BufferRef[Constants.MaxStorageBufferBindings];
|
public readonly BufferRef[] StorageBufferRefs = new BufferRef[Constants.MaxStorageBufferBindings];
|
||||||
public readonly TextureRef[] TextureRefs = new TextureRef[Constants.MaxTextureBindings];
|
public readonly TextureRef[] TextureRefs = new TextureRef[Constants.MaxTextureBindings];
|
||||||
|
|
||||||
public Auto<DisposableBuffer> IndexBuffer = default;
|
public IndexBufferState IndexBuffer = default;
|
||||||
public MTLIndexType IndexType = MTLIndexType.UInt16;
|
|
||||||
public ulong IndexBufferOffset = 0;
|
|
||||||
|
|
||||||
public MTLDepthClipMode DepthClipMode = MTLDepthClipMode.Clip;
|
public MTLDepthClipMode DepthClipMode = MTLDepthClipMode.Clip;
|
||||||
|
|
||||||
|
@ -115,7 +113,7 @@ namespace Ryujinx.Graphics.Metal
|
||||||
public MTLScissorRect[] Scissors = new MTLScissorRect[Constants.MaxViewports];
|
public MTLScissorRect[] Scissors = new MTLScissorRect[Constants.MaxViewports];
|
||||||
|
|
||||||
// Changes to attachments take recreation!
|
// Changes to attachments take recreation!
|
||||||
public Texture DepthStencil = default;
|
public Texture DepthStencil;
|
||||||
public Texture[] RenderTargets = new Texture[Constants.MaxColorAttachments];
|
public Texture[] RenderTargets = new Texture[Constants.MaxColorAttachments];
|
||||||
public ITexture PreMaskDepthStencil = default;
|
public ITexture PreMaskDepthStencil = default;
|
||||||
public ITexture[] PreMaskRenderTargets;
|
public ITexture[] PreMaskRenderTargets;
|
||||||
|
|
|
@ -23,9 +23,7 @@ namespace Ryujinx.Graphics.Metal
|
||||||
private readonly EncoderState _mainState = new();
|
private readonly EncoderState _mainState = new();
|
||||||
private EncoderState _currentState;
|
private EncoderState _currentState;
|
||||||
|
|
||||||
public readonly Auto<DisposableBuffer> IndexBuffer => _currentState.IndexBuffer;
|
public readonly IndexBufferState IndexBuffer => _currentState.IndexBuffer;
|
||||||
public readonly MTLIndexType IndexType => _currentState.IndexType;
|
|
||||||
public readonly ulong IndexBufferOffset => _currentState.IndexBufferOffset;
|
|
||||||
public readonly PrimitiveTopology Topology => _currentState.Topology;
|
public readonly PrimitiveTopology Topology => _currentState.Topology;
|
||||||
public readonly Texture[] RenderTargets => _currentState.RenderTargets;
|
public readonly Texture[] RenderTargets => _currentState.RenderTargets;
|
||||||
public readonly Texture DepthStencil => _currentState.DepthStencil;
|
public readonly Texture DepthStencil => _currentState.DepthStencil;
|
||||||
|
@ -305,18 +303,11 @@ namespace Ryujinx.Graphics.Metal
|
||||||
{
|
{
|
||||||
if (buffer.Handle != BufferHandle.Null)
|
if (buffer.Handle != BufferHandle.Null)
|
||||||
{
|
{
|
||||||
if (type == GAL.IndexType.UByte)
|
_currentState.IndexBuffer = new IndexBufferState(buffer.Handle, buffer.Offset, buffer.Size, type);
|
||||||
{
|
|
||||||
_currentState.IndexType = MTLIndexType.UInt16;
|
|
||||||
_currentState.IndexBufferOffset = 0;
|
|
||||||
_currentState.IndexBuffer = _bufferManager.GetBufferI8ToI16(_pipeline.Cbs, buffer.Handle, buffer.Offset, buffer.Size);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_currentState.IndexType = type.Convert();
|
_currentState.IndexBuffer = IndexBufferState.Null;
|
||||||
_currentState.IndexBufferOffset = (ulong)buffer.Offset;
|
|
||||||
_currentState.IndexBuffer = _bufferManager.GetBuffer(buffer.Handle, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
74
src/Ryujinx.Graphics.Metal/IndexBufferState.cs
Normal file
74
src/Ryujinx.Graphics.Metal/IndexBufferState.cs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
using Ryujinx.Graphics.GAL;
|
||||||
|
using SharpMetal.Metal;
|
||||||
|
using System;
|
||||||
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
|
namespace Ryujinx.Graphics.Metal
|
||||||
|
{
|
||||||
|
[SupportedOSPlatform("macos")]
|
||||||
|
internal struct IndexBufferState
|
||||||
|
{
|
||||||
|
public static IndexBufferState Null => new(BufferHandle.Null, 0, 0);
|
||||||
|
|
||||||
|
private readonly int _offset;
|
||||||
|
private readonly int _size;
|
||||||
|
private readonly IndexType _type;
|
||||||
|
|
||||||
|
private readonly BufferHandle _handle;
|
||||||
|
|
||||||
|
public IndexBufferState(BufferHandle handle, int offset, int size, IndexType type)
|
||||||
|
{
|
||||||
|
_handle = handle;
|
||||||
|
_offset = offset;
|
||||||
|
_size = size;
|
||||||
|
_type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexBufferState(BufferHandle handle, int offset, int size)
|
||||||
|
{
|
||||||
|
_handle = handle;
|
||||||
|
_offset = offset;
|
||||||
|
_size = size;
|
||||||
|
_type = IndexType.UInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public (MTLBuffer, int, MTLIndexType) GetIndexBuffer(MetalRenderer renderer, CommandBufferScoped cbs)
|
||||||
|
{
|
||||||
|
Auto<DisposableBuffer> autoBuffer;
|
||||||
|
int offset, size;
|
||||||
|
MTLIndexType type;
|
||||||
|
|
||||||
|
if (_type == IndexType.UByte)
|
||||||
|
{
|
||||||
|
// Index type is not supported. Convert to I16.
|
||||||
|
autoBuffer = renderer.BufferManager.GetBufferI8ToI16(cbs, _handle, _offset, _size);
|
||||||
|
|
||||||
|
type = MTLIndexType.UInt16;
|
||||||
|
offset = 0;
|
||||||
|
size = _size * 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
autoBuffer = renderer.BufferManager.GetBuffer(_handle, false, out int bufferSize);
|
||||||
|
|
||||||
|
if (_offset >= bufferSize)
|
||||||
|
{
|
||||||
|
autoBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
type = _type.Convert();
|
||||||
|
offset = _offset;
|
||||||
|
size = _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (autoBuffer != null)
|
||||||
|
{
|
||||||
|
DisposableBuffer buffer = autoBuffer.Get(cbs, offset, size);
|
||||||
|
|
||||||
|
return (buffer.Value, offset, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new MTLBuffer(IntPtr.Zero), 0, MTLIndexType.UInt16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -377,8 +377,6 @@ namespace Ryujinx.Graphics.Metal
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var renderCommandEncoder = GetOrCreateRenderEncoder(true);
|
|
||||||
|
|
||||||
// TODO: Reindex unsupported topologies
|
// TODO: Reindex unsupported topologies
|
||||||
if (TopologyUnsupported(_encoderStateManager.Topology))
|
if (TopologyUnsupported(_encoderStateManager.Topology))
|
||||||
{
|
{
|
||||||
|
@ -387,22 +385,23 @@ namespace Ryujinx.Graphics.Metal
|
||||||
|
|
||||||
var primitiveType = TopologyRemap(_encoderStateManager.Topology).Convert();
|
var primitiveType = TopologyRemap(_encoderStateManager.Topology).Convert();
|
||||||
|
|
||||||
var indexBuffer = _encoderStateManager.IndexBuffer;
|
(MTLBuffer mtlBuffer, int offset, MTLIndexType type) = _encoderStateManager.IndexBuffer.GetIndexBuffer(_renderer, Cbs);
|
||||||
|
|
||||||
ulong offset = _encoderStateManager.IndexBufferOffset;
|
if (mtlBuffer.NativePtr != IntPtr.Zero)
|
||||||
MTLIndexType type = _encoderStateManager.IndexType;
|
{
|
||||||
int indexSize = type == MTLIndexType.UInt32 ? sizeof(int) : sizeof(short);
|
var renderCommandEncoder = GetOrCreateRenderEncoder(true);
|
||||||
|
|
||||||
renderCommandEncoder.DrawIndexedPrimitives(
|
renderCommandEncoder.DrawIndexedPrimitives(
|
||||||
primitiveType,
|
primitiveType,
|
||||||
(ulong)indexCount,
|
(ulong)indexCount,
|
||||||
type,
|
type,
|
||||||
indexBuffer.Get(Cbs, (int)offset, indexCount * indexSize).Value,
|
mtlBuffer,
|
||||||
offset,
|
(ulong)offset,
|
||||||
(ulong)instanceCount,
|
(ulong)instanceCount,
|
||||||
firstVertex,
|
firstVertex,
|
||||||
(ulong)firstInstance);
|
(ulong)firstInstance);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void DrawIndexedIndirect(BufferRange indirectBuffer)
|
public void DrawIndexedIndirect(BufferRange indirectBuffer)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue