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. 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. // 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 itterations
  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 unsafe static 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. }