Pipeline.cs 29 KB

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