BsVulkanResource.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  22. * Wraps one or multiple native Vulkan objects. Allows the object usage to be tracked in command buffers, handles
  23. * ownership transitions between different queues, and handles delayed object destruction.
  24. */
  25. class VulkanResource
  26. {
  27. public:
  28. VulkanResource(VulkanResourceManager* owner, bool concurrency);
  29. virtual ~VulkanResource();
  30. /**
  31. * Notifies the resource that it is currently being used on the provided command buffer. This means the command
  32. * buffer has actually been submitted to the queue and the resource is used by the GPU.
  33. *
  34. * A resource can only be used by a single command buffer at a time.
  35. */
  36. virtual void notifyUsed(VulkanCmdBuffer* buffer, VulkanUseFlags flags);
  37. /**
  38. * Notifies the resource that it is no longer used by on the GPU. This makes the resource usable on other command
  39. * buffers again.
  40. */
  41. virtual void notifyDone(VulkanCmdBuffer* buffer);
  42. /**
  43. * Checks is the resource currently used on a device.
  44. *
  45. * @note Resource usage is only checked at certain points of the program. This means the resource could be
  46. * done on the device but this method may still report true. If you need to know the latest state
  47. * call VulkanCommandBufferManager::refreshStates() before checking for usage.
  48. */
  49. bool isUsed() const { return mNumHandles > 0; }
  50. /**
  51. * Destroys the resource and frees its memory. If the resource is currently being used on a device, the
  52. * destruction is delayed until the device is done with it.
  53. */
  54. void destroy();
  55. protected:
  56. /** Possible states of this object. */
  57. enum class State
  58. {
  59. Normal,
  60. Shared,
  61. Destroyed
  62. };
  63. /** Information about use of this resource on a specific command buffer. */
  64. struct UseHandle
  65. {
  66. VulkanCmdBuffer* buffer;
  67. VulkanUseFlags flags;
  68. };
  69. VulkanResourceManager* mOwner;
  70. UINT32 mQueueFamily;
  71. State mState;
  72. UseHandle* mHandles;
  73. UINT32 mNumHandles;
  74. UINT32 mHandleCapacity;
  75. static const UINT32 INITIAL_HANDLE_CAPACITY = 2;
  76. StaticAlloc<sizeof(UseHandle) * INITIAL_HANDLE_CAPACITY> mAlloc;
  77. };
  78. /** Creates and destroys annd VulkanResource%s on a single device. */
  79. class VulkanResourceManager
  80. {
  81. public:
  82. ~VulkanResourceManager();
  83. /**
  84. * Creates a new Vulkan resource of the specified type. User must call VulkanResource::destroy() when done using
  85. * the resource.
  86. *
  87. * @param[in] concurrency If true, the resource is allowed to be used on multiple queue types at once.
  88. */
  89. template<class Type, class... Args>
  90. VulkanResource* create(bool concurrency, Args &&...args)
  91. {
  92. VulkanResource* resource = new (bs_alloc(sizeof(Type))) Type(this, concurrency, std::forward<Args>(args)...);
  93. #if BS_DEBUG_MODE
  94. mResources.insert(resource);
  95. #endif
  96. return resource;
  97. }
  98. private:
  99. friend VulkanResource;
  100. /**
  101. * Destroys a previously created Vulkan resource. Caller must ensure the resource is not currently being used
  102. * on the device.
  103. */
  104. void destroy(VulkanResource* resource);
  105. #if BS_DEBUG_MODE
  106. UnorderedSet<VulkanResource*> mResources;
  107. #endif
  108. };
  109. /** @} */
  110. }