AudioRenderSystem.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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.Common;
  19. using Ryujinx.Audio.Renderer.Dsp.Command;
  20. using Ryujinx.Audio.Renderer.Parameter;
  21. using Ryujinx.Audio.Renderer.Server.Effect;
  22. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  23. using Ryujinx.Audio.Renderer.Server.Mix;
  24. using Ryujinx.Audio.Renderer.Server.Performance;
  25. using Ryujinx.Audio.Renderer.Server.Sink;
  26. using Ryujinx.Audio.Renderer.Server.Splitter;
  27. using Ryujinx.Audio.Renderer.Server.Types;
  28. using Ryujinx.Audio.Renderer.Server.Upsampler;
  29. using Ryujinx.Audio.Renderer.Server.Voice;
  30. using Ryujinx.Audio.Renderer.Utils;
  31. using Ryujinx.Common;
  32. using Ryujinx.Common.Logging;
  33. using Ryujinx.Memory;
  34. using System;
  35. using System.Buffers;
  36. using System.Diagnostics;
  37. using System.Threading;
  38. using CpuAddress = System.UInt64;
  39. namespace Ryujinx.Audio.Renderer.Server
  40. {
  41. public class AudioRenderSystem : IDisposable
  42. {
  43. private object _lock = new object();
  44. private AudioRendererExecutionMode _executionMode;
  45. private IWritableEvent _systemEvent;
  46. private ManualResetEvent _terminationEvent;
  47. private MemoryPoolState _dspMemoryPoolState;
  48. private VoiceContext _voiceContext;
  49. private MixContext _mixContext;
  50. private SinkContext _sinkContext;
  51. private SplitterContext _splitterContext;
  52. private EffectContext _effectContext;
  53. private PerformanceManager _performanceManager;
  54. private UpsamplerManager _upsamplerManager;
  55. private bool _isActive;
  56. private BehaviourContext _behaviourContext;
  57. private ulong _totalElapsedTicksUpdating;
  58. private ulong _totalElapsedTicks;
  59. private int _sessionId;
  60. private Memory<MemoryPoolState> _memoryPools;
  61. private uint _sampleRate;
  62. private uint _sampleCount;
  63. private uint _mixBufferCount;
  64. private uint _voiceChannelCountMax;
  65. private uint _upsamplerCount;
  66. private uint _memoryPoolCount;
  67. private uint _processHandle;
  68. private ulong _appletResourceId;
  69. private WritableRegion _workBufferRegion;
  70. private MemoryHandle _workBufferMemoryPin;
  71. private Memory<float> _mixBuffer;
  72. private Memory<float> _depopBuffer;
  73. private uint _renderingTimeLimitPercent;
  74. private bool _voiceDropEnabled;
  75. private uint _voiceDropCount;
  76. private bool _isDspRunningBehind;
  77. private ICommandProcessingTimeEstimator _commandProcessingTimeEstimator;
  78. private Memory<byte> _performanceBuffer;
  79. public IVirtualMemoryManager MemoryManager { get; private set; }
  80. private ulong _elapsedFrameCount;
  81. private ulong _renderingStartTick;
  82. private AudioRendererManager _manager;
  83. public AudioRenderSystem(AudioRendererManager manager, IWritableEvent systemEvent)
  84. {
  85. _manager = manager;
  86. _terminationEvent = new ManualResetEvent(false);
  87. _dspMemoryPoolState = MemoryPoolState.Create(MemoryPoolState.LocationType.Dsp);
  88. _voiceContext = new VoiceContext();
  89. _mixContext = new MixContext();
  90. _sinkContext = new SinkContext();
  91. _splitterContext = new SplitterContext();
  92. _effectContext = new EffectContext();
  93. _commandProcessingTimeEstimator = null;
  94. _systemEvent = systemEvent;
  95. _behaviourContext = new BehaviourContext();
  96. _totalElapsedTicksUpdating = 0;
  97. _sessionId = 0;
  98. }
  99. public ResultCode Initialize(ref AudioRendererConfiguration parameter, uint processHandle, CpuAddress workBuffer, ulong workBufferSize, int sessionId, ulong appletResourceId, IVirtualMemoryManager memoryManager)
  100. {
  101. if (!BehaviourContext.CheckValidRevision(parameter.Revision))
  102. {
  103. return ResultCode.OperationFailed;
  104. }
  105. if (GetWorkBufferSize(ref parameter) > workBufferSize)
  106. {
  107. return ResultCode.WorkBufferTooSmall;
  108. }
  109. Debug.Assert(parameter.RenderingDevice == AudioRendererRenderingDevice.Dsp && parameter.ExecutionMode == AudioRendererExecutionMode.Auto);
  110. Logger.Info?.Print(LogClass.AudioRenderer, $"Initializing with REV{BehaviourContext.GetRevisionNumber(parameter.Revision)}");
  111. _behaviourContext.SetUserRevision(parameter.Revision);
  112. _sampleRate = parameter.SampleRate;
  113. _sampleCount = parameter.SampleCount;
  114. _mixBufferCount = parameter.MixBufferCount;
  115. _voiceChannelCountMax = Constants.VoiceChannelCountMax;
  116. _upsamplerCount = parameter.SinkCount + parameter.SubMixBufferCount;
  117. _appletResourceId = appletResourceId;
  118. _memoryPoolCount = parameter.EffectCount + parameter.VoiceCount * Constants.VoiceWaveBufferCount;
  119. _executionMode = parameter.ExecutionMode;
  120. _sessionId = sessionId;
  121. MemoryManager = memoryManager;
  122. if (memoryManager is IRefCounted rc)
  123. {
  124. rc.IncrementReferenceCount();
  125. }
  126. WorkBufferAllocator workBufferAllocator;
  127. _workBufferRegion = MemoryManager.GetWritableRegion(workBuffer, (int)workBufferSize);
  128. _workBufferRegion.Memory.Span.Fill(0);
  129. _workBufferMemoryPin = _workBufferRegion.Memory.Pin();
  130. workBufferAllocator = new WorkBufferAllocator(_workBufferRegion.Memory);
  131. PoolMapper poolMapper = new PoolMapper(processHandle, false);
  132. poolMapper.InitializeSystemPool(ref _dspMemoryPoolState, workBuffer, workBufferSize);
  133. _mixBuffer = workBufferAllocator.Allocate<float>(_sampleCount * (_voiceChannelCountMax + _mixBufferCount), 0x10);
  134. if (_mixBuffer.IsEmpty)
  135. {
  136. return ResultCode.WorkBufferTooSmall;
  137. }
  138. Memory<float> upSamplerWorkBuffer = workBufferAllocator.Allocate<float>(Constants.TargetSampleCount * (_voiceChannelCountMax + _mixBufferCount) * _upsamplerCount, 0x10);
  139. if (upSamplerWorkBuffer.IsEmpty)
  140. {
  141. return ResultCode.WorkBufferTooSmall;
  142. }
  143. _depopBuffer = workBufferAllocator.Allocate<float>((ulong)BitUtils.AlignUp(parameter.MixBufferCount, Constants.BufferAlignment), Constants.BufferAlignment);
  144. if (_depopBuffer.IsEmpty)
  145. {
  146. return ResultCode.WorkBufferTooSmall;
  147. }
  148. // Invalidate DSP cache on what was currently allocated with workBuffer.
  149. AudioProcessorMemoryManager.InvalidateDspCache(_dspMemoryPoolState.Translate(workBuffer, workBufferAllocator.Offset), workBufferAllocator.Offset);
  150. Debug.Assert((workBufferAllocator.Offset % Constants.BufferAlignment) == 0);
  151. Memory<VoiceState> voices = workBufferAllocator.Allocate<VoiceState>(parameter.VoiceCount, VoiceState.Alignment);
  152. if (voices.IsEmpty)
  153. {
  154. return ResultCode.WorkBufferTooSmall;
  155. }
  156. foreach (ref VoiceState voice in voices.Span)
  157. {
  158. voice.Initialize();
  159. }
  160. // A pain to handle as we can't have VoiceState*, use indices to be a bit more safe
  161. Memory<int> sortedVoices = workBufferAllocator.Allocate<int>(parameter.VoiceCount, 0x10);
  162. if (sortedVoices.IsEmpty)
  163. {
  164. return ResultCode.WorkBufferTooSmall;
  165. }
  166. // Clear memory (use -1 as it's an invalid index)
  167. sortedVoices.Span.Fill(-1);
  168. Memory<VoiceChannelResource> voiceChannelResources = workBufferAllocator.Allocate<VoiceChannelResource>(parameter.VoiceCount, VoiceChannelResource.Alignment);
  169. if (voiceChannelResources.IsEmpty)
  170. {
  171. return ResultCode.WorkBufferTooSmall;
  172. }
  173. for (uint id = 0; id < voiceChannelResources.Length; id++)
  174. {
  175. ref VoiceChannelResource voiceChannelResource = ref voiceChannelResources.Span[(int)id];
  176. voiceChannelResource.Id = id;
  177. voiceChannelResource.IsUsed = false;
  178. }
  179. Memory<VoiceUpdateState> voiceUpdateStates = workBufferAllocator.Allocate<VoiceUpdateState>(parameter.VoiceCount, VoiceUpdateState.Align);
  180. if (voiceUpdateStates.IsEmpty)
  181. {
  182. return ResultCode.WorkBufferTooSmall;
  183. }
  184. uint mixesCount = parameter.SubMixBufferCount + 1;
  185. Memory<MixState> mixes = workBufferAllocator.Allocate<MixState>(mixesCount, MixState.Alignment);
  186. if (mixes.IsEmpty)
  187. {
  188. return ResultCode.WorkBufferTooSmall;
  189. }
  190. if (parameter.EffectCount == 0)
  191. {
  192. foreach (ref MixState mix in mixes.Span)
  193. {
  194. mix = new MixState(Memory<int>.Empty, ref _behaviourContext);
  195. }
  196. }
  197. else
  198. {
  199. Memory<int> effectProcessingOrderArray = workBufferAllocator.Allocate<int>(parameter.EffectCount * mixesCount, 0x10);
  200. foreach (ref MixState mix in mixes.Span)
  201. {
  202. mix = new MixState(effectProcessingOrderArray.Slice(0, (int)parameter.EffectCount), ref _behaviourContext);
  203. effectProcessingOrderArray = effectProcessingOrderArray.Slice((int)parameter.EffectCount);
  204. }
  205. }
  206. // Initialize the final mix id
  207. mixes.Span[0].MixId = Constants.FinalMixId;
  208. Memory<int> sortedMixesState = workBufferAllocator.Allocate<int>(mixesCount, 0x10);
  209. if (sortedMixesState.IsEmpty)
  210. {
  211. return ResultCode.WorkBufferTooSmall;
  212. }
  213. // Clear memory (use -1 as it's an invalid index)
  214. sortedMixesState.Span.Fill(-1);
  215. Memory<byte> nodeStatesWorkBuffer = Memory<byte>.Empty;
  216. Memory<byte> edgeMatrixWorkBuffer = Memory<byte>.Empty;
  217. if (_behaviourContext.IsSplitterSupported())
  218. {
  219. nodeStatesWorkBuffer = workBufferAllocator.Allocate((uint)NodeStates.GetWorkBufferSize((int)mixesCount), 1);
  220. edgeMatrixWorkBuffer = workBufferAllocator.Allocate((uint)EdgeMatrix.GetWorkBufferSize((int)mixesCount), 1);
  221. if (nodeStatesWorkBuffer.IsEmpty || edgeMatrixWorkBuffer.IsEmpty)
  222. {
  223. return ResultCode.WorkBufferTooSmall;
  224. }
  225. }
  226. _mixContext.Initialize(sortedMixesState, mixes, nodeStatesWorkBuffer, edgeMatrixWorkBuffer);
  227. _memoryPools = workBufferAllocator.Allocate<MemoryPoolState>(_memoryPoolCount, MemoryPoolState.Alignment);
  228. if (_memoryPools.IsEmpty)
  229. {
  230. return ResultCode.WorkBufferTooSmall;
  231. }
  232. foreach (ref MemoryPoolState state in _memoryPools.Span)
  233. {
  234. state = MemoryPoolState.Create(MemoryPoolState.LocationType.Cpu);
  235. }
  236. if (!_splitterContext.Initialize(ref _behaviourContext, ref parameter, workBufferAllocator))
  237. {
  238. return ResultCode.WorkBufferTooSmall;
  239. }
  240. _processHandle = processHandle;
  241. _upsamplerManager = new UpsamplerManager(upSamplerWorkBuffer, _upsamplerCount);
  242. _effectContext.Initialize(parameter.EffectCount, _behaviourContext.IsEffectInfoVersion2Supported() ? parameter.EffectCount : 0);
  243. _sinkContext.Initialize(parameter.SinkCount);
  244. Memory<VoiceUpdateState> voiceUpdateStatesDsp = workBufferAllocator.Allocate<VoiceUpdateState>(parameter.VoiceCount, VoiceUpdateState.Align);
  245. if (voiceUpdateStatesDsp.IsEmpty)
  246. {
  247. return ResultCode.WorkBufferTooSmall;
  248. }
  249. _voiceContext.Initialize(sortedVoices, voices, voiceChannelResources, voiceUpdateStates, voiceUpdateStatesDsp, parameter.VoiceCount);
  250. if (parameter.PerformanceMetricFramesCount > 0)
  251. {
  252. ulong performanceBufferSize = PerformanceManager.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter, ref _behaviourContext) * (parameter.PerformanceMetricFramesCount + 1) + 0xC;
  253. _performanceBuffer = workBufferAllocator.Allocate(performanceBufferSize, Constants.BufferAlignment);
  254. if (_performanceBuffer.IsEmpty)
  255. {
  256. return ResultCode.WorkBufferTooSmall;
  257. }
  258. _performanceManager = PerformanceManager.Create(_performanceBuffer, ref parameter, _behaviourContext);
  259. }
  260. else
  261. {
  262. _performanceManager = null;
  263. }
  264. _totalElapsedTicksUpdating = 0;
  265. _totalElapsedTicks = 0;
  266. _renderingTimeLimitPercent = 100;
  267. _voiceDropEnabled = parameter.VoiceDropEnabled && _executionMode == AudioRendererExecutionMode.Auto;
  268. AudioProcessorMemoryManager.InvalidateDataCache(workBuffer, workBufferSize);
  269. _processHandle = processHandle;
  270. _elapsedFrameCount = 0;
  271. switch (_behaviourContext.GetCommandProcessingTimeEstimatorVersion())
  272. {
  273. case 1:
  274. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion1(_sampleCount, _mixBufferCount);
  275. break;
  276. case 2:
  277. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion2(_sampleCount, _mixBufferCount);
  278. break;
  279. case 3:
  280. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion3(_sampleCount, _mixBufferCount);
  281. break;
  282. default:
  283. throw new NotImplementedException($"Unsupported processing time estimator version {_behaviourContext.GetCommandProcessingTimeEstimatorVersion()}.");
  284. }
  285. return ResultCode.Success;
  286. }
  287. public void Start()
  288. {
  289. Logger.Info?.Print(LogClass.AudioRenderer, $"Starting renderer id {_sessionId}");
  290. lock (_lock)
  291. {
  292. _elapsedFrameCount = 0;
  293. _isActive = true;
  294. }
  295. }
  296. public void Stop()
  297. {
  298. Logger.Info?.Print(LogClass.AudioRenderer, $"Stopping renderer id {_sessionId}");
  299. lock (_lock)
  300. {
  301. _isActive = false;
  302. }
  303. if (_executionMode == AudioRendererExecutionMode.Auto)
  304. {
  305. _terminationEvent.WaitOne();
  306. }
  307. Logger.Info?.Print(LogClass.AudioRenderer, $"Stopped renderer id {_sessionId}");
  308. }
  309. public ResultCode Update(Memory<byte> output, Memory<byte> performanceOutput, ReadOnlyMemory<byte> input)
  310. {
  311. lock (_lock)
  312. {
  313. ulong updateStartTicks = GetSystemTicks();
  314. output.Span.Fill(0);
  315. StateUpdater stateUpdater = new StateUpdater(input, output, _processHandle, _behaviourContext);
  316. ResultCode result;
  317. result = stateUpdater.UpdateBehaviourContext();
  318. if (result != ResultCode.Success)
  319. {
  320. return result;
  321. }
  322. result = stateUpdater.UpdateMemoryPools(_memoryPools.Span);
  323. if (result != ResultCode.Success)
  324. {
  325. return result;
  326. }
  327. result = stateUpdater.UpdateVoiceChannelResources(_voiceContext);
  328. if (result != ResultCode.Success)
  329. {
  330. return result;
  331. }
  332. result = stateUpdater.UpdateVoices(_voiceContext, _memoryPools);
  333. if (result != ResultCode.Success)
  334. {
  335. return result;
  336. }
  337. result = stateUpdater.UpdateEffects(_effectContext, _isActive, _memoryPools);
  338. if (result != ResultCode.Success)
  339. {
  340. return result;
  341. }
  342. if (_behaviourContext.IsSplitterSupported())
  343. {
  344. result = stateUpdater.UpdateSplitter(_splitterContext);
  345. if (result != ResultCode.Success)
  346. {
  347. return result;
  348. }
  349. }
  350. result = stateUpdater.UpdateMixes(_mixContext, GetMixBufferCount(), _effectContext, _splitterContext);
  351. if (result != ResultCode.Success)
  352. {
  353. return result;
  354. }
  355. result = stateUpdater.UpdateSinks(_sinkContext, _memoryPools);
  356. if (result != ResultCode.Success)
  357. {
  358. return result;
  359. }
  360. result = stateUpdater.UpdatePerformanceBuffer(_performanceManager, performanceOutput.Span);
  361. if (result != ResultCode.Success)
  362. {
  363. return result;
  364. }
  365. result = stateUpdater.UpdateErrorInfo();
  366. if (result != ResultCode.Success)
  367. {
  368. return result;
  369. }
  370. if (_behaviourContext.IsElapsedFrameCountSupported())
  371. {
  372. result = stateUpdater.UpdateRendererInfo(_elapsedFrameCount);
  373. if (result != ResultCode.Success)
  374. {
  375. return result;
  376. }
  377. }
  378. result = stateUpdater.CheckConsumedSize();
  379. if (result != ResultCode.Success)
  380. {
  381. return result;
  382. }
  383. _systemEvent.Clear();
  384. ulong updateEndTicks = GetSystemTicks();
  385. _totalElapsedTicksUpdating += (updateEndTicks - updateStartTicks);
  386. return result;
  387. }
  388. }
  389. private ulong GetSystemTicks()
  390. {
  391. double ticks = ARMeilleure.State.ExecutionContext.ElapsedTicks * ARMeilleure.State.ExecutionContext.TickFrequency;
  392. return (ulong)(ticks * Constants.TargetTimerFrequency);
  393. }
  394. private uint ComputeVoiceDrop(CommandBuffer commandBuffer, long voicesEstimatedTime, long deltaTimeDsp)
  395. {
  396. int i;
  397. for (i = 0; i < commandBuffer.CommandList.Commands.Count; i++)
  398. {
  399. ICommand command = commandBuffer.CommandList.Commands[i];
  400. CommandType commandType = command.CommandType;
  401. if (commandType == CommandType.AdpcmDataSourceVersion1 ||
  402. commandType == CommandType.AdpcmDataSourceVersion2 ||
  403. commandType == CommandType.PcmInt16DataSourceVersion1 ||
  404. commandType == CommandType.PcmInt16DataSourceVersion2 ||
  405. commandType == CommandType.PcmFloatDataSourceVersion1 ||
  406. commandType == CommandType.PcmFloatDataSourceVersion2 ||
  407. commandType == CommandType.Performance)
  408. {
  409. break;
  410. }
  411. }
  412. uint voiceDropped = 0;
  413. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  414. {
  415. ICommand targetCommand = commandBuffer.CommandList.Commands[i];
  416. int targetNodeId = targetCommand.NodeId;
  417. if (voicesEstimatedTime <= deltaTimeDsp || NodeIdHelper.GetType(targetNodeId) != NodeIdType.Voice)
  418. {
  419. break;
  420. }
  421. ref VoiceState voice = ref _voiceContext.GetState(NodeIdHelper.GetBase(targetNodeId));
  422. if (voice.Priority == Constants.VoiceHighestPriority)
  423. {
  424. break;
  425. }
  426. // We can safely drop this voice, disable all associated commands while activating depop preparation commands.
  427. voiceDropped++;
  428. voice.VoiceDropFlag = true;
  429. Logger.Warning?.Print(LogClass.AudioRenderer, $"Dropping voice {voice.NodeId}");
  430. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  431. {
  432. ICommand command = commandBuffer.CommandList.Commands[i];
  433. if (command.NodeId != targetNodeId)
  434. {
  435. break;
  436. }
  437. if (command.CommandType == CommandType.DepopPrepare)
  438. {
  439. command.Enabled = true;
  440. }
  441. else if (command.CommandType == CommandType.Performance || !command.Enabled)
  442. {
  443. continue;
  444. }
  445. else
  446. {
  447. command.Enabled = false;
  448. voicesEstimatedTime -= (long)command.EstimatedProcessingTime;
  449. }
  450. }
  451. }
  452. return voiceDropped;
  453. }
  454. private void GenerateCommandList(out CommandList commandList)
  455. {
  456. Debug.Assert(_executionMode == AudioRendererExecutionMode.Auto);
  457. PoolMapper.ClearUsageState(_memoryPools);
  458. ulong startTicks = GetSystemTicks();
  459. commandList = new CommandList(this);
  460. if (_performanceManager != null)
  461. {
  462. _performanceManager.TapFrame(_isDspRunningBehind, _voiceDropCount, _renderingStartTick);
  463. _isDspRunningBehind = false;
  464. _voiceDropCount = 0;
  465. _renderingStartTick = 0;
  466. }
  467. CommandBuffer commandBuffer = new CommandBuffer(commandList, _commandProcessingTimeEstimator);
  468. CommandGenerator commandGenerator = new CommandGenerator(commandBuffer, GetContext(), _voiceContext, _mixContext, _effectContext, _sinkContext, _splitterContext, _performanceManager);
  469. _voiceContext.Sort();
  470. commandGenerator.GenerateVoices();
  471. long voicesEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  472. commandGenerator.GenerateSubMixes();
  473. commandGenerator.GenerateFinalMixes();
  474. commandGenerator.GenerateSinks();
  475. long totalEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  476. if (_voiceDropEnabled)
  477. {
  478. long maxDspTime = GetMaxAllocatedTimeForDsp();
  479. long restEstimateTime = totalEstimatedTime - voicesEstimatedTime;
  480. long deltaTimeDsp = Math.Max(maxDspTime - restEstimateTime, 0);
  481. _voiceDropCount = ComputeVoiceDrop(commandBuffer, voicesEstimatedTime, deltaTimeDsp);
  482. }
  483. _voiceContext.UpdateForCommandGeneration();
  484. if (_behaviourContext.IsEffectInfoVersion2Supported())
  485. {
  486. _effectContext.UpdateResultStateForCommandGeneration();
  487. }
  488. ulong endTicks = GetSystemTicks();
  489. _totalElapsedTicks = endTicks - startTicks;
  490. _renderingStartTick = GetSystemTicks();
  491. _elapsedFrameCount++;
  492. }
  493. private int GetMaxAllocatedTimeForDsp()
  494. {
  495. return (int)(Constants.AudioProcessorMaxUpdateTimePerSessions * _behaviourContext.GetAudioRendererProcessingTimeLimit() * (GetRenderingTimeLimit() / 100.0f));
  496. }
  497. public void SendCommands()
  498. {
  499. lock (_lock)
  500. {
  501. if (_isActive)
  502. {
  503. _terminationEvent.Reset();
  504. GenerateCommandList(out CommandList commands);
  505. _manager.Processor.Send(_sessionId,
  506. commands,
  507. GetMaxAllocatedTimeForDsp(),
  508. _appletResourceId);
  509. _systemEvent.Signal();
  510. }
  511. else
  512. {
  513. _terminationEvent.Set();
  514. }
  515. }
  516. }
  517. public uint GetMixBufferCount()
  518. {
  519. return _mixBufferCount;
  520. }
  521. public void SetRenderingTimeLimitPercent(uint percent)
  522. {
  523. Debug.Assert(percent <= 100);
  524. _renderingTimeLimitPercent = percent;
  525. }
  526. public uint GetRenderingTimeLimit()
  527. {
  528. return _renderingTimeLimitPercent;
  529. }
  530. public Memory<float> GetMixBuffer()
  531. {
  532. return _mixBuffer;
  533. }
  534. public uint GetSampleCount()
  535. {
  536. return _sampleCount;
  537. }
  538. public uint GetSampleRate()
  539. {
  540. return _sampleRate;
  541. }
  542. public uint GetVoiceChannelCountMax()
  543. {
  544. return _voiceChannelCountMax;
  545. }
  546. public bool IsActive()
  547. {
  548. return _isActive;
  549. }
  550. private RendererSystemContext GetContext()
  551. {
  552. return new RendererSystemContext
  553. {
  554. ChannelCount = _manager.Processor.OutputDevices[_sessionId].GetChannelCount(),
  555. BehaviourContext = _behaviourContext,
  556. DepopBuffer = _depopBuffer,
  557. MixBufferCount = GetMixBufferCount(),
  558. SessionId = _sessionId,
  559. UpsamplerManager = _upsamplerManager
  560. };
  561. }
  562. public int GetSessionId()
  563. {
  564. return _sessionId;
  565. }
  566. public static ulong GetWorkBufferSize(ref AudioRendererConfiguration parameter)
  567. {
  568. BehaviourContext behaviourContext = new BehaviourContext();
  569. behaviourContext.SetUserRevision(parameter.Revision);
  570. uint mixesCount = parameter.SubMixBufferCount + 1;
  571. uint memoryPoolCount = parameter.EffectCount + parameter.VoiceCount * Constants.VoiceWaveBufferCount;
  572. ulong size = 0;
  573. // Mix Buffers
  574. size = WorkBufferAllocator.GetTargetSize<float>(size, parameter.SampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount), 0x10);
  575. // Upsampler workbuffer
  576. size = WorkBufferAllocator.GetTargetSize<float>(size, Constants.TargetSampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount) * (parameter.SinkCount + parameter.SubMixBufferCount), 0x10);
  577. // Depop buffer
  578. size = WorkBufferAllocator.GetTargetSize<float>(size, (ulong)BitUtils.AlignUp(parameter.MixBufferCount, Constants.BufferAlignment), Constants.BufferAlignment);
  579. // Voice
  580. size = WorkBufferAllocator.GetTargetSize<VoiceState>(size, parameter.VoiceCount, VoiceState.Alignment);
  581. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.VoiceCount, 0x10);
  582. size = WorkBufferAllocator.GetTargetSize<VoiceChannelResource>(size, parameter.VoiceCount, VoiceChannelResource.Alignment);
  583. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  584. // Mix
  585. size = WorkBufferAllocator.GetTargetSize<MixState>(size, mixesCount, MixState.Alignment);
  586. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.EffectCount * mixesCount, 0x10);
  587. size = WorkBufferAllocator.GetTargetSize<int>(size, mixesCount, 0x10);
  588. if (behaviourContext.IsSplitterSupported())
  589. {
  590. size += (ulong)BitUtils.AlignUp(NodeStates.GetWorkBufferSize((int)mixesCount) + EdgeMatrix.GetWorkBufferSize((int)mixesCount), 0x10);
  591. }
  592. // Memory Pool
  593. size = WorkBufferAllocator.GetTargetSize<MemoryPoolState>(size, memoryPoolCount, MemoryPoolState.Alignment);
  594. // Splitter
  595. size = SplitterContext.GetWorkBufferSize(size, ref behaviourContext, ref parameter);
  596. // DSP Voice
  597. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  598. // Performance
  599. if (parameter.PerformanceMetricFramesCount > 0)
  600. {
  601. ulong performanceMetricsPerFramesSize = PerformanceManager.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter, ref behaviourContext) * (parameter.PerformanceMetricFramesCount + 1) + 0xC;
  602. size += BitUtils.AlignUp(performanceMetricsPerFramesSize, Constants.PerformanceMetricsPerFramesSizeAlignment);
  603. }
  604. return BitUtils.AlignUp(size, Constants.WorkBufferAlignment);
  605. }
  606. public ResultCode QuerySystemEvent(out IWritableEvent systemEvent)
  607. {
  608. systemEvent = default;
  609. if (_executionMode == AudioRendererExecutionMode.Manual)
  610. {
  611. return ResultCode.UnsupportedOperation;
  612. }
  613. systemEvent = _systemEvent;
  614. return ResultCode.Success;
  615. }
  616. public void Dispose()
  617. {
  618. Dispose(true);
  619. }
  620. protected virtual void Dispose(bool disposing)
  621. {
  622. if (disposing)
  623. {
  624. if (_isActive)
  625. {
  626. Stop();
  627. }
  628. PoolMapper mapper = new PoolMapper(_processHandle, false);
  629. mapper.Unmap(ref _dspMemoryPoolState);
  630. PoolMapper.ClearUsageState(_memoryPools);
  631. for (int i = 0; i < _memoryPoolCount; i++)
  632. {
  633. ref MemoryPoolState memoryPool = ref _memoryPools.Span[i];
  634. if (memoryPool.IsMapped())
  635. {
  636. mapper.Unmap(ref memoryPool);
  637. }
  638. }
  639. _manager.Unregister(this);
  640. _terminationEvent.Dispose();
  641. _workBufferMemoryPin.Dispose();
  642. _workBufferRegion.Dispose();
  643. if (MemoryManager is IRefCounted rc)
  644. {
  645. rc.DecrementReferenceCount();
  646. MemoryManager = null;
  647. }
  648. }
  649. }
  650. }
  651. }