OGLPipeline.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. this.Buffer = Buffer;
  86. this.RenderTarget = RenderTarget;
  87. this.Rasterizer = Rasterizer;
  88. this.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. bool forceUpdate;
  234. for (int Index = 0; Index < New.ScissorTestCount; Index++)
  235. {
  236. forceUpdate = false;
  237. if (New.ScissorTestEnabled[Index])
  238. {
  239. // If there is only 1 scissor test, geometry shaders are disabled so the scissor test applies to all viewports
  240. if (New.ScissorTestCount == 1)
  241. {
  242. GL.Enable(EnableCap.ScissorTest);
  243. }
  244. else
  245. {
  246. GL.Enable(IndexedEnableCap.ScissorTest, Index);
  247. }
  248. forceUpdate = true;
  249. }
  250. else
  251. {
  252. GL.Disable(IndexedEnableCap.ScissorTest, Index);
  253. }
  254. if (New.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. forceUpdate)) // Force update intentionally last to reduce if comparisons
  260. {
  261. // If there is only 1 scissor test geometry shaders are disables so the scissor test applies to all viewports
  262. if (New.ScissorTestCount == 1)
  263. {
  264. GL.Scissor(New.ScissorTestX[Index], New.ScissorTestY[Index],
  265. New.ScissorTestWidth[Index], New.ScissorTestHeight[Index]);
  266. }
  267. else
  268. {
  269. GL.ScissorIndexed(Index, New.ScissorTestX[Index], New.ScissorTestY[Index],
  270. New.ScissorTestWidth[Index], New.ScissorTestHeight[Index]);
  271. }
  272. }
  273. }
  274. if (New.BlendIndependent)
  275. {
  276. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  277. {
  278. SetBlendState(Index, New.Blends[Index], Old.Blends[Index]);
  279. }
  280. }
  281. else
  282. {
  283. if (New.BlendIndependent != Old.BlendIndependent)
  284. {
  285. SetAllBlendState(New.Blends[0]);
  286. }
  287. else
  288. {
  289. SetBlendState(New.Blends[0], Old.Blends[0]);
  290. }
  291. }
  292. if (New.ColorMaskCommon)
  293. {
  294. if (New.ColorMaskCommon != Old.ColorMaskCommon || !New.ColorMasks[0].Equals(Old.ColorMasks[0]))
  295. {
  296. GL.ColorMask(
  297. New.ColorMasks[0].Red,
  298. New.ColorMasks[0].Green,
  299. New.ColorMasks[0].Blue,
  300. New.ColorMasks[0].Alpha);
  301. }
  302. }
  303. else
  304. {
  305. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  306. {
  307. if (!New.ColorMasks[Index].Equals(Old.ColorMasks[Index]))
  308. {
  309. GL.ColorMask(
  310. Index,
  311. New.ColorMasks[Index].Red,
  312. New.ColorMasks[Index].Green,
  313. New.ColorMasks[Index].Blue,
  314. New.ColorMasks[Index].Alpha);
  315. }
  316. }
  317. }
  318. if (New.PrimitiveRestartEnabled != Old.PrimitiveRestartEnabled)
  319. {
  320. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  321. }
  322. if (New.PrimitiveRestartEnabled)
  323. {
  324. if (New.PrimitiveRestartIndex != Old.PrimitiveRestartIndex)
  325. {
  326. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  327. }
  328. }
  329. Old = New;
  330. }
  331. private void SetAllBlendState(BlendState New)
  332. {
  333. Enable(EnableCap.Blend, New.Enabled);
  334. if (New.Enabled)
  335. {
  336. if (New.SeparateAlpha)
  337. {
  338. GL.BlendEquationSeparate(
  339. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  340. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  341. GL.BlendFuncSeparate(
  342. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  343. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  344. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  345. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  346. }
  347. else
  348. {
  349. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  350. GL.BlendFunc(
  351. OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  352. OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  353. }
  354. }
  355. }
  356. private void SetBlendState(BlendState New, BlendState Old)
  357. {
  358. if (New.Enabled != Old.Enabled)
  359. {
  360. Enable(EnableCap.Blend, New.Enabled);
  361. }
  362. if (New.Enabled)
  363. {
  364. if (New.SeparateAlpha)
  365. {
  366. if (New.EquationRgb != Old.EquationRgb ||
  367. New.EquationAlpha != Old.EquationAlpha)
  368. {
  369. GL.BlendEquationSeparate(
  370. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  371. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  372. }
  373. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  374. New.FuncDstRgb != Old.FuncDstRgb ||
  375. New.FuncSrcAlpha != Old.FuncSrcAlpha ||
  376. New.FuncDstAlpha != Old.FuncDstAlpha)
  377. {
  378. GL.BlendFuncSeparate(
  379. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  380. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  381. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  382. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  383. }
  384. }
  385. else
  386. {
  387. if (New.EquationRgb != Old.EquationRgb)
  388. {
  389. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  390. }
  391. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  392. New.FuncDstRgb != Old.FuncDstRgb)
  393. {
  394. GL.BlendFunc(
  395. OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  396. OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  397. }
  398. }
  399. }
  400. }
  401. private void SetBlendState(int Index, BlendState New, BlendState Old)
  402. {
  403. if (New.Enabled != Old.Enabled)
  404. {
  405. Enable(IndexedEnableCap.Blend, Index, New.Enabled);
  406. }
  407. if (New.Enabled)
  408. {
  409. if (New.SeparateAlpha)
  410. {
  411. if (New.EquationRgb != Old.EquationRgb ||
  412. New.EquationAlpha != Old.EquationAlpha)
  413. {
  414. GL.BlendEquationSeparate(
  415. Index,
  416. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  417. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  418. }
  419. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  420. New.FuncDstRgb != Old.FuncDstRgb ||
  421. New.FuncSrcAlpha != Old.FuncSrcAlpha ||
  422. New.FuncDstAlpha != Old.FuncDstAlpha)
  423. {
  424. GL.BlendFuncSeparate(
  425. Index,
  426. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  427. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  428. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  429. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  430. }
  431. }
  432. else
  433. {
  434. if (New.EquationRgb != Old.EquationRgb)
  435. {
  436. GL.BlendEquation(Index, OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  437. }
  438. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  439. New.FuncDstRgb != Old.FuncDstRgb)
  440. {
  441. GL.BlendFunc(
  442. Index,
  443. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  444. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  445. }
  446. }
  447. }
  448. }
  449. private void BindConstBuffers(GalPipelineState New)
  450. {
  451. int FreeBinding = OGLShader.ReservedCbufCount;
  452. void BindIfNotNull(OGLShaderStage Stage)
  453. {
  454. if (Stage != null)
  455. {
  456. foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
  457. {
  458. long Key = New.ConstBufferKeys[(int)Stage.Type][DeclInfo.Cbuf];
  459. if (Key != 0 && Buffer.TryGetUbo(Key, out int UboHandle))
  460. {
  461. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, UboHandle);
  462. }
  463. FreeBinding++;
  464. }
  465. }
  466. }
  467. BindIfNotNull(Shader.Current.Vertex);
  468. BindIfNotNull(Shader.Current.TessControl);
  469. BindIfNotNull(Shader.Current.TessEvaluation);
  470. BindIfNotNull(Shader.Current.Geometry);
  471. BindIfNotNull(Shader.Current.Fragment);
  472. }
  473. private void BindVertexLayout(GalPipelineState New)
  474. {
  475. foreach (GalVertexBinding Binding in New.VertexBindings)
  476. {
  477. if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
  478. {
  479. continue;
  480. }
  481. if (VaoHandle == 0)
  482. {
  483. VaoHandle = GL.GenVertexArray();
  484. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  485. //if you want to use it, move this line out of the if
  486. GL.BindVertexArray(VaoHandle);
  487. }
  488. foreach (GalVertexAttrib Attrib in Binding.Attribs)
  489. {
  490. //Skip uninitialized attributes.
  491. if (Attrib.Size == 0)
  492. {
  493. continue;
  494. }
  495. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  496. bool Unsigned =
  497. Attrib.Type == GalVertexAttribType.Unorm ||
  498. Attrib.Type == GalVertexAttribType.Uint ||
  499. Attrib.Type == GalVertexAttribType.Uscaled;
  500. bool Normalize =
  501. Attrib.Type == GalVertexAttribType.Snorm ||
  502. Attrib.Type == GalVertexAttribType.Unorm;
  503. VertexAttribPointerType Type = 0;
  504. if (Attrib.Type == GalVertexAttribType.Float)
  505. {
  506. Type = GetType(FloatAttribTypes, Attrib);
  507. }
  508. else
  509. {
  510. if (Unsigned)
  511. {
  512. Type = GetType(UnsignedAttribTypes, Attrib);
  513. }
  514. else
  515. {
  516. Type = GetType(SignedAttribTypes, Attrib);
  517. }
  518. }
  519. if (!AttribElements.TryGetValue(Attrib.Size, out int Size))
  520. {
  521. throw new InvalidOperationException("Invalid attribute size \"" + Attrib.Size + "\"!");
  522. }
  523. int Offset = Attrib.Offset;
  524. if (Binding.Stride != 0)
  525. {
  526. GL.EnableVertexAttribArray(Attrib.Index);
  527. if (Attrib.Type == GalVertexAttribType.Sint ||
  528. Attrib.Type == GalVertexAttribType.Uint)
  529. {
  530. IntPtr Pointer = new IntPtr(Offset);
  531. VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;
  532. GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
  533. }
  534. else
  535. {
  536. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
  537. }
  538. }
  539. else
  540. {
  541. GL.DisableVertexAttribArray(Attrib.Index);
  542. SetConstAttrib(Attrib);
  543. }
  544. if (Binding.Instanced && Binding.Divisor != 0)
  545. {
  546. GL.VertexAttribDivisor(Attrib.Index, 1);
  547. }
  548. else
  549. {
  550. GL.VertexAttribDivisor(Attrib.Index, 0);
  551. }
  552. }
  553. }
  554. }
  555. private static VertexAttribPointerType GetType(Dictionary<GalVertexAttribSize, VertexAttribPointerType> Dict, GalVertexAttrib Attrib)
  556. {
  557. if (!Dict.TryGetValue(Attrib.Size, out VertexAttribPointerType Type))
  558. {
  559. ThrowUnsupportedAttrib(Attrib);
  560. }
  561. return Type;
  562. }
  563. private unsafe static void SetConstAttrib(GalVertexAttrib Attrib)
  564. {
  565. if (Attrib.Size == GalVertexAttribSize._10_10_10_2 ||
  566. Attrib.Size == GalVertexAttribSize._11_11_10)
  567. {
  568. ThrowUnsupportedAttrib(Attrib);
  569. }
  570. fixed (byte* Ptr = Attrib.Data)
  571. {
  572. if (Attrib.Type == GalVertexAttribType.Unorm)
  573. {
  574. switch (Attrib.Size)
  575. {
  576. case GalVertexAttribSize._8:
  577. case GalVertexAttribSize._8_8:
  578. case GalVertexAttribSize._8_8_8:
  579. case GalVertexAttribSize._8_8_8_8:
  580. GL.VertexAttrib4N((uint)Attrib.Index, Ptr);
  581. break;
  582. case GalVertexAttribSize._16:
  583. case GalVertexAttribSize._16_16:
  584. case GalVertexAttribSize._16_16_16:
  585. case GalVertexAttribSize._16_16_16_16:
  586. GL.VertexAttrib4N((uint)Attrib.Index, (ushort*)Ptr);
  587. break;
  588. case GalVertexAttribSize._32:
  589. case GalVertexAttribSize._32_32:
  590. case GalVertexAttribSize._32_32_32:
  591. case GalVertexAttribSize._32_32_32_32:
  592. GL.VertexAttrib4N((uint)Attrib.Index, (uint*)Ptr);
  593. break;
  594. }
  595. }
  596. else if (Attrib.Type == GalVertexAttribType.Snorm)
  597. {
  598. switch (Attrib.Size)
  599. {
  600. case GalVertexAttribSize._8:
  601. case GalVertexAttribSize._8_8:
  602. case GalVertexAttribSize._8_8_8:
  603. case GalVertexAttribSize._8_8_8_8:
  604. GL.VertexAttrib4N((uint)Attrib.Index, (sbyte*)Ptr);
  605. break;
  606. case GalVertexAttribSize._16:
  607. case GalVertexAttribSize._16_16:
  608. case GalVertexAttribSize._16_16_16:
  609. case GalVertexAttribSize._16_16_16_16:
  610. GL.VertexAttrib4N((uint)Attrib.Index, (short*)Ptr);
  611. break;
  612. case GalVertexAttribSize._32:
  613. case GalVertexAttribSize._32_32:
  614. case GalVertexAttribSize._32_32_32:
  615. case GalVertexAttribSize._32_32_32_32:
  616. GL.VertexAttrib4N((uint)Attrib.Index, (int*)Ptr);
  617. break;
  618. }
  619. }
  620. else if (Attrib.Type == GalVertexAttribType.Uint)
  621. {
  622. switch (Attrib.Size)
  623. {
  624. case GalVertexAttribSize._8:
  625. case GalVertexAttribSize._8_8:
  626. case GalVertexAttribSize._8_8_8:
  627. case GalVertexAttribSize._8_8_8_8:
  628. GL.VertexAttribI4((uint)Attrib.Index, Ptr);
  629. break;
  630. case GalVertexAttribSize._16:
  631. case GalVertexAttribSize._16_16:
  632. case GalVertexAttribSize._16_16_16:
  633. case GalVertexAttribSize._16_16_16_16:
  634. GL.VertexAttribI4((uint)Attrib.Index, (ushort*)Ptr);
  635. break;
  636. case GalVertexAttribSize._32:
  637. case GalVertexAttribSize._32_32:
  638. case GalVertexAttribSize._32_32_32:
  639. case GalVertexAttribSize._32_32_32_32:
  640. GL.VertexAttribI4((uint)Attrib.Index, (uint*)Ptr);
  641. break;
  642. }
  643. }
  644. else if (Attrib.Type == GalVertexAttribType.Sint)
  645. {
  646. switch (Attrib.Size)
  647. {
  648. case GalVertexAttribSize._8:
  649. case GalVertexAttribSize._8_8:
  650. case GalVertexAttribSize._8_8_8:
  651. case GalVertexAttribSize._8_8_8_8:
  652. GL.VertexAttribI4((uint)Attrib.Index, (sbyte*)Ptr);
  653. break;
  654. case GalVertexAttribSize._16:
  655. case GalVertexAttribSize._16_16:
  656. case GalVertexAttribSize._16_16_16:
  657. case GalVertexAttribSize._16_16_16_16:
  658. GL.VertexAttribI4((uint)Attrib.Index, (short*)Ptr);
  659. break;
  660. case GalVertexAttribSize._32:
  661. case GalVertexAttribSize._32_32:
  662. case GalVertexAttribSize._32_32_32:
  663. case GalVertexAttribSize._32_32_32_32:
  664. GL.VertexAttribI4((uint)Attrib.Index, (int*)Ptr);
  665. break;
  666. }
  667. }
  668. else if (Attrib.Type == GalVertexAttribType.Float)
  669. {
  670. switch (Attrib.Size)
  671. {
  672. case GalVertexAttribSize._32:
  673. case GalVertexAttribSize._32_32:
  674. case GalVertexAttribSize._32_32_32:
  675. case GalVertexAttribSize._32_32_32_32:
  676. GL.VertexAttrib4(Attrib.Index, (float*)Ptr);
  677. break;
  678. default: ThrowUnsupportedAttrib(Attrib); break;
  679. }
  680. }
  681. }
  682. }
  683. private static void ThrowUnsupportedAttrib(GalVertexAttrib Attrib)
  684. {
  685. throw new NotImplementedException("Unsupported size \"" + Attrib.Size + "\" on type \"" + Attrib.Type + "\"!");
  686. }
  687. private void Enable(EnableCap Cap, bool Enabled)
  688. {
  689. if (Enabled)
  690. {
  691. GL.Enable(Cap);
  692. }
  693. else
  694. {
  695. GL.Disable(Cap);
  696. }
  697. }
  698. private void Enable(IndexedEnableCap Cap, int Index, bool Enabled)
  699. {
  700. if (Enabled)
  701. {
  702. GL.Enable(Cap, Index);
  703. }
  704. else
  705. {
  706. GL.Disable(Cap, Index);
  707. }
  708. }
  709. public void ResetDepthMask()
  710. {
  711. Old.DepthWriteEnabled = true;
  712. }
  713. public void ResetColorMask(int Index)
  714. {
  715. Old.ColorMasks[Index] = ColorMaskState.Default;
  716. }
  717. }
  718. }