MethodCopyTexture.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. int srcX1 = (int)(region.SrcXF >> 32);
  23. int srcY1 = (int)(region.SrcYF >> 32);
  24. int srcX2 = (int)((region.SrcXF + region.SrcWidthRF * region.DstWidth) >> 32);
  25. int srcY2 = (int)((region.SrcYF + region.SrcHeightRF * region.DstHeight) >> 32);
  26. int dstX1 = region.DstX;
  27. int dstY1 = region.DstY;
  28. int dstX2 = region.DstX + region.DstWidth;
  29. int dstY2 = region.DstY + region.DstHeight;
  30. // The source and destination textures should at least be as big as the region being requested.
  31. // The hints will only resize within alignment constraints, so out of bound copies won't resize in most cases.
  32. var srcHint = new Size(srcX2, srcY2, 1);
  33. var dstHint = new Size(dstX2, dstY2, 1);
  34. var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
  35. int srcWidthAligned = srcCopyTexture.Stride / srcCopyTextureFormat.BytesPerPixel;
  36. ulong offset = 0;
  37. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  38. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  39. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  40. // of the next line on the source texture.
  41. // This can be done by simply adding an offset to the texture address, so that the initial
  42. // gap is skipped and the copy is inside bounds again.
  43. // This is required by the proprietary guest OpenGL driver.
  44. if (srcCopyTexture.LinearLayout && srcCopyTexture.Width == srcX2 && srcX2 > srcWidthAligned && srcX1 > 0)
  45. {
  46. offset = (ulong)(srcX1 * srcCopyTextureFormat.BytesPerPixel);
  47. srcCopyTexture.Width -= srcX1;
  48. srcX2 -= srcX1;
  49. srcX1 = 0;
  50. }
  51. Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, offset, srcCopyTextureFormat, true, srcHint);
  52. if (srcTexture == null)
  53. {
  54. return;
  55. }
  56. // When the source texture that was found has a depth format,
  57. // we must enforce the target texture also has a depth format,
  58. // as copies between depth and color formats are not allowed.
  59. FormatInfo dstCopyTextureFormat;
  60. if (srcTexture.Format.IsDepthOrStencil())
  61. {
  62. dstCopyTextureFormat = srcTexture.Info.FormatInfo;
  63. }
  64. else
  65. {
  66. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  67. }
  68. Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, 0, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint);
  69. if (dstTexture == null)
  70. {
  71. return;
  72. }
  73. float scale = srcTexture.ScaleFactor;
  74. float dstScale = dstTexture.ScaleFactor;
  75. Extents2D srcRegion = new Extents2D(
  76. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  77. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  78. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  79. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  80. Extents2D dstRegion = new Extents2D(
  81. (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)),
  82. (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)),
  83. (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)),
  84. (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY)));
  85. bool linearFilter = control.UnpackLinearFilter();
  86. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  87. dstTexture.SignalModified();
  88. }
  89. }
  90. }