MixCommand.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Runtime.CompilerServices;
  19. using System.Runtime.InteropServices;
  20. using System.Runtime.Intrinsics;
  21. using System.Runtime.Intrinsics.Arm;
  22. using System.Runtime.Intrinsics.X86;
  23. namespace Ryujinx.Audio.Renderer.Dsp.Command
  24. {
  25. public class MixCommand : ICommand
  26. {
  27. public bool Enabled { get; set; }
  28. public int NodeId { get; }
  29. public CommandType CommandType => CommandType.Mix;
  30. public ulong EstimatedProcessingTime { get; set; }
  31. public ushort InputBufferIndex { get; }
  32. public ushort OutputBufferIndex { get; }
  33. public float Volume { get; }
  34. public MixCommand(uint inputBufferIndex, uint outputBufferIndex, int nodeId, float volume)
  35. {
  36. Enabled = true;
  37. NodeId = nodeId;
  38. InputBufferIndex = (ushort)inputBufferIndex;
  39. OutputBufferIndex = (ushort)outputBufferIndex;
  40. Volume = volume;
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. private void ProcessMixAvx(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  44. {
  45. Vector256<float> volumeVec = Vector256.Create(Volume);
  46. ReadOnlySpan<Vector256<float>> inputVec = MemoryMarshal.Cast<float, Vector256<float>>(inputMix);
  47. Span<Vector256<float>> outputVec = MemoryMarshal.Cast<float, Vector256<float>>(outputMix);
  48. int sisdStart = inputVec.Length * 8;
  49. for (int i = 0; i < inputVec.Length; i++)
  50. {
  51. outputVec[i] = Avx.Add(outputVec[i], Avx.Ceiling(Avx.Multiply(inputVec[i], volumeVec)));
  52. }
  53. for (int i = sisdStart; i < inputMix.Length; i++)
  54. {
  55. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  56. }
  57. }
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. private void ProcessMixSse41(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  60. {
  61. Vector128<float> volumeVec = Vector128.Create(Volume);
  62. ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(inputMix);
  63. Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(outputMix);
  64. int sisdStart = inputVec.Length * 4;
  65. for (int i = 0; i < inputVec.Length; i++)
  66. {
  67. outputVec[i] = Sse.Add(outputVec[i], Sse41.Ceiling(Sse.Multiply(inputVec[i], volumeVec)));
  68. }
  69. for (int i = sisdStart; i < inputMix.Length; i++)
  70. {
  71. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  72. }
  73. }
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. private void ProcessMixAdvSimd(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  76. {
  77. Vector128<float> volumeVec = Vector128.Create(Volume);
  78. ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(inputMix);
  79. Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(outputMix);
  80. int sisdStart = inputVec.Length * 4;
  81. for (int i = 0; i < inputVec.Length; i++)
  82. {
  83. outputVec[i] = AdvSimd.Add(outputVec[i], AdvSimd.Ceiling(AdvSimd.Multiply(inputVec[i], volumeVec)));
  84. }
  85. for (int i = sisdStart; i < inputMix.Length; i++)
  86. {
  87. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  88. }
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. private void ProcessMixSlowPath(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  92. {
  93. for (int i = 0; i < inputMix.Length; i++)
  94. {
  95. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  96. }
  97. }
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. private void ProcessMix(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  100. {
  101. if (Avx.IsSupported)
  102. {
  103. ProcessMixAvx(outputMix, inputMix);
  104. }
  105. else if (Sse41.IsSupported)
  106. {
  107. ProcessMixSse41(outputMix, inputMix);
  108. }
  109. else if (AdvSimd.IsSupported)
  110. {
  111. ProcessMixAdvSimd(outputMix, inputMix);
  112. }
  113. else
  114. {
  115. ProcessMixSlowPath(outputMix, inputMix);
  116. }
  117. }
  118. public void Process(CommandList context)
  119. {
  120. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  121. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  122. ProcessMix(outputBuffer, inputBuffer);
  123. }
  124. }
  125. }