DmaClass.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Device;
  3. using Ryujinx.Graphics.Gpu.State;
  4. using Ryujinx.Graphics.Texture;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.Intrinsics;
  9. namespace Ryujinx.Graphics.Gpu.Engine.Dma
  10. {
  11. /// <summary>
  12. /// Represents a DMA copy engine class.
  13. /// </summary>
  14. class DmaClass : IDeviceState
  15. {
  16. private readonly GpuContext _context;
  17. private readonly GpuChannel _channel;
  18. private readonly DeviceState<DmaClassState> _state;
  19. /// <summary>
  20. /// Copy flags passed on DMA launch.
  21. /// </summary>
  22. [Flags]
  23. private enum CopyFlags
  24. {
  25. SrcLinear = 1 << 7,
  26. DstLinear = 1 << 8,
  27. MultiLineEnable = 1 << 9,
  28. RemapEnable = 1 << 10
  29. }
  30. /// <summary>
  31. /// Creates a new instance of the DMA copy engine class.
  32. /// </summary>
  33. /// <param name="context">GPU context</param>
  34. /// <param name="channel">GPU channel</param>
  35. public DmaClass(GpuContext context, GpuChannel channel)
  36. {
  37. _context = context;
  38. _channel = channel;
  39. _state = new DeviceState<DmaClassState>(new Dictionary<string, RwCallback>
  40. {
  41. { nameof(DmaClassState.LaunchDma), new RwCallback(LaunchDma, null) }
  42. });
  43. }
  44. /// <summary>
  45. /// Reads data from the class registers.
  46. /// </summary>
  47. /// <param name="offset">Register byte offset</param>
  48. /// <returns>Data at the specified offset</returns>
  49. public int Read(int offset) => _state.Read(offset);
  50. /// <summary>
  51. /// Writes data to the class registers.
  52. /// </summary>
  53. /// <param name="offset">Register byte offset</param>
  54. /// <param name="data">Data to be written</param>
  55. public void Write(int offset, int data) => _state.Write(offset, data);
  56. /// <summary>
  57. /// Determine if a buffer-to-texture region covers the entirety of a texture.
  58. /// </summary>
  59. /// <param name="tex">Texture to compare</param>
  60. /// <param name="linear">True if the texture is linear, false if block linear</param>
  61. /// <param name="bpp">Texture bytes per pixel</param>
  62. /// <param name="stride">Texture stride</param>
  63. /// <param name="xCount">Number of pixels to be copied</param>
  64. /// <param name="yCount">Number of lines to be copied</param>
  65. /// <returns></returns>
  66. private static bool IsTextureCopyComplete(CopyBufferTexture tex, bool linear, int bpp, int stride, int xCount, int yCount)
  67. {
  68. if (linear)
  69. {
  70. int alignWidth = Constants.StrideAlignment / bpp;
  71. return tex.RegionX == 0 &&
  72. tex.RegionY == 0 &&
  73. stride / bpp == BitUtils.AlignUp(xCount, alignWidth);
  74. }
  75. else
  76. {
  77. int alignWidth = Constants.GobAlignment / bpp;
  78. return tex.RegionX == 0 &&
  79. tex.RegionY == 0 &&
  80. tex.Width == BitUtils.AlignUp(xCount, alignWidth) &&
  81. tex.Height == yCount;
  82. }
  83. }
  84. /// <summary>
  85. /// Performs a buffer to buffer, or buffer to texture copy.
  86. /// </summary>
  87. /// <param name="argument">Method call argument</param>
  88. private void LaunchDma(int argument)
  89. {
  90. var memoryManager = _channel.MemoryManager;
  91. CopyFlags copyFlags = (CopyFlags)argument;
  92. bool srcLinear = copyFlags.HasFlag(CopyFlags.SrcLinear);
  93. bool dstLinear = copyFlags.HasFlag(CopyFlags.DstLinear);
  94. bool copy2D = copyFlags.HasFlag(CopyFlags.MultiLineEnable);
  95. bool remap = copyFlags.HasFlag(CopyFlags.RemapEnable);
  96. uint size = _state.State.LineLengthIn;
  97. if (size == 0)
  98. {
  99. return;
  100. }
  101. ulong srcGpuVa = ((ulong)_state.State.OffsetInUpperUpper << 32) | _state.State.OffsetInLower;
  102. ulong dstGpuVa = ((ulong)_state.State.OffsetOutUpperUpper << 32) | _state.State.OffsetOutLower;
  103. int xCount = (int)_state.State.LineLengthIn;
  104. int yCount = (int)_state.State.LineCount;
  105. _context.Methods.FlushUboDirty(memoryManager);
  106. if (copy2D)
  107. {
  108. // Buffer to texture copy.
  109. int componentSize = (int)_state.State.SetRemapComponentsComponentSize + 1;
  110. int srcBpp = remap ? ((int)_state.State.SetRemapComponentsNumSrcComponents + 1) * componentSize : 1;
  111. int dstBpp = remap ? ((int)_state.State.SetRemapComponentsNumDstComponents + 1) * componentSize : 1;
  112. var dst = Unsafe.As<uint, CopyBufferTexture>(ref _state.State.SetDstBlockSize);
  113. var src = Unsafe.As<uint, CopyBufferTexture>(ref _state.State.SetSrcBlockSize);
  114. int srcStride = (int)_state.State.PitchIn;
  115. int dstStride = (int)_state.State.PitchOut;
  116. var srcCalculator = new OffsetCalculator(
  117. src.Width,
  118. src.Height,
  119. srcStride,
  120. srcLinear,
  121. src.MemoryLayout.UnpackGobBlocksInY(),
  122. src.MemoryLayout.UnpackGobBlocksInZ(),
  123. srcBpp);
  124. var dstCalculator = new OffsetCalculator(
  125. dst.Width,
  126. dst.Height,
  127. dstStride,
  128. dstLinear,
  129. dst.MemoryLayout.UnpackGobBlocksInY(),
  130. dst.MemoryLayout.UnpackGobBlocksInZ(),
  131. dstBpp);
  132. ulong srcBaseAddress = memoryManager.Translate(srcGpuVa);
  133. ulong dstBaseAddress = memoryManager.Translate(dstGpuVa);
  134. (int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(src.RegionX, src.RegionY, xCount, yCount);
  135. (int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dst.RegionX, dst.RegionY, xCount, yCount);
  136. ReadOnlySpan<byte> srcSpan = memoryManager.Physical.GetSpan(srcBaseAddress + (ulong)srcBaseOffset, srcSize, true);
  137. Span<byte> dstSpan = memoryManager.Physical.GetSpan(dstBaseAddress + (ulong)dstBaseOffset, dstSize).ToArray();
  138. bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount);
  139. bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount);
  140. if (completeSource && completeDest)
  141. {
  142. var target = memoryManager.Physical.TextureCache.FindTexture(
  143. memoryManager,
  144. dst,
  145. dstGpuVa,
  146. dstBpp,
  147. dstStride,
  148. xCount,
  149. yCount,
  150. dstLinear);
  151. if (target != null)
  152. {
  153. ReadOnlySpan<byte> data;
  154. if (srcLinear)
  155. {
  156. data = LayoutConverter.ConvertLinearStridedToLinear(
  157. target.Info.Width,
  158. target.Info.Height,
  159. 1,
  160. 1,
  161. srcStride,
  162. target.Info.FormatInfo.BytesPerPixel,
  163. srcSpan);
  164. }
  165. else
  166. {
  167. data = LayoutConverter.ConvertBlockLinearToLinear(
  168. src.Width,
  169. src.Height,
  170. 1,
  171. target.Info.Levels,
  172. 1,
  173. 1,
  174. 1,
  175. srcBpp,
  176. src.MemoryLayout.UnpackGobBlocksInY(),
  177. src.MemoryLayout.UnpackGobBlocksInZ(),
  178. 1,
  179. new SizeInfo((int)target.Size),
  180. srcSpan);
  181. }
  182. target.SetData(data);
  183. target.SignalModified();
  184. return;
  185. }
  186. else if (srcCalculator.LayoutMatches(dstCalculator))
  187. {
  188. srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
  189. memoryManager.Physical.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
  190. return;
  191. }
  192. }
  193. unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
  194. {
  195. fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
  196. {
  197. byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
  198. byte* srcBase = srcPtr - srcBaseOffset;
  199. for (int y = 0; y < yCount; y++)
  200. {
  201. srcCalculator.SetY(src.RegionY + y);
  202. dstCalculator.SetY(dst.RegionY + y);
  203. for (int x = 0; x < xCount; x++)
  204. {
  205. int srcOffset = srcCalculator.GetOffset(src.RegionX + x);
  206. int dstOffset = dstCalculator.GetOffset(dst.RegionX + x);
  207. *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
  208. }
  209. }
  210. }
  211. return true;
  212. }
  213. bool _ = srcBpp switch
  214. {
  215. 1 => Convert<byte>(dstSpan, srcSpan),
  216. 2 => Convert<ushort>(dstSpan, srcSpan),
  217. 4 => Convert<uint>(dstSpan, srcSpan),
  218. 8 => Convert<ulong>(dstSpan, srcSpan),
  219. 12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
  220. 16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
  221. _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
  222. };
  223. memoryManager.Physical.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
  224. }
  225. else
  226. {
  227. if (remap &&
  228. _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA &&
  229. _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA &&
  230. _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA &&
  231. _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA &&
  232. _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One &&
  233. _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One &&
  234. _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four)
  235. {
  236. // Fast path for clears when remap is enabled.
  237. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA);
  238. }
  239. else
  240. {
  241. // TODO: Implement remap functionality.
  242. // Buffer to buffer copy.
  243. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size);
  244. }
  245. }
  246. }
  247. }
  248. }