MethodCopyTexture.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Gpu.State;
  4. using Ryujinx.Graphics.Texture;
  5. using System;
  6. namespace Ryujinx.Graphics.Gpu.Engine
  7. {
  8. using Texture = Image.Texture;
  9. partial class Methods
  10. {
  11. /// <summary>
  12. /// Performs a texture to texture copy.
  13. /// </summary>
  14. /// <param name="state">Current GPU state</param>
  15. /// <param name="argument">Method call argument</param>
  16. private void CopyTexture(GpuState state, int argument)
  17. {
  18. var dstCopyTexture = state.Get<CopyTexture>(MethodOffset.CopyDstTexture);
  19. var srcCopyTexture = state.Get<CopyTexture>(MethodOffset.CopySrcTexture);
  20. var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
  21. var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
  22. bool originCorner = control.UnpackOriginCorner();
  23. long srcX = region.SrcXF;
  24. long srcY = region.SrcYF;
  25. if (originCorner)
  26. {
  27. // If the origin is corner, it is assumed that the guest API
  28. // is manually centering the origin by adding a offset to the
  29. // source region X/Y coordinates.
  30. // Here we attempt to remove such offset to ensure we have the correct region.
  31. // The offset is calculated as FactorXY / 2.0, where FactorXY = SrcXY / DstXY,
  32. // so we do the same here by dividing the fixed point value by 2, while
  33. // throwing away the fractional part to avoid rounding errors.
  34. srcX -= (region.SrcWidthRF >> 33) << 32;
  35. srcY -= (region.SrcHeightRF >> 33) << 32;
  36. }
  37. int srcX1 = (int)(srcX >> 32);
  38. int srcY1 = (int)(srcY >> 32);
  39. int srcX2 = srcX1 + (int)((region.SrcWidthRF * region.DstWidth + uint.MaxValue) >> 32);
  40. int srcY2 = srcY1 + (int)((region.SrcHeightRF * region.DstHeight + uint.MaxValue) >> 32);
  41. int dstX1 = region.DstX;
  42. int dstY1 = region.DstY;
  43. int dstX2 = region.DstX + region.DstWidth;
  44. int dstY2 = region.DstY + region.DstHeight;
  45. // The source and destination textures should at least be as big as the region being requested.
  46. // The hints will only resize within alignment constraints, so out of bound copies won't resize in most cases.
  47. var srcHint = new Size(srcX2, srcY2, 1);
  48. var dstHint = new Size(dstX2, dstY2, 1);
  49. var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
  50. int srcWidthAligned = srcCopyTexture.Stride / srcCopyTextureFormat.BytesPerPixel;
  51. ulong offset = 0;
  52. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  53. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  54. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  55. // of the next line on the source texture.
  56. // This can be done by simply adding an offset to the texture address, so that the initial
  57. // gap is skipped and the copy is inside bounds again.
  58. // This is required by the proprietary guest OpenGL driver.
  59. if (srcCopyTexture.LinearLayout && srcCopyTexture.Width == srcX2 && srcX2 > srcWidthAligned && srcX1 > 0)
  60. {
  61. offset = (ulong)(srcX1 * srcCopyTextureFormat.BytesPerPixel);
  62. srcCopyTexture.Width -= srcX1;
  63. srcX2 -= srcX1;
  64. srcX1 = 0;
  65. }
  66. Texture srcTexture = TextureCache.FindOrCreateTexture(srcCopyTexture, offset, srcCopyTextureFormat, true, srcHint);
  67. if (srcTexture == null)
  68. {
  69. return;
  70. }
  71. // When the source texture that was found has a depth format,
  72. // we must enforce the target texture also has a depth format,
  73. // as copies between depth and color formats are not allowed.
  74. FormatInfo dstCopyTextureFormat;
  75. if (srcTexture.Format.IsDepthOrStencil())
  76. {
  77. dstCopyTextureFormat = srcTexture.Info.FormatInfo;
  78. }
  79. else
  80. {
  81. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  82. }
  83. Texture dstTexture = TextureCache.FindOrCreateTexture(dstCopyTexture, 0, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint);
  84. if (dstTexture == null)
  85. {
  86. return;
  87. }
  88. float scale = srcTexture.ScaleFactor;
  89. float dstScale = dstTexture.ScaleFactor;
  90. Extents2D srcRegion = new Extents2D(
  91. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  92. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  93. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  94. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  95. Extents2D dstRegion = new Extents2D(
  96. (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)),
  97. (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)),
  98. (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)),
  99. (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY)));
  100. bool linearFilter = control.UnpackLinearFilter();
  101. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  102. dstTexture.SignalModified();
  103. }
  104. }
  105. }