blob: b5b6889da8d50174dbff32cdef069bf711200093 [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;
Ian Elliottf851ddf2015-04-28 15:57:32 -0600128 VkResult rtn_value;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600129 char *rtn_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600130 DWORD rtn_len = 0;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600131 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,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600146 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600147 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,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600153 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600154 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;
Tony Barbour18f71552015-04-22 11:36:22 -0600180 size_t 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 Elliottf851ddf2015-04-28 15:57:32 -0600187 registry_len = (registry_str) ? (DWORD) 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
Jon Ashburnae053e92015-04-24 14:10:50 -0700236#if defined(WIN32)
237 OutputDebugString(msg);
Jon Ashburn1de39402015-05-05 16:20:46 -0600238#endif
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600239 fputs(msg, stderr);
240 fputc('\n', stderr);
Jon Ashburn1de39402015-05-05 16:20:46 -0600241
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242}
243
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600244static bool has_extension(struct extension_property *exts, uint32_t count,
245 const char *name, bool must_be_hosted)
246{
247 uint32_t i;
248 for (i = 0; i < count; i++) {
249 if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
250 return true;
251 }
252 return false;
253}
254
255static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
256 uint32_t *count_out,
257 struct extension_property **props_out)
258{
259 uint32_t i, count, cur;
260 size_t siz = sizeof(count);
261 struct extension_property *ext_props;
262 VkExtensionProperties vk_prop;
263 VkResult res;
264
265 *count_out = 0;
266 *props_out = NULL;
267 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
268 if (res != VK_SUCCESS) {
269 loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
270 return;
271 }
272 ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
273 if (ext_props == NULL) {
274 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
275 return;
276 }
277 siz = sizeof(VkExtensionProperties);
278 cur = 0;
279 for (i = 0; i < count; i++) {
280 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
281 if (res == VK_SUCCESS) {
282 (ext_props + cur)->hosted = false;
283 (ext_props + cur)->version = vk_prop.version;
284 strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
285 (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
286 cur++;
287 }
288 *count_out = cur;
289 *props_out = ext_props;
290 }
291 return;
292}
293
294static void loader_init_ext_list()
295{
296 loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
297 loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
298 memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
299 loader.scanned_ext_list_count = 0;
300}
301
302#if 0 // currently no place to call this
303static void loader_destroy_ext_list()
304{
305 free(loader.scanned_ext_list);
306 loader.scanned_ext_list_capacity = 0;
307 loader.scanned_ext_list_count = 0;
308}
309#endif
310
311static void loader_add_to_ext_list(uint32_t count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600312 struct extension_property *prop_list,
313 bool is_layer_ext)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600314{
315 uint32_t i, j;
316 bool duplicate;
317 struct extension_property *cur_ext;
318
319 if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
320 loader_init_ext_list();
321
322 if (loader.scanned_ext_list == NULL)
323 return;
324
325 struct extension_property *ext_list, **ext_list_addr;
326
327 for (i = 0; i < count; i++) {
328 cur_ext = prop_list + i;
329
330 // look for duplicates or not
331 duplicate = false;
332 for (j = 0; j < loader.scanned_ext_list_count; j++) {
333 ext_list = loader.scanned_ext_list[j];
334 if (!strcmp(cur_ext->extName, ext_list->extName)) {
335 duplicate = true;
336 ext_list->hosted = false;
337 break;
338 }
339 }
340
341 // add to list at end
342 if (!duplicate) {
343 // check for enough capacity
344 if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
345 >= loader.scanned_ext_list_capacity) {
346 // double capacity
347 loader.scanned_ext_list_capacity *= 2;
348 loader.scanned_ext_list = realloc(loader.scanned_ext_list,
349 loader.scanned_ext_list_capacity);
350 }
351 ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
352 *ext_list_addr = cur_ext;
353 cur_ext->hosted = true;
354 }
355
356 }
357}
358
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600359static bool loader_is_extension_scanned(const char *name)
360{
361 uint32_t i;
362
363 for (i = 0; i < loader.scanned_ext_list_count; i++) {
364 if (!strcmp(name, loader.scanned_ext_list[i]->extName))
365 return true;
366 }
367 return false;
368}
369
Tony Barbour18f71552015-04-22 11:36:22 -0600370static void loader_coalesce_extensions(void)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600371{
372 uint32_t i;
373 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
374
375 // traverse scanned icd list adding non-duplicate extensions to the list
376 while (icd_list != NULL) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600377 loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600378 icd_list = icd_list->next;
379 };
380
381 //Traverse layers list adding non-duplicate extensions to the list
382 for (i = 0; i < loader.scanned_layer_count; i++) {
383 loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600384 loader.scanned_layers[i].extensions, true);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600385 }
386}
387
388static void loader_icd_destroy(struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800389{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700390 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800391 free(icd);
392}
393
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600394static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800395{
396 struct loader_icd *icd;
397
398 icd = malloc(sizeof(*icd));
399 if (!icd)
400 return NULL;
401
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600402 memset(icd, 0, sizeof(*icd));
403
Jon Ashburn46d1f582015-01-28 11:01:35 -0700404 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800405
406 return icd;
407}
408
Jon Ashburn46888392015-01-29 15:45:51 -0700409static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
410 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800411{
412 struct loader_icd *icd;
413
Jon Ashburn46d1f582015-01-28 11:01:35 -0700414 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800415 if (!icd)
416 return NULL;
417
Chia-I Wu13a61a52014-08-04 11:18:20 +0800418 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700419 icd->next = ptr_inst->icds;
420 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800421
422 return icd;
423}
424
Jon Ashburn46d1f582015-01-28 11:01:35 -0700425static void loader_scanned_icd_add(const char *filename)
426{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700427 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600428 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
429 void *fp_get_global_ext_info;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700430 struct loader_scanned_icds *new_node;
431
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700432 // Used to call: dlopen(filename, RTLD_LAZY);
433 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700434 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600435 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700436 return;
437 }
438
439#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600440 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700441 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600442 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700443 return; \
444 } \
445} while (0)
446
447 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700448 LOOKUP(fp_create_inst, CreateInstance);
449 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn83a64252015-04-15 11:31:12 -0600450 LOOKUP(fp_enumerate, EnumeratePhysicalDevices);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600451 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700452#undef LOOKUP
453
454 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
455 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600456 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700457 return;
458 }
459
460 new_node->handle = handle;
461 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700462 new_node->CreateInstance = fp_create_inst;
463 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn83a64252015-04-15 11:31:12 -0600464 new_node->EnumeratePhysicalDevices = fp_enumerate;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600465 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
466 new_node->extension_count = 0;
467 new_node->extensions = NULL;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700468 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700469
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600470 loader.scanned_icd_list = new_node;
471
472 if (fp_get_global_ext_info) {
473 get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
474 &new_node->extension_count,
475 &new_node->extensions);
476 } else {
477 loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
478 }
479}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700480
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600481/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600482 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600483 *
484 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600485 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
486 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600487 *
488 * \returns
489 * void; but side effect is to set loader_icd_scanned to true
490 */
491static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800492{
Ian Elliott4470a302015-02-17 10:33:47 -0700493 const char *p, *next;
494 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600495 DIR *sysdir;
496 struct dirent *dent;
497 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600498 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700499 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700500#if defined(WIN32)
501 bool must_free_libPaths;
502 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
503 DRIVER_PATH_REGISTRY_VALUE);
504 if (libPaths != NULL) {
505 must_free_libPaths = true;
506 } else {
507 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600508 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700509 }
510#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600511 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700512 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
513 libPaths = getenv(DRIVER_PATH_ENV);
514 }
515 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600516 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600517 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700518#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800519
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600520 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700521 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600522 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700523 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600524 next = p + len;
525 }
526 else {
Ian Elliott19628802015-02-04 12:06:46 -0700527 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600528 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
529 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600530 next++;
531 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800532
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700533 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
534 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600535 sysdir = opendir(p);
536 if (sysdir) {
537 dent = readdir(sysdir);
538 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600539 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
540 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700541 */
542 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600543 VK_DRIVER_LIBRARY_PREFIX,
544 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700545 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600546 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
547 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700548 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600549 VK_LIBRARY_SUFFIX,
550 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700551 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
552 loader_scanned_icd_add(icd_library);
553 }
554 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600555
556 dent = readdir(sysdir);
557 }
558 closedir(sysdir);
559 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800560 }
561
Ian Elliott4470a302015-02-17 10:33:47 -0700562#if defined(WIN32)
563 // Free any allocated memory:
564 if (must_free_libPaths) {
565 free(libPaths);
566 }
567#endif // WIN32
568
569 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700570 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800571}
572
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600573
Ian Elliott4470a302015-02-17 10:33:47 -0700574static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600575{
576 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700577 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600578 DIR *curdir;
579 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700580 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600581 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600582 uint32_t count;
583 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600584
Ian Elliott4470a302015-02-17 10:33:47 -0700585#if defined(WIN32)
586 bool must_free_libPaths;
587 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
588 LAYERS_PATH_REGISTRY_VALUE);
589 if (libPaths != NULL) {
590 must_free_libPaths = true;
591 } else {
592 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600593 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600594 }
Ian Elliott4470a302015-02-17 10:33:47 -0700595#else // WIN32
596 if (geteuid() == getuid()) {
597 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700598 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700599 }
600 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600601 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700602 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700603#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600604
Ian Elliott4470a302015-02-17 10:33:47 -0700605 if (libPaths == NULL) {
606 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700607 return;
608 }
Ian Elliott4470a302015-02-17 10:33:47 -0700609 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700610 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700611 if (loader.layer_dirs == NULL) {
612 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700613 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700614 }
615 // Alloc passed, so we know there is enough space to hold the string, don't
616 // need strncpy
617 strcpy(loader.layer_dirs, libPaths);
618#if defined(WIN32)
619 // Free any allocated memory:
620 if (must_free_libPaths) {
621 free(libPaths);
622 must_free_libPaths = false;
623 }
624#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700625 libPaths = loader.layer_dirs;
626
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600627 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600628 for (i = 0; i < loader.scanned_layer_count; i++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600629 if (loader.scanned_layers[i].name != NULL)
630 free(loader.scanned_layers[i].name);
631 if (loader.scanned_layers[i].extensions != NULL)
632 free(loader.scanned_layers[i].extensions);
633 loader.scanned_layers[i].name = NULL;
634 loader.scanned_layers[i].extensions = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600635 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600636 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600637 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600638
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600639 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600640 next = strchr(p, PATH_SEPERATOR);
641 if (next == NULL) {
642 len = (uint32_t) strlen(p);
643 next = p + len;
644 }
645 else {
646 len = (uint32_t) (next - p);
647 *(char *) next = '\0';
648 next++;
649 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600650
651 curdir = opendir(p);
652 if (curdir) {
653 dent = readdir(curdir);
654 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600655 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
656 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700657 */
658 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600659 VK_LAYER_LIBRARY_PREFIX,
660 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700661 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600662 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
663 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700664 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600665 VK_LIBRARY_SUFFIX,
666 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700667 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600668 snprintf(temp_str, sizeof(temp_str),
669 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700670 // Used to call: dlopen(temp_str, RTLD_LAZY)
671 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
672 dent = readdir(curdir);
673 continue;
674 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600675 if (count == MAX_LAYER_LIBRARIES) {
676 loader_log(VK_DBG_MSG_ERROR, 0,
677 "%s ignored: max layer libraries exceed",
678 temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700679 break;
680 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600681 fp_get_ext = loader_platform_get_proc_address(handle,
682 "vkGetGlobalExtensionInfo");
683
684 if (!fp_get_ext) {
685 loader_log(VK_DBG_MSG_WARNING, 0,
686 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
687 temp_str);
688 dent = readdir(curdir);
689 loader_platform_close_library(handle);
690 continue;
691 }
692
693 loader.scanned_layers[count].name =
694 malloc(strlen(temp_str) + 1);
695 if (loader.scanned_layers[count].name == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600696 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700697 break;
698 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600699
700 get_global_extensions(fp_get_ext,
701 &loader.scanned_layers[count].extension_count,
702 &loader.scanned_layers[count].extensions);
703
704
705 strcpy(loader.scanned_layers[count].name, temp_str);
706 count++;
707 loader_platform_close_library(handle);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700708 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600709 }
710
711 dent = readdir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600712 } // while (dir_entry)
713 if (count == MAX_LAYER_LIBRARIES)
714 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600715 closedir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600716 } // if (curdir))
717 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600718
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600719 loader.scanned_layer_count = count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600720 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600721}
722
Chia-I Wu04ac7ff2015-04-17 10:49:15 +0800723static void * VKAPI loader_gpa_internal(VkPhysicalDevice gpu, const char * pName)
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600724{
Mike Stroyanb050c682015-04-17 12:36:38 -0600725 if (gpu == VK_NULL_HANDLE) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600726 return NULL;;
727 }
728 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
729 VkLayerDispatchTable * disp_table = * (VkLayerDispatchTable **) gpuw->baseObject;
730 void *addr;
731
732 if (disp_table == NULL)
733 return NULL;
734
735 addr = loader_lookup_dispatch_table(disp_table, pName);
736 if (addr)
737 return addr;
738 else {
739 if (disp_table->GetProcAddr == NULL)
740 return NULL;
Jon Ashburn3a11c8e2015-04-24 17:15:00 -0700741 if (gpuw->baseObject == gpuw->nextObject)
742 return gpuw->pGPA(gpuw->baseObject, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600743 return disp_table->GetProcAddr(gpuw->nextObject, pName);
744 }
745}
746
Jon Ashburn1ed042c2015-05-01 18:00:33 -0600747struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600748{
Jon Ashburn4d9f4652015-04-08 21:33:34 -0600749 /*
750 * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
751 * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
752 * need to test it against the wrapped GPU's base object.
753 */
Jon Ashburn98bd4542015-01-29 16:44:24 -0700754 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
755 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
756 for (uint32_t i = 0; i < icd->gpu_count; i++)
Mike Stroyanb050c682015-04-17 12:36:38 -0600757 if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700758 *gpu_index = i;
759 return icd;
760 }
761 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600762 }
763 return NULL;
764}
765
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600766static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600767{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600768 if (icd->layer_count[gpu_index])
769 return true;
770 else
771 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600772}
773
Jon Ashburn19c25022015-04-14 14:14:48 -0600774static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
775 struct layer_name_pair * pLayerNames,
776 uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600777{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600778 if (!icd)
779 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600780
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600781 struct loader_layers *obj;
782 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600783 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600784 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600785 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600786 if (icd->layer_libs[gpu_index][j].lib_handle &&
787 !strcmp(icd->layer_libs[gpu_index][j].name,
788 (char *) pLayerNames[i].layer_name) &&
789 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600790 foundLib = true;
791 break;
792 }
793 }
794 if (!foundLib) {
795 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700796 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
797 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700798 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
799 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600800 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600801 continue;
802 } else {
Jon Ashburn19c25022015-04-14 14:14:48 -0600803 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s",
804 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600805 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700806 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600807 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600808 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600809 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600810}
811
Jon Ashburn3d454752015-05-08 14:35:08 -0600812static bool find_layer_extension(const char *pExtName, uint32_t *out_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600813 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnb8358052014-11-18 09:06:04 -0700814{
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700815 char *search_name;
Jon Ashburn19c25022015-04-14 14:14:48 -0600816 uint32_t j, found_count = 0;
817 bool must_be_hosted;
818 bool found = false;
Jon Ashburnb8358052014-11-18 09:06:04 -0700819
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700820 /*
821 * The loader provides the abstraction that make layers and extensions work via
822 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600823 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700824 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600825 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700826 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700827
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600828 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700829
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600830 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600831
832 if (!strcmp("Validation", pExtName))
833 must_be_hosted = false;
834 else
835 must_be_hosted = true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600836 if (has_extension(loader.scanned_layers[j].extensions,
837 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn19c25022015-04-14 14:14:48 -0600838 must_be_hosted)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700839
Jon Ashburn19c25022015-04-14 14:14:48 -0600840 found = true;
841 lib_name[found_count] = loader.scanned_layers[j].name;
842 found_count++;
843 } else {
844 // Extension not found in list for the layer, so test the layer name
845 // as if it is an extension name. Use default layer name based on
846 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
847 char *pEnd;
848 size_t siz;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700849
Jon Ashburn19c25022015-04-14 14:14:48 -0600850 search_name = loader.scanned_layers[j].name;
851 search_name = basename(search_name);
852 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
853 pEnd = strrchr(search_name, '.');
854 siz = (int) (pEnd - search_name);
855 if (siz != strlen(pExtName))
856 continue;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700857
Jon Ashburn19c25022015-04-14 14:14:48 -0600858 if (strncmp(search_name, pExtName, siz) == 0) {
859 found = true;
860 lib_name[found_count] = loader.scanned_layers[j].name;
861 found_count++;
862 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700863 }
864 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600865
866 *out_count = found_count;
867 return found;
Jon Ashburnb8358052014-11-18 09:06:04 -0700868}
869
Jon Ashburn3d454752015-05-08 14:35:08 -0600870static uint32_t loader_get_layer_env(struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600871{
Ian Elliott4470a302015-02-17 10:33:47 -0700872 char *layerEnv;
Jon Ashburn19c25022015-04-14 14:14:48 -0600873 uint32_t i, len, found_count, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600874 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600875
Ian Elliott4470a302015-02-17 10:33:47 -0700876#if defined(WIN32)
877 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
878 LAYER_NAMES_REGISTRY_VALUE);
879#else // WIN32
880 layerEnv = getenv(LAYER_NAMES_ENV);
881#endif // WIN32
882 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600883 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700884 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600885 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700886 if (p == NULL) {
887#if defined(WIN32)
888 free(layerEnv);
889#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600890 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700891 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600892 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700893#if defined(WIN32)
894 free(layerEnv);
895#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600896 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600897
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600898 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600899 char *lib_name[MAX_LAYER_LIBRARIES];
900 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700901 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600902 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700903 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600904 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700905 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700906 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600907 *(char *) next = '\0';
908 next++;
909 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600910 name = basename(p);
Jon Ashburn3d454752015-05-08 14:35:08 -0600911 if (!find_layer_extension(name, &found_count, lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600912 p = next;
913 continue;
914 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600915
Jon Ashburn19c25022015-04-14 14:14:48 -0600916 for (i = 0; i < found_count; i++) {
917 len = (uint32_t) strlen(name);
918 pLayerNames[count].layer_name = malloc(len + 1);
919 if (!pLayerNames[count].layer_name) {
920 free(pOrig);
921 return count;
922 }
923 strncpy((char *) pLayerNames[count].layer_name, name, len);
924 pLayerNames[count].layer_name[len] = '\0';
925 pLayerNames[count].lib_name = lib_name[i];
926 count++;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600927 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600928 p = next;
929
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700930 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600931
Jon Ashburnd09bd102014-10-22 21:15:26 -0600932 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600933 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600934}
935
Jon Ashburn3d454752015-05-08 14:35:08 -0600936static uint32_t loader_get_layer_libs(uint32_t ext_count, const char *const* ext_names, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600937{
Jon Ashburnb8358052014-11-18 09:06:04 -0700938 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn19c25022015-04-14 14:14:48 -0600939 char *lib_name[MAX_LAYER_LIBRARIES];
940 uint32_t found_count, count = 0;
941 bool skip;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600942
943 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700944 /* Load any layers specified in the environment first */
Jon Ashburn3d454752015-05-08 14:35:08 -0600945 count = loader_get_layer_env(layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600946
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600947 for (uint32_t i = 0; i < ext_count; i++) {
948 const char *pExtName = ext_names[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600949
Jon Ashburn19c25022015-04-14 14:14:48 -0600950 skip = false;
951 for (uint32_t j = 0; j < count; j++) {
952 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
953 // Extension / Layer already on the list skip it
954 skip = true;
955 break;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600956 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600957 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700958
Jon Ashburn3d454752015-05-08 14:35:08 -0600959 if (!skip && find_layer_extension(pExtName, &found_count, lib_name)) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600960
961 for (uint32_t j = 0; j < found_count; j++) {
962 uint32_t len;
963 len = (uint32_t) strlen(pExtName);
964
965
966 layerNames[count].layer_name = malloc(len + 1);
967 if (!layerNames[count].layer_name)
968 return count;
969 strncpy((char *) layerNames[count].layer_name, pExtName, len);
970 layerNames[count].layer_name[len] = '\0';
971 layerNames[count].lib_name = lib_name[j];
972 count++;
973 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600974 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600975 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700976
977 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600978}
979
Jon Ashburn46d1f582015-01-28 11:01:35 -0700980static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600981{
982 struct loader_icd *icd;
983 struct loader_layers *libs;
984
Jon Ashburn46d1f582015-01-28 11:01:35 -0700985 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600986 if (icd->gpus)
987 free(icd->gpus);
988 icd->gpus = NULL;
989 if (icd->loader_dispatch)
990 free(icd->loader_dispatch);
991 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600992 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600993 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600994 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600995 libs = &(icd->layer_libs[j][i]);
996 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700997 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600998 libs->lib_handle = NULL;
999 }
Jon Ashburnd09bd102014-10-22 21:15:26 -06001000 if (icd->wrappedGpus[j])
1001 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001002 }
1003 icd->layer_count[j] = 0;
1004 }
1005 icd->gpu_count = 0;
1006 }
1007}
1008
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001009extern 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 -06001010{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001011 uint32_t count;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001012 VkBaseLayerObject *gpu;
Jon Ashburnb8358052014-11-18 09:06:04 -07001013 struct layer_name_pair *pLayerNames;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001014 if (!icd)
1015 return 0;
Jon Ashburn83a64252015-04-15 11:31:12 -06001016 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001017
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001018 gpu = icd->gpus + gpu_index;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001019 /* activate any layer libraries */
1020 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001021 VkBaseLayerObject *gpuObj = gpu;
Mike Stroyanb050c682015-04-17 12:36:38 -06001022 VkBaseLayerObject *nextGpuObj, *baseObj = (VkBaseLayerObject *) gpuObj->baseObject;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001023 PFN_vkGetProcAddr nextGPA = loader_gpa_internal;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001024
Jon Ashburn3d454752015-05-08 14:35:08 -06001025 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001026 if (!count)
1027 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -07001028 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001029
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001030 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburnd09bd102014-10-22 21:15:26 -06001031 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001032 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001033 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -06001034 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001035 nextGpuObj->pGPA = nextGPA;
Mike Stroyanb050c682015-04-17 12:36:38 -06001036 nextGpuObj->baseObject = (VkObject) baseObj;
1037 nextGpuObj->nextObject = (VkObject) gpuObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001038 gpuObj = nextGpuObj;
1039
Jon Ashburn79113cc2014-12-01 14:22:40 -07001040 char funcStr[256];
1041 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001042 if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1043 nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001044 if (!nextGPA) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001045 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 -06001046 continue;
1047 }
1048
Jon Ashburnf2610012014-10-24 15:48:55 -06001049 if (i == 0) {
Jon Ashburnfbb4e252015-05-04 16:27:53 -06001050 loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -06001051 //Insert the new wrapped objects into the list with loader object at head
Mike Stroyanb050c682015-04-17 12:36:38 -06001052 gpu->nextObject = (VkObject) gpuObj;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001053 gpu->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -06001054 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
Mike Stroyanb050c682015-04-17 12:36:38 -06001055 gpuObj->nextObject = (VkObject) baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001056 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -06001057 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001058
1059 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001060 }
1061 else {
1062 //make sure requested Layers matches currently activated Layers
Jon Ashburn3d454752015-05-08 14:35:08 -06001063 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001064 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -07001065 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001066 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001067 break;
1068 }
1069 }
1070 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001071 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001072 }
1073 }
1074 return icd->layer_count[gpu_index];
1075}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001076
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001077LOADER_EXPORT VkResult VKAPI vkCreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001078 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001079 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001080{
1081 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -07001082 struct loader_scanned_icds *scanned_icds;
1083 struct loader_icd *icd;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001084 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn340d6662015-04-08 15:49:00 -06001085 uint32_t i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001086
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001087 /* Scan/discover all ICD libraries in a single-threaded manner */
1088 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001089
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001090 /* get layer libraries in a single-threaded manner */
1091 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -07001092
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001093 /* merge any duplicate extensions */
1094 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1095
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001096 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
1097 if (ptr_instance == NULL) {
Tony Barbourd1c35722015-04-16 15:59:00 -06001098 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001099 }
1100 memset(ptr_instance, 0, sizeof(struct loader_instance));
Jon Ashburn340d6662015-04-08 15:49:00 -06001101 ptr_instance->extension_count = pCreateInfo->extensionCount;
1102 ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
1103 malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001104 if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0))
Tony Barbourd1c35722015-04-16 15:59:00 -06001105 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn340d6662015-04-08 15:49:00 -06001106 for (i = 0; i < ptr_instance->extension_count; i++) {
Jon Ashburnfc2e38c2015-04-14 09:15:32 -06001107 if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i]))
1108 return VK_ERROR_INVALID_EXTENSION;
1109 ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1);
Jon Ashburn340d6662015-04-08 15:49:00 -06001110 if (ptr_instance->extension_names[i] == NULL)
Tony Barbourd1c35722015-04-16 15:59:00 -06001111 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn340d6662015-04-08 15:49:00 -06001112 strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
1113 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001114 ptr_instance->next = loader.instances;
1115 loader.instances = ptr_instance;
1116
Jon Ashburn46888392015-01-29 15:45:51 -07001117 scanned_icds = loader.scanned_icd_list;
1118 while (scanned_icds) {
1119 icd = loader_icd_add(ptr_instance, scanned_icds);
1120 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001121 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn46888392015-01-29 15:45:51 -07001122 &(scanned_icds->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001123 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001124 {
1125 ptr_instance->icds = ptr_instance->icds->next;
1126 loader_icd_destroy(icd);
Mike Stroyanb050c682015-04-17 12:36:38 -06001127 scanned_icds->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001128 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001129 "ICD ignored: failed to CreateInstance on device");
1130 }
1131 }
1132 scanned_icds = scanned_icds->next;
1133 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001134
Ian Elliotteb450762015-02-05 15:19:15 -07001135 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001136 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001137 }
Jon Ashburn46888392015-01-29 15:45:51 -07001138
Jon Ashburnfbb4e252015-05-04 16:27:53 -06001139 loader_init_instance_dispatch_table(&ptr_instance->disp);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001140 *pInstance = (VkInstance) ptr_instance;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001141 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001142}
1143
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001144LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
1145 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001146{
1147 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -07001148 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001149 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -06001150 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001151
1152 // Remove this instance from the list of instances:
1153 struct loader_instance *prev = NULL;
1154 struct loader_instance *next = loader.instances;
1155 while (next != NULL) {
1156 if (next == ptr_instance) {
1157 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -06001158 for (i = 0; i < ptr_instance->extension_count; i++) {
1159 free(ptr_instance->extension_names[i]);
1160 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001161 if (prev)
1162 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001163 else
1164 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001165 break;
1166 }
1167 prev = next;
1168 next = next->next;
1169 }
1170 if (next == NULL) {
1171 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001172 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001173 }
1174
Jon Ashburn46888392015-01-29 15:45:51 -07001175 // cleanup any prior layer initializations
1176 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001177
Jon Ashburn46888392015-01-29 15:45:51 -07001178 scanned_icds = loader.scanned_icd_list;
1179 while (scanned_icds) {
Tony Barbourf20f87b2015-04-22 09:02:32 -06001180 if (scanned_icds->instance) {
Jon Ashburn46888392015-01-29 15:45:51 -07001181 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Tony Barbourf20f87b2015-04-22 09:02:32 -06001182 if (res != VK_SUCCESS)
1183 loader_log(VK_DBG_MSG_WARNING, 0,
1184 "ICD ignored: failed to DestroyInstance on device");
1185 }
Mike Stroyanb050c682015-04-17 12:36:38 -06001186 scanned_icds->instance = VK_NULL_HANDLE;
Jon Ashburn46888392015-01-29 15:45:51 -07001187 scanned_icds = scanned_icds->next;
1188 }
1189
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001190 free(ptr_instance);
1191
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001192 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001193}
1194
Jon Ashburn83a64252015-04-15 11:31:12 -06001195LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001196 VkInstance instance,
1197 uint32_t* pPhysicalDeviceCount,
1198 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001199{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001200 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1201 struct loader_icd *icd;
Jon Ashburn83a64252015-04-15 11:31:12 -06001202 uint32_t n, count = 0;
Tony Barbourf20f87b2015-04-22 09:02:32 -06001203 VkResult res = VK_ERROR_UNKNOWN;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001204
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001205 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001206 icd = ptr_instance->icds;
Jon Ashburn83a64252015-04-15 11:31:12 -06001207 if (pPhysicalDevices == NULL) {
1208 while (icd) {
1209 res = icd->scanned_icds->EnumeratePhysicalDevices(
1210 icd->scanned_icds->instance,
1211 &n, NULL);
1212 if (res != VK_SUCCESS)
1213 return res;
1214 icd->gpu_count = n;
1215 count += n;
1216 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001217 }
1218
Jon Ashburn83a64252015-04-15 11:31:12 -06001219 ptr_instance->total_gpu_count = count;
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001220
Jon Ashburn83a64252015-04-15 11:31:12 -06001221 } else
1222 {
Tony Barbourd1c35722015-04-16 15:59:00 -06001223 VkPhysicalDevice* gpus;
Jon Ashburn83a64252015-04-15 11:31:12 -06001224 if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1225 return VK_ERROR_INVALID_VALUE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001226 gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount);
Jon Ashburn83a64252015-04-15 11:31:12 -06001227 if (!gpus)
Tony Barbourd1c35722015-04-16 15:59:00 -06001228 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn83a64252015-04-15 11:31:12 -06001229 while (icd) {
1230 VkBaseLayerObject * wrapped_gpus;
1231 PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
1232
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001233 n = *pPhysicalDeviceCount;
Jon Ashburn83a64252015-04-15 11:31:12 -06001234 res = icd->scanned_icds->EnumeratePhysicalDevices(
1235 icd->scanned_icds->instance,
1236 &n,
1237 gpus);
1238 if (res == VK_SUCCESS && n) {
1239 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1240 sizeof(VkBaseLayerObject));
1241 icd->gpus = wrapped_gpus;
1242 icd->gpu_count = n;
1243 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1244 sizeof(VkLayerDispatchTable));
1245 for (unsigned int i = 0; i < n; i++) {
1246 (wrapped_gpus + i)->baseObject = gpus[i];
1247 (wrapped_gpus + i)->pGPA = get_proc_addr;
1248 (wrapped_gpus + i)->nextObject = gpus[i];
1249 memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
Jon Ashburnfbb4e252015-05-04 16:27:53 -06001250 loader_init_device_dispatch_table(icd->loader_dispatch + i,
Jon Ashburn83a64252015-04-15 11:31:12 -06001251 get_proc_addr, gpus[i]);
1252
1253 /* Verify ICD compatibility */
Tony Barbourfc15ff82015-04-20 16:28:46 -06001254 if (!valid_loader_magic_value(gpus[i])) {
Jon Ashburn83a64252015-04-15 11:31:12 -06001255 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001256 "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 -06001257 assert(0);
1258 }
1259
1260 const VkLayerDispatchTable **disp;
1261 disp = (const VkLayerDispatchTable **) gpus[i];
1262 *disp = icd->loader_dispatch + i;
1263 loader_activate_layers(icd, i, ptr_instance->extension_count,
1264 (const char *const*) ptr_instance->extension_names);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001265 }
1266
Jon Ashburn83a64252015-04-15 11:31:12 -06001267 count += n;
1268
1269 if (count >= *pPhysicalDeviceCount) {
1270 break;
1271 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001272 }
1273
Jon Ashburn83a64252015-04-15 11:31:12 -06001274 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001275 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001276 }
1277
Jon Ashburn83a64252015-04-15 11:31:12 -06001278 *pPhysicalDeviceCount = count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001279
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001280 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001281}
1282
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001283LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName)
1284{
1285 if (instance != VK_NULL_HANDLE) {
1286
1287 /* return entrypoint addresses that are global (in the loader)*/
1288 return globalGetProcAddr(pName);
1289 }
1290
1291 return NULL;
1292}
1293
Tony Barbourd1c35722015-04-16 15:59:00 -06001294LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001295{
Mike Stroyanb050c682015-04-17 12:36:38 -06001296 if (gpu == VK_NULL_HANDLE) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001297
1298 /* return entrypoint addresses that are global (in the loader)*/
1299 return globalGetProcAddr(pName);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001300 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001301
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001302 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001303
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001304 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1305 make sure the loader entrypoint is returned */
1306 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001307 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001308 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001309 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001310
1311 /* return the dispatch table entrypoint for the fastest case */
1312 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001313 if (disp_table == NULL)
1314 return NULL;
1315
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001316 addr = loader_lookup_dispatch_table(disp_table, pName);
1317 if (addr)
1318 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001319 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001320 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001321 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001322 return disp_table->GetProcAddr(gpu, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001323 }
1324}
1325
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001326//TODO make sure createInstance enables extensions that are valid (loader does)
1327//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1328
1329//TODO how is layer extension going to be enabled?
1330//Need to call createInstance on the layer or something
1331
1332LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
1333 VkExtensionInfoType infoType,
1334 uint32_t extensionIndex,
1335 size_t* pDataSize,
1336 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001337{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001338 VkExtensionProperties *ext_props;
1339 uint32_t *count;
1340 /* Scan/discover all ICD libraries in a single-threaded manner */
1341 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001342
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001343 /* get layer libraries in a single-threaded manner */
1344 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001345
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001346 /* merge any duplicate extensions */
1347 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1348
1349
1350 if (pDataSize == NULL)
1351 return VK_ERROR_INVALID_POINTER;
1352
1353 switch (infoType) {
1354 case VK_EXTENSION_INFO_TYPE_COUNT:
1355 *pDataSize = sizeof(uint32_t);
1356 if (pData == NULL)
1357 return VK_SUCCESS;
1358 count = (uint32_t *) pData;
1359 *count = loader.scanned_ext_list_count;
1360 break;
1361 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1362 *pDataSize = sizeof(VkExtensionProperties);
1363 if (pData == NULL)
1364 return VK_SUCCESS;
1365 if (extensionIndex >= loader.scanned_ext_list_count)
1366 return VK_ERROR_INVALID_VALUE;
1367 ext_props = (VkExtensionProperties *) pData;
1368 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1369 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1370 , VK_MAX_EXTENSION_NAME);
1371 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1372 break;
1373 default:
1374 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1375 return VK_ERROR_INVALID_VALUE;
1376 };
1377
1378 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001379}
1380
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001381LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001382{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001383 size_t maxLayerCount;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001384 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001385 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001386 char *lib_name;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001387 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001388 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001389 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001390 char layer_buf[16][256];
1391 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001392
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001393 if (pLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001394 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001395
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001396 maxLayerCount = *pLayerCount;
1397
Jon Ashburn46d1f582015-01-28 11:01:35 -07001398 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001399 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001400
Jon Ashburn1323eb72014-12-02 13:03:09 -07001401 for (int i = 0; i < 16; i++)
1402 layers[i] = &layer_buf[i][0];
1403
1404 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001405 lib_name = loader.scanned_layers[j].name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001406 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1407 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001408 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001409 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1410 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001411 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001412 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001413 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001414 lib_name = basename(lib_name);
1415 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001416 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001417 if (pEnd == NULL || siz <= 0)
1418 continue;
1419 cpyStr = malloc(siz);
1420 if (cpyStr == NULL) {
1421 free(cpyStr);
1422 continue;
1423 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001424 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001425 cpyStr[siz - 1] = '\0';
1426 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001427 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001428 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1429 pOutLayers[count][siz - 1] = '\0';
1430 count++;
1431 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001432 } else {
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001433 size_t cnt = 16; /* only allow 16 layers, for now */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001434 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001435 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001436 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001437 res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001438 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001439 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001440 continue;
1441 if (cnt + count > maxLayerCount)
1442 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001443 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001444 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1445 if (n > 0)
1446 pOutLayers[i - count][n - 1] = '\0';
1447 }
1448 count += cnt;
1449 }
1450 }
1451
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001452 *pLayerCount = count;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001453
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001454 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001455}
1456
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001457LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001458{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001459 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001460 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001461 VkResult res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001462 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001463
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001464 if (instance == VK_NULL_HANDLE)
1465 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001466
1467 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001468
Jon Ashburn98bd4542015-01-29 16:44:24 -07001469 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001470 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001471 break;
1472 }
1473
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001474 if (inst == VK_NULL_HANDLE)
1475 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001476
1477 for (icd = inst->icds; icd; icd = icd->next) {
1478 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1479 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001480 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001481 if (res != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001482 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001483 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001484 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001485 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001486 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001487 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001488 }
1489
Jon Ashburnbb510252015-03-17 13:47:55 -06001490
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001491 /* roll back on errors */
1492 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001493 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001494 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001495 for (uint32_t i = 0; i < icd->gpu_count; i++)
1496 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001497 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001498 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001499 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001500 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001501
1502 return res;
1503 }
1504
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001505 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001506}
1507
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001508LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
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;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001514
1515 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) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001518 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001519 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
1525 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1526 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001527 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001528 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001529 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001530 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001531 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001532 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001533 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001534 return res;
1535}
1536
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001537LOADER_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 +08001538{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001539 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001540 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001541 if (instance == VK_NULL_HANDLE)
1542 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001543
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001544 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001545
Jon Ashburnbb510252015-03-17 13:47:55 -06001546 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001547 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001548 break;
1549 }
1550
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001551 if (inst == VK_NULL_HANDLE)
1552 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001553 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1554 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001555 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001556 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001557 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001558 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001559 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001560 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001561 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001562 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001563 }
1564
1565 return res;
1566}