AuxiliaryBufferCommand.cs 6.6 KB

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