Pipeline.cs 26 KB

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