MixCommand.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Intrinsics;
  5. using System.Runtime.Intrinsics.Arm;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace Ryujinx.Audio.Renderer.Dsp.Command
  8. {
  9. public class MixCommand : ICommand
  10. {
  11. public bool Enabled { get; set; }
  12. public int NodeId { get; }
  13. public CommandType CommandType => CommandType.Mix;
  14. public uint EstimatedProcessingTime { get; set; }
  15. public ushort InputBufferIndex { get; }
  16. public ushort OutputBufferIndex { get; }
  17. public float Volume { get; }
  18. public MixCommand(uint inputBufferIndex, uint outputBufferIndex, int nodeId, float volume)
  19. {
  20. Enabled = true;
  21. NodeId = nodeId;
  22. InputBufferIndex = (ushort)inputBufferIndex;
  23. OutputBufferIndex = (ushort)outputBufferIndex;
  24. Volume = volume;
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. private void ProcessMixAvx(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  28. {
  29. Vector256<float> volumeVec = Vector256.Create(Volume);
  30. ReadOnlySpan<Vector256<float>> inputVec = MemoryMarshal.Cast<float, Vector256<float>>(inputMix);
  31. Span<Vector256<float>> outputVec = MemoryMarshal.Cast<float, Vector256<float>>(outputMix);
  32. int sisdStart = inputVec.Length * 8;
  33. for (int i = 0; i < inputVec.Length; i++)
  34. {
  35. outputVec[i] = Avx.Add(outputVec[i], Avx.Ceiling(Avx.Multiply(inputVec[i], volumeVec)));
  36. }
  37. for (int i = sisdStart; i < inputMix.Length; i++)
  38. {
  39. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  40. }
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. private void ProcessMixSse41(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  44. {
  45. Vector128<float> volumeVec = Vector128.Create(Volume);
  46. ReadOnlySpan<Vector128<float>> inputVec = MemoryMarshal.Cast<float, Vector128<float>>(inputMix);
  47. Span<Vector128<float>> outputVec = MemoryMarshal.Cast<float, Vector128<float>>(outputMix);
  48. int sisdStart = inputVec.Length * 4;
  49. for (int i = 0; i < inputVec.Length; i++)
  50. {
  51. outputVec[i] = Sse.Add(outputVec[i], Sse41.Ceiling(Sse.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 ProcessMixAdvSimd(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] = AdvSimd.Add(outputVec[i], AdvSimd.Ceiling(AdvSimd.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 ProcessMixSlowPath(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  76. {
  77. for (int i = 0; i < inputMix.Length; i++)
  78. {
  79. outputMix[i] += FloatingPointHelper.MultiplyRoundUp(inputMix[i], Volume);
  80. }
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. private void ProcessMix(Span<float> outputMix, ReadOnlySpan<float> inputMix)
  84. {
  85. if (Avx.IsSupported)
  86. {
  87. ProcessMixAvx(outputMix, inputMix);
  88. }
  89. else if (Sse41.IsSupported)
  90. {
  91. ProcessMixSse41(outputMix, inputMix);
  92. }
  93. else if (AdvSimd.IsSupported)
  94. {
  95. ProcessMixAdvSimd(outputMix, inputMix);
  96. }
  97. else
  98. {
  99. ProcessMixSlowPath(outputMix, inputMix);
  100. }
  101. }
  102. public void Process(CommandList context)
  103. {
  104. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  105. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  106. ProcessMix(outputBuffer, inputBuffer);
  107. }
  108. }
  109. }