AuxiliaryBufferCommand.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Ryujinx.Audio.Renderer.Common;
  18. using Ryujinx.Cpu;
  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. private uint Read(MemoryManager memoryManager, ulong bufferAddress, uint countMax, Span<int> outBuffer, uint count, uint readOffset, uint updateCount)
  58. {
  59. if (countMax == 0 || bufferAddress == 0)
  60. {
  61. return 0;
  62. }
  63. uint targetReadOffset = readOffset + AuxiliaryBufferInfo.GetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo);
  64. if (targetReadOffset > countMax)
  65. {
  66. return 0;
  67. }
  68. uint remaining = count;
  69. uint outBufferOffset = 0;
  70. while (remaining != 0)
  71. {
  72. uint countToWrite = Math.Min(countMax - targetReadOffset, remaining);
  73. memoryManager.Read(bufferAddress + targetReadOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(outBuffer.Slice((int)outBufferOffset, (int)countToWrite)));
  74. targetReadOffset = (targetReadOffset + countToWrite) % countMax;
  75. remaining -= countToWrite;
  76. outBufferOffset += countToWrite;
  77. }
  78. if (updateCount != 0)
  79. {
  80. uint newReadOffset = (AuxiliaryBufferInfo.GetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo) + updateCount) % countMax;
  81. AuxiliaryBufferInfo.SetReadOffset(memoryManager, BufferInfo.ReturnBufferInfo, newReadOffset);
  82. }
  83. return count;
  84. }
  85. private uint Write(MemoryManager memoryManager, ulong outBufferAddress, uint countMax, ReadOnlySpan<int> buffer, uint count, uint writeOffset, uint updateCount)
  86. {
  87. if (countMax == 0 || outBufferAddress == 0)
  88. {
  89. return 0;
  90. }
  91. uint targetWriteOffset = writeOffset + AuxiliaryBufferInfo.GetWriteOffset(memoryManager, BufferInfo.SendBufferInfo);
  92. if (targetWriteOffset > countMax)
  93. {
  94. return 0;
  95. }
  96. uint remaining = count;
  97. uint inBufferOffset = 0;
  98. while (remaining != 0)
  99. {
  100. uint countToWrite = Math.Min(countMax - targetWriteOffset, remaining);
  101. memoryManager.Write(outBufferAddress + targetWriteOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(buffer.Slice((int)inBufferOffset, (int)countToWrite)));
  102. targetWriteOffset = (targetWriteOffset + countToWrite) % countMax;
  103. remaining -= countToWrite;
  104. inBufferOffset += countToWrite;
  105. }
  106. if (updateCount != 0)
  107. {
  108. uint newWriteOffset = (AuxiliaryBufferInfo.GetWriteOffset(memoryManager, BufferInfo.SendBufferInfo) + updateCount) % countMax;
  109. AuxiliaryBufferInfo.SetWriteOffset(memoryManager, BufferInfo.SendBufferInfo, newWriteOffset);
  110. }
  111. return count;
  112. }
  113. public void Process(CommandList context)
  114. {
  115. Span<float> inputBuffer = context.GetBuffer((int)InputBufferIndex);
  116. Span<float> outputBuffer = context.GetBuffer((int)OutputBufferIndex);
  117. if (IsEffectEnabled)
  118. {
  119. Span<int> inputBufferInt = MemoryMarshal.Cast<float, int>(inputBuffer);
  120. Span<int> outputBufferInt = MemoryMarshal.Cast<float, int>(outputBuffer);
  121. // Convert input data to the target format for user (int)
  122. DataSourceHelper.ToInt(inputBufferInt, inputBuffer, outputBuffer.Length);
  123. // Send the input to the user
  124. Write(context.MemoryManager, OutputBuffer, CountMax, inputBufferInt, context.SampleCount, WriteOffset, UpdateCount);
  125. // Convert back to float just in case it's reused
  126. DataSourceHelper.ToFloat(inputBuffer, inputBufferInt, inputBuffer.Length);
  127. // Retrieve the input from user
  128. uint readResult = Read(context.MemoryManager, InputBuffer, CountMax, outputBufferInt, context.SampleCount, WriteOffset, UpdateCount);
  129. // Convert the outputBuffer back to the target format of the renderer (float)
  130. DataSourceHelper.ToFloat(outputBuffer, outputBufferInt, outputBuffer.Length);
  131. if (readResult != context.SampleCount)
  132. {
  133. outputBuffer.Slice((int)readResult, (int)context.SampleCount - (int)readResult).Fill(0);
  134. }
  135. }
  136. else
  137. {
  138. MemoryHelper.FillWithZeros(context.MemoryManager, (long)BufferInfo.SendBufferInfo, Unsafe.SizeOf<AuxiliaryBufferInfo>());
  139. MemoryHelper.FillWithZeros(context.MemoryManager, (long)BufferInfo.ReturnBufferInfo, Unsafe.SizeOf<AuxiliaryBufferInfo>());
  140. if (InputBufferIndex != OutputBufferIndex)
  141. {
  142. inputBuffer.CopyTo(outputBuffer);
  143. }
  144. }
  145. }
  146. }
  147. }