blob: 855d35fa58ff104e5b363ced43c9b4da4030e3e8 [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"
Chia-I Wu19300602014-08-04 08:03:57 +080045#include "loader.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060046#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070047// The following is #included again to catch certain OS-specific functions
48// being used:
49#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080050
Jon Ashburn1beab2d2015-01-26 14:51:40 -070051struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070052 struct loader_icd *icds;
53 struct loader_instance *next;
Jon Ashburn340d6662015-04-08 15:49:00 -060054 uint32_t extension_count;
55 char **extension_names;
Jon Ashburn1beab2d2015-01-26 14:51:40 -070056};
57
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060058struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070059 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070060 char name[256];
61};
62
63struct layer_name_pair {
64 char *layer_name;
65 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060066};
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
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060071 VK_LAYER_DISPATCH_TABLE *loader_dispatch;
72 uint32_t layer_count[VK_MAX_PHYSICAL_GPUS];
73 struct loader_layers layer_libs[VK_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
74 VK_BASE_LAYER_OBJECT *wrappedGpus[VK_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060075 uint32_t gpu_count;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060076 VK_BASE_LAYER_OBJECT *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;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060084 PFN_vkGetProcAddr GetProcAddr;
85 PFN_vkCreateInstance CreateInstance;
86 PFN_vkDestroyInstance DestroyInstance;
87 PFN_vkEnumerateGpus EnumerateGpus;
88 PFN_vkGetExtensionSupport GetExtensionSupport;
89 VkInstance instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070090 struct loader_scanned_icds *next;
91};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060092
Jon Ashburn1beab2d2015-01-26 14:51:40 -070093// Note: Since the following is a static structure, all members are initialized
94// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080095static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070096 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070097 bool icds_scanned;
98 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060099 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700100 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600101 unsigned int scanned_layer_count;
102 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800103} loader;
104
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700105
Ian Elliott4470a302015-02-17 10:33:47 -0700106#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600107char *loader_get_registry_string(const HKEY hive,
108 const LPCTSTR sub_key,
109 const char *value)
110{
111 DWORD access_flags = KEY_QUERY_VALUE;
112 DWORD value_type;
113 HKEY key;
114 LONG rtn_value;
115 char *rtn_str = NULL;
116 size_t rtn_len = 0;
117 size_t allocated_len = 0;
118
119 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
120 if (rtn_value != ERROR_SUCCESS) {
121 // We didn't find the key. Try the 32-bit hive (where we've seen the
122 // key end up on some people's systems):
123 access_flags |= KEY_WOW64_32KEY;
124 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
125 if (rtn_value != ERROR_SUCCESS) {
126 // We still couldn't find the key, so give up:
127 return NULL;
128 }
129 }
130
131 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
132 (PVOID) rtn_str, &rtn_len);
133 if (rtn_value == ERROR_SUCCESS) {
134 // If we get to here, we found the key, and need to allocate memory
135 // large enough for rtn_str, and query again:
136 allocated_len = rtn_len + 4;
137 rtn_str = malloc(allocated_len);
138 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
139 (PVOID) rtn_str, &rtn_len);
140 if (rtn_value == ERROR_SUCCESS) {
141 // We added 4 extra bytes to rtn_str, so that we can ensure that
142 // the string is NULL-terminated (albeit, in a brute-force manner):
143 rtn_str[allocated_len-1] = '\0';
144 } else {
145 // This should never occur, but in case it does, clean up:
146 free(rtn_str);
147 rtn_str = NULL;
148 }
149 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
150
151 // Close the registry key that was opened:
152 RegCloseKey(key);
153
154 return rtn_str;
155}
156
157
Ian Elliott4470a302015-02-17 10:33:47 -0700158// For ICD developers, look in the registry, and look for an environment
159// variable for a path(s) where to find the ICD(s):
160static char *loader_get_registry_and_env(const char *env_var,
161 const char *registry_value)
162{
163 char *env_str = getenv(env_var);
164 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600165 char *registry_str = NULL;
166 DWORD registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700167 char *rtn_str = NULL;
168 size_t rtn_len;
169
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600170 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600171 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600172 registry_value);
Ian Elliott2de26bc2015-04-03 13:13:01 -0600173 registry_len = (registry_str) ? strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700174
175 rtn_len = env_len + registry_len + 1;
176 if (rtn_len <= 2) {
177 // We found neither the desired registry value, nor the environment
178 // variable; return NULL:
179 return NULL;
180 } else {
181 // We found something, and so we need to allocate memory for the string
182 // to return:
183 rtn_str = malloc(rtn_len);
184 }
185
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600186 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700187 // We didn't find the desired registry value, and so we must have found
188 // only the environment variable:
189 _snprintf(rtn_str, rtn_len, "%s", env_str);
190 } else if (env_str != NULL) {
191 // We found both the desired registry value and the environment
192 // variable, so concatenate them both:
193 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
194 } else {
195 // We must have only found the desired registry value:
196 _snprintf(rtn_str, rtn_len, "%s", registry_str);
197 }
198
Ian Elliott2de26bc2015-04-03 13:13:01 -0600199 if (registry_str) {
200 free(registry_str);
201 }
Ian Elliott4470a302015-02-17 10:33:47 -0700202
203 return(rtn_str);
204}
205#endif // WIN32
206
207
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600208static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800209 const char *format, ...)
210{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800211 char msg[256];
212 va_list ap;
213 int ret;
214
215 va_start(ap, format);
216 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700217 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800218 msg[sizeof(msg) - 1] = '\0';
219 }
220 va_end(ap);
221
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600222 fputs(msg, stderr);
223 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800224}
225
226static void
227loader_icd_destroy(struct loader_icd *icd)
228{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700229 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800230 free(icd);
231}
232
233static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700234loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800235{
236 struct loader_icd *icd;
237
238 icd = malloc(sizeof(*icd));
239 if (!icd)
240 return NULL;
241
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600242 memset(icd, 0, sizeof(*icd));
243
Jon Ashburn46d1f582015-01-28 11:01:35 -0700244 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800245
246 return icd;
247}
248
Jon Ashburn46888392015-01-29 15:45:51 -0700249static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
250 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800251{
252 struct loader_icd *icd;
253
Jon Ashburn46d1f582015-01-28 11:01:35 -0700254 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800255 if (!icd)
256 return NULL;
257
Chia-I Wu13a61a52014-08-04 11:18:20 +0800258 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700259 icd->next = ptr_inst->icds;
260 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800261
262 return icd;
263}
264
Jon Ashburn46d1f582015-01-28 11:01:35 -0700265static void loader_scanned_icd_add(const char *filename)
266{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700267 loader_platform_dl_handle handle;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700268 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst, *fp_get_extension_support;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700269 struct loader_scanned_icds *new_node;
270
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700271 // Used to call: dlopen(filename, RTLD_LAZY);
272 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700273 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600274 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700275 return;
276 }
277
278#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600279 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700280 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600281 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700282 return; \
283 } \
284} while (0)
285
286 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700287 LOOKUP(fp_create_inst, CreateInstance);
288 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700289 LOOKUP(fp_enumerate, EnumerateGpus);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700290 LOOKUP(fp_get_extension_support, GetExtensionSupport);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700291#undef LOOKUP
292
293 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
294 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600295 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700296 return;
297 }
298
299 new_node->handle = handle;
300 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700301 new_node->CreateInstance = fp_create_inst;
302 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700303 new_node->EnumerateGpus = fp_enumerate;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700304 new_node->GetExtensionSupport = fp_get_extension_support;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700305 new_node->next = loader.scanned_icd_list;
306 loader.scanned_icd_list = new_node;
307}
308
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700309
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600310/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600311 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600312 *
313 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600314 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
315 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600316 *
317 * \returns
318 * void; but side effect is to set loader_icd_scanned to true
319 */
320static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800321{
Ian Elliott4470a302015-02-17 10:33:47 -0700322 const char *p, *next;
323 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600324 DIR *sysdir;
325 struct dirent *dent;
326 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600327 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700328 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700329#if defined(WIN32)
330 bool must_free_libPaths;
331 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
332 DRIVER_PATH_REGISTRY_VALUE);
333 if (libPaths != NULL) {
334 must_free_libPaths = true;
335 } else {
336 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600337 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700338 }
339#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600340 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700341 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
342 libPaths = getenv(DRIVER_PATH_ENV);
343 }
344 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600345 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600346 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700347#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800348
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600349 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700350 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600351 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700352 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600353 next = p + len;
354 }
355 else {
Ian Elliott19628802015-02-04 12:06:46 -0700356 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600357 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
358 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600359 next++;
360 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800361
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700362 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
363 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600364 sysdir = opendir(p);
365 if (sysdir) {
366 dent = readdir(sysdir);
367 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600368 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
369 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700370 */
371 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600372 VK_DRIVER_LIBRARY_PREFIX,
373 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700374 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600375 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
376 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700377 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600378 VK_LIBRARY_SUFFIX,
379 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700380 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
381 loader_scanned_icd_add(icd_library);
382 }
383 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600384
385 dent = readdir(sysdir);
386 }
387 closedir(sysdir);
388 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800389 }
390
Ian Elliott4470a302015-02-17 10:33:47 -0700391#if defined(WIN32)
392 // Free any allocated memory:
393 if (must_free_libPaths) {
394 free(libPaths);
395 }
396#endif // WIN32
397
398 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700399 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800400}
401
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600402
Ian Elliott4470a302015-02-17 10:33:47 -0700403static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600404{
405 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700406 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600407 DIR *curdir;
408 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700409 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600410 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600411
Ian Elliott4470a302015-02-17 10:33:47 -0700412#if defined(WIN32)
413 bool must_free_libPaths;
414 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
415 LAYERS_PATH_REGISTRY_VALUE);
416 if (libPaths != NULL) {
417 must_free_libPaths = true;
418 } else {
419 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600420 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600421 }
Ian Elliott4470a302015-02-17 10:33:47 -0700422#else // WIN32
423 if (geteuid() == getuid()) {
424 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700425 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700426 }
427 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600428 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700429 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700430#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600431
Ian Elliott4470a302015-02-17 10:33:47 -0700432 if (libPaths == NULL) {
433 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700434 return;
435 }
Ian Elliott4470a302015-02-17 10:33:47 -0700436 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700437 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700438 if (loader.layer_dirs == NULL) {
439 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700440 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700441 }
442 // Alloc passed, so we know there is enough space to hold the string, don't
443 // need strncpy
444 strcpy(loader.layer_dirs, libPaths);
445#if defined(WIN32)
446 // Free any allocated memory:
447 if (must_free_libPaths) {
448 free(libPaths);
449 must_free_libPaths = false;
450 }
451#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700452 libPaths = loader.layer_dirs;
453
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600454 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600455 for (i = 0; i < loader.scanned_layer_count; i++) {
456 if (loader.scanned_layer_names[i] != NULL)
457 free(loader.scanned_layer_names[i]);
458 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600459 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600460 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600461
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600462 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700463 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600464 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700465 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600466 next = p + len;
467 }
468 else {
Ian Elliott19628802015-02-04 12:06:46 -0700469 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600470 *(char *) next = '\0';
471 next++;
472 }
473
474 curdir = opendir(p);
475 if (curdir) {
476 dent = readdir(curdir);
477 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600478 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
479 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700480 */
481 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600482 VK_LAYER_LIBRARY_PREFIX,
483 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700484 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600485 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
486 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700487 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600488 VK_LIBRARY_SUFFIX,
489 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700490 loader_platform_dl_handle handle;
491 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
492 // Used to call: dlopen(temp_str, RTLD_LAZY)
493 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
494 dent = readdir(curdir);
495 continue;
496 }
497 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600498 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700499 break;
500 }
501 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600502 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700503 break;
504 }
505 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
506 loader.scanned_layer_count++;
507 loader_platform_close_library(handle);
508 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600509 }
510
511 dent = readdir(curdir);
512 }
513 closedir(curdir);
514 }
515 }
516
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600517 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600518}
519
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600520static void loader_init_dispatch_table(VK_LAYER_DISPATCH_TABLE *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalGpu gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600521{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800522 loader_initialize_dispatch_table(tab, fpGPA, gpu);
523
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600524 if (tab->EnumerateLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600525 tab->EnumerateLayers = vkEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600526}
527
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600528static struct loader_icd * loader_get_icd(const VK_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600529{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700530 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
531 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
532 for (uint32_t i = 0; i < icd->gpu_count; i++)
533 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
534 gpu->baseObject) {
535 *gpu_index = i;
536 return icd;
537 }
538 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600539 }
540 return NULL;
541}
542
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600543static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600544{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600545 if (icd->layer_count[gpu_index])
546 return true;
547 else
548 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600549}
550
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600551static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair * pLayerNames, uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600552{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600553 if (!icd)
554 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600555
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600556 struct loader_layers *obj;
557 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600558 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600559 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600560 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700561 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600562 foundLib = true;
563 break;
564 }
565 }
566 if (!foundLib) {
567 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700568 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
569 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700570 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
571 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600572 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600573 continue;
574 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600575 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600576 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700577 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600578 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600579 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600580 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600581}
582
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600583static VkResult find_layer_extension(struct loader_icd *icd, uint32_t gpu_index, const char *pExtName, const char **lib_name)
Jon Ashburnb8358052014-11-18 09:06:04 -0700584{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600585 VkResult err;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700586 char *search_name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700587 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600588 PFN_vkGetExtensionSupport fpGetExtensionSupport;
Jon Ashburnb8358052014-11-18 09:06:04 -0700589
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700590 /*
591 * The loader provides the abstraction that make layers and extensions work via
592 * the currently defined extension mechanism. That is, when app queries for an extension
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600593 * via vkGetExtensionSupport, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700594 * to see who implements that extension. Then, if the app enables the extension during
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600595 * vkCreateDevice the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700596 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700597
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700598 // TODO: What if extension is in multiple places?
599
600 // TODO: Who should we ask first? Driver or layers? Do driver for now.
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600601 err = icd->scanned_icds[gpu_index].GetExtensionSupport((VkPhysicalGpu) (icd->gpus[gpu_index].nextObject), pExtName);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600602 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700603 if (lib_name) {
604 *lib_name = NULL;
Jon Ashburnb8358052014-11-18 09:06:04 -0700605 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600606 return VK_SUCCESS;
Jon Ashburnb8358052014-11-18 09:06:04 -0700607 }
608
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700609 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
610 search_name = loader.scanned_layer_names[j];
611
612 if ((handle = loader_platform_open_library(search_name)) == NULL)
613 continue;
614
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600615 fpGetExtensionSupport = loader_platform_get_proc_address(handle, "vkGetExtensionSupport");
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700616
617 if (fpGetExtensionSupport != NULL) {
618 // Found layer's GetExtensionSupport call
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600619 err = fpGetExtensionSupport((VkPhysicalGpu) (icd->gpus + gpu_index), pExtName);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700620
621 loader_platform_close_library(handle);
622
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600623 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700624 if (lib_name) {
625 *lib_name = loader.scanned_layer_names[j];
626 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600627 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700628 }
629 } else {
630 loader_platform_close_library(handle);
631 }
632
633 // No GetExtensionSupport or GetExtensionSupport returned invalid extension
634 // for the layer, so test the layer name as if it is an extension name
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600635 // use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700636 char *pEnd;
637 size_t siz;
638
639 search_name = basename(search_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600640 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700641 pEnd = strrchr(search_name, '.');
642 siz = (int) (pEnd - search_name);
643 if (siz != strlen(pExtName))
644 continue;
645
646 if (strncmp(search_name, pExtName, siz) == 0) {
647 if (lib_name) {
648 *lib_name = loader.scanned_layer_names[j];
649 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600650 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700651 }
652 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600653 return VK_ERROR_INVALID_EXTENSION;
Jon Ashburnb8358052014-11-18 09:06:04 -0700654}
655
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600656static 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 -0600657{
Ian Elliott4470a302015-02-17 10:33:47 -0700658 char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600659 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600660 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600661
Ian Elliott4470a302015-02-17 10:33:47 -0700662#if defined(WIN32)
663 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
664 LAYER_NAMES_REGISTRY_VALUE);
665#else // WIN32
666 layerEnv = getenv(LAYER_NAMES_ENV);
667#endif // WIN32
668 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600669 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700670 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600671 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700672 if (p == NULL) {
673#if defined(WIN32)
674 free(layerEnv);
675#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600676 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700677 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600678 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700679#if defined(WIN32)
680 free(layerEnv);
681#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600682 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600683
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600684 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700685 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700686 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600687 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700688 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600689 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700690 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700691 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600692 *(char *) next = '\0';
693 next++;
694 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600695 name = basename(p);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600696 if (find_layer_extension(icd, gpu_index, name, &lib_name) != VK_SUCCESS) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600697 p = next;
698 continue;
699 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600700
Ian Elliott19628802015-02-04 12:06:46 -0700701 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700702 pLayerNames[count].layer_name = malloc(len + 1);
703 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600704 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600705 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600706 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700707 strncpy((char *) pLayerNames[count].layer_name, name, len);
708 pLayerNames[count].layer_name[len] = '\0';
709 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600710 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600711 p = next;
712
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700713 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600714
Jon Ashburnd09bd102014-10-22 21:15:26 -0600715 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600716 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600717}
718
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600719static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, const VkDeviceCreateInfo* pCreateInfo, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600720{
Jon Ashburnb8358052014-11-18 09:06:04 -0700721 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700722 const char *lib_name = NULL;
723 uint32_t count = 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600724
725 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700726 /* Load any layers specified in the environment first */
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700727 count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600728
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700729 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
730 const char *pExtName = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600731
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600732 if (find_layer_extension(icd, gpu_index, pExtName, &lib_name) == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700733 uint32_t len;
734
735 /*
736 * the library name is NULL if the driver supports this
737 * extension and thus no layer to load.
738 */
739 if (lib_name == NULL)
740 continue;
741
742 len = (uint32_t) strlen(pExtName);
743 for (uint32_t j = 0; j < count; j++) {
744 if (len == strlen(layerNames[j].layer_name) &&
745 strncmp(pExtName, layerNames[j].layer_name, len) == 0) {
746 // Extension / Layer already on the list
747 continue;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700748 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600749 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700750
751 layerNames[count].layer_name = malloc(len + 1);
752 if (!layerNames[count].layer_name)
753 return count;
754 strncpy((char *) layerNames[count].layer_name, pExtName, len);
755 layerNames[count].layer_name[len] = '\0';
756 layerNames[count].lib_name = lib_name;
757 count++;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600758 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600759 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700760
761 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600762}
763
Jon Ashburn46d1f582015-01-28 11:01:35 -0700764static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600765{
766 struct loader_icd *icd;
767 struct loader_layers *libs;
768
Jon Ashburn46d1f582015-01-28 11:01:35 -0700769 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600770 if (icd->gpus)
771 free(icd->gpus);
772 icd->gpus = NULL;
773 if (icd->loader_dispatch)
774 free(icd->loader_dispatch);
775 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600776 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600777 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600778 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600779 libs = &(icd->layer_libs[j][i]);
780 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700781 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600782 libs->lib_handle = NULL;
783 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600784 if (icd->wrappedGpus[j])
785 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600786 }
787 icd->layer_count[j] = 0;
788 }
789 icd->gpu_count = 0;
790 }
791}
792
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600793extern uint32_t loader_activate_layers(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600794{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600795 uint32_t gpu_index;
796 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700797 struct layer_name_pair *pLayerNames;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600798 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600799
800 if (!icd)
801 return 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600802 assert(gpu_index < VK_MAX_PHYSICAL_GPUS);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600803
804 /* activate any layer libraries */
805 if (!loader_layers_activated(icd, gpu_index)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600806 VK_BASE_LAYER_OBJECT *gpuObj = (VK_BASE_LAYER_OBJECT *) gpu;
807 VK_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600808 PFN_vkGetProcAddr nextGPA = vkGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600809
Jon Ashburnb8358052014-11-18 09:06:04 -0700810 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600811 if (!count)
812 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700813 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600814
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 icd->wrappedGpus[gpu_index] = malloc(sizeof(VK_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600816 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600817 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600818 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600819 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600820 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600821 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600822 nextGpuObj->nextObject = gpuObj;
823 gpuObj = nextGpuObj;
824
Jon Ashburn79113cc2014-12-01 14:22:40 -0700825 char funcStr[256];
826 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600827 if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
828 nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600829 if (!nextGPA) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600830 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 -0600831 continue;
832 }
833
Jon Ashburnf2610012014-10-24 15:48:55 -0600834 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600835 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600836 //Insert the new wrapped objects into the list with loader object at head
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600837 ((VK_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
838 ((VK_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600839 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
840 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700841 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600842 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600843
844 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600845 }
846 else {
847 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700848 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600849 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700850 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600851 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600852 break;
853 }
854 }
855 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600856 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600857 }
858 }
859 return icd->layer_count[gpu_index];
860}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600861
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600862LOADER_EXPORT VkResult VKAPI vkCreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600863 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600864 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700865{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700866 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
867 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700868 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700869 struct loader_scanned_icds *scanned_icds;
870 struct loader_icd *icd;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600871 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn340d6662015-04-08 15:49:00 -0600872 uint32_t i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600873
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700874 /* Scan/discover all ICD libraries in a single-threaded manner */
875 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700876
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700877 /* get layer libraries in a single-threaded manner */
878 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700879
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700880 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
881 if (ptr_instance == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600882 return VK_ERROR_OUT_OF_MEMORY;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700883 }
884 memset(ptr_instance, 0, sizeof(struct loader_instance));
Jon Ashburn340d6662015-04-08 15:49:00 -0600885 ptr_instance->extension_count = pCreateInfo->extensionCount;
886 ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
887 malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
888 for (i = 0; i < ptr_instance->extension_count; i++) {
889 ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i] + 1));
890 if (ptr_instance->extension_names[i] == NULL)
891 return VK_ERROR_OUT_OF_MEMORY;
892 strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
893 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700894 ptr_instance->next = loader.instances;
895 loader.instances = ptr_instance;
896
Jon Ashburn46888392015-01-29 15:45:51 -0700897 scanned_icds = loader.scanned_icd_list;
898 while (scanned_icds) {
899 icd = loader_icd_add(ptr_instance, scanned_icds);
900 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -0600901 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn46888392015-01-29 15:45:51 -0700902 &(scanned_icds->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600903 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -0700904 {
905 ptr_instance->icds = ptr_instance->icds->next;
906 loader_icd_destroy(icd);
907 scanned_icds->instance = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600908 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -0700909 "ICD ignored: failed to CreateInstance on device");
910 }
911 }
912 scanned_icds = scanned_icds->next;
913 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700914
Ian Elliotteb450762015-02-05 15:19:15 -0700915 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600916 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -0700917 }
Jon Ashburn46888392015-01-29 15:45:51 -0700918
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600919 *pInstance = (VkInstance) ptr_instance;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600920 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700921}
922
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600923LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
924 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700925{
926 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700927 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600928 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -0600929 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700930
931 // Remove this instance from the list of instances:
932 struct loader_instance *prev = NULL;
933 struct loader_instance *next = loader.instances;
934 while (next != NULL) {
935 if (next == ptr_instance) {
936 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -0600937 for (i = 0; i < ptr_instance->extension_count; i++) {
938 free(ptr_instance->extension_names[i]);
939 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700940 if (prev)
941 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700942 else
943 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700944 break;
945 }
946 prev = next;
947 next = next->next;
948 }
949 if (next == NULL) {
950 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600951 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700952 }
953
Jon Ashburn46888392015-01-29 15:45:51 -0700954 // cleanup any prior layer initializations
955 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700956
Jon Ashburn46888392015-01-29 15:45:51 -0700957 scanned_icds = loader.scanned_icd_list;
958 while (scanned_icds) {
959 if (scanned_icds->instance)
960 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600961 if (res != VK_SUCCESS)
962 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -0700963 "ICD ignored: failed to DestroyInstance on device");
964 scanned_icds->instance = NULL;
965 scanned_icds = scanned_icds->next;
966 }
967
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700968 free(ptr_instance);
969
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600970 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700971}
972
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600973LOADER_EXPORT VkResult VKAPI vkEnumerateGpus(
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700974
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600975 VkInstance instance,
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700976 uint32_t maxGpus,
977 uint32_t* pGpuCount,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600978 VkPhysicalGpu* pGpus)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700979{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700980 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
981 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700982 uint32_t count = 0;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600983 VkResult res;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700984
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600985 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700986 icd = ptr_instance->icds;
987 while (icd) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600988 VkPhysicalGpu gpus[VK_MAX_PHYSICAL_GPUS];
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600989 VK_BASE_LAYER_OBJECT * wrapped_gpus;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600990 PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700991 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700992
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600993 if (max > VK_MAX_PHYSICAL_GPUS) {
994 max = VK_MAX_PHYSICAL_GPUS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700995 }
996
Jon Ashburn46888392015-01-29 15:45:51 -0700997 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
998 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700999 gpus);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001000 if (res == VK_SUCCESS && n) {
1001 wrapped_gpus = (VK_BASE_LAYER_OBJECT*) malloc(n *
1002 sizeof(VK_BASE_LAYER_OBJECT));
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001003 icd->gpus = wrapped_gpus;
1004 icd->gpu_count = n;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001005 icd->loader_dispatch = (VK_LAYER_DISPATCH_TABLE *) malloc(n *
1006 sizeof(VK_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -07001007 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001008 (wrapped_gpus + i)->baseObject = gpus[i];
1009 (wrapped_gpus + i)->pGPA = get_proc_addr;
1010 (wrapped_gpus + i)->nextObject = gpus[i];
1011 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
1012 loader_init_dispatch_table(icd->loader_dispatch + i,
1013 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001014
1015 /* Verify ICD compatibility */
1016 if (!valid_loader_magic_value(gpus[i])) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001017 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001018 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
1019 assert(0);
1020 }
1021
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001022 const VK_LAYER_DISPATCH_TABLE **disp;
1023 disp = (const VK_LAYER_DISPATCH_TABLE **) gpus[i];
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001024 *disp = icd->loader_dispatch + i;
1025 }
1026
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001027 count += n;
1028
1029 if (count >= maxGpus) {
1030 break;
1031 }
1032 }
1033
1034 icd = icd->next;
1035 }
1036
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001037 *pGpuCount = count;
1038
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001039 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001040}
1041
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001042LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001043{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001044 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001045 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001046 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001047 VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu;
1048 VK_LAYER_DISPATCH_TABLE * disp_table = * (VK_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001049 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001050
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001051 if (disp_table == NULL)
1052 return NULL;
1053
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001054 addr = loader_lookup_dispatch_table(disp_table, pName);
1055 if (addr)
1056 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001057 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001058 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001059 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001060 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001061 }
1062}
1063
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001064LOADER_EXPORT VkResult VKAPI vkGetExtensionSupport(VkPhysicalGpu gpu, const char *pExtName)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001065{
1066 uint32_t gpu_index;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001067 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001068
1069 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001070 return VK_ERROR_UNAVAILABLE;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001071
1072 return find_layer_extension(icd, gpu_index, pExtName, NULL);
1073}
1074
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001075LOADER_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 -06001076{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001077 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001078 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001079 char *lib_name;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001080 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001081 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001082 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001083 char layer_buf[16][256];
1084 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001085
Jon Ashburn1323eb72014-12-02 13:03:09 -07001086 if (pOutLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001087 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001088
Jon Ashburn46d1f582015-01-28 11:01:35 -07001089 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001090 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001091
Jon Ashburn1323eb72014-12-02 13:03:09 -07001092 for (int i = 0; i < 16; i++)
1093 layers[i] = &layer_buf[i][0];
1094
1095 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1096 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001097 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1098 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001099 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001100 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1101 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001102 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001103 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001104 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001105 lib_name = basename(lib_name);
1106 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001107 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001108 if (pEnd == NULL || siz <= 0)
1109 continue;
1110 cpyStr = malloc(siz);
1111 if (cpyStr == NULL) {
1112 free(cpyStr);
1113 continue;
1114 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001115 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001116 cpyStr[siz - 1] = '\0';
1117 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001118 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001119 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1120 pOutLayers[count][siz - 1] = '\0';
1121 count++;
1122 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001123 } else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001124 size_t cnt;
1125 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001126 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001127 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001128 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1129 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001130 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001131 continue;
1132 if (cnt + count > maxLayerCount)
1133 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001134 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001135 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1136 if (n > 0)
1137 pOutLayers[i - count][n - 1] = '\0';
1138 }
1139 count += cnt;
1140 }
1141 }
1142
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001143 *pOutLayerCount = count;
1144
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001145 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001146}
1147
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001148LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001149{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001150 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001151 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001152 VkResult res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001153 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001154
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001155 if (instance == VK_NULL_HANDLE)
1156 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001157
1158 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001159
Jon Ashburn98bd4542015-01-29 16:44:24 -07001160 for (inst = loader.instances; inst; inst = inst->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001161 if (inst == instance)
1162 break;
1163 }
1164
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001165 if (inst == VK_NULL_HANDLE)
1166 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001167
1168 for (icd = inst->icds; icd; icd = icd->next) {
1169 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1170 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001171 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001172 if (res != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001173 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001174 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001175 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001176 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001177 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001178 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001179 }
1180
Jon Ashburnbb510252015-03-17 13:47:55 -06001181
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001182 /* roll back on errors */
1183 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001184 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001185 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001186 for (uint32_t i = 0; i < icd->gpu_count; i++)
1187 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001188 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001189 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001190 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001191 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001192
1193 return res;
1194 }
1195
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001196 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001197}
1198
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001199LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001200{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001201 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001202 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001203 if (instance == VK_NULL_HANDLE)
1204 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001205
1206 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001207
Jon Ashburnbb510252015-03-17 13:47:55 -06001208 for (inst = loader.instances; inst; inst = inst->next) {
1209 if (inst == instance)
1210 break;
1211 }
1212
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213 if (inst == VK_NULL_HANDLE)
1214 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001215
1216 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1217 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001218 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001219 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001220 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001221 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001222 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001223 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001224 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001225 return res;
1226}
1227
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001228LOADER_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 +08001229{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001230 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001231 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001232 if (instance == VK_NULL_HANDLE)
1233 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001234
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001235 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001236
Jon Ashburnbb510252015-03-17 13:47:55 -06001237 for (inst = loader.instances; inst; inst = inst->next) {
1238 if (inst == instance)
1239 break;
1240 }
1241
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001242 if (inst == VK_NULL_HANDLE)
1243 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001244 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1245 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001246 VkResult r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001247 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001248 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001249 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001251 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001252 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001253 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001254 }
1255
1256 return res;
1257}