blob: 20646a7b1194fbac623c8398b285950d2ce7c77d [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;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060072 uint32_t layer_count[VK_MAX_PHYSICAL_GPUS];
73 struct loader_layers layer_libs[VK_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnbacb0f52015-04-06 10:58:22 -060074 VkBaseLayerObject *wrappedGpus[VK_MAX_PHYSICAL_GPUS];
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;
88 PFN_vkEnumerateGpus EnumerateGpus;
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 Ashburn4c392fb2015-01-28 19:57:09 -0700446 LOOKUP(fp_enumerate, EnumerateGpus);
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 Ashburn4c392fb2015-01-28 19:57:09 -0700460 new_node->EnumerateGpus = 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
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600719static void loader_init_dispatch_table(VkLayerDispatchTable *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalGpu 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
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600727static void *loader_gpa_internal(VkPhysicalGpu gpu, const char * pName)
728{
729 if (gpu == NULL) {
730 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++)
759 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
Jon Ashburn4d9f4652015-04-08 21:33:34 -0600760 gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700761 *gpu_index = i;
762 return icd;
763 }
764 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600765 }
766 return NULL;
767}
768
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600769static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600770{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600771 if (icd->layer_count[gpu_index])
772 return true;
773 else
774 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600775}
776
Jon Ashburn19c25022015-04-14 14:14:48 -0600777static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
778 struct layer_name_pair * pLayerNames,
779 uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600780{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600781 if (!icd)
782 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600783
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600784 struct loader_layers *obj;
785 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600786 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600787 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600788 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600789 if (icd->layer_libs[gpu_index][j].lib_handle &&
790 !strcmp(icd->layer_libs[gpu_index][j].name,
791 (char *) pLayerNames[i].layer_name) &&
792 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600793 foundLib = true;
794 break;
795 }
796 }
797 if (!foundLib) {
798 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700799 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
800 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700801 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
802 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600803 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600804 continue;
805 } else {
Jon Ashburn19c25022015-04-14 14:14:48 -0600806 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s",
807 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600808 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700809 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600810 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600811 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600812 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600813}
814
Jon Ashburn19c25022015-04-14 14:14:48 -0600815static bool find_layer_extension(struct loader_icd *icd, uint32_t gpu_index,
816 const char *pExtName, uint32_t *out_count,
817 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnb8358052014-11-18 09:06:04 -0700818{
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700819 char *search_name;
Jon Ashburn19c25022015-04-14 14:14:48 -0600820 uint32_t j, found_count = 0;
821 bool must_be_hosted;
822 bool found = false;
Jon Ashburnb8358052014-11-18 09:06:04 -0700823
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700824 /*
825 * The loader provides the abstraction that make layers and extensions work via
826 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600827 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700828 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600829 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700830 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700831
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600832 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700833
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600834 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600835
836 if (!strcmp("Validation", pExtName))
837 must_be_hosted = false;
838 else
839 must_be_hosted = true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600840 if (has_extension(loader.scanned_layers[j].extensions,
841 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn19c25022015-04-14 14:14:48 -0600842 must_be_hosted)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700843
Jon Ashburn19c25022015-04-14 14:14:48 -0600844 found = true;
845 lib_name[found_count] = loader.scanned_layers[j].name;
846 found_count++;
847 } else {
848 // Extension not found in list for the layer, so test the layer name
849 // as if it is an extension name. Use default layer name based on
850 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
851 char *pEnd;
852 size_t siz;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700853
Jon Ashburn19c25022015-04-14 14:14:48 -0600854 search_name = loader.scanned_layers[j].name;
855 search_name = basename(search_name);
856 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
857 pEnd = strrchr(search_name, '.');
858 siz = (int) (pEnd - search_name);
859 if (siz != strlen(pExtName))
860 continue;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700861
Jon Ashburn19c25022015-04-14 14:14:48 -0600862 if (strncmp(search_name, pExtName, siz) == 0) {
863 found = true;
864 lib_name[found_count] = loader.scanned_layers[j].name;
865 found_count++;
866 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700867 }
868 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600869
870 *out_count = found_count;
871 return found;
Jon Ashburnb8358052014-11-18 09:06:04 -0700872}
873
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600874static 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 -0600875{
Ian Elliott4470a302015-02-17 10:33:47 -0700876 char *layerEnv;
Jon Ashburn19c25022015-04-14 14:14:48 -0600877 uint32_t i, len, found_count, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600878 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600879
Ian Elliott4470a302015-02-17 10:33:47 -0700880#if defined(WIN32)
881 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
882 LAYER_NAMES_REGISTRY_VALUE);
883#else // WIN32
884 layerEnv = getenv(LAYER_NAMES_ENV);
885#endif // WIN32
886 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600887 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700888 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600889 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700890 if (p == NULL) {
891#if defined(WIN32)
892 free(layerEnv);
893#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600894 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700895 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600896 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700897#if defined(WIN32)
898 free(layerEnv);
899#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600900 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600901
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600902 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600903 char *lib_name[MAX_LAYER_LIBRARIES];
904 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700905 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600906 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700907 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600908 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700909 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700910 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600911 *(char *) next = '\0';
912 next++;
913 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600914 name = basename(p);
Jon Ashburn19c25022015-04-14 14:14:48 -0600915 if (!find_layer_extension(icd, gpu_index, name, &found_count, lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600916 p = next;
917 continue;
918 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600919
Jon Ashburn19c25022015-04-14 14:14:48 -0600920 for (i = 0; i < found_count; i++) {
921 len = (uint32_t) strlen(name);
922 pLayerNames[count].layer_name = malloc(len + 1);
923 if (!pLayerNames[count].layer_name) {
924 free(pOrig);
925 return count;
926 }
927 strncpy((char *) pLayerNames[count].layer_name, name, len);
928 pLayerNames[count].layer_name[len] = '\0';
929 pLayerNames[count].lib_name = lib_name[i];
930 count++;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600931 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600932 p = next;
933
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700934 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600935
Jon Ashburnd09bd102014-10-22 21:15:26 -0600936 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600937 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600938}
939
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600940static 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 -0600941{
Jon Ashburnb8358052014-11-18 09:06:04 -0700942 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn19c25022015-04-14 14:14:48 -0600943 char *lib_name[MAX_LAYER_LIBRARIES];
944 uint32_t found_count, count = 0;
945 bool skip;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600946
947 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700948 /* Load any layers specified in the environment first */
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700949 count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600950
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600951 for (uint32_t i = 0; i < ext_count; i++) {
952 const char *pExtName = ext_names[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600953
Jon Ashburn19c25022015-04-14 14:14:48 -0600954 skip = false;
955 for (uint32_t j = 0; j < count; j++) {
956 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
957 // Extension / Layer already on the list skip it
958 skip = true;
959 break;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600960 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600961 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700962
Jon Ashburn19c25022015-04-14 14:14:48 -0600963 if (!skip && find_layer_extension(icd, gpu_index, pExtName, &found_count, lib_name)) {
964
965 for (uint32_t j = 0; j < found_count; j++) {
966 uint32_t len;
967 len = (uint32_t) strlen(pExtName);
968
969
970 layerNames[count].layer_name = malloc(len + 1);
971 if (!layerNames[count].layer_name)
972 return count;
973 strncpy((char *) layerNames[count].layer_name, pExtName, len);
974 layerNames[count].layer_name[len] = '\0';
975 layerNames[count].lib_name = lib_name[j];
976 count++;
977 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600978 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600979 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700980
981 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600982}
983
Jon Ashburn46d1f582015-01-28 11:01:35 -0700984static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600985{
986 struct loader_icd *icd;
987 struct loader_layers *libs;
988
Jon Ashburn46d1f582015-01-28 11:01:35 -0700989 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600990 if (icd->gpus)
991 free(icd->gpus);
992 icd->gpus = NULL;
993 if (icd->loader_dispatch)
994 free(icd->loader_dispatch);
995 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600996 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600997 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600998 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600999 libs = &(icd->layer_libs[j][i]);
1000 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001001 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001002 libs->lib_handle = NULL;
1003 }
Jon Ashburnd09bd102014-10-22 21:15:26 -06001004 if (icd->wrappedGpus[j])
1005 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001006 }
1007 icd->layer_count[j] = 0;
1008 }
1009 icd->gpu_count = 0;
1010 }
1011}
1012
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001013extern 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 -06001014{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001015 uint32_t count;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001016 VkBaseLayerObject *gpu;
Jon Ashburnb8358052014-11-18 09:06:04 -07001017 struct layer_name_pair *pLayerNames;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001018 if (!icd)
1019 return 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001020 assert(gpu_index < VK_MAX_PHYSICAL_GPUS);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001021
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001022 gpu = icd->gpus + gpu_index;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001023 /* activate any layer libraries */
1024 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001025 VkBaseLayerObject *gpuObj = gpu;
1026 VkBaseLayerObject *nextGpuObj, *baseObj = gpuObj->baseObject;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001027 PFN_vkGetProcAddr nextGPA = loader_gpa_internal;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001028
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001029 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001030 if (!count)
1031 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -07001032 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001033
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001034 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburnd09bd102014-10-22 21:15:26 -06001035 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001036 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001037 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -06001038 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001039 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -06001040 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001041 nextGpuObj->nextObject = gpuObj;
1042 gpuObj = nextGpuObj;
1043
Jon Ashburn79113cc2014-12-01 14:22:40 -07001044 char funcStr[256];
1045 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001046 if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1047 nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001048 if (!nextGPA) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001049 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 -06001050 continue;
1051 }
1052
Jon Ashburnf2610012014-10-24 15:48:55 -06001053 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001054 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -06001055 //Insert the new wrapped objects into the list with loader object at head
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001056 gpu->nextObject = gpuObj;
1057 gpu->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -06001058 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
1059 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001060 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -06001061 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001062
1063 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001064 }
1065 else {
1066 //make sure requested Layers matches currently activated Layers
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001067 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001068 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -07001069 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001070 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001071 break;
1072 }
1073 }
1074 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001075 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001076 }
1077 }
1078 return icd->layer_count[gpu_index];
1079}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001080
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001081LOADER_EXPORT VkResult VKAPI vkCreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001082 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001083 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001084{
1085 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -07001086 struct loader_scanned_icds *scanned_icds;
1087 struct loader_icd *icd;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001088 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn340d6662015-04-08 15:49:00 -06001089 uint32_t i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001090
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001091 /* Scan/discover all ICD libraries in a single-threaded manner */
1092 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001093
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001094 /* get layer libraries in a single-threaded manner */
1095 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001096
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001097 /* merge any duplicate extensions */
1098 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1099
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001100 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
1101 if (ptr_instance == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001102 return VK_ERROR_OUT_OF_MEMORY;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001103 }
1104 memset(ptr_instance, 0, sizeof(struct loader_instance));
Jon Ashburn340d6662015-04-08 15:49:00 -06001105 ptr_instance->extension_count = pCreateInfo->extensionCount;
1106 ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
1107 malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001108 if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0))
1109 return VK_ERROR_OUT_OF_MEMORY;
Jon Ashburn340d6662015-04-08 15:49:00 -06001110 for (i = 0; i < ptr_instance->extension_count; i++) {
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001111 if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i]))
1112 return VK_ERROR_INVALID_EXTENSION;
1113 ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1);
Jon Ashburn340d6662015-04-08 15:49:00 -06001114 if (ptr_instance->extension_names[i] == NULL)
1115 return VK_ERROR_OUT_OF_MEMORY;
1116 strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
1117 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001118 ptr_instance->next = loader.instances;
1119 loader.instances = ptr_instance;
1120
Jon Ashburn46888392015-01-29 15:45:51 -07001121 scanned_icds = loader.scanned_icd_list;
1122 while (scanned_icds) {
1123 icd = loader_icd_add(ptr_instance, scanned_icds);
1124 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001125 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn46888392015-01-29 15:45:51 -07001126 &(scanned_icds->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001127 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001128 {
1129 ptr_instance->icds = ptr_instance->icds->next;
1130 loader_icd_destroy(icd);
1131 scanned_icds->instance = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001132 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001133 "ICD ignored: failed to CreateInstance on device");
1134 }
1135 }
1136 scanned_icds = scanned_icds->next;
1137 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001138
Ian Elliotteb450762015-02-05 15:19:15 -07001139 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001140 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001141 }
Jon Ashburn46888392015-01-29 15:45:51 -07001142
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001143 *pInstance = (VkInstance) ptr_instance;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001144 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001145}
1146
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001147LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
1148 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001149{
1150 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -07001151 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001152 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -06001153 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001154
1155 // Remove this instance from the list of instances:
1156 struct loader_instance *prev = NULL;
1157 struct loader_instance *next = loader.instances;
1158 while (next != NULL) {
1159 if (next == ptr_instance) {
1160 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -06001161 for (i = 0; i < ptr_instance->extension_count; i++) {
1162 free(ptr_instance->extension_names[i]);
1163 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001164 if (prev)
1165 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001166 else
1167 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001168 break;
1169 }
1170 prev = next;
1171 next = next->next;
1172 }
1173 if (next == NULL) {
1174 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001175 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001176 }
1177
Jon Ashburn46888392015-01-29 15:45:51 -07001178 // cleanup any prior layer initializations
1179 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001180
Jon Ashburn46888392015-01-29 15:45:51 -07001181 scanned_icds = loader.scanned_icd_list;
1182 while (scanned_icds) {
1183 if (scanned_icds->instance)
1184 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001185 if (res != VK_SUCCESS)
1186 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001187 "ICD ignored: failed to DestroyInstance on device");
1188 scanned_icds->instance = NULL;
1189 scanned_icds = scanned_icds->next;
1190 }
1191
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001192 free(ptr_instance);
1193
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001194 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001195}
1196
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001197LOADER_EXPORT VkResult VKAPI vkEnumerateGpus(
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001198
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001199 VkInstance instance,
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001200 uint32_t maxGpus,
1201 uint32_t* pGpuCount,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001202 VkPhysicalGpu* pGpus)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001203{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001204 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1205 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -07001206 uint32_t count = 0;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001207 VkResult res;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001208
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001209 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001210 icd = ptr_instance->icds;
1211 while (icd) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001212 VkPhysicalGpu gpus[VK_MAX_PHYSICAL_GPUS];
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001213 VkBaseLayerObject * wrapped_gpus;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001214 PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -07001215 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001216
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 if (max > VK_MAX_PHYSICAL_GPUS) {
1218 max = VK_MAX_PHYSICAL_GPUS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001219 }
1220
Jon Ashburn46888392015-01-29 15:45:51 -07001221 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
1222 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001223 gpus);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001224 if (res == VK_SUCCESS && n) {
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001225 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1226 sizeof(VkBaseLayerObject));
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001227 icd->gpus = wrapped_gpus;
1228 icd->gpu_count = n;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001229 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1230 sizeof(VkLayerDispatchTable));
Ian Elliott19628802015-02-04 12:06:46 -07001231 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001232 (wrapped_gpus + i)->baseObject = gpus[i];
1233 (wrapped_gpus + i)->pGPA = get_proc_addr;
1234 (wrapped_gpus + i)->nextObject = gpus[i];
Jon Ashburn4d9f4652015-04-08 21:33:34 -06001235 memcpy(pGpus + count, gpus, sizeof(*pGpus));
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001236 loader_init_dispatch_table(icd->loader_dispatch + i,
1237 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001238
1239 /* Verify ICD compatibility */
1240 if (!valid_loader_magic_value(gpus[i])) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001241 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001242 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
1243 assert(0);
1244 }
1245
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001246 const VkLayerDispatchTable **disp;
1247 disp = (const VkLayerDispatchTable **) gpus[i];
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001248 *disp = icd->loader_dispatch + i;
Jon Ashburnecceaad2015-04-08 18:20:54 -06001249 loader_activate_layers(icd, i, ptr_instance->extension_count,
1250 (const char *const*) ptr_instance->extension_names);
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001251 }
1252
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001253 count += n;
1254
1255 if (count >= maxGpus) {
1256 break;
1257 }
1258 }
1259
1260 icd = icd->next;
1261 }
1262
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001263 *pGpuCount = count;
1264
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001265 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001266}
1267
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001268LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001269{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001270 if (gpu == NULL) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001271
1272 /* return entrypoint addresses that are global (in the loader)*/
1273 return globalGetProcAddr(pName);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001274 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001275
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001276 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001277
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001278 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1279 make sure the loader entrypoint is returned */
1280 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001281 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001282 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001283 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001284
1285 /* return the dispatch table entrypoint for the fastest case */
1286 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001287 if (disp_table == NULL)
1288 return NULL;
1289
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001290 addr = loader_lookup_dispatch_table(disp_table, pName);
1291 if (addr)
1292 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001293 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001294 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001295 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001296 return disp_table->GetProcAddr(gpu, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001297 }
1298}
1299
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001300//TODO make sure createInstance enables extensions that are valid (loader does)
1301//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1302
1303//TODO how is layer extension going to be enabled?
1304//Need to call createInstance on the layer or something
1305
1306LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
1307 VkExtensionInfoType infoType,
1308 uint32_t extensionIndex,
1309 size_t* pDataSize,
1310 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001311{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001312 VkExtensionProperties *ext_props;
1313 uint32_t *count;
1314 /* Scan/discover all ICD libraries in a single-threaded manner */
1315 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001316
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001317 /* get layer libraries in a single-threaded manner */
1318 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001319
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001320 /* merge any duplicate extensions */
1321 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1322
1323
1324 if (pDataSize == NULL)
1325 return VK_ERROR_INVALID_POINTER;
1326
1327 switch (infoType) {
1328 case VK_EXTENSION_INFO_TYPE_COUNT:
1329 *pDataSize = sizeof(uint32_t);
1330 if (pData == NULL)
1331 return VK_SUCCESS;
1332 count = (uint32_t *) pData;
1333 *count = loader.scanned_ext_list_count;
1334 break;
1335 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1336 *pDataSize = sizeof(VkExtensionProperties);
1337 if (pData == NULL)
1338 return VK_SUCCESS;
1339 if (extensionIndex >= loader.scanned_ext_list_count)
1340 return VK_ERROR_INVALID_VALUE;
1341 ext_props = (VkExtensionProperties *) pData;
1342 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1343 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1344 , VK_MAX_EXTENSION_NAME);
1345 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1346 break;
1347 default:
1348 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1349 return VK_ERROR_INVALID_VALUE;
1350 };
1351
1352 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001353}
1354
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001355LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalGpu gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001356{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001357 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001358 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001359 char *lib_name;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001360 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001361 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001362 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001363 char layer_buf[16][256];
1364 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001365
Jon Ashburn1323eb72014-12-02 13:03:09 -07001366 if (pOutLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001367 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001368
Jon Ashburn46d1f582015-01-28 11:01:35 -07001369 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001370 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001371
Jon Ashburn1323eb72014-12-02 13:03:09 -07001372 for (int i = 0; i < 16; i++)
1373 layers[i] = &layer_buf[i][0];
1374
1375 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001376 lib_name = loader.scanned_layers[j].name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001377 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1378 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001379 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001380 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1381 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001382 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001383 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001384 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001385 lib_name = basename(lib_name);
1386 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001387 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001388 if (pEnd == NULL || siz <= 0)
1389 continue;
1390 cpyStr = malloc(siz);
1391 if (cpyStr == NULL) {
1392 free(cpyStr);
1393 continue;
1394 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001395 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001396 cpyStr[siz - 1] = '\0';
1397 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001398 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001399 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1400 pOutLayers[count][siz - 1] = '\0';
1401 count++;
1402 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001403 } else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001404 size_t cnt;
1405 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001406 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001407 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001408 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1409 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001410 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001411 continue;
1412 if (cnt + count > maxLayerCount)
1413 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001414 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001415 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1416 if (n > 0)
1417 pOutLayers[i - count][n - 1] = '\0';
1418 }
1419 count += cnt;
1420 }
1421 }
1422
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001423 *pOutLayerCount = count;
1424
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001425 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001426}
1427
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001428LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001429{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001430 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001431 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001432 VkResult res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001433 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001434
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 if (instance == VK_NULL_HANDLE)
1436 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001437
1438 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001439
Jon Ashburn98bd4542015-01-29 16:44:24 -07001440 for (inst = loader.instances; inst; inst = inst->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001441 if (inst == instance)
1442 break;
1443 }
1444
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001445 if (inst == VK_NULL_HANDLE)
1446 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001447
1448 for (icd = inst->icds; icd; icd = icd->next) {
1449 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1450 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001451 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001452 if (res != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001453 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001454 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001455 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001456 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001457 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001458 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001459 }
1460
Jon Ashburnbb510252015-03-17 13:47:55 -06001461
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001462 /* roll back on errors */
1463 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001464 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001465 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001466 for (uint32_t i = 0; i < icd->gpu_count; i++)
1467 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001468 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001469 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001470 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001471 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001472
1473 return res;
1474 }
1475
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001476 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001477}
1478
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001479LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001480{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001481 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001482 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001483 if (instance == VK_NULL_HANDLE)
1484 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001485
1486 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001487
Jon Ashburnbb510252015-03-17 13:47:55 -06001488 for (inst = loader.instances; inst; inst = inst->next) {
1489 if (inst == instance)
1490 break;
1491 }
1492
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001493 if (inst == VK_NULL_HANDLE)
1494 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001495
1496 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1497 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001498 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001499 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001500 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001501 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001502 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001503 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001504 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001505 return res;
1506}
1507
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001508LOADER_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 +08001509{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001510 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001511 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001512 if (instance == VK_NULL_HANDLE)
1513 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001514
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001515 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001516
Jon Ashburnbb510252015-03-17 13:47:55 -06001517 for (inst = loader.instances; inst; inst = inst->next) {
1518 if (inst == instance)
1519 break;
1520 }
1521
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001522 if (inst == VK_NULL_HANDLE)
1523 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001524 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1525 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001526 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001527 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001528 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001529 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001530 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001531 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001532 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001533 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001534 }
1535
1536 return res;
1537}