MethodCopyTexture.cs 6.0 KB

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