OGLPipeline.cs 17 KB

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