Window.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. if (crop.Left == 0 && crop.Right == 0)
  85. {
  86. srcX0 = 0;
  87. srcX1 = viewConverted.Width;
  88. }
  89. else
  90. {
  91. srcX0 = crop.Left;
  92. srcX1 = crop.Right;
  93. }
  94. if (crop.Top == 0 && crop.Bottom == 0)
  95. {
  96. srcY0 = 0;
  97. srcY1 = viewConverted.Height;
  98. }
  99. else
  100. {
  101. srcY0 = crop.Top;
  102. srcY1 = crop.Bottom;
  103. }
  104. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  105. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  106. int dstWidth = (int)(_width * ratioX);
  107. int dstHeight = (int)(_height * ratioY);
  108. int dstPaddingX = (_width - dstWidth) / 2;
  109. int dstPaddingY = (_height - dstHeight) / 2;
  110. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  111. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  112. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  113. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  114. if (ScreenCaptureRequested)
  115. {
  116. CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
  117. ScreenCaptureRequested = false;
  118. }
  119. if (_scalingFilter != null)
  120. {
  121. if (viewConverted.Format.IsBgr() && !_isBgra)
  122. {
  123. RecreateUpscalingTexture(true);
  124. }
  125. _scalingFilter.Run(
  126. viewConverted,
  127. _upscaledTexture,
  128. _width,
  129. _height,
  130. new Extents2D(
  131. srcX0,
  132. srcY0,
  133. srcX1,
  134. srcY1),
  135. new Extents2D(
  136. dstX0,
  137. dstY0,
  138. dstX1,
  139. dstY1)
  140. );
  141. srcX0 = dstX0;
  142. srcY0 = dstY0;
  143. srcX1 = dstX1;
  144. srcY1 = dstY1;
  145. GL.FramebufferTexture(
  146. FramebufferTarget.ReadFramebuffer,
  147. FramebufferAttachment.ColorAttachment0,
  148. _upscaledTexture.Handle,
  149. 0);
  150. }
  151. GL.BlitFramebuffer(
  152. srcX0,
  153. srcY0,
  154. srcX1,
  155. srcY1,
  156. dstX0,
  157. dstY0,
  158. dstX1,
  159. dstY1,
  160. ClearBufferMask.ColorBufferBit,
  161. _isLinear ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest);
  162. // Remove Alpha channel
  163. GL.ColorMask(false, false, false, true);
  164. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  165. GL.Clear(ClearBufferMask.ColorBufferBit);
  166. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  167. {
  168. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  169. }
  170. // Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
  171. GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
  172. GL.Viewport(0, 0, _width, _height);
  173. GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
  174. swapBuffersCallback();
  175. ((Pipeline)_renderer.Pipeline).RestoreClipControl();
  176. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  177. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  178. ((Pipeline)_renderer.Pipeline).RestoreViewport0();
  179. if (viewConverted != view)
  180. {
  181. viewConverted.Dispose();
  182. }
  183. }
  184. private int GetCopyFramebufferHandleLazy()
  185. {
  186. int handle = _copyFramebufferHandle;
  187. if (handle == 0)
  188. {
  189. handle = GL.GenFramebuffer();
  190. _copyFramebufferHandle = handle;
  191. }
  192. return handle;
  193. }
  194. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  195. {
  196. BackgroundContext = new BackgroundContextWorker(baseContext);
  197. _initialized = true;
  198. }
  199. public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  200. {
  201. long size = Math.Abs(4 * width * height);
  202. byte[] bitmap = new byte[size];
  203. GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
  204. _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  205. }
  206. public void Dispose()
  207. {
  208. if (!_initialized)
  209. {
  210. return;
  211. }
  212. BackgroundContext.Dispose();
  213. if (_copyFramebufferHandle != 0)
  214. {
  215. GL.DeleteFramebuffer(_copyFramebufferHandle);
  216. _copyFramebufferHandle = 0;
  217. }
  218. _antiAliasing?.Dispose();
  219. _scalingFilter?.Dispose();
  220. _upscaledTexture?.Dispose();
  221. }
  222. public void SetAntiAliasing(AntiAliasing effect)
  223. {
  224. if (_currentAntiAliasing == effect && _antiAliasing != null)
  225. {
  226. return;
  227. }
  228. _currentAntiAliasing = effect;
  229. _updateEffect = true;
  230. }
  231. public void SetScalingFilter(ScalingFilter type)
  232. {
  233. if (_currentScalingFilter == type && _antiAliasing != null)
  234. {
  235. return;
  236. }
  237. _currentScalingFilter = type;
  238. _updateScalingFilter = true;
  239. }
  240. public void SetColorSpacePassthrough(bool colorSpacePassthroughEnabled) { }
  241. private void UpdateEffect()
  242. {
  243. if (_updateEffect)
  244. {
  245. _updateEffect = false;
  246. switch (_currentAntiAliasing)
  247. {
  248. case AntiAliasing.Fxaa:
  249. _antiAliasing?.Dispose();
  250. _antiAliasing = new FxaaPostProcessingEffect(_renderer);
  251. break;
  252. case AntiAliasing.None:
  253. _antiAliasing?.Dispose();
  254. _antiAliasing = null;
  255. break;
  256. case AntiAliasing.SmaaLow:
  257. case AntiAliasing.SmaaMedium:
  258. case AntiAliasing.SmaaHigh:
  259. case AntiAliasing.SmaaUltra:
  260. var quality = _currentAntiAliasing - AntiAliasing.SmaaLow;
  261. if (_antiAliasing is SmaaPostProcessingEffect smaa)
  262. {
  263. smaa.Quality = quality;
  264. }
  265. else
  266. {
  267. _antiAliasing?.Dispose();
  268. _antiAliasing = new SmaaPostProcessingEffect(_renderer, quality);
  269. }
  270. break;
  271. }
  272. }
  273. if (_updateSize && !_updateScalingFilter)
  274. {
  275. RecreateUpscalingTexture();
  276. }
  277. _updateSize = false;
  278. if (_updateScalingFilter)
  279. {
  280. _updateScalingFilter = false;
  281. switch (_currentScalingFilter)
  282. {
  283. case ScalingFilter.Bilinear:
  284. case ScalingFilter.Nearest:
  285. _scalingFilter?.Dispose();
  286. _scalingFilter = null;
  287. _isLinear = _currentScalingFilter == ScalingFilter.Bilinear;
  288. _upscaledTexture?.Dispose();
  289. _upscaledTexture = null;
  290. break;
  291. case ScalingFilter.Fsr:
  292. if (_scalingFilter is not FsrScalingFilter)
  293. {
  294. _scalingFilter?.Dispose();
  295. _scalingFilter = new FsrScalingFilter(_renderer);
  296. }
  297. _isLinear = false;
  298. _scalingFilter.Level = _scalingFilterLevel;
  299. RecreateUpscalingTexture();
  300. break;
  301. case ScalingFilter.Area:
  302. if (_scalingFilter is not AreaScalingFilter)
  303. {
  304. _scalingFilter?.Dispose();
  305. _scalingFilter = new AreaScalingFilter(_renderer);
  306. }
  307. _isLinear = false;
  308. RecreateUpscalingTexture();
  309. break;
  310. }
  311. }
  312. }
  313. private void RecreateUpscalingTexture(bool forceBgra = false)
  314. {
  315. _upscaledTexture?.Dispose();
  316. var info = new TextureCreateInfo(
  317. _width,
  318. _height,
  319. 1,
  320. 1,
  321. 1,
  322. 1,
  323. 1,
  324. 1,
  325. Format.R8G8B8A8Unorm,
  326. DepthStencilMode.Depth,
  327. Target.Texture2D,
  328. forceBgra ? SwizzleComponent.Blue : SwizzleComponent.Red,
  329. SwizzleComponent.Green,
  330. forceBgra ? SwizzleComponent.Red : SwizzleComponent.Blue,
  331. SwizzleComponent.Alpha);
  332. _isBgra = forceBgra;
  333. _upscaledTexture = _renderer.CreateTexture(info) as TextureView;
  334. }
  335. public void SetScalingFilterLevel(float level)
  336. {
  337. _scalingFilterLevel = level;
  338. _updateScalingFilter = true;
  339. }
  340. }
  341. }