GLScreen.cs 13 KB

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