AuxiliaryBufferCommand.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Ryujinx.Audio.Renderer.Common;
  18. using Ryujinx.Memory;
  19. using System;
  20. using System.Runtime.CompilerServices;
  21. using System.Runtime.InteropServices;
  22. using static Ryujinx.Audio.Renderer.Dsp.State.AuxiliaryBufferHeader;
  23. using CpuAddress = System.UInt64;
  24. namespace Ryujinx.Audio.Renderer.Dsp.Command
  25. {
  26. public class AuxiliaryBufferCommand : ICommand
  27. {
  28. public bool Enabled { get; set; }
  29. public int NodeId { get; }
  30. public CommandType CommandType => CommandType.AuxiliaryBuffer;
  31. public ulong EstimatedProcessingTime { get; set; }
  32. public uint InputBufferIndex { get; }
  33. public uint OutputBufferIndex { get; }
  34. public AuxiliaryBufferAddresses BufferInfo { get; }
  35. public CpuAddress InputBuffer { get; }
  36. public CpuAddress OutputBuffer { get; }
  37. public uint CountMax { get; }
  38. public uint UpdateCount { get; }
  39. public uint WriteOffset { get; }
  40. public bool IsEffectEnabled { get; }
  41. public AuxiliaryBufferCommand(uint bufferOffset, byte inputBufferOffset, byte outputBufferOffset,
  42. ref AuxiliaryBufferAddresses sendBufferInfo, bool isEnabled, uint countMax,
  43. CpuAddress outputBuffer, CpuAddress inputBuffer, uint updateCount, uint writeOffset, int nodeId)
  44. {
  45. Enabled = true;
  46. NodeId = nodeId;
  47. InputBufferIndex = bufferOffset + inputBufferOffset;
  48. OutputBufferIndex = bufferOffset + outputBufferOffset;
  49. BufferInfo = sendBufferInfo;
  50. InputBuffer = inputBuffer;
  51. OutputBuffer = outputBuffer;
  52. CountMax = countMax;
  53. UpdateCount = updateCount;
  54. WriteOffset = writeOffset;
  55. IsEffectEnabled = isEnabled;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. private uint Read(IVirtualMemoryManager memoryManager, ulong bufferAddress, uint countMax, Span<int> outBuffer, uint count, uint readOffset, uint updateCount)
  59. {
  60. if (countMax == 0 || bufferAddress == 0)
  61. {
  62. return 0;
  63. }
  64. uint targetReadOffset = readOffset + AuxiliaryBufferInfo.GetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo);
  65. if (targetReadOffset > countMax)
  66. {
  67. return 0;
  68. }
  69. uint remaining = count;
  70. uint outBufferOffset = 0;
  71. while (remaining != 0)
  72. {
  73. uint countToWrite = Math.Min(countMax - targetReadOffset, remaining);
  74. memoryManager.Read(bufferAddress + targetReadOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(outBuffer.Slice((int)outBufferOffset, (int)countToWrite)));
  75. targetReadOffset = (targetReadOffset + countToWrite) % countMax;
  76. remaining -= countToWrite;
  77. outBufferOffset += countToWrite;
  78. }
  79. if (updateCount != 0)
  80. {
  81. uint newReadOffset = (AuxiliaryBufferInfo.GetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo) + updateCount) % countMax;
  82. AuxiliaryBufferInfo.SetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo, newReadOffset);
  83. }
  84. return count;
  85. }
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. private uint Write(IVirtualMemoryManager memoryManager, ulong outBufferAddress, uint countMax, ReadOnlySpan<int> buffer, uint count, uint writeOffset, uint updateCount)
  88. {
  89. if (countMax == 0 || outBufferAddress == 0)
  90. {
  91. return 0;
  92. }
  93. uint targetWriteOffset = writeOffset + AuxiliaryBufferInfo.GetWriteOffset(memoryManager, BufferInfo.SendBufferInfo);
  94. if (targetWriteOffset > countMax)
  95. {
  96. return 0;
  97. }
  98. uint remaining = count;
  99. uint inBufferOffset = 0;
  100. while (remaining != 0)
  101. {
  102. uint countToWrite = Math.Min(countMax - targetWriteOffset, remaining);
  103. memoryManager.Write(outBufferAddress + targetWriteOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(buffer.Slice((int)inBufferOffset, (int)countToWrite)));
  104. targetWriteOffset = (targetWriteOffset + countToWrite) % countMax;
  105. remaining -= countToWrite;
  106. inBufferOffset += countToWrite;
  107. }
  108. if (updateCount != 0)
  109. {
  110. uint newWriteOffset = (AuxiliaryBufferInfo.GetWriteOffset(memoryManager, BufferInfo.SendBufferInfo) + updateCount) % countMax;
  111. AuxiliaryBufferInfo.SetWriteOffset(memoryManager, BufferInfo.SendBufferInfo, newWriteOffset);
  112. }
  113. return count;
  114. }
  115. public void Process(CommandList context)
  116. {
  117. Span<float> inputBuffer = context.GetBuffer((int)InputBufferIndex);
  118. Span<float> outputBuffer = context.GetBuffer((int)OutputBufferIndex);
  119. if (IsEffectEnabled)
  120. {
  121. Span<int> inputBufferInt = MemoryMarshal.Cast<float, int>(inputBuffer);
  122. Span<int> outputBufferInt = MemoryMarshal.Cast<float, int>(outputBuffer);
  123. // Convert input data to the target format for user (int)
  124. DataSourceHelper.ToInt(inputBufferInt, inputBuffer, inputBuffer.Length);
  125. // Send the input to the user
  126. Write(context.MemoryManager, OutputBuffer, CountMax, inputBufferInt, context.SampleCount, WriteOffset, UpdateCount);
  127. // Convert back to float just in case it's reused
  128. DataSourceHelper.ToFloat(inputBuffer, inputBufferInt, inputBuffer.Length);
  129. // Retrieve the input from user
  130. uint readResult = Read(context.MemoryManager, InputBuffer, CountMax, outputBufferInt, context.SampleCount, WriteOffset, UpdateCount);
  131. // Convert the outputBuffer back to the target format of the renderer (float)
  132. DataSourceHelper.ToFloat(outputBuffer, outputBufferInt, outputBuffer.Length);
  133. if (readResult != context.SampleCount)
  134. {
  135. outputBuffer.Slice((int)readResult, (int)context.SampleCount - (int)readResult).Fill(0);
  136. }
  137. }
  138. else
  139. {
  140. AuxiliaryBufferInfo.Reset(context.MemoryManager, BufferInfo.SendBufferInfo);
  141. AuxiliaryBufferInfo.Reset(context.MemoryManager, BufferInfo.ReturnBufferInfo);
  142. if (InputBufferIndex != OutputBufferIndex)
  143. {
  144. inputBuffer.CopyTo(outputBuffer);
  145. }
  146. }
  147. }
  148. }
  149. }