PipelineBase.cs 56 KB

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