Window.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.OpenGL.Helper;
  4. using Ryujinx.Graphics.OpenGL.Image;
  5. using System;
  6. namespace Ryujinx.Graphics.OpenGL
  7. {
  8. class Window : IWindow, IDisposable
  9. {
  10. private const int TextureCount = 3;
  11. private readonly Renderer _renderer;
  12. private int _width;
  13. private int _height;
  14. private bool _sizeChanged;
  15. private int _copyFramebufferHandle;
  16. private int _stagingFrameBuffer;
  17. private int[] _stagingTextures;
  18. private int _currentTexture;
  19. internal BackgroundContextWorker BackgroundContext { get; private set; }
  20. internal bool ScreenCaptureRequested { get; set; }
  21. public Window(Renderer renderer)
  22. {
  23. _renderer = renderer;
  24. _stagingTextures = new int[TextureCount];
  25. }
  26. public void Present(ITexture texture, ImageCrop crop, Action<object> swapBuffersCallback)
  27. {
  28. GL.Disable(EnableCap.FramebufferSrgb);
  29. if (_sizeChanged)
  30. {
  31. if (_stagingFrameBuffer != 0)
  32. {
  33. GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
  34. GL.DeleteFramebuffer(_stagingFrameBuffer);
  35. }
  36. CreateStagingFramebuffer();
  37. _sizeChanged = false;
  38. }
  39. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  40. CopyTextureToFrameBufferRGB(_stagingFrameBuffer, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);
  41. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  42. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  43. GL.Enable(EnableCap.FramebufferSrgb);
  44. // Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
  45. GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
  46. }
  47. private void CreateStagingFramebuffer()
  48. {
  49. _stagingFrameBuffer = GL.GenFramebuffer();
  50. GL.GenTextures(_stagingTextures.Length, _stagingTextures);
  51. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _stagingFrameBuffer);
  52. foreach (var stagingTexture in _stagingTextures)
  53. {
  54. GL.BindTexture(TextureTarget.Texture2D, stagingTexture);
  55. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _width, _height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
  56. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
  57. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
  58. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, stagingTexture, 0);
  59. }
  60. GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  61. GL.BindTexture(TextureTarget.Texture2D, 0);
  62. }
  63. public void SetSize(int width, int height)
  64. {
  65. _width = width;
  66. _height = height;
  67. _sizeChanged = true;
  68. }
  69. private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop, Action<object> swapBuffersCallback)
  70. {
  71. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  72. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  73. GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, _stagingTextures[_currentTexture], 0);
  74. TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
  75. GL.FramebufferTexture(
  76. FramebufferTarget.ReadFramebuffer,
  77. FramebufferAttachment.ColorAttachment0,
  78. viewConverted.Handle,
  79. 0);
  80. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  81. GL.Disable(EnableCap.RasterizerDiscard);
  82. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  83. GL.Clear(ClearBufferMask.ColorBufferBit);
  84. int srcX0, srcX1, srcY0, srcY1;
  85. float scale = view.ScaleFactor;
  86. if (crop.Left == 0 && crop.Right == 0)
  87. {
  88. srcX0 = 0;
  89. srcX1 = (int)(view.Width / scale);
  90. }
  91. else
  92. {
  93. srcX0 = crop.Left;
  94. srcX1 = crop.Right;
  95. }
  96. if (crop.Top == 0 && crop.Bottom == 0)
  97. {
  98. srcY0 = 0;
  99. srcY1 = (int)(view.Height / scale);
  100. }
  101. else
  102. {
  103. srcY0 = crop.Top;
  104. srcY1 = crop.Bottom;
  105. }
  106. if (scale != 1f)
  107. {
  108. srcX0 = (int)(srcX0 * scale);
  109. srcY0 = (int)(srcY0 * scale);
  110. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  111. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  112. }
  113. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  114. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  115. int dstWidth = (int)(_width * ratioX);
  116. int dstHeight = (int)(_height * ratioY);
  117. int dstPaddingX = (_width - dstWidth) / 2;
  118. int dstPaddingY = (_height - dstHeight) / 2;
  119. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  120. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  121. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  122. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  123. if (ScreenCaptureRequested)
  124. {
  125. CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
  126. ScreenCaptureRequested = false;
  127. }
  128. GL.BlitFramebuffer(
  129. srcX0,
  130. srcY0,
  131. srcX1,
  132. srcY1,
  133. dstX0,
  134. dstY0,
  135. dstX1,
  136. dstY1,
  137. ClearBufferMask.ColorBufferBit,
  138. BlitFramebufferFilter.Linear);
  139. // Remove Alpha channel
  140. GL.ColorMask(false, false, false, true);
  141. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  142. GL.Clear(ClearBufferMask.ColorBufferBit);
  143. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  144. {
  145. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  146. }
  147. // Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
  148. GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
  149. GL.Viewport(0, 0, _width, _height);
  150. GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
  151. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _stagingFrameBuffer);
  152. swapBuffersCallback((object)_stagingTextures[_currentTexture]);
  153. _currentTexture = ++_currentTexture % _stagingTextures.Length;
  154. ((Pipeline)_renderer.Pipeline).RestoreClipControl();
  155. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  156. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  157. ((Pipeline)_renderer.Pipeline).RestoreViewport0();
  158. if (viewConverted != view)
  159. {
  160. viewConverted.Dispose();
  161. }
  162. }
  163. private int GetCopyFramebufferHandleLazy()
  164. {
  165. int handle = _copyFramebufferHandle;
  166. if (handle == 0)
  167. {
  168. handle = GL.GenFramebuffer();
  169. _copyFramebufferHandle = handle;
  170. }
  171. return handle;
  172. }
  173. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  174. {
  175. BackgroundContext = new BackgroundContextWorker(baseContext);
  176. }
  177. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  178. {
  179. long size = Math.Abs(4 * width * height);
  180. byte[] bitmap = new byte[size];
  181. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  182. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  183. }
  184. public void Dispose()
  185. {
  186. BackgroundContext.Dispose();
  187. if (_copyFramebufferHandle != 0)
  188. {
  189. GL.DeleteFramebuffer(_copyFramebufferHandle);
  190. _copyFramebufferHandle = 0;
  191. }
  192. if (_stagingFrameBuffer != 0)
  193. {
  194. GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
  195. GL.DeleteFramebuffer(_stagingFrameBuffer);
  196. _stagingFrameBuffer = 0;
  197. _stagingTextures = null;
  198. }
  199. }
  200. }
  201. }