InlineToMemoryClass.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ulong dstBaseAddress = _channel.MemoryManager.Translate(dstGpuVa);
  96. // Trigger read tracking, to flush any managed resources in the destination region.
  97. _channel.MemoryManager.Physical.GetSpan(dstBaseAddress, _size, true);
  98. _dstGpuVa = dstGpuVa;
  99. _dstX = state.SetDstOriginBytesXV;
  100. _dstY = state.SetDstOriginSamplesYV;
  101. _dstWidth = (int)state.SetDstWidth;
  102. _dstHeight = (int)state.SetDstHeight;
  103. _dstStride = (int)state.PitchOut;
  104. _dstGobBlocksInY = 1 << (int)state.SetDstBlockSizeHeight;
  105. _lineLengthIn = (int)state.LineLengthIn;
  106. _lineCount = (int)state.LineCount;
  107. _finished = false;
  108. }
  109. /// <summary>
  110. /// Pushes a block of data to the Inline-to-Memory engine.
  111. /// </summary>
  112. /// <param name="data">Data to push</param>
  113. public void LoadInlineData(ReadOnlySpan<int> data)
  114. {
  115. if (!_finished)
  116. {
  117. int copySize = Math.Min(data.Length, _buffer.Length - _offset);
  118. data.Slice(0, copySize).CopyTo(new Span<int>(_buffer).Slice(_offset, copySize));
  119. _offset += copySize;
  120. if (_offset * 4 >= _size)
  121. {
  122. FinishTransfer();
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Pushes a word of data to the Inline-to-Memory engine.
  128. /// </summary>
  129. /// <param name="argument">Method call argument</param>
  130. public void LoadInlineData(int argument)
  131. {
  132. if (!_finished)
  133. {
  134. _buffer[_offset++] = argument;
  135. if (_offset * 4 >= _size)
  136. {
  137. FinishTransfer();
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Performs actual copy of the inline data after the transfer is finished.
  143. /// </summary>
  144. private void FinishTransfer()
  145. {
  146. var memoryManager = _channel.MemoryManager;
  147. var data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  148. if (_isLinear && _lineCount == 1)
  149. {
  150. memoryManager.Write(_dstGpuVa, data);
  151. }
  152. else
  153. {
  154. var dstCalculator = new OffsetCalculator(
  155. _dstWidth,
  156. _dstHeight,
  157. _dstStride,
  158. _isLinear,
  159. _dstGobBlocksInY,
  160. 1);
  161. int srcOffset = 0;
  162. for (int y = _dstY; y < _dstY + _lineCount; y++)
  163. {
  164. int x1 = _dstX;
  165. int x2 = _dstX + _lineLengthIn;
  166. int x1Round = BitUtils.AlignUp(_dstX, 16);
  167. int x2Trunc = BitUtils.AlignDown(x2, 16);
  168. int x = x1;
  169. if (x1Round <= x2)
  170. {
  171. for (; x < x1Round; x++, srcOffset++)
  172. {
  173. int dstOffset = dstCalculator.GetOffset(x, y);
  174. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  175. memoryManager.Write(dstAddress, data[srcOffset]);
  176. }
  177. }
  178. for (; x < x2Trunc; x += 16, srcOffset += 16)
  179. {
  180. int dstOffset = dstCalculator.GetOffset(x, y);
  181. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  182. memoryManager.Write(dstAddress, MemoryMarshal.Cast<byte, Vector128<byte>>(data.Slice(srcOffset, 16))[0]);
  183. }
  184. for (; x < x2; x++, srcOffset++)
  185. {
  186. int dstOffset = dstCalculator.GetOffset(x, y);
  187. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  188. memoryManager.Write(dstAddress, data[srcOffset]);
  189. }
  190. }
  191. }
  192. _finished = true;
  193. _context.AdvanceSequence();
  194. }
  195. }
  196. }