DmaClass.cs 12 KB

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