OpenGLRenderer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
  118. {
  119. ActionsQueue.Enqueue(() => Rasterizer.ClearBuffers(RtIndex, Flags));
  120. }
  121. public void SetVertexArray(int VbIndex, int Stride, byte[] Buffer, GalVertexAttrib[] Attribs)
  122. {
  123. if ((uint)VbIndex > 31)
  124. {
  125. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  126. }
  127. ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride,
  128. Buffer ?? throw new ArgumentNullException(nameof(Buffer)),
  129. Attribs ?? throw new ArgumentNullException(nameof(Attribs))));
  130. }
  131. public void SetIndexArray(byte[] Buffer, GalIndexFormat Format)
  132. {
  133. if (Buffer == null)
  134. {
  135. throw new ArgumentNullException(nameof(Buffer));
  136. }
  137. ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Buffer, Format));
  138. }
  139. public void DrawArrays(int VbIndex, GalPrimitiveType PrimType)
  140. {
  141. if ((uint)VbIndex > 31)
  142. {
  143. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  144. }
  145. ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(VbIndex, PrimType));
  146. }
  147. public void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType)
  148. {
  149. if ((uint)VbIndex > 31)
  150. {
  151. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  152. }
  153. ActionsQueue.Enqueue(() => Rasterizer.DrawElements(VbIndex, First, PrimType));
  154. }
  155. public void CreateShader(long Tag, GalShaderType Type, byte[] Data)
  156. {
  157. if (Data == null)
  158. {
  159. throw new ArgumentNullException(nameof(Data));
  160. }
  161. Shader.Create(Tag, Type, Data);
  162. }
  163. public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)
  164. {
  165. if (Data == null)
  166. {
  167. throw new ArgumentNullException(nameof(Data));
  168. }
  169. ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data));
  170. }
  171. public void SetUniform1(string UniformName, int Value)
  172. {
  173. if (UniformName == null)
  174. {
  175. throw new ArgumentNullException(nameof(UniformName));
  176. }
  177. ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value));
  178. }
  179. public void SetUniform2F(string UniformName, float X, float Y)
  180. {
  181. if (UniformName == null)
  182. {
  183. throw new ArgumentNullException(nameof(UniformName));
  184. }
  185. ActionsQueue.Enqueue(() => Shader.SetUniform2F(UniformName, X, Y));
  186. }
  187. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)
  188. {
  189. return Shader.GetTextureUsage(Tag);
  190. }
  191. public void BindShader(long Tag)
  192. {
  193. ActionsQueue.Enqueue(() => Shader.Bind(Tag));
  194. }
  195. public void BindProgram()
  196. {
  197. ActionsQueue.Enqueue(() => Shader.BindProgram());
  198. }
  199. public void SetTextureAndSampler(int Index, GalTexture Texture, GalTextureSampler Sampler)
  200. {
  201. ActionsQueue.Enqueue(() =>
  202. {
  203. this.Texture.Set(Index, Texture);
  204. OGLTexture.Set(Sampler);
  205. });
  206. }
  207. public void BindTexture(int Index)
  208. {
  209. ActionsQueue.Enqueue(() => Texture.Bind(Index));
  210. }
  211. }
  212. }