Window.cs 5.6 KB

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