OpenGLRenderer.cs 9.7 KB

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