using Ryujinx.Graphics.GAL;
using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Gpu.Memory
{
///
/// Buffer, used to store vertex and index data, uniform and storage buffers, and others.
///
class MultiRangeBuffer : IMultiRangeItem, IDisposable
{
private readonly GpuContext _context;
///
/// Host buffer handle.
///
public BufferHandle Handle { get; }
///
/// Range of memory where the data is located.
///
public MultiRange Range { get; }
///
/// Ever increasing counter value indicating when the buffer was modified relative to other buffers.
///
public int ModificationSequenceNumber { get; private set; }
///
/// Physical buffer dependency entry.
///
private readonly struct PhysicalDependency
{
///
/// Physical buffer.
///
public readonly Buffer PhysicalBuffer;
///
/// Offset of the range on the physical buffer.
///
public readonly ulong PhysicalOffset;
///
/// Offset of the range on the virtual buffer.
///
public readonly ulong VirtualOffset;
///
/// Size of the range.
///
public readonly ulong Size;
///
/// Creates a new physical dependency.
///
/// Physical buffer
/// Offset of the range on the physical buffer
/// Offset of the range on the virtual buffer
/// Size of the range
public PhysicalDependency(Buffer physicalBuffer, ulong physicalOffset, ulong virtualOffset, ulong size)
{
PhysicalBuffer = physicalBuffer;
PhysicalOffset = physicalOffset;
VirtualOffset = virtualOffset;
Size = size;
}
}
private List _dependencies;
private BufferModifiedRangeList _modifiedRanges = null;
///
/// Creates a new instance of the buffer.
///
/// GPU context that the buffer belongs to
/// Range of memory where the data is mapped
public MultiRangeBuffer(GpuContext context, MultiRange range)
{
_context = context;
Range = range;
Handle = context.Renderer.CreateBuffer((int)range.GetSize());
}
///
/// Creates a new instance of the buffer.
///
/// GPU context that the buffer belongs to
/// Range of memory where the data is mapped
/// Backing memory for the buffer
public MultiRangeBuffer(GpuContext context, MultiRange range, ReadOnlySpan storages)
{
_context = context;
Range = range;
Handle = context.Renderer.CreateBufferSparse(storages);
}
///
/// Gets a sub-range from the buffer.
///
///
/// This can be used to bind and use sub-ranges of the buffer on the host API.
///
/// Range of memory where the data is mapped
/// The buffer sub-range
public BufferRange GetRange(MultiRange range)
{
int offset = Range.FindOffset(range);
return new BufferRange(Handle, offset, (int)range.GetSize());
}
///
/// Removes all physical buffer dependencies.
///
public void ClearPhysicalDependencies()
{
_dependencies?.Clear();
}
///
/// Adds a physical buffer dependency.
///
/// Physical buffer to be added
/// Address inside the physical buffer where the virtual buffer range is located
/// Offset inside the virtual buffer where the physical range is located
/// Size of the range in bytes
public void AddPhysicalDependency(Buffer buffer, ulong rangeAddress, ulong dstOffset, ulong rangeSize)
{
(_dependencies ??= new()).Add(new(buffer, rangeAddress - buffer.Address, dstOffset, rangeSize));
buffer.AddVirtualDependency(this);
}
///
/// Tries to get the physical range corresponding to the given physical buffer.
///
/// Physical buffer
/// Minimum virtual offset that a range match can have
/// Physical offset of the match
/// Virtual offset of the match, always greater than or equal
/// Size of the range match
/// True if a match was found for the given parameters, false otherwise
public bool TryGetPhysicalOffset(Buffer buffer, ulong minimumVirtOffset, out ulong physicalOffset, out ulong virtualOffset, out ulong size)
{
physicalOffset = 0;
virtualOffset = 0;
size = 0;
if (_dependencies != null)
{
foreach (var dependency in _dependencies)
{
if (dependency.PhysicalBuffer == buffer && dependency.VirtualOffset >= minimumVirtOffset)
{
physicalOffset = dependency.PhysicalOffset;
virtualOffset = dependency.VirtualOffset;
size = dependency.Size;
return true;
}
}
}
return false;
}
///
/// Adds a modified virtual memory range.
///
///
/// This is only required when the host does not support sparse buffers, otherwise only physical buffers need to track modification.
///
/// Modified range
/// ModificationSequenceNumber
public void AddModifiedRegion(MultiRange range, int modifiedSequenceNumber)
{
_modifiedRanges ??= new(_context, null, null);
for (int i = 0; i < range.Count; i++)
{
MemoryRange subRange = range.GetSubRange(i);
_modifiedRanges.SignalModified(subRange.Address, subRange.Size);
}
ModificationSequenceNumber = modifiedSequenceNumber;
}
///
/// Calls the specified for all modified ranges that overlaps with .
///
/// Buffer to have its range checked
/// Action to perform for modified ranges
public void ConsumeModifiedRegion(Buffer buffer, Action rangeAction)
{
ConsumeModifiedRegion(buffer.Address, buffer.Size, rangeAction);
}
///
/// Calls the specified for all modified ranges that overlaps with and .
///
/// Address of the region to consume
/// Size of the region to consume
/// Action to perform for modified ranges
public void ConsumeModifiedRegion(ulong address, ulong size, Action rangeAction)
{
if (_modifiedRanges != null)
{
_modifiedRanges.GetRanges(address, size, rangeAction);
_modifiedRanges.Clear(address, size);
}
}
///
/// Gets data from the specified region of the buffer, and places it on .
///
/// Span to put the data into
/// Offset of the buffer to get the data from
/// Size of the data in bytes
public void GetData(Span output, int offset, int size)
{
using PinnedSpan data = _context.Renderer.GetBufferData(Handle, offset, size);
data.Get().CopyTo(output);
}
///
/// Disposes the host buffer.
///
public void Dispose()
{
if (_dependencies != null)
{
foreach (var dependency in _dependencies)
{
dependency.PhysicalBuffer.RemoveVirtualDependency(this);
}
_dependencies = null;
}
_context.Renderer.DeleteBuffer(Handle);
}
}
}