rendering_context_driver_vulkan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* rendering_context_driver_vulkan.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #ifdef VULKAN_ENABLED
  32. #include "servers/rendering/rendering_context_driver.h"
  33. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  34. #define VK_TRACK_DRIVER_MEMORY
  35. #define VK_TRACK_DEVICE_MEMORY
  36. #endif
  37. #include "drivers/vulkan/godot_vulkan.h"
  38. class RenderingContextDriverVulkan : public RenderingContextDriver {
  39. public:
  40. struct Functions {
  41. // Physical device.
  42. PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2 = nullptr;
  43. PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;
  44. // Device.
  45. PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;
  46. // Surfaces.
  47. PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr;
  48. PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr;
  49. PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
  50. PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr;
  51. // Debug utils.
  52. PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT = nullptr;
  53. PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT = nullptr;
  54. PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT = nullptr;
  55. PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT = nullptr;
  56. PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT = nullptr;
  57. bool debug_util_functions_available() const {
  58. return CreateDebugUtilsMessengerEXT != nullptr &&
  59. DestroyDebugUtilsMessengerEXT != nullptr &&
  60. CmdBeginDebugUtilsLabelEXT != nullptr &&
  61. CmdEndDebugUtilsLabelEXT != nullptr &&
  62. SetDebugUtilsObjectNameEXT != nullptr;
  63. }
  64. // Debug report.
  65. PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallbackEXT = nullptr;
  66. PFN_vkDebugReportMessageEXT DebugReportMessageEXT = nullptr;
  67. PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallbackEXT = nullptr;
  68. // Debug marker extensions.
  69. PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
  70. PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
  71. PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
  72. PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
  73. bool debug_report_functions_available() const {
  74. return CreateDebugReportCallbackEXT != nullptr &&
  75. DebugReportMessageEXT != nullptr &&
  76. DestroyDebugReportCallbackEXT != nullptr;
  77. }
  78. };
  79. private:
  80. struct DeviceQueueFamilies {
  81. TightLocalVector<VkQueueFamilyProperties> properties;
  82. };
  83. VkInstance instance = VK_NULL_HANDLE;
  84. uint32_t instance_api_version = VK_API_VERSION_1_0;
  85. HashMap<CharString, bool> requested_instance_extensions;
  86. HashSet<CharString> enabled_instance_extension_names;
  87. TightLocalVector<Device> driver_devices;
  88. TightLocalVector<VkPhysicalDevice> physical_devices;
  89. TightLocalVector<DeviceQueueFamilies> device_queue_families;
  90. VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;
  91. VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
  92. Functions functions;
  93. Error _initialize_vulkan_version();
  94. void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);
  95. Error _initialize_instance_extensions();
  96. Error _initialize_instance();
  97. Error _initialize_devices();
  98. void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);
  99. // Static callbacks.
  100. static VKAPI_ATTR VkBool32 VKAPI_CALL _debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT p_message_severity, VkDebugUtilsMessageTypeFlagsEXT p_message_type, const VkDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data);
  101. static VKAPI_ATTR VkBool32 VKAPI_CALL _debug_report_callback(VkDebugReportFlagsEXT p_flags, VkDebugReportObjectTypeEXT p_object_type, uint64_t p_object, size_t p_location, int32_t p_message_code, const char *p_layer_prefix, const char *p_message, void *p_user_data);
  102. // Debug marker extensions.
  103. VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
  104. protected:
  105. Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;
  106. // Can be overridden by platform-specific drivers.
  107. virtual const char *_get_platform_surface_extension() const { return nullptr; }
  108. virtual bool _use_validation_layers() const;
  109. virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);
  110. public:
  111. virtual Error initialize() override;
  112. virtual const Device &device_get(uint32_t p_device_index) const override;
  113. virtual uint32_t device_get_count() const override;
  114. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  115. virtual RenderingDeviceDriver *driver_create() override;
  116. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  117. virtual SurfaceID surface_create(const void *p_platform_data) override;
  118. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  119. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  120. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  121. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  122. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  123. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  124. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  125. virtual void surface_destroy(SurfaceID p_surface) override;
  126. virtual bool is_debug_utils_enabled() const override;
  127. // Vulkan-only methods.
  128. struct Surface {
  129. VkSurfaceKHR vk_surface = VK_NULL_HANDLE;
  130. uint32_t width = 0;
  131. uint32_t height = 0;
  132. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  133. bool needs_resize = false;
  134. };
  135. VkInstance instance_get() const;
  136. VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;
  137. uint32_t queue_family_get_count(uint32_t p_device_index) const;
  138. VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;
  139. bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;
  140. const Functions &functions_get() const;
  141. static VkAllocationCallbacks *get_allocation_callbacks(VkObjectType p_type);
  142. #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
  143. enum VkTrackedObjectType {
  144. VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR = VK_OBJECT_TYPE_COMMAND_POOL + 1,
  145. VK_TRACKED_OBJECT_TYPE_SURFACE,
  146. VK_TRACKED_OBJECT_TYPE_SWAPCHAIN,
  147. VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT,
  148. VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT,
  149. VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE,
  150. VK_TRACKED_OBJECT_TYPE_VMA,
  151. VK_TRACKED_OBJECT_TYPE_COUNT
  152. };
  153. enum VkTrackedSystemAllocationScope {
  154. VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT = VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE + 1
  155. };
  156. #endif
  157. const char *get_tracked_object_name(uint32_t p_type_index) const override;
  158. #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
  159. uint64_t get_tracked_object_type_count() const override;
  160. #endif
  161. #if defined(VK_TRACK_DRIVER_MEMORY)
  162. uint64_t get_driver_total_memory() const override;
  163. uint64_t get_driver_allocation_count() const override;
  164. uint64_t get_driver_memory_by_object_type(uint32_t p_type) const override;
  165. uint64_t get_driver_allocs_by_object_type(uint32_t p_type) const override;
  166. #endif
  167. #if defined(VK_TRACK_DEVICE_MEMORY)
  168. uint64_t get_device_total_memory() const override;
  169. uint64_t get_device_allocation_count() const override;
  170. uint64_t get_device_memory_by_object_type(uint32_t p_type) const override;
  171. uint64_t get_device_allocs_by_object_type(uint32_t p_type) const override;
  172. static VKAPI_ATTR void VKAPI_CALL memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data);
  173. #endif
  174. RenderingContextDriverVulkan();
  175. virtual ~RenderingContextDriverVulkan() override;
  176. };
  177. #endif // VULKAN_ENABLED