blob: 01750468b43ef699c53e2fdda80875016bb6d0cd [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 Wu19300602014-08-04 08:03:57 +080044#include "loader.h"
Jon Ashburn07daee72015-05-21 18:13:33 -060045#include "wsi_lunarg.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060046#include "gpa_helper.h"
47#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060048#include "debug_report.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060049#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070050// The following is #included again to catch certain OS-specific functions
51// being used:
52#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080053
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060054void loader_add_to_ext_list(
55 struct loader_extension_list *ext_list,
56 uint32_t prop_list_count,
57 const struct loader_extension_property *prop_list);
58
Courtney Goeltzenleuchtered488302015-06-01 14:09:34 -060059static loader_platform_dl_handle loader_add_layer_lib(
60 const char *chain_type,
61 struct loader_extension_property *ext_prop);
62
63static void loader_remove_layer_lib(
64 struct loader_instance *inst,
65 struct loader_extension_property *ext_prop);
66
Jon Ashburn27cd5842015-05-12 17:26:48 -060067struct loader_struct loader = {0};
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068
Courtney Goeltzenleuchter9ec39ac2015-06-22 17:45:21 -060069static void * VKAPI loader_GetInstanceProcAddr(VkInstance instance, const char * pName);
70
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060071enum loader_debug {
72 LOADER_INFO_BIT = VK_BIT(0),
73 LOADER_WARN_BIT = VK_BIT(1),
74 LOADER_PERF_BIT = VK_BIT(2),
75 LOADER_ERROR_BIT = VK_BIT(3),
76 LOADER_DEBUG_BIT = VK_BIT(4),
77};
78
79uint32_t g_loader_debug = 0;
80uint32_t g_loader_log_msgs = 0;
81
Jon Ashburn6301a0f2015-05-29 13:15:39 -060082//thread safety lock for accessing global data structures such as "loader"
83// all entrypoints on the instance chain need to be locked except GPA
84// additionally DestroyDevice needs to be locked
85loader_platform_thread_mutex loader_lock;
86
87const VkLayerInstanceDispatchTable instance_disp = {
Courtney Goeltzenleuchter9ec39ac2015-06-22 17:45:21 -060088 .GetInstanceProcAddr = loader_GetInstanceProcAddr,
Jon Ashburn27cd5842015-05-12 17:26:48 -060089 .CreateInstance = loader_CreateInstance,
90 .DestroyInstance = loader_DestroyInstance,
91 .EnumeratePhysicalDevices = loader_EnumeratePhysicalDevices,
Chris Forbesbc0bb772015-06-21 22:55:02 +120092 .GetPhysicalDeviceFeatures = loader_GetPhysicalDeviceFeatures,
93 .GetPhysicalDeviceFormatInfo = loader_GetPhysicalDeviceFormatInfo,
94 .GetPhysicalDeviceLimits = loader_GetPhysicalDeviceLimits,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060095 .CreateDevice = loader_CreateDevice,
Tony Barbour59a47322015-06-24 16:06:58 -060096 .GetPhysicalDeviceProperties = loader_GetPhysicalDeviceProperties,
97 .GetPhysicalDevicePerformance = loader_GetPhysicalDevicePerformance,
98 .GetPhysicalDeviceQueueCount = loader_GetPhysicalDeviceQueueCount,
99 .GetPhysicalDeviceQueueProperties = loader_GetPhysicalDeviceQueueProperties,
100 .GetPhysicalDeviceMemoryProperties = loader_GetPhysicalDeviceMemoryProperties,
101 .GetPhysicalDeviceExtensionCount = loader_GetPhysicalDeviceExtensionCount,
102 .GetPhysicalDeviceExtensionProperties = loader_GetPhysicalDeviceExtensionProperties,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600103 .DbgCreateMsgCallback = loader_DbgCreateMsgCallback,
104 .DbgDestroyMsgCallback = loader_DbgDestroyMsgCallback,
Jon Ashburn27cd5842015-05-12 17:26:48 -0600105};
106
107LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
108LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
109LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700110
Ian Elliott4470a302015-02-17 10:33:47 -0700111#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600112char *loader_get_registry_string(const HKEY hive,
113 const LPCTSTR sub_key,
114 const char *value)
115{
116 DWORD access_flags = KEY_QUERY_VALUE;
117 DWORD value_type;
118 HKEY key;
Ian Elliottf851ddf2015-04-28 15:57:32 -0600119 VkResult rtn_value;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600120 char *rtn_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600121 DWORD rtn_len = 0;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600122 size_t allocated_len = 0;
123
124 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
125 if (rtn_value != ERROR_SUCCESS) {
126 // We didn't find the key. Try the 32-bit hive (where we've seen the
127 // key end up on some people's systems):
128 access_flags |= KEY_WOW64_32KEY;
129 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
130 if (rtn_value != ERROR_SUCCESS) {
131 // We still couldn't find the key, so give up:
132 return NULL;
133 }
134 }
135
136 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600137 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600138 if (rtn_value == ERROR_SUCCESS) {
139 // If we get to here, we found the key, and need to allocate memory
140 // large enough for rtn_str, and query again:
141 allocated_len = rtn_len + 4;
142 rtn_str = malloc(allocated_len);
143 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600144 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600145 if (rtn_value == ERROR_SUCCESS) {
146 // We added 4 extra bytes to rtn_str, so that we can ensure that
147 // the string is NULL-terminated (albeit, in a brute-force manner):
148 rtn_str[allocated_len-1] = '\0';
149 } else {
150 // This should never occur, but in case it does, clean up:
151 free(rtn_str);
152 rtn_str = NULL;
153 }
154 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
155
156 // Close the registry key that was opened:
157 RegCloseKey(key);
158
159 return rtn_str;
160}
161
162
Ian Elliott4470a302015-02-17 10:33:47 -0700163// For ICD developers, look in the registry, and look for an environment
164// variable for a path(s) where to find the ICD(s):
165static char *loader_get_registry_and_env(const char *env_var,
166 const char *registry_value)
167{
168 char *env_str = getenv(env_var);
169 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600170 char *registry_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600171 size_t registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700172 char *rtn_str = NULL;
173 size_t rtn_len;
174
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600175 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600176 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600177 registry_value);
Ian Elliottf851ddf2015-04-28 15:57:32 -0600178 registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700179
180 rtn_len = env_len + registry_len + 1;
181 if (rtn_len <= 2) {
182 // We found neither the desired registry value, nor the environment
183 // variable; return NULL:
184 return NULL;
185 } else {
186 // We found something, and so we need to allocate memory for the string
187 // to return:
188 rtn_str = malloc(rtn_len);
189 }
190
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600191 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700192 // We didn't find the desired registry value, and so we must have found
193 // only the environment variable:
194 _snprintf(rtn_str, rtn_len, "%s", env_str);
195 } else if (env_str != NULL) {
196 // We found both the desired registry value and the environment
197 // variable, so concatenate them both:
198 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
199 } else {
200 // We must have only found the desired registry value:
201 _snprintf(rtn_str, rtn_len, "%s", registry_str);
202 }
203
Ian Elliott2de26bc2015-04-03 13:13:01 -0600204 if (registry_str) {
205 free(registry_str);
206 }
Ian Elliott4470a302015-02-17 10:33:47 -0700207
208 return(rtn_str);
209}
210#endif // WIN32
211
212
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600213static void loader_log(VkFlags msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800214 const char *format, ...)
215{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216 char msg[256];
217 va_list ap;
218 int ret;
219
Courtney Goeltzenleuchtera585ecb2015-06-09 09:42:23 -0600220 if (!(msg_type & g_loader_log_msgs)) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600221 return;
222 }
223
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800224 va_start(ap, format);
225 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700226 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800227 msg[sizeof(msg) - 1] = '\0';
228 }
229 va_end(ap);
230
Jon Ashburnae053e92015-04-24 14:10:50 -0700231#if defined(WIN32)
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600232 OutputDebugString(msg);
Jon Ashburn1de39402015-05-05 16:20:46 -0600233#endif
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600234 fputs(msg, stderr);
235 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800236}
237
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600238bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2)
239{
240 return memcmp(op1, op2, sizeof(VkExtensionProperties)) == 0 ? true : false;
241}
242
243/*
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600244 * Search the given ext_list for an extension
245 * matching the given vk_ext_prop
246 */
247bool has_vk_extension_property(
248 const VkExtensionProperties *vk_ext_prop,
249 const struct loader_extension_list *ext_list)
250{
251 for (uint32_t i = 0; i < ext_list->count; i++) {
252 if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop))
253 return true;
254 }
255 return false;
256}
257
258/*
259 * Search the given ext_list for an extension
260 * matching the given vk_ext_prop
261 */
262static struct loader_extension_property *get_extension_property_from_vkext(
263 const VkExtensionProperties *vk_ext_prop,
264 const struct loader_extension_list *ext_list)
265{
266 for (uint32_t i = 0; i < ext_list->count; i++) {
267 if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop))
268 return &ext_list->list[i];
269 }
270 return NULL;
271}
272
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600273static void loader_get_global_extensions(
Tony Barbour59a47322015-06-24 16:06:58 -0600274 const PFN_vkGetGlobalExtensionCount fp_get_count,
275 const PFN_vkGetGlobalExtensionProperties fp_get_props,
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600276 const PFN_vkGPA get_proc_addr,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600277 const char *lib_name,
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600278 const loader_platform_dl_handle lib_handle,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600279 const enum extension_origin origin,
280 struct loader_extension_list *ext_list)
281{
282 uint32_t i, count;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600283 struct loader_extension_property ext_props;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600284 VkResult res;
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600285 PFN_vkGPA ext_get_proc_addr;
286 PFN_vkGetInstanceProcAddr get_instance_proc_addr;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600287
Tony Barbour59a47322015-06-24 16:06:58 -0600288 res = fp_get_count(&count);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600289 if (res != VK_SUCCESS) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600290 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Error getting global extension count from ICD");
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600291 return;
292 }
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600293
294 if (get_proc_addr == NULL)
295 get_instance_proc_addr = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Tony Barbour59a47322015-06-24 16:06:58 -0600296
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600297 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600298 memset(&ext_props, 0, sizeof(ext_props));
Tony Barbour59a47322015-06-24 16:06:58 -0600299 res = fp_get_props(i, &ext_props.info);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600300 if (res == VK_SUCCESS) {
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600301 //TODO eventually get this from the layer config file
302 if (get_proc_addr == NULL) {
303 char funcStr[MAX_EXTENSION_NAME_SIZE+1];
304 snprintf(funcStr, MAX_EXTENSION_NAME_SIZE, "%sGetInstanceProcAddr", ext_props.info.name);
305
306 if ((ext_get_proc_addr = (PFN_vkGPA) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
307 ext_get_proc_addr = get_instance_proc_addr;
308 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600309 ext_props.origin = origin;
310 ext_props.lib_name = lib_name;
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600311 ext_props.get_proc_addr = (get_proc_addr == NULL) ? ext_get_proc_addr : get_proc_addr;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600312 loader_add_to_ext_list(ext_list, 1, &ext_props);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600313 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600314 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600315
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600316 return;
317}
318
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600319static void loader_get_physical_device_layer_extensions(
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600320 struct loader_instance *ptr_instance,
321 VkPhysicalDevice physical_device,
322 const uint32_t layer_index,
323 struct loader_extension_list *ext_list)
324{
325 uint32_t i, count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600326 VkResult res;
327 loader_platform_dl_handle lib_handle;
Tony Barbour59a47322015-06-24 16:06:58 -0600328 PFN_vkGetPhysicalDeviceExtensionProperties get_phys_dev_ext_props;
329 PFN_vkGetPhysicalDeviceExtensionCount get_phys_dev_ext_count;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600330 struct loader_extension_property ext_props;
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600331 PFN_vkGPA ext_get_proc_addr;
332 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600333
334 if (!loader.scanned_layers[layer_index].physical_device_extensions_supported) {
335 return;
336 }
337
338 ext_props.origin = VK_EXTENSION_ORIGIN_LAYER;
339 ext_props.lib_name = loader.scanned_layers[layer_index].lib_name;
Jon Ashburn128f9422015-05-28 19:16:58 -0600340 char funcStr[MAX_EXTENSION_NAME_SIZE+1]; // add one character for 0 termination
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600341
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600342 lib_handle = loader_add_layer_lib("device", &ext_props);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600343
Tony Barbour59a47322015-06-24 16:06:58 -0600344 get_phys_dev_ext_props = (PFN_vkGetPhysicalDeviceExtensionProperties) loader_platform_get_proc_address(lib_handle, "vkGetPhysicalDeviceExtensionProperties");
345 get_phys_dev_ext_count = (PFN_vkGetPhysicalDeviceExtensionCount) loader_platform_get_proc_address(lib_handle, "vkGetPhysicalDeviceExtensionCount");
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600346 get_device_proc_addr = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Tony Barbour59a47322015-06-24 16:06:58 -0600347 if (get_phys_dev_ext_count) {
348 res = get_phys_dev_ext_count(physical_device, &count);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600349 if (res == VK_SUCCESS) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600350 for (i = 0; i < count; i++) {
351 memset(&ext_props, 0, sizeof(ext_props));
Tony Barbour59a47322015-06-24 16:06:58 -0600352 res = get_phys_dev_ext_props(physical_device, i, &ext_props.info);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600353 if (res == VK_SUCCESS && (ext_props.info.sType == VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES)) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600354 ext_props.origin = VK_EXTENSION_ORIGIN_LAYER;
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600355 //TODO eventually get this from the layer config file
356 snprintf(funcStr, MAX_EXTENSION_NAME_SIZE, "%sGetDeviceProcAddr", ext_props.info.name);
357
358 if ((ext_get_proc_addr = (PFN_vkGPA) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
359 ext_get_proc_addr = get_device_proc_addr;
360 ext_props.get_proc_addr = ext_get_proc_addr;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600361 ext_props.lib_name = loader.scanned_layers[layer_index].lib_name;
362 loader_add_to_ext_list(ext_list, 1, &ext_props);
363 }
364 }
365 } else {
366 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Error getting physical device extension info count from Layer %s", ext_props.lib_name);
367 }
368 }
369
370 loader_remove_layer_lib(ptr_instance, &ext_props);
371 return;
372}
373
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600374static bool loader_init_ext_list(struct loader_extension_list *ext_info)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600375{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600376 ext_info->capacity = 32 * sizeof(struct loader_extension_property);
377 ext_info->list = malloc(ext_info->capacity);
378 if (ext_info->list == NULL) {
379 return false;
380 }
381 memset(ext_info->list, 0, ext_info->capacity);
382 ext_info->count = 0;
383 return true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600384}
385
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -0600386void loader_destroy_ext_list(struct loader_extension_list *ext_info)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600387{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600388 free(ext_info->list);
389 ext_info->count = 0;
390 ext_info->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600391}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600392
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600393static void loader_add_vk_ext_to_ext_list(
394 struct loader_extension_list *ext_list,
395 uint32_t prop_list_count,
396 const VkExtensionProperties *props,
397 const struct loader_extension_list *search_list)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600398{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600399 struct loader_extension_property *ext_prop;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600400
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600401 for (uint32_t i = 0; i < prop_list_count; i++) {
402 // look for duplicates
403 if (has_vk_extension_property(&props[i], ext_list)) {
404 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600405 }
406
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600407 ext_prop = get_extension_property_from_vkext(&props[i], search_list);
408 if (!ext_prop) {
409 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Unable to find extension %s", props[i].name);
410 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600411 }
412
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600413 loader_add_to_ext_list(ext_list, 1, ext_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600414 }
415}
416
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600417/*
418 * Append non-duplicate extension properties defined in prop_list
419 * to the given ext_info list
420 */
421void loader_add_to_ext_list(
422 struct loader_extension_list *ext_list,
423 uint32_t prop_list_count,
424 const struct loader_extension_property *props)
425{
426 uint32_t i;
427 struct loader_extension_property *cur_ext;
428
429 if (ext_list->list == NULL || ext_list->capacity == 0) {
430 loader_init_ext_list(ext_list);
431 }
432
433 if (ext_list->list == NULL)
434 return;
435
436 for (i = 0; i < prop_list_count; i++) {
437 cur_ext = (struct loader_extension_property *) &props[i];
438
439 // look for duplicates
440 if (has_vk_extension_property(&cur_ext->info, ext_list)) {
441 continue;
442 }
443
444 // add to list at end
445 // check for enough capacity
446 if (ext_list->count * sizeof(struct loader_extension_property)
447 >= ext_list->capacity) {
448 // double capacity
449 ext_list->capacity *= 2;
450 ext_list->list = realloc(ext_list->list, ext_list->capacity);
451 }
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600452
453 /*
454 * Check if any extensions already on the list come from the same
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600455 * library and use the same Get*ProcAddr. If so, link this
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600456 * extension to the previous as an alias. That way when we activate
457 * extensions we only activiate the associated layer once no
458 * matter how many extensions are used.
459 */
460 for (uint32_t j = 0; j < ext_list->count; j++) {
461 struct loader_extension_property *active_property = &ext_list->list[j];
462 if (cur_ext->lib_name &&
463 cur_ext->origin == VK_EXTENSION_ORIGIN_LAYER &&
464 active_property->origin == VK_EXTENSION_ORIGIN_LAYER &&
465 strcmp(cur_ext->lib_name, active_property->lib_name) == 0 &&
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600466 (cur_ext->get_proc_addr == active_property->get_proc_addr) &&
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600467 active_property->alias == NULL) {
468 cur_ext->alias = active_property;
469 break;
470 }
471 }
472
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600473 memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(struct loader_extension_property));
474 ext_list->count++;
475 }
476}
477
478/*
479 * Search the search_list for any extension with
480 * a name that matches the given ext_name.
481 * Add all matching extensions to the found_list
482 * Do not add if found VkExtensionProperties is already
483 * on the found_list
484 */
485static void loader_search_ext_list_for_name(
486 const char *ext_name,
487 const struct loader_extension_list *search_list,
488 struct loader_extension_list *found_list)
489{
490 for (uint32_t i = 0; i < search_list->count; i++) {
491 struct loader_extension_property *ext_prop = &search_list->list[i];
Courtney Goeltzenleuchtera6743522015-06-09 09:14:48 -0600492 if (ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER &&
493 0 == strcmp(ext_prop->info.name, ext_name)) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600494 /* Found an extension with the same name, add to found_list */
495 loader_add_to_ext_list(found_list, 1, &search_list->list[i]);
496 }
497 }
498}
499
500bool loader_is_extension_scanned(const VkExtensionProperties *ext_prop)
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600501{
502 uint32_t i;
503
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600504 for (i = 0; i < loader.global_extensions.count; i++) {
505 if (compare_vk_extension_properties(&loader.global_extensions.list[i].info, ext_prop))
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600506 return true;
507 }
508 return false;
509}
510
Jon Ashburn27cd5842015-05-12 17:26:48 -0600511void loader_coalesce_extensions(void)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600512{
513 uint32_t i;
514 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
515
516 // traverse scanned icd list adding non-duplicate extensions to the list
517 while (icd_list != NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600518 loader_add_to_ext_list(&loader.global_extensions,
519 icd_list->global_extension_list.count,
520 icd_list->global_extension_list.list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600521 icd_list = icd_list->next;
522 };
523
524 //Traverse layers list adding non-duplicate extensions to the list
525 for (i = 0; i < loader.scanned_layer_count; i++) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600526 loader_add_to_ext_list(&loader.global_extensions,
527 loader.scanned_layers[i].global_extension_list.count,
528 loader.scanned_layers[i].global_extension_list.list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600529 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600530
531 // Traverse loader's extensions, adding non-duplicate extensions to the list
532 debug_report_add_instance_extensions(&loader.global_extensions);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600533}
534
Jon Ashburndc6fcad2015-06-10 10:06:06 -0600535static struct loader_icd *loader_get_icd_and_device(const VkDevice device,
536 struct loader_device **found_dev)
537{
538 *found_dev = NULL;
539 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
540 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
541 for (struct loader_device *dev = icd->logical_device_list; dev; dev = dev->next)
542 if (dev->device == device) {
543 *found_dev = dev;
544 return icd;
545 }
546 }
547 }
548 return NULL;
549}
550
551static void loader_destroy_logical_device(struct loader_device *dev)
552{
553 free(dev->app_extension_props);
554 if (dev->enabled_device_extensions.count)
555 loader_destroy_ext_list(&dev->enabled_device_extensions);
556 if (dev->activated_layer_list.count)
557 loader_destroy_ext_list(&dev->activated_layer_list);
558 free(dev);
559}
560
561static struct loader_device *loader_add_logical_device(const VkDevice dev, struct loader_device **device_list)
562{
563 struct loader_device *new_dev;
564
565 new_dev = malloc(sizeof(struct loader_device));
566 if (!new_dev) {
567 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc struct laoder-device");
568 return NULL;
569 }
570
571 memset(new_dev, 0, sizeof(struct loader_device));
572
573 new_dev->next = *device_list;
574 new_dev->device = dev;
575 *device_list = new_dev;
576 return new_dev;
577}
578
579void loader_remove_logical_device(VkDevice device)
580{
581 struct loader_device *found_dev, *dev, *prev_dev;
582 struct loader_icd *icd;
583 icd = loader_get_icd_and_device(device, &found_dev);
584
585 if (!icd || !found_dev)
586 return;
587
588 prev_dev = NULL;
589 dev = icd->logical_device_list;
590 while (dev && dev != found_dev) {
591 prev_dev = dev;
592 dev = dev->next;
593 }
594
595 if (prev_dev)
596 prev_dev->next = found_dev->next;
597 else
598 icd->logical_device_list = found_dev->next;
599 loader_destroy_logical_device(found_dev);
600}
601
602
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600603static void loader_icd_destroy(
604 struct loader_instance *ptr_inst,
605 struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800606{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600607 ptr_inst->total_icd_count--;
Jon Ashburn128f9422015-05-28 19:16:58 -0600608 free(icd->gpus);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -0600609 for (struct loader_device *dev = icd->logical_device_list; dev; ) {
610 struct loader_device *next_dev = dev->next;
Jon Ashburndc6fcad2015-06-10 10:06:06 -0600611 loader_destroy_logical_device(dev);
Courtney Goeltzenleuchter1f157ac2015-06-14 19:57:15 -0600612 dev = next_dev;
613 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -0600614
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800615 free(icd);
616}
617
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600618static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800619{
620 struct loader_icd *icd;
621
622 icd = malloc(sizeof(*icd));
623 if (!icd)
624 return NULL;
625
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600626 memset(icd, 0, sizeof(*icd));
627
Jon Ashburn46d1f582015-01-28 11:01:35 -0700628 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800629
630 return icd;
631}
632
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600633static struct loader_icd *loader_icd_add(
634 struct loader_instance *ptr_inst,
635 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800636{
637 struct loader_icd *icd;
638
Jon Ashburn46d1f582015-01-28 11:01:35 -0700639 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800640 if (!icd)
641 return NULL;
642
Chia-I Wu13a61a52014-08-04 11:18:20 +0800643 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700644 icd->next = ptr_inst->icds;
645 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600646 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800647
648 return icd;
649}
650
Jon Ashburn46d1f582015-01-28 11:01:35 -0700651static void loader_scanned_icd_add(const char *filename)
652{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700653 loader_platform_dl_handle handle;
Jon Ashburn3da71f22015-05-14 12:43:38 -0600654 void *fp_create_inst;
Tony Barbour59a47322015-06-24 16:06:58 -0600655 void *fp_get_global_ext_props;
656 void *fp_get_global_ext_count;
657 void *fp_get_device_ext_props;
658 void *fp_get_device_ext_count;
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600659 PFN_vkGPA fp_get_proc_addr;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700660 struct loader_scanned_icds *new_node;
661
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700662 // Used to call: dlopen(filename, RTLD_LAZY);
663 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700664 if (!handle) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600665 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700666 return;
667 }
668
669#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600670 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700671 if (!func_ptr) { \
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600672 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700673 return; \
674 } \
675} while (0)
676
Jon Ashburn46888392015-01-29 15:45:51 -0700677 LOOKUP(fp_create_inst, CreateInstance);
Tony Barbour59a47322015-06-24 16:06:58 -0600678 LOOKUP(fp_get_global_ext_props, GetGlobalExtensionProperties);
679 LOOKUP(fp_get_global_ext_count, GetGlobalExtensionCount);
680 LOOKUP(fp_get_device_ext_props, GetPhysicalDeviceExtensionProperties);
681 LOOKUP(fp_get_device_ext_count, GetPhysicalDeviceExtensionCount);
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600682 LOOKUP(fp_get_proc_addr, GetDeviceProcAddr);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700683#undef LOOKUP
684
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600685 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds)
686 + strlen(filename) + 1);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700687 if (!new_node) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600688 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700689 return;
690 }
691
692 new_node->handle = handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700693 new_node->CreateInstance = fp_create_inst;
Tony Barbour59a47322015-06-24 16:06:58 -0600694 new_node->GetGlobalExtensionProperties = fp_get_global_ext_props;
695 new_node->GetGlobalExtensionCount = fp_get_global_ext_count;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600696 loader_init_ext_list(&new_node->global_extension_list);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600697 loader_init_ext_list(&new_node->device_extension_list);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700698 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700699
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600700 new_node->lib_name = (char *) (new_node + 1);
701 if (!new_node->lib_name) {
702 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd");
703 return;
704 }
705 strcpy(new_node->lib_name, filename);
706
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600707 loader.scanned_icd_list = new_node;
708
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600709 loader_get_global_extensions(
Tony Barbour59a47322015-06-24 16:06:58 -0600710 (PFN_vkGetGlobalExtensionCount) fp_get_global_ext_count,
711 (PFN_vkGetGlobalExtensionProperties) fp_get_global_ext_props,
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600712 fp_get_proc_addr,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600713 new_node->lib_name,
Jon Ashburn953bb3c2015-06-10 16:11:42 -0600714 handle,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600715 VK_EXTENSION_ORIGIN_ICD,
716 &new_node->global_extension_list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600717}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700718
Jon Ashburn3da71f22015-05-14 12:43:38 -0600719static void loader_icd_init_entrys(struct loader_icd *icd,
720 struct loader_scanned_icds *scanned_icds)
721{
722 /* initialize entrypoint function pointers */
723
724 #define LOOKUP(func) do { \
725 icd->func = (PFN_vk ##func) loader_platform_get_proc_address(scanned_icds->handle, "vk" #func); \
726 if (!icd->func) { \
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600727 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn3da71f22015-05-14 12:43:38 -0600728 return; \
729 } \
730 } while (0)
731
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600732 /* could change this to use GetInstanceProcAddr in driver instead of dlsym */
733 LOOKUP(GetDeviceProcAddr);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600734 LOOKUP(DestroyInstance);
735 LOOKUP(EnumeratePhysicalDevices);
Chris Forbesbc0bb772015-06-21 22:55:02 +1200736 LOOKUP(GetPhysicalDeviceFeatures);
737 LOOKUP(GetPhysicalDeviceFormatInfo);
738 LOOKUP(GetPhysicalDeviceLimits);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600739 LOOKUP(CreateDevice);
Tony Barbour59a47322015-06-24 16:06:58 -0600740 LOOKUP(GetPhysicalDeviceProperties);
741 LOOKUP(GetPhysicalDeviceMemoryProperties);
742 LOOKUP(GetPhysicalDevicePerformance);
743 LOOKUP(GetPhysicalDeviceQueueCount);
744 LOOKUP(GetPhysicalDeviceQueueProperties);
745 LOOKUP(GetPhysicalDeviceExtensionProperties);
746 LOOKUP(GetPhysicalDeviceExtensionCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600747 LOOKUP(DbgCreateMsgCallback);
748 LOOKUP(DbgDestroyMsgCallback);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600749#undef LOOKUP
750
751 return;
752}
753
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600754static void loader_debug_init(void)
755{
756 const char *env;
757
758 if (g_loader_debug > 0)
759 return;
760
761 g_loader_debug = 0;
762
763 /* parse comma-separated debug options */
764 env = getenv("LOADER_DEBUG");
765 while (env) {
766 const char *p = strchr(env, ',');
767 size_t len;
768
769 if (p)
770 len = p - env;
771 else
772 len = strlen(env);
773
774 if (len > 0) {
775 if (strncmp(env, "warn", len) == 0) {
776 g_loader_debug |= LOADER_WARN_BIT;
777 g_loader_log_msgs |= VK_DBG_REPORT_WARN_BIT;
778 } else if (strncmp(env, "info", len) == 0) {
779 g_loader_debug |= LOADER_INFO_BIT;
780 g_loader_log_msgs |= VK_DBG_REPORT_INFO_BIT;
781 } else if (strncmp(env, "perf", len) == 0) {
782 g_loader_debug |= LOADER_PERF_BIT;
783 g_loader_log_msgs |= VK_DBG_REPORT_PERF_WARN_BIT;
784 } else if (strncmp(env, "error", len) == 0) {
785 g_loader_debug |= LOADER_ERROR_BIT;
786 g_loader_log_msgs |= VK_DBG_REPORT_ERROR_BIT;
787 } else if (strncmp(env, "debug", len) == 0) {
788 g_loader_debug |= LOADER_DEBUG_BIT;
789 g_loader_log_msgs |= VK_DBG_REPORT_DEBUG_BIT;
790 }
791 }
792
793 if (!p)
794 break;
795
796 env = p + 1;
797 }
798}
799
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600800/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600801 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600802 *
803 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600804 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
805 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600806 *
807 * \returns
808 * void; but side effect is to set loader_icd_scanned to true
809 */
Jon Ashburn27cd5842015-05-12 17:26:48 -0600810void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800811{
Ian Elliott4470a302015-02-17 10:33:47 -0700812 const char *p, *next;
813 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600814 DIR *sysdir;
815 struct dirent *dent;
816 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600817 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700818 uint32_t len;
Jon Ashburn6301a0f2015-05-29 13:15:39 -0600819
820 // convenient place to initialize a mutex
821 loader_platform_thread_create_mutex(&loader_lock);
822
Ian Elliott4470a302015-02-17 10:33:47 -0700823#if defined(WIN32)
824 bool must_free_libPaths;
825 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
826 DRIVER_PATH_REGISTRY_VALUE);
827 if (libPaths != NULL) {
828 must_free_libPaths = true;
829 } else {
830 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600831 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700832 }
833#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600834 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700835 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
836 libPaths = getenv(DRIVER_PATH_ENV);
837 }
838 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600839 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600840 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700841#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800842
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600843 loader_debug_init();
844
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600845 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700846 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600847 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700848 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600849 next = p + len;
850 }
851 else {
Ian Elliott19628802015-02-04 12:06:46 -0700852 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600853 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
854 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600855 next++;
856 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800857
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700858 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
859 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600860 sysdir = opendir(p);
861 if (sysdir) {
862 dent = readdir(sysdir);
863 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600864 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
865 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700866 */
867 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600868 VK_DRIVER_LIBRARY_PREFIX,
869 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700870 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600871 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
872 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700873 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600874 VK_LIBRARY_SUFFIX,
875 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700876 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
877 loader_scanned_icd_add(icd_library);
878 }
879 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600880
881 dent = readdir(sysdir);
882 }
883 closedir(sysdir);
884 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800885 }
886
Ian Elliott4470a302015-02-17 10:33:47 -0700887#if defined(WIN32)
888 // Free any allocated memory:
889 if (must_free_libPaths) {
890 free(libPaths);
891 }
892#endif // WIN32
893
894 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700895 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800896}
897
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600898
Jon Ashburn27cd5842015-05-12 17:26:48 -0600899void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600900{
901 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700902 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600903 DIR *curdir;
904 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700905 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600906 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600907 uint32_t count;
Tony Barbour59a47322015-06-24 16:06:58 -0600908 PFN_vkGetGlobalExtensionProperties fp_get_props;
909 PFN_vkGetGlobalExtensionCount fp_get_count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600910
Ian Elliott4470a302015-02-17 10:33:47 -0700911#if defined(WIN32)
912 bool must_free_libPaths;
913 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
914 LAYERS_PATH_REGISTRY_VALUE);
915 if (libPaths != NULL) {
916 must_free_libPaths = true;
917 } else {
918 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600919 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600920 }
Ian Elliott4470a302015-02-17 10:33:47 -0700921#else // WIN32
922 if (geteuid() == getuid()) {
923 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700924 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700925 }
926 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600927 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700928 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700929#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600930
Ian Elliott4470a302015-02-17 10:33:47 -0700931 if (libPaths == NULL) {
932 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700933 return;
934 }
Ian Elliott4470a302015-02-17 10:33:47 -0700935 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700936 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700937 if (loader.layer_dirs == NULL) {
Jon Ashburn90c6a0e2015-06-04 15:30:58 -0600938 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add layer directories");
939
Ian Elliott4470a302015-02-17 10:33:47 -0700940 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700941 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700942 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600943 // Alloc passed, so we know there is enough space to hold the string
Ian Elliott4470a302015-02-17 10:33:47 -0700944 strcpy(loader.layer_dirs, libPaths);
945#if defined(WIN32)
946 // Free any allocated memory:
947 if (must_free_libPaths) {
948 free(libPaths);
949 must_free_libPaths = false;
950 }
951#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700952 libPaths = loader.layer_dirs;
953
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600954 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600955 for (i = 0; i < loader.scanned_layer_count; i++) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600956 if (loader.scanned_layers[i].lib_name != NULL)
957 free(loader.scanned_layers[i].lib_name);
958 loader_destroy_ext_list(&loader.scanned_layers[i].global_extension_list);
959 loader.scanned_layers[i].lib_name = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600960 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600961 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600962 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600963
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600964 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600965 next = strchr(p, PATH_SEPERATOR);
966 if (next == NULL) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600967 len = (uint32_t) strlen(p);
968 next = p + len;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600969 }
970 else {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600971 len = (uint32_t) (next - p);
972 *(char *) next = '\0';
973 next++;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600974 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600975
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600976 curdir = opendir(p);
977 if (curdir) {
978 dent = readdir(curdir);
979 while (dent) {
980 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
981 * ending with VK_LIBRARY_SUFFIX
982 */
983 if (!strncmp(dent->d_name,
984 VK_LAYER_LIBRARY_PREFIX,
985 VK_LAYER_LIBRARY_PREFIX_LEN)) {
986 uint32_t nlen = (uint32_t) strlen(dent->d_name);
987 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
988 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
989 !strncmp(suf,
990 VK_LIBRARY_SUFFIX,
991 VK_LIBRARY_SUFFIX_LEN)) {
992 loader_platform_dl_handle handle;
993 snprintf(temp_str, sizeof(temp_str),
994 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
995 // Used to call: dlopen(temp_str, RTLD_LAZY)
996 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600997 "Attempt to open library: %s", temp_str);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600998 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600999 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0, "open library failed");
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001000 dent = readdir(curdir);
1001 continue;
1002 }
1003 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -06001004 "Opened library: %s", temp_str);
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06001005
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001006 /* TODO: Remove fixed count */
1007 if (count == MAX_LAYER_LIBRARIES) {
1008 loader_log(VK_DBG_REPORT_ERROR_BIT, 0,
1009 "%s ignored: max layer libraries exceed",
1010 temp_str);
1011 break;
1012 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001013
Tony Barbour59a47322015-06-24 16:06:58 -06001014 fp_get_count = loader_platform_get_proc_address(handle, "vkGetGlobalExtensionCount");
1015 fp_get_props = loader_platform_get_proc_address(handle, "vkGetGlobalExtensionProperties");
1016 if (!fp_get_props || !fp_get_count) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001017 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
Tony Barbour59a47322015-06-24 16:06:58 -06001018 "Couldn't dlsym vkGetGlobalExtensionCount and/or vkGetGlobalExtensionProperties from library %s",
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001019 temp_str);
1020 dent = readdir(curdir);
1021 loader_platform_close_library(handle);
1022 continue;
1023 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001024
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001025 loader.scanned_layers[count].lib_name =
1026 malloc(strlen(temp_str) + 1);
1027 if (loader.scanned_layers[count].lib_name == NULL) {
1028 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "%s ignored: out of memory", temp_str);
1029 break;
1030 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001031
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001032 strcpy(loader.scanned_layers[count].lib_name, temp_str);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001033
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -06001034 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0, "Collecting global extensions for %s", temp_str);
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001035 loader_get_global_extensions(
Tony Barbour59a47322015-06-24 16:06:58 -06001036 fp_get_count,
1037 fp_get_props,
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001038 NULL,
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001039 loader.scanned_layers[count].lib_name,
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001040 handle,
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001041 VK_EXTENSION_ORIGIN_LAYER,
1042 &loader.scanned_layers[count].global_extension_list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001043
Tony Barbour59a47322015-06-24 16:06:58 -06001044 fp_get_count = loader_platform_get_proc_address(handle,
1045 "vkGetPhysicalDeviceExtensionCount");
1046 fp_get_props = loader_platform_get_proc_address(handle,
1047 "vkGetPhysicalDeviceExtensionProperties");
1048 if (fp_get_props && fp_get_count) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001049 loader.scanned_layers[count].physical_device_extensions_supported = true;
1050 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001051
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001052 count++;
1053 loader_platform_close_library(handle);
1054 }
1055 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001056
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001057 dent = readdir(curdir);
1058 } // while (dir_entry)
1059 if (count == MAX_LAYER_LIBRARIES)
1060 break;
1061 closedir(curdir);
1062 } // if (curdir))
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001063 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001064
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001065 loader.scanned_layer_count = count;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001066 loader.layers_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001067}
1068
Jon Ashburn27cd5842015-05-12 17:26:48 -06001069static void* VKAPI loader_gpa_instance_internal(VkInstance inst, const char * pName)
1070{
1071 // inst is not wrapped
1072 if (inst == VK_NULL_HANDLE) {
1073 return NULL;
1074 }
1075 VkLayerInstanceDispatchTable* disp_table = * (VkLayerInstanceDispatchTable **) inst;
1076 void *addr;
1077
Jon Ashburn8fd08252015-05-28 16:25:02 -06001078 if (!strcmp(pName, "vkGetInstanceProcAddr"))
1079 return (void *) loader_gpa_instance_internal;
1080
Jon Ashburn27cd5842015-05-12 17:26:48 -06001081 if (disp_table == NULL)
1082 return NULL;
1083
1084 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001085 if (addr) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06001086 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001087 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001088
1089 if (disp_table->GetInstanceProcAddr == NULL) {
1090 return NULL;
1091 }
1092 return disp_table->GetInstanceProcAddr(inst, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001093}
1094
Jon Ashburn128f9422015-05-28 19:16:58 -06001095struct loader_icd * loader_get_icd(const VkPhysicalDevice gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -06001096{
Jon Ashburn128f9422015-05-28 19:16:58 -06001097
Jon Ashburn98bd4542015-01-29 16:44:24 -07001098 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
1099 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
1100 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn128f9422015-05-28 19:16:58 -06001101 if (icd->gpus[i] == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -07001102 *gpu_index = i;
1103 return icd;
1104 }
1105 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -06001106 }
1107 return NULL;
1108}
1109
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001110static loader_platform_dl_handle loader_add_layer_lib(
Jon Ashburn4f67d742015-05-27 13:19:22 -06001111 const char *chain_type,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001112 struct loader_extension_property *ext_prop)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001113{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001114 struct loader_lib_info *new_layer_lib_list, *my_lib;
1115
1116 /* Only loader layer libraries here */
1117 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1118 return NULL;
1119 }
1120
1121 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
1122 if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) {
1123 /* Have already loaded this library, just increment ref count */
1124 loader.loaded_layer_lib_list[i].ref_count++;
Courtney Goeltzenleuchterca8c81a2015-06-14 11:59:07 -06001125 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001126 "%s Chain: Increment layer reference count for layer library %s",
1127 chain_type, ext_prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001128 return loader.loaded_layer_lib_list[i].lib_handle;
1129 }
1130 }
1131
1132 /* Haven't seen this library so load it */
1133 new_layer_lib_list = realloc(loader.loaded_layer_lib_list,
1134 (loader.loaded_layer_lib_count + 1) * sizeof(struct loader_lib_info));
1135 if (!new_layer_lib_list) {
1136 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed");
1137 return NULL;
1138 }
1139
1140 my_lib = &new_layer_lib_list[loader.loaded_layer_lib_count];
1141
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001142 /* NOTE: We require that the extension property be immutable */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001143 my_lib->lib_name = ext_prop->lib_name;
1144 my_lib->ref_count = 0;
1145 my_lib->lib_handle = NULL;
1146
1147 if ((my_lib->lib_handle = loader_platform_open_library(my_lib->lib_name)) == NULL) {
1148 loader_log(VK_DBG_REPORT_ERROR_BIT, 0,
1149 loader_platform_open_library_error(my_lib->lib_name));
1150 return NULL;
1151 } else {
Courtney Goeltzenleuchterca8c81a2015-06-14 11:59:07 -06001152 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001153 "Chain: %s: Loading layer library %s",
1154 chain_type, ext_prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001155 }
1156 loader.loaded_layer_lib_count++;
1157 loader.loaded_layer_lib_list = new_layer_lib_list;
1158 my_lib->ref_count++;
1159
1160 return my_lib->lib_handle;
1161}
1162
1163static void loader_remove_layer_lib(
1164 struct loader_instance *inst,
1165 struct loader_extension_property *ext_prop)
1166{
1167 uint32_t idx;
1168 struct loader_lib_info *new_layer_lib_list, *my_lib;
1169
1170 /* Only loader layer libraries here */
1171 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001172 return;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001173 }
Jon Ashburndf7d5842014-10-16 15:48:50 -06001174
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001175 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
1176 if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) {
1177 /* found matching library */
1178 idx = i;
1179 my_lib = &loader.loaded_layer_lib_list[i];
1180 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001181 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001182 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001183
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001184 my_lib->ref_count--;
1185 inst->layer_count--;
1186 if (my_lib->ref_count > 0) {
Courtney Goeltzenleuchterca8c81a2015-06-14 11:59:07 -06001187 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001188 "Decrement reference count for layer library %s", ext_prop->lib_name);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001189 return;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001190 }
Jon Ashburn19c25022015-04-14 14:14:48 -06001191
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001192 loader_platform_close_library(my_lib->lib_handle);
Courtney Goeltzenleuchterca8c81a2015-06-14 11:59:07 -06001193 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001194 "Unloading layer library %s", ext_prop->lib_name);
1195
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001196 /* Need to remove unused library from list */
1197 new_layer_lib_list = malloc((loader.loaded_layer_lib_count - 1) * sizeof(struct loader_lib_info));
1198 if (!new_layer_lib_list) {
1199 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed");
1200 return;
1201 }
1202
1203 if (idx > 0) {
1204 /* Copy records before idx */
1205 memcpy(new_layer_lib_list, &loader.loaded_layer_lib_list[0],
1206 sizeof(struct loader_lib_info) * idx);
1207 }
1208 if (idx < (loader.loaded_layer_lib_count - 1)) {
1209 /* Copy records after idx */
1210 memcpy(&new_layer_lib_list[idx], &loader.loaded_layer_lib_list[idx+1],
1211 sizeof(struct loader_lib_info) * (loader.loaded_layer_lib_count - idx - 1));
1212 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001213
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001214 free(loader.loaded_layer_lib_list);
1215 loader.loaded_layer_lib_count--;
1216 loader.loaded_layer_lib_list = new_layer_lib_list;
Jon Ashburnb8358052014-11-18 09:06:04 -07001217}
1218
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001219static void loader_add_layer_env(
1220 struct loader_extension_list *ext_list,
1221 const struct loader_extension_list *search_list)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001222{
Ian Elliott4470a302015-02-17 10:33:47 -07001223 char *layerEnv;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001224 uint32_t len;
Jon Ashburnd09bd102014-10-22 21:15:26 -06001225 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001226
Ian Elliott4470a302015-02-17 10:33:47 -07001227#if defined(WIN32)
1228 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
1229 LAYER_NAMES_REGISTRY_VALUE);
1230#else // WIN32
1231 layerEnv = getenv(LAYER_NAMES_ENV);
1232#endif // WIN32
1233 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001234 return;
Ian Elliott4470a302015-02-17 10:33:47 -07001235 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001236 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -07001237 if (p == NULL) {
1238#if defined(WIN32)
1239 free(layerEnv);
1240#endif // WIN32
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001241 return;
Ian Elliott4470a302015-02-17 10:33:47 -07001242 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001243 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -07001244#if defined(WIN32)
1245 free(layerEnv);
1246#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -06001247 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001248
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001249 while (p && *p ) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001250 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001251 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -07001252 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001253 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001254 } else {
Ian Elliott19628802015-02-04 12:06:46 -07001255 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001256 *(char *) next = '\0';
1257 next++;
1258 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001259 name = basename(p);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001260 loader_search_ext_list_for_name(name, search_list, ext_list);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001261 p = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001262 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001263
Jon Ashburnd09bd102014-10-22 21:15:26 -06001264 free(pOrig);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001265 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001266}
1267
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -06001268void loader_deactivate_instance_layers(struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001269{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001270 if (!instance->layer_count) {
1271 return;
1272 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001273
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001274 /* Create instance chain of enabled layers */
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -06001275 for (uint32_t i = 0; i < instance->activated_layer_list.count; i++) {
1276 struct loader_extension_property *ext_prop = &instance->activated_layer_list.list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001277
1278 loader_remove_layer_lib(instance, ext_prop);
1279
1280 instance->layer_count--;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001281 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001282 loader_destroy_ext_list(&instance->activated_layer_list);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001283
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001284}
1285
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001286void loader_enable_instance_layers(struct loader_instance *inst)
1287{
1288 if (inst == NULL)
1289 return;
1290
1291 /* Add any layers specified in the environment first */
1292 loader_add_layer_env(&inst->enabled_instance_extensions, &loader.global_extensions);
1293
1294 /* Add layers / extensions specified by the application */
1295 loader_add_vk_ext_to_ext_list(
1296 &inst->enabled_instance_extensions,
1297 inst->app_extension_count,
1298 inst->app_extension_props,
1299 &loader.global_extensions);
1300}
1301
Jon Ashburn27cd5842015-05-12 17:26:48 -06001302uint32_t loader_activate_instance_layers(struct loader_instance *inst)
1303{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001304 uint32_t layer_idx;
Jon Ashburn128f9422015-05-28 19:16:58 -06001305 VkBaseLayerObject *wrappedInstance;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001306
David Pinedoa0a8a242015-06-24 15:29:18 -06001307 if (inst == NULL) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06001308 return 0;
David Pinedoa0a8a242015-06-24 15:29:18 -06001309 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001310
1311 // NOTE inst is unwrapped at this point in time
1312 VkObject baseObj = (VkObject) inst;
1313 VkObject nextObj = (VkObject) inst;
1314 VkBaseLayerObject *nextInstObj;
1315 PFN_vkGetInstanceProcAddr nextGPA = loader_gpa_instance_internal;
1316
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001317 /*
1318 * Figure out how many actual layers will need to be wrapped.
1319 */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001320 for (uint32_t i = 0; i < inst->enabled_instance_extensions.count; i++) {
1321 struct loader_extension_property *ext_prop = &inst->enabled_instance_extensions.list[i];
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001322 if (ext_prop->alias) {
1323 ext_prop = ext_prop->alias;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001324 }
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001325 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1326 continue;
1327 }
1328 loader_add_to_ext_list(&inst->activated_layer_list, 1, ext_prop);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001329 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001330
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001331 inst->layer_count = inst->activated_layer_list.count;
1332
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001333 if (!inst->layer_count) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06001334 return 0;
1335 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001336
Jon Ashburn128f9422015-05-28 19:16:58 -06001337 wrappedInstance = malloc(sizeof(VkBaseLayerObject)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001338 * inst->layer_count);
Jon Ashburn128f9422015-05-28 19:16:58 -06001339 if (!wrappedInstance) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001340 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Instance objects for layer");
1341 return 0;
1342 }
1343
1344 /* Create instance chain of enabled layers */
1345 layer_idx = inst->layer_count - 1;
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001346 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
1347 struct loader_extension_property *ext_prop = &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001348 loader_platform_dl_handle lib_handle;
1349
1350 /*
Jon Ashburn922c8f62015-06-18 09:05:37 -06001351 * For global exenstions implemented within the loader (i.e. DEBUG_REPORT
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001352 * the extension must provide two entry points for the loader to use:
1353 * - "trampoline" entry point - this is the address returned by GetProcAddr
1354 * and will always do what's necessary to support a global call.
1355 * - "terminator" function - this function will be put at the end of the
1356 * instance chain and will contain the necessary logica to call / process
1357 * the extension for the appropriate ICDs that are available.
1358 * There is no generic mechanism for including these functions, the references
1359 * must be placed into the appropriate loader entry points.
1360 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for GetProcAddr requests
1361 * loader_coalesce_extensions(void) - add extension records to the list of global
1362 * extension available to the app.
1363 * instance_disp - add function pointer for terminator function to this array.
1364 * The extension itself should be in a separate file that will be
1365 * linked directly with the loader.
1366 * Note: An extension's Get*ProcAddr should not return a function pointer for
1367 * any extension entry points until the extension has been enabled.
1368 * To do this requires a different behavior from Get*ProcAddr functions implemented
1369 * in layers.
1370 * The very first call to a layer will be it's Get*ProcAddr function requesting
1371 * the layer's vkGet*ProcAddr. The layer should intialize it's internal dispatch table
1372 * with the wrapped object given (either Instance or Device) and return the layer's
1373 * Get*ProcAddr function. The layer should also use this opportunity to record the
1374 * baseObject so that it can find the correct local dispatch table on future calls.
1375 * Subsequent calls to Get*ProcAddr, CreateInstance, CreateDevice
1376 * will not use a wrapped object and must look up their local dispatch table from
1377 * the given baseObject.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001378 */
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001379 assert(ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001380
Jon Ashburn128f9422015-05-28 19:16:58 -06001381 nextInstObj = (wrappedInstance + layer_idx);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001382 nextInstObj->pGPA = nextGPA;
1383 nextInstObj->baseObject = baseObj;
1384 nextInstObj->nextObject = nextObj;
1385 nextObj = (VkObject) nextInstObj;
1386
1387 char funcStr[256];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001388 snprintf(funcStr, 256, "%sGetInstanceProcAddr", ext_prop->info.name);
Jon Ashburn4f67d742015-05-27 13:19:22 -06001389 lib_handle = loader_add_layer_lib("instance", ext_prop);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001390 if ((nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
1391 nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Jon Ashburn27cd5842015-05-12 17:26:48 -06001392 if (!nextGPA) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001393 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetInstanceProcAddr in layer %s", ext_prop->lib_name);
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06001394
1395 /* TODO: Should we return nextObj, nextGPA to previous? */
Jon Ashburn27cd5842015-05-12 17:26:48 -06001396 continue;
1397 }
1398
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001399 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1400 "Insert instance layer library %s for extension: %s",
1401 ext_prop->lib_name, ext_prop->info.name);
1402
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001403 layer_idx--;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001404 }
1405
Jon Ashburn8fd08252015-05-28 16:25:02 -06001406 loader_init_instance_core_dispatch_table(inst->disp, nextGPA, (VkInstance) nextObj, (VkInstance) baseObj);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001407
Jon Ashburn128f9422015-05-28 19:16:58 -06001408 free(wrappedInstance);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001409 return inst->layer_count;
1410}
1411
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001412void loader_activate_instance_layer_extensions(struct loader_instance *inst)
1413{
1414
1415 loader_init_instance_extension_dispatch_table(inst->disp,
1416 inst->disp->GetInstanceProcAddr,
Jon Ashburn128f9422015-05-28 19:16:58 -06001417 (VkInstance) inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001418}
1419
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001420static void loader_enable_device_layers(struct loader_device *dev)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001421{
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001422 if (dev == NULL)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001423 return;
1424
1425 /* Add any layers specified in the environment first */
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001426 loader_add_layer_env(&dev->enabled_device_extensions, &loader.global_extensions);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001427
1428 /* Add layers / extensions specified by the application */
1429 loader_add_vk_ext_to_ext_list(
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001430 &dev->enabled_device_extensions,
1431 dev->app_extension_count,
1432 dev->app_extension_props,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001433 &loader.global_extensions);
1434}
1435
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001436static uint32_t loader_activate_device_layers(
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001437 VkDevice device,
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001438 struct loader_device *dev,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001439 struct loader_icd *icd,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001440 uint32_t ext_count,
1441 const VkExtensionProperties *ext_props)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001442{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001443 if (!icd)
1444 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001445
David Pinedoa0a8a242015-06-24 15:29:18 -06001446 if (!dev) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001447 return 0;
David Pinedoa0a8a242015-06-24 15:29:18 -06001448 }
Jon Ashburn94e70492015-06-10 10:13:10 -06001449
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001450 /* activate any layer libraries */
Jon Ashburn94e70492015-06-10 10:13:10 -06001451 VkObject nextObj = (VkObject) device;
1452 VkObject baseObj = nextObj;
1453 VkBaseLayerObject *nextGpuObj;
1454 PFN_vkGetDeviceProcAddr nextGPA = icd->GetDeviceProcAddr;
1455 VkBaseLayerObject *wrappedGpus;
1456 /*
1457 * Figure out how many actual layers will need to be wrapped.
1458 */
1459 for (uint32_t i = 0; i < dev->enabled_device_extensions.count; i++) {
1460 struct loader_extension_property *ext_prop = &dev->enabled_device_extensions.list[i];
1461 if (ext_prop->alias) {
1462 ext_prop = ext_prop->alias;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001463 }
Jon Ashburn94e70492015-06-10 10:13:10 -06001464 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1465 continue;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001466 }
Jon Ashburn94e70492015-06-10 10:13:10 -06001467 loader_add_to_ext_list(&dev->activated_layer_list, 1, ext_prop);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001468 }
Jon Ashburn94e70492015-06-10 10:13:10 -06001469
1470 if (!dev->activated_layer_list.count)
1471 return 0;
1472
1473 wrappedGpus = malloc(sizeof (VkBaseLayerObject) * dev->activated_layer_list.count);
1474 if (!wrappedGpus) {
1475 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Gpu objects for layer");
1476 return 0;
1477 }
1478 for (int32_t i = dev->activated_layer_list.count - 1; i >= 0; i--) {
1479
1480 struct loader_extension_property *ext_prop = &dev->activated_layer_list.list[i];
1481 loader_platform_dl_handle lib_handle;
1482
1483 assert(ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER);
1484
1485 nextGpuObj = (wrappedGpus + i);
1486 nextGpuObj->pGPA = nextGPA;
1487 nextGpuObj->baseObject = baseObj;
1488 nextGpuObj->nextObject = nextObj;
1489 nextObj = (VkObject) nextGpuObj;
1490
1491 char funcStr[256];
1492 snprintf(funcStr, 256, "%sGetDeviceProcAddr", ext_prop->info.name);
1493 lib_handle = loader_add_layer_lib("device", ext_prop);
1494 if ((nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
1495 nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
1496 if (!nextGPA) {
1497 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetDeviceProcAddr in layer %s", ext_prop->info.name);
1498 continue;
1499 }
1500
1501 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1502 "Insert device layer library %s for extension: %s",
1503 ext_prop->lib_name, ext_prop->info.name);
1504
1505 }
1506
1507 loader_init_device_dispatch_table(&dev->loader_dispatch, nextGPA,
1508 (VkPhysicalDevice) nextObj, (VkPhysicalDevice) baseObj);
1509 free(wrappedGpus);
1510
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001511 return dev->activated_layer_list.count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001512}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001513
Jon Ashburn27cd5842015-05-12 17:26:48 -06001514VkResult loader_CreateInstance(
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001515 const VkInstanceCreateInfo* pCreateInfo,
1516 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001517{
Jon Ashburneed0c002015-05-21 17:42:17 -06001518 struct loader_instance *ptr_instance = *(struct loader_instance **) pInstance;
Jon Ashburn46888392015-01-29 15:45:51 -07001519 struct loader_scanned_icds *scanned_icds;
1520 struct loader_icd *icd;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001521 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001522
Jon Ashburn46888392015-01-29 15:45:51 -07001523 scanned_icds = loader.scanned_icd_list;
1524 while (scanned_icds) {
1525 icd = loader_icd_add(ptr_instance, scanned_icds);
1526 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001527 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn3da71f22015-05-14 12:43:38 -06001528 &(icd->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001529 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001530 {
1531 ptr_instance->icds = ptr_instance->icds->next;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001532 loader_icd_destroy(ptr_instance, icd);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001533 icd->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001534 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001535 "ICD ignored: failed to CreateInstance on device");
Jon Ashburn3da71f22015-05-14 12:43:38 -06001536 } else
1537 {
1538 loader_icd_init_entrys(icd, scanned_icds);
Jon Ashburn46888392015-01-29 15:45:51 -07001539 }
1540 }
1541 scanned_icds = scanned_icds->next;
1542 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001543
Ian Elliotteb450762015-02-05 15:19:15 -07001544 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001545 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001546 }
Jon Ashburn46888392015-01-29 15:45:51 -07001547
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001548 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001549}
1550
Jon Ashburn27cd5842015-05-12 17:26:48 -06001551VkResult loader_DestroyInstance(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001552 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001553{
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06001554 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001555 struct loader_icd *icds = ptr_instance->icds;
Jon Ashburna6fd2612015-06-16 14:43:19 -06001556 struct loader_icd *next_icd;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001557 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001558
1559 // Remove this instance from the list of instances:
1560 struct loader_instance *prev = NULL;
1561 struct loader_instance *next = loader.instances;
1562 while (next != NULL) {
1563 if (next == ptr_instance) {
1564 // Remove this instance from the list:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001565 free(ptr_instance->app_extension_props);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001566 if (prev)
1567 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001568 else
1569 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001570 break;
1571 }
1572 prev = next;
1573 next = next->next;
1574 }
1575 if (next == NULL) {
1576 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001577 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001578 }
1579
Jon Ashburn3da71f22015-05-14 12:43:38 -06001580 while (icds) {
1581 if (icds->instance) {
1582 res = icds->DestroyInstance(icds->instance);
Tony Barbourf20f87b2015-04-22 09:02:32 -06001583 if (res != VK_SUCCESS)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001584 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
Tony Barbourf20f87b2015-04-22 09:02:32 -06001585 "ICD ignored: failed to DestroyInstance on device");
1586 }
Jon Ashburna6fd2612015-06-16 14:43:19 -06001587 next_icd = icds->next;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001588 icds->instance = VK_NULL_HANDLE;
Jon Ashburna6fd2612015-06-16 14:43:19 -06001589 loader_icd_destroy(ptr_instance, icds);
1590
1591 icds = next_icd;
Jon Ashburn46888392015-01-29 15:45:51 -07001592 }
1593
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001594
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001595 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001596}
1597
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001598VkResult loader_init_physical_device_info(
1599 struct loader_instance *ptr_instance)
1600{
1601 struct loader_icd *icd;
1602 uint32_t n, count = 0;
1603 VkResult res = VK_ERROR_UNKNOWN;
1604
1605 icd = ptr_instance->icds;
1606 while (icd) {
1607 res = icd->EnumeratePhysicalDevices(icd->instance, &n, NULL);
1608 if (res != VK_SUCCESS)
1609 return res;
1610 icd->gpu_count = n;
1611 count += n;
1612 icd = icd->next;
1613 }
1614
1615 ptr_instance->total_gpu_count = count;
1616
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001617 icd = ptr_instance->icds;
1618 while (icd) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001619
1620 n = icd->gpu_count;
Jon Ashburn128f9422015-05-28 19:16:58 -06001621 icd->gpus = (VkPhysicalDevice *) malloc(n * sizeof(VkPhysicalDevice));
1622 if (!icd->gpus) {
1623 /* TODO: Add cleanup code here */
1624 return VK_ERROR_OUT_OF_HOST_MEMORY;
1625 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001626 res = icd->EnumeratePhysicalDevices(
1627 icd->instance,
1628 &n,
Jon Ashburn128f9422015-05-28 19:16:58 -06001629 icd->gpus);
1630 if ((res == VK_SUCCESS) && (n == icd->gpu_count)) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001631
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001632 for (unsigned int i = 0; i < n; i++) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001633
Jon Ashburn128f9422015-05-28 19:16:58 -06001634 loader_init_dispatch(icd->gpus[i], ptr_instance->disp);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001635
1636 if (!loader_init_ext_list(&icd->device_extension_cache[i])) {
1637 /* TODO: Add cleanup code here */
1638 res = VK_ERROR_OUT_OF_HOST_MEMORY;
1639 }
Tony Barbour59a47322015-06-24 16:06:58 -06001640 if (res == VK_SUCCESS && icd->GetPhysicalDeviceExtensionProperties && icd->GetPhysicalDeviceExtensionCount) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001641 uint32_t extension_count;
1642
Tony Barbour59a47322015-06-24 16:06:58 -06001643 res = icd->GetPhysicalDeviceExtensionCount(icd->gpus[i], &extension_count);
1644 if (res == VK_SUCCESS) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001645 struct loader_extension_property ext_props;
1646
1647 /* Gather all the ICD extensions */
1648 for (uint32_t extension_id = 0; extension_id < extension_count; extension_id++) {
Tony Barbour59a47322015-06-24 16:06:58 -06001649 res = icd->GetPhysicalDeviceExtensionProperties(icd->gpus[i],
1650 extension_id, &ext_props.info);
1651 if (res == VK_SUCCESS) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001652 ext_props.origin = VK_EXTENSION_ORIGIN_ICD;
1653 ext_props.lib_name = icd->scanned_icds->lib_name;
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001654 ext_props.get_proc_addr = icd->GetDeviceProcAddr;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001655 loader_add_to_ext_list(&icd->device_extension_cache[i], 1, &ext_props);
1656 }
1657 }
1658
1659 // Traverse layers list adding non-duplicate extensions to the list
1660 for (uint32_t l = 0; l < loader.scanned_layer_count; l++) {
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001661 loader_get_physical_device_layer_extensions(ptr_instance, icd->gpus[i], l, &icd->device_extension_cache[i]);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001662 }
1663 }
1664 }
1665
1666 if (res != VK_SUCCESS) {
1667 /* clean up any extension lists previously created before this request failed */
1668 for (uint32_t j = 0; j < i; j++) {
1669 loader_destroy_ext_list(&icd->device_extension_cache[i]);
1670 }
1671 return res;
1672 }
1673 }
1674
1675 count += n;
1676 }
1677
1678 icd = icd->next;
1679 }
1680
1681 return VK_SUCCESS;
1682}
1683
Jon Ashburn27cd5842015-05-12 17:26:48 -06001684VkResult loader_EnumeratePhysicalDevices(
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001685 VkInstance instance,
1686 uint32_t* pPhysicalDeviceCount,
1687 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001688{
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001689 uint32_t index = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001690 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001691 struct loader_icd *icd = ptr_instance->icds;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001692
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001693 if (ptr_instance->total_gpu_count == 0) {
1694 loader_init_physical_device_info(ptr_instance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001695 }
1696
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001697 *pPhysicalDeviceCount = ptr_instance->total_gpu_count;
1698 if (!pPhysicalDevices) {
1699 return VK_SUCCESS;
1700 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001701
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001702 while (icd) {
1703 assert((index + icd->gpu_count) <= *pPhysicalDeviceCount);
Jon Ashburn128f9422015-05-28 19:16:58 -06001704 memcpy(&pPhysicalDevices[index], icd->gpus, icd->gpu_count * sizeof(VkPhysicalDevice));
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001705 index += icd->gpu_count;
1706 icd = icd->next;
1707 }
1708
1709 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001710}
1711
Tony Barbour59a47322015-06-24 16:06:58 -06001712VkResult loader_GetPhysicalDeviceProperties(
Jon Ashburn3da71f22015-05-14 12:43:38 -06001713 VkPhysicalDevice gpu,
Tony Barbour59a47322015-06-24 16:06:58 -06001714 VkPhysicalDeviceProperties* pProperties)
Jon Ashburn3da71f22015-05-14 12:43:38 -06001715{
1716 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06001717 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001718 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1719
Tony Barbour59a47322015-06-24 16:06:58 -06001720 if (icd->GetPhysicalDeviceProperties)
1721 res = icd->GetPhysicalDeviceProperties(gpu, pProperties);
1722
1723 return res;
1724}
1725
1726VkResult loader_GetPhysicalDevicePerformance(
1727 VkPhysicalDevice gpu,
1728 VkPhysicalDevicePerformance* pPerformance)
1729{
1730 uint32_t gpu_index;
1731 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
1732 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1733
1734 if (icd->GetPhysicalDevicePerformance)
1735 res = icd->GetPhysicalDevicePerformance(gpu, pPerformance);
1736
1737 return res;
1738}
1739
1740VkResult loader_GetPhysicalDeviceQueueCount(
1741 VkPhysicalDevice gpu,
1742 uint32_t* pCount)
1743{
1744 uint32_t gpu_index;
1745 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
1746 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1747
1748 if (icd->GetPhysicalDeviceQueueCount)
1749 res = icd->GetPhysicalDeviceQueueCount(gpu, pCount);
1750
1751 return res;
1752}
1753
1754VkResult loader_GetPhysicalDeviceQueueProperties (
1755 VkPhysicalDevice gpu,
1756 uint32_t count,
1757 VkPhysicalDeviceQueueProperties * pProperties)
1758{
1759 uint32_t gpu_index;
1760 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
1761 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1762
1763 if (icd->GetPhysicalDeviceQueueProperties)
1764 res = icd->GetPhysicalDeviceQueueProperties(gpu, count, pProperties);
1765
1766 return res;
1767}
1768
1769VkResult loader_GetPhysicalDeviceMemoryProperties (
1770 VkPhysicalDevice gpu,
1771 VkPhysicalDeviceMemoryProperties* pProperties)
1772{
1773 uint32_t gpu_index;
1774 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
1775 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1776
1777 if (icd->GetPhysicalDeviceMemoryProperties)
1778 res = icd->GetPhysicalDeviceMemoryProperties(gpu, pProperties);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001779
1780 return res;
1781}
1782
Chris Forbesbc0bb772015-06-21 22:55:02 +12001783VkResult loader_GetPhysicalDeviceFeatures(
1784 VkPhysicalDevice physicalDevice,
1785 VkPhysicalDeviceFeatures* pFeatures)
1786{
1787 uint32_t gpu_index;
1788 struct loader_icd *icd = loader_get_icd(physicalDevice, &gpu_index);
1789 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1790
1791 if (icd->GetPhysicalDeviceFeatures)
1792 res = icd->GetPhysicalDeviceFeatures(physicalDevice, pFeatures);
1793
1794 return res;
1795}
1796
1797VkResult loader_GetPhysicalDeviceFormatInfo(
1798 VkPhysicalDevice physicalDevice,
1799 VkFormat format,
1800 VkFormatProperties* pFormatInfo)
1801{
1802 uint32_t gpu_index;
1803 struct loader_icd *icd = loader_get_icd(physicalDevice, &gpu_index);
1804 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1805
1806 if (icd->GetPhysicalDeviceFormatInfo)
1807 res = icd->GetPhysicalDeviceFormatInfo(physicalDevice, format, pFormatInfo);
1808
1809 return res;
1810}
1811
1812VkResult loader_GetPhysicalDeviceLimits(
1813 VkPhysicalDevice physicalDevice,
1814 VkPhysicalDeviceLimits* pLimits)
1815{
1816 uint32_t gpu_index;
1817 struct loader_icd *icd = loader_get_icd(physicalDevice, &gpu_index);
1818 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1819
1820 if (icd->GetPhysicalDeviceLimits)
1821 res = icd->GetPhysicalDeviceLimits(physicalDevice, pLimits);
1822
1823 return res;
1824}
1825
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001826VkResult loader_CreateDevice(
1827 VkPhysicalDevice gpu,
1828 const VkDeviceCreateInfo* pCreateInfo,
1829 VkDevice* pDevice)
1830{
1831 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06001832 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001833 struct loader_device *dev;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001834 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001835
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001836 if (icd->CreateDevice) {
1837 res = icd->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001838 if (res != VK_SUCCESS) {
1839 return res;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001840 }
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001841 dev = loader_add_logical_device(*pDevice, &icd->logical_device_list);
David Pinedoa0a8a242015-06-24 15:29:18 -06001842 if (dev == NULL) {
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001843 return VK_ERROR_OUT_OF_HOST_MEMORY;
David Pinedoa0a8a242015-06-24 15:29:18 -06001844 }
Jon Ashburnd6a5b672015-06-08 16:38:48 -06001845 PFN_vkGetDeviceProcAddr get_proc_addr = icd->GetDeviceProcAddr;
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001846 loader_init_device_dispatch_table(&dev->loader_dispatch, get_proc_addr,
1847 icd->gpus[gpu_index], icd->gpus[gpu_index]);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001848
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001849 loader_init_dispatch(*pDevice, &dev->loader_dispatch);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001850
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001851 dev->app_extension_count = pCreateInfo->extensionCount;
1852 dev->app_extension_props = (VkExtensionProperties *) malloc(sizeof(VkExtensionProperties) * pCreateInfo->extensionCount);
1853 if (dev->app_extension_props == NULL && (dev->app_extension_count > 0)) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001854 return VK_ERROR_OUT_OF_HOST_MEMORY;
1855 }
1856
1857 /* Make local copy of extension list */
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001858 if (dev->app_extension_count > 0 && dev->app_extension_props != NULL) {
1859 memcpy(dev->app_extension_props, pCreateInfo->pEnabledExtensions, sizeof(VkExtensionProperties) * pCreateInfo->extensionCount);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001860 }
1861
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001862 /*
1863 * Put together the complete list of extensions to enable
1864 * This includes extensions requested via environment variables.
1865 */
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001866 loader_enable_device_layers(dev);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001867
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001868 /*
1869 * Load the libraries needed by the extensions on the
Jon Ashburn953bb3c2015-06-10 16:11:42 -06001870 * enabled extension list. This will build the device chain
1871 * terminating with the selected device.
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001872 */
Jon Ashburndc6fcad2015-06-10 10:06:06 -06001873 loader_activate_device_layers(*pDevice, dev, icd,
1874 dev->app_extension_count,
1875 dev->app_extension_props);
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001876 }
1877
1878 return res;
1879}
1880
Courtney Goeltzenleuchter9ec39ac2015-06-22 17:45:21 -06001881static void * VKAPI loader_GetInstanceProcAddr(VkInstance instance, const char * pName)
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001882{
Jon Ashburn07daee72015-05-21 18:13:33 -06001883 if (instance == VK_NULL_HANDLE)
1884 return NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001885
Jon Ashburn07daee72015-05-21 18:13:33 -06001886 void *addr;
1887 /* get entrypoint addresses that are global (in the loader)*/
1888 addr = globalGetProcAddr(pName);
1889 if (addr)
1890 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001891
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001892 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1893
Jon Ashburn922c8f62015-06-18 09:05:37 -06001894 /* return any extension global entrypoints */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001895 addr = debug_report_instance_gpa(ptr_instance, pName);
1896 if (addr) {
1897 return addr;
1898 }
1899
Jon Ashburn922c8f62015-06-18 09:05:37 -06001900 /* TODO Remove this once WSI has no loader special code */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001901 addr = wsi_lunarg_GetInstanceProcAddr(instance, pName);
David Pinedoa0a8a242015-06-24 15:29:18 -06001902 if (addr) {
Jon Ashburn922c8f62015-06-18 09:05:37 -06001903 return addr;
David Pinedoa0a8a242015-06-24 15:29:18 -06001904 }
Jon Ashburn07daee72015-05-21 18:13:33 -06001905
1906 /* return the instance dispatch table entrypoint for extensions */
1907 const VkLayerInstanceDispatchTable *disp_table = * (VkLayerInstanceDispatchTable **) instance;
1908 if (disp_table == NULL)
1909 return NULL;
1910
1911 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
1912 if (addr)
1913 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001914
1915 return NULL;
1916}
1917
Courtney Goeltzenleuchter9ec39ac2015-06-22 17:45:21 -06001918LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName)
1919{
1920 return loader_GetInstanceProcAddr(instance, pName);
1921}
1922
1923static void * VKAPI loader_GetDeviceProcAddr(VkDevice device, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001924{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001925 if (device == VK_NULL_HANDLE) {
1926 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001927 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001928
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001929 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001930
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001931 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1932 make sure the loader entrypoint is returned */
1933 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001934 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001935 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001936 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001937
Jon Ashburn07daee72015-05-21 18:13:33 -06001938 /* return any extension device entrypoints the loader knows about */
Jon Ashburn922c8f62015-06-18 09:05:37 -06001939 /* TODO once WSI has no loader special code remove this */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001940 addr = wsi_lunarg_GetDeviceProcAddr(device, pName);
David Pinedoa0a8a242015-06-24 15:29:18 -06001941 if (addr) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001942 return addr;
David Pinedoa0a8a242015-06-24 15:29:18 -06001943 }
Jon Ashburn07daee72015-05-21 18:13:33 -06001944
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001945 /* return the dispatch table entrypoint for the fastest case */
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001946 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001947 if (disp_table == NULL)
1948 return NULL;
1949
Jon Ashburn27cd5842015-05-12 17:26:48 -06001950 addr = loader_lookup_device_dispatch_table(disp_table, pName);
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001951 if (addr)
1952 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001953 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001954 if (disp_table->GetDeviceProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001955 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001956 return disp_table->GetDeviceProcAddr(device, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001957 }
1958}
1959
Courtney Goeltzenleuchter9ec39ac2015-06-22 17:45:21 -06001960LOADER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char * pName)
1961{
1962 return loader_GetDeviceProcAddr(device, pName);
1963}
1964
Tony Barbour59a47322015-06-24 16:06:58 -06001965LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionCount(
1966 uint32_t* pCount)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001967{
Tony Barbour59a47322015-06-24 16:06:58 -06001968 /* Scan/discover all ICD libraries in a single-threaded manner */
1969 loader_platform_thread_once(&once_icd, loader_icd_scan);
1970
1971 /* get layer libraries in a single-threaded manner */
1972 loader_platform_thread_once(&once_layer, layer_lib_scan);
1973
1974 /* merge any duplicate extensions */
1975 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1976
1977 loader_platform_thread_lock_mutex(&loader_lock);
1978 *pCount = loader.global_extensions.count;
1979 loader_platform_thread_unlock_mutex(&loader_lock);
1980 return VK_SUCCESS;
1981}
1982
1983LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
1984 uint32_t extensionIndex,
1985 VkExtensionProperties* pProperties)
1986{
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001987
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001988 /* Scan/discover all ICD libraries in a single-threaded manner */
1989 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001990
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001991 /* get layer libraries in a single-threaded manner */
1992 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001993
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001994 /* merge any duplicate extensions */
1995 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1996
Tony Barbour59a47322015-06-24 16:06:58 -06001997 if (extensionIndex >= loader.global_extensions.count)
1998 return VK_ERROR_INVALID_VALUE;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001999
Jon Ashburn6301a0f2015-05-29 13:15:39 -06002000 loader_platform_thread_lock_mutex(&loader_lock);
Tony Barbour59a47322015-06-24 16:06:58 -06002001 memcpy(pProperties,
2002 &loader.global_extensions.list[extensionIndex],
2003 sizeof(VkExtensionProperties));
2004
Jon Ashburn6301a0f2015-05-29 13:15:39 -06002005 loader_platform_thread_unlock_mutex(&loader_lock);
Tony Barbour59a47322015-06-24 16:06:58 -06002006 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07002007}
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06002008
Tony Barbour59a47322015-06-24 16:06:58 -06002009VkResult loader_GetPhysicalDeviceExtensionCount(
Jon Ashburn95a77ba2015-05-15 15:09:35 -06002010 VkPhysicalDevice gpu,
Tony Barbour59a47322015-06-24 16:06:58 -06002011 uint32_t* pCount)
Jon Ashburn95a77ba2015-05-15 15:09:35 -06002012{
2013 uint32_t gpu_index;
Tony Barbour59a47322015-06-24 16:06:58 -06002014 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
2015 *pCount = icd->device_extension_cache[gpu_index].count;
2016
2017 return VK_SUCCESS;
2018}
2019
2020VkResult loader_GetPhysicalDeviceExtensionProperties(
2021 VkPhysicalDevice gpu,
2022 uint32_t extensionIndex,
2023 VkExtensionProperties* pProperties)
2024{
2025 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06002026 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07002027
Tony Barbour59a47322015-06-24 16:06:58 -06002028 if (extensionIndex >= icd->device_extension_cache[gpu_index].count)
2029 return VK_ERROR_INVALID_VALUE;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06002030
Tony Barbour59a47322015-06-24 16:06:58 -06002031 memcpy( pProperties,
2032 &icd->device_extension_cache[gpu_index].list[extensionIndex],
2033 sizeof(VkExtensionProperties));
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06002034
2035 return VK_SUCCESS;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06002036}