blob: 60c5f2195f03273bdf8bb39560625533374009f1 [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;
54};
55
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060056struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070057 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070058 char name[256];
59};
60
61struct layer_name_pair {
62 char *layer_name;
63 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060064};
65
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080066struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070067 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060069 VK_LAYER_DISPATCH_TABLE *loader_dispatch;
70 uint32_t layer_count[VK_MAX_PHYSICAL_GPUS];
71 struct loader_layers layer_libs[VK_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
72 VK_BASE_LAYER_OBJECT *wrappedGpus[VK_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060073 uint32_t gpu_count;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060074 VK_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060075
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080076 struct loader_icd *next;
77};
78
Jon Ashburnd38bfb12014-10-14 19:15:22 -060079
Jon Ashburn46d1f582015-01-28 11:01:35 -070080struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070081 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060082 vkGetProcAddrType GetProcAddr;
83 vkCreateInstanceType CreateInstance;
84 vkDestroyInstanceType DestroyInstance;
85 vkEnumerateGpusType EnumerateGpus;
86 vkGetExtensionSupportType GetExtensionSupport;
87 VK_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070088 struct loader_scanned_icds *next;
89};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060090
Jon Ashburn1beab2d2015-01-26 14:51:40 -070091// Note: Since the following is a static structure, all members are initialized
92// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080093static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070094 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070095 bool icds_scanned;
96 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060097 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -070098 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060099 unsigned int scanned_layer_count;
100 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800101} loader;
102
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700103
Ian Elliott4470a302015-02-17 10:33:47 -0700104#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600105char *loader_get_registry_string(const HKEY hive,
106 const LPCTSTR sub_key,
107 const char *value)
108{
109 DWORD access_flags = KEY_QUERY_VALUE;
110 DWORD value_type;
111 HKEY key;
112 LONG rtn_value;
113 char *rtn_str = NULL;
114 size_t rtn_len = 0;
115 size_t allocated_len = 0;
116
117 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
118 if (rtn_value != ERROR_SUCCESS) {
119 // We didn't find the key. Try the 32-bit hive (where we've seen the
120 // key end up on some people's systems):
121 access_flags |= KEY_WOW64_32KEY;
122 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
123 if (rtn_value != ERROR_SUCCESS) {
124 // We still couldn't find the key, so give up:
125 return NULL;
126 }
127 }
128
129 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
130 (PVOID) rtn_str, &rtn_len);
131 if (rtn_value == ERROR_SUCCESS) {
132 // If we get to here, we found the key, and need to allocate memory
133 // large enough for rtn_str, and query again:
134 allocated_len = rtn_len + 4;
135 rtn_str = malloc(allocated_len);
136 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
137 (PVOID) rtn_str, &rtn_len);
138 if (rtn_value == ERROR_SUCCESS) {
139 // We added 4 extra bytes to rtn_str, so that we can ensure that
140 // the string is NULL-terminated (albeit, in a brute-force manner):
141 rtn_str[allocated_len-1] = '\0';
142 } else {
143 // This should never occur, but in case it does, clean up:
144 free(rtn_str);
145 rtn_str = NULL;
146 }
147 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
148
149 // Close the registry key that was opened:
150 RegCloseKey(key);
151
152 return rtn_str;
153}
154
155
Ian Elliott4470a302015-02-17 10:33:47 -0700156// For ICD developers, look in the registry, and look for an environment
157// variable for a path(s) where to find the ICD(s):
158static char *loader_get_registry_and_env(const char *env_var,
159 const char *registry_value)
160{
161 char *env_str = getenv(env_var);
162 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600163 char *registry_str = NULL;
164 DWORD registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700165 char *rtn_str = NULL;
166 size_t rtn_len;
167
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600168 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600169 "Software\\VK",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600170 registry_value);
Ian Elliott2de26bc2015-04-03 13:13:01 -0600171 registry_len = (registry_str) ? strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700172
173 rtn_len = env_len + registry_len + 1;
174 if (rtn_len <= 2) {
175 // We found neither the desired registry value, nor the environment
176 // variable; return NULL:
177 return NULL;
178 } else {
179 // We found something, and so we need to allocate memory for the string
180 // to return:
181 rtn_str = malloc(rtn_len);
182 }
183
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600184 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700185 // We didn't find the desired registry value, and so we must have found
186 // only the environment variable:
187 _snprintf(rtn_str, rtn_len, "%s", env_str);
188 } else if (env_str != NULL) {
189 // We found both the desired registry value and the environment
190 // variable, so concatenate them both:
191 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
192 } else {
193 // We must have only found the desired registry value:
194 _snprintf(rtn_str, rtn_len, "%s", registry_str);
195 }
196
Ian Elliott2de26bc2015-04-03 13:13:01 -0600197 if (registry_str) {
198 free(registry_str);
199 }
Ian Elliott4470a302015-02-17 10:33:47 -0700200
201 return(rtn_str);
202}
203#endif // WIN32
204
205
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600206static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800207 const char *format, ...)
208{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800209 char msg[256];
210 va_list ap;
211 int ret;
212
213 va_start(ap, format);
214 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700215 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216 msg[sizeof(msg) - 1] = '\0';
217 }
218 va_end(ap);
219
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600220 fputs(msg, stderr);
221 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800222}
223
224static void
225loader_icd_destroy(struct loader_icd *icd)
226{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700227 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800228 free(icd);
229}
230
231static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700232loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800233{
234 struct loader_icd *icd;
235
236 icd = malloc(sizeof(*icd));
237 if (!icd)
238 return NULL;
239
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600240 memset(icd, 0, sizeof(*icd));
241
Jon Ashburn46d1f582015-01-28 11:01:35 -0700242 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800243
244 return icd;
245}
246
Jon Ashburn46888392015-01-29 15:45:51 -0700247static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
248 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800249{
250 struct loader_icd *icd;
251
Jon Ashburn46d1f582015-01-28 11:01:35 -0700252 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800253 if (!icd)
254 return NULL;
255
Chia-I Wu13a61a52014-08-04 11:18:20 +0800256 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700257 icd->next = ptr_inst->icds;
258 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800259
260 return icd;
261}
262
Jon Ashburn46d1f582015-01-28 11:01:35 -0700263static void loader_scanned_icd_add(const char *filename)
264{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700265 loader_platform_dl_handle handle;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700266 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst, *fp_get_extension_support;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700267 struct loader_scanned_icds *new_node;
268
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700269 // Used to call: dlopen(filename, RTLD_LAZY);
270 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700271 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600272 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700273 return;
274 }
275
276#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600277 func_ptr = (vk ##func## Type) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700278 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600279 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700280 return; \
281 } \
282} while (0)
283
284 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700285 LOOKUP(fp_create_inst, CreateInstance);
286 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700287 LOOKUP(fp_enumerate, EnumerateGpus);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700288 LOOKUP(fp_get_extension_support, GetExtensionSupport);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700289#undef LOOKUP
290
291 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
292 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600293 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700294 return;
295 }
296
297 new_node->handle = handle;
298 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700299 new_node->CreateInstance = fp_create_inst;
300 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700301 new_node->EnumerateGpus = fp_enumerate;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700302 new_node->GetExtensionSupport = fp_get_extension_support;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700303 new_node->next = loader.scanned_icd_list;
304 loader.scanned_icd_list = new_node;
305}
306
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700307
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600308/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600309 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600310 *
311 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600312 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
313 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600314 *
315 * \returns
316 * void; but side effect is to set loader_icd_scanned to true
317 */
318static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800319{
Ian Elliott4470a302015-02-17 10:33:47 -0700320 const char *p, *next;
321 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600322 DIR *sysdir;
323 struct dirent *dent;
324 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600325 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700326 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700327#if defined(WIN32)
328 bool must_free_libPaths;
329 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
330 DRIVER_PATH_REGISTRY_VALUE);
331 if (libPaths != NULL) {
332 must_free_libPaths = true;
333 } else {
334 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600335 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700336 }
337#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600338 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700339 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
340 libPaths = getenv(DRIVER_PATH_ENV);
341 }
342 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600343 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600344 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700345#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800346
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600347 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700348 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600349 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700350 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600351 next = p + len;
352 }
353 else {
Ian Elliott19628802015-02-04 12:06:46 -0700354 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600355 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
356 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600357 next++;
358 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800359
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700360 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
361 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600362 sysdir = opendir(p);
363 if (sysdir) {
364 dent = readdir(sysdir);
365 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600366 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
367 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700368 */
369 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370 VK_DRIVER_LIBRARY_PREFIX,
371 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700372 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600373 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
374 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700375 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600376 VK_LIBRARY_SUFFIX,
377 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700378 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
379 loader_scanned_icd_add(icd_library);
380 }
381 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600382
383 dent = readdir(sysdir);
384 }
385 closedir(sysdir);
386 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800387 }
388
Ian Elliott4470a302015-02-17 10:33:47 -0700389#if defined(WIN32)
390 // Free any allocated memory:
391 if (must_free_libPaths) {
392 free(libPaths);
393 }
394#endif // WIN32
395
396 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700397 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800398}
399
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600400
Ian Elliott4470a302015-02-17 10:33:47 -0700401static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600402{
403 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700404 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600405 DIR *curdir;
406 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700407 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600408 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600409
Ian Elliott4470a302015-02-17 10:33:47 -0700410#if defined(WIN32)
411 bool must_free_libPaths;
412 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
413 LAYERS_PATH_REGISTRY_VALUE);
414 if (libPaths != NULL) {
415 must_free_libPaths = true;
416 } else {
417 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600418 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600419 }
Ian Elliott4470a302015-02-17 10:33:47 -0700420#else // WIN32
421 if (geteuid() == getuid()) {
422 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700423 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700424 }
425 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600426 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700427 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700428#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600429
Ian Elliott4470a302015-02-17 10:33:47 -0700430 if (libPaths == NULL) {
431 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700432 return;
433 }
Ian Elliott4470a302015-02-17 10:33:47 -0700434 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700435 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700436 if (loader.layer_dirs == NULL) {
437 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700438 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700439 }
440 // Alloc passed, so we know there is enough space to hold the string, don't
441 // need strncpy
442 strcpy(loader.layer_dirs, libPaths);
443#if defined(WIN32)
444 // Free any allocated memory:
445 if (must_free_libPaths) {
446 free(libPaths);
447 must_free_libPaths = false;
448 }
449#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700450 libPaths = loader.layer_dirs;
451
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600452 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600453 for (i = 0; i < loader.scanned_layer_count; i++) {
454 if (loader.scanned_layer_names[i] != NULL)
455 free(loader.scanned_layer_names[i]);
456 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600457 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600458 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600459
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600460 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700461 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600462 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700463 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600464 next = p + len;
465 }
466 else {
Ian Elliott19628802015-02-04 12:06:46 -0700467 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600468 *(char *) next = '\0';
469 next++;
470 }
471
472 curdir = opendir(p);
473 if (curdir) {
474 dent = readdir(curdir);
475 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600476 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
477 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700478 */
479 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600480 VK_LAYER_LIBRARY_PREFIX,
481 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700482 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600483 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
484 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700485 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600486 VK_LIBRARY_SUFFIX,
487 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700488 loader_platform_dl_handle handle;
489 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
490 // Used to call: dlopen(temp_str, RTLD_LAZY)
491 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
492 dent = readdir(curdir);
493 continue;
494 }
495 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600496 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700497 break;
498 }
499 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600500 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700501 break;
502 }
503 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
504 loader.scanned_layer_count++;
505 loader_platform_close_library(handle);
506 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600507 }
508
509 dent = readdir(curdir);
510 }
511 closedir(curdir);
512 }
513 }
514
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600515 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600516}
517
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600518static void loader_init_dispatch_table(VK_LAYER_DISPATCH_TABLE *tab, vkGetProcAddrType fpGPA, VK_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600519{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800520 loader_initialize_dispatch_table(tab, fpGPA, gpu);
521
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600522 if (tab->EnumerateLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600523 tab->EnumerateLayers = vkEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600524}
525
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600526static struct loader_icd * loader_get_icd(const VK_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600527{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700528 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
529 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
530 for (uint32_t i = 0; i < icd->gpu_count; i++)
531 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
532 gpu->baseObject) {
533 *gpu_index = i;
534 return icd;
535 }
536 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600537 }
538 return NULL;
539}
540
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600541static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600542{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600543 if (icd->layer_count[gpu_index])
544 return true;
545 else
546 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600547}
548
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600549static 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 -0600550{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600551 if (!icd)
552 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600553
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600554 struct loader_layers *obj;
555 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600556 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600557 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600558 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700559 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 -0600560 foundLib = true;
561 break;
562 }
563 }
564 if (!foundLib) {
565 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700566 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
567 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700568 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
569 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600570 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600571 continue;
572 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600573 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 -0600574 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700575 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600576 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600577 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600578 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600579}
580
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600581static VK_RESULT 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 -0700582{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600583 VK_RESULT err;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700584 char *search_name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700585 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600586 vkGetExtensionSupportType fpGetExtensionSupport;
Jon Ashburnb8358052014-11-18 09:06:04 -0700587
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700588 /*
589 * The loader provides the abstraction that make layers and extensions work via
590 * the currently defined extension mechanism. That is, when app queries for an extension
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600591 * via vkGetExtensionSupport, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700592 * to see who implements that extension. Then, if the app enables the extension during
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600593 * vkCreateDevice the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700594 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700595
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700596 // TODO: What if extension is in multiple places?
597
598 // TODO: Who should we ask first? Driver or layers? Do driver for now.
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600599 err = icd->scanned_icds[gpu_index].GetExtensionSupport((VK_PHYSICAL_GPU) (icd->gpus[gpu_index].nextObject), pExtName);
600 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700601 if (lib_name) {
602 *lib_name = NULL;
Jon Ashburnb8358052014-11-18 09:06:04 -0700603 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600604 return VK_SUCCESS;
Jon Ashburnb8358052014-11-18 09:06:04 -0700605 }
606
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700607 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
608 search_name = loader.scanned_layer_names[j];
609
610 if ((handle = loader_platform_open_library(search_name)) == NULL)
611 continue;
612
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600613 fpGetExtensionSupport = loader_platform_get_proc_address(handle, "vkGetExtensionSupport");
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700614
615 if (fpGetExtensionSupport != NULL) {
616 // Found layer's GetExtensionSupport call
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600617 err = fpGetExtensionSupport((VK_PHYSICAL_GPU) (icd->gpus + gpu_index), pExtName);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700618
619 loader_platform_close_library(handle);
620
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600621 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700622 if (lib_name) {
623 *lib_name = loader.scanned_layer_names[j];
624 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600625 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700626 }
627 } else {
628 loader_platform_close_library(handle);
629 }
630
631 // No GetExtensionSupport or GetExtensionSupport returned invalid extension
632 // for the layer, so test the layer name as if it is an extension name
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600633 // use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700634 char *pEnd;
635 size_t siz;
636
637 search_name = basename(search_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600638 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700639 pEnd = strrchr(search_name, '.');
640 siz = (int) (pEnd - search_name);
641 if (siz != strlen(pExtName))
642 continue;
643
644 if (strncmp(search_name, pExtName, siz) == 0) {
645 if (lib_name) {
646 *lib_name = loader.scanned_layer_names[j];
647 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600648 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700649 }
650 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600651 return VK_ERROR_INVALID_EXTENSION;
Jon Ashburnb8358052014-11-18 09:06:04 -0700652}
653
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600654static 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 -0600655{
Ian Elliott4470a302015-02-17 10:33:47 -0700656 char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600657 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600658 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600659
Ian Elliott4470a302015-02-17 10:33:47 -0700660#if defined(WIN32)
661 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
662 LAYER_NAMES_REGISTRY_VALUE);
663#else // WIN32
664 layerEnv = getenv(LAYER_NAMES_ENV);
665#endif // WIN32
666 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600667 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700668 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600669 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700670 if (p == NULL) {
671#if defined(WIN32)
672 free(layerEnv);
673#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600674 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700675 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600676 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700677#if defined(WIN32)
678 free(layerEnv);
679#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600680 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600681
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600682 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700683 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700684 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600685 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700686 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600687 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700688 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700689 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600690 *(char *) next = '\0';
691 next++;
692 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600693 name = basename(p);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600694 if (find_layer_extension(icd, gpu_index, name, &lib_name) != VK_SUCCESS) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600695 p = next;
696 continue;
697 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600698
Ian Elliott19628802015-02-04 12:06:46 -0700699 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700700 pLayerNames[count].layer_name = malloc(len + 1);
701 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600702 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600703 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600704 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700705 strncpy((char *) pLayerNames[count].layer_name, name, len);
706 pLayerNames[count].layer_name[len] = '\0';
707 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600708 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600709 p = next;
710
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700711 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600712
Jon Ashburnd09bd102014-10-22 21:15:26 -0600713 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600714 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600715}
716
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600717static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, const VK_DEVICE_CREATE_INFO* pCreateInfo, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600718{
Jon Ashburnb8358052014-11-18 09:06:04 -0700719 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700720 const char *lib_name = NULL;
721 uint32_t count = 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600722
723 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700724 /* Load any layers specified in the environment first */
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700725 count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600726
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700727 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
728 const char *pExtName = pCreateInfo->ppEnabledExtensionNames[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600729
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600730 if (find_layer_extension(icd, gpu_index, pExtName, &lib_name) == VK_SUCCESS) {
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700731 uint32_t len;
732
733 /*
734 * the library name is NULL if the driver supports this
735 * extension and thus no layer to load.
736 */
737 if (lib_name == NULL)
738 continue;
739
740 len = (uint32_t) strlen(pExtName);
741 for (uint32_t j = 0; j < count; j++) {
742 if (len == strlen(layerNames[j].layer_name) &&
743 strncmp(pExtName, layerNames[j].layer_name, len) == 0) {
744 // Extension / Layer already on the list
745 continue;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700746 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600747 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700748
749 layerNames[count].layer_name = malloc(len + 1);
750 if (!layerNames[count].layer_name)
751 return count;
752 strncpy((char *) layerNames[count].layer_name, pExtName, len);
753 layerNames[count].layer_name[len] = '\0';
754 layerNames[count].lib_name = lib_name;
755 count++;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600756 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600757 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700758
759 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600760}
761
Jon Ashburn46d1f582015-01-28 11:01:35 -0700762static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600763{
764 struct loader_icd *icd;
765 struct loader_layers *libs;
766
Jon Ashburn46d1f582015-01-28 11:01:35 -0700767 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600768 if (icd->gpus)
769 free(icd->gpus);
770 icd->gpus = NULL;
771 if (icd->loader_dispatch)
772 free(icd->loader_dispatch);
773 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600774 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600775 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600776 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600777 libs = &(icd->layer_libs[j][i]);
778 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700779 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600780 libs->lib_handle = NULL;
781 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600782 if (icd->wrappedGpus[j])
783 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600784 }
785 icd->layer_count[j] = 0;
786 }
787 icd->gpu_count = 0;
788 }
789}
790
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600791extern uint32_t loader_activate_layers(VK_PHYSICAL_GPU gpu, const VK_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600792{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600793 uint32_t gpu_index;
794 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700795 struct layer_name_pair *pLayerNames;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600796 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600797
798 if (!icd)
799 return 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600800 assert(gpu_index < VK_MAX_PHYSICAL_GPUS);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600801
802 /* activate any layer libraries */
803 if (!loader_layers_activated(icd, gpu_index)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600804 VK_BASE_LAYER_OBJECT *gpuObj = (VK_BASE_LAYER_OBJECT *) gpu;
805 VK_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
806 vkGetProcAddrType nextGPA = vkGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600807
Jon Ashburnb8358052014-11-18 09:06:04 -0700808 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600809 if (!count)
810 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700811 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600812
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600813 icd->wrappedGpus[gpu_index] = malloc(sizeof(VK_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600814 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600815 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600816 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600817 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600818 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600819 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600820 nextGpuObj->nextObject = gpuObj;
821 gpuObj = nextGpuObj;
822
Jon Ashburn79113cc2014-12-01 14:22:40 -0700823 char funcStr[256];
824 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600825 if ((nextGPA = (vkGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
826 nextGPA = (vkGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600827 if (!nextGPA) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600828 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 -0600829 continue;
830 }
831
Jon Ashburnf2610012014-10-24 15:48:55 -0600832 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600833 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600834 //Insert the new wrapped objects into the list with loader object at head
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600835 ((VK_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
836 ((VK_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600837 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
838 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700839 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600840 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600841
842 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600843 }
844 else {
845 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700846 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600847 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700848 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600849 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600850 break;
851 }
852 }
853 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600854 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600855 }
856 }
857 return icd->layer_count[gpu_index];
858}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600859
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600860LOADER_EXPORT VK_RESULT VKAPI vkCreateInstance(
861 const VK_INSTANCE_CREATE_INFO* pCreateInfo,
862 VK_INSTANCE* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700863{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700864 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
865 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700866 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700867 struct loader_scanned_icds *scanned_icds;
868 struct loader_icd *icd;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600869 VK_RESULT res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600870
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700871 /* Scan/discover all ICD libraries in a single-threaded manner */
872 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700873
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700874 /* get layer libraries in a single-threaded manner */
875 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700876
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700877 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
878 if (ptr_instance == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600879 return VK_ERROR_OUT_OF_MEMORY;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700880 }
881 memset(ptr_instance, 0, sizeof(struct loader_instance));
882
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700883 ptr_instance->next = loader.instances;
884 loader.instances = ptr_instance;
885
Jon Ashburn46888392015-01-29 15:45:51 -0700886 scanned_icds = loader.scanned_icd_list;
887 while (scanned_icds) {
888 icd = loader_icd_add(ptr_instance, scanned_icds);
889 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -0600890 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn46888392015-01-29 15:45:51 -0700891 &(scanned_icds->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600892 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -0700893 {
894 ptr_instance->icds = ptr_instance->icds->next;
895 loader_icd_destroy(icd);
896 scanned_icds->instance = NULL;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600897 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -0700898 "ICD ignored: failed to CreateInstance on device");
899 }
900 }
901 scanned_icds = scanned_icds->next;
902 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700903
Ian Elliotteb450762015-02-05 15:19:15 -0700904 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600905 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -0700906 }
Jon Ashburn46888392015-01-29 15:45:51 -0700907
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600908 *pInstance = (VK_INSTANCE) ptr_instance;
909 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700910}
911
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600912LOADER_EXPORT VK_RESULT VKAPI vkDestroyInstance(
913 VK_INSTANCE instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700914{
915 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700916 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600917 VK_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700918
919 // Remove this instance from the list of instances:
920 struct loader_instance *prev = NULL;
921 struct loader_instance *next = loader.instances;
922 while (next != NULL) {
923 if (next == ptr_instance) {
924 // Remove this instance from the list:
925 if (prev)
926 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700927 else
928 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700929 break;
930 }
931 prev = next;
932 next = next->next;
933 }
934 if (next == NULL) {
935 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600936 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700937 }
938
Jon Ashburn46888392015-01-29 15:45:51 -0700939 // cleanup any prior layer initializations
940 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700941
Jon Ashburn46888392015-01-29 15:45:51 -0700942 scanned_icds = loader.scanned_icd_list;
943 while (scanned_icds) {
944 if (scanned_icds->instance)
945 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600946 if (res != VK_SUCCESS)
947 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -0700948 "ICD ignored: failed to DestroyInstance on device");
949 scanned_icds->instance = NULL;
950 scanned_icds = scanned_icds->next;
951 }
952
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700953 free(ptr_instance);
954
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600955 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700956}
957
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600958LOADER_EXPORT VK_RESULT VKAPI vkEnumerateGpus(
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700959
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600960 VK_INSTANCE instance,
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700961 uint32_t maxGpus,
962 uint32_t* pGpuCount,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600963 VK_PHYSICAL_GPU* pGpus)
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700964{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700965 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
966 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700967 uint32_t count = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600968 VK_RESULT res;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700969
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600970 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700971 icd = ptr_instance->icds;
972 while (icd) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600973 VK_PHYSICAL_GPU gpus[VK_MAX_PHYSICAL_GPUS];
974 VK_BASE_LAYER_OBJECT * wrapped_gpus;
975 vkGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700976 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700977
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600978 if (max > VK_MAX_PHYSICAL_GPUS) {
979 max = VK_MAX_PHYSICAL_GPUS;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700980 }
981
Jon Ashburn46888392015-01-29 15:45:51 -0700982 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
983 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700984 gpus);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600985 if (res == VK_SUCCESS && n) {
986 wrapped_gpus = (VK_BASE_LAYER_OBJECT*) malloc(n *
987 sizeof(VK_BASE_LAYER_OBJECT));
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700988 icd->gpus = wrapped_gpus;
989 icd->gpu_count = n;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600990 icd->loader_dispatch = (VK_LAYER_DISPATCH_TABLE *) malloc(n *
991 sizeof(VK_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -0700992 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700993 (wrapped_gpus + i)->baseObject = gpus[i];
994 (wrapped_gpus + i)->pGPA = get_proc_addr;
995 (wrapped_gpus + i)->nextObject = gpus[i];
996 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
997 loader_init_dispatch_table(icd->loader_dispatch + i,
998 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -0700999
1000 /* Verify ICD compatibility */
1001 if (!valid_loader_magic_value(gpus[i])) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001002 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001003 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
1004 assert(0);
1005 }
1006
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001007 const VK_LAYER_DISPATCH_TABLE **disp;
1008 disp = (const VK_LAYER_DISPATCH_TABLE **) gpus[i];
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001009 *disp = icd->loader_dispatch + i;
1010 }
1011
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001012 count += n;
1013
1014 if (count >= maxGpus) {
1015 break;
1016 }
1017 }
1018
1019 icd = icd->next;
1020 }
1021
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001022 *pGpuCount = count;
1023
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001024 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001025}
1026
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001027LOADER_EXPORT void * VKAPI vkGetProcAddr(VK_PHYSICAL_GPU gpu, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001028{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001029 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001030 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001031 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001032 VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu;
1033 VK_LAYER_DISPATCH_TABLE * disp_table = * (VK_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001034 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001035
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001036 if (disp_table == NULL)
1037 return NULL;
1038
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001039 addr = loader_lookup_dispatch_table(disp_table, pName);
1040 if (addr)
1041 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001042 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001043 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001044 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001045 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001046 }
1047}
1048
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001049LOADER_EXPORT VK_RESULT VKAPI vkGetExtensionSupport(VK_PHYSICAL_GPU gpu, const char *pExtName)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001050{
1051 uint32_t gpu_index;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001052 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001053
1054 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001055 return VK_ERROR_UNAVAILABLE;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001056
1057 return find_layer_extension(icd, gpu_index, pExtName, NULL);
1058}
1059
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001060LOADER_EXPORT VK_RESULT VKAPI vkEnumerateLayers(VK_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001061{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001062 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001063 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001064 char *lib_name;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001065 struct loader_icd *icd = loader_get_icd((const VK_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001066 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001067 vkEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001068 char layer_buf[16][256];
1069 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001070
Jon Ashburn1323eb72014-12-02 13:03:09 -07001071 if (pOutLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001072 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001073
Jon Ashburn46d1f582015-01-28 11:01:35 -07001074 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001075 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001076
Jon Ashburn1323eb72014-12-02 13:03:09 -07001077 for (int i = 0; i < 16; i++)
1078 layers[i] = &layer_buf[i][0];
1079
1080 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1081 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001082 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1083 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001084 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001085 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1086 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001087 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001088 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001089 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001090 lib_name = basename(lib_name);
1091 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001092 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001093 if (pEnd == NULL || siz <= 0)
1094 continue;
1095 cpyStr = malloc(siz);
1096 if (cpyStr == NULL) {
1097 free(cpyStr);
1098 continue;
1099 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001100 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001101 cpyStr[siz - 1] = '\0';
1102 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001103 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001104 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1105 pOutLayers[count][siz - 1] = '\0';
1106 count++;
1107 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001108 } else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001109 size_t cnt;
1110 uint32_t n;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001111 VK_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001112 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001113 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1114 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001115 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001116 continue;
1117 if (cnt + count > maxLayerCount)
1118 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001119 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001120 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1121 if (n > 0)
1122 pOutLayers[i - count][n - 1] = '\0';
1123 }
1124 count += cnt;
1125 }
1126 }
1127
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001128 *pOutLayerCount = count;
1129
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001130 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001131}
1132
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001133LOADER_EXPORT VK_RESULT VKAPI vkDbgRegisterMsgCallback(VK_INSTANCE instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001134{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001135 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001136 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001137 VK_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001138 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001139
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001140 if (instance == VK_NULL_HANDLE)
1141 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001142
1143 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001144
Jon Ashburn98bd4542015-01-29 16:44:24 -07001145 for (inst = loader.instances; inst; inst = inst->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001146 if (inst == instance)
1147 break;
1148 }
1149
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001150 if (inst == VK_NULL_HANDLE)
1151 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001152
1153 for (icd = inst->icds; icd; icd = icd->next) {
1154 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1155 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001156 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001157 if (res != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001158 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001159 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001160 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001161 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001162 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001163 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001164 }
1165
Jon Ashburnbb510252015-03-17 13:47:55 -06001166
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001167 /* roll back on errors */
1168 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001169 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001170 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001171 for (uint32_t i = 0; i < icd->gpu_count; i++)
1172 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001173 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001174 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001175 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001176 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001177
1178 return res;
1179 }
1180
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001181 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001182}
1183
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001184LOADER_EXPORT VK_RESULT VKAPI vkDbgUnregisterMsgCallback(VK_INSTANCE instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001185{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 VK_RESULT res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001187 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001188 if (instance == VK_NULL_HANDLE)
1189 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001190
1191 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001192
Jon Ashburnbb510252015-03-17 13:47:55 -06001193 for (inst = loader.instances; inst; inst = inst->next) {
1194 if (inst == instance)
1195 break;
1196 }
1197
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001198 if (inst == VK_NULL_HANDLE)
1199 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001200
1201 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1202 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001203 VK_RESULT r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001204 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001205 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001206 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001207 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001208 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001209 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001210 return res;
1211}
1212
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001213LOADER_EXPORT VK_RESULT VKAPI vkDbgSetGlobalOption(VK_INSTANCE instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001214{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001215 VK_RESULT res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001216 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001217 if (instance == VK_NULL_HANDLE)
1218 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001219
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001220 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001221
Jon Ashburnbb510252015-03-17 13:47:55 -06001222 for (inst = loader.instances; inst; inst = inst->next) {
1223 if (inst == instance)
1224 break;
1225 }
1226
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001227 if (inst == VK_NULL_HANDLE)
1228 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001229 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1230 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001231 VK_RESULT r;
Jon Ashburnbb510252015-03-17 13:47:55 -06001232 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001233 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001234 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001235 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001236 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001237 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001238 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001239 }
1240
1241 return res;
1242}