Pipeline.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Shader;
  5. using System;
  6. namespace Ryujinx.Graphics.OpenGL
  7. {
  8. class Pipeline : IPipeline, IDisposable
  9. {
  10. private Program _program;
  11. private bool _rasterizerDiscard;
  12. private VertexArray _vertexArray;
  13. private Framebuffer _framebuffer;
  14. private IntPtr _indexBaseOffset;
  15. private DrawElementsType _elementsType;
  16. private PrimitiveType _primitiveType;
  17. private int _stencilFrontMask;
  18. private bool _depthMask;
  19. private bool _depthTest;
  20. private bool _hasDepthBuffer;
  21. private TextureBase _unit0Texture;
  22. private ClipOrigin _clipOrigin;
  23. private ClipDepthMode _clipDepthMode;
  24. private uint[] _componentMasks;
  25. private bool _scissor0Enable = false;
  26. ColorF _blendConstant = new ColorF(0, 0, 0, 0);
  27. internal Pipeline()
  28. {
  29. _rasterizerDiscard = false;
  30. _clipOrigin = ClipOrigin.LowerLeft;
  31. _clipDepthMode = ClipDepthMode.NegativeOneToOne;
  32. }
  33. public void Barrier()
  34. {
  35. GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
  36. }
  37. public void ClearRenderTargetColor(int index, uint componentMask, ColorF color)
  38. {
  39. GL.ColorMask(
  40. index,
  41. (componentMask & 1) != 0,
  42. (componentMask & 2) != 0,
  43. (componentMask & 4) != 0,
  44. (componentMask & 8) != 0);
  45. float[] colors = new float[] { color.Red, color.Green, color.Blue, color.Alpha };
  46. GL.ClearBuffer(ClearBuffer.Color, index, colors);
  47. RestoreComponentMask(index);
  48. _framebuffer.SignalModified();
  49. }
  50. public void ClearRenderTargetDepthStencil(float depthValue, bool depthMask, int stencilValue, int stencilMask)
  51. {
  52. bool stencilMaskChanged =
  53. stencilMask != 0 &&
  54. stencilMask != _stencilFrontMask;
  55. bool depthMaskChanged = depthMask && depthMask != _depthMask;
  56. if (stencilMaskChanged)
  57. {
  58. GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
  59. }
  60. if (depthMaskChanged)
  61. {
  62. GL.DepthMask(depthMask);
  63. }
  64. if (depthMask && stencilMask != 0)
  65. {
  66. GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
  67. }
  68. else if (depthMask)
  69. {
  70. GL.ClearBuffer(ClearBuffer.Depth, 0, ref depthValue);
  71. }
  72. else if (stencilMask != 0)
  73. {
  74. GL.ClearBuffer(ClearBuffer.Stencil, 0, ref stencilValue);
  75. }
  76. if (stencilMaskChanged)
  77. {
  78. GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
  79. }
  80. if (depthMaskChanged)
  81. {
  82. GL.DepthMask(_depthMask);
  83. }
  84. _framebuffer.SignalModified();
  85. }
  86. public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
  87. {
  88. if (!_program.IsLinked)
  89. {
  90. Logger.PrintDebug(LogClass.Gpu, "Dispatch error, shader not linked.");
  91. return;
  92. }
  93. PrepareForDispatch();
  94. GL.DispatchCompute(groupsX, groupsY, groupsZ);
  95. }
  96. public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
  97. {
  98. if (!_program.IsLinked)
  99. {
  100. Logger.PrintDebug(LogClass.Gpu, "Draw error, shader not linked.");
  101. return;
  102. }
  103. PrepareForDraw();
  104. if (_primitiveType == PrimitiveType.Quads)
  105. {
  106. DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  107. }
  108. else if (_primitiveType == PrimitiveType.QuadStrip)
  109. {
  110. DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  111. }
  112. else
  113. {
  114. DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  115. }
  116. _framebuffer.SignalModified();
  117. }
  118. private void DrawQuadsImpl(
  119. int vertexCount,
  120. int instanceCount,
  121. int firstVertex,
  122. int firstInstance)
  123. {
  124. // TODO: Instanced rendering.
  125. int quadsCount = vertexCount / 4;
  126. int[] firsts = new int[quadsCount];
  127. int[] counts = new int[quadsCount];
  128. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  129. {
  130. firsts[quadIndex] = firstVertex + quadIndex * 4;
  131. counts[quadIndex] = 4;
  132. }
  133. GL.MultiDrawArrays(
  134. PrimitiveType.TriangleFan,
  135. firsts,
  136. counts,
  137. quadsCount);
  138. }
  139. private void DrawQuadStripImpl(
  140. int vertexCount,
  141. int instanceCount,
  142. int firstVertex,
  143. int firstInstance)
  144. {
  145. int quadsCount = (vertexCount - 2) / 2;
  146. if (firstInstance != 0 || instanceCount != 1)
  147. {
  148. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  149. {
  150. GL.DrawArraysInstancedBaseInstance(PrimitiveType.TriangleFan, firstVertex + quadIndex * 2, 4, instanceCount, firstInstance);
  151. }
  152. }
  153. else
  154. {
  155. int[] firsts = new int[quadsCount];
  156. int[] counts = new int[quadsCount];
  157. firsts[0] = firstVertex;
  158. counts[0] = 4;
  159. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  160. {
  161. firsts[quadIndex] = firstVertex + quadIndex * 2;
  162. counts[quadIndex] = 4;
  163. }
  164. GL.MultiDrawArrays(
  165. PrimitiveType.TriangleFan,
  166. firsts,
  167. counts,
  168. quadsCount);
  169. }
  170. }
  171. private void DrawImpl(
  172. int vertexCount,
  173. int instanceCount,
  174. int firstVertex,
  175. int firstInstance)
  176. {
  177. if (firstInstance == 0 && instanceCount == 1)
  178. {
  179. GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
  180. }
  181. else if (firstInstance == 0)
  182. {
  183. GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
  184. }
  185. else
  186. {
  187. GL.DrawArraysInstancedBaseInstance(
  188. _primitiveType,
  189. firstVertex,
  190. vertexCount,
  191. instanceCount,
  192. firstInstance);
  193. }
  194. }
  195. public void DrawIndexed(
  196. int indexCount,
  197. int instanceCount,
  198. int firstIndex,
  199. int firstVertex,
  200. int firstInstance)
  201. {
  202. if (!_program.IsLinked)
  203. {
  204. Logger.PrintDebug(LogClass.Gpu, "Draw error, shader not linked.");
  205. return;
  206. }
  207. PrepareForDraw();
  208. int indexElemSize = 1;
  209. switch (_elementsType)
  210. {
  211. case DrawElementsType.UnsignedShort: indexElemSize = 2; break;
  212. case DrawElementsType.UnsignedInt: indexElemSize = 4; break;
  213. }
  214. IntPtr indexBaseOffset = _indexBaseOffset + firstIndex * indexElemSize;
  215. if (_primitiveType == PrimitiveType.Quads)
  216. {
  217. DrawQuadsIndexedImpl(
  218. indexCount,
  219. instanceCount,
  220. indexBaseOffset,
  221. indexElemSize,
  222. firstVertex,
  223. firstInstance);
  224. }
  225. else if (_primitiveType == PrimitiveType.QuadStrip)
  226. {
  227. DrawQuadStripIndexedImpl(
  228. indexCount,
  229. instanceCount,
  230. indexBaseOffset,
  231. indexElemSize,
  232. firstVertex,
  233. firstInstance);
  234. }
  235. else
  236. {
  237. DrawIndexedImpl(
  238. indexCount,
  239. instanceCount,
  240. indexBaseOffset,
  241. firstVertex,
  242. firstInstance);
  243. }
  244. _framebuffer.SignalModified();
  245. }
  246. private void DrawQuadsIndexedImpl(
  247. int indexCount,
  248. int instanceCount,
  249. IntPtr indexBaseOffset,
  250. int indexElemSize,
  251. int firstVertex,
  252. int firstInstance)
  253. {
  254. int quadsCount = indexCount / 4;
  255. if (firstInstance != 0 || instanceCount != 1)
  256. {
  257. if (firstVertex != 0 && firstInstance != 0)
  258. {
  259. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  260. {
  261. GL.DrawElementsInstancedBaseVertexBaseInstance(
  262. PrimitiveType.TriangleFan,
  263. 4,
  264. _elementsType,
  265. indexBaseOffset + quadIndex * 4 * indexElemSize,
  266. instanceCount,
  267. firstVertex,
  268. firstInstance);
  269. }
  270. }
  271. else if (firstInstance != 0)
  272. {
  273. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  274. {
  275. GL.DrawElementsInstancedBaseInstance(
  276. PrimitiveType.TriangleFan,
  277. 4,
  278. _elementsType,
  279. indexBaseOffset + quadIndex * 4 * indexElemSize,
  280. instanceCount,
  281. firstInstance);
  282. }
  283. }
  284. else
  285. {
  286. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  287. {
  288. GL.DrawElementsInstanced(
  289. PrimitiveType.TriangleFan,
  290. 4,
  291. _elementsType,
  292. indexBaseOffset + quadIndex * 4 * indexElemSize,
  293. instanceCount);
  294. }
  295. }
  296. }
  297. else
  298. {
  299. IntPtr[] indices = new IntPtr[quadsCount];
  300. int[] counts = new int[quadsCount];
  301. int[] baseVertices = new int[quadsCount];
  302. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  303. {
  304. indices[quadIndex] = indexBaseOffset + quadIndex * 4 * indexElemSize;
  305. counts[quadIndex] = 4;
  306. baseVertices[quadIndex] = firstVertex;
  307. }
  308. GL.MultiDrawElementsBaseVertex(
  309. PrimitiveType.TriangleFan,
  310. counts,
  311. _elementsType,
  312. indices,
  313. quadsCount,
  314. baseVertices);
  315. }
  316. }
  317. private void DrawQuadStripIndexedImpl(
  318. int indexCount,
  319. int instanceCount,
  320. IntPtr indexBaseOffset,
  321. int indexElemSize,
  322. int firstVertex,
  323. int firstInstance)
  324. {
  325. // TODO: Instanced rendering.
  326. int quadsCount = (indexCount - 2) / 2;
  327. IntPtr[] indices = new IntPtr[quadsCount];
  328. int[] counts = new int[quadsCount];
  329. int[] baseVertices = new int[quadsCount];
  330. indices[0] = indexBaseOffset;
  331. counts[0] = 4;
  332. baseVertices[0] = firstVertex;
  333. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  334. {
  335. indices[quadIndex] = indexBaseOffset + quadIndex * 2 * indexElemSize;
  336. counts[quadIndex] = 4;
  337. baseVertices[quadIndex] = firstVertex;
  338. }
  339. GL.MultiDrawElementsBaseVertex(
  340. PrimitiveType.TriangleFan,
  341. counts,
  342. _elementsType,
  343. indices,
  344. quadsCount,
  345. baseVertices);
  346. }
  347. private void DrawIndexedImpl(
  348. int indexCount,
  349. int instanceCount,
  350. IntPtr indexBaseOffset,
  351. int firstVertex,
  352. int firstInstance)
  353. {
  354. if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
  355. {
  356. GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
  357. }
  358. else if (firstInstance == 0 && instanceCount == 1)
  359. {
  360. GL.DrawElementsBaseVertex(
  361. _primitiveType,
  362. indexCount,
  363. _elementsType,
  364. indexBaseOffset,
  365. firstVertex);
  366. }
  367. else if (firstInstance == 0 && firstVertex == 0)
  368. {
  369. GL.DrawElementsInstanced(
  370. _primitiveType,
  371. indexCount,
  372. _elementsType,
  373. indexBaseOffset,
  374. instanceCount);
  375. }
  376. else if (firstInstance == 0)
  377. {
  378. GL.DrawElementsInstancedBaseVertex(
  379. _primitiveType,
  380. indexCount,
  381. _elementsType,
  382. indexBaseOffset,
  383. instanceCount,
  384. firstVertex);
  385. }
  386. else if (firstVertex == 0)
  387. {
  388. GL.DrawElementsInstancedBaseInstance(
  389. _primitiveType,
  390. indexCount,
  391. _elementsType,
  392. indexBaseOffset,
  393. instanceCount,
  394. firstInstance);
  395. }
  396. else
  397. {
  398. GL.DrawElementsInstancedBaseVertexBaseInstance(
  399. _primitiveType,
  400. indexCount,
  401. _elementsType,
  402. indexBaseOffset,
  403. instanceCount,
  404. firstVertex,
  405. firstInstance);
  406. }
  407. }
  408. public void SetBlendState(int index, BlendDescriptor blend)
  409. {
  410. if (!blend.Enable)
  411. {
  412. GL.Disable(IndexedEnableCap.Blend, index);
  413. return;
  414. }
  415. GL.BlendEquationSeparate(
  416. index,
  417. blend.ColorOp.Convert(),
  418. blend.AlphaOp.Convert());
  419. GL.BlendFuncSeparate(
  420. index,
  421. (BlendingFactorSrc)blend.ColorSrcFactor.Convert(),
  422. (BlendingFactorDest)blend.ColorDstFactor.Convert(),
  423. (BlendingFactorSrc)blend.AlphaSrcFactor.Convert(),
  424. (BlendingFactorDest)blend.AlphaDstFactor.Convert());
  425. if (_blendConstant != blend.BlendConstant)
  426. {
  427. _blendConstant = blend.BlendConstant;
  428. GL.BlendColor(
  429. blend.BlendConstant.Red,
  430. blend.BlendConstant.Green,
  431. blend.BlendConstant.Blue,
  432. blend.BlendConstant.Alpha);
  433. }
  434. GL.Enable(IndexedEnableCap.Blend, index);
  435. }
  436. public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
  437. {
  438. if ((enables & PolygonModeMask.Point) != 0)
  439. {
  440. GL.Enable(EnableCap.PolygonOffsetPoint);
  441. }
  442. else
  443. {
  444. GL.Disable(EnableCap.PolygonOffsetPoint);
  445. }
  446. if ((enables & PolygonModeMask.Line) != 0)
  447. {
  448. GL.Enable(EnableCap.PolygonOffsetLine);
  449. }
  450. else
  451. {
  452. GL.Disable(EnableCap.PolygonOffsetLine);
  453. }
  454. if ((enables & PolygonModeMask.Fill) != 0)
  455. {
  456. GL.Enable(EnableCap.PolygonOffsetFill);
  457. }
  458. else
  459. {
  460. GL.Disable(EnableCap.PolygonOffsetFill);
  461. }
  462. if (enables == 0)
  463. {
  464. return;
  465. }
  466. GL.PolygonOffset(factor, units / 2f);
  467. // TODO: Enable when GL_EXT_polygon_offset_clamp is supported.
  468. // GL.PolygonOffsetClamp(factor, units, clamp);
  469. }
  470. public void SetDepthClamp(bool clamp)
  471. {
  472. if (!clamp)
  473. {
  474. GL.Disable(EnableCap.DepthClamp);
  475. return;
  476. }
  477. GL.Enable(EnableCap.DepthClamp);
  478. }
  479. public void SetDepthMode(DepthMode mode)
  480. {
  481. ClipDepthMode depthMode = mode.Convert();
  482. if (_clipDepthMode != depthMode)
  483. {
  484. _clipDepthMode = depthMode;
  485. GL.ClipControl(_clipOrigin, depthMode);
  486. }
  487. }
  488. public void SetDepthTest(DepthTestDescriptor depthTest)
  489. {
  490. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  491. _depthMask = depthTest.WriteEnable;
  492. _depthTest = depthTest.TestEnable;
  493. UpdateDepthTest();
  494. }
  495. public void SetFaceCulling(bool enable, Face face)
  496. {
  497. if (!enable)
  498. {
  499. GL.Disable(EnableCap.CullFace);
  500. return;
  501. }
  502. GL.CullFace(face.Convert());
  503. GL.Enable(EnableCap.CullFace);
  504. }
  505. public void SetFrontFace(FrontFace frontFace)
  506. {
  507. GL.FrontFace(frontFace.Convert());
  508. }
  509. public void SetImage(int index, ShaderStage stage, ITexture texture)
  510. {
  511. int unit = _program.GetImageUnit(stage, index);
  512. if (unit != -1 && texture != null)
  513. {
  514. TextureBase texBase = (TextureBase)texture;
  515. FormatInfo formatInfo = FormatTable.GetFormatInfo(texBase.Format);
  516. SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;
  517. GL.BindImageTexture(unit, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
  518. }
  519. }
  520. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  521. {
  522. _elementsType = type.Convert();
  523. _indexBaseOffset = (IntPtr)buffer.Offset;
  524. EnsureVertexArray();
  525. _vertexArray.SetIndexBuffer((Buffer)buffer.Buffer);
  526. }
  527. public void SetPointSize(float size)
  528. {
  529. GL.PointSize(size);
  530. }
  531. public void SetPrimitiveRestart(bool enable, int index)
  532. {
  533. if (!enable)
  534. {
  535. GL.Disable(EnableCap.PrimitiveRestart);
  536. return;
  537. }
  538. GL.PrimitiveRestartIndex(index);
  539. GL.Enable(EnableCap.PrimitiveRestart);
  540. }
  541. public void SetPrimitiveTopology(PrimitiveTopology topology)
  542. {
  543. _primitiveType = topology.Convert();
  544. }
  545. public void SetProgram(IProgram program)
  546. {
  547. _program = (Program)program;
  548. _program.Bind();
  549. }
  550. public void SetRasterizerDiscard(bool discard)
  551. {
  552. if (discard)
  553. {
  554. GL.Enable(EnableCap.RasterizerDiscard);
  555. }
  556. else
  557. {
  558. GL.Disable(EnableCap.RasterizerDiscard);
  559. }
  560. _rasterizerDiscard = discard;
  561. }
  562. public void SetRenderTargetColorMasks(uint[] componentMasks)
  563. {
  564. _componentMasks = (uint[])componentMasks.Clone();
  565. for (int index = 0; index < componentMasks.Length; index++)
  566. {
  567. RestoreComponentMask(index);
  568. }
  569. }
  570. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  571. {
  572. EnsureFramebuffer();
  573. for (int index = 0; index < colors.Length; index++)
  574. {
  575. TextureView color = (TextureView)colors[index];
  576. _framebuffer.AttachColor(index, color);
  577. }
  578. TextureView depthStencilView = (TextureView)depthStencil;
  579. _framebuffer.AttachDepthStencil(depthStencilView);
  580. _framebuffer.SetDrawBuffers(colors.Length);
  581. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  582. UpdateDepthTest();
  583. }
  584. public void SetSampler(int index, ShaderStage stage, ISampler sampler)
  585. {
  586. int unit = _program.GetTextureUnit(stage, index);
  587. if (unit != -1 && sampler != null)
  588. {
  589. ((Sampler)sampler).Bind(unit);
  590. }
  591. }
  592. public void SetScissorEnable(int index, bool enable)
  593. {
  594. if (enable)
  595. {
  596. GL.Enable(IndexedEnableCap.ScissorTest, index);
  597. }
  598. else
  599. {
  600. GL.Disable(IndexedEnableCap.ScissorTest, index);
  601. }
  602. if (index == 0)
  603. {
  604. _scissor0Enable = enable;
  605. }
  606. }
  607. public void SetScissor(int index, int x, int y, int width, int height)
  608. {
  609. GL.ScissorIndexed(index, x, y, width, height);
  610. }
  611. public void SetStencilTest(StencilTestDescriptor stencilTest)
  612. {
  613. if (!stencilTest.TestEnable)
  614. {
  615. GL.Disable(EnableCap.StencilTest);
  616. return;
  617. }
  618. GL.StencilOpSeparate(
  619. StencilFace.Front,
  620. stencilTest.FrontSFail.Convert(),
  621. stencilTest.FrontDpFail.Convert(),
  622. stencilTest.FrontDpPass.Convert());
  623. GL.StencilFuncSeparate(
  624. StencilFace.Front,
  625. (StencilFunction)stencilTest.FrontFunc.Convert(),
  626. stencilTest.FrontFuncRef,
  627. stencilTest.FrontFuncMask);
  628. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  629. GL.StencilOpSeparate(
  630. StencilFace.Back,
  631. stencilTest.BackSFail.Convert(),
  632. stencilTest.BackDpFail.Convert(),
  633. stencilTest.BackDpPass.Convert());
  634. GL.StencilFuncSeparate(
  635. StencilFace.Back,
  636. (StencilFunction)stencilTest.BackFunc.Convert(),
  637. stencilTest.BackFuncRef,
  638. stencilTest.BackFuncMask);
  639. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  640. GL.Enable(EnableCap.StencilTest);
  641. _stencilFrontMask = stencilTest.FrontMask;
  642. }
  643. public void SetStorageBuffer(int index, ShaderStage stage, BufferRange buffer)
  644. {
  645. SetBuffer(index, stage, buffer, isStorage: true);
  646. }
  647. public void SetTexture(int index, ShaderStage stage, ITexture texture)
  648. {
  649. int unit = _program.GetTextureUnit(stage, index);
  650. if (unit != -1 && texture != null)
  651. {
  652. if (unit == 0)
  653. {
  654. _unit0Texture = (TextureBase)texture;
  655. }
  656. else
  657. {
  658. ((TextureBase)texture).Bind(unit);
  659. }
  660. }
  661. }
  662. public void SetUniformBuffer(int index, ShaderStage stage, BufferRange buffer)
  663. {
  664. SetBuffer(index, stage, buffer, isStorage: false);
  665. }
  666. public void SetVertexAttribs(VertexAttribDescriptor[] vertexAttribs)
  667. {
  668. EnsureVertexArray();
  669. _vertexArray.SetVertexAttributes(vertexAttribs);
  670. }
  671. public void SetVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
  672. {
  673. EnsureVertexArray();
  674. _vertexArray.SetVertexBuffers(vertexBuffers);
  675. }
  676. public void SetViewports(int first, Viewport[] viewports)
  677. {
  678. bool flipY = false;
  679. float[] viewportArray = new float[viewports.Length * 4];
  680. double[] depthRangeArray = new double[viewports.Length * 2];
  681. for (int index = 0; index < viewports.Length; index++)
  682. {
  683. int viewportElemIndex = index * 4;
  684. Viewport viewport = viewports[index];
  685. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  686. viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
  687. // OpenGL does not support per-viewport flipping, so
  688. // instead we decide that based on the viewport 0 value.
  689. // It will apply to all viewports.
  690. if (index == 0)
  691. {
  692. flipY = viewport.Region.Height < 0;
  693. }
  694. if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
  695. {
  696. flipY = !flipY;
  697. }
  698. viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
  699. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  700. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  701. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  702. }
  703. GL.ViewportArray(first, viewports.Length, viewportArray);
  704. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  705. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  706. }
  707. public void TextureBarrier()
  708. {
  709. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  710. }
  711. public void TextureBarrierTiled()
  712. {
  713. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  714. }
  715. private void SetBuffer(int index, ShaderStage stage, BufferRange buffer, bool isStorage)
  716. {
  717. int bindingPoint = isStorage
  718. ? _program.GetStorageBufferBindingPoint(stage, index)
  719. : _program.GetUniformBufferBindingPoint(stage, index);
  720. if (bindingPoint == -1)
  721. {
  722. return;
  723. }
  724. BufferRangeTarget target = isStorage
  725. ? BufferRangeTarget.ShaderStorageBuffer
  726. : BufferRangeTarget.UniformBuffer;
  727. if (buffer.Buffer == null)
  728. {
  729. GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
  730. return;
  731. }
  732. int bufferHandle = ((Buffer)buffer.Buffer).Handle;
  733. IntPtr bufferOffset = (IntPtr)buffer.Offset;
  734. GL.BindBufferRange(target, bindingPoint, bufferHandle, bufferOffset, buffer.Size);
  735. }
  736. private void SetOrigin(ClipOrigin origin)
  737. {
  738. if (_clipOrigin != origin)
  739. {
  740. _clipOrigin = origin;
  741. GL.ClipControl(origin, _clipDepthMode);
  742. }
  743. }
  744. private void EnsureVertexArray()
  745. {
  746. if (_vertexArray == null)
  747. {
  748. _vertexArray = new VertexArray();
  749. _vertexArray.Bind();
  750. }
  751. }
  752. private void EnsureFramebuffer()
  753. {
  754. if (_framebuffer == null)
  755. {
  756. _framebuffer = new Framebuffer();
  757. _framebuffer.Bind();
  758. GL.Enable(EnableCap.FramebufferSrgb);
  759. }
  760. }
  761. private void UpdateDepthTest()
  762. {
  763. // Enabling depth operations is only valid when we have
  764. // a depth buffer, otherwise it's not allowed.
  765. if (_hasDepthBuffer)
  766. {
  767. if (_depthTest)
  768. {
  769. GL.Enable(EnableCap.DepthTest);
  770. }
  771. else
  772. {
  773. GL.Disable(EnableCap.DepthTest);
  774. }
  775. GL.DepthMask(_depthMask);
  776. }
  777. else
  778. {
  779. GL.Disable(EnableCap.DepthTest);
  780. GL.DepthMask(false);
  781. }
  782. }
  783. private void PrepareForDispatch()
  784. {
  785. if (_unit0Texture != null)
  786. {
  787. _unit0Texture.Bind(0);
  788. }
  789. }
  790. private void PrepareForDraw()
  791. {
  792. _vertexArray.Validate();
  793. if (_unit0Texture != null)
  794. {
  795. _unit0Texture.Bind(0);
  796. }
  797. }
  798. private void RestoreComponentMask(int index)
  799. {
  800. if (_componentMasks != null)
  801. {
  802. GL.ColorMask(
  803. index,
  804. (_componentMasks[index] & 1u) != 0,
  805. (_componentMasks[index] & 2u) != 0,
  806. (_componentMasks[index] & 4u) != 0,
  807. (_componentMasks[index] & 8u) != 0);
  808. }
  809. }
  810. public void RestoreScissor0Enable()
  811. {
  812. if (_scissor0Enable)
  813. {
  814. GL.Enable(IndexedEnableCap.ScissorTest, 0);
  815. }
  816. }
  817. public void RestoreRasterizerDiscard()
  818. {
  819. if (_rasterizerDiscard)
  820. {
  821. GL.Enable(EnableCap.RasterizerDiscard);
  822. }
  823. }
  824. public void Dispose()
  825. {
  826. _framebuffer?.Dispose();
  827. _vertexArray?.Dispose();
  828. }
  829. }
  830. }