BufferUsageBitmap.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Some usages can be out of bounds (vertex buffer on amd), so bound if necessary.
  23. if (offset + size > _size)
  24. {
  25. size = _size - offset;
  26. }
  27. int cbBase = cbIndex * _bitsPerCb;
  28. int start = cbBase + offset / _granularity;
  29. int end = cbBase + (offset + size - 1) / _granularity;
  30. _bitmap.SetRange(start, end);
  31. }
  32. public bool OverlapsWith(int cbIndex, int offset, int size)
  33. {
  34. int cbBase = cbIndex * _bitsPerCb;
  35. int start = cbBase + offset / _granularity;
  36. int end = cbBase + (offset + size - 1) / _granularity;
  37. return _bitmap.IsSet(start, end);
  38. }
  39. public bool OverlapsWith(int offset, int size)
  40. {
  41. for (int i = 0; i < CommandBufferPool.MaxCommandBuffers; i++)
  42. {
  43. if (OverlapsWith(i, offset, size))
  44. {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. public void Clear(int cbIndex)
  51. {
  52. _bitmap.ClearInt(cbIndex * _intsPerCb, (cbIndex + 1) * _intsPerCb - 1);
  53. }
  54. }
  55. }