EffectContext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Ryujinx.Audio.Renderer.Parameter;
  2. using Ryujinx.Audio.Renderer.Utils;
  3. using System;
  4. using System.Diagnostics;
  5. namespace Ryujinx.Audio.Renderer.Server.Effect
  6. {
  7. /// <summary>
  8. /// Effect context.
  9. /// </summary>
  10. public class EffectContext
  11. {
  12. /// <summary>
  13. /// Storage for <see cref="BaseEffect"/>.
  14. /// </summary>
  15. private BaseEffect[] _effects;
  16. /// <summary>
  17. /// The total effect count.
  18. /// </summary>
  19. private uint _effectCount;
  20. private EffectResultState[] _resultStatesCpu;
  21. private EffectResultState[] _resultStatesDsp;
  22. /// <summary>
  23. /// Create a new <see cref="EffectContext"/>.
  24. /// </summary>
  25. public EffectContext()
  26. {
  27. _effects = null;
  28. _effectCount = 0;
  29. }
  30. /// <summary>
  31. /// Initialize the <see cref="EffectContext"/>.
  32. /// </summary>
  33. /// <param name="effectCount">The total effect count.</param>
  34. /// <param name="resultStateCount">The total result state count.</param>
  35. public void Initialize(uint effectCount, uint resultStateCount)
  36. {
  37. _effectCount = effectCount;
  38. _effects = new BaseEffect[effectCount];
  39. for (int i = 0; i < _effectCount; i++)
  40. {
  41. _effects[i] = new BaseEffect();
  42. }
  43. _resultStatesCpu = new EffectResultState[resultStateCount];
  44. _resultStatesDsp = new EffectResultState[resultStateCount];
  45. }
  46. /// <summary>
  47. /// Get the total effect count.
  48. /// </summary>
  49. /// <returns>The total effect count.</returns>
  50. public uint GetCount()
  51. {
  52. return _effectCount;
  53. }
  54. /// <summary>
  55. /// Get a reference to a <see cref="BaseEffect"/> at the given <paramref name="index"/>.
  56. /// </summary>
  57. /// <param name="index">The index to use.</param>
  58. /// <returns>A reference to a <see cref="BaseEffect"/> at the given <paramref name="index"/>.</returns>
  59. public ref BaseEffect GetEffect(int index)
  60. {
  61. Debug.Assert(index >= 0 && index < _effectCount);
  62. return ref _effects[index];
  63. }
  64. /// <summary>
  65. /// Get a reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
  66. /// </summary>
  67. /// <param name="index">The index to use.</param>
  68. /// <returns>A reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
  69. /// <remarks>The returned <see cref="EffectResultState"/> should only be used when updating the server state.</remarks>
  70. public ref EffectResultState GetState(int index)
  71. {
  72. Debug.Assert(index >= 0 && index < _resultStatesCpu.Length);
  73. return ref _resultStatesCpu[index];
  74. }
  75. /// <summary>
  76. /// Get a reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
  77. /// </summary>
  78. /// <param name="index">The index to use.</param>
  79. /// <returns>A reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
  80. /// <remarks>The returned <see cref="EffectResultState"/> should only be used in the context of processing on the <see cref="Dsp.AudioProcessor"/>.</remarks>
  81. public ref EffectResultState GetDspState(int index)
  82. {
  83. Debug.Assert(index >= 0 && index < _resultStatesDsp.Length);
  84. return ref _resultStatesDsp[index];
  85. }
  86. /// <summary>
  87. /// Get a memory instance to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
  88. /// </summary>
  89. /// <param name="index">The index to use.</param>
  90. /// <returns>A memory instance to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
  91. /// <remarks>The returned <see cref="Memory{EffectResultState}"/> should only be used in the context of processing on the <see cref="Dsp.AudioProcessor"/>.</remarks>
  92. public Memory<EffectResultState> GetDspStateMemory(int index)
  93. {
  94. return SpanIOHelper.GetMemory(_resultStatesDsp.AsMemory(), index, (uint)_resultStatesDsp.Length);
  95. }
  96. /// <summary>
  97. /// Update internal state during command generation.
  98. /// </summary>
  99. public void UpdateResultStateForCommandGeneration()
  100. {
  101. for (int index = 0; index < _resultStatesCpu.Length; index++)
  102. {
  103. _effects[index].UpdateResultState(ref _resultStatesCpu[index], ref _resultStatesDsp[index]);
  104. }
  105. }
  106. }
  107. }