TextureCopy.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Ryujinx.Graphics.GAL;
  2. using OpenTK.Graphics.OpenGL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL.Image
  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 oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  23. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
  24. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
  25. Attach(FramebufferTarget.ReadFramebuffer, src.Format, src.Handle);
  26. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle);
  27. ClearBufferMask mask = GetMask(src.Format);
  28. BlitFramebufferFilter filter = linearFilter
  29. ? BlitFramebufferFilter.Linear
  30. : BlitFramebufferFilter.Nearest;
  31. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  32. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  33. GL.Disable(EnableCap.RasterizerDiscard);
  34. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  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).RestoreScissor0Enable();
  49. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  50. }
  51. private static void Attach(FramebufferTarget target, Format format, int handle)
  52. {
  53. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  54. {
  55. GL.FramebufferTexture(target, FramebufferAttachment.DepthStencilAttachment, handle, 0);
  56. }
  57. else if (IsDepthOnly(format))
  58. {
  59. GL.FramebufferTexture(target, FramebufferAttachment.DepthAttachment, handle, 0);
  60. }
  61. else if (format == Format.S8Uint)
  62. {
  63. GL.FramebufferTexture(target, FramebufferAttachment.StencilAttachment, handle, 0);
  64. }
  65. else
  66. {
  67. GL.FramebufferTexture(target, FramebufferAttachment.ColorAttachment0, handle, 0);
  68. }
  69. }
  70. private static ClearBufferMask GetMask(Format format)
  71. {
  72. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  73. {
  74. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  75. }
  76. else if (IsDepthOnly(format))
  77. {
  78. return ClearBufferMask.DepthBufferBit;
  79. }
  80. else if (format == Format.S8Uint)
  81. {
  82. return ClearBufferMask.StencilBufferBit;
  83. }
  84. else
  85. {
  86. return ClearBufferMask.ColorBufferBit;
  87. }
  88. }
  89. private static bool IsDepthOnly(Format format)
  90. {
  91. return format == Format.D16Unorm ||
  92. format == Format.D24X8Unorm ||
  93. format == Format.D32Float;
  94. }
  95. private int GetSrcFramebufferLazy()
  96. {
  97. if (_srcFramebuffer == 0)
  98. {
  99. _srcFramebuffer = GL.GenFramebuffer();
  100. }
  101. return _srcFramebuffer;
  102. }
  103. private int GetDstFramebufferLazy()
  104. {
  105. if (_dstFramebuffer == 0)
  106. {
  107. _dstFramebuffer = GL.GenFramebuffer();
  108. }
  109. return _dstFramebuffer;
  110. }
  111. public void Dispose()
  112. {
  113. if (_srcFramebuffer != 0)
  114. {
  115. GL.DeleteFramebuffer(_srcFramebuffer);
  116. _srcFramebuffer = 0;
  117. }
  118. if (_dstFramebuffer != 0)
  119. {
  120. GL.DeleteFramebuffer(_dstFramebuffer);
  121. _dstFramebuffer = 0;
  122. }
  123. }
  124. }
  125. }