BufferManager.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. /// <summary>
  119. /// Sets a transform feedback buffer on the graphics pipeline.
  120. /// The output from the vertex transformation stages are written into the feedback buffer.
  121. /// </summary>
  122. /// <param name="index">Index of the transform feedback buffer</param>
  123. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  124. /// <param name="size">Size in bytes of the transform feedback buffer</param>
  125. public void SetTransformFeedbackBuffer(int index, ulong gpuVa, ulong size)
  126. {
  127. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  128. _transformFeedbackBuffers[index].Address = address;
  129. _transformFeedbackBuffers[index].Size = size;
  130. _transformFeedbackBuffersDirty = true;
  131. }
  132. /// <summary>
  133. /// Sets a storage buffer on the compute pipeline.
  134. /// Storage buffers can be read and written to on shaders.
  135. /// </summary>
  136. /// <param name="index">Index of the storage buffer</param>
  137. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  138. /// <param name="size">Size in bytes of the storage buffer</param>
  139. public void SetComputeStorageBuffer(int index, ulong gpuVa, ulong size)
  140. {
  141. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  142. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  143. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  144. _cpStorageBuffers.Bind(index, address, size);
  145. }
  146. /// <summary>
  147. /// Sets a storage buffer on the graphics pipeline.
  148. /// Storage buffers can be read and written to on shaders.
  149. /// </summary>
  150. /// <param name="stage">Index of the shader stage</param>
  151. /// <param name="index">Index of the storage buffer</param>
  152. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  153. /// <param name="size">Size in bytes of the storage buffer</param>
  154. public void SetGraphicsStorageBuffer(int stage, int index, ulong gpuVa, ulong size)
  155. {
  156. size += gpuVa & ((ulong)_context.Capabilities.StorageBufferOffsetAlignment - 1);
  157. gpuVa = BitUtils.AlignDown(gpuVa, _context.Capabilities.StorageBufferOffsetAlignment);
  158. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  159. if (_gpStorageBuffers[stage].Buffers[index].Address != address ||
  160. _gpStorageBuffers[stage].Buffers[index].Size != size)
  161. {
  162. _gpStorageBuffersDirty = true;
  163. }
  164. _gpStorageBuffers[stage].Bind(index, address, size);
  165. }
  166. /// <summary>
  167. /// Sets a uniform buffer on the compute pipeline.
  168. /// Uniform buffers are read-only from shaders, and have a small capacity.
  169. /// </summary>
  170. /// <param name="index">Index of the uniform buffer</param>
  171. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  172. /// <param name="size">Size in bytes of the storage buffer</param>
  173. public void SetComputeUniformBuffer(int index, ulong gpuVa, ulong size)
  174. {
  175. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  176. _cpUniformBuffers.Bind(index, address, size);
  177. }
  178. /// <summary>
  179. /// Sets a uniform buffer on the graphics pipeline.
  180. /// Uniform buffers are read-only from shaders, and have a small capacity.
  181. /// </summary>
  182. /// <param name="stage">Index of the shader stage</param>
  183. /// <param name="index">Index of the uniform buffer</param>
  184. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  185. /// <param name="size">Size in bytes of the storage buffer</param>
  186. public void SetGraphicsUniformBuffer(int stage, int index, ulong gpuVa, ulong size)
  187. {
  188. ulong address = TranslateAndCreateBuffer(gpuVa, size);
  189. _gpUniformBuffers[stage].Bind(index, address, size);
  190. _gpUniformBuffersDirty = true;
  191. }
  192. /// <summary>
  193. /// Sets the enabled storage buffers mask on the compute pipeline.
  194. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  195. /// </summary>
  196. /// <param name="mask">Buffer enable mask</param>
  197. public void SetComputeStorageBufferEnableMask(uint mask)
  198. {
  199. _cpStorageBuffers.EnableMask = mask;
  200. }
  201. /// <summary>
  202. /// Sets the enabled storage buffers mask on the graphics pipeline.
  203. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  204. /// </summary>
  205. /// <param name="stage">Index of the shader stage</param>
  206. /// <param name="mask">Buffer enable mask</param>
  207. public void SetGraphicsStorageBufferEnableMask(int stage, uint mask)
  208. {
  209. _gpStorageBuffers[stage].EnableMask = mask;
  210. _gpStorageBuffersDirty = true;
  211. }
  212. /// <summary>
  213. /// Sets the enabled uniform buffers mask on the compute pipeline.
  214. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  215. /// </summary>
  216. /// <param name="mask">Buffer enable mask</param>
  217. public void SetComputeUniformBufferEnableMask(uint mask)
  218. {
  219. _cpUniformBuffers.EnableMask = mask;
  220. }
  221. /// <summary>
  222. /// Gets a bit mask indicating which compute uniform buffers are currently bound.
  223. /// </summary>
  224. /// <returns>Mask where each bit set indicates a bound constant buffer</returns>
  225. public uint GetComputeUniformBufferUseMask()
  226. {
  227. uint mask = 0;
  228. for (int i = 0; i < _cpUniformBuffers.Buffers.Length; i++)
  229. {
  230. if (_cpUniformBuffers.Buffers[i].Address != 0)
  231. {
  232. mask |= 1u << i;
  233. }
  234. }
  235. return mask;
  236. }
  237. /// <summary>
  238. /// Sets the enabled uniform buffers mask on the graphics pipeline.
  239. /// Each bit set on the mask indicates that the respective buffer index is enabled.
  240. /// </summary>
  241. /// <param name="stage">Index of the shader stage</param>
  242. /// <param name="mask">Buffer enable mask</param>
  243. public void SetGraphicsUniformBufferEnableMask(int stage, uint mask)
  244. {
  245. _gpUniformBuffers[stage].EnableMask = mask;
  246. _gpUniformBuffersDirty = true;
  247. }
  248. /// <summary>
  249. /// Gets a bit mask indicating which graphics uniform buffers are currently bound.
  250. /// </summary>
  251. /// <param name="stage">Index of the shader stage</param>
  252. /// <returns>Mask where each bit set indicates a bound constant buffer</returns>
  253. public uint GetGraphicsUniformBufferUseMask(int stage)
  254. {
  255. uint mask = 0;
  256. for (int i = 0; i < _gpUniformBuffers[stage].Buffers.Length; i++)
  257. {
  258. if (_gpUniformBuffers[stage].Buffers[i].Address != 0)
  259. {
  260. mask |= 1u << i;
  261. }
  262. }
  263. return mask;
  264. }
  265. /// <summary>
  266. /// Performs address translation of the GPU virtual address, and creates a
  267. /// new buffer, if needed, for the specified range.
  268. /// </summary>
  269. /// <param name="gpuVa">Start GPU virtual address of the buffer</param>
  270. /// <param name="size">Size in bytes of the buffer</param>
  271. /// <returns>CPU virtual address of the buffer, after address translation</returns>
  272. private ulong TranslateAndCreateBuffer(ulong gpuVa, ulong size)
  273. {
  274. if (gpuVa == 0)
  275. {
  276. return 0;
  277. }
  278. ulong address = _context.MemoryManager.Translate(gpuVa);
  279. if (address == MemoryManager.BadAddress)
  280. {
  281. return 0;
  282. }
  283. CreateBuffer(address, size);
  284. return address;
  285. }
  286. /// <summary>
  287. /// Creates a new buffer for the specified range, if it does not yet exist.
  288. /// This can be used to ensure the existance of a buffer.
  289. /// </summary>
  290. /// <param name="address">Address of the buffer in memory</param>
  291. /// <param name="size">Size of the buffer in bytes</param>
  292. public void CreateBuffer(ulong address, ulong size)
  293. {
  294. ulong endAddress = address + size;
  295. ulong alignedAddress = address & ~BufferAlignmentMask;
  296. ulong alignedEndAddress = (endAddress + BufferAlignmentMask) & ~BufferAlignmentMask;
  297. // The buffer must have the size of at least one page.
  298. if (alignedEndAddress == alignedAddress)
  299. {
  300. alignedEndAddress += BufferAlignmentSize;
  301. }
  302. CreateBufferAligned(alignedAddress, alignedEndAddress - alignedAddress);
  303. }
  304. /// <summary>
  305. /// Creates a new buffer for the specified range, if needed.
  306. /// If a buffer where this range can be fully contained already exists,
  307. /// then the creation of a new buffer is not necessary.
  308. /// </summary>
  309. /// <param name="address">Address of the buffer in guest memory</param>
  310. /// <param name="size">Size in bytes of the buffer</param>
  311. private void CreateBufferAligned(ulong address, ulong size)
  312. {
  313. int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  314. if (overlapsCount != 0)
  315. {
  316. // The buffer already exists. We can just return the existing buffer
  317. // if the buffer we need is fully contained inside the overlapping buffer.
  318. // Otherwise, we must delete the overlapping buffers and create a bigger buffer
  319. // that fits all the data we need. We also need to copy the contents from the
  320. // old buffer(s) to the new buffer.
  321. ulong endAddress = address + size;
  322. if (_bufferOverlaps[0].Address > address || _bufferOverlaps[0].EndAddress < endAddress)
  323. {
  324. for (int index = 0; index < overlapsCount; index++)
  325. {
  326. Buffer buffer = _bufferOverlaps[index];
  327. address = Math.Min(address, buffer.Address);
  328. endAddress = Math.Max(endAddress, buffer.EndAddress);
  329. buffer.SynchronizeMemory(buffer.Address, buffer.Size);
  330. _buffers.Remove(buffer);
  331. }
  332. Buffer newBuffer = new Buffer(_context, address, endAddress - address);
  333. _buffers.Add(newBuffer);
  334. for (int index = 0; index < overlapsCount; index++)
  335. {
  336. Buffer buffer = _bufferOverlaps[index];
  337. int dstOffset = (int)(buffer.Address - newBuffer.Address);
  338. buffer.CopyTo(newBuffer, dstOffset);
  339. buffer.Dispose();
  340. }
  341. // Existing buffers were modified, we need to rebind everything.
  342. _rebind = true;
  343. }
  344. }
  345. else
  346. {
  347. // No overlap, just create a new buffer.
  348. Buffer buffer = new Buffer(_context, address, size);
  349. _buffers.Add(buffer);
  350. }
  351. ShrinkOverlapsBufferIfNeeded();
  352. }
  353. /// <summary>
  354. /// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
  355. /// </summary>
  356. private void ShrinkOverlapsBufferIfNeeded()
  357. {
  358. if (_bufferOverlaps.Length > OverlapsBufferMaxCapacity)
  359. {
  360. Array.Resize(ref _bufferOverlaps, OverlapsBufferMaxCapacity);
  361. }
  362. }
  363. /// <summary>
  364. /// Gets the address of the compute uniform buffer currently bound at the given index.
  365. /// </summary>
  366. /// <param name="index">Index of the uniform buffer binding</param>
  367. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  368. public ulong GetComputeUniformBufferAddress(int index)
  369. {
  370. return _cpUniformBuffers.Buffers[index].Address;
  371. }
  372. /// <summary>
  373. /// Gets the address of the graphics uniform buffer currently bound at the given index.
  374. /// </summary>
  375. /// <param name="stage">Index of the shader stage</param>
  376. /// <param name="index">Index of the uniform buffer binding</param>
  377. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  378. public ulong GetGraphicsUniformBufferAddress(int stage, int index)
  379. {
  380. return _gpUniformBuffers[stage].Buffers[index].Address;
  381. }
  382. /// <summary>
  383. /// Ensures that the compute engine bindings are visible to the host GPU.
  384. /// Note: this actually performs the binding using the host graphics API.
  385. /// </summary>
  386. public void CommitComputeBindings()
  387. {
  388. uint enableMask = _cpStorageBuffers.EnableMask;
  389. for (int index = 0; (enableMask >> index) != 0; index++)
  390. {
  391. if ((enableMask & (1u << index)) == 0)
  392. {
  393. continue;
  394. }
  395. BufferBounds bounds = _cpStorageBuffers.Buffers[index];
  396. if (bounds.Address == 0)
  397. {
  398. continue;
  399. }
  400. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  401. _context.Renderer.Pipeline.SetStorageBuffer(index, ShaderStage.Compute, buffer);
  402. }
  403. enableMask = _cpUniformBuffers.EnableMask;
  404. for (int index = 0; (enableMask >> index) != 0; index++)
  405. {
  406. if ((enableMask & (1u << index)) == 0)
  407. {
  408. continue;
  409. }
  410. BufferBounds bounds = _cpUniformBuffers.Buffers[index];
  411. if (bounds.Address == 0)
  412. {
  413. continue;
  414. }
  415. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  416. _context.Renderer.Pipeline.SetUniformBuffer(index, ShaderStage.Compute, buffer);
  417. }
  418. // Force rebind after doing compute work.
  419. _rebind = true;
  420. }
  421. /// <summary>
  422. /// Ensures that the graphics engine bindings are visible to the host GPU.
  423. /// Note: this actually performs the binding using the host graphics API.
  424. /// </summary>
  425. public void CommitGraphicsBindings()
  426. {
  427. if (_indexBufferDirty || _rebind)
  428. {
  429. _indexBufferDirty = false;
  430. if (_indexBuffer.Address != 0)
  431. {
  432. BufferRange buffer = GetBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  433. _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type);
  434. }
  435. }
  436. else if (_indexBuffer.Address != 0)
  437. {
  438. SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  439. }
  440. uint vbEnableMask = _vertexBuffersEnableMask;
  441. if (_vertexBuffersDirty || _rebind)
  442. {
  443. _vertexBuffersDirty = false;
  444. Span<VertexBufferDescriptor> vertexBuffers = stackalloc VertexBufferDescriptor[Constants.TotalVertexBuffers];
  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. BufferRange buffer = GetBufferRange(vb.Address, vb.Size);
  453. vertexBuffers[index] = new VertexBufferDescriptor(buffer, vb.Stride, vb.Divisor);
  454. }
  455. _context.Renderer.Pipeline.SetVertexBuffers(vertexBuffers);
  456. }
  457. else
  458. {
  459. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  460. {
  461. VertexBuffer vb = _vertexBuffers[index];
  462. if (vb.Address == 0)
  463. {
  464. continue;
  465. }
  466. SynchronizeBufferRange(vb.Address, vb.Size);
  467. }
  468. }
  469. if (_transformFeedbackBuffersDirty || _rebind)
  470. {
  471. _transformFeedbackBuffersDirty = false;
  472. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  473. {
  474. BufferBounds tfb = _transformFeedbackBuffers[index];
  475. if (tfb.Address == 0)
  476. {
  477. _context.Renderer.Pipeline.SetTransformFeedbackBuffer(index, new BufferRange(BufferHandle.Null, 0, 0));
  478. continue;
  479. }
  480. BufferRange buffer = GetBufferRange(tfb.Address, tfb.Size);
  481. _context.Renderer.Pipeline.SetTransformFeedbackBuffer(index, buffer);
  482. }
  483. }
  484. else
  485. {
  486. for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
  487. {
  488. BufferBounds tfb = _transformFeedbackBuffers[index];
  489. if (tfb.Address == 0)
  490. {
  491. continue;
  492. }
  493. SynchronizeBufferRange(tfb.Address, tfb.Size);
  494. }
  495. }
  496. if (_gpStorageBuffersDirty || _rebind)
  497. {
  498. _gpStorageBuffersDirty = false;
  499. BindBuffers(_gpStorageBuffers, isStorage: true);
  500. }
  501. else
  502. {
  503. UpdateBuffers(_gpStorageBuffers);
  504. }
  505. if (_gpUniformBuffersDirty || _rebind)
  506. {
  507. _gpUniformBuffersDirty = false;
  508. BindBuffers(_gpUniformBuffers, isStorage: false);
  509. }
  510. else
  511. {
  512. UpdateBuffers(_gpUniformBuffers);
  513. }
  514. _rebind = false;
  515. }
  516. /// <summary>
  517. /// Bind respective buffer bindings on the host API.
  518. /// </summary>
  519. /// <param name="bindings">Bindings to bind</param>
  520. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
  521. private void BindBuffers(BuffersPerStage[] bindings, bool isStorage)
  522. {
  523. BindOrUpdateBuffers(bindings, bind: true, isStorage);
  524. }
  525. /// <summary>
  526. /// Updates data for the already bound buffer bindings.
  527. /// </summary>
  528. /// <param name="bindings">Bindings to update</param>
  529. private void UpdateBuffers(BuffersPerStage[] bindings)
  530. {
  531. BindOrUpdateBuffers(bindings, bind: false);
  532. }
  533. /// <summary>
  534. /// This binds buffers into the host API, or updates data for already bound buffers.
  535. /// </summary>
  536. /// <param name="bindings">Bindings to bind or update</param>
  537. /// <param name="bind">True to bind, false to update</param>
  538. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  539. private void BindOrUpdateBuffers(BuffersPerStage[] bindings, bool bind, bool isStorage = false)
  540. {
  541. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  542. {
  543. uint enableMask = bindings[(int)stage - 1].EnableMask;
  544. if (enableMask == 0)
  545. {
  546. continue;
  547. }
  548. for (int index = 0; (enableMask >> index) != 0; index++)
  549. {
  550. if ((enableMask & (1u << index)) == 0)
  551. {
  552. continue;
  553. }
  554. BufferBounds bounds = bindings[(int)stage - 1].Buffers[index];
  555. if (bounds.Address == 0)
  556. {
  557. continue;
  558. }
  559. if (bind)
  560. {
  561. BindBuffer(index, stage, bounds, isStorage);
  562. }
  563. else
  564. {
  565. SynchronizeBufferRange(bounds.Address, bounds.Size);
  566. }
  567. }
  568. }
  569. }
  570. /// <summary>
  571. /// Binds a buffer on the host API.
  572. /// </summary>
  573. /// <param name="index">Index to bind the buffer into</param>
  574. /// <param name="stage">Shader stage to bind the buffer into</param>
  575. /// <param name="bounds">Buffer address and size</param>
  576. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  577. private void BindBuffer(int index, ShaderStage stage, BufferBounds bounds, bool isStorage)
  578. {
  579. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  580. if (isStorage)
  581. {
  582. _context.Renderer.Pipeline.SetStorageBuffer(index, stage, buffer);
  583. }
  584. else
  585. {
  586. _context.Renderer.Pipeline.SetUniformBuffer(index, stage, buffer);
  587. }
  588. }
  589. /// <summary>
  590. /// Sets the buffer storage of a buffer texture.
  591. /// </summary>
  592. /// <param name="texture">Buffer texture</param>
  593. /// <param name="address">Address of the buffer in memory</param>
  594. /// <param name="size">Size of the buffer in bytes</param>
  595. /// <param name="compute">Indicates if the buffer texture belongs to the compute or graphics pipeline</param>
  596. public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, bool compute)
  597. {
  598. CreateBuffer(address, size);
  599. if (_rebind)
  600. {
  601. // We probably had to modify existing buffers to create the texture buffer,
  602. // so rebind everything to ensure we're using the new buffers for all bound resources.
  603. if (compute)
  604. {
  605. CommitComputeBindings();
  606. }
  607. else
  608. {
  609. CommitGraphicsBindings();
  610. }
  611. }
  612. texture.SetStorage(GetBufferRange(address, size));
  613. }
  614. /// <summary>
  615. /// Copy a buffer data from a given address to another.
  616. /// </summary>
  617. /// <remarks>
  618. /// This does a GPU side copy.
  619. /// </remarks>
  620. /// <param name="srcVa">GPU virtual address of the copy source</param>
  621. /// <param name="dstVa">GPU virtual address of the copy destination</param>
  622. /// <param name="size">Size in bytes of the copy</param>
  623. public void CopyBuffer(GpuVa srcVa, GpuVa dstVa, ulong size)
  624. {
  625. ulong srcAddress = TranslateAndCreateBuffer(srcVa.Pack(), size);
  626. ulong dstAddress = TranslateAndCreateBuffer(dstVa.Pack(), size);
  627. Buffer srcBuffer = GetBuffer(srcAddress, size);
  628. Buffer dstBuffer = GetBuffer(dstAddress, size);
  629. int srcOffset = (int)(srcAddress - srcBuffer.Address);
  630. int dstOffset = (int)(dstAddress - dstBuffer.Address);
  631. _context.Renderer.Pipeline.CopyBuffer(
  632. srcBuffer.Handle,
  633. dstBuffer.Handle,
  634. srcOffset,
  635. dstOffset,
  636. (int)size);
  637. dstBuffer.Flush(dstAddress, size);
  638. }
  639. /// <summary>
  640. /// Gets a buffer sub-range for a given memory range.
  641. /// </summary>
  642. /// <param name="address">Start address of the memory range</param>
  643. /// <param name="size">Size in bytes of the memory range</param>
  644. /// <returns>The buffer sub-range for the given range</returns>
  645. private BufferRange GetBufferRange(ulong address, ulong size)
  646. {
  647. return GetBuffer(address, size).GetRange(address, size);
  648. }
  649. /// <summary>
  650. /// Gets a buffer for a given memory range.
  651. /// A buffer overlapping with the specified range is assumed to already exist on the cache.
  652. /// </summary>
  653. /// <param name="address">Start address of the memory range</param>
  654. /// <param name="size">Size in bytes of the memory range</param>
  655. /// <returns>The buffer where the range is fully contained</returns>
  656. private Buffer GetBuffer(ulong address, ulong size)
  657. {
  658. Buffer buffer;
  659. if (size != 0)
  660. {
  661. buffer = _buffers.FindFirstOverlap(address, size);
  662. buffer.SynchronizeMemory(address, size);
  663. }
  664. else
  665. {
  666. buffer = _buffers.FindFirstOverlap(address, 1);
  667. }
  668. return buffer;
  669. }
  670. /// <summary>
  671. /// Performs guest to host memory synchronization of a given memory range.
  672. /// </summary>
  673. /// <param name="address">Start address of the memory range</param>
  674. /// <param name="size">Size in bytes of the memory range</param>
  675. private void SynchronizeBufferRange(ulong address, ulong size)
  676. {
  677. if (size != 0)
  678. {
  679. Buffer buffer = _buffers.FindFirstOverlap(address, size);
  680. buffer.SynchronizeMemory(address, size);
  681. }
  682. }
  683. /// <summary>
  684. /// Disposes all buffers in the cache.
  685. /// It's an error to use the buffer manager after disposal.
  686. /// </summary>
  687. public void Dispose()
  688. {
  689. foreach (Buffer buffer in _buffers)
  690. {
  691. buffer.Dispose();
  692. }
  693. }
  694. }
  695. }