OGLPipeline.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 OGLRasterizer Rasterizer;
  76. private OGLShader Shader;
  77. private int VaoHandle;
  78. public OGLPipeline(OGLConstBuffer Buffer, OGLRasterizer Rasterizer, OGLShader Shader)
  79. {
  80. this.Buffer = Buffer;
  81. this.Rasterizer = Rasterizer;
  82. this.Shader = Shader;
  83. //These values match OpenGL's defaults
  84. Old = new GalPipelineState
  85. {
  86. FrontFace = GalFrontFace.CCW,
  87. CullFaceEnabled = false,
  88. CullFace = GalCullFace.Back,
  89. DepthTestEnabled = false,
  90. DepthWriteEnabled = true,
  91. DepthFunc = GalComparisonOp.Less,
  92. DepthRangeNear = 0,
  93. DepthRangeFar = 1,
  94. StencilTestEnabled = false,
  95. StencilBackFuncFunc = GalComparisonOp.Always,
  96. StencilBackFuncRef = 0,
  97. StencilBackFuncMask = UInt32.MaxValue,
  98. StencilBackOpFail = GalStencilOp.Keep,
  99. StencilBackOpZFail = GalStencilOp.Keep,
  100. StencilBackOpZPass = GalStencilOp.Keep,
  101. StencilBackMask = UInt32.MaxValue,
  102. StencilFrontFuncFunc = GalComparisonOp.Always,
  103. StencilFrontFuncRef = 0,
  104. StencilFrontFuncMask = UInt32.MaxValue,
  105. StencilFrontOpFail = GalStencilOp.Keep,
  106. StencilFrontOpZFail = GalStencilOp.Keep,
  107. StencilFrontOpZPass = GalStencilOp.Keep,
  108. StencilFrontMask = UInt32.MaxValue,
  109. BlendIndependent = false,
  110. PrimitiveRestartEnabled = false,
  111. PrimitiveRestartIndex = 0
  112. };
  113. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  114. {
  115. Old.Blends[Index] = BlendState.Default;
  116. Old.ColorMasks[Index] = ColorMaskState.Default;
  117. }
  118. }
  119. public void Bind(GalPipelineState New)
  120. {
  121. BindConstBuffers(New);
  122. BindVertexLayout(New);
  123. if (New.FramebufferSrgb != Old.FramebufferSrgb)
  124. {
  125. Enable(EnableCap.FramebufferSrgb, New.FramebufferSrgb);
  126. }
  127. if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY || New.Instance != Old.Instance)
  128. {
  129. Shader.SetExtraData(New.FlipX, New.FlipY, New.Instance);
  130. }
  131. //Note: Uncomment SetFrontFace and SetCullFace when flipping issues are solved
  132. //if (New.FrontFace != Old.FrontFace)
  133. //{
  134. // GL.FrontFace(OGLEnumConverter.GetFrontFace(New.FrontFace));
  135. //}
  136. //if (New.CullFaceEnabled != Old.CullFaceEnabled)
  137. //{
  138. // Enable(EnableCap.CullFace, New.CullFaceEnabled);
  139. //}
  140. //if (New.CullFaceEnabled)
  141. //{
  142. // if (New.CullFace != Old.CullFace)
  143. // {
  144. // GL.CullFace(OGLEnumConverter.GetCullFace(New.CullFace));
  145. // }
  146. //}
  147. if (New.DepthTestEnabled != Old.DepthTestEnabled)
  148. {
  149. Enable(EnableCap.DepthTest, New.DepthTestEnabled);
  150. }
  151. if (New.DepthWriteEnabled != Old.DepthWriteEnabled)
  152. {
  153. GL.DepthMask(New.DepthWriteEnabled);
  154. }
  155. if (New.DepthTestEnabled)
  156. {
  157. if (New.DepthFunc != Old.DepthFunc)
  158. {
  159. GL.DepthFunc(OGLEnumConverter.GetDepthFunc(New.DepthFunc));
  160. }
  161. }
  162. if (New.DepthRangeNear != Old.DepthRangeNear ||
  163. New.DepthRangeFar != Old.DepthRangeFar)
  164. {
  165. GL.DepthRange(New.DepthRangeNear, New.DepthRangeFar);
  166. }
  167. if (New.StencilTestEnabled != Old.StencilTestEnabled)
  168. {
  169. Enable(EnableCap.StencilTest, New.StencilTestEnabled);
  170. }
  171. if (New.StencilTwoSideEnabled != Old.StencilTwoSideEnabled)
  172. {
  173. Enable((EnableCap)All.StencilTestTwoSideExt, New.StencilTwoSideEnabled);
  174. }
  175. if (New.StencilTestEnabled)
  176. {
  177. if (New.StencilBackFuncFunc != Old.StencilBackFuncFunc ||
  178. New.StencilBackFuncRef != Old.StencilBackFuncRef ||
  179. New.StencilBackFuncMask != Old.StencilBackFuncMask)
  180. {
  181. GL.StencilFuncSeparate(
  182. StencilFace.Back,
  183. OGLEnumConverter.GetStencilFunc(New.StencilBackFuncFunc),
  184. New.StencilBackFuncRef,
  185. New.StencilBackFuncMask);
  186. }
  187. if (New.StencilBackOpFail != Old.StencilBackOpFail ||
  188. New.StencilBackOpZFail != Old.StencilBackOpZFail ||
  189. New.StencilBackOpZPass != Old.StencilBackOpZPass)
  190. {
  191. GL.StencilOpSeparate(
  192. StencilFace.Back,
  193. OGLEnumConverter.GetStencilOp(New.StencilBackOpFail),
  194. OGLEnumConverter.GetStencilOp(New.StencilBackOpZFail),
  195. OGLEnumConverter.GetStencilOp(New.StencilBackOpZPass));
  196. }
  197. if (New.StencilBackMask != Old.StencilBackMask)
  198. {
  199. GL.StencilMaskSeparate(StencilFace.Back, New.StencilBackMask);
  200. }
  201. if (New.StencilFrontFuncFunc != Old.StencilFrontFuncFunc ||
  202. New.StencilFrontFuncRef != Old.StencilFrontFuncRef ||
  203. New.StencilFrontFuncMask != Old.StencilFrontFuncMask)
  204. {
  205. GL.StencilFuncSeparate(
  206. StencilFace.Front,
  207. OGLEnumConverter.GetStencilFunc(New.StencilFrontFuncFunc),
  208. New.StencilFrontFuncRef,
  209. New.StencilFrontFuncMask);
  210. }
  211. if (New.StencilFrontOpFail != Old.StencilFrontOpFail ||
  212. New.StencilFrontOpZFail != Old.StencilFrontOpZFail ||
  213. New.StencilFrontOpZPass != Old.StencilFrontOpZPass)
  214. {
  215. GL.StencilOpSeparate(
  216. StencilFace.Front,
  217. OGLEnumConverter.GetStencilOp(New.StencilFrontOpFail),
  218. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZFail),
  219. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZPass));
  220. }
  221. if (New.StencilFrontMask != Old.StencilFrontMask)
  222. {
  223. GL.StencilMaskSeparate(StencilFace.Front, New.StencilFrontMask);
  224. }
  225. }
  226. if (New.BlendIndependent)
  227. {
  228. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  229. {
  230. SetBlendState(Index, New.Blends[Index], Old.Blends[Index]);
  231. }
  232. }
  233. else
  234. {
  235. if (New.BlendIndependent != Old.BlendIndependent)
  236. {
  237. SetAllBlendState(New.Blends[0]);
  238. }
  239. else
  240. {
  241. SetBlendState(New.Blends[0], Old.Blends[0]);
  242. }
  243. }
  244. if (New.ColorMaskCommon)
  245. {
  246. if (New.ColorMaskCommon != Old.ColorMaskCommon || !New.ColorMasks[0].Equals(Old.ColorMasks[0]))
  247. {
  248. GL.ColorMask(
  249. New.ColorMasks[0].Red,
  250. New.ColorMasks[0].Green,
  251. New.ColorMasks[0].Blue,
  252. New.ColorMasks[0].Alpha);
  253. }
  254. }
  255. else
  256. {
  257. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  258. {
  259. if (!New.ColorMasks[Index].Equals(Old.ColorMasks[Index]))
  260. {
  261. GL.ColorMask(
  262. Index,
  263. New.ColorMasks[Index].Red,
  264. New.ColorMasks[Index].Green,
  265. New.ColorMasks[Index].Blue,
  266. New.ColorMasks[Index].Alpha);
  267. }
  268. }
  269. }
  270. if (New.PrimitiveRestartEnabled != Old.PrimitiveRestartEnabled)
  271. {
  272. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  273. }
  274. if (New.PrimitiveRestartEnabled)
  275. {
  276. if (New.PrimitiveRestartIndex != Old.PrimitiveRestartIndex)
  277. {
  278. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  279. }
  280. }
  281. Old = New;
  282. }
  283. private void SetAllBlendState(BlendState New)
  284. {
  285. Enable(EnableCap.Blend, New.Enabled);
  286. if (New.Enabled)
  287. {
  288. if (New.SeparateAlpha)
  289. {
  290. GL.BlendEquationSeparate(
  291. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  292. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  293. GL.BlendFuncSeparate(
  294. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  295. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  296. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  297. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  298. }
  299. else
  300. {
  301. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  302. GL.BlendFunc(
  303. OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  304. OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  305. }
  306. }
  307. }
  308. private void SetBlendState(BlendState New, BlendState Old)
  309. {
  310. if (New.Enabled != Old.Enabled)
  311. {
  312. Enable(EnableCap.Blend, New.Enabled);
  313. }
  314. if (New.Enabled)
  315. {
  316. if (New.SeparateAlpha)
  317. {
  318. if (New.EquationRgb != Old.EquationRgb ||
  319. New.EquationAlpha != Old.EquationAlpha)
  320. {
  321. GL.BlendEquationSeparate(
  322. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  323. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  324. }
  325. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  326. New.FuncDstRgb != Old.FuncDstRgb ||
  327. New.FuncSrcAlpha != Old.FuncSrcAlpha ||
  328. New.FuncDstAlpha != Old.FuncDstAlpha)
  329. {
  330. GL.BlendFuncSeparate(
  331. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  332. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  333. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  334. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  335. }
  336. }
  337. else
  338. {
  339. if (New.EquationRgb != Old.EquationRgb)
  340. {
  341. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  342. }
  343. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  344. New.FuncDstRgb != Old.FuncDstRgb)
  345. {
  346. GL.BlendFunc(
  347. OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  348. OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  349. }
  350. }
  351. }
  352. }
  353. private void SetBlendState(int Index, BlendState New, BlendState Old)
  354. {
  355. if (New.Enabled != Old.Enabled)
  356. {
  357. Enable(IndexedEnableCap.Blend, Index, New.Enabled);
  358. }
  359. if (New.Enabled)
  360. {
  361. if (New.SeparateAlpha)
  362. {
  363. if (New.EquationRgb != Old.EquationRgb ||
  364. New.EquationAlpha != Old.EquationAlpha)
  365. {
  366. GL.BlendEquationSeparate(
  367. Index,
  368. OGLEnumConverter.GetBlendEquation(New.EquationRgb),
  369. OGLEnumConverter.GetBlendEquation(New.EquationAlpha));
  370. }
  371. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  372. New.FuncDstRgb != Old.FuncDstRgb ||
  373. New.FuncSrcAlpha != Old.FuncSrcAlpha ||
  374. New.FuncDstAlpha != Old.FuncDstAlpha)
  375. {
  376. GL.BlendFuncSeparate(
  377. Index,
  378. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  379. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb),
  380. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcAlpha),
  381. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstAlpha));
  382. }
  383. }
  384. else
  385. {
  386. if (New.EquationRgb != Old.EquationRgb)
  387. {
  388. GL.BlendEquation(Index, OGLEnumConverter.GetBlendEquation(New.EquationRgb));
  389. }
  390. if (New.FuncSrcRgb != Old.FuncSrcRgb ||
  391. New.FuncDstRgb != Old.FuncDstRgb)
  392. {
  393. GL.BlendFunc(
  394. Index,
  395. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.FuncSrcRgb),
  396. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.FuncDstRgb));
  397. }
  398. }
  399. }
  400. }
  401. private void BindConstBuffers(GalPipelineState New)
  402. {
  403. int FreeBinding = OGLShader.ReservedCbufCount;
  404. void BindIfNotNull(OGLShaderStage Stage)
  405. {
  406. if (Stage != null)
  407. {
  408. foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
  409. {
  410. long Key = New.ConstBufferKeys[(int)Stage.Type][DeclInfo.Cbuf];
  411. if (Key != 0 && Buffer.TryGetUbo(Key, out int UboHandle))
  412. {
  413. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, UboHandle);
  414. }
  415. FreeBinding++;
  416. }
  417. }
  418. }
  419. BindIfNotNull(Shader.Current.Vertex);
  420. BindIfNotNull(Shader.Current.TessControl);
  421. BindIfNotNull(Shader.Current.TessEvaluation);
  422. BindIfNotNull(Shader.Current.Geometry);
  423. BindIfNotNull(Shader.Current.Fragment);
  424. }
  425. private void BindVertexLayout(GalPipelineState New)
  426. {
  427. foreach (GalVertexBinding Binding in New.VertexBindings)
  428. {
  429. if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
  430. {
  431. continue;
  432. }
  433. if (VaoHandle == 0)
  434. {
  435. VaoHandle = GL.GenVertexArray();
  436. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  437. //if you want to use it, move this line out of the if
  438. GL.BindVertexArray(VaoHandle);
  439. }
  440. foreach (GalVertexAttrib Attrib in Binding.Attribs)
  441. {
  442. //Skip uninitialized attributes.
  443. if (Attrib.Size == 0)
  444. {
  445. continue;
  446. }
  447. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  448. bool Unsigned =
  449. Attrib.Type == GalVertexAttribType.Unorm ||
  450. Attrib.Type == GalVertexAttribType.Uint ||
  451. Attrib.Type == GalVertexAttribType.Uscaled;
  452. bool Normalize =
  453. Attrib.Type == GalVertexAttribType.Snorm ||
  454. Attrib.Type == GalVertexAttribType.Unorm;
  455. VertexAttribPointerType Type = 0;
  456. if (Attrib.Type == GalVertexAttribType.Float)
  457. {
  458. Type = GetType(FloatAttribTypes, Attrib);
  459. }
  460. else
  461. {
  462. if (Unsigned)
  463. {
  464. Type = GetType(UnsignedAttribTypes, Attrib);
  465. }
  466. else
  467. {
  468. Type = GetType(SignedAttribTypes, Attrib);
  469. }
  470. }
  471. if (!AttribElements.TryGetValue(Attrib.Size, out int Size))
  472. {
  473. throw new InvalidOperationException("Invalid attribute size \"" + Attrib.Size + "\"!");
  474. }
  475. int Offset = Attrib.Offset;
  476. if (Binding.Stride != 0)
  477. {
  478. GL.EnableVertexAttribArray(Attrib.Index);
  479. if (Attrib.Type == GalVertexAttribType.Sint ||
  480. Attrib.Type == GalVertexAttribType.Uint)
  481. {
  482. IntPtr Pointer = new IntPtr(Offset);
  483. VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;
  484. GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
  485. }
  486. else
  487. {
  488. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
  489. }
  490. }
  491. else
  492. {
  493. GL.DisableVertexAttribArray(Attrib.Index);
  494. SetConstAttrib(Attrib);
  495. }
  496. if (Binding.Instanced && Binding.Divisor != 0)
  497. {
  498. GL.VertexAttribDivisor(Attrib.Index, 1);
  499. }
  500. else
  501. {
  502. GL.VertexAttribDivisor(Attrib.Index, 0);
  503. }
  504. }
  505. }
  506. }
  507. private static VertexAttribPointerType GetType(Dictionary<GalVertexAttribSize, VertexAttribPointerType> Dict, GalVertexAttrib Attrib)
  508. {
  509. if (!Dict.TryGetValue(Attrib.Size, out VertexAttribPointerType Type))
  510. {
  511. ThrowUnsupportedAttrib(Attrib);
  512. }
  513. return Type;
  514. }
  515. private unsafe static void SetConstAttrib(GalVertexAttrib Attrib)
  516. {
  517. if (Attrib.Size == GalVertexAttribSize._10_10_10_2 ||
  518. Attrib.Size == GalVertexAttribSize._11_11_10)
  519. {
  520. ThrowUnsupportedAttrib(Attrib);
  521. }
  522. if (Attrib.Type == GalVertexAttribType.Unorm)
  523. {
  524. switch (Attrib.Size)
  525. {
  526. case GalVertexAttribSize._8:
  527. case GalVertexAttribSize._8_8:
  528. case GalVertexAttribSize._8_8_8:
  529. case GalVertexAttribSize._8_8_8_8:
  530. GL.VertexAttrib4N((uint)Attrib.Index, (byte*)Attrib.Pointer);
  531. break;
  532. case GalVertexAttribSize._16:
  533. case GalVertexAttribSize._16_16:
  534. case GalVertexAttribSize._16_16_16:
  535. case GalVertexAttribSize._16_16_16_16:
  536. GL.VertexAttrib4N((uint)Attrib.Index, (ushort*)Attrib.Pointer);
  537. break;
  538. case GalVertexAttribSize._32:
  539. case GalVertexAttribSize._32_32:
  540. case GalVertexAttribSize._32_32_32:
  541. case GalVertexAttribSize._32_32_32_32:
  542. GL.VertexAttrib4N((uint)Attrib.Index, (uint*)Attrib.Pointer);
  543. break;
  544. }
  545. }
  546. else if (Attrib.Type == GalVertexAttribType.Snorm)
  547. {
  548. switch (Attrib.Size)
  549. {
  550. case GalVertexAttribSize._8:
  551. case GalVertexAttribSize._8_8:
  552. case GalVertexAttribSize._8_8_8:
  553. case GalVertexAttribSize._8_8_8_8:
  554. GL.VertexAttrib4N((uint)Attrib.Index, (sbyte*)Attrib.Pointer);
  555. break;
  556. case GalVertexAttribSize._16:
  557. case GalVertexAttribSize._16_16:
  558. case GalVertexAttribSize._16_16_16:
  559. case GalVertexAttribSize._16_16_16_16:
  560. GL.VertexAttrib4N((uint)Attrib.Index, (short*)Attrib.Pointer);
  561. break;
  562. case GalVertexAttribSize._32:
  563. case GalVertexAttribSize._32_32:
  564. case GalVertexAttribSize._32_32_32:
  565. case GalVertexAttribSize._32_32_32_32:
  566. GL.VertexAttrib4N((uint)Attrib.Index, (int*)Attrib.Pointer);
  567. break;
  568. }
  569. }
  570. else if (Attrib.Type == GalVertexAttribType.Uint)
  571. {
  572. switch (Attrib.Size)
  573. {
  574. case GalVertexAttribSize._8:
  575. case GalVertexAttribSize._8_8:
  576. case GalVertexAttribSize._8_8_8:
  577. case GalVertexAttribSize._8_8_8_8:
  578. GL.VertexAttribI4((uint)Attrib.Index, (byte*)Attrib.Pointer);
  579. break;
  580. case GalVertexAttribSize._16:
  581. case GalVertexAttribSize._16_16:
  582. case GalVertexAttribSize._16_16_16:
  583. case GalVertexAttribSize._16_16_16_16:
  584. GL.VertexAttribI4((uint)Attrib.Index, (ushort*)Attrib.Pointer);
  585. break;
  586. case GalVertexAttribSize._32:
  587. case GalVertexAttribSize._32_32:
  588. case GalVertexAttribSize._32_32_32:
  589. case GalVertexAttribSize._32_32_32_32:
  590. GL.VertexAttribI4((uint)Attrib.Index, (uint*)Attrib.Pointer);
  591. break;
  592. }
  593. }
  594. else if (Attrib.Type == GalVertexAttribType.Sint)
  595. {
  596. switch (Attrib.Size)
  597. {
  598. case GalVertexAttribSize._8:
  599. case GalVertexAttribSize._8_8:
  600. case GalVertexAttribSize._8_8_8:
  601. case GalVertexAttribSize._8_8_8_8:
  602. GL.VertexAttribI4((uint)Attrib.Index, (sbyte*)Attrib.Pointer);
  603. break;
  604. case GalVertexAttribSize._16:
  605. case GalVertexAttribSize._16_16:
  606. case GalVertexAttribSize._16_16_16:
  607. case GalVertexAttribSize._16_16_16_16:
  608. GL.VertexAttribI4((uint)Attrib.Index, (short*)Attrib.Pointer);
  609. break;
  610. case GalVertexAttribSize._32:
  611. case GalVertexAttribSize._32_32:
  612. case GalVertexAttribSize._32_32_32:
  613. case GalVertexAttribSize._32_32_32_32:
  614. GL.VertexAttribI4((uint)Attrib.Index, (int*)Attrib.Pointer);
  615. break;
  616. }
  617. }
  618. else if (Attrib.Type == GalVertexAttribType.Float)
  619. {
  620. switch (Attrib.Size)
  621. {
  622. case GalVertexAttribSize._32:
  623. case GalVertexAttribSize._32_32:
  624. case GalVertexAttribSize._32_32_32:
  625. case GalVertexAttribSize._32_32_32_32:
  626. GL.VertexAttrib4(Attrib.Index, (float*)Attrib.Pointer);
  627. break;
  628. default: ThrowUnsupportedAttrib(Attrib); break;
  629. }
  630. }
  631. }
  632. private static void ThrowUnsupportedAttrib(GalVertexAttrib Attrib)
  633. {
  634. throw new NotImplementedException("Unsupported size \"" + Attrib.Size + "\" on type \"" + Attrib.Type + "\"!");
  635. }
  636. private void Enable(EnableCap Cap, bool Enabled)
  637. {
  638. if (Enabled)
  639. {
  640. GL.Enable(Cap);
  641. }
  642. else
  643. {
  644. GL.Disable(Cap);
  645. }
  646. }
  647. private void Enable(IndexedEnableCap Cap, int Index, bool Enabled)
  648. {
  649. if (Enabled)
  650. {
  651. GL.Enable(Cap, Index);
  652. }
  653. else
  654. {
  655. GL.Disable(Cap, Index);
  656. }
  657. }
  658. public void ResetDepthMask()
  659. {
  660. Old.DepthWriteEnabled = true;
  661. }
  662. public void ResetColorMask(int Index)
  663. {
  664. Old.ColorMasks[Index] = ColorMaskState.Default;
  665. }
  666. }
  667. }