CommandList.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2019-2020 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.Renderer.Integration;
  18. using Ryujinx.Audio.Renderer.Server;
  19. using Ryujinx.Common;
  20. using Ryujinx.Common.Logging;
  21. using Ryujinx.Cpu;
  22. using System;
  23. using System.Collections.Generic;
  24. namespace Ryujinx.Audio.Renderer.Dsp.Command
  25. {
  26. public class CommandList
  27. {
  28. public ulong StartTime { get; private set; }
  29. public ulong EndTime { get; private set; }
  30. public uint SampleCount { get; }
  31. public uint SampleRate { get; }
  32. public Memory<float> Buffers { get; }
  33. public uint BufferCount { get; }
  34. public List<ICommand> Commands { get; }
  35. public MemoryManager MemoryManager { get; }
  36. public HardwareDevice OutputDevice { get; private set; }
  37. public CommandList(AudioRenderSystem renderSystem) : this(renderSystem.MemoryManager,
  38. renderSystem.GetMixBuffer(),
  39. renderSystem.GetSampleCount(),
  40. renderSystem.GetSampleRate(),
  41. renderSystem.GetMixBufferCount(),
  42. renderSystem.GetVoiceChannelCountMax())
  43. {
  44. }
  45. public CommandList(MemoryManager memoryManager, Memory<float> mixBuffer, uint sampleCount, uint sampleRate, uint mixBufferCount, uint voiceChannelCountMax)
  46. {
  47. SampleCount = sampleCount;
  48. SampleRate = sampleRate;
  49. BufferCount = mixBufferCount + voiceChannelCountMax;
  50. Buffers = mixBuffer;
  51. Commands = new List<ICommand>();
  52. MemoryManager = memoryManager;
  53. }
  54. public void AddCommand(ICommand command)
  55. {
  56. Commands.Add(command);
  57. }
  58. public void AddCommand<T>(T command) where T : unmanaged, ICommand
  59. {
  60. throw new NotImplementedException();
  61. }
  62. public Memory<float> GetBufferMemory(int index)
  63. {
  64. return Buffers.Slice(index * (int)SampleCount, (int)SampleCount);
  65. }
  66. public Span<float> GetBuffer(int index)
  67. {
  68. return Buffers.Span.Slice(index * (int)SampleCount, (int)SampleCount);
  69. }
  70. public ulong GetTimeElapsedSinceDspStartedProcessing()
  71. {
  72. return (ulong)PerformanceCounter.ElapsedNanoseconds - StartTime;
  73. }
  74. public void Process(HardwareDevice outputDevice)
  75. {
  76. OutputDevice = outputDevice;
  77. StartTime = (ulong)PerformanceCounter.ElapsedNanoseconds;
  78. foreach (ICommand command in Commands)
  79. {
  80. if (command.Enabled)
  81. {
  82. bool shouldMeter = command.ShouldMeter();
  83. long startTime = 0;
  84. if (shouldMeter)
  85. {
  86. startTime = PerformanceCounter.ElapsedNanoseconds;
  87. }
  88. command.Process(this);
  89. if (shouldMeter)
  90. {
  91. ulong effectiveElapsedTime = (ulong)(PerformanceCounter.ElapsedNanoseconds - startTime);
  92. if (effectiveElapsedTime > command.EstimatedProcessingTime)
  93. {
  94. Logger.Warning?.Print(LogClass.AudioRenderer, $"Command {command.GetType().Name} took {effectiveElapsedTime}ns (expected {command.EstimatedProcessingTime}ns)");
  95. }
  96. }
  97. }
  98. }
  99. EndTime = (ulong)PerformanceCounter.ElapsedNanoseconds;
  100. }
  101. }
  102. }