TextureCopy.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Ryujinx.Graphics.GAL;
  2. using OpenTK.Graphics.OpenGL;
  3. namespace Ryujinx.Graphics.OpenGL
  4. {
  5. class TextureCopy
  6. {
  7. private int _srcFramebuffer;
  8. private int _dstFramebuffer;
  9. public void Copy(
  10. TextureView src,
  11. TextureView dst,
  12. Extents2D srcRegion,
  13. Extents2D dstRegion,
  14. bool linearFilter)
  15. {
  16. GL.Disable(EnableCap.FramebufferSrgb);
  17. int oldReadFramebufferHandle = GL.GetInteger(GetPName.ReadFramebufferBinding);
  18. int oldDrawFramebufferHandle = GL.GetInteger(GetPName.DrawFramebufferBinding);
  19. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
  20. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
  21. Attach(FramebufferTarget.ReadFramebuffer, src.Format, src.Handle);
  22. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle);
  23. ClearBufferMask mask = GetMask(src.Format);
  24. BlitFramebufferFilter filter = linearFilter
  25. ? BlitFramebufferFilter.Linear
  26. : BlitFramebufferFilter.Nearest;
  27. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  28. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  29. GL.BlitFramebuffer(
  30. srcRegion.X1,
  31. srcRegion.Y1,
  32. srcRegion.X2,
  33. srcRegion.Y2,
  34. dstRegion.X1,
  35. dstRegion.Y1,
  36. dstRegion.X2,
  37. dstRegion.Y2,
  38. mask,
  39. filter);
  40. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  41. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  42. GL.Enable(EnableCap.FramebufferSrgb);
  43. }
  44. private static void Detach(FramebufferTarget target, Format format)
  45. {
  46. Attach(target, format, 0);
  47. }
  48. private static void Attach(FramebufferTarget target, Format format, int handle)
  49. {
  50. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  51. {
  52. GL.FramebufferTexture(target, FramebufferAttachment.DepthStencilAttachment, handle, 0);
  53. }
  54. else if (IsDepthOnly(format))
  55. {
  56. GL.FramebufferTexture(target, FramebufferAttachment.DepthAttachment, handle, 0);
  57. }
  58. else if (format == Format.S8Uint)
  59. {
  60. GL.FramebufferTexture(target, FramebufferAttachment.StencilAttachment, handle, 0);
  61. }
  62. else
  63. {
  64. GL.FramebufferTexture(target, FramebufferAttachment.ColorAttachment0, handle, 0);
  65. }
  66. }
  67. private static ClearBufferMask GetMask(Format format)
  68. {
  69. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  70. {
  71. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  72. }
  73. else if (IsDepthOnly(format))
  74. {
  75. return ClearBufferMask.DepthBufferBit;
  76. }
  77. else if (format == Format.S8Uint)
  78. {
  79. return ClearBufferMask.StencilBufferBit;
  80. }
  81. else
  82. {
  83. return ClearBufferMask.ColorBufferBit;
  84. }
  85. }
  86. private static bool IsDepthOnly(Format format)
  87. {
  88. return format == Format.D16Unorm ||
  89. format == Format.D24X8Unorm ||
  90. format == Format.D32Float;
  91. }
  92. private int GetSrcFramebufferLazy()
  93. {
  94. if (_srcFramebuffer == 0)
  95. {
  96. _srcFramebuffer = GL.GenFramebuffer();
  97. }
  98. return _srcFramebuffer;
  99. }
  100. private int GetDstFramebufferLazy()
  101. {
  102. if (_dstFramebuffer == 0)
  103. {
  104. _dstFramebuffer = GL.GenFramebuffer();
  105. }
  106. return _dstFramebuffer;
  107. }
  108. }
  109. }