GLScreen.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. if (Renderer.FrameBufferPtr == 0)
  53. {
  54. return;
  55. }
  56. byte* SrcPtr = (byte*)IntPtr.Add(Ns.Ram, (int)Renderer.FrameBufferPtr);
  57. for (int Y = 0; Y < Height; Y++)
  58. {
  59. for (int X = 0; X < Width; X++)
  60. {
  61. int SrcOffs = GetSwizzleOffset(X, Y, 4);
  62. Pixels[X + Y * Width] = *((int*)(SrcPtr + SrcOffs));
  63. }
  64. }
  65. GL.BindTexture(TextureTarget.Texture2D, TexHandle);
  66. GL.TexSubImage2D(TextureTarget.Texture2D,
  67. 0,
  68. 0,
  69. 0,
  70. Width,
  71. Height,
  72. PixelFormat.Rgba,
  73. PixelType.UnsignedByte,
  74. Pixels);
  75. }
  76. private int GetSwizzleOffset(int X, int Y, int Bpp)
  77. {
  78. int Pos;
  79. Pos = (Y & 0x7f) >> 4;
  80. Pos += (X >> 4) << 3;
  81. Pos += (Y >> 7) * ((Width >> 4) << 3);
  82. Pos *= 1024;
  83. Pos += ((Y & 0xf) >> 3) << 9;
  84. Pos += ((X & 0xf) >> 3) << 8;
  85. Pos += ((Y & 0x7) >> 1) << 6;
  86. Pos += ((X & 0x7) >> 2) << 5;
  87. Pos += ((Y & 0x1) >> 0) << 4;
  88. Pos += ((X & 0x3) >> 0) << 2;
  89. return Pos;
  90. }
  91. private bool disposed;
  92. public void Dispose()
  93. {
  94. Dispose(true);
  95. GC.SuppressFinalize(this);
  96. }
  97. void Dispose(bool disposing)
  98. {
  99. if (!disposed)
  100. {
  101. if (disposing)
  102. {
  103. GL.DeleteTexture(TexHandle);
  104. }
  105. disposed = true;
  106. }
  107. }
  108. }
  109. private string VtxShaderSource = @"
  110. #version 330 core
  111. precision highp float;
  112. layout(location = 0) in vec3 in_position;
  113. layout(location = 1) in vec4 in_color;
  114. layout(location = 2) in vec2 in_tex_coord;
  115. out vec4 color;
  116. out vec2 tex_coord;
  117. void main(void) {
  118. color = in_color;
  119. tex_coord = in_tex_coord;
  120. gl_Position = vec4((in_position + vec3(-960, 270, 0)) / vec3(1920, 270, 1), 1);
  121. }";
  122. private string FragShaderSource = @"
  123. #version 330 core
  124. precision highp float;
  125. uniform sampler2D tex;
  126. in vec4 color;
  127. in vec2 tex_coord;
  128. out vec4 out_frag_color;
  129. void main(void) {
  130. out_frag_color = vec4(texture(tex, tex_coord).rgb, color.a);
  131. }";
  132. private int VtxShaderHandle,
  133. FragShaderHandle,
  134. PrgShaderHandle;
  135. private int VaoHandle;
  136. private int VboHandle;
  137. private Switch Ns;
  138. private IGalRenderer Renderer;
  139. private ScreenTexture ScreenTex;
  140. public GLScreen(Switch Ns, IGalRenderer Renderer)
  141. : base(1280, 720,
  142. new GraphicsMode(), "Ryujinx", 0,
  143. DisplayDevice.Default, 3, 3,
  144. GraphicsContextFlags.ForwardCompatible)
  145. {
  146. this.Ns = Ns;
  147. this.Renderer = Renderer;
  148. ScreenTex = new ScreenTexture(Ns, Renderer, 1280, 720);
  149. }
  150. protected override void OnLoad (EventArgs e)
  151. {
  152. VSync = VSyncMode.On;
  153. CreateShaders();
  154. CreateVbo();
  155. GL.Enable(EnableCap.Blend);
  156. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  157. }
  158. protected override void OnUnload(EventArgs e)
  159. {
  160. ScreenTex.Dispose();
  161. GL.DeleteVertexArray(VaoHandle);
  162. GL.DeleteBuffer(VboHandle);
  163. }
  164. private void CreateVbo()
  165. {
  166. VaoHandle = GL.GenVertexArray();
  167. VboHandle = GL.GenBuffer();
  168. uint[] Buffer = new uint[]
  169. {
  170. 0xc4700000, 0x80000000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x00000000,
  171. 0x45340000, 0x80000000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x00000000,
  172. 0xc4700000, 0xc4070000, 0x00000000, 0xffffffff, 0x00000000, 0x00000000, 0x3f800000,
  173. 0x45340000, 0xc4070000, 0x00000000, 0xffffffff, 0x00000000, 0x3f800000, 0x3f800000
  174. };
  175. IntPtr Length = new IntPtr(Buffer.Length * 4);
  176. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  177. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  178. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  179. GL.BindVertexArray(VaoHandle);
  180. GL.EnableVertexAttribArray(0);
  181. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  182. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 28, 0);
  183. GL.EnableVertexAttribArray(1);
  184. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  185. GL.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, false, 28, 12);
  186. GL.EnableVertexAttribArray(2);
  187. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  188. GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 28, 20);
  189. GL.BindVertexArray(0);
  190. }
  191. private void CreateShaders()
  192. {
  193. VtxShaderHandle = GL.CreateShader(ShaderType.VertexShader);
  194. FragShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
  195. GL.ShaderSource(VtxShaderHandle, VtxShaderSource);
  196. GL.ShaderSource(FragShaderHandle, FragShaderSource);
  197. GL.CompileShader(VtxShaderHandle);
  198. GL.CompileShader(FragShaderHandle);
  199. PrgShaderHandle = GL.CreateProgram();
  200. GL.AttachShader(PrgShaderHandle, VtxShaderHandle);
  201. GL.AttachShader(PrgShaderHandle, FragShaderHandle);
  202. GL.LinkProgram(PrgShaderHandle);
  203. GL.UseProgram(PrgShaderHandle);
  204. int TexLocation = GL.GetUniformLocation(PrgShaderHandle, "tex");
  205. GL.Uniform1(TexLocation, 0);
  206. }
  207. protected override void OnUpdateFrame(FrameEventArgs e)
  208. {
  209. unsafe
  210. {
  211. byte* Ptr = (byte*)IntPtr.Add(Ns.Ram, (int)Ns.Os.HidOffset);
  212. int State = 0;
  213. if (Keyboard[OpenTK.Input.Key.Up])
  214. {
  215. State |= 0x2000;
  216. }
  217. if (Keyboard[OpenTK.Input.Key.Down])
  218. {
  219. State |= 0x8000;
  220. }
  221. if (Keyboard[OpenTK.Input.Key.Left])
  222. {
  223. State |= 0x1000;
  224. }
  225. if (Keyboard[OpenTK.Input.Key.Right])
  226. {
  227. State |= 0x4000;
  228. }
  229. if (Keyboard[OpenTK.Input.Key.A])
  230. {
  231. State |= 0x1;
  232. }
  233. if (Keyboard[OpenTK.Input.Key.S])
  234. {
  235. State |= 0x2;
  236. }
  237. if (Keyboard[OpenTK.Input.Key.Z])
  238. {
  239. State |= 0x4;
  240. }
  241. if (Keyboard[OpenTK.Input.Key.X])
  242. {
  243. State |= 0x8;
  244. }
  245. if (Keyboard[OpenTK.Input.Key.Enter])
  246. {
  247. State |= 0x400;
  248. }
  249. if (Keyboard[OpenTK.Input.Key.Tab])
  250. {
  251. State |= 0x800;
  252. }
  253. *((int*)(Ptr + 0xae38)) = (int)State;
  254. }
  255. if (Keyboard[OpenTK.Input.Key.Escape])
  256. {
  257. this.Exit();
  258. }
  259. }
  260. protected override void OnRenderFrame(FrameEventArgs e)
  261. {
  262. GL.Viewport(0, 0, 1280, 720);
  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. }