DmaClass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Device;
  4. using Ryujinx.Graphics.Gpu.Engine.Threed;
  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. Span<byte> dstSpan = memoryManager.GetSpan(dstGpuVa + (ulong)dstBaseOffset, dstSize).ToArray();
  184. bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount);
  185. bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount);
  186. if (completeSource && completeDest)
  187. {
  188. var target = memoryManager.Physical.TextureCache.FindTexture(
  189. memoryManager,
  190. dst,
  191. dstGpuVa,
  192. dstBpp,
  193. dstStride,
  194. xCount,
  195. yCount,
  196. dstLinear);
  197. if (target != null)
  198. {
  199. ReadOnlySpan<byte> data;
  200. if (srcLinear)
  201. {
  202. data = LayoutConverter.ConvertLinearStridedToLinear(
  203. target.Info.Width,
  204. target.Info.Height,
  205. 1,
  206. 1,
  207. xCount * srcBpp,
  208. srcStride,
  209. target.Info.FormatInfo.BytesPerPixel,
  210. srcSpan);
  211. }
  212. else
  213. {
  214. data = LayoutConverter.ConvertBlockLinearToLinear(
  215. src.Width,
  216. src.Height,
  217. src.Depth,
  218. 1,
  219. 1,
  220. 1,
  221. 1,
  222. 1,
  223. srcBpp,
  224. src.MemoryLayout.UnpackGobBlocksInY(),
  225. src.MemoryLayout.UnpackGobBlocksInZ(),
  226. 1,
  227. new SizeInfo((int)target.Size),
  228. srcSpan);
  229. }
  230. target.SynchronizeMemory();
  231. target.SetData(data);
  232. target.SignalModified();
  233. return;
  234. }
  235. else if (srcCalculator.LayoutMatches(dstCalculator))
  236. {
  237. srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
  238. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, dstSpan);
  239. return;
  240. }
  241. }
  242. unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
  243. {
  244. fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
  245. {
  246. byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
  247. byte* srcBase = srcPtr - srcBaseOffset;
  248. for (int y = 0; y < yCount; y++)
  249. {
  250. srcCalculator.SetY(srcRegionY + y);
  251. dstCalculator.SetY(dstRegionY + y);
  252. for (int x = 0; x < xCount; x++)
  253. {
  254. int srcOffset = srcCalculator.GetOffset(srcRegionX + x);
  255. int dstOffset = dstCalculator.GetOffset(dstRegionX + x);
  256. *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
  257. }
  258. }
  259. }
  260. return true;
  261. }
  262. bool _ = srcBpp switch
  263. {
  264. 1 => Convert<byte>(dstSpan, srcSpan),
  265. 2 => Convert<ushort>(dstSpan, srcSpan),
  266. 4 => Convert<uint>(dstSpan, srcSpan),
  267. 8 => Convert<ulong>(dstSpan, srcSpan),
  268. 12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
  269. 16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
  270. _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
  271. };
  272. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, dstSpan);
  273. }
  274. else
  275. {
  276. if (remap &&
  277. _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA &&
  278. _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA &&
  279. _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA &&
  280. _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA &&
  281. _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One &&
  282. _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One &&
  283. _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four)
  284. {
  285. // Fast path for clears when remap is enabled.
  286. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA);
  287. }
  288. else
  289. {
  290. // TODO: Implement remap functionality.
  291. // Buffer to buffer copy.
  292. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size);
  293. }
  294. }
  295. }
  296. /// <summary>
  297. /// Performs a buffer to buffer, or buffer to texture copy, then optionally releases a semaphore.
  298. /// </summary>
  299. /// <param name="argument">Method call argument</param>
  300. private void LaunchDma(int argument)
  301. {
  302. DmaCopy(argument);
  303. ReleaseSemaphore(argument);
  304. }
  305. }
  306. }