vulkan_context.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* vulkan_context.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 VULKAN_CONTEXT_H
  31. #define VULKAN_CONTEXT_H
  32. #include "core/error/error_list.h"
  33. #include "core/os/mutex.h"
  34. #include "core/string/ustring.h"
  35. #include "core/templates/map.h"
  36. #include "core/templates/rid_owner.h"
  37. #include "servers/display_server.h"
  38. #include <vulkan/vulkan.h>
  39. class VulkanContext {
  40. enum {
  41. MAX_EXTENSIONS = 128,
  42. MAX_LAYERS = 64,
  43. FRAME_LAG = 2
  44. };
  45. VkInstance inst;
  46. VkSurfaceKHR surface;
  47. VkPhysicalDevice gpu;
  48. VkPhysicalDeviceProperties gpu_props;
  49. uint32_t queue_family_count;
  50. VkQueueFamilyProperties *queue_props = nullptr;
  51. VkDevice device;
  52. bool device_initialized = false;
  53. bool inst_initialized = false;
  54. bool buffers_prepared = false;
  55. // Present queue.
  56. bool queues_initialized = false;
  57. uint32_t graphics_queue_family_index;
  58. uint32_t present_queue_family_index;
  59. bool separate_present_queue;
  60. VkQueue graphics_queue;
  61. VkQueue present_queue;
  62. VkColorSpaceKHR color_space;
  63. VkFormat format;
  64. VkSemaphore image_acquired_semaphores[FRAME_LAG];
  65. VkSemaphore draw_complete_semaphores[FRAME_LAG];
  66. VkSemaphore image_ownership_semaphores[FRAME_LAG];
  67. int frame_index;
  68. VkFence fences[FRAME_LAG];
  69. VkPhysicalDeviceMemoryProperties memory_properties;
  70. VkPhysicalDeviceFeatures physical_device_features;
  71. typedef struct {
  72. VkImage image;
  73. VkCommandBuffer graphics_to_present_cmd;
  74. VkImageView view;
  75. VkFramebuffer framebuffer;
  76. } SwapchainImageResources;
  77. struct Window {
  78. VkSurfaceKHR surface = VK_NULL_HANDLE;
  79. VkSwapchainKHR swapchain = VK_NULL_HANDLE;
  80. SwapchainImageResources *swapchain_image_resources = VK_NULL_HANDLE;
  81. VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
  82. uint32_t current_buffer = 0;
  83. int width = 0;
  84. int height = 0;
  85. VkCommandPool present_cmd_pool; // For separate present queue.
  86. VkRenderPass render_pass = VK_NULL_HANDLE;
  87. };
  88. struct LocalDevice {
  89. bool waiting = false;
  90. VkDevice device;
  91. VkQueue queue;
  92. };
  93. RID_Owner<LocalDevice, true> local_device_owner;
  94. Map<DisplayServer::WindowID, Window> windows;
  95. uint32_t swapchainImageCount = 0;
  96. // Commands.
  97. bool prepared;
  98. Vector<VkCommandBuffer> command_buffer_queue;
  99. int command_buffer_count = 1;
  100. // Extensions.
  101. bool VK_KHR_incremental_present_enabled = true;
  102. bool VK_GOOGLE_display_timing_enabled = true;
  103. uint32_t enabled_extension_count = 0;
  104. const char *extension_names[MAX_EXTENSIONS];
  105. uint32_t enabled_layer_count = 0;
  106. const char *enabled_layers[MAX_LAYERS];
  107. PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
  108. PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
  109. PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
  110. PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
  111. PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
  112. PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
  113. PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
  114. PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
  115. PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
  116. PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
  117. PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
  118. PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
  119. PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
  120. PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
  121. PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
  122. PFN_vkQueuePresentKHR fpQueuePresentKHR;
  123. PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
  124. PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
  125. VkDebugUtilsMessengerEXT dbg_messenger;
  126. Error _create_validation_layers();
  127. Error _initialize_extensions();
  128. VkBool32 _check_layers(uint32_t check_count, const char **check_names, uint32_t layer_count, VkLayerProperties *layers);
  129. static VKAPI_ATTR VkBool32 VKAPI_CALL _debug_messenger_callback(
  130. VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
  131. VkDebugUtilsMessageTypeFlagsEXT messageType,
  132. const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
  133. void *pUserData);
  134. Error _create_physical_device();
  135. Error _initialize_queues(VkSurfaceKHR surface);
  136. Error _create_device();
  137. Error _clean_up_swap_chain(Window *window);
  138. Error _update_swap_chain(Window *window);
  139. Error _create_swap_chain();
  140. Error _create_semaphores();
  141. protected:
  142. virtual const char *_get_platform_surface_extension() const = 0;
  143. // Enabled via command line argument.
  144. bool use_validation_layers = false;
  145. virtual Error _window_create(DisplayServer::WindowID p_window_id, VkSurfaceKHR p_surface, int p_width, int p_height);
  146. VkInstance _get_instance() {
  147. return inst;
  148. }
  149. public:
  150. VkDevice get_device();
  151. VkPhysicalDevice get_physical_device();
  152. int get_swapchain_image_count() const;
  153. uint32_t get_graphics_queue() const;
  154. void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);
  155. int window_get_width(DisplayServer::WindowID p_window = 0);
  156. int window_get_height(DisplayServer::WindowID p_window = 0);
  157. void window_destroy(DisplayServer::WindowID p_window_id);
  158. VkFramebuffer window_get_framebuffer(DisplayServer::WindowID p_window = 0);
  159. VkRenderPass window_get_render_pass(DisplayServer::WindowID p_window = 0);
  160. RID local_device_create();
  161. VkDevice local_device_get_vk_device(RID p_local_device);
  162. void local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count);
  163. void local_device_sync(RID p_local_device);
  164. void local_device_free(RID p_local_device);
  165. VkFormat get_screen_format() const;
  166. VkPhysicalDeviceLimits get_device_limits() const;
  167. void set_setup_buffer(const VkCommandBuffer &pCommandBuffer);
  168. void append_command_buffer(const VkCommandBuffer &pCommandBuffer);
  169. void resize_notify();
  170. void flush(bool p_flush_setup = false, bool p_flush_pending = false);
  171. Error prepare_buffers();
  172. Error swap_buffers();
  173. Error initialize();
  174. VulkanContext();
  175. virtual ~VulkanContext();
  176. };
  177. #endif // VULKAN_DEVICE_H