Pipeline.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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
  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. // GL.PolygonOffsetClamp(factor, units, clamp);
  398. }
  399. public void SetDepthMode(DepthMode mode)
  400. {
  401. ClipDepthMode depthMode = mode.Convert();
  402. if (_clipDepthMode != depthMode)
  403. {
  404. _clipDepthMode = depthMode;
  405. GL.ClipControl(_clipOrigin, depthMode);
  406. }
  407. }
  408. public void SetDepthTest(DepthTestDescriptor depthTest)
  409. {
  410. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  411. _depthMask = depthTest.WriteEnable;
  412. _depthTest = depthTest.TestEnable;
  413. UpdateDepthTest();
  414. }
  415. public void SetFaceCulling(bool enable, Face face)
  416. {
  417. if (!enable)
  418. {
  419. GL.Disable(EnableCap.CullFace);
  420. return;
  421. }
  422. GL.CullFace(face.Convert());
  423. GL.Enable(EnableCap.CullFace);
  424. }
  425. public void SetFrontFace(FrontFace frontFace)
  426. {
  427. GL.FrontFace(frontFace.Convert());
  428. }
  429. public void SetImage(int index, ShaderStage stage, ITexture texture)
  430. {
  431. int unit = _program.GetImageUnit(stage, index);
  432. if (unit != -1 && texture != null)
  433. {
  434. TextureView view = (TextureView)texture;
  435. FormatInfo formatInfo = FormatTable.GetFormatInfo(view.Format);
  436. SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;
  437. GL.BindImageTexture(unit, view.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
  438. }
  439. }
  440. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  441. {
  442. _elementsType = type.Convert();
  443. _indexBaseOffset = (IntPtr)buffer.Offset;
  444. EnsureVertexArray();
  445. _vertexArray.SetIndexBuffer((Buffer)buffer.Buffer);
  446. }
  447. public void SetPrimitiveRestart(bool enable, int index)
  448. {
  449. if (!enable)
  450. {
  451. GL.Disable(EnableCap.PrimitiveRestart);
  452. return;
  453. }
  454. GL.PrimitiveRestartIndex(index);
  455. GL.Enable(EnableCap.PrimitiveRestart);
  456. }
  457. public void SetPrimitiveTopology(PrimitiveTopology topology)
  458. {
  459. _primitiveType = topology.Convert();
  460. }
  461. public void SetProgram(IProgram program)
  462. {
  463. _program = (Program)program;
  464. _program.Bind();
  465. }
  466. public void SetRenderTargetColorMasks(uint[] componentMasks)
  467. {
  468. _componentMasks = (uint[])componentMasks.Clone();
  469. for (int index = 0; index < componentMasks.Length; index++)
  470. {
  471. RestoreComponentMask(index);
  472. }
  473. }
  474. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  475. {
  476. EnsureFramebuffer();
  477. for (int index = 0; index < colors.Length; index++)
  478. {
  479. TextureView color = (TextureView)colors[index];
  480. _framebuffer.AttachColor(index, color);
  481. }
  482. TextureView depthStencilView = (TextureView)depthStencil;
  483. _framebuffer.AttachDepthStencil(depthStencilView);
  484. _framebuffer.SetDrawBuffers(colors.Length);
  485. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  486. UpdateDepthTest();
  487. }
  488. public void SetSampler(int index, ShaderStage stage, ISampler sampler)
  489. {
  490. int unit = _program.GetTextureUnit(stage, index);
  491. if (unit != -1 && sampler != null)
  492. {
  493. ((Sampler)sampler).Bind(unit);
  494. }
  495. }
  496. public void SetStencilTest(StencilTestDescriptor stencilTest)
  497. {
  498. if (!stencilTest.TestEnable)
  499. {
  500. GL.Disable(EnableCap.StencilTest);
  501. return;
  502. }
  503. GL.StencilOpSeparate(
  504. StencilFace.Front,
  505. stencilTest.FrontSFail.Convert(),
  506. stencilTest.FrontDpFail.Convert(),
  507. stencilTest.FrontDpPass.Convert());
  508. GL.StencilFuncSeparate(
  509. StencilFace.Front,
  510. (StencilFunction)stencilTest.FrontFunc.Convert(),
  511. stencilTest.FrontFuncRef,
  512. stencilTest.FrontFuncMask);
  513. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  514. GL.StencilOpSeparate(
  515. StencilFace.Back,
  516. stencilTest.BackSFail.Convert(),
  517. stencilTest.BackDpFail.Convert(),
  518. stencilTest.BackDpPass.Convert());
  519. GL.StencilFuncSeparate(
  520. StencilFace.Back,
  521. (StencilFunction)stencilTest.BackFunc.Convert(),
  522. stencilTest.BackFuncRef,
  523. stencilTest.BackFuncMask);
  524. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  525. GL.Enable(EnableCap.StencilTest);
  526. _stencilFrontMask = stencilTest.FrontMask;
  527. }
  528. public void SetStorageBuffer(int index, ShaderStage stage, BufferRange buffer)
  529. {
  530. SetBuffer(index, stage, buffer, isStorage: true);
  531. }
  532. public void SetTexture(int index, ShaderStage stage, ITexture texture)
  533. {
  534. int unit = _program.GetTextureUnit(stage, index);
  535. if (unit != -1 && texture != null)
  536. {
  537. if (unit == 0)
  538. {
  539. _unit0Texture = ((TextureView)texture);
  540. }
  541. else
  542. {
  543. ((TextureView)texture).Bind(unit);
  544. }
  545. }
  546. }
  547. public void SetUniformBuffer(int index, ShaderStage stage, BufferRange buffer)
  548. {
  549. SetBuffer(index, stage, buffer, isStorage: false);
  550. }
  551. public void SetVertexAttribs(VertexAttribDescriptor[] vertexAttribs)
  552. {
  553. EnsureVertexArray();
  554. _vertexArray.SetVertexAttributes(vertexAttribs);
  555. }
  556. public void SetVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
  557. {
  558. EnsureVertexArray();
  559. _vertexArray.SetVertexBuffers(vertexBuffers);
  560. }
  561. public void SetViewports(int first, Viewport[] viewports)
  562. {
  563. bool flipY = false;
  564. float[] viewportArray = new float[viewports.Length * 4];
  565. double[] depthRangeArray = new double[viewports.Length * 2];
  566. for (int index = 0; index < viewports.Length; index++)
  567. {
  568. int viewportElemIndex = index * 4;
  569. Viewport viewport = viewports[index];
  570. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  571. viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
  572. // OpenGL does not support per-viewport flipping, so
  573. // instead we decide that based on the viewport 0 value.
  574. // It will apply to all viewports.
  575. if (index == 0)
  576. {
  577. flipY = viewport.Region.Height < 0;
  578. }
  579. if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
  580. {
  581. flipY = !flipY;
  582. }
  583. viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
  584. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  585. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  586. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  587. }
  588. GL.ViewportArray(first, viewports.Length, viewportArray);
  589. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  590. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  591. }
  592. public void TextureBarrier()
  593. {
  594. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  595. }
  596. public void TextureBarrierTiled()
  597. {
  598. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  599. }
  600. private void SetBuffer(int index, ShaderStage stage, BufferRange buffer, bool isStorage)
  601. {
  602. int bindingPoint = isStorage
  603. ? _program.GetStorageBufferBindingPoint(stage, index)
  604. : _program.GetUniformBufferBindingPoint(stage, index);
  605. if (bindingPoint == -1)
  606. {
  607. return;
  608. }
  609. BufferRangeTarget target = isStorage
  610. ? BufferRangeTarget.ShaderStorageBuffer
  611. : BufferRangeTarget.UniformBuffer;
  612. if (buffer.Buffer == null)
  613. {
  614. GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
  615. return;
  616. }
  617. int bufferHandle = ((Buffer)buffer.Buffer).Handle;
  618. IntPtr bufferOffset = (IntPtr)buffer.Offset;
  619. GL.BindBufferRange(target, bindingPoint, bufferHandle, bufferOffset, buffer.Size);
  620. }
  621. private void SetOrigin(ClipOrigin origin)
  622. {
  623. if (_clipOrigin != origin)
  624. {
  625. _clipOrigin = origin;
  626. GL.ClipControl(origin, _clipDepthMode);
  627. }
  628. }
  629. private void EnsureVertexArray()
  630. {
  631. if (_vertexArray == null)
  632. {
  633. _vertexArray = new VertexArray();
  634. _vertexArray.Bind();
  635. }
  636. }
  637. private void EnsureFramebuffer()
  638. {
  639. if (_framebuffer == null)
  640. {
  641. _framebuffer = new Framebuffer();
  642. _framebuffer.Bind();
  643. GL.Enable(EnableCap.FramebufferSrgb);
  644. }
  645. }
  646. private void UpdateDepthTest()
  647. {
  648. // Enabling depth operations is only valid when we have
  649. // a depth buffer, otherwise it's not allowed.
  650. if (_hasDepthBuffer)
  651. {
  652. if (_depthTest)
  653. {
  654. GL.Enable(EnableCap.DepthTest);
  655. }
  656. else
  657. {
  658. GL.Disable(EnableCap.DepthTest);
  659. }
  660. GL.DepthMask(_depthMask);
  661. }
  662. else
  663. {
  664. GL.Disable(EnableCap.DepthTest);
  665. GL.DepthMask(false);
  666. }
  667. }
  668. private void PrepareForDispatch()
  669. {
  670. if (_unit0Texture != null)
  671. {
  672. _unit0Texture.Bind(0);
  673. }
  674. }
  675. private void PrepareForDraw()
  676. {
  677. _vertexArray.Validate();
  678. if (_unit0Texture != null)
  679. {
  680. _unit0Texture.Bind(0);
  681. }
  682. }
  683. private void RestoreComponentMask(int index)
  684. {
  685. if (_componentMasks != null)
  686. {
  687. GL.ColorMask(
  688. index,
  689. (_componentMasks[index] & 1u) != 0,
  690. (_componentMasks[index] & 2u) != 0,
  691. (_componentMasks[index] & 4u) != 0,
  692. (_componentMasks[index] & 8u) != 0);
  693. }
  694. }
  695. }
  696. }