0
0
Fork 0

Add XML documentation to Ryujinx.Graphics.Gpu.Memory

This commit is contained in:
gdkchan 2019-12-31 00:22:58 -03:00 committed by Thog
parent 80ff2eab29
commit e58b540c4e
11 changed files with 471 additions and 16 deletions

View file

@ -3,21 +3,43 @@ 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<Buffer>, IDisposable
{
private GpuContext _context;
private IBuffer _buffer;
/// <summary>
/// Host buffer object.
/// </summary>
public IBuffer HostBuffer => _buffer;
/// <summary>
/// Start address of the buffer in guest memory.
/// </summary>
public ulong Address { get; }
public ulong Size { 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 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;
@ -31,6 +53,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
Invalidate();
}
/// <summary>
/// Gets a sub-range from the buffer.
/// This can be used to bind and use sub-ranges of the buffer on the host API.
/// </summary>
/// <param name="address">Start address of the sub-range, must be greater 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);
@ -38,11 +67,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
return new BufferRange(_buffer, 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.
/// This causes the buffer data to be overwritten if a write was detected from the CPU,
/// since the last call to this method.
/// </summary>
/// <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;
@ -83,11 +125,22 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <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)
{
_buffer.CopyTo(destination._buffer, 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);
@ -97,11 +150,17 @@ namespace Ryujinx.Graphics.Gpu.Memory
_context.PhysicalMemory.Write(address, data);
}
/// <summary>
/// Invalidates all the buffer data, causing it to be read from guest memory.
/// </summary>
public void Invalidate()
{
_buffer.SetData(0, _context.PhysicalMemory.Read(Address, Size));
}
/// <summary>
/// Disposes the host buffer.
/// </summary>
public void Dispose()
{
_buffer.Dispose();

View file

@ -1,5 +1,8 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Memory range used for buffers.
/// </summary>
struct BufferBounds
{
public ulong Address;

View file

@ -6,6 +6,9 @@ using System;
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Buffer manager.
/// </summary>
class BufferManager
{
private const int OverlapsBufferInitialCapacity = 10;
@ -56,6 +59,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
private bool _rebind;
/// <summary>
/// Creates a new instance of the buffer manager.
/// </summary>
/// <param name="context">The GPU context that the buffer manager belongs to</param>
public BufferManager(GpuContext context)
{
_context = context;
@ -79,6 +86,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Sets the memory range with the index buffer data, to be used for subsequent draw calls.
/// </summary>
/// <param name="gpuVa">Start GPU virtual address of the index buffer</param>
/// <param name="size">Size, in bytes, of the index buffer</param>
/// <param name="type">Type of each index buffer element</param>
public void SetIndexBuffer(ulong gpuVa, ulong size, IndexType type)
{
ulong address = TranslateAndCreateBuffer(gpuVa, size);
@ -90,6 +103,14 @@ namespace Ryujinx.Graphics.Gpu.Memory
_indexBufferDirty = true;
}
/// <summary>
/// Sets the memory range with vertex buffer data, to be used for subsequent draw calls.
/// </summary>
/// <param name="index">Index of the vertex buffer (up to 16)</param>
/// <param name="gpuVa">GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the buffer</param>
/// <param name="stride">Stride of the buffer, defined as the number of bytes of each vertex</param>
/// <param name="divisor">Vertex divisor of the buffer, for instanced draws</param>
public void SetVertexBuffer(int index, ulong gpuVa, ulong size, int stride, int divisor)
{
ulong address = TranslateAndCreateBuffer(gpuVa, size);
@ -111,6 +132,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Sets a storage buffer on the compute pipeline.
/// Storage buffers can be read and written to on shaders.
/// </summary>
/// <param name="index">Index of the storage buffer</param>
/// <param name="gpuVa">Start GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the storage buffer</param>
public void SetComputeStorageBuffer(int index, ulong gpuVa, ulong size)
{
size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
@ -122,6 +150,14 @@ namespace Ryujinx.Graphics.Gpu.Memory
_cpStorageBuffers.Bind(index, address, size);
}
/// <summary>
/// Sets a storage buffer on the graphics pipeline.
/// Storage buffers can be read and written to on shaders.
/// </summary>
/// <param name="stage">Index of the shader stage</param>
/// <param name="index">Index of the storage buffer</param>
/// <param name="gpuVa">Start GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the storage buffer</param>
public void SetGraphicsStorageBuffer(int stage, int index, ulong gpuVa, ulong size)
{
size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
@ -139,6 +175,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
_gpStorageBuffers[stage].Bind(index, address, size);
}
/// <summary>
/// Sets a uniform buffer on the compute pipeline.
/// Uniform buffers are read-only from shaders, and have a small capacity.
/// </summary>
/// <param name="index">Index of the uniform buffer</param>
/// <param name="gpuVa">Start GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the storage buffer</param>
public void SetComputeUniformBuffer(int index, ulong gpuVa, ulong size)
{
ulong address = TranslateAndCreateBuffer(gpuVa, size);
@ -146,6 +189,14 @@ namespace Ryujinx.Graphics.Gpu.Memory
_cpUniformBuffers.Bind(index, address, size);
}
/// <summary>
/// Sets a uniform buffer on the graphics pipeline.
/// Uniform buffers are read-only from shaders, and have a small capacity.
/// </summary>
/// <param name="stage">Index of the shader stage</param>
/// <param name="index">Index of the uniform buffer</param>
/// <param name="gpuVa">Start GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the storage buffer</param>
public void SetGraphicsUniformBuffer(int stage, int index, ulong gpuVa, ulong size)
{
ulong address = TranslateAndCreateBuffer(gpuVa, size);
@ -155,11 +206,22 @@ namespace Ryujinx.Graphics.Gpu.Memory
_gpUniformBuffersDirty = true;
}
/// <summary>
/// Sets the enabled storage buffers mask on the compute pipeline.
/// Each bit set on the mask indicates that the respective buffer index is enabled.
/// </summary>
/// <param name="mask">Buffer enable mask</param>
public void SetComputeStorageBufferEnableMask(uint mask)
{
_cpStorageBuffers.EnableMask = mask;
}
/// <summary>
/// Sets the enabled storage buffers mask on the graphics pipeline.
/// Each bit set on the mask indicates that the respective buffer index is enabled.
/// </summary>
/// <param name="stage">Index of the shader stage</param>
/// <param name="mask">Buffer enable mask</param>
public void SetGraphicsStorageBufferEnableMask(int stage, uint mask)
{
_gpStorageBuffers[stage].EnableMask = mask;
@ -167,11 +229,22 @@ namespace Ryujinx.Graphics.Gpu.Memory
_gpStorageBuffersDirty = true;
}
/// <summary>
/// Sets the enabled uniform buffers mask on the compute pipeline.
/// Each bit set on the mask indicates that the respective buffer index is enabled.
/// </summary>
/// <param name="mask">Buffer enable mask</param>
public void SetComputeUniformBufferEnableMask(uint mask)
{
_cpUniformBuffers.EnableMask = mask;
}
/// <summary>
/// Sets the enabled uniform buffers mask on the graphics pipeline.
/// Each bit set on the mask indicates that the respective buffer index is enabled.
/// </summary>
/// <param name="stage">Index of the shader stage</param>
/// <param name="mask">Buffer enable mask</param>
public void SetGraphicsUniformBufferEnableMask(int stage, uint mask)
{
_gpUniformBuffers[stage].EnableMask = mask;
@ -179,6 +252,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
_gpUniformBuffersDirty = true;
}
/// <summary>
/// Performs address translation of the GPU virtual address, and creates a
/// new buffer, if needed, for the specified range.
/// </summary>
/// <param name="gpuVa">Start GPU virtual address of the buffer</param>
/// <param name="size">Size in bytes of the buffer</param>
/// <returns>CPU virtual address of the buffer, after address translation</returns>
private ulong TranslateAndCreateBuffer(ulong gpuVa, ulong size)
{
if (gpuVa == 0)
@ -210,6 +290,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
return address;
}
/// <summary>
/// Creates a new buffer for the specified range, if needed.
/// If a buffer where this range can be fully contained already exists,
/// then the creation of a new buffer is not necessary.
/// </summary>
/// <param name="address">Address of the buffer in guest memory</param>
/// <param name="size">Size in bytes of the buffer</param>
private void CreateBuffer(ulong address, ulong size)
{
int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
@ -266,6 +353,9 @@ namespace Ryujinx.Graphics.Gpu.Memory
ShrinkOverlapsBufferIfNeeded();
}
/// <summary>
/// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
/// </summary>
private void ShrinkOverlapsBufferIfNeeded()
{
if (_bufferOverlaps.Length > OverlapsBufferMaxCapacity)
@ -274,16 +364,31 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Gets the address of the compute uniform buffer currently bound at the given index.
/// </summary>
/// <param name="index">Index of the uniform buffer binding</param>
/// <returns>The uniform buffer address, or a undefined value if the buffer is not currently bound</returns>
public ulong GetComputeUniformBufferAddress(int index)
{
return _cpUniformBuffers.Buffers[index].Address;
}
/// <summary>
/// Gets the address of the graphics uniform buffer currently bound at the given index.
/// </summary>
/// <param name="stage">Index of the shader stage</param>
/// <param name="index">Index of the uniform buffer binding</param>
/// <returns>The uniform buffer address, or a undefined value if the buffer is not currently bound</returns>
public ulong GetGraphicsUniformBufferAddress(int stage, int index)
{
return _gpUniformBuffers[stage].Buffers[index].Address;
}
/// <summary>
/// Ensures that the compute engine bindings are visible to the host GPU.
/// This actually performs the binding using the host graphics API.
/// </summary>
public void CommitComputeBindings()
{
uint enableMask = _cpStorageBuffers.EnableMask;
@ -332,6 +437,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
_rebind = true;
}
/// <summary>
/// Ensures that the graphics engine bindings are visible to the host GPU.
/// This actually performs the binding using the host graphics API.
/// </summary>
public void CommitBindings()
{
if (_indexBufferDirty || _rebind)
@ -414,16 +523,31 @@ namespace Ryujinx.Graphics.Gpu.Memory
_rebind = false;
}
/// <summary>
/// Bind respective buffer bindings on the host API.
/// </summary>
/// <param name="bindings">Bindings to bind</param>
/// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
private void BindBuffers(BuffersPerStage[] bindings, bool isStorage)
{
BindOrUpdateBuffers(bindings, bind: true, isStorage);
}
/// <summary>
/// Updates data for the already bound buffer bindings.
/// </summary>
/// <param name="bindings">Bindings to update</param>
private void UpdateBuffers(BuffersPerStage[] bindings)
{
BindOrUpdateBuffers(bindings, bind: false);
}
/// <summary>
/// This binds buffer into the host API, or updates data for already bound buffers.
/// </summary>
/// <param name="bindings">Bindings to bind or update</param>
/// <param name="bind">True to bind, false to update</param>
/// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
private void BindOrUpdateBuffers(BuffersPerStage[] bindings, bool bind, bool isStorage = false)
{
for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
@ -461,6 +585,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Binds a buffer on the host API.
/// </summary>
/// <param name="index">Index to bind the buffer into</param>
/// <param name="stage">Shader stage to bind the buffer into</param>
/// <param name="bounds">Buffer address and size</param>
/// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
private void BindBuffer(int index, ShaderStage stage, BufferBounds bounds, bool isStorage)
{
BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
@ -475,6 +606,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Copy a buffer data from a given address to another.
/// This does a GPU side copy.
/// </summary>
/// <param name="srcVa">GPU virtual address of the copy source</param>
/// <param name="dstVa">GPU virtual address of the copy destination</param>
/// <param name="size">Size in bytes of the copy</param>
public void CopyBuffer(GpuVa srcVa, GpuVa dstVa, ulong size)
{
ulong srcAddress = TranslateAndCreateBuffer(srcVa.Pack(), size);
@ -495,11 +633,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
dstBuffer.Flush(dstAddress, size);
}
/// <summary>
/// Gets a buffer sub-range for a given memory range.
/// </summary>
/// <param name="address">Start address of the memory range</param>
/// <param name="size">Size in bytes of the memory range</param>
/// <returns>The buffer sub-range for the given range</returns>
private BufferRange GetBufferRange(ulong address, ulong size)
{
return GetBuffer(address, size).GetRange(address, size);
}
/// <summary>
/// Gets a buffer for a given memory range.
/// A buffer overlapping with the specified range is assumed to already exist on the cache.
/// </summary>
/// <param name="address">Start address of the memory range</param>
/// <param name="size">Size in bytes of the memory range</param>
/// <returns>The buffer where the range is fully contained</returns>
private Buffer GetBuffer(ulong address, ulong size)
{
Buffer buffer;
@ -518,6 +669,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return buffer;
}
/// <summary>
/// Performs guest to host memory synchronization of a given memory range.
/// </summary>
/// <param name="address">Start address of the memory range</param>
/// <param name="size">Size in bytes of the memory range</param>
private void SynchronizeBufferRange(ulong address, ulong size)
{
if (size != 0)

View file

@ -1,5 +1,9 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Range of memory.
/// </summary>
/// <typeparam name="T">GPU resource type</typeparam>
interface IRange<T>
{
ulong Address { get; }

View file

@ -2,6 +2,9 @@ using Ryujinx.Graphics.GAL;
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// GPU Index Buffer information.
/// </summary>
struct IndexBuffer
{
public ulong Address;

View file

@ -3,15 +3,29 @@ using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// GPU mapped memory accessor.
/// </summary>
class MemoryAccessor
{
private GpuContext _context;
/// <summary>
/// Creates a new instance of the GPU memory accessor.
/// </summary>
/// <param name="context">GPU context that the memory accessor belongs to</param>
public MemoryAccessor(GpuContext context)
{
_context = context;
}
/// <summary>
/// Reads data from GPU mapped memory.
/// This reads as much data as possible, up to the specified maximum size.
/// </summary>
/// <param name="gpuVa">GPU virtual address where the data is located</param>
/// <param name="maxSize">Maximum size of the data</param>
/// <returns>The data at the specified memory location</returns>
public Span<byte> Read(ulong gpuVa, ulong maxSize)
{
ulong processVa = _context.MemoryManager.Translate(gpuVa);
@ -21,6 +35,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
return _context.PhysicalMemory.Read(processVa, size);
}
/// <summary>
/// Reads a structure from GPU mapped memory.
/// </summary>
/// <typeparam name="T">Type of the structure</typeparam>
/// <param name="gpuVa">GPU virtual address where the strcture is located</param>
/// <returns>The structure at the specified memory location</returns>
public T Read<T>(ulong gpuVa) where T : struct
{
ulong processVa = _context.MemoryManager.Translate(gpuVa);
@ -30,6 +50,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.Read(processVa, size))[0];
}
/// <summary>
/// Reads a 32-bits signed integer from GPU mapped memory.
/// </summary>
/// <param name="gpuVa">GPU virtual address where the value is located</param>
/// <returns>The value at the specified memory location</returns>
public int ReadInt32(ulong gpuVa)
{
ulong processVa = _context.MemoryManager.Translate(gpuVa);
@ -37,6 +62,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return BitConverter.ToInt32(_context.PhysicalMemory.Read(processVa, 4));
}
/// <summary>
/// Writes a 32-bits signed integer to GPU mapped memory.
/// </summary>
/// <param name="gpuVa">GPU virtual address to write the value into</param>
/// <param name="value">The value to be written</param>
public void Write(ulong gpuVa, int value)
{
ulong processVa = _context.MemoryManager.Translate(gpuVa);
@ -44,6 +74,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
_context.PhysicalMemory.Write(processVa, BitConverter.GetBytes(value));
}
/// <summary>
/// Writes data to GPU mapped memory.
/// </summary>
/// <param name="gpuVa">GPU virtual address to write the data into</param>
/// <param name="data">The data to be written</param>
public void Write(ulong gpuVa, Span<byte> data)
{
ulong processVa = _context.MemoryManager.Translate(gpuVa);

View file

@ -1,5 +1,8 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// GPU memory manager.
/// </summary>
public class MemoryManager
{
private const ulong AddressSpaceSize = 1UL << 40;
@ -26,11 +29,22 @@ namespace Ryujinx.Graphics.Gpu.Memory
private ulong[][] _pageTable;
/// <summary>
/// Creates a new instance of the GPU memory manager.
/// </summary>
public MemoryManager()
{
_pageTable = new ulong[PtLvl0Size][];
}
/// <summary>
/// Maps a given range of pages to the specified CPU virtual address.
/// All addresses and sizes must be page aligned.
/// </summary>
/// <param name="pa">CPU virtual address to map into</param>
/// <param name="va">GPU virtual address to be mapped</param>
/// <param name="size">Size in bytes of the mapping</param>
/// <returns>The GPU virtual address of the mapping</returns>
public ulong Map(ulong pa, ulong va, ulong size)
{
lock (_pageTable)
@ -44,6 +58,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
return va;
}
/// <summary>
/// Maps a given range of pages to a allocated GPU virtual address.
/// The memory is automatically allocated by the memory manager.
/// </summary>
/// <param name="pa">CPU virtual address to map into</param>
/// <param name="size">Mapping size in bytes</param>
/// <returns>GPU virtual address where the range was mapped, or an all ones mask in case of failure</returns>
public ulong Map(ulong pa, ulong size)
{
lock (_pageTable)
@ -62,6 +83,14 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Maps a given range of pages to a allocated GPU virtual address.
/// The memory is automatically allocated by the memory manager.
/// This also ensures that the mapping is always done in the first 4GB of GPU address space.
/// </summary>
/// <param name="pa">CPU virtual address to map into</param>
/// <param name="size">Mapping size in bytes</param>
/// <returns>GPU virtual address where the range was mapped, or an all ones mask in case of failure</returns>
public ulong MapLow(ulong pa, ulong size)
{
lock (_pageTable)
@ -84,6 +113,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Reserves memory at a fixed GPU memory location.
/// This prevents the reserved region from being used for memory allocation for map.
/// </summary>
/// <param name="va">CPU virtual address to reserve</param>
/// <param name="size">Reservation size in bytes</param>
/// <returns>GPU virtual address of the reservation, or an all ones mask in case of failure</returns>
public ulong ReserveFixed(ulong va, ulong size)
{
lock (_pageTable)
@ -105,6 +141,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
return va;
}
/// <summary>
/// Reserves memory at any GPU memory location.
/// </summary>
/// <param name="size">Reservation size in bytes</param>
/// <param name="alignment">Reservation address alignment in bytes</param>
/// <returns>GPU virtual address of the reservation, or an all ones mask in case of failure</returns>
public ulong Reserve(ulong size, ulong alignment)
{
lock (_pageTable)
@ -123,6 +165,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Frees memory that was previously allocated by a map or reserved.
/// </summary>
/// <param name="va">GPU virtual address to free</param>
/// <param name="size">Size in bytes of the region being freed</param>
public void Free(ulong va, ulong size)
{
lock (_pageTable)
@ -134,6 +181,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Gets the address of a unused (free) region of the specified size.
/// </summary>
/// <param name="size">Size of the region in bytes</param>
/// <param name="alignment">Required alignment of the region address in bytes</param>
/// <param name="start">Start address of the search on the address space</param>
/// <returns>GPU virtual address of the allocation, or an all ones mask in case of failure</returns>
private ulong GetFreePosition(ulong size, ulong alignment = 1, ulong start = 1UL << 32)
{
// Note: Address 0 is not considered valid by the driver,
@ -176,6 +230,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return PteUnmapped;
}
/// <summary>
/// Gets the number of mapped or reserved pages on a given region.
/// </summary>
/// <param name="gpuVa">Start GPU virtual address of the region</param>
/// <returns>Mapped size in bytes of the specified region</returns>
internal ulong GetSubSize(ulong gpuVa)
{
ulong size = 0;
@ -188,6 +247,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return size;
}
/// <summary>
/// Translated a GPU virtual address to a CPU virtual address.
/// </summary>
/// <param name="gpuVa">GPU virtual address to be translated</param>
/// <returns>CPU virtual address</returns>
internal ulong Translate(ulong gpuVa)
{
ulong baseAddress = GetPte(gpuVa);
@ -200,11 +264,17 @@ namespace Ryujinx.Graphics.Gpu.Memory
return baseAddress + (gpuVa & PageMask);
}
public bool IsRegionFree(ulong va, ulong size)
/// <summary>
/// Checks if a given memory region is currently unmapped.
/// </summary>
/// <param name="gpuVa">Start GPU virtual address of the region</param>
/// <param name="size">Size in bytes of the region</param>
/// <returns>True if the region is unmapped (free), false otherwise</returns>
public bool IsRegionFree(ulong gpuVa, ulong size)
{
for (ulong offset = 0; offset < size; offset += PageSize)
{
if (IsPageInUse(va + offset))
if (IsPageInUse(gpuVa + offset))
{
return false;
}
@ -213,15 +283,20 @@ namespace Ryujinx.Graphics.Gpu.Memory
return true;
}
private bool IsPageInUse(ulong va)
/// <summary>
/// Checks if a given memory page is mapped or reserved.
/// </summary>
/// <param name="gpuVa">GPU virtual address of the page</param>
/// <returns>True if the page is mapped or reserved, false otherwise</returns>
private bool IsPageInUse(ulong gpuVa)
{
if (va >> PtLvl0Bits + PtLvl1Bits + PtPageBits != 0)
if (gpuVa >> PtLvl0Bits + PtLvl1Bits + PtPageBits != 0)
{
return false;
}
ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
if (_pageTable[l0] == null)
{
@ -231,10 +306,15 @@ namespace Ryujinx.Graphics.Gpu.Memory
return _pageTable[l0][l1] != PteUnmapped;
}
private ulong GetPte(ulong address)
/// <summary>
/// Gets the Page Table entry for a given GPU virtual address.
/// </summary>
/// <param name="gpuVa">GPU virtual address</param>
/// <returns>Page table entry (CPU virtual address)</returns>
private ulong GetPte(ulong gpuVa)
{
ulong l0 = (address >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (address >> PtLvl1Bit) & PtLvl1Mask;
ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
if (_pageTable[l0] == null)
{
@ -244,10 +324,15 @@ namespace Ryujinx.Graphics.Gpu.Memory
return _pageTable[l0][l1];
}
private void SetPte(ulong address, ulong tgtAddr)
/// <summary>
/// Sets a Page Table entry at a given GPU virtual address.
/// </summary>
/// <param name="gpuVa">GPU virtual address</param>
/// <param name="pte">Page table entry (CPU virtual address)</param>
private void SetPte(ulong gpuVa, ulong pte)
{
ulong l0 = (address >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (address >> PtLvl1Bit) & PtLvl1Mask;
ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
if (_pageTable[l0] == null)
{
@ -259,7 +344,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
_pageTable[l0][l1] = tgtAddr;
_pageTable[l0][l1] = pte;
}
}
}

View file

@ -4,25 +4,51 @@ namespace Ryujinx.Graphics.Gpu.Memory
{
using CpuMemoryManager = ARMeilleure.Memory.MemoryManager;
/// <summary>
/// Represents physical memory, accessible from the GPU.
/// This is actually working CPU virtual addresses, of memory mapped on the game process.
/// </summary>
class PhysicalMemory
{
private readonly CpuMemoryManager _cpuMemory;
/// <summary>
/// Creates a new instance of the physical memory.
/// </summary>
/// <param name="cpuMemory">CPU memory manager of the application process</param>
public PhysicalMemory(CpuMemoryManager cpuMemory)
{
_cpuMemory = cpuMemory;
}
/// <summary>
/// Reads data from the application process.
/// </summary>
/// <param name="address">Address to be read</param>
/// <param name="size">Size in bytes to be read</param>
/// <returns>The data at the specified memory location</returns>
public Span<byte> Read(ulong address, ulong size)
{
return _cpuMemory.ReadBytes((long)address, (long)size);
}
/// <summary>
/// Writes data to the application process.
/// </summary>
/// <param name="address">Address to write into</param>
/// <param name="data">Data to be written</param>
public void Write(ulong address, Span<byte> data)
{
_cpuMemory.WriteBytes((long)address, data.ToArray());
}
/// <summary>
/// Gets the modified ranges for a given range of the application process mapped memory.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <param name="size">Size, in bytes, of the range</param>
/// <param name="name">Name of the GPU resource being checked</param>
/// <returns>Ranges, composed of address and size, modified by the application process, form the CPU</returns>
public (ulong, ulong)[] GetModifiedRanges(ulong address, ulong size, ResourceName name)
{
return _cpuMemory.GetModifiedRanges(address, size, (int)name);

View file

@ -3,17 +3,28 @@ using System.Collections.Generic;
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Lists of GPU resources with data on guest memory.
/// </summary>
/// <typeparam name="T">Type of the GPU resource</typeparam>
class RangeList<T> where T : IRange<T>
{
private const int ArrayGrowthSize = 32;
private List<T> _items;
/// <summary>
/// Creates a new GPU resources list.
/// </summary>
public RangeList()
{
_items = new List<T>();
}
/// <summary>
/// Adds a new item to the list.
/// </summary>
/// <param name="item">The item to be added</param>
public void Add(T item)
{
int index = BinarySearch(item.Address);
@ -26,6 +37,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
_items.Insert(index, item);
}
/// <summary>
/// Removes a item from the list.
/// </summary>
/// <param name="item">The item to be removed</param>
/// <returns>True if the item was removed, or false if it was not found</returns>
public bool Remove(T item)
{
int index = BinarySearch(item.Address);
@ -58,11 +74,26 @@ namespace Ryujinx.Graphics.Gpu.Memory
return false;
}
/// <summary>
/// Gets the first item on the list overlapping in memory with the specified item.
/// Despite the name, this has no ordering guarantees of the returned item.
/// It only ensures that the item returned overlaps the specified item.
/// </summary>
/// <param name="item">Item to check for overlaps</param>
/// <returns>The overlapping item, or the default value for the type if none found</returns>
public T FindFirstOverlap(T item)
{
return FindFirstOverlap(item.Address, item.Size);
}
/// <summary>
/// Gets the first item on the list overlapping the specified memory range.
/// Despite the name, this has no ordering guarantees of the returned item.
/// It only ensures that the item returned overlaps the specified memory range.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <param name="size">Size in bytes or the rangee</param>
/// <returns>The overlapping item, or the default value for the type if none found</returns>
public T FindFirstOverlap(ulong address, ulong size)
{
int index = BinarySearch(address, size);
@ -75,11 +106,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
return _items[index];
}
/// <summary>
/// Gets all items overlapping with the specified item in memory.
/// </summary>
/// <param name="item">Item to check for overlaps</param>
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
/// <returns>The number of overlapping items found</returns>
public int FindOverlaps(T item, ref T[] output)
{
return FindOverlaps(item.Address, item.Size, ref output);
}
/// <summary>
/// Gets all items on the list overlapping the specified memory range.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <param name="size">Size in bytes or the rangee</param>
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
/// <returns>The number of overlapping items found</returns>
public int FindOverlaps(ulong address, ulong size, ref T[] output)
{
int outputIndex = 0;
@ -110,19 +154,36 @@ namespace Ryujinx.Graphics.Gpu.Memory
return outputIndex;
}
/// <summary>
/// Gets all items overlapping with the specified item in memory.
/// This method only returns correct results if none of the items on the list overlaps with
/// each other. If that is not the case, this method should not be used.
/// This method is faster than the regular method to find all overlaps.
/// </summary>
/// <param name="item">Item to check for overlaps</param>
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
/// <returns>The number of overlapping items found</returns>
public int FindOverlapsNonOverlapping(T item, ref T[] output)
{
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
}
/// <summary>
/// Gets all items on the list overlapping the specified memory range.
/// This method only returns correct results if none of the items on the list overlaps with
/// each other. If that is not the case, this method should not be used.
/// This method is faster than the regular method to find all overlaps.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <param name="size">Size in bytes or the rangee</param>
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
/// <returns>The number of overlapping items found</returns>
public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
{
// This is a bit faster than FindOverlaps, but only works
// when none of the items on the list overlaps with each other.
int outputIndex = 0;
ulong endAddress = address + size;
int index = BinarySearch(address, size);
if (index >= 0)
@ -147,6 +208,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
return outputIndex;
}
/// <summary>
/// Gets all items on the list with the specified memory address.
/// </summary>
/// <param name="address">Address to find</param>
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
/// <returns>The number of matches found</returns>
public int FindOverlaps(ulong address, ref T[] output)
{
int index = BinarySearch(address);
@ -181,6 +248,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return outputIndex;
}
/// <summary>
/// Performs binary search on the internal list of items.
/// </summary>
/// <param name="address">Address to find</param>
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
private int BinarySearch(ulong address)
{
int left = 0;
@ -212,6 +284,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
return ~left;
}
/// <summary>
/// Performs binary search for items overlapping a given memory range.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <param name="size">Size of the range in bytes</param>
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
private int BinarySearch(ulong address, ulong size)
{
int left = 0;

View file

@ -1,5 +1,8 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Name of a GPU resource.
/// </summary>
public enum ResourceName
{
Buffer,

View file

@ -1,5 +1,8 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// GPU Vertex Buffer information.
/// </summary>
struct VertexBuffer
{
public ulong Address;