Pipeline.cs 35 KB

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