DmaClass.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ulong srcBaseAddress = memoryManager.Translate(srcGpuVa);
  136. ulong dstBaseAddress = memoryManager.Translate(dstGpuVa);
  137. (int srcBaseOffset, int srcSize) = srcCalculator.GetRectangleRange(src.RegionX, src.RegionY, xCount, yCount);
  138. (int dstBaseOffset, int dstSize) = dstCalculator.GetRectangleRange(dst.RegionX, dst.RegionY, xCount, yCount);
  139. ReadOnlySpan<byte> srcSpan = memoryManager.Physical.GetSpan(srcBaseAddress + (ulong)srcBaseOffset, srcSize, true);
  140. Span<byte> dstSpan = memoryManager.Physical.GetSpan(dstBaseAddress + (ulong)dstBaseOffset, dstSize).ToArray();
  141. bool completeSource = IsTextureCopyComplete(src, srcLinear, srcBpp, srcStride, xCount, yCount);
  142. bool completeDest = IsTextureCopyComplete(dst, dstLinear, dstBpp, dstStride, xCount, yCount);
  143. if (completeSource && completeDest)
  144. {
  145. var target = memoryManager.Physical.TextureCache.FindTexture(
  146. memoryManager,
  147. dst,
  148. dstGpuVa,
  149. dstBpp,
  150. dstStride,
  151. xCount,
  152. yCount,
  153. dstLinear);
  154. if (target != null)
  155. {
  156. ReadOnlySpan<byte> data;
  157. if (srcLinear)
  158. {
  159. data = LayoutConverter.ConvertLinearStridedToLinear(
  160. target.Info.Width,
  161. target.Info.Height,
  162. 1,
  163. 1,
  164. srcStride,
  165. target.Info.FormatInfo.BytesPerPixel,
  166. srcSpan);
  167. }
  168. else
  169. {
  170. data = LayoutConverter.ConvertBlockLinearToLinear(
  171. src.Width,
  172. src.Height,
  173. 1,
  174. target.Info.Levels,
  175. 1,
  176. 1,
  177. 1,
  178. srcBpp,
  179. src.MemoryLayout.UnpackGobBlocksInY(),
  180. src.MemoryLayout.UnpackGobBlocksInZ(),
  181. 1,
  182. new SizeInfo((int)target.Size),
  183. srcSpan);
  184. }
  185. target.SetData(data);
  186. target.SignalModified();
  187. return;
  188. }
  189. else if (srcCalculator.LayoutMatches(dstCalculator))
  190. {
  191. srcSpan.CopyTo(dstSpan); // No layout conversion has to be performed, just copy the data entirely.
  192. memoryManager.Physical.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
  193. return;
  194. }
  195. }
  196. unsafe bool Convert<T>(Span<byte> dstSpan, ReadOnlySpan<byte> srcSpan) where T : unmanaged
  197. {
  198. fixed (byte* dstPtr = dstSpan, srcPtr = srcSpan)
  199. {
  200. byte* dstBase = dstPtr - dstBaseOffset; // Layout offset is relative to the base, so we need to subtract the span's offset.
  201. byte* srcBase = srcPtr - srcBaseOffset;
  202. for (int y = 0; y < yCount; y++)
  203. {
  204. srcCalculator.SetY(src.RegionY + y);
  205. dstCalculator.SetY(dst.RegionY + y);
  206. for (int x = 0; x < xCount; x++)
  207. {
  208. int srcOffset = srcCalculator.GetOffset(src.RegionX + x);
  209. int dstOffset = dstCalculator.GetOffset(dst.RegionX + x);
  210. *(T*)(dstBase + dstOffset) = *(T*)(srcBase + srcOffset);
  211. }
  212. }
  213. }
  214. return true;
  215. }
  216. bool _ = srcBpp switch
  217. {
  218. 1 => Convert<byte>(dstSpan, srcSpan),
  219. 2 => Convert<ushort>(dstSpan, srcSpan),
  220. 4 => Convert<uint>(dstSpan, srcSpan),
  221. 8 => Convert<ulong>(dstSpan, srcSpan),
  222. 12 => Convert<Bpp12Pixel>(dstSpan, srcSpan),
  223. 16 => Convert<Vector128<byte>>(dstSpan, srcSpan),
  224. _ => throw new NotSupportedException($"Unable to copy ${srcBpp} bpp pixel format.")
  225. };
  226. memoryManager.Physical.Write(dstBaseAddress + (ulong)dstBaseOffset, dstSpan);
  227. }
  228. else
  229. {
  230. if (remap &&
  231. _state.State.SetRemapComponentsDstX == SetRemapComponentsDst.ConstA &&
  232. _state.State.SetRemapComponentsDstY == SetRemapComponentsDst.ConstA &&
  233. _state.State.SetRemapComponentsDstZ == SetRemapComponentsDst.ConstA &&
  234. _state.State.SetRemapComponentsDstW == SetRemapComponentsDst.ConstA &&
  235. _state.State.SetRemapComponentsNumSrcComponents == SetRemapComponentsNumComponents.One &&
  236. _state.State.SetRemapComponentsNumDstComponents == SetRemapComponentsNumComponents.One &&
  237. _state.State.SetRemapComponentsComponentSize == SetRemapComponentsComponentSize.Four)
  238. {
  239. // Fast path for clears when remap is enabled.
  240. memoryManager.Physical.BufferCache.ClearBuffer(memoryManager, dstGpuVa, size * 4, _state.State.SetRemapConstA);
  241. }
  242. else
  243. {
  244. // TODO: Implement remap functionality.
  245. // Buffer to buffer copy.
  246. memoryManager.Physical.BufferCache.CopyBuffer(memoryManager, srcGpuVa, dstGpuVa, size);
  247. }
  248. }
  249. }
  250. }
  251. }