vulkan_context.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_list.h"
  33. #include "core/map.h"
  34. #include "core/os/mutex.h"
  35. #include "core/rid_owner.h"
  36. #include "core/ustring.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;
  51. VkDevice device;
  52. //present
  53. bool queues_initialized;
  54. uint32_t graphics_queue_family_index;
  55. uint32_t present_queue_family_index;
  56. bool separate_present_queue;
  57. VkQueue graphics_queue;
  58. VkQueue present_queue;
  59. VkColorSpaceKHR color_space;
  60. VkFormat format;
  61. VkSemaphore image_acquired_semaphores[FRAME_LAG];
  62. VkSemaphore draw_complete_semaphores[FRAME_LAG];
  63. VkSemaphore image_ownership_semaphores[FRAME_LAG];
  64. int frame_index;
  65. VkFence fences[FRAME_LAG];
  66. VkPhysicalDeviceMemoryProperties memory_properties;
  67. VkPhysicalDeviceFeatures physical_device_features;
  68. typedef struct {
  69. VkImage image;
  70. VkCommandBuffer graphics_to_present_cmd;
  71. VkImageView view;
  72. VkFramebuffer framebuffer;
  73. } SwapchainImageResources;
  74. struct Window {
  75. bool is_minimzed;
  76. VkSurfaceKHR surface;
  77. VkSwapchainKHR swapchain;
  78. SwapchainImageResources *swapchain_image_resources;
  79. VkPresentModeKHR presentMode;
  80. uint32_t current_buffer;
  81. int width;
  82. int height;
  83. VkCommandPool present_cmd_pool; //for separate present queue
  84. VkRenderPass render_pass;
  85. Window() {
  86. width = 0;
  87. height = 0;
  88. render_pass = VK_NULL_HANDLE;
  89. current_buffer = 0;
  90. surface = VK_NULL_HANDLE;
  91. swapchain_image_resources = VK_NULL_HANDLE;
  92. swapchain = VK_NULL_HANDLE;
  93. is_minimzed = false;
  94. presentMode = VK_PRESENT_MODE_FIFO_KHR;
  95. }
  96. };
  97. struct LocalDevice {
  98. bool waiting = false;
  99. VkDevice device;
  100. VkQueue queue;
  101. };
  102. RID_Owner<LocalDevice, true> local_device_owner;
  103. Map<DisplayServer::WindowID, Window> windows;
  104. uint32_t swapchainImageCount;
  105. //commands
  106. bool prepared;
  107. //extensions
  108. bool VK_KHR_incremental_present_enabled;
  109. bool VK_GOOGLE_display_timing_enabled;
  110. const char **instance_validation_layers;
  111. uint32_t enabled_extension_count;
  112. uint32_t enabled_layer_count;
  113. const char *extension_names[MAX_EXTENSIONS];
  114. const char *enabled_layers[MAX_LAYERS];
  115. PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
  116. PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
  117. PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
  118. PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
  119. PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
  120. PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
  121. PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
  122. PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
  123. PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
  124. PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
  125. PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
  126. PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
  127. PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
  128. PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
  129. PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
  130. PFN_vkQueuePresentKHR fpQueuePresentKHR;
  131. PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
  132. PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
  133. VkDebugUtilsMessengerEXT dbg_messenger;
  134. Error _create_validation_layers();
  135. Error _initialize_extensions();
  136. VkBool32 _check_layers(uint32_t check_count, const char **check_names, uint32_t layer_count, VkLayerProperties *layers);
  137. static VKAPI_ATTR VkBool32 VKAPI_CALL _debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
  138. VkDebugUtilsMessageTypeFlagsEXT messageType,
  139. const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
  140. void *pUserData);
  141. Error _create_physical_device();
  142. Error _initialize_queues(VkSurfaceKHR surface);
  143. Error _create_device();
  144. Error _clean_up_swap_chain(Window *window);
  145. Error _update_swap_chain(Window *window);
  146. Error _create_swap_chain();
  147. Error _create_semaphores();
  148. Vector<VkCommandBuffer> command_buffer_queue;
  149. int command_buffer_count;
  150. protected:
  151. virtual const char *_get_platform_surface_extension() const = 0;
  152. // virtual VkResult _create_surface(VkSurfaceKHR *surface, VkInstance p_instance) = 0;
  153. virtual Error _window_create(DisplayServer::WindowID p_window_id, VkSurfaceKHR p_surface, int p_width, int p_height);
  154. VkInstance _get_instance() {
  155. return inst;
  156. }
  157. bool buffers_prepared;
  158. bool use_validation_layers;
  159. public:
  160. VkDevice get_device();
  161. VkPhysicalDevice get_physical_device();
  162. int get_swapchain_image_count() const;
  163. uint32_t get_graphics_queue() const;
  164. void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);
  165. int window_get_width(DisplayServer::WindowID p_window = 0);
  166. int window_get_height(DisplayServer::WindowID p_window = 0);
  167. void window_destroy(DisplayServer::WindowID p_window_id);
  168. VkFramebuffer window_get_framebuffer(DisplayServer::WindowID p_window = 0);
  169. VkRenderPass window_get_render_pass(DisplayServer::WindowID p_window = 0);
  170. RID local_device_create();
  171. VkDevice local_device_get_vk_device(RID p_local_device);
  172. void local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count);
  173. void local_device_sync(RID p_local_device);
  174. void local_device_free(RID p_local_device);
  175. VkFormat get_screen_format() const;
  176. VkPhysicalDeviceLimits get_device_limits() const;
  177. void set_setup_buffer(const VkCommandBuffer &pCommandBuffer);
  178. void append_command_buffer(const VkCommandBuffer &pCommandBuffer);
  179. void resize_notify();
  180. void flush(bool p_flush_setup = false, bool p_flush_pending = false);
  181. Error prepare_buffers();
  182. Error swap_buffers();
  183. Error initialize();
  184. VulkanContext();
  185. virtual ~VulkanContext();
  186. };
  187. #endif // VULKAN_DEVICE_H