GLScreen.cs 13 KB

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