InlineToMemoryClass.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. namespace Ryujinx.Graphics.Gpu.Engine.InlineToMemory
  8. {
  9. /// <summary>
  10. /// Represents a Inline-to-Memory engine class.
  11. /// </summary>
  12. class InlineToMemoryClass : IDeviceState
  13. {
  14. private readonly GpuContext _context;
  15. private readonly GpuChannel _channel;
  16. private readonly DeviceState<InlineToMemoryClassState> _state;
  17. private bool _isLinear;
  18. private int _offset;
  19. private int _size;
  20. private ulong _dstGpuVa;
  21. private int _dstX;
  22. private int _dstY;
  23. private int _dstWidth;
  24. private int _dstHeight;
  25. private int _dstStride;
  26. private int _dstGobBlocksInY;
  27. private int _lineLengthIn;
  28. private int _lineCount;
  29. private bool _finished;
  30. private int[] _buffer;
  31. /// <summary>
  32. /// Creates a new instance of the Inline-to-Memory engine class.
  33. /// </summary>
  34. /// <param name="context">GPU context</param>
  35. /// <param name="channel">GPU channel</param>
  36. /// <param name="initializeState">Indicates if the internal state should be initialized. Set to false if part of another engine</param>
  37. public InlineToMemoryClass(GpuContext context, GpuChannel channel, bool initializeState)
  38. {
  39. _context = context;
  40. _channel = channel;
  41. if (initializeState)
  42. {
  43. _state = new DeviceState<InlineToMemoryClassState>(new Dictionary<string, RwCallback>
  44. {
  45. { nameof(InlineToMemoryClassState.LaunchDma), new RwCallback(LaunchDma, null) },
  46. { nameof(InlineToMemoryClassState.LoadInlineData), new RwCallback(LoadInlineData, null) }
  47. });
  48. }
  49. }
  50. /// <summary>
  51. /// Creates a new instance of the inline-to-memory engine class.
  52. /// </summary>
  53. /// <param name="context">GPU context</param>
  54. /// <param name="channel">GPU channel</param>
  55. public InlineToMemoryClass(GpuContext context, GpuChannel channel) : this(context, channel, true)
  56. {
  57. }
  58. /// <summary>
  59. /// Reads data from the class registers.
  60. /// </summary>
  61. /// <param name="offset">Register byte offset</param>
  62. /// <returns>Data at the specified offset</returns>
  63. public int Read(int offset) => _state.Read(offset);
  64. /// <summary>
  65. /// Writes data to the class registers.
  66. /// </summary>
  67. /// <param name="offset">Register byte offset</param>
  68. /// <param name="data">Data to be written</param>
  69. public void Write(int offset, int data) => _state.Write(offset, data);
  70. /// <summary>
  71. /// Launches Inline-to-Memory engine DMA copy.
  72. /// </summary>
  73. /// <param name="argument">Method call argument</param>
  74. private void LaunchDma(int argument)
  75. {
  76. LaunchDma(ref _state.State, argument);
  77. }
  78. /// <summary>
  79. /// Launches Inline-to-Memory engine DMA copy.
  80. /// </summary>
  81. /// <param name="state">Current class state</param>
  82. /// <param name="argument">Method call argument</param>
  83. public void LaunchDma(ref InlineToMemoryClassState state, int argument)
  84. {
  85. _isLinear = (argument & 1) != 0;
  86. _offset = 0;
  87. _size = (int)(state.LineLengthIn * state.LineCount);
  88. int count = BitUtils.DivRoundUp(_size, 4);
  89. if (_buffer == null || _buffer.Length < count)
  90. {
  91. _buffer = new int[count];
  92. }
  93. ulong dstGpuVa = ((ulong)state.OffsetOutUpperValue << 32) | state.OffsetOut;
  94. ulong dstBaseAddress = _channel.MemoryManager.Translate(dstGpuVa);
  95. // Trigger read tracking, to flush any managed resources in the destination region.
  96. _channel.MemoryManager.Physical.GetSpan(dstBaseAddress, _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. Span<byte> data = MemoryMarshal.Cast<int, byte>(_buffer).Slice(0, _size);
  146. if (_isLinear && _lineCount == 1)
  147. {
  148. ulong address = _channel.MemoryManager.Translate(_dstGpuVa);
  149. _channel.MemoryManager.Physical.Write(address, 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. ulong dstBaseAddress = _channel.MemoryManager.Translate(_dstGpuVa);
  162. for (int y = _dstY; y < _dstY + _lineCount; y++)
  163. {
  164. int x1 = _dstX;
  165. int x2 = _dstX + _lineLengthIn;
  166. int x2Trunc = _dstX + BitUtils.AlignDown(_lineLengthIn, 16);
  167. int x;
  168. for (x = x1; x < x2Trunc; x += 16, srcOffset += 16)
  169. {
  170. int dstOffset = dstCalculator.GetOffset(x, y);
  171. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  172. Span<byte> pixel = data.Slice(srcOffset, 16);
  173. _channel.MemoryManager.Physical.Write(dstAddress, pixel);
  174. }
  175. for (; x < x2; x++, srcOffset++)
  176. {
  177. int dstOffset = dstCalculator.GetOffset(x, y);
  178. ulong dstAddress = dstBaseAddress + (ulong)dstOffset;
  179. Span<byte> pixel = data.Slice(srcOffset, 1);
  180. _channel.MemoryManager.Physical.Write(dstAddress, pixel);
  181. }
  182. }
  183. }
  184. _finished = true;
  185. _context.AdvanceSequence();
  186. }
  187. }
  188. }