BufferManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. ulong endAddress = address + size;
  224. ulong alignedAddress = address & ~BufferAlignmentMask;
  225. ulong alignedEndAddress = (endAddress + BufferAlignmentMask) & ~BufferAlignmentMask;
  226. // The buffer must have the size of at least one page.
  227. if (alignedEndAddress == alignedAddress)
  228. {
  229. alignedEndAddress += BufferAlignmentSize;
  230. }
  231. CreateBuffer(alignedAddress, alignedEndAddress - alignedAddress);
  232. return address;
  233. }
  234. /// <summary>
  235. /// Creates a new buffer for the specified range, if needed.
  236. /// If a buffer where this range can be fully contained already exists,
  237. /// then the creation of a new buffer is not necessary.
  238. /// </summary>
  239. /// <param name="address">Address of the buffer in guest memory</param>
  240. /// <param name="size">Size in bytes of the buffer</param>
  241. private void CreateBuffer(ulong address, ulong size)
  242. {
  243. int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
  244. if (overlapsCount != 0)
  245. {
  246. // The buffer already exists. We can just return the existing buffer
  247. // if the buffer we need is fully contained inside the overlapping buffer.
  248. // Otherwise, we must delete the overlapping buffers and create a bigger buffer
  249. // that fits all the data we need. We also need to copy the contents from the
  250. // old buffer(s) to the new buffer.
  251. ulong endAddress = address + size;
  252. if (_bufferOverlaps[0].Address > address || _bufferOverlaps[0].EndAddress < endAddress)
  253. {
  254. for (int index = 0; index < overlapsCount; index++)
  255. {
  256. Buffer buffer = _bufferOverlaps[index];
  257. address = Math.Min(address, buffer.Address);
  258. endAddress = Math.Max(endAddress, buffer.EndAddress);
  259. buffer.SynchronizeMemory(buffer.Address, buffer.Size);
  260. _buffers.Remove(buffer);
  261. }
  262. Buffer newBuffer = new Buffer(_context, address, endAddress - address);
  263. _buffers.Add(newBuffer);
  264. for (int index = 0; index < overlapsCount; index++)
  265. {
  266. Buffer buffer = _bufferOverlaps[index];
  267. int dstOffset = (int)(buffer.Address - newBuffer.Address);
  268. buffer.CopyTo(newBuffer, dstOffset);
  269. buffer.Dispose();
  270. }
  271. _rebind = true;
  272. }
  273. }
  274. else
  275. {
  276. // No overlap, just create a new buffer.
  277. Buffer buffer = new Buffer(_context, address, size);
  278. _buffers.Add(buffer);
  279. }
  280. ShrinkOverlapsBufferIfNeeded();
  281. }
  282. /// <summary>
  283. /// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
  284. /// </summary>
  285. private void ShrinkOverlapsBufferIfNeeded()
  286. {
  287. if (_bufferOverlaps.Length > OverlapsBufferMaxCapacity)
  288. {
  289. Array.Resize(ref _bufferOverlaps, OverlapsBufferMaxCapacity);
  290. }
  291. }
  292. /// <summary>
  293. /// Gets the address of the compute uniform buffer currently bound at the given index.
  294. /// </summary>
  295. /// <param name="index">Index of the uniform buffer binding</param>
  296. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  297. public ulong GetComputeUniformBufferAddress(int index)
  298. {
  299. return _cpUniformBuffers.Buffers[index].Address;
  300. }
  301. /// <summary>
  302. /// Gets the address of the graphics uniform buffer currently bound at the given index.
  303. /// </summary>
  304. /// <param name="stage">Index of the shader stage</param>
  305. /// <param name="index">Index of the uniform buffer binding</param>
  306. /// <returns>The uniform buffer address, or an undefined value if the buffer is not currently bound</returns>
  307. public ulong GetGraphicsUniformBufferAddress(int stage, int index)
  308. {
  309. return _gpUniformBuffers[stage].Buffers[index].Address;
  310. }
  311. /// <summary>
  312. /// Ensures that the compute engine bindings are visible to the host GPU.
  313. /// Note: this actually performs the binding using the host graphics API.
  314. /// </summary>
  315. public void CommitComputeBindings()
  316. {
  317. uint enableMask = _cpStorageBuffers.EnableMask;
  318. for (int index = 0; (enableMask >> index) != 0; index++)
  319. {
  320. if ((enableMask & (1u << index)) == 0)
  321. {
  322. continue;
  323. }
  324. BufferBounds bounds = _cpStorageBuffers.Buffers[index];
  325. if (bounds.Address == 0)
  326. {
  327. continue;
  328. }
  329. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  330. _context.Renderer.Pipeline.SetStorageBuffer(index, ShaderStage.Compute, buffer);
  331. }
  332. enableMask = _cpUniformBuffers.EnableMask;
  333. for (int index = 0; (enableMask >> index) != 0; index++)
  334. {
  335. if ((enableMask & (1u << index)) == 0)
  336. {
  337. continue;
  338. }
  339. BufferBounds bounds = _cpUniformBuffers.Buffers[index];
  340. if (bounds.Address == 0)
  341. {
  342. continue;
  343. }
  344. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  345. _context.Renderer.Pipeline.SetUniformBuffer(index, ShaderStage.Compute, buffer);
  346. }
  347. // Force rebind after doing compute work.
  348. _rebind = true;
  349. }
  350. /// <summary>
  351. /// Ensures that the graphics engine bindings are visible to the host GPU.
  352. /// Note: this actually performs the binding using the host graphics API.
  353. /// </summary>
  354. public void CommitBindings()
  355. {
  356. if (_indexBufferDirty || _rebind)
  357. {
  358. _indexBufferDirty = false;
  359. if (_indexBuffer.Address != 0)
  360. {
  361. BufferRange buffer = GetBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  362. _context.Renderer.Pipeline.SetIndexBuffer(buffer, _indexBuffer.Type);
  363. }
  364. }
  365. else if (_indexBuffer.Address != 0)
  366. {
  367. SynchronizeBufferRange(_indexBuffer.Address, _indexBuffer.Size);
  368. }
  369. uint vbEnableMask = _vertexBuffersEnableMask;
  370. if (_vertexBuffersDirty || _rebind)
  371. {
  372. _vertexBuffersDirty = false;
  373. VertexBufferDescriptor[] vertexBuffers = new VertexBufferDescriptor[Constants.TotalVertexBuffers];
  374. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  375. {
  376. VertexBuffer vb = _vertexBuffers[index];
  377. if (vb.Address == 0)
  378. {
  379. continue;
  380. }
  381. BufferRange buffer = GetBufferRange(vb.Address, vb.Size);
  382. vertexBuffers[index] = new VertexBufferDescriptor(buffer, vb.Stride, vb.Divisor);
  383. }
  384. _context.Renderer.Pipeline.SetVertexBuffers(vertexBuffers);
  385. }
  386. else
  387. {
  388. for (int index = 0; (vbEnableMask >> index) != 0; index++)
  389. {
  390. VertexBuffer vb = _vertexBuffers[index];
  391. if (vb.Address == 0)
  392. {
  393. continue;
  394. }
  395. SynchronizeBufferRange(vb.Address, vb.Size);
  396. }
  397. }
  398. if (_gpStorageBuffersDirty || _rebind)
  399. {
  400. _gpStorageBuffersDirty = false;
  401. BindBuffers(_gpStorageBuffers, isStorage: true);
  402. }
  403. else
  404. {
  405. UpdateBuffers(_gpStorageBuffers);
  406. }
  407. if (_gpUniformBuffersDirty || _rebind)
  408. {
  409. _gpUniformBuffersDirty = false;
  410. BindBuffers(_gpUniformBuffers, isStorage: false);
  411. }
  412. else
  413. {
  414. UpdateBuffers(_gpUniformBuffers);
  415. }
  416. _rebind = false;
  417. }
  418. /// <summary>
  419. /// Bind respective buffer bindings on the host API.
  420. /// </summary>
  421. /// <param name="bindings">Bindings to bind</param>
  422. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffers</param>
  423. private void BindBuffers(BuffersPerStage[] bindings, bool isStorage)
  424. {
  425. BindOrUpdateBuffers(bindings, bind: true, isStorage);
  426. }
  427. /// <summary>
  428. /// Updates data for the already bound buffer bindings.
  429. /// </summary>
  430. /// <param name="bindings">Bindings to update</param>
  431. private void UpdateBuffers(BuffersPerStage[] bindings)
  432. {
  433. BindOrUpdateBuffers(bindings, bind: false);
  434. }
  435. /// <summary>
  436. /// This binds buffers into the host API, or updates data for already bound buffers.
  437. /// </summary>
  438. /// <param name="bindings">Bindings to bind or update</param>
  439. /// <param name="bind">True to bind, false to update</param>
  440. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  441. private void BindOrUpdateBuffers(BuffersPerStage[] bindings, bool bind, bool isStorage = false)
  442. {
  443. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  444. {
  445. uint enableMask = bindings[(int)stage - 1].EnableMask;
  446. if (enableMask == 0)
  447. {
  448. continue;
  449. }
  450. for (int index = 0; (enableMask >> index) != 0; index++)
  451. {
  452. if ((enableMask & (1u << index)) == 0)
  453. {
  454. continue;
  455. }
  456. BufferBounds bounds = bindings[(int)stage - 1].Buffers[index];
  457. if (bounds.Address == 0)
  458. {
  459. continue;
  460. }
  461. if (bind)
  462. {
  463. BindBuffer(index, stage, bounds, isStorage);
  464. }
  465. else
  466. {
  467. SynchronizeBufferRange(bounds.Address, bounds.Size);
  468. }
  469. }
  470. }
  471. }
  472. /// <summary>
  473. /// Binds a buffer on the host API.
  474. /// </summary>
  475. /// <param name="index">Index to bind the buffer into</param>
  476. /// <param name="stage">Shader stage to bind the buffer into</param>
  477. /// <param name="bounds">Buffer address and size</param>
  478. /// <param name="isStorage">True to bind as storage buffer, false to bind as uniform buffer</param>
  479. private void BindBuffer(int index, ShaderStage stage, BufferBounds bounds, bool isStorage)
  480. {
  481. BufferRange buffer = GetBufferRange(bounds.Address, bounds.Size);
  482. if (isStorage)
  483. {
  484. _context.Renderer.Pipeline.SetStorageBuffer(index, stage, buffer);
  485. }
  486. else
  487. {
  488. _context.Renderer.Pipeline.SetUniformBuffer(index, stage, buffer);
  489. }
  490. }
  491. /// <summary>
  492. /// Copy a buffer data from a given address to another.
  493. /// </summary>
  494. /// <remarks>
  495. /// This does a GPU side copy.
  496. /// </remarks>
  497. /// <param name="srcVa">GPU virtual address of the copy source</param>
  498. /// <param name="dstVa">GPU virtual address of the copy destination</param>
  499. /// <param name="size">Size in bytes of the copy</param>
  500. public void CopyBuffer(GpuVa srcVa, GpuVa dstVa, ulong size)
  501. {
  502. ulong srcAddress = TranslateAndCreateBuffer(srcVa.Pack(), size);
  503. ulong dstAddress = TranslateAndCreateBuffer(dstVa.Pack(), size);
  504. Buffer srcBuffer = GetBuffer(srcAddress, size);
  505. Buffer dstBuffer = GetBuffer(dstAddress, size);
  506. int srcOffset = (int)(srcAddress - srcBuffer.Address);
  507. int dstOffset = (int)(dstAddress - dstBuffer.Address);
  508. srcBuffer.HostBuffer.CopyTo(
  509. dstBuffer.HostBuffer,
  510. srcOffset,
  511. dstOffset,
  512. (int)size);
  513. dstBuffer.Flush(dstAddress, size);
  514. }
  515. /// <summary>
  516. /// Gets a buffer sub-range for a given memory range.
  517. /// </summary>
  518. /// <param name="address">Start address of the memory range</param>
  519. /// <param name="size">Size in bytes of the memory range</param>
  520. /// <returns>The buffer sub-range for the given range</returns>
  521. private BufferRange GetBufferRange(ulong address, ulong size)
  522. {
  523. return GetBuffer(address, size).GetRange(address, size);
  524. }
  525. /// <summary>
  526. /// Gets a buffer for a given memory range.
  527. /// A buffer overlapping with the specified range is assumed to already exist on the cache.
  528. /// </summary>
  529. /// <param name="address">Start address of the memory range</param>
  530. /// <param name="size">Size in bytes of the memory range</param>
  531. /// <returns>The buffer where the range is fully contained</returns>
  532. private Buffer GetBuffer(ulong address, ulong size)
  533. {
  534. Buffer buffer;
  535. if (size != 0)
  536. {
  537. buffer = _buffers.FindFirstOverlap(address, size);
  538. buffer.SynchronizeMemory(address, size);
  539. }
  540. else
  541. {
  542. buffer = _buffers.FindFirstOverlap(address, 1);
  543. }
  544. return buffer;
  545. }
  546. /// <summary>
  547. /// Performs guest to host memory synchronization of a given memory range.
  548. /// </summary>
  549. /// <param name="address">Start address of the memory range</param>
  550. /// <param name="size">Size in bytes of the memory range</param>
  551. private void SynchronizeBufferRange(ulong address, ulong size)
  552. {
  553. if (size != 0)
  554. {
  555. Buffer buffer = _buffers.FindFirstOverlap(address, size);
  556. buffer.SynchronizeMemory(address, size);
  557. }
  558. }
  559. /// <summary>
  560. /// Disposes all buffers in the cache.
  561. /// It's an error to use the buffer manager after disposal.
  562. /// </summary>
  563. public void Dispose()
  564. {
  565. foreach (Buffer buffer in _buffers)
  566. {
  567. buffer.Dispose();
  568. }
  569. }
  570. }
  571. }