Pipeline.cs 31 KB

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