Ryujinx/src/Ryujinx.Graphics.Vulkan/VertexBufferState.cs
riperiperi 492a046335
Vulkan: Buffer Mirrors for MacOS performance (#4899)
* Initial implementation of buffer mirrors

Generally slower right now, goal is to reduce render passes in games that do inline updates

Fix support buffer mirrors

Reintroduce vertex buffer mirror

Add storage buffer support

Optimisation part 1

More optimisation

Avoid useless data copies.

Remove unused cbIndex stuff

Properly set write flag for storage buffers.

Fix minor issues

Not sure why this was here.

Fix BufferRangeList

Fix some big issues

Align storage buffers rather than getting full buffer as a range

Improves mirrorability of read-only storage buffers

Increase staging buffer size, as it now contains mirrors

Fix some issues with buffers not updating

Fix buffer SetDataUnchecked offset for one of the paths when using mirrors

Fix buffer mirrors interaction with buffer textures

Fix mirror rebinding

Move GetBuffer calls on indirect draws before BeginRenderPass to avoid draws without render pass

Fix mirrors rebase

Fix rebase 2023

* Fix crash when using stale vertex buffer

Similar to `Get` with a size that's too large, just treat it as a clamp.

* Explicitly set support buffer as mirrorable

* Address feedback

* Remove unused fragment of MVK workaround

* Replace logging for staging buffer OOM

* Address format issues

* Address more format issues

* Mini cleanup

* Address more things

* Rename BufferRangeList

* Support bounding range for ClearMirrors and UploadPendingData

* Add maximum size for vertex buffer mirrors

* Enable index buffer mirrors

Enabled on all platforms for the IbStreamer.

* Feedback

* Remove mystery BufferCache change

Probably macos related?

* Fix mirrors not creating when staging buffer is empty.

* Change log level to debug
2023-08-14 14:18:47 -03:00

137 lines
4.6 KiB
C#

using Ryujinx.Graphics.GAL;
namespace Ryujinx.Graphics.Vulkan
{
internal struct VertexBufferState
{
private const int VertexBufferMaxMirrorable = 0x20000;
public static VertexBufferState Null => new(null, 0, 0, 0);
private readonly int _offset;
private readonly int _size;
private readonly int _stride;
private readonly BufferHandle _handle;
private Auto<DisposableBuffer> _buffer;
internal readonly int DescriptorIndex;
internal int AttributeScalarAlignment;
public VertexBufferState(Auto<DisposableBuffer> buffer, int descriptorIndex, int offset, int size, int stride = 0)
{
_buffer = buffer;
_handle = BufferHandle.Null;
_offset = offset;
_size = size;
_stride = stride;
DescriptorIndex = descriptorIndex;
AttributeScalarAlignment = 1;
buffer?.IncrementReferenceCount();
}
public VertexBufferState(BufferHandle handle, int descriptorIndex, int offset, int size, int stride = 0)
{
// This buffer state may be rewritten at bind time, so it must be retrieved on bind.
_buffer = null;
_handle = handle;
_offset = offset;
_size = size;
_stride = stride;
DescriptorIndex = descriptorIndex;
AttributeScalarAlignment = 1;
}
public void BindVertexBuffer(VulkanRenderer gd, CommandBufferScoped cbs, uint binding, ref PipelineState state, VertexBufferUpdater updater)
{
var autoBuffer = _buffer;
if (_handle != BufferHandle.Null)
{
// May need to restride the vertex buffer.
if (gd.NeedsVertexBufferAlignment(AttributeScalarAlignment, out int alignment) && (_stride % alignment) != 0)
{
autoBuffer = gd.BufferManager.GetAlignedVertexBuffer(cbs, _handle, _offset, _size, _stride, alignment);
if (autoBuffer != null)
{
int stride = (_stride + (alignment - 1)) & -alignment;
int newSize = (_size / _stride) * stride;
var buffer = autoBuffer.Get(cbs, 0, newSize).Value;
updater.BindVertexBuffer(cbs, binding, buffer, 0, (ulong)newSize, (ulong)stride);
_buffer = autoBuffer;
state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)stride;
}
return;
}
autoBuffer = gd.BufferManager.GetBuffer(cbs.CommandBuffer, _handle, false, out int size);
// The original stride must be reapplied in case it was rewritten.
state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)_stride;
if (_offset >= size)
{
autoBuffer = null;
}
}
if (autoBuffer != null)
{
int offset = _offset;
bool mirrorable = _size <= VertexBufferMaxMirrorable;
var buffer = mirrorable ? autoBuffer.GetMirrorable(cbs, ref offset, _size, out _).Value : autoBuffer.Get(cbs, offset, _size).Value;
updater.BindVertexBuffer(cbs, binding, buffer, (ulong)offset, (ulong)_size, (ulong)_stride);
}
}
public readonly bool BoundEquals(Auto<DisposableBuffer> buffer)
{
return _buffer == buffer;
}
public readonly bool Overlaps(Auto<DisposableBuffer> buffer, int offset, int size)
{
return buffer == _buffer && offset < _offset + _size && offset + size > _offset;
}
public readonly bool Matches(Auto<DisposableBuffer> buffer, int descriptorIndex, int offset, int size, int stride = 0)
{
return _buffer == buffer && DescriptorIndex == descriptorIndex && _offset == offset && _size == size && _stride == stride;
}
public void Swap(Auto<DisposableBuffer> from, Auto<DisposableBuffer> to)
{
if (_buffer == from)
{
_buffer.DecrementReferenceCount();
to.IncrementReferenceCount();
_buffer = to;
}
}
public readonly void Dispose()
{
// Only dispose if this buffer is not refetched on each bind.
if (_handle == BufferHandle.Null)
{
_buffer?.DecrementReferenceCount();
}
}
}
}