OpenGLRenderer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. public OpenGLRenderer()
  16. {
  17. Blend = new OGLBlend();
  18. FrameBuffer = new OGLFrameBuffer();
  19. Rasterizer = new OGLRasterizer();
  20. Shader = new OGLShader();
  21. Texture = new OGLTexture();
  22. ActionsQueue = new ConcurrentQueue<Action>();
  23. }
  24. public void QueueAction(Action ActionMthd)
  25. {
  26. ActionsQueue.Enqueue(ActionMthd);
  27. }
  28. public void RunActions()
  29. {
  30. int Count = ActionsQueue.Count;
  31. while (Count-- > 0 && ActionsQueue.TryDequeue(out Action RenderAction))
  32. {
  33. RenderAction();
  34. }
  35. }
  36. public void Render()
  37. {
  38. FrameBuffer.Render();
  39. }
  40. public void SetWindowSize(int Width, int Height)
  41. {
  42. FrameBuffer.SetWindowSize(Width, Height);
  43. }
  44. public void SetBlendEnable(bool Enable)
  45. {
  46. if (Enable)
  47. {
  48. ActionsQueue.Enqueue(() => Blend.Enable());
  49. }
  50. else
  51. {
  52. ActionsQueue.Enqueue(() => Blend.Disable());
  53. }
  54. }
  55. public void SetBlend(
  56. GalBlendEquation Equation,
  57. GalBlendFactor FuncSrc,
  58. GalBlendFactor FuncDst)
  59. {
  60. ActionsQueue.Enqueue(() => Blend.Set(Equation, FuncSrc, FuncDst));
  61. }
  62. public void SetBlendSeparate(
  63. GalBlendEquation EquationRgb,
  64. GalBlendEquation EquationAlpha,
  65. GalBlendFactor FuncSrcRgb,
  66. GalBlendFactor FuncDstRgb,
  67. GalBlendFactor FuncSrcAlpha,
  68. GalBlendFactor FuncDstAlpha)
  69. {
  70. ActionsQueue.Enqueue(() =>
  71. {
  72. Blend.SetSeparate(
  73. EquationRgb,
  74. EquationAlpha,
  75. FuncSrcRgb,
  76. FuncDstRgb,
  77. FuncSrcAlpha,
  78. FuncDstAlpha);
  79. });
  80. }
  81. public void CreateFrameBuffer(long Tag, int Width, int Height)
  82. {
  83. ActionsQueue.Enqueue(() => FrameBuffer.Create(Tag, Width, Height));
  84. }
  85. public void BindFrameBuffer(long Tag)
  86. {
  87. ActionsQueue.Enqueue(() => FrameBuffer.Bind(Tag));
  88. }
  89. public void BindFrameBufferTexture(long Tag, int Index, GalTextureSampler Sampler)
  90. {
  91. ActionsQueue.Enqueue(() =>
  92. {
  93. FrameBuffer.BindTexture(Tag, Index);
  94. OGLTexture.Set(Sampler);
  95. });
  96. }
  97. public void SetFrameBuffer(long Tag)
  98. {
  99. ActionsQueue.Enqueue(() => FrameBuffer.Set(Tag));
  100. }
  101. public void SetFrameBuffer(byte[] Data, int Width, int Height)
  102. {
  103. ActionsQueue.Enqueue(() => FrameBuffer.Set(Data, Width, Height));
  104. }
  105. public void SetFrameBufferTransform(float SX, float SY, float Rotate, float TX, float TY)
  106. {
  107. Matrix2 Transform;
  108. Transform = Matrix2.CreateScale(SX, SY);
  109. Transform *= Matrix2.CreateRotation(Rotate);
  110. Vector2 Offs = new Vector2(TX, TY);
  111. ActionsQueue.Enqueue(() => FrameBuffer.SetTransform(Transform, Offs));
  112. }
  113. public void SetViewport(int X, int Y, int Width, int Height)
  114. {
  115. ActionsQueue.Enqueue(() => FrameBuffer.SetViewport(X, Y, Width, Height));
  116. }
  117. public void GetFrameBufferData(long Tag, Action<byte[]> Callback)
  118. {
  119. ActionsQueue.Enqueue(() => FrameBuffer.GetBufferData(Tag, Callback));
  120. }
  121. public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
  122. {
  123. ActionsQueue.Enqueue(() => Rasterizer.ClearBuffers(RtIndex, Flags));
  124. }
  125. public void SetVertexArray(int VbIndex, int Stride, byte[] Buffer, GalVertexAttrib[] Attribs)
  126. {
  127. if ((uint)VbIndex > 31)
  128. {
  129. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  130. }
  131. ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride,
  132. Buffer ?? throw new ArgumentNullException(nameof(Buffer)),
  133. Attribs ?? throw new ArgumentNullException(nameof(Attribs))));
  134. }
  135. public void SetIndexArray(byte[] Buffer, GalIndexFormat Format)
  136. {
  137. if (Buffer == null)
  138. {
  139. throw new ArgumentNullException(nameof(Buffer));
  140. }
  141. ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Buffer, Format));
  142. }
  143. public void DrawArrays(int VbIndex, int First, int PrimCount, GalPrimitiveType PrimType)
  144. {
  145. if ((uint)VbIndex > 31)
  146. {
  147. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  148. }
  149. ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(VbIndex, First, PrimCount, PrimType));
  150. }
  151. public void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType)
  152. {
  153. if ((uint)VbIndex > 31)
  154. {
  155. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  156. }
  157. ActionsQueue.Enqueue(() => Rasterizer.DrawElements(VbIndex, First, PrimType));
  158. }
  159. public void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type)
  160. {
  161. if (Memory == null)
  162. {
  163. throw new ArgumentNullException(nameof(Memory));
  164. }
  165. Shader.Create(Memory, Tag, Type);
  166. }
  167. public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)
  168. {
  169. if (Data == null)
  170. {
  171. throw new ArgumentNullException(nameof(Data));
  172. }
  173. ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data));
  174. }
  175. public void SetUniform1(string UniformName, int Value)
  176. {
  177. if (UniformName == null)
  178. {
  179. throw new ArgumentNullException(nameof(UniformName));
  180. }
  181. ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value));
  182. }
  183. public void SetUniform2F(string UniformName, float X, float Y)
  184. {
  185. if (UniformName == null)
  186. {
  187. throw new ArgumentNullException(nameof(UniformName));
  188. }
  189. ActionsQueue.Enqueue(() => Shader.SetUniform2F(UniformName, X, Y));
  190. }
  191. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)
  192. {
  193. return Shader.GetTextureUsage(Tag);
  194. }
  195. public void BindShader(long Tag)
  196. {
  197. ActionsQueue.Enqueue(() => Shader.Bind(Tag));
  198. }
  199. public void BindProgram()
  200. {
  201. ActionsQueue.Enqueue(() => Shader.BindProgram());
  202. }
  203. public void SetTextureAndSampler(int Index, GalTexture Texture, GalTextureSampler Sampler)
  204. {
  205. ActionsQueue.Enqueue(() =>
  206. {
  207. this.Texture.Set(Index, Texture);
  208. OGLTexture.Set(Sampler);
  209. });
  210. }
  211. public void BindTexture(int Index)
  212. {
  213. ActionsQueue.Enqueue(() => Texture.Bind(Index));
  214. }
  215. }
  216. }