PipelineBase.cs 46 KB

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