Pipeline.cs 28 KB

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