BsVulkanDevice.cpp 9.8 KB

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