GLScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. layout(location = 0) in vec3 in_position;
  114. layout(location = 1) in vec4 in_color;
  115. layout(location = 2) in vec2 in_tex_coord;
  116. out vec4 color;
  117. out vec2 tex_coord;
  118. void main(void) {
  119. color = in_color;
  120. tex_coord = in_tex_coord;
  121. gl_Position = vec4((in_position + vec3(-960, 270, 0)) / vec3(1920, 270, 1), 1);
  122. }";
  123. private string FragShaderSource = @"
  124. #version 330 core
  125. precision highp float;
  126. uniform sampler2D tex;
  127. in vec4 color;
  128. in vec2 tex_coord;
  129. out vec4 out_frag_color;
  130. void main(void) {
  131. out_frag_color = vec4(texture(tex, tex_coord).rgb, color.a);
  132. }";
  133. private int VtxShaderHandle,
  134. FragShaderHandle,
  135. PrgShaderHandle;
  136. private int VaoHandle;
  137. private int VboHandle;
  138. private Switch Ns;
  139. private IGalRenderer Renderer;
  140. private ScreenTexture ScreenTex;
  141. public GLScreen(Switch Ns, IGalRenderer Renderer)
  142. : base(1280, 720,
  143. new GraphicsMode(), "Ryujinx", 0,
  144. DisplayDevice.Default, 3, 3,
  145. GraphicsContextFlags.ForwardCompatible)
  146. {
  147. this.Ns = Ns;
  148. this.Renderer = Renderer;
  149. ScreenTex = new ScreenTexture(Ns, Renderer, 1280, 720);
  150. }
  151. protected override void OnLoad(EventArgs e)
  152. {
  153. VSync = VSyncMode.On;
  154. CreateShaders();
  155. CreateVbo();
  156. GL.Enable(EnableCap.Blend);
  157. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  158. }
  159. protected override void OnUnload(EventArgs e)
  160. {
  161. ScreenTex.Dispose();
  162. GL.DeleteVertexArray(VaoHandle);
  163. GL.DeleteBuffer(VboHandle);
  164. }
  165. private void CreateVbo()
  166. {
  167. VaoHandle = GL.GenVertexArray();
  168. VboHandle = GL.GenBuffer();
  169. uint[] Buffer = new uint[]
  170. {
  171. 0xc4700000, 0x80000000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x00000000,
  172. 0x45340000, 0x80000000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x00000000,
  173. 0xc4700000, 0xc4070000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x3f800000,
  174. 0x45340000, 0xc4070000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x3f800000
  175. };
  176. IntPtr Length = new IntPtr(Buffer.Length * 4);
  177. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  178. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  179. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  180. GL.BindVertexArray(VaoHandle);
  181. GL.EnableVertexAttribArray(0);
  182. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  183. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 28, 0);
  184. GL.EnableVertexAttribArray(1);
  185. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  186. GL.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, false, 28, 12);
  187. GL.EnableVertexAttribArray(2);
  188. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  189. GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 28, 20);
  190. GL.BindVertexArray(0);
  191. }
  192. private void CreateShaders()
  193. {
  194. VtxShaderHandle = GL.CreateShader(ShaderType.VertexShader);
  195. FragShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
  196. GL.ShaderSource(VtxShaderHandle, VtxShaderSource);
  197. GL.ShaderSource(FragShaderHandle, FragShaderSource);
  198. GL.CompileShader(VtxShaderHandle);
  199. GL.CompileShader(FragShaderHandle);
  200. PrgShaderHandle = GL.CreateProgram();
  201. GL.AttachShader(PrgShaderHandle, VtxShaderHandle);
  202. GL.AttachShader(PrgShaderHandle, FragShaderHandle);
  203. GL.LinkProgram(PrgShaderHandle);
  204. GL.UseProgram(PrgShaderHandle);
  205. int TexLocation = GL.GetUniformLocation(PrgShaderHandle, "tex");
  206. GL.Uniform1(TexLocation, 0);
  207. }
  208. protected override void OnUpdateFrame(FrameEventArgs e)
  209. {
  210. HidControllerKeys CurrentButton = 0;
  211. JoystickPosition LeftJoystick;
  212. JoystickPosition RightJoystick;
  213. if (Keyboard[OpenTK.Input.Key.Escape]) this.Exit();
  214. //RightJoystick
  215. int LeftJoystickDX = 0;
  216. int LeftJoystickDY = 0;
  217. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  218. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  219. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  220. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  221. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickButton]) CurrentButton |= HidControllerKeys.KEY_LSTICK;
  222. //LeftButtons
  223. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadUp]) CurrentButton |= HidControllerKeys.KEY_DUP;
  224. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadDown]) CurrentButton |= HidControllerKeys.KEY_DDOWN;
  225. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadLeft]) CurrentButton |= HidControllerKeys.KEY_DLEFT;
  226. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadRight]) CurrentButton |= HidControllerKeys.KEY_DRIGHT;
  227. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonMinus]) CurrentButton |= HidControllerKeys.KEY_MINUS;
  228. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonL]) CurrentButton |= HidControllerKeys.KEY_L;
  229. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonZL]) CurrentButton |= HidControllerKeys.KEY_ZL;
  230. //RightJoystick
  231. int RightJoystickDX = 0;
  232. int RightJoystickDY = 0;
  233. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickUp]) RightJoystickDY = short.MaxValue;
  234. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  235. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  236. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue;
  237. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerKeys.KEY_RSTICK;
  238. //RightButtons
  239. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerKeys.KEY_A;
  240. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonB]) CurrentButton |= HidControllerKeys.KEY_B;
  241. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonX]) CurrentButton |= HidControllerKeys.KEY_X;
  242. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonY]) CurrentButton |= HidControllerKeys.KEY_Y;
  243. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonPlus]) CurrentButton |= HidControllerKeys.KEY_PLUS;
  244. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonR]) CurrentButton |= HidControllerKeys.KEY_R;
  245. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonZR]) CurrentButton |= HidControllerKeys.KEY_ZR;
  246. LeftJoystick = new JoystickPosition
  247. {
  248. DX = LeftJoystickDX,
  249. DY = LeftJoystickDY
  250. };
  251. RightJoystick = new JoystickPosition
  252. {
  253. DX = RightJoystickDX,
  254. DY = RightJoystickDY
  255. };
  256. //We just need one pair of JoyCon because it's emulate by the keyboard.
  257. Ns.Hid.SendControllerButtons(HidControllerID.CONTROLLER_HANDHELD, HidControllerLayouts.Main, CurrentButton, LeftJoystick, RightJoystick);
  258. }
  259. protected override void OnRenderFrame(FrameEventArgs e)
  260. {
  261. GL.Viewport(0, 0, 1280, 720);
  262. Title = $"Ryujinx Screen - (Vsync: {VSync} - FPS: {1f / e.Time:0})";
  263. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  264. RenderFb();
  265. GL.UseProgram(PrgShaderHandle);
  266. Renderer.RunActions();
  267. Renderer.BindTexture(0);
  268. Renderer.Render();
  269. SwapBuffers();
  270. }
  271. void RenderFb()
  272. {
  273. GL.ActiveTexture(TextureUnit.Texture0);
  274. GL.BindTexture(TextureTarget.Texture2D, ScreenTex.Texture);
  275. GL.BindVertexArray(VaoHandle);
  276. GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
  277. }
  278. }
  279. }