Window.cs 5.8 KB

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