blob: 3325a109a974a9a8c2212ac3306254ace36ed864 [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003 *
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.
Chia-I Wu701f3f62014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn01e2d662014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu701f3f62014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Ian Elliott5aa4ea22015-03-31 15:32:41 -060028 * Ian Elliott <ian@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080029 */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060030#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080031#include <stdio.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <string.h>
36
Chia-I Wu13a61a52014-08-04 11:18:20 +080037#include <sys/types.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070038#if defined(WIN32)
39#include "dirent_on_windows.h"
40#else // WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080041#include <dirent.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070042#endif // WIN32
43#include "loader_platform.h"
Chia-I Wuf46b81a2015-01-04 11:12:47 +080044#include "table_ops.h"
Jon Ashburn3d526cb2015-04-13 18:10:06 -060045#include "gpa_helper.h"
Chia-I Wu19300602014-08-04 08:03:57 +080046#include "loader.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060047#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070048// The following is #included again to catch certain OS-specific functions
49// being used:
50#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080051
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060052struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070053 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070054 char name[256];
55};
56
57struct layer_name_pair {
58 char *layer_name;
59 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060060};
61
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060062struct extension_property {
63 char extName[VK_MAX_EXTENSION_NAME];
64 uint32_t version;
65 bool hosted; // does the extension reside in one driver/layer
66};
67
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070069 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080070
Jon Ashburnbacb0f52015-04-06 10:58:22 -060071 VkLayerDispatchTable *loader_dispatch;
Jon Ashburn83a64252015-04-15 11:31:12 -060072 uint32_t layer_count[MAX_GPUS_FOR_LAYER];
73 struct loader_layers layer_libs[MAX_GPUS_FOR_LAYER][MAX_LAYER_LIBRARIES];
74 VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060075 uint32_t gpu_count;
Jon Ashburnbacb0f52015-04-06 10:58:22 -060076 VkBaseLayerObject *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060077
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080078 struct loader_icd *next;
79};
80
Jon Ashburnd38bfb12014-10-14 19:15:22 -060081
Jon Ashburn46d1f582015-01-28 11:01:35 -070082struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070083 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060084
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060085 PFN_vkGetProcAddr GetProcAddr;
86 PFN_vkCreateInstance CreateInstance;
87 PFN_vkDestroyInstance DestroyInstance;
Jon Ashburn83a64252015-04-15 11:31:12 -060088 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060089 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060090 VkInstance instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070091 struct loader_scanned_icds *next;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060092 uint32_t extension_count;
93 struct extension_property *extensions;
Jon Ashburn46d1f582015-01-28 11:01:35 -070094};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060095
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060096struct loader_scanned_layers {
97 char *name;
98 uint32_t extension_count;
99 struct extension_property *extensions;
100};
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700101// Note: Since the following is a static structure, all members are initialized
102// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800103static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700104 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700105 bool icds_scanned;
106 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600107 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700108 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600109 unsigned int scanned_layer_count;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600110 struct loader_scanned_layers scanned_layers[MAX_LAYER_LIBRARIES];
111 size_t scanned_ext_list_capacity;
112 uint32_t scanned_ext_list_count; // coalesced from all layers/drivers
113 struct extension_property **scanned_ext_list;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800114} loader;
115
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600116static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
117static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
118static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700119
Ian Elliott4470a302015-02-17 10:33:47 -0700120#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600121char *loader_get_registry_string(const HKEY hive,
122 const LPCTSTR sub_key,
123 const char *value)
124{
125 DWORD access_flags = KEY_QUERY_VALUE;
126 DWORD value_type;
127 HKEY key;
128 LONG rtn_value;
129 char *rtn_str = NULL;
130 size_t rtn_len = 0;
131 size_t allocated_len = 0;
132
133 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
134 if (rtn_value != ERROR_SUCCESS) {
135 // We didn't find the key. Try the 32-bit hive (where we've seen the
136 // key end up on some people's systems):
137 access_flags |= KEY_WOW64_32KEY;
138 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
139 if (rtn_value != ERROR_SUCCESS) {
140 // We still couldn't find the key, so give up:
141 return NULL;
142 }
143 }
144
145 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
146 (PVOID) rtn_str, &rtn_len);
147 if (rtn_value == ERROR_SUCCESS) {
148 // If we get to here, we found the key, and need to allocate memory
149 // large enough for rtn_str, and query again:
150 allocated_len = rtn_len + 4;
151 rtn_str = malloc(allocated_len);
152 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
153 (PVOID) rtn_str, &rtn_len);
154 if (rtn_value == ERROR_SUCCESS) {
155 // We added 4 extra bytes to rtn_str, so that we can ensure that
156 // the string is NULL-terminated (albeit, in a brute-force manner):
157 rtn_str[allocated_len-1] = '\0';
158 } else {
159 // This should never occur, but in case it does, clean up:
160 free(rtn_str);
161 rtn_str = NULL;
162 }
163 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
164
165 // Close the registry key that was opened:
166 RegCloseKey(key);
167
168 return rtn_str;
169}
170
171
Ian Elliott4470a302015-02-17 10:33:47 -0700172// For ICD developers, look in the registry, and look for an environment
173// variable for a path(s) where to find the ICD(s):
174static char *loader_get_registry_and_env(const char *env_var,
175 const char *registry_value)
176{
177 char *env_str = getenv(env_var);
178 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600179 char *registry_str = NULL;
180 DWORD registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700181 char *rtn_str = NULL;
182 size_t rtn_len;
183
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600184 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600185 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600186 registry_value);
Ian Elliott2de26bc2015-04-03 13:13:01 -0600187 registry_len = (registry_str) ? strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700188
189 rtn_len = env_len + registry_len + 1;
190 if (rtn_len <= 2) {
191 // We found neither the desired registry value, nor the environment
192 // variable; return NULL:
193 return NULL;
194 } else {
195 // We found something, and so we need to allocate memory for the string
196 // to return:
197 rtn_str = malloc(rtn_len);
198 }
199
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600200 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700201 // We didn't find the desired registry value, and so we must have found
202 // only the environment variable:
203 _snprintf(rtn_str, rtn_len, "%s", env_str);
204 } else if (env_str != NULL) {
205 // We found both the desired registry value and the environment
206 // variable, so concatenate them both:
207 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
208 } else {
209 // We must have only found the desired registry value:
210 _snprintf(rtn_str, rtn_len, "%s", registry_str);
211 }
212
Ian Elliott2de26bc2015-04-03 13:13:01 -0600213 if (registry_str) {
214 free(registry_str);
215 }
Ian Elliott4470a302015-02-17 10:33:47 -0700216
217 return(rtn_str);
218}
219#endif // WIN32
220
221
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600222static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800223 const char *format, ...)
224{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800225 char msg[256];
226 va_list ap;
227 int ret;
228
229 va_start(ap, format);
230 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700231 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800232 msg[sizeof(msg) - 1] = '\0';
233 }
234 va_end(ap);
235
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600236 fputs(msg, stderr);
237 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800238}
239
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600240static bool has_extension(struct extension_property *exts, uint32_t count,
241 const char *name, bool must_be_hosted)
242{
243 uint32_t i;
244 for (i = 0; i < count; i++) {
245 if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
246 return true;
247 }
248 return false;
249}
250
251static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
252 uint32_t *count_out,
253 struct extension_property **props_out)
254{
255 uint32_t i, count, cur;
256 size_t siz = sizeof(count);
257 struct extension_property *ext_props;
258 VkExtensionProperties vk_prop;
259 VkResult res;
260
261 *count_out = 0;
262 *props_out = NULL;
263 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
264 if (res != VK_SUCCESS) {
265 loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
266 return;
267 }
268 ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
269 if (ext_props == NULL) {
270 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
271 return;
272 }
273 siz = sizeof(VkExtensionProperties);
274 cur = 0;
275 for (i = 0; i < count; i++) {
276 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
277 if (res == VK_SUCCESS) {
278 (ext_props + cur)->hosted = false;
279 (ext_props + cur)->version = vk_prop.version;
280 strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
281 (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
282 cur++;
283 }
284 *count_out = cur;
285 *props_out = ext_props;
286 }
287 return;
288}
289
290static void loader_init_ext_list()
291{
292 loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
293 loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
294 memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
295 loader.scanned_ext_list_count = 0;
296}
297
298#if 0 // currently no place to call this
299static void loader_destroy_ext_list()
300{
301 free(loader.scanned_ext_list);
302 loader.scanned_ext_list_capacity = 0;
303 loader.scanned_ext_list_count = 0;
304}
305#endif
306
307static void loader_add_to_ext_list(uint32_t count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600308 struct extension_property *prop_list,
309 bool is_layer_ext)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600310{
311 uint32_t i, j;
312 bool duplicate;
313 struct extension_property *cur_ext;
314
315 if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
316 loader_init_ext_list();
317
318 if (loader.scanned_ext_list == NULL)
319 return;
320
321 struct extension_property *ext_list, **ext_list_addr;
322
323 for (i = 0; i < count; i++) {
324 cur_ext = prop_list + i;
325
326 // look for duplicates or not
327 duplicate = false;
328 for (j = 0; j < loader.scanned_ext_list_count; j++) {
329 ext_list = loader.scanned_ext_list[j];
330 if (!strcmp(cur_ext->extName, ext_list->extName)) {
331 duplicate = true;
332 ext_list->hosted = false;
333 break;
334 }
335 }
336
337 // add to list at end
338 if (!duplicate) {
339 // check for enough capacity
340 if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
341 >= loader.scanned_ext_list_capacity) {
342 // double capacity
343 loader.scanned_ext_list_capacity *= 2;
344 loader.scanned_ext_list = realloc(loader.scanned_ext_list,
345 loader.scanned_ext_list_capacity);
346 }
347 ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
348 *ext_list_addr = cur_ext;
349 cur_ext->hosted = true;
350 }
351
352 }
353}
354
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600355static bool loader_is_extension_scanned(const char *name)
356{
357 uint32_t i;
358
359 for (i = 0; i < loader.scanned_ext_list_count; i++) {
360 if (!strcmp(name, loader.scanned_ext_list[i]->extName))
361 return true;
362 }
363 return false;
364}
365
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600366static void loader_coalesce_extensions()
367{
368 uint32_t i;
369 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
370
371 // traverse scanned icd list adding non-duplicate extensions to the list
372 while (icd_list != NULL) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600373 loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600374 icd_list = icd_list->next;
375 };
376
377 //Traverse layers list adding non-duplicate extensions to the list
378 for (i = 0; i < loader.scanned_layer_count; i++) {
379 loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600380 loader.scanned_layers[i].extensions, true);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600381 }
382}
383
384static void loader_icd_destroy(struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800385{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700386 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800387 free(icd);
388}
389
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600390static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800391{
392 struct loader_icd *icd;
393
394 icd = malloc(sizeof(*icd));
395 if (!icd)
396 return NULL;
397
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600398 memset(icd, 0, sizeof(*icd));
399
Jon Ashburn46d1f582015-01-28 11:01:35 -0700400 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800401
402 return icd;
403}
404
Jon Ashburn46888392015-01-29 15:45:51 -0700405static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
406 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800407{
408 struct loader_icd *icd;
409
Jon Ashburn46d1f582015-01-28 11:01:35 -0700410 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800411 if (!icd)
412 return NULL;
413
Chia-I Wu13a61a52014-08-04 11:18:20 +0800414 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700415 icd->next = ptr_inst->icds;
416 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800417
418 return icd;
419}
420
Jon Ashburn46d1f582015-01-28 11:01:35 -0700421static void loader_scanned_icd_add(const char *filename)
422{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700423 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600424 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
425 void *fp_get_global_ext_info;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700426 struct loader_scanned_icds *new_node;
427
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700428 // Used to call: dlopen(filename, RTLD_LAZY);
429 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700430 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600431 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700432 return;
433 }
434
435#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600436 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700437 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600438 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700439 return; \
440 } \
441} while (0)
442
443 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700444 LOOKUP(fp_create_inst, CreateInstance);
445 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn83a64252015-04-15 11:31:12 -0600446 LOOKUP(fp_enumerate, EnumeratePhysicalDevices);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600447 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700448#undef LOOKUP
449
450 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
451 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600452 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700453 return;
454 }
455
456 new_node->handle = handle;
457 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700458 new_node->CreateInstance = fp_create_inst;
459 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn83a64252015-04-15 11:31:12 -0600460 new_node->EnumeratePhysicalDevices = fp_enumerate;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600461 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
462 new_node->extension_count = 0;
463 new_node->extensions = NULL;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700464 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700465
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600466 loader.scanned_icd_list = new_node;
467
468 if (fp_get_global_ext_info) {
469 get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
470 &new_node->extension_count,
471 &new_node->extensions);
472 } else {
473 loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
474 }
475}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700476
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600477/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600478 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600479 *
480 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600481 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
482 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600483 *
484 * \returns
485 * void; but side effect is to set loader_icd_scanned to true
486 */
487static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800488{
Ian Elliott4470a302015-02-17 10:33:47 -0700489 const char *p, *next;
490 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600491 DIR *sysdir;
492 struct dirent *dent;
493 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600494 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700495 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700496#if defined(WIN32)
497 bool must_free_libPaths;
498 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
499 DRIVER_PATH_REGISTRY_VALUE);
500 if (libPaths != NULL) {
501 must_free_libPaths = true;
502 } else {
503 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600504 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700505 }
506#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600507 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700508 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
509 libPaths = getenv(DRIVER_PATH_ENV);
510 }
511 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600512 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600513 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700514#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800515
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600516 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700517 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600518 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700519 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600520 next = p + len;
521 }
522 else {
Ian Elliott19628802015-02-04 12:06:46 -0700523 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600524 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
525 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600526 next++;
527 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800528
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700529 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
530 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600531 sysdir = opendir(p);
532 if (sysdir) {
533 dent = readdir(sysdir);
534 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600535 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
536 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700537 */
538 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600539 VK_DRIVER_LIBRARY_PREFIX,
540 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700541 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600542 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
543 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700544 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600545 VK_LIBRARY_SUFFIX,
546 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700547 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
548 loader_scanned_icd_add(icd_library);
549 }
550 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600551
552 dent = readdir(sysdir);
553 }
554 closedir(sysdir);
555 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800556 }
557
Ian Elliott4470a302015-02-17 10:33:47 -0700558#if defined(WIN32)
559 // Free any allocated memory:
560 if (must_free_libPaths) {
561 free(libPaths);
562 }
563#endif // WIN32
564
565 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700566 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800567}
568
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600569
Ian Elliott4470a302015-02-17 10:33:47 -0700570static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600571{
572 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700573 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600574 DIR *curdir;
575 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700576 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600577 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600578 uint32_t count;
579 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600580
Ian Elliott4470a302015-02-17 10:33:47 -0700581#if defined(WIN32)
582 bool must_free_libPaths;
583 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
584 LAYERS_PATH_REGISTRY_VALUE);
585 if (libPaths != NULL) {
586 must_free_libPaths = true;
587 } else {
588 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600589 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600590 }
Ian Elliott4470a302015-02-17 10:33:47 -0700591#else // WIN32
592 if (geteuid() == getuid()) {
593 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700594 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700595 }
596 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600597 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700598 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700599#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600600
Ian Elliott4470a302015-02-17 10:33:47 -0700601 if (libPaths == NULL) {
602 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700603 return;
604 }
Ian Elliott4470a302015-02-17 10:33:47 -0700605 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700606 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700607 if (loader.layer_dirs == NULL) {
608 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700609 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700610 }
611 // Alloc passed, so we know there is enough space to hold the string, don't
612 // need strncpy
613 strcpy(loader.layer_dirs, libPaths);
614#if defined(WIN32)
615 // Free any allocated memory:
616 if (must_free_libPaths) {
617 free(libPaths);
618 must_free_libPaths = false;
619 }
620#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700621 libPaths = loader.layer_dirs;
622
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600623 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600624 for (i = 0; i < loader.scanned_layer_count; i++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600625 if (loader.scanned_layers[i].name != NULL)
626 free(loader.scanned_layers[i].name);
627 if (loader.scanned_layers[i].extensions != NULL)
628 free(loader.scanned_layers[i].extensions);
629 loader.scanned_layers[i].name = NULL;
630 loader.scanned_layers[i].extensions = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600631 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600632 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600633 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600634
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600635 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600636 next = strchr(p, PATH_SEPERATOR);
637 if (next == NULL) {
638 len = (uint32_t) strlen(p);
639 next = p + len;
640 }
641 else {
642 len = (uint32_t) (next - p);
643 *(char *) next = '\0';
644 next++;
645 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600646
647 curdir = opendir(p);
648 if (curdir) {
649 dent = readdir(curdir);
650 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600651 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
652 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700653 */
654 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600655 VK_LAYER_LIBRARY_PREFIX,
656 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700657 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600658 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
659 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700660 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600661 VK_LIBRARY_SUFFIX,
662 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700663 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600664 snprintf(temp_str, sizeof(temp_str),
665 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700666 // Used to call: dlopen(temp_str, RTLD_LAZY)
667 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
668 dent = readdir(curdir);
669 continue;
670 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600671 if (count == MAX_LAYER_LIBRARIES) {
672 loader_log(VK_DBG_MSG_ERROR, 0,
673 "%s ignored: max layer libraries exceed",
674 temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700675 break;
676 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600677 fp_get_ext = loader_platform_get_proc_address(handle,
678 "vkGetGlobalExtensionInfo");
679
680 if (!fp_get_ext) {
681 loader_log(VK_DBG_MSG_WARNING, 0,
682 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
683 temp_str);
684 dent = readdir(curdir);
685 loader_platform_close_library(handle);
686 continue;
687 }
688
689 loader.scanned_layers[count].name =
690 malloc(strlen(temp_str) + 1);
691 if (loader.scanned_layers[count].name == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600692 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700693 break;
694 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600695
696 get_global_extensions(fp_get_ext,
697 &loader.scanned_layers[count].extension_count,
698 &loader.scanned_layers[count].extensions);
699
700
701 strcpy(loader.scanned_layers[count].name, temp_str);
702 count++;
703 loader_platform_close_library(handle);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700704 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600705 }
706
707 dent = readdir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600708 } // while (dir_entry)
709 if (count == MAX_LAYER_LIBRARIES)
710 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600711 closedir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600712 } // if (curdir))
713 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600714
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600715 loader.scanned_layer_count = count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600716 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600717}
718
Tony Barbourd1c35722015-04-16 15:59:00 -0600719static void loader_init_dispatch_table(VkLayerDispatchTable *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalDevice gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600720{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800721 loader_initialize_dispatch_table(tab, fpGPA, gpu);
722
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600723 if (tab->EnumerateLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600724 tab->EnumerateLayers = vkEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600725}
726
Chia-I Wu04ac7ff2015-04-17 10:49:15 +0800727static void * VKAPI loader_gpa_internal(VkPhysicalDevice gpu, const char * pName)
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600728{
Mike Stroyanb050c682015-04-17 12:36:38 -0600729 if (gpu == VK_NULL_HANDLE) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600730 return NULL;;
731 }
732 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
733 VkLayerDispatchTable * disp_table = * (VkLayerDispatchTable **) gpuw->baseObject;
734 void *addr;
735
736 if (disp_table == NULL)
737 return NULL;
738
739 addr = loader_lookup_dispatch_table(disp_table, pName);
740 if (addr)
741 return addr;
742 else {
743 if (disp_table->GetProcAddr == NULL)
744 return NULL;
745 return disp_table->GetProcAddr(gpuw->nextObject, pName);
746 }
747}
748
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600749extern struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600750{
Jon Ashburn4d9f4652015-04-08 21:33:34 -0600751 /*
752 * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
753 * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
754 * need to test it against the wrapped GPU's base object.
755 */
Jon Ashburn98bd4542015-01-29 16:44:24 -0700756 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
757 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
758 for (uint32_t i = 0; i < icd->gpu_count; i++)
Mike Stroyanb050c682015-04-17 12:36:38 -0600759 if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700760 *gpu_index = i;
761 return icd;
762 }
763 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600764 }
765 return NULL;
766}
767
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600768static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600769{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600770 if (icd->layer_count[gpu_index])
771 return true;
772 else
773 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600774}
775
Jon Ashburn19c25022015-04-14 14:14:48 -0600776static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
777 struct layer_name_pair * pLayerNames,
778 uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600779{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600780 if (!icd)
781 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600782
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600783 struct loader_layers *obj;
784 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600785 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600786 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600787 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600788 if (icd->layer_libs[gpu_index][j].lib_handle &&
789 !strcmp(icd->layer_libs[gpu_index][j].name,
790 (char *) pLayerNames[i].layer_name) &&
791 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600792 foundLib = true;
793 break;
794 }
795 }
796 if (!foundLib) {
797 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700798 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
799 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700800 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
801 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600802 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600803 continue;
804 } else {
Jon Ashburn19c25022015-04-14 14:14:48 -0600805 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s",
806 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600807 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700808 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600809 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600810 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600811 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600812}
813
Jon Ashburn19c25022015-04-14 14:14:48 -0600814static bool find_layer_extension(struct loader_icd *icd, uint32_t gpu_index,
815 const char *pExtName, uint32_t *out_count,
816 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnb8358052014-11-18 09:06:04 -0700817{
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700818 char *search_name;
Jon Ashburn19c25022015-04-14 14:14:48 -0600819 uint32_t j, found_count = 0;
820 bool must_be_hosted;
821 bool found = false;
Jon Ashburnb8358052014-11-18 09:06:04 -0700822
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700823 /*
824 * The loader provides the abstraction that make layers and extensions work via
825 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600826 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700827 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600828 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700829 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700830
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600831 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700832
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600833 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600834
835 if (!strcmp("Validation", pExtName))
836 must_be_hosted = false;
837 else
838 must_be_hosted = true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600839 if (has_extension(loader.scanned_layers[j].extensions,
840 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn19c25022015-04-14 14:14:48 -0600841 must_be_hosted)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700842
Jon Ashburn19c25022015-04-14 14:14:48 -0600843 found = true;
844 lib_name[found_count] = loader.scanned_layers[j].name;
845 found_count++;
846 } else {
847 // Extension not found in list for the layer, so test the layer name
848 // as if it is an extension name. Use default layer name based on
849 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
850 char *pEnd;
851 size_t siz;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700852
Jon Ashburn19c25022015-04-14 14:14:48 -0600853 search_name = loader.scanned_layers[j].name;
854 search_name = basename(search_name);
855 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
856 pEnd = strrchr(search_name, '.');
857 siz = (int) (pEnd - search_name);
858 if (siz != strlen(pExtName))
859 continue;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700860
Jon Ashburn19c25022015-04-14 14:14:48 -0600861 if (strncmp(search_name, pExtName, siz) == 0) {
862 found = true;
863 lib_name[found_count] = loader.scanned_layers[j].name;
864 found_count++;
865 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700866 }
867 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600868
869 *out_count = found_count;
870 return found;
Jon Ashburnb8358052014-11-18 09:06:04 -0700871}
872
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600873static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600874{
Ian Elliott4470a302015-02-17 10:33:47 -0700875 char *layerEnv;
Jon Ashburn19c25022015-04-14 14:14:48 -0600876 uint32_t i, len, found_count, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600877 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600878
Ian Elliott4470a302015-02-17 10:33:47 -0700879#if defined(WIN32)
880 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
881 LAYER_NAMES_REGISTRY_VALUE);
882#else // WIN32
883 layerEnv = getenv(LAYER_NAMES_ENV);
884#endif // WIN32
885 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600886 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700887 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600888 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700889 if (p == NULL) {
890#if defined(WIN32)
891 free(layerEnv);
892#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600893 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700894 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600895 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700896#if defined(WIN32)
897 free(layerEnv);
898#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600899 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600900
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600901 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600902 char *lib_name[MAX_LAYER_LIBRARIES];
903 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700904 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600905 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700906 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600907 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700908 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700909 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600910 *(char *) next = '\0';
911 next++;
912 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600913 name = basename(p);
Jon Ashburn19c25022015-04-14 14:14:48 -0600914 if (!find_layer_extension(icd, gpu_index, name, &found_count, lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600915 p = next;
916 continue;
917 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600918
Jon Ashburn19c25022015-04-14 14:14:48 -0600919 for (i = 0; i < found_count; i++) {
920 len = (uint32_t) strlen(name);
921 pLayerNames[count].layer_name = malloc(len + 1);
922 if (!pLayerNames[count].layer_name) {
923 free(pOrig);
924 return count;
925 }
926 strncpy((char *) pLayerNames[count].layer_name, name, len);
927 pLayerNames[count].layer_name[len] = '\0';
928 pLayerNames[count].lib_name = lib_name[i];
929 count++;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600930 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600931 p = next;
932
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700933 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600934
Jon Ashburnd09bd102014-10-22 21:15:26 -0600935 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600936 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600937}
938
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600939static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600940{
Jon Ashburnb8358052014-11-18 09:06:04 -0700941 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn19c25022015-04-14 14:14:48 -0600942 char *lib_name[MAX_LAYER_LIBRARIES];
943 uint32_t found_count, count = 0;
944 bool skip;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600945
946 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700947 /* Load any layers specified in the environment first */
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700948 count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600949
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600950 for (uint32_t i = 0; i < ext_count; i++) {
951 const char *pExtName = ext_names[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600952
Jon Ashburn19c25022015-04-14 14:14:48 -0600953 skip = false;
954 for (uint32_t j = 0; j < count; j++) {
955 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
956 // Extension / Layer already on the list skip it
957 skip = true;
958 break;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600959 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600960 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700961
Jon Ashburn19c25022015-04-14 14:14:48 -0600962 if (!skip && find_layer_extension(icd, gpu_index, pExtName, &found_count, lib_name)) {
963
964 for (uint32_t j = 0; j < found_count; j++) {
965 uint32_t len;
966 len = (uint32_t) strlen(pExtName);
967
968
969 layerNames[count].layer_name = malloc(len + 1);
970 if (!layerNames[count].layer_name)
971 return count;
972 strncpy((char *) layerNames[count].layer_name, pExtName, len);
973 layerNames[count].layer_name[len] = '\0';
974 layerNames[count].lib_name = lib_name[j];
975 count++;
976 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600977 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600978 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700979
980 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600981}
982
Jon Ashburn46d1f582015-01-28 11:01:35 -0700983static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600984{
985 struct loader_icd *icd;
986 struct loader_layers *libs;
987
Jon Ashburn46d1f582015-01-28 11:01:35 -0700988 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600989 if (icd->gpus)
990 free(icd->gpus);
991 icd->gpus = NULL;
992 if (icd->loader_dispatch)
993 free(icd->loader_dispatch);
994 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600995 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600996 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600997 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600998 libs = &(icd->layer_libs[j][i]);
999 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001000 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001001 libs->lib_handle = NULL;
1002 }
Jon Ashburnd09bd102014-10-22 21:15:26 -06001003 if (icd->wrappedGpus[j])
1004 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001005 }
1006 icd->layer_count[j] = 0;
1007 }
1008 icd->gpu_count = 0;
1009 }
1010}
1011
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001012extern uint32_t loader_activate_layers(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001013{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001014 uint32_t count;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001015 VkBaseLayerObject *gpu;
Jon Ashburnb8358052014-11-18 09:06:04 -07001016 struct layer_name_pair *pLayerNames;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001017 if (!icd)
1018 return 0;
Jon Ashburn83a64252015-04-15 11:31:12 -06001019 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001020
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001021 gpu = icd->gpus + gpu_index;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001022 /* activate any layer libraries */
1023 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001024 VkBaseLayerObject *gpuObj = gpu;
Mike Stroyanb050c682015-04-17 12:36:38 -06001025 VkBaseLayerObject *nextGpuObj, *baseObj = (VkBaseLayerObject *) gpuObj->baseObject;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001026 PFN_vkGetProcAddr nextGPA = loader_gpa_internal;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001027
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001028 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001029 if (!count)
1030 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -07001031 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001032
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001033 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburnd09bd102014-10-22 21:15:26 -06001034 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001035 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001036 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -06001037 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001038 nextGpuObj->pGPA = nextGPA;
Mike Stroyanb050c682015-04-17 12:36:38 -06001039 nextGpuObj->baseObject = (VkObject) baseObj;
1040 nextGpuObj->nextObject = (VkObject) gpuObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001041 gpuObj = nextGpuObj;
1042
Jon Ashburn79113cc2014-12-01 14:22:40 -07001043 char funcStr[256];
1044 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001045 if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1046 nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001047 if (!nextGPA) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001048 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001049 continue;
1050 }
1051
Jon Ashburnf2610012014-10-24 15:48:55 -06001052 if (i == 0) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001053 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -06001054 //Insert the new wrapped objects into the list with loader object at head
Mike Stroyanb050c682015-04-17 12:36:38 -06001055 gpu->nextObject = (VkObject) gpuObj;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001056 gpu->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -06001057 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
Mike Stroyanb050c682015-04-17 12:36:38 -06001058 gpuObj->nextObject = (VkObject) baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001059 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -06001060 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001061
1062 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001063 }
1064 else {
1065 //make sure requested Layers matches currently activated Layers
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001066 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001067 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -07001068 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001069 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001070 break;
1071 }
1072 }
1073 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001074 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001075 }
1076 }
1077 return icd->layer_count[gpu_index];
1078}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001079
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001080LOADER_EXPORT VkResult VKAPI vkCreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001081 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001082 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001083{
1084 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -07001085 struct loader_scanned_icds *scanned_icds;
1086 struct loader_icd *icd;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001087 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn340d6662015-04-08 15:49:00 -06001088 uint32_t i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001089
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001090 /* Scan/discover all ICD libraries in a single-threaded manner */
1091 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001092
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001093 /* get layer libraries in a single-threaded manner */
1094 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001095
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001096 /* merge any duplicate extensions */
1097 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1098
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001099 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
1100 if (ptr_instance == NULL) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001101 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001102 }
1103 memset(ptr_instance, 0, sizeof(struct loader_instance));
Jon Ashburn340d6662015-04-08 15:49:00 -06001104 ptr_instance->extension_count = pCreateInfo->extensionCount;
1105 ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
1106 malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001107 if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0))
Tony Barbourd1c35722015-04-16 15:59:00 -06001108 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn340d6662015-04-08 15:49:00 -06001109 for (i = 0; i < ptr_instance->extension_count; i++) {
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001110 if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i]))
1111 return VK_ERROR_INVALID_EXTENSION;
1112 ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1);
Jon Ashburn340d6662015-04-08 15:49:00 -06001113 if (ptr_instance->extension_names[i] == NULL)
Tony Barbourd1c35722015-04-16 15:59:00 -06001114 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn340d6662015-04-08 15:49:00 -06001115 strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
1116 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001117 ptr_instance->next = loader.instances;
1118 loader.instances = ptr_instance;
1119
Jon Ashburn46888392015-01-29 15:45:51 -07001120 scanned_icds = loader.scanned_icd_list;
1121 while (scanned_icds) {
1122 icd = loader_icd_add(ptr_instance, scanned_icds);
1123 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001124 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn46888392015-01-29 15:45:51 -07001125 &(scanned_icds->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001126 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001127 {
1128 ptr_instance->icds = ptr_instance->icds->next;
1129 loader_icd_destroy(icd);
Mike Stroyanb050c682015-04-17 12:36:38 -06001130 scanned_icds->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001131 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001132 "ICD ignored: failed to CreateInstance on device");
1133 }
1134 }
1135 scanned_icds = scanned_icds->next;
1136 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001137
Ian Elliotteb450762015-02-05 15:19:15 -07001138 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001139 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001140 }
Jon Ashburn46888392015-01-29 15:45:51 -07001141
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001142 *pInstance = (VkInstance) ptr_instance;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001143 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001144}
1145
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001146LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
1147 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001148{
1149 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -07001150 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001151 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -06001152 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001153
1154 // Remove this instance from the list of instances:
1155 struct loader_instance *prev = NULL;
1156 struct loader_instance *next = loader.instances;
1157 while (next != NULL) {
1158 if (next == ptr_instance) {
1159 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -06001160 for (i = 0; i < ptr_instance->extension_count; i++) {
1161 free(ptr_instance->extension_names[i]);
1162 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001163 if (prev)
1164 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001165 else
1166 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001167 break;
1168 }
1169 prev = next;
1170 next = next->next;
1171 }
1172 if (next == NULL) {
1173 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001174 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001175 }
1176
Jon Ashburn46888392015-01-29 15:45:51 -07001177 // cleanup any prior layer initializations
1178 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001179
Jon Ashburn46888392015-01-29 15:45:51 -07001180 scanned_icds = loader.scanned_icd_list;
1181 while (scanned_icds) {
1182 if (scanned_icds->instance)
1183 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001184 if (res != VK_SUCCESS)
1185 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001186 "ICD ignored: failed to DestroyInstance on device");
Mike Stroyanb050c682015-04-17 12:36:38 -06001187 scanned_icds->instance = VK_NULL_HANDLE;
Jon Ashburn46888392015-01-29 15:45:51 -07001188 scanned_icds = scanned_icds->next;
1189 }
1190
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001191 free(ptr_instance);
1192
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001193 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001194}
1195
Jon Ashburn83a64252015-04-15 11:31:12 -06001196LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001197
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001198 VkInstance instance,
Jon Ashburn83a64252015-04-15 11:31:12 -06001199 uint32_t* pPhysicalDeviceCount,
Tony Barbourd1c35722015-04-16 15:59:00 -06001200 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001201{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001202 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1203 struct loader_icd *icd;
Jon Ashburn83a64252015-04-15 11:31:12 -06001204 uint32_t n, count = 0;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001205 VkResult res;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001206
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001207 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001208 icd = ptr_instance->icds;
Jon Ashburn83a64252015-04-15 11:31:12 -06001209 if (pPhysicalDevices == NULL) {
1210 while (icd) {
1211 res = icd->scanned_icds->EnumeratePhysicalDevices(
1212 icd->scanned_icds->instance,
1213 &n, NULL);
1214 if (res != VK_SUCCESS)
1215 return res;
1216 icd->gpu_count = n;
1217 count += n;
1218 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001219 }
1220
Jon Ashburn83a64252015-04-15 11:31:12 -06001221 ptr_instance->total_gpu_count = count;
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001222
Jon Ashburn83a64252015-04-15 11:31:12 -06001223 } else
1224 {
Tony Barbourd1c35722015-04-16 15:59:00 -06001225 VkPhysicalDevice* gpus;
Jon Ashburn83a64252015-04-15 11:31:12 -06001226 if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1227 return VK_ERROR_INVALID_VALUE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001228 gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount);
Jon Ashburn83a64252015-04-15 11:31:12 -06001229 if (!gpus)
Tony Barbourd1c35722015-04-16 15:59:00 -06001230 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn83a64252015-04-15 11:31:12 -06001231 while (icd) {
1232 VkBaseLayerObject * wrapped_gpus;
1233 PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
1234
1235 res = icd->scanned_icds->EnumeratePhysicalDevices(
1236 icd->scanned_icds->instance,
1237 &n,
1238 gpus);
1239 if (res == VK_SUCCESS && n) {
1240 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1241 sizeof(VkBaseLayerObject));
1242 icd->gpus = wrapped_gpus;
1243 icd->gpu_count = n;
1244 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1245 sizeof(VkLayerDispatchTable));
1246 for (unsigned int i = 0; i < n; i++) {
1247 (wrapped_gpus + i)->baseObject = gpus[i];
1248 (wrapped_gpus + i)->pGPA = get_proc_addr;
1249 (wrapped_gpus + i)->nextObject = gpus[i];
1250 memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
1251 loader_init_dispatch_table(icd->loader_dispatch + i,
1252 get_proc_addr, gpus[i]);
1253
1254 /* Verify ICD compatibility */
Tony Barbourfc15ff82015-04-20 16:28:46 -06001255 if (!valid_loader_magic_value(gpus[i])) {
Jon Ashburn83a64252015-04-15 11:31:12 -06001256 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001257 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
Jon Ashburn83a64252015-04-15 11:31:12 -06001258 assert(0);
1259 }
1260
1261 const VkLayerDispatchTable **disp;
1262 disp = (const VkLayerDispatchTable **) gpus[i];
1263 *disp = icd->loader_dispatch + i;
1264 loader_activate_layers(icd, i, ptr_instance->extension_count,
1265 (const char *const*) ptr_instance->extension_names);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001266 }
1267
Jon Ashburn83a64252015-04-15 11:31:12 -06001268 count += n;
1269
1270 if (count >= *pPhysicalDeviceCount) {
1271 break;
1272 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001273 }
1274
Jon Ashburn83a64252015-04-15 11:31:12 -06001275 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001276 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001277 }
1278
Jon Ashburn83a64252015-04-15 11:31:12 -06001279 *pPhysicalDeviceCount = count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001280
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001281 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001282}
1283
Tony Barbourd1c35722015-04-16 15:59:00 -06001284LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001285{
Mike Stroyanb050c682015-04-17 12:36:38 -06001286 if (gpu == VK_NULL_HANDLE) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001287
1288 /* return entrypoint addresses that are global (in the loader)*/
1289 return globalGetProcAddr(pName);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001290 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001291
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001292 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001293
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001294 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1295 make sure the loader entrypoint is returned */
1296 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001297 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001298 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001299 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001300
1301 /* return the dispatch table entrypoint for the fastest case */
1302 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001303 if (disp_table == NULL)
1304 return NULL;
1305
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001306 addr = loader_lookup_dispatch_table(disp_table, pName);
1307 if (addr)
1308 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001309 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001310 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001311 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001312 return disp_table->GetProcAddr(gpu, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001313 }
1314}
1315
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001316//TODO make sure createInstance enables extensions that are valid (loader does)
1317//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1318
1319//TODO how is layer extension going to be enabled?
1320//Need to call createInstance on the layer or something
1321
1322LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
1323 VkExtensionInfoType infoType,
1324 uint32_t extensionIndex,
1325 size_t* pDataSize,
1326 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001327{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001328 VkExtensionProperties *ext_props;
1329 uint32_t *count;
1330 /* Scan/discover all ICD libraries in a single-threaded manner */
1331 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001332
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001333 /* get layer libraries in a single-threaded manner */
1334 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001335
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001336 /* merge any duplicate extensions */
1337 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1338
1339
1340 if (pDataSize == NULL)
1341 return VK_ERROR_INVALID_POINTER;
1342
1343 switch (infoType) {
1344 case VK_EXTENSION_INFO_TYPE_COUNT:
1345 *pDataSize = sizeof(uint32_t);
1346 if (pData == NULL)
1347 return VK_SUCCESS;
1348 count = (uint32_t *) pData;
1349 *count = loader.scanned_ext_list_count;
1350 break;
1351 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1352 *pDataSize = sizeof(VkExtensionProperties);
1353 if (pData == NULL)
1354 return VK_SUCCESS;
1355 if (extensionIndex >= loader.scanned_ext_list_count)
1356 return VK_ERROR_INVALID_VALUE;
1357 ext_props = (VkExtensionProperties *) pData;
1358 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1359 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1360 , VK_MAX_EXTENSION_NAME);
1361 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1362 break;
1363 default:
1364 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1365 return VK_ERROR_INVALID_VALUE;
1366 };
1367
1368 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001369}
1370
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001371LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001372{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001373 size_t maxLayerCount;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001374 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001375 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001376 char *lib_name;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001377 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001378 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001379 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001380 char layer_buf[16][256];
1381 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001382
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001383 if (pLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001384 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001385
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001386 maxLayerCount = *pLayerCount;
1387
Jon Ashburn46d1f582015-01-28 11:01:35 -07001388 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001389 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001390
Jon Ashburn1323eb72014-12-02 13:03:09 -07001391 for (int i = 0; i < 16; i++)
1392 layers[i] = &layer_buf[i][0];
1393
1394 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001395 lib_name = loader.scanned_layers[j].name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001396 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1397 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001398 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001399 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1400 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001401 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001402 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001403 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001404 lib_name = basename(lib_name);
1405 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001406 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001407 if (pEnd == NULL || siz <= 0)
1408 continue;
1409 cpyStr = malloc(siz);
1410 if (cpyStr == NULL) {
1411 free(cpyStr);
1412 continue;
1413 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001414 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001415 cpyStr[siz - 1] = '\0';
1416 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001417 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001418 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1419 pOutLayers[count][siz - 1] = '\0';
1420 count++;
1421 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001422 } else {
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001423 size_t cnt = 16; /* only allow 16 layers, for now */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001424 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001425 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001426 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001427 res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001428 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001429 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001430 continue;
1431 if (cnt + count > maxLayerCount)
1432 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001433 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001434 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1435 if (n > 0)
1436 pOutLayers[i - count][n - 1] = '\0';
1437 }
1438 count += cnt;
1439 }
1440 }
1441
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001442 *pLayerCount = count;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001443
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001444 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001445}
1446
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001447LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001448{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001449 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001450 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001451 VkResult res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001452 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001453
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001454 if (instance == VK_NULL_HANDLE)
1455 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001456
1457 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001458
Jon Ashburn98bd4542015-01-29 16:44:24 -07001459 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001460 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001461 break;
1462 }
1463
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001464 if (inst == VK_NULL_HANDLE)
1465 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001466
1467 for (icd = inst->icds; icd; icd = icd->next) {
1468 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1469 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001470 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001471 if (res != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001472 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001473 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001474 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001475 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001476 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001477 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001478 }
1479
Jon Ashburnbb510252015-03-17 13:47:55 -06001480
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001481 /* roll back on errors */
1482 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001483 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001484 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001485 for (uint32_t i = 0; i < icd->gpu_count; i++)
1486 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001487 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001488 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001489 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001490 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001491
1492 return res;
1493 }
1494
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001495 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001496}
1497
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001498LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001499{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001500 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001501 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001502 if (instance == VK_NULL_HANDLE)
1503 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001504
1505 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001506
Jon Ashburnbb510252015-03-17 13:47:55 -06001507 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001508 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001509 break;
1510 }
1511
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001512 if (inst == VK_NULL_HANDLE)
1513 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001514
1515 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1516 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001517 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001518 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001519 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001520 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001521 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001522 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001523 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001524 return res;
1525}
1526
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001527LOADER_EXPORT VkResult VKAPI vkDbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001528{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001529 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001530 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001531 if (instance == VK_NULL_HANDLE)
1532 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001533
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001534 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001535
Jon Ashburnbb510252015-03-17 13:47:55 -06001536 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001537 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001538 break;
1539 }
1540
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001541 if (inst == VK_NULL_HANDLE)
1542 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001543 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1544 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001545 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001546 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001547 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001548 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001549 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001550 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001551 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001552 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001553 }
1554
1555 return res;
1556}