MethodCopyTexture.cs 3.9 KB

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