Pipeline.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 SetDepthClamp(bool clampNear, bool clampFar)
  465. {
  466. // TODO: Use GL_AMD_depth_clamp_separate or similar if available?
  467. // Currently enables clamping if either is set.
  468. bool clamp = clampNear || clampFar;
  469. if (!clamp)
  470. {
  471. GL.Disable(EnableCap.DepthClamp);
  472. return;
  473. }
  474. GL.Enable(EnableCap.DepthClamp);
  475. }
  476. public void SetDepthMode(DepthMode mode)
  477. {
  478. ClipDepthMode depthMode = mode.Convert();
  479. if (_clipDepthMode != depthMode)
  480. {
  481. _clipDepthMode = depthMode;
  482. GL.ClipControl(_clipOrigin, depthMode);
  483. }
  484. }
  485. public void SetDepthTest(DepthTestDescriptor depthTest)
  486. {
  487. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  488. _depthMask = depthTest.WriteEnable;
  489. _depthTest = depthTest.TestEnable;
  490. UpdateDepthTest();
  491. }
  492. public void SetFaceCulling(bool enable, Face face)
  493. {
  494. if (!enable)
  495. {
  496. GL.Disable(EnableCap.CullFace);
  497. return;
  498. }
  499. GL.CullFace(face.Convert());
  500. GL.Enable(EnableCap.CullFace);
  501. }
  502. public void SetFrontFace(FrontFace frontFace)
  503. {
  504. GL.FrontFace(frontFace.Convert());
  505. }
  506. public void SetImage(int index, ShaderStage stage, ITexture texture)
  507. {
  508. int unit = _program.GetImageUnit(stage, index);
  509. if (unit != -1 && texture != null)
  510. {
  511. TextureView view = (TextureView)texture;
  512. FormatInfo formatInfo = FormatTable.GetFormatInfo(view.Format);
  513. SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;
  514. GL.BindImageTexture(unit, view.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
  515. }
  516. }
  517. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  518. {
  519. _elementsType = type.Convert();
  520. _indexBaseOffset = (IntPtr)buffer.Offset;
  521. EnsureVertexArray();
  522. _vertexArray.SetIndexBuffer((Buffer)buffer.Buffer);
  523. }
  524. public void SetPointSize(float size)
  525. {
  526. GL.PointSize(size);
  527. }
  528. public void SetPrimitiveRestart(bool enable, int index)
  529. {
  530. if (!enable)
  531. {
  532. GL.Disable(EnableCap.PrimitiveRestart);
  533. return;
  534. }
  535. GL.PrimitiveRestartIndex(index);
  536. GL.Enable(EnableCap.PrimitiveRestart);
  537. }
  538. public void SetPrimitiveTopology(PrimitiveTopology topology)
  539. {
  540. _primitiveType = topology.Convert();
  541. }
  542. public void SetProgram(IProgram program)
  543. {
  544. _program = (Program)program;
  545. _program.Bind();
  546. }
  547. public void SetRasterizerDiscard(bool discard)
  548. {
  549. if (discard)
  550. {
  551. GL.Enable(EnableCap.RasterizerDiscard);
  552. }
  553. else
  554. {
  555. GL.Disable(EnableCap.RasterizerDiscard);
  556. }
  557. _rasterizerDiscard = discard;
  558. }
  559. public void SetRenderTargetColorMasks(uint[] componentMasks)
  560. {
  561. _componentMasks = (uint[])componentMasks.Clone();
  562. for (int index = 0; index < componentMasks.Length; index++)
  563. {
  564. RestoreComponentMask(index);
  565. }
  566. }
  567. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  568. {
  569. EnsureFramebuffer();
  570. for (int index = 0; index < colors.Length; index++)
  571. {
  572. TextureView color = (TextureView)colors[index];
  573. _framebuffer.AttachColor(index, color);
  574. }
  575. TextureView depthStencilView = (TextureView)depthStencil;
  576. _framebuffer.AttachDepthStencil(depthStencilView);
  577. _framebuffer.SetDrawBuffers(colors.Length);
  578. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  579. UpdateDepthTest();
  580. }
  581. public void SetSampler(int index, ShaderStage stage, ISampler sampler)
  582. {
  583. int unit = _program.GetTextureUnit(stage, index);
  584. if (unit != -1 && sampler != null)
  585. {
  586. ((Sampler)sampler).Bind(unit);
  587. }
  588. }
  589. public void SetScissorEnable(int index, bool enable)
  590. {
  591. if (enable)
  592. {
  593. GL.Enable(IndexedEnableCap.ScissorTest, index);
  594. }
  595. else
  596. {
  597. GL.Disable(IndexedEnableCap.ScissorTest, index);
  598. }
  599. if (index == 0)
  600. {
  601. _scissor0Enable = enable;
  602. }
  603. }
  604. public void SetScissor(int index, int x, int y, int width, int height)
  605. {
  606. GL.ScissorIndexed(index, x, y, width, height);
  607. }
  608. public void SetStencilTest(StencilTestDescriptor stencilTest)
  609. {
  610. if (!stencilTest.TestEnable)
  611. {
  612. GL.Disable(EnableCap.StencilTest);
  613. return;
  614. }
  615. GL.StencilOpSeparate(
  616. StencilFace.Front,
  617. stencilTest.FrontSFail.Convert(),
  618. stencilTest.FrontDpFail.Convert(),
  619. stencilTest.FrontDpPass.Convert());
  620. GL.StencilFuncSeparate(
  621. StencilFace.Front,
  622. (StencilFunction)stencilTest.FrontFunc.Convert(),
  623. stencilTest.FrontFuncRef,
  624. stencilTest.FrontFuncMask);
  625. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  626. GL.StencilOpSeparate(
  627. StencilFace.Back,
  628. stencilTest.BackSFail.Convert(),
  629. stencilTest.BackDpFail.Convert(),
  630. stencilTest.BackDpPass.Convert());
  631. GL.StencilFuncSeparate(
  632. StencilFace.Back,
  633. (StencilFunction)stencilTest.BackFunc.Convert(),
  634. stencilTest.BackFuncRef,
  635. stencilTest.BackFuncMask);
  636. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  637. GL.Enable(EnableCap.StencilTest);
  638. _stencilFrontMask = stencilTest.FrontMask;
  639. }
  640. public void SetStorageBuffer(int index, ShaderStage stage, BufferRange buffer)
  641. {
  642. SetBuffer(index, stage, buffer, isStorage: true);
  643. }
  644. public void SetTexture(int index, ShaderStage stage, ITexture texture)
  645. {
  646. int unit = _program.GetTextureUnit(stage, index);
  647. if (unit != -1 && texture != null)
  648. {
  649. if (unit == 0)
  650. {
  651. _unit0Texture = ((TextureView)texture);
  652. }
  653. else
  654. {
  655. ((TextureView)texture).Bind(unit);
  656. }
  657. }
  658. }
  659. public void SetUniformBuffer(int index, ShaderStage stage, BufferRange buffer)
  660. {
  661. SetBuffer(index, stage, buffer, isStorage: false);
  662. }
  663. public void SetVertexAttribs(VertexAttribDescriptor[] vertexAttribs)
  664. {
  665. EnsureVertexArray();
  666. _vertexArray.SetVertexAttributes(vertexAttribs);
  667. }
  668. public void SetVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
  669. {
  670. EnsureVertexArray();
  671. _vertexArray.SetVertexBuffers(vertexBuffers);
  672. }
  673. public void SetViewports(int first, Viewport[] viewports)
  674. {
  675. bool flipY = false;
  676. float[] viewportArray = new float[viewports.Length * 4];
  677. double[] depthRangeArray = new double[viewports.Length * 2];
  678. for (int index = 0; index < viewports.Length; index++)
  679. {
  680. int viewportElemIndex = index * 4;
  681. Viewport viewport = viewports[index];
  682. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  683. viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
  684. // OpenGL does not support per-viewport flipping, so
  685. // instead we decide that based on the viewport 0 value.
  686. // It will apply to all viewports.
  687. if (index == 0)
  688. {
  689. flipY = viewport.Region.Height < 0;
  690. }
  691. if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
  692. {
  693. flipY = !flipY;
  694. }
  695. viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
  696. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  697. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  698. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  699. }
  700. GL.ViewportArray(first, viewports.Length, viewportArray);
  701. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  702. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  703. }
  704. public void TextureBarrier()
  705. {
  706. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  707. }
  708. public void TextureBarrierTiled()
  709. {
  710. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  711. }
  712. private void SetBuffer(int index, ShaderStage stage, BufferRange buffer, bool isStorage)
  713. {
  714. int bindingPoint = isStorage
  715. ? _program.GetStorageBufferBindingPoint(stage, index)
  716. : _program.GetUniformBufferBindingPoint(stage, index);
  717. if (bindingPoint == -1)
  718. {
  719. return;
  720. }
  721. BufferRangeTarget target = isStorage
  722. ? BufferRangeTarget.ShaderStorageBuffer
  723. : BufferRangeTarget.UniformBuffer;
  724. if (buffer.Buffer == null)
  725. {
  726. GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
  727. return;
  728. }
  729. int bufferHandle = ((Buffer)buffer.Buffer).Handle;
  730. IntPtr bufferOffset = (IntPtr)buffer.Offset;
  731. GL.BindBufferRange(target, bindingPoint, bufferHandle, bufferOffset, buffer.Size);
  732. }
  733. private void SetOrigin(ClipOrigin origin)
  734. {
  735. if (_clipOrigin != origin)
  736. {
  737. _clipOrigin = origin;
  738. GL.ClipControl(origin, _clipDepthMode);
  739. }
  740. }
  741. private void EnsureVertexArray()
  742. {
  743. if (_vertexArray == null)
  744. {
  745. _vertexArray = new VertexArray();
  746. _vertexArray.Bind();
  747. }
  748. }
  749. private void EnsureFramebuffer()
  750. {
  751. if (_framebuffer == null)
  752. {
  753. _framebuffer = new Framebuffer();
  754. _framebuffer.Bind();
  755. GL.Enable(EnableCap.FramebufferSrgb);
  756. }
  757. }
  758. private void UpdateDepthTest()
  759. {
  760. // Enabling depth operations is only valid when we have
  761. // a depth buffer, otherwise it's not allowed.
  762. if (_hasDepthBuffer)
  763. {
  764. if (_depthTest)
  765. {
  766. GL.Enable(EnableCap.DepthTest);
  767. }
  768. else
  769. {
  770. GL.Disable(EnableCap.DepthTest);
  771. }
  772. GL.DepthMask(_depthMask);
  773. }
  774. else
  775. {
  776. GL.Disable(EnableCap.DepthTest);
  777. GL.DepthMask(false);
  778. }
  779. }
  780. private void PrepareForDispatch()
  781. {
  782. if (_unit0Texture != null)
  783. {
  784. _unit0Texture.Bind(0);
  785. }
  786. }
  787. private void PrepareForDraw()
  788. {
  789. _vertexArray.Validate();
  790. if (_unit0Texture != null)
  791. {
  792. _unit0Texture.Bind(0);
  793. }
  794. }
  795. private void RestoreComponentMask(int index)
  796. {
  797. if (_componentMasks != null)
  798. {
  799. GL.ColorMask(
  800. index,
  801. (_componentMasks[index] & 1u) != 0,
  802. (_componentMasks[index] & 2u) != 0,
  803. (_componentMasks[index] & 4u) != 0,
  804. (_componentMasks[index] & 8u) != 0);
  805. }
  806. }
  807. public void RestoreScissor0Enable()
  808. {
  809. if (_scissor0Enable)
  810. {
  811. GL.Enable(IndexedEnableCap.ScissorTest, 0);
  812. }
  813. }
  814. public void RestoreRasterizerDiscard()
  815. {
  816. if (_rasterizerDiscard)
  817. {
  818. GL.Enable(EnableCap.RasterizerDiscard);
  819. }
  820. }
  821. public void Dispose()
  822. {
  823. _framebuffer?.Dispose();
  824. _vertexArray?.Dispose();
  825. }
  826. }
  827. }