OGLFrameBuffer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using OpenTK;
  2. using OpenTK.Graphics.OpenGL;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gal.OpenGL
  6. {
  7. public class OGLFrameBuffer : IGalFrameBuffer
  8. {
  9. private struct Rect
  10. {
  11. public int X { get; private set; }
  12. public int Y { get; private set; }
  13. public int Width { get; private set; }
  14. public int Height { get; private set; }
  15. public Rect(int X, int Y, int Width, int Height)
  16. {
  17. this.X = X;
  18. this.Y = Y;
  19. this.Width = Width;
  20. this.Height = Height;
  21. }
  22. }
  23. private class FrameBuffer
  24. {
  25. public int Width { get; set; }
  26. public int Height { get; set; }
  27. public int Handle { get; private set; }
  28. public int RbHandle { get; private set; }
  29. public int TexHandle { get; private set; }
  30. public FrameBuffer(int Width, int Height)
  31. {
  32. this.Width = Width;
  33. this.Height = Height;
  34. Handle = GL.GenFramebuffer();
  35. RbHandle = GL.GenRenderbuffer();
  36. TexHandle = GL.GenTexture();
  37. }
  38. }
  39. private struct ShaderProgram
  40. {
  41. public int Handle;
  42. public int VpHandle;
  43. public int FpHandle;
  44. }
  45. private Dictionary<long, FrameBuffer> Fbs;
  46. private ShaderProgram Shader;
  47. private Rect Viewport;
  48. private Rect Window;
  49. private bool IsInitialized;
  50. private int RawFbTexWidth;
  51. private int RawFbTexHeight;
  52. private int RawFbTexHandle;
  53. private int CurrFbHandle;
  54. private int CurrTexHandle;
  55. private int VaoHandle;
  56. private int VboHandle;
  57. public OGLFrameBuffer()
  58. {
  59. Fbs = new Dictionary<long, FrameBuffer>();
  60. Shader = new ShaderProgram();
  61. }
  62. public void Create(long Key, int Width, int Height)
  63. {
  64. //TODO: We should either use the original frame buffer size,
  65. //or just remove the Width/Height arguments.
  66. Width = Window.Width;
  67. Height = Window.Height;
  68. if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
  69. {
  70. if (Fb.Width != Width ||
  71. Fb.Height != Height)
  72. {
  73. SetupTexture(Fb.TexHandle, Width, Height);
  74. Fb.Width = Width;
  75. Fb.Height = Height;
  76. }
  77. return;
  78. }
  79. Fb = new FrameBuffer(Width, Height);
  80. SetupTexture(Fb.TexHandle, Width, Height);
  81. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Fb.Handle);
  82. GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, Fb.RbHandle);
  83. GL.RenderbufferStorage(
  84. RenderbufferTarget.Renderbuffer,
  85. RenderbufferStorage.Depth24Stencil8,
  86. Width,
  87. Height);
  88. GL.FramebufferRenderbuffer(
  89. FramebufferTarget.Framebuffer,
  90. FramebufferAttachment.DepthStencilAttachment,
  91. RenderbufferTarget.Renderbuffer,
  92. Fb.RbHandle);
  93. GL.FramebufferTexture(
  94. FramebufferTarget.Framebuffer,
  95. FramebufferAttachment.ColorAttachment0,
  96. Fb.TexHandle,
  97. 0);
  98. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  99. GL.Viewport(0, 0, Width, Height);
  100. Fbs.Add(Key, Fb);
  101. }
  102. public void Bind(long Key)
  103. {
  104. if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
  105. {
  106. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Fb.Handle);
  107. CurrFbHandle = Fb.Handle;
  108. }
  109. }
  110. public void BindTexture(long Key, int Index)
  111. {
  112. if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
  113. {
  114. GL.ActiveTexture(TextureUnit.Texture0 + Index);
  115. GL.BindTexture(TextureTarget.Texture2D, Fb.TexHandle);
  116. }
  117. }
  118. public void Set(long Key)
  119. {
  120. if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
  121. {
  122. CurrTexHandle = Fb.TexHandle;
  123. }
  124. }
  125. public void Set(byte[] Data, int Width, int Height)
  126. {
  127. if (RawFbTexHandle == 0)
  128. {
  129. RawFbTexHandle = GL.GenTexture();
  130. }
  131. if (RawFbTexWidth != Width ||
  132. RawFbTexHeight != Height)
  133. {
  134. SetupTexture(RawFbTexHandle, Width, Height);
  135. RawFbTexWidth = Width;
  136. RawFbTexHeight = Height;
  137. }
  138. GL.ActiveTexture(TextureUnit.Texture0);
  139. GL.BindTexture(TextureTarget.Texture2D, RawFbTexHandle);
  140. (PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8);
  141. GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, Width, Height, Format, Type, Data);
  142. CurrTexHandle = RawFbTexHandle;
  143. }
  144. public void SetTransform(float SX, float SY, float Rotate, float TX, float TY)
  145. {
  146. EnsureInitialized();
  147. Matrix2 Transform;
  148. Transform = Matrix2.CreateScale(SX, SY);
  149. Transform *= Matrix2.CreateRotation(Rotate);
  150. Vector2 Offs = new Vector2(TX, TY);
  151. int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
  152. GL.UseProgram(Shader.Handle);
  153. int TransformUniformLocation = GL.GetUniformLocation(Shader.Handle, "transform");
  154. GL.UniformMatrix2(TransformUniformLocation, false, ref Transform);
  155. int OffsetUniformLocation = GL.GetUniformLocation(Shader.Handle, "offset");
  156. GL.Uniform2(OffsetUniformLocation, ref Offs);
  157. GL.UseProgram(CurrentProgram);
  158. }
  159. public void SetWindowSize(int Width, int Height)
  160. {
  161. int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
  162. GL.UseProgram(Shader.Handle);
  163. int WindowSizeUniformLocation = GL.GetUniformLocation(Shader.Handle, "window_size");
  164. GL.Uniform2(WindowSizeUniformLocation, new Vector2(Width, Height));
  165. GL.UseProgram(CurrentProgram);
  166. Window = new Rect(0, 0, Width, Height);
  167. }
  168. public void SetViewport(int X, int Y, int Width, int Height)
  169. {
  170. Viewport = new Rect(X, Y, Width, Height);
  171. //TODO
  172. }
  173. public void Render()
  174. {
  175. if (CurrTexHandle != 0)
  176. {
  177. EnsureInitialized();
  178. bool AlphaBlendEnable = GL.GetInteger(GetPName.Blend) != 0;
  179. GL.Disable(EnableCap.Blend);
  180. GL.ActiveTexture(TextureUnit.Texture0);
  181. GL.BindTexture(TextureTarget.Texture2D, CurrTexHandle);
  182. int CurrentProgram = GL.GetInteger(GetPName.CurrentProgram);
  183. GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
  184. SetViewport(Window);
  185. GL.Clear(
  186. ClearBufferMask.ColorBufferBit |
  187. ClearBufferMask.DepthBufferBit);
  188. GL.BindVertexArray(VaoHandle);
  189. GL.UseProgram(Shader.Handle);
  190. GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
  191. //Restore the original state.
  192. GL.BindFramebuffer(FramebufferTarget.Framebuffer, CurrFbHandle);
  193. GL.UseProgram(CurrentProgram);
  194. if (AlphaBlendEnable)
  195. {
  196. GL.Enable(EnableCap.Blend);
  197. }
  198. //GL.Viewport(0, 0, 1280, 720);
  199. }
  200. }
  201. public void GetBufferData(long Key, Action<byte[]> Callback)
  202. {
  203. if (Fbs.TryGetValue(Key, out FrameBuffer Fb))
  204. {
  205. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, Fb.Handle);
  206. byte[] Data = new byte[Fb.Width * Fb.Height * 4];
  207. (PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8);
  208. GL.ReadPixels(
  209. 0,
  210. 0,
  211. Fb.Width,
  212. Fb.Height,
  213. Format,
  214. Type,
  215. Data);
  216. Callback(Data);
  217. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, CurrFbHandle);
  218. }
  219. }
  220. private void SetViewport(Rect Viewport)
  221. {
  222. GL.Viewport(
  223. Viewport.X,
  224. Viewport.Y,
  225. Viewport.Width,
  226. Viewport.Height);
  227. }
  228. private void EnsureInitialized()
  229. {
  230. if (!IsInitialized)
  231. {
  232. IsInitialized = true;
  233. SetupShader();
  234. SetupVertex();
  235. }
  236. }
  237. private void SetupShader()
  238. {
  239. Shader.VpHandle = GL.CreateShader(ShaderType.VertexShader);
  240. Shader.FpHandle = GL.CreateShader(ShaderType.FragmentShader);
  241. string VpSource = EmbeddedResource.GetString("GlFbVtxShader");
  242. string FpSource = EmbeddedResource.GetString("GlFbFragShader");
  243. GL.ShaderSource(Shader.VpHandle, VpSource);
  244. GL.ShaderSource(Shader.FpHandle, FpSource);
  245. GL.CompileShader(Shader.VpHandle);
  246. GL.CompileShader(Shader.FpHandle);
  247. Shader.Handle = GL.CreateProgram();
  248. GL.AttachShader(Shader.Handle, Shader.VpHandle);
  249. GL.AttachShader(Shader.Handle, Shader.FpHandle);
  250. GL.LinkProgram(Shader.Handle);
  251. GL.UseProgram(Shader.Handle);
  252. Matrix2 Transform = Matrix2.Identity;
  253. int TexUniformLocation = GL.GetUniformLocation(Shader.Handle, "tex");
  254. GL.Uniform1(TexUniformLocation, 0);
  255. int WindowSizeUniformLocation = GL.GetUniformLocation(Shader.Handle, "window_size");
  256. GL.Uniform2(WindowSizeUniformLocation, new Vector2(1280.0f, 720.0f));
  257. int TransformUniformLocation = GL.GetUniformLocation(Shader.Handle, "transform");
  258. GL.UniformMatrix2(TransformUniformLocation, false, ref Transform);
  259. }
  260. private void SetupVertex()
  261. {
  262. VaoHandle = GL.GenVertexArray();
  263. VboHandle = GL.GenBuffer();
  264. float[] Buffer = new float[]
  265. {
  266. -1, 1, 0, 0,
  267. 1, 1, 1, 0,
  268. -1, -1, 0, 1,
  269. 1, -1, 1, 1
  270. };
  271. IntPtr Length = new IntPtr(Buffer.Length * 4);
  272. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  273. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  274. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  275. GL.BindVertexArray(VaoHandle);
  276. GL.EnableVertexAttribArray(0);
  277. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  278. GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 16, 0);
  279. GL.EnableVertexAttribArray(1);
  280. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  281. GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 16, 8);
  282. }
  283. private void SetupTexture(int Handle, int Width, int Height)
  284. {
  285. GL.BindTexture(TextureTarget.Texture2D, Handle);
  286. const int MinFilter = (int)TextureMinFilter.Linear;
  287. const int MagFilter = (int)TextureMagFilter.Linear;
  288. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter);
  289. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter);
  290. (PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8);
  291. const PixelInternalFormat InternalFmt = PixelInternalFormat.Rgba;
  292. const int Level = 0;
  293. const int Border = 0;
  294. GL.TexImage2D(
  295. TextureTarget.Texture2D,
  296. Level,
  297. InternalFmt,
  298. Width,
  299. Height,
  300. Border,
  301. Format,
  302. Type,
  303. IntPtr.Zero);
  304. }
  305. }
  306. }