DmaClass.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Device;
  3. using Ryujinx.Graphics.Gpu.Engine.Threed;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Texture;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.CompilerServices;
  9. using System.Runtime.Intrinsics;
  10. namespace Ryujinx.Graphics.Gpu.Engine.Dma
  11. {
  12. /// <summary>
  13. /// Represents a DMA copy engine class.
  14. /// </summary>
  15. class DmaClass : IDeviceState
  16. {
  17. private readonly GpuContext _context;
  18. private readonly GpuChannel _channel;
  19. private readonly ThreedClass _3dEngine;
  20. private readonly DeviceState<DmaClassState> _state;
  21. /// <summary>
  22. /// Copy flags passed on DMA launch.
  23. /// </summary>
  24. [Flags]
  25. private enum CopyFlags
  26. {
  27. SrcLinear = 1 << 7,
  28. DstLinear = 1 << 8,
  29. MultiLineEnable = 1 << 9,
  30. RemapEnable = 1 << 10
  31. }
  32. /// <summary>
  33. /// Creates a new instance of the DMA copy engine class.
  34. /// </summary>
  35. /// <param name="context">GPU context</param>
  36. /// <param name="channel">GPU channel</param>
  37. /// <param name="threedEngine">3D engine</param>
  38. public DmaClass(GpuContext context, GpuChannel channel, ThreedClass threedEngine)
  39. {
  40. _context = context;
  41. _channel = channel;
  42. _3dEngine = threedEngine;
  43. _state = new DeviceState<DmaClassState>(new Dictionary<string, RwCallback>
  44. {
  45. { nameof(DmaClassState.LaunchDma), new RwCallback(LaunchDma, null) }
  46. });
  47. }
  48. /// <summary>
  49. /// Reads data from the class registers.
  50. /// </summary>
  51. /// <param name="offset">Register byte offset</param>
  52. /// <returns>Data at the specified offset</returns>
  53. public int Read(int offset) => _state.Read(offset);
  54. /// <summary>
  55. /// Writes data to the class registers.
  56. /// </summary>
  57. /// <param name="offset">Register byte offset</param>
  58. /// <param name="data">Data to be written</param>
  59. public void Write(int offset, int data) => _state.Write(offset, data);
  60. /// <summary>
  61. /// Determine if a buffer-to-texture region covers the entirety of a texture.
  62. /// </summary>
  63. /// <param name="tex">Texture to compare</param>
  64. /// <param name="linear">True if the texture is linear, false if block linear</param>
  65. /// <param name="bpp">Texture bytes per pixel</param>
  66. /// <param name="stride">Texture stride</param>
  67. /// <param name="xCount">Number of pixels to be copied</param>
  68. /// <param name="yCount">Number of lines to be copied</param>
  69. /// <returns></returns>
  70. private static bool IsTextureCopyComplete(DmaTexture tex, bool linear, int bpp, int stride, int xCount, int yCount)
  71. {
  72. if (linear)
  73. {
  74. // If the stride is negative, the texture has to be flipped, so
  75. // the fast copy is not trivial, use the slow path.
  76. if (stride <= 0)
  77. {
  78. return false;
  79. }
  80. int alignWidth = Constants.StrideAlignment / bpp;
  81. return stride / bpp == BitUtils.AlignUp(xCount, alignWidth);
  82. }
  83. else
  84. {
  85. int alignWidth = Constants.GobAlignment / bpp;
  86. return tex.RegionX == 0 &&
  87. tex.RegionY == 0 &&
  88. tex.Width == BitUtils.AlignUp(xCount, alignWidth) &&
  89. tex.Height == yCount;
  90. }
  91. }
  92. /// <summary>
  93. /// Releases a semaphore for a given LaunchDma method call.
  94. /// </summary>
  95. /// <param name="argument">The LaunchDma call argument</param>
  96. private void ReleaseSemaphore(int argument)
  97. {
  98. LaunchDmaSemaphoreType type = (LaunchDmaSemaphoreType)((argument >> 3) & 0x3);
  99. if (type != LaunchDmaSemaphoreType.None)
  100. {
  101. ulong address = ((ulong)_state.State.SetSemaphoreA << 32) | _state.State.SetSemaphoreB;
  102. if (type == LaunchDmaSemaphoreType.ReleaseOneWordSemaphore)
  103. {
  104. _channel.MemoryManager.Write(address, _state.State.SetSemaphorePayload);
  105. }
  106. else /* if (type == LaunchDmaSemaphoreType.ReleaseFourWordSemaphore) */
  107. {
  108. _channel.MemoryManager.Write(address + 8, _context.GetTimestamp());
  109. _channel.MemoryManager.Write(address, (ulong)_state.State.SetSemaphorePayload);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Performs a buffer to buffer, or buffer to texture copy.
  115. /// </summary>
  116. /// <param name="argument">The LaunchDma call argument</param>
  117. private void DmaCopy(int argument)
  118. {
  119. var memoryManager = _channel.MemoryManager;
  120. CopyFlags copyFlags = (CopyFlags)argument;
  121. bool srcLinear = copyFlags.HasFlag(CopyFlags.SrcLinear);
  122. bool dstLinear = copyFlags.HasFlag(CopyFlags.DstLinear);
  123. bool copy2D = copyFlags.HasFlag(CopyFlags.MultiLineEnable);
  124. bool remap = copyFlags.HasFlag(CopyFlags.RemapEnable);
  125. uint size = _state.State.LineLengthIn;
  126. if (size == 0)
  127. {
  128. return;
  129. }
  130. ulong srcGpuVa = ((ulong)_state.State.OffsetInUpperUpper << 32) | _state.State.OffsetInLower;
  131. ulong dstGpuVa = ((ulong)_state.State.OffsetOutUpperUpper << 32) | _state.State.OffsetOutLower;
  132. int xCount = (int)_state.State.LineLengthIn;
  133. int yCount = (int)_state.State.LineCount;
  134. _3dEngine.FlushUboDirty();
  135. if (copy2D)
  136. {
  137. // Buffer to texture copy.
  138. int componentSize = (int)_state.State.SetRemapComponentsComponentSize + 1;
  139. int srcBpp = remap ? ((int)_state.State.SetRemapComponentsNumSrcComponents + 1) * componentSize : 1;
  140. int dstBpp = remap ? ((int)_state.State.SetRemapComponentsNumDstComponents + 1) * componentSize : 1;
  141. var dst = Unsafe.As<uint, DmaTexture>(ref _state.State.SetDstBlockSize);
  142. var src = Unsafe.As<uint, DmaTexture>(ref _state.State.SetSrcBlockSize);
  143. int srcRegionX = 0, srcRegionY = 0, dstRegionX = 0, dstRegionY = 0;
  144. if (!srcLinear)
  145. {
  146. srcRegionX = src.RegionX;
  147. srcRegionY = src.RegionY;
  148. }
  149. if (!dstLinear)
  150. {
  151. dstRegionX = dst.RegionX;
  152. dstRegionY = dst.RegionY;
  153. }
  154. int srcStride = (int)_state.State.PitchIn;
  155. int dstStride = (int)_state.State.PitchOut;
  156. var srcCalculator = new OffsetCalculator(
  157. src.Width,
  158. src.Height,
  159. srcStride,
  160. srcLinear,
  161. src.MemoryLayout.UnpackGobBlocksInY(),
  162. src.MemoryLayout.UnpackGobBlocksInZ(),
  163. srcBpp);
  164. var dstCalculator = new OffsetCalculator(
  165. dst.Width,
  166. dst.Height,
  167. dstStride,
  168. dstLinear,
  169. dst.MemoryLayout.UnpackGobBlocksInY(),
  170. dst.MemoryLayout.UnpackGobBlocksInZ(),
  171. dstBpp);
  172. (int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(srcRegionX, srcRegionY, xCount, yCount);
  173. (int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dstRegionX, dstRegionY, xCount, yCount);
  174. if (srcLinear && srcStride < 0)
  175. {
  176. srcBaseOffset += srcStride * (yCount - 1);
  177. }
  178. if (dstLinear && dstStride < 0)
  179. {
  180. dstBaseOffset += dstStride * (yCount - 1);
  181. }
  182. ReadOnlySpan<byte> srcSpan = memoryManager.GetSpan(srcGpuVa + (ulong)srcBaseOffset, srcSize, true);
  183. bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount);
  184. bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount);
  185. if (completeSource && completeDest)
  186. {
  187. var target = memoryManager.Physical.TextureCache.FindTexture(
  188. memoryManager,
  189. dst,
  190. dstGpuVa,
  191. dstBpp,
  192. dstStride,
  193. xCount,
  194. yCount,
  195. dstLinear);
  196. if (target != null)
  197. {
  198. ReadOnlySpan<byte> data;
  199. if (srcLinear)
  200. {
  201. data = LayoutConverter.ConvertLinearStridedToLinear(
  202. target.Info.Width,
  203. target.Info.Height,
  204. 1,
  205. 1,
  206. xCount * srcBpp,
  207. srcStride,
  208. target.Info.FormatInfo.BytesPerPixel,
  209. srcSpan);
  210. }
  211. else
  212. {
  213. data = LayoutConverter.ConvertBlockLinearToLinear(
  214. src.Width,
  215. src.Height,
  216. src.Depth,
  217. 1,
  218. 1,
  219. 1,
  220. 1,
  221. 1,
  222. srcBpp,
  223. src.MemoryLayout.UnpackGobBlocksInY(),
  224. src.MemoryLayout.UnpackGobBlocksInZ(),
  225. 1,
  226. new SizeInfo((int)target.Size),
  227. srcSpan);
  228. }
  229. target.SynchronizeMemory();
  230. target.SetData(data);
  231. target.SignalModified();
  232. return;
  233. }
  234. else if (srcCalculator.LayoutMatches(dstCalculator))
  235. {
  236. // No layout conversion has to be performed, just copy the data entirely.
  237. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, srcSpan);
  238. return;
  239. }
  240. }
  241. unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
  242. {
  243. if (srcLinear && dstLinear && srcBpp == dstBpp)
  244. {
  245. // Optimized path for purely linear copies - we don't need to calculate every single byte offset,
  246. // and we can make use of Span.CopyTo which is very very fast (even compared to pointers)
  247. for (int y = 0; y < yCount; y++)
  248. {
  249. srcCalculator.SetY(srcRegionY + y);
  250. dstCalculator.SetY(dstRegionY + y);
  251. int srcOffset = srcCalculator.GetOffset(srcRegionX);
  252. int dstOffset = dstCalculator.GetOffset(dstRegionX);
  253. srcSpan.Slice(srcOffset - srcBaseOffset, xCount * srcBpp)
  254. .CopyTo(dstSpan.Slice(dstOffset - dstBaseOffset, xCount * dstBpp));
  255. }
  256. }
  257. else
  258. {
  259. fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
  260. {
  261. byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
  262. byte* srcBase = srcPtr - srcBaseOffset;
  263. for (int y = 0; y < yCount; y++)
  264. {
  265. srcCalculator.SetY(srcRegionY + y);
  266. dstCalculator.SetY(dstRegionY + y);
  267. for (int x = 0; x < xCount; x++)
  268. {
  269. int srcOffset = srcCalculator.GetOffset(srcRegionX + x);
  270. int dstOffset = dstCalculator.GetOffset(dstRegionX + x);
  271. *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
  272. }
  273. }
  274. }
  275. }
  276. return true;
  277. }
  278. // OPT: This allocates a (potentially) huge temporary array and then copies an existing
  279. // region of memory into it, data that might get overwritten entirely anyways. Ideally this should
  280. // all be rewritten to use pooled arrays, but that gets complicated with packed data and strides
  281. Span<byte> dstSpan = memoryManager.GetSpan(dstGpuVa + (ulong)dstBaseOffset, dstSize).ToArray();
  282. bool _ = srcBpp switch
  283. {
  284. 1 => Convert<byte>(dstSpan, srcSpan),
  285. 2 => Convert<ushort>(dstSpan, srcSpan),
  286. 4 => Convert<uint>(dstSpan, srcSpan),
  287. 8 => Convert<ulong>(dstSpan, srcSpan),
  288. 12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
  289. 16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
  290. _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
  291. };
  292. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, dstSpan);
  293. }
  294. else
  295. {
  296. if (remap &&
  297. _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA &&
  298. _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA &&
  299. _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA &&
  300. _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA &&
  301. _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One &&
  302. _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One &&
  303. _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four)
  304. {
  305. // Fast path for clears when remap is enabled.
  306. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA);
  307. }
  308. else
  309. {
  310. // TODO: Implement remap functionality.
  311. // Buffer to buffer copy.
  312. bool srcIsPitchKind = memoryManager.GetKind(srcGpuVa).IsPitch();
  313. bool dstIsPitchKind = memoryManager.GetKind(dstGpuVa).IsPitch();
  314. if (!srcIsPitchKind && dstIsPitchKind)
  315. {
  316. CopyGobBlockLinearToLinear(memoryManager, srcGpuVa, dstGpuVa, size);
  317. }
  318. else if (srcIsPitchKind && !dstIsPitchKind)
  319. {
  320. CopyGobLinearToBlockLinear(memoryManager, srcGpuVa, dstGpuVa, size);
  321. }
  322. else
  323. {
  324. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size);
  325. }
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// Copies block linear data with block linear GOBs to a block linear destination with linear GOBs.
  331. /// </summary>
  332. /// <param name="memoryManager">GPU memory manager</param>
  333. /// <param name="srcGpuVa">Source GPU virtual address</param>
  334. /// <param name="dstGpuVa">Destination GPU virtual address</param>
  335. /// <param name="size">Size in bytes of the copy</param>
  336. private static void CopyGobBlockLinearToLinear(MemoryManager memoryManager, ulong srcGpuVa, ulong dstGpuVa, ulong size)
  337. {
  338. if (((srcGpuVa | dstGpuVa | size) & 0xf) == 0)
  339. {
  340. for (ulong offset = 0; offset < size; offset += 16)
  341. {
  342. Vector128<byte> data = memoryManager.Read<Vector128<byte>>(ConvertGobLinearToBlockLinearAddress(srcGpuVa + offset), true);
  343. memoryManager.Write(dstGpuVa + offset, data);
  344. }
  345. }
  346. else
  347. {
  348. for (ulong offset = 0; offset < size; offset++)
  349. {
  350. byte data = memoryManager.Read<byte>(ConvertGobLinearToBlockLinearAddress(srcGpuVa + offset), true);
  351. memoryManager.Write(dstGpuVa + offset, data);
  352. }
  353. }
  354. }
  355. /// <summary>
  356. /// Copies block linear data with linear GOBs to a block linear destination with block linear GOBs.
  357. /// </summary>
  358. /// <param name="memoryManager">GPU memory manager</param>
  359. /// <param name="srcGpuVa">Source GPU virtual address</param>
  360. /// <param name="dstGpuVa">Destination GPU virtual address</param>
  361. /// <param name="size">Size in bytes of the copy</param>
  362. private static void CopyGobLinearToBlockLinear(MemoryManager memoryManager, ulong srcGpuVa, ulong dstGpuVa, ulong size)
  363. {
  364. if (((srcGpuVa | dstGpuVa | size) & 0xf) == 0)
  365. {
  366. for (ulong offset = 0; offset < size; offset += 16)
  367. {
  368. Vector128<byte> data = memoryManager.Read<Vector128<byte>>(srcGpuVa + offset, true);
  369. memoryManager.Write(ConvertGobLinearToBlockLinearAddress(dstGpuVa + offset), data);
  370. }
  371. }
  372. else
  373. {
  374. for (ulong offset = 0; offset < size; offset++)
  375. {
  376. byte data = memoryManager.Read<byte>(srcGpuVa + offset, true);
  377. memoryManager.Write(ConvertGobLinearToBlockLinearAddress(dstGpuVa + offset), data);
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// Calculates the GOB block linear address from a linear address.
  383. /// </summary>
  384. /// <param name="address">Linear address</param>
  385. /// <returns>Block linear address</returns>
  386. private static ulong ConvertGobLinearToBlockLinearAddress(ulong address)
  387. {
  388. // y2 y1 y0 x5 x4 x3 x2 x1 x0 -> x5 y2 y1 x4 y0 x3 x2 x1 x0
  389. return (address & ~0x1f0UL) |
  390. ((address & 0x40) >> 2) |
  391. ((address & 0x10) << 1) |
  392. ((address & 0x180) >> 1) |
  393. ((address & 0x20) << 3);
  394. }
  395. /// <summary>
  396. /// Performs a buffer to buffer, or buffer to texture copy, then optionally releases a semaphore.
  397. /// </summary>
  398. /// <param name="argument">Method call argument</param>
  399. private void LaunchDma(int argument)
  400. {
  401. DmaCopy(argument);
  402. ReleaseSemaphore(argument);
  403. }
  404. }
  405. }