From cee712105850ac3385cd0091a923438167433f9f Mon Sep 17 00:00:00 2001
From: TSR Berry <20988865+TSRBerry@users.noreply.github.com>
Date: Sat, 8 Apr 2023 01:22:00 +0200
Subject: Move solution and projects to src
---
.../Renderer/Server/Splitter/SplitterContext.cs | 303 +++++++++++++++++++++
.../Server/Splitter/SplitterDestination.cs | 193 +++++++++++++
.../Renderer/Server/Splitter/SplitterState.cs | 221 +++++++++++++++
3 files changed, 717 insertions(+)
create mode 100644 src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterContext.cs
create mode 100644 src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterDestination.cs
create mode 100644 src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs
(limited to 'src/Ryujinx.Audio/Renderer/Server/Splitter')
diff --git a/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterContext.cs b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterContext.cs
new file mode 100644
index 00000000..91877cdd
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterContext.cs
@@ -0,0 +1,303 @@
+using Ryujinx.Audio.Renderer.Common;
+using Ryujinx.Audio.Renderer.Parameter;
+using Ryujinx.Audio.Renderer.Utils;
+using Ryujinx.Common;
+using System;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Audio.Renderer.Server.Splitter
+{
+ ///
+ /// Splitter context.
+ ///
+ public class SplitterContext
+ {
+ ///
+ /// Storage for .
+ ///
+ private Memory _splitters;
+
+ ///
+ /// Storage for .
+ ///
+ private Memory _splitterDestinations;
+
+ ///
+ /// If set to true, trust the user destination count in .
+ ///
+ public bool IsBugFixed { get; private set; }
+
+ ///
+ /// Initialize .
+ ///
+ /// The behaviour context.
+ /// The audio renderer configuration.
+ /// The .
+ /// Return true if the initialization was successful.
+ public bool Initialize(ref BehaviourContext behaviourContext, ref AudioRendererConfiguration parameter, WorkBufferAllocator workBufferAllocator)
+ {
+ if (!behaviourContext.IsSplitterSupported() || parameter.SplitterCount <= 0 || parameter.SplitterDestinationCount <= 0)
+ {
+ Setup(Memory.Empty, Memory.Empty, false);
+
+ return true;
+ }
+
+ Memory splitters = workBufferAllocator.Allocate(parameter.SplitterCount, SplitterState.Alignment);
+
+ if (splitters.IsEmpty)
+ {
+ return false;
+ }
+
+ int splitterId = 0;
+
+ foreach (ref SplitterState splitter in splitters.Span)
+ {
+ splitter = new SplitterState(splitterId++);
+ }
+
+ Memory splitterDestinations = workBufferAllocator.Allocate(parameter.SplitterDestinationCount,
+ SplitterDestination.Alignment);
+
+ if (splitterDestinations.IsEmpty)
+ {
+ return false;
+ }
+
+ int splitterDestinationId = 0;
+ foreach (ref SplitterDestination data in splitterDestinations.Span)
+ {
+ data = new SplitterDestination(splitterDestinationId++);
+ }
+
+ SplitterState.InitializeSplitters(splitters.Span);
+
+ Setup(splitters, splitterDestinations, behaviourContext.IsSplitterBugFixed());
+
+ return true;
+ }
+
+ ///
+ /// Get the work buffer size while adding the size needed for splitter to operate.
+ ///
+ /// The current size.
+ /// The behaviour context.
+ /// The renderer configuration.
+ /// Return the new size taking splitter into account.
+ public static ulong GetWorkBufferSize(ulong size, ref BehaviourContext behaviourContext, ref AudioRendererConfiguration parameter)
+ {
+ if (behaviourContext.IsSplitterSupported())
+ {
+ size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterCount, SplitterState.Alignment);
+ size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterDestinationCount, SplitterDestination.Alignment);
+
+ if (behaviourContext.IsSplitterBugFixed())
+ {
+ size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterDestinationCount, 0x10);
+ }
+
+ return size;
+ }
+ else
+ {
+ return size;
+ }
+ }
+
+ ///
+ /// Setup the instance.
+ ///
+ /// The storage.
+ /// The storage.
+ /// If set to true, trust the user destination count in .
+ private void Setup(Memory splitters, Memory splitterDestinations, bool isBugFixed)
+ {
+ _splitters = splitters;
+ _splitterDestinations = splitterDestinations;
+ IsBugFixed = isBugFixed;
+ }
+
+ ///
+ /// Clear the new connection flag.
+ ///
+ private void ClearAllNewConnectionFlag()
+ {
+ foreach (ref SplitterState splitter in _splitters.Span)
+ {
+ splitter.ClearNewConnectionFlag();
+ }
+ }
+
+ ///
+ /// Get the destination count using the count of splitter.
+ ///
+ /// The destination count using the count of splitter.
+ public int GetDestinationCountPerStateForCompatibility()
+ {
+ if (_splitters.IsEmpty)
+ {
+ return 0;
+ }
+
+ return _splitterDestinations.Length / _splitters.Length;
+ }
+
+ ///
+ /// Update one or multiple from user parameters.
+ ///
+ /// The splitter header.
+ /// The raw data after the splitter header.
+ private void UpdateState(scoped ref SplitterInParameterHeader inputHeader, ref ReadOnlySpan input)
+ {
+ for (int i = 0; i < inputHeader.SplitterCount; i++)
+ {
+ SplitterInParameter parameter = MemoryMarshal.Read(input);
+
+ Debug.Assert(parameter.IsMagicValid());
+
+ if (parameter.IsMagicValid())
+ {
+ if (parameter.Id >= 0 && parameter.Id < _splitters.Length)
+ {
+ ref SplitterState splitter = ref GetState(parameter.Id);
+
+ splitter.Update(this, ref parameter, input.Slice(Unsafe.SizeOf()));
+ }
+
+ input = input.Slice(0x1C + (int)parameter.DestinationCount * 4);
+ }
+ }
+ }
+
+ ///
+ /// Update one or multiple from user parameters.
+ ///
+ /// The splitter header.
+ /// The raw data after the splitter header.
+ private void UpdateData(scoped ref SplitterInParameterHeader inputHeader, ref ReadOnlySpan input)
+ {
+ for (int i = 0; i < inputHeader.SplitterDestinationCount; i++)
+ {
+ SplitterDestinationInParameter parameter = MemoryMarshal.Read(input);
+
+ Debug.Assert(parameter.IsMagicValid());
+
+ if (parameter.IsMagicValid())
+ {
+ if (parameter.Id >= 0 && parameter.Id < _splitterDestinations.Length)
+ {
+ ref SplitterDestination destination = ref GetDestination(parameter.Id);
+
+ destination.Update(parameter);
+ }
+
+ input = input.Slice(Unsafe.SizeOf());
+ }
+ }
+ }
+
+ ///
+ /// Update splitter from user parameters.
+ ///
+ /// The input raw user data.
+ /// The total consumed size.
+ /// Return true if the update was successful.
+ public bool Update(ReadOnlySpan input, out int consumedSize)
+ {
+ if (_splitterDestinations.IsEmpty || _splitters.IsEmpty)
+ {
+ consumedSize = 0;
+
+ return true;
+ }
+
+ int originalSize = input.Length;
+
+ SplitterInParameterHeader header = SpanIOHelper.Read(ref input);
+
+ if (header.IsMagicValid())
+ {
+ ClearAllNewConnectionFlag();
+
+ UpdateState(ref header, ref input);
+ UpdateData(ref header, ref input);
+
+ consumedSize = BitUtils.AlignUp(originalSize - input.Length, 0x10);
+
+ return true;
+ }
+ else
+ {
+ consumedSize = 0;
+
+ return false;
+ }
+ }
+
+ ///
+ /// Get a reference to a at the given .
+ ///
+ /// The index to use.
+ /// A reference to a at the given .
+ public ref SplitterState GetState(int id)
+ {
+ return ref SpanIOHelper.GetFromMemory(_splitters, id, (uint)_splitters.Length);
+ }
+
+ ///
+ /// Get a reference to a at the given .
+ ///
+ /// The index to use.
+ /// A reference to a at the given .
+ public ref SplitterDestination GetDestination(int id)
+ {
+ return ref SpanIOHelper.GetFromMemory(_splitterDestinations, id, (uint)_splitterDestinations.Length);
+ }
+
+ ///
+ /// Get a at the given .
+ ///
+ /// The index to use.
+ /// A at the given .
+ public Memory GetDestinationMemory(int id)
+ {
+ return SpanIOHelper.GetMemory(_splitterDestinations, id, (uint)_splitterDestinations.Length);
+ }
+
+ ///
+ /// Get a in the at and pass to .
+ ///
+ /// The index to use to get the .
+ /// The index of the .
+ /// A .
+ public Span GetDestination(int id, int destinationId)
+ {
+ ref SplitterState splitter = ref GetState(id);
+
+ return splitter.GetData(destinationId);
+ }
+
+ ///
+ /// Return true if the audio renderer has any splitters.
+ ///
+ /// True if the audio renderer has any splitters.
+ public bool UsingSplitter()
+ {
+ return !_splitters.IsEmpty && !_splitterDestinations.IsEmpty;
+ }
+
+ ///
+ /// Update the internal state of all splitters.
+ ///
+ public void UpdateInternalState()
+ {
+ foreach (ref SplitterState splitter in _splitters.Span)
+ {
+ splitter.UpdateInternalState();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterDestination.cs b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterDestination.cs
new file mode 100644
index 00000000..c074e4a7
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterDestination.cs
@@ -0,0 +1,193 @@
+using Ryujinx.Audio.Renderer.Parameter;
+using Ryujinx.Common.Utilities;
+using System;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Audio.Renderer.Server.Splitter
+{
+ ///
+ /// Server state for a splitter destination.
+ ///
+ [StructLayout(LayoutKind.Sequential, Size = 0xE0, Pack = Alignment)]
+ public struct SplitterDestination
+ {
+ public const int Alignment = 0x10;
+
+ ///
+ /// The unique id of this .
+ ///
+ public int Id;
+
+ ///
+ /// The mix to output the result of the splitter.
+ ///
+ public int DestinationId;
+
+ ///
+ /// Mix buffer volumes storage.
+ ///
+ private MixArray _mix;
+ private MixArray _previousMix;
+
+ ///
+ /// Pointer to the next linked element.
+ ///
+ private unsafe SplitterDestination* _next;
+
+ ///
+ /// Set to true if in use.
+ ///
+ [MarshalAs(UnmanagedType.I1)]
+ public bool IsUsed;
+
+ ///
+ /// Set to true if the internal state need to be updated.
+ ///
+ [MarshalAs(UnmanagedType.I1)]
+ public bool NeedToUpdateInternalState;
+
+ [StructLayout(LayoutKind.Sequential, Size = 4 * Constants.MixBufferCountMax, Pack = 1)]
+ private struct MixArray { }
+
+ ///
+ /// Mix buffer volumes.
+ ///
+ /// Used when a splitter id is specified in the mix.
+ public Span MixBufferVolume => SpanHelpers.AsSpan(ref _mix);
+
+ ///
+ /// Previous mix buffer volumes.
+ ///
+ /// Used when a splitter id is specified in the mix.
+ public Span PreviousMixBufferVolume => SpanHelpers.AsSpan(ref _previousMix);
+
+ ///
+ /// Get the of the next element or if not present.
+ ///
+ public Span Next
+ {
+ get
+ {
+ unsafe
+ {
+ return _next != null ? new Span(_next, 1) : Span.Empty;
+ }
+ }
+ }
+
+ ///
+ /// Create a new .
+ ///
+ /// The unique id of this .
+ public SplitterDestination(int id) : this()
+ {
+ Id = id;
+ DestinationId = Constants.UnusedMixId;
+
+ ClearVolumes();
+ }
+
+ ///
+ /// Update the from user parameter.
+ ///
+ /// The user parameter.
+ public void Update(SplitterDestinationInParameter parameter)
+ {
+ Debug.Assert(Id == parameter.Id);
+
+ if (parameter.IsMagicValid() && Id == parameter.Id)
+ {
+ DestinationId = parameter.DestinationId;
+
+ parameter.MixBufferVolume.CopyTo(MixBufferVolume);
+
+ if (!IsUsed && parameter.IsUsed)
+ {
+ MixBufferVolume.CopyTo(PreviousMixBufferVolume);
+
+ NeedToUpdateInternalState = false;
+ }
+
+ IsUsed = parameter.IsUsed;
+ }
+ }
+
+ ///
+ /// Update the internal state of the instance.
+ ///
+ public void UpdateInternalState()
+ {
+ if (IsUsed && NeedToUpdateInternalState)
+ {
+ MixBufferVolume.CopyTo(PreviousMixBufferVolume);
+ }
+
+ NeedToUpdateInternalState = false;
+ }
+
+ ///
+ /// Set the update internal state marker.
+ ///
+ public void MarkAsNeedToUpdateInternalState()
+ {
+ NeedToUpdateInternalState = true;
+ }
+
+ ///
+ /// Return true if the is used and has a destination.
+ ///
+ /// True if the is used and has a destination.
+ public bool IsConfigured()
+ {
+ return IsUsed && DestinationId != Constants.UnusedMixId;
+ }
+
+ ///
+ /// Get the volume for a given destination.
+ ///
+ /// The destination index to use.
+ /// The volume for the given destination.
+ public float GetMixVolume(int destinationIndex)
+ {
+ Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax);
+
+ return MixBufferVolume[destinationIndex];
+ }
+
+ ///
+ /// Clear the volumes.
+ ///
+ public void ClearVolumes()
+ {
+ MixBufferVolume.Fill(0);
+ PreviousMixBufferVolume.Fill(0);
+ }
+
+ ///
+ /// Link the next element to the given .
+ ///
+ /// The given to link.
+ public void Link(ref SplitterDestination next)
+ {
+ unsafe
+ {
+ fixed (SplitterDestination* nextPtr = &next)
+ {
+ _next = nextPtr;
+ }
+ }
+ }
+
+ ///
+ /// Remove the link to the next element.
+ ///
+ public void Unlink()
+ {
+ unsafe
+ {
+ _next = null;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs
new file mode 100644
index 00000000..15a0c6ba
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs
@@ -0,0 +1,221 @@
+using Ryujinx.Audio.Renderer.Parameter;
+using System;
+using System.Buffers;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Audio.Renderer.Server.Splitter
+{
+ ///
+ /// Server state for a splitter.
+ ///
+ [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
+ public struct SplitterState
+ {
+ public const int Alignment = 0x10;
+
+ ///
+ /// The unique id of this .
+ ///
+ public int Id;
+
+ ///
+ /// Target sample rate to use on the splitter.
+ ///
+ public uint SampleRate;
+
+ ///
+ /// Count of splitter destinations ().
+ ///
+ public int DestinationCount;
+
+ ///
+ /// Set to true if the splitter has a new connection.
+ ///
+ [MarshalAs(UnmanagedType.I1)]
+ public bool HasNewConnection;
+
+ ///
+ /// Linked list of .
+ ///
+ private unsafe SplitterDestination* _destinationsData;
+
+ ///
+ /// Span to the first element of the linked list of .
+ ///
+ public Span Destinations
+ {
+ get
+ {
+ unsafe
+ {
+ return (IntPtr)_destinationsData != IntPtr.Zero ? new Span(_destinationsData, 1) : Span.Empty;
+ }
+ }
+ }
+
+ ///
+ /// Create a new .
+ ///
+ /// The unique id of this .
+ public SplitterState(int id) : this()
+ {
+ Id = id;
+ }
+
+ public Span GetData(int index)
+ {
+ int i = 0;
+
+ Span result = Destinations;
+
+ while (i < index)
+ {
+ if (result.IsEmpty)
+ {
+ break;
+ }
+
+ result = result[0].Next;
+ i++;
+ }
+
+ return result;
+ }
+
+ ///
+ /// Clear the new connection flag.
+ ///
+ public void ClearNewConnectionFlag()
+ {
+ HasNewConnection = false;
+ }
+
+ ///
+ /// Utility function to apply a given to all .
+ ///
+ /// The action to execute on each elements.
+ private void ForEachDestination(SpanAction action)
+ {
+ Span temp = Destinations;
+
+ int i = 0;
+
+ while (true)
+ {
+ if (temp.IsEmpty)
+ {
+ break;
+ }
+
+ Span next = temp[0].Next;
+
+ action.Invoke(temp, i++);
+
+ temp = next;
+ }
+ }
+
+ ///
+ /// Update the from user parameter.
+ ///
+ /// The splitter context.
+ /// The user parameter.
+ /// The raw input data after the .
+ public void Update(SplitterContext context, ref SplitterInParameter parameter, ReadOnlySpan input)
+ {
+ ClearLinks();
+
+ int destinationCount;
+
+ if (context.IsBugFixed)
+ {
+ destinationCount = parameter.DestinationCount;
+ }
+ else
+ {
+ destinationCount = Math.Min(context.GetDestinationCountPerStateForCompatibility(), parameter.DestinationCount);
+ }
+
+ if (destinationCount > 0)
+ {
+ ReadOnlySpan destinationIds = MemoryMarshal.Cast(input);
+
+ Memory destination = context.GetDestinationMemory(destinationIds[0]);
+
+ SetDestination(ref destination.Span[0]);
+
+ DestinationCount = destinationCount;
+
+ for (int i = 1; i < destinationCount; i++)
+ {
+ Memory nextDestination = context.GetDestinationMemory(destinationIds[i]);
+
+ destination.Span[0].Link(ref nextDestination.Span[0]);
+ destination = nextDestination;
+ }
+ }
+
+ Debug.Assert(parameter.Id == Id);
+
+ if (parameter.Id == Id)
+ {
+ SampleRate = parameter.SampleRate;
+ HasNewConnection = true;
+ }
+ }
+
+ ///
+ /// Set the head of the linked list of .
+ ///
+ /// A reference to a .
+ public void SetDestination(ref SplitterDestination newValue)
+ {
+ unsafe
+ {
+ fixed (SplitterDestination* newValuePtr = &newValue)
+ {
+ _destinationsData = newValuePtr;
+ }
+ }
+ }
+
+ ///
+ /// Update the internal state of this instance.
+ ///
+ public void UpdateInternalState()
+ {
+ ForEachDestination((destination, _) => destination[0].UpdateInternalState());
+ }
+
+ ///
+ /// Clear all links from the .
+ ///
+ public void ClearLinks()
+ {
+ ForEachDestination((destination, _) => destination[0].Unlink());
+
+ unsafe
+ {
+ _destinationsData = (SplitterDestination*)IntPtr.Zero;
+ }
+ }
+
+ ///
+ /// Initialize a given .
+ ///
+ /// All the to initialize.
+ public static void InitializeSplitters(Span splitters)
+ {
+ foreach (ref SplitterState splitter in splitters)
+ {
+ unsafe
+ {
+ splitter._destinationsData = (SplitterDestination*)IntPtr.Zero;
+ }
+
+ splitter.DestinationCount = 0;
+ }
+ }
+ }
+}
\ No newline at end of file
--
cgit v1.2.3