EffectContext.cs 5.3 KB

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