TextureCopy.cs 4.6 KB

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