TwodClass.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Ryujinx.Graphics.Device;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.Types;
  4. using Ryujinx.Graphics.Gpu.Image;
  5. using Ryujinx.Graphics.Texture;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.CompilerServices;
  9. namespace Ryujinx.Graphics.Gpu.Engine.Twod
  10. {
  11. /// <summary>
  12. /// Represents a 2D engine class.
  13. /// </summary>
  14. class TwodClass : IDeviceState
  15. {
  16. private readonly GpuChannel _channel;
  17. private readonly DeviceState<TwodClassState> _state;
  18. /// <summary>
  19. /// Creates a new instance of the 2D engine class.
  20. /// </summary>
  21. /// <param name="channel">The channel that will make use of the engine</param>
  22. public TwodClass(GpuChannel channel)
  23. {
  24. _channel = channel;
  25. _state = new DeviceState<TwodClassState>(new Dictionary<string, RwCallback>
  26. {
  27. { nameof(TwodClassState.PixelsFromMemorySrcY0Int), new RwCallback(PixelsFromMemorySrcY0Int, null) }
  28. });
  29. }
  30. /// <summary>
  31. /// Reads data from the class registers.
  32. /// </summary>
  33. /// <param name="offset">Register byte offset</param>
  34. /// <returns>Data at the specified offset</returns>
  35. public int Read(int offset) => _state.Read(offset);
  36. /// <summary>
  37. /// Writes data to the class registers.
  38. /// </summary>
  39. /// <param name="offset">Register byte offset</param>
  40. /// <param name="data">Data to be written</param>
  41. public void Write(int offset, int data) => _state.Write(offset, data);
  42. /// <summary>
  43. /// Performs the blit operation, triggered by the register write.
  44. /// </summary>
  45. /// <param name="argument">Method call argument</param>
  46. private void PixelsFromMemorySrcY0Int(int argument)
  47. {
  48. var memoryManager = _channel.MemoryManager;
  49. var dstCopyTexture = Unsafe.As<uint, TwodTexture>(ref _state.State.SetDstFormat);
  50. var srcCopyTexture = Unsafe.As<uint, TwodTexture>(ref _state.State.SetSrcFormat);
  51. long srcX = ((long)_state.State.SetPixelsFromMemorySrcX0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcX0Frac;
  52. long srcY = ((long)_state.State.PixelsFromMemorySrcY0Int << 32) | (long)(ulong)_state.State.SetPixelsFromMemorySrcY0Frac;
  53. long duDx = ((long)_state.State.SetPixelsFromMemoryDuDxInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDuDxFrac;
  54. long dvDy = ((long)_state.State.SetPixelsFromMemoryDvDyInt << 32) | (long)(ulong)_state.State.SetPixelsFromMemoryDvDyFrac;
  55. bool originCorner = _state.State.SetPixelsFromMemorySampleModeOrigin == SetPixelsFromMemorySampleModeOrigin.Corner;
  56. if (originCorner)
  57. {
  58. // If the origin is corner, it is assumed that the guest API
  59. // is manually centering the origin by adding a offset to the
  60. // source region X/Y coordinates.
  61. // Here we attempt to remove such offset to ensure we have the correct region.
  62. // The offset is calculated as FactorXY / 2.0, where FactorXY = SrcXY / DstXY,
  63. // so we do the same here by dividing the fixed point value by 2, while
  64. // throwing away the fractional part to avoid rounding errors.
  65. srcX -= (duDx >> 33) << 32;
  66. srcY -= (dvDy >> 33) << 32;
  67. }
  68. int srcX1 = (int)(srcX >> 32);
  69. int srcY1 = (int)(srcY >> 32);
  70. int srcX2 = srcX1 + (int)((duDx * _state.State.SetPixelsFromMemoryDstWidth + uint.MaxValue) >> 32);
  71. int srcY2 = srcY1 + (int)((dvDy * _state.State.SetPixelsFromMemoryDstHeight + uint.MaxValue) >> 32);
  72. int dstX1 = (int)_state.State.SetPixelsFromMemoryDstX0;
  73. int dstY1 = (int)_state.State.SetPixelsFromMemoryDstY0;
  74. int dstX2 = dstX1 + (int)_state.State.SetPixelsFromMemoryDstWidth;
  75. int dstY2 = dstY1 + (int)_state.State.SetPixelsFromMemoryDstHeight;
  76. // The source and destination textures should at least be as big as the region being requested.
  77. // The hints will only resize within alignment constraints, so out of bound copies won't resize in most cases.
  78. var srcHint = new Size(srcX2, srcY2, 1);
  79. var dstHint = new Size(dstX2, dstY2, 1);
  80. var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
  81. int srcWidthAligned = srcCopyTexture.Stride / srcCopyTextureFormat.BytesPerPixel;
  82. ulong offset = 0;
  83. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  84. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  85. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  86. // of the next line on the source texture.
  87. // This can be done by simply adding an offset to the texture address, so that the initial
  88. // gap is skipped and the copy is inside bounds again.
  89. // This is required by the proprietary guest OpenGL driver.
  90. if (srcCopyTexture.LinearLayout && srcCopyTexture.Width == srcX2 && srcX2 > srcWidthAligned && srcX1 > 0)
  91. {
  92. offset = (ulong)(srcX1 * srcCopyTextureFormat.BytesPerPixel);
  93. srcCopyTexture.Width -= srcX1;
  94. srcX2 -= srcX1;
  95. srcX1 = 0;
  96. }
  97. var srcTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture(
  98. memoryManager,
  99. srcCopyTexture,
  100. offset,
  101. srcCopyTextureFormat,
  102. false,
  103. srcHint);
  104. if (srcTexture == null)
  105. {
  106. return;
  107. }
  108. memoryManager.Physical.TextureCache.Lift(srcTexture);
  109. // When the source texture that was found has a depth format,
  110. // we must enforce the target texture also has a depth format,
  111. // as copies between depth and color formats are not allowed.
  112. FormatInfo dstCopyTextureFormat;
  113. if (srcTexture.Format.IsDepthOrStencil())
  114. {
  115. dstCopyTextureFormat = srcTexture.Info.FormatInfo;
  116. }
  117. else
  118. {
  119. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  120. }
  121. var dstTexture = memoryManager.Physical.TextureCache.FindOrCreateTexture(
  122. memoryManager,
  123. dstCopyTexture,
  124. 0,
  125. dstCopyTextureFormat,
  126. srcTexture.ScaleMode == TextureScaleMode.Scaled,
  127. dstHint);
  128. if (dstTexture == null)
  129. {
  130. return;
  131. }
  132. float scale = srcTexture.ScaleFactor;
  133. float dstScale = dstTexture.ScaleFactor;
  134. Extents2D srcRegion = new Extents2D(
  135. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  136. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  137. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  138. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  139. Extents2D dstRegion = new Extents2D(
  140. (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)),
  141. (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)),
  142. (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)),
  143. (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY)));
  144. bool linearFilter = _state.State.SetPixelsFromMemorySampleModeFilter == SetPixelsFromMemorySampleModeFilter.Bilinear;
  145. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  146. dstTexture.SignalModified();
  147. }
  148. }
  149. }