wrapper.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <exception>
  6. #include <memory>
  7. #include <optional>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "video_core/renderer_vulkan/wrapper.h"
  12. namespace Vulkan::vk {
  13. namespace {
  14. void SortPhysicalDevices(std::vector<VkPhysicalDevice>& devices, const InstanceDispatch& dld) {
  15. std::stable_sort(devices.begin(), devices.end(), [&](auto lhs, auto rhs) {
  16. // This will call Vulkan more than needed, but these calls are cheap.
  17. const auto lhs_properties = vk::PhysicalDevice(lhs, dld).GetProperties();
  18. const auto rhs_properties = vk::PhysicalDevice(rhs, dld).GetProperties();
  19. // Prefer discrete GPUs, Nvidia over AMD, AMD over Intel, Intel over the rest.
  20. const bool preferred =
  21. (lhs_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
  22. rhs_properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) ||
  23. (lhs_properties.vendorID == 0x10DE && rhs_properties.vendorID != 0x10DE) ||
  24. (lhs_properties.vendorID == 0x1002 && rhs_properties.vendorID != 0x1002) ||
  25. (lhs_properties.vendorID == 0x8086 && rhs_properties.vendorID != 0x8086);
  26. return !preferred;
  27. });
  28. }
  29. template <typename T>
  30. bool Proc(T& result, const InstanceDispatch& dld, const char* proc_name,
  31. VkInstance instance = nullptr) noexcept {
  32. result = reinterpret_cast<T>(dld.vkGetInstanceProcAddr(instance, proc_name));
  33. return result != nullptr;
  34. }
  35. template <typename T>
  36. void Proc(T& result, const DeviceDispatch& dld, const char* proc_name, VkDevice device) noexcept {
  37. result = reinterpret_cast<T>(dld.vkGetDeviceProcAddr(device, proc_name));
  38. }
  39. void Load(VkDevice device, DeviceDispatch& dld) noexcept {
  40. #define X(name) Proc(dld.name, dld, #name, device)
  41. X(vkAcquireNextImageKHR);
  42. X(vkAllocateCommandBuffers);
  43. X(vkAllocateDescriptorSets);
  44. X(vkAllocateMemory);
  45. X(vkBeginCommandBuffer);
  46. X(vkBindBufferMemory);
  47. X(vkBindImageMemory);
  48. X(vkCmdBeginQuery);
  49. X(vkCmdBeginRenderPass);
  50. X(vkCmdBeginTransformFeedbackEXT);
  51. X(vkCmdBindDescriptorSets);
  52. X(vkCmdBindIndexBuffer);
  53. X(vkCmdBindPipeline);
  54. X(vkCmdBindTransformFeedbackBuffersEXT);
  55. X(vkCmdBindVertexBuffers);
  56. X(vkCmdBlitImage);
  57. X(vkCmdClearAttachments);
  58. X(vkCmdCopyBuffer);
  59. X(vkCmdCopyBufferToImage);
  60. X(vkCmdCopyImage);
  61. X(vkCmdCopyImageToBuffer);
  62. X(vkCmdDispatch);
  63. X(vkCmdDraw);
  64. X(vkCmdDrawIndexed);
  65. X(vkCmdEndQuery);
  66. X(vkCmdEndRenderPass);
  67. X(vkCmdEndTransformFeedbackEXT);
  68. X(vkCmdFillBuffer);
  69. X(vkCmdPipelineBarrier);
  70. X(vkCmdPushConstants);
  71. X(vkCmdSetBlendConstants);
  72. X(vkCmdSetDepthBias);
  73. X(vkCmdSetDepthBounds);
  74. X(vkCmdSetEvent);
  75. X(vkCmdSetScissor);
  76. X(vkCmdSetStencilCompareMask);
  77. X(vkCmdSetStencilReference);
  78. X(vkCmdSetStencilWriteMask);
  79. X(vkCmdSetViewport);
  80. X(vkCmdWaitEvents);
  81. X(vkCmdBindVertexBuffers2EXT);
  82. X(vkCmdSetCullModeEXT);
  83. X(vkCmdSetDepthBoundsTestEnableEXT);
  84. X(vkCmdSetDepthCompareOpEXT);
  85. X(vkCmdSetDepthTestEnableEXT);
  86. X(vkCmdSetDepthWriteEnableEXT);
  87. X(vkCmdSetFrontFaceEXT);
  88. X(vkCmdSetPrimitiveTopologyEXT);
  89. X(vkCmdSetStencilOpEXT);
  90. X(vkCmdSetStencilTestEnableEXT);
  91. X(vkCreateBuffer);
  92. X(vkCreateBufferView);
  93. X(vkCreateCommandPool);
  94. X(vkCreateComputePipelines);
  95. X(vkCreateDescriptorPool);
  96. X(vkCreateDescriptorSetLayout);
  97. X(vkCreateDescriptorUpdateTemplateKHR);
  98. X(vkCreateEvent);
  99. X(vkCreateFence);
  100. X(vkCreateFramebuffer);
  101. X(vkCreateGraphicsPipelines);
  102. X(vkCreateImage);
  103. X(vkCreateImageView);
  104. X(vkCreatePipelineLayout);
  105. X(vkCreateQueryPool);
  106. X(vkCreateRenderPass);
  107. X(vkCreateSampler);
  108. X(vkCreateSemaphore);
  109. X(vkCreateShaderModule);
  110. X(vkCreateSwapchainKHR);
  111. X(vkDestroyBuffer);
  112. X(vkDestroyBufferView);
  113. X(vkDestroyCommandPool);
  114. X(vkDestroyDescriptorPool);
  115. X(vkDestroyDescriptorSetLayout);
  116. X(vkDestroyDescriptorUpdateTemplateKHR);
  117. X(vkDestroyEvent);
  118. X(vkDestroyFence);
  119. X(vkDestroyFramebuffer);
  120. X(vkDestroyImage);
  121. X(vkDestroyImageView);
  122. X(vkDestroyPipeline);
  123. X(vkDestroyPipelineLayout);
  124. X(vkDestroyQueryPool);
  125. X(vkDestroyRenderPass);
  126. X(vkDestroySampler);
  127. X(vkDestroySemaphore);
  128. X(vkDestroyShaderModule);
  129. X(vkDestroySwapchainKHR);
  130. X(vkDeviceWaitIdle);
  131. X(vkEndCommandBuffer);
  132. X(vkFreeCommandBuffers);
  133. X(vkFreeDescriptorSets);
  134. X(vkFreeMemory);
  135. X(vkGetBufferMemoryRequirements);
  136. X(vkGetDeviceQueue);
  137. X(vkGetEventStatus);
  138. X(vkGetFenceStatus);
  139. X(vkGetImageMemoryRequirements);
  140. X(vkGetQueryPoolResults);
  141. X(vkMapMemory);
  142. X(vkQueueSubmit);
  143. X(vkResetFences);
  144. X(vkResetQueryPoolEXT);
  145. X(vkUnmapMemory);
  146. X(vkUpdateDescriptorSetWithTemplateKHR);
  147. X(vkUpdateDescriptorSets);
  148. X(vkWaitForFences);
  149. #undef X
  150. }
  151. } // Anonymous namespace
  152. bool Load(InstanceDispatch& dld) noexcept {
  153. #define X(name) Proc(dld.name, dld, #name)
  154. return X(vkCreateInstance) && X(vkEnumerateInstanceExtensionProperties);
  155. #undef X
  156. }
  157. bool Load(VkInstance instance, InstanceDispatch& dld) noexcept {
  158. #define X(name) Proc(dld.name, dld, #name, instance)
  159. // These functions may fail to load depending on the enabled extensions.
  160. // Don't return a failure on these.
  161. X(vkCreateDebugUtilsMessengerEXT);
  162. X(vkDestroyDebugUtilsMessengerEXT);
  163. X(vkDestroySurfaceKHR);
  164. X(vkGetPhysicalDeviceFeatures2KHR);
  165. X(vkGetPhysicalDeviceProperties2KHR);
  166. X(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
  167. X(vkGetPhysicalDeviceSurfaceFormatsKHR);
  168. X(vkGetPhysicalDeviceSurfacePresentModesKHR);
  169. X(vkGetPhysicalDeviceSurfaceSupportKHR);
  170. X(vkGetSwapchainImagesKHR);
  171. X(vkQueuePresentKHR);
  172. return X(vkCreateDevice) && X(vkDestroyDevice) && X(vkDestroyDevice) &&
  173. X(vkEnumerateDeviceExtensionProperties) && X(vkEnumeratePhysicalDevices) &&
  174. X(vkGetDeviceProcAddr) && X(vkGetPhysicalDeviceFormatProperties) &&
  175. X(vkGetPhysicalDeviceMemoryProperties) && X(vkGetPhysicalDeviceProperties) &&
  176. X(vkGetPhysicalDeviceQueueFamilyProperties);
  177. #undef X
  178. }
  179. const char* Exception::what() const noexcept {
  180. return ToString(result);
  181. }
  182. const char* ToString(VkResult result) noexcept {
  183. switch (result) {
  184. case VkResult::VK_SUCCESS:
  185. return "VK_SUCCESS";
  186. case VkResult::VK_NOT_READY:
  187. return "VK_NOT_READY";
  188. case VkResult::VK_TIMEOUT:
  189. return "VK_TIMEOUT";
  190. case VkResult::VK_EVENT_SET:
  191. return "VK_EVENT_SET";
  192. case VkResult::VK_EVENT_RESET:
  193. return "VK_EVENT_RESET";
  194. case VkResult::VK_INCOMPLETE:
  195. return "VK_INCOMPLETE";
  196. case VkResult::VK_ERROR_OUT_OF_HOST_MEMORY:
  197. return "VK_ERROR_OUT_OF_HOST_MEMORY";
  198. case VkResult::VK_ERROR_OUT_OF_DEVICE_MEMORY:
  199. return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
  200. case VkResult::VK_ERROR_INITIALIZATION_FAILED:
  201. return "VK_ERROR_INITIALIZATION_FAILED";
  202. case VkResult::VK_ERROR_DEVICE_LOST:
  203. return "VK_ERROR_DEVICE_LOST";
  204. case VkResult::VK_ERROR_MEMORY_MAP_FAILED:
  205. return "VK_ERROR_MEMORY_MAP_FAILED";
  206. case VkResult::VK_ERROR_LAYER_NOT_PRESENT:
  207. return "VK_ERROR_LAYER_NOT_PRESENT";
  208. case VkResult::VK_ERROR_EXTENSION_NOT_PRESENT:
  209. return "VK_ERROR_EXTENSION_NOT_PRESENT";
  210. case VkResult::VK_ERROR_FEATURE_NOT_PRESENT:
  211. return "VK_ERROR_FEATURE_NOT_PRESENT";
  212. case VkResult::VK_ERROR_INCOMPATIBLE_DRIVER:
  213. return "VK_ERROR_INCOMPATIBLE_DRIVER";
  214. case VkResult::VK_ERROR_TOO_MANY_OBJECTS:
  215. return "VK_ERROR_TOO_MANY_OBJECTS";
  216. case VkResult::VK_ERROR_FORMAT_NOT_SUPPORTED:
  217. return "VK_ERROR_FORMAT_NOT_SUPPORTED";
  218. case VkResult::VK_ERROR_FRAGMENTED_POOL:
  219. return "VK_ERROR_FRAGMENTED_POOL";
  220. case VkResult::VK_ERROR_OUT_OF_POOL_MEMORY:
  221. return "VK_ERROR_OUT_OF_POOL_MEMORY";
  222. case VkResult::VK_ERROR_INVALID_EXTERNAL_HANDLE:
  223. return "VK_ERROR_INVALID_EXTERNAL_HANDLE";
  224. case VkResult::VK_ERROR_SURFACE_LOST_KHR:
  225. return "VK_ERROR_SURFACE_LOST_KHR";
  226. case VkResult::VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
  227. return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
  228. case VkResult::VK_SUBOPTIMAL_KHR:
  229. return "VK_SUBOPTIMAL_KHR";
  230. case VkResult::VK_ERROR_OUT_OF_DATE_KHR:
  231. return "VK_ERROR_OUT_OF_DATE_KHR";
  232. case VkResult::VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
  233. return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
  234. case VkResult::VK_ERROR_VALIDATION_FAILED_EXT:
  235. return "VK_ERROR_VALIDATION_FAILED_EXT";
  236. case VkResult::VK_ERROR_INVALID_SHADER_NV:
  237. return "VK_ERROR_INVALID_SHADER_NV";
  238. case VkResult::VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT:
  239. return "VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT";
  240. case VkResult::VK_ERROR_FRAGMENTATION_EXT:
  241. return "VK_ERROR_FRAGMENTATION_EXT";
  242. case VkResult::VK_ERROR_NOT_PERMITTED_EXT:
  243. return "VK_ERROR_NOT_PERMITTED_EXT";
  244. case VkResult::VK_ERROR_INVALID_DEVICE_ADDRESS_EXT:
  245. return "VK_ERROR_INVALID_DEVICE_ADDRESS_EXT";
  246. case VkResult::VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT:
  247. return "VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT";
  248. }
  249. return "Unknown";
  250. }
  251. void Destroy(VkInstance instance, const InstanceDispatch& dld) noexcept {
  252. dld.vkDestroyInstance(instance, nullptr);
  253. }
  254. void Destroy(VkDevice device, const InstanceDispatch& dld) noexcept {
  255. dld.vkDestroyDevice(device, nullptr);
  256. }
  257. void Destroy(VkDevice device, VkBuffer handle, const DeviceDispatch& dld) noexcept {
  258. dld.vkDestroyBuffer(device, handle, nullptr);
  259. }
  260. void Destroy(VkDevice device, VkBufferView handle, const DeviceDispatch& dld) noexcept {
  261. dld.vkDestroyBufferView(device, handle, nullptr);
  262. }
  263. void Destroy(VkDevice device, VkCommandPool handle, const DeviceDispatch& dld) noexcept {
  264. dld.vkDestroyCommandPool(device, handle, nullptr);
  265. }
  266. void Destroy(VkDevice device, VkDescriptorPool handle, const DeviceDispatch& dld) noexcept {
  267. dld.vkDestroyDescriptorPool(device, handle, nullptr);
  268. }
  269. void Destroy(VkDevice device, VkDescriptorSetLayout handle, const DeviceDispatch& dld) noexcept {
  270. dld.vkDestroyDescriptorSetLayout(device, handle, nullptr);
  271. }
  272. void Destroy(VkDevice device, VkDescriptorUpdateTemplateKHR handle,
  273. const DeviceDispatch& dld) noexcept {
  274. dld.vkDestroyDescriptorUpdateTemplateKHR(device, handle, nullptr);
  275. }
  276. void Destroy(VkDevice device, VkDeviceMemory handle, const DeviceDispatch& dld) noexcept {
  277. dld.vkFreeMemory(device, handle, nullptr);
  278. }
  279. void Destroy(VkDevice device, VkEvent handle, const DeviceDispatch& dld) noexcept {
  280. dld.vkDestroyEvent(device, handle, nullptr);
  281. }
  282. void Destroy(VkDevice device, VkFence handle, const DeviceDispatch& dld) noexcept {
  283. dld.vkDestroyFence(device, handle, nullptr);
  284. }
  285. void Destroy(VkDevice device, VkFramebuffer handle, const DeviceDispatch& dld) noexcept {
  286. dld.vkDestroyFramebuffer(device, handle, nullptr);
  287. }
  288. void Destroy(VkDevice device, VkImage handle, const DeviceDispatch& dld) noexcept {
  289. dld.vkDestroyImage(device, handle, nullptr);
  290. }
  291. void Destroy(VkDevice device, VkImageView handle, const DeviceDispatch& dld) noexcept {
  292. dld.vkDestroyImageView(device, handle, nullptr);
  293. }
  294. void Destroy(VkDevice device, VkPipeline handle, const DeviceDispatch& dld) noexcept {
  295. dld.vkDestroyPipeline(device, handle, nullptr);
  296. }
  297. void Destroy(VkDevice device, VkPipelineLayout handle, const DeviceDispatch& dld) noexcept {
  298. dld.vkDestroyPipelineLayout(device, handle, nullptr);
  299. }
  300. void Destroy(VkDevice device, VkQueryPool handle, const DeviceDispatch& dld) noexcept {
  301. dld.vkDestroyQueryPool(device, handle, nullptr);
  302. }
  303. void Destroy(VkDevice device, VkRenderPass handle, const DeviceDispatch& dld) noexcept {
  304. dld.vkDestroyRenderPass(device, handle, nullptr);
  305. }
  306. void Destroy(VkDevice device, VkSampler handle, const DeviceDispatch& dld) noexcept {
  307. dld.vkDestroySampler(device, handle, nullptr);
  308. }
  309. void Destroy(VkDevice device, VkSwapchainKHR handle, const DeviceDispatch& dld) noexcept {
  310. dld.vkDestroySwapchainKHR(device, handle, nullptr);
  311. }
  312. void Destroy(VkDevice device, VkSemaphore handle, const DeviceDispatch& dld) noexcept {
  313. dld.vkDestroySemaphore(device, handle, nullptr);
  314. }
  315. void Destroy(VkDevice device, VkShaderModule handle, const DeviceDispatch& dld) noexcept {
  316. dld.vkDestroyShaderModule(device, handle, nullptr);
  317. }
  318. void Destroy(VkInstance instance, VkDebugUtilsMessengerEXT handle,
  319. const InstanceDispatch& dld) noexcept {
  320. dld.vkDestroyDebugUtilsMessengerEXT(instance, handle, nullptr);
  321. }
  322. void Destroy(VkInstance instance, VkSurfaceKHR handle, const InstanceDispatch& dld) noexcept {
  323. dld.vkDestroySurfaceKHR(instance, handle, nullptr);
  324. }
  325. VkResult Free(VkDevice device, VkDescriptorPool handle, Span<VkDescriptorSet> sets,
  326. const DeviceDispatch& dld) noexcept {
  327. return dld.vkFreeDescriptorSets(device, handle, sets.size(), sets.data());
  328. }
  329. VkResult Free(VkDevice device, VkCommandPool handle, Span<VkCommandBuffer> buffers,
  330. const DeviceDispatch& dld) noexcept {
  331. dld.vkFreeCommandBuffers(device, handle, buffers.size(), buffers.data());
  332. return VK_SUCCESS;
  333. }
  334. Instance Instance::Create(Span<const char*> layers, Span<const char*> extensions,
  335. InstanceDispatch& dld) noexcept {
  336. VkApplicationInfo application_info;
  337. application_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  338. application_info.pNext = nullptr;
  339. application_info.pApplicationName = "yuzu Emulator";
  340. application_info.applicationVersion = VK_MAKE_VERSION(0, 1, 0);
  341. application_info.pEngineName = "yuzu Emulator";
  342. application_info.engineVersion = VK_MAKE_VERSION(0, 1, 0);
  343. application_info.apiVersion = VK_API_VERSION_1_1;
  344. VkInstanceCreateInfo ci;
  345. ci.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  346. ci.pNext = nullptr;
  347. ci.flags = 0;
  348. ci.pApplicationInfo = &application_info;
  349. ci.enabledLayerCount = layers.size();
  350. ci.ppEnabledLayerNames = layers.data();
  351. ci.enabledExtensionCount = extensions.size();
  352. ci.ppEnabledExtensionNames = extensions.data();
  353. VkInstance instance;
  354. if (dld.vkCreateInstance(&ci, nullptr, &instance) != VK_SUCCESS) {
  355. // Failed to create the instance.
  356. return {};
  357. }
  358. if (!Proc(dld.vkDestroyInstance, dld, "vkDestroyInstance", instance)) {
  359. // We successfully created an instance but the destroy function couldn't be loaded.
  360. // This is a good moment to panic.
  361. return {};
  362. }
  363. return Instance(instance, dld);
  364. }
  365. std::optional<std::vector<VkPhysicalDevice>> Instance::EnumeratePhysicalDevices() {
  366. u32 num;
  367. if (dld->vkEnumeratePhysicalDevices(handle, &num, nullptr) != VK_SUCCESS) {
  368. return std::nullopt;
  369. }
  370. std::vector<VkPhysicalDevice> physical_devices(num);
  371. if (dld->vkEnumeratePhysicalDevices(handle, &num, physical_devices.data()) != VK_SUCCESS) {
  372. return std::nullopt;
  373. }
  374. SortPhysicalDevices(physical_devices, *dld);
  375. return std::make_optional(std::move(physical_devices));
  376. }
  377. DebugCallback Instance::TryCreateDebugCallback(
  378. PFN_vkDebugUtilsMessengerCallbackEXT callback) noexcept {
  379. VkDebugUtilsMessengerCreateInfoEXT ci;
  380. ci.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  381. ci.pNext = nullptr;
  382. ci.flags = 0;
  383. ci.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
  384. VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
  385. VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
  386. VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
  387. ci.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
  388. VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
  389. VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  390. ci.pfnUserCallback = callback;
  391. ci.pUserData = nullptr;
  392. VkDebugUtilsMessengerEXT messenger;
  393. if (dld->vkCreateDebugUtilsMessengerEXT(handle, &ci, nullptr, &messenger) != VK_SUCCESS) {
  394. return {};
  395. }
  396. return DebugCallback(messenger, handle, *dld);
  397. }
  398. void Buffer::BindMemory(VkDeviceMemory memory, VkDeviceSize offset) const {
  399. Check(dld->vkBindBufferMemory(owner, handle, memory, offset));
  400. }
  401. void Image::BindMemory(VkDeviceMemory memory, VkDeviceSize offset) const {
  402. Check(dld->vkBindImageMemory(owner, handle, memory, offset));
  403. }
  404. DescriptorSets DescriptorPool::Allocate(const VkDescriptorSetAllocateInfo& ai) const {
  405. const std::size_t num = ai.descriptorSetCount;
  406. std::unique_ptr sets = std::make_unique<VkDescriptorSet[]>(num);
  407. switch (const VkResult result = dld->vkAllocateDescriptorSets(owner, &ai, sets.get())) {
  408. case VK_SUCCESS:
  409. return DescriptorSets(std::move(sets), num, owner, handle, *dld);
  410. case VK_ERROR_OUT_OF_POOL_MEMORY:
  411. return {};
  412. default:
  413. throw Exception(result);
  414. }
  415. }
  416. CommandBuffers CommandPool::Allocate(std::size_t num_buffers, VkCommandBufferLevel level) const {
  417. VkCommandBufferAllocateInfo ai;
  418. ai.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  419. ai.pNext = nullptr;
  420. ai.commandPool = handle;
  421. ai.level = level;
  422. ai.commandBufferCount = static_cast<u32>(num_buffers);
  423. std::unique_ptr buffers = std::make_unique<VkCommandBuffer[]>(num_buffers);
  424. switch (const VkResult result = dld->vkAllocateCommandBuffers(owner, &ai, buffers.get())) {
  425. case VK_SUCCESS:
  426. return CommandBuffers(std::move(buffers), num_buffers, owner, handle, *dld);
  427. case VK_ERROR_OUT_OF_POOL_MEMORY:
  428. return {};
  429. default:
  430. throw Exception(result);
  431. }
  432. }
  433. std::vector<VkImage> SwapchainKHR::GetImages() const {
  434. u32 num;
  435. Check(dld->vkGetSwapchainImagesKHR(owner, handle, &num, nullptr));
  436. std::vector<VkImage> images(num);
  437. Check(dld->vkGetSwapchainImagesKHR(owner, handle, &num, images.data()));
  438. return images;
  439. }
  440. Device Device::Create(VkPhysicalDevice physical_device, Span<VkDeviceQueueCreateInfo> queues_ci,
  441. Span<const char*> enabled_extensions, const void* next,
  442. DeviceDispatch& dld) noexcept {
  443. VkDeviceCreateInfo ci;
  444. ci.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  445. ci.pNext = next;
  446. ci.flags = 0;
  447. ci.queueCreateInfoCount = queues_ci.size();
  448. ci.pQueueCreateInfos = queues_ci.data();
  449. ci.enabledLayerCount = 0;
  450. ci.ppEnabledLayerNames = nullptr;
  451. ci.enabledExtensionCount = enabled_extensions.size();
  452. ci.ppEnabledExtensionNames = enabled_extensions.data();
  453. ci.pEnabledFeatures = nullptr;
  454. VkDevice device;
  455. if (dld.vkCreateDevice(physical_device, &ci, nullptr, &device) != VK_SUCCESS) {
  456. return {};
  457. }
  458. Load(device, dld);
  459. return Device(device, dld);
  460. }
  461. Queue Device::GetQueue(u32 family_index) const noexcept {
  462. VkQueue queue;
  463. dld->vkGetDeviceQueue(handle, family_index, 0, &queue);
  464. return Queue(queue, *dld);
  465. }
  466. Buffer Device::CreateBuffer(const VkBufferCreateInfo& ci) const {
  467. VkBuffer object;
  468. Check(dld->vkCreateBuffer(handle, &ci, nullptr, &object));
  469. return Buffer(object, handle, *dld);
  470. }
  471. BufferView Device::CreateBufferView(const VkBufferViewCreateInfo& ci) const {
  472. VkBufferView object;
  473. Check(dld->vkCreateBufferView(handle, &ci, nullptr, &object));
  474. return BufferView(object, handle, *dld);
  475. }
  476. Image Device::CreateImage(const VkImageCreateInfo& ci) const {
  477. VkImage object;
  478. Check(dld->vkCreateImage(handle, &ci, nullptr, &object));
  479. return Image(object, handle, *dld);
  480. }
  481. ImageView Device::CreateImageView(const VkImageViewCreateInfo& ci) const {
  482. VkImageView object;
  483. Check(dld->vkCreateImageView(handle, &ci, nullptr, &object));
  484. return ImageView(object, handle, *dld);
  485. }
  486. Semaphore Device::CreateSemaphore() const {
  487. VkSemaphoreCreateInfo ci;
  488. ci.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  489. ci.pNext = nullptr;
  490. ci.flags = 0;
  491. VkSemaphore object;
  492. Check(dld->vkCreateSemaphore(handle, &ci, nullptr, &object));
  493. return Semaphore(object, handle, *dld);
  494. }
  495. Fence Device::CreateFence(const VkFenceCreateInfo& ci) const {
  496. VkFence object;
  497. Check(dld->vkCreateFence(handle, &ci, nullptr, &object));
  498. return Fence(object, handle, *dld);
  499. }
  500. DescriptorPool Device::CreateDescriptorPool(const VkDescriptorPoolCreateInfo& ci) const {
  501. VkDescriptorPool object;
  502. Check(dld->vkCreateDescriptorPool(handle, &ci, nullptr, &object));
  503. return DescriptorPool(object, handle, *dld);
  504. }
  505. RenderPass Device::CreateRenderPass(const VkRenderPassCreateInfo& ci) const {
  506. VkRenderPass object;
  507. Check(dld->vkCreateRenderPass(handle, &ci, nullptr, &object));
  508. return RenderPass(object, handle, *dld);
  509. }
  510. DescriptorSetLayout Device::CreateDescriptorSetLayout(
  511. const VkDescriptorSetLayoutCreateInfo& ci) const {
  512. VkDescriptorSetLayout object;
  513. Check(dld->vkCreateDescriptorSetLayout(handle, &ci, nullptr, &object));
  514. return DescriptorSetLayout(object, handle, *dld);
  515. }
  516. PipelineLayout Device::CreatePipelineLayout(const VkPipelineLayoutCreateInfo& ci) const {
  517. VkPipelineLayout object;
  518. Check(dld->vkCreatePipelineLayout(handle, &ci, nullptr, &object));
  519. return PipelineLayout(object, handle, *dld);
  520. }
  521. Pipeline Device::CreateGraphicsPipeline(const VkGraphicsPipelineCreateInfo& ci) const {
  522. VkPipeline object;
  523. Check(dld->vkCreateGraphicsPipelines(handle, nullptr, 1, &ci, nullptr, &object));
  524. return Pipeline(object, handle, *dld);
  525. }
  526. Pipeline Device::CreateComputePipeline(const VkComputePipelineCreateInfo& ci) const {
  527. VkPipeline object;
  528. Check(dld->vkCreateComputePipelines(handle, nullptr, 1, &ci, nullptr, &object));
  529. return Pipeline(object, handle, *dld);
  530. }
  531. Sampler Device::CreateSampler(const VkSamplerCreateInfo& ci) const {
  532. VkSampler object;
  533. Check(dld->vkCreateSampler(handle, &ci, nullptr, &object));
  534. return Sampler(object, handle, *dld);
  535. }
  536. Framebuffer Device::CreateFramebuffer(const VkFramebufferCreateInfo& ci) const {
  537. VkFramebuffer object;
  538. Check(dld->vkCreateFramebuffer(handle, &ci, nullptr, &object));
  539. return Framebuffer(object, handle, *dld);
  540. }
  541. CommandPool Device::CreateCommandPool(const VkCommandPoolCreateInfo& ci) const {
  542. VkCommandPool object;
  543. Check(dld->vkCreateCommandPool(handle, &ci, nullptr, &object));
  544. return CommandPool(object, handle, *dld);
  545. }
  546. DescriptorUpdateTemplateKHR Device::CreateDescriptorUpdateTemplateKHR(
  547. const VkDescriptorUpdateTemplateCreateInfoKHR& ci) const {
  548. VkDescriptorUpdateTemplateKHR object;
  549. Check(dld->vkCreateDescriptorUpdateTemplateKHR(handle, &ci, nullptr, &object));
  550. return DescriptorUpdateTemplateKHR(object, handle, *dld);
  551. }
  552. QueryPool Device::CreateQueryPool(const VkQueryPoolCreateInfo& ci) const {
  553. VkQueryPool object;
  554. Check(dld->vkCreateQueryPool(handle, &ci, nullptr, &object));
  555. return QueryPool(object, handle, *dld);
  556. }
  557. ShaderModule Device::CreateShaderModule(const VkShaderModuleCreateInfo& ci) const {
  558. VkShaderModule object;
  559. Check(dld->vkCreateShaderModule(handle, &ci, nullptr, &object));
  560. return ShaderModule(object, handle, *dld);
  561. }
  562. Event Device::CreateEvent() const {
  563. VkEventCreateInfo ci;
  564. ci.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
  565. ci.pNext = nullptr;
  566. ci.flags = 0;
  567. VkEvent object;
  568. Check(dld->vkCreateEvent(handle, &ci, nullptr, &object));
  569. return Event(object, handle, *dld);
  570. }
  571. SwapchainKHR Device::CreateSwapchainKHR(const VkSwapchainCreateInfoKHR& ci) const {
  572. VkSwapchainKHR object;
  573. Check(dld->vkCreateSwapchainKHR(handle, &ci, nullptr, &object));
  574. return SwapchainKHR(object, handle, *dld);
  575. }
  576. DeviceMemory Device::TryAllocateMemory(const VkMemoryAllocateInfo& ai) const noexcept {
  577. VkDeviceMemory memory;
  578. if (dld->vkAllocateMemory(handle, &ai, nullptr, &memory) != VK_SUCCESS) {
  579. return {};
  580. }
  581. return DeviceMemory(memory, handle, *dld);
  582. }
  583. DeviceMemory Device::AllocateMemory(const VkMemoryAllocateInfo& ai) const {
  584. VkDeviceMemory memory;
  585. Check(dld->vkAllocateMemory(handle, &ai, nullptr, &memory));
  586. return DeviceMemory(memory, handle, *dld);
  587. }
  588. VkMemoryRequirements Device::GetBufferMemoryRequirements(VkBuffer buffer) const noexcept {
  589. VkMemoryRequirements requirements;
  590. dld->vkGetBufferMemoryRequirements(handle, buffer, &requirements);
  591. return requirements;
  592. }
  593. VkMemoryRequirements Device::GetImageMemoryRequirements(VkImage image) const noexcept {
  594. VkMemoryRequirements requirements;
  595. dld->vkGetImageMemoryRequirements(handle, image, &requirements);
  596. return requirements;
  597. }
  598. void Device::UpdateDescriptorSets(Span<VkWriteDescriptorSet> writes,
  599. Span<VkCopyDescriptorSet> copies) const noexcept {
  600. dld->vkUpdateDescriptorSets(handle, writes.size(), writes.data(), copies.size(), copies.data());
  601. }
  602. VkPhysicalDeviceProperties PhysicalDevice::GetProperties() const noexcept {
  603. VkPhysicalDeviceProperties properties;
  604. dld->vkGetPhysicalDeviceProperties(physical_device, &properties);
  605. return properties;
  606. }
  607. void PhysicalDevice::GetProperties2KHR(VkPhysicalDeviceProperties2KHR& properties) const noexcept {
  608. dld->vkGetPhysicalDeviceProperties2KHR(physical_device, &properties);
  609. }
  610. VkPhysicalDeviceFeatures PhysicalDevice::GetFeatures() const noexcept {
  611. VkPhysicalDeviceFeatures2KHR features2;
  612. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
  613. features2.pNext = nullptr;
  614. dld->vkGetPhysicalDeviceFeatures2KHR(physical_device, &features2);
  615. return features2.features;
  616. }
  617. void PhysicalDevice::GetFeatures2KHR(VkPhysicalDeviceFeatures2KHR& features) const noexcept {
  618. dld->vkGetPhysicalDeviceFeatures2KHR(physical_device, &features);
  619. }
  620. VkFormatProperties PhysicalDevice::GetFormatProperties(VkFormat format) const noexcept {
  621. VkFormatProperties properties;
  622. dld->vkGetPhysicalDeviceFormatProperties(physical_device, format, &properties);
  623. return properties;
  624. }
  625. std::vector<VkExtensionProperties> PhysicalDevice::EnumerateDeviceExtensionProperties() const {
  626. u32 num;
  627. dld->vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &num, nullptr);
  628. std::vector<VkExtensionProperties> properties(num);
  629. dld->vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &num, properties.data());
  630. return properties;
  631. }
  632. std::vector<VkQueueFamilyProperties> PhysicalDevice::GetQueueFamilyProperties() const {
  633. u32 num;
  634. dld->vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &num, nullptr);
  635. std::vector<VkQueueFamilyProperties> properties(num);
  636. dld->vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &num, properties.data());
  637. return properties;
  638. }
  639. bool PhysicalDevice::GetSurfaceSupportKHR(u32 queue_family_index, VkSurfaceKHR surface) const {
  640. VkBool32 supported;
  641. Check(dld->vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, queue_family_index, surface,
  642. &supported));
  643. return supported == VK_TRUE;
  644. }
  645. VkSurfaceCapabilitiesKHR PhysicalDevice::GetSurfaceCapabilitiesKHR(VkSurfaceKHR surface) const {
  646. VkSurfaceCapabilitiesKHR capabilities;
  647. Check(dld->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &capabilities));
  648. return capabilities;
  649. }
  650. std::vector<VkSurfaceFormatKHR> PhysicalDevice::GetSurfaceFormatsKHR(VkSurfaceKHR surface) const {
  651. u32 num;
  652. Check(dld->vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &num, nullptr));
  653. std::vector<VkSurfaceFormatKHR> formats(num);
  654. Check(
  655. dld->vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &num, formats.data()));
  656. return formats;
  657. }
  658. std::vector<VkPresentModeKHR> PhysicalDevice::GetSurfacePresentModesKHR(
  659. VkSurfaceKHR surface) const {
  660. u32 num;
  661. Check(dld->vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &num, nullptr));
  662. std::vector<VkPresentModeKHR> modes(num);
  663. Check(dld->vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &num,
  664. modes.data()));
  665. return modes;
  666. }
  667. VkPhysicalDeviceMemoryProperties PhysicalDevice::GetMemoryProperties() const noexcept {
  668. VkPhysicalDeviceMemoryProperties properties;
  669. dld->vkGetPhysicalDeviceMemoryProperties(physical_device, &properties);
  670. return properties;
  671. }
  672. std::optional<std::vector<VkExtensionProperties>> EnumerateInstanceExtensionProperties(
  673. const InstanceDispatch& dld) {
  674. u32 num;
  675. if (dld.vkEnumerateInstanceExtensionProperties(nullptr, &num, nullptr) != VK_SUCCESS) {
  676. return std::nullopt;
  677. }
  678. std::vector<VkExtensionProperties> properties(num);
  679. if (dld.vkEnumerateInstanceExtensionProperties(nullptr, &num, properties.data()) !=
  680. VK_SUCCESS) {
  681. return std::nullopt;
  682. }
  683. return properties;
  684. }
  685. } // namespace Vulkan::vk