2023-07-01 12:31:42 +02:00
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2022-09-09 01:30:19 +02:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Vulkan
|
|
|
|
|
{
|
|
|
|
|
internal struct VertexBufferState
|
|
|
|
|
{
|
2023-08-14 19:18:47 +02:00
|
|
|
|
private const int VertexBufferMaxMirrorable = 0x20000;
|
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
public static VertexBufferState Null => new(null, 0, 0, 0);
|
2022-09-09 01:30:19 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 10:59:26 +02:00
|
|
|
|
public void BindVertexBuffer(VulkanRenderer gd, CommandBufferScoped cbs, uint binding, ref PipelineState state, VertexBufferUpdater updater)
|
2022-09-09 01:30:19 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2022-10-17 00:38:58 +02:00
|
|
|
|
if (autoBuffer != null)
|
2022-09-09 01:30:19 +02:00
|
|
|
|
{
|
2022-10-17 00:38:58 +02:00
|
|
|
|
int stride = (_stride + (alignment - 1)) & -alignment;
|
|
|
|
|
int newSize = (_size / _stride) * stride;
|
|
|
|
|
|
|
|
|
|
var buffer = autoBuffer.Get(cbs, 0, newSize).Value;
|
|
|
|
|
|
2023-05-08 10:59:26 +02:00
|
|
|
|
updater.BindVertexBuffer(cbs, binding, buffer, 0, (ulong)newSize, (ulong)stride);
|
2022-10-17 00:38:58 +02:00
|
|
|
|
|
|
|
|
|
_buffer = autoBuffer;
|
2022-09-09 01:30:19 +02:00
|
|
|
|
|
2023-01-13 01:31:21 +01:00
|
|
|
|
state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)stride;
|
|
|
|
|
}
|
2022-09-09 01:30:19 +02:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
autoBuffer = gd.BufferManager.GetBuffer(cbs.CommandBuffer, _handle, false, out int size);
|
2022-10-17 00:38:58 +02:00
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
// The original stride must be reapplied in case it was rewritten.
|
|
|
|
|
state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)_stride;
|
|
|
|
|
|
|
|
|
|
if (_offset >= size)
|
|
|
|
|
{
|
|
|
|
|
autoBuffer = null;
|
2022-09-09 01:30:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (autoBuffer != null)
|
|
|
|
|
{
|
2023-08-14 19:18:47 +02:00
|
|
|
|
int offset = _offset;
|
|
|
|
|
bool mirrorable = _size <= VertexBufferMaxMirrorable;
|
|
|
|
|
var buffer = mirrorable ? autoBuffer.GetMirrorable(cbs, ref offset, _size, out _).Value : autoBuffer.Get(cbs, offset, _size).Value;
|
2022-09-09 01:30:19 +02:00
|
|
|
|
|
2023-08-14 19:18:47 +02:00
|
|
|
|
updater.BindVertexBuffer(cbs, binding, buffer, (ulong)offset, (ulong)_size, (ulong)_stride);
|
2022-09-09 01:30:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
public readonly bool BoundEquals(Auto<DisposableBuffer> buffer)
|
2022-09-09 01:30:19 +02:00
|
|
|
|
{
|
|
|
|
|
return _buffer == buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 19:18:47 +02:00
|
|
|
|
public readonly bool Overlaps(Auto<DisposableBuffer> buffer, int offset, int size)
|
|
|
|
|
{
|
|
|
|
|
return buffer == _buffer && offset < _offset + _size && offset + size > _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
public readonly bool Matches(Auto<DisposableBuffer> buffer, int descriptorIndex, int offset, int size, int stride = 0)
|
2023-05-08 10:59:26 +02:00
|
|
|
|
{
|
|
|
|
|
return _buffer == buffer && DescriptorIndex == descriptorIndex && _offset == offset && _size == size && _stride == stride;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 21:56:48 +01:00
|
|
|
|
public void Swap(Auto<DisposableBuffer> from, Auto<DisposableBuffer> to)
|
|
|
|
|
{
|
|
|
|
|
if (_buffer == from)
|
|
|
|
|
{
|
|
|
|
|
_buffer.DecrementReferenceCount();
|
|
|
|
|
to.IncrementReferenceCount();
|
|
|
|
|
|
|
|
|
|
_buffer = to;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 12:31:42 +02:00
|
|
|
|
public readonly void Dispose()
|
2022-09-09 01:30:19 +02:00
|
|
|
|
{
|
|
|
|
|
// Only dispose if this buffer is not refetched on each bind.
|
|
|
|
|
|
|
|
|
|
if (_handle == BufferHandle.Null)
|
|
|
|
|
{
|
|
|
|
|
_buffer?.DecrementReferenceCount();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|