using Ryujinx.Graphics.GAL;
using System;
namespace Ryujinx.Graphics.Gpu.Memory
{
///
/// Buffer, used to store vertex and index data, uniform and storage buffers, and others.
///
class Buffer : IRange, IDisposable
{
private readonly GpuContext _context;
///
/// Host buffer object.
///
public IBuffer HostBuffer { get; }
///
/// Start address of the buffer in guest memory.
///
public ulong Address { get; }
///
/// Size of the buffer in bytes.
///
public ulong Size { get; }
///
/// End address of the buffer in guest memory.
///
public ulong EndAddress => Address + Size;
private int[] _sequenceNumbers;
///
/// Creates a new instance of the buffer.
///
/// GPU context that the buffer belongs to
/// Start address of the buffer
/// Size of the buffer in bytes
public Buffer(GpuContext context, ulong address, ulong size)
{
_context = context;
Address = address;
Size = size;
HostBuffer = context.Renderer.CreateBuffer((int)size);
_sequenceNumbers = new int[size / MemoryManager.PageSize];
}
///
/// Gets a sub-range from the buffer.
///
///
/// This can be used to bind and use sub-ranges of the buffer on the host API.
///
/// Start address of the sub-range, must be greater than or equal to the buffer address
/// Size in bytes of the sub-range, must be less than or equal to the buffer size
/// The buffer sub-range
public BufferRange GetRange(ulong address, ulong size)
{
int offset = (int)(address - Address);
return new BufferRange(HostBuffer, offset, (int)size);
}
///
/// Checks if a given range overlaps with the buffer.
///
/// Start address of the range
/// Size in bytes of the range
/// True if the range overlaps, false otherwise
public bool OverlapsWith(ulong address, ulong size)
{
return Address < address + size && address < EndAddress;
}
///
/// Performs guest to host memory synchronization of the buffer data.
///
///
/// This causes the buffer data to be overwritten if a write was detected from the CPU,
/// since the last call to this method.
///
/// Start address of the range to synchronize
/// Size in bytes of the range to synchronize
public void SynchronizeMemory(ulong address, ulong size)
{
int currentSequenceNumber = _context.SequenceNumber;
bool needsSync = false;
ulong buffOffset = address - Address;
ulong buffEndOffset = (buffOffset + size + MemoryManager.PageMask) & ~MemoryManager.PageMask;
int startIndex = (int)(buffOffset / MemoryManager.PageSize);
int endIndex = (int)(buffEndOffset / MemoryManager.PageSize);
for (int index = startIndex; index < endIndex; index++)
{
if (_sequenceNumbers[index] != currentSequenceNumber)
{
_sequenceNumbers[index] = currentSequenceNumber;
needsSync = true;
}
}
if (!needsSync)
{
return;
}
(ulong, ulong)[] modifiedRanges = _context.PhysicalMemory.GetModifiedRanges(address, size, ResourceName.Buffer);
for (int index = 0; index < modifiedRanges.Length; index++)
{
(ulong mAddress, ulong mSize) = modifiedRanges[index];
int offset = (int)(mAddress - Address);
HostBuffer.SetData(offset, _context.PhysicalMemory.GetSpan(mAddress, mSize));
}
}
///
/// Performs copy of all the buffer data from one buffer to another.
///
/// The destination buffer to copy the data into
/// The offset of the destination buffer to copy into
public void CopyTo(Buffer destination, int dstOffset)
{
HostBuffer.CopyTo(destination.HostBuffer, 0, dstOffset, (int)Size);
}
///
/// Flushes a range of the buffer.
/// This writes the range data back into guest memory.
///
/// Start address of the range
/// Size in bytes of the range
public void Flush(ulong address, ulong size)
{
int offset = (int)(address - Address);
byte[] data = HostBuffer.GetData(offset, (int)size);
_context.PhysicalMemory.Write(address, data);
}
///
/// Disposes the host buffer.
///
public void Dispose()
{
HostBuffer.Dispose();
}
}
}