BehaviourContext.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 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. /// Last revision supported by the implementation.
  83. /// </summary>
  84. public const int LastRevision = Revision8;
  85. /// <summary>
  86. /// Target revision magic supported by the implementation.
  87. /// </summary>
  88. public const int ProcessRevision = BaseRevisionMagic + LastRevision;
  89. /// <summary>
  90. /// Get the revision number from the revision magic.
  91. /// </summary>
  92. /// <param name="revision">The revision magic.</param>
  93. /// <returns>The revision number.</returns>
  94. public static int GetRevisionNumber(int revision) => (revision - BaseRevisionMagic) >> 24;
  95. /// <summary>
  96. /// Current active revision.
  97. /// </summary>
  98. public int UserRevision { get; private set; }
  99. /// <summary>
  100. /// Error storage.
  101. /// </summary>
  102. private ErrorInfo[] _errorInfos;
  103. /// <summary>
  104. /// Current position in the <see cref="_errorInfos"/> array.
  105. /// </summary>
  106. private uint _errorIndex;
  107. /// <summary>
  108. /// Current flags of the <see cref="BehaviourContext"/>.
  109. /// </summary>
  110. private ulong _flags;
  111. /// <summary>
  112. /// Create a new instance of <see cref="BehaviourContext"/>.
  113. /// </summary>
  114. public BehaviourContext()
  115. {
  116. UserRevision = 0;
  117. _errorInfos = new ErrorInfo[RendererConstants.MaxErrorInfos];
  118. _errorIndex = 0;
  119. }
  120. /// <summary>
  121. /// Set the active revision.
  122. /// </summary>
  123. /// <param name="userRevision">The active revision.</param>
  124. public void SetUserRevision(int userRevision)
  125. {
  126. UserRevision = userRevision;
  127. }
  128. /// <summary>
  129. /// Update flags of the <see cref="BehaviourContext"/>.
  130. /// </summary>
  131. /// <param name="flags">The new flags.</param>
  132. public void UpdateFlags(ulong flags)
  133. {
  134. _flags = flags;
  135. }
  136. /// <summary>
  137. /// Check if a given revision is valid/supported.
  138. /// </summary>
  139. /// <param name="revision">The revision magic to check.</param>
  140. /// <returns>Returns true if the given revision is valid/supported</returns>
  141. public static bool CheckValidRevision(int revision)
  142. {
  143. return GetRevisionNumber(revision) <= GetRevisionNumber(ProcessRevision);
  144. }
  145. /// <summary>
  146. /// Check if the given revision is greater than or equal the supported revision.
  147. /// </summary>
  148. /// <param name="revision">The revision magic to check.</param>
  149. /// <param name="supportedRevision">The revision magic of the supported revision.</param>
  150. /// <returns>Returns true if the given revision is greater than or equal the supported revision.</returns>
  151. public static bool CheckFeatureSupported(int revision, int supportedRevision)
  152. {
  153. int revA = GetRevisionNumber(revision);
  154. int revB = GetRevisionNumber(supportedRevision);
  155. if (revA > LastRevision)
  156. {
  157. revA = 1;
  158. }
  159. if (revB > LastRevision)
  160. {
  161. revB = 1;
  162. }
  163. return revA >= revB;
  164. }
  165. /// <summary>
  166. /// Check if the memory pool mapping bypass flag is active.
  167. /// </summary>
  168. /// <returns>True if the memory pool mapping bypass flag is active.</returns>
  169. public bool IsMemoryPoolForceMappingEnabled()
  170. {
  171. return (_flags & 1) != 0;
  172. }
  173. /// <summary>
  174. /// Check if the audio renderer should fix the GC-ADPCM context not being provided to the DSP.
  175. /// </summary>
  176. /// <returns>True if if the audio renderer should fix it.</returns>
  177. public bool IsAdpcmLoopContextBugFixed()
  178. {
  179. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  180. }
  181. /// <summary>
  182. /// Check if the audio renderer should accept splitters.
  183. /// </summary>
  184. /// <returns>True if the audio renderer should accept splitters.</returns>
  185. public bool IsSplitterSupported()
  186. {
  187. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  188. }
  189. /// <summary>
  190. /// Check if the audio renderer should use a max pre-delay of 350 instead of 150.
  191. /// </summary>
  192. /// <returns>True if the max pre-delay must be 350.</returns>
  193. public bool IsLongSizePreDelaySupported()
  194. {
  195. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision3);
  196. }
  197. /// <summary>
  198. /// Check if the audio renderer should expose USB audio device.
  199. /// </summary>
  200. /// <returns>True if the audio renderer should expose USB audio device.</returns>
  201. public bool IsAudioUsbDeviceOutputSupported()
  202. {
  203. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4);
  204. }
  205. /// <summary>
  206. /// Get the percentage allocated to the audio renderer on the DSP for processing.
  207. /// </summary>
  208. /// <returns>The percentage allocated to the audio renderer on the DSP for processing.</returns>
  209. public float GetAudioRendererProcessingTimeLimit()
  210. {
  211. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  212. {
  213. return 0.80f;
  214. }
  215. else if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4))
  216. {
  217. return 0.75f;
  218. }
  219. return 0.70f;
  220. }
  221. /// <summary>
  222. /// Check if the audio render should support voice flushing.
  223. /// </summary>
  224. /// <returns>True if the audio render should support voice flushing.</returns>
  225. public bool IsFlushVoiceWaveBuffersSupported()
  226. {
  227. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  228. }
  229. /// <summary>
  230. /// Check if the audio renderer should trust the user destination count in <see cref="Splitter.SplitterState.Update(Splitter.SplitterContext, ref Parameter.SplitterInParameter, ReadOnlySpan{byte})"/>.
  231. /// </summary>
  232. /// <returns>True if the audio renderer should trust the user destination count.</returns>
  233. public bool IsSplitterBugFixed()
  234. {
  235. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  236. }
  237. /// <summary>
  238. /// Check if the audio renderer should supply the elapsed frame count to the user when updating.
  239. /// </summary>
  240. /// <returns>True if the audio renderer should supply the elapsed frame count to the user when updating.</returns>
  241. public bool IsElapsedFrameCountSupported()
  242. {
  243. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  244. }
  245. /// <summary>
  246. /// Get the performance metric data format version.
  247. /// </summary>
  248. /// <returns>The performance metric data format version.</returns>
  249. public uint GetPerformanceMetricsDataFormat()
  250. {
  251. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  252. {
  253. return 2;
  254. }
  255. else
  256. {
  257. return 1;
  258. }
  259. }
  260. /// <summary>
  261. /// Check if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.
  262. /// </summary>
  263. /// <returns>True if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.</returns>
  264. public bool IsDecodingBehaviourFlagSupported()
  265. {
  266. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  267. }
  268. /// <summary>
  269. /// 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.
  270. /// </summary>
  271. /// <returns>True if the biquad filter state should be cleared.</returns>
  272. public bool IsBiquadFilterEffectStateClearBugFixed()
  273. {
  274. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision6);
  275. }
  276. /// <summary>
  277. /// Check if the audio renderer should accept partial mix updates.
  278. /// </summary>
  279. /// <returns>True if the audio renderer should accept partial mix updates.</returns>
  280. public bool IsMixInParameterDirtyOnlyUpdateSupported()
  281. {
  282. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision7);
  283. }
  284. /// <summary>
  285. /// Check if the audio renderer should use the new wavebuffer format.
  286. /// </summary>
  287. /// <returns>True if the audio renderer should use the new wavebuffer format.</returns>
  288. public bool IsWaveBufferVersion2Supported()
  289. {
  290. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8);
  291. }
  292. /// <summary>
  293. /// Get the version of the <see cref="ICommandProcessingTimeEstimator"/>.
  294. /// </summary>
  295. /// <returns>The version of the <see cref="ICommandProcessingTimeEstimator"/>.</returns>
  296. public int GetCommandProcessingTimeEstimatorVersion()
  297. {
  298. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8))
  299. {
  300. return 3;
  301. }
  302. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  303. {
  304. return 2;
  305. }
  306. return 1;
  307. }
  308. /// <summary>
  309. /// Append a new <see cref="ErrorInfo"/> to the error array.
  310. /// </summary>
  311. /// <param name="errorInfo">The new <see cref="ErrorInfo"/> to add.</param>
  312. public void AppendError(ref ErrorInfo errorInfo)
  313. {
  314. Debug.Assert(errorInfo.ErrorCode == ResultCode.Success);
  315. if (_errorIndex <= RendererConstants.MaxErrorInfos - 1)
  316. {
  317. _errorInfos[_errorIndex++] = errorInfo;
  318. }
  319. }
  320. /// <summary>
  321. /// Copy the internal <see cref="ErrorInfo"/> array to the given <see cref="Span{ErrorInfo}"/> and output the count copied.
  322. /// </summary>
  323. /// <param name="errorInfos">The output <see cref="Span{ErrorInfo}"/>.</param>
  324. /// <param name="errorCount">The output error count containing the count of <see cref="ErrorInfo"/> copied.</param>
  325. public void CopyErrorInfo(Span<ErrorInfo> errorInfos, out uint errorCount)
  326. {
  327. if (errorInfos.Length != RendererConstants.MaxErrorInfos)
  328. {
  329. throw new ArgumentException("Invalid size of errorInfos span!");
  330. }
  331. errorCount = Math.Min(_errorIndex, RendererConstants.MaxErrorInfos);
  332. for (int i = 0; i < RendererConstants.MaxErrorInfos; i++)
  333. {
  334. if (i < errorCount)
  335. {
  336. errorInfos[i] = _errorInfos[i];
  337. }
  338. else
  339. {
  340. errorInfos[i] = new ErrorInfo
  341. {
  342. ErrorCode = 0,
  343. ExtraErrorInfo = 0
  344. };
  345. }
  346. }
  347. }
  348. /// <summary>
  349. /// Clear the <see cref="ErrorInfo"/> array.
  350. /// </summary>
  351. public void ClearError()
  352. {
  353. _errorIndex = 0;
  354. }
  355. }
  356. }