BsVulkanResource.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. namespace BansheeEngine
  6. {
  7. /** @addtogroup Vulkan
  8. * @{
  9. */
  10. /** Flags that determine how is a resource being used by the GPU. */
  11. enum class VulkanUseFlag
  12. {
  13. None = 0,
  14. Read = 0x1,
  15. Write = 0x2
  16. };
  17. class VulkanResourceManager;
  18. typedef Flags<VulkanUseFlag> VulkanUseFlags;
  19. BS_FLAGS_OPERATORS(VulkanUseFlag);
  20. /**
  21. * Wraps one or multiple native Vulkan objects. Allows the object usage to be tracked in command buffers, handles
  22. * ownership transitions between different queues, and handles delayed object destruction.
  23. */
  24. class VulkanResource
  25. {
  26. public:
  27. VulkanResource(VulkanResourceManager* owner);
  28. virtual ~VulkanResource();
  29. /**
  30. * Notifies the resource that it is currently being used on the provided command buffer. This means the command
  31. * buffer has actually been submitted to the queue and the resource is used by the GPU.
  32. *
  33. * A resource can only be used by a single command buffer at a time.
  34. */
  35. virtual void notifyUsed(VulkanCmdBuffer& buffer, VulkanUseFlags flags);
  36. /**
  37. * Notifies the resource that it is no longer used by on the GPU. This makes the resource usable on other command
  38. * buffers again.
  39. */
  40. virtual void notifyDone();
  41. /**
  42. * Checks is the resource currently used on a device.
  43. *
  44. * @note Resource usage is only checked at certain points of the program. This means the resource could be
  45. * done on the device but this method may still report true. If you need to know the latest state
  46. * call VulkanCommandBufferManager::refreshStates() before checking for usage.
  47. */
  48. bool isUsed() const { return mCmdBufferId != -1; }
  49. /**
  50. * Destroys the resource and frees its memory. If the resource is currently being used on a device, the
  51. * destruction is delayed until the device is done with it.
  52. */
  53. void destroy();
  54. protected:
  55. VulkanResourceManager* mOwner;
  56. VulkanUseFlags mFlags;
  57. UINT32 mCmdBufferId = -1;
  58. bool mIsDestroyed = false;
  59. };
  60. /** Creates and destroys annd VulkanResource%s on a single device. */
  61. class VulkanResourceManager
  62. {
  63. public:
  64. ~VulkanResourceManager();
  65. /**
  66. * Creates a new Vulkan resource of the specified type. User must call VulkanResource::destroy() when done using
  67. * the resource.
  68. */
  69. template<class Type, class... Args>
  70. VulkanResource* create(Args &&...args)
  71. {
  72. VulkanResource* resource = new (bs_alloc(sizeof(Type))) Type(std::forward<Args>(args)...);
  73. #if BS_DEBUG_MODE
  74. mResources.insert(resource);
  75. #endif
  76. return resource;
  77. }
  78. private:
  79. friend VulkanResource;
  80. /**
  81. * Destroys a previously created Vulkan resource. Caller must ensure the resource is not currently being used
  82. * on the device.
  83. */
  84. void destroy(VulkanResource* resource);
  85. #if BS_DEBUG_MODE
  86. UnorderedSet<VulkanResource*> mResources;
  87. #endif
  88. };
  89. /** @} */
  90. }