PipelineBase.cs 61 KB

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