Pipeline.cs 25 KB

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