BehaviourContext.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 System;
  18. using System.Diagnostics;
  19. using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
  20. namespace Ryujinx.Audio.Renderer.Server
  21. {
  22. /// <summary>
  23. /// Behaviour context.
  24. /// </summary>
  25. /// <remarks>This handles features based on the audio renderer revision provided by the user.</remarks>
  26. public class BehaviourContext
  27. {
  28. /// <summary>
  29. /// The base magic of the Audio Renderer revision.
  30. /// </summary>
  31. public const int BaseRevisionMagic = ('R' << 0) | ('E' << 8) | ('V' << 16) | ('0' << 24);
  32. /// <summary>
  33. /// REV1: first revision.
  34. /// </summary>
  35. public const int Revision1 = 1 << 24;
  36. /// <summary>
  37. /// REV2: Added support for splitter and fix GC-ADPCM context not being provided to the DSP.
  38. /// </summary>
  39. /// <remarks>This was added in system update 2.0.0</remarks>
  40. public const int Revision2 = 2 << 24;
  41. /// <summary>
  42. /// REV3: Incremented the max pre-delay from 150 to 350 for the reverb command and removed the (unused) codec system.
  43. /// </summary>
  44. /// <remarks>This was added in system update 3.0.0</remarks>
  45. public const int Revision3 = 3 << 24;
  46. /// <summary>
  47. /// REV4: Added USB audio device support and incremented the rendering limit percent to 75%.
  48. /// </summary>
  49. /// <remarks>This was added in system update 4.0.0</remarks>
  50. public const int Revision4 = 4 << 24;
  51. /// <summary>
  52. /// REV5: <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>, <see cref="Parameter.VoiceInParameter.FlushWaveBufferCount"/> were added to voice.
  53. /// A new performance frame format (version 2) was added with support for more information about DSP timing.
  54. /// <see cref="Parameter.RendererInfoOutStatus"/> was added to supply the count of update done sent to the DSP.
  55. /// A new version of the command estimator was added to address timing changes caused by the voice changes.
  56. /// Additionally, the rendering limit percent was incremented to 80%.
  57. ///
  58. /// </summary>
  59. /// <remarks>This was added in system update 6.0.0</remarks>
  60. public const int Revision5 = 5 << 24;
  61. /// <summary>
  62. /// REV6: This fixed a bug in the biquad filter command not clearing up <see cref="Dsp.State.BiquadFilterState"/> with <see cref="Effect.UsageState.New"/> usage state.
  63. /// </summary>
  64. /// <remarks>This was added in system update 6.1.0</remarks>
  65. public const int Revision6 = 6 << 24;
  66. /// <summary>
  67. /// REV7: Client side (finally) doesn't send all the mix client state to the server and can do partial updates.
  68. /// </summary>
  69. /// <remarks>This was added in system update 8.0.0</remarks>
  70. public const int Revision7 = 7 << 24;
  71. /// <summary>
  72. /// REV8:
  73. /// Wavebuffer was changed to support more control over loop (you can now specify where to start and end a loop, and how many times to loop).
  74. /// <see cref="Parameter.VoiceInParameter.SrcQuality"/> was added (see <see cref="Parameter.VoiceInParameter.SampleRateConversionQuality"/> for more info).
  75. /// Final leftovers of the codec system were removed.
  76. /// <see cref="Common.SampleFormat.PcmFloat"/> support was added.
  77. /// A new version of the command estimator was added to address timing changes caused by the voice and command changes.
  78. /// </summary>
  79. /// <remarks>This was added in system update 9.0.0</remarks>
  80. public const int Revision8 = 8 << 24;
  81. /// <summary>
  82. /// REV9:
  83. /// EffectInfo parameters were revisited with a new revision (version 2) allowing more data control between the client and server.
  84. /// A new effect was added: Limiter. This effect is effectively implemented with a DRC while providing statistics on the processing on <see cref="Parameter.EffectOutStatusVersion2"/>.
  85. /// </summary>
  86. /// <remarks>This was added in system update 12.0.0</remarks>
  87. public const int Revision9 = 9 << 24;
  88. /// <summary>
  89. /// Last revision supported by the implementation.
  90. /// </summary>
  91. public const int LastRevision = Revision9;
  92. /// <summary>
  93. /// Target revision magic supported by the implementation.
  94. /// </summary>
  95. public const int ProcessRevision = BaseRevisionMagic + LastRevision;
  96. /// <summary>
  97. /// Get the revision number from the revision magic.
  98. /// </summary>
  99. /// <param name="revision">The revision magic.</param>
  100. /// <returns>The revision number.</returns>
  101. public static int GetRevisionNumber(int revision) => (revision - BaseRevisionMagic) >> 24;
  102. /// <summary>
  103. /// Current active revision.
  104. /// </summary>
  105. public int UserRevision { get; private set; }
  106. /// <summary>
  107. /// Error storage.
  108. /// </summary>
  109. private ErrorInfo[] _errorInfos;
  110. /// <summary>
  111. /// Current position in the <see cref="_errorInfos"/> array.
  112. /// </summary>
  113. private uint _errorIndex;
  114. /// <summary>
  115. /// Current flags of the <see cref="BehaviourContext"/>.
  116. /// </summary>
  117. private ulong _flags;
  118. /// <summary>
  119. /// Create a new instance of <see cref="BehaviourContext"/>.
  120. /// </summary>
  121. public BehaviourContext()
  122. {
  123. UserRevision = 0;
  124. _errorInfos = new ErrorInfo[Constants.MaxErrorInfos];
  125. _errorIndex = 0;
  126. }
  127. /// <summary>
  128. /// Set the active revision.
  129. /// </summary>
  130. /// <param name="userRevision">The active revision.</param>
  131. public void SetUserRevision(int userRevision)
  132. {
  133. UserRevision = userRevision;
  134. }
  135. /// <summary>
  136. /// Update flags of the <see cref="BehaviourContext"/>.
  137. /// </summary>
  138. /// <param name="flags">The new flags.</param>
  139. public void UpdateFlags(ulong flags)
  140. {
  141. _flags = flags;
  142. }
  143. /// <summary>
  144. /// Check if a given revision is valid/supported.
  145. /// </summary>
  146. /// <param name="revision">The revision magic to check.</param>
  147. /// <returns>Returns true if the given revision is valid/supported</returns>
  148. public static bool CheckValidRevision(int revision)
  149. {
  150. return GetRevisionNumber(revision) <= GetRevisionNumber(ProcessRevision);
  151. }
  152. /// <summary>
  153. /// Check if the given revision is greater than or equal the supported revision.
  154. /// </summary>
  155. /// <param name="revision">The revision magic to check.</param>
  156. /// <param name="supportedRevision">The revision magic of the supported revision.</param>
  157. /// <returns>Returns true if the given revision is greater than or equal the supported revision.</returns>
  158. public static bool CheckFeatureSupported(int revision, int supportedRevision)
  159. {
  160. int revA = GetRevisionNumber(revision);
  161. int revB = GetRevisionNumber(supportedRevision);
  162. if (revA > LastRevision)
  163. {
  164. revA = 1;
  165. }
  166. if (revB > LastRevision)
  167. {
  168. revB = 1;
  169. }
  170. return revA >= revB;
  171. }
  172. /// <summary>
  173. /// Check if the memory pool mapping bypass flag is active.
  174. /// </summary>
  175. /// <returns>True if the memory pool mapping bypass flag is active.</returns>
  176. public bool IsMemoryPoolForceMappingEnabled()
  177. {
  178. return (_flags & 1) != 0;
  179. }
  180. /// <summary>
  181. /// Check if the audio renderer should fix the GC-ADPCM context not being provided to the DSP.
  182. /// </summary>
  183. /// <returns>True if if the audio renderer should fix it.</returns>
  184. public bool IsAdpcmLoopContextBugFixed()
  185. {
  186. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  187. }
  188. /// <summary>
  189. /// Check if the audio renderer should accept splitters.
  190. /// </summary>
  191. /// <returns>True if the audio renderer should accept splitters.</returns>
  192. public bool IsSplitterSupported()
  193. {
  194. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  195. }
  196. /// <summary>
  197. /// Check if the audio renderer should use a max pre-delay of 350 instead of 150.
  198. /// </summary>
  199. /// <returns>True if the max pre-delay must be 350.</returns>
  200. public bool IsLongSizePreDelaySupported()
  201. {
  202. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision3);
  203. }
  204. /// <summary>
  205. /// Check if the audio renderer should expose USB audio device.
  206. /// </summary>
  207. /// <returns>True if the audio renderer should expose USB audio device.</returns>
  208. public bool IsAudioUsbDeviceOutputSupported()
  209. {
  210. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4);
  211. }
  212. /// <summary>
  213. /// Get the percentage allocated to the audio renderer on the DSP for processing.
  214. /// </summary>
  215. /// <returns>The percentage allocated to the audio renderer on the DSP for processing.</returns>
  216. public float GetAudioRendererProcessingTimeLimit()
  217. {
  218. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  219. {
  220. return 0.80f;
  221. }
  222. else if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4))
  223. {
  224. return 0.75f;
  225. }
  226. return 0.70f;
  227. }
  228. /// <summary>
  229. /// Check if the audio render should support voice flushing.
  230. /// </summary>
  231. /// <returns>True if the audio render should support voice flushing.</returns>
  232. public bool IsFlushVoiceWaveBuffersSupported()
  233. {
  234. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  235. }
  236. /// <summary>
  237. /// Check if the audio renderer should trust the user destination count in <see cref="Splitter.SplitterState.Update(Splitter.SplitterContext, ref Parameter.SplitterInParameter, ReadOnlySpan{byte})"/>.
  238. /// </summary>
  239. /// <returns>True if the audio renderer should trust the user destination count.</returns>
  240. public bool IsSplitterBugFixed()
  241. {
  242. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  243. }
  244. /// <summary>
  245. /// Check if the audio renderer should supply the elapsed frame count to the user when updating.
  246. /// </summary>
  247. /// <returns>True if the audio renderer should supply the elapsed frame count to the user when updating.</returns>
  248. public bool IsElapsedFrameCountSupported()
  249. {
  250. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  251. }
  252. /// <summary>
  253. /// Get the performance metric data format version.
  254. /// </summary>
  255. /// <returns>The performance metric data format version.</returns>
  256. public uint GetPerformanceMetricsDataFormat()
  257. {
  258. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  259. {
  260. return 2;
  261. }
  262. else
  263. {
  264. return 1;
  265. }
  266. }
  267. /// <summary>
  268. /// Check if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.
  269. /// </summary>
  270. /// <returns>True if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.</returns>
  271. public bool IsDecodingBehaviourFlagSupported()
  272. {
  273. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  274. }
  275. /// <summary>
  276. /// Check if the audio renderer should fix the biquad filter command not clearing up <see cref="Dsp.State.BiquadFilterState"/> with <see cref="Effect.UsageState.New"/> usage state.
  277. /// </summary>
  278. /// <returns>True if the biquad filter state should be cleared.</returns>
  279. public bool IsBiquadFilterEffectStateClearBugFixed()
  280. {
  281. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision6);
  282. }
  283. /// <summary>
  284. /// Check if the audio renderer should accept partial mix updates.
  285. /// </summary>
  286. /// <returns>True if the audio renderer should accept partial mix updates.</returns>
  287. public bool IsMixInParameterDirtyOnlyUpdateSupported()
  288. {
  289. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision7);
  290. }
  291. /// <summary>
  292. /// Check if the audio renderer should use the new wavebuffer format.
  293. /// </summary>
  294. /// <returns>True if the audio renderer should use the new wavebuffer format.</returns>
  295. public bool IsWaveBufferVersion2Supported()
  296. {
  297. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8);
  298. }
  299. /// <summary>
  300. /// Check if the audio renderer should use the new effect info format.
  301. /// </summary>
  302. /// <returns>True if the audio renderer should use the new effect info format.</returns>
  303. public bool IsEffectInfoVersion2Supported()
  304. {
  305. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision9);
  306. }
  307. /// <summary>
  308. /// Get the version of the <see cref="ICommandProcessingTimeEstimator"/>.
  309. /// </summary>
  310. /// <returns>The version of the <see cref="ICommandProcessingTimeEstimator"/>.</returns>
  311. public int GetCommandProcessingTimeEstimatorVersion()
  312. {
  313. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8))
  314. {
  315. return 3;
  316. }
  317. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  318. {
  319. return 2;
  320. }
  321. return 1;
  322. }
  323. /// <summary>
  324. /// Append a new <see cref="ErrorInfo"/> to the error array.
  325. /// </summary>
  326. /// <param name="errorInfo">The new <see cref="ErrorInfo"/> to add.</param>
  327. public void AppendError(ref ErrorInfo errorInfo)
  328. {
  329. Debug.Assert(errorInfo.ErrorCode == ResultCode.Success);
  330. if (_errorIndex <= Constants.MaxErrorInfos - 1)
  331. {
  332. _errorInfos[_errorIndex++] = errorInfo;
  333. }
  334. }
  335. /// <summary>
  336. /// Copy the internal <see cref="ErrorInfo"/> array to the given <see cref="Span{ErrorInfo}"/> and output the count copied.
  337. /// </summary>
  338. /// <param name="errorInfos">The output <see cref="Span{ErrorInfo}"/>.</param>
  339. /// <param name="errorCount">The output error count containing the count of <see cref="ErrorInfo"/> copied.</param>
  340. public void CopyErrorInfo(Span<ErrorInfo> errorInfos, out uint errorCount)
  341. {
  342. if (errorInfos.Length != Constants.MaxErrorInfos)
  343. {
  344. throw new ArgumentException("Invalid size of errorInfos span!");
  345. }
  346. errorCount = Math.Min(_errorIndex, Constants.MaxErrorInfos);
  347. for (int i = 0; i < Constants.MaxErrorInfos; i++)
  348. {
  349. if (i < errorCount)
  350. {
  351. errorInfos[i] = _errorInfos[i];
  352. }
  353. else
  354. {
  355. errorInfos[i] = new ErrorInfo
  356. {
  357. ErrorCode = 0,
  358. ExtraErrorInfo = 0
  359. };
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// Clear the <see cref="ErrorInfo"/> array.
  365. /// </summary>
  366. public void ClearError()
  367. {
  368. _errorIndex = 0;
  369. }
  370. }
  371. }