BufferManager.cs 28 KB

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