CommandList.cs 6.0 KB

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