rendering_context_driver_vulkan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef RENDERING_CONTEXT_DRIVER_VULKAN_H
  31. #define RENDERING_CONTEXT_DRIVER_VULKAN_H
  32. #ifdef VULKAN_ENABLED
  33. #include "servers/rendering/rendering_context_driver.h"
  34. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  35. #define VK_TRACK_DRIVER_MEMORY
  36. #define VK_TRACK_DEVICE_MEMORY
  37. #endif
  38. #include "drivers/vulkan/godot_vulkan.h"
  39. class RenderingContextDriverVulkan : public RenderingContextDriver {
  40. public:
  41. struct Functions {
  42. // Physical device.
  43. PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2 = nullptr;
  44. PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;
  45. // Device.
  46. PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;
  47. // Surfaces.
  48. PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr;
  49. PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr;
  50. PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
  51. PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr;
  52. // Debug utils.
  53. PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT = nullptr;
  54. PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT = nullptr;
  55. PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT = nullptr;
  56. PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT = nullptr;
  57. PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT = nullptr;
  58. bool debug_util_functions_available() const {
  59. return CreateDebugUtilsMessengerEXT != nullptr &&
  60. DestroyDebugUtilsMessengerEXT != nullptr &&
  61. CmdBeginDebugUtilsLabelEXT != nullptr &&
  62. CmdEndDebugUtilsLabelEXT != nullptr &&
  63. SetDebugUtilsObjectNameEXT != nullptr;
  64. }
  65. // Debug report.
  66. PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallbackEXT = nullptr;
  67. PFN_vkDebugReportMessageEXT DebugReportMessageEXT = nullptr;
  68. PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallbackEXT = nullptr;
  69. // Debug marker extensions.
  70. PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
  71. PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
  72. PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
  73. PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
  74. bool debug_report_functions_available() const {
  75. return CreateDebugReportCallbackEXT != nullptr &&
  76. DebugReportMessageEXT != nullptr &&
  77. DestroyDebugReportCallbackEXT != nullptr;
  78. }
  79. };
  80. private:
  81. struct DeviceQueueFamilies {
  82. TightLocalVector<VkQueueFamilyProperties> properties;
  83. };
  84. VkInstance instance = VK_NULL_HANDLE;
  85. uint32_t instance_api_version = VK_API_VERSION_1_0;
  86. HashMap<CharString, bool> requested_instance_extensions;
  87. HashSet<CharString> enabled_instance_extension_names;
  88. TightLocalVector<Device> driver_devices;
  89. TightLocalVector<VkPhysicalDevice> physical_devices;
  90. TightLocalVector<DeviceQueueFamilies> device_queue_families;
  91. VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;
  92. VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
  93. Functions functions;
  94. Error _initialize_vulkan_version();
  95. void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);
  96. Error _initialize_instance_extensions();
  97. Error _initialize_instance();
  98. Error _initialize_devices();
  99. void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);
  100. // Static callbacks.
  101. 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);
  102. 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);
  103. // Debug marker extensions.
  104. VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
  105. protected:
  106. Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;
  107. // Can be overridden by platform-specific drivers.
  108. virtual const char *_get_platform_surface_extension() const { return nullptr; }
  109. virtual bool _use_validation_layers() const;
  110. virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);
  111. public:
  112. virtual Error initialize() override;
  113. virtual const Device &device_get(uint32_t p_device_index) const override;
  114. virtual uint32_t device_get_count() const override;
  115. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  116. virtual RenderingDeviceDriver *driver_create() override;
  117. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  118. virtual SurfaceID surface_create(const void *p_platform_data) override;
  119. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  120. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  121. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  122. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  123. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  124. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  125. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  126. virtual void surface_destroy(SurfaceID p_surface) override;
  127. virtual bool is_debug_utils_enabled() const override;
  128. // Vulkan-only methods.
  129. struct Surface {
  130. VkSurfaceKHR vk_surface = VK_NULL_HANDLE;
  131. uint32_t width = 0;
  132. uint32_t height = 0;
  133. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  134. bool needs_resize = false;
  135. };
  136. VkInstance instance_get() const;
  137. VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;
  138. uint32_t queue_family_get_count(uint32_t p_device_index) const;
  139. VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;
  140. bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;
  141. const Functions &functions_get() const;
  142. static VkAllocationCallbacks *get_allocation_callbacks(VkObjectType p_type);
  143. #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
  144. enum VkTrackedObjectType {
  145. VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR = VK_OBJECT_TYPE_COMMAND_POOL + 1,
  146. VK_TRACKED_OBJECT_TYPE_SURFACE,
  147. VK_TRACKED_OBJECT_TYPE_SWAPCHAIN,
  148. VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT,
  149. VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT,
  150. VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE,
  151. VK_TRACKED_OBJECT_TYPE_VMA,
  152. VK_TRACKED_OBJECT_TYPE_COUNT
  153. };
  154. enum VkTrackedSystemAllocationScope {
  155. VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT = VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE + 1
  156. };
  157. #endif
  158. const char *get_tracked_object_name(uint32_t p_type_index) const override;
  159. #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
  160. uint64_t get_tracked_object_type_count() const override;
  161. #endif
  162. #if defined(VK_TRACK_DRIVER_MEMORY)
  163. uint64_t get_driver_total_memory() const override;
  164. uint64_t get_driver_allocation_count() const override;
  165. uint64_t get_driver_memory_by_object_type(uint32_t p_type) const override;
  166. uint64_t get_driver_allocs_by_object_type(uint32_t p_type) const override;
  167. #endif
  168. #if defined(VK_TRACK_DEVICE_MEMORY)
  169. uint64_t get_device_total_memory() const override;
  170. uint64_t get_device_allocation_count() const override;
  171. uint64_t get_device_memory_by_object_type(uint32_t p_type) const override;
  172. uint64_t get_device_allocs_by_object_type(uint32_t p_type) const override;
  173. static VKAPI_ATTR void VKAPI_CALL memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data);
  174. #endif
  175. RenderingContextDriverVulkan();
  176. virtual ~RenderingContextDriverVulkan() override;
  177. };
  178. #endif // VULKAN_ENABLED
  179. #endif // RENDERING_CONTEXT_DRIVER_VULKAN_H