Pipeline.cs 26 KB

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