Window.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 OpenGLRenderer _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(OpenGLRenderer 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. public void ChangeVSyncMode(bool vsyncEnabled) { }
  48. private void CreateStagingFramebuffer()
  49. {
  50. _stagingFrameBuffer = GL.GenFramebuffer();
  51. GL.GenTextures(_stagingTextures.Length, _stagingTextures);
  52. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _stagingFrameBuffer);
  53. foreach (var stagingTexture in _stagingTextures)
  54. {
  55. GL.BindTexture(TextureTarget.Texture2D, stagingTexture);
  56. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _width, _height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
  57. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
  58. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
  59. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, stagingTexture, 0);
  60. }
  61. GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  62. GL.BindTexture(TextureTarget.Texture2D, 0);
  63. }
  64. public void SetSize(int width, int height)
  65. {
  66. _width = width;
  67. _height = height;
  68. _sizeChanged = true;
  69. }
  70. private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop, Action<object> swapBuffersCallback)
  71. {
  72. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  73. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  74. GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, _stagingTextures[_currentTexture], 0);
  75. TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
  76. GL.FramebufferTexture(
  77. FramebufferTarget.ReadFramebuffer,
  78. FramebufferAttachment.ColorAttachment0,
  79. viewConverted.Handle,
  80. 0);
  81. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  82. GL.Disable(EnableCap.RasterizerDiscard);
  83. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  84. GL.Clear(ClearBufferMask.ColorBufferBit);
  85. int srcX0, srcX1, srcY0, srcY1;
  86. float scale = view.ScaleFactor;
  87. if (crop.Left == 0 && crop.Right == 0)
  88. {
  89. srcX0 = 0;
  90. srcX1 = (int)(view.Width / scale);
  91. }
  92. else
  93. {
  94. srcX0 = crop.Left;
  95. srcX1 = crop.Right;
  96. }
  97. if (crop.Top == 0 && crop.Bottom == 0)
  98. {
  99. srcY0 = 0;
  100. srcY1 = (int)(view.Height / scale);
  101. }
  102. else
  103. {
  104. srcY0 = crop.Top;
  105. srcY1 = crop.Bottom;
  106. }
  107. if (scale != 1f)
  108. {
  109. srcX0 = (int)(srcX0 * scale);
  110. srcY0 = (int)(srcY0 * scale);
  111. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  112. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  113. }
  114. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  115. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  116. int dstWidth = (int)(_width * ratioX);
  117. int dstHeight = (int)(_height * ratioY);
  118. int dstPaddingX = (_width - dstWidth) / 2;
  119. int dstPaddingY = (_height - dstHeight) / 2;
  120. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  121. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  122. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  123. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  124. if (ScreenCaptureRequested)
  125. {
  126. CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
  127. ScreenCaptureRequested = false;
  128. }
  129. GL.BlitFramebuffer(
  130. srcX0,
  131. srcY0,
  132. srcX1,
  133. srcY1,
  134. dstX0,
  135. dstY0,
  136. dstX1,
  137. dstY1,
  138. ClearBufferMask.ColorBufferBit,
  139. BlitFramebufferFilter.Linear);
  140. // Remove Alpha channel
  141. GL.ColorMask(false, false, false, true);
  142. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  143. GL.Clear(ClearBufferMask.ColorBufferBit);
  144. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  145. {
  146. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  147. }
  148. // Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
  149. GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
  150. GL.Viewport(0, 0, _width, _height);
  151. GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
  152. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _stagingFrameBuffer);
  153. swapBuffersCallback((object)_stagingTextures[_currentTexture]);
  154. _currentTexture = ++_currentTexture % _stagingTextures.Length;
  155. ((Pipeline)_renderer.Pipeline).RestoreClipControl();
  156. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  157. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  158. ((Pipeline)_renderer.Pipeline).RestoreViewport0();
  159. if (viewConverted != view)
  160. {
  161. viewConverted.Dispose();
  162. }
  163. }
  164. private int GetCopyFramebufferHandleLazy()
  165. {
  166. int handle = _copyFramebufferHandle;
  167. if (handle == 0)
  168. {
  169. handle = GL.GenFramebuffer();
  170. _copyFramebufferHandle = handle;
  171. }
  172. return handle;
  173. }
  174. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  175. {
  176. BackgroundContext = new BackgroundContextWorker(baseContext);
  177. }
  178. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  179. {
  180. long size = Math.Abs(4 * width * height);
  181. byte[] bitmap = new byte[size];
  182. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  183. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  184. }
  185. public void Dispose()
  186. {
  187. BackgroundContext.Dispose();
  188. if (_copyFramebufferHandle != 0)
  189. {
  190. GL.DeleteFramebuffer(_copyFramebufferHandle);
  191. _copyFramebufferHandle = 0;
  192. }
  193. if (_stagingFrameBuffer != 0)
  194. {
  195. GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
  196. GL.DeleteFramebuffer(_stagingFrameBuffer);
  197. _stagingFrameBuffer = 0;
  198. _stagingTextures = null;
  199. }
  200. }
  201. }
  202. }