1 #include "vulkancontext.h"
2 #include "vulkancontext_platform.h"
5 #define VK_USE_PLATFORM_XLIB_KHR
6 #include <vulkan/vulkan.h>
7 #include <msp/core/application.h>
8 #include <msp/debug/debugapi.h>
9 #include <msp/io/print.h>
10 #include "display_private.h"
11 #include "window_private.h"
18 string vulkan_error::get_error_message(unsigned code)
20 switch(static_cast<VkResult>(code))
22 case VK_SUCCESS: return "success";
23 case VK_NOT_READY: return "not ready";
24 case VK_TIMEOUT: return "timeout";
25 case VK_EVENT_SET: return "event set";
26 case VK_EVENT_RESET: return "event reset";
27 case VK_INCOMPLETE: return "incomplete";
28 case VK_ERROR_OUT_OF_HOST_MEMORY: return "out of host memory";
29 case VK_ERROR_OUT_OF_DEVICE_MEMORY: return "out of device memory";
30 case VK_ERROR_INITIALIZATION_FAILED: return "initialization failed";
31 case VK_ERROR_DEVICE_LOST: return "device lost";
32 case VK_ERROR_MEMORY_MAP_FAILED: return "memory map failed";
33 case VK_ERROR_LAYER_NOT_PRESENT: return "layer not present";
34 case VK_ERROR_EXTENSION_NOT_PRESENT: return "extension not present";
35 case VK_ERROR_FEATURE_NOT_PRESENT: return "feature not present";
36 case VK_ERROR_INCOMPATIBLE_DRIVER: return "incompatible driver";
37 case VK_ERROR_TOO_MANY_OBJECTS: return "too many objects";
38 case VK_ERROR_FORMAT_NOT_SUPPORTED: return "format not supported";
39 case VK_ERROR_FRAGMENTED_POOL: return "fragmented pool";
40 case VK_ERROR_UNKNOWN: return "unknown";
41 case VK_ERROR_OUT_OF_POOL_MEMORY: return "out of pool memory";
42 case VK_ERROR_INVALID_EXTERNAL_HANDLE: return "invalidernal handle";
43 case VK_ERROR_FRAGMENTATION: return "fragmentation";
44 case VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS: return "invalid opaque capture address";
45 case VK_ERROR_SURFACE_LOST_KHR: return "surface lost";
46 case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: return "native window in use";
47 case VK_SUBOPTIMAL_KHR: return "suboptimal";
48 case VK_ERROR_OUT_OF_DATE_KHR: return "out of date";
49 case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: return "incompatible display";
50 case VK_ERROR_VALIDATION_FAILED_EXT: return "validation failed";
51 case VK_ERROR_INVALID_SHADER_NV: return "invalid shader";
52 case VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT: return "invalid drm format modifier plane layout";
53 case VK_ERROR_NOT_PERMITTED_EXT: return "not permitted";
54 case VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT: return "full screen exclusive mode lost";
55 case VK_THREAD_IDLE_KHR: return "thread idle";
56 case VK_THREAD_DONE_KHR: return "thread done";
57 case VK_OPERATION_DEFERRED_KHR: return "operation deferred";
58 case VK_OPERATION_NOT_DEFERRED_KHR: return "operation not deferred";
59 case VK_PIPELINE_COMPILE_REQUIRED_EXT: return "pipeline compile required";
60 default: return format("VkResult(%d)", code);
65 void VulkanContext::platform_init(const VulkanOptions &opts)
68 VulkanFunctions &f = priv->functions;
72 f.vkCreateInstance = get_function<PFN_vkCreateInstance>("vkCreateInstance");
74 vector<const char *> layers;
75 if(opts.enable_validation)
76 layers.push_back("VK_LAYER_KHRONOS_validation");
78 vector<const char *> extensions;
79 extensions.push_back("VK_KHR_surface");
80 extensions.push_back("VK_KHR_xlib_surface");
81 extensions.push_back("VK_EXT_debug_utils");
82 if(opts.enable_debug_report)
83 extensions.push_back("VK_EXT_debug_report");
85 VkApplicationInfo app_info = { };
86 app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
87 app_info.pApplicationName = Application::get_name().c_str();
88 app_info.pEngineName = "MSP";
89 app_info.apiVersion = (1<<22)|(2<<12);
91 VkInstanceCreateInfo instance_create_info = { };
92 instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
93 instance_create_info.pApplicationInfo = &app_info;
94 instance_create_info.enabledLayerCount = layers.size();
95 instance_create_info.ppEnabledLayerNames = layers.data();
96 instance_create_info.enabledExtensionCount = extensions.size();
97 instance_create_info.ppEnabledExtensionNames = extensions.data();
99 VkResult result = f.vkCreateInstance(&instance_create_info, 0, &priv->instance);
100 if(result!=VK_SUCCESS)
101 throw vulkan_error(result, "vkCreateInstance");
103 f.vkDestroyInstance = get_function<PFN_vkDestroyInstance>("vkDestroyInstance");
104 f.vkEnumeratePhysicalDevices = get_function<PFN_vkEnumeratePhysicalDevices>("vkEnumeratePhysicalDevices");
105 f.vkGetPhysicalDeviceQueueFamilyProperties = get_function<PFN_vkGetPhysicalDeviceQueueFamilyProperties>("vkGetPhysicalDeviceQueueFamilyProperties");
106 f.vkGetPhysicalDeviceSurfaceSupport = get_function<PFN_vkGetPhysicalDeviceSurfaceSupportKHR>("vkGetPhysicalDeviceSurfaceSupportKHR");
107 f.vkCreateDevice = get_function<PFN_vkCreateDevice>("vkCreateDevice");
108 f.vkDestroyDevice = get_function<PFN_vkDestroyDevice>("vkDestroyDevice");
109 f.vkGetDeviceQueue = get_function<PFN_vkGetDeviceQueue>("vkGetDeviceQueue");
110 f.vkCreateXlibSurface = get_function<PFN_vkCreateXlibSurfaceKHR>("vkCreateXlibSurfaceKHR");
111 f.vkDestroySurface = get_function<PFN_vkDestroySurfaceKHR>("vkDestroySurfaceKHR");
112 f.vkCreateDebugReportCallback = get_function<PFN_vkCreateDebugReportCallbackEXT>("vkCreateDebugReportCallbackEXT");
113 f.vkDestroyDebugReportCallback = get_function<PFN_vkDestroyDebugReportCallbackEXT>("vkDestroyDebugReportCallbackEXT");
115 if(opts.enable_debug_report)
117 VkDebugReportCallbackCreateInfoEXT debug_report_create_info = { };
118 debug_report_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
119 debug_report_create_info.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT|VK_DEBUG_REPORT_ERROR_BIT_EXT|VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT|VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
120 debug_report_create_info.pfnCallback = &Private::debug_report_func;
121 debug_report_create_info.pUserData = this;
123 result = f.vkCreateDebugReportCallback(priv->instance, &debug_report_create_info, 0, &priv->debug_report_callback);
124 if(result!=VK_SUCCESS)
125 throw vulkan_error(result, "vkCreateDebugReportCallback");
128 VkXlibSurfaceCreateInfoKHR surface_create_info = { };
129 surface_create_info.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
130 surface_create_info.dpy = display.get_private().display;
131 surface_create_info.window = window.get_private().window;
133 result = f.vkCreateXlibSurface(priv->instance, &surface_create_info, 0, &priv->surface);
134 if(result!=VK_SUCCESS)
135 throw vulkan_error(result, "vkCreateXlibSurface");
137 unsigned n_phys_devices = 0;
138 result = f.vkEnumeratePhysicalDevices(priv->instance, &n_phys_devices, 0);
139 if(result!=VK_SUCCESS)
140 throw vulkan_error(result, "vkEnumeratePhysicalDevices");
141 else if(!n_phys_devices)
142 throw runtime_error("no physical device found");
143 vector<VkPhysicalDevice> phys_devices(n_phys_devices);
144 result = f.vkEnumeratePhysicalDevices(priv->instance, &n_phys_devices, &phys_devices[0]);
145 if(result!=VK_SUCCESS)
146 throw vulkan_error(result, "vkEnumeratePhysicalDevices");
148 unsigned gfx_queue_index = 0;
149 for(unsigned i=0; i<n_phys_devices; ++i)
151 VkPhysicalDevice phys_device = phys_devices[i];
153 unsigned n_queue_families = 0;
154 f.vkGetPhysicalDeviceQueueFamilyProperties(phys_device, &n_queue_families, 0);
155 vector<VkQueueFamilyProperties> queue_family_props(n_queue_families);
156 f.vkGetPhysicalDeviceQueueFamilyProperties(phys_device, &n_queue_families, queue_family_props.data());
158 for(; gfx_queue_index<n_queue_families; ++gfx_queue_index)
159 if((queue_family_props[gfx_queue_index].queueFlags&VK_QUEUE_GRAPHICS_BIT))
162 if(gfx_queue_index>=n_queue_families)
165 VkBool32 supported = VK_FALSE;
166 result = f.vkGetPhysicalDeviceSurfaceSupport(phys_device, gfx_queue_index, priv->surface, &supported);
167 if(result!=VK_SUCCESS)
172 priv->physical_device = phys_device;
177 if(!priv->physical_device)
178 throw runtime_error("no usable physical devices found");
180 priv->graphics_queue_family = gfx_queue_index;
182 float queue_priority = 1.0f;
183 VkDeviceQueueCreateInfo queue_create_info = { };
184 queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
185 queue_create_info.queueFamilyIndex = gfx_queue_index;
186 queue_create_info.queueCount = 1;
187 queue_create_info.pQueuePriorities = &queue_priority;
190 extensions.push_back("VK_KHR_swapchain");
192 VkPhysicalDeviceFeatures features = { };
193 features.geometryShader = (opts.enable_geometry_shader ? VK_TRUE : VK_FALSE);
194 features.tessellationShader = (opts.enable_tessellation_shader ? VK_TRUE : VK_FALSE);
196 VkDeviceCreateInfo device_create_info = { };
197 device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
198 device_create_info.queueCreateInfoCount = 1;
199 device_create_info.pQueueCreateInfos = &queue_create_info;
200 device_create_info.enabledExtensionCount = extensions.size();
201 device_create_info.ppEnabledExtensionNames = extensions.data();
202 device_create_info.pEnabledFeatures = &features;
204 result = f.vkCreateDevice(priv->physical_device, &device_create_info, 0, &priv->device);
205 if(result!=VK_SUCCESS)
206 throw vulkan_error(result, "vkCreateDevice");
208 f.vkGetDeviceQueue(priv->device, gfx_queue_index, 0, &priv->graphics_queue);
213 f.vkDestroyDevice(priv->device, 0);
215 f.vkDestroySurface(priv->instance, priv->surface, 0);
216 if(priv->debug_report_callback)
217 f.vkDestroyDebugReportCallback(priv->instance, priv->debug_report_callback, 0);
219 f.vkDestroyInstance(priv->instance, 0);
225 VulkanContext::~VulkanContext()
227 const VulkanFunctions &f = priv->functions;
228 f.vkDestroyDevice(priv->device, 0);
229 f.vkDestroySurface(priv->instance, priv->surface, 0);
230 if(priv->debug_report_callback)
231 f.vkDestroyDebugReportCallback(priv->instance, priv->debug_report_callback, 0);
232 f.vkDestroyInstance(priv->instance, 0);
236 void (*VulkanContext::_get_function(const string &name) const)()
238 return vkGetInstanceProcAddr(priv->instance, name.c_str());
242 VkBool32 VulkanContext::Private::debug_report_func(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT, uint64_t, size_t, int32_t, const char *, const char *message, void *)
244 IO::print(IO::cerr, "%s\n", message);
245 if((flags&VK_DEBUG_REPORT_ERROR_BIT_EXT) && Debug::check_debugger()==Debug::GDB)
246 Debug::debug_break();
250 } // namespace Graphics