CaptureBufferCommand.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Ryujinx.Audio.Renderer.Dsp.State;
  2. using Ryujinx.Audio.Renderer.Parameter;
  3. using Ryujinx.Memory;
  4. using System;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using static Ryujinx.Audio.Renderer.Dsp.State.AuxiliaryBufferHeader;
  8. using CpuAddress = System.UInt64;
  9. namespace Ryujinx.Audio.Renderer.Dsp.Command
  10. {
  11. public class CaptureBufferCommand : ICommand
  12. {
  13. public bool Enabled { get; set; }
  14. public int NodeId { get; }
  15. public CommandType CommandType => CommandType.CaptureBuffer;
  16. public ulong EstimatedProcessingTime { get; set; }
  17. public uint InputBufferIndex { get; }
  18. public ulong CpuBufferInfoAddress { get; }
  19. public ulong DspBufferInfoAddress { 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 CaptureBufferCommand(uint bufferOffset, byte inputBufferOffset, ulong sendBufferInfo, bool isEnabled,
  26. uint countMax, CpuAddress outputBuffer, uint updateCount, uint writeOffset, int nodeId)
  27. {
  28. Enabled = true;
  29. NodeId = nodeId;
  30. InputBufferIndex = bufferOffset + inputBufferOffset;
  31. CpuBufferInfoAddress = sendBufferInfo;
  32. DspBufferInfoAddress = sendBufferInfo + (ulong)Unsafe.SizeOf<AuxiliaryBufferHeader>();
  33. OutputBuffer = outputBuffer;
  34. CountMax = countMax;
  35. UpdateCount = updateCount;
  36. WriteOffset = writeOffset;
  37. IsEffectEnabled = isEnabled;
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. private uint Write(IVirtualMemoryManager memoryManager, ulong outBufferAddress, uint countMax, ReadOnlySpan<int> buffer, uint count, uint writeOffset, uint updateCount)
  41. {
  42. if (countMax == 0 || outBufferAddress == 0)
  43. {
  44. return 0;
  45. }
  46. uint targetWriteOffset = writeOffset + AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress);
  47. if (targetWriteOffset > countMax)
  48. {
  49. return 0;
  50. }
  51. uint remaining = count;
  52. uint inBufferOffset = 0;
  53. while (remaining != 0)
  54. {
  55. uint countToWrite = Math.Min(countMax - targetWriteOffset, remaining);
  56. memoryManager.Write(outBufferAddress + targetWriteOffset * sizeof(int), MemoryMarshal.Cast<int, byte>(buffer.Slice((int)inBufferOffset, (int)countToWrite)));
  57. targetWriteOffset = (targetWriteOffset + countToWrite) % countMax;
  58. remaining -= countToWrite;
  59. inBufferOffset += countToWrite;
  60. }
  61. if (updateCount != 0)
  62. {
  63. uint dspTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, DspBufferInfoAddress);
  64. uint cpuTotalSampleCount = AuxiliaryBufferInfo.GetTotalSampleCount(memoryManager, CpuBufferInfoAddress);
  65. uint totalSampleCountDiff = dspTotalSampleCount - cpuTotalSampleCount;
  66. if (totalSampleCountDiff >= countMax)
  67. {
  68. uint dspLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, DspBufferInfoAddress);
  69. uint cpuLostSampleCount = AuxiliaryBufferInfo.GetLostSampleCount(memoryManager, CpuBufferInfoAddress);
  70. uint lostSampleCountDiff = dspLostSampleCount - cpuLostSampleCount;
  71. uint newLostSampleCount = lostSampleCountDiff + updateCount;
  72. if (lostSampleCountDiff > newLostSampleCount)
  73. {
  74. newLostSampleCount = cpuLostSampleCount - 1;
  75. }
  76. AuxiliaryBufferInfo.SetLostSampleCount(memoryManager, DspBufferInfoAddress, newLostSampleCount);
  77. }
  78. uint newWriteOffset = (AuxiliaryBufferInfo.GetWriteOffset(memoryManager, DspBufferInfoAddress) + updateCount) % countMax;
  79. AuxiliaryBufferInfo.SetWriteOffset(memoryManager, DspBufferInfoAddress, newWriteOffset);
  80. uint newTotalSampleCount = totalSampleCountDiff + newWriteOffset;
  81. AuxiliaryBufferInfo.SetTotalSampleCount(memoryManager, DspBufferInfoAddress, newTotalSampleCount);
  82. }
  83. return count;
  84. }
  85. public void Process(CommandList context)
  86. {
  87. Span<float> inputBuffer = context.GetBuffer((int)InputBufferIndex);
  88. if (IsEffectEnabled)
  89. {
  90. Span<int> inputBufferInt = MemoryMarshal.Cast<float, int>(inputBuffer);
  91. // Convert input data to the target format for user (int)
  92. DataSourceHelper.ToInt(inputBufferInt, inputBuffer, inputBuffer.Length);
  93. // Send the input to the user
  94. Write(context.MemoryManager, OutputBuffer, CountMax, inputBufferInt, context.SampleCount, WriteOffset, UpdateCount);
  95. // Convert back to float
  96. DataSourceHelper.ToFloat(inputBuffer, inputBufferInt, inputBuffer.Length);
  97. }
  98. else
  99. {
  100. AuxiliaryBufferInfo.Reset(context.MemoryManager, DspBufferInfoAddress);
  101. }
  102. }
  103. }
  104. }