wrapper.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <exception>
  5. #include <memory>
  6. #include <optional>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "video_core/renderer_vulkan/wrapper.h"
  11. namespace Vulkan::vk {
  12. namespace {
  13. template <typename T>
  14. bool Proc(T& result, const InstanceDispatch& dld, const char* proc_name,
  15. VkInstance instance = nullptr) noexcept {
  16. result = reinterpret_cast<T>(dld.vkGetInstanceProcAddr(instance, proc_name));
  17. return result != nullptr;
  18. }
  19. template <typename T>
  20. void Proc(T& result, const DeviceDispatch& dld, const char* proc_name, VkDevice device) noexcept {
  21. result = reinterpret_cast<T>(dld.vkGetDeviceProcAddr(device, proc_name));
  22. }
  23. void Load(VkDevice device, DeviceDispatch& dld) noexcept {
  24. #define X(name) Proc(dld.name, dld, #name, device)
  25. X(vkAcquireNextImageKHR);
  26. X(vkAllocateCommandBuffers);
  27. X(vkAllocateDescriptorSets);
  28. X(vkAllocateMemory);
  29. X(vkBeginCommandBuffer);
  30. X(vkBindBufferMemory);
  31. X(vkBindImageMemory);
  32. X(vkCmdBeginQuery);
  33. X(vkCmdBeginRenderPass);
  34. X(vkCmdBeginTransformFeedbackEXT);
  35. X(vkCmdBindDescriptorSets);
  36. X(vkCmdBindIndexBuffer);
  37. X(vkCmdBindPipeline);
  38. X(vkCmdBindTransformFeedbackBuffersEXT);
  39. X(vkCmdBindVertexBuffers);
  40. X(vkCmdBlitImage);
  41. X(vkCmdClearAttachments);
  42. X(vkCmdCopyBuffer);
  43. X(vkCmdCopyBufferToImage);
  44. X(vkCmdCopyImage);
  45. X(vkCmdCopyImageToBuffer);
  46. X(vkCmdDispatch);
  47. X(vkCmdDraw);
  48. X(vkCmdDrawIndexed);
  49. X(vkCmdEndQuery);
  50. X(vkCmdEndRenderPass);
  51. X(vkCmdEndTransformFeedbackEXT);
  52. X(vkCmdFillBuffer);
  53. X(vkCmdPipelineBarrier);
  54. X(vkCmdPushConstants);
  55. X(vkCmdSetBlendConstants);
  56. X(vkCmdSetCheckpointNV);
  57. X(vkCmdSetDepthBias);
  58. X(vkCmdSetDepthBounds);
  59. X(vkCmdSetScissor);
  60. X(vkCmdSetStencilCompareMask);
  61. X(vkCmdSetStencilReference);
  62. X(vkCmdSetStencilWriteMask);
  63. X(vkCmdSetViewport);
  64. X(vkCreateBuffer);
  65. X(vkCreateBufferView);
  66. X(vkCreateCommandPool);
  67. X(vkCreateComputePipelines);
  68. X(vkCreateDescriptorPool);
  69. X(vkCreateDescriptorSetLayout);
  70. X(vkCreateDescriptorUpdateTemplateKHR);
  71. X(vkCreateFence);
  72. X(vkCreateFramebuffer);
  73. X(vkCreateGraphicsPipelines);
  74. X(vkCreateImage);
  75. X(vkCreateImageView);
  76. X(vkCreatePipelineLayout);
  77. X(vkCreateQueryPool);
  78. X(vkCreateRenderPass);
  79. X(vkCreateSampler);
  80. X(vkCreateSemaphore);
  81. X(vkCreateShaderModule);
  82. X(vkCreateSwapchainKHR);
  83. X(vkDestroyBuffer);
  84. X(vkDestroyBufferView);
  85. X(vkDestroyCommandPool);
  86. X(vkDestroyDescriptorPool);
  87. X(vkDestroyDescriptorSetLayout);
  88. X(vkDestroyDescriptorUpdateTemplateKHR);
  89. X(vkDestroyFence);
  90. X(vkDestroyFramebuffer);
  91. X(vkDestroyImage);
  92. X(vkDestroyImageView);
  93. X(vkDestroyPipeline);
  94. X(vkDestroyPipelineLayout);
  95. X(vkDestroyQueryPool);
  96. X(vkDestroyRenderPass);
  97. X(vkDestroySampler);
  98. X(vkDestroySemaphore);
  99. X(vkDestroyShaderModule);
  100. X(vkDestroySwapchainKHR);
  101. X(vkDeviceWaitIdle);
  102. X(vkEndCommandBuffer);
  103. X(vkFreeCommandBuffers);
  104. X(vkFreeDescriptorSets);
  105. X(vkFreeMemory);
  106. X(vkGetBufferMemoryRequirements);
  107. X(vkGetDeviceQueue);
  108. X(vkGetFenceStatus);
  109. X(vkGetImageMemoryRequirements);
  110. X(vkGetQueryPoolResults);
  111. X(vkGetQueueCheckpointDataNV);
  112. X(vkMapMemory);
  113. X(vkQueueSubmit);
  114. X(vkResetFences);
  115. X(vkResetQueryPoolEXT);
  116. X(vkUnmapMemory);
  117. X(vkUpdateDescriptorSetWithTemplateKHR);
  118. X(vkUpdateDescriptorSets);
  119. X(vkWaitForFences);
  120. #undef X
  121. }
  122. } // Anonymous namespace
  123. bool Load(InstanceDispatch& dld) noexcept {
  124. #define X(name) Proc(dld.name, dld, #name)
  125. return X(vkCreateInstance) && X(vkEnumerateInstanceExtensionProperties);
  126. #undef X
  127. }
  128. bool Load(VkInstance instance, InstanceDispatch& dld) noexcept {
  129. #define X(name) Proc(dld.name, dld, #name, instance)
  130. // These functions may fail to load depending on the enabled extensions.
  131. // Don't return a failure on these.
  132. X(vkCreateDebugUtilsMessengerEXT);
  133. X(vkDestroyDebugUtilsMessengerEXT);
  134. X(vkDestroySurfaceKHR);
  135. X(vkGetPhysicalDeviceFeatures2KHR);
  136. X(vkGetPhysicalDeviceProperties2KHR);
  137. X(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
  138. X(vkGetPhysicalDeviceSurfaceFormatsKHR);
  139. X(vkGetPhysicalDeviceSurfacePresentModesKHR);
  140. X(vkGetPhysicalDeviceSurfaceSupportKHR);
  141. X(vkGetSwapchainImagesKHR);
  142. X(vkQueuePresentKHR);
  143. return X(vkCreateDevice) && X(vkDestroyDevice) && X(vkDestroyDevice) &&
  144. X(vkEnumerateDeviceExtensionProperties) && X(vkEnumeratePhysicalDevices) &&
  145. X(vkGetDeviceProcAddr) && X(vkGetPhysicalDeviceFormatProperties) &&
  146. X(vkGetPhysicalDeviceMemoryProperties) && X(vkGetPhysicalDeviceProperties) &&
  147. X(vkGetPhysicalDeviceQueueFamilyProperties);
  148. #undef X
  149. }
  150. const char* Exception::what() const noexcept {
  151. return ToString(result);
  152. }
  153. const char* ToString(VkResult result) noexcept {
  154. switch (result) {
  155. case VkResult::VK_SUCCESS:
  156. return "VK_SUCCESS";
  157. case VkResult::VK_NOT_READY:
  158. return "VK_NOT_READY";
  159. case VkResult::VK_TIMEOUT:
  160. return "VK_TIMEOUT";
  161. case VkResult::VK_EVENT_SET:
  162. return "VK_EVENT_SET";
  163. case VkResult::VK_EVENT_RESET:
  164. return "VK_EVENT_RESET";
  165. case VkResult::VK_INCOMPLETE:
  166. return "VK_INCOMPLETE";
  167. case VkResult::VK_ERROR_OUT_OF_HOST_MEMORY:
  168. return "VK_ERROR_OUT_OF_HOST_MEMORY";
  169. case VkResult::VK_ERROR_OUT_OF_DEVICE_MEMORY:
  170. return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
  171. case VkResult::VK_ERROR_INITIALIZATION_FAILED:
  172. return "VK_ERROR_INITIALIZATION_FAILED";
  173. case VkResult::VK_ERROR_DEVICE_LOST:
  174. return "VK_ERROR_DEVICE_LOST";
  175. case VkResult::VK_ERROR_MEMORY_MAP_FAILED:
  176. return "VK_ERROR_MEMORY_MAP_FAILED";
  177. case VkResult::VK_ERROR_LAYER_NOT_PRESENT:
  178. return "VK_ERROR_LAYER_NOT_PRESENT";
  179. case VkResult::VK_ERROR_EXTENSION_NOT_PRESENT:
  180. return "VK_ERROR_EXTENSION_NOT_PRESENT";
  181. case VkResult::VK_ERROR_FEATURE_NOT_PRESENT:
  182. return "VK_ERROR_FEATURE_NOT_PRESENT";
  183. case VkResult::VK_ERROR_INCOMPATIBLE_DRIVER:
  184. return "VK_ERROR_INCOMPATIBLE_DRIVER";
  185. case VkResult::VK_ERROR_TOO_MANY_OBJECTS:
  186. return "VK_ERROR_TOO_MANY_OBJECTS";
  187. case VkResult::VK_ERROR_FORMAT_NOT_SUPPORTED:
  188. return "VK_ERROR_FORMAT_NOT_SUPPORTED";
  189. case VkResult::VK_ERROR_FRAGMENTED_POOL:
  190. return "VK_ERROR_FRAGMENTED_POOL";
  191. case VkResult::VK_ERROR_OUT_OF_POOL_MEMORY:
  192. return "VK_ERROR_OUT_OF_POOL_MEMORY";
  193. case VkResult::VK_ERROR_INVALID_EXTERNAL_HANDLE:
  194. return "VK_ERROR_INVALID_EXTERNAL_HANDLE";
  195. case VkResult::VK_ERROR_SURFACE_LOST_KHR:
  196. return "VK_ERROR_SURFACE_LOST_KHR";
  197. case VkResult::VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
  198. return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
  199. case VkResult::VK_SUBOPTIMAL_KHR:
  200. return "VK_SUBOPTIMAL_KHR";
  201. case VkResult::VK_ERROR_OUT_OF_DATE_KHR:
  202. return "VK_ERROR_OUT_OF_DATE_KHR";
  203. case VkResult::VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
  204. return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
  205. case VkResult::VK_ERROR_VALIDATION_FAILED_EXT:
  206. return "VK_ERROR_VALIDATION_FAILED_EXT";
  207. case VkResult::VK_ERROR_INVALID_SHADER_NV:
  208. return "VK_ERROR_INVALID_SHADER_NV";
  209. case VkResult::VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT:
  210. return "VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT";
  211. case VkResult::VK_ERROR_FRAGMENTATION_EXT:
  212. return "VK_ERROR_FRAGMENTATION_EXT";
  213. case VkResult::VK_ERROR_NOT_PERMITTED_EXT:
  214. return "VK_ERROR_NOT_PERMITTED_EXT";
  215. case VkResult::VK_ERROR_INVALID_DEVICE_ADDRESS_EXT:
  216. return "VK_ERROR_INVALID_DEVICE_ADDRESS_EXT";
  217. case VkResult::VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT:
  218. return "VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT";
  219. }
  220. return "Unknown";
  221. }
  222. void Destroy(VkInstance instance, const InstanceDispatch& dld) noexcept {
  223. dld.vkDestroyInstance(instance, nullptr);
  224. }
  225. void Destroy(VkDevice device, const InstanceDispatch& dld) noexcept {
  226. dld.vkDestroyDevice(device, nullptr);
  227. }
  228. void Destroy(VkDevice device, VkBuffer handle, const DeviceDispatch& dld) noexcept {
  229. dld.vkDestroyBuffer(device, handle, nullptr);
  230. }
  231. void Destroy(VkDevice device, VkBufferView handle, const DeviceDispatch& dld) noexcept {
  232. dld.vkDestroyBufferView(device, handle, nullptr);
  233. }
  234. void Destroy(VkDevice device, VkCommandPool handle, const DeviceDispatch& dld) noexcept {
  235. dld.vkDestroyCommandPool(device, handle, nullptr);
  236. }
  237. void Destroy(VkDevice device, VkDescriptorPool handle, const DeviceDispatch& dld) noexcept {
  238. dld.vkDestroyDescriptorPool(device, handle, nullptr);
  239. }
  240. void Destroy(VkDevice device, VkDescriptorSetLayout handle, const DeviceDispatch& dld) noexcept {
  241. dld.vkDestroyDescriptorSetLayout(device, handle, nullptr);
  242. }
  243. void Destroy(VkDevice device, VkDescriptorUpdateTemplateKHR handle,
  244. const DeviceDispatch& dld) noexcept {
  245. dld.vkDestroyDescriptorUpdateTemplateKHR(device, handle, nullptr);
  246. }
  247. void Destroy(VkDevice device, VkDeviceMemory handle, const DeviceDispatch& dld) noexcept {
  248. dld.vkFreeMemory(device, handle, nullptr);
  249. }
  250. void Destroy(VkDevice device, VkFence handle, const DeviceDispatch& dld) noexcept {
  251. dld.vkDestroyFence(device, handle, nullptr);
  252. }
  253. void Destroy(VkDevice device, VkFramebuffer handle, const DeviceDispatch& dld) noexcept {
  254. dld.vkDestroyFramebuffer(device, handle, nullptr);
  255. }
  256. void Destroy(VkDevice device, VkImage handle, const DeviceDispatch& dld) noexcept {
  257. dld.vkDestroyImage(device, handle, nullptr);
  258. }
  259. void Destroy(VkDevice device, VkImageView handle, const DeviceDispatch& dld) noexcept {
  260. dld.vkDestroyImageView(device, handle, nullptr);
  261. }
  262. void Destroy(VkDevice device, VkPipeline handle, const DeviceDispatch& dld) noexcept {
  263. dld.vkDestroyPipeline(device, handle, nullptr);
  264. }
  265. void Destroy(VkDevice device, VkPipelineLayout handle, const DeviceDispatch& dld) noexcept {
  266. dld.vkDestroyPipelineLayout(device, handle, nullptr);
  267. }
  268. void Destroy(VkDevice device, VkQueryPool handle, const DeviceDispatch& dld) noexcept {
  269. dld.vkDestroyQueryPool(device, handle, nullptr);
  270. }
  271. void Destroy(VkDevice device, VkRenderPass handle, const DeviceDispatch& dld) noexcept {
  272. dld.vkDestroyRenderPass(device, handle, nullptr);
  273. }
  274. void Destroy(VkDevice device, VkSampler handle, const DeviceDispatch& dld) noexcept {
  275. dld.vkDestroySampler(device, handle, nullptr);
  276. }
  277. void Destroy(VkDevice device, VkSwapchainKHR handle, const DeviceDispatch& dld) noexcept {
  278. dld.vkDestroySwapchainKHR(device, handle, nullptr);
  279. }
  280. void Destroy(VkDevice device, VkSemaphore handle, const DeviceDispatch& dld) noexcept {
  281. dld.vkDestroySemaphore(device, handle, nullptr);
  282. }
  283. void Destroy(VkDevice device, VkShaderModule handle, const DeviceDispatch& dld) noexcept {
  284. dld.vkDestroyShaderModule(device, handle, nullptr);
  285. }
  286. void Destroy(VkInstance instance, VkDebugUtilsMessengerEXT handle,
  287. const InstanceDispatch& dld) noexcept {
  288. dld.vkDestroyDebugUtilsMessengerEXT(instance, handle, nullptr);
  289. }
  290. void Destroy(VkInstance instance, VkSurfaceKHR handle, const InstanceDispatch& dld) noexcept {
  291. dld.vkDestroySurfaceKHR(instance, handle, nullptr);
  292. }
  293. VkResult Free(VkDevice device, VkDescriptorPool handle, Span<VkDescriptorSet> sets,
  294. const DeviceDispatch& dld) noexcept {
  295. return dld.vkFreeDescriptorSets(device, handle, sets.size(), sets.data());
  296. }
  297. VkResult Free(VkDevice device, VkCommandPool handle, Span<VkCommandBuffer> buffers,
  298. const DeviceDispatch& dld) noexcept {
  299. dld.vkFreeCommandBuffers(device, handle, buffers.size(), buffers.data());
  300. return VK_SUCCESS;
  301. }
  302. Instance Instance::Create(Span<const char*> layers, Span<const char*> extensions,
  303. InstanceDispatch& dld) noexcept {
  304. VkApplicationInfo application_info;
  305. application_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  306. application_info.pNext = nullptr;
  307. application_info.pApplicationName = "yuzu Emulator";
  308. application_info.applicationVersion = VK_MAKE_VERSION(0, 1, 0);
  309. application_info.pEngineName = "yuzu Emulator";
  310. application_info.engineVersion = VK_MAKE_VERSION(0, 1, 0);
  311. application_info.apiVersion = VK_API_VERSION_1_1;
  312. VkInstanceCreateInfo ci;
  313. ci.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  314. ci.pNext = nullptr;
  315. ci.flags = 0;
  316. ci.pApplicationInfo = &application_info;
  317. ci.enabledLayerCount = layers.size();
  318. ci.ppEnabledLayerNames = layers.data();
  319. ci.enabledExtensionCount = extensions.size();
  320. ci.ppEnabledExtensionNames = extensions.data();
  321. VkInstance instance;
  322. if (dld.vkCreateInstance(&ci, nullptr, &instance) != VK_SUCCESS) {
  323. // Failed to create the instance.
  324. return {};
  325. }
  326. if (!Proc(dld.vkDestroyInstance, dld, "vkDestroyInstance", instance)) {
  327. // We successfully created an instance but the destroy function couldn't be loaded.
  328. // This is a good moment to panic.
  329. return {};
  330. }
  331. return Instance(instance, dld);
  332. }
  333. std::optional<std::vector<VkPhysicalDevice>> Instance::EnumeratePhysicalDevices() {
  334. u32 num;
  335. if (dld->vkEnumeratePhysicalDevices(handle, &num, nullptr) != VK_SUCCESS) {
  336. return std::nullopt;
  337. }
  338. std::vector<VkPhysicalDevice> physical_devices(num);
  339. if (dld->vkEnumeratePhysicalDevices(handle, &num, physical_devices.data()) != VK_SUCCESS) {
  340. return std::nullopt;
  341. }
  342. return physical_devices;
  343. }
  344. DebugCallback Instance::TryCreateDebugCallback(
  345. PFN_vkDebugUtilsMessengerCallbackEXT callback) noexcept {
  346. VkDebugUtilsMessengerCreateInfoEXT ci;
  347. ci.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  348. ci.pNext = nullptr;
  349. ci.flags = 0;
  350. ci.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
  351. VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
  352. VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
  353. VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
  354. ci.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
  355. VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
  356. VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  357. ci.pfnUserCallback = callback;
  358. ci.pUserData = nullptr;
  359. VkDebugUtilsMessengerEXT messenger;
  360. if (dld->vkCreateDebugUtilsMessengerEXT(handle, &ci, nullptr, &messenger) != VK_SUCCESS) {
  361. return {};
  362. }
  363. return DebugCallback(messenger, handle, *dld);
  364. }
  365. } // namespace Vulkan::vk