GLScreen.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // This code was written for the OpenTK library and has been released
  2. // to the Public Domain.
  3. // It is provided "as is" without express or implied warranty of any kind.
  4. using OpenTK;
  5. using OpenTK.Graphics;
  6. using OpenTK.Graphics.OpenGL;
  7. using Ryujinx.Core;
  8. using Ryujinx.Graphics.Gal;
  9. using System;
  10. namespace Ryujinx
  11. {
  12. public class GLScreen : GameWindow
  13. {
  14. class ScreenTexture : IDisposable
  15. {
  16. private Switch Ns;
  17. private IGalRenderer Renderer;
  18. private int Width;
  19. private int Height;
  20. private int TexHandle;
  21. private int[] Pixels;
  22. public ScreenTexture(Switch Ns, IGalRenderer Renderer, int Width, int Height)
  23. {
  24. this.Ns = Ns;
  25. this.Renderer = Renderer;
  26. this.Width = Width;
  27. this.Height = Height;
  28. Pixels = new int[Width * Height];
  29. TexHandle = GL.GenTexture();
  30. GL.BindTexture(TextureTarget.Texture2D, TexHandle);
  31. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  32. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  33. GL.TexImage2D(TextureTarget.Texture2D,
  34. 0,
  35. PixelInternalFormat.Rgba,
  36. Width,
  37. Height,
  38. 0,
  39. PixelFormat.Rgba,
  40. PixelType.UnsignedByte,
  41. IntPtr.Zero);
  42. }
  43. public int Texture
  44. {
  45. get
  46. {
  47. UploadBitmap();
  48. return TexHandle;
  49. }
  50. }
  51. unsafe void UploadBitmap()
  52. {
  53. int FbSize = Width * Height * 4;
  54. if (Renderer.FrameBufferPtr == 0 || Renderer.FrameBufferPtr + FbSize > uint.MaxValue)
  55. {
  56. return;
  57. }
  58. byte* SrcPtr = (byte*)Ns.Ram + (uint)Renderer.FrameBufferPtr;
  59. for (int Y = 0; Y < Height; Y++)
  60. {
  61. for (int X = 0; X < Width; X++)
  62. {
  63. int SrcOffs = GetSwizzleOffset(X, Y, 4);
  64. Pixels[X + Y * Width] = *((int*)(SrcPtr + SrcOffs));
  65. }
  66. }
  67. GL.BindTexture(TextureTarget.Texture2D, TexHandle);
  68. GL.TexSubImage2D(TextureTarget.Texture2D,
  69. 0,
  70. 0,
  71. 0,
  72. Width,
  73. Height,
  74. PixelFormat.Rgba,
  75. PixelType.UnsignedByte,
  76. Pixels);
  77. }
  78. private int GetSwizzleOffset(int X, int Y, int Bpp)
  79. {
  80. int Pos;
  81. Pos = (Y & 0x7f) >> 4;
  82. Pos += (X >> 4) << 3;
  83. Pos += (Y >> 7) * ((Width >> 4) << 3);
  84. Pos *= 1024;
  85. Pos += ((Y & 0xf) >> 3) << 9;
  86. Pos += ((X & 0xf) >> 3) << 8;
  87. Pos += ((Y & 0x7) >> 1) << 6;
  88. Pos += ((X & 0x7) >> 2) << 5;
  89. Pos += ((Y & 0x1) >> 0) << 4;
  90. Pos += ((X & 0x3) >> 0) << 2;
  91. return Pos;
  92. }
  93. private bool disposed;
  94. public void Dispose()
  95. {
  96. Dispose(true);
  97. GC.SuppressFinalize(this);
  98. }
  99. void Dispose(bool disposing)
  100. {
  101. if (!disposed)
  102. {
  103. if (disposing)
  104. {
  105. GL.DeleteTexture(TexHandle);
  106. }
  107. disposed = true;
  108. }
  109. }
  110. }
  111. private string VtxShaderSource = @"
  112. #version 330 core
  113. precision highp float;
  114. uniform vec2 window_size;
  115. layout(location = 0) in vec3 in_position;
  116. layout(location = 1) in vec4 in_color;
  117. layout(location = 2) in vec2 in_tex_coord;
  118. out vec4 color;
  119. out vec2 tex_coord;
  120. // Have a fixed aspect ratio, fit the image within the available space.
  121. vec3 get_scale_ratio() {
  122. vec2 native_size = vec2(1280, 720);
  123. vec2 ratio = vec2(
  124. (window_size.y * native_size.x) / (native_size.y * window_size.x),
  125. (window_size.x * native_size.y) / (native_size.x * window_size.y)
  126. );
  127. return vec3(min(ratio, vec2(1, 1)) * vec2(1, -1), 1);
  128. }
  129. void main(void) {
  130. color = in_color;
  131. tex_coord = in_tex_coord;
  132. gl_Position = vec4(in_position * get_scale_ratio(), 1);
  133. }";
  134. private string FragShaderSource = @"
  135. #version 330 core
  136. precision highp float;
  137. uniform sampler2D tex;
  138. in vec4 color;
  139. in vec2 tex_coord;
  140. out vec4 out_frag_color;
  141. void main(void) {
  142. out_frag_color = vec4(texture(tex, tex_coord).rgb, color.a);
  143. }";
  144. private int VtxShaderHandle,
  145. FragShaderHandle,
  146. PrgShaderHandle;
  147. private int WindowSizeUniformLocation;
  148. private int VaoHandle;
  149. private int VboHandle;
  150. private Switch Ns;
  151. private IGalRenderer Renderer;
  152. private ScreenTexture ScreenTex;
  153. public GLScreen(Switch Ns, IGalRenderer Renderer)
  154. : base(1280, 720,
  155. new GraphicsMode(), "Ryujinx", 0,
  156. DisplayDevice.Default, 3, 3,
  157. GraphicsContextFlags.ForwardCompatible)
  158. {
  159. this.Ns = Ns;
  160. this.Renderer = Renderer;
  161. ScreenTex = new ScreenTexture(Ns, Renderer, 1280, 720);
  162. }
  163. protected override void OnLoad(EventArgs e)
  164. {
  165. VSync = VSyncMode.On;
  166. CreateShaders();
  167. CreateVbo();
  168. GL.Enable(EnableCap.Blend);
  169. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  170. }
  171. protected override void OnUnload(EventArgs e)
  172. {
  173. ScreenTex.Dispose();
  174. GL.DeleteVertexArray(VaoHandle);
  175. GL.DeleteBuffer(VboHandle);
  176. }
  177. private void CreateVbo()
  178. {
  179. VaoHandle = GL.GenVertexArray();
  180. VboHandle = GL.GenBuffer();
  181. uint[] Buffer = new uint[]
  182. {
  183. 0xbf800000, 0x3f800000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x00000000,
  184. 0x3f800000, 0x3f800000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x00000000,
  185. 0xbf800000, 0xbf800000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x3f800000,
  186. 0x3f800000, 0xbf800000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x3f800000
  187. };
  188. IntPtr Length = new IntPtr(Buffer.Length * 4);
  189. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  190. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  191. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  192. GL.BindVertexArray(VaoHandle);
  193. GL.EnableVertexAttribArray(0);
  194. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  195. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 28, 0);
  196. GL.EnableVertexAttribArray(1);
  197. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  198. GL.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, false, 28, 12);
  199. GL.EnableVertexAttribArray(2);
  200. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  201. GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 28, 20);
  202. GL.BindVertexArray(0);
  203. }
  204. private void CreateShaders()
  205. {
  206. VtxShaderHandle = GL.CreateShader(ShaderType.VertexShader);
  207. FragShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
  208. GL.ShaderSource(VtxShaderHandle, VtxShaderSource);
  209. GL.ShaderSource(FragShaderHandle, FragShaderSource);
  210. GL.CompileShader(VtxShaderHandle);
  211. GL.CompileShader(FragShaderHandle);
  212. PrgShaderHandle = GL.CreateProgram();
  213. GL.AttachShader(PrgShaderHandle, VtxShaderHandle);
  214. GL.AttachShader(PrgShaderHandle, FragShaderHandle);
  215. GL.LinkProgram(PrgShaderHandle);
  216. GL.UseProgram(PrgShaderHandle);
  217. int TexLocation = GL.GetUniformLocation(PrgShaderHandle, "tex");
  218. GL.Uniform1(TexLocation, 0);
  219. WindowSizeUniformLocation = GL.GetUniformLocation(PrgShaderHandle, "window_size");
  220. GL.Uniform2(WindowSizeUniformLocation, new Vector2(1280.0f, 720.0f));
  221. }
  222. protected override void OnUpdateFrame(FrameEventArgs e)
  223. {
  224. HidControllerKeys CurrentButton = 0;
  225. JoystickPosition LeftJoystick;
  226. JoystickPosition RightJoystick;
  227. if (Keyboard[OpenTK.Input.Key.Escape]) this.Exit();
  228. //RightJoystick
  229. int LeftJoystickDX = 0;
  230. int LeftJoystickDY = 0;
  231. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  232. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  233. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  234. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  235. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickButton]) CurrentButton |= HidControllerKeys.KEY_LSTICK;
  236. //LeftButtons
  237. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadUp]) CurrentButton |= HidControllerKeys.KEY_DUP;
  238. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadDown]) CurrentButton |= HidControllerKeys.KEY_DDOWN;
  239. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadLeft]) CurrentButton |= HidControllerKeys.KEY_DLEFT;
  240. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadRight]) CurrentButton |= HidControllerKeys.KEY_DRIGHT;
  241. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonMinus]) CurrentButton |= HidControllerKeys.KEY_MINUS;
  242. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonL]) CurrentButton |= HidControllerKeys.KEY_L;
  243. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonZL]) CurrentButton |= HidControllerKeys.KEY_ZL;
  244. //RightJoystick
  245. int RightJoystickDX = 0;
  246. int RightJoystickDY = 0;
  247. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickUp]) RightJoystickDY = short.MaxValue;
  248. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  249. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  250. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue;
  251. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerKeys.KEY_RSTICK;
  252. //RightButtons
  253. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerKeys.KEY_A;
  254. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonB]) CurrentButton |= HidControllerKeys.KEY_B;
  255. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonX]) CurrentButton |= HidControllerKeys.KEY_X;
  256. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonY]) CurrentButton |= HidControllerKeys.KEY_Y;
  257. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonPlus]) CurrentButton |= HidControllerKeys.KEY_PLUS;
  258. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonR]) CurrentButton |= HidControllerKeys.KEY_R;
  259. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonZR]) CurrentButton |= HidControllerKeys.KEY_ZR;
  260. LeftJoystick = new JoystickPosition
  261. {
  262. DX = LeftJoystickDX,
  263. DY = LeftJoystickDY
  264. };
  265. RightJoystick = new JoystickPosition
  266. {
  267. DX = RightJoystickDX,
  268. DY = RightJoystickDY
  269. };
  270. //Get screen touch position from left mouse click
  271. //Opentk always captures mouse events, even if out of focus, so check if window is focused.
  272. if (Mouse != null && Focused)
  273. if (Mouse.GetState().LeftButton == OpenTK.Input.ButtonState.Pressed)
  274. {
  275. HidTouchScreenEntryTouch CurrentPoint = new HidTouchScreenEntryTouch
  276. {
  277. Timestamp = (uint)Environment.TickCount,
  278. X = (uint)Mouse.X,
  279. Y = (uint)Mouse.Y,
  280. //Placeholder values till more data is acquired
  281. DiameterX = 10,
  282. DiameterY = 10,
  283. Angle = 90,
  284. //Only support single touch
  285. TouchIndex = 0,
  286. };
  287. if (Mouse.X > -1 && Mouse.Y > -1)
  288. Ns.SendTouchScreenEntry(CurrentPoint);
  289. }
  290. //We just need one pair of JoyCon because it's emulate by the keyboard.
  291. Ns.SendControllerButtons(HidControllerID.CONTROLLER_HANDHELD, HidControllerLayouts.Main, CurrentButton, LeftJoystick, RightJoystick);
  292. }
  293. protected override void OnRenderFrame(FrameEventArgs e)
  294. {
  295. GL.Viewport(0, 0, Width, Height);
  296. Title = $"Ryujinx Screen - (Vsync: {VSync} - FPS: {1f / e.Time:0})";
  297. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  298. RenderFb();
  299. GL.UseProgram(PrgShaderHandle);
  300. Renderer.RunActions();
  301. Renderer.BindTexture(0);
  302. Renderer.Render();
  303. SwapBuffers();
  304. }
  305. protected override void OnResize(EventArgs e)
  306. {
  307. GL.UseProgram(PrgShaderHandle);
  308. GL.Uniform2(WindowSizeUniformLocation, new Vector2(Width, Height));
  309. }
  310. void RenderFb()
  311. {
  312. GL.ActiveTexture(TextureUnit.Texture0);
  313. GL.BindTexture(TextureTarget.Texture2D, ScreenTex.Texture);
  314. GL.BindVertexArray(VaoHandle);
  315. GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
  316. }
  317. }
  318. }