BufferMap.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Threading;
  5. namespace Ryujinx.Graphics.GAL.Multithreading
  6. {
  7. /// <summary>
  8. /// Buffer handles given to the client are not the same as those provided by the backend,
  9. /// as their handle is created at a later point on the queue.
  10. /// The handle returned is a unique identifier that will map to the real buffer when it is available.
  11. /// Note that any uses within the queue should be safe, but outside you must use MapBufferBlocking.
  12. /// </summary>
  13. class BufferMap
  14. {
  15. private ulong _bufferHandle = 0;
  16. private Dictionary<BufferHandle, BufferHandle> _bufferMap = new Dictionary<BufferHandle, BufferHandle>();
  17. private HashSet<BufferHandle> _inFlight = new HashSet<BufferHandle>();
  18. private AutoResetEvent _inFlightChanged = new AutoResetEvent(false);
  19. internal BufferHandle CreateBufferHandle()
  20. {
  21. ulong handle64 = Interlocked.Increment(ref _bufferHandle);
  22. BufferHandle threadedHandle = Unsafe.As<ulong, BufferHandle>(ref handle64);
  23. lock (_inFlight)
  24. {
  25. _inFlight.Add(threadedHandle);
  26. }
  27. return threadedHandle;
  28. }
  29. internal void AssignBuffer(BufferHandle threadedHandle, BufferHandle realHandle)
  30. {
  31. lock (_bufferMap)
  32. {
  33. _bufferMap[threadedHandle] = realHandle;
  34. }
  35. lock (_inFlight)
  36. {
  37. _inFlight.Remove(threadedHandle);
  38. }
  39. _inFlightChanged.Set();
  40. }
  41. internal void UnassignBuffer(BufferHandle threadedHandle)
  42. {
  43. lock (_bufferMap)
  44. {
  45. _bufferMap.Remove(threadedHandle);
  46. }
  47. }
  48. internal BufferHandle MapBuffer(BufferHandle handle)
  49. {
  50. // Maps a threaded buffer to a backend one.
  51. // Threaded buffers are returned on creation as the buffer
  52. // isn't actually created until the queue runs the command.
  53. BufferHandle result;
  54. lock (_bufferMap)
  55. {
  56. if (!_bufferMap.TryGetValue(handle, out result))
  57. {
  58. result = BufferHandle.Null;
  59. }
  60. return result;
  61. }
  62. }
  63. internal BufferHandle MapBufferBlocking(BufferHandle handle)
  64. {
  65. // Blocks until the handle is available.
  66. BufferHandle result;
  67. lock (_bufferMap)
  68. {
  69. if (_bufferMap.TryGetValue(handle, out result))
  70. {
  71. return result;
  72. }
  73. }
  74. bool signal = false;
  75. while (true)
  76. {
  77. lock (_inFlight)
  78. {
  79. if (!_inFlight.Contains(handle))
  80. {
  81. break;
  82. }
  83. }
  84. _inFlightChanged.WaitOne();
  85. signal = true;
  86. }
  87. if (signal)
  88. {
  89. // Signal other threads which might still be waiting.
  90. _inFlightChanged.Set();
  91. }
  92. return MapBuffer(handle);
  93. }
  94. internal BufferRange MapBufferRange(BufferRange range)
  95. {
  96. return new BufferRange(MapBuffer(range.Handle), range.Offset, range.Size);
  97. }
  98. internal Span<BufferRange> MapBufferRanges(Span<BufferRange> ranges)
  99. {
  100. // Rewrite the buffer ranges to point to the mapped handles.
  101. lock (_bufferMap)
  102. {
  103. for (int i = 0; i < ranges.Length; i++)
  104. {
  105. ref BufferRange range = ref ranges[i];
  106. BufferHandle result;
  107. if (!_bufferMap.TryGetValue(range.Handle, out result))
  108. {
  109. result = BufferHandle.Null;
  110. }
  111. range = new BufferRange(result, range.Offset, range.Size);
  112. }
  113. }
  114. return ranges;
  115. }
  116. internal Span<VertexBufferDescriptor> MapBufferRanges(Span<VertexBufferDescriptor> ranges)
  117. {
  118. // Rewrite the buffer ranges to point to the mapped handles.
  119. lock (_bufferMap)
  120. {
  121. for (int i = 0; i < ranges.Length; i++)
  122. {
  123. BufferRange range = ranges[i].Buffer;
  124. BufferHandle result;
  125. if (!_bufferMap.TryGetValue(range.Handle, out result))
  126. {
  127. result = BufferHandle.Null;
  128. }
  129. range = new BufferRange(result, range.Offset, range.Size);
  130. ranges[i] = new VertexBufferDescriptor(range, ranges[i].Stride, ranges[i].Divisor);
  131. }
  132. }
  133. return ranges;
  134. }
  135. }
  136. }