aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.GAL/Multithreading/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/Ryujinx.Graphics.GAL/Multithreading/Model')
-rw-r--r--src/Ryujinx.Graphics.GAL/Multithreading/Model/CircularSpanPool.cs89
-rw-r--r--src/Ryujinx.Graphics.GAL/Multithreading/Model/ResultBox.cs7
-rw-r--r--src/Ryujinx.Graphics.GAL/Multithreading/Model/SpanRef.cs39
-rw-r--r--src/Ryujinx.Graphics.GAL/Multithreading/Model/TableRef.cs22
4 files changed, 157 insertions, 0 deletions
diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Model/CircularSpanPool.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Model/CircularSpanPool.cs
new file mode 100644
index 00000000..4ea1a2c7
--- /dev/null
+++ b/src/Ryujinx.Graphics.GAL/Multithreading/Model/CircularSpanPool.cs
@@ -0,0 +1,89 @@
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace Ryujinx.Graphics.GAL.Multithreading.Model
+{
+ /// <summary>
+ /// A memory pool for passing through Span<T> resources with one producer and consumer.
+ /// Data is copied on creation to part of the pool, then that region is reserved until it is disposed by the consumer.
+ /// Similar to the command queue, this pool assumes that data is created and disposed in the same order.
+ /// </summary>
+ class CircularSpanPool
+ {
+ private ThreadedRenderer _renderer;
+ private byte[] _pool;
+ private int _size;
+
+ private int _producerPtr;
+ private int _producerSkipPosition = -1;
+ private int _consumerPtr;
+
+ public CircularSpanPool(ThreadedRenderer renderer, int size)
+ {
+ _renderer = renderer;
+ _size = size;
+ _pool = new byte[size];
+ }
+
+ public SpanRef<T> Insert<T>(ReadOnlySpan<T> data) where T : unmanaged
+ {
+ int size = data.Length * Unsafe.SizeOf<T>();
+
+ // Wrapping aware circular queue.
+ // If there's no space at the end of the pool for this span, we can't fragment it.
+ // So just loop back around to the start. Remember the last skipped position.
+
+ bool wraparound = _producerPtr + size >= _size;
+ int index = wraparound ? 0 : _producerPtr;
+
+ // _consumerPtr is from another thread, and we're taking it without a lock, so treat this as a snapshot in the past.
+ // We know that it will always be before or equal to the producer pointer, and it cannot pass it.
+ // This is enough to reason about if there is space in the queue for the data, even if we're checking against an outdated value.
+
+ int consumer = _consumerPtr;
+ bool beforeConsumer = _producerPtr < consumer;
+
+ if (size > _size - 1 || (wraparound && beforeConsumer) || ((index < consumer || wraparound) && index + size >= consumer))
+ {
+ // Just get an array in the following situations:
+ // - The data is too large to fit in the pool.
+ // - A wraparound would happen but the consumer would be covered by it.
+ // - The producer would catch up to the consumer as a result.
+
+ return new SpanRef<T>(_renderer, data.ToArray());
+ }
+
+ data.CopyTo(MemoryMarshal.Cast<byte, T>(new Span<byte>(_pool).Slice(index, size)));
+
+ if (wraparound)
+ {
+ _producerSkipPosition = _producerPtr;
+ }
+
+ _producerPtr = index + size;
+
+ return new SpanRef<T>(data.Length);
+ }
+
+ public Span<T> Get<T>(int length) where T : unmanaged
+ {
+ int size = length * Unsafe.SizeOf<T>();
+
+ if (_consumerPtr == Interlocked.CompareExchange(ref _producerSkipPosition, -1, _consumerPtr))
+ {
+ _consumerPtr = 0;
+ }
+
+ return MemoryMarshal.Cast<byte, T>(new Span<byte>(_pool).Slice(_consumerPtr, size));
+ }
+
+ public void Dispose<T>(int length) where T : unmanaged
+ {
+ int size = length * Unsafe.SizeOf<T>();
+
+ _consumerPtr = _consumerPtr + size;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Model/ResultBox.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Model/ResultBox.cs
new file mode 100644
index 00000000..7a0be785
--- /dev/null
+++ b/src/Ryujinx.Graphics.GAL/Multithreading/Model/ResultBox.cs
@@ -0,0 +1,7 @@
+namespace Ryujinx.Graphics.GAL.Multithreading.Model
+{
+ public class ResultBox<T>
+ {
+ public T Result;
+ }
+}
diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Model/SpanRef.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Model/SpanRef.cs
new file mode 100644
index 00000000..7dbebc76
--- /dev/null
+++ b/src/Ryujinx.Graphics.GAL/Multithreading/Model/SpanRef.cs
@@ -0,0 +1,39 @@
+using System;
+
+namespace Ryujinx.Graphics.GAL.Multithreading.Model
+{
+ struct SpanRef<T> where T : unmanaged
+ {
+ private int _packedLengthId;
+
+ public SpanRef(ThreadedRenderer renderer, T[] data)
+ {
+ _packedLengthId = -(renderer.AddTableRef(data) + 1);
+ }
+
+ public SpanRef(int length)
+ {
+ _packedLengthId = length;
+ }
+
+ public Span<T> Get(ThreadedRenderer renderer)
+ {
+ if (_packedLengthId >= 0)
+ {
+ return renderer.SpanPool.Get<T>(_packedLengthId);
+ }
+ else
+ {
+ return new Span<T>((T[])renderer.RemoveTableRef(-(_packedLengthId + 1)));
+ }
+ }
+
+ public void Dispose(ThreadedRenderer renderer)
+ {
+ if (_packedLengthId > 0)
+ {
+ renderer.SpanPool.Dispose<T>(_packedLengthId);
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.GAL/Multithreading/Model/TableRef.cs b/src/Ryujinx.Graphics.GAL/Multithreading/Model/TableRef.cs
new file mode 100644
index 00000000..166aa71a
--- /dev/null
+++ b/src/Ryujinx.Graphics.GAL/Multithreading/Model/TableRef.cs
@@ -0,0 +1,22 @@
+namespace Ryujinx.Graphics.GAL.Multithreading.Model
+{
+ struct TableRef<T>
+ {
+ private int _index;
+
+ public TableRef(ThreadedRenderer renderer, T reference)
+ {
+ _index = renderer.AddTableRef(reference);
+ }
+
+ public T Get(ThreadedRenderer renderer)
+ {
+ return (T)renderer.RemoveTableRef(_index);
+ }
+
+ public T2 GetAs<T2>(ThreadedRenderer renderer) where T2 : T
+ {
+ return (T2)renderer.RemoveTableRef(_index);
+ }
+ }
+}