BaseEffect.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.Renderer.Common;
  18. using Ryujinx.Audio.Renderer.Parameter;
  19. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  20. using Ryujinx.Audio.Renderer.Utils;
  21. using System;
  22. using System.Diagnostics;
  23. using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
  24. using DspAddress = System.UInt64;
  25. namespace Ryujinx.Audio.Renderer.Server.Effect
  26. {
  27. /// <summary>
  28. /// Base class used as a server state for an effect.
  29. /// </summary>
  30. public class BaseEffect
  31. {
  32. /// <summary>
  33. /// The <see cref="EffectType"/> of the effect.
  34. /// </summary>
  35. public EffectType Type;
  36. /// <summary>
  37. /// Set to true if the effect must be active.
  38. /// </summary>
  39. public bool IsEnabled;
  40. /// <summary>
  41. /// Set to true if the internal effect work buffers used wasn't mapped.
  42. /// </summary>
  43. public bool BufferUnmapped;
  44. /// <summary>
  45. /// The current state of the effect.
  46. /// </summary>
  47. public UsageState UsageState;
  48. /// <summary>
  49. /// The target mix id of the effect.
  50. /// </summary>
  51. public int MixId;
  52. /// <summary>
  53. /// Position of the effect while processing effects.
  54. /// </summary>
  55. public uint ProcessingOrder;
  56. /// <summary>
  57. /// Array of all the work buffer used by the effect.
  58. /// </summary>
  59. protected AddressInfo[] WorkBuffers;
  60. /// <summary>
  61. /// Create a new <see cref="BaseEffect"/>.
  62. /// </summary>
  63. public BaseEffect()
  64. {
  65. Type = TargetEffectType;
  66. UsageState = UsageState.Invalid;
  67. IsEnabled = false;
  68. BufferUnmapped = false;
  69. MixId = Constants.UnusedMixId;
  70. ProcessingOrder = uint.MaxValue;
  71. WorkBuffers = new AddressInfo[2];
  72. foreach (ref AddressInfo info in WorkBuffers.AsSpan())
  73. {
  74. info = AddressInfo.Create();
  75. }
  76. }
  77. /// <summary>
  78. /// The target <see cref="EffectType"/> handled by this <see cref="BaseEffect"/>.
  79. /// </summary>
  80. public virtual EffectType TargetEffectType => EffectType.Invalid;
  81. /// <summary>
  82. /// Check if the <see cref="EffectType"/> sent by the user match the internal <see cref="EffectType"/>.
  83. /// </summary>
  84. /// <param name="parameter">The user parameter.</param>
  85. /// <returns>Returns true if the <see cref="EffectType"/> sent by the user matches the internal <see cref="EffectType"/>.</returns>
  86. public bool IsTypeValid<T>(ref T parameter) where T: unmanaged, IEffectInParameter
  87. {
  88. return parameter.Type == TargetEffectType;
  89. }
  90. /// <summary>
  91. /// Update the usage state during command generation.
  92. /// </summary>
  93. protected void UpdateUsageStateForCommandGeneration()
  94. {
  95. UsageState = IsEnabled ? UsageState.Enabled : UsageState.Disabled;
  96. }
  97. /// <summary>
  98. /// Update the internal common parameters from a user parameter.
  99. /// </summary>
  100. /// <param name="parameter">The user parameter.</param>
  101. protected void UpdateParameterBase<T>(ref T parameter) where T : unmanaged, IEffectInParameter
  102. {
  103. MixId = parameter.MixId;
  104. ProcessingOrder = parameter.ProcessingOrder;
  105. }
  106. /// <summary>
  107. /// Force unmap all the work buffers.
  108. /// </summary>
  109. /// <param name="mapper">The mapper to use.</param>
  110. public void ForceUnmapBuffers(PoolMapper mapper)
  111. {
  112. foreach (ref AddressInfo info in WorkBuffers.AsSpan())
  113. {
  114. if (info.GetReference(false) != 0)
  115. {
  116. mapper.ForceUnmap(ref info);
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Check if the effect needs to be skipped.
  122. /// </summary>
  123. /// <returns>Returns true if the effect needs to be skipped.</returns>
  124. public bool ShouldSkip()
  125. {
  126. return BufferUnmapped;
  127. }
  128. /// <summary>
  129. /// Update the <see cref="BaseEffect"/> state during command generation.
  130. /// </summary>
  131. public virtual void UpdateForCommandGeneration()
  132. {
  133. Debug.Assert(Type == TargetEffectType);
  134. }
  135. /// <summary>
  136. /// Initialize the given <paramref name="state"/> result state.
  137. /// </summary>
  138. /// <param name="state">The state to initalize</param>
  139. public virtual void InitializeResultState(ref EffectResultState state) {}
  140. /// <summary>
  141. /// Update the <paramref name="destState"/> result state with <paramref name="srcState"/>.
  142. /// </summary>
  143. /// <param name="destState">The destination result state</param>
  144. /// <param name="srcState">The source result state</param>
  145. public virtual void UpdateResultState(ref EffectResultState destState, ref EffectResultState srcState) {}
  146. /// <summary>
  147. /// Update the internal state from a user version 1 parameter.
  148. /// </summary>
  149. /// <param name="updateErrorInfo">The possible <see cref="ErrorInfo"/> that was generated.</param>
  150. /// <param name="parameter">The user parameter.</param>
  151. /// <param name="mapper">The mapper to use.</param>
  152. public virtual void Update(out ErrorInfo updateErrorInfo, ref EffectInParameterVersion1 parameter, PoolMapper mapper)
  153. {
  154. Debug.Assert(IsTypeValid(ref parameter));
  155. updateErrorInfo = new ErrorInfo();
  156. }
  157. /// <summary>
  158. /// Update the internal state from a user version 2 parameter.
  159. /// </summary>
  160. /// <param name="updateErrorInfo">The possible <see cref="ErrorInfo"/> that was generated.</param>
  161. /// <param name="parameter">The user parameter.</param>
  162. /// <param name="mapper">The mapper to use.</param>
  163. public virtual void Update(out ErrorInfo updateErrorInfo, ref EffectInParameterVersion2 parameter, PoolMapper mapper)
  164. {
  165. Debug.Assert(IsTypeValid(ref parameter));
  166. updateErrorInfo = new ErrorInfo();
  167. }
  168. /// <summary>
  169. /// Get the work buffer DSP address at the given index.
  170. /// </summary>
  171. /// <param name="index">The index of the work buffer</param>
  172. /// <returns>The work buffer DSP address at the given index.</returns>
  173. public virtual DspAddress GetWorkBuffer(int index)
  174. {
  175. throw new InvalidOperationException();
  176. }
  177. /// <summary>
  178. /// Get the first work buffer DSP address.
  179. /// </summary>
  180. /// <returns>The first work buffer DSP address.</returns>
  181. protected DspAddress GetSingleBuffer()
  182. {
  183. if (IsEnabled)
  184. {
  185. return WorkBuffers[0].GetReference(true);
  186. }
  187. if (UsageState != UsageState.Disabled)
  188. {
  189. DspAddress address = WorkBuffers[0].GetReference(false);
  190. ulong size = WorkBuffers[0].Size;
  191. if (address != 0 && size != 0)
  192. {
  193. AudioProcessorMemoryManager.InvalidateDataCache(address, size);
  194. }
  195. }
  196. return 0;
  197. }
  198. /// <summary>
  199. /// Store the output status to the given user output.
  200. /// </summary>
  201. /// <param name="outStatus">The given user output.</param>
  202. /// <param name="isAudioRendererActive">If set to true, the <see cref="AudioRenderSystem"/> is active.</param>
  203. public void StoreStatus<T>(ref T outStatus, bool isAudioRendererActive) where T: unmanaged, IEffectOutStatus
  204. {
  205. if (isAudioRendererActive)
  206. {
  207. if (UsageState == UsageState.Disabled)
  208. {
  209. outStatus.State = EffectState.Disabled;
  210. }
  211. else
  212. {
  213. outStatus.State = EffectState.Enabled;
  214. }
  215. }
  216. else if (UsageState == UsageState.New)
  217. {
  218. outStatus.State = EffectState.Enabled;
  219. }
  220. else
  221. {
  222. outStatus.State = EffectState.Disabled;
  223. }
  224. }
  225. /// <summary>
  226. /// Get the <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.
  227. /// </summary>
  228. /// <returns>The <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.</returns>
  229. public PerformanceDetailType GetPerformanceDetailType()
  230. {
  231. switch (Type)
  232. {
  233. case EffectType.BiquadFilter:
  234. return PerformanceDetailType.BiquadFilter;
  235. case EffectType.AuxiliaryBuffer:
  236. return PerformanceDetailType.Aux;
  237. case EffectType.Delay:
  238. return PerformanceDetailType.Delay;
  239. case EffectType.Reverb:
  240. return PerformanceDetailType.Reverb;
  241. case EffectType.Reverb3d:
  242. return PerformanceDetailType.Reverb3d;
  243. case EffectType.BufferMix:
  244. return PerformanceDetailType.Mix;
  245. case EffectType.Limiter:
  246. return PerformanceDetailType.Limiter;
  247. case EffectType.CaptureBuffer:
  248. return PerformanceDetailType.CaptureBuffer;
  249. default:
  250. throw new NotImplementedException($"{Type}");
  251. }
  252. }
  253. }
  254. }