OpenGLRenderer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 bool IsVboCached(long Tag, long DataSize)
  126. {
  127. return Rasterizer.IsVboCached(Tag, DataSize);
  128. }
  129. public bool IsIboCached(long Tag, long DataSize)
  130. {
  131. return Rasterizer.IsIboCached(Tag, DataSize);
  132. }
  133. public void CreateVbo(long Tag, byte[] Buffer)
  134. {
  135. ActionsQueue.Enqueue(() => Rasterizer.CreateVbo(Tag, Buffer));
  136. }
  137. public void CreateIbo(long Tag, byte[] Buffer)
  138. {
  139. ActionsQueue.Enqueue(() => Rasterizer.CreateIbo(Tag, Buffer));
  140. }
  141. public void SetVertexArray(int VbIndex, int Stride, long VboTag, GalVertexAttrib[] Attribs)
  142. {
  143. if ((uint)VbIndex > 31)
  144. {
  145. throw new ArgumentOutOfRangeException(nameof(VbIndex));
  146. }
  147. if (Attribs == null)
  148. {
  149. throw new ArgumentNullException(nameof(Attribs));
  150. }
  151. ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride, VboTag, Attribs));
  152. }
  153. public void SetIndexArray(long Tag, int Size, GalIndexFormat Format)
  154. {
  155. ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Tag, Size, Format));
  156. }
  157. public void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType)
  158. {
  159. ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(First, PrimCount, PrimType));
  160. }
  161. public void DrawElements(long IboTag, int First, GalPrimitiveType PrimType)
  162. {
  163. ActionsQueue.Enqueue(() => Rasterizer.DrawElements(IboTag, First, PrimType));
  164. }
  165. public void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type)
  166. {
  167. if (Memory == null)
  168. {
  169. throw new ArgumentNullException(nameof(Memory));
  170. }
  171. Shader.Create(Memory, Tag, Type);
  172. }
  173. public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)
  174. {
  175. if (Data == null)
  176. {
  177. throw new ArgumentNullException(nameof(Data));
  178. }
  179. ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data));
  180. }
  181. public void SetUniform1(string UniformName, int Value)
  182. {
  183. if (UniformName == null)
  184. {
  185. throw new ArgumentNullException(nameof(UniformName));
  186. }
  187. ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value));
  188. }
  189. public void SetUniform2F(string UniformName, float X, float Y)
  190. {
  191. if (UniformName == null)
  192. {
  193. throw new ArgumentNullException(nameof(UniformName));
  194. }
  195. ActionsQueue.Enqueue(() => Shader.SetUniform2F(UniformName, X, Y));
  196. }
  197. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)
  198. {
  199. return Shader.GetTextureUsage(Tag);
  200. }
  201. public void BindShader(long Tag)
  202. {
  203. ActionsQueue.Enqueue(() => Shader.Bind(Tag));
  204. }
  205. public void BindProgram()
  206. {
  207. ActionsQueue.Enqueue(() => Shader.BindProgram());
  208. }
  209. public void SetTextureAndSampler(long Tag, byte[] Data, GalTexture Texture, GalTextureSampler Sampler)
  210. {
  211. ActionsQueue.Enqueue(() =>
  212. {
  213. this.Texture.Create(Tag, Data, Texture);
  214. OGLTexture.Set(Sampler);
  215. });
  216. }
  217. public bool TryGetCachedTexture(long Tag, long DataSize, out GalTexture Texture)
  218. {
  219. return this.Texture.TryGetCachedTexture(Tag, DataSize, out Texture);
  220. }
  221. public void BindTexture(long Tag, int Index)
  222. {
  223. ActionsQueue.Enqueue(() => Texture.Bind(Tag, Index));
  224. }
  225. }
  226. }