TextureCopy.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. private int _copyPboHandle;
  12. private int _copyPboSize;
  13. public TextureCopy(Renderer renderer)
  14. {
  15. _renderer = renderer;
  16. }
  17. public void Copy(
  18. TextureView src,
  19. TextureView dst,
  20. Extents2D srcRegion,
  21. Extents2D dstRegion,
  22. bool linearFilter)
  23. {
  24. TextureView srcConverted = src.Format.IsBgra8() != dst.Format.IsBgra8() ? BgraSwap(src) : src;
  25. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  26. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
  27. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
  28. Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle);
  29. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle);
  30. ClearBufferMask mask = GetMask(src.Format);
  31. if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
  32. {
  33. linearFilter = false;
  34. }
  35. BlitFramebufferFilter filter = linearFilter
  36. ? BlitFramebufferFilter.Linear
  37. : BlitFramebufferFilter.Nearest;
  38. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  39. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  40. GL.Disable(EnableCap.RasterizerDiscard);
  41. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  42. GL.BlitFramebuffer(
  43. srcRegion.X1,
  44. srcRegion.Y1,
  45. srcRegion.X2,
  46. srcRegion.Y2,
  47. dstRegion.X1,
  48. dstRegion.Y1,
  49. dstRegion.X2,
  50. dstRegion.Y2,
  51. mask,
  52. filter);
  53. Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
  54. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);
  55. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  56. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  57. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  58. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  59. if (srcConverted != src)
  60. {
  61. srcConverted.Dispose();
  62. }
  63. }
  64. private static void Attach(FramebufferTarget target, Format format, int handle)
  65. {
  66. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  67. {
  68. GL.FramebufferTexture(target, FramebufferAttachment.DepthStencilAttachment, handle, 0);
  69. }
  70. else if (IsDepthOnly(format))
  71. {
  72. GL.FramebufferTexture(target, FramebufferAttachment.DepthAttachment, handle, 0);
  73. }
  74. else if (format == Format.S8Uint)
  75. {
  76. GL.FramebufferTexture(target, FramebufferAttachment.StencilAttachment, handle, 0);
  77. }
  78. else
  79. {
  80. GL.FramebufferTexture(target, FramebufferAttachment.ColorAttachment0, handle, 0);
  81. }
  82. }
  83. private static ClearBufferMask GetMask(Format format)
  84. {
  85. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  86. {
  87. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  88. }
  89. else if (IsDepthOnly(format))
  90. {
  91. return ClearBufferMask.DepthBufferBit;
  92. }
  93. else if (format == Format.S8Uint)
  94. {
  95. return ClearBufferMask.StencilBufferBit;
  96. }
  97. else
  98. {
  99. return ClearBufferMask.ColorBufferBit;
  100. }
  101. }
  102. private static bool IsDepthOnly(Format format)
  103. {
  104. return format == Format.D16Unorm ||
  105. format == Format.D24X8Unorm ||
  106. format == Format.D32Float;
  107. }
  108. public TextureView BgraSwap(TextureView from)
  109. {
  110. TextureView to = (TextureView)_renderer.CreateTexture(from.Info, 1f);
  111. EnsurePbo(from);
  112. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  113. from.WriteToPbo(0, forceBgra: true);
  114. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  115. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
  116. to.ReadFromPbo(0, _copyPboSize);
  117. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
  118. return to;
  119. }
  120. private void EnsurePbo(TextureView view)
  121. {
  122. int requiredSize = 0;
  123. for (int level = 0; level < view.Info.Levels; level++)
  124. {
  125. requiredSize += view.Info.GetMipSize(level);
  126. }
  127. if (_copyPboSize < requiredSize && _copyPboHandle != 0)
  128. {
  129. GL.DeleteBuffer(_copyPboHandle);
  130. _copyPboHandle = 0;
  131. }
  132. if (_copyPboHandle == 0)
  133. {
  134. _copyPboHandle = GL.GenBuffer();
  135. _copyPboSize = requiredSize;
  136. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  137. GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
  138. }
  139. }
  140. private int GetSrcFramebufferLazy()
  141. {
  142. if (_srcFramebuffer == 0)
  143. {
  144. _srcFramebuffer = GL.GenFramebuffer();
  145. }
  146. return _srcFramebuffer;
  147. }
  148. private int GetDstFramebufferLazy()
  149. {
  150. if (_dstFramebuffer == 0)
  151. {
  152. _dstFramebuffer = GL.GenFramebuffer();
  153. }
  154. return _dstFramebuffer;
  155. }
  156. public void Dispose()
  157. {
  158. if (_srcFramebuffer != 0)
  159. {
  160. GL.DeleteFramebuffer(_srcFramebuffer);
  161. _srcFramebuffer = 0;
  162. }
  163. if (_dstFramebuffer != 0)
  164. {
  165. GL.DeleteFramebuffer(_dstFramebuffer);
  166. _dstFramebuffer = 0;
  167. }
  168. if (_copyPboHandle != 0)
  169. {
  170. GL.DeleteBuffer(_copyPboHandle);
  171. _copyPboHandle = 0;
  172. }
  173. }
  174. }
  175. }