OpenGLRenderer.cs 9.7 KB

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