PipelineBase.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Shader;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using System.Numerics;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class PipelineBase : IDisposable
  9. {
  10. public const int DescriptorSetLayouts = 4;
  11. public const int UniformSetIndex = 0;
  12. public const int StorageSetIndex = 1;
  13. public const int TextureSetIndex = 2;
  14. public const int ImageSetIndex = 3;
  15. protected readonly VulkanRenderer Gd;
  16. protected readonly Device Device;
  17. public readonly PipelineCache PipelineCache;
  18. private PipelineDynamicState _dynamicState;
  19. private PipelineState _newState;
  20. private bool _stateDirty;
  21. private GAL.PrimitiveTopology _topology;
  22. private ulong _currentPipelineHandle;
  23. protected Auto<DisposablePipeline> Pipeline;
  24. protected PipelineBindPoint Pbp;
  25. protected CommandBufferScoped Cbs;
  26. protected CommandBufferScoped? PreloadCbs;
  27. protected CommandBuffer CommandBuffer;
  28. public CommandBufferScoped CurrentCommandBuffer => Cbs;
  29. private ShaderCollection _program;
  30. private Vector4<float>[] _renderScale = new Vector4<float>[73];
  31. private int _fragmentScaleCount;
  32. protected FramebufferParams FramebufferParams;
  33. private Auto<DisposableFramebuffer> _framebuffer;
  34. private Auto<DisposableRenderPass> _renderPass;
  35. private int _writtenAttachmentCount;
  36. private bool _renderPassActive;
  37. private readonly DescriptorSetUpdater _descriptorSetUpdater;
  38. private BufferState _indexBuffer;
  39. private readonly BufferState[] _transformFeedbackBuffers;
  40. private readonly VertexBufferState[] _vertexBuffers;
  41. private ulong _vertexBuffersDirty;
  42. protected Rectangle<int> ClearScissor;
  43. public SupportBufferUpdater SupportBufferUpdater;
  44. private bool _needsIndexBufferRebind;
  45. private bool _needsTransformFeedbackBuffersRebind;
  46. private bool _tfEnabled;
  47. private bool _tfActive;
  48. public ulong DrawCount { get; private set; }
  49. public unsafe PipelineBase(VulkanRenderer gd, Device device)
  50. {
  51. Gd = gd;
  52. Device = device;
  53. var pipelineCacheCreateInfo = new PipelineCacheCreateInfo()
  54. {
  55. SType = StructureType.PipelineCacheCreateInfo
  56. };
  57. gd.Api.CreatePipelineCache(device, pipelineCacheCreateInfo, null, out PipelineCache).ThrowOnError();
  58. _descriptorSetUpdater = new DescriptorSetUpdater(gd, this);
  59. _transformFeedbackBuffers = new BufferState[Constants.MaxTransformFeedbackBuffers];
  60. _vertexBuffers = new VertexBufferState[Constants.MaxVertexBuffers + 1];
  61. const int EmptyVbSize = 16;
  62. using var emptyVb = gd.BufferManager.Create(gd, EmptyVbSize);
  63. emptyVb.SetData(0, new byte[EmptyVbSize]);
  64. _vertexBuffers[0] = new VertexBufferState(emptyVb.GetBuffer(), 0, EmptyVbSize, 0);
  65. _vertexBuffersDirty = ulong.MaxValue >> (64 - _vertexBuffers.Length);
  66. ClearScissor = new Rectangle<int>(0, 0, 0xffff, 0xffff);
  67. var defaultScale = new Vector4<float> { X = 1f, Y = 0f, Z = 0f, W = 0f };
  68. new Span<Vector4<float>>(_renderScale).Fill(defaultScale);
  69. _newState.Initialize();
  70. _newState.LineWidth = 1f;
  71. _newState.SamplesCount = 1;
  72. }
  73. public void Initialize()
  74. {
  75. SupportBufferUpdater = new SupportBufferUpdater(Gd);
  76. SupportBufferUpdater.UpdateRenderScale(_renderScale, 0, SupportBuffer.RenderScaleMaxCount);
  77. }
  78. public unsafe void Barrier()
  79. {
  80. MemoryBarrier memoryBarrier = new MemoryBarrier()
  81. {
  82. SType = StructureType.MemoryBarrier,
  83. SrcAccessMask = AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit,
  84. DstAccessMask = AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit
  85. };
  86. Gd.Api.CmdPipelineBarrier(
  87. CommandBuffer,
  88. PipelineStageFlags.PipelineStageFragmentShaderBit,
  89. PipelineStageFlags.PipelineStageFragmentShaderBit,
  90. 0,
  91. 1,
  92. memoryBarrier,
  93. 0,
  94. null,
  95. 0,
  96. null);
  97. }
  98. public void BeginTransformFeedback(GAL.PrimitiveTopology topology)
  99. {
  100. _tfEnabled = true;
  101. }
  102. public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
  103. {
  104. EndRenderPass();
  105. var dst = Gd.BufferManager.GetBuffer(CommandBuffer, destination, true).Get(Cbs, offset, size).Value;
  106. BufferHolder.InsertBufferBarrier(
  107. Gd,
  108. Cbs.CommandBuffer,
  109. dst,
  110. BufferHolder.DefaultAccessFlags,
  111. AccessFlags.AccessTransferWriteBit,
  112. PipelineStageFlags.PipelineStageAllCommandsBit,
  113. PipelineStageFlags.PipelineStageTransferBit,
  114. offset,
  115. size);
  116. Gd.Api.CmdFillBuffer(CommandBuffer, dst, (ulong)offset, (ulong)size, value);
  117. BufferHolder.InsertBufferBarrier(
  118. Gd,
  119. Cbs.CommandBuffer,
  120. dst,
  121. AccessFlags.AccessTransferWriteBit,
  122. BufferHolder.DefaultAccessFlags,
  123. PipelineStageFlags.PipelineStageTransferBit,
  124. PipelineStageFlags.PipelineStageAllCommandsBit,
  125. offset,
  126. size);
  127. }
  128. public unsafe void ClearRenderTargetColor(int index, int layer, int layerCount, ColorF color)
  129. {
  130. if (FramebufferParams == null || !FramebufferParams.IsValidColorAttachment(index))
  131. {
  132. return;
  133. }
  134. if (_renderPass == null)
  135. {
  136. CreateRenderPass();
  137. }
  138. BeginRenderPass();
  139. var clearValue = new ClearValue(new ClearColorValue(color.Red, color.Green, color.Blue, color.Alpha));
  140. var attachment = new ClearAttachment(ImageAspectFlags.ImageAspectColorBit, (uint)index, clearValue);
  141. var clearRect = FramebufferParams.GetClearRect(ClearScissor, layer, layerCount);
  142. Gd.Api.CmdClearAttachments(CommandBuffer, 1, &attachment, 1, &clearRect);
  143. }
  144. public unsafe void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask)
  145. {
  146. // TODO: Use stencilMask (fully)
  147. if (FramebufferParams == null || !FramebufferParams.HasDepthStencil)
  148. {
  149. return;
  150. }
  151. if (_renderPass == null)
  152. {
  153. CreateRenderPass();
  154. }
  155. BeginRenderPass();
  156. var clearValue = new ClearValue(null, new ClearDepthStencilValue(depthValue, (uint)stencilValue));
  157. var flags = depthMask ? ImageAspectFlags.ImageAspectDepthBit : 0;
  158. if (stencilMask != 0)
  159. {
  160. flags |= ImageAspectFlags.ImageAspectStencilBit;
  161. }
  162. var attachment = new ClearAttachment(flags, 0, clearValue);
  163. var clearRect = FramebufferParams.GetClearRect(ClearScissor, layer, layerCount);
  164. Gd.Api.CmdClearAttachments(CommandBuffer, 1, &attachment, 1, &clearRect);
  165. }
  166. public void CommandBufferBarrier()
  167. {
  168. // TODO: More specific barrier?
  169. Barrier();
  170. }
  171. public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
  172. {
  173. EndRenderPass();
  174. var src = Gd.BufferManager.GetBuffer(CommandBuffer, source, false);
  175. var dst = Gd.BufferManager.GetBuffer(CommandBuffer, destination, true);
  176. BufferHolder.Copy(Gd, Cbs, src, dst, srcOffset, dstOffset, size);
  177. }
  178. public void DirtyVertexBuffer(Auto<DisposableBuffer> buffer)
  179. {
  180. for (int i = 0; i < _vertexBuffers.Length; i++)
  181. {
  182. if (_vertexBuffers[i].BoundEquals(buffer))
  183. {
  184. _vertexBuffersDirty |= 1UL << i;
  185. }
  186. }
  187. }
  188. public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
  189. {
  190. if (!_program.IsLinked)
  191. {
  192. return;
  193. }
  194. EndRenderPass();
  195. RecreatePipelineIfNeeded(PipelineBindPoint.Compute);
  196. Gd.Api.CmdDispatch(CommandBuffer, (uint)groupsX, (uint)groupsY, (uint)groupsZ);
  197. }
  198. public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
  199. {
  200. if (!_program.IsLinked)
  201. {
  202. return;
  203. }
  204. RecreatePipelineIfNeeded(PipelineBindPoint.Graphics);
  205. BeginRenderPass();
  206. ResumeTransformFeedbackInternal();
  207. DrawCount++;
  208. if (_topology == GAL.PrimitiveTopology.Quads)
  209. {
  210. int quadsCount = vertexCount / 4;
  211. for (int i = 0; i < quadsCount; i++)
  212. {
  213. Gd.Api.CmdDraw(CommandBuffer, 4, (uint)instanceCount, (uint)(firstVertex + i * 4), (uint)firstInstance);
  214. }
  215. }
  216. else
  217. {
  218. Gd.Api.CmdDraw(CommandBuffer, (uint)vertexCount, (uint)instanceCount, (uint)firstVertex, (uint)firstInstance);
  219. }
  220. }
  221. public void DrawIndexed(int indexCount, int instanceCount, int firstIndex, int firstVertex, int firstInstance)
  222. {
  223. if (!_program.IsLinked)
  224. {
  225. return;
  226. }
  227. RecreatePipelineIfNeeded(PipelineBindPoint.Graphics);
  228. BeginRenderPass();
  229. ResumeTransformFeedbackInternal();
  230. DrawCount++;
  231. if (_topology == GAL.PrimitiveTopology.Quads)
  232. {
  233. int quadsCount = indexCount / 4;
  234. for (int i = 0; i < quadsCount; i++)
  235. {
  236. Gd.Api.CmdDrawIndexed(CommandBuffer, 4, (uint)instanceCount, (uint)(firstIndex + i * 4), firstVertex, (uint)firstInstance);
  237. }
  238. }
  239. else
  240. {
  241. Gd.Api.CmdDrawIndexed(CommandBuffer, (uint)indexCount, (uint)instanceCount, (uint)firstIndex, firstVertex, (uint)firstInstance);
  242. }
  243. }
  244. public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
  245. {
  246. if (texture is TextureView srcTexture)
  247. {
  248. SupportBufferUpdater.Commit();
  249. var oldCullMode = _newState.CullMode;
  250. var oldStencilTestEnable = _newState.StencilTestEnable;
  251. var oldDepthTestEnable = _newState.DepthTestEnable;
  252. var oldDepthWriteEnable = _newState.DepthWriteEnable;
  253. var oldTopology = _newState.Topology;
  254. var oldViewports = _dynamicState.Viewports;
  255. var oldViewportsCount = _newState.ViewportsCount;
  256. _newState.CullMode = CullModeFlags.CullModeNone;
  257. _newState.StencilTestEnable = false;
  258. _newState.DepthTestEnable = false;
  259. _newState.DepthWriteEnable = false;
  260. SignalStateChange();
  261. Gd.HelperShader.DrawTexture(
  262. Gd,
  263. this,
  264. srcTexture,
  265. sampler,
  266. srcRegion,
  267. dstRegion);
  268. _newState.CullMode = oldCullMode;
  269. _newState.StencilTestEnable = oldStencilTestEnable;
  270. _newState.DepthTestEnable = oldDepthTestEnable;
  271. _newState.DepthWriteEnable = oldDepthWriteEnable;
  272. _newState.Topology = oldTopology;
  273. _dynamicState.Viewports = oldViewports;
  274. _dynamicState.ViewportsCount = (int)oldViewportsCount;
  275. _dynamicState.SetViewportsDirty();
  276. _newState.ViewportsCount = oldViewportsCount;
  277. SignalStateChange();
  278. }
  279. }
  280. public void EndTransformFeedback()
  281. {
  282. PauseTransformFeedbackInternal();
  283. _tfEnabled = false;
  284. }
  285. public bool IsCommandBufferActive(CommandBuffer cb)
  286. {
  287. return CommandBuffer.Handle == cb.Handle;
  288. }
  289. public void MultiDrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
  290. {
  291. if (!Gd.Capabilities.SupportsIndirectParameters)
  292. {
  293. throw new NotSupportedException();
  294. }
  295. if (_program.LinkStatus != ProgramLinkStatus.Success)
  296. {
  297. return;
  298. }
  299. RecreatePipelineIfNeeded(PipelineBindPoint.Graphics);
  300. BeginRenderPass();
  301. ResumeTransformFeedbackInternal();
  302. DrawCount++;
  303. var buffer = Gd.BufferManager.GetBuffer(CommandBuffer, indirectBuffer.Handle, true).Get(Cbs, indirectBuffer.Offset, indirectBuffer.Size).Value;
  304. var countBuffer = Gd.BufferManager.GetBuffer(CommandBuffer, parameterBuffer.Handle, true).Get(Cbs, parameterBuffer.Offset, parameterBuffer.Size).Value;
  305. Gd.DrawIndirectCountApi.CmdDrawIndirectCount(
  306. CommandBuffer,
  307. buffer,
  308. (ulong)indirectBuffer.Offset,
  309. countBuffer,
  310. (ulong)parameterBuffer.Offset,
  311. (uint)maxDrawCount,
  312. (uint)stride);
  313. }
  314. public void MultiDrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
  315. {
  316. if (!Gd.Capabilities.SupportsIndirectParameters)
  317. {
  318. throw new NotSupportedException();
  319. }
  320. if (_program.LinkStatus != ProgramLinkStatus.Success)
  321. {
  322. return;
  323. }
  324. RecreatePipelineIfNeeded(PipelineBindPoint.Graphics);
  325. BeginRenderPass();
  326. ResumeTransformFeedbackInternal();
  327. DrawCount++;
  328. var buffer = Gd.BufferManager.GetBuffer(CommandBuffer, indirectBuffer.Handle, true).Get(Cbs, indirectBuffer.Offset, indirectBuffer.Size).Value;
  329. var countBuffer = Gd.BufferManager.GetBuffer(CommandBuffer, parameterBuffer.Handle, true).Get(Cbs, parameterBuffer.Offset, parameterBuffer.Size).Value;
  330. Gd.DrawIndirectCountApi.CmdDrawIndexedIndirectCount(
  331. CommandBuffer,
  332. buffer,
  333. (ulong)indirectBuffer.Offset,
  334. countBuffer,
  335. (ulong)parameterBuffer.Offset,
  336. (uint)maxDrawCount,
  337. (uint)stride);
  338. }
  339. public void SetAlphaTest(bool enable, float reference, GAL.CompareOp op)
  340. {
  341. // This is currently handled using shader specialization, as Vulkan does not support alpha test.
  342. // In the future, we may want to use this to write the reference value into the support buffer,
  343. // to avoid creating one version of the shader per reference value used.
  344. }
  345. public void SetBlendState(int index, BlendDescriptor blend)
  346. {
  347. ref var vkBlend = ref _newState.Internal.ColorBlendAttachmentState[index];
  348. vkBlend.BlendEnable = blend.Enable;
  349. vkBlend.SrcColorBlendFactor = blend.ColorSrcFactor.Convert();
  350. vkBlend.DstColorBlendFactor = blend.ColorDstFactor.Convert();
  351. vkBlend.ColorBlendOp = blend.ColorOp.Convert();
  352. vkBlend.SrcAlphaBlendFactor = blend.AlphaSrcFactor.Convert();
  353. vkBlend.DstAlphaBlendFactor = blend.AlphaDstFactor.Convert();
  354. vkBlend.AlphaBlendOp = blend.AlphaOp.Convert();
  355. _newState.BlendConstantR = blend.BlendConstant.Red;
  356. _newState.BlendConstantG = blend.BlendConstant.Green;
  357. _newState.BlendConstantB = blend.BlendConstant.Blue;
  358. _newState.BlendConstantA = blend.BlendConstant.Alpha;
  359. SignalStateChange();
  360. }
  361. public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
  362. {
  363. _dynamicState.SetDepthBias(factor, units, clamp);
  364. _newState.DepthBiasEnable = enables != 0;
  365. SignalStateChange();
  366. }
  367. public void SetDepthClamp(bool clamp)
  368. {
  369. _newState.DepthClampEnable = clamp;
  370. SignalStateChange();
  371. }
  372. public void SetDepthMode(DepthMode mode)
  373. {
  374. // Currently this is emulated on the shader, because Vulkan had no support for changing the depth mode.
  375. // In the future, we may want to use the VK_EXT_depth_clip_control extension to change it here.
  376. }
  377. public void SetDepthTest(DepthTestDescriptor depthTest)
  378. {
  379. _newState.DepthTestEnable = depthTest.TestEnable;
  380. _newState.DepthWriteEnable = depthTest.WriteEnable;
  381. _newState.DepthCompareOp = depthTest.Func.Convert();
  382. SignalStateChange();
  383. }
  384. public void SetFaceCulling(bool enable, Face face)
  385. {
  386. _newState.CullMode = enable ? face.Convert() : CullModeFlags.CullModeNone;
  387. SignalStateChange();
  388. }
  389. public void SetFrontFace(GAL.FrontFace frontFace)
  390. {
  391. _newState.FrontFace = frontFace.Convert();
  392. SignalStateChange();
  393. }
  394. public void SetImage(int binding, ITexture image, GAL.Format imageFormat)
  395. {
  396. _descriptorSetUpdater.SetImage(binding, image, imageFormat);
  397. }
  398. public void SetIndexBuffer(BufferRange buffer, GAL.IndexType type)
  399. {
  400. _indexBuffer.Dispose();
  401. if (buffer.Handle != BufferHandle.Null)
  402. {
  403. Auto<DisposableBuffer> ib = null;
  404. int offset = buffer.Offset;
  405. int size = buffer.Size;
  406. if (type == GAL.IndexType.UByte && !Gd.Capabilities.SupportsIndexTypeUint8)
  407. {
  408. ib = Gd.BufferManager.GetBufferI8ToI16(Cbs, buffer.Handle, offset, size);
  409. offset = 0;
  410. size *= 2;
  411. type = GAL.IndexType.UShort;
  412. }
  413. else
  414. {
  415. ib = Gd.BufferManager.GetBuffer(CommandBuffer, buffer.Handle, false);
  416. }
  417. _indexBuffer = new BufferState(ib, offset, size, type.Convert());
  418. }
  419. else
  420. {
  421. _indexBuffer = BufferState.Null;
  422. }
  423. _indexBuffer.BindIndexBuffer(Gd.Api, Cbs);
  424. }
  425. public void SetLineParameters(float width, bool smooth)
  426. {
  427. _newState.LineWidth = width;
  428. SignalStateChange();
  429. }
  430. public void SetLogicOpState(bool enable, LogicalOp op)
  431. {
  432. _newState.LogicOpEnable = enable;
  433. _newState.LogicOp = op.Convert();
  434. SignalStateChange();
  435. }
  436. public void SetMultisampleState(MultisampleDescriptor multisample)
  437. {
  438. _newState.AlphaToCoverageEnable = multisample.AlphaToCoverageEnable;
  439. _newState.AlphaToOneEnable = multisample.AlphaToOneEnable;
  440. SignalStateChange();
  441. }
  442. public void SetOrigin(Origin origin)
  443. {
  444. // TODO.
  445. }
  446. public unsafe void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
  447. {
  448. _newState.PatchControlPoints = (uint)vertices;
  449. SignalStateChange();
  450. // TODO: Default levels (likely needs emulation on shaders?)
  451. }
  452. public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
  453. {
  454. // TODO.
  455. }
  456. public void SetPolygonMode(GAL.PolygonMode frontMode, GAL.PolygonMode backMode)
  457. {
  458. // TODO.
  459. }
  460. public void SetPrimitiveRestart(bool enable, int index)
  461. {
  462. _newState.PrimitiveRestartEnable = enable;
  463. // TODO: What to do about the index?
  464. SignalStateChange();
  465. }
  466. public void SetPrimitiveTopology(GAL.PrimitiveTopology topology)
  467. {
  468. _topology = topology;
  469. var vkTopology = topology.Convert();
  470. _newState.Topology = vkTopology;
  471. SignalStateChange();
  472. }
  473. public void SetProgram(IProgram program)
  474. {
  475. var internalProgram = (ShaderCollection)program;
  476. var stages = internalProgram.GetInfos();
  477. _program = internalProgram;
  478. _descriptorSetUpdater.SetProgram(internalProgram);
  479. _newState.PipelineLayout = internalProgram.PipelineLayout;
  480. _newState.StagesCount = (uint)stages.Length;
  481. stages.CopyTo(_newState.Stages.AsSpan().Slice(0, stages.Length));
  482. SignalStateChange();
  483. }
  484. protected virtual void SignalAttachmentChange()
  485. {
  486. }
  487. public void SetRasterizerDiscard(bool discard)
  488. {
  489. _newState.RasterizerDiscardEnable = discard;
  490. SignalStateChange();
  491. }
  492. public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask)
  493. {
  494. int count = Math.Min(Constants.MaxRenderTargets, componentMask.Length);
  495. int writtenAttachments = 0;
  496. for (int i = 0; i < count; i++)
  497. {
  498. ref var vkBlend = ref _newState.Internal.ColorBlendAttachmentState[i];
  499. vkBlend.ColorWriteMask = (ColorComponentFlags)componentMask[i];
  500. if (componentMask[i] != 0)
  501. {
  502. writtenAttachments++;
  503. }
  504. }
  505. SignalStateChange();
  506. if (writtenAttachments != _writtenAttachmentCount)
  507. {
  508. SignalAttachmentChange();
  509. _writtenAttachmentCount = writtenAttachments;
  510. }
  511. }
  512. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  513. {
  514. FramebufferParams?.UpdateModifications();
  515. CreateFramebuffer(colors, depthStencil);
  516. CreateRenderPass();
  517. SignalStateChange();
  518. SignalAttachmentChange();
  519. }
  520. public void SetRenderTargetScale(float scale)
  521. {
  522. _renderScale[0].X = scale;
  523. SupportBufferUpdater.UpdateRenderScale(_renderScale, 0, 1); // Just the first element.
  524. }
  525. public void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
  526. {
  527. int maxScissors = Gd.Capabilities.SupportsMultiView ? Constants.MaxViewports : 1;
  528. int count = Math.Min(maxScissors, regions.Length);
  529. if (count > 0)
  530. {
  531. ClearScissor = regions[0];
  532. }
  533. for (int i = 0; i < count; i++)
  534. {
  535. var region = regions[i];
  536. var offset = new Offset2D(region.X, region.Y);
  537. var extent = new Extent2D((uint)region.Width, (uint)region.Height);
  538. _dynamicState.SetScissor(i, new Rect2D(offset, extent));
  539. }
  540. _dynamicState.ScissorsCount = count;
  541. _newState.ScissorsCount = (uint)count;
  542. SignalStateChange();
  543. }
  544. public void SetStencilTest(StencilTestDescriptor stencilTest)
  545. {
  546. _dynamicState.SetStencilMasks(
  547. (uint)stencilTest.BackFuncMask,
  548. (uint)stencilTest.BackMask,
  549. (uint)stencilTest.BackFuncRef,
  550. (uint)stencilTest.FrontFuncMask,
  551. (uint)stencilTest.FrontMask,
  552. (uint)stencilTest.FrontFuncRef);
  553. _newState.StencilTestEnable = stencilTest.TestEnable;
  554. _newState.StencilBackFailOp = stencilTest.BackSFail.Convert();
  555. _newState.StencilBackPassOp = stencilTest.BackDpPass.Convert();
  556. _newState.StencilBackDepthFailOp = stencilTest.BackDpFail.Convert();
  557. _newState.StencilBackCompareOp = stencilTest.BackFunc.Convert();
  558. _newState.StencilFrontFailOp = stencilTest.FrontSFail.Convert();
  559. _newState.StencilFrontPassOp = stencilTest.FrontDpPass.Convert();
  560. _newState.StencilFrontDepthFailOp = stencilTest.FrontDpFail.Convert();
  561. _newState.StencilFrontCompareOp = stencilTest.FrontFunc.Convert();
  562. SignalStateChange();
  563. }
  564. public void SetStorageBuffers(int first, ReadOnlySpan<BufferRange> buffers)
  565. {
  566. _descriptorSetUpdater.SetStorageBuffers(CommandBuffer, first, buffers);
  567. }
  568. public void SetStorageBuffers(int first, ReadOnlySpan<Auto<DisposableBuffer>> buffers)
  569. {
  570. _descriptorSetUpdater.SetStorageBuffers(CommandBuffer, first, buffers);
  571. }
  572. public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
  573. {
  574. _descriptorSetUpdater.SetTextureAndSampler(Cbs, stage, binding, texture, sampler);
  575. }
  576. public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
  577. {
  578. PauseTransformFeedbackInternal();
  579. int count = Math.Min(Constants.MaxTransformFeedbackBuffers, buffers.Length);
  580. for (int i = 0; i < count; i++)
  581. {
  582. var range = buffers[i];
  583. _transformFeedbackBuffers[i].Dispose();
  584. if (range.Handle != BufferHandle.Null)
  585. {
  586. _transformFeedbackBuffers[i] = new BufferState(Gd.BufferManager.GetBuffer(CommandBuffer, range.Handle, true), range.Offset, range.Size);
  587. _transformFeedbackBuffers[i].BindTransformFeedbackBuffer(Gd, Cbs, (uint)i);
  588. }
  589. else
  590. {
  591. _transformFeedbackBuffers[i] = BufferState.Null;
  592. }
  593. }
  594. }
  595. public void SetUniformBuffers(int first, ReadOnlySpan<BufferRange> buffers)
  596. {
  597. _descriptorSetUpdater.SetUniformBuffers(CommandBuffer, first, buffers);
  598. }
  599. public void SetUserClipDistance(int index, bool enableClip)
  600. {
  601. // TODO.
  602. }
  603. public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
  604. {
  605. var formatCapabilities = Gd.FormatCapabilities;
  606. Span<int> newVbScalarSizes = stackalloc int[Constants.MaxVertexBuffers];
  607. int count = Math.Min(Constants.MaxVertexAttributes, vertexAttribs.Length);
  608. uint dirtyVbSizes = 0;
  609. for (int i = 0; i < count; i++)
  610. {
  611. var attribute = vertexAttribs[i];
  612. var rawIndex = attribute.BufferIndex;
  613. var bufferIndex = attribute.IsZero ? 0 : rawIndex + 1;
  614. if (!attribute.IsZero)
  615. {
  616. newVbScalarSizes[rawIndex] = Math.Max(newVbScalarSizes[rawIndex], attribute.Format.GetScalarSize());
  617. dirtyVbSizes |= 1u << rawIndex;
  618. }
  619. _newState.Internal.VertexAttributeDescriptions[i] = new VertexInputAttributeDescription(
  620. (uint)i,
  621. (uint)bufferIndex,
  622. formatCapabilities.ConvertToVertexVkFormat(attribute.Format),
  623. (uint)attribute.Offset);
  624. }
  625. while (dirtyVbSizes != 0)
  626. {
  627. int dirtyBit = BitOperations.TrailingZeroCount(dirtyVbSizes);
  628. ref var buffer = ref _vertexBuffers[dirtyBit + 1];
  629. if (buffer.AttributeScalarAlignment != newVbScalarSizes[dirtyBit])
  630. {
  631. _vertexBuffersDirty |= 1UL << (dirtyBit + 1);
  632. buffer.AttributeScalarAlignment = newVbScalarSizes[dirtyBit];
  633. }
  634. dirtyVbSizes &= ~(1u << dirtyBit);
  635. }
  636. _newState.VertexAttributeDescriptionsCount = (uint)count;
  637. SignalStateChange();
  638. }
  639. public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
  640. {
  641. int count = Math.Min(Constants.MaxVertexBuffers, vertexBuffers.Length);
  642. _newState.Internal.VertexBindingDescriptions[0] = new VertexInputBindingDescription(0, 0, VertexInputRate.Vertex);
  643. int validCount = 1;
  644. for (int i = 0; i < count; i++)
  645. {
  646. var vertexBuffer = vertexBuffers[i];
  647. // TODO: Support divisor > 1
  648. var inputRate = vertexBuffer.Divisor != 0 ? VertexInputRate.Instance : VertexInputRate.Vertex;
  649. if (vertexBuffer.Buffer.Handle != BufferHandle.Null)
  650. {
  651. var vb = Gd.BufferManager.GetBuffer(CommandBuffer, vertexBuffer.Buffer.Handle, false);
  652. if (vb != null)
  653. {
  654. int binding = i + 1;
  655. int descriptorIndex = validCount++;
  656. _newState.Internal.VertexBindingDescriptions[descriptorIndex] = new VertexInputBindingDescription(
  657. (uint)binding,
  658. (uint)vertexBuffer.Stride,
  659. inputRate);
  660. int vbSize = vertexBuffer.Buffer.Size;
  661. if (Gd.Vendor == Vendor.Amd && vertexBuffer.Stride > 0)
  662. {
  663. // AMD has a bug where if offset + stride * count is greater than
  664. // the size, then the last attribute will have the wrong value.
  665. // As a workaround, simply use the full buffer size.
  666. int remainder = vbSize % vertexBuffer.Stride;
  667. if (remainder != 0)
  668. {
  669. vbSize += vertexBuffer.Stride - remainder;
  670. }
  671. }
  672. ref var buffer = ref _vertexBuffers[binding];
  673. int oldScalarAlign = buffer.AttributeScalarAlignment;
  674. buffer.Dispose();
  675. if ((vertexBuffer.Stride % FormatExtensions.MaxBufferFormatScalarSize) == 0)
  676. {
  677. buffer = new VertexBufferState(
  678. vb,
  679. descriptorIndex,
  680. vertexBuffer.Buffer.Offset,
  681. vbSize,
  682. vertexBuffer.Stride);
  683. buffer.BindVertexBuffer(Gd, Cbs, (uint)binding, ref _newState);
  684. }
  685. else
  686. {
  687. // May need to be rewritten. Bind this buffer before draw.
  688. buffer = new VertexBufferState(
  689. vertexBuffer.Buffer.Handle,
  690. descriptorIndex,
  691. vertexBuffer.Buffer.Offset,
  692. vbSize,
  693. vertexBuffer.Stride);
  694. _vertexBuffersDirty |= 1UL << binding;
  695. }
  696. buffer.AttributeScalarAlignment = oldScalarAlign;
  697. }
  698. }
  699. }
  700. _newState.VertexBindingDescriptionsCount = (uint)validCount;
  701. SignalStateChange();
  702. }
  703. public void SetViewports(ReadOnlySpan<GAL.Viewport> viewports, bool disableTransform)
  704. {
  705. int maxViewports = Gd.Capabilities.SupportsMultiView ? Constants.MaxViewports : 1;
  706. int count = Math.Min(maxViewports, viewports.Length);
  707. static float Clamp(float value)
  708. {
  709. return Math.Clamp(value, 0f, 1f);
  710. }
  711. for (int i = 0; i < count; i++)
  712. {
  713. var viewport = viewports[i];
  714. _dynamicState.SetViewport(i, new Silk.NET.Vulkan.Viewport(
  715. viewport.Region.X,
  716. viewport.Region.Y,
  717. viewport.Region.Width == 0f ? 1f : viewport.Region.Width,
  718. viewport.Region.Height == 0f ? 1f : viewport.Region.Height,
  719. Clamp(viewport.DepthNear),
  720. Clamp(viewport.DepthFar)));
  721. }
  722. _dynamicState.ViewportsCount = count;
  723. float disableTransformF = disableTransform ? 1.0f : 0.0f;
  724. if (SupportBufferUpdater.Data.ViewportInverse.W != disableTransformF || disableTransform)
  725. {
  726. float scale = _renderScale[0].X;
  727. SupportBufferUpdater.UpdateViewportInverse(new Vector4<float>
  728. {
  729. X = scale * 2f / viewports[0].Region.Width,
  730. Y = scale * 2f / viewports[0].Region.Height,
  731. Z = 1,
  732. W = disableTransformF
  733. });
  734. }
  735. _newState.ViewportsCount = (uint)count;
  736. SignalStateChange();
  737. }
  738. public unsafe void TextureBarrier()
  739. {
  740. MemoryBarrier memoryBarrier = new MemoryBarrier()
  741. {
  742. SType = StructureType.MemoryBarrier,
  743. SrcAccessMask = AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit,
  744. DstAccessMask = AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit
  745. };
  746. Gd.Api.CmdPipelineBarrier(
  747. CommandBuffer,
  748. PipelineStageFlags.PipelineStageFragmentShaderBit,
  749. PipelineStageFlags.PipelineStageFragmentShaderBit,
  750. 0,
  751. 1,
  752. memoryBarrier,
  753. 0,
  754. null,
  755. 0,
  756. null);
  757. }
  758. public void TextureBarrierTiled()
  759. {
  760. TextureBarrier();
  761. }
  762. public void UpdateRenderScale(ReadOnlySpan<float> scales, int totalCount, int fragmentCount)
  763. {
  764. bool changed = false;
  765. for (int index = 0; index < totalCount; index++)
  766. {
  767. if (_renderScale[1 + index].X != scales[index])
  768. {
  769. _renderScale[1 + index].X = scales[index];
  770. changed = true;
  771. }
  772. }
  773. // Only update fragment count if there are scales after it for the vertex stage.
  774. if (fragmentCount != totalCount && fragmentCount != _fragmentScaleCount)
  775. {
  776. _fragmentScaleCount = fragmentCount;
  777. SupportBufferUpdater.UpdateFragmentRenderScaleCount(_fragmentScaleCount);
  778. }
  779. if (changed)
  780. {
  781. SupportBufferUpdater.UpdateRenderScale(_renderScale, 0, 1 + totalCount);
  782. }
  783. }
  784. protected void SignalCommandBufferChange()
  785. {
  786. _needsIndexBufferRebind = true;
  787. _needsTransformFeedbackBuffersRebind = true;
  788. _vertexBuffersDirty = ulong.MaxValue >> (64 - _vertexBuffers.Length);
  789. _descriptorSetUpdater.SignalCommandBufferChange();
  790. _dynamicState.ForceAllDirty();
  791. _currentPipelineHandle = 0;
  792. }
  793. private void CreateFramebuffer(ITexture[] colors, ITexture depthStencil)
  794. {
  795. FramebufferParams = new FramebufferParams(Device, colors, depthStencil);
  796. UpdatePipelineAttachmentFormats();
  797. _newState.SamplesCount = FramebufferParams.AttachmentSamples.Length != 0 ? FramebufferParams.AttachmentSamples[0] : 1;
  798. }
  799. protected void UpdatePipelineAttachmentFormats()
  800. {
  801. var dstAttachmentFormats = _newState.Internal.AttachmentFormats.AsSpan();
  802. FramebufferParams.AttachmentFormats.CopyTo(dstAttachmentFormats);
  803. int maxAttachmentIndex = FramebufferParams.MaxColorAttachmentIndex + (FramebufferParams.HasDepthStencil ? 1 : 0);
  804. for (int i = FramebufferParams.AttachmentFormats.Length; i <= maxAttachmentIndex; i++)
  805. {
  806. dstAttachmentFormats[i] = 0;
  807. }
  808. _newState.ColorBlendAttachmentStateCount = (uint)(FramebufferParams.MaxColorAttachmentIndex + 1);
  809. _newState.HasDepthStencil = FramebufferParams.HasDepthStencil;
  810. }
  811. protected unsafe void CreateRenderPass()
  812. {
  813. const int MaxAttachments = Constants.MaxRenderTargets + 1;
  814. AttachmentDescription[] attachmentDescs = null;
  815. var subpass = new SubpassDescription()
  816. {
  817. PipelineBindPoint = PipelineBindPoint.Graphics
  818. };
  819. AttachmentReference* attachmentReferences = stackalloc AttachmentReference[MaxAttachments];
  820. var hasFramebuffer = FramebufferParams != null;
  821. if (hasFramebuffer && FramebufferParams.AttachmentsCount != 0)
  822. {
  823. attachmentDescs = new AttachmentDescription[FramebufferParams.AttachmentsCount];
  824. for (int i = 0; i < FramebufferParams.AttachmentsCount; i++)
  825. {
  826. int bindIndex = FramebufferParams.AttachmentIndices[i];
  827. attachmentDescs[i] = new AttachmentDescription(
  828. 0,
  829. FramebufferParams.AttachmentFormats[i],
  830. TextureStorage.ConvertToSampleCountFlags(FramebufferParams.AttachmentSamples[i]),
  831. AttachmentLoadOp.Load,
  832. AttachmentStoreOp.Store,
  833. AttachmentLoadOp.Load,
  834. AttachmentStoreOp.Store,
  835. ImageLayout.General,
  836. ImageLayout.General);
  837. }
  838. int colorAttachmentsCount = FramebufferParams.ColorAttachmentsCount;
  839. if (colorAttachmentsCount > MaxAttachments - 1)
  840. {
  841. colorAttachmentsCount = MaxAttachments - 1;
  842. }
  843. if (colorAttachmentsCount != 0)
  844. {
  845. int maxAttachmentIndex = FramebufferParams.MaxColorAttachmentIndex;
  846. subpass.ColorAttachmentCount = (uint)maxAttachmentIndex + 1;
  847. subpass.PColorAttachments = &attachmentReferences[0];
  848. // Fill with VK_ATTACHMENT_UNUSED to cover any gaps.
  849. for (int i = 0; i <= maxAttachmentIndex; i++)
  850. {
  851. subpass.PColorAttachments[i] = new AttachmentReference(Vk.AttachmentUnused, ImageLayout.Undefined);
  852. }
  853. for (int i = 0; i < colorAttachmentsCount; i++)
  854. {
  855. int bindIndex = FramebufferParams.AttachmentIndices[i];
  856. subpass.PColorAttachments[bindIndex] = new AttachmentReference((uint)i, ImageLayout.General);
  857. }
  858. }
  859. if (FramebufferParams.HasDepthStencil)
  860. {
  861. uint dsIndex = (uint)FramebufferParams.AttachmentsCount - 1;
  862. subpass.PDepthStencilAttachment = &attachmentReferences[MaxAttachments - 1];
  863. *subpass.PDepthStencilAttachment = new AttachmentReference(dsIndex, ImageLayout.General);
  864. }
  865. }
  866. var subpassDependency = new SubpassDependency(
  867. 0,
  868. 0,
  869. PipelineStageFlags.PipelineStageAllGraphicsBit,
  870. PipelineStageFlags.PipelineStageAllGraphicsBit,
  871. AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit | AccessFlags.AccessColorAttachmentWriteBit,
  872. AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit | AccessFlags.AccessShaderReadBit,
  873. 0);
  874. fixed (AttachmentDescription* pAttachmentDescs = attachmentDescs)
  875. {
  876. var renderPassCreateInfo = new RenderPassCreateInfo()
  877. {
  878. SType = StructureType.RenderPassCreateInfo,
  879. PAttachments = pAttachmentDescs,
  880. AttachmentCount = attachmentDescs != null ? (uint)attachmentDescs.Length : 0,
  881. PSubpasses = &subpass,
  882. SubpassCount = 1,
  883. PDependencies = &subpassDependency,
  884. DependencyCount = 1
  885. };
  886. Gd.Api.CreateRenderPass(Device, renderPassCreateInfo, null, out var renderPass).ThrowOnError();
  887. _renderPass?.Dispose();
  888. _renderPass = new Auto<DisposableRenderPass>(new DisposableRenderPass(Gd.Api, Device, renderPass));
  889. }
  890. EndRenderPass();
  891. _framebuffer?.Dispose();
  892. _framebuffer = hasFramebuffer ? FramebufferParams.Create(Gd.Api, Cbs, _renderPass) : null;
  893. }
  894. protected void SignalStateChange()
  895. {
  896. _stateDirty = true;
  897. }
  898. private void RecreatePipelineIfNeeded(PipelineBindPoint pbp)
  899. {
  900. _dynamicState.ReplayIfDirty(Gd.Api, CommandBuffer);
  901. // Commit changes to the support buffer before drawing.
  902. SupportBufferUpdater.Commit();
  903. if (_needsIndexBufferRebind)
  904. {
  905. _indexBuffer.BindIndexBuffer(Gd.Api, Cbs);
  906. _needsIndexBufferRebind = false;
  907. }
  908. if (_needsTransformFeedbackBuffersRebind)
  909. {
  910. PauseTransformFeedbackInternal();
  911. for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
  912. {
  913. _transformFeedbackBuffers[i].BindTransformFeedbackBuffer(Gd, Cbs, (uint)i);
  914. }
  915. _needsTransformFeedbackBuffersRebind = false;
  916. }
  917. if (_vertexBuffersDirty != 0)
  918. {
  919. while (_vertexBuffersDirty != 0)
  920. {
  921. int i = BitOperations.TrailingZeroCount(_vertexBuffersDirty);
  922. _vertexBuffers[i].BindVertexBuffer(Gd, Cbs, (uint)i, ref _newState);
  923. _vertexBuffersDirty &= ~(1u << i);
  924. }
  925. }
  926. if (_stateDirty || Pbp != pbp)
  927. {
  928. CreatePipeline(pbp);
  929. _stateDirty = false;
  930. Pbp = pbp;
  931. }
  932. _descriptorSetUpdater.UpdateAndBindDescriptorSets(Cbs, pbp);
  933. }
  934. private void CreatePipeline(PipelineBindPoint pbp)
  935. {
  936. // We can only create a pipeline if the have the shader stages set.
  937. if (_newState.Stages != null)
  938. {
  939. if (pbp == PipelineBindPoint.Graphics && _renderPass == null)
  940. {
  941. CreateRenderPass();
  942. }
  943. var pipeline = pbp == PipelineBindPoint.Compute
  944. ? _newState.CreateComputePipeline(Gd, Device, _program, PipelineCache)
  945. : _newState.CreateGraphicsPipeline(Gd, Device, _program, PipelineCache, _renderPass.Get(Cbs).Value);
  946. ulong pipelineHandle = pipeline.GetUnsafe().Value.Handle;
  947. if (_currentPipelineHandle != pipelineHandle)
  948. {
  949. _currentPipelineHandle = pipelineHandle;
  950. Pipeline = pipeline;
  951. PauseTransformFeedbackInternal();
  952. Gd.Api.CmdBindPipeline(CommandBuffer, pbp, Pipeline.Get(Cbs).Value);
  953. }
  954. }
  955. }
  956. private unsafe void BeginRenderPass()
  957. {
  958. if (!_renderPassActive)
  959. {
  960. var renderArea = new Rect2D(null, new Extent2D(FramebufferParams.Width, FramebufferParams.Height));
  961. var clearValue = new ClearValue();
  962. var renderPassBeginInfo = new RenderPassBeginInfo()
  963. {
  964. SType = StructureType.RenderPassBeginInfo,
  965. RenderPass = _renderPass.Get(Cbs).Value,
  966. Framebuffer = _framebuffer.Get(Cbs).Value,
  967. RenderArea = renderArea,
  968. PClearValues = &clearValue,
  969. ClearValueCount = 1
  970. };
  971. Gd.Api.CmdBeginRenderPass(CommandBuffer, renderPassBeginInfo, SubpassContents.Inline);
  972. _renderPassActive = true;
  973. }
  974. }
  975. public void EndRenderPass()
  976. {
  977. if (_renderPassActive)
  978. {
  979. PauseTransformFeedbackInternal();
  980. Gd.Api.CmdEndRenderPass(CommandBuffer);
  981. SignalRenderPassEnd();
  982. _renderPassActive = false;
  983. }
  984. }
  985. protected virtual void SignalRenderPassEnd()
  986. {
  987. }
  988. private void PauseTransformFeedbackInternal()
  989. {
  990. if (_tfEnabled && _tfActive)
  991. {
  992. EndTransformFeedbackInternal();
  993. _tfActive = false;
  994. }
  995. }
  996. private void ResumeTransformFeedbackInternal()
  997. {
  998. if (_tfEnabled && !_tfActive)
  999. {
  1000. BeginTransformFeedbackInternal();
  1001. _tfActive = true;
  1002. }
  1003. }
  1004. private unsafe void BeginTransformFeedbackInternal()
  1005. {
  1006. Gd.TransformFeedbackApi.CmdBeginTransformFeedback(CommandBuffer, 0, 0, null, null);
  1007. }
  1008. private unsafe void EndTransformFeedbackInternal()
  1009. {
  1010. Gd.TransformFeedbackApi.CmdEndTransformFeedback(CommandBuffer, 0, 0, null, null);
  1011. }
  1012. protected virtual void Dispose(bool disposing)
  1013. {
  1014. if (disposing)
  1015. {
  1016. _renderPass?.Dispose();
  1017. _framebuffer?.Dispose();
  1018. _indexBuffer.Dispose();
  1019. _newState.Dispose();
  1020. _descriptorSetUpdater.Dispose();
  1021. for (int i = 0; i < _vertexBuffers.Length; i++)
  1022. {
  1023. _vertexBuffers[i].Dispose();
  1024. }
  1025. for (int i = 0; i < _transformFeedbackBuffers.Length; i++)
  1026. {
  1027. _transformFeedbackBuffers[i].Dispose();
  1028. }
  1029. Pipeline?.Dispose();
  1030. unsafe
  1031. {
  1032. Gd.Api.DestroyPipelineCache(Device, PipelineCache, null);
  1033. }
  1034. SupportBufferUpdater.Dispose();
  1035. }
  1036. }
  1037. public void Dispose()
  1038. {
  1039. Dispose(true);
  1040. }
  1041. }
  1042. }