DmaClass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 tex.RegionX == 0 &&
  82. tex.RegionY == 0 &&
  83. stride / bpp == BitUtils.AlignUp(xCount, alignWidth);
  84. }
  85. else
  86. {
  87. int alignWidth = Constants.GobAlignment / bpp;
  88. return tex.RegionX == 0 &&
  89. tex.RegionY == 0 &&
  90. tex.Width == BitUtils.AlignUp(xCount, alignWidth) &&
  91. tex.Height == yCount;
  92. }
  93. }
  94. /// <summary>
  95. /// Releases a semaphore for a given LaunchDma method call.
  96. /// </summary>
  97. /// <param name="argument">The LaunchDma call argument</param>
  98. private void ReleaseSemaphore(int argument)
  99. {
  100. LaunchDmaSemaphoreType type = (LaunchDmaSemaphoreType)((argument >> 3) & 0x3);
  101. if (type != LaunchDmaSemaphoreType.None)
  102. {
  103. ulong address = ((ulong)_state.State.SetSemaphoreA << 32) | _state.State.SetSemaphoreB;
  104. if (type == LaunchDmaSemaphoreType.ReleaseOneWordSemaphore)
  105. {
  106. _channel.MemoryManager.Write(address, _state.State.SetSemaphorePayload);
  107. }
  108. else /* if (type == LaunchDmaSemaphoreType.ReleaseFourWordSemaphore) */
  109. {
  110. _channel.MemoryManager.Write(address + 8, _context.GetTimestamp());
  111. _channel.MemoryManager.Write(address, (ulong)_state.State.SetSemaphorePayload);
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Performs a buffer to buffer, or buffer to texture copy.
  117. /// </summary>
  118. /// <param name="argument">The LaunchDma call argument</param>
  119. private void DmaCopy(int argument)
  120. {
  121. var memoryManager = _channel.MemoryManager;
  122. CopyFlags copyFlags = (CopyFlags)argument;
  123. bool srcLinear = copyFlags.HasFlag(CopyFlags.SrcLinear);
  124. bool dstLinear = copyFlags.HasFlag(CopyFlags.DstLinear);
  125. bool copy2D = copyFlags.HasFlag(CopyFlags.MultiLineEnable);
  126. bool remap = copyFlags.HasFlag(CopyFlags.RemapEnable);
  127. uint size = _state.State.LineLengthIn;
  128. if (size == 0)
  129. {
  130. return;
  131. }
  132. ulong srcGpuVa = ((ulong)_state.State.OffsetInUpperUpper << 32) | _state.State.OffsetInLower;
  133. ulong dstGpuVa = ((ulong)_state.State.OffsetOutUpperUpper << 32) | _state.State.OffsetOutLower;
  134. int xCount = (int)_state.State.LineLengthIn;
  135. int yCount = (int)_state.State.LineCount;
  136. _3dEngine.FlushUboDirty();
  137. if (copy2D)
  138. {
  139. // Buffer to texture copy.
  140. int componentSize = (int)_state.State.SetRemapComponentsComponentSize + 1;
  141. int srcBpp = remap ? ((int)_state.State.SetRemapComponentsNumSrcComponents + 1) * componentSize : 1;
  142. int dstBpp = remap ? ((int)_state.State.SetRemapComponentsNumDstComponents + 1) * componentSize : 1;
  143. var dst = Unsafe.As<uint, DmaTexture>(ref _state.State.SetDstBlockSize);
  144. var src = Unsafe.As<uint, DmaTexture>(ref _state.State.SetSrcBlockSize);
  145. int srcStride = (int)_state.State.PitchIn;
  146. int dstStride = (int)_state.State.PitchOut;
  147. var srcCalculator = new OffsetCalculator(
  148. src.Width,
  149. src.Height,
  150. srcStride,
  151. srcLinear,
  152. src.MemoryLayout.UnpackGobBlocksInY(),
  153. src.MemoryLayout.UnpackGobBlocksInZ(),
  154. srcBpp);
  155. var dstCalculator = new OffsetCalculator(
  156. dst.Width,
  157. dst.Height,
  158. dstStride,
  159. dstLinear,
  160. dst.MemoryLayout.UnpackGobBlocksInY(),
  161. dst.MemoryLayout.UnpackGobBlocksInZ(),
  162. dstBpp);
  163. (int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(src.RegionX, src.RegionY, xCount, yCount);
  164. (int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dst.RegionX, dst.RegionY, xCount, yCount);
  165. if (srcLinear && srcStride < 0)
  166. {
  167. srcBaseOffset += srcStride * (yCount - 1);
  168. }
  169. if (dstLinear && dstStride < 0)
  170. {
  171. dstBaseOffset += dstStride * (yCount - 1);
  172. }
  173. ReadOnlySpan<byte> srcSpan = memoryManager.GetSpan(srcGpuVa + (ulong)srcBaseOffset, srcSize, true);
  174. Span<byte> dstSpan = memoryManager.GetSpan(dstGpuVa + (ulong)dstBaseOffset, dstSize).ToArray();
  175. bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount);
  176. bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount);
  177. if (completeSource && completeDest)
  178. {
  179. var target = memoryManager.Physical.TextureCache.FindTexture(
  180. memoryManager,
  181. dst,
  182. dstGpuVa,
  183. dstBpp,
  184. dstStride,
  185. xCount,
  186. yCount,
  187. dstLinear);
  188. if (target != null)
  189. {
  190. ReadOnlySpan<byte> data;
  191. if (srcLinear)
  192. {
  193. data = LayoutConverter.ConvertLinearStridedToLinear(
  194. target.Info.Width,
  195. target.Info.Height,
  196. 1,
  197. 1,
  198. xCount * srcBpp,
  199. srcStride,
  200. target.Info.FormatInfo.BytesPerPixel,
  201. srcSpan);
  202. }
  203. else
  204. {
  205. data = LayoutConverter.ConvertBlockLinearToLinear(
  206. src.Width,
  207. src.Height,
  208. src.Depth,
  209. 1,
  210. 1,
  211. 1,
  212. 1,
  213. 1,
  214. srcBpp,
  215. src.MemoryLayout.UnpackGobBlocksInY(),
  216. src.MemoryLayout.UnpackGobBlocksInZ(),
  217. 1,
  218. new SizeInfo((int)target.Size),
  219. srcSpan);
  220. }
  221. target.SynchronizeMemory();
  222. target.SetData(data);
  223. target.SignalModified();
  224. return;
  225. }
  226. else if (srcCalculator.LayoutMatches(dstCalculator))
  227. {
  228. srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
  229. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, dstSpan);
  230. return;
  231. }
  232. }
  233. unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
  234. {
  235. fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
  236. {
  237. byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
  238. byte* srcBase = srcPtr - srcBaseOffset;
  239. for (int y = 0; y < yCount; y++)
  240. {
  241. srcCalculator.SetY(src.RegionY + y);
  242. dstCalculator.SetY(dst.RegionY + y);
  243. for (int x = 0; x < xCount; x++)
  244. {
  245. int srcOffset = srcCalculator.GetOffset(src.RegionX + x);
  246. int dstOffset = dstCalculator.GetOffset(dst.RegionX + x);
  247. *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
  248. }
  249. }
  250. }
  251. return true;
  252. }
  253. bool _ = srcBpp switch
  254. {
  255. 1 => Convert<byte>(dstSpan, srcSpan),
  256. 2 => Convert<ushort>(dstSpan, srcSpan),
  257. 4 => Convert<uint>(dstSpan, srcSpan),
  258. 8 => Convert<ulong>(dstSpan, srcSpan),
  259. 12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
  260. 16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
  261. _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
  262. };
  263. memoryManager.Write(dstGpuVa + (ulong)dstBaseOffset, dstSpan);
  264. }
  265. else
  266. {
  267. if (remap &&
  268. _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA &&
  269. _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA &&
  270. _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA &&
  271. _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA &&
  272. _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One &&
  273. _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One &&
  274. _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four)
  275. {
  276. // Fast path for clears when remap is enabled.
  277. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA);
  278. }
  279. else
  280. {
  281. // TODO: Implement remap functionality.
  282. // Buffer to buffer copy.
  283. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size);
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Performs a buffer to buffer, or buffer to texture copy, then optionally releases a semaphore.
  289. /// </summary>
  290. /// <param name="argument">Method call argument</param>
  291. private void LaunchDma(int argument)
  292. {
  293. DmaCopy(argument);
  294. ReleaseSemaphore(argument);
  295. }
  296. }
  297. }