CaptureBufferCommand.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Dsp.State;
  18. using Ryujinx.Audio.Renderer.Parameter;
  19. using Ryujinx.Memory;
  20. using System;
  21. using System.Runtime.CompilerServices;
  22. using System.Runtime.InteropServices;
  23. using static Ryujinx.Audio.Renderer.Dsp.State.AuxiliaryBufferHeader;
  24. using CpuAddress = System.UInt64;
  25. namespace Ryujinx.Audio.Renderer.Dsp.Command
  26. {
  27. public class CaptureBufferCommand : ICommand
  28. {
  29. public bool Enabled { get; set; }
  30. public int NodeId { get; }
  31. public CommandType CommandType => CommandType.CaptureBuffer;
  32. public ulong EstimatedProcessingTime { get; set; }
  33. public uint InputBufferIndex { get; }
  34. public ulong CpuBufferInfoAddress { get; }
  35. public ulong DspBufferInfoAddress { 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 CaptureBufferCommand(uint bufferOffset, byte inputBufferOffset, ulong sendBufferInfo, bool isEnabled,
  42. uint countMax, CpuAddress outputBuffer, uint updateCount, uint writeOffset, int nodeId)
  43. {
  44. Enabled = true;
  45. NodeId = nodeId;
  46. InputBufferIndex = bufferOffset + inputBufferOffset;
  47. CpuBufferInfoAddress = sendBufferInfo;
  48. DspBufferInfoAddress = sendBufferInfo + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>();
  49. OutputBuffer = outputBuffer;
  50. CountMax = countMax;
  51. UpdateCount = updateCount;
  52. WriteOffset = writeOffset;
  53. IsEffectEnabled = isEnabled;
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. private uint Write(IVirtualMemoryManager memoryManager, ulong outBufferAddress, uint countMax, ReadOnlySpan<int> buffer, uint count, uint writeOffset, uint updateCount)
  57. {
  58. if (countMax == 0 || outBufferAddress == 0)
  59. {
  60. return 0;
  61. }
  62. uint targetWriteOffset = writeOffset + AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress);
  63. if (targetWriteOffset > countMax)
  64. {
  65. return 0;
  66. }
  67. uint remaining = count;
  68. uint inBufferOffset = 0;
  69. while (remaining != 0)
  70. {
  71. uint countToWrite = Math.Min(countMax - targetWriteOffset, remaining);
  72. memoryManager.Write(outBufferAddress + targetWriteOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(buffer.Slice((int)inBufferOffset, (int)countToWrite)));
  73. targetWriteOffset = (targetWriteOffset + countToWrite) % countMax;
  74. remaining -= countToWrite;
  75. inBufferOffset += countToWrite;
  76. }
  77. if (updateCount != 0)
  78. {
  79. uint dspTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, DspBufferInfoAddress);
  80. uint cpuTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, CpuBufferInfoAddress);
  81. uint totalSampleCountDiff = dspTotalSampleCount - cpuTotalSampleCount;
  82. if (totalSampleCountDiff >= countMax)
  83. {
  84. uint dspLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, DspBufferInfoAddress);
  85. uint cpuLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, CpuBufferInfoAddress);
  86. uint lostSampleCountDiff = dspLostSampleCount - cpuLostSampleCount;
  87. uint newLostSampleCount = lostSampleCountDiff + updateCount;
  88. if (lostSampleCountDiff > newLostSampleCount)
  89. {
  90. newLostSampleCount = cpuLostSampleCount - 1;
  91. }
  92. AuxiliaryBufferInfo.SetLostSampleCount(memoryManager, DspBufferInfoAddress, newLostSampleCount);
  93. }
  94. uint newWriteOffset = (AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress) + updateCount) % countMax;
  95. AuxiliaryBufferInfo.SetWriteOffset(memoryManager, DspBufferInfoAddress, newWriteOffset);
  96. uint newTotalSampleCount = totalSampleCountDiff + newWriteOffset;
  97. AuxiliaryBufferInfo.SetTotalSampleCount(memoryManager, DspBufferInfoAddress, newTotalSampleCount);
  98. }
  99. return count;
  100. }
  101. public void Process(CommandList context)
  102. {
  103. Span<float> inputBuffer = context.GetBuffer((int)InputBufferIndex);
  104. if (IsEffectEnabled)
  105. {
  106. Span<int> inputBufferInt = MemoryMarshal.Cast<float, int>(inputBuffer);
  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
  112. DataSourceHelper.ToFloat(inputBuffer, inputBufferInt, inputBuffer.Length);
  113. }
  114. else
  115. {
  116. AuxiliaryBufferInfo.Reset(context.MemoryManager, DspBufferInfoAddress);
  117. }
  118. }
  119. }
  120. }