GLScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. unsafe
  212. {
  213. long HidOffset = Ns.Os.GetVirtHidOffset();
  214. if (HidOffset == 0 || HidOffset + Horizon.HidSize > uint.MaxValue)
  215. {
  216. return;
  217. }
  218. byte* Ptr = (byte*)Ns.Ram + (uint)HidOffset;
  219. int State = 0;
  220. if (Keyboard[OpenTK.Input.Key.Up])
  221. {
  222. State |= 0x2000;
  223. }
  224. if (Keyboard[OpenTK.Input.Key.Down])
  225. {
  226. State |= 0x8000;
  227. }
  228. if (Keyboard[OpenTK.Input.Key.Left])
  229. {
  230. State |= 0x1000;
  231. }
  232. if (Keyboard[OpenTK.Input.Key.Right])
  233. {
  234. State |= 0x4000;
  235. }
  236. if (Keyboard[OpenTK.Input.Key.A])
  237. {
  238. State |= 0x1;
  239. }
  240. if (Keyboard[OpenTK.Input.Key.S])
  241. {
  242. State |= 0x2;
  243. }
  244. if (Keyboard[OpenTK.Input.Key.Z])
  245. {
  246. State |= 0x4;
  247. }
  248. if (Keyboard[OpenTK.Input.Key.X])
  249. {
  250. State |= 0x8;
  251. }
  252. if (Keyboard[OpenTK.Input.Key.Enter])
  253. {
  254. State |= 0x400;
  255. }
  256. if (Keyboard[OpenTK.Input.Key.Tab])
  257. {
  258. State |= 0x800;
  259. }
  260. *((int*)(Ptr + 0xae38)) = (int)State;
  261. }
  262. if (Keyboard[OpenTK.Input.Key.Escape])
  263. {
  264. this.Exit();
  265. }
  266. }
  267. protected override void OnRenderFrame(FrameEventArgs e)
  268. {
  269. GL.Viewport(0, 0, 1280, 720);
  270. Title = $"Ryujinx Screen - (Vsync: {VSync} - FPS: {1f / e.Time:0})";
  271. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  272. RenderFb();
  273. GL.UseProgram(PrgShaderHandle);
  274. Renderer.RunActions();
  275. Renderer.BindTexture(0);
  276. Renderer.Render();
  277. SwapBuffers();
  278. }
  279. void RenderFb()
  280. {
  281. GL.ActiveTexture(TextureUnit.Texture0);
  282. GL.BindTexture(TextureTarget.Texture2D, ScreenTex.Texture);
  283. GL.BindVertexArray(VaoHandle);
  284. GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
  285. }
  286. }
  287. }