Audio rendering: reduce memory allocations (#6604)

* - WritableRegion: enable wrapping IMemoryOwner<byte>
- IVirtualMemoryManager impls of GetWritableRegion() use pooled memory when region is non-contiguous.
- IVirtualMemoryManager: add GetReadOnlySequence() and impls
- ByteMemoryPool: add new method RentCopy()
- ByteMemoryPool: make class static, remove ctor and singleton field from earlier impl

* - BytesReadOnlySequenceSegment: move from Ryujinx.Common.Memory to Ryujinx.Memory
- BytesReadOnlySequenceSegment: add IsContiguousWith() and Replace() methods
- VirtualMemoryManagerBase:
  - remove generic type parameters, instead use ulong for virtual addresses and nuint for host/physical addresses
  - implement IWritableBlock
  - add virtual GetReadOnlySequence() with coalescing of contiguous segments
  - add virtual GetSpan()
  - add virtual GetWritableRegion()
  - add abstract IsMapped()
  - add virtual MapForeign(ulong, nuint, ulong)
  - add virtual Read<T>()
  - add virtual Read(ulong, Span<byte>)
  - add virtual ReadTracked<T>()
  - add virtual SignalMemoryTracking()
  - add virtual Write()
  - add virtual Write<T>()
  - add virtual WriteUntracked()
  - add virtual WriteWithRedundancyCheck()
- VirtualMemoryManagerRefCountedBase: remove generic type parameters
- AddressSpaceManager: remove redundant methods, add required overrides
- HvMemoryManager: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling
- MemoryManager: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling
- MemoryManagerHostMapped: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling
- NativeMemoryManager: add get properties for Pointer and Length
- throughout: removed invalid <inheritdoc/> comments

* - WritableRegion: enable wrapping IMemoryOwner<byte>
- IVirtualMemoryManager impls of GetWritableRegion() use pooled memory when region is non-contiguous.
- IVirtualMemoryManager: add GetReadOnlySequence() and impls
- ByteMemoryPool: add new method RentCopy()
- ByteMemoryPool: make class static, remove ctor and singleton field from earlier impl

* add PagedMemoryRange enumerator types, use them in IVirtualMemoryManager implementations to consolidate page-handling logic and add a new capability - the coalescing of pages for consolidating memory copies and segmentation.

* new: more tests for PagedMemoryRangeCoalescingEnumerator showing coalescing of contiguous segments

* make some struct properties readonly

* put braces around `foreach` bodies

* encourage inlining of some PagedMemoryRange*Enumerator members

* DynamicRingBuffer:
 - use ByteMemoryPool
 - make some methods return without locking when size/count argument = 0
 - make generic Read<T>()/Write<T>() non-generic because its only usage is as T = byte
 - change Read(byte[]...) to Read(Span<byte>...)
 - change Write(byte[]...) to Write(Span<byte>...)

* change IAudioRenderer.RequestUpdate() to take a ReadOnlySequence<byte>, enabling zero-copy audio rendering

* HipcGenerator: support ReadOnlySequence<byte> as IPC method parameter

* change IAudioRenderer/AudioRenderer RequestUpdate* methods to take input as ReadOnlySequence<byte>

* MemoryManagerHostTracked: use rented memory when contiguous in `GetWritableRegion()`

* rebase cleanup

* dotnet format fixes

* format and comment fixes

* format long parameter list - take 2

* - add support to HipcGenerator for buffers of type `Memory<byte>`
- change `AudioRenderer` `RequestUpdate()` and `RequestUpdateAuto()` to use Memory<byte> for output buffers, removing another memory block allocation/copy

* SplitterContext `UpdateState()` and `UpdateData()` smooth out advance/rewind logic, only rewind if magic is invalid

* DynamicRingBuffer.Write(): change Span<byte> to ReadOnlySpan<byte>
This commit is contained in:
jhorv 2024-04-07 17:07:32 -04:00 committed by GitHub
parent 3e0d67533f
commit ead9a25141
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 847 additions and 262 deletions

View file

@ -17,6 +17,8 @@ namespace Ryujinx.Horizon.Generators.Hipc
private const string ResponseVariableName = "response";
private const string OutRawDataVariableName = "outRawData";
private const string TypeSystemBuffersReadOnlySequence = "System.Buffers.ReadOnlySequence";
private const string TypeSystemMemory = "System.Memory";
private const string TypeSystemReadOnlySpan = "System.ReadOnlySpan";
private const string TypeSystemSpan = "System.Span";
private const string TypeStructLayoutAttribute = "System.Runtime.InteropServices.StructLayoutAttribute";
@ -329,7 +331,15 @@ namespace Ryujinx.Horizon.Generators.Hipc
value = $"{InObjectsVariableName}[{inObjectIndex++}]";
break;
case CommandArgType.Buffer:
if (IsReadOnlySpan(compilation, parameter))
if (IsMemory(compilation, parameter))
{
value = $"CommandSerialization.GetWritableRegion(processor.GetBufferRange({index}))";
}
else if (IsReadOnlySequence(compilation, parameter))
{
value = $"CommandSerialization.GetReadOnlySequence(processor.GetBufferRange({index}))";
}
else if (IsReadOnlySpan(compilation, parameter))
{
string spanGenericTypeName = GetCanonicalTypeNameOfGenericArgument(compilation, parameter.Type, 0);
value = GenerateSpanCast(spanGenericTypeName, $"CommandSerialization.GetReadOnlySpan(processor.GetBufferRange({index}))");
@ -346,7 +356,13 @@ namespace Ryujinx.Horizon.Generators.Hipc
break;
}
if (IsSpan(compilation, parameter))
if (IsMemory(compilation, parameter))
{
generator.AppendLine($"using var {argName} = {value};");
argName = $"{argName}.Memory";
}
else if (IsSpan(compilation, parameter))
{
generator.AppendLine($"using var {argName} = {value};");
@ -637,7 +653,9 @@ namespace Ryujinx.Horizon.Generators.Hipc
private static bool IsValidTypeForBuffer(Compilation compilation, ParameterSyntax parameter)
{
return IsReadOnlySpan(compilation, parameter) ||
return IsMemory(compilation, parameter) ||
IsReadOnlySequence(compilation, parameter) ||
IsReadOnlySpan(compilation, parameter) ||
IsSpan(compilation, parameter) ||
IsUnmanagedType(compilation, parameter.Type);
}
@ -649,6 +667,16 @@ namespace Ryujinx.Horizon.Generators.Hipc
return typeInfo.Type.IsUnmanagedType;
}
private static bool IsMemory(Compilation compilation, ParameterSyntax parameter)
{
return GetCanonicalTypeName(compilation, parameter.Type) == TypeSystemMemory;
}
private static bool IsReadOnlySequence(Compilation compilation, ParameterSyntax parameter)
{
return GetCanonicalTypeName(compilation, parameter.Type) == TypeSystemBuffersReadOnlySequence;
}
private static bool IsReadOnlySpan(Compilation compilation, ParameterSyntax parameter)
{
return GetCanonicalTypeName(compilation, parameter.Type) == TypeSystemReadOnlySpan;