Pipeline.cs 28 KB

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