BsVulkanDevice.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "RenderAPI/BsRenderAPI.h"
  6. #include "Managers/BsVulkanDescriptorManager.h"
  7. namespace bs { namespace ct
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Contains format describing a Vulkan surface. */
  13. struct SurfaceFormat
  14. {
  15. VkFormat colorFormat;
  16. VkFormat depthFormat;
  17. VkColorSpaceKHR colorSpace;
  18. };
  19. /** Represents a single GPU device usable by Vulkan. */
  20. class VulkanDevice
  21. {
  22. public:
  23. VulkanDevice(VkPhysicalDevice device, UINT32 deviceIdx);
  24. ~VulkanDevice();
  25. /** Returns an object describing the physical properties of the device. */
  26. VkPhysicalDevice getPhysical() const { return mPhysicalDevice; }
  27. /** Returns an object describing the logical properties of the device. */
  28. VkDevice getLogical() const { return mLogicalDevice; }
  29. /** Returns true if the device is one of the primary GPU's. */
  30. bool isPrimary() const { return mIsPrimary; }
  31. /** Returns the unique index of the device. */
  32. UINT32 getIndex() const { return mDeviceIdx; }
  33. /** Blocks the calling thread until all operations on the device finish. */
  34. void waitIdle() const;
  35. /** Returns a set of properties describing the physical device. */
  36. const VkPhysicalDeviceProperties& getDeviceProperties() const { return mDeviceProperties; }
  37. /** Returns a set of features that the application can use to check if a specific feature is supported. */
  38. const VkPhysicalDeviceFeatures& getDeviceFeatures() const { return mDeviceFeatures; }
  39. /** Returns a set of properties describing the memory of the physical device. */
  40. const VkPhysicalDeviceMemoryProperties& getMemoryProperties() const { return mMemoryProperties; }
  41. /** Returns the number of queue supported on the device, per type. */
  42. UINT32 getNumQueues(GpuQueueType type) const { return (UINT32)mQueueInfos[(int)type].queues.size(); }
  43. /** Returns queue of the specified type at the specified index. Index must be in range [0, getNumQueues()). */
  44. VulkanQueue* getQueue(GpuQueueType type, UINT32 idx) const { return mQueueInfos[(int)type].queues[idx]; }
  45. /**
  46. * Returns index of the queue family for the specified queue type. Returns -1 if no queues for the specified type
  47. * exist. There will always be a queue family for the graphics type.
  48. */
  49. UINT32 getQueueFamily(GpuQueueType type) const { return mQueueInfos[(int)type].familyIdx; }
  50. /**
  51. * Fills out a mask that has bits set for every queue index that maps to the same physical queue as the provided
  52. * index. This is useful as different queue indices will sometimes map to the same physical queue.
  53. */
  54. UINT32 getQueueMask(GpuQueueType type, UINT32 queueIdx) const;
  55. /** Returns the best matching surface format according to the provided parameters. */
  56. SurfaceFormat getSurfaceFormat(const VkSurfaceKHR& surface, bool gamma) const;
  57. /** Returns a pool that can be used for allocating command buffers for all queues on this device. */
  58. VulkanCmdBufferPool& getCmdBufferPool() const { return *mCommandBufferPool; }
  59. /** Returns a pool that can be used for allocating queries on this device. */
  60. VulkanQueryPool& getQueryPool() const { return *mQueryPool; }
  61. /** Returns a manager that can be used for allocating descriptor layouts and sets. */
  62. VulkanDescriptorManager& getDescriptorManager() const { return *mDescriptorManager; }
  63. /** Returns a manager that can be used for allocating Vulkan objects wrapped as managed resources. */
  64. VulkanResourceManager& getResourceManager() const { return *mResourceManager; }
  65. /**
  66. * Allocates memory for the provided image, and binds it to the image. Returns null if it cannot find memory
  67. * with the specified flags.
  68. */
  69. VkDeviceMemory allocateMemory(VkImage image, VkMemoryPropertyFlags flags);
  70. /**
  71. * Allocates memory for the provided buffer, and binds it to the buffer. Returns null if it cannot find memory
  72. * with the specified flags.
  73. */
  74. VkDeviceMemory allocateMemory(VkBuffer buffer, VkMemoryPropertyFlags flags);
  75. /**
  76. * Allocates a block of memory according to the provided memory requirements. Returns null if it cannot find memory
  77. * with the specified flags.
  78. */
  79. VkDeviceMemory allocateMemory(const VkMemoryRequirements& reqs, VkMemoryPropertyFlags flags);
  80. /** Frees a previously allocated block of memory. */
  81. void freeMemory(VkDeviceMemory memory);
  82. private:
  83. friend class VulkanRenderAPI;
  84. /** Attempts to find a memory type that matches the requirements bits and the requested flags. */
  85. uint32_t findMemoryType(uint32_t requirementBits, VkMemoryPropertyFlags wantedFlags);
  86. /** Marks the device as a primary device. */
  87. void setIsPrimary() { mIsPrimary = true; }
  88. VkPhysicalDevice mPhysicalDevice;
  89. VkDevice mLogicalDevice;
  90. bool mIsPrimary;
  91. UINT32 mDeviceIdx;
  92. VulkanCmdBufferPool* mCommandBufferPool;
  93. VulkanQueryPool* mQueryPool;
  94. VulkanDescriptorManager* mDescriptorManager;
  95. VulkanResourceManager* mResourceManager;
  96. VkPhysicalDeviceProperties mDeviceProperties;
  97. VkPhysicalDeviceFeatures mDeviceFeatures;
  98. VkPhysicalDeviceMemoryProperties mMemoryProperties;
  99. /** Contains data about a set of queues of a specific type. */
  100. struct QueueInfo
  101. {
  102. UINT32 familyIdx;
  103. Vector<VulkanQueue*> queues;
  104. };
  105. QueueInfo mQueueInfos[GQT_COUNT];
  106. };
  107. /** @} */
  108. }}