InlineToMemoryClass.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Device;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Intrinsics;
  8. namespace Ryujinx.Graphics.Gpu.Engine.InlineToMemory
  9. {
  10. /// <summary>
  11. /// Represents a Inline-to-Memory engine class.
  12. /// </summary>
  13. class InlineToMemoryClass : IDeviceState
  14. {
  15. private readonly GpuContext _context;
  16. private readonly GpuChannel _channel;
  17. private readonly DeviceState<InlineToMemoryClassState> _state;
  18. private bool _isLinear;
  19. private int _offset;
  20. private int _size;
  21. private ulong _dstGpuVa;
  22. private int _dstX;
  23. private int _dstY;
  24. private int _dstWidth;
  25. private int _dstHeight;
  26. private int _dstStride;
  27. private int _dstGobBlocksInY;
  28. private int _lineLengthIn;
  29. private int _lineCount;
  30. private bool _finished;
  31. private int[] _buffer;
  32. /// <summary>
  33. /// Creates a new instance of the Inline-to-Memory engine class.
  34. /// </summary>
  35. /// <param name="context">GPU context</param>
  36. /// <param name="channel">GPU channel</param>
  37. /// <param name="initializeState">Indicates if the internal state should be initialized. Set to false if part of another engine</param>
  38. public InlineToMemoryClass(GpuContext context, GpuChannel channel, bool initializeState)
  39. {
  40. _context = context;
  41. _channel = channel;
  42. if (initializeState)
  43. {
  44. _state = new DeviceState<InlineToMemoryClassState>(new Dictionary<string, RwCallback>
  45. {
  46. { nameof(InlineToMemoryClassState.LaunchDma), new RwCallback(LaunchDma, null) },
  47. { nameof(InlineToMemoryClassState.LoadInlineData), new RwCallback(LoadInlineData, null) }
  48. });
  49. }
  50. }
  51. /// <summary>
  52. /// Creates a new instance of the inline-to-memory engine class.
  53. /// </summary>
  54. /// <param name="context">GPU context</param>
  55. /// <param name="channel">GPU channel</param>
  56. public InlineToMemoryClass(GpuContext context, GpuChannel channel) : this(context, channel, true)
  57. {
  58. }
  59. /// <summary>
  60. /// Reads data from the class registers.
  61. /// </summary>
  62. /// <param name="offset">Register byte offset</param>
  63. /// <returns>Data at the specified offset</returns>
  64. public int Read(int offset) => _state.Read(offset);
  65. /// <summary>
  66. /// Writes data to the class registers.
  67. /// </summary>
  68. /// <param name="offset">Register byte offset</param>
  69. /// <param name="data">Data to be written</param>
  70. public void Write(int offset, int data) => _state.Write(offset, data);
  71. /// <summary>
  72. /// Launches Inline-to-Memory engine DMA copy.
  73. /// </summary>
  74. /// <param name="argument">Method call argument</param>
  75. private void LaunchDma(int argument)
  76. {
  77. LaunchDma(ref _state.State, argument);
  78. }
  79. /// <summary>
  80. /// Launches Inline-to-Memory engine DMA copy.
  81. /// </summary>
  82. /// <param name="state">Current class state</param>
  83. /// <param name="argument">Method call argument</param>
  84. public void LaunchDma(ref InlineToMemoryClassState state, int argument)
  85. {
  86. _isLinear = (argument & 1) != 0;
  87. _offset = 0;
  88. _size = (int)(state.LineLengthIn * state.LineCount);
  89. int count = BitUtils.DivRoundUp(_size, 4);
  90. if (_buffer == null || _buffer.Length < count)
  91. {
  92. _buffer = new int[count];
  93. }
  94. ulong dstGpuVa = ((ulong)state.OffsetOutUpperValue << 32) | state.OffsetOut;
  95. // Trigger read tracking, to flush any managed resources in the destination region.
  96. _channel.MemoryManager.GetSpan(dstGpuVa, _size, true);
  97. _dstGpuVa = dstGpuVa;
  98. _dstX = state.SetDstOriginBytesXV;
  99. _dstY = state.SetDstOriginSamplesYV;
  100. _dstWidth = (int)state.SetDstWidth;
  101. _dstHeight = (int)state.SetDstHeight;
  102. _dstStride = (int)state.PitchOut;
  103. _dstGobBlocksInY = 1 << (int)state.SetDstBlockSizeHeight;
  104. _lineLengthIn = (int)state.LineLengthIn;
  105. _lineCount = (int)state.LineCount;
  106. _finished = false;
  107. }
  108. /// <summary>
  109. /// Pushes a block of data to the Inline-to-Memory engine.
  110. /// </summary>
  111. /// <param name="data">Data to push</param>
  112. public void LoadInlineData(ReadOnlySpan<int> data)
  113. {
  114. if (!_finished)
  115. {
  116. int copySize = Math.Min(data.Length, _buffer.Length - _offset);
  117. data.Slice(0, copySize).CopyTo(new Span<int>(_buffer).Slice(_offset, copySize));
  118. _offset += copySize;
  119. if (_offset * 4 >= _size)
  120. {
  121. FinishTransfer();
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Pushes a word of data to the Inline-to-Memory engine.
  127. /// </summary>
  128. /// <param name="argument">Method call argument</param>
  129. public void LoadInlineData(int argument)
  130. {
  131. if (!_finished)
  132. {
  133. _buffer[_offset++] = argument;
  134. if (_offset * 4 >= _size)
  135. {
  136. FinishTransfer();
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Performs actual copy of the inline data after the transfer is finished.
  142. /// </summary>
  143. private void FinishTransfer()
  144. {
  145. var memoryManager = _channel.MemoryManager;
  146. var data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  147. if (_isLinear && _lineCount == 1)
  148. {
  149. memoryManager.Write(_dstGpuVa, data);
  150. }
  151. else
  152. {
  153. var dstCalculator = new OffsetCalculator(
  154. _dstWidth,
  155. _dstHeight,
  156. _dstStride,
  157. _isLinear,
  158. _dstGobBlocksInY,
  159. 1);
  160. int srcOffset = 0;
  161. for (int y = _dstY; y < _dstY + _lineCount; y++)
  162. {
  163. int x1 = _dstX;
  164. int x2 = _dstX + _lineLengthIn;
  165. int x1Round = BitUtils.AlignUp(_dstX, 16);
  166. int x2Trunc = BitUtils.AlignDown(x2, 16);
  167. int x = x1;
  168. if (x1Round <= x2)
  169. {
  170. for (; x < x1Round; x++, srcOffset++)
  171. {
  172. int dstOffset = dstCalculator.GetOffset(x, y);
  173. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  174. memoryManager.Write(dstAddress, data[srcOffset]);
  175. }
  176. }
  177. for (; x < x2Trunc; x += 16, srcOffset += 16)
  178. {
  179. int dstOffset = dstCalculator.GetOffset(x, y);
  180. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  181. memoryManager.Write(dstAddress, MemoryMarshal.Cast<byte, Vector128<byte>>(data.Slice(srcOffset, 16))[0]);
  182. }
  183. for (; x < x2; x++, srcOffset++)
  184. {
  185. int dstOffset = dstCalculator.GetOffset(x, y);
  186. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  187. memoryManager.Write(dstAddress, data[srcOffset]);
  188. }
  189. }
  190. }
  191. _finished = true;
  192. _context.AdvanceSequence();
  193. }
  194. }
  195. }