PipelineBase.cs 63 KB

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