Pipeline.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. GL.DispatchCompute(groupsX, groupsY, groupsZ);
  225. }
  226. public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
  227. {
  228. if (!_program.IsLinked)
  229. {
  230. return;
  231. }
  232. PrepareForDraw();
  233. if (_primitiveType == PrimitiveType.Quads)
  234. {
  235. DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  236. }
  237. else if (_primitiveType == PrimitiveType.QuadStrip)
  238. {
  239. DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  240. }
  241. else
  242. {
  243. DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  244. }
  245. }
  246. private void DrawQuadsImpl(
  247. int vertexCount,
  248. int instanceCount,
  249. int firstVertex,
  250. int firstInstance)
  251. {
  252. // TODO: Instanced rendering.
  253. int quadsCount = vertexCount / 4;
  254. int[] firsts = new int[quadsCount];
  255. int[] counts = new int[quadsCount];
  256. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  257. {
  258. firsts[quadIndex] = firstVertex + quadIndex * 4;
  259. counts[quadIndex] = 4;
  260. }
  261. GL.MultiDrawArrays(
  262. PrimitiveType.TriangleFan,
  263. firsts,
  264. counts,
  265. quadsCount);
  266. }
  267. private void DrawQuadStripImpl(
  268. int vertexCount,
  269. int instanceCount,
  270. int firstVertex,
  271. int firstInstance)
  272. {
  273. // TODO: Instanced rendering.
  274. int quadsCount = (vertexCount - 2) / 2;
  275. int[] firsts = new int[quadsCount];
  276. int[] counts = new int[quadsCount];
  277. firsts[0] = firstVertex;
  278. counts[0] = 4;
  279. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  280. {
  281. firsts[quadIndex] = firstVertex + quadIndex * 2;
  282. counts[quadIndex] = 4;
  283. }
  284. GL.MultiDrawArrays(
  285. PrimitiveType.TriangleFan,
  286. firsts,
  287. counts,
  288. quadsCount);
  289. }
  290. private void DrawImpl(
  291. int vertexCount,
  292. int instanceCount,
  293. int firstVertex,
  294. int firstInstance)
  295. {
  296. if (firstInstance == 0 && instanceCount == 1)
  297. {
  298. GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
  299. }
  300. else if (firstInstance == 0)
  301. {
  302. GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
  303. }
  304. else
  305. {
  306. GL.DrawArraysInstancedBaseInstance(
  307. _primitiveType,
  308. firstVertex,
  309. vertexCount,
  310. instanceCount,
  311. firstInstance);
  312. }
  313. }
  314. public void DrawIndexed(
  315. int indexCount,
  316. int instanceCount,
  317. int firstIndex,
  318. int firstVertex,
  319. int firstInstance)
  320. {
  321. if (!_program.IsLinked)
  322. {
  323. return;
  324. }
  325. PrepareForDraw();
  326. int firstIndexOffset = firstIndex;
  327. int indexElemSize = 1;
  328. switch (_elementsType)
  329. {
  330. case DrawElementsType.UnsignedShort: indexElemSize = 2; break;
  331. case DrawElementsType.UnsignedInt: indexElemSize = 4; break;
  332. }
  333. IntPtr indexBaseOffset = _indexBaseOffset + firstIndex * indexElemSize;
  334. if (_primitiveType == PrimitiveType.Quads)
  335. {
  336. DrawQuadsIndexedImpl(
  337. indexCount,
  338. instanceCount,
  339. indexBaseOffset,
  340. indexElemSize,
  341. firstVertex,
  342. firstInstance);
  343. }
  344. else if (_primitiveType == PrimitiveType.QuadStrip)
  345. {
  346. DrawQuadStripIndexedImpl(
  347. indexCount,
  348. instanceCount,
  349. indexBaseOffset,
  350. indexElemSize,
  351. firstVertex,
  352. firstInstance);
  353. }
  354. else
  355. {
  356. DrawIndexedImpl(
  357. indexCount,
  358. instanceCount,
  359. indexBaseOffset,
  360. indexElemSize,
  361. firstVertex,
  362. firstInstance);
  363. }
  364. }
  365. private void DrawQuadsIndexedImpl(
  366. int indexCount,
  367. int instanceCount,
  368. IntPtr indexBaseOffset,
  369. int indexElemSize,
  370. int firstVertex,
  371. int firstInstance)
  372. {
  373. // TODO: Instanced rendering.
  374. int quadsCount = indexCount / 4;
  375. IntPtr[] indices = new IntPtr[quadsCount];
  376. int[] counts = new int[quadsCount];
  377. int[] baseVertices = new int[quadsCount];
  378. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  379. {
  380. indices[quadIndex] = indexBaseOffset + quadIndex * 4 * indexElemSize;
  381. counts[quadIndex] = 4;
  382. baseVertices[quadIndex] = firstVertex;
  383. }
  384. GL.MultiDrawElementsBaseVertex(
  385. PrimitiveType.TriangleFan,
  386. counts,
  387. _elementsType,
  388. indices,
  389. quadsCount,
  390. baseVertices);
  391. }
  392. private void DrawQuadStripIndexedImpl(
  393. int indexCount,
  394. int instanceCount,
  395. IntPtr indexBaseOffset,
  396. int indexElemSize,
  397. int firstVertex,
  398. int firstInstance)
  399. {
  400. // TODO: Instanced rendering.
  401. int quadsCount = (indexCount - 2) / 2;
  402. IntPtr[] indices = new IntPtr[quadsCount];
  403. int[] counts = new int[quadsCount];
  404. int[] baseVertices = new int[quadsCount];
  405. indices[0] = indexBaseOffset;
  406. counts[0] = 4;
  407. baseVertices[0] = firstVertex;
  408. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  409. {
  410. indices[quadIndex] = indexBaseOffset + quadIndex * 2 * indexElemSize;
  411. counts[quadIndex] = 4;
  412. baseVertices[quadIndex] = firstVertex;
  413. }
  414. GL.MultiDrawElementsBaseVertex(
  415. PrimitiveType.TriangleFan,
  416. counts,
  417. _elementsType,
  418. indices,
  419. quadsCount,
  420. baseVertices);
  421. }
  422. private void DrawIndexedImpl(
  423. int indexCount,
  424. int instanceCount,
  425. IntPtr indexBaseOffset,
  426. int indexElemSize,
  427. int firstVertex,
  428. int firstInstance)
  429. {
  430. if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
  431. {
  432. GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
  433. }
  434. else if (firstInstance == 0 && instanceCount == 1)
  435. {
  436. GL.DrawElementsBaseVertex(
  437. _primitiveType,
  438. indexCount,
  439. _elementsType,
  440. indexBaseOffset,
  441. firstVertex);
  442. }
  443. else if (firstInstance == 0 && firstVertex == 0)
  444. {
  445. GL.DrawElementsInstanced(
  446. _primitiveType,
  447. indexCount,
  448. _elementsType,
  449. indexBaseOffset,
  450. instanceCount);
  451. }
  452. else if (firstInstance == 0)
  453. {
  454. GL.DrawElementsInstancedBaseVertex(
  455. _primitiveType,
  456. indexCount,
  457. _elementsType,
  458. indexBaseOffset,
  459. instanceCount,
  460. firstVertex);
  461. }
  462. else if (firstVertex == 0)
  463. {
  464. GL.DrawElementsInstancedBaseInstance(
  465. _primitiveType,
  466. indexCount,
  467. _elementsType,
  468. indexBaseOffset,
  469. instanceCount,
  470. firstInstance);
  471. }
  472. else
  473. {
  474. GL.DrawElementsInstancedBaseVertexBaseInstance(
  475. _primitiveType,
  476. indexCount,
  477. _elementsType,
  478. indexBaseOffset,
  479. instanceCount,
  480. firstVertex,
  481. firstInstance);
  482. }
  483. }
  484. public void DrawIndirect(BufferRange buffer, ulong offset, int drawCount, int stride)
  485. {
  486. }
  487. public void DrawIndexedIndirect(BufferRange buffer, ulong offset, int drawCount, int stride)
  488. {
  489. }
  490. public void SetBlendColor(ColorF color)
  491. {
  492. GL.BlendColor(color.Red, color.Green, color.Blue, color.Alpha);
  493. }
  494. public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
  495. {
  496. if ((enables & PolygonModeMask.Point) != 0)
  497. {
  498. GL.Enable(EnableCap.PolygonOffsetPoint);
  499. }
  500. else
  501. {
  502. GL.Disable(EnableCap.PolygonOffsetPoint);
  503. }
  504. if ((enables & PolygonModeMask.Line) != 0)
  505. {
  506. GL.Enable(EnableCap.PolygonOffsetLine);
  507. }
  508. else
  509. {
  510. GL.Disable(EnableCap.PolygonOffsetLine);
  511. }
  512. if ((enables & PolygonModeMask.Fill) != 0)
  513. {
  514. GL.Enable(EnableCap.PolygonOffsetFill);
  515. }
  516. else
  517. {
  518. GL.Disable(EnableCap.PolygonOffsetFill);
  519. }
  520. if (enables == 0)
  521. {
  522. return;
  523. }
  524. GL.PolygonOffset(factor, units);
  525. // GL.PolygonOffsetClamp(factor, units, clamp);
  526. }
  527. public void SetDepthTest(DepthTestDescriptor depthTest)
  528. {
  529. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  530. _depthMask = depthTest.WriteEnable;
  531. _depthTest = depthTest.TestEnable;
  532. UpdateDepthTest();
  533. }
  534. public void SetFaceCulling(bool enable, Face face)
  535. {
  536. if (!enable)
  537. {
  538. GL.Disable(EnableCap.CullFace);
  539. return;
  540. }
  541. GL.CullFace(face.Convert());
  542. GL.Enable(EnableCap.CullFace);
  543. }
  544. public void SetFrontFace(FrontFace frontFace)
  545. {
  546. GL.FrontFace(frontFace.Convert());
  547. }
  548. public void SetPrimitiveRestart(bool enable, int index)
  549. {
  550. if (!enable)
  551. {
  552. GL.Disable(EnableCap.PrimitiveRestart);
  553. return;
  554. }
  555. GL.PrimitiveRestartIndex(index);
  556. GL.Enable(EnableCap.PrimitiveRestart);
  557. }
  558. public void SetPrimitiveTopology(PrimitiveTopology topology)
  559. {
  560. _primitiveType = topology.Convert();
  561. }
  562. public void SetRenderTargetColorMasks(uint[] componentMasks)
  563. {
  564. _componentMasks = (uint[])componentMasks.Clone();
  565. for (int index = 0; index < componentMasks.Length; index++)
  566. {
  567. RestoreComponentMask(index);
  568. }
  569. }
  570. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  571. {
  572. EnsureFramebuffer();
  573. for (int index = 0; index < colors.Length; index++)
  574. {
  575. TextureView color = (TextureView)colors[index];
  576. _framebuffer.AttachColor(index, color);
  577. }
  578. TextureView depthStencilView = (TextureView)depthStencil;
  579. _framebuffer.AttachDepthStencil(depthStencilView);
  580. _framebuffer.SetDrawBuffers(colors.Length);
  581. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  582. UpdateDepthTest();
  583. }
  584. public void SetStencilTest(StencilTestDescriptor stencilTest)
  585. {
  586. if (!stencilTest.TestEnable)
  587. {
  588. GL.Disable(EnableCap.StencilTest);
  589. return;
  590. }
  591. GL.StencilOpSeparate(
  592. StencilFace.Front,
  593. stencilTest.FrontSFail.Convert(),
  594. stencilTest.FrontDpFail.Convert(),
  595. stencilTest.FrontDpPass.Convert());
  596. GL.StencilFuncSeparate(
  597. StencilFace.Front,
  598. (StencilFunction)stencilTest.FrontFunc.Convert(),
  599. stencilTest.FrontFuncRef,
  600. stencilTest.FrontFuncMask);
  601. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  602. GL.StencilOpSeparate(
  603. StencilFace.Back,
  604. stencilTest.BackSFail.Convert(),
  605. stencilTest.BackDpFail.Convert(),
  606. stencilTest.BackDpPass.Convert());
  607. GL.StencilFuncSeparate(
  608. StencilFace.Back,
  609. (StencilFunction)stencilTest.BackFunc.Convert(),
  610. stencilTest.BackFuncRef,
  611. stencilTest.BackFuncMask);
  612. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  613. GL.Enable(EnableCap.StencilTest);
  614. _stencilFrontMask = stencilTest.FrontMask;
  615. }
  616. public void SetViewports(int first, Viewport[] viewports)
  617. {
  618. bool flipY = false;
  619. float[] viewportArray = new float[viewports.Length * 4];
  620. double[] depthRangeArray = new double[viewports.Length * 2];
  621. for (int index = 0; index < viewports.Length; index++)
  622. {
  623. int viewportElemIndex = index * 4;
  624. Viewport viewport = viewports[index];
  625. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  626. viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
  627. // OpenGL does not support per-viewport flipping, so
  628. // instead we decide that based on the viewport 0 value.
  629. // It will apply to all viewports.
  630. if (index == 0)
  631. {
  632. flipY = viewport.Region.Height < 0;
  633. }
  634. if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
  635. {
  636. flipY = !flipY;
  637. }
  638. viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
  639. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  640. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  641. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  642. }
  643. GL.ViewportArray(first, viewports.Length, viewportArray);
  644. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  645. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  646. }
  647. public void TextureBarrier()
  648. {
  649. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  650. }
  651. public void TextureBarrierTiled()
  652. {
  653. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  654. }
  655. private void SetOrigin(ClipOrigin origin)
  656. {
  657. if (_clipOrigin != origin)
  658. {
  659. _clipOrigin = origin;
  660. GL.ClipControl(origin, ClipDepthMode.NegativeOneToOne);
  661. }
  662. }
  663. private void EnsureVertexArray()
  664. {
  665. if (_vertexArray == null)
  666. {
  667. _vertexArray = new VertexArray();
  668. _vertexArray.Bind();
  669. }
  670. }
  671. private void EnsureFramebuffer()
  672. {
  673. if (_framebuffer == null)
  674. {
  675. _framebuffer = new Framebuffer();
  676. _framebuffer.Bind();
  677. GL.Enable(EnableCap.FramebufferSrgb);
  678. }
  679. }
  680. private void UpdateDepthTest()
  681. {
  682. // Enabling depth operations is only valid when we have
  683. // a depth buffer, otherwise it's not allowed.
  684. if (_hasDepthBuffer)
  685. {
  686. if (_depthTest)
  687. {
  688. GL.Enable(EnableCap.DepthTest);
  689. }
  690. else
  691. {
  692. GL.Disable(EnableCap.DepthTest);
  693. }
  694. GL.DepthMask(_depthMask);
  695. }
  696. else
  697. {
  698. GL.Disable(EnableCap.DepthTest);
  699. GL.DepthMask(false);
  700. }
  701. }
  702. private void PrepareForDraw()
  703. {
  704. _vertexArray.Validate();
  705. if (_unit0Texture != null)
  706. {
  707. _unit0Texture.Bind(0);
  708. }
  709. }
  710. private void RestoreComponentMask(int index)
  711. {
  712. if (_componentMasks != null)
  713. {
  714. GL.ColorMask(
  715. index,
  716. (_componentMasks[index] & 1u) != 0,
  717. (_componentMasks[index] & 2u) != 0,
  718. (_componentMasks[index] & 4u) != 0,
  719. (_componentMasks[index] & 8u) != 0);
  720. }
  721. }
  722. public void RebindProgram()
  723. {
  724. _program?.Bind();
  725. }
  726. }
  727. }