MixCommand.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2019-2020 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.X86;
  22. namespace Ryujinx.Audio.Renderer.Dsp.Command
  23. {
  24. public class MixCommand : ICommand
  25. {
  26. public bool Enabled { get; set; }
  27. public int NodeId { get; }
  28. public CommandType CommandType => CommandType.Mix;
  29. public ulong EstimatedProcessingTime { get; set; }
  30. public ushort InputBufferIndex { get; }
  31. public ushort OutputBufferIndex { get; }
  32. public float Volume { get; }
  33. public MixCommand(uint inputBufferIndex, uint outputBufferIndex, int nodeId, float volume)
  34. {
  35. Enabled = true;
  36. NodeId = nodeId;
  37. InputBufferIndex = (ushort)inputBufferIndex;
  38. OutputBufferIndex = (ushort)outputBufferIndex;
  39. Volume = volume;
  40. }
  41. private void ProcessMixAvx(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  42. {
  43. Vector256<float> volumeVec = Vector256.Create(Volume);
  44. ReadOnlySpan<Vector256<float>> inputVec = MemoryMarshal.Cast<float, Vector256<float>>(inputMix);
  45. Span<Vector256<float>> outputVec = MemoryMarshal.Cast<float, Vector256<float>>(outputMix);
  46. int sisdStart = inputVec.Length * 8;
  47. for (int i = 0; i < inputVec.Length; i++)
  48. {
  49. outputVec[i] = Avx.Add(outputVec[i], Avx.Ceiling(Avx.Multiply(inputVec[i], volumeVec)));
  50. }
  51. for (int i = sisdStart; i < inputMix.Length; i++)
  52. {
  53. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  54. }
  55. }
  56. private void ProcessMixSse41(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  57. {
  58. Vector128<float> volumeVec = Vector128.Create(Volume);
  59. ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(inputMix);
  60. Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(outputMix);
  61. int sisdStart = inputVec.Length * 4;
  62. for (int i = 0; i < inputVec.Length; i++)
  63. {
  64. outputVec[i] = Sse.Add(outputVec[i], Sse41.Ceiling(Sse.Multiply(inputVec[i], volumeVec)));
  65. }
  66. for (int i = sisdStart; i < inputMix.Length; i++)
  67. {
  68. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  69. }
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. private void ProcessMixSlowPath(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  73. {
  74. for (int i = 0; i < inputMix.Length; i++)
  75. {
  76. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  77. }
  78. }
  79. private void ProcessMix(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  80. {
  81. if (Avx.IsSupported)
  82. {
  83. ProcessMixAvx(outputMix, inputMix);
  84. }
  85. else if (Sse41.IsSupported)
  86. {
  87. ProcessMixSse41(outputMix, inputMix);
  88. }
  89. else
  90. {
  91. ProcessMixSlowPath(outputMix, inputMix);
  92. }
  93. }
  94. public void Process(CommandList context)
  95. {
  96. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  97. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  98. ProcessMix(outputBuffer, inputBuffer);
  99. }
  100. }
  101. }