BsVulkanResource.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsVulkanPrerequisites.h"
  5. #include "BsStaticAlloc.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Vulkan
  9. * @{
  10. */
  11. /** Flags that determine how is a resource being used by the GPU. */
  12. enum class VulkanUseFlag
  13. {
  14. None = 0,
  15. Read = 0x1,
  16. Write = 0x2
  17. };
  18. class VulkanResourceManager;
  19. typedef Flags<VulkanUseFlag> VulkanUseFlags;
  20. BS_FLAGS_OPERATORS(VulkanUseFlag);
  21. /** Types of VulkanResource. */
  22. enum class VulkanResourceType
  23. {
  24. Generic,
  25. Image,
  26. Buffer
  27. };
  28. /**
  29. * Wraps one or multiple native Vulkan objects. Allows the object usage to be tracked in command buffers, handles
  30. * ownership transitions between different queues, and handles delayed object destruction.
  31. */
  32. class VulkanResource
  33. {
  34. public:
  35. VulkanResource(VulkanResourceManager* owner, bool concurrency, VulkanResourceType type = VulkanResourceType::Generic);
  36. virtual ~VulkanResource();
  37. /**
  38. * Notifies the resource that it is currently being used on the provided command buffer. This means the command
  39. * buffer has actually been submitted to the queue and the resource is used by the GPU.
  40. *
  41. * A resource can only be used by a single command buffer at a time.
  42. */
  43. virtual void notifyUsed(VulkanCmdBuffer* buffer, VulkanUseFlags flags);
  44. /**
  45. * Notifies the resource that it is no longer used by on the GPU. This makes the resource usable on other command
  46. * buffers again.
  47. */
  48. virtual void notifyDone(VulkanCmdBuffer* buffer);
  49. /**
  50. * Checks is the resource currently used on a device.
  51. *
  52. * @note Resource usage is only checked at certain points of the program. This means the resource could be
  53. * done on the device but this method may still report true. If you need to know the latest state
  54. * call VulkanCommandBufferManager::refreshStates() before checking for usage.
  55. */
  56. bool isUsed() const { return mNumHandles > 0; }
  57. /** Returns the type of the object wrapped by the resource. */
  58. VulkanResourceType getType() const { return mType; }
  59. /**
  60. * Returns the queue family the resource is currently owned by. Returns -1 if owned by no queue.
  61. *
  62. * @note If resource concurrency is enabled, then this value has no meaning as the resource can be used on
  63. * multiple queue families at once.
  64. */
  65. UINT32 getQueueFamily() const { return mQueueFamily; }
  66. /** Returns true if the resource is only allowed to be used by a single queue family at once. */
  67. bool isExclusive() const { return mState != State::Shared; }
  68. /**
  69. * Destroys the resource and frees its memory. If the resource is currently being used on a device, the
  70. * destruction is delayed until the device is done with it.
  71. */
  72. void destroy();
  73. protected:
  74. /** Possible states of this object. */
  75. enum class State
  76. {
  77. Normal,
  78. Shared,
  79. Destroyed
  80. };
  81. /** Information about use of this resource on a specific command buffer. */
  82. struct UseHandle
  83. {
  84. VulkanCmdBuffer* buffer;
  85. VulkanUseFlags flags;
  86. };
  87. VulkanResourceManager* mOwner;
  88. UINT32 mQueueFamily;
  89. State mState;
  90. VulkanResourceType mType;
  91. UseHandle* mHandles;
  92. UINT32 mNumHandles;
  93. UINT32 mHandleCapacity;
  94. static const UINT32 INITIAL_HANDLE_CAPACITY = 2;
  95. StaticAlloc<sizeof(UseHandle) * INITIAL_HANDLE_CAPACITY> mAlloc;
  96. };
  97. /** Creates and destroys annd VulkanResource%s on a single device. */
  98. class VulkanResourceManager
  99. {
  100. public:
  101. VulkanResourceManager(VulkanDevice& device);
  102. ~VulkanResourceManager();
  103. /**
  104. * Creates a new Vulkan resource of the specified type. User must call VulkanResource::destroy() when done using
  105. * the resource.
  106. */
  107. template<class Type, class... Args>
  108. Type* create(Args &&...args)
  109. {
  110. Type* resource = new (bs_alloc(sizeof(Type))) Type(this, std::forward<Args>(args)...);
  111. #if BS_DEBUG_MODE
  112. mResources.insert(resource);
  113. #endif
  114. return resource;
  115. }
  116. /** Returns the device that owns this manager. */
  117. VulkanDevice& getDevice() const { return mDevice; }
  118. private:
  119. friend VulkanResource;
  120. /**
  121. * Destroys a previously created Vulkan resource. Caller must ensure the resource is not currently being used
  122. * on the device.
  123. */
  124. void destroy(VulkanResource* resource);
  125. VulkanDevice& mDevice;
  126. #if BS_DEBUG_MODE
  127. UnorderedSet<VulkanResource*> mResources;
  128. #endif
  129. };
  130. /** @} */
  131. }