BufferManager.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.State;
  4. using Ryujinx.Graphics.Shader;
  5. using System;
  6. namespace Ryujinx.Graphics.Gpu.Memory
  7. {
  8. /// <summary>
  9. /// Buffer manager.
  10. /// </summary>
  11. class BufferManager
  12. {
  13. private const int OverlapsBufferInitialCapacity = 10;
  14. private const int OverlapsBufferMaxCapacity = 10000;
  15. private const ulong BufferAlignmentSize = 0x1000;
  16. private const ulong BufferAlignmentMask = BufferAlignmentSize - 1;
  17. private GpuContext _context;
  18. private RangeList<Buffer> _buffers;
  19. private Buffer[] _bufferOverlaps;
  20. private IndexBuffer _indexBuffer;
  21. private VertexBuffer[] _vertexBuffers;
  22. private BufferBounds[] _transformFeedbackBuffers;
  23. private class BuffersPerStage
  24. {
  25. public uint EnableMask { get; set; }
  26. public BufferBounds[] Buffers { get; }
  27. public BuffersPerStage(int count)
  28. {
  29. Buffers = new BufferBounds[count];
  30. }
  31. public void Bind(int index, ulong address, ulong size)
  32. {
  33. Buffers[index].Address = address;
  34. Buffers[index].Size = size;
  35. }
  36. }
  37. private BuffersPerStage _cpStorageBuffers;
  38. private BuffersPerStage _cpUniformBuffers;
  39. private BuffersPerStage[] _gpStorageBuffers;
  40. private BuffersPerStage[] _gpUniformBuffers;
  41. private bool _gpStorageBuffersDirty;
  42. private bool _gpUniformBuffersDirty;
  43. private bool _indexBufferDirty;
  44. private bool _vertexBuffersDirty;
  45. private uint _vertexBuffersEnableMask;
  46. private bool _transformFeedbackBuffersDirty;
  47. private bool _rebind;
  48. /// <summary>
  49. /// Creates a new instance of the buffer manager.
  50. /// </summary>
  51. /// <param name="context">The GPU context that the buffer manager belongs to</param>
  52. public BufferManager(GpuContext context)
  53. {
  54. _context = context;
  55. _buffers = new RangeList<Buffer>();
  56. _bufferOverlaps = new Buffer[OverlapsBufferInitialCapacity];
  57. _vertexBuffers = new VertexBuffer[Constants.TotalVertexBuffers];
  58. _transformFeedbackBuffers = new BufferBounds[Constants.TotalTransformFeedbackBuffers];
  59. _cpStorageBuffers = new BuffersPerStage(Constants.TotalCpStorageBuffers);
  60. _cpUniformBuffers = new BuffersPerStage(Constants.TotalCpUniformBuffers);
  61. _gpStorageBuffers = new BuffersPerStage[Constants.ShaderStages];
  62. _gpUniformBuffers = new BuffersPerStage[Constants.ShaderStages];
  63. for (int index = 0; index < Constants.ShaderStages; index++)
  64. {
  65. _gpStorageBuffers[index] = new BuffersPerStage(Constants.TotalGpStorageBuffers);
  66. _gpUniformBuffers[index] = new BuffersPerStage(Constants.TotalGpUniformBuffers);
  67. }
  68. }
  69. /// <summary>
  70. /// Sets the memory range with the index buffer data, to be used for subsequent draw calls.
  71. /// </summary>
  72. /// <param name="gpuVa">Start GPU virtual address of the index buffer</param>
  73. /// <param name="size">Size, in bytes, of the index buffer</param>
  74. /// <param name="type">Type of each index buffer element</param>
  75. public void SetIndexBuffer(ulong gpuVa, ulong size, IndexType type)
  76. {
  77. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  78. _indexBuffer.Address = address;
  79. _indexBuffer.Size = size;
  80. _indexBuffer.Type = type;
  81. _indexBufferDirty = true;
  82. }
  83. /// <summary>
  84. /// Sets a new index buffer that overrides the one set on the call to <see cref="CommitGraphicsBindings"/>.
  85. /// </summary>
  86. /// <param name="buffer">Buffer to be used as index buffer</param>
  87. /// <param name="type">Type of each index buffer element</param>
  88. public void SetIndexBuffer(BufferRange buffer, IndexType type)
  89. {
  90. _context.Renderer.Pipeline.SetIndexBuffer(buffer, type);
  91. _indexBufferDirty = true;
  92. }
  93. /// <summary>
  94. /// Sets the memory range with vertex buffer data, to be used for subsequent draw calls.
  95. /// </summary>
  96. /// <param name="index">Index of the vertex buffer (up to 16)</param>
  97. /// <param name="gpuVa">GPU virtual address of the buffer</param>
  98. /// <param name="size">Size in bytes of the buffer</param>
  99. /// <param name="stride">Stride of the buffer, defined as the number of bytes of each vertex</param>
  100. /// <param name="divisor">Vertex divisor of the buffer, for instanced draws</param>
  101. public void SetVertexBuffer(int index, ulong gpuVa, ulong size, int stride, int divisor)
  102. {
  103. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  104. _vertexBuffers[index].Address = address;
  105. _vertexBuffers[index].Size = size;
  106. _vertexBuffers[index].Stride = stride;
  107. _vertexBuffers[index].Divisor = divisor;
  108. _vertexBuffersDirty = true;
  109. if (address != 0)
  110. {
  111. _vertexBuffersEnableMask |= 1u << index;
  112. }
  113. else
  114. {
  115. _vertexBuffersEnableMask &= ~(1u << index);
  116. }
  117. }
  118. public void SetTransformFeedbackBuffer(int index, ulong gpuVa, ulong size)
  119. {
  120. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  121. _transformFeedbackBuffers[index].Address = address;
  122. _transformFeedbackBuffers[index].Size = size;
  123. _transformFeedbackBuffersDirty = true;
  124. }
  125. /// <summary>
  126. /// Sets a storage buffer on the compute pipeline.
  127. /// Storage buffers can be read and written to on shaders.
  128. /// </summary>
  129. /// <param name="index">Index of the storage buffer</param>
  130. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  131. /// <param name="size">Size in bytes of the storage buffer</param>
  132. public void SetComputeStorageBuffer(int index, ulong gpuVa, ulong size)
  133. {
  134. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  135. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  136. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  137. _cpStorageBuffers.Bind(index, address, size);
  138. }
  139. /// <summary>
  140. /// Sets a storage buffer on the graphics pipeline.
  141. /// Storage buffers can be read and written to on shaders.
  142. /// </summary>
  143. /// <param name="stage">Index of the shader stage</param>
  144. /// <param name="index">Index of the storage buffer</param>
  145. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  146. /// <param name="size">Size in bytes of the storage buffer</param>
  147. public void SetGraphicsStorageBuffer(int stage, int index, ulong gpuVa, ulong size)
  148. {
  149. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  150. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  151. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  152. if (_gpStorageBuffers[stage].Buffers[index].Address != address ||
  153. _gpStorageBuffers[stage].Buffers[index].Size != size)
  154. {
  155. _gpStorageBuffersDirty = true;
  156. }
  157. _gpStorageBuffers[stage].Bind(index, address, size);
  158. }
  159. /// <summary>
  160. /// Sets a uniform buffer on the compute pipeline.
  161. /// Uniform buffers are read-only from shaders, and have a small capacity.
  162. /// </summary>
  163. /// <param name="index">Index of the uniform buffer</param>
  164. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  165. /// <param name="size">Size in bytes of the storage buffer</param>
  166. public void SetComputeUniformBuffer(int index, ulong gpuVa, ulong size)
  167. {
  168. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  169. _cpUniformBuffers.Bind(index, address, size);
  170. }
  171. /// <summary>
  172. /// Sets a uniform buffer on the graphics pipeline.
  173. /// Uniform buffers are read-only from shaders, and have a small capacity.
  174. /// </summary>
  175. /// <param name="stage">Index of the shader stage</param>
  176. /// <param name="index">Index of the uniform 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. public void SetGraphicsUniformBuffer(int stage, int index, ulong gpuVa, ulong size)
  180. {
  181. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  182. _gpUniformBuffers[stage].Bind(index, address, size);
  183. _gpUniformBuffersDirty = true;
  184. }
  185. /// <summary>
  186. /// Sets the enabled storage buffers mask on the compute pipeline.
  187. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  188. /// </summary>
  189. /// <param name="mask">Buffer enable mask</param>
  190. public void SetComputeStorageBufferEnableMask(uint mask)
  191. {
  192. _cpStorageBuffers.EnableMask = mask;
  193. }
  194. /// <summary>
  195. /// Sets the enabled storage buffers mask on the graphics pipeline.
  196. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  197. /// </summary>
  198. /// <param name="stage">Index of the shader stage</param>
  199. /// <param name="mask">Buffer enable mask</param>
  200. public void SetGraphicsStorageBufferEnableMask(int stage, uint mask)
  201. {
  202. _gpStorageBuffers[stage].EnableMask = mask;
  203. _gpStorageBuffersDirty = true;
  204. }
  205. /// <summary>
  206. /// Sets the enabled uniform buffers mask on the compute pipeline.
  207. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  208. /// </summary>
  209. /// <param name="mask">Buffer enable mask</param>
  210. public void SetComputeUniformBufferEnableMask(uint mask)
  211. {
  212. _cpUniformBuffers.EnableMask = mask;
  213. }
  214. /// <summary>
  215. /// Sets the enabled uniform buffers mask on the graphics pipeline.
  216. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  217. /// </summary>
  218. /// <param name="stage">Index of the shader stage</param>
  219. /// <param name="mask">Buffer enable mask</param>
  220. public void SetGraphicsUniformBufferEnableMask(int stage, uint mask)
  221. {
  222. _gpUniformBuffers[stage].EnableMask = mask;
  223. _gpUniformBuffersDirty = true;
  224. }
  225. /// <summary>
  226. /// Performs address translation of the GPU virtual address, and creates a
  227. /// new buffer, if needed, for the specified range.
  228. /// </summary>
  229. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  230. /// <param name="size">Size in bytes of the buffer</param>
  231. /// <returns>CPU virtual address of the buffer, after address translation</returns>
  232. private ulong TranslateAndCreateBuffer(ulong gpuVa, ulong size)
  233. {
  234. if (gpuVa == 0)
  235. {
  236. return 0;
  237. }
  238. ulong address = _context.MemoryManager.Translate(gpuVa);
  239. if (address == MemoryManager.BadAddress)
  240. {
  241. return 0;
  242. }
  243. CreateBuffer(address, size);
  244. return address;
  245. }
  246. /// <summary>
  247. /// Creates a new buffer for the specified range, if it does not yet exist.
  248. /// This can be used to ensure the existance of a buffer.
  249. /// </summary>
  250. /// <param name="address">Address of the buffer in memory</param>
  251. /// <param name="size">Size of the buffer in bytes</param>
  252. public void CreateBuffer(ulong address, ulong size)
  253. {
  254. ulong endAddress = address + size;
  255. ulong alignedAddress = address & ~BufferAlignmentMask;
  256. ulong alignedEndAddress = (endAddress + BufferAlignmentMask) & ~BufferAlignmentMask;
  257. // The buffer must have the size of at least one page.
  258. if (alignedEndAddress == alignedAddress)
  259. {
  260. alignedEndAddress += BufferAlignmentSize;
  261. }
  262. CreateBufferAligned(alignedAddress, alignedEndAddress - alignedAddress);
  263. }
  264. /// <summary>
  265. /// Creates a new buffer for the specified range, if needed.
  266. /// If a buffer where this range can be fully contained already exists,
  267. /// then the creation of a new buffer is not necessary.
  268. /// </summary>
  269. /// <param name="address">Address of the buffer in guest memory</param>
  270. /// <param name="size">Size in bytes of the buffer</param>
  271. private void CreateBufferAligned(ulong address, ulong size)
  272. {
  273. int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  274. if (overlapsCount != 0)
  275. {
  276. // The buffer already exists. We can just return the existing buffer
  277. // if the buffer we need is fully contained inside the overlapping buffer.
  278. // Otherwise, we must delete the overlapping buffers and create a bigger buffer
  279. // that fits all the data we need. We also need to copy the contents from the
  280. // old buffer(s) to the new buffer.
  281. ulong endAddress = address + size;
  282. if (_bufferOverlaps[0].Address > address || _bufferOverlaps[0].EndAddress < endAddress)
  283. {
  284. for (int index = 0; index < overlapsCount; index++)
  285. {
  286. Buffer buffer = _bufferOverlaps[index];
  287. address = Math.Min(address, buffer.Address);
  288. endAddress = Math.Max(endAddress, buffer.EndAddress);
  289. buffer.SynchronizeMemory(buffer.Address, buffer.Size);
  290. _buffers.Remove(buffer);
  291. }
  292. Buffer newBuffer = new Buffer(_context, address, endAddress - address);
  293. _buffers.Add(newBuffer);
  294. for (int index = 0; index < overlapsCount; index++)
  295. {
  296. Buffer buffer = _bufferOverlaps[index];
  297. int dstOffset = (int)(buffer.Address - newBuffer.Address);
  298. buffer.CopyTo(newBuffer, dstOffset);
  299. buffer.Dispose();
  300. }
  301. // Existing buffers were modified, we need to rebind everything.
  302. _rebind = true;
  303. }
  304. }
  305. else
  306. {
  307. // No overlap, just create a new buffer.
  308. Buffer buffer = new Buffer(_context, address, size);
  309. _buffers.Add(buffer);
  310. }
  311. ShrinkOverlapsBufferIfNeeded();
  312. }
  313. /// <summary>
  314. /// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
  315. /// </summary>
  316. private void ShrinkOverlapsBufferIfNeeded()
  317. {
  318. if (_bufferOverlaps.Length > OverlapsBufferMaxCapacity)
  319. {
  320. Array.Resize(ref _bufferOverlaps, OverlapsBufferMaxCapacity);
  321. }
  322. }
  323. /// <summary>
  324. /// Gets the address of the compute uniform buffer currently bound at the given index.
  325. /// </summary>
  326. /// <param name="index">Index of the uniform buffer binding</param>
  327. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  328. public ulong GetComputeUniformBufferAddress(int index)
  329. {
  330. return _cpUniformBuffers.Buffers[index].Address;
  331. }
  332. /// <summary>
  333. /// Gets the address of the graphics uniform buffer currently bound at the given index.
  334. /// </summary>
  335. /// <param name="stage">Index of the shader stage</param>
  336. /// <param name="index">Index of the uniform buffer binding</param>
  337. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  338. public ulong GetGraphicsUniformBufferAddress(int stage, int index)
  339. {
  340. return _gpUniformBuffers[stage].Buffers[index].Address;
  341. }
  342. /// <summary>
  343. /// Ensures that the compute engine bindings are visible to the host GPU.
  344. /// Note: this actually performs the binding using the host graphics API.
  345. /// </summary>
  346. public void CommitComputeBindings()
  347. {
  348. uint enableMask = _cpStorageBuffers.EnableMask;
  349. for (int index = 0; (enableMask >> index) != 0; index++)
  350. {
  351. if ((enableMask & (1u << index)) == 0)
  352. {
  353. continue;
  354. }
  355. BufferBounds bounds = _cpStorageBuffers.Buffers[index];
  356. if (bounds.Address == 0)
  357. {
  358. continue;
  359. }
  360. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  361. _context.Renderer.Pipeline.SetStorageBuffer(index, ShaderStage.Compute, buffer);
  362. }
  363. enableMask = _cpUniformBuffers.EnableMask;
  364. for (int index = 0; (enableMask >> index) != 0; index++)
  365. {
  366. if ((enableMask & (1u << index)) == 0)
  367. {
  368. continue;
  369. }
  370. BufferBounds bounds = _cpUniformBuffers.Buffers[index];
  371. if (bounds.Address == 0)
  372. {
  373. continue;
  374. }
  375. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  376. _context.Renderer.Pipeline.SetUniformBuffer(index, ShaderStage.Compute, buffer);
  377. }
  378. // Force rebind after doing compute work.
  379. _rebind = true;
  380. }
  381. /// <summary>
  382. /// Ensures that the graphics engine bindings are visible to the host GPU.
  383. /// Note: this actually performs the binding using the host graphics API.
  384. /// </summary>
  385. public void CommitGraphicsBindings()
  386. {
  387. if (_indexBufferDirty || _rebind)
  388. {
  389. _indexBufferDirty = false;
  390. if (_indexBuffer.Address != 0)
  391. {
  392. BufferRange buffer = GetBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  393. _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type);
  394. }
  395. }
  396. else if (_indexBuffer.Address != 0)
  397. {
  398. SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  399. }
  400. uint vbEnableMask = _vertexBuffersEnableMask;
  401. if (_vertexBuffersDirty || _rebind)
  402. {
  403. _vertexBuffersDirty = false;
  404. Span<VertexBufferDescriptor> vertexBuffers = stackalloc VertexBufferDescriptor[Constants.TotalVertexBuffers];
  405. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  406. {
  407. VertexBuffer vb = _vertexBuffers[index];
  408. if (vb.Address == 0)
  409. {
  410. continue;
  411. }
  412. BufferRange buffer = GetBufferRange(vb.Address, vb.Size);
  413. vertexBuffers[index] = new VertexBufferDescriptor(buffer, vb.Stride, vb.Divisor);
  414. }
  415. _context.Renderer.Pipeline.SetVertexBuffers(vertexBuffers);
  416. }
  417. else
  418. {
  419. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  420. {
  421. VertexBuffer vb = _vertexBuffers[index];
  422. if (vb.Address == 0)
  423. {
  424. continue;
  425. }
  426. SynchronizeBufferRange(vb.Address, vb.Size);
  427. }
  428. }
  429. if (_transformFeedbackBuffersDirty || _rebind)
  430. {
  431. _transformFeedbackBuffersDirty = false;
  432. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  433. {
  434. BufferBounds tfb = _transformFeedbackBuffers[index];
  435. if (tfb.Address == 0)
  436. {
  437. _context.Renderer.Pipeline.SetTransformFeedbackBuffer(index, new BufferRange(BufferHandle.Null, 0, 0));
  438. continue;
  439. }
  440. BufferRange buffer = GetBufferRange(tfb.Address, tfb.Size);
  441. _context.Renderer.Pipeline.SetTransformFeedbackBuffer(index, buffer);
  442. }
  443. }
  444. else
  445. {
  446. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  447. {
  448. BufferBounds tfb = _transformFeedbackBuffers[index];
  449. if (tfb.Address == 0)
  450. {
  451. continue;
  452. }
  453. SynchronizeBufferRange(tfb.Address, tfb.Size);
  454. }
  455. }
  456. if (_gpStorageBuffersDirty || _rebind)
  457. {
  458. _gpStorageBuffersDirty = false;
  459. BindBuffers(_gpStorageBuffers, isStorage: true);
  460. }
  461. else
  462. {
  463. UpdateBuffers(_gpStorageBuffers);
  464. }
  465. if (_gpUniformBuffersDirty || _rebind)
  466. {
  467. _gpUniformBuffersDirty = false;
  468. BindBuffers(_gpUniformBuffers, isStorage: false);
  469. }
  470. else
  471. {
  472. UpdateBuffers(_gpUniformBuffers);
  473. }
  474. _rebind = false;
  475. }
  476. /// <summary>
  477. /// Bind respective buffer bindings on the host API.
  478. /// </summary>
  479. /// <param name="bindings">Bindings to bind</param>
  480. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
  481. private void BindBuffers(BuffersPerStage[] bindings, bool isStorage)
  482. {
  483. BindOrUpdateBuffers(bindings, bind: true, isStorage);
  484. }
  485. /// <summary>
  486. /// Updates data for the already bound buffer bindings.
  487. /// </summary>
  488. /// <param name="bindings">Bindings to update</param>
  489. private void UpdateBuffers(BuffersPerStage[] bindings)
  490. {
  491. BindOrUpdateBuffers(bindings, bind: false);
  492. }
  493. /// <summary>
  494. /// This binds buffers into the host API, or updates data for already bound buffers.
  495. /// </summary>
  496. /// <param name="bindings">Bindings to bind or update</param>
  497. /// <param name="bind">True to bind, false to update</param>
  498. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  499. private void BindOrUpdateBuffers(BuffersPerStage[] bindings, bool bind, bool isStorage = false)
  500. {
  501. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  502. {
  503. uint enableMask = bindings[(int)stage - 1].EnableMask;
  504. if (enableMask == 0)
  505. {
  506. continue;
  507. }
  508. for (int index = 0; (enableMask >> index) != 0; index++)
  509. {
  510. if ((enableMask & (1u << index)) == 0)
  511. {
  512. continue;
  513. }
  514. BufferBounds bounds = bindings[(int)stage - 1].Buffers[index];
  515. if (bounds.Address == 0)
  516. {
  517. continue;
  518. }
  519. if (bind)
  520. {
  521. BindBuffer(index, stage, bounds, isStorage);
  522. }
  523. else
  524. {
  525. SynchronizeBufferRange(bounds.Address, bounds.Size);
  526. }
  527. }
  528. }
  529. }
  530. /// <summary>
  531. /// Binds a buffer on the host API.
  532. /// </summary>
  533. /// <param name="index">Index to bind the buffer into</param>
  534. /// <param name="stage">Shader stage to bind the buffer into</param>
  535. /// <param name="bounds">Buffer address and size</param>
  536. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  537. private void BindBuffer(int index, ShaderStage stage, BufferBounds bounds, bool isStorage)
  538. {
  539. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  540. if (isStorage)
  541. {
  542. _context.Renderer.Pipeline.SetStorageBuffer(index, stage, buffer);
  543. }
  544. else
  545. {
  546. _context.Renderer.Pipeline.SetUniformBuffer(index, stage, buffer);
  547. }
  548. }
  549. /// <summary>
  550. /// Sets the buffer storage of a buffer texture.
  551. /// </summary>
  552. /// <param name="texture">Buffer texture</param>
  553. /// <param name="address">Address of the buffer in memory</param>
  554. /// <param name="size">Size of the buffer in bytes</param>
  555. /// <param name="compute">Indicates if the buffer texture belongs to the compute or graphics pipeline</param>
  556. public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, bool compute)
  557. {
  558. CreateBuffer(address, size);
  559. if (_rebind)
  560. {
  561. // We probably had to modify existing buffers to create the texture buffer,
  562. // so rebind everything to ensure we're using the new buffers for all bound resources.
  563. if (compute)
  564. {
  565. CommitComputeBindings();
  566. }
  567. else
  568. {
  569. CommitGraphicsBindings();
  570. }
  571. }
  572. texture.SetStorage(GetBufferRange(address, size));
  573. }
  574. /// <summary>
  575. /// Copy a buffer data from a given address to another.
  576. /// </summary>
  577. /// <remarks>
  578. /// This does a GPU side copy.
  579. /// </remarks>
  580. /// <param name="srcVa">GPU virtual address of the copy source</param>
  581. /// <param name="dstVa">GPU virtual address of the copy destination</param>
  582. /// <param name="size">Size in bytes of the copy</param>
  583. public void CopyBuffer(GpuVa srcVa, GpuVa dstVa, ulong size)
  584. {
  585. ulong srcAddress = TranslateAndCreateBuffer(srcVa.Pack(), size);
  586. ulong dstAddress = TranslateAndCreateBuffer(dstVa.Pack(), size);
  587. Buffer srcBuffer = GetBuffer(srcAddress, size);
  588. Buffer dstBuffer = GetBuffer(dstAddress, size);
  589. int srcOffset = (int)(srcAddress - srcBuffer.Address);
  590. int dstOffset = (int)(dstAddress - dstBuffer.Address);
  591. _context.Renderer.Pipeline.CopyBuffer(
  592. srcBuffer.Handle,
  593. dstBuffer.Handle,
  594. srcOffset,
  595. dstOffset,
  596. (int)size);
  597. dstBuffer.Flush(dstAddress, size);
  598. }
  599. /// <summary>
  600. /// Gets a buffer sub-range for a given memory range.
  601. /// </summary>
  602. /// <param name="address">Start address of the memory range</param>
  603. /// <param name="size">Size in bytes of the memory range</param>
  604. /// <returns>The buffer sub-range for the given range</returns>
  605. private BufferRange GetBufferRange(ulong address, ulong size)
  606. {
  607. return GetBuffer(address, size).GetRange(address, size);
  608. }
  609. /// <summary>
  610. /// Gets a buffer for a given memory range.
  611. /// A buffer overlapping with the specified range is assumed to already exist on the cache.
  612. /// </summary>
  613. /// <param name="address">Start address of the memory range</param>
  614. /// <param name="size">Size in bytes of the memory range</param>
  615. /// <returns>The buffer where the range is fully contained</returns>
  616. private Buffer GetBuffer(ulong address, ulong size)
  617. {
  618. Buffer buffer;
  619. if (size != 0)
  620. {
  621. buffer = _buffers.FindFirstOverlap(address, size);
  622. buffer.SynchronizeMemory(address, size);
  623. }
  624. else
  625. {
  626. buffer = _buffers.FindFirstOverlap(address, 1);
  627. }
  628. return buffer;
  629. }
  630. /// <summary>
  631. /// Performs guest to host memory synchronization of a given memory range.
  632. /// </summary>
  633. /// <param name="address">Start address of the memory range</param>
  634. /// <param name="size">Size in bytes of the memory range</param>
  635. private void SynchronizeBufferRange(ulong address, ulong size)
  636. {
  637. if (size != 0)
  638. {
  639. Buffer buffer = _buffers.FindFirstOverlap(address, size);
  640. buffer.SynchronizeMemory(address, size);
  641. }
  642. }
  643. /// <summary>
  644. /// Disposes all buffers in the cache.
  645. /// It's an error to use the buffer manager after disposal.
  646. /// </summary>
  647. public void Dispose()
  648. {
  649. foreach (Buffer buffer in _buffers)
  650. {
  651. buffer.Dispose();
  652. }
  653. }
  654. }
  655. }