Window.cs 7.0 KB

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