rendering_context_driver_vulkan.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifdef USE_VOLK
  35. #include <volk.h>
  36. #else
  37. #include <vulkan/vulkan.h>
  38. #endif
  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. bool debug_report_functions_available() const {
  70. return CreateDebugReportCallbackEXT != nullptr &&
  71. DebugReportMessageEXT != nullptr &&
  72. DestroyDebugReportCallbackEXT != nullptr;
  73. }
  74. };
  75. private:
  76. struct DeviceQueueFamilies {
  77. TightLocalVector<VkQueueFamilyProperties> properties;
  78. };
  79. VkInstance instance = VK_NULL_HANDLE;
  80. uint32_t instance_api_version = VK_API_VERSION_1_0;
  81. HashMap<CharString, bool> requested_instance_extensions;
  82. HashSet<CharString> enabled_instance_extension_names;
  83. TightLocalVector<Device> driver_devices;
  84. TightLocalVector<VkPhysicalDevice> physical_devices;
  85. TightLocalVector<DeviceQueueFamilies> device_queue_families;
  86. VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;
  87. VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
  88. Functions functions;
  89. Error _initialize_vulkan_version();
  90. void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);
  91. Error _initialize_instance_extensions();
  92. Error _initialize_instance();
  93. Error _initialize_devices();
  94. void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);
  95. // Static callbacks.
  96. 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);
  97. 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);
  98. protected:
  99. Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;
  100. // Can be overridden by platform-specific drivers.
  101. virtual const char *_get_platform_surface_extension() const { return nullptr; }
  102. virtual bool _use_validation_layers() const;
  103. virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);
  104. public:
  105. virtual Error initialize() override;
  106. virtual const Device &device_get(uint32_t p_device_index) const override;
  107. virtual uint32_t device_get_count() const override;
  108. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  109. virtual RenderingDeviceDriver *driver_create() override;
  110. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  111. virtual SurfaceID surface_create(const void *p_platform_data) override;
  112. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  113. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  114. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  115. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  116. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  117. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  118. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  119. virtual void surface_destroy(SurfaceID p_surface) override;
  120. virtual bool is_debug_utils_enabled() const override;
  121. // Vulkan-only methods.
  122. struct Surface {
  123. VkSurfaceKHR vk_surface = VK_NULL_HANDLE;
  124. uint32_t width = 0;
  125. uint32_t height = 0;
  126. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  127. bool needs_resize = false;
  128. };
  129. VkInstance instance_get() const;
  130. VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;
  131. uint32_t queue_family_get_count(uint32_t p_device_index) const;
  132. VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;
  133. bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;
  134. const Functions &functions_get() const;
  135. RenderingContextDriverVulkan();
  136. virtual ~RenderingContextDriverVulkan() override;
  137. };
  138. #endif // VULKAN_ENABLED
  139. #endif // RENDERING_CONTEXT_DRIVER_VULKAN_H