OglPipeline.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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> _floatAttribTypes =
  27. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  28. {
  29. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.Float },
  30. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.Float },
  31. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.HalfFloat },
  32. { GalVertexAttribSize._32_32, VertexAttribPointerType.Float },
  33. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.HalfFloat },
  34. { GalVertexAttribSize._16_16, VertexAttribPointerType.HalfFloat },
  35. { GalVertexAttribSize._32, VertexAttribPointerType.Float },
  36. { GalVertexAttribSize._16, VertexAttribPointerType.HalfFloat }
  37. };
  38. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> _signedAttribTypes =
  39. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  40. {
  41. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.Int },
  42. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.Int },
  43. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.Short },
  44. { GalVertexAttribSize._32_32, VertexAttribPointerType.Int },
  45. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.Short },
  46. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.Byte },
  47. { GalVertexAttribSize._16_16, VertexAttribPointerType.Short },
  48. { GalVertexAttribSize._32, VertexAttribPointerType.Int },
  49. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.Byte },
  50. { GalVertexAttribSize._8_8, VertexAttribPointerType.Byte },
  51. { GalVertexAttribSize._16, VertexAttribPointerType.Short },
  52. { GalVertexAttribSize._8, VertexAttribPointerType.Byte },
  53. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.Int2101010Rev }
  54. };
  55. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> _unsignedAttribTypes =
  56. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  57. {
  58. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.UnsignedInt },
  59. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.UnsignedInt },
  60. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.UnsignedShort },
  61. { GalVertexAttribSize._32_32, VertexAttribPointerType.UnsignedInt },
  62. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.UnsignedShort },
  63. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.UnsignedByte },
  64. { GalVertexAttribSize._16_16, VertexAttribPointerType.UnsignedShort },
  65. { GalVertexAttribSize._32, VertexAttribPointerType.UnsignedInt },
  66. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.UnsignedByte },
  67. { GalVertexAttribSize._8_8, VertexAttribPointerType.UnsignedByte },
  68. { GalVertexAttribSize._16, VertexAttribPointerType.UnsignedShort },
  69. { GalVertexAttribSize._8, VertexAttribPointerType.UnsignedByte },
  70. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.UnsignedInt2101010Rev },
  71. { GalVertexAttribSize._11_11_10, VertexAttribPointerType.UnsignedInt10F11F11FRev }
  72. };
  73. private GalPipelineState _old;
  74. private OglConstBuffer _buffer;
  75. private OglRenderTarget _renderTarget;
  76. private OglRasterizer _rasterizer;
  77. private OglShader _shader;
  78. private int _vaoHandle;
  79. public OglPipeline(
  80. OglConstBuffer buffer,
  81. OglRenderTarget renderTarget,
  82. OglRasterizer rasterizer,
  83. OglShader shader)
  84. {
  85. _buffer = buffer;
  86. _renderTarget = renderTarget;
  87. _rasterizer = rasterizer;
  88. _shader = shader;
  89. //These values match OpenGL's defaults
  90. _old = new GalPipelineState
  91. {
  92. FrontFace = GalFrontFace.Ccw,
  93. CullFaceEnabled = false,
  94. CullFace = GalCullFace.Back,
  95. DepthTestEnabled = false,
  96. DepthWriteEnabled = true,
  97. DepthFunc = GalComparisonOp.Less,
  98. DepthRangeNear = 0,
  99. DepthRangeFar = 1,
  100. StencilTestEnabled = false,
  101. StencilBackFuncFunc = GalComparisonOp.Always,
  102. StencilBackFuncRef = 0,
  103. StencilBackFuncMask = UInt32.MaxValue,
  104. StencilBackOpFail = GalStencilOp.Keep,
  105. StencilBackOpZFail = GalStencilOp.Keep,
  106. StencilBackOpZPass = GalStencilOp.Keep,
  107. StencilBackMask = UInt32.MaxValue,
  108. StencilFrontFuncFunc = GalComparisonOp.Always,
  109. StencilFrontFuncRef = 0,
  110. StencilFrontFuncMask = UInt32.MaxValue,
  111. StencilFrontOpFail = GalStencilOp.Keep,
  112. StencilFrontOpZFail = GalStencilOp.Keep,
  113. StencilFrontOpZPass = GalStencilOp.Keep,
  114. StencilFrontMask = UInt32.MaxValue,
  115. BlendIndependent = false,
  116. PrimitiveRestartEnabled = false,
  117. PrimitiveRestartIndex = 0
  118. };
  119. for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++)
  120. {
  121. _old.Blends[index] = BlendState.Default;
  122. _old.ColorMasks[index] = ColorMaskState.Default;
  123. }
  124. }
  125. public void Bind(GalPipelineState New)
  126. {
  127. BindConstBuffers(New);
  128. BindVertexLayout(New);
  129. if (New.FramebufferSrgb != _old.FramebufferSrgb)
  130. {
  131. Enable(EnableCap.FramebufferSrgb, New.FramebufferSrgb);
  132. _renderTarget.FramebufferSrgb = New.FramebufferSrgb;
  133. }
  134. if (New.FlipX != _old.FlipX || New.FlipY != _old.FlipY || New.Instance != _old.Instance)
  135. {
  136. _shader.SetExtraData(New.FlipX, New.FlipY, New.Instance);
  137. }
  138. if (New.FrontFace != _old.FrontFace)
  139. {
  140. GL.FrontFace(OglEnumConverter.GetFrontFace(New.FrontFace));
  141. }
  142. if (New.CullFaceEnabled != _old.CullFaceEnabled)
  143. {
  144. Enable(EnableCap.CullFace, New.CullFaceEnabled);
  145. }
  146. if (New.CullFaceEnabled)
  147. {
  148. if (New.CullFace != _old.CullFace)
  149. {
  150. GL.CullFace(OglEnumConverter.GetCullFace(New.CullFace));
  151. }
  152. }
  153. if (New.DepthTestEnabled != _old.DepthTestEnabled)
  154. {
  155. Enable(EnableCap.DepthTest, New.DepthTestEnabled);
  156. }
  157. if (New.DepthWriteEnabled != _old.DepthWriteEnabled)
  158. {
  159. GL.DepthMask(New.DepthWriteEnabled);
  160. }
  161. if (New.DepthTestEnabled)
  162. {
  163. if (New.DepthFunc != _old.DepthFunc)
  164. {
  165. GL.DepthFunc(OglEnumConverter.GetDepthFunc(New.DepthFunc));
  166. }
  167. }
  168. if (New.DepthRangeNear != _old.DepthRangeNear ||
  169. New.DepthRangeFar != _old.DepthRangeFar)
  170. {
  171. GL.DepthRange(New.DepthRangeNear, New.DepthRangeFar);
  172. }
  173. if (New.StencilTestEnabled != _old.StencilTestEnabled)
  174. {
  175. Enable(EnableCap.StencilTest, New.StencilTestEnabled);
  176. }
  177. if (New.StencilTwoSideEnabled != _old.StencilTwoSideEnabled)
  178. {
  179. Enable((EnableCap)All.StencilTestTwoSideExt, New.StencilTwoSideEnabled);
  180. }
  181. if (New.StencilTestEnabled)
  182. {
  183. if (New.StencilBackFuncFunc != _old.StencilBackFuncFunc ||
  184. New.StencilBackFuncRef != _old.StencilBackFuncRef ||
  185. New.StencilBackFuncMask != _old.StencilBackFuncMask)
  186. {
  187. GL.StencilFuncSeparate(
  188. StencilFace.Back,
  189. OglEnumConverter.GetStencilFunc(New.StencilBackFuncFunc),
  190. New.StencilBackFuncRef,
  191. New.StencilBackFuncMask);
  192. }
  193. if (New.StencilBackOpFail != _old.StencilBackOpFail ||
  194. New.StencilBackOpZFail != _old.StencilBackOpZFail ||
  195. New.StencilBackOpZPass != _old.StencilBackOpZPass)
  196. {
  197. GL.StencilOpSeparate(
  198. StencilFace.Back,
  199. OglEnumConverter.GetStencilOp(New.StencilBackOpFail),
  200. OglEnumConverter.GetStencilOp(New.StencilBackOpZFail),
  201. OglEnumConverter.GetStencilOp(New.StencilBackOpZPass));
  202. }
  203. if (New.StencilBackMask != _old.StencilBackMask)
  204. {
  205. GL.StencilMaskSeparate(StencilFace.Back, New.StencilBackMask);
  206. }
  207. if (New.StencilFrontFuncFunc != _old.StencilFrontFuncFunc ||
  208. New.StencilFrontFuncRef != _old.StencilFrontFuncRef ||
  209. New.StencilFrontFuncMask != _old.StencilFrontFuncMask)
  210. {
  211. GL.StencilFuncSeparate(
  212. StencilFace.Front,
  213. OglEnumConverter.GetStencilFunc(New.StencilFrontFuncFunc),
  214. New.StencilFrontFuncRef,
  215. New.StencilFrontFuncMask);
  216. }
  217. if (New.StencilFrontOpFail != _old.StencilFrontOpFail ||
  218. New.StencilFrontOpZFail != _old.StencilFrontOpZFail ||
  219. New.StencilFrontOpZPass != _old.StencilFrontOpZPass)
  220. {
  221. GL.StencilOpSeparate(
  222. StencilFace.Front,
  223. OglEnumConverter.GetStencilOp(New.StencilFrontOpFail),
  224. OglEnumConverter.GetStencilOp(New.StencilFrontOpZFail),
  225. OglEnumConverter.GetStencilOp(New.StencilFrontOpZPass));
  226. }
  227. if (New.StencilFrontMask != _old.StencilFrontMask)
  228. {
  229. GL.StencilMaskSeparate(StencilFace.Front, New.StencilFrontMask);
  230. }
  231. }
  232. // Scissor Test
  233. // All scissor test are disabled before drawing final framebuffer to screen so we don't need to handle disabling
  234. // Skip if there are no scissor tests to enable
  235. if (New.ScissorTestCount != 0)
  236. {
  237. int scissorsApplied = 0;
  238. bool applyToAll = false;
  239. for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++)
  240. {
  241. if (New.ScissorTestEnabled[index])
  242. {
  243. // If viewport arrays are unavailable apply first scissor test to all or
  244. // there is only 1 scissor test and it's the first, the scissor test applies to all viewports
  245. if (!OglExtension.Required.ViewportArray || (index == 0 && New.ScissorTestCount == 1))
  246. {
  247. GL.Enable(EnableCap.ScissorTest);
  248. applyToAll = true;
  249. }
  250. else
  251. {
  252. GL.Enable(IndexedEnableCap.ScissorTest, index);
  253. }
  254. if (New.ScissorTestEnabled[index] != _old.ScissorTestEnabled[index] ||
  255. New.ScissorTestX[index] != _old.ScissorTestX[index] ||
  256. New.ScissorTestY[index] != _old.ScissorTestY[index] ||
  257. New.ScissorTestWidth[index] != _old.ScissorTestWidth[index] ||
  258. New.ScissorTestHeight[index] != _old.ScissorTestHeight[index])
  259. {
  260. if (applyToAll)
  261. {
  262. GL.Scissor(New.ScissorTestX[index], New.ScissorTestY[index],
  263. New.ScissorTestWidth[index], New.ScissorTestHeight[index]);
  264. }
  265. else
  266. {
  267. GL.ScissorIndexed(index, New.ScissorTestX[index], New.ScissorTestY[index],
  268. New.ScissorTestWidth[index], New.ScissorTestHeight[index]);
  269. }
  270. }
  271. // If all scissor tests have been applied, or viewport arrays are unavailable we can skip remaining iterations
  272. if (!OglExtension.Required.ViewportArray || ++scissorsApplied == New.ScissorTestCount)
  273. {
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. if (New.BlendIndependent)
  280. {
  281. for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++)
  282. {
  283. SetBlendState(index, New.Blends[index], _old.Blends[index]);
  284. }
  285. }
  286. else
  287. {
  288. if (New.BlendIndependent != _old.BlendIndependent)
  289. {
  290. SetAllBlendState(New.Blends[0]);
  291. }
  292. else
  293. {
  294. SetBlendState(New.Blends[0], _old.Blends[0]);
  295. }
  296. }
  297. if (New.ColorMaskCommon)
  298. {
  299. if (New.ColorMaskCommon != _old.ColorMaskCommon || !New.ColorMasks[0].Equals(_old.ColorMasks[0]))
  300. {
  301. GL.ColorMask(
  302. New.ColorMasks[0].Red,
  303. New.ColorMasks[0].Green,
  304. New.ColorMasks[0].Blue,
  305. New.ColorMasks[0].Alpha);
  306. }
  307. }
  308. else
  309. {
  310. for (int index = 0; index < GalPipelineState.RenderTargetsCount; index++)
  311. {
  312. if (!New.ColorMasks[index].Equals(_old.ColorMasks[index]))
  313. {
  314. GL.ColorMask(
  315. index,
  316. New.ColorMasks[index].Red,
  317. New.ColorMasks[index].Green,
  318. New.ColorMasks[index].Blue,
  319. New.ColorMasks[index].Alpha);
  320. }
  321. }
  322. }
  323. if (New.PrimitiveRestartEnabled != _old.PrimitiveRestartEnabled)
  324. {
  325. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  326. }
  327. if (New.PrimitiveRestartEnabled)
  328. {
  329. if (New.PrimitiveRestartIndex != _old.PrimitiveRestartIndex)
  330. {
  331. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  332. }
  333. }
  334. _old = New;
  335. }
  336. public void Unbind(GalPipelineState state)
  337. {
  338. if (state.ScissorTestCount > 0)
  339. {
  340. GL.Disable(EnableCap.ScissorTest);
  341. }
  342. }
  343. private void SetAllBlendState(BlendState New)
  344. {
  345. Enable(EnableCap.Blend, New.Enabled);
  346. if (New.Enabled)
  347. {
  348. if (New.SeparateAlpha)
  349. {
  350. GL.BlendEquationSeparate(
  351. OglEnumConverter.GetBlendEquation(New.EquationRgb),
  352. OglEnumConverter.GetBlendEquation(New.EquationAlpha));
  353. GL.BlendFuncSeparate(
  354. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  355. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstRgb),
  356. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  357. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  358. }
  359. else
  360. {
  361. GL.BlendEquation(OglEnumConverter.GetBlendEquation(New.EquationRgb));
  362. GL.BlendFunc(
  363. OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  364. OglEnumConverter.GetBlendFactor(New.FuncDstRgb));
  365. }
  366. }
  367. }
  368. private void SetBlendState(BlendState New, BlendState old)
  369. {
  370. if (New.Enabled != old.Enabled)
  371. {
  372. Enable(EnableCap.Blend, New.Enabled);
  373. }
  374. if (New.Enabled)
  375. {
  376. if (New.SeparateAlpha)
  377. {
  378. if (New.EquationRgb != old.EquationRgb ||
  379. New.EquationAlpha != old.EquationAlpha)
  380. {
  381. GL.BlendEquationSeparate(
  382. OglEnumConverter.GetBlendEquation(New.EquationRgb),
  383. OglEnumConverter.GetBlendEquation(New.EquationAlpha));
  384. }
  385. if (New.FuncSrcRgb != old.FuncSrcRgb ||
  386. New.FuncDstRgb != old.FuncDstRgb ||
  387. New.FuncSrcAlpha != old.FuncSrcAlpha ||
  388. New.FuncDstAlpha != old.FuncDstAlpha)
  389. {
  390. GL.BlendFuncSeparate(
  391. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  392. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstRgb),
  393. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  394. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  395. }
  396. }
  397. else
  398. {
  399. if (New.EquationRgb != old.EquationRgb)
  400. {
  401. GL.BlendEquation(OglEnumConverter.GetBlendEquation(New.EquationRgb));
  402. }
  403. if (New.FuncSrcRgb != old.FuncSrcRgb ||
  404. New.FuncDstRgb != old.FuncDstRgb)
  405. {
  406. GL.BlendFunc(
  407. OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  408. OglEnumConverter.GetBlendFactor(New.FuncDstRgb));
  409. }
  410. }
  411. }
  412. }
  413. private void SetBlendState(int index, BlendState New, BlendState old)
  414. {
  415. if (New.Enabled != old.Enabled)
  416. {
  417. Enable(IndexedEnableCap.Blend, index, New.Enabled);
  418. }
  419. if (New.Enabled)
  420. {
  421. if (New.SeparateAlpha)
  422. {
  423. if (New.EquationRgb != old.EquationRgb ||
  424. New.EquationAlpha != old.EquationAlpha)
  425. {
  426. GL.BlendEquationSeparate(
  427. index,
  428. OglEnumConverter.GetBlendEquation(New.EquationRgb),
  429. OglEnumConverter.GetBlendEquation(New.EquationAlpha));
  430. }
  431. if (New.FuncSrcRgb != old.FuncSrcRgb ||
  432. New.FuncDstRgb != old.FuncDstRgb ||
  433. New.FuncSrcAlpha != old.FuncSrcAlpha ||
  434. New.FuncDstAlpha != old.FuncDstAlpha)
  435. {
  436. GL.BlendFuncSeparate(
  437. index,
  438. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  439. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstRgb),
  440. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  441. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  442. }
  443. }
  444. else
  445. {
  446. if (New.EquationRgb != old.EquationRgb)
  447. {
  448. GL.BlendEquation(index, OglEnumConverter.GetBlendEquation(New.EquationRgb));
  449. }
  450. if (New.FuncSrcRgb != old.FuncSrcRgb ||
  451. New.FuncDstRgb != old.FuncDstRgb)
  452. {
  453. GL.BlendFunc(
  454. index,
  455. (BlendingFactorSrc) OglEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  456. (BlendingFactorDest)OglEnumConverter.GetBlendFactor(New.FuncDstRgb));
  457. }
  458. }
  459. }
  460. }
  461. private void BindConstBuffers(GalPipelineState New)
  462. {
  463. int freeBinding = OglShader.ReservedCbufCount;
  464. void BindIfNotNull(OglShaderStage stage)
  465. {
  466. if (stage != null)
  467. {
  468. foreach (ShaderDeclInfo declInfo in stage.ConstBufferUsage)
  469. {
  470. long key = New.ConstBufferKeys[(int)stage.Type][declInfo.Cbuf];
  471. if (key != 0 && _buffer.TryGetUbo(key, out int uboHandle))
  472. {
  473. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, freeBinding, uboHandle);
  474. }
  475. freeBinding++;
  476. }
  477. }
  478. }
  479. BindIfNotNull(_shader.Current.Vertex);
  480. BindIfNotNull(_shader.Current.TessControl);
  481. BindIfNotNull(_shader.Current.TessEvaluation);
  482. BindIfNotNull(_shader.Current.Geometry);
  483. BindIfNotNull(_shader.Current.Fragment);
  484. }
  485. private void BindVertexLayout(GalPipelineState New)
  486. {
  487. foreach (GalVertexBinding binding in New.VertexBindings)
  488. {
  489. if (!binding.Enabled || !_rasterizer.TryGetVbo(binding.VboKey, out int vboHandle))
  490. {
  491. continue;
  492. }
  493. if (_vaoHandle == 0)
  494. {
  495. _vaoHandle = GL.GenVertexArray();
  496. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  497. //if you want to use it, move this line out of the if
  498. GL.BindVertexArray(_vaoHandle);
  499. }
  500. foreach (GalVertexAttrib attrib in binding.Attribs)
  501. {
  502. //Skip uninitialized attributes.
  503. if (attrib.Size == 0)
  504. {
  505. continue;
  506. }
  507. GL.BindBuffer(BufferTarget.ArrayBuffer, vboHandle);
  508. bool unsigned =
  509. attrib.Type == GalVertexAttribType.Unorm ||
  510. attrib.Type == GalVertexAttribType.Uint ||
  511. attrib.Type == GalVertexAttribType.Uscaled;
  512. bool normalize =
  513. attrib.Type == GalVertexAttribType.Snorm ||
  514. attrib.Type == GalVertexAttribType.Unorm;
  515. VertexAttribPointerType type = 0;
  516. if (attrib.Type == GalVertexAttribType.Float)
  517. {
  518. type = GetType(_floatAttribTypes, attrib);
  519. }
  520. else
  521. {
  522. if (unsigned)
  523. {
  524. type = GetType(_unsignedAttribTypes, attrib);
  525. }
  526. else
  527. {
  528. type = GetType(_signedAttribTypes, attrib);
  529. }
  530. }
  531. if (!_attribElements.TryGetValue(attrib.Size, out int size))
  532. {
  533. throw new InvalidOperationException("Invalid attribute size \"" + attrib.Size + "\"!");
  534. }
  535. int offset = attrib.Offset;
  536. if (binding.Stride != 0)
  537. {
  538. GL.EnableVertexAttribArray(attrib.Index);
  539. if (attrib.Type == GalVertexAttribType.Sint ||
  540. attrib.Type == GalVertexAttribType.Uint)
  541. {
  542. IntPtr pointer = new IntPtr(offset);
  543. VertexAttribIntegerType iType = (VertexAttribIntegerType)type;
  544. GL.VertexAttribIPointer(attrib.Index, size, iType, binding.Stride, pointer);
  545. }
  546. else
  547. {
  548. GL.VertexAttribPointer(attrib.Index, size, type, normalize, binding.Stride, offset);
  549. }
  550. }
  551. else
  552. {
  553. GL.DisableVertexAttribArray(attrib.Index);
  554. SetConstAttrib(attrib);
  555. }
  556. if (binding.Instanced && binding.Divisor != 0)
  557. {
  558. GL.VertexAttribDivisor(attrib.Index, 1);
  559. }
  560. else
  561. {
  562. GL.VertexAttribDivisor(attrib.Index, 0);
  563. }
  564. }
  565. }
  566. }
  567. private static VertexAttribPointerType GetType(Dictionary<GalVertexAttribSize, VertexAttribPointerType> dict, GalVertexAttrib attrib)
  568. {
  569. if (!dict.TryGetValue(attrib.Size, out VertexAttribPointerType type))
  570. {
  571. ThrowUnsupportedAttrib(attrib);
  572. }
  573. return type;
  574. }
  575. private static unsafe void SetConstAttrib(GalVertexAttrib attrib)
  576. {
  577. if (attrib.Size == GalVertexAttribSize._10_10_10_2 ||
  578. attrib.Size == GalVertexAttribSize._11_11_10)
  579. {
  580. ThrowUnsupportedAttrib(attrib);
  581. }
  582. fixed (byte* ptr = attrib.Data)
  583. {
  584. if (attrib.Type == GalVertexAttribType.Unorm)
  585. {
  586. switch (attrib.Size)
  587. {
  588. case GalVertexAttribSize._8:
  589. case GalVertexAttribSize._8_8:
  590. case GalVertexAttribSize._8_8_8:
  591. case GalVertexAttribSize._8_8_8_8:
  592. GL.VertexAttrib4N((uint)attrib.Index, ptr);
  593. break;
  594. case GalVertexAttribSize._16:
  595. case GalVertexAttribSize._16_16:
  596. case GalVertexAttribSize._16_16_16:
  597. case GalVertexAttribSize._16_16_16_16:
  598. GL.VertexAttrib4N((uint)attrib.Index, (ushort*)ptr);
  599. break;
  600. case GalVertexAttribSize._32:
  601. case GalVertexAttribSize._32_32:
  602. case GalVertexAttribSize._32_32_32:
  603. case GalVertexAttribSize._32_32_32_32:
  604. GL.VertexAttrib4N((uint)attrib.Index, (uint*)ptr);
  605. break;
  606. }
  607. }
  608. else if (attrib.Type == GalVertexAttribType.Snorm)
  609. {
  610. switch (attrib.Size)
  611. {
  612. case GalVertexAttribSize._8:
  613. case GalVertexAttribSize._8_8:
  614. case GalVertexAttribSize._8_8_8:
  615. case GalVertexAttribSize._8_8_8_8:
  616. GL.VertexAttrib4N((uint)attrib.Index, (sbyte*)ptr);
  617. break;
  618. case GalVertexAttribSize._16:
  619. case GalVertexAttribSize._16_16:
  620. case GalVertexAttribSize._16_16_16:
  621. case GalVertexAttribSize._16_16_16_16:
  622. GL.VertexAttrib4N((uint)attrib.Index, (short*)ptr);
  623. break;
  624. case GalVertexAttribSize._32:
  625. case GalVertexAttribSize._32_32:
  626. case GalVertexAttribSize._32_32_32:
  627. case GalVertexAttribSize._32_32_32_32:
  628. GL.VertexAttrib4N((uint)attrib.Index, (int*)ptr);
  629. break;
  630. }
  631. }
  632. else if (attrib.Type == GalVertexAttribType.Uint)
  633. {
  634. switch (attrib.Size)
  635. {
  636. case GalVertexAttribSize._8:
  637. case GalVertexAttribSize._8_8:
  638. case GalVertexAttribSize._8_8_8:
  639. case GalVertexAttribSize._8_8_8_8:
  640. GL.VertexAttribI4((uint)attrib.Index, ptr);
  641. break;
  642. case GalVertexAttribSize._16:
  643. case GalVertexAttribSize._16_16:
  644. case GalVertexAttribSize._16_16_16:
  645. case GalVertexAttribSize._16_16_16_16:
  646. GL.VertexAttribI4((uint)attrib.Index, (ushort*)ptr);
  647. break;
  648. case GalVertexAttribSize._32:
  649. case GalVertexAttribSize._32_32:
  650. case GalVertexAttribSize._32_32_32:
  651. case GalVertexAttribSize._32_32_32_32:
  652. GL.VertexAttribI4((uint)attrib.Index, (uint*)ptr);
  653. break;
  654. }
  655. }
  656. else if (attrib.Type == GalVertexAttribType.Sint)
  657. {
  658. switch (attrib.Size)
  659. {
  660. case GalVertexAttribSize._8:
  661. case GalVertexAttribSize._8_8:
  662. case GalVertexAttribSize._8_8_8:
  663. case GalVertexAttribSize._8_8_8_8:
  664. GL.VertexAttribI4((uint)attrib.Index, (sbyte*)ptr);
  665. break;
  666. case GalVertexAttribSize._16:
  667. case GalVertexAttribSize._16_16:
  668. case GalVertexAttribSize._16_16_16:
  669. case GalVertexAttribSize._16_16_16_16:
  670. GL.VertexAttribI4((uint)attrib.Index, (short*)ptr);
  671. break;
  672. case GalVertexAttribSize._32:
  673. case GalVertexAttribSize._32_32:
  674. case GalVertexAttribSize._32_32_32:
  675. case GalVertexAttribSize._32_32_32_32:
  676. GL.VertexAttribI4((uint)attrib.Index, (int*)ptr);
  677. break;
  678. }
  679. }
  680. else if (attrib.Type == GalVertexAttribType.Float)
  681. {
  682. switch (attrib.Size)
  683. {
  684. case GalVertexAttribSize._32:
  685. case GalVertexAttribSize._32_32:
  686. case GalVertexAttribSize._32_32_32:
  687. case GalVertexAttribSize._32_32_32_32:
  688. GL.VertexAttrib4(attrib.Index, (float*)ptr);
  689. break;
  690. default: ThrowUnsupportedAttrib(attrib); break;
  691. }
  692. }
  693. }
  694. }
  695. private static void ThrowUnsupportedAttrib(GalVertexAttrib attrib)
  696. {
  697. throw new NotImplementedException("Unsupported size \"" + attrib.Size + "\" on type \"" + attrib.Type + "\"!");
  698. }
  699. private void Enable(EnableCap cap, bool enabled)
  700. {
  701. if (enabled)
  702. {
  703. GL.Enable(cap);
  704. }
  705. else
  706. {
  707. GL.Disable(cap);
  708. }
  709. }
  710. private void Enable(IndexedEnableCap cap, int index, bool enabled)
  711. {
  712. if (enabled)
  713. {
  714. GL.Enable(cap, index);
  715. }
  716. else
  717. {
  718. GL.Disable(cap, index);
  719. }
  720. }
  721. public void ResetDepthMask()
  722. {
  723. _old.DepthWriteEnabled = true;
  724. }
  725. public void ResetColorMask(int index)
  726. {
  727. _old.ColorMasks[index] = ColorMaskState.Default;
  728. }
  729. }
  730. }