OGLPipeline.cs 31 KB

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