aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs
blob: ce9985318819a1bd88b2d59be91c4851f17231a4 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Threading;

namespace Ryujinx.Graphics.Gpu.Memory
{
    /// <summary>
    /// A record of when buffer data was copied from multiple buffers to one migration target,
    /// along with the SyncNumber when the migration will be complete.
    /// Keeps the source buffers alive for data flushes until the migration is complete.
    /// All spans cover the full range of the "destination" buffer.
    /// </summary>
    internal class BufferMigration : IDisposable
    {
        /// <summary>
        /// Ranges from source buffers that were copied as part of this migration.
        /// Ordered by increasing base address.
        /// </summary>
        public BufferMigrationSpan[] Spans { get; private set; }

        /// <summary>
        /// The destination range list. This range list must be updated when flushing the source.
        /// </summary>
        public readonly BufferModifiedRangeList Destination;

        /// <summary>
        /// The sync number that needs to be reached for this migration to be removed. This is set to the pending sync number on creation.
        /// </summary>
        public readonly ulong SyncNumber;

        /// <summary>
        /// Number of active users there are traversing this migration's spans.
        /// </summary>
        private int _refCount;

        /// <summary>
        /// Create a new buffer migration.
        /// </summary>
        /// <param name="spans">Source spans for the migration</param>
        /// <param name="destination">Destination buffer range list</param>
        /// <param name="syncNumber">Sync number where this migration will be complete</param>
        public BufferMigration(BufferMigrationSpan[] spans, BufferModifiedRangeList destination, ulong syncNumber)
        {
            Spans = spans;
            Destination = destination;
            SyncNumber = syncNumber;
        }

        /// <summary>
        /// Add a span to the migration. Allocates a new array with the target size, and replaces it.
        /// </summary>
        /// <remarks>
        /// The base address for the span is assumed to be higher than all other spans in the migration,
        /// to keep the span array ordered.
        /// </remarks>
        public void AddSpanToEnd(BufferMigrationSpan span)
        {
            BufferMigrationSpan[] oldSpans = Spans;

            BufferMigrationSpan[] newSpans = new BufferMigrationSpan[oldSpans.Length + 1];

            oldSpans.CopyTo(newSpans, 0);

            newSpans[oldSpans.Length] = span;

            Spans = newSpans;
        }

        /// <summary>
        /// Performs the given range action, or one from a migration that overlaps and has not synced yet.
        /// </summary>
        /// <param name="offset">The offset to pass to the action</param>
        /// <param name="size">The size to pass to the action</param>
        /// <param name="syncNumber">The sync number that has been reached</param>
        /// <param name="rangeAction">The action to perform</param>
        public void RangeActionWithMigration(ulong offset, ulong size, ulong syncNumber, BufferFlushAction rangeAction)
        {
            long syncDiff = (long)(syncNumber - SyncNumber);

            if (syncDiff >= 0)
            {
                // The migration has completed. Run the parent action.
                rangeAction(offset, size, syncNumber);
            }
            else
            {
                Interlocked.Increment(ref _refCount);

                ulong prevAddress = offset;
                ulong endAddress = offset + size;

                foreach (BufferMigrationSpan span in Spans)
                {
                    if (!span.Overlaps(offset, size))
                    {
                        continue;
                    }

                    if (span.Address > prevAddress)
                    {
                        // There's a gap between this span and the last (or the start address). Flush the range using the parent action.

                        rangeAction(prevAddress, span.Address - prevAddress, syncNumber);
                    }

                    span.RangeActionWithMigration(offset, size, syncNumber);

                    prevAddress = span.Address + span.Size;
                }

                if (endAddress > prevAddress)
                {
                    // There's a gap at the end of the range with no migration. Flush the range using the parent action.
                    rangeAction(prevAddress, endAddress - prevAddress, syncNumber);
                }

                Interlocked.Decrement(ref _refCount);
            }
        }

        /// <summary>
        /// Dispose the buffer migration. This removes the reference from the destination range list,
        /// and runs all the dispose buffers for the migration spans. (typically disposes the source buffer)
        /// </summary>
        public void Dispose()
        {
            while (Volatile.Read(ref _refCount) > 0)
            {
                // Coming into this method, the sync for the migration will be met, so nothing can increment the ref count.
                // However, an existing traversal of the spans for data flush could still be in progress.
                // Spin if this is ever the case, so they don't get disposed before the operation is complete.
            }

            Destination.RemoveMigration(this);

            foreach (BufferMigrationSpan span in Spans)
            {
                span.Dispose();
            }
        }
    }

    /// <summary>
    /// A record of when buffer data was copied from one buffer to another, for a specific range in a source buffer.
    /// Keeps the source buffer alive for data flushes until the migration is complete.
    /// </summary>
    internal readonly struct BufferMigrationSpan : IDisposable
    {
        /// <summary>
        /// The offset for the migrated region.
        /// </summary>
        public readonly ulong Address;

        /// <summary>
        /// The size for the migrated region.
        /// </summary>
        public readonly ulong Size;

        /// <summary>
        /// The action to perform when the migration isn't needed anymore.
        /// </summary>
        private readonly Action _disposeAction;

        /// <summary>
        /// The source range action, to be called on overlap with an unreached sync number.
        /// </summary>
        private readonly BufferFlushAction _sourceRangeAction;

        /// <summary>
        /// Optional migration for the source data. Can chain together if many migrations happen in a short time.
        /// If this is null, then _sourceRangeAction will always provide up to date data.
        /// </summary>
        private readonly BufferMigration _source;

        /// <summary>
        /// Creates a record for a buffer migration.
        /// </summary>
        /// <param name="buffer">The source buffer for this migration</param>
        /// <param name="disposeAction">The action to perform when the migration isn't needed anymore</param>
        /// <param name="sourceRangeAction">The flush action for the source buffer</param>
        /// <param name="source">Pending migration for the source buffer</param>
        public BufferMigrationSpan(
            Buffer buffer,
            Action disposeAction,
            BufferFlushAction sourceRangeAction,
            BufferMigration source)
        {
            Address = buffer.Address;
            Size = buffer.Size;
            _disposeAction = disposeAction;
            _sourceRangeAction = sourceRangeAction;
            _source = source;
        }

        /// <summary>
        /// Creates a record for a buffer migration, using the default buffer dispose action.
        /// </summary>
        /// <param name="buffer">The source buffer for this migration</param>
        /// <param name="sourceRangeAction">The flush action for the source buffer</param>
        /// <param name="source">Pending migration for the source buffer</param>
        public BufferMigrationSpan(
            Buffer buffer,
            BufferFlushAction sourceRangeAction,
            BufferMigration source) : this(buffer, buffer.DecrementReferenceCount, sourceRangeAction, source) { }

        /// <summary>
        /// Determine if the given range overlaps this migration, and has not been completed yet.
        /// </summary>
        /// <param name="offset">Start offset</param>
        /// <param name="size">Range size</param>
        /// <returns>True if overlapping and in progress, false otherwise</returns>
        public bool Overlaps(ulong offset, ulong size)
        {
            ulong end = offset + size;
            ulong destEnd = Address + Size;

            return !(end <= Address || offset >= destEnd);
        }

        /// <summary>
        /// Perform the migration source's range action on the range provided, clamped to the bounds of the source buffer.
        /// </summary>
        /// <param name="offset">Start offset</param>
        /// <param name="size">Range size</param>
        /// <param name="syncNumber">Current sync number</param>
        public void RangeActionWithMigration(ulong offset, ulong size, ulong syncNumber)
        {
            ulong end = offset + size;
            end = Math.Min(Address + Size, end);
            offset = Math.Max(Address, offset);

            size = end - offset;

            if (_source != null)
            {
                _source.RangeActionWithMigration(offset, size, syncNumber, _sourceRangeAction);
            }
            else
            {
                _sourceRangeAction(offset, size, syncNumber);
            }
        }

        /// <summary>
        /// Removes this migration span, potentially allowing for the source buffer to be disposed.
        /// </summary>
        public void Dispose()
        {
            _disposeAction();
        }
    }
}