BaseEffect.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 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 = RendererConstants.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(ref EffectInParameter parameter)
  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(ref EffectInParameter parameter)
  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. /// Update the internal state from a user parameter.
  137. /// </summary>
  138. /// <param name="updateErrorInfo">The possible <see cref="ErrorInfo"/> that was generated.</param>
  139. /// <param name="parameter">The user parameter.</param>
  140. /// <param name="mapper">The mapper to use.</param>
  141. public virtual void Update(out ErrorInfo updateErrorInfo, ref EffectInParameter parameter, PoolMapper mapper)
  142. {
  143. Debug.Assert(IsTypeValid(ref parameter));
  144. updateErrorInfo = new ErrorInfo();
  145. }
  146. /// <summary>
  147. /// Get the work buffer DSP address at the given index.
  148. /// </summary>
  149. /// <param name="index">The index of the work buffer</param>
  150. /// <returns>The work buffer DSP address at the given index.</returns>
  151. public virtual DspAddress GetWorkBuffer(int index)
  152. {
  153. throw new InvalidOperationException();
  154. }
  155. /// <summary>
  156. /// Get the first work buffer DSP address.
  157. /// </summary>
  158. /// <returns>The first work buffer DSP address.</returns>
  159. protected DspAddress GetSingleBuffer()
  160. {
  161. if (IsEnabled)
  162. {
  163. return WorkBuffers[0].GetReference(true);
  164. }
  165. if (UsageState != UsageState.Disabled)
  166. {
  167. DspAddress address = WorkBuffers[0].GetReference(false);
  168. ulong size = WorkBuffers[0].Size;
  169. if (address != 0 && size != 0)
  170. {
  171. AudioProcessorMemoryManager.InvalidateDataCache(address, size);
  172. }
  173. }
  174. return 0;
  175. }
  176. /// <summary>
  177. /// Store the output status to the given user output.
  178. /// </summary>
  179. /// <param name="outStatus">The given user output.</param>
  180. /// <param name="isAudioRendererActive">If set to true, the <see cref="AudioRenderSystem"/> is active.</param>
  181. public void StoreStatus(ref EffectOutStatus outStatus, bool isAudioRendererActive)
  182. {
  183. if (isAudioRendererActive)
  184. {
  185. if (UsageState == UsageState.Disabled)
  186. {
  187. outStatus.State = EffectOutStatus.EffectState.Disabled;
  188. }
  189. else
  190. {
  191. outStatus.State = EffectOutStatus.EffectState.Enabled;
  192. }
  193. }
  194. else if (UsageState == UsageState.New)
  195. {
  196. outStatus.State = EffectOutStatus.EffectState.Enabled;
  197. }
  198. else
  199. {
  200. outStatus.State = EffectOutStatus.EffectState.Disabled;
  201. }
  202. }
  203. /// <summary>
  204. /// Get the <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.
  205. /// </summary>
  206. /// <returns>The <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.</returns>
  207. public PerformanceDetailType GetPerformanceDetailType()
  208. {
  209. switch (Type)
  210. {
  211. case EffectType.BiquadFilter:
  212. return PerformanceDetailType.BiquadFilter;
  213. case EffectType.AuxiliaryBuffer:
  214. return PerformanceDetailType.Aux;
  215. case EffectType.Delay:
  216. return PerformanceDetailType.Delay;
  217. case EffectType.Reverb:
  218. return PerformanceDetailType.Reverb;
  219. case EffectType.Reverb3d:
  220. return PerformanceDetailType.Reverb3d;
  221. case EffectType.BufferMix:
  222. return PerformanceDetailType.Mix;
  223. default:
  224. throw new NotImplementedException($"{Type}");
  225. }
  226. }
  227. }
  228. }