diff options
| author | Mary <me@thog.eu> | 2021-09-19 12:29:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-19 12:29:19 +0200 |
| commit | e17eb7bfafdd95084baea8e9f3dc77ee3f755347 (patch) | |
| tree | 4982e2593a279c9e2c4906ead4d1764a9ddadb54 /Ryujinx.Audio/Renderer/Dsp | |
| parent | fe9d5a1981cfe43c4535b7473064c9858addb3b5 (diff) | |
amadeus: Update to REV10 (#2654)
* amadeus: Update to REV10
This implements all the changes made with REV10 on 13.0.0.
* Address Ack's comment
* Address gdkchan's comment
Diffstat (limited to 'Ryujinx.Audio/Renderer/Dsp')
8 files changed, 384 insertions, 38 deletions
diff --git a/Ryujinx.Audio/Renderer/Dsp/BiquadFilterHelper.cs b/Ryujinx.Audio/Renderer/Dsp/BiquadFilterHelper.cs new file mode 100644 index 00000000..113f20f9 --- /dev/null +++ b/Ryujinx.Audio/Renderer/Dsp/BiquadFilterHelper.cs @@ -0,0 +1,100 @@ +// +// Copyright (c) 2019-2021 Ryujinx +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. +// + +using Ryujinx.Audio.Renderer.Dsp.State; +using Ryujinx.Audio.Renderer.Parameter; +using System; +using System.Runtime.CompilerServices; + +namespace Ryujinx.Audio.Renderer.Dsp +{ + public static class BiquadFilterHelper + { + private const int FixedPointPrecisionForParameter = 14; + + /// <summary> + /// Apply a single biquad filter. + /// </summary> + /// <remarks>This is implemented with a direct form 2.</remarks> + /// <param name="parameter">The biquad filter parameter</param> + /// <param name="state">The biquad filter state</param> + /// <param name="outputBuffer">The output buffer to write the result</param> + /// <param name="inputBuffer">The input buffer to write the result</param> + /// <param name="sampleCount">The count of samples to process</param> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ProcessBiquadFilter(ref BiquadFilterParameter parameter, ref BiquadFilterState state, Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, uint sampleCount) + { + float a0 = FixedPointHelper.ToFloat(parameter.Numerator[0], FixedPointPrecisionForParameter); + float a1 = FixedPointHelper.ToFloat(parameter.Numerator[1], FixedPointPrecisionForParameter); + float a2 = FixedPointHelper.ToFloat(parameter.Numerator[2], FixedPointPrecisionForParameter); + + float b1 = FixedPointHelper.ToFloat(parameter.Denominator[0], FixedPointPrecisionForParameter); + float b2 = FixedPointHelper.ToFloat(parameter.Denominator[1], FixedPointPrecisionForParameter); + + for (int i = 0; i < sampleCount; i++) + { + float input = inputBuffer[i]; + float output = input * a0 + state.State0; + + state.State0 = input * a1 + output * b1 + state.State1; + state.State1 = input * a2 + output * b2; + + outputBuffer[i] = output; + } + } + + /// <summary> + /// Apply multiple biquad filter. + /// </summary> + /// <remarks>This is implemented with a direct form 1.</remarks> + /// <param name="parameters">The biquad filter parameter</param> + /// <param name="states">The biquad filter state</param> + /// <param name="outputBuffer">The output buffer to write the result</param> + /// <param name="inputBuffer">The input buffer to write the result</param> + /// <param name="sampleCount">The count of samples to process</param> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ProcessBiquadFilter(ReadOnlySpan<BiquadFilterParameter> parameters, Span<BiquadFilterState> states, Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, uint sampleCount) + { + for (int stageIndex = 0; stageIndex < parameters.Length; stageIndex++) + { + BiquadFilterParameter parameter = parameters[stageIndex]; + + ref BiquadFilterState state = ref states[stageIndex]; + + float a0 = FixedPointHelper.ToFloat(parameter.Numerator[0], FixedPointPrecisionForParameter); + float a1 = FixedPointHelper.ToFloat(parameter.Numerator[1], FixedPointPrecisionForParameter); + float a2 = FixedPointHelper.ToFloat(parameter.Numerator[2], FixedPointPrecisionForParameter); + + float b1 = FixedPointHelper.ToFloat(parameter.Denominator[0], FixedPointPrecisionForParameter); + float b2 = FixedPointHelper.ToFloat(parameter.Denominator[1], FixedPointPrecisionForParameter); + + for (int i = 0; i < sampleCount; i++) + { + float input = inputBuffer[i]; + float output = input * a0 + state.State0 * a1 + state.State1 * a2 + state.State2 * b1 + state.State3 * b2; + + state.State1 = state.State0; + state.State0 = input; + state.State3 = state.State2; + state.State2 = output; + + outputBuffer[i] = output; + } + } + } + } +} diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/AuxiliaryBufferCommand.cs b/Ryujinx.Audio/Renderer/Dsp/Command/AuxiliaryBufferCommand.cs index 78d7ea87..15293dd0 100644 --- a/Ryujinx.Audio/Renderer/Dsp/Command/AuxiliaryBufferCommand.cs +++ b/Ryujinx.Audio/Renderer/Dsp/Command/AuxiliaryBufferCommand.cs @@ -156,7 +156,7 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command Span<int> outputBufferInt = MemoryMarshal.Cast<float, int>(outputBuffer); // Convert input data to the target format for user (int) - DataSourceHelper.ToInt(inputBufferInt, inputBuffer, outputBuffer.Length); + DataSourceHelper.ToInt(inputBufferInt, inputBuffer, inputBuffer.Length); // Send the input to the user Write(context.MemoryManager, OutputBuffer, CountMax, inputBufferInt, context.SampleCount, WriteOffset, UpdateCount); @@ -177,8 +177,8 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command } else { - context.MemoryManager.Fill(BufferInfo.SendBufferInfo, (ulong)Unsafe.SizeOf<AuxiliaryBufferInfo>(), 0); - context.MemoryManager.Fill(BufferInfo.ReturnBufferInfo, (ulong)Unsafe.SizeOf<AuxiliaryBufferInfo>(), 0); + AuxiliaryBufferInfo.Reset(context.MemoryManager, BufferInfo.SendBufferInfo); + AuxiliaryBufferInfo.Reset(context.MemoryManager, BufferInfo.ReturnBufferInfo); if (InputBufferIndex != OutputBufferIndex) { diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/BiquadFilterCommand.cs b/Ryujinx.Audio/Renderer/Dsp/Command/BiquadFilterCommand.cs index c850bb01..6a1d8ac0 100644 --- a/Ryujinx.Audio/Renderer/Dsp/Command/BiquadFilterCommand.cs +++ b/Ryujinx.Audio/Renderer/Dsp/Command/BiquadFilterCommand.cs @@ -32,15 +32,16 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command public ulong EstimatedProcessingTime { get; set; } - public BiquadFilterParameter Parameter { get; } public Memory<BiquadFilterState> BiquadFilterState { get; } public int InputBufferIndex { get; } public int OutputBufferIndex { get; } public bool NeedInitialization { get; } + private BiquadFilterParameter _parameter; + public BiquadFilterCommand(int baseIndex, ref BiquadFilterParameter filter, Memory<BiquadFilterState> biquadFilterStateMemory, int inputBufferOffset, int outputBufferOffset, bool needInitialization, int nodeId) { - Parameter = filter; + _parameter = filter; BiquadFilterState = biquadFilterStateMemory; InputBufferIndex = baseIndex + inputBufferOffset; OutputBufferIndex = baseIndex + outputBufferOffset; @@ -50,30 +51,6 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command NodeId = nodeId; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ProcessBiquadFilter(ref BiquadFilterState state, Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, uint sampleCount) - { - const int fixedPointPrecisionForParameter = 14; - - float a0 = FixedPointHelper.ToFloat(Parameter.Numerator[0], fixedPointPrecisionForParameter); - float a1 = FixedPointHelper.ToFloat(Parameter.Numerator[1], fixedPointPrecisionForParameter); - float a2 = FixedPointHelper.ToFloat(Parameter.Numerator[2], fixedPointPrecisionForParameter); - - float b1 = FixedPointHelper.ToFloat(Parameter.Denominator[0], fixedPointPrecisionForParameter); - float b2 = FixedPointHelper.ToFloat(Parameter.Denominator[1], fixedPointPrecisionForParameter); - - for (int i = 0; i < sampleCount; i++) - { - float input = inputBuffer[i]; - float output = input * a0 + state.Z1; - - state.Z1 = input * a1 + output * b1 + state.Z2; - state.Z2 = input * a2 + output * b2; - - outputBuffer[i] = output; - } - } - public void Process(CommandList context) { ref BiquadFilterState state = ref BiquadFilterState.Span[0]; @@ -86,7 +63,7 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command state = new BiquadFilterState(); } - ProcessBiquadFilter(ref state, outputBuffer, inputBuffer, context.SampleCount); + BiquadFilterHelper.ProcessBiquadFilter(ref _parameter, ref state, outputBuffer, inputBuffer, context.SampleCount); } } } diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/CaptureBufferCommand.cs b/Ryujinx.Audio/Renderer/Dsp/Command/CaptureBufferCommand.cs new file mode 100644 index 00000000..c6c579d3 --- /dev/null +++ b/Ryujinx.Audio/Renderer/Dsp/Command/CaptureBufferCommand.cs @@ -0,0 +1,154 @@ +// +// Copyright (c) 2019-2021 Ryujinx +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. +// + +using Ryujinx.Audio.Renderer.Dsp.State; +using Ryujinx.Audio.Renderer.Parameter; +using Ryujinx.Memory; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static Ryujinx.Audio.Renderer.Dsp.State.AuxiliaryBufferHeader; +using CpuAddress = System.UInt64; + +namespace Ryujinx.Audio.Renderer.Dsp.Command +{ + public class CaptureBufferCommand : ICommand + { + public bool Enabled { get; set; } + + public int NodeId { get; } + + public CommandType CommandType => CommandType.CaptureBuffer; + + public ulong EstimatedProcessingTime { get; set; } + + public uint InputBufferIndex { get; } + + public ulong CpuBufferInfoAddress { get; } + public ulong DspBufferInfoAddress { get; } + + public CpuAddress OutputBuffer { get; } + public uint CountMax { get; } + public uint UpdateCount { get; } + public uint WriteOffset { get; } + + public bool IsEffectEnabled { get; } + + public CaptureBufferCommand(uint bufferOffset, byte inputBufferOffset, ulong sendBufferInfo, bool isEnabled, + uint countMax, CpuAddress outputBuffer, uint updateCount, uint writeOffset, int nodeId) + { + Enabled = true; + NodeId = nodeId; + InputBufferIndex = bufferOffset + inputBufferOffset; + CpuBufferInfoAddress = sendBufferInfo; + DspBufferInfoAddress = sendBufferInfo + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>(); + OutputBuffer = outputBuffer; + CountMax = countMax; + UpdateCount = updateCount; + WriteOffset = writeOffset; + IsEffectEnabled = isEnabled; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private uint Write(IVirtualMemoryManager memoryManager, ulong outBufferAddress, uint countMax, ReadOnlySpan<int> buffer, uint count, uint writeOffset, uint updateCount) + { + if (countMax == 0 || outBufferAddress == 0) + { + return 0; + } + + uint targetWriteOffset = writeOffset + AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress); + + if (targetWriteOffset > countMax) + { + return 0; + } + + uint remaining = count; + + uint inBufferOffset = 0; + + while (remaining != 0) + { + uint countToWrite = Math.Min(countMax - targetWriteOffset, remaining); + + memoryManager.Write(outBufferAddress + targetWriteOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(buffer.Slice((int)inBufferOffset, (int)countToWrite))); + + targetWriteOffset = (targetWriteOffset + countToWrite) % countMax; + remaining -= countToWrite; + inBufferOffset += countToWrite; + } + + if (updateCount != 0) + { + uint dspTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, DspBufferInfoAddress); + uint cpuTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, CpuBufferInfoAddress); + + uint totalSampleCountDiff = dspTotalSampleCount - cpuTotalSampleCount; + + if (totalSampleCountDiff >= countMax) + { + uint dspLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, DspBufferInfoAddress); + uint cpuLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, CpuBufferInfoAddress); + + uint lostSampleCountDiff = dspLostSampleCount - cpuLostSampleCount; + uint newLostSampleCount = lostSampleCountDiff + updateCount; + + if (lostSampleCountDiff > newLostSampleCount) + { + newLostSampleCount = cpuLostSampleCount - 1; + } + + AuxiliaryBufferInfo.SetLostSampleCount(memoryManager, DspBufferInfoAddress, newLostSampleCount); + } + + uint newWriteOffset = (AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress) + updateCount) % countMax; + + AuxiliaryBufferInfo.SetWriteOffset(memoryManager, DspBufferInfoAddress, newWriteOffset); + + uint newTotalSampleCount = totalSampleCountDiff + newWriteOffset; + + AuxiliaryBufferInfo.SetTotalSampleCount(memoryManager, DspBufferInfoAddress, newTotalSampleCount); + } + + return count; + } + + public void Process(CommandList context) + { + Span<float> inputBuffer = context.GetBuffer((int)InputBufferIndex); + + if (IsEffectEnabled) + { + Span<int> inputBufferInt = MemoryMarshal.Cast<float, int>(inputBuffer); + + // Convert input data to the target format for user (int) + DataSourceHelper.ToInt(inputBufferInt, inputBuffer, inputBuffer.Length); + + // Send the input to the user + Write(context.MemoryManager, OutputBuffer, CountMax, inputBufferInt, context.SampleCount, WriteOffset, UpdateCount); + + // Convert back to float + DataSourceHelper.ToFloat(inputBuffer, inputBufferInt, inputBuffer.Length); + } + else + { + AuxiliaryBufferInfo.Reset(context.MemoryManager, DspBufferInfoAddress); + } + } + } +} diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/CommandType.cs b/Ryujinx.Audio/Renderer/Dsp/Command/CommandType.cs index 997a080e..6f324d0e 100644 --- a/Ryujinx.Audio/Renderer/Dsp/Command/CommandType.cs +++ b/Ryujinx.Audio/Renderer/Dsp/Command/CommandType.cs @@ -46,6 +46,8 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command ClearMixBuffer, CopyMixBuffer, LimiterVersion1, - LimiterVersion2 + LimiterVersion2, + GroupedBiquadFilter, + CaptureBuffer } } diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/GroupedBiquadFilterCommand.cs b/Ryujinx.Audio/Renderer/Dsp/Command/GroupedBiquadFilterCommand.cs new file mode 100644 index 00000000..394d74ba --- /dev/null +++ b/Ryujinx.Audio/Renderer/Dsp/Command/GroupedBiquadFilterCommand.cs @@ -0,0 +1,80 @@ +// +// Copyright (c) 2019-2021 Ryujinx +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. +// + +using Ryujinx.Audio.Renderer.Dsp.State; +using Ryujinx.Audio.Renderer.Parameter; +using System; + +namespace Ryujinx.Audio.Renderer.Dsp.Command +{ + public class GroupedBiquadFilterCommand : ICommand + { + public bool Enabled { get; set; } + + public int NodeId { get; } + + public CommandType CommandType => CommandType.GroupedBiquadFilter; + + public ulong EstimatedProcessingTime { get; set; } + + private BiquadFilterParameter[] _parameters; + private Memory<BiquadFilterState> _biquadFilterStates; + private int _inputBufferIndex; + private int _outputBufferIndex; + private bool[] _isInitialized; + + public GroupedBiquadFilterCommand(int baseIndex, ReadOnlySpan<BiquadFilterParameter> filters, Memory<BiquadFilterState> biquadFilterStateMemory, int inputBufferOffset, int outputBufferOffset, ReadOnlySpan<bool> isInitialized, int nodeId) + { + _parameters = filters.ToArray(); + _biquadFilterStates = biquadFilterStateMemory; + _inputBufferIndex = baseIndex + inputBufferOffset; + _outputBufferIndex = baseIndex + outputBufferOffset; + _isInitialized = isInitialized.ToArray(); + + Enabled = true; + NodeId = nodeId; + } + + public void Process(CommandList context) + { + Span<BiquadFilterState> states = _biquadFilterStates.Span; + + ReadOnlySpan<float> inputBuffer = context.GetBuffer(_inputBufferIndex); + Span<float> outputBuffer = context.GetBuffer(_outputBufferIndex); + + for (int i = 0; i < _parameters.Length; i++) + { + if (!_isInitialized[i]) + { + states[i] = new BiquadFilterState(); + } + } + + // NOTE: Nintendo also implements a hot path for double biquad filters, but no generic path when the command definition suggests it could be done. + // As such we currently only implement a generic path for simplicity. + // TODO: Implement double biquad filters fast path. + if (_parameters.Length == 1) + { + BiquadFilterHelper.ProcessBiquadFilter(ref _parameters[0], ref states[0], outputBuffer, inputBuffer, context.SampleCount); + } + else + { + BiquadFilterHelper.ProcessBiquadFilter(_parameters, states, outputBuffer, inputBuffer, context.SampleCount); + } + } + } +} diff --git a/Ryujinx.Audio/Renderer/Dsp/State/AuxiliaryBufferHeader.cs b/Ryujinx.Audio/Renderer/Dsp/State/AuxiliaryBufferHeader.cs index 3cf24302..69a16a3d 100644 --- a/Ryujinx.Audio/Renderer/Dsp/State/AuxiliaryBufferHeader.cs +++ b/Ryujinx.Audio/Renderer/Dsp/State/AuxiliaryBufferHeader.cs @@ -20,18 +20,22 @@ using System.Runtime.InteropServices; namespace Ryujinx.Audio.Renderer.Dsp.State { - [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x40)] + [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x80)] public struct AuxiliaryBufferHeader { - [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0xC)] + [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x40)] public struct AuxiliaryBufferInfo { private const uint ReadOffsetPosition = 0x0; private const uint WriteOffsetPosition = 0x4; + private const uint LostSampleCountPosition = 0x8; + private const uint TotalSampleCountPosition = 0xC; public uint ReadOffset; public uint WriteOffset; - private uint _reserved; + public uint LostSampleCount; + public uint TotalSampleCount; + private unsafe fixed uint _unknown[12]; public static uint GetReadOffset(IVirtualMemoryManager manager, ulong bufferAddress) { @@ -43,6 +47,16 @@ namespace Ryujinx.Audio.Renderer.Dsp.State return manager.Read<uint>(bufferAddress + WriteOffsetPosition); } + public static uint GetLostSampleCount(IVirtualMemoryManager manager, ulong bufferAddress) + { + return manager.Read<uint>(bufferAddress + LostSampleCountPosition); + } + + public static uint GetTotalSampleCount(IVirtualMemoryManager manager, ulong bufferAddress) + { + return manager.Read<uint>(bufferAddress + TotalSampleCountPosition); + } + public static void SetReadOffset(IVirtualMemoryManager manager, ulong bufferAddress, uint value) { manager.Write(bufferAddress + ReadOffsetPosition, value); @@ -52,9 +66,26 @@ namespace Ryujinx.Audio.Renderer.Dsp.State { manager.Write(bufferAddress + WriteOffsetPosition, value); } + + public static void SetLostSampleCount(IVirtualMemoryManager manager, ulong bufferAddress, uint value) + { + manager.Write(bufferAddress + LostSampleCountPosition, value); + } + + public static void SetTotalSampleCount(IVirtualMemoryManager manager, ulong bufferAddress, uint value) + { + manager.Write(bufferAddress + TotalSampleCountPosition, value); + } + + public static void Reset(IVirtualMemoryManager manager, ulong bufferAddress) + { + // NOTE: Lost sample count is never reset, since REV10. + manager.Write(bufferAddress + ReadOffsetPosition, 0UL); + manager.Write(bufferAddress + TotalSampleCountPosition, 0); + } } - public AuxiliaryBufferInfo BufferInfo; - public unsafe fixed uint Unknown[13]; + public AuxiliaryBufferInfo CpuBufferInfo; + public AuxiliaryBufferInfo DspBufferInfo; } } diff --git a/Ryujinx.Audio/Renderer/Dsp/State/BiquadFilterState.cs b/Ryujinx.Audio/Renderer/Dsp/State/BiquadFilterState.cs index 9677333d..f9e677e0 100644 --- a/Ryujinx.Audio/Renderer/Dsp/State/BiquadFilterState.cs +++ b/Ryujinx.Audio/Renderer/Dsp/State/BiquadFilterState.cs @@ -22,7 +22,9 @@ namespace Ryujinx.Audio.Renderer.Dsp.State [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)] public struct BiquadFilterState { - public float Z1; - public float Z2; + public float State0; + public float State1; + public float State2; + public float State3; } } |
