CommandList.cs 5.3 KB

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