rendering_context_driver_vulkan.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "drivers/vulkan/godot_vulkan.h"
  35. class RenderingContextDriverVulkan : public RenderingContextDriver {
  36. public:
  37. struct Functions {
  38. // Physical device.
  39. PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2 = nullptr;
  40. PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;
  41. // Device.
  42. PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;
  43. // Surfaces.
  44. PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr;
  45. PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr;
  46. PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
  47. PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr;
  48. // Debug utils.
  49. PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT = nullptr;
  50. PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT = nullptr;
  51. PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT = nullptr;
  52. PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT = nullptr;
  53. PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT = nullptr;
  54. bool debug_util_functions_available() const {
  55. return CreateDebugUtilsMessengerEXT != nullptr &&
  56. DestroyDebugUtilsMessengerEXT != nullptr &&
  57. CmdBeginDebugUtilsLabelEXT != nullptr &&
  58. CmdEndDebugUtilsLabelEXT != nullptr &&
  59. SetDebugUtilsObjectNameEXT != nullptr;
  60. }
  61. // Debug report.
  62. PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallbackEXT = nullptr;
  63. PFN_vkDebugReportMessageEXT DebugReportMessageEXT = nullptr;
  64. PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallbackEXT = nullptr;
  65. bool debug_report_functions_available() const {
  66. return CreateDebugReportCallbackEXT != nullptr &&
  67. DebugReportMessageEXT != nullptr &&
  68. DestroyDebugReportCallbackEXT != nullptr;
  69. }
  70. };
  71. private:
  72. struct DeviceQueueFamilies {
  73. TightLocalVector<VkQueueFamilyProperties> properties;
  74. };
  75. VkInstance instance = VK_NULL_HANDLE;
  76. uint32_t instance_api_version = VK_API_VERSION_1_0;
  77. HashMap<CharString, bool> requested_instance_extensions;
  78. HashSet<CharString> enabled_instance_extension_names;
  79. TightLocalVector<Device> driver_devices;
  80. TightLocalVector<VkPhysicalDevice> physical_devices;
  81. TightLocalVector<DeviceQueueFamilies> device_queue_families;
  82. VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;
  83. VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
  84. Functions functions;
  85. Error _initialize_vulkan_version();
  86. void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);
  87. Error _initialize_instance_extensions();
  88. Error _initialize_instance();
  89. Error _initialize_devices();
  90. void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);
  91. // Static callbacks.
  92. 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);
  93. 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);
  94. protected:
  95. Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;
  96. // Can be overridden by platform-specific drivers.
  97. virtual const char *_get_platform_surface_extension() const { return nullptr; }
  98. virtual bool _use_validation_layers() const;
  99. virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);
  100. public:
  101. virtual Error initialize() override;
  102. virtual const Device &device_get(uint32_t p_device_index) const override;
  103. virtual uint32_t device_get_count() const override;
  104. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  105. virtual RenderingDeviceDriver *driver_create() override;
  106. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  107. virtual SurfaceID surface_create(const void *p_platform_data) override;
  108. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  109. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  110. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  111. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  112. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  113. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  114. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  115. virtual void surface_destroy(SurfaceID p_surface) override;
  116. virtual bool is_debug_utils_enabled() const override;
  117. // Vulkan-only methods.
  118. struct Surface {
  119. VkSurfaceKHR vk_surface = VK_NULL_HANDLE;
  120. uint32_t width = 0;
  121. uint32_t height = 0;
  122. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  123. bool needs_resize = false;
  124. };
  125. VkInstance instance_get() const;
  126. VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;
  127. uint32_t queue_family_get_count(uint32_t p_device_index) const;
  128. VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;
  129. bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;
  130. const Functions &functions_get() const;
  131. RenderingContextDriverVulkan();
  132. virtual ~RenderingContextDriverVulkan() override;
  133. };
  134. #endif // VULKAN_ENABLED
  135. #endif // RENDERING_CONTEXT_DRIVER_VULKAN_H