BufferManager.cs 28 KB

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