PipelineBase.cs 58 KB

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