MethodCopyTexture.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. float scale = srcTexture.ScaleFactor;
  58. float dstScale = dstTexture.ScaleFactor;
  59. Extents2D srcRegion = new Extents2D(
  60. (int)Math.Ceiling(scale * (srcX1 / srcTexture.Info.SamplesInX)),
  61. (int)Math.Ceiling(scale * (srcY1 / srcTexture.Info.SamplesInY)),
  62. (int)Math.Ceiling(scale * (srcX2 / srcTexture.Info.SamplesInX)),
  63. (int)Math.Ceiling(scale * (srcY2 / srcTexture.Info.SamplesInY)));
  64. Extents2D dstRegion = new Extents2D(
  65. (int)Math.Ceiling(dstScale * (dstX1 / dstTexture.Info.SamplesInX)),
  66. (int)Math.Ceiling(dstScale * (dstY1 / dstTexture.Info.SamplesInY)),
  67. (int)Math.Ceiling(dstScale * (dstX2 / dstTexture.Info.SamplesInX)),
  68. (int)Math.Ceiling(dstScale * (dstY2 / dstTexture.Info.SamplesInY)));
  69. bool linearFilter = control.UnpackLinearFilter();
  70. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  71. // For an out of bounds copy, we must ensure that the copy wraps to the next line,
  72. // so for a copy from a 64x64 texture, in the region [32, 96[, there are 32 pixels that are
  73. // outside the bounds of the texture. We fill the destination with the first 32 pixels
  74. // of the next line on the source texture.
  75. // This can be emulated with 2 copies (the first copy handles the region inside the bounds,
  76. // the second handles the region outside of the bounds).
  77. // We must also extend the source texture by one line to ensure we can wrap on the last line.
  78. // This is required by the (guest) OpenGL driver.
  79. if (srcX2 / srcTexture.Info.SamplesInX > srcTexture.Info.Width)
  80. {
  81. srcCopyTexture.Height++;
  82. srcTexture = TextureManager.FindOrCreateTexture(srcCopyTexture, srcCopyTextureFormat, srcTexture.ScaleMode == TextureScaleMode.Scaled, srcHint);
  83. scale = srcTexture.ScaleFactor;
  84. srcRegion = new Extents2D(
  85. (int)Math.Ceiling(scale * ((srcX1 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  86. (int)Math.Ceiling(scale * ((srcY1 / srcTexture.Info.SamplesInY) + 1)),
  87. (int)Math.Ceiling(scale * ((srcX2 / srcTexture.Info.SamplesInX) - srcTexture.Info.Width)),
  88. (int)Math.Ceiling(scale * ((srcY2 / srcTexture.Info.SamplesInY) + 1)));
  89. srcTexture.HostTexture.CopyTo(dstTexture.HostTexture, srcRegion, dstRegion, linearFilter);
  90. }
  91. dstTexture.SignalModified();
  92. }
  93. }
  94. }