BufferManager.cs 27 KB

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