Window.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.OpenGL
  6. {
  7. class Window : IWindow
  8. {
  9. private const int NativeWidth = 1280;
  10. private const int NativeHeight = 720;
  11. private int _width;
  12. private int _height;
  13. private int _resizeWidth;
  14. private int _resizeHeight;
  15. private bool _sizeChanged;
  16. private object _resizeLocker;
  17. private int _blitFramebufferHandle;
  18. private int _copyFramebufferHandle;
  19. private int _screenTextureHandle;
  20. private TextureReleaseCallback _release;
  21. private struct PresentationTexture
  22. {
  23. public TextureView Texture { get; }
  24. public ImageCrop Crop { get; }
  25. public object Context { get; }
  26. public PresentationTexture(TextureView texture, ImageCrop crop, object context)
  27. {
  28. Texture = texture;
  29. Crop = crop;
  30. Context = context;
  31. }
  32. }
  33. private Queue<PresentationTexture> _textures;
  34. public Window()
  35. {
  36. _width = NativeWidth;
  37. _height = NativeHeight;
  38. _resizeLocker = new object();
  39. _textures = new Queue<PresentationTexture>();
  40. }
  41. public void Present()
  42. {
  43. GL.Disable(EnableCap.FramebufferSrgb);
  44. CopyTextureFromQueue();
  45. int oldReadFramebufferHandle = GL.GetInteger(GetPName.ReadFramebufferBinding);
  46. int oldDrawFramebufferHandle = GL.GetInteger(GetPName.DrawFramebufferBinding);
  47. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
  48. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetCopyFramebufferHandleLazy());
  49. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  50. GL.Clear(ClearBufferMask.ColorBufferBit);
  51. int windowWidth = _width;
  52. int windowHeight = _height;
  53. GL.BlitFramebuffer(
  54. 0,
  55. 0,
  56. windowWidth,
  57. windowHeight,
  58. 0,
  59. 0,
  60. windowWidth,
  61. windowHeight,
  62. ClearBufferMask.ColorBufferBit,
  63. BlitFramebufferFilter.Linear);
  64. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  65. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  66. GL.Enable(EnableCap.FramebufferSrgb);
  67. }
  68. private void CopyTextureFromQueue()
  69. {
  70. if (!_textures.TryDequeue(out PresentationTexture presentationTexture))
  71. {
  72. return;
  73. }
  74. TextureView texture = presentationTexture.Texture;
  75. ImageCrop crop = presentationTexture.Crop;
  76. object context = presentationTexture.Context;
  77. int oldReadFramebufferHandle = GL.GetInteger(GetPName.ReadFramebufferBinding);
  78. int oldDrawFramebufferHandle = GL.GetInteger(GetPName.DrawFramebufferBinding);
  79. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetCopyFramebufferHandleLazy());
  80. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetBlitFramebufferHandleLazy());
  81. GL.FramebufferTexture(
  82. FramebufferTarget.ReadFramebuffer,
  83. FramebufferAttachment.ColorAttachment0,
  84. texture.Handle,
  85. 0);
  86. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  87. GL.Clear(ClearBufferMask.ColorBufferBit);
  88. int srcX0, srcX1, srcY0, srcY1;
  89. if (crop.Left == 0 && crop.Right == 0)
  90. {
  91. srcX0 = 0;
  92. srcX1 = texture.Width;
  93. }
  94. else
  95. {
  96. srcX0 = crop.Left;
  97. srcX1 = crop.Right;
  98. }
  99. if (crop.Top == 0 && crop.Bottom == 0)
  100. {
  101. srcY0 = 0;
  102. srcY1 = texture.Height;
  103. }
  104. else
  105. {
  106. srcY0 = crop.Top;
  107. srcY1 = crop.Bottom;
  108. }
  109. float ratioX = MathF.Min(1f, (_height * (float)NativeWidth) / ((float)NativeHeight * _width));
  110. float ratioY = MathF.Min(1f, (_width * (float)NativeHeight) / ((float)NativeWidth * _height));
  111. int dstWidth = (int)(_width * ratioX);
  112. int dstHeight = (int)(_height * ratioY);
  113. int dstPaddingX = (_width - dstWidth) / 2;
  114. int dstPaddingY = (_height - dstHeight) / 2;
  115. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  116. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  117. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  118. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  119. GL.BlitFramebuffer(
  120. srcX0,
  121. srcY0,
  122. srcX1,
  123. srcY1,
  124. dstX0,
  125. dstY0,
  126. dstX1,
  127. dstY1,
  128. ClearBufferMask.ColorBufferBit,
  129. BlitFramebufferFilter.Linear);
  130. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  131. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  132. texture.Release();
  133. Release(context);
  134. }
  135. public void QueueTexture(ITexture texture, ImageCrop crop, object context)
  136. {
  137. if (texture == null)
  138. {
  139. Release(context);
  140. return;
  141. }
  142. TextureView textureView = (TextureView)texture;
  143. textureView.Acquire();
  144. _textures.Enqueue(new PresentationTexture(textureView, crop, context));
  145. }
  146. public void RegisterTextureReleaseCallback(TextureReleaseCallback callback)
  147. {
  148. _release = callback;
  149. }
  150. public void SetSize(int width, int height)
  151. {
  152. lock (_resizeLocker)
  153. {
  154. _resizeWidth = width;
  155. _resizeHeight = height;
  156. _sizeChanged = true;
  157. }
  158. }
  159. private void Release(object context)
  160. {
  161. if (_release != null)
  162. {
  163. _release(context);
  164. }
  165. }
  166. private int GetBlitFramebufferHandleLazy()
  167. {
  168. int handle = _blitFramebufferHandle;
  169. if (handle == 0)
  170. {
  171. handle = GL.GenFramebuffer();
  172. _blitFramebufferHandle = handle;
  173. }
  174. return handle;
  175. }
  176. private int GetCopyFramebufferHandleLazy()
  177. {
  178. int handle = _copyFramebufferHandle;
  179. void GenerateAndBindTexture()
  180. {
  181. int textureHandle = GenerateWindowTexture();
  182. GL.BindFramebuffer(FramebufferTarget.Framebuffer, handle);
  183. GL.FramebufferTexture(
  184. FramebufferTarget.Framebuffer,
  185. FramebufferAttachment.ColorAttachment0,
  186. textureHandle,
  187. 0);
  188. _screenTextureHandle = textureHandle;
  189. }
  190. if (handle == 0)
  191. {
  192. handle = GL.GenFramebuffer();
  193. _copyFramebufferHandle = handle;
  194. GenerateAndBindTexture();
  195. }
  196. else if (_sizeChanged)
  197. {
  198. GL.DeleteTexture(_screenTextureHandle);
  199. lock (_resizeLocker)
  200. {
  201. _width = _resizeWidth;
  202. _height = _resizeHeight;
  203. _sizeChanged = false;
  204. }
  205. GenerateAndBindTexture();
  206. }
  207. return handle;
  208. }
  209. private int GenerateWindowTexture()
  210. {
  211. int handle = GL.GenTexture();
  212. GL.BindTexture(TextureTarget.Texture2D, handle);
  213. GL.TexImage2D(
  214. TextureTarget.Texture2D,
  215. 0,
  216. PixelInternalFormat.Rgba8,
  217. _width,
  218. _height,
  219. 0,
  220. PixelFormat.Rgba,
  221. PixelType.UnsignedByte,
  222. IntPtr.Zero);
  223. return handle;
  224. }
  225. }
  226. }