TextureCopy.cs 4.5 KB

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