Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/vulkan/rendering_context_driver_vulkan.h
9903 views
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
31
#pragma once
32
33
#ifdef VULKAN_ENABLED
34
35
#include "servers/rendering/rendering_context_driver.h"
36
37
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
38
#define VK_TRACK_DRIVER_MEMORY
39
#define VK_TRACK_DEVICE_MEMORY
40
#endif
41
42
#include "drivers/vulkan/godot_vulkan.h"
43
44
class RenderingContextDriverVulkan : public RenderingContextDriver {
45
public:
46
struct Functions {
47
// Physical device.
48
PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2 = nullptr;
49
PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;
50
51
// Device.
52
PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;
53
54
// Surfaces.
55
PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr;
56
PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr;
57
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
58
PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr;
59
60
// Debug utils.
61
PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT = nullptr;
62
PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT = nullptr;
63
PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT = nullptr;
64
PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT = nullptr;
65
PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT = nullptr;
66
67
bool debug_util_functions_available() const {
68
return CreateDebugUtilsMessengerEXT != nullptr &&
69
DestroyDebugUtilsMessengerEXT != nullptr &&
70
CmdBeginDebugUtilsLabelEXT != nullptr &&
71
CmdEndDebugUtilsLabelEXT != nullptr &&
72
SetDebugUtilsObjectNameEXT != nullptr;
73
}
74
75
// Debug report.
76
PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallbackEXT = nullptr;
77
PFN_vkDebugReportMessageEXT DebugReportMessageEXT = nullptr;
78
PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallbackEXT = nullptr;
79
80
// Debug marker extensions.
81
PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
82
PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
83
PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
84
PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
85
86
bool debug_report_functions_available() const {
87
return CreateDebugReportCallbackEXT != nullptr &&
88
DebugReportMessageEXT != nullptr &&
89
DestroyDebugReportCallbackEXT != nullptr;
90
}
91
};
92
93
private:
94
struct DeviceQueueFamilies {
95
TightLocalVector<VkQueueFamilyProperties> properties;
96
};
97
98
VkInstance instance = VK_NULL_HANDLE;
99
uint32_t instance_api_version = VK_API_VERSION_1_0;
100
HashMap<CharString, bool> requested_instance_extensions;
101
HashSet<CharString> enabled_instance_extension_names;
102
TightLocalVector<Device> driver_devices;
103
TightLocalVector<VkPhysicalDevice> physical_devices;
104
TightLocalVector<DeviceQueueFamilies> device_queue_families;
105
VkDebugUtilsMessengerEXT debug_messenger = VK_NULL_HANDLE;
106
VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
107
Functions functions;
108
109
Error _initialize_vulkan_version();
110
void _register_requested_instance_extension(const CharString &p_extension_name, bool p_required);
111
Error _initialize_instance_extensions();
112
Error _initialize_instance();
113
Error _initialize_devices();
114
void _check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device);
115
116
// Static callbacks.
117
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);
118
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);
119
// Debug marker extensions.
120
VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
121
122
protected:
123
Error _find_validation_layers(TightLocalVector<const char *> &r_layer_names) const;
124
125
// Can be overridden by platform-specific drivers.
126
virtual const char *_get_platform_surface_extension() const { return nullptr; }
127
virtual bool _use_validation_layers() const;
128
virtual Error _create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance);
129
130
public:
131
virtual Error initialize() override;
132
virtual const Device &device_get(uint32_t p_device_index) const override;
133
virtual uint32_t device_get_count() const override;
134
virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
135
virtual RenderingDeviceDriver *driver_create() override;
136
virtual void driver_free(RenderingDeviceDriver *p_driver) override;
137
virtual SurfaceID surface_create(const void *p_platform_data) override;
138
virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
139
virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
140
virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
141
virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
142
virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
143
virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
144
virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
145
virtual void surface_destroy(SurfaceID p_surface) override;
146
virtual bool is_debug_utils_enabled() const override;
147
148
// Vulkan-only methods.
149
struct Surface {
150
VkSurfaceKHR vk_surface = VK_NULL_HANDLE;
151
uint32_t width = 0;
152
uint32_t height = 0;
153
DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
154
bool needs_resize = false;
155
};
156
157
VkInstance instance_get() const;
158
VkPhysicalDevice physical_device_get(uint32_t p_device_index) const;
159
uint32_t queue_family_get_count(uint32_t p_device_index) const;
160
VkQueueFamilyProperties queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const;
161
bool queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const;
162
const Functions &functions_get() const;
163
164
static VkAllocationCallbacks *get_allocation_callbacks(VkObjectType p_type);
165
166
#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
167
enum VkTrackedObjectType {
168
VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR = VK_OBJECT_TYPE_COMMAND_POOL + 1,
169
VK_TRACKED_OBJECT_TYPE_SURFACE,
170
VK_TRACKED_OBJECT_TYPE_SWAPCHAIN,
171
VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT,
172
VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT,
173
VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE,
174
VK_TRACKED_OBJECT_TYPE_VMA,
175
VK_TRACKED_OBJECT_TYPE_COUNT
176
};
177
178
enum VkTrackedSystemAllocationScope {
179
VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT = VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE + 1
180
};
181
#endif
182
183
const char *get_tracked_object_name(uint32_t p_type_index) const override;
184
#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)
185
uint64_t get_tracked_object_type_count() const override;
186
#endif
187
188
#if defined(VK_TRACK_DRIVER_MEMORY)
189
uint64_t get_driver_total_memory() const override;
190
uint64_t get_driver_allocation_count() const override;
191
uint64_t get_driver_memory_by_object_type(uint32_t p_type) const override;
192
uint64_t get_driver_allocs_by_object_type(uint32_t p_type) const override;
193
#endif
194
195
#if defined(VK_TRACK_DEVICE_MEMORY)
196
uint64_t get_device_total_memory() const override;
197
uint64_t get_device_allocation_count() const override;
198
uint64_t get_device_memory_by_object_type(uint32_t p_type) const override;
199
uint64_t get_device_allocs_by_object_type(uint32_t p_type) const override;
200
static VKAPI_ATTR void VKAPI_CALL memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data);
201
#endif
202
203
RenderingContextDriverVulkan();
204
virtual ~RenderingContextDriverVulkan() override;
205
};
206
207
#endif // VULKAN_ENABLED
208
209