Window.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. swapBuffersCallback();
  182. ((Pipeline)_renderer.Pipeline).RestoreClipControl();
  183. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  184. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  185. ((Pipeline)_renderer.Pipeline).RestoreViewport0();
  186. if (viewConverted != view)
  187. {
  188. viewConverted.Dispose();
  189. }
  190. }
  191. private int GetCopyFramebufferHandleLazy()
  192. {
  193. int handle = _copyFramebufferHandle;
  194. if (handle == 0)
  195. {
  196. handle = GL.GenFramebuffer();
  197. _copyFramebufferHandle = handle;
  198. }
  199. return handle;
  200. }
  201. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  202. {
  203. BackgroundContext = new BackgroundContextWorker(baseContext);
  204. _initialized = true;
  205. }
  206. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  207. {
  208. long size = Math.Abs(4 * width * height);
  209. byte[] bitmap = new byte[size];
  210. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  211. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  212. }
  213. public void Dispose()
  214. {
  215. if (!_initialized)
  216. {
  217. return;
  218. }
  219. BackgroundContext.Dispose();
  220. if (_copyFramebufferHandle != 0)
  221. {
  222. GL.DeleteFramebuffer(_copyFramebufferHandle);
  223. _copyFramebufferHandle = 0;
  224. }
  225. _antiAliasing?.Dispose();
  226. _scalingFilter?.Dispose();
  227. _upscaledTexture?.Dispose();
  228. }
  229. public void SetAntiAliasing(AntiAliasing effect)
  230. {
  231. if (_currentAntiAliasing == effect && _antiAliasing != null)
  232. {
  233. return;
  234. }
  235. _currentAntiAliasing = effect;
  236. _updateEffect = true;
  237. }
  238. public void SetScalingFilter(ScalingFilter type)
  239. {
  240. if (_currentScalingFilter == type && _antiAliasing != null)
  241. {
  242. return;
  243. }
  244. _currentScalingFilter = type;
  245. _updateScalingFilter = true;
  246. }
  247. private void UpdateEffect()
  248. {
  249. if (_updateEffect)
  250. {
  251. _updateEffect = false;
  252. switch (_currentAntiAliasing)
  253. {
  254. case AntiAliasing.Fxaa:
  255. _antiAliasing?.Dispose();
  256. _antiAliasing = new FxaaPostProcessingEffect(_renderer);
  257. break;
  258. case AntiAliasing.None:
  259. _antiAliasing?.Dispose();
  260. _antiAliasing = null;
  261. break;
  262. case AntiAliasing.SmaaLow:
  263. case AntiAliasing.SmaaMedium:
  264. case AntiAliasing.SmaaHigh:
  265. case AntiAliasing.SmaaUltra:
  266. var quality = _currentAntiAliasing - AntiAliasing.SmaaLow;
  267. if (_antiAliasing is SmaaPostProcessingEffect smaa)
  268. {
  269. smaa.Quality = quality;
  270. }
  271. else
  272. {
  273. _antiAliasing?.Dispose();
  274. _antiAliasing = new SmaaPostProcessingEffect(_renderer, quality);
  275. }
  276. break;
  277. }
  278. }
  279. if (_updateSize && !_updateScalingFilter)
  280. {
  281. RecreateUpscalingTexture();
  282. }
  283. _updateSize = false;
  284. if (_updateScalingFilter)
  285. {
  286. _updateScalingFilter = false;
  287. switch (_currentScalingFilter)
  288. {
  289. case ScalingFilter.Bilinear:
  290. case ScalingFilter.Nearest:
  291. _scalingFilter?.Dispose();
  292. _scalingFilter = null;
  293. _isLinear = _currentScalingFilter == ScalingFilter.Bilinear;
  294. _upscaledTexture?.Dispose();
  295. _upscaledTexture = null;
  296. break;
  297. case ScalingFilter.Fsr:
  298. if (_scalingFilter is not FsrScalingFilter)
  299. {
  300. _scalingFilter?.Dispose();
  301. _scalingFilter = new FsrScalingFilter(_renderer, _antiAliasing);
  302. }
  303. _isLinear = false;
  304. _scalingFilter.Level = _scalingFilterLevel;
  305. RecreateUpscalingTexture();
  306. break;
  307. }
  308. }
  309. }
  310. private void RecreateUpscalingTexture(bool forceBgra = false)
  311. {
  312. _upscaledTexture?.Dispose();
  313. var info = new TextureCreateInfo(
  314. _width,
  315. _height,
  316. 1,
  317. 1,
  318. 1,
  319. 1,
  320. 1,
  321. 1,
  322. Format.R8G8B8A8Unorm,
  323. DepthStencilMode.Depth,
  324. Target.Texture2D,
  325. forceBgra ? SwizzleComponent.Blue : SwizzleComponent.Red,
  326. SwizzleComponent.Green,
  327. forceBgra ? SwizzleComponent.Red : SwizzleComponent.Blue,
  328. SwizzleComponent.Alpha);
  329. _isBgra = forceBgra;
  330. _upscaledTexture = _renderer.CreateTexture(info, 1) as TextureView;
  331. }
  332. public void SetScalingFilterLevel(float level)
  333. {
  334. _scalingFilterLevel = level;
  335. _updateScalingFilter = true;
  336. }
  337. }
  338. }