Pipeline.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. using System.Runtime.CompilerServices;
  9. using System.Runtime.InteropServices;
  10. namespace Ryujinx.Graphics.OpenGL
  11. {
  12. class Pipeline : IPipeline, IDisposable
  13. {
  14. internal ulong DrawCount { get; private set; }
  15. private Program _program;
  16. private bool _rasterizerDiscard;
  17. private VertexArray _vertexArray;
  18. private Framebuffer _framebuffer;
  19. private IntPtr _indexBaseOffset;
  20. private DrawElementsType _elementsType;
  21. private PrimitiveType _primitiveType;
  22. private int _stencilFrontMask;
  23. private bool _depthMask;
  24. private int _boundDrawFramebuffer;
  25. private int _boundReadFramebuffer;
  26. private CounterQueueEvent _activeConditionalRender;
  27. private struct Vector4<T>
  28. {
  29. public T X;
  30. public T Y;
  31. public T Z;
  32. public T W;
  33. }
  34. private Vector4<int>[] _fpIsBgra = new Vector4<int>[SupportBuffer.FragmentIsBgraCount];
  35. private Vector4<float>[] _renderScale = new Vector4<float>[65];
  36. private TextureBase _unit0Texture;
  37. private FrontFaceDirection _frontFace;
  38. private ClipOrigin _clipOrigin;
  39. private ClipDepthMode _clipDepthMode;
  40. private readonly uint[] _componentMasks;
  41. private uint _scissorEnables;
  42. private bool _tfEnabled;
  43. private TransformFeedbackPrimitiveType _tfTopology;
  44. private BufferHandle _supportBuffer;
  45. private readonly BufferHandle[] _tfbs;
  46. private readonly BufferRange[] _tfbTargets;
  47. private ColorF _blendConstant;
  48. internal Pipeline()
  49. {
  50. _rasterizerDiscard = false;
  51. _clipOrigin = ClipOrigin.LowerLeft;
  52. _clipDepthMode = ClipDepthMode.NegativeOneToOne;
  53. _componentMasks = new uint[Constants.MaxRenderTargets];
  54. for (int index = 0; index < Constants.MaxRenderTargets; index++)
  55. {
  56. _componentMasks[index] = 0xf;
  57. }
  58. var defaultScale = new Vector4<float> { X = 1f, Y = 0f, Z = 0f, W = 0f };
  59. new Span<Vector4<float>>(_renderScale).Fill(defaultScale);
  60. _tfbs = new BufferHandle[Constants.MaxTransformFeedbackBuffers];
  61. _tfbTargets = new BufferRange[Constants.MaxTransformFeedbackBuffers];
  62. }
  63. public void Initialize()
  64. {
  65. _supportBuffer = Buffer.Create(SupportBuffer.RequiredSize);
  66. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, Unsafe.As<BufferHandle, int>(ref _supportBuffer));
  67. SetSupportBufferData<Vector4<int>>(SupportBuffer.FragmentIsBgraOffset, _fpIsBgra, SupportBuffer.FragmentIsBgraCount);
  68. SetSupportBufferData<Vector4<float>>(SupportBuffer.FragmentRenderScaleOffset, _renderScale, SupportBuffer.RenderScaleMaxCount);
  69. }
  70. public void Barrier()
  71. {
  72. GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
  73. }
  74. public void BeginTransformFeedback(PrimitiveTopology topology)
  75. {
  76. GL.BeginTransformFeedback(_tfTopology = topology.ConvertToTfType());
  77. _tfEnabled = true;
  78. }
  79. public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
  80. {
  81. Buffer.Clear(destination, offset, size, value);
  82. }
  83. public void ClearRenderTargetColor(int index, uint componentMask, ColorF color)
  84. {
  85. GL.ColorMask(
  86. index,
  87. (componentMask & 1) != 0,
  88. (componentMask & 2) != 0,
  89. (componentMask & 4) != 0,
  90. (componentMask & 8) != 0);
  91. float[] colors = new float[] { color.Red, color.Green, color.Blue, color.Alpha };
  92. GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Color, index, colors);
  93. RestoreComponentMask(index);
  94. }
  95. public void ClearRenderTargetDepthStencil(float depthValue, bool depthMask, int stencilValue, int stencilMask)
  96. {
  97. bool stencilMaskChanged =
  98. stencilMask != 0 &&
  99. stencilMask != _stencilFrontMask;
  100. bool depthMaskChanged = depthMask && depthMask != _depthMask;
  101. if (stencilMaskChanged)
  102. {
  103. GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
  104. }
  105. if (depthMaskChanged)
  106. {
  107. GL.DepthMask(depthMask);
  108. }
  109. if (depthMask && stencilMask != 0)
  110. {
  111. GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
  112. }
  113. else if (depthMask)
  114. {
  115. GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Depth, 0, ref depthValue);
  116. }
  117. else if (stencilMask != 0)
  118. {
  119. GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Stencil, 0, ref stencilValue);
  120. }
  121. if (stencilMaskChanged)
  122. {
  123. GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
  124. }
  125. if (depthMaskChanged)
  126. {
  127. GL.DepthMask(_depthMask);
  128. }
  129. }
  130. public void CommandBufferBarrier()
  131. {
  132. GL.MemoryBarrier(MemoryBarrierFlags.CommandBarrierBit);
  133. }
  134. public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
  135. {
  136. Buffer.Copy(source, destination, srcOffset, dstOffset, size);
  137. }
  138. public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
  139. {
  140. if (!_program.IsLinked)
  141. {
  142. Logger.Debug?.Print(LogClass.Gpu, "Dispatch error, shader not linked.");
  143. return;
  144. }
  145. PrepareForDispatch();
  146. GL.DispatchCompute(groupsX, groupsY, groupsZ);
  147. }
  148. public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
  149. {
  150. if (!_program.IsLinked)
  151. {
  152. Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
  153. return;
  154. }
  155. PreDraw();
  156. if (_primitiveType == PrimitiveType.Quads && !HwCapabilities.SupportsQuads)
  157. {
  158. DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  159. }
  160. else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
  161. {
  162. DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  163. }
  164. else
  165. {
  166. DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
  167. }
  168. PostDraw();
  169. }
  170. private void DrawQuadsImpl(
  171. int vertexCount,
  172. int instanceCount,
  173. int firstVertex,
  174. int firstInstance)
  175. {
  176. // TODO: Instanced rendering.
  177. int quadsCount = vertexCount / 4;
  178. int[] firsts = new int[quadsCount];
  179. int[] counts = new int[quadsCount];
  180. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  181. {
  182. firsts[quadIndex] = firstVertex + quadIndex * 4;
  183. counts[quadIndex] = 4;
  184. }
  185. GL.MultiDrawArrays(
  186. PrimitiveType.TriangleFan,
  187. firsts,
  188. counts,
  189. quadsCount);
  190. }
  191. private void DrawQuadStripImpl(
  192. int vertexCount,
  193. int instanceCount,
  194. int firstVertex,
  195. int firstInstance)
  196. {
  197. int quadsCount = (vertexCount - 2) / 2;
  198. if (firstInstance != 0 || instanceCount != 1)
  199. {
  200. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  201. {
  202. GL.DrawArraysInstancedBaseInstance(PrimitiveType.TriangleFan, firstVertex + quadIndex * 2, 4, instanceCount, firstInstance);
  203. }
  204. }
  205. else
  206. {
  207. int[] firsts = new int[quadsCount];
  208. int[] counts = new int[quadsCount];
  209. firsts[0] = firstVertex;
  210. counts[0] = 4;
  211. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  212. {
  213. firsts[quadIndex] = firstVertex + quadIndex * 2;
  214. counts[quadIndex] = 4;
  215. }
  216. GL.MultiDrawArrays(
  217. PrimitiveType.TriangleFan,
  218. firsts,
  219. counts,
  220. quadsCount);
  221. }
  222. }
  223. private void DrawImpl(
  224. int vertexCount,
  225. int instanceCount,
  226. int firstVertex,
  227. int firstInstance)
  228. {
  229. if (firstInstance == 0 && instanceCount == 1)
  230. {
  231. GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
  232. }
  233. else if (firstInstance == 0)
  234. {
  235. GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
  236. }
  237. else
  238. {
  239. GL.DrawArraysInstancedBaseInstance(
  240. _primitiveType,
  241. firstVertex,
  242. vertexCount,
  243. instanceCount,
  244. firstInstance);
  245. }
  246. }
  247. public void DrawIndexed(
  248. int indexCount,
  249. int instanceCount,
  250. int firstIndex,
  251. int firstVertex,
  252. int firstInstance)
  253. {
  254. if (!_program.IsLinked)
  255. {
  256. Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
  257. return;
  258. }
  259. PreDraw();
  260. int indexElemSize = 1;
  261. switch (_elementsType)
  262. {
  263. case DrawElementsType.UnsignedShort: indexElemSize = 2; break;
  264. case DrawElementsType.UnsignedInt: indexElemSize = 4; break;
  265. }
  266. IntPtr indexBaseOffset = _indexBaseOffset + firstIndex * indexElemSize;
  267. if (_primitiveType == PrimitiveType.Quads && !HwCapabilities.SupportsQuads)
  268. {
  269. DrawQuadsIndexedImpl(
  270. indexCount,
  271. instanceCount,
  272. indexBaseOffset,
  273. indexElemSize,
  274. firstVertex,
  275. firstInstance);
  276. }
  277. else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
  278. {
  279. DrawQuadStripIndexedImpl(
  280. indexCount,
  281. instanceCount,
  282. indexBaseOffset,
  283. indexElemSize,
  284. firstVertex,
  285. firstInstance);
  286. }
  287. else
  288. {
  289. DrawIndexedImpl(
  290. indexCount,
  291. instanceCount,
  292. indexBaseOffset,
  293. firstVertex,
  294. firstInstance);
  295. }
  296. PostDraw();
  297. }
  298. private void DrawQuadsIndexedImpl(
  299. int indexCount,
  300. int instanceCount,
  301. IntPtr indexBaseOffset,
  302. int indexElemSize,
  303. int firstVertex,
  304. int firstInstance)
  305. {
  306. int quadsCount = indexCount / 4;
  307. if (firstInstance != 0 || instanceCount != 1)
  308. {
  309. if (firstVertex != 0 && firstInstance != 0)
  310. {
  311. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  312. {
  313. GL.DrawElementsInstancedBaseVertexBaseInstance(
  314. PrimitiveType.TriangleFan,
  315. 4,
  316. _elementsType,
  317. indexBaseOffset + quadIndex * 4 * indexElemSize,
  318. instanceCount,
  319. firstVertex,
  320. firstInstance);
  321. }
  322. }
  323. else if (firstInstance != 0)
  324. {
  325. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  326. {
  327. GL.DrawElementsInstancedBaseInstance(
  328. PrimitiveType.TriangleFan,
  329. 4,
  330. _elementsType,
  331. indexBaseOffset + quadIndex * 4 * indexElemSize,
  332. instanceCount,
  333. firstInstance);
  334. }
  335. }
  336. else
  337. {
  338. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  339. {
  340. GL.DrawElementsInstanced(
  341. PrimitiveType.TriangleFan,
  342. 4,
  343. _elementsType,
  344. indexBaseOffset + quadIndex * 4 * indexElemSize,
  345. instanceCount);
  346. }
  347. }
  348. }
  349. else
  350. {
  351. IntPtr[] indices = new IntPtr[quadsCount];
  352. int[] counts = new int[quadsCount];
  353. int[] baseVertices = new int[quadsCount];
  354. for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
  355. {
  356. indices[quadIndex] = indexBaseOffset + quadIndex * 4 * indexElemSize;
  357. counts[quadIndex] = 4;
  358. baseVertices[quadIndex] = firstVertex;
  359. }
  360. GL.MultiDrawElementsBaseVertex(
  361. PrimitiveType.TriangleFan,
  362. counts,
  363. _elementsType,
  364. indices,
  365. quadsCount,
  366. baseVertices);
  367. }
  368. }
  369. private void DrawQuadStripIndexedImpl(
  370. int indexCount,
  371. int instanceCount,
  372. IntPtr indexBaseOffset,
  373. int indexElemSize,
  374. int firstVertex,
  375. int firstInstance)
  376. {
  377. // TODO: Instanced rendering.
  378. int quadsCount = (indexCount - 2) / 2;
  379. IntPtr[] indices = new IntPtr[quadsCount];
  380. int[] counts = new int[quadsCount];
  381. int[] baseVertices = new int[quadsCount];
  382. indices[0] = indexBaseOffset;
  383. counts[0] = 4;
  384. baseVertices[0] = firstVertex;
  385. for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
  386. {
  387. indices[quadIndex] = indexBaseOffset + quadIndex * 2 * indexElemSize;
  388. counts[quadIndex] = 4;
  389. baseVertices[quadIndex] = firstVertex;
  390. }
  391. GL.MultiDrawElementsBaseVertex(
  392. PrimitiveType.TriangleFan,
  393. counts,
  394. _elementsType,
  395. indices,
  396. quadsCount,
  397. baseVertices);
  398. }
  399. private void DrawIndexedImpl(
  400. int indexCount,
  401. int instanceCount,
  402. IntPtr indexBaseOffset,
  403. int firstVertex,
  404. int firstInstance)
  405. {
  406. if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
  407. {
  408. GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
  409. }
  410. else if (firstInstance == 0 && instanceCount == 1)
  411. {
  412. GL.DrawElementsBaseVertex(
  413. _primitiveType,
  414. indexCount,
  415. _elementsType,
  416. indexBaseOffset,
  417. firstVertex);
  418. }
  419. else if (firstInstance == 0 && firstVertex == 0)
  420. {
  421. GL.DrawElementsInstanced(
  422. _primitiveType,
  423. indexCount,
  424. _elementsType,
  425. indexBaseOffset,
  426. instanceCount);
  427. }
  428. else if (firstInstance == 0)
  429. {
  430. GL.DrawElementsInstancedBaseVertex(
  431. _primitiveType,
  432. indexCount,
  433. _elementsType,
  434. indexBaseOffset,
  435. instanceCount,
  436. firstVertex);
  437. }
  438. else if (firstVertex == 0)
  439. {
  440. GL.DrawElementsInstancedBaseInstance(
  441. _primitiveType,
  442. indexCount,
  443. _elementsType,
  444. indexBaseOffset,
  445. instanceCount,
  446. firstInstance);
  447. }
  448. else
  449. {
  450. GL.DrawElementsInstancedBaseVertexBaseInstance(
  451. _primitiveType,
  452. indexCount,
  453. _elementsType,
  454. indexBaseOffset,
  455. instanceCount,
  456. firstVertex,
  457. firstInstance);
  458. }
  459. }
  460. public void EndTransformFeedback()
  461. {
  462. GL.EndTransformFeedback();
  463. _tfEnabled = false;
  464. }
  465. public void MultiDrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
  466. {
  467. if (!_program.IsLinked)
  468. {
  469. Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
  470. return;
  471. }
  472. PreDraw();
  473. GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
  474. GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
  475. GL.MultiDrawArraysIndirectCount(
  476. _primitiveType,
  477. (IntPtr)indirectBuffer.Offset,
  478. (IntPtr)parameterBuffer.Offset,
  479. maxDrawCount,
  480. stride);
  481. PostDraw();
  482. }
  483. public void MultiDrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
  484. {
  485. if (!_program.IsLinked)
  486. {
  487. Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
  488. return;
  489. }
  490. PreDraw();
  491. _vertexArray.SetRangeOfIndexBuffer();
  492. GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
  493. GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
  494. GL.MultiDrawElementsIndirectCount(
  495. _primitiveType,
  496. (Version46)_elementsType,
  497. (IntPtr)indirectBuffer.Offset,
  498. (IntPtr)parameterBuffer.Offset,
  499. maxDrawCount,
  500. stride);
  501. _vertexArray.RestoreIndexBuffer();
  502. PostDraw();
  503. }
  504. public void SetAlphaTest(bool enable, float reference, CompareOp op)
  505. {
  506. if (!enable)
  507. {
  508. GL.Disable(EnableCap.AlphaTest);
  509. return;
  510. }
  511. GL.AlphaFunc((AlphaFunction)op.Convert(), reference);
  512. GL.Enable(EnableCap.AlphaTest);
  513. }
  514. public void SetBlendState(int index, BlendDescriptor blend)
  515. {
  516. if (!blend.Enable)
  517. {
  518. GL.Disable(IndexedEnableCap.Blend, index);
  519. return;
  520. }
  521. GL.BlendEquationSeparate(
  522. index,
  523. blend.ColorOp.Convert(),
  524. blend.AlphaOp.Convert());
  525. GL.BlendFuncSeparate(
  526. index,
  527. (BlendingFactorSrc)blend.ColorSrcFactor.Convert(),
  528. (BlendingFactorDest)blend.ColorDstFactor.Convert(),
  529. (BlendingFactorSrc)blend.AlphaSrcFactor.Convert(),
  530. (BlendingFactorDest)blend.AlphaDstFactor.Convert());
  531. static bool IsDualSource(BlendFactor factor)
  532. {
  533. switch (factor)
  534. {
  535. case BlendFactor.Src1Color:
  536. case BlendFactor.Src1ColorGl:
  537. case BlendFactor.Src1Alpha:
  538. case BlendFactor.Src1AlphaGl:
  539. case BlendFactor.OneMinusSrc1Color:
  540. case BlendFactor.OneMinusSrc1ColorGl:
  541. case BlendFactor.OneMinusSrc1Alpha:
  542. case BlendFactor.OneMinusSrc1AlphaGl:
  543. return true;
  544. }
  545. return false;
  546. }
  547. EnsureFramebuffer();
  548. _framebuffer.SetDualSourceBlend(
  549. IsDualSource(blend.ColorSrcFactor) ||
  550. IsDualSource(blend.ColorDstFactor) ||
  551. IsDualSource(blend.AlphaSrcFactor) ||
  552. IsDualSource(blend.AlphaDstFactor));
  553. if (_blendConstant != blend.BlendConstant)
  554. {
  555. _blendConstant = blend.BlendConstant;
  556. GL.BlendColor(
  557. blend.BlendConstant.Red,
  558. blend.BlendConstant.Green,
  559. blend.BlendConstant.Blue,
  560. blend.BlendConstant.Alpha);
  561. }
  562. GL.Enable(IndexedEnableCap.Blend, index);
  563. }
  564. public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
  565. {
  566. if ((enables & PolygonModeMask.Point) != 0)
  567. {
  568. GL.Enable(EnableCap.PolygonOffsetPoint);
  569. }
  570. else
  571. {
  572. GL.Disable(EnableCap.PolygonOffsetPoint);
  573. }
  574. if ((enables & PolygonModeMask.Line) != 0)
  575. {
  576. GL.Enable(EnableCap.PolygonOffsetLine);
  577. }
  578. else
  579. {
  580. GL.Disable(EnableCap.PolygonOffsetLine);
  581. }
  582. if ((enables & PolygonModeMask.Fill) != 0)
  583. {
  584. GL.Enable(EnableCap.PolygonOffsetFill);
  585. }
  586. else
  587. {
  588. GL.Disable(EnableCap.PolygonOffsetFill);
  589. }
  590. if (enables == 0)
  591. {
  592. return;
  593. }
  594. if (HwCapabilities.SupportsPolygonOffsetClamp)
  595. {
  596. GL.PolygonOffsetClamp(factor, units, clamp);
  597. }
  598. else
  599. {
  600. GL.PolygonOffset(factor, units);
  601. }
  602. }
  603. public void SetDepthClamp(bool clamp)
  604. {
  605. if (!clamp)
  606. {
  607. GL.Disable(EnableCap.DepthClamp);
  608. return;
  609. }
  610. GL.Enable(EnableCap.DepthClamp);
  611. }
  612. public void SetDepthMode(DepthMode mode)
  613. {
  614. ClipDepthMode depthMode = mode.Convert();
  615. if (_clipDepthMode != depthMode)
  616. {
  617. _clipDepthMode = depthMode;
  618. GL.ClipControl(_clipOrigin, depthMode);
  619. }
  620. }
  621. public void SetDepthTest(DepthTestDescriptor depthTest)
  622. {
  623. if (depthTest.TestEnable)
  624. {
  625. GL.Enable(EnableCap.DepthTest);
  626. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  627. }
  628. else
  629. {
  630. GL.Disable(EnableCap.DepthTest);
  631. }
  632. GL.DepthMask(depthTest.WriteEnable);
  633. _depthMask = depthTest.WriteEnable;
  634. }
  635. public void SetFaceCulling(bool enable, Face face)
  636. {
  637. if (!enable)
  638. {
  639. GL.Disable(EnableCap.CullFace);
  640. return;
  641. }
  642. GL.CullFace(face.Convert());
  643. GL.Enable(EnableCap.CullFace);
  644. }
  645. public void SetFrontFace(FrontFace frontFace)
  646. {
  647. SetFrontFace(_frontFace = frontFace.Convert());
  648. }
  649. public void SetImage(int binding, ITexture texture, Format imageFormat)
  650. {
  651. if (texture == null)
  652. {
  653. return;
  654. }
  655. TextureBase texBase = (TextureBase)texture;
  656. SizedInternalFormat format = FormatTable.GetImageFormat(imageFormat);
  657. if (format != 0)
  658. {
  659. GL.BindImageTexture(binding, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
  660. }
  661. }
  662. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  663. {
  664. _elementsType = type.Convert();
  665. _indexBaseOffset = (IntPtr)buffer.Offset;
  666. EnsureVertexArray();
  667. _vertexArray.SetIndexBuffer(buffer);
  668. }
  669. public void SetLogicOpState(bool enable, LogicalOp op)
  670. {
  671. if (enable)
  672. {
  673. GL.Enable(EnableCap.ColorLogicOp);
  674. GL.LogicOp((LogicOp)op.Convert());
  675. }
  676. else
  677. {
  678. GL.Disable(EnableCap.ColorLogicOp);
  679. }
  680. }
  681. public void SetLineParameters(float width, bool smooth)
  682. {
  683. if (smooth)
  684. {
  685. GL.Enable(EnableCap.LineSmooth);
  686. }
  687. else
  688. {
  689. GL.Disable(EnableCap.LineSmooth);
  690. }
  691. GL.LineWidth(width);
  692. }
  693. public unsafe void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
  694. {
  695. GL.PatchParameter(PatchParameterInt.PatchVertices, vertices);
  696. fixed (float* pOuterLevel = defaultOuterLevel)
  697. {
  698. GL.PatchParameter(PatchParameterFloat.PatchDefaultOuterLevel, pOuterLevel);
  699. }
  700. fixed (float* pInnerLevel = defaultInnerLevel)
  701. {
  702. GL.PatchParameter(PatchParameterFloat.PatchDefaultInnerLevel, pInnerLevel);
  703. }
  704. }
  705. public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
  706. {
  707. // GL_POINT_SPRITE was deprecated in core profile 3.2+ and causes GL_INVALID_ENUM when set.
  708. // As we don't know if the current context is core or compat, it's safer to keep this code.
  709. if (enablePointSprite)
  710. {
  711. GL.Enable(EnableCap.PointSprite);
  712. }
  713. else
  714. {
  715. GL.Disable(EnableCap.PointSprite);
  716. }
  717. if (isProgramPointSize)
  718. {
  719. GL.Enable(EnableCap.ProgramPointSize);
  720. }
  721. else
  722. {
  723. GL.Disable(EnableCap.ProgramPointSize);
  724. }
  725. GL.PointParameter(origin == Origin.LowerLeft
  726. ? PointSpriteCoordOriginParameter.LowerLeft
  727. : PointSpriteCoordOriginParameter.UpperLeft);
  728. // Games seem to set point size to 0 which generates a GL_INVALID_VALUE
  729. // From the spec, GL_INVALID_VALUE is generated if size is less than or equal to 0.
  730. GL.PointSize(Math.Max(float.Epsilon, size));
  731. }
  732. public void SetPolygonMode(GAL.PolygonMode frontMode, GAL.PolygonMode backMode)
  733. {
  734. if (frontMode == backMode)
  735. {
  736. GL.PolygonMode(MaterialFace.FrontAndBack, frontMode.Convert());
  737. }
  738. else
  739. {
  740. GL.PolygonMode(MaterialFace.Front, frontMode.Convert());
  741. GL.PolygonMode(MaterialFace.Back, backMode.Convert());
  742. }
  743. }
  744. public void SetPrimitiveRestart(bool enable, int index)
  745. {
  746. if (!enable)
  747. {
  748. GL.Disable(EnableCap.PrimitiveRestart);
  749. return;
  750. }
  751. GL.PrimitiveRestartIndex(index);
  752. GL.Enable(EnableCap.PrimitiveRestart);
  753. }
  754. public void SetPrimitiveTopology(PrimitiveTopology topology)
  755. {
  756. _primitiveType = topology.Convert();
  757. }
  758. public void SetProgram(IProgram program)
  759. {
  760. _program = (Program)program;
  761. if (_tfEnabled)
  762. {
  763. GL.EndTransformFeedback();
  764. _program.Bind();
  765. GL.BeginTransformFeedback(_tfTopology);
  766. }
  767. else
  768. {
  769. _program.Bind();
  770. }
  771. }
  772. public void SetRasterizerDiscard(bool discard)
  773. {
  774. if (discard)
  775. {
  776. GL.Enable(EnableCap.RasterizerDiscard);
  777. }
  778. else
  779. {
  780. GL.Disable(EnableCap.RasterizerDiscard);
  781. }
  782. _rasterizerDiscard = discard;
  783. }
  784. public void SetRenderTargetScale(float scale)
  785. {
  786. _renderScale[0].X = scale;
  787. SetSupportBufferData<Vector4<float>>(SupportBuffer.FragmentRenderScaleOffset, _renderScale, 1); // Just the first element.
  788. }
  789. public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMasks)
  790. {
  791. for (int index = 0; index < componentMasks.Length; index++)
  792. {
  793. _componentMasks[index] = componentMasks[index];
  794. RestoreComponentMask(index);
  795. }
  796. }
  797. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  798. {
  799. EnsureFramebuffer();
  800. bool isBgraChanged = false;
  801. for (int index = 0; index < colors.Length; index++)
  802. {
  803. TextureView color = (TextureView)colors[index];
  804. _framebuffer.AttachColor(index, color);
  805. int isBgra = color != null && color.Format.IsBgr() ? 1 : 0;
  806. if (_fpIsBgra[index].X != isBgra)
  807. {
  808. _fpIsBgra[index].X = isBgra;
  809. isBgraChanged = true;
  810. RestoreComponentMask(index);
  811. }
  812. }
  813. if (isBgraChanged)
  814. {
  815. SetSupportBufferData<Vector4<int>>(SupportBuffer.FragmentIsBgraOffset, _fpIsBgra, SupportBuffer.FragmentIsBgraCount);
  816. }
  817. TextureView depthStencilView = (TextureView)depthStencil;
  818. _framebuffer.AttachDepthStencil(depthStencilView);
  819. _framebuffer.SetDrawBuffers(colors.Length);
  820. }
  821. public void SetSampler(int binding, ISampler sampler)
  822. {
  823. if (sampler == null)
  824. {
  825. return;
  826. }
  827. ((Sampler)sampler).Bind(binding);
  828. }
  829. public void SetScissor(int index, bool enable, int x, int y, int width, int height)
  830. {
  831. uint mask = 1u << index;
  832. if (!enable)
  833. {
  834. if ((_scissorEnables & mask) != 0)
  835. {
  836. _scissorEnables &= ~mask;
  837. GL.Disable(IndexedEnableCap.ScissorTest, index);
  838. }
  839. return;
  840. }
  841. if ((_scissorEnables & mask) == 0)
  842. {
  843. _scissorEnables |= mask;
  844. GL.Enable(IndexedEnableCap.ScissorTest, index);
  845. }
  846. GL.ScissorIndexed(index, x, y, width, height);
  847. }
  848. public void SetStencilTest(StencilTestDescriptor stencilTest)
  849. {
  850. if (!stencilTest.TestEnable)
  851. {
  852. GL.Disable(EnableCap.StencilTest);
  853. return;
  854. }
  855. GL.StencilOpSeparate(
  856. StencilFace.Front,
  857. stencilTest.FrontSFail.Convert(),
  858. stencilTest.FrontDpFail.Convert(),
  859. stencilTest.FrontDpPass.Convert());
  860. GL.StencilFuncSeparate(
  861. StencilFace.Front,
  862. (StencilFunction)stencilTest.FrontFunc.Convert(),
  863. stencilTest.FrontFuncRef,
  864. stencilTest.FrontFuncMask);
  865. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  866. GL.StencilOpSeparate(
  867. StencilFace.Back,
  868. stencilTest.BackSFail.Convert(),
  869. stencilTest.BackDpFail.Convert(),
  870. stencilTest.BackDpPass.Convert());
  871. GL.StencilFuncSeparate(
  872. StencilFace.Back,
  873. (StencilFunction)stencilTest.BackFunc.Convert(),
  874. stencilTest.BackFuncRef,
  875. stencilTest.BackFuncMask);
  876. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  877. GL.Enable(EnableCap.StencilTest);
  878. _stencilFrontMask = stencilTest.FrontMask;
  879. }
  880. public void SetStorageBuffers(int first, ReadOnlySpan<BufferRange> buffers)
  881. {
  882. SetBuffers(first, buffers, isStorage: true);
  883. }
  884. public void SetTexture(int binding, ITexture texture)
  885. {
  886. if (texture == null)
  887. {
  888. return;
  889. }
  890. if (binding == 0)
  891. {
  892. _unit0Texture = (TextureBase)texture;
  893. }
  894. else
  895. {
  896. ((TextureBase)texture).Bind(binding);
  897. }
  898. }
  899. public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
  900. {
  901. if (_tfEnabled)
  902. {
  903. GL.EndTransformFeedback();
  904. }
  905. int count = Math.Min(buffers.Length, Constants.MaxTransformFeedbackBuffers);
  906. for (int i = 0; i < count; i++)
  907. {
  908. BufferRange buffer = buffers[i];
  909. _tfbTargets[i] = buffer;
  910. if (buffer.Handle == BufferHandle.Null)
  911. {
  912. GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, 0);
  913. continue;
  914. }
  915. if (_tfbs[i] == BufferHandle.Null)
  916. {
  917. _tfbs[i] = Buffer.Create();
  918. }
  919. Buffer.Resize(_tfbs[i], buffer.Size);
  920. Buffer.Copy(buffer.Handle, _tfbs[i], buffer.Offset, 0, buffer.Size);
  921. GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, _tfbs[i].ToInt32());
  922. }
  923. if (_tfEnabled)
  924. {
  925. GL.BeginTransformFeedback(_tfTopology);
  926. }
  927. }
  928. public void SetUniformBuffers(int first, ReadOnlySpan<BufferRange> buffers)
  929. {
  930. SetBuffers(first, buffers, isStorage: false);
  931. }
  932. public void SetUserClipDistance(int index, bool enableClip)
  933. {
  934. if (!enableClip)
  935. {
  936. GL.Disable(EnableCap.ClipDistance0 + index);
  937. return;
  938. }
  939. GL.Enable(EnableCap.ClipDistance0 + index);
  940. }
  941. public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
  942. {
  943. EnsureVertexArray();
  944. _vertexArray.SetVertexAttributes(vertexAttribs);
  945. }
  946. public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
  947. {
  948. EnsureVertexArray();
  949. _vertexArray.SetVertexBuffers(vertexBuffers);
  950. }
  951. public void SetViewports(int first, ReadOnlySpan<Viewport> viewports)
  952. {
  953. float[] viewportArray = new float[viewports.Length * 4];
  954. double[] depthRangeArray = new double[viewports.Length * 2];
  955. for (int index = 0; index < viewports.Length; index++)
  956. {
  957. int viewportElemIndex = index * 4;
  958. Viewport viewport = viewports[index];
  959. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  960. viewportArray[viewportElemIndex + 1] = viewport.Region.Y + (viewport.Region.Height < 0 ? viewport.Region.Height : 0);
  961. viewportArray[viewportElemIndex + 2] = viewport.Region.Width;
  962. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  963. if (HwCapabilities.SupportsViewportSwizzle)
  964. {
  965. GL.NV.ViewportSwizzle(
  966. index,
  967. viewport.SwizzleX.Convert(),
  968. viewport.SwizzleY.Convert(),
  969. viewport.SwizzleZ.Convert(),
  970. viewport.SwizzleW.Convert());
  971. }
  972. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  973. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  974. }
  975. bool flipY = viewports.Length != 0 && viewports[0].Region.Height < 0;
  976. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  977. GL.ViewportArray(first, viewports.Length, viewportArray);
  978. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  979. }
  980. public void TextureBarrier()
  981. {
  982. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  983. }
  984. public void TextureBarrierTiled()
  985. {
  986. GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
  987. }
  988. private void SetBuffers(int first, ReadOnlySpan<BufferRange> buffers, bool isStorage)
  989. {
  990. BufferRangeTarget target = isStorage ? BufferRangeTarget.ShaderStorageBuffer : BufferRangeTarget.UniformBuffer;
  991. for (int index = 0; index < buffers.Length; index++)
  992. {
  993. BufferRange buffer = buffers[index];
  994. if (buffer.Handle == BufferHandle.Null)
  995. {
  996. GL.BindBufferRange(target, first + index, 0, IntPtr.Zero, 0);
  997. continue;
  998. }
  999. GL.BindBufferRange(target, first + index, buffer.Handle.ToInt32(), (IntPtr)buffer.Offset, buffer.Size);
  1000. }
  1001. }
  1002. private void SetOrigin(ClipOrigin origin)
  1003. {
  1004. if (_clipOrigin != origin)
  1005. {
  1006. _clipOrigin = origin;
  1007. GL.ClipControl(origin, _clipDepthMode);
  1008. SetFrontFace(_frontFace);
  1009. }
  1010. }
  1011. private void SetFrontFace(FrontFaceDirection frontFace)
  1012. {
  1013. // Changing clip origin will also change the front face to compensate
  1014. // for the flipped viewport, we flip it again here to compensate as
  1015. // this effect is undesirable for us.
  1016. if (_clipOrigin == ClipOrigin.UpperLeft)
  1017. {
  1018. frontFace = frontFace == FrontFaceDirection.Ccw ? FrontFaceDirection.Cw : FrontFaceDirection.Ccw;
  1019. }
  1020. GL.FrontFace(frontFace);
  1021. }
  1022. private void EnsureVertexArray()
  1023. {
  1024. if (_vertexArray == null)
  1025. {
  1026. _vertexArray = new VertexArray();
  1027. _vertexArray.Bind();
  1028. }
  1029. }
  1030. private void EnsureFramebuffer()
  1031. {
  1032. if (_framebuffer == null)
  1033. {
  1034. _framebuffer = new Framebuffer();
  1035. int boundHandle = _framebuffer.Bind();
  1036. _boundDrawFramebuffer = _boundReadFramebuffer = boundHandle;
  1037. GL.Enable(EnableCap.FramebufferSrgb);
  1038. }
  1039. }
  1040. internal (int drawHandle, int readHandle) GetBoundFramebuffers()
  1041. {
  1042. if (BackgroundContextWorker.InBackground)
  1043. {
  1044. return (0, 0);
  1045. }
  1046. return (_boundDrawFramebuffer, _boundReadFramebuffer);
  1047. }
  1048. public void UpdateRenderScale(ShaderStage stage, ReadOnlySpan<float> scales, int textureCount, int imageCount)
  1049. {
  1050. if (stage != ShaderStage.Compute && stage != ShaderStage.Fragment)
  1051. {
  1052. return;
  1053. }
  1054. bool changed = false;
  1055. for (int index = 0; index < textureCount + imageCount; index++)
  1056. {
  1057. if (_renderScale[1 + index].X != scales[index])
  1058. {
  1059. _renderScale[1 + index].X = scales[index];
  1060. changed = true;
  1061. }
  1062. }
  1063. if (changed)
  1064. {
  1065. SetSupportBufferData<Vector4<float>>(SupportBuffer.FragmentRenderScaleOffset, _renderScale, 1 + textureCount + imageCount);
  1066. }
  1067. }
  1068. private void SetSupportBufferData<T>(int offset, ReadOnlySpan<T> data, int count) where T : unmanaged
  1069. {
  1070. Buffer.SetData(_supportBuffer, offset, MemoryMarshal.Cast<T, byte>(data.Slice(0, count)));
  1071. }
  1072. private void PrepareForDispatch()
  1073. {
  1074. if (_unit0Texture != null)
  1075. {
  1076. _unit0Texture.Bind(0);
  1077. }
  1078. }
  1079. private void PreDraw()
  1080. {
  1081. DrawCount++;
  1082. _vertexArray.Validate();
  1083. if (_unit0Texture != null)
  1084. {
  1085. _unit0Texture.Bind(0);
  1086. }
  1087. }
  1088. private void PostDraw()
  1089. {
  1090. if (_tfEnabled)
  1091. {
  1092. for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
  1093. {
  1094. if (_tfbTargets[i].Handle != BufferHandle.Null)
  1095. {
  1096. Buffer.Copy(_tfbs[i], _tfbTargets[i].Handle, 0, _tfbTargets[i].Offset, _tfbTargets[i].Size);
  1097. }
  1098. }
  1099. }
  1100. }
  1101. public void RestoreComponentMask(int index)
  1102. {
  1103. // If the bound render target is bgra, swap the red and blue masks.
  1104. uint redMask = _fpIsBgra[index].X == 0 ? 1u : 4u;
  1105. uint blueMask = _fpIsBgra[index].X == 0 ? 4u : 1u;
  1106. GL.ColorMask(
  1107. index,
  1108. (_componentMasks[index] & redMask) != 0,
  1109. (_componentMasks[index] & 2u) != 0,
  1110. (_componentMasks[index] & blueMask) != 0,
  1111. (_componentMasks[index] & 8u) != 0);
  1112. }
  1113. public void RestoreScissor0Enable()
  1114. {
  1115. if ((_scissorEnables & 1u) != 0)
  1116. {
  1117. GL.Enable(IndexedEnableCap.ScissorTest, 0);
  1118. }
  1119. }
  1120. public void RestoreRasterizerDiscard()
  1121. {
  1122. if (_rasterizerDiscard)
  1123. {
  1124. GL.Enable(EnableCap.RasterizerDiscard);
  1125. }
  1126. }
  1127. public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
  1128. {
  1129. if (value is CounterQueueEvent)
  1130. {
  1131. // Compare an event and a constant value.
  1132. CounterQueueEvent evt = (CounterQueueEvent)value;
  1133. // Easy host conditional rendering when the check matches what GL can do:
  1134. // - Event is of type samples passed.
  1135. // - Result is not a combination of multiple queries.
  1136. // - Comparing against 0.
  1137. // - Event has not already been flushed.
  1138. if (compare == 0 && evt.Type == QueryTarget.SamplesPassed && evt.ClearCounter)
  1139. {
  1140. if (!value.ReserveForHostAccess())
  1141. {
  1142. // If the event has been flushed, then just use the values on the CPU.
  1143. // The query object may already be repurposed for another draw (eg. begin + end).
  1144. return false;
  1145. }
  1146. GL.BeginConditionalRender(evt.Query, isEqual ? ConditionalRenderType.QueryNoWaitInverted : ConditionalRenderType.QueryNoWait);
  1147. _activeConditionalRender = evt;
  1148. return true;
  1149. }
  1150. }
  1151. // The GPU will flush the queries to CPU and evaluate the condition there instead.
  1152. GL.Flush(); // The thread will be stalled manually flushing the counter, so flush GL commands now.
  1153. return false;
  1154. }
  1155. public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
  1156. {
  1157. GL.Flush(); // The GPU thread will be stalled manually flushing the counter, so flush GL commands now.
  1158. return false; // We don't currently have a way to compare two counters for conditional rendering.
  1159. }
  1160. public void EndHostConditionalRendering()
  1161. {
  1162. GL.EndConditionalRender();
  1163. _activeConditionalRender?.ReleaseHostAccess();
  1164. _activeConditionalRender = null;
  1165. }
  1166. public void Dispose()
  1167. {
  1168. if (_supportBuffer != BufferHandle.Null)
  1169. {
  1170. Buffer.Delete(_supportBuffer);
  1171. _supportBuffer = BufferHandle.Null;
  1172. }
  1173. for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
  1174. {
  1175. if (_tfbs[i] != BufferHandle.Null)
  1176. {
  1177. Buffer.Delete(_tfbs[i]);
  1178. _tfbs[i] = BufferHandle.Null;
  1179. }
  1180. }
  1181. _activeConditionalRender?.ReleaseHostAccess();
  1182. _framebuffer?.Dispose();
  1183. _vertexArray?.Dispose();
  1184. }
  1185. }
  1186. }