MethodCopyTexture.cs 3.7 KB

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