Window.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.OpenGL.Effects;
  4. using Ryujinx.Graphics.OpenGL.Effects.Smaa;
  5. using Ryujinx.Graphics.OpenGL.Image;
  6. using System;
  7. namespace Ryujinx.Graphics.OpenGL
  8. {
  9. class Window : IWindow, IDisposable
  10. {
  11. private readonly OpenGLRenderer _renderer;
  12. private bool _initialized;
  13. private int _width;
  14. private int _height;
  15. private bool _updateSize;
  16. private int _copyFramebufferHandle;
  17. private IPostProcessingEffect _antiAliasing;
  18. private IScalingFilter _scalingFilter;
  19. private bool _isLinear;
  20. private AntiAliasing _currentAntiAliasing;
  21. private bool _updateEffect;
  22. private ScalingFilter _currentScalingFilter;
  23. private float _scalingFilterLevel;
  24. private bool _updateScalingFilter;
  25. private bool _isBgra;
  26. private TextureView _upscaledTexture;
  27. internal BackgroundContextWorker BackgroundContext { get; private set; }
  28. internal bool ScreenCaptureRequested { get; set; }
  29. public Window(OpenGLRenderer renderer)
  30. {
  31. _renderer = renderer;
  32. }
  33. public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
  34. {
  35. GL.Disable(EnableCap.FramebufferSrgb);
  36. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  37. CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);
  38. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  39. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  40. GL.Enable(EnableCap.FramebufferSrgb);
  41. // Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
  42. GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
  43. }
  44. public void ChangeVSyncMode(bool vsyncEnabled) { }
  45. public void SetSize(int width, int height)
  46. {
  47. _width = width;
  48. _height = height;
  49. _updateSize = true;
  50. }
  51. private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop, Action swapBuffersCallback)
  52. {
  53. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  54. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  55. TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
  56. UpdateEffect();
  57. if (_antiAliasing != null)
  58. {
  59. var oldView = viewConverted;
  60. viewConverted = _antiAliasing.Run(viewConverted, _width, _height);
  61. if (viewConverted.Format.IsBgr())
  62. {
  63. var swappedView = _renderer.TextureCopy.BgraSwap(viewConverted);
  64. viewConverted?.Dispose();
  65. viewConverted = swappedView;
  66. }
  67. if (viewConverted != oldView && oldView != view)
  68. {
  69. oldView.Dispose();
  70. }
  71. }
  72. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  73. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  74. GL.FramebufferTexture(
  75. FramebufferTarget.ReadFramebuffer,
  76. FramebufferAttachment.ColorAttachment0,
  77. viewConverted.Handle,
  78. 0);
  79. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  80. GL.Disable(EnableCap.RasterizerDiscard);
  81. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  82. GL.Clear(ClearBufferMask.ColorBufferBit);
  83. int srcX0, srcX1, srcY0, srcY1;
  84. float scale = viewConverted.ScaleFactor;
  85. if (crop.Left == 0 && crop.Right == 0)
  86. {
  87. srcX0 = 0;
  88. srcX1 = (int)(viewConverted.Width / scale);
  89. }
  90. else
  91. {
  92. srcX0 = crop.Left;
  93. srcX1 = crop.Right;
  94. }
  95. if (crop.Top == 0 && crop.Bottom == 0)
  96. {
  97. srcY0 = 0;
  98. srcY1 = (int)(viewConverted.Height / scale);
  99. }
  100. else
  101. {
  102. srcY0 = crop.Top;
  103. srcY1 = crop.Bottom;
  104. }
  105. if (scale != 1f)
  106. {
  107. srcX0 = (int)(srcX0 * scale);
  108. srcY0 = (int)(srcY0 * scale);
  109. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  110. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  111. }
  112. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  113. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  114. int dstWidth = (int)(_width * ratioX);
  115. int dstHeight = (int)(_height * ratioY);
  116. int dstPaddingX = (_width - dstWidth) / 2;
  117. int dstPaddingY = (_height - dstHeight) / 2;
  118. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  119. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  120. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  121. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  122. if (ScreenCaptureRequested)
  123. {
  124. CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
  125. ScreenCaptureRequested = false;
  126. }
  127. if (_scalingFilter != null)
  128. {
  129. if (viewConverted.Format.IsBgr() && !_isBgra)
  130. {
  131. RecreateUpscalingTexture(true);
  132. }
  133. _scalingFilter.Run(
  134. viewConverted,
  135. _upscaledTexture,
  136. _width,
  137. _height,
  138. new Extents2D(
  139. srcX0,
  140. srcY0,
  141. srcX1,
  142. srcY1),
  143. new Extents2D(
  144. dstX0,
  145. dstY0,
  146. dstX1,
  147. dstY1)
  148. );
  149. srcX0 = dstX0;
  150. srcY0 = dstY0;
  151. srcX1 = dstX1;
  152. srcY1 = dstY1;
  153. GL.FramebufferTexture(
  154. FramebufferTarget.ReadFramebuffer,
  155. FramebufferAttachment.ColorAttachment0,
  156. _upscaledTexture.Handle,
  157. 0);
  158. }
  159. GL.BlitFramebuffer(
  160. srcX0,
  161. srcY0,
  162. srcX1,
  163. srcY1,
  164. dstX0,
  165. dstY0,
  166. dstX1,
  167. dstY1,
  168. ClearBufferMask.ColorBufferBit,
  169. _isLinear ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest);
  170. // Remove Alpha channel
  171. GL.ColorMask(false, false, false, true);
  172. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  173. GL.Clear(ClearBufferMask.ColorBufferBit);
  174. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  175. {
  176. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  177. }
  178. // Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
  179. GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
  180. GL.Viewport(0, 0, _width, _height);
  181. GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
  182. swapBuffersCallback();
  183. ((Pipeline)_renderer.Pipeline).RestoreClipControl();
  184. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  185. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  186. ((Pipeline)_renderer.Pipeline).RestoreViewport0();
  187. if (viewConverted != view)
  188. {
  189. viewConverted.Dispose();
  190. }
  191. }
  192. private int GetCopyFramebufferHandleLazy()
  193. {
  194. int handle = _copyFramebufferHandle;
  195. if (handle == 0)
  196. {
  197. handle = GL.GenFramebuffer();
  198. _copyFramebufferHandle = handle;
  199. }
  200. return handle;
  201. }
  202. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  203. {
  204. BackgroundContext = new BackgroundContextWorker(baseContext);
  205. _initialized = true;
  206. }
  207. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  208. {
  209. long size = Math.Abs(4 * width * height);
  210. byte[] bitmap = new byte[size];
  211. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  212. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  213. }
  214. public void Dispose()
  215. {
  216. if (!_initialized)
  217. {
  218. return;
  219. }
  220. BackgroundContext.Dispose();
  221. if (_copyFramebufferHandle != 0)
  222. {
  223. GL.DeleteFramebuffer(_copyFramebufferHandle);
  224. _copyFramebufferHandle = 0;
  225. }
  226. _antiAliasing?.Dispose();
  227. _scalingFilter?.Dispose();
  228. _upscaledTexture?.Dispose();
  229. }
  230. public void SetAntiAliasing(AntiAliasing effect)
  231. {
  232. if (_currentAntiAliasing == effect && _antiAliasing != null)
  233. {
  234. return;
  235. }
  236. _currentAntiAliasing = effect;
  237. _updateEffect = true;
  238. }
  239. public void SetScalingFilter(ScalingFilter type)
  240. {
  241. if (_currentScalingFilter == type && _antiAliasing != null)
  242. {
  243. return;
  244. }
  245. _currentScalingFilter = type;
  246. _updateScalingFilter = true;
  247. }
  248. private void UpdateEffect()
  249. {
  250. if (_updateEffect)
  251. {
  252. _updateEffect = false;
  253. switch (_currentAntiAliasing)
  254. {
  255. case AntiAliasing.Fxaa:
  256. _antiAliasing?.Dispose();
  257. _antiAliasing = new FxaaPostProcessingEffect(_renderer);
  258. break;
  259. case AntiAliasing.None:
  260. _antiAliasing?.Dispose();
  261. _antiAliasing = null;
  262. break;
  263. case AntiAliasing.SmaaLow:
  264. case AntiAliasing.SmaaMedium:
  265. case AntiAliasing.SmaaHigh:
  266. case AntiAliasing.SmaaUltra:
  267. var quality = _currentAntiAliasing - AntiAliasing.SmaaLow;
  268. if (_antiAliasing is SmaaPostProcessingEffect smaa)
  269. {
  270. smaa.Quality = quality;
  271. }
  272. else
  273. {
  274. _antiAliasing?.Dispose();
  275. _antiAliasing = new SmaaPostProcessingEffect(_renderer, quality);
  276. }
  277. break;
  278. }
  279. }
  280. if (_updateSize && !_updateScalingFilter)
  281. {
  282. RecreateUpscalingTexture();
  283. }
  284. _updateSize = false;
  285. if (_updateScalingFilter)
  286. {
  287. _updateScalingFilter = false;
  288. switch (_currentScalingFilter)
  289. {
  290. case ScalingFilter.Bilinear:
  291. case ScalingFilter.Nearest:
  292. _scalingFilter?.Dispose();
  293. _scalingFilter = null;
  294. _isLinear = _currentScalingFilter == ScalingFilter.Bilinear;
  295. _upscaledTexture?.Dispose();
  296. _upscaledTexture = null;
  297. break;
  298. case ScalingFilter.Fsr:
  299. if (_scalingFilter is not FsrScalingFilter)
  300. {
  301. _scalingFilter?.Dispose();
  302. _scalingFilter = new FsrScalingFilter(_renderer, _antiAliasing);
  303. }
  304. _isLinear = false;
  305. _scalingFilter.Level = _scalingFilterLevel;
  306. RecreateUpscalingTexture();
  307. break;
  308. }
  309. }
  310. }
  311. private void RecreateUpscalingTexture(bool forceBgra = false)
  312. {
  313. _upscaledTexture?.Dispose();
  314. var info = new TextureCreateInfo(
  315. _width,
  316. _height,
  317. 1,
  318. 1,
  319. 1,
  320. 1,
  321. 1,
  322. 1,
  323. Format.R8G8B8A8Unorm,
  324. DepthStencilMode.Depth,
  325. Target.Texture2D,
  326. forceBgra ? SwizzleComponent.Blue : SwizzleComponent.Red,
  327. SwizzleComponent.Green,
  328. forceBgra ? SwizzleComponent.Red : SwizzleComponent.Blue,
  329. SwizzleComponent.Alpha);
  330. _isBgra = forceBgra;
  331. _upscaledTexture = _renderer.CreateTexture(info, 1) as TextureView;
  332. }
  333. public void SetScalingFilterLevel(float level)
  334. {
  335. _scalingFilterLevel = level;
  336. _updateScalingFilter = true;
  337. }
  338. }
  339. }