Pipeline.cs 29 KB

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