mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-18 23:16:39 +00:00
f77694e4f7
* Implement a new physical memory manager and replace DeviceMemory * Proper generic constraints * Fix debug build * Add memory tests * New CPU memory manager and general code cleanup * Remove host memory management from CPU project, use Ryujinx.Memory instead * Fix tests * Document exceptions on MemoryBlock * Fix leak on unix memory allocation * Proper disposal of some objects on tests * Fix JitCache not being set as initialized * GetRef without checks for 8-bits and 16-bits CAS * Add MemoryBlock destructor * Throw in separate method to improve codegen * Address PR feedback * QueryModified improvements * Fix memory write tracking not marking all pages as modified in some cases * Simplify MarkRegionAsModified * Remove XML doc for ghost param * Add back optimization to avoid useless buffer updates * Add Ryujinx.Cpu project, move MemoryManager there and remove MemoryBlockWrapper * Some nits * Do not perform address translation when size is 0 * Address PR feedback and format NativeInterface class * Remove ghost parameter description * Update Ryujinx.Cpu to .NET Core 3.1 * Address PR feedback * Fix build * Return a well defined value for GetPhysicalAddress with invalid VA, and do not return unmapped ranges as modified * Typo
165 lines
No EOL
5.7 KiB
C#
165 lines
No EOL
5.7 KiB
C#
using Ryujinx.Graphics.GAL;
|
|
using System;
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
{
|
|
/// <summary>
|
|
/// Buffer, used to store vertex and index data, uniform and storage buffers, and others.
|
|
/// </summary>
|
|
class Buffer : IRange, IDisposable
|
|
{
|
|
private readonly GpuContext _context;
|
|
|
|
/// <summary>
|
|
/// Host buffer object.
|
|
/// </summary>
|
|
public IBuffer HostBuffer { get; }
|
|
|
|
/// <summary>
|
|
/// Start address of the buffer in guest memory.
|
|
/// </summary>
|
|
public ulong Address { get; }
|
|
|
|
/// <summary>
|
|
/// Size of the buffer in bytes.
|
|
/// </summary>
|
|
public ulong Size { get; }
|
|
|
|
/// <summary>
|
|
/// End address of the buffer in guest memory.
|
|
/// </summary>
|
|
public ulong EndAddress => Address + Size;
|
|
|
|
private readonly (ulong, ulong)[] _modifiedRanges;
|
|
|
|
private readonly int[] _sequenceNumbers;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the buffer.
|
|
/// </summary>
|
|
/// <param name="context">GPU context that the buffer belongs to</param>
|
|
/// <param name="address">Start address of the buffer</param>
|
|
/// <param name="size">Size of the buffer in bytes</param>
|
|
public Buffer(GpuContext context, ulong address, ulong size)
|
|
{
|
|
_context = context;
|
|
Address = address;
|
|
Size = size;
|
|
|
|
HostBuffer = context.Renderer.CreateBuffer((int)size);
|
|
|
|
_modifiedRanges = new (ulong, ulong)[size / PhysicalMemory.PageSize];
|
|
|
|
_sequenceNumbers = new int[size / MemoryManager.PageSize];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a sub-range from the buffer.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This can be used to bind and use sub-ranges of the buffer on the host API.
|
|
/// </remarks>
|
|
/// <param name="address">Start address of the sub-range, must be greater than or equal to the buffer address</param>
|
|
/// <param name="size">Size in bytes of the sub-range, must be less than or equal to the buffer size</param>
|
|
/// <returns>The buffer sub-range</returns>
|
|
public BufferRange GetRange(ulong address, ulong size)
|
|
{
|
|
int offset = (int)(address - Address);
|
|
|
|
return new BufferRange(HostBuffer, offset, (int)size);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if a given range overlaps with the buffer.
|
|
/// </summary>
|
|
/// <param name="address">Start address of the range</param>
|
|
/// <param name="size">Size in bytes of the range</param>
|
|
/// <returns>True if the range overlaps, false otherwise</returns>
|
|
public bool OverlapsWith(ulong address, ulong size)
|
|
{
|
|
return Address < address + size && address < EndAddress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Performs guest to host memory synchronization of the buffer data.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This causes the buffer data to be overwritten if a write was detected from the CPU,
|
|
/// since the last call to this method.
|
|
/// </remarks>
|
|
/// <param name="address">Start address of the range to synchronize</param>
|
|
/// <param name="size">Size in bytes of the range to synchronize</param>
|
|
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;
|
|
}
|
|
|
|
int count = _context.PhysicalMemory.QueryModified(address, size, ResourceName.Buffer, _modifiedRanges);
|
|
|
|
for (int index = 0; index < count; index++)
|
|
{
|
|
(ulong mAddress, ulong mSize) = _modifiedRanges[index];
|
|
|
|
int offset = (int)(mAddress - Address);
|
|
|
|
HostBuffer.SetData(offset, _context.PhysicalMemory.GetSpan(mAddress, mSize));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Performs copy of all the buffer data from one buffer to another.
|
|
/// </summary>
|
|
/// <param name="destination">The destination buffer to copy the data into</param>
|
|
/// <param name="dstOffset">The offset of the destination buffer to copy into</param>
|
|
public void CopyTo(Buffer destination, int dstOffset)
|
|
{
|
|
HostBuffer.CopyTo(destination.HostBuffer, 0, dstOffset, (int)Size);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Flushes a range of the buffer.
|
|
/// This writes the range data back into guest memory.
|
|
/// </summary>
|
|
/// <param name="address">Start address of the range</param>
|
|
/// <param name="size">Size in bytes of the range</param>
|
|
public void Flush(ulong address, ulong size)
|
|
{
|
|
int offset = (int)(address - Address);
|
|
|
|
byte[] data = HostBuffer.GetData(offset, (int)size);
|
|
|
|
_context.PhysicalMemory.Write(address, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disposes the host buffer.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
HostBuffer.Dispose();
|
|
}
|
|
}
|
|
} |