InlineToMemoryClass.cs 7.7 KB

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