aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Server/Effect/EffectContext.cs
blob: 619f31100d08c517d6e7ed7f5335ee7263b1e4c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Ryujinx.Audio.Renderer.Parameter;
using Ryujinx.Audio.Renderer.Utils;
using System;
using System.Diagnostics;

namespace Ryujinx.Audio.Renderer.Server.Effect
{
    /// <summary>
    /// Effect context.
    /// </summary>
    public class EffectContext
    {
        /// <summary>
        /// Storage for <see cref="BaseEffect"/>.
        /// </summary>
        private BaseEffect[] _effects;

        /// <summary>
        /// The total effect count.
        /// </summary>
        private uint _effectCount;

        private EffectResultState[] _resultStatesCpu;
        private EffectResultState[] _resultStatesDsp;

        /// <summary>
        /// Create a new <see cref="EffectContext"/>.
        /// </summary>
        public EffectContext()
        {
            _effects = null;
            _effectCount = 0;
        }

        /// <summary>
        /// Initialize the <see cref="EffectContext"/>.
        /// </summary>
        /// <param name="effectCount">The total effect count.</param>
        /// <param name="resultStateCount">The total result state count.</param>
        public void Initialize(uint effectCount, uint resultStateCount)
        {
            _effectCount = effectCount;
            _effects = new BaseEffect[effectCount];

            for (int i = 0; i < _effectCount; i++)
            {
                _effects[i] = new BaseEffect();
            }

            _resultStatesCpu = new EffectResultState[resultStateCount];
            _resultStatesDsp = new EffectResultState[resultStateCount];
        }

        /// <summary>
        /// Get the total effect count.
        /// </summary>
        /// <returns>The total effect count.</returns>
        public uint GetCount()
        {
            return _effectCount;
        }

        /// <summary>
        /// Get a reference to a <see cref="BaseEffect"/> at the given <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index to use.</param>
        /// <returns>A reference to a <see cref="BaseEffect"/> at the given <paramref name="index"/>.</returns>
        public ref BaseEffect GetEffect(int index)
        {
            Debug.Assert(index >= 0 && index < _effectCount);

            return ref _effects[index];
        }

        /// <summary>
        /// Get a reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index to use.</param>
        /// <returns>A reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
        /// <remarks>The returned <see cref="EffectResultState"/> should only be used when updating the server state.</remarks>
        public ref EffectResultState GetState(int index)
        {
            Debug.Assert(index >= 0 && index < _resultStatesCpu.Length);

            return ref _resultStatesCpu[index];
        }

        /// <summary>
        /// Get a reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index to use.</param>
        /// <returns>A reference to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
        /// <remarks>The returned <see cref="EffectResultState"/> should only be used in the context of processing on the <see cref="Dsp.AudioProcessor"/>.</remarks>
        public ref EffectResultState GetDspState(int index)
        {
            Debug.Assert(index >= 0 && index < _resultStatesDsp.Length);

            return ref _resultStatesDsp[index];
        }

        /// <summary>
        /// Get a memory instance to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index to use.</param>
        /// <returns>A memory instance to a <see cref="EffectResultState"/> at the given <paramref name="index"/>.</returns>
        /// <remarks>The returned <see cref="Memory{EffectResultState}"/> should only be used in the context of processing on the <see cref="Dsp.AudioProcessor"/>.</remarks>
        public Memory<EffectResultState> GetDspStateMemory(int index)
        {
            return SpanIOHelper.GetMemory(_resultStatesDsp.AsMemory(), index, (uint)_resultStatesDsp.Length);
        }

        /// <summary>
        /// Update internal state during command generation.
        /// </summary>
        public void UpdateResultStateForCommandGeneration()
        {
            for (int index = 0; index < _resultStatesCpu.Length; index++)
            {
                _effects[index].UpdateResultState(ref _resultStatesCpu[index], ref _resultStatesDsp[index]);
            }
        }
    }
}