OpenGLRenderer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using OpenTK;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gal.OpenGL
  6. {
  7. public class OpenGLRenderer : IGalRenderer
  8. {
  9. private OGLBlend Blend;
  10. private OGLFrameBuffer FrameBuffer;
  11. private OGLRasterizer Rasterizer;
  12. private OGLShader Shader;
  13. private OGLTexture Texture;
  14. private ConcurrentQueue<Action> ActionsQueue;
  15. private FrameBuffer FbRenderer;
  16. public OpenGLRenderer()
  17. {
  18. Blend = new OGLBlend();
  19. FrameBuffer = new OGLFrameBuffer();
  20. Rasterizer = new OGLRasterizer();
  21. Shader = new OGLShader();
  22. Texture = new OGLTexture();
  23. ActionsQueue = new ConcurrentQueue<Action>();
  24. }
  25. public void InitializeFrameBuffer()
  26. {
  27. FbRenderer = new FrameBuffer(1280, 720);
  28. }
  29. public void ResetFrameBuffer()
  30. {
  31. FbRenderer.Reset();
  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 && ActionsQueue.TryDequeue(out Action RenderAction))
  41. {
  42. RenderAction();
  43. }
  44. }
  45. public void Render()
  46. {
  47. FbRenderer.Render();
  48. }
  49. public void SetWindowSize(int Width, int Height)
  50. {
  51. FbRenderer.WindowWidth = Width;
  52. FbRenderer.WindowHeight = Height;
  53. }
  54. public unsafe void SetFrameBuffer(
  55. byte* Fb,
  56. int Width,
  57. int Height,
  58. float ScaleX,
  59. float ScaleY,
  60. float OffsX,
  61. float OffsY,
  62. float Rotate)
  63. {
  64. Matrix2 Transform;
  65. Transform = Matrix2.CreateScale(ScaleX, ScaleY);
  66. Transform *= Matrix2.CreateRotation(Rotate);
  67. Vector2 Offs = new Vector2(OffsX, OffsY);
  68. FbRenderer.Set(Fb, Width, Height, Transform, Offs);
  69. }
  70. public void SetBlendEnable(bool Enable)
  71. {
  72. if (Enable)
  73. {
  74. ActionsQueue.Enqueue(() => Blend.Enable());
  75. }
  76. else
  77. {
  78. ActionsQueue.Enqueue(() => Blend.Disable());
  79. }
  80. }
  81. public void SetBlend(
  82. GalBlendEquation Equation,
  83. GalBlendFactor FuncSrc,
  84. GalBlendFactor FuncDst)
  85. {
  86. ActionsQueue.Enqueue(() => Blend.Set(Equation, FuncSrc, FuncDst));
  87. }
  88. public void SetBlendSeparate(
  89. GalBlendEquation EquationRgb,
  90. GalBlendEquation EquationAlpha,
  91. GalBlendFactor FuncSrcRgb,
  92. GalBlendFactor FuncDstRgb,
  93. GalBlendFactor FuncSrcAlpha,
  94. GalBlendFactor FuncDstAlpha)
  95. {
  96. ActionsQueue.Enqueue(() =>
  97. {
  98. Blend.SetSeparate(
  99. EquationRgb,
  100. EquationAlpha,
  101. FuncSrcRgb,
  102. FuncDstRgb,
  103. FuncSrcAlpha,
  104. FuncDstAlpha);
  105. });
  106. }
  107. public void SetFb(int FbIndex, int Width, int Height)
  108. {
  109. ActionsQueue.Enqueue(() => FrameBuffer.Set(FbIndex, Width, Height));
  110. }
  111. public void BindFrameBuffer(int FbIndex)
  112. {
  113. ActionsQueue.Enqueue(() => FrameBuffer.Bind(FbIndex));
  114. }
  115. public void DrawFrameBuffer(int FbIndex)
  116. {
  117. ActionsQueue.Enqueue(() => FrameBuffer.Draw(FbIndex));
  118. }
  119. public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
  120. {
  121. ActionsQueue.Enqueue(() => Rasterizer.ClearBuffers(RtIndex, Flags));
  122. }
  123. public void SetVertexArray(int VbIndex, int Stride, byte[] Buffer, GalVertexAttrib[] Attribs)
  124. {
  125. if ((uint)VbIndex > 31)
  126. {
  127. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  128. }
  129. ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride,
  130. Buffer ?? throw new ArgumentNullException(nameof(Buffer)),
  131. Attribs ?? throw new ArgumentNullException(nameof(Attribs))));
  132. }
  133. public void SetIndexArray(byte[] Buffer, GalIndexFormat Format)
  134. {
  135. if (Buffer == null)
  136. {
  137. throw new ArgumentNullException(nameof(Buffer));
  138. }
  139. ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Buffer, Format));
  140. }
  141. public void DrawArrays(int VbIndex, GalPrimitiveType PrimType)
  142. {
  143. if ((uint)VbIndex > 31)
  144. {
  145. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  146. }
  147. ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(VbIndex, PrimType));
  148. }
  149. public void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType)
  150. {
  151. if ((uint)VbIndex > 31)
  152. {
  153. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  154. }
  155. ActionsQueue.Enqueue(() => Rasterizer.DrawElements(VbIndex, First, PrimType));
  156. }
  157. public void CreateShader(long Tag, GalShaderType Type, byte[] Data)
  158. {
  159. if (Data == null)
  160. {
  161. throw new ArgumentNullException(nameof(Data));
  162. }
  163. Shader.Create(Tag, Type, Data);
  164. }
  165. public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)
  166. {
  167. if (Data == null)
  168. {
  169. throw new ArgumentNullException(nameof(Data));
  170. }
  171. ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data));
  172. }
  173. public void SetUniform1(string UniformName, int Value)
  174. {
  175. if (UniformName == null)
  176. {
  177. throw new ArgumentNullException(nameof(UniformName));
  178. }
  179. ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value));
  180. }
  181. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)
  182. {
  183. return Shader.GetTextureUsage(Tag);
  184. }
  185. public void BindShader(long Tag)
  186. {
  187. ActionsQueue.Enqueue(() => Shader.Bind(Tag));
  188. }
  189. public void BindProgram()
  190. {
  191. ActionsQueue.Enqueue(() => Shader.BindProgram());
  192. }
  193. public void SetTexture(int Index, GalTexture Tex)
  194. {
  195. ActionsQueue.Enqueue(() => Texture.Set(Index, Tex));
  196. }
  197. public void SetSampler(int Index, GalTextureSampler Sampler)
  198. {
  199. ActionsQueue.Enqueue(() => Texture.Set(Index, Sampler));
  200. }
  201. }
  202. }