CaptureBufferCommand.cs 5.3 KB

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