AudioRenderSystem.cs 31 KB

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