TextureCopy.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
  29. {
  30. linearFilter = false;
  31. }
  32. BlitFramebufferFilter filter = linearFilter
  33. ? BlitFramebufferFilter.Linear
  34. : BlitFramebufferFilter.Nearest;
  35. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  36. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  37. GL.Disable(EnableCap.RasterizerDiscard);
  38. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  39. GL.BlitFramebuffer(
  40. srcRegion.X1,
  41. srcRegion.Y1,
  42. srcRegion.X2,
  43. srcRegion.Y2,
  44. dstRegion.X1,
  45. dstRegion.Y1,
  46. dstRegion.X2,
  47. dstRegion.Y2,
  48. mask,
  49. filter);
  50. Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
  51. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);
  52. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  53. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  54. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  55. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  56. }
  57. private static void Attach(FramebufferTarget target, Format format, int handle)
  58. {
  59. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  60. {
  61. GL.FramebufferTexture(target, FramebufferAttachment.DepthStencilAttachment, handle, 0);
  62. }
  63. else if (IsDepthOnly(format))
  64. {
  65. GL.FramebufferTexture(target, FramebufferAttachment.DepthAttachment, handle, 0);
  66. }
  67. else if (format == Format.S8Uint)
  68. {
  69. GL.FramebufferTexture(target, FramebufferAttachment.StencilAttachment, handle, 0);
  70. }
  71. else
  72. {
  73. GL.FramebufferTexture(target, FramebufferAttachment.ColorAttachment0, handle, 0);
  74. }
  75. }
  76. private static ClearBufferMask GetMask(Format format)
  77. {
  78. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  79. {
  80. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  81. }
  82. else if (IsDepthOnly(format))
  83. {
  84. return ClearBufferMask.DepthBufferBit;
  85. }
  86. else if (format == Format.S8Uint)
  87. {
  88. return ClearBufferMask.StencilBufferBit;
  89. }
  90. else
  91. {
  92. return ClearBufferMask.ColorBufferBit;
  93. }
  94. }
  95. private static bool IsDepthOnly(Format format)
  96. {
  97. return format == Format.D16Unorm ||
  98. format == Format.D24X8Unorm ||
  99. format == Format.D32Float;
  100. }
  101. private int GetSrcFramebufferLazy()
  102. {
  103. if (_srcFramebuffer == 0)
  104. {
  105. _srcFramebuffer = GL.GenFramebuffer();
  106. }
  107. return _srcFramebuffer;
  108. }
  109. private int GetDstFramebufferLazy()
  110. {
  111. if (_dstFramebuffer == 0)
  112. {
  113. _dstFramebuffer = GL.GenFramebuffer();
  114. }
  115. return _dstFramebuffer;
  116. }
  117. public void Dispose()
  118. {
  119. if (_srcFramebuffer != 0)
  120. {
  121. GL.DeleteFramebuffer(_srcFramebuffer);
  122. _srcFramebuffer = 0;
  123. }
  124. if (_dstFramebuffer != 0)
  125. {
  126. GL.DeleteFramebuffer(_dstFramebuffer);
  127. _dstFramebuffer = 0;
  128. }
  129. }
  130. }
  131. }