TwodClass.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.Device;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Gpu.Engine.Types;
  5. using Ryujinx.Graphics.Gpu.Image;
  6. using Ryujinx.Graphics.Texture;
  7. using Ryujinx.Memory;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.InteropServices;
  12. using System.Runtime.Intrinsics;
  13. namespace Ryujinx.Graphics.Gpu.Engine.Twod
  14. {
  15. /// <summary>
  16. /// Represents a 2D engine class.
  17. /// </summary>
  18. class TwodClass : IDeviceState
  19. {
  20. private readonly GpuChannel _channel;
  21. private readonly DeviceState<TwodClassState> _state;
  22. /// <summary>
  23. /// Creates a new instance of the 2D engine class.
  24. /// </summary>
  25. /// <param name="channel">The channel that will make use of the engine</param>
  26. public TwodClass(GpuChannel channel)
  27. {
  28. _channel = channel;
  29. _state = new DeviceState<TwodClassState>(new Dictionary<string, RwCallback>
  30. {
  31. { nameof(TwodClassState.PixelsFromMemorySrcY0Int), new RwCallback(PixelsFromMemorySrcY0Int, null) }
  32. });
  33. }
  34. /// <summary>
  35. /// Reads data from the class registers.
  36. /// </summary>
  37. /// <param name="offset">Register byte offset</param>
  38. /// <returns>Data at the specified offset</returns>
  39. public int Read(int offset) => _state.Read(offset);
  40. /// <summary>
  41. /// Writes data to the class registers.
  42. /// </summary>
  43. /// <param name="offset">Register byte offset</param>
  44. /// <param name="data">Data to be written</param>
  45. public void Write(int offset, int data) => _state.Write(offset, data);
  46. /// <summary>
  47. /// Determines if data is compatible between the source and destination texture.
  48. /// The two textures must have the same size, layout, and bytes per pixel.
  49. /// </summary>
  50. /// <param name="lhs">Info for the first texture</param>
  51. /// <param name="rhs">Info for the second texture</param>
  52. /// <param name="lhsFormat">Format of the first texture</param>
  53. /// <param name="rhsFormat">Format of the second texture</param>
  54. /// <returns>True if the data is compatible, false otherwise</returns>
  55. private bool IsDataCompatible(TwodTexture lhs, TwodTexture rhs, FormatInfo lhsFormat, FormatInfo rhsFormat)
  56. {
  57. if (lhsFormat.BytesPerPixel != rhsFormat.BytesPerPixel ||
  58. lhs.Height != rhs.Height ||
  59. lhs.Depth != rhs.Depth ||
  60. lhs.LinearLayout != rhs.LinearLayout ||
  61. lhs.MemoryLayout.Packed != rhs.MemoryLayout.Packed)
  62. {
  63. return false;
  64. }
  65. if (lhs.LinearLayout)
  66. {
  67. return lhs.Stride == rhs.Stride;
  68. }
  69. else
  70. {
  71. return lhs.Width == rhs.Width;
  72. }
  73. }
  74. /// <summary>
  75. /// Determine if the given region covers the full texture, also considering width alignment.
  76. /// </summary>
  77. /// <param name="texture">The texture to check</param>
  78. /// <param name="formatInfo"></param>
  79. /// <param name="x1">Region start x</param>
  80. /// <param name="y1">Region start y</param>
  81. /// <param name="x2">Region end x</param>
  82. /// <param name="y2">Region end y</param>
  83. /// <returns>True if the region covers the full texture, false otherwise</returns>
  84. private bool IsCopyRegionComplete(TwodTexture texture, FormatInfo formatInfo, int x1, int y1, int x2, int y2)
  85. {
  86. if (x1 != 0 || y1 != 0 || y2 != texture.Height)
  87. {
  88. return false;
  89. }
  90. int width;
  91. int widthAlignment;
  92. if (texture.LinearLayout)
  93. {
  94. widthAlignment = 1;
  95. width = texture.Stride / formatInfo.BytesPerPixel;
  96. }
  97. else
  98. {
  99. widthAlignment = Constants.GobAlignment / formatInfo.BytesPerPixel;
  100. width = texture.Width;
  101. }
  102. return width == BitUtils.AlignUp(x2, widthAlignment);
  103. }
  104. /// <summary>
  105. /// Performs a full data copy between two textures, reading and writing guest memory directly.
  106. /// The textures must have a matching layout, size, and bytes per pixel.
  107. /// </summary>
  108. /// <param name="src">The source texture</param>
  109. /// <param name="dst">The destination texture</param>
  110. /// <param name="w">Copy width</param>
  111. /// <param name="h">Copy height</param>
  112. /// <param name="bpp">Bytes per pixel</param>
  113. private void UnscaledFullCopy(TwodTexture src, TwodTexture dst, int w, int h, int bpp)
  114. {
  115. var srcCalculator = new OffsetCalculator(
  116. w,
  117. h,
  118. src.Stride,
  119. src.LinearLayout,
  120. src.MemoryLayout.UnpackGobBlocksInY(),
  121. src.MemoryLayout.UnpackGobBlocksInZ(),
  122. bpp);
  123. (int _, int srcSize) = srcCalculator.GetRectangleRange(0, 0, w, h);
  124. var memoryManager = _channel.MemoryManager;
  125. ulong srcGpuVa = src.Address.Pack();
  126. ulong dstGpuVa = dst.Address.Pack();
  127. ReadOnlySpan<byte> srcSpan = memoryManager.GetSpan(srcGpuVa, srcSize, true);
  128. int width;
  129. int height = src.Height;
  130. if (src.LinearLayout)
  131. {
  132. width = src.Stride / bpp;
  133. }
  134. else
  135. {
  136. width = src.Width;
  137. }
  138. // If the copy is not equal to the width and height of the texture, we will need to copy partially.
  139. // It's worth noting that it has already been established that the src and dst are the same size.
  140. if (w == width && h == height)
  141. {
  142. memoryManager.Write(dstGpuVa, srcSpan);
  143. }
  144. else
  145. {
  146. using WritableRegion dstRegion = memoryManager.GetWritableRegion(dstGpuVa, srcSize, true);
  147. Span<byte> dstSpan = dstRegion.Memory.Span;
  148. if (src.LinearLayout)
  149. {
  150. int stride = src.Stride;
  151. int offset = 0;
  152. int lineSize = width * bpp;
  153. for (int y = 0; y < height; y++)
  154. {
  155. srcSpan.Slice(offset, lineSize).CopyTo(dstSpan.Slice(offset));
  156. offset += stride;
  157. }
  158. }
  159. else
  160. {
  161. // Copy with the block linear layout in mind.
  162. // Recreate the offset calculate with bpp 1 for copy.
  163. int stride = w * bpp;
  164. srcCalculator = new OffsetCalculator(
  165. stride,
  166. h,
  167. 0,
  168. false,
  169. src.MemoryLayout.UnpackGobBlocksInY(),
  170. src.MemoryLayout.UnpackGobBlocksInZ(),
  171. 1);
  172. int strideTrunc = BitUtils.AlignDown(stride, 16);
  173. ReadOnlySpan<Vector128<byte>> srcVec = MemoryMarshal.Cast<byte, Vector128<byte>>(srcSpan);
  174. Span<Vector128<byte>> dstVec = MemoryMarshal.Cast<byte, Vector128<byte>>(dstSpan);
  175. for (int y = 0; y < h; y++)
  176. {
  177. int x = 0;
  178. srcCalculator.SetY(y);
  179. for (; x < strideTrunc; x += 16)
  180. {
  181. int offset = srcCalculator.GetOffset(x) >> 4;
  182. dstVec[offset] = srcVec[offset];
  183. }
  184. for (; x < stride; x++)
  185. {
  186. int offset = srcCalculator.GetOffset(x);
  187. dstSpan[offset] = srcSpan[offset];
  188. }
  189. }
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Performs the blit operation, triggered by the register write.
  195. /// </summary>
  196. /// <param name="argument">Method call argument</param>
  197. private void PixelsFromMemorySrcY0Int(int argument)
  198. {
  199. var memoryManager = _channel.MemoryManager;
  200. var dstCopyTexture = Unsafe.As<uint, TwodTexture>(ref _state.State.SetDstFormat);
  201. var srcCopyTexture = Unsafe.As<uint, TwodTexture>(ref _state.State.SetSrcFormat);
  202. long srcX = ((long)_state.State.SetPixelsFromMemorySrcX0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcX0Frac;
  203. long srcY = ((long)_state.State.PixelsFromMemorySrcY0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcY0Frac;
  204. long duDx = ((long)_state.State.SetPixelsFromMemoryDuDxInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDuDxFrac;
  205. long dvDy = ((long)_state.State.SetPixelsFromMemoryDvDyInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDvDyFrac;
  206. bool originCorner = _state.State.SetPixelsFromMemorySampleModeOrigin == SetPixelsFromMemorySampleModeOrigin.Corner;
  207. if (originCorner)
  208. {
  209. // If the origin is corner, it is assumed that the guest API
  210. // is manually centering the origin by adding a offset to the
  211. // source region X/Y coordinates.
  212. // Here we attempt to remove such offset to ensure we have the correct region.
  213. // The offset is calculated as FactorXY / 2.0, where FactorXY = SrcXY / DstXY,
  214. // so we do the same here by dividing the fixed point value by 2, while
  215. // throwing away the fractional part to avoid rounding errors.
  216. srcX -= (duDx >> 33) << 32;
  217. srcY -= (dvDy >> 33) << 32;
  218. }
  219. int srcX1 = (int)(srcX >> 32);
  220. int srcY1 = (int)(srcY >> 32);
  221. int srcX2 = srcX1 + (int)((duDx * _state.State.SetPixelsFromMemoryDstWidth + uint.MaxValue) >> 32);
  222. int srcY2 = srcY1 + (int)((dvDy * _state.State.SetPixelsFromMemoryDstHeight + uint.MaxValue) >> 32);
  223. int dstX1 = (int)_state.State.SetPixelsFromMemoryDstX0;
  224. int dstY1 = (int)_state.State.SetPixelsFromMemoryDstY0;
  225. int dstX2 = dstX1 + (int)_state.State.SetPixelsFromMemoryDstWidth;
  226. int dstY2 = dstY1 + (int)_state.State.SetPixelsFromMemoryDstHeight;
  227. // The source and destination textures should at least be as big as the region being requested.
  228. // The hints will only resize within alignment constraints, so out of bound copies won't resize in most cases.
  229. var srcHint = new Size(srcX2, srcY2, 1);
  230. var dstHint = new Size(dstX2, dstY2, 1);
  231. var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
  232. int srcWidthAligned = srcCopyTexture.Stride / srcCopyTextureFormat.BytesPerPixel;
  233. ulong offset = 0;
  234. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  235. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  236. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  237. // of the next line on the source texture.
  238. // This can be done by simply adding an offset to the texture address, so that the initial
  239. // gap is skipped and the copy is inside bounds again.
  240. // This is required by the proprietary guest OpenGL driver.
  241. if (srcCopyTexture.LinearLayout && srcCopyTexture.Width == srcX2 && srcX2 > srcWidthAligned && srcX1 > 0)
  242. {
  243. offset = (ulong)(srcX1 * srcCopyTextureFormat.BytesPerPixel);
  244. srcCopyTexture.Width -= srcX1;
  245. srcX2 -= srcX1;
  246. srcX1 = 0;
  247. }
  248. FormatInfo dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  249. bool canDirectCopy = GraphicsConfig.Fast2DCopy &&
  250. srcX2 == dstX2 && srcY2 == dstY2 &&
  251. IsDataCompatible(srcCopyTexture, dstCopyTexture, srcCopyTextureFormat, dstCopyTextureFormat) &&
  252. IsCopyRegionComplete(srcCopyTexture, srcCopyTextureFormat, srcX1, srcY1, srcX2, srcY2) &&
  253. IsCopyRegionComplete(dstCopyTexture, dstCopyTextureFormat, dstX1, dstY1, dstX2, dstY2);
  254. var srcTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture(
  255. memoryManager,
  256. srcCopyTexture,
  257. offset,
  258. srcCopyTextureFormat,
  259. !canDirectCopy,
  260. false,
  261. srcHint);
  262. if (srcTexture == null)
  263. {
  264. if (canDirectCopy)
  265. {
  266. // Directly copy the data on CPU.
  267. UnscaledFullCopy(srcCopyTexture, dstCopyTexture, srcX2, srcY2, srcCopyTextureFormat.BytesPerPixel);
  268. }
  269. return;
  270. }
  271. memoryManager.Physical.TextureCache.Lift(srcTexture);
  272. // When the source texture that was found has a depth format,
  273. // we must enforce the target texture also has a depth format,
  274. // as copies between depth and color formats are not allowed.
  275. if (srcTexture.Format.IsDepthOrStencil())
  276. {
  277. dstCopyTextureFormat = srcTexture.Info.FormatInfo;
  278. }
  279. else
  280. {
  281. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  282. }
  283. var dstTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture(
  284. memoryManager,
  285. dstCopyTexture,
  286. 0,
  287. dstCopyTextureFormat,
  288. true,
  289. srcTexture.ScaleMode == TextureScaleMode.Scaled,
  290. dstHint);
  291. if (dstTexture == null)
  292. {
  293. return;
  294. }
  295. float scale = srcTexture.ScaleFactor;
  296. float dstScale = dstTexture.ScaleFactor;
  297. Extents2D srcRegion = new Extents2D(
  298. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  299. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  300. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  301. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  302. Extents2D dstRegion = new Extents2D(
  303. (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)),
  304. (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)),
  305. (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)),
  306. (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY)));
  307. bool linearFilter = _state.State.SetPixelsFromMemorySampleModeFilter == SetPixelsFromMemorySampleModeFilter.Bilinear;
  308. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  309. dstTexture.SignalModified();
  310. }
  311. }
  312. }