Pipeline.cs 27 KB

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