TextureCopy.cs 4.3 KB

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