MethodCopyTexture.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Texture srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture);
  20. if (srcTexture == null)
  21. {
  22. return;
  23. }
  24. // When the source texture that was found has a depth format,
  25. // we must enforce the target texture also has a depth format,
  26. // as copies between depth and color formats are not allowed.
  27. dstCopyTexture.Format = TextureCompatibility.DeriveDepthFormat(dstCopyTexture.Format, srcTexture.Format);
  28. Texture dstTexture = TextureManager.FindOrCreateTexture(dstCopyTexture, srcTexture.ScaleMode == Image.TextureScaleMode.Scaled);
  29. if (dstTexture == null)
  30. {
  31. return;
  32. }
  33. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  34. {
  35. srcTexture.PropagateScale(dstTexture);
  36. }
  37. var control = state.Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
  38. var region = state.Get<CopyRegion>(MethodOffset.CopyRegion);
  39. int srcX1 = (int)(region.SrcXF >> 32);
  40. int srcY1 = (int)(region.SrcYF >> 32);
  41. int srcX2 = (int)((region.SrcXF + region.SrcWidthRF * region.DstWidth) >> 32);
  42. int srcY2 = (int)((region.SrcYF + region.SrcHeightRF * region.DstHeight) >> 32);
  43. int dstX1 = region.DstX;
  44. int dstY1 = region.DstY;
  45. int dstX2 = region.DstX + region.DstWidth;
  46. int dstY2 = region.DstY + region.DstHeight;
  47. float scale = srcTexture.ScaleFactor; // src and dest scales are identical now.
  48. Extents2D srcRegion = new Extents2D(
  49. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  50. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  51. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  52. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  53. Extents2D dstRegion = new Extents2D(
  54. (int)Math.Ceiling(scale * (dstX1 / dstTexture.Info.SamplesInX)),
  55. (int)Math.Ceiling(scale * (dstY1 / dstTexture.Info.SamplesInY)),
  56. (int)Math.Ceiling(scale * (dstX2 / dstTexture.Info.SamplesInX)),
  57. (int)Math.Ceiling(scale * (dstY2 / dstTexture.Info.SamplesInY)));
  58. bool linearFilter = control.UnpackLinearFilter();
  59. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  60. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  61. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  62. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  63. // of the next line on the source texture.
  64. // This can be emulated with 2 copies (the first copy handles the region inside the bounds,
  65. // the second handles the region outside of the bounds).
  66. // We must also extend the source texture by one line to ensure we can wrap on the last line.
  67. // This is required by the (guest) OpenGL driver.
  68. if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
  69. {
  70. srcCopyTexture.Height++;
  71. srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcTexture.ScaleMode == Image.TextureScaleMode.Scaled);
  72. if (srcTexture.ScaleFactor != dstTexture.ScaleFactor)
  73. {
  74. srcTexture.PropagateScale(dstTexture);
  75. }
  76. srcRegion = new Extents2D(
  77. (int)Math.Ceiling(scale * ((srcX1 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  78. (int)Math.Ceiling(scale * ((srcY1 / srcTexture.Info.SamplesInY) + 1)),
  79. (int)Math.Ceiling(scale * ((srcX2 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  80. (int)Math.Ceiling(scale * ((srcY2 / srcTexture.Info.SamplesInY) + 1)));
  81. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  82. }
  83. dstTexture.SignalModified();
  84. }
  85. }
  86. }