MethodCopyTexture.cs 4.9 KB

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