Window.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 readonly Renderer _renderer;
  10. private int _width;
  11. private int _height;
  12. private int _copyFramebufferHandle;
  13. internal BackgroundContextWorker BackgroundContext { get; private set; }
  14. public Window(Renderer renderer)
  15. {
  16. _renderer = renderer;
  17. }
  18. public void Present(ITexture texture, ImageCrop crop)
  19. {
  20. GL.Disable(EnableCap.FramebufferSrgb);
  21. CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop);
  22. GL.Enable(EnableCap.FramebufferSrgb);
  23. }
  24. public void SetSize(int width, int height)
  25. {
  26. _width = width;
  27. _height = height;
  28. }
  29. private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop)
  30. {
  31. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  32. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
  33. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
  34. TextureView viewConverted = view.Format.IsBgra8() ? _renderer.TextureCopy.BgraSwap(view) : view;
  35. GL.FramebufferTexture(
  36. FramebufferTarget.ReadFramebuffer,
  37. FramebufferAttachment.ColorAttachment0,
  38. viewConverted.Handle,
  39. 0);
  40. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  41. GL.Disable(EnableCap.RasterizerDiscard);
  42. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  43. GL.Clear(ClearBufferMask.ColorBufferBit);
  44. int srcX0, srcX1, srcY0, srcY1;
  45. float scale = view.ScaleFactor;
  46. if (crop.Left == 0 && crop.Right == 0)
  47. {
  48. srcX0 = 0;
  49. srcX1 = (int)(view.Width / scale);
  50. }
  51. else
  52. {
  53. srcX0 = crop.Left;
  54. srcX1 = crop.Right;
  55. }
  56. if (crop.Top == 0 && crop.Bottom == 0)
  57. {
  58. srcY0 = 0;
  59. srcY1 = (int)(view.Height / scale);
  60. }
  61. else
  62. {
  63. srcY0 = crop.Top;
  64. srcY1 = crop.Bottom;
  65. }
  66. if (scale != 1f)
  67. {
  68. srcX0 = (int)(srcX0 * scale);
  69. srcY0 = (int)(srcY0 * scale);
  70. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  71. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  72. }
  73. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  74. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  75. int dstWidth = (int)(_width * ratioX);
  76. int dstHeight = (int)(_height * ratioY);
  77. int dstPaddingX = (_width - dstWidth) / 2;
  78. int dstPaddingY = (_height - dstHeight) / 2;
  79. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  80. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  81. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  82. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  83. GL.BlitFramebuffer(
  84. srcX0,
  85. srcY0,
  86. srcX1,
  87. srcY1,
  88. dstX0,
  89. dstY0,
  90. dstX1,
  91. dstY1,
  92. ClearBufferMask.ColorBufferBit,
  93. BlitFramebufferFilter.Linear);
  94. // Remove Alpha channel
  95. GL.ColorMask(false, false, false, true);
  96. GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  97. GL.Clear(ClearBufferMask.ColorBufferBit);
  98. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  99. {
  100. ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
  101. }
  102. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  103. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  104. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  105. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  106. if (viewConverted != view)
  107. {
  108. viewConverted.Dispose();
  109. }
  110. }
  111. private int GetCopyFramebufferHandleLazy()
  112. {
  113. int handle = _copyFramebufferHandle;
  114. if (handle == 0)
  115. {
  116. handle = GL.GenFramebuffer();
  117. _copyFramebufferHandle = handle;
  118. }
  119. return handle;
  120. }
  121. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  122. {
  123. BackgroundContext = new BackgroundContextWorker(baseContext);
  124. }
  125. public void Dispose()
  126. {
  127. BackgroundContext.Dispose();
  128. if (_copyFramebufferHandle != 0)
  129. {
  130. GL.DeleteFramebuffer(_copyFramebufferHandle);
  131. _copyFramebufferHandle = 0;
  132. }
  133. }
  134. }
  135. }