OpenGLRenderer.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using OpenTK;
  2. using OpenTK.Graphics.OpenGL;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.Graphics.Gal.OpenGL
  7. {
  8. public class OpenGLRenderer : IGalRenderer
  9. {
  10. private struct VertexBuffer
  11. {
  12. public int VaoHandle;
  13. public int VboHandle;
  14. public int PrimCount;
  15. }
  16. private struct Texture
  17. {
  18. public int Handle;
  19. }
  20. private List<VertexBuffer> VertexBuffers;
  21. private Texture[] Textures;
  22. private ConcurrentQueue<Action> ActionsQueue;
  23. private FrameBuffer FbRenderer;
  24. public OpenGLRenderer()
  25. {
  26. VertexBuffers = new List<VertexBuffer>();
  27. Textures = new Texture[8];
  28. ActionsQueue = new ConcurrentQueue<Action>();
  29. }
  30. public void InitializeFrameBuffer()
  31. {
  32. FbRenderer = new FrameBuffer(1280, 720);
  33. }
  34. public void QueueAction(Action ActionMthd)
  35. {
  36. ActionsQueue.Enqueue(ActionMthd);
  37. }
  38. public void RunActions()
  39. {
  40. int Count = ActionsQueue.Count;
  41. while (Count-- > 0 && ActionsQueue.TryDequeue(out Action RenderAction))
  42. {
  43. RenderAction();
  44. }
  45. }
  46. public void Render()
  47. {
  48. FbRenderer.Render();
  49. for (int Index = 0; Index < VertexBuffers.Count; Index++)
  50. {
  51. VertexBuffer Vb = VertexBuffers[Index];
  52. if (Vb.VaoHandle != 0 &&
  53. Vb.PrimCount != 0)
  54. {
  55. GL.BindVertexArray(Vb.VaoHandle);
  56. GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Vb.PrimCount);
  57. }
  58. }
  59. }
  60. public void SetWindowSize(int Width, int Height)
  61. {
  62. FbRenderer.WindowWidth = Width;
  63. FbRenderer.WindowHeight = Height;
  64. }
  65. public unsafe void SetFrameBuffer(
  66. byte* Fb,
  67. int Width,
  68. int Height,
  69. float ScaleX,
  70. float ScaleY,
  71. float OffsX,
  72. float OffsY,
  73. float Rotate)
  74. {
  75. Matrix2 Transform;
  76. Transform = Matrix2.CreateScale(ScaleX, ScaleY);
  77. Transform *= Matrix2.CreateRotation(Rotate);
  78. Vector2 Offs = new Vector2(OffsX, OffsY);
  79. FbRenderer.Set(Fb, Width, Height, Transform, Offs);
  80. }
  81. public void SendVertexBuffer(int Index, byte[] Buffer, int Stride, GalVertexAttrib[] Attribs)
  82. {
  83. if (Index < 0)
  84. {
  85. throw new ArgumentOutOfRangeException(nameof(Index));
  86. }
  87. if (Buffer.Length == 0 || Stride == 0)
  88. {
  89. return;
  90. }
  91. EnsureVbInitialized(Index);
  92. VertexBuffer Vb = VertexBuffers[Index];
  93. Vb.PrimCount = Buffer.Length / Stride;
  94. VertexBuffers[Index] = Vb;
  95. IntPtr Length = new IntPtr(Buffer.Length);
  96. GL.BindBuffer(BufferTarget.ArrayBuffer, Vb.VboHandle);
  97. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  98. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  99. GL.BindVertexArray(Vb.VaoHandle);
  100. for (int Attr = 0; Attr < 16; Attr++)
  101. {
  102. GL.DisableVertexAttribArray(Attr);
  103. }
  104. foreach (GalVertexAttrib Attrib in Attribs)
  105. {
  106. if (Attrib.Index >= 3) break;
  107. GL.EnableVertexAttribArray(Attrib.Index);
  108. GL.BindBuffer(BufferTarget.ArrayBuffer, Vb.VboHandle);
  109. int Size = 0;
  110. switch (Attrib.Size)
  111. {
  112. case GalVertexAttribSize._8:
  113. case GalVertexAttribSize._16:
  114. case GalVertexAttribSize._32:
  115. Size = 1;
  116. break;
  117. case GalVertexAttribSize._8_8:
  118. case GalVertexAttribSize._16_16:
  119. case GalVertexAttribSize._32_32:
  120. Size = 2;
  121. break;
  122. case GalVertexAttribSize._8_8_8:
  123. case GalVertexAttribSize._11_11_10:
  124. case GalVertexAttribSize._16_16_16:
  125. case GalVertexAttribSize._32_32_32:
  126. Size = 3;
  127. break;
  128. case GalVertexAttribSize._8_8_8_8:
  129. case GalVertexAttribSize._10_10_10_2:
  130. case GalVertexAttribSize._16_16_16_16:
  131. case GalVertexAttribSize._32_32_32_32:
  132. Size = 4;
  133. break;
  134. }
  135. bool Signed =
  136. Attrib.Type == GalVertexAttribType.Snorm ||
  137. Attrib.Type == GalVertexAttribType.Sint ||
  138. Attrib.Type == GalVertexAttribType.Sscaled;
  139. bool Normalize =
  140. Attrib.Type == GalVertexAttribType.Snorm ||
  141. Attrib.Type == GalVertexAttribType.Unorm;
  142. VertexAttribPointerType Type = 0;
  143. switch (Attrib.Type)
  144. {
  145. case GalVertexAttribType.Snorm:
  146. case GalVertexAttribType.Unorm:
  147. case GalVertexAttribType.Sint:
  148. case GalVertexAttribType.Uint:
  149. case GalVertexAttribType.Uscaled:
  150. case GalVertexAttribType.Sscaled:
  151. {
  152. switch (Attrib.Size)
  153. {
  154. case GalVertexAttribSize._8:
  155. case GalVertexAttribSize._8_8:
  156. case GalVertexAttribSize._8_8_8:
  157. case GalVertexAttribSize._8_8_8_8:
  158. {
  159. Type = Signed
  160. ? VertexAttribPointerType.Byte
  161. : VertexAttribPointerType.UnsignedByte;
  162. break;
  163. }
  164. case GalVertexAttribSize._16:
  165. case GalVertexAttribSize._16_16:
  166. case GalVertexAttribSize._16_16_16:
  167. case GalVertexAttribSize._16_16_16_16:
  168. {
  169. Type = Signed
  170. ? VertexAttribPointerType.Short
  171. : VertexAttribPointerType.UnsignedShort;
  172. break;
  173. }
  174. case GalVertexAttribSize._10_10_10_2:
  175. case GalVertexAttribSize._11_11_10:
  176. case GalVertexAttribSize._32:
  177. case GalVertexAttribSize._32_32:
  178. case GalVertexAttribSize._32_32_32:
  179. case GalVertexAttribSize._32_32_32_32:
  180. {
  181. Type = Signed
  182. ? VertexAttribPointerType.Int
  183. : VertexAttribPointerType.UnsignedInt;
  184. break;
  185. }
  186. }
  187. break;
  188. }
  189. case GalVertexAttribType.Float:
  190. {
  191. Type = VertexAttribPointerType.Float;
  192. break;
  193. }
  194. }
  195. GL.VertexAttribPointer(
  196. Attrib.Index,
  197. Size,
  198. Type,
  199. Normalize,
  200. Stride,
  201. Attrib.Offset);
  202. }
  203. GL.BindVertexArray(0);
  204. }
  205. public void SendR8G8B8A8Texture(int Index, byte[] Buffer, int Width, int Height)
  206. {
  207. EnsureTexInitialized(Index);
  208. GL.BindTexture(TextureTarget.Texture2D, Textures[Index].Handle);
  209. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
  210. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
  211. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
  212. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
  213. GL.TexImage2D(TextureTarget.Texture2D,
  214. 0,
  215. PixelInternalFormat.Rgba,
  216. Width,
  217. Height,
  218. 0,
  219. PixelFormat.Rgba,
  220. PixelType.UnsignedByte,
  221. Buffer);
  222. }
  223. public void BindTexture(int Index)
  224. {
  225. GL.ActiveTexture(TextureUnit.Texture0 + Index);
  226. GL.BindTexture(TextureTarget.Texture2D, Textures[Index].Handle);
  227. }
  228. private void EnsureVbInitialized(int VbIndex)
  229. {
  230. while (VbIndex >= VertexBuffers.Count)
  231. {
  232. VertexBuffers.Add(new VertexBuffer());
  233. }
  234. VertexBuffer Vb = VertexBuffers[VbIndex];
  235. if (Vb.VaoHandle == 0)
  236. {
  237. Vb.VaoHandle = GL.GenVertexArray();
  238. }
  239. if (Vb.VboHandle == 0)
  240. {
  241. Vb.VboHandle = GL.GenBuffer();
  242. }
  243. VertexBuffers[VbIndex] = Vb;
  244. }
  245. private void EnsureTexInitialized(int TexIndex)
  246. {
  247. Texture Tex = Textures[TexIndex];
  248. if (Tex.Handle == 0)
  249. {
  250. Tex.Handle = GL.GenTexture();
  251. }
  252. Textures[TexIndex] = Tex;
  253. }
  254. }
  255. }