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 | |
| 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')
25 files changed, 779 insertions, 69 deletions
diff --git a/Ryujinx.Audio/Renderer/Common/EffectType.cs b/Ryujinx.Audio/Renderer/Common/EffectType.cs index daa22036..f57f1279 100644 --- a/Ryujinx.Audio/Renderer/Common/EffectType.cs +++ b/Ryujinx.Audio/Renderer/Common/EffectType.cs @@ -61,5 +61,10 @@ namespace Ryujinx.Audio.Renderer.Common /// Effect applying a limiter (DRC). /// </summary> Limiter, + + /// <summary> + /// Effect to capture mixes (via auxiliary buffers). + /// </summary> + CaptureBuffer } } diff --git a/Ryujinx.Audio/Renderer/Common/PerformanceDetailType.cs b/Ryujinx.Audio/Renderer/Common/PerformanceDetailType.cs index e9e946ce..60de6079 100644 --- a/Ryujinx.Audio/Renderer/Common/PerformanceDetailType.cs +++ b/Ryujinx.Audio/Renderer/Common/PerformanceDetailType.cs @@ -30,6 +30,7 @@ namespace Ryujinx.Audio.Renderer.Common Reverb, Reverb3d, PcmFloat, - Limiter + Limiter, + CaptureBuffer } } diff --git a/Ryujinx.Audio/Renderer/Device/VirtualDevice.cs b/Ryujinx.Audio/Renderer/Device/VirtualDevice.cs index 29395e5c..3ec37069 100644 --- a/Ryujinx.Audio/Renderer/Device/VirtualDevice.cs +++ b/Ryujinx.Audio/Renderer/Device/VirtualDevice.cs @@ -27,12 +27,13 @@ namespace Ryujinx.Audio.Renderer.Device /// <summary> /// All the defined virtual devices. /// </summary> - public static readonly VirtualDevice[] Devices = new VirtualDevice[4] + public static readonly VirtualDevice[] Devices = new VirtualDevice[5] { - new VirtualDevice("AudioStereoJackOutput", 2), - new VirtualDevice("AudioBuiltInSpeakerOutput", 2), - new VirtualDevice("AudioTvOutput", 6), - new VirtualDevice("AudioUsbDeviceOutput", 2), + new VirtualDevice("AudioStereoJackOutput", 2, true), + new VirtualDevice("AudioBuiltInSpeakerOutput", 2, false), + new VirtualDevice("AudioTvOutput", 6, false), + new VirtualDevice("AudioUsbDeviceOutput", 2, true), + new VirtualDevice("AudioExternalOutput", 6, true), }; /// <summary> @@ -51,14 +52,21 @@ namespace Ryujinx.Audio.Renderer.Device public float MasterVolume { get; private set; } /// <summary> + /// Define if the <see cref="VirtualDevice"/> is provided by an external interface. + /// </summary> + public bool IsExternalOutput { get; } + + /// <summary> /// Create a new <see cref="VirtualDevice"/> instance. /// </summary> /// <param name="name">The name of the <see cref="VirtualDevice"/>.</param> /// <param name="channelCount">The count of channels supported by the <see cref="VirtualDevice"/>.</param> - private VirtualDevice(string name, uint channelCount) + /// <param name="isExternalOutput">Indicate if the <see cref="VirtualDevice"/> is provided by an external interface.</param> + private VirtualDevice(string name, uint channelCount, bool isExternalOutput) { Name = name; ChannelCount = channelCount; + IsExternalOutput = isExternalOutput; } /// <summary> @@ -80,5 +88,19 @@ namespace Ryujinx.Audio.Renderer.Device { return Name.Equals("AudioUsbDeviceOutput"); } + + /// <summary> + /// Get the output device name of the <see cref="VirtualDevice"/>. + /// </summary> + /// <returns>The output device name of the <see cref="VirtualDevice"/>.</returns> + public string GetOutputDeviceName() + { + if (IsExternalOutput) + { + return "AudioExternalOutput"; + } + + return Name; + } } } 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; } } diff --git a/Ryujinx.Audio/Renderer/Parameter/Effect/AuxiliaryBufferParameter.cs b/Ryujinx.Audio/Renderer/Parameter/Effect/AuxiliaryBufferParameter.cs index c30c4013..b1cbd679 100644 --- a/Ryujinx.Audio/Renderer/Parameter/Effect/AuxiliaryBufferParameter.cs +++ b/Ryujinx.Audio/Renderer/Parameter/Effect/AuxiliaryBufferParameter.cs @@ -21,7 +21,7 @@ using System.Runtime.InteropServices; namespace Ryujinx.Audio.Renderer.Parameter.Effect { /// <summary> - /// <see cref="IEffectInParameter.SpecificData"/> for <see cref="Common.EffectType.AuxiliaryBuffer"/>. + /// <see cref="IEffectInParameter.SpecificData"/> for <see cref="Common.EffectType.AuxiliaryBuffer"/> and <see cref="Common.EffectType.CaptureBuffer"/>. /// </summary> [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct AuxiliaryBufferParameter @@ -71,6 +71,7 @@ namespace Ryujinx.Audio.Renderer.Parameter.Effect /// <summary> /// The address of the start of the region containing two <see cref="Dsp.State.AuxiliaryBufferHeader"/> followed by the data that will be read by the <see cref="Dsp.AudioProcessor"/>. /// </summary> + /// <remarks>Unused with <see cref="Common.EffectType.CaptureBuffer"/>.</remarks> public ulong ReturnBufferInfoAddress; /// <summary> diff --git a/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs b/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs index afbe56a6..277c2474 100644 --- a/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs +++ b/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs @@ -360,6 +360,9 @@ namespace Ryujinx.Audio.Renderer.Server case 3: _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion3(_sampleCount, _mixBufferCount); break; + case 4: + _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion4(_sampleCount, _mixBufferCount); + break; default: throw new NotImplementedException($"Unsupported processing time estimator version {_behaviourContext.GetCommandProcessingTimeEstimatorVersion()}."); } diff --git a/Ryujinx.Audio/Renderer/Server/BehaviourContext.cs b/Ryujinx.Audio/Renderer/Server/BehaviourContext.cs index ed1f402e..d3a65b72 100644 --- a/Ryujinx.Audio/Renderer/Server/BehaviourContext.cs +++ b/Ryujinx.Audio/Renderer/Server/BehaviourContext.cs @@ -98,9 +98,19 @@ namespace Ryujinx.Audio.Renderer.Server public const int Revision9 = 9 << 24; /// <summary> + /// REV10: + /// Added Bluetooth audio device support and removed the unused "GetAudioSystemMasterVolumeSetting" audio device API. + /// A new effect was added: Capture. This effect allows the client side to capture audio buffers of a mix. + /// A new command was added for double biquad filters on voices. This is implemented using a direct form 1 (instead of the usual direct form 2). + /// A new version of the command estimator was added to support the new commands. + /// </summary> + /// <remarks>This was added in system update 13.0.0</remarks> + public const int Revision10 = 10 << 24; + + /// <summary> /// Last revision supported by the implementation. /// </summary> - public const int LastRevision = Revision9; + public const int LastRevision = Revision10; /// <summary> /// Target revision magic supported by the implementation. @@ -348,11 +358,25 @@ namespace Ryujinx.Audio.Renderer.Server } /// <summary> + /// Check if the audio renderer should use an optimized Biquad Filter (Direct Form 1) in case of two biquad filters are defined on a voice. + /// </summary> + /// <returns>True if the audio renderer should use the optimization.</returns> + public bool IsBiquadFilterGroupedOptimizationSupported() + { + return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10); + } + + /// <summary> /// Get the version of the <see cref="ICommandProcessingTimeEstimator"/>. /// </summary> /// <returns>The version of the <see cref="ICommandProcessingTimeEstimator"/>.</returns> public int GetCommandProcessingTimeEstimatorVersion() { + if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10)) + { + return 4; + } + if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8)) { return 3; diff --git a/Ryujinx.Audio/Renderer/Server/CommandBuffer.cs b/Ryujinx.Audio/Renderer/Server/CommandBuffer.cs index deb20f3d..0e74c301 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandBuffer.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandBuffer.cs @@ -24,6 +24,7 @@ using Ryujinx.Audio.Renderer.Server.Performance; using Ryujinx.Audio.Renderer.Server.Sink; using Ryujinx.Audio.Renderer.Server.Upsampler; using Ryujinx.Audio.Renderer.Server.Voice; +using Ryujinx.Common.Memory; using System; using CpuAddress = System.UInt64; @@ -221,6 +222,25 @@ namespace Ryujinx.Audio.Renderer.Server } /// <summary> + /// Create a new <see cref="GroupedBiquadFilterCommand"/>. + /// </summary> + /// <param name="baseIndex">The base index of the input and output buffer.</param> + /// <param name="filters">The biquad filter parameters.</param> + /// <param name="biquadFilterStatesMemory">The biquad states.</param> + /// <param name="inputBufferOffset">The input buffer offset.</param> + /// <param name="outputBufferOffset">The output buffer offset.</param> + /// <param name="isInitialized">Set to true if the biquad filter state is initialized.</param> + /// <param name="nodeId">The node id associated to this command.</param> + public void GenerateGroupedBiquadFilter(int baseIndex, ReadOnlySpan<BiquadFilterParameter> filters, Memory<BiquadFilterState> biquadFilterStatesMemory, int inputBufferOffset, int outputBufferOffset, ReadOnlySpan<bool> isInitialized, int nodeId) + { + GroupedBiquadFilterCommand command = new GroupedBiquadFilterCommand(baseIndex, filters, biquadFilterStatesMemory, inputBufferOffset, outputBufferOffset, isInitialized, nodeId); + + command.EstimatedProcessingTime = _commandProcessingTimeEstimator.Estimate(command); + + AddCommand(command); + } + + /// <summary> /// Generate a new <see cref="MixRampGroupedCommand"/>. /// </summary> /// <param name="mixBufferCount">The mix buffer count.</param> @@ -441,6 +461,30 @@ namespace Ryujinx.Audio.Renderer.Server } /// <summary> + /// Generate a new <see cref="CaptureBufferCommand"/>. + /// </summary> + /// <param name="bufferOffset">The target buffer offset.</param> + /// <param name="inputBufferOffset">The input buffer offset.</param> + /// <param name="sendBufferInfo">The capture state.</param> + /// <param name="isEnabled">Set to true if the effect should be active.</param> + /// <param name="countMax">The limit of the circular buffer.</param> + /// <param name="outputBuffer">The guest address of the output buffer.</param> + /// <param name="updateCount">The count to add on the offset after write operations.</param> + /// <param name="writeOffset">The write offset.</param> + /// <param name="nodeId">The node id associated to this command.</param> + public void GenerateCaptureEffect(uint bufferOffset, byte inputBufferOffset, ulong sendBufferInfo, bool isEnabled, uint countMax, CpuAddress outputBuffer, uint updateCount, uint writeOffset, int nodeId) + { + if (sendBufferInfo != 0) + { + CaptureBufferCommand command = new CaptureBufferCommand(bufferOffset, inputBufferOffset, sendBufferInfo, isEnabled, countMax, outputBuffer, updateCount, writeOffset, nodeId); + + command.EstimatedProcessingTime = _commandProcessingTimeEstimator.Estimate(command); + + AddCommand(command); + } + } + + /// <summary> /// Generate a new <see cref="VolumeCommand"/>. /// </summary> /// <param name="volume">The target volume to apply.</param> diff --git a/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs b/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs index 01e7c927..8e4ecd25 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs @@ -151,23 +151,35 @@ namespace Ryujinx.Audio.Renderer.Server private void GenerateBiquadFilterForVoice(ref VoiceState voiceState, Memory<VoiceUpdateState> state, int baseIndex, int bufferOffset, int nodeId) { - for (int i = 0; i < voiceState.BiquadFilters.Length; i++) + bool supportsOptimizedPath = _rendererContext.BehaviourContext.IsBiquadFilterGroupedOptimizationSupported(); + + if (supportsOptimizedPath && voiceState.BiquadFilters[0].Enable && voiceState.BiquadFilters[1].Enable) { - ref BiquadFilterParameter filter = ref voiceState.BiquadFilters[i]; + Memory<byte> biquadStateRawMemory = SpanMemoryManager<byte>.Cast(state).Slice(VoiceUpdateState.BiquadStateOffset, VoiceUpdateState.BiquadStateSize * Constants.VoiceBiquadFilterCount); + Memory<BiquadFilterState> stateMemory = SpanMemoryManager<BiquadFilterState>.Cast(biquadStateRawMemory); - if (filter.Enable) + _commandBuffer.GenerateGroupedBiquadFilter(baseIndex, voiceState.BiquadFilters.ToSpan(), stateMemory, bufferOffset, bufferOffset, voiceState.BiquadFilterNeedInitialization, nodeId); + } + else + { + for (int i = 0; i < voiceState.BiquadFilters.Length; i++) { - Memory<byte> biquadStateRawMemory = SpanMemoryManager<byte>.Cast(state).Slice(VoiceUpdateState.BiquadStateOffset, VoiceUpdateState.BiquadStateSize * Constants.VoiceBiquadFilterCount); + ref BiquadFilterParameter filter = ref voiceState.BiquadFilters[i]; - Memory<BiquadFilterState> stateMemory = SpanMemoryManager<BiquadFilterState>.Cast(biquadStateRawMemory); + if (filter.Enable) + { + Memory<byte> biquadStateRawMemory = SpanMemoryManager<byte>.Cast(state).Slice(VoiceUpdateState.BiquadStateOffset, VoiceUpdateState.BiquadStateSize * Constants.VoiceBiquadFilterCount); - _commandBuffer.GenerateBiquadFilter(baseIndex, - ref filter, - stateMemory.Slice(i, 1), - bufferOffset, - bufferOffset, - !voiceState.BiquadFilterNeedInitialization[i], - nodeId); + Memory<BiquadFilterState> stateMemory = SpanMemoryManager<BiquadFilterState>.Cast(biquadStateRawMemory); + + _commandBuffer.GenerateBiquadFilter(baseIndex, + ref filter, + stateMemory.Slice(i, 1), + bufferOffset, + bufferOffset, + !voiceState.BiquadFilterNeedInitialization[i], + nodeId); + } } } } @@ -443,7 +455,7 @@ namespace Ryujinx.Audio.Renderer.Server uint updateCount; - if ((channelIndex - 1) != 0) + if (channelIndex != 1) { updateCount = 0; } @@ -556,6 +568,52 @@ namespace Ryujinx.Audio.Renderer.Server } } + private void GenerateCaptureEffect(uint bufferOffset, CaptureBufferEffect effect, int nodeId) + { + Debug.Assert(effect.Type == EffectType.CaptureBuffer); + + if (effect.IsEnabled) + { + effect.GetWorkBuffer(0); + } + + if (effect.State.SendBufferInfoBase != 0) + { + int i = 0; + uint writeOffset = 0; + + for (uint channelIndex = effect.Parameter.ChannelCount; channelIndex != 0; channelIndex--) + { + uint newUpdateCount = writeOffset + _commandBuffer.CommandList.SampleCount; + + uint updateCount; + + if (channelIndex != 1) + { + updateCount = 0; + } + else + { + updateCount = newUpdateCount; + } + + _commandBuffer.GenerateCaptureEffect(bufferOffset, + effect.Parameter.Input[i], + effect.State.SendBufferInfo, + effect.IsEnabled, + effect.Parameter.BufferStorageSize, + effect.State.SendBufferInfoBase, + updateCount, + writeOffset, + nodeId); + + writeOffset = newUpdateCount; + + i++; + } + } + } + private void GenerateEffect(ref MixState mix, int effectId, BaseEffect effect) { int nodeId = mix.NodeId; @@ -597,6 +655,9 @@ namespace Ryujinx.Audio.Renderer.Server case EffectType.Limiter: GenerateLimiterEffect(mix.BufferOffset, (LimiterEffect)effect, nodeId, effectId); break; + case EffectType.CaptureBuffer: + GenerateCaptureEffect(mix.BufferOffset, (CaptureBufferEffect)effect, nodeId); + break; default: throw new NotImplementedException($"Unsupported effect type {effect.Type}"); } diff --git a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion1.cs b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion1.cs index feb3706f..6821cccf 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion1.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion1.cs @@ -186,5 +186,15 @@ namespace Ryujinx.Audio.Renderer.Server { return 0; } + + public uint Estimate(GroupedBiquadFilterCommand command) + { + return 0; + } + + public uint Estimate(CaptureBufferCommand command) + { + return 0; + } } } diff --git a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion2.cs b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion2.cs index 227f3c81..daf50de9 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion2.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion2.cs @@ -550,5 +550,15 @@ namespace Ryujinx.Audio.Renderer.Server { return 0; } + + public uint Estimate(GroupedBiquadFilterCommand command) + { + return 0; + } + + public uint Estimate(CaptureBufferCommand command) + { + return 0; + } } } diff --git a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion3.cs b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion3.cs index e00fcf7b..75d3d05b 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion3.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion3.cs @@ -16,7 +16,6 @@ // using Ryujinx.Audio.Common; -using Ryujinx.Audio.Renderer.Common; using Ryujinx.Audio.Renderer.Dsp.Command; using Ryujinx.Audio.Renderer.Parameter.Effect; using System; @@ -30,8 +29,8 @@ namespace Ryujinx.Audio.Renderer.Server /// </summary> public class CommandProcessingTimeEstimatorVersion3 : ICommandProcessingTimeEstimator { - private uint _sampleCount; - private uint _bufferCount; + protected uint _sampleCount; + protected uint _bufferCount; public CommandProcessingTimeEstimatorVersion3(uint sampleCount, uint bufferCount) { @@ -755,5 +754,15 @@ namespace Ryujinx.Audio.Renderer.Server throw new NotImplementedException($"{command.Parameter.ChannelCount}"); } } + + public virtual uint Estimate(GroupedBiquadFilterCommand command) + { + return 0; + } + + public virtual uint Estimate(CaptureBufferCommand command) + { + return 0; + } } } diff --git a/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion4.cs b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion4.cs new file mode 100644 index 00000000..ea11f69c --- /dev/null +++ b/Ryujinx.Audio/Renderer/Server/CommandProcessingTimeEstimatorVersion4.cs @@ -0,0 +1,68 @@ +// +// 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.Common; +using Ryujinx.Audio.Renderer.Dsp.Command; +using Ryujinx.Audio.Renderer.Parameter.Effect; +using System; +using System.Diagnostics; +using static Ryujinx.Audio.Renderer.Parameter.VoiceInParameter; + +namespace Ryujinx.Audio.Renderer.Server +{ + /// <summary> + /// <see cref="ICommandProcessingTimeEstimator"/> version 4. (added with REV10) + /// </summary> + public class CommandProcessingTimeEstimatorVersion4 : CommandProcessingTimeEstimatorVersion3 + { + public CommandProcessingTimeEstimatorVersion4(uint sampleCount, uint bufferCount) : base(sampleCount, bufferCount) { } + + public override uint Estimate(GroupedBiquadFilterCommand command) + { + Debug.Assert(_sampleCount == 160 || _sampleCount == 240); + + if (_sampleCount == 160) + { + return (uint)7424.5f; + } + + return (uint)9730.4f; + } + + public override uint Estimate(CaptureBufferCommand command) + { + Debug.Assert(_sampleCount == 160 || _sampleCount == 240); + + if (_sampleCount == 160) + { + if (command.Enabled) + { + return (uint)435.2f; + } + + return (uint)4261.0f; + } + + if (command.Enabled) + { + return (uint)5858.26f; + } + + return (uint)435.2f; + } + } +} diff --git a/Ryujinx.Audio/Renderer/Server/Effect/AuxiliaryBufferEffect.cs b/Ryujinx.Audio/Renderer/Server/Effect/AuxiliaryBufferEffect.cs index eac1708e..3a13d377 100644 --- a/Ryujinx.Audio/Renderer/Server/Effect/AuxiliaryBufferEffect.cs +++ b/Ryujinx.Audio/Renderer/Server/Effect/AuxiliaryBufferEffect.cs @@ -23,7 +23,7 @@ using Ryujinx.Audio.Renderer.Server.MemoryPool; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - +using static Ryujinx.Audio.Renderer.Dsp.State.AuxiliaryBufferHeader; using DspAddress = System.UInt64; namespace Ryujinx.Audio.Renderer.Server.Effect @@ -73,7 +73,7 @@ namespace Ryujinx.Audio.Renderer.Server.Effect if (BufferUnmapped || parameter.IsNew) { - ulong bufferSize = (ulong)Unsafe.SizeOf<int>() * Parameter.BufferStorageSize + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>() * 2; + ulong bufferSize = (ulong)Unsafe.SizeOf<int>() * Parameter.BufferStorageSize + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>(); bool sendBufferUnmapped = !mapper.TryAttachBuffer(out updateErrorInfo, ref WorkBuffers[0], Parameter.SendBufferInfoAddress, bufferSize); bool returnBufferUnmapped = !mapper.TryAttachBuffer(out updateErrorInfo, ref WorkBuffers[1], Parameter.ReturnBufferInfoAddress, bufferSize); @@ -85,11 +85,11 @@ namespace Ryujinx.Audio.Renderer.Server.Effect DspAddress sendDspAddress = WorkBuffers[0].GetReference(false); DspAddress returnDspAddress = WorkBuffers[1].GetReference(false); - State.SendBufferInfo = sendDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>(); - State.SendBufferInfoBase = sendDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>() * 2; + State.SendBufferInfo = sendDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferInfo>(); + State.SendBufferInfoBase = sendDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>(); - State.ReturnBufferInfo = returnDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>(); - State.ReturnBufferInfoBase = returnDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>() * 2; + State.ReturnBufferInfo = returnDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferInfo>(); + State.ReturnBufferInfoBase = returnDspAddress + (uint)Unsafe.SizeOf<AuxiliaryBufferHeader>(); } } } diff --git a/Ryujinx.Audio/Renderer/Server/Effect/BaseEffect.cs b/Ryujinx.Audio/Renderer/Server/Effect/BaseEffect.cs index c7b06e7a..40e87530 100644 --- a/Ryujinx.Audio/Renderer/Server/Effect/BaseEffect.cs +++ b/Ryujinx.Audio/Renderer/Server/Effect/BaseEffect.cs @@ -277,6 +277,8 @@ namespace Ryujinx.Audio.Renderer.Server.Effect return PerformanceDetailType.Mix; case EffectType.Limiter: return PerformanceDetailType.Limiter; + case EffectType.CaptureBuffer: + return PerformanceDetailType.CaptureBuffer; default: throw new NotImplementedException($"{Type}"); } diff --git a/Ryujinx.Audio/Renderer/Server/Effect/CaptureBufferEffect.cs b/Ryujinx.Audio/Renderer/Server/Effect/CaptureBufferEffect.cs new file mode 100644 index 00000000..6ba0040e --- /dev/null +++ b/Ryujinx.Audio/Renderer/Server/Effect/CaptureBufferEffect.cs @@ -0,0 +1,99 @@ +// +// 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.Common; +using Ryujinx.Audio.Renderer.Dsp.State; +using Ryujinx.Audio.Renderer.Parameter; +using Ryujinx.Audio.Renderer.Parameter.Effect; +using Ryujinx.Audio.Renderer.Server.MemoryPool; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using DspAddress = System.UInt64; + +namespace Ryujinx.Audio.Renderer.Server.Effect +{ + /// <summary> + /// Server state for an capture buffer effect. + /// </summary> + public class CaptureBufferEffect : BaseEffect + { + /// <summary> + /// The capture buffer parameter. + /// </summary> + public AuxiliaryBufferParameter Parameter; + + /// <summary> + /// Capture buffer state. + /// </summary> + public AuxiliaryBufferAddresses State; + + public override EffectType TargetEffectType => EffectType.CaptureBuffer; + + public override DspAddress GetWorkBuffer(int index) + { + return WorkBuffers[index].GetReference(true); + } + + public override void Update(out BehaviourParameter.ErrorInfo updateErrorInfo, ref EffectInParameterVersion1 parameter, PoolMapper mapper) + { + Update(out updateErrorInfo, ref parameter, mapper); + } + + public override void Update(out BehaviourParameter.ErrorInfo updateErrorInfo, ref EffectInParameterVersion2 parameter, PoolMapper mapper) + { + Update(out updateErrorInfo, ref parameter, mapper); + } + + public void Update<T>(out BehaviourParameter.ErrorInfo updateErrorInfo, ref T parameter, PoolMapper mapper) where T : unmanaged, IEffectInParameter + { + Debug.Assert(IsTypeValid(ref parameter)); + + UpdateParameterBase(ref parameter); + + Parameter = MemoryMarshal.Cast<byte, AuxiliaryBufferParameter>(parameter.SpecificData)[0]; + IsEnabled = parameter.IsEnabled; + + updateErrorInfo = new BehaviourParameter.ErrorInfo(); + + if (BufferUnmapped || parameter.IsNew) + { + ulong bufferSize = (ulong)Unsafe.SizeOf<int>() * Parameter.BufferStorageSize + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>(); + + bool sendBufferUnmapped = !mapper.TryAttachBuffer(out updateErrorInfo, ref WorkBuffers[0], Parameter.SendBufferInfoAddress, bufferSize); + + BufferUnmapped = sendBufferUnmapped; + + if (!BufferUnmapped) + { + DspAddress sendDspAddress = WorkBuffers[0].GetReference(false); + + // NOTE: Nintendo directly interact with the CPU side structure in the processing of the DSP command. + State.SendBufferInfo = sendDspAddress; + State.SendBufferInfoBase = sendDspAddress + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>(); + State.ReturnBufferInfo = 0; + State.ReturnBufferInfoBase = 0; + } + } + } + + public override void UpdateForCommandGeneration() + { + UpdateUsageStateForCommandGeneration(); + } + } +} diff --git a/Ryujinx.Audio/Renderer/Server/ICommandProcessingTimeEstimator.cs b/Ryujinx.Audio/Renderer/Server/ICommandProcessingTimeEstimator.cs index eae48be6..4100f357 100644 --- a/Ryujinx.Audio/Renderer/Server/ICommandProcessingTimeEstimator.cs +++ b/Ryujinx.Audio/Renderer/Server/ICommandProcessingTimeEstimator.cs @@ -50,5 +50,7 @@ namespace Ryujinx.Audio.Renderer.Server uint Estimate(UpsampleCommand command); uint Estimate(LimiterCommandVersion1 command); uint Estimate(LimiterCommandVersion2 command); + uint Estimate(GroupedBiquadFilterCommand command); + uint Estimate(CaptureBufferCommand command); } } diff --git a/Ryujinx.Audio/Renderer/Server/StateUpdater.cs b/Ryujinx.Audio/Renderer/Server/StateUpdater.cs index e7a982c4..1f50864d 100644 --- a/Ryujinx.Audio/Renderer/Server/StateUpdater.cs +++ b/Ryujinx.Audio/Renderer/Server/StateUpdater.cs @@ -254,6 +254,9 @@ namespace Ryujinx.Audio.Renderer.Server case EffectType.Limiter: effect = new LimiterEffect(); break; + case EffectType.CaptureBuffer: + effect = new CaptureBufferEffect(); + break; default: throw new NotImplementedException($"EffectType {parameter.Type} not implemented!"); } |
