jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 8 | #include "SkAutoMalloc.h" |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 9 | #include "vk/GrVkBackendContext.h" |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 10 | #include "vk/GrVkExtensions.h" |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 11 | #include "vk/GrVkInterface.h" |
| 12 | #include "vk/GrVkUtil.h" |
| 13 | |
| 14 | //////////////////////////////////////////////////////////////////////////////// |
| 15 | // Helper code to set up Vulkan context objects |
| 16 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 17 | #ifdef SK_ENABLE_VK_LAYERS |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 18 | const char* kDebugLayerNames[] = { |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 19 | // elements of VK_LAYER_LUNARG_standard_validation |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 20 | "VK_LAYER_GOOGLE_threading", |
| 21 | "VK_LAYER_LUNARG_parameter_validation", |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 22 | "VK_LAYER_LUNARG_object_tracker", |
| 23 | "VK_LAYER_LUNARG_image", |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 24 | "VK_LAYER_LUNARG_core_validation", |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 25 | "VK_LAYER_LUNARG_swapchain", |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 26 | "VK_LAYER_GOOGLE_unique_objects", |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 27 | // not included in standard_validation |
| 28 | //"VK_LAYER_LUNARG_api_dump", |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 29 | //"VK_LAYER_LUNARG_vktrace", |
| 30 | //"VK_LAYER_LUNARG_screenshot", |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 31 | }; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 34 | // the minimum version of Vulkan supported |
liyuqian | b138fdc | 2016-04-29 10:00:26 -0700 | [diff] [blame] | 35 | #ifdef SK_BUILD_FOR_ANDROID |
| 36 | const uint32_t kGrVkMinimumVersion = VK_MAKE_VERSION(1, 0, 3); |
| 37 | #else |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 38 | const uint32_t kGrVkMinimumVersion = VK_MAKE_VERSION(1, 0, 8); |
liyuqian | b138fdc | 2016-04-29 10:00:26 -0700 | [diff] [blame] | 39 | #endif |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 40 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 41 | #define ACQUIRE_VK_PROC(name, instance, device) \ |
| 42 | PFN_vk##name grVk##name = \ |
| 43 | reinterpret_cast<PFN_vk##name>(getProc("vk" #name, instance, device)); \ |
| 44 | if (grVk##name == nullptr) { \ |
| 45 | SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 46 | return nullptr; \ |
| 47 | } |
| 48 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 49 | // Create the base Vulkan objects needed by the GrVkGpu object |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 50 | const GrVkBackendContext* GrVkBackendContext::Create(uint32_t* presentQueueIndexPtr, |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 51 | CanPresentFn canPresent, |
| 52 | GrVkInterface::GetProc getProc) { |
Brian Salomon | cc90174 | 2017-04-25 13:28:45 -0400 | [diff] [blame] | 53 | if (!getProc) { |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 54 | #ifdef SK_LINK_WITH_VULKAN |
Brian Salomon | cc90174 | 2017-04-25 13:28:45 -0400 | [diff] [blame] | 55 | return GrVkBackendContext::Create(vkGetInstanceProcAddr, vkGetDeviceProcAddr, |
| 56 | presentQueueIndexPtr, canPresent); |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 57 | #else |
Brian Salomon | cc90174 | 2017-04-25 13:28:45 -0400 | [diff] [blame] | 58 | return nullptr; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 59 | #endif |
Brian Salomon | cc90174 | 2017-04-25 13:28:45 -0400 | [diff] [blame] | 60 | } |
| 61 | SkASSERT(getProc); |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 62 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 63 | VkPhysicalDevice physDev; |
| 64 | VkDevice device; |
| 65 | VkInstance inst; |
| 66 | VkResult err; |
| 67 | |
| 68 | const VkApplicationInfo app_info = { |
| 69 | VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
| 70 | nullptr, // pNext |
| 71 | "vktest", // pApplicationName |
| 72 | 0, // applicationVersion |
| 73 | "vktest", // pEngineName |
| 74 | 0, // engineVerison |
| 75 | kGrVkMinimumVersion, // apiVersion |
| 76 | }; |
| 77 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 78 | GrVkExtensions extensions(getProc); |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 79 | extensions.initInstance(kGrVkMinimumVersion); |
| 80 | |
| 81 | SkTArray<const char*> instanceLayerNames; |
| 82 | SkTArray<const char*> instanceExtensionNames; |
| 83 | uint32_t extensionFlags = 0; |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 84 | #ifdef SK_ENABLE_VK_LAYERS |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 85 | for (size_t i = 0; i < SK_ARRAY_COUNT(kDebugLayerNames); ++i) { |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 86 | if (extensions.hasInstanceLayer(kDebugLayerNames[i])) { |
| 87 | instanceLayerNames.push_back(kDebugLayerNames[i]); |
| 88 | } |
| 89 | } |
| 90 | if (extensions.hasInstanceExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) { |
| 91 | instanceExtensionNames.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 92 | extensionFlags |= kEXT_debug_report_GrVkExtensionFlag; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 93 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 94 | #endif |
| 95 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 96 | if (extensions.hasInstanceExtension(VK_KHR_SURFACE_EXTENSION_NAME)) { |
| 97 | instanceExtensionNames.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
| 98 | extensionFlags |= kKHR_surface_GrVkExtensionFlag; |
| 99 | } |
| 100 | if (extensions.hasInstanceExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
| 101 | instanceExtensionNames.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 102 | extensionFlags |= kKHR_swapchain_GrVkExtensionFlag; |
| 103 | } |
| 104 | #ifdef SK_BUILD_FOR_WIN |
| 105 | if (extensions.hasInstanceExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME)) { |
| 106 | instanceExtensionNames.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); |
| 107 | extensionFlags |= kKHR_win32_surface_GrVkExtensionFlag; |
| 108 | } |
djsollen | 7e73108 | 2016-06-09 13:07:13 -0700 | [diff] [blame] | 109 | #elif defined(SK_BUILD_FOR_ANDROID) |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 110 | if (extensions.hasInstanceExtension(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME)) { |
| 111 | instanceExtensionNames.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); |
| 112 | extensionFlags |= kKHR_android_surface_GrVkExtensionFlag; |
jvanverth | 1d15596 | 2016-05-23 13:13:36 -0700 | [diff] [blame] | 113 | } |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 114 | #elif defined(SK_BUILD_FOR_UNIX) && !defined(__Fuchsia__) |
jvanverth | 1d15596 | 2016-05-23 13:13:36 -0700 | [diff] [blame] | 115 | if (extensions.hasInstanceExtension(VK_KHR_XCB_SURFACE_EXTENSION_NAME)) { |
| 116 | instanceExtensionNames.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); |
| 117 | extensionFlags |= kKHR_xcb_surface_GrVkExtensionFlag; |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 118 | } |
| 119 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 120 | |
| 121 | const VkInstanceCreateInfo instance_create = { |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 122 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
| 123 | nullptr, // pNext |
| 124 | 0, // flags |
| 125 | &app_info, // pApplicationInfo |
| 126 | (uint32_t) instanceLayerNames.count(), // enabledLayerNameCount |
| 127 | instanceLayerNames.begin(), // ppEnabledLayerNames |
| 128 | (uint32_t) instanceExtensionNames.count(), // enabledExtensionNameCount |
| 129 | instanceExtensionNames.begin(), // ppEnabledExtensionNames |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 132 | ACQUIRE_VK_PROC(CreateInstance, VK_NULL_HANDLE, VK_NULL_HANDLE); |
| 133 | err = grVkCreateInstance(&instance_create, nullptr, &inst); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 134 | if (err < 0) { |
| 135 | SkDebugf("vkCreateInstance failed: %d\n", err); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 136 | return nullptr; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 139 | ACQUIRE_VK_PROC(DestroyInstance, inst, VK_NULL_HANDLE); |
| 140 | ACQUIRE_VK_PROC(EnumeratePhysicalDevices, inst, VK_NULL_HANDLE); |
| 141 | ACQUIRE_VK_PROC(GetPhysicalDeviceQueueFamilyProperties, inst, VK_NULL_HANDLE); |
| 142 | ACQUIRE_VK_PROC(GetPhysicalDeviceFeatures, inst, VK_NULL_HANDLE); |
| 143 | ACQUIRE_VK_PROC(CreateDevice, inst, VK_NULL_HANDLE); |
| 144 | ACQUIRE_VK_PROC(GetDeviceQueue, inst, VK_NULL_HANDLE); |
| 145 | ACQUIRE_VK_PROC(DeviceWaitIdle, inst, VK_NULL_HANDLE); |
| 146 | ACQUIRE_VK_PROC(DestroyDevice, inst, VK_NULL_HANDLE); |
| 147 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 148 | uint32_t gpuCount; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 149 | err = grVkEnumeratePhysicalDevices(inst, &gpuCount, nullptr); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 150 | if (err) { |
| 151 | SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 152 | grVkDestroyInstance(inst, nullptr); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 153 | return nullptr; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 154 | } |
| 155 | SkASSERT(gpuCount > 0); |
| 156 | // Just returning the first physical device instead of getting the whole array. |
| 157 | // TODO: find best match for our needs |
| 158 | gpuCount = 1; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 159 | err = grVkEnumeratePhysicalDevices(inst, &gpuCount, &physDev); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 160 | if (err) { |
| 161 | SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 162 | grVkDestroyInstance(inst, nullptr); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 163 | return nullptr; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // query to get the initial queue props size |
| 167 | uint32_t queueCount; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 168 | grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, nullptr); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 169 | SkASSERT(queueCount >= 1); |
| 170 | |
| 171 | SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties)); |
| 172 | // now get the actual queue props |
| 173 | VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get(); |
| 174 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 175 | grVkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueProps); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 176 | |
| 177 | // iterate to find the graphics queue |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 178 | uint32_t graphicsQueueIndex = queueCount; |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 179 | for (uint32_t i = 0; i < queueCount; i++) { |
| 180 | if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
| 181 | graphicsQueueIndex = i; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | SkASSERT(graphicsQueueIndex < queueCount); |
| 186 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 187 | // iterate to find the present queue, if needed |
| 188 | uint32_t presentQueueIndex = graphicsQueueIndex; |
| 189 | if (presentQueueIndexPtr && canPresent) { |
| 190 | for (uint32_t i = 0; i < queueCount; i++) { |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 191 | if (canPresent(inst, physDev, i)) { |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 192 | presentQueueIndex = i; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | SkASSERT(presentQueueIndex < queueCount); |
| 197 | *presentQueueIndexPtr = presentQueueIndex; |
| 198 | } |
| 199 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 200 | extensions.initDevice(kGrVkMinimumVersion, inst, physDev); |
| 201 | |
| 202 | SkTArray<const char*> deviceLayerNames; |
| 203 | SkTArray<const char*> deviceExtensionNames; |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 204 | #ifdef SK_ENABLE_VK_LAYERS |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 205 | for (size_t i = 0; i < SK_ARRAY_COUNT(kDebugLayerNames); ++i) { |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 206 | if (extensions.hasDeviceLayer(kDebugLayerNames[i])) { |
| 207 | deviceLayerNames.push_back(kDebugLayerNames[i]); |
| 208 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 209 | } |
| 210 | #endif |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 211 | if (extensions.hasDeviceExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
| 212 | deviceExtensionNames.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 213 | extensionFlags |= kKHR_swapchain_GrVkExtensionFlag; |
| 214 | } |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 215 | if (extensions.hasDeviceExtension("VK_NV_glsl_shader")) { |
| 216 | deviceExtensionNames.push_back("VK_NV_glsl_shader"); |
| 217 | extensionFlags |= kNV_glsl_shader_GrVkExtensionFlag; |
| 218 | } |
| 219 | |
| 220 | // query to get the physical device properties |
| 221 | VkPhysicalDeviceFeatures deviceFeatures; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 222 | grVkGetPhysicalDeviceFeatures(physDev, &deviceFeatures); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 223 | // this looks like it would slow things down, |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 224 | // and we can't depend on it on all platforms |
| 225 | deviceFeatures.robustBufferAccess = VK_FALSE; |
| 226 | |
| 227 | uint32_t featureFlags = 0; |
| 228 | if (deviceFeatures.geometryShader) { |
| 229 | featureFlags |= kGeometryShader_GrVkFeatureFlag; |
| 230 | } |
| 231 | if (deviceFeatures.dualSrcBlend) { |
| 232 | featureFlags |= kDualSrcBlend_GrVkFeatureFlag; |
| 233 | } |
| 234 | if (deviceFeatures.sampleRateShading) { |
| 235 | featureFlags |= kSampleRateShading_GrVkFeatureFlag; |
| 236 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 237 | |
| 238 | float queuePriorities[1] = { 0.0 }; |
| 239 | // Here we assume no need for swapchain queue |
| 240 | // If one is needed, the client will need its own setup code |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 241 | const VkDeviceQueueCreateInfo queueInfo[2] = { |
| 242 | { |
| 243 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 244 | nullptr, // pNext |
| 245 | 0, // VkDeviceQueueCreateFlags |
| 246 | graphicsQueueIndex, // queueFamilyIndex |
| 247 | 1, // queueCount |
| 248 | queuePriorities, // pQueuePriorities |
| 249 | }, |
| 250 | { |
| 251 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
| 252 | nullptr, // pNext |
| 253 | 0, // VkDeviceQueueCreateFlags |
| 254 | presentQueueIndex, // queueFamilyIndex |
| 255 | 1, // queueCount |
| 256 | queuePriorities, // pQueuePriorities |
| 257 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 258 | }; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 259 | uint32_t queueInfoCount = (presentQueueIndex != graphicsQueueIndex) ? 2 : 1; |
| 260 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 261 | const VkDeviceCreateInfo deviceInfo = { |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 262 | VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
| 263 | nullptr, // pNext |
| 264 | 0, // VkDeviceCreateFlags |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 265 | queueInfoCount, // queueCreateInfoCount |
| 266 | queueInfo, // pQueueCreateInfos |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 267 | (uint32_t) deviceLayerNames.count(), // layerCount |
| 268 | deviceLayerNames.begin(), // ppEnabledLayerNames |
| 269 | (uint32_t) deviceExtensionNames.count(), // extensionCount |
| 270 | deviceExtensionNames.begin(), // ppEnabledExtensionNames |
| 271 | &deviceFeatures // ppEnabledFeatures |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 274 | err = grVkCreateDevice(physDev, &deviceInfo, nullptr, &device); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 275 | if (err) { |
| 276 | SkDebugf("CreateDevice failed: %d\n", err); |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 277 | grVkDestroyInstance(inst, nullptr); |
| 278 | return nullptr; |
| 279 | } |
| 280 | |
| 281 | auto interface = |
| 282 | sk_make_sp<GrVkInterface>(getProc, inst, device, extensionFlags); |
| 283 | if (!interface->validate(extensionFlags)) { |
| 284 | SkDebugf("Vulkan interface validation failed\n"); |
| 285 | grVkDeviceWaitIdle(device); |
| 286 | grVkDestroyDevice(device, nullptr); |
| 287 | grVkDestroyInstance(inst, nullptr); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 288 | return nullptr; |
| 289 | } |
| 290 | |
| 291 | VkQueue queue; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 292 | grVkGetDeviceQueue(device, graphicsQueueIndex, 0, &queue); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 293 | |
| 294 | GrVkBackendContext* ctx = new GrVkBackendContext(); |
| 295 | ctx->fInstance = inst; |
| 296 | ctx->fPhysicalDevice = physDev; |
| 297 | ctx->fDevice = device; |
| 298 | ctx->fQueue = queue; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 299 | ctx->fGraphicsQueueIndex = graphicsQueueIndex; |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 300 | ctx->fMinAPIVersion = kGrVkMinimumVersion; |
| 301 | ctx->fExtensions = extensionFlags; |
| 302 | ctx->fFeatures = featureFlags; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 303 | ctx->fInterface.reset(interface.release()); |
Brian Salomon | 290c690 | 2017-04-25 15:03:41 -0400 | [diff] [blame^] | 304 | ctx->fOwnsInstanceAndDevice = true; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 305 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 306 | return ctx; |
| 307 | } |
| 308 | |
| 309 | GrVkBackendContext::~GrVkBackendContext() { |
Brian Salomon | 290c690 | 2017-04-25 15:03:41 -0400 | [diff] [blame^] | 310 | if (fInterface == nullptr || !fOwnsInstanceAndDevice) { |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | fInterface->fFunctions.fDeviceWaitIdle(fDevice); |
| 315 | fInterface->fFunctions.fDestroyDevice(fDevice, nullptr); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 316 | fDevice = VK_NULL_HANDLE; |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 317 | fInterface->fFunctions.fDestroyInstance(fInstance, nullptr); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 318 | fInstance = VK_NULL_HANDLE; |
| 319 | } |