Window.cs 6.8 KB

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