CommandList.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Ryujinx.Audio.Integration;
  2. using Ryujinx.Audio.Renderer.Server;
  3. using Ryujinx.Common;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Memory;
  6. using System;
  7. using System.Buffers;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Runtime.CompilerServices;
  11. namespace Ryujinx.Audio.Renderer.Dsp.Command
  12. {
  13. public class CommandList : IDisposable
  14. {
  15. public ulong StartTime { get; private set; }
  16. public ulong EndTime { get; private set; }
  17. public uint SampleCount { get; }
  18. public uint SampleRate { get; }
  19. public Memory<float> Buffers { get; }
  20. public uint BufferCount { get; }
  21. public List<ICommand> Commands { get; }
  22. public IVirtualMemoryManager MemoryManager { get; }
  23. public IHardwareDevice OutputDevice { get; private set; }
  24. private readonly int _sampleCount;
  25. private readonly int _buffersEntryCount;
  26. private readonly MemoryHandle _buffersMemoryHandle;
  27. public CommandList(AudioRenderSystem renderSystem) : this(renderSystem.MemoryManager,
  28. renderSystem.GetMixBuffer(),
  29. renderSystem.GetSampleCount(),
  30. renderSystem.GetSampleRate(),
  31. renderSystem.GetMixBufferCount(),
  32. renderSystem.GetVoiceChannelCountMax())
  33. {
  34. }
  35. public CommandList(IVirtualMemoryManager memoryManager, Memory<float> mixBuffer, uint sampleCount, uint sampleRate, uint mixBufferCount, uint voiceChannelCountMax)
  36. {
  37. SampleCount = sampleCount;
  38. _sampleCount = (int)SampleCount;
  39. SampleRate = sampleRate;
  40. BufferCount = mixBufferCount + voiceChannelCountMax;
  41. Buffers = mixBuffer;
  42. Commands = new List<ICommand>();
  43. MemoryManager = memoryManager;
  44. _buffersEntryCount = Buffers.Length;
  45. _buffersMemoryHandle = Buffers.Pin();
  46. }
  47. public void AddCommand(ICommand command)
  48. {
  49. Commands.Add(command);
  50. }
  51. public void AddCommand<T>(T command) where T : unmanaged, ICommand
  52. {
  53. throw new NotImplementedException();
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public unsafe IntPtr GetBufferPointer(int index)
  57. {
  58. if (index >= 0 && index < _buffersEntryCount)
  59. {
  60. return (IntPtr)((float*)_buffersMemoryHandle.Pointer + index * _sampleCount);
  61. }
  62. throw new ArgumentOutOfRangeException();
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public unsafe void ClearBuffer(int index)
  66. {
  67. Unsafe.InitBlock((void*)GetBufferPointer(index), 0, SampleCount);
  68. }
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public unsafe void ClearBuffers()
  71. {
  72. Unsafe.InitBlock(_buffersMemoryHandle.Pointer, 0, (uint)_buffersEntryCount * sizeof(float));
  73. }
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public unsafe void CopyBuffer(int outputBufferIndex, int inputBufferIndex)
  76. {
  77. Unsafe.CopyBlock((void*)GetBufferPointer(outputBufferIndex), (void*)GetBufferPointer(inputBufferIndex), SampleCount);
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. public Span<float> GetBuffer(int index)
  81. {
  82. if (index < 0 || index >= _buffersEntryCount)
  83. {
  84. return Span<float>.Empty;
  85. }
  86. unsafe
  87. {
  88. return new Span<float>((float*)_buffersMemoryHandle.Pointer + index * _sampleCount, _sampleCount);
  89. }
  90. }
  91. public ulong GetTimeElapsedSinceDspStartedProcessing()
  92. {
  93. return (ulong)PerformanceCounter.ElapsedNanoseconds - StartTime;
  94. }
  95. public void Process(IHardwareDevice outputDevice)
  96. {
  97. OutputDevice = outputDevice;
  98. StartTime = (ulong)PerformanceCounter.ElapsedNanoseconds;
  99. foreach (ICommand command in Commands)
  100. {
  101. if (command.Enabled)
  102. {
  103. bool shouldMeter = command.ShouldMeter();
  104. long startTime = 0;
  105. if (shouldMeter)
  106. {
  107. startTime = PerformanceCounter.ElapsedNanoseconds;
  108. }
  109. command.Process(this);
  110. if (shouldMeter)
  111. {
  112. ulong effectiveElapsedTime = (ulong)(PerformanceCounter.ElapsedNanoseconds - startTime);
  113. if (effectiveElapsedTime > command.EstimatedProcessingTime)
  114. {
  115. Logger.Warning?.Print(LogClass.AudioRenderer, $"Command {command.GetType().Name} took {effectiveElapsedTime}ns (expected {command.EstimatedProcessingTime}ns)");
  116. }
  117. }
  118. }
  119. }
  120. EndTime = (ulong)PerformanceCounter.ElapsedNanoseconds;
  121. }
  122. public void Dispose()
  123. {
  124. _buffersMemoryHandle.Dispose();
  125. }
  126. }
  127. }