2020-09-24 01:48:34 +02:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine.Threed
|
2020-09-24 01:48:34 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Holds inline index buffer state.
|
|
|
|
|
/// The inline index buffer data is sent to the GPU through the command buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
struct IbStreamer
|
|
|
|
|
{
|
2023-03-24 14:19:54 +01:00
|
|
|
|
private const int BufferCapacity = 256; // Must be a power of 2.
|
|
|
|
|
|
2020-09-24 01:48:34 +02:00
|
|
|
|
private BufferHandle _inlineIndexBuffer;
|
|
|
|
|
private int _inlineIndexBufferSize;
|
|
|
|
|
private int _inlineIndexCount;
|
2023-03-24 14:19:54 +01:00
|
|
|
|
private uint[] _buffer;
|
2023-07-02 02:47:54 +02:00
|
|
|
|
#pragma warning disable IDE0051 // Remove unused private member
|
|
|
|
|
private readonly int _bufferOffset;
|
|
|
|
|
#pragma warning restore IDE0051
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates if any index buffer data has been pushed.
|
|
|
|
|
/// </summary>
|
2023-07-02 02:47:54 +02:00
|
|
|
|
public readonly bool HasInlineIndexData => _inlineIndexCount != 0;
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2022-06-11 04:44:49 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Total numbers of indices that have been pushed.
|
|
|
|
|
/// </summary>
|
2023-07-02 02:47:54 +02:00
|
|
|
|
public readonly int InlineIndexCount => _inlineIndexCount;
|
2022-06-11 04:44:49 +02:00
|
|
|
|
|
2020-09-24 01:48:34 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the handle for the host buffer currently holding the inline index buffer data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Host buffer handle</returns>
|
2023-07-02 02:47:54 +02:00
|
|
|
|
public readonly BufferHandle GetInlineIndexBuffer()
|
2020-09-24 01:48:34 +02:00
|
|
|
|
{
|
|
|
|
|
return _inlineIndexBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the number of elements on the current inline index buffer,
|
2023-07-02 02:47:54 +02:00
|
|
|
|
/// while also resetting it to zero for the next draw.
|
2020-09-24 01:48:34 +02:00
|
|
|
|
/// </summary>
|
2023-03-24 14:19:54 +01:00
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
2023-07-02 02:47:54 +02:00
|
|
|
|
/// <returns>Inline index buffer count</returns>
|
2023-03-24 14:19:54 +01:00
|
|
|
|
public int GetAndResetInlineIndexCount(IRenderer renderer)
|
2020-09-24 01:48:34 +02:00
|
|
|
|
{
|
2023-03-24 14:19:54 +01:00
|
|
|
|
UpdateRemaining(renderer);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
int temp = _inlineIndexCount;
|
|
|
|
|
_inlineIndexCount = 0;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes four 8-bit index buffer elements.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void VbElementU8(IRenderer renderer, int argument)
|
|
|
|
|
{
|
|
|
|
|
byte i0 = (byte)argument;
|
|
|
|
|
byte i1 = (byte)(argument >> 8);
|
|
|
|
|
byte i2 = (byte)(argument >> 16);
|
|
|
|
|
byte i3 = (byte)(argument >> 24);
|
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
int offset = _inlineIndexCount;
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
PushData(renderer, offset, i0);
|
|
|
|
|
PushData(renderer, offset + 1, i1);
|
|
|
|
|
PushData(renderer, offset + 2, i2);
|
|
|
|
|
PushData(renderer, offset + 3, i3);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
|
|
|
|
_inlineIndexCount += 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes two 16-bit index buffer elements.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void VbElementU16(IRenderer renderer, int argument)
|
|
|
|
|
{
|
|
|
|
|
ushort i0 = (ushort)argument;
|
|
|
|
|
ushort i1 = (ushort)(argument >> 16);
|
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
int offset = _inlineIndexCount;
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
PushData(renderer, offset, i0);
|
|
|
|
|
PushData(renderer, offset + 1, i1);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
|
|
|
|
_inlineIndexCount += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes one 32-bit index buffer element.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void VbElementU32(IRenderer renderer, int argument)
|
|
|
|
|
{
|
|
|
|
|
uint i0 = (uint)argument;
|
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
int offset = _inlineIndexCount++;
|
|
|
|
|
|
|
|
|
|
PushData(renderer, offset, i0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes a 32-bit value to the index buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
/// <param name="offset">Offset where the data should be written, in 32-bit words</param>
|
|
|
|
|
/// <param name="value">Index value to be written</param>
|
|
|
|
|
private void PushData(IRenderer renderer, int offset, uint value)
|
|
|
|
|
{
|
2023-07-02 02:47:54 +02:00
|
|
|
|
_buffer ??= new uint[BufferCapacity];
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
// We upload data in chunks.
|
|
|
|
|
// If we are at the start of a chunk, then the buffer might be full,
|
|
|
|
|
// in that case we need to submit any existing data before overwriting the buffer.
|
|
|
|
|
int subOffset = offset & (BufferCapacity - 1);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
if (subOffset == 0 && offset != 0)
|
|
|
|
|
{
|
|
|
|
|
int baseOffset = (offset - BufferCapacity) * sizeof(uint);
|
|
|
|
|
BufferHandle buffer = GetInlineIndexBuffer(renderer, baseOffset, BufferCapacity * sizeof(uint));
|
|
|
|
|
renderer.SetBufferData(buffer, baseOffset, MemoryMarshal.Cast<uint, byte>(_buffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_buffer[subOffset] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Makes sure that any pending data is submitted to the GPU before the index buffer is used.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
private void UpdateRemaining(IRenderer renderer)
|
|
|
|
|
{
|
|
|
|
|
int offset = _inlineIndexCount;
|
|
|
|
|
if (offset == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int count = offset & (BufferCapacity - 1);
|
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
count = BufferCapacity;
|
|
|
|
|
}
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
2023-03-24 14:19:54 +01:00
|
|
|
|
int baseOffset = (offset - count) * sizeof(uint);
|
|
|
|
|
int length = count * sizeof(uint);
|
|
|
|
|
BufferHandle buffer = GetInlineIndexBuffer(renderer, baseOffset, length);
|
2023-07-02 02:47:54 +02:00
|
|
|
|
renderer.SetBufferData(buffer, baseOffset, MemoryMarshal.Cast<uint, byte>(_buffer)[..length]);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the handle of a buffer large enough to hold the data that will be written to <paramref name="offset"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="renderer">Host renderer</param>
|
|
|
|
|
/// <param name="offset">Offset where the data will be written</param>
|
2023-03-24 14:19:54 +01:00
|
|
|
|
/// <param name="length">Number of bytes that will be written</param>
|
2020-09-24 01:48:34 +02:00
|
|
|
|
/// <returns>Buffer handle</returns>
|
2023-03-24 14:19:54 +01:00
|
|
|
|
private BufferHandle GetInlineIndexBuffer(IRenderer renderer, int offset, int length)
|
2020-09-24 01:48:34 +02:00
|
|
|
|
{
|
|
|
|
|
// Calculate a reasonable size for the buffer that can fit all the data,
|
|
|
|
|
// and that also won't require frequent resizes if we need to push more data.
|
2023-03-24 14:19:54 +01:00
|
|
|
|
int size = BitUtils.AlignUp(offset + length + 0x10, 0x200);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
|
|
|
|
|
if (_inlineIndexBuffer == BufferHandle.Null)
|
|
|
|
|
{
|
2023-08-14 19:18:47 +02:00
|
|
|
|
_inlineIndexBuffer = renderer.CreateBuffer(size, BufferAccess.Stream);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
_inlineIndexBufferSize = size;
|
|
|
|
|
}
|
|
|
|
|
else if (_inlineIndexBufferSize < size)
|
|
|
|
|
{
|
|
|
|
|
BufferHandle oldBuffer = _inlineIndexBuffer;
|
|
|
|
|
int oldSize = _inlineIndexBufferSize;
|
|
|
|
|
|
2023-08-14 19:18:47 +02:00
|
|
|
|
_inlineIndexBuffer = renderer.CreateBuffer(size, BufferAccess.Stream);
|
2020-09-24 01:48:34 +02:00
|
|
|
|
_inlineIndexBufferSize = size;
|
|
|
|
|
|
|
|
|
|
renderer.Pipeline.CopyBuffer(oldBuffer, _inlineIndexBuffer, 0, 0, oldSize);
|
|
|
|
|
renderer.DeleteBuffer(oldBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _inlineIndexBuffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|