Window.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class Window : IWindow
  7. {
  8. private const int NativeWidth = 1280;
  9. private const int NativeHeight = 720;
  10. private int _width;
  11. private int _height;
  12. private int _copyFramebufferHandle;
  13. public Window()
  14. {
  15. _width = NativeWidth;
  16. _height = NativeHeight;
  17. }
  18. public void Present(ITexture texture, ImageCrop crop)
  19. {
  20. TextureView view = (TextureView)texture;
  21. GL.Disable(EnableCap.FramebufferSrgb);
  22. int oldReadFramebufferHandle = GL.GetInteger(GetPName.ReadFramebufferBinding);
  23. int oldDrawFramebufferHandle = GL.GetInteger(GetPName.DrawFramebufferBinding);
  24. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
  25. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetCopyFramebufferHandleLazy());
  26. GL.FramebufferTexture(
  27. FramebufferTarget.ReadFramebuffer,
  28. FramebufferAttachment.ColorAttachment0,
  29. view.Handle,
  30. 0);
  31. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  32. GL.Clear(ClearBufferMask.ColorBufferBit);
  33. int srcX0, srcX1, srcY0, srcY1;
  34. if (crop.Left == 0 && crop.Right == 0)
  35. {
  36. srcX0 = 0;
  37. srcX1 = view.Width;
  38. }
  39. else
  40. {
  41. srcX0 = crop.Left;
  42. srcX1 = crop.Right;
  43. }
  44. if (crop.Top == 0 && crop.Bottom == 0)
  45. {
  46. srcY0 = 0;
  47. srcY1 = view.Height;
  48. }
  49. else
  50. {
  51. srcY0 = crop.Top;
  52. srcY1 = crop.Bottom;
  53. }
  54. float ratioX = MathF.Min(1f, (_height * (float)NativeWidth) / ((float)NativeHeight * _width));
  55. float ratioY = MathF.Min(1f, (_width * (float)NativeHeight) / ((float)NativeWidth * _height));
  56. int dstWidth = (int)(_width * ratioX);
  57. int dstHeight = (int)(_height * ratioY);
  58. int dstPaddingX = (_width - dstWidth) / 2;
  59. int dstPaddingY = (_height - dstHeight) / 2;
  60. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  61. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  62. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  63. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  64. GL.BlitFramebuffer(
  65. srcX0,
  66. srcY0,
  67. srcX1,
  68. srcY1,
  69. dstX0,
  70. dstY0,
  71. dstX1,
  72. dstY1,
  73. ClearBufferMask.ColorBufferBit,
  74. BlitFramebufferFilter.Linear);
  75. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  76. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  77. GL.Enable(EnableCap.FramebufferSrgb);
  78. }
  79. public void SetSize(int width, int height)
  80. {
  81. _width = width;
  82. _height = height;
  83. }
  84. private int GetCopyFramebufferHandleLazy()
  85. {
  86. int handle = _copyFramebufferHandle;
  87. if (handle == 0)
  88. {
  89. handle = GL.GenFramebuffer();
  90. _copyFramebufferHandle = handle;
  91. }
  92. return handle;
  93. }
  94. }
  95. }