BsVulkanDevice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanDevice.h"
  4. #include "BsVulkanQueue.h"
  5. #include "BsVulkanCommandBuffer.h"
  6. #include "Managers/BsVulkanDescriptorManager.h"
  7. #include "Managers/BsVulkanQueryManager.h"
  8. #define VMA_IMPLEMENTATION
  9. #include "ThirdParty/vk_mem_alloc.h"
  10. namespace bs { namespace ct
  11. {
  12. VulkanDevice::VulkanDevice(VkPhysicalDevice device, UINT32 deviceIdx)
  13. : mPhysicalDevice(device), mLogicalDevice(nullptr), mIsPrimary(false), mDeviceIdx(deviceIdx), mQueueInfos()
  14. {
  15. // Set to default
  16. for (UINT32 i = 0; i < GQT_COUNT; i++)
  17. mQueueInfos[i].familyIdx = (UINT32)-1;
  18. vkGetPhysicalDeviceProperties(device, &mDeviceProperties);
  19. vkGetPhysicalDeviceFeatures(device, &mDeviceFeatures);
  20. vkGetPhysicalDeviceMemoryProperties(device, &mMemoryProperties);
  21. uint32_t numQueueFamilies;
  22. vkGetPhysicalDeviceQueueFamilyProperties(device, &numQueueFamilies, nullptr);
  23. Vector<VkQueueFamilyProperties> queueFamilyProperties(numQueueFamilies);
  24. vkGetPhysicalDeviceQueueFamilyProperties(device, &numQueueFamilies, queueFamilyProperties.data());
  25. // Create queues
  26. const float defaultQueuePriorities[BS_MAX_QUEUES_PER_TYPE] = { 0.0f };
  27. Vector<VkDeviceQueueCreateInfo> queueCreateInfos;
  28. auto populateQueueInfo = [&](GpuQueueType type, uint32_t familyIdx)
  29. {
  30. queueCreateInfos.push_back(VkDeviceQueueCreateInfo());
  31. VkDeviceQueueCreateInfo& createInfo = queueCreateInfos.back();
  32. createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  33. createInfo.pNext = nullptr;
  34. createInfo.flags = 0;
  35. createInfo.queueFamilyIndex = familyIdx;
  36. createInfo.queueCount = std::min(queueFamilyProperties[familyIdx].queueCount, (uint32_t)BS_MAX_QUEUES_PER_TYPE);
  37. createInfo.pQueuePriorities = defaultQueuePriorities;
  38. mQueueInfos[type].familyIdx = familyIdx;
  39. mQueueInfos[type].queues.resize(createInfo.queueCount, nullptr);
  40. };
  41. // Look for dedicated compute queues
  42. for (UINT32 i = 0; i < (UINT32)queueFamilyProperties.size(); i++)
  43. {
  44. if ((queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT) && (queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0)
  45. {
  46. populateQueueInfo(GQT_COMPUTE, i);
  47. break;
  48. }
  49. }
  50. // Look for dedicated upload queues
  51. for (UINT32 i = 0; i < (UINT32)queueFamilyProperties.size(); i++)
  52. {
  53. if ((queueFamilyProperties[i].queueFlags & VK_QUEUE_TRANSFER_BIT) &&
  54. ((queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0) &&
  55. ((queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT) == 0))
  56. {
  57. populateQueueInfo(GQT_UPLOAD, i);
  58. break;
  59. }
  60. }
  61. // Looks for graphics queues
  62. for (UINT32 i = 0; i < (UINT32)queueFamilyProperties.size(); i++)
  63. {
  64. if (queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
  65. {
  66. populateQueueInfo(GQT_GRAPHICS, i);
  67. break;
  68. }
  69. }
  70. // Set up extensions
  71. const char* extensions[5];
  72. uint32_t numExtensions = 0;
  73. extensions[numExtensions++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
  74. extensions[numExtensions++] = VK_KHR_MAINTENANCE1_EXTENSION_NAME;
  75. extensions[numExtensions++] = VK_KHR_MAINTENANCE2_EXTENSION_NAME;
  76. // Enumerate supported extensions
  77. bool dedicatedAllocExt = false;
  78. bool getMemReqExt = false;
  79. uint32_t numAvailableExtensions = 0;
  80. vkEnumerateDeviceExtensionProperties(device, nullptr, &numAvailableExtensions, nullptr);
  81. if (numAvailableExtensions > 0)
  82. {
  83. Vector<VkExtensionProperties> availableExtensions(numAvailableExtensions);
  84. if (vkEnumerateDeviceExtensionProperties(device, nullptr, &numAvailableExtensions, availableExtensions.data()) == VK_SUCCESS)
  85. {
  86. for (auto entry : extensions)
  87. {
  88. if(entry == VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME)
  89. {
  90. extensions[numExtensions++] = VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME;
  91. dedicatedAllocExt = true;
  92. }
  93. else if(entry == VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME)
  94. {
  95. extensions[numExtensions++] = VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME;
  96. getMemReqExt = true;
  97. }
  98. }
  99. }
  100. }
  101. VkDeviceCreateInfo deviceInfo;
  102. deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  103. deviceInfo.pNext = nullptr;
  104. deviceInfo.flags = 0;
  105. deviceInfo.queueCreateInfoCount = (uint32_t)queueCreateInfos.size();
  106. deviceInfo.pQueueCreateInfos = queueCreateInfos.data();
  107. deviceInfo.pEnabledFeatures = &mDeviceFeatures;
  108. deviceInfo.enabledExtensionCount = numExtensions;
  109. deviceInfo.ppEnabledExtensionNames = extensions;
  110. deviceInfo.enabledLayerCount = 0;
  111. deviceInfo.ppEnabledLayerNames = nullptr;
  112. VkResult result = vkCreateDevice(device, &deviceInfo, gVulkanAllocator, &mLogicalDevice);
  113. assert(result == VK_SUCCESS);
  114. // Retrieve queues
  115. for(UINT32 i = 0; i < GQT_COUNT; i++)
  116. {
  117. UINT32 numQueues = (UINT32)mQueueInfos[i].queues.size();
  118. for (UINT32 j = 0; j < numQueues; j++)
  119. {
  120. VkQueue queue;
  121. vkGetDeviceQueue(mLogicalDevice, mQueueInfos[i].familyIdx, j, &queue);
  122. mQueueInfos[i].queues[j] = bs_new<VulkanQueue>(*this, queue, (GpuQueueType)i, j);
  123. }
  124. }
  125. // Set up the memory allocator
  126. VmaAllocatorCreateInfo allocatorCI = {};
  127. allocatorCI.physicalDevice = device;
  128. allocatorCI.device = mLogicalDevice;
  129. allocatorCI.pAllocationCallbacks = gVulkanAllocator;
  130. if(dedicatedAllocExt && getMemReqExt)
  131. allocatorCI.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
  132. vmaCreateAllocator(&allocatorCI, &mAllocator);
  133. // Create pools/managers
  134. mCommandBufferPool = bs_new<VulkanCmdBufferPool>(*this);
  135. mQueryPool = bs_new<VulkanQueryPool>(*this);
  136. mDescriptorManager = bs_new<VulkanDescriptorManager>(*this);
  137. mResourceManager = bs_new<VulkanResourceManager>(*this);
  138. }
  139. VulkanDevice::~VulkanDevice()
  140. {
  141. waitIdle();
  142. for (UINT32 i = 0; i < GQT_COUNT; i++)
  143. {
  144. UINT32 numQueues = (UINT32)mQueueInfos[i].queues.size();
  145. for (UINT32 j = 0; j < numQueues; j++)
  146. {
  147. mQueueInfos[i].queues[j]->refreshStates(true, true);
  148. bs_delete(mQueueInfos[i].queues[j]);
  149. }
  150. }
  151. bs_delete(mDescriptorManager);
  152. bs_delete(mQueryPool);
  153. bs_delete(mCommandBufferPool);
  154. // Needs to happen after query pool & command buffer pool shutdown, to ensure their resources are destroyed
  155. bs_delete(mResourceManager);
  156. vmaDestroyAllocator(mAllocator);
  157. vkDestroyDevice(mLogicalDevice, gVulkanAllocator);
  158. }
  159. void VulkanDevice::waitIdle() const
  160. {
  161. VkResult result = vkDeviceWaitIdle(mLogicalDevice);
  162. assert(result == VK_SUCCESS);
  163. }
  164. UINT32 VulkanDevice::getQueueMask(GpuQueueType type, UINT32 queueIdx) const
  165. {
  166. UINT32 numQueues = getNumQueues(type);
  167. if (numQueues == 0)
  168. return 0;
  169. UINT32 idMask = 0;
  170. UINT32 curIdx = queueIdx % numQueues;
  171. while (curIdx < BS_MAX_QUEUES_PER_TYPE)
  172. {
  173. idMask |= CommandSyncMask::getGlobalQueueMask(type, curIdx);
  174. curIdx += numQueues;
  175. }
  176. return idMask;
  177. }
  178. SurfaceFormat VulkanDevice::getSurfaceFormat(const VkSurfaceKHR& surface, bool gamma) const
  179. {
  180. uint32_t numFormats;
  181. VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDevice, surface, &numFormats, nullptr);
  182. assert(result == VK_SUCCESS);
  183. assert(numFormats > 0);
  184. VkSurfaceFormatKHR* surfaceFormats = bs_stack_alloc<VkSurfaceFormatKHR>(numFormats);
  185. result = vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDevice, surface, &numFormats, surfaceFormats);
  186. assert(result == VK_SUCCESS);
  187. SurfaceFormat output;
  188. output.colorFormat = VK_FORMAT_R8G8B8A8_UNORM;
  189. output.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
  190. output.depthFormat = VK_FORMAT_D24_UNORM_S8_UINT;
  191. // If there is no preferred format, use standard RGBA
  192. if ((numFormats == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED))
  193. {
  194. if (gamma)
  195. output.colorFormat = VK_FORMAT_R8G8B8A8_SRGB;
  196. else
  197. output.colorFormat = VK_FORMAT_B8G8R8A8_UNORM;
  198. output.colorSpace = surfaceFormats[0].colorSpace;
  199. }
  200. else
  201. {
  202. bool foundFormat = false;
  203. VkFormat wantedFormatsUNORM[] =
  204. {
  205. VK_FORMAT_R8G8B8A8_UNORM,
  206. VK_FORMAT_B8G8R8A8_UNORM,
  207. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  208. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  209. VK_FORMAT_R8G8B8_UNORM,
  210. VK_FORMAT_B8G8R8_UNORM
  211. };
  212. VkFormat wantedFormatsSRGB[] =
  213. {
  214. VK_FORMAT_R8G8B8A8_SRGB,
  215. VK_FORMAT_B8G8R8A8_SRGB,
  216. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  217. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  218. VK_FORMAT_R8G8B8_SRGB,
  219. VK_FORMAT_B8G8R8_SRGB
  220. };
  221. UINT32 numWantedFormats;
  222. VkFormat* wantedFormats;
  223. if (gamma)
  224. {
  225. numWantedFormats = sizeof(wantedFormatsSRGB) / sizeof(wantedFormatsSRGB[0]);
  226. wantedFormats = wantedFormatsSRGB;
  227. }
  228. else
  229. {
  230. numWantedFormats = sizeof(wantedFormatsUNORM) / sizeof(wantedFormatsUNORM[0]);
  231. wantedFormats = wantedFormatsUNORM;
  232. }
  233. for(UINT32 i = 0; i < numWantedFormats; i++)
  234. {
  235. for(UINT32 j = 0; j < numFormats; j++)
  236. {
  237. if(surfaceFormats[j].format == wantedFormats[i])
  238. {
  239. output.colorFormat = surfaceFormats[j].format;
  240. output.colorSpace = surfaceFormats[j].colorSpace;
  241. foundFormat = true;
  242. break;
  243. }
  244. }
  245. if (foundFormat)
  246. break;
  247. }
  248. // If we haven't found anything, fall back to first available
  249. if(!foundFormat)
  250. {
  251. output.colorFormat = surfaceFormats[0].format;
  252. output.colorSpace = surfaceFormats[0].colorSpace;
  253. if (gamma)
  254. LOGERR("Cannot find a valid sRGB format for a render window surface, falling back to a default format.");
  255. }
  256. }
  257. bs_stack_free(surfaceFormats);
  258. return output;
  259. }
  260. VmaAllocation VulkanDevice::allocateMemory(VkImage image, VkMemoryPropertyFlags flags)
  261. {
  262. VmaAllocationCreateInfo allocCI = {};
  263. allocCI.requiredFlags = flags;
  264. VmaAllocationInfo allocInfo;
  265. VmaAllocation allocation;
  266. VkResult result = vmaAllocateMemoryForImage(mAllocator, image, &allocCI, &allocation, &allocInfo);
  267. assert(result == VK_SUCCESS);
  268. result = vkBindImageMemory(mLogicalDevice, image, allocInfo.deviceMemory, allocInfo.offset);
  269. assert(result == VK_SUCCESS);
  270. return allocation;
  271. }
  272. VmaAllocation VulkanDevice::allocateMemory(VkBuffer buffer, VkMemoryPropertyFlags flags)
  273. {
  274. VmaAllocationCreateInfo allocCI = {};
  275. allocCI.requiredFlags = flags;
  276. VmaAllocationInfo allocInfo;
  277. VmaAllocation memory;
  278. VkResult result = vmaAllocateMemoryForBuffer(mAllocator, buffer, &allocCI, &memory, &allocInfo);
  279. assert(result == VK_SUCCESS);
  280. result = vkBindBufferMemory(mLogicalDevice, buffer, allocInfo.deviceMemory, allocInfo.offset);
  281. assert(result == VK_SUCCESS);
  282. return memory;
  283. }
  284. void VulkanDevice::freeMemory(VmaAllocation allocation)
  285. {
  286. vmaFreeMemory(mAllocator, allocation);
  287. }
  288. void VulkanDevice::getAllocationInfo(VmaAllocation allocation, VkDeviceMemory& memory, VkDeviceSize& offset)
  289. {
  290. VmaAllocationInfo allocInfo;
  291. vmaGetAllocationInfo(mAllocator, allocation, &allocInfo);
  292. memory = allocInfo.deviceMemory;
  293. offset = allocInfo.offset;
  294. }
  295. uint32_t VulkanDevice::findMemoryType(uint32_t requirementBits, VkMemoryPropertyFlags wantedFlags)
  296. {
  297. for (uint32_t i = 0; i < mMemoryProperties.memoryTypeCount; i++)
  298. {
  299. if (requirementBits & (1 << i))
  300. {
  301. if ((mMemoryProperties.memoryTypes[i].propertyFlags & wantedFlags) == wantedFlags)
  302. return i;
  303. }
  304. }
  305. return -1;
  306. }
  307. }}