PipelineBase.cs 58 KB

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