DmaClass.cs 12 KB

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