Path: blob/master/drivers/vulkan/rendering_context_driver_vulkan.h
9903 views
/**************************************************************************/1/* rendering_context_driver_vulkan.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#ifdef VULKAN_ENABLED3334#include "servers/rendering/rendering_context_driver.h"3536#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)37#define VK_TRACK_DRIVER_MEMORY38#define VK_TRACK_DEVICE_MEMORY39#endif4041#include "drivers/vulkan/godot_vulkan.h"4243class RenderingContextDriverVulkan : public RenderingContextDriver {44public:45struct Functions {46// Physical device.47PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2 = nullptr;48PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;4950// Device.51PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;5253// Surfaces.54PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr;55PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr;56PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;57PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr;5859// Debug utils.60PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT = nullptr;61PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT = nullptr;62PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT = nullptr;63PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT = nullptr;64PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT = nullptr;6566bool debug_util_functions_available() const {67return CreateDebugUtilsMessengerEXT != nullptr &&68DestroyDebugUtilsMessengerEXT != nullptr &&69CmdBeginDebugUtilsLabelEXT != nullptr &&70CmdEndDebugUtilsLabelEXT != nullptr &&71SetDebugUtilsObjectNameEXT != nullptr;72}7374// Debug report.75PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallbackEXT = nullptr;76PFN_vkDebugReportMessageEXT DebugReportMessageEXT = nullptr;77PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallbackEXT = nullptr;7879// Debug marker extensions.80PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;81PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;82PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;83PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;8485bool debug_report_functions_available() const {86return CreateDebugReportCallbackEXT != nullptr &&87DebugReportMessageEXT != nullptr &&88DestroyDebugReportCallbackEXT != nullptr;89}90};9192private:93struct DeviceQueueFamilies {94TightLocalVector<VkQueueFamilyProperties> properties;95};9697VkInstance instance = VK_NULL_HANDLE;98uint32_t instance_api_version = VK_API_VERSION_1_0;99HashMap<CharString, bool> requested_instance_extensions;100HashSet<CharString> enabled_instance_extension_names;101TightLocalVector<Device> driver_devices;102TightLocalVector<VkPhysicalDevice> physical_devices;103TightLocalVector<DeviceQueueFamilies> device_queue_families;104VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;105VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;106Functions functions;107108Error _initialize_vulkan_version();109void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);110Error _initialize_instance_extensions();111Error _initialize_instance();112Error _initialize_devices();113void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);114115// Static callbacks.116static 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);117static 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);118// Debug marker extensions.119VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);120121protected:122Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;123124// Can be overridden by platform-specific drivers.125virtual const char *_get_platform_surface_extension() const { return nullptr; }126virtual bool _use_validation_layers() const;127virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);128129public:130virtual Error initialize() override;131virtual const Device &device_get(uint32_t p_device_index) const override;132virtual uint32_t device_get_count() const override;133virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;134virtual RenderingDeviceDriver *driver_create() override;135virtual void driver_free(RenderingDeviceDriver *p_driver) override;136virtual SurfaceID surface_create(const void *p_platform_data) override;137virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;138virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;139virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;140virtual uint32_t surface_get_width(SurfaceID p_surface) const override;141virtual uint32_t surface_get_height(SurfaceID p_surface) const override;142virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;143virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;144virtual void surface_destroy(SurfaceID p_surface) override;145virtual bool is_debug_utils_enabled() const override;146147// Vulkan-only methods.148struct Surface {149VkSurfaceKHR vk_surface = VK_NULL_HANDLE;150uint32_t width = 0;151uint32_t height = 0;152DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;153bool needs_resize = false;154};155156VkInstance instance_get() const;157VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;158uint32_t queue_family_get_count(uint32_t p_device_index) const;159VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;160bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;161const Functions &functions_get() const;162163static VkAllocationCallbacks *get_allocation_callbacks(VkObjectType p_type);164165#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)166enum VkTrackedObjectType {167VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR = VK_OBJECT_TYPE_COMMAND_POOL + 1,168VK_TRACKED_OBJECT_TYPE_SURFACE,169VK_TRACKED_OBJECT_TYPE_SWAPCHAIN,170VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT,171VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT,172VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE,173VK_TRACKED_OBJECT_TYPE_VMA,174VK_TRACKED_OBJECT_TYPE_COUNT175};176177enum VkTrackedSystemAllocationScope {178VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT = VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE + 1179};180#endif181182const char *get_tracked_object_name(uint32_t p_type_index) const override;183#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)184uint64_t get_tracked_object_type_count() const override;185#endif186187#if defined(VK_TRACK_DRIVER_MEMORY)188uint64_t get_driver_total_memory() const override;189uint64_t get_driver_allocation_count() const override;190uint64_t get_driver_memory_by_object_type(uint32_t p_type) const override;191uint64_t get_driver_allocs_by_object_type(uint32_t p_type) const override;192#endif193194#if defined(VK_TRACK_DEVICE_MEMORY)195uint64_t get_device_total_memory() const override;196uint64_t get_device_allocation_count() const override;197uint64_t get_device_memory_by_object_type(uint32_t p_type) const override;198uint64_t get_device_allocs_by_object_type(uint32_t p_type) const override;199static VKAPI_ATTR void VKAPI_CALL memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data);200#endif201202RenderingContextDriverVulkan();203virtual ~RenderingContextDriverVulkan() override;204};205206#endif // VULKAN_ENABLED207208209