InlineToMemoryClass.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 _dstGobBlocksInZ;
  29. private int _lineLengthIn;
  30. private int _lineCount;
  31. private bool _finished;
  32. private int[] _buffer;
  33. /// <summary>
  34. /// Creates a new instance of the Inline-to-Memory engine class.
  35. /// </summary>
  36. /// <param name="context">GPU context</param>
  37. /// <param name="channel">GPU channel</param>
  38. /// <param name="initializeState">Indicates if the internal state should be initialized. Set to false if part of another engine</param>
  39. public InlineToMemoryClass(GpuContext context, GpuChannel channel, bool initializeState)
  40. {
  41. _context = context;
  42. _channel = channel;
  43. if (initializeState)
  44. {
  45. _state = new DeviceState<InlineToMemoryClassState>(new Dictionary<string, RwCallback>
  46. {
  47. { nameof(InlineToMemoryClassState.LaunchDma), new RwCallback(LaunchDma, null) },
  48. { nameof(InlineToMemoryClassState.LoadInlineData), new RwCallback(LoadInlineData, null) }
  49. });
  50. }
  51. }
  52. /// <summary>
  53. /// Creates a new instance of the inline-to-memory engine class.
  54. /// </summary>
  55. /// <param name="context">GPU context</param>
  56. /// <param name="channel">GPU channel</param>
  57. public InlineToMemoryClass(GpuContext context, GpuChannel channel) : this(context, channel, true)
  58. {
  59. }
  60. /// <summary>
  61. /// Reads data from the class registers.
  62. /// </summary>
  63. /// <param name="offset">Register byte offset</param>
  64. /// <returns>Data at the specified offset</returns>
  65. public int Read(int offset) => _state.Read(offset);
  66. /// <summary>
  67. /// Writes data to the class registers.
  68. /// </summary>
  69. /// <param name="offset">Register byte offset</param>
  70. /// <param name="data">Data to be written</param>
  71. public void Write(int offset, int data) => _state.Write(offset, data);
  72. /// <summary>
  73. /// Launches Inline-to-Memory engine DMA copy.
  74. /// </summary>
  75. /// <param name="argument">Method call argument</param>
  76. private void LaunchDma(int argument)
  77. {
  78. LaunchDma(ref _state.State, argument);
  79. }
  80. /// <summary>
  81. /// Launches Inline-to-Memory engine DMA copy.
  82. /// </summary>
  83. /// <param name="state">Current class state</param>
  84. /// <param name="argument">Method call argument</param>
  85. public void LaunchDma(ref InlineToMemoryClassState state, int argument)
  86. {
  87. _isLinear = (argument & 1) != 0;
  88. _offset = 0;
  89. _size = (int)(BitUtils.AlignUp(state.LineLengthIn, 4) * state.LineCount);
  90. int count = _size / 4;
  91. if (_buffer == null || _buffer.Length < count)
  92. {
  93. _buffer = new int[count];
  94. }
  95. ulong dstGpuVa = ((ulong)state.OffsetOutUpperValue << 32) | state.OffsetOut;
  96. _dstGpuVa = dstGpuVa;
  97. _dstX = state.SetDstOriginBytesXV;
  98. _dstY = state.SetDstOriginSamplesYV;
  99. _dstWidth = (int)state.SetDstWidth;
  100. _dstHeight = (int)state.SetDstHeight;
  101. _dstStride = (int)state.PitchOut;
  102. _dstGobBlocksInY = 1 << (int)state.SetDstBlockSizeHeight;
  103. _dstGobBlocksInZ = 1 << (int)state.SetDstBlockSizeDepth;
  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.WriteTrackedResource(_dstGpuVa, data.Slice(0, _lineLengthIn));
  150. _context.AdvanceSequence();
  151. }
  152. else
  153. {
  154. // TODO: Verify if the destination X/Y and width/height are taken into account
  155. // for linear texture transfers. If not, we can use the fast path for that aswell.
  156. // Right now the copy code at the bottom assumes that it is used on both which might be incorrect.
  157. if (!_isLinear)
  158. {
  159. var target = memoryManager.Physical.TextureCache.FindTexture(
  160. memoryManager,
  161. _dstGpuVa,
  162. 1,
  163. _dstStride,
  164. _dstHeight,
  165. _lineLengthIn,
  166. _lineCount,
  167. _isLinear,
  168. _dstGobBlocksInY,
  169. _dstGobBlocksInZ);
  170. if (target != null)
  171. {
  172. target.SetData(data, 0, 0, new GAL.Rectangle<int>(_dstX, _dstY, _lineLengthIn / target.Info.FormatInfo.BytesPerPixel, _lineCount));
  173. return;
  174. }
  175. }
  176. var dstCalculator = new OffsetCalculator(
  177. _dstWidth,
  178. _dstHeight,
  179. _dstStride,
  180. _isLinear,
  181. _dstGobBlocksInY,
  182. 1);
  183. int srcOffset = 0;
  184. for (int y = _dstY; y < _dstY + _lineCount; y++)
  185. {
  186. int x1 = _dstX;
  187. int x2 = _dstX + _lineLengthIn;
  188. int x1Round = BitUtils.AlignUp(_dstX, 16);
  189. int x2Trunc = BitUtils.AlignDown(x2, 16);
  190. int x = x1;
  191. if (x1Round <= x2)
  192. {
  193. for (; x < x1Round; x++, srcOffset++)
  194. {
  195. int dstOffset = dstCalculator.GetOffset(x, y);
  196. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  197. memoryManager.Write(dstAddress, data[srcOffset]);
  198. }
  199. }
  200. for (; x < x2Trunc; x += 16, srcOffset += 16)
  201. {
  202. int dstOffset = dstCalculator.GetOffset(x, y);
  203. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  204. memoryManager.Write(dstAddress, MemoryMarshal.Cast<byte, Vector128<byte>>(data.Slice(srcOffset, 16))[0]);
  205. }
  206. for (; x < x2; x++, srcOffset++)
  207. {
  208. int dstOffset = dstCalculator.GetOffset(x, y);
  209. ulong dstAddress = _dstGpuVa + (uint)dstOffset;
  210. memoryManager.Write(dstAddress, data[srcOffset]);
  211. }
  212. // All lines must be aligned to 4 bytes, as the data is pushed one word at a time.
  213. // If our copy length is not a multiple of 4, then we need to skip the padding bytes here.
  214. int misalignment = _lineLengthIn & 3;
  215. if (misalignment != 0)
  216. {
  217. srcOffset += 4 - misalignment;
  218. }
  219. }
  220. _context.AdvanceSequence();
  221. }
  222. _finished = true;
  223. }
  224. }
  225. }