BufferManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Shader;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. namespace Ryujinx.Graphics.Gpu.Memory
  10. {
  11. /// <summary>
  12. /// Buffer manager.
  13. /// </summary>
  14. class BufferManager : IDisposable
  15. {
  16. private const int StackToHeapThreshold = 16;
  17. private readonly GpuContext _context;
  18. private IndexBuffer _indexBuffer;
  19. private readonly VertexBuffer[] _vertexBuffers;
  20. private readonly BufferBounds[] _transformFeedbackBuffers;
  21. private readonly List<BufferTextureBinding> _bufferTextures;
  22. /// <summary>
  23. /// Holds shader stage buffer state and binding information.
  24. /// </summary>
  25. private class BuffersPerStage
  26. {
  27. /// <summary>
  28. /// Shader buffer binding information.
  29. /// </summary>
  30. public BufferDescriptor[] Bindings { get; }
  31. /// <summary>
  32. /// Buffer regions.
  33. /// </summary>
  34. public BufferBounds[] Buffers { get; }
  35. /// <summary>
  36. /// Total amount of buffers used on the shader.
  37. /// </summary>
  38. public int Count { get; private set; }
  39. /// <summary>
  40. /// Creates a new instance of the shader stage buffer information.
  41. /// </summary>
  42. /// <param name="count">Maximum amount of buffers that the shader stage can use</param>
  43. public BuffersPerStage(int count)
  44. {
  45. Bindings = new BufferDescriptor[count];
  46. Buffers = new BufferBounds[count];
  47. }
  48. /// <summary>
  49. /// Sets the region of a buffer at a given slot.
  50. /// </summary>
  51. /// <param name="index">Buffer slot</param>
  52. /// <param name="address">Region virtual address</param>
  53. /// <param name="size">Region size in bytes</param>
  54. /// <param name="flags">Buffer usage flags</param>
  55. public void SetBounds(int index, ulong address, ulong size, BufferUsageFlags flags = BufferUsageFlags.None)
  56. {
  57. Buffers[index] = new BufferBounds(address, size, flags);
  58. }
  59. /// <summary>
  60. /// Sets shader buffer binding information.
  61. /// </summary>
  62. /// <param name="descriptors">Buffer binding information</param>
  63. public void SetBindings(ReadOnlyCollection<BufferDescriptor> descriptors)
  64. {
  65. if (descriptors == null)
  66. {
  67. Count = 0;
  68. return;
  69. }
  70. descriptors.CopyTo(Bindings, 0);
  71. Count = descriptors.Count;
  72. }
  73. }
  74. private readonly BuffersPerStage _cpStorageBuffers;
  75. private readonly BuffersPerStage _cpUniformBuffers;
  76. private readonly BuffersPerStage[] _gpStorageBuffers;
  77. private readonly BuffersPerStage[] _gpUniformBuffers;
  78. private int _cpStorageBufferBindings;
  79. private int _cpUniformBufferBindings;
  80. private int _gpStorageBufferBindings;
  81. private int _gpUniformBufferBindings;
  82. private bool _gpStorageBuffersDirty;
  83. private bool _gpUniformBuffersDirty;
  84. private bool _indexBufferDirty;
  85. private bool _vertexBuffersDirty;
  86. private uint _vertexBuffersEnableMask;
  87. private bool _transformFeedbackBuffersDirty;
  88. private bool _rebind;
  89. /// <summary>
  90. /// Creates a new instance of the buffer manager.
  91. /// </summary>
  92. /// <param name="context">GPU context that the buffer manager belongs to</param>
  93. public BufferManager(GpuContext context)
  94. {
  95. _context = context;
  96. _vertexBuffers = new VertexBuffer[Constants.TotalVertexBuffers];
  97. _transformFeedbackBuffers = new BufferBounds[Constants.TotalTransformFeedbackBuffers];
  98. _cpStorageBuffers = new BuffersPerStage(Constants.TotalCpStorageBuffers);
  99. _cpUniformBuffers = new BuffersPerStage(Constants.TotalCpUniformBuffers);
  100. _gpStorageBuffers = new BuffersPerStage[Constants.ShaderStages];
  101. _gpUniformBuffers = new BuffersPerStage[Constants.ShaderStages];
  102. for (int index = 0; index < Constants.ShaderStages; index++)
  103. {
  104. _gpStorageBuffers[index] = new BuffersPerStage(Constants.TotalGpStorageBuffers);
  105. _gpUniformBuffers[index] = new BuffersPerStage(Constants.TotalGpUniformBuffers);
  106. }
  107. _bufferTextures = new List<BufferTextureBinding>();
  108. context.Methods.BufferCache.NotifyBuffersModified += Rebind;
  109. }
  110. /// <summary>
  111. /// Sets the memory range with the index buffer data, to be used for subsequent draw calls.
  112. /// </summary>
  113. /// <param name="gpuVa">Start GPU virtual address of the index buffer</param>
  114. /// <param name="size">Size, in bytes, of the index buffer</param>
  115. /// <param name="type">Type of each index buffer element</param>
  116. public void SetIndexBuffer(ulong gpuVa, ulong size, IndexType type)
  117. {
  118. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  119. _indexBuffer.Address = address;
  120. _indexBuffer.Size = size;
  121. _indexBuffer.Type = type;
  122. _indexBufferDirty = true;
  123. }
  124. /// <summary>
  125. /// Sets a new index buffer that overrides the one set on the call to <see cref="CommitGraphicsBindings"/>.
  126. /// </summary>
  127. /// <param name="buffer">Buffer to be used as index buffer</param>
  128. /// <param name="type">Type of each index buffer element</param>
  129. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  130. {
  131. _context.Renderer.Pipeline.SetIndexBuffer(buffer, type);
  132. _indexBufferDirty = true;
  133. }
  134. /// <summary>
  135. /// Sets the memory range with vertex buffer data, to be used for subsequent draw calls.
  136. /// </summary>
  137. /// <param name="index">Index of the vertex buffer (up to 16)</param>
  138. /// <param name="gpuVa">GPU virtual address of the buffer</param>
  139. /// <param name="size">Size in bytes of the buffer</param>
  140. /// <param name="stride">Stride of the buffer, defined as the number of bytes of each vertex</param>
  141. /// <param name="divisor">Vertex divisor of the buffer, for instanced draws</param>
  142. public void SetVertexBuffer(int index, ulong gpuVa, ulong size, int stride, int divisor)
  143. {
  144. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  145. _vertexBuffers[index].Address = address;
  146. _vertexBuffers[index].Size = size;
  147. _vertexBuffers[index].Stride = stride;
  148. _vertexBuffers[index].Divisor = divisor;
  149. _vertexBuffersDirty = true;
  150. if (address != 0)
  151. {
  152. _vertexBuffersEnableMask |= 1u << index;
  153. }
  154. else
  155. {
  156. _vertexBuffersEnableMask &= ~(1u << index);
  157. }
  158. }
  159. /// <summary>
  160. /// Sets a transform feedback buffer on the graphics pipeline.
  161. /// The output from the vertex transformation stages are written into the feedback buffer.
  162. /// </summary>
  163. /// <param name="index">Index of the transform feedback buffer</param>
  164. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  165. /// <param name="size">Size in bytes of the transform feedback buffer</param>
  166. public void SetTransformFeedbackBuffer(int index, ulong gpuVa, ulong size)
  167. {
  168. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  169. _transformFeedbackBuffers[index] = new BufferBounds(address, size);
  170. _transformFeedbackBuffersDirty = true;
  171. }
  172. /// <summary>
  173. /// Sets a storage buffer on the compute pipeline.
  174. /// Storage buffers can be read and written to on shaders.
  175. /// </summary>
  176. /// <param name="index">Index of the storage buffer</param>
  177. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  178. /// <param name="size">Size in bytes of the storage buffer</param>
  179. /// <param name="flags">Buffer usage flags</param>
  180. public void SetComputeStorageBuffer(int index, ulong gpuVa, ulong size, BufferUsageFlags flags)
  181. {
  182. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  183. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  184. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  185. _cpStorageBuffers.SetBounds(index, address, size, flags);
  186. }
  187. /// <summary>
  188. /// Sets a storage buffer on the graphics pipeline.
  189. /// Storage buffers can be read and written to on shaders.
  190. /// </summary>
  191. /// <param name="stage">Index of the shader stage</param>
  192. /// <param name="index">Index of the storage buffer</param>
  193. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  194. /// <param name="size">Size in bytes of the storage buffer</param>
  195. /// <param name="flags">Buffer usage flags</param>
  196. public void SetGraphicsStorageBuffer(int stage, int index, ulong gpuVa, ulong size, BufferUsageFlags flags)
  197. {
  198. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  199. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  200. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  201. if (_gpStorageBuffers[stage].Buffers[index].Address != address ||
  202. _gpStorageBuffers[stage].Buffers[index].Size != size)
  203. {
  204. _gpStorageBuffersDirty = true;
  205. }
  206. _gpStorageBuffers[stage].SetBounds(index, address, size, flags);
  207. }
  208. /// <summary>
  209. /// Sets a uniform buffer on the compute pipeline.
  210. /// Uniform buffers are read-only from shaders, and have a small capacity.
  211. /// </summary>
  212. /// <param name="index">Index of the uniform buffer</param>
  213. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  214. /// <param name="size">Size in bytes of the storage buffer</param>
  215. public void SetComputeUniformBuffer(int index, ulong gpuVa, ulong size)
  216. {
  217. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  218. _cpUniformBuffers.SetBounds(index, address, size);
  219. }
  220. /// <summary>
  221. /// Sets a uniform buffer on the graphics pipeline.
  222. /// Uniform buffers are read-only from shaders, and have a small capacity.
  223. /// </summary>
  224. /// <param name="stage">Index of the shader stage</param>
  225. /// <param name="index">Index of the uniform buffer</param>
  226. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  227. /// <param name="size">Size in bytes of the storage buffer</param>
  228. public void SetGraphicsUniformBuffer(int stage, int index, ulong gpuVa, ulong size)
  229. {
  230. ulong address = _context.Methods.BufferCache.TranslateAndCreateBuffer(gpuVa, size);
  231. _gpUniformBuffers[stage].SetBounds(index, address, size);
  232. _gpUniformBuffersDirty = true;
  233. }
  234. /// <summary>
  235. /// Sets the binding points for the storage buffers bound on the compute pipeline.
  236. /// </summary>
  237. /// <param name="descriptors">Buffer descriptors with the binding point values</param>
  238. public void SetComputeStorageBufferBindings(ReadOnlyCollection<BufferDescriptor> descriptors)
  239. {
  240. _cpStorageBuffers.SetBindings(descriptors);
  241. _cpStorageBufferBindings = descriptors.Count != 0 ? descriptors.Max(x => x.Binding) + 1 : 0;
  242. }
  243. /// <summary>
  244. /// Sets the binding points for the storage buffers bound on the graphics pipeline.
  245. /// </summary>
  246. /// <param name="stage">Index of the shader stage</param>
  247. /// <param name="descriptors">Buffer descriptors with the binding point values</param>
  248. public void SetGraphicsStorageBufferBindings(int stage, ReadOnlyCollection<BufferDescriptor> descriptors)
  249. {
  250. _gpStorageBuffers[stage].SetBindings(descriptors);
  251. _gpStorageBuffersDirty = true;
  252. }
  253. /// <summary>
  254. /// Sets the total number of storage buffer bindings used.
  255. /// </summary>
  256. /// <param name="count">Number of storage buffer bindings used</param>
  257. public void SetGraphicsStorageBufferBindingsCount(int count)
  258. {
  259. _gpStorageBufferBindings = count;
  260. }
  261. /// <summary>
  262. /// Sets the binding points for the uniform buffers bound on the compute pipeline.
  263. /// </summary>
  264. /// <param name="descriptors">Buffer descriptors with the binding point values</param>
  265. public void SetComputeUniformBufferBindings(ReadOnlyCollection<BufferDescriptor> descriptors)
  266. {
  267. _cpUniformBuffers.SetBindings(descriptors);
  268. _cpUniformBufferBindings = descriptors.Count != 0 ? descriptors.Max(x => x.Binding) + 1 : 0;
  269. }
  270. /// <summary>
  271. /// Sets the enabled uniform buffers mask on the graphics pipeline.
  272. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  273. /// </summary>
  274. /// <param name="stage">Index of the shader stage</param>
  275. /// <param name="descriptors">Buffer descriptors with the binding point values</param>
  276. public void SetGraphicsUniformBufferBindings(int stage, ReadOnlyCollection<BufferDescriptor> descriptors)
  277. {
  278. _gpUniformBuffers[stage].SetBindings(descriptors);
  279. _gpUniformBuffersDirty = true;
  280. }
  281. /// <summary>
  282. /// Sets the total number of uniform buffer bindings used.
  283. /// </summary>
  284. /// <param name="count">Number of uniform buffer bindings used</param>
  285. public void SetGraphicsUniformBufferBindingsCount(int count)
  286. {
  287. _gpUniformBufferBindings = count;
  288. }
  289. /// <summary>
  290. /// Gets a bit mask indicating which compute uniform buffers are currently bound.
  291. /// </summary>
  292. /// <returns>Mask where each bit set indicates a bound constant buffer</returns>
  293. public uint GetComputeUniformBufferUseMask()
  294. {
  295. uint mask = 0;
  296. for (int i = 0; i < _cpUniformBuffers.Buffers.Length; i++)
  297. {
  298. if (_cpUniformBuffers.Buffers[i].Address != 0)
  299. {
  300. mask |= 1u << i;
  301. }
  302. }
  303. return mask;
  304. }
  305. /// <summary>
  306. /// Gets a bit mask indicating which graphics uniform buffers are currently bound.
  307. /// </summary>
  308. /// <param name="stage">Index of the shader stage</param>
  309. /// <returns>Mask where each bit set indicates a bound constant buffer</returns>
  310. public uint GetGraphicsUniformBufferUseMask(int stage)
  311. {
  312. uint mask = 0;
  313. for (int i = 0; i < _gpUniformBuffers[stage].Buffers.Length; i++)
  314. {
  315. if (_gpUniformBuffers[stage].Buffers[i].Address != 0)
  316. {
  317. mask |= 1u << i;
  318. }
  319. }
  320. return mask;
  321. }
  322. /// <summary>
  323. /// Gets the address of the compute uniform buffer currently bound at the given index.
  324. /// </summary>
  325. /// <param name="index">Index of the uniform buffer binding</param>
  326. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  327. public ulong GetComputeUniformBufferAddress(int index)
  328. {
  329. return _cpUniformBuffers.Buffers[index].Address;
  330. }
  331. /// <summary>
  332. /// Gets the address of the graphics uniform buffer currently bound at the given index.
  333. /// </summary>
  334. /// <param name="stage">Index of the shader stage</param>
  335. /// <param name="index">Index of the uniform buffer binding</param>
  336. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  337. public ulong GetGraphicsUniformBufferAddress(int stage, int index)
  338. {
  339. return _gpUniformBuffers[stage].Buffers[index].Address;
  340. }
  341. /// <summary>
  342. /// Ensures that the compute engine bindings are visible to the host GPU.
  343. /// Note: this actually performs the binding using the host graphics API.
  344. /// </summary>
  345. public void CommitComputeBindings()
  346. {
  347. int sCount = _cpStorageBufferBindings;
  348. Span<BufferRange> sRanges = sCount < StackToHeapThreshold ? stackalloc BufferRange[sCount] : new BufferRange[sCount];
  349. for (int index = 0; index < _cpStorageBuffers.Count; index++)
  350. {
  351. ref var bindingInfo = ref _cpStorageBuffers.Bindings[index];
  352. BufferBounds bounds = _cpStorageBuffers.Buffers[bindingInfo.Slot];
  353. if (bounds.Address != 0)
  354. {
  355. // The storage buffer size is not reliable (it might be lower than the actual size),
  356. // so we bind the entire buffer to allow otherwise out of range accesses to work.
  357. sRanges[bindingInfo.Binding] = _context.Methods.BufferCache.GetBufferRangeTillEnd(
  358. bounds.Address,
  359. bounds.Size,
  360. bounds.Flags.HasFlag(BufferUsageFlags.Write));
  361. }
  362. }
  363. _context.Renderer.Pipeline.SetStorageBuffers(sRanges);
  364. int uCount = _cpUniformBufferBindings;
  365. Span<BufferRange> uRanges = uCount < StackToHeapThreshold ? stackalloc BufferRange[uCount] : new BufferRange[uCount];
  366. for (int index = 0; index < _cpUniformBuffers.Count; index++)
  367. {
  368. ref var bindingInfo = ref _cpUniformBuffers.Bindings[index];
  369. BufferBounds bounds = _cpUniformBuffers.Buffers[bindingInfo.Slot];
  370. if (bounds.Address != 0)
  371. {
  372. uRanges[bindingInfo.Binding] = _context.Methods.BufferCache.GetBufferRange(bounds.Address, bounds.Size);
  373. }
  374. }
  375. _context.Renderer.Pipeline.SetUniformBuffers(uRanges);
  376. CommitBufferTextureBindings();
  377. // Force rebind after doing compute work.
  378. Rebind();
  379. }
  380. /// <summary>
  381. /// Commit any queued buffer texture bindings.
  382. /// </summary>
  383. private void CommitBufferTextureBindings()
  384. {
  385. if (_bufferTextures.Count > 0)
  386. {
  387. foreach (var binding in _bufferTextures)
  388. {
  389. var isStore = binding.BindingInfo.Flags.HasFlag(TextureUsageFlags.ImageStore);
  390. var range = _context.Methods.BufferCache.GetBufferRange(binding.Address, binding.Size, isStore);
  391. binding.Texture.SetStorage(range);
  392. // The texture must be rebound to use the new storage if it was updated.
  393. if (binding.IsImage)
  394. {
  395. _context.Renderer.Pipeline.SetImage(binding.BindingInfo.Binding, binding.Texture, binding.Format);
  396. }
  397. else
  398. {
  399. _context.Renderer.Pipeline.SetTexture(binding.BindingInfo.Binding, binding.Texture);
  400. }
  401. }
  402. _bufferTextures.Clear();
  403. }
  404. }
  405. /// <summary>
  406. /// Ensures that the graphics engine bindings are visible to the host GPU.
  407. /// Note: this actually performs the binding using the host graphics API.
  408. /// </summary>
  409. public void CommitGraphicsBindings()
  410. {
  411. if (_indexBufferDirty || _rebind)
  412. {
  413. _indexBufferDirty = false;
  414. if (_indexBuffer.Address != 0)
  415. {
  416. BufferRange buffer = _context.Methods.BufferCache.GetBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  417. _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type);
  418. }
  419. }
  420. else if (_indexBuffer.Address != 0)
  421. {
  422. _context.Methods.BufferCache.SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  423. }
  424. uint vbEnableMask = _vertexBuffersEnableMask;
  425. if (_vertexBuffersDirty || _rebind)
  426. {
  427. _vertexBuffersDirty = false;
  428. Span<VertexBufferDescriptor> vertexBuffers = stackalloc VertexBufferDescriptor[Constants.TotalVertexBuffers];
  429. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  430. {
  431. VertexBuffer vb = _vertexBuffers[index];
  432. if (vb.Address == 0)
  433. {
  434. continue;
  435. }
  436. BufferRange buffer = _context.Methods.BufferCache.GetBufferRange(vb.Address, vb.Size);
  437. vertexBuffers[index] = new VertexBufferDescriptor(buffer, vb.Stride, vb.Divisor);
  438. }
  439. _context.Renderer.Pipeline.SetVertexBuffers(vertexBuffers);
  440. }
  441. else
  442. {
  443. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  444. {
  445. VertexBuffer vb = _vertexBuffers[index];
  446. if (vb.Address == 0)
  447. {
  448. continue;
  449. }
  450. _context.Methods.BufferCache.SynchronizeBufferRange(vb.Address, vb.Size);
  451. }
  452. }
  453. if (_transformFeedbackBuffersDirty || _rebind)
  454. {
  455. _transformFeedbackBuffersDirty = false;
  456. Span<BufferRange> tfbs = stackalloc BufferRange[Constants.TotalTransformFeedbackBuffers];
  457. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  458. {
  459. BufferBounds tfb = _transformFeedbackBuffers[index];
  460. if (tfb.Address == 0)
  461. {
  462. tfbs[index] = BufferRange.Empty;
  463. continue;
  464. }
  465. tfbs[index] = _context.Methods.BufferCache.GetBufferRange(tfb.Address, tfb.Size);
  466. }
  467. _context.Renderer.Pipeline.SetTransformFeedbackBuffers(tfbs);
  468. }
  469. else
  470. {
  471. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  472. {
  473. BufferBounds tfb = _transformFeedbackBuffers[index];
  474. if (tfb.Address == 0)
  475. {
  476. continue;
  477. }
  478. _context.Methods.BufferCache.SynchronizeBufferRange(tfb.Address, tfb.Size);
  479. }
  480. }
  481. if (_gpStorageBuffersDirty || _rebind)
  482. {
  483. _gpStorageBuffersDirty = false;
  484. BindBuffers(_gpStorageBuffers, isStorage: true);
  485. }
  486. else
  487. {
  488. UpdateBuffers(_gpStorageBuffers);
  489. }
  490. if (_gpUniformBuffersDirty || _rebind)
  491. {
  492. _gpUniformBuffersDirty = false;
  493. BindBuffers(_gpUniformBuffers, isStorage: false);
  494. }
  495. else
  496. {
  497. UpdateBuffers(_gpUniformBuffers);
  498. }
  499. CommitBufferTextureBindings();
  500. _rebind = false;
  501. }
  502. /// <summary>
  503. /// Bind respective buffer bindings on the host API.
  504. /// </summary>
  505. /// <param name="bindings">Bindings to bind</param>
  506. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
  507. private void BindBuffers(BuffersPerStage[] bindings, bool isStorage)
  508. {
  509. int count = isStorage ? _gpStorageBufferBindings : _gpUniformBufferBindings;
  510. Span<BufferRange> ranges = count < StackToHeapThreshold ? stackalloc BufferRange[count] : new BufferRange[count];
  511. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  512. {
  513. ref var buffers = ref bindings[(int)stage - 1];
  514. for (int index = 0; index < buffers.Count; index++)
  515. {
  516. ref var bindingInfo = ref buffers.Bindings[index];
  517. BufferBounds bounds = buffers.Buffers[bindingInfo.Slot];
  518. if (bounds.Address != 0)
  519. {
  520. var isWrite = bounds.Flags.HasFlag(BufferUsageFlags.Write);
  521. ranges[bindingInfo.Binding] = isStorage
  522. ? _context.Methods.BufferCache.GetBufferRangeTillEnd(bounds.Address, bounds.Size, isWrite)
  523. : _context.Methods.BufferCache.GetBufferRange(bounds.Address, bounds.Size, isWrite);
  524. }
  525. }
  526. }
  527. if (isStorage)
  528. {
  529. _context.Renderer.Pipeline.SetStorageBuffers(ranges);
  530. }
  531. else
  532. {
  533. _context.Renderer.Pipeline.SetUniformBuffers(ranges);
  534. }
  535. }
  536. /// <summary>
  537. /// Updates data for the already bound buffer bindings.
  538. /// </summary>
  539. /// <param name="bindings">Bindings to update</param>
  540. private void UpdateBuffers(BuffersPerStage[] bindings)
  541. {
  542. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  543. {
  544. ref var buffers = ref bindings[(int)stage - 1];
  545. for (int index = 0; index < buffers.Count; index++)
  546. {
  547. ref var binding = ref buffers.Bindings[index];
  548. BufferBounds bounds = buffers.Buffers[binding.Slot];
  549. if (bounds.Address == 0)
  550. {
  551. continue;
  552. }
  553. _context.Methods.BufferCache.SynchronizeBufferRange(bounds.Address, bounds.Size);
  554. }
  555. }
  556. }
  557. /// <summary>
  558. /// Sets the buffer storage of a buffer texture. This will be bound when the buffer manager commits bindings.
  559. /// </summary>
  560. /// <param name="texture">Buffer texture</param>
  561. /// <param name="address">Address of the buffer in memory</param>
  562. /// <param name="size">Size of the buffer in bytes</param>
  563. /// <param name="bindingInfo">Binding info for the buffer texture</param>
  564. /// <param name="format">Format of the buffer texture</param>
  565. /// <param name="isImage">Whether the binding is for an image or a sampler</param>
  566. public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, TextureBindingInfo bindingInfo, Format format, bool isImage)
  567. {
  568. _context.Methods.BufferCache.CreateBuffer(address, size);
  569. _bufferTextures.Add(new BufferTextureBinding(texture, address, size, bindingInfo, format, isImage));
  570. }
  571. /// <summary>
  572. /// Force all bound textures and images to be rebound the next time CommitBindings is called.
  573. /// </summary>
  574. public void Rebind()
  575. {
  576. _rebind = true;
  577. }
  578. /// <summary>
  579. /// Disposes the buffer manager.
  580. /// It is an error to use the buffer manager after disposal.
  581. /// </summary>
  582. public void Dispose()
  583. {
  584. _context.Methods.BufferCache.NotifyBuffersModified -= Rebind;
  585. }
  586. }
  587. }