MethodCopyTexture.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, true, srcHint);
  36. if (srcTexture == null)
  37. {
  38. return;
  39. }
  40. // When the source texture that was found has a depth format,
  41. // we must enforce the target texture also has a depth format,
  42. // as copies between depth and color formats are not allowed.
  43. FormatInfo dstCopyTextureFormat;
  44. if (srcTexture.Format.IsDepthOrStencil())
  45. {
  46. dstCopyTextureFormat = srcTexture.Info.FormatInfo;
  47. }
  48. else
  49. {
  50. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  51. }
  52. Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, dstHint);
  53. if (dstTexture == null)
  54. {
  55. return;
  56. }
  57. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  58. {
  59. srcTexture.PropagateScale(dstTexture);
  60. }
  61. float scale = srcTexture.ScaleFactor; // src and dest scales are identical now.
  62. Extents2D srcRegion = new Extents2D(
  63. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  64. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  65. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  66. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  67. Extents2D dstRegion = new Extents2D(
  68. (int)Math.Ceiling(scale * (dstX1 / dstTexture.Info.SamplesInX)),
  69. (int)Math.Ceiling(scale * (dstY1 / dstTexture.Info.SamplesInY)),
  70. (int)Math.Ceiling(scale * (dstX2 / dstTexture.Info.SamplesInX)),
  71. (int)Math.Ceiling(scale * (dstY2 / dstTexture.Info.SamplesInY)));
  72. bool linearFilter = control.UnpackLinearFilter();
  73. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  74. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  75. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  76. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  77. // of the next line on the source texture.
  78. // This can be emulated with 2 copies (the first copy handles the region inside the bounds,
  79. // the second handles the region outside of the bounds).
  80. // We must also extend the source texture by one line to ensure we can wrap on the last line.
  81. // This is required by the (guest) OpenGL driver.
  82. if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
  83. {
  84. srcCopyTexture.Height++;
  85. srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, srcHint);
  86. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  87. {
  88. srcTexture.PropagateScale(dstTexture);
  89. }
  90. srcRegion = new Extents2D(
  91. (int)Math.Ceiling(scale * ((srcX1 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  92. (int)Math.Ceiling(scale * ((srcY1 / srcTexture.Info.SamplesInY) + 1)),
  93. (int)Math.Ceiling(scale * ((srcX2 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  94. (int)Math.Ceiling(scale * ((srcY2 / srcTexture.Info.SamplesInY) + 1)));
  95. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  96. }
  97. dstTexture.SignalModified();
  98. }
  99. }
  100. }