PipelineBase.cs 56 KB

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