BsVulkanResource.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  67. * Destroys the resource and frees its memory. If the resource is currently being used on a device, the
  68. * destruction is delayed until the device is done with it.
  69. */
  70. void destroy();
  71. protected:
  72. /** Possible states of this object. */
  73. enum class State
  74. {
  75. Normal,
  76. Shared,
  77. Destroyed
  78. };
  79. /** Information about use of this resource on a specific command buffer. */
  80. struct UseHandle
  81. {
  82. VulkanCmdBuffer* buffer;
  83. VulkanUseFlags flags;
  84. };
  85. VulkanResourceManager* mOwner;
  86. UINT32 mQueueFamily;
  87. State mState;
  88. VulkanResourceType mType;
  89. UseHandle* mHandles;
  90. UINT32 mNumHandles;
  91. UINT32 mHandleCapacity;
  92. static const UINT32 INITIAL_HANDLE_CAPACITY = 2;
  93. StaticAlloc<sizeof(UseHandle) * INITIAL_HANDLE_CAPACITY> mAlloc;
  94. };
  95. /** Creates and destroys annd VulkanResource%s on a single device. */
  96. class VulkanResourceManager
  97. {
  98. public:
  99. VulkanResourceManager(VulkanDevice& device);
  100. ~VulkanResourceManager();
  101. /**
  102. * Creates a new Vulkan resource of the specified type. User must call VulkanResource::destroy() when done using
  103. * the resource.
  104. */
  105. template<class Type, class... Args>
  106. Type* create(Args &&...args)
  107. {
  108. Type* resource = new (bs_alloc(sizeof(Type))) Type(this, std::forward<Args>(args)...);
  109. #if BS_DEBUG_MODE
  110. mResources.insert(resource);
  111. #endif
  112. return resource;
  113. }
  114. /** Returns the device that owns this manager. */
  115. VulkanDevice& getDevice() const { return mDevice; }
  116. private:
  117. friend VulkanResource;
  118. /**
  119. * Destroys a previously created Vulkan resource. Caller must ensure the resource is not currently being used
  120. * on the device.
  121. */
  122. void destroy(VulkanResource* resource);
  123. VulkanDevice& mDevice;
  124. #if BS_DEBUG_MODE
  125. UnorderedSet<VulkanResource*> mResources;
  126. #endif
  127. };
  128. /** @} */
  129. }