Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan 3-D graphics library |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2014 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 25 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
| 26 | * Chia-I Wu <olv@lunarg.com> |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 27 | */ |
| 28 | |
Chia-I Wu | 023ef68 | 2014-09-15 11:06:50 +0800 | [diff] [blame] | 29 | #include "icd-enumerate-drm.h" |
Chia-I Wu | a6e3349 | 2014-08-05 13:35:08 +0800 | [diff] [blame] | 30 | #include "gpu.h" |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 31 | #include "instance.h" |
Courtney Goeltzenleuchter | e06e72d | 2014-08-01 12:44:23 -0600 | [diff] [blame] | 32 | |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 33 | static int intel_devid_override; |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 34 | int intel_debug = -1; |
| 35 | |
| 36 | static void intel_debug_init(void) |
| 37 | { |
| 38 | const char *env; |
| 39 | |
| 40 | if (intel_debug >= 0) |
| 41 | return; |
| 42 | |
| 43 | intel_debug = 0; |
| 44 | |
| 45 | /* parse comma-separated debug options */ |
| 46 | env = getenv("INTEL_DEBUG"); |
| 47 | while (env) { |
| 48 | const char *p = strchr(env, ','); |
| 49 | size_t len; |
| 50 | |
| 51 | if (p) |
| 52 | len = p - env; |
| 53 | else |
| 54 | len = strlen(env); |
| 55 | |
Courtney Goeltzenleuchter | d9fc984 | 2014-10-13 12:58:25 -0600 | [diff] [blame] | 56 | if (len > 0) { |
| 57 | if (strncmp(env, "batch", len) == 0) { |
| 58 | intel_debug |= INTEL_DEBUG_BATCH; |
| 59 | } else if (strncmp(env, "nohw", len) == 0) { |
| 60 | intel_debug |= INTEL_DEBUG_NOHW; |
Chia-I Wu | 3fb47ce | 2014-10-28 11:19:36 +0800 | [diff] [blame] | 61 | } else if (strncmp(env, "nocache", len) == 0) { |
| 62 | intel_debug |= INTEL_DEBUG_NOCACHE; |
Chia-I Wu | c45db53 | 2015-02-19 11:20:38 -0700 | [diff] [blame] | 63 | } else if (strncmp(env, "nohiz", len) == 0) { |
| 64 | intel_debug |= INTEL_DEBUG_NOHIZ; |
Chia-I Wu | 465fe21 | 2015-02-11 11:27:06 -0700 | [diff] [blame] | 65 | } else if (strncmp(env, "hang", len) == 0) { |
| 66 | intel_debug |= INTEL_DEBUG_HANG; |
Courtney Goeltzenleuchter | d9fc984 | 2014-10-13 12:58:25 -0600 | [diff] [blame] | 67 | } else if (strncmp(env, "0x", 2) == 0) { |
| 68 | intel_debug |= INTEL_DEBUG_NOHW; |
| 69 | intel_devid_override = strtol(env, NULL, 16); |
| 70 | } |
Chia-I Wu | 73019ad | 2014-08-29 12:01:13 +0800 | [diff] [blame] | 71 | } |
Chia-I Wu | 1c52701 | 2014-08-23 14:57:35 +0800 | [diff] [blame] | 72 | |
| 73 | if (!p) |
| 74 | break; |
| 75 | |
| 76 | env = p + 1; |
| 77 | } |
| 78 | } |
Jon Ashburn | 815bddd | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 79 | |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 80 | static void intel_instance_add_gpu(struct intel_instance *instance, |
| 81 | struct intel_gpu *gpu) |
| 82 | { |
| 83 | gpu->next = instance->gpus; |
| 84 | instance->gpus = gpu; |
| 85 | } |
| 86 | |
| 87 | static void intel_instance_remove_gpus(struct intel_instance *instance) |
| 88 | { |
| 89 | struct intel_gpu *gpu = instance->gpus; |
| 90 | |
| 91 | while (gpu) { |
| 92 | struct intel_gpu *next = gpu->next; |
| 93 | |
| 94 | intel_gpu_destroy(gpu); |
| 95 | gpu = next; |
| 96 | } |
| 97 | |
| 98 | instance->gpus = NULL; |
| 99 | } |
| 100 | |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 101 | static void intel_instance_destroy(struct intel_instance *instance) |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 102 | { |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 103 | struct icd_instance *icd = instance->icd; |
| 104 | |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 105 | intel_instance_remove_gpus(instance); |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 106 | icd_instance_free(icd, instance); |
| 107 | |
| 108 | icd_instance_destroy(icd); |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 111 | static struct intel_instance *intel_instance_create(const VkInstanceCreateInfo* info) |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 112 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 113 | struct intel_instance *instance; |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 114 | struct icd_instance *icd; |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 115 | uint32_t i; |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 116 | |
| 117 | intel_debug_init(); |
| 118 | |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 119 | icd = icd_instance_create(info->pAppInfo, info->pAllocCb); |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 120 | if (!icd) |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 121 | return NULL; |
| 122 | |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 123 | instance = icd_instance_alloc(icd, sizeof(*instance), 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 124 | VK_SYSTEM_ALLOC_API_OBJECT); |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 125 | if (!instance) { |
| 126 | icd_instance_destroy(icd); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 130 | memset(instance, 0, sizeof(*instance)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 131 | intel_handle_init(&instance->handle, VK_DBG_OBJECT_INSTANCE, icd); |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 132 | |
Chia-I Wu | 96a41bc | 2015-02-21 14:19:23 +0800 | [diff] [blame] | 133 | instance->icd = icd; |
| 134 | |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 135 | for (i = 0; i < info->extensionCount; i++) { |
| 136 | const enum intel_ext_type ext = intel_gpu_lookup_extension(NULL, |
| 137 | info->ppEnabledExtensionNames[i]); |
| 138 | |
| 139 | if (ext != INTEL_EXT_INVALID) |
| 140 | instance->exts[ext] = true; |
| 141 | } |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 142 | return instance; |
Jon Ashburn | fa836e0 | 2015-01-28 18:26:16 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 145 | ICD_EXPORT VkResult VKAPI vkCreateInstance( |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 146 | const VkInstanceCreateInfo* pCreateInfo, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 147 | VkInstance* pInstance) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 148 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 149 | struct intel_instance *instance; |
| 150 | |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 151 | instance = intel_instance_create(pCreateInfo); |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 152 | if (!instance) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 153 | return VK_ERROR_OUT_OF_MEMORY; |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 154 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 155 | *pInstance = (VkInstance) instance; |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 156 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 157 | return VK_SUCCESS; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 160 | ICD_EXPORT VkResult VKAPI vkDestroyInstance( |
| 161 | VkInstance pInstance) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 162 | { |
Chia-I Wu | 94ae71a | 2015-02-20 12:26:08 -0700 | [diff] [blame] | 163 | struct intel_instance *instance = intel_instance(pInstance); |
| 164 | |
| 165 | intel_instance_destroy(instance); |
| 166 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 167 | return VK_SUCCESS; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 170 | ICD_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices( |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 171 | VkInstance instance_, |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 172 | uint32_t* pPhysicalDeviceCount, |
| 173 | VkPhysicalGpu* pPhysicalDevices) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 174 | { |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 175 | struct intel_instance *instance = intel_instance(instance_); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 176 | struct icd_drm_device *devices, *dev; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 177 | VkResult ret; |
Jon Ashburn | 92e8013 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 178 | uint32_t count; |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 179 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 180 | if (pPhysicalDevices == NULL) { |
| 181 | *pPhysicalDeviceCount = 1; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 182 | return VK_SUCCESS; |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 185 | intel_instance_remove_gpus(instance); |
| 186 | |
Chia-I Wu | 15517d8 | 2015-02-20 13:41:57 -0700 | [diff] [blame] | 187 | devices = icd_drm_enumerate(instance->icd, 0x8086); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 188 | |
| 189 | count = 0; |
| 190 | dev = devices; |
| 191 | while (dev) { |
| 192 | const char *primary_node, *render_node; |
| 193 | int devid; |
| 194 | struct intel_gpu *gpu; |
| 195 | |
| 196 | primary_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_LEGACY); |
| 197 | if (!primary_node) |
| 198 | continue; |
| 199 | |
| 200 | render_node = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER); |
| 201 | |
| 202 | devid = (intel_devid_override) ? intel_devid_override : dev->devid; |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 203 | ret = intel_gpu_create(instance, devid, |
| 204 | primary_node, render_node, &gpu); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 205 | if (ret == VK_SUCCESS) { |
Chia-I Wu | d71ff55 | 2015-02-20 12:50:12 -0700 | [diff] [blame] | 206 | intel_instance_add_gpu(instance, gpu); |
| 207 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 208 | pPhysicalDevices[count++] = (VkPhysicalGpu) gpu; |
| 209 | if (count >= *pPhysicalDeviceCount) |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | |
| 213 | dev = dev->next; |
| 214 | } |
| 215 | |
Chia-I Wu | 15517d8 | 2015-02-20 13:41:57 -0700 | [diff] [blame] | 216 | icd_drm_release(instance->icd, devices); |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 217 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame^] | 218 | *pPhysicalDeviceCount = count; |
Jon Ashburn | 5f078e9 | 2015-01-28 19:15:45 -0700 | [diff] [blame] | 219 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 220 | return (count > 0) ? VK_SUCCESS : VK_ERROR_UNAVAILABLE; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 223 | ICD_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback( |
| 224 | VkInstance instance_, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 225 | VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 226 | void* pUserData) |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 227 | { |
Courtney Goeltzenleuchter | 0629efa | 2015-04-13 14:59:19 -0600 | [diff] [blame] | 228 | struct intel_instance *instance = intel_instance(instance_); |
| 229 | |
| 230 | return icd_instance_add_logger(instance->icd, pfnMsgCallback, pUserData); |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 233 | ICD_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback( |
| 234 | VkInstance instance_, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 235 | VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 236 | { |
Courtney Goeltzenleuchter | 0629efa | 2015-04-13 14:59:19 -0600 | [diff] [blame] | 237 | struct intel_instance *instance = intel_instance(instance_); |
| 238 | |
| 239 | return icd_instance_remove_logger(instance->icd, pfnMsgCallback); |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 242 | ICD_EXPORT VkResult VKAPI vkDbgSetGlobalOption( |
| 243 | VkInstance instance_, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 244 | VK_DBG_GLOBAL_OPTION dbgOption, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 245 | size_t dataSize, |
| 246 | const void* pData) |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 247 | { |
Courtney Goeltzenleuchter | 0629efa | 2015-04-13 14:59:19 -0600 | [diff] [blame] | 248 | struct intel_instance *instance = intel_instance(instance_); |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 249 | VkResult res = VK_SUCCESS; |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 250 | |
| 251 | if (dataSize == 0) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 252 | return VK_ERROR_INVALID_VALUE; |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 253 | |
| 254 | switch (dbgOption) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 255 | case VK_DBG_OPTION_DEBUG_ECHO_ENABLE: |
| 256 | case VK_DBG_OPTION_BREAK_ON_ERROR: |
| 257 | case VK_DBG_OPTION_BREAK_ON_WARNING: |
Courtney Goeltzenleuchter | 0629efa | 2015-04-13 14:59:19 -0600 | [diff] [blame] | 258 | res = icd_instance_set_bool(instance->icd, dbgOption, |
| 259 | *((const bool *) pData)); |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 260 | break; |
| 261 | default: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 262 | res = VK_ERROR_INVALID_VALUE; |
Chia-I Wu | b02558c | 2015-01-03 15:21:51 +0800 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
| 266 | return res; |
| 267 | } |