OpenGLRenderer.cs 10.0 KB

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