GrVkMemoryAllocator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrVkMemoryAllocator_DEFINED
  8. #define GrVkMemoryAllocator_DEFINED
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/gpu/GrTypes.h"
  11. #include "include/gpu/vk/GrVkTypes.h"
  12. class GrVkMemoryAllocator : public SkRefCnt {
  13. public:
  14. enum class AllocationPropertyFlags {
  15. kNone = 0,
  16. // Allocation will be placed in its own VkDeviceMemory and not suballocated from some larger
  17. // block.
  18. kDedicatedAllocation = 0x1,
  19. // Says that the backing memory can only be accessed by the device. Additionally the device
  20. // may lazily allocate the memory. This cannot be used with buffers that will be host
  21. // visible. Setting this flag does not guarantee that we will allocate memory that respects
  22. // it, but we will try to prefer memory that can respect it.
  23. kLazyAllocation = 0x2,
  24. // The allocation will be mapped immediately and stay mapped until it is destroyed. This
  25. // flag is only valid for buffers which are host visible (i.e. must have a usage other than
  26. // BufferUsage::kGpuOnly).
  27. kPersistentlyMapped = 0x4,
  28. // Allocation can only be accessed by the device using a protected context.
  29. kProtected = 0x8,
  30. };
  31. GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AllocationPropertyFlags);
  32. enum class BufferUsage {
  33. // Buffers that will only be accessed from the device (large const buffers). Will always be
  34. // in device local memory.
  35. kGpuOnly,
  36. // Buffers that will be accessed on the host and copied to and from a GPU resource (transfer
  37. // buffers). Will always be mappable and coherent memory.
  38. kCpuOnly,
  39. // Buffers that typically will be updated multiple times by the host and read on the gpu
  40. // (e.g. uniform or vertex buffers). Will always be mappable memory, and will prefer to be
  41. // in device local memory.
  42. kCpuWritesGpuReads,
  43. // Buffers which are typically writted to by the GPU and then read on the host. Will always
  44. // be mappable memory, and will prefer coherent and cached memory.
  45. kGpuWritesCpuReads,
  46. };
  47. virtual bool allocateMemoryForImage(VkImage image, AllocationPropertyFlags flags,
  48. GrVkBackendMemory*) = 0;
  49. virtual bool allocateMemoryForBuffer(VkBuffer buffer, BufferUsage usage,
  50. AllocationPropertyFlags flags, GrVkBackendMemory*) = 0;
  51. // Fills out the passed in GrVkAlloc struct for the passed in GrVkBackendMemory.
  52. virtual void getAllocInfo(const GrVkBackendMemory&, GrVkAlloc*) const = 0;
  53. // Maps the entire allocation and returns a pointer to the start of the allocation. The
  54. // implementation may map more memory than just the allocation, but the returned pointer must
  55. // point at the start of the memory for the requested allocation.
  56. virtual void* mapMemory(const GrVkBackendMemory&) = 0;
  57. virtual void unmapMemory(const GrVkBackendMemory&) = 0;
  58. // The following two calls are used for managing non-coherent memory. The offset is relative to
  59. // the start of the allocation and not the underlying VkDeviceMemory. Additionaly the client
  60. // must make sure that the offset + size passed in is less that or equal to the allocation size.
  61. // It is the responsibility of the implementation to make sure all alignment requirements are
  62. // followed. The client should not have to deal with any sort of alignment issues.
  63. virtual void flushMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset,
  64. VkDeviceSize size) = 0;
  65. virtual void invalidateMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset,
  66. VkDeviceSize size)= 0;
  67. virtual void freeMemory(const GrVkBackendMemory&) = 0;
  68. // Returns the total amount of memory that is allocated and in use by an allocation for this
  69. // allocator.
  70. virtual uint64_t totalUsedMemory() const = 0;
  71. // Returns the total amount of memory that is allocated by this allocator.
  72. virtual uint64_t totalAllocatedMemory() const = 0;
  73. };
  74. GR_MAKE_BITFIELD_CLASS_OPS(GrVkMemoryAllocator::AllocationPropertyFlags)
  75. #endif