Window.cs 6.8 KB

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