MethodCopyTexture.cs 3.7 KB

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