MethodCopyTexture.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Gpu.State;
  4. using System;
  5. namespace Ryujinx.Graphics.Gpu.Engine
  6. {
  7. using Texture = Image.Texture;
  8. partial class Methods
  9. {
  10. /// <summary>
  11. /// Performs a texture to texture copy.
  12. /// </summary>
  13. /// <param name="state">Current GPU state</param>
  14. /// <param name="argument">Method call argument</param>
  15. private void CopyTexture(GpuState state, int argument)
  16. {
  17. var dstCopyTexture = state.Get<CopyTexture>(MethodOffset.CopyDstTexture);
  18. var srcCopyTexture = state.Get<CopyTexture>(MethodOffset.CopySrcTexture);
  19. var srcCopyTextureFormat = srcCopyTexture.Format.Convert();
  20. Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat);
  21. if (srcTexture == null)
  22. {
  23. return;
  24. }
  25. // When the source texture that was found has a depth format,
  26. // we must enforce the target texture also has a depth format,
  27. // as copies between depth and color formats are not allowed.
  28. FormatInfo dstCopyTextureFormat;
  29. if (srcTexture.Format.IsDepthOrStencil())
  30. {
  31. dstCopyTextureFormat = srcCopyTextureFormat;
  32. }
  33. else
  34. {
  35. dstCopyTextureFormat = dstCopyTexture.Format.Convert();
  36. }
  37. Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, dstCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled);
  38. if (dstTexture == null)
  39. {
  40. return;
  41. }
  42. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  43. {
  44. srcTexture.PropagateScale(dstTexture);
  45. }
  46. var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
  47. var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
  48. int srcX1 = (int)(region.SrcXF >> 32);
  49. int srcY1 = (int)(region.SrcYF >> 32);
  50. int srcX2 = (int)((region.SrcXF + region.SrcWidthRF * region.DstWidth) >> 32);
  51. int srcY2 = (int)((region.SrcYF + region.SrcHeightRF * region.DstHeight) >> 32);
  52. int dstX1 = region.DstX;
  53. int dstY1 = region.DstY;
  54. int dstX2 = region.DstX + region.DstWidth;
  55. int dstY2 = region.DstY + region.DstHeight;
  56. float scale = srcTexture.ScaleFactor; // src and dest scales are identical now.
  57. Extents2D srcRegion = new Extents2D(
  58. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  59. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  60. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  61. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  62. Extents2D dstRegion = new Extents2D(
  63. (int)Math.Ceiling(scale * (dstX1 / dstTexture.Info.SamplesInX)),
  64. (int)Math.Ceiling(scale * (dstY1 / dstTexture.Info.SamplesInY)),
  65. (int)Math.Ceiling(scale * (dstX2 / dstTexture.Info.SamplesInX)),
  66. (int)Math.Ceiling(scale * (dstY2 / dstTexture.Info.SamplesInY)));
  67. bool linearFilter = control.UnpackLinearFilter();
  68. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  69. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  70. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  71. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  72. // of the next line on the source texture.
  73. // This can be emulated with 2 copies (the first copy handles the region inside the bounds,
  74. // the second handles the region outside of the bounds).
  75. // We must also extend the source texture by one line to ensure we can wrap on the last line.
  76. // This is required by the (guest) OpenGL driver.
  77. if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
  78. {
  79. srcCopyTexture.Height++;
  80. srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled);
  81. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  82. {
  83. srcTexture.PropagateScale(dstTexture);
  84. }
  85. srcRegion = new Extents2D(
  86. (int)Math.Ceiling(scale * ((srcX1 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  87. (int)Math.Ceiling(scale * ((srcY1 / srcTexture.Info.SamplesInY) + 1)),
  88. (int)Math.Ceiling(scale * ((srcX2 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  89. (int)Math.Ceiling(scale * ((srcY2 / srcTexture.Info.SamplesInY) + 1)));
  90. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  91. }
  92. dstTexture.SignalModified();
  93. }
  94. }
  95. }