|
11 | 11 | #define GLFW_INCLUDE_VULKAN |
12 | 12 | #include <GLFW/glfw3.h> |
13 | 13 |
|
| 14 | +#ifdef USE_SHARED_VK_CONTEXT |
| 15 | + |
| 16 | +// An example of an external vkInstance/vkDevice provider. |
| 17 | +// It uses the first available device and queue |
| 18 | +class VkContext { |
| 19 | +public: |
| 20 | + VkContext() = default; |
| 21 | + ~VkContext() { |
| 22 | + device.reset(); |
| 23 | + instance.reset(); |
| 24 | + } |
| 25 | + |
| 26 | + static VkContext& shared() { |
| 27 | + static std::unique_ptr<VkContext> context; |
| 28 | + if (!context) { |
| 29 | + context = std::make_unique<VkContext>(); |
| 30 | + } |
| 31 | + |
| 32 | + return *context; |
| 33 | + } |
| 34 | + |
| 35 | + vk::Instance getInstance() { |
| 36 | + if (instance) { |
| 37 | + return instance.get(); |
| 38 | + } |
| 39 | + |
| 40 | + dispatcher.init(dynamicLoader); |
| 41 | + |
| 42 | +#ifdef __APPLE__ |
| 43 | + vk::InstanceCreateFlags instanceFlags = vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR; |
| 44 | +#else |
| 45 | + vk::InstanceCreateFlags instanceFlags = {}; |
| 46 | +#endif |
| 47 | + |
| 48 | + const std::vector<const char*> layers = {"VK_LAYER_KHRONOS_validation"}; |
| 49 | + std::vector<const char*> extensions = { |
| 50 | + VK_EXT_DEBUG_UTILS_EXTENSION_NAME, |
| 51 | +#ifdef __APPLE__ |
| 52 | + "VK_KHR_portability_enumeration", |
| 53 | +#endif |
| 54 | + }; |
| 55 | + |
| 56 | + uint32_t glfwExtensionCount = 0; |
| 57 | + const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); |
| 58 | + std::copy(glfwExtensions, glfwExtensions + glfwExtensionCount, std::back_inserter(extensions)); |
| 59 | + |
| 60 | + vk::ApplicationInfo appInfo("maplibre-native", 1, "maplibre-native", 1, VK_API_VERSION_1_0); |
| 61 | + const auto createInfo = vk::InstanceCreateInfo(instanceFlags) |
| 62 | + .setPApplicationInfo(&appInfo) |
| 63 | + .setPEnabledExtensionNames(extensions) |
| 64 | + .setPEnabledLayerNames(layers); |
| 65 | + |
| 66 | + instance = vk::createInstanceUnique(createInfo, nullptr, dispatcher); |
| 67 | + dispatcher.init(instance.get()); |
| 68 | + |
| 69 | + return instance.get(); |
| 70 | + } |
| 71 | + |
| 72 | + vk::PhysicalDevice getPhysicalDevice() { |
| 73 | + if (!physicalDevice) { |
| 74 | + physicalDevice = instance->enumeratePhysicalDevices(dispatcher)[0]; |
| 75 | + } |
| 76 | + |
| 77 | + return physicalDevice; |
| 78 | + } |
| 79 | + |
| 80 | + vk::Device getDevice() { |
| 81 | + if (device) { |
| 82 | + return device.get(); |
| 83 | + } |
| 84 | + |
| 85 | + const std::vector<const char*> layers = {"VK_LAYER_KHRONOS_validation"}; |
| 86 | + const std::vector<const char*> extensions = { |
| 87 | + VK_KHR_SWAPCHAIN_EXTENSION_NAME, |
| 88 | +#ifdef __APPLE__ |
| 89 | + "VK_KHR_portability_subset", |
| 90 | +#endif |
| 91 | + }; |
| 92 | + |
| 93 | + float queuePriority = 1.0f; |
| 94 | + vk::DeviceQueueCreateInfo queueCreateInfo(vk::DeviceQueueCreateFlags(), getQueueIndex(), 1, &queuePriority); |
| 95 | + |
| 96 | + auto createInfo = vk::DeviceCreateInfo() |
| 97 | + .setQueueCreateInfos(queueCreateInfo) |
| 98 | + .setPEnabledExtensionNames(extensions) |
| 99 | + .setPEnabledLayerNames(layers); |
| 100 | + |
| 101 | + device = physicalDevice.createDeviceUnique(createInfo, nullptr, dispatcher); |
| 102 | + dispatcher.init(device.get()); |
| 103 | + |
| 104 | + return device.get(); |
| 105 | + } |
| 106 | + |
| 107 | + uint32_t getQueueIndex() { return graphicsQueueIndex; } |
| 108 | + |
| 109 | +private: |
| 110 | + vk::DynamicLoader dynamicLoader; |
| 111 | + vk::DispatchLoaderDynamic dispatcher; |
| 112 | + |
| 113 | + vk::UniqueInstance instance; |
| 114 | + vk::PhysicalDevice physicalDevice; |
| 115 | + vk::UniqueDevice device; |
| 116 | + uint32_t graphicsQueueIndex = 0; |
| 117 | +}; |
| 118 | + |
| 119 | +#endif |
| 120 | + |
14 | 121 | class GLFWVulkanRenderableResource final : public mbgl::vulkan::SurfaceRenderableResource { |
15 | 122 | public: |
16 | 123 | explicit GLFWVulkanRenderableResource(GLFWVulkanBackend& backend_, bool capFrameRate) |
@@ -75,6 +182,39 @@ std::vector<const char*> GLFWVulkanBackend::getInstanceExtensions() { |
75 | 182 | return extensions; |
76 | 183 | } |
77 | 184 |
|
| 185 | +#ifdef USE_SHARED_VK_CONTEXT |
| 186 | + |
| 187 | +void GLFWVulkanBackend::initInstance() { |
| 188 | + // tell the backend to keep the objects alive |
| 189 | + usingSharedContext = true; |
| 190 | + |
| 191 | + // this method is required to set `instance` to a valid vkInstance |
| 192 | + instance = vk::UniqueInstance(VkContext::shared().getInstance(), |
| 193 | + vk::ObjectDestroy<vk::NoParent, vk::DispatchLoaderDynamic>(nullptr, dispatcher)); |
| 194 | + |
| 195 | + // debug builds also require: |
| 196 | + // - enabling either `VK_EXT_debug_utils` (and updating `debugUtilsEnabled`) or `VK_EXT_debug_report` extension |
| 197 | + // - or passing the values returned by `getDebugExtensions()` as enabled extensions during instance creation |
| 198 | + debugUtilsEnabled = true; |
| 199 | +} |
| 200 | + |
| 201 | +void GLFWVulkanBackend::initDevice() { |
| 202 | + // this method is required to populate: |
| 203 | + // `physicalDevice` |
| 204 | + // `device` |
| 205 | + // `graphicsQueueIndex`/`presentQueueIndex` |
| 206 | + // `physicalDeviceFeatures` - enabled features (optional) |
| 207 | + |
| 208 | + physicalDevice = VkContext::shared().getPhysicalDevice(); |
| 209 | + device = vk::UniqueDevice(VkContext::shared().getDevice(), |
| 210 | + vk::ObjectDestroy<vk::NoParent, vk::DispatchLoaderDynamic>(nullptr, dispatcher)); |
| 211 | + |
| 212 | + graphicsQueueIndex = presentQueueIndex = VkContext::shared().getQueueIndex(); |
| 213 | + physicalDeviceFeatures = vk::PhysicalDeviceFeatures(); |
| 214 | +} |
| 215 | + |
| 216 | +#endif |
| 217 | + |
78 | 218 | namespace mbgl { |
79 | 219 | namespace gfx { |
80 | 220 |
|
|
0 commit comments