OGLPipeline.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gal.OpenGL
  5. {
  6. class OGLPipeline : IGalPipeline
  7. {
  8. private static Dictionary<GalVertexAttribSize, int> AttribElements =
  9. new Dictionary<GalVertexAttribSize, int>()
  10. {
  11. { GalVertexAttribSize._32_32_32_32, 4 },
  12. { GalVertexAttribSize._32_32_32, 3 },
  13. { GalVertexAttribSize._16_16_16_16, 4 },
  14. { GalVertexAttribSize._32_32, 2 },
  15. { GalVertexAttribSize._16_16_16, 3 },
  16. { GalVertexAttribSize._8_8_8_8, 4 },
  17. { GalVertexAttribSize._16_16, 2 },
  18. { GalVertexAttribSize._32, 1 },
  19. { GalVertexAttribSize._8_8_8, 3 },
  20. { GalVertexAttribSize._8_8, 2 },
  21. { GalVertexAttribSize._16, 1 },
  22. { GalVertexAttribSize._8, 1 },
  23. { GalVertexAttribSize._10_10_10_2, 4 },
  24. { GalVertexAttribSize._11_11_10, 3 }
  25. };
  26. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> AttribTypes =
  27. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  28. {
  29. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.Int },
  30. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.Int },
  31. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.Short },
  32. { GalVertexAttribSize._32_32, VertexAttribPointerType.Int },
  33. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.Short },
  34. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.Byte },
  35. { GalVertexAttribSize._16_16, VertexAttribPointerType.Short },
  36. { GalVertexAttribSize._32, VertexAttribPointerType.Int },
  37. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.Byte },
  38. { GalVertexAttribSize._8_8, VertexAttribPointerType.Byte },
  39. { GalVertexAttribSize._16, VertexAttribPointerType.Short },
  40. { GalVertexAttribSize._8, VertexAttribPointerType.Byte },
  41. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.Int }, //?
  42. { GalVertexAttribSize._11_11_10, VertexAttribPointerType.Int } //?
  43. };
  44. private GalPipelineState Old;
  45. private OGLConstBuffer Buffer;
  46. private OGLRasterizer Rasterizer;
  47. private OGLShader Shader;
  48. private int VaoHandle;
  49. public OGLPipeline(OGLConstBuffer Buffer, OGLRasterizer Rasterizer, OGLShader Shader)
  50. {
  51. this.Buffer = Buffer;
  52. this.Rasterizer = Rasterizer;
  53. this.Shader = Shader;
  54. //These values match OpenGL's defaults
  55. Old = new GalPipelineState
  56. {
  57. FrontFace = GalFrontFace.CCW,
  58. CullFaceEnabled = false,
  59. CullFace = GalCullFace.Back,
  60. DepthTestEnabled = false,
  61. DepthFunc = GalComparisonOp.Less,
  62. StencilTestEnabled = false,
  63. StencilBackFuncFunc = GalComparisonOp.Always,
  64. StencilBackFuncRef = 0,
  65. StencilBackFuncMask = UInt32.MaxValue,
  66. StencilBackOpFail = GalStencilOp.Keep,
  67. StencilBackOpZFail = GalStencilOp.Keep,
  68. StencilBackOpZPass = GalStencilOp.Keep,
  69. StencilBackMask = UInt32.MaxValue,
  70. StencilFrontFuncFunc = GalComparisonOp.Always,
  71. StencilFrontFuncRef = 0,
  72. StencilFrontFuncMask = UInt32.MaxValue,
  73. StencilFrontOpFail = GalStencilOp.Keep,
  74. StencilFrontOpZFail = GalStencilOp.Keep,
  75. StencilFrontOpZPass = GalStencilOp.Keep,
  76. StencilFrontMask = UInt32.MaxValue,
  77. BlendEnabled = false,
  78. BlendSeparateAlpha = false,
  79. BlendEquationRgb = 0,
  80. BlendFuncSrcRgb = GalBlendFactor.One,
  81. BlendFuncDstRgb = GalBlendFactor.Zero,
  82. BlendEquationAlpha = 0,
  83. BlendFuncSrcAlpha = GalBlendFactor.One,
  84. BlendFuncDstAlpha = GalBlendFactor.Zero,
  85. PrimitiveRestartEnabled = false,
  86. PrimitiveRestartIndex = 0
  87. };
  88. }
  89. public void Bind(GalPipelineState New)
  90. {
  91. BindConstBuffers(New);
  92. BindVertexLayout(New);
  93. if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY)
  94. {
  95. Shader.SetFlip(New.FlipX, New.FlipY);
  96. }
  97. //Note: Uncomment SetFrontFace and SetCullFace when flipping issues are solved
  98. //if (New.FrontFace != O.FrontFace)
  99. //{
  100. // GL.FrontFace(OGLEnumConverter.GetFrontFace(New.FrontFace));
  101. //}
  102. //if (New.CullFaceEnabled != O.CullFaceEnabled)
  103. //{
  104. // Enable(EnableCap.CullFace, New.CullFaceEnabled);
  105. //}
  106. //if (New.CullFaceEnabled)
  107. //{
  108. // if (New.CullFace != O.CullFace)
  109. // {
  110. // GL.CullFace(OGLEnumConverter.GetCullFace(New.CullFace));
  111. // }
  112. //}
  113. if (New.DepthTestEnabled != Old.DepthTestEnabled)
  114. {
  115. Enable(EnableCap.DepthTest, New.DepthTestEnabled);
  116. }
  117. if (New.DepthTestEnabled)
  118. {
  119. if (New.DepthFunc != Old.DepthFunc)
  120. {
  121. GL.DepthFunc(OGLEnumConverter.GetDepthFunc(New.DepthFunc));
  122. }
  123. }
  124. if (New.StencilTestEnabled != Old.StencilTestEnabled)
  125. {
  126. Enable(EnableCap.StencilTest, New.StencilTestEnabled);
  127. }
  128. if (New.StencilTestEnabled)
  129. {
  130. if (New.StencilBackFuncFunc != Old.StencilBackFuncFunc ||
  131. New.StencilBackFuncRef != Old.StencilBackFuncRef ||
  132. New.StencilBackFuncMask != Old.StencilBackFuncMask)
  133. {
  134. GL.StencilFuncSeparate(
  135. StencilFace.Back,
  136. OGLEnumConverter.GetStencilFunc(New.StencilBackFuncFunc),
  137. New.StencilBackFuncRef,
  138. New.StencilBackFuncMask);
  139. }
  140. if (New.StencilBackOpFail != Old.StencilBackOpFail ||
  141. New.StencilBackOpZFail != Old.StencilBackOpZFail ||
  142. New.StencilBackOpZPass != Old.StencilBackOpZPass)
  143. {
  144. GL.StencilOpSeparate(
  145. StencilFace.Back,
  146. OGLEnumConverter.GetStencilOp(New.StencilBackOpFail),
  147. OGLEnumConverter.GetStencilOp(New.StencilBackOpZFail),
  148. OGLEnumConverter.GetStencilOp(New.StencilBackOpZPass));
  149. }
  150. if (New.StencilBackMask != Old.StencilBackMask)
  151. {
  152. GL.StencilMaskSeparate(StencilFace.Back, New.StencilBackMask);
  153. }
  154. if (New.StencilFrontFuncFunc != Old.StencilFrontFuncFunc ||
  155. New.StencilFrontFuncRef != Old.StencilFrontFuncRef ||
  156. New.StencilFrontFuncMask != Old.StencilFrontFuncMask)
  157. {
  158. GL.StencilFuncSeparate(
  159. StencilFace.Front,
  160. OGLEnumConverter.GetStencilFunc(New.StencilFrontFuncFunc),
  161. New.StencilFrontFuncRef,
  162. New.StencilFrontFuncMask);
  163. }
  164. if (New.StencilFrontOpFail != Old.StencilFrontOpFail ||
  165. New.StencilFrontOpZFail != Old.StencilFrontOpZFail ||
  166. New.StencilFrontOpZPass != Old.StencilFrontOpZPass)
  167. {
  168. GL.StencilOpSeparate(
  169. StencilFace.Front,
  170. OGLEnumConverter.GetStencilOp(New.StencilFrontOpFail),
  171. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZFail),
  172. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZPass));
  173. }
  174. if (New.StencilFrontMask != Old.StencilFrontMask)
  175. {
  176. GL.StencilMaskSeparate(StencilFace.Front, New.StencilFrontMask);
  177. }
  178. }
  179. if (New.BlendEnabled != Old.BlendEnabled)
  180. {
  181. Enable(EnableCap.Blend, New.BlendEnabled);
  182. }
  183. if (New.BlendEnabled)
  184. {
  185. if (New.BlendSeparateAlpha)
  186. {
  187. if (New.BlendEquationRgb != Old.BlendEquationRgb ||
  188. New.BlendEquationAlpha != Old.BlendEquationAlpha)
  189. {
  190. GL.BlendEquationSeparate(
  191. OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb),
  192. OGLEnumConverter.GetBlendEquation(New.BlendEquationAlpha));
  193. }
  194. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  195. New.BlendFuncDstRgb != Old.BlendFuncDstRgb ||
  196. New.BlendFuncSrcAlpha != Old.BlendFuncSrcAlpha ||
  197. New.BlendFuncDstAlpha != Old.BlendFuncDstAlpha)
  198. {
  199. GL.BlendFuncSeparate(
  200. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  201. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb),
  202. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcAlpha),
  203. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstAlpha));
  204. }
  205. }
  206. else
  207. {
  208. if (New.BlendEquationRgb != Old.BlendEquationRgb)
  209. {
  210. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb));
  211. }
  212. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  213. New.BlendFuncDstRgb != Old.BlendFuncDstRgb)
  214. {
  215. GL.BlendFunc(
  216. OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  217. OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb));
  218. }
  219. }
  220. }
  221. if (New.PrimitiveRestartEnabled != Old.PrimitiveRestartEnabled)
  222. {
  223. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  224. }
  225. if (New.PrimitiveRestartEnabled)
  226. {
  227. if (New.PrimitiveRestartIndex != Old.PrimitiveRestartIndex)
  228. {
  229. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  230. }
  231. }
  232. Old = New;
  233. }
  234. private void BindConstBuffers(GalPipelineState New)
  235. {
  236. //Index 0 is reserved
  237. int FreeBinding = 1;
  238. void BindIfNotNull(OGLShaderStage Stage)
  239. {
  240. if (Stage != null)
  241. {
  242. foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage)
  243. {
  244. long Key = New.ConstBufferKeys[(int)Stage.Type][DeclInfo.Cbuf];
  245. if (Key != 0 && Buffer.TryGetUbo(Key, out int UboHandle))
  246. {
  247. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, UboHandle);
  248. }
  249. FreeBinding++;
  250. }
  251. }
  252. }
  253. BindIfNotNull(Shader.Current.Vertex);
  254. BindIfNotNull(Shader.Current.TessControl);
  255. BindIfNotNull(Shader.Current.TessEvaluation);
  256. BindIfNotNull(Shader.Current.Geometry);
  257. BindIfNotNull(Shader.Current.Fragment);
  258. }
  259. private void BindVertexLayout(GalPipelineState New)
  260. {
  261. foreach (GalVertexBinding Binding in New.VertexBindings)
  262. {
  263. if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
  264. {
  265. continue;
  266. }
  267. if (VaoHandle == 0)
  268. {
  269. VaoHandle = GL.GenVertexArray();
  270. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  271. //if you want to use it, move this line out of the if
  272. GL.BindVertexArray(VaoHandle);
  273. }
  274. foreach (GalVertexAttrib Attrib in Binding.Attribs)
  275. {
  276. GL.EnableVertexAttribArray(Attrib.Index);
  277. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  278. bool Unsigned =
  279. Attrib.Type == GalVertexAttribType.Unorm ||
  280. Attrib.Type == GalVertexAttribType.Uint ||
  281. Attrib.Type == GalVertexAttribType.Uscaled;
  282. bool Normalize =
  283. Attrib.Type == GalVertexAttribType.Snorm ||
  284. Attrib.Type == GalVertexAttribType.Unorm;
  285. VertexAttribPointerType Type = 0;
  286. if (Attrib.Type == GalVertexAttribType.Float)
  287. {
  288. Type = VertexAttribPointerType.Float;
  289. }
  290. else
  291. {
  292. Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
  293. }
  294. int Size = AttribElements[Attrib.Size];
  295. int Offset = Attrib.Offset;
  296. if (Attrib.Type == GalVertexAttribType.Sint ||
  297. Attrib.Type == GalVertexAttribType.Uint)
  298. {
  299. IntPtr Pointer = new IntPtr(Offset);
  300. VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;
  301. GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
  302. }
  303. else
  304. {
  305. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
  306. }
  307. }
  308. }
  309. }
  310. private void Enable(EnableCap Cap, bool Enabled)
  311. {
  312. if (Enabled)
  313. {
  314. GL.Enable(Cap);
  315. }
  316. else
  317. {
  318. GL.Disable(Cap);
  319. }
  320. }
  321. }
  322. }