BufferUsageBitmap.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. namespace Ryujinx.Graphics.Vulkan
  2. {
  3. internal class BufferUsageBitmap
  4. {
  5. private BitMap _bitmap;
  6. private int _size;
  7. private int _granularity;
  8. private int _bits;
  9. private int _intsPerCb;
  10. private int _bitsPerCb;
  11. public BufferUsageBitmap(int size, int granularity)
  12. {
  13. _size = size;
  14. _granularity = granularity;
  15. _bits = (size + (granularity - 1)) / granularity;
  16. _intsPerCb = (_bits + (BitMap.IntSize - 1)) / BitMap.IntSize;
  17. _bitsPerCb = _intsPerCb * BitMap.IntSize;
  18. _bitmap = new BitMap(_bitsPerCb * CommandBufferPool.MaxCommandBuffers);
  19. }
  20. public void Add(int cbIndex, int offset, int size)
  21. {
  22. if (size == 0)
  23. {
  24. return;
  25. }
  26. // Some usages can be out of bounds (vertex buffer on amd), so bound if necessary.
  27. if (offset + size > _size)
  28. {
  29. size = _size - offset;
  30. }
  31. int cbBase = cbIndex * _bitsPerCb;
  32. int start = cbBase + offset / _granularity;
  33. int end = cbBase + (offset + size - 1) / _granularity;
  34. _bitmap.SetRange(start, end);
  35. }
  36. public bool OverlapsWith(int cbIndex, int offset, int size)
  37. {
  38. if (size == 0)
  39. {
  40. return false;
  41. }
  42. int cbBase = cbIndex * _bitsPerCb;
  43. int start = cbBase + offset / _granularity;
  44. int end = cbBase + (offset + size - 1) / _granularity;
  45. return _bitmap.IsSet(start, end);
  46. }
  47. public bool OverlapsWith(int offset, int size)
  48. {
  49. for (int i = 0; i < CommandBufferPool.MaxCommandBuffers; i++)
  50. {
  51. if (OverlapsWith(i, offset, size))
  52. {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public void Clear(int cbIndex)
  59. {
  60. _bitmap.ClearInt(cbIndex * _intsPerCb, (cbIndex + 1) * _intsPerCb - 1);
  61. }
  62. }
  63. }