Pipeline.cs 32 KB

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